mermaid-cli 0.30.0

Open-source AI pair programmer with agentic capabilities. Local-first with Ollama, native tool calling, and beautiful TUI.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Config keys Mermaid once read and no longer does, and the cleanup that
//! deletes them from the user's file.
//!
//! An unknown key is normally a spelling error, so the loader's warning says
//! "check for a typo". A key on this list is not one: it is a section that an
//! older Mermaid read (and that `mermaid init` used to write out in full), so
//! the warning instead names the release that dropped it and how to get rid of
//! it. `mermaid clean-config`, and the y/N offer at TUI startup, delete exactly
//! these keys from the user file — a backup first, comments, order and every
//! other key kept — and never touch a key that is merely unrecognized.

use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use toml_edit::{DocumentMut, Item};

/// A dotted config path and the release that stopped reading it. A path
/// covers everything beneath it: `plan` also covers `[plan.permissions]`.
pub(crate) struct RemovedKey {
    pub(crate) path: &'static str,
    pub(crate) version: &'static str,
}

/// Every config key a released Mermaid read and the current one ignores,
/// found by walking the `Config` schema at each release tag. Renamed keys
/// that migrate on load (`model_profiles`) are not here — they still work.
pub(crate) const REMOVED_KEYS: &[RemovedKey] = &[
    RemovedKey {
        path: "plan",
        version: "0.28.0",
    },
    RemovedKey {
        path: "compaction.tool_output_max_chars",
        version: "0.28.0",
    },
    RemovedKey {
        path: "computer_use",
        version: "0.26.0",
    },
    RemovedKey {
        path: "non_interactive",
        version: "0.26.0",
    },
    RemovedKey {
        path: "ollama.cloud_api_key",
        version: "0.12.0",
    },
    RemovedKey {
        path: "anthropic",
        version: "0.5.0",
    },
    RemovedKey {
        path: "openai",
        version: "0.5.0",
    },
    RemovedKey {
        path: "mode",
        version: "0.5.0",
    },
    RemovedKey {
        path: "behavior",
        version: "0.5.0",
    },
    RemovedKey {
        path: "default_model.system_prompt",
        version: "0.5.0",
    },
    RemovedKey {
        path: "context",
        version: "0.4.1",
    },
    RemovedKey {
        path: "ui.show_line_numbers",
        version: "0.4.1",
    },
    RemovedKey {
        path: "ui.show_sidebar",
        version: "0.4.1",
    },
    RemovedKey {
        path: "ui.syntax_theme",
        version: "0.4.1",
    },
    RemovedKey {
        path: "litellm",
        version: "0.3.0",
    },
];

/// The removed key an ignored config path falls under, if any. `path` is a
/// dotted path as `serde_ignored` reports it, relative to one layer's table.
pub(crate) fn removed_key_for(path: &str) -> Option<&'static RemovedKey> {
    REMOVED_KEYS.iter().find(|key| {
        path == key.path
            || path
                .strip_prefix(key.path)
                .is_some_and(|rest| rest.starts_with('.'))
    })
}

/// One removed key found in the user file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FoundKey {
    /// Where it sits in the file: `plan`, or `profiles.ci.plan` inside a
    /// `--profile` overlay.
    pub path: String,
    /// The release that stopped reading it.
    pub version: &'static str,
}

/// What a cleanup did.
#[derive(Debug)]
pub struct Cleanup {
    /// The config file that was checked.
    pub config_path: PathBuf,
    /// The keys deleted (empty: nothing to do, and nothing was written).
    pub removed: Vec<FoundKey>,
    /// Where the untouched original was copied, when anything was deleted.
    pub backup: Option<PathBuf>,
}

/// Delete every removed key from `doc` — at the top level and inside each
/// `[profiles.<name>]` overlay — and report what went.
fn strip_removed_keys(doc: &mut DocumentMut) -> Vec<FoundKey> {
    let mut found = strip_from(doc.as_table_mut(), "");
    if let Some(profiles) = doc.get_mut("profiles").and_then(Item::as_table_like_mut) {
        for (name, overlay) in profiles.iter_mut() {
            if let Some(overlay) = overlay.as_table_like_mut() {
                let prefix = format!("profiles.{}.", name.get());
                found.extend(strip_from(overlay, &prefix));
            }
        }
    }
    found
}

fn strip_from(table: &mut dyn toml_edit::TableLike, prefix: &str) -> Vec<FoundKey> {
    let mut found = Vec::new();
    for key in REMOVED_KEYS {
        let segments: Vec<&str> = key.path.split('.').collect();
        if remove_path(table, &segments) {
            found.push(FoundKey {
                path: format!("{prefix}{}", key.path),
                version: key.version,
            });
        }
    }
    found
}

/// Remove the item at `segments` below `table`. Parents are never created and
/// are left in place when they empty out — an empty `[ui]` is harmless.
fn remove_path(table: &mut dyn toml_edit::TableLike, segments: &[&str]) -> bool {
    match segments {
        [] => false,
        [last] => table.remove(last).is_some(),
        [first, rest @ ..] => table
            .get_mut(first)
            .and_then(Item::as_table_like_mut)
            .is_some_and(|child| remove_path(child, rest)),
    }
}

/// The removed keys in the user config file, without changing it. An absent
/// file has none.
///
/// # Errors
///
/// Reading the file, or a file that is not valid TOML.
pub fn find_removed_config_keys(config_path: &Path) -> Result<Vec<FoundKey>> {
    if !config_path.exists() {
        return Ok(Vec::new());
    }
    let mut doc = read_document(config_path)?;
    Ok(strip_removed_keys(&mut doc))
}

fn read_document(config_path: &Path) -> Result<DocumentMut> {
    let raw = std::fs::read_to_string(config_path)
        .with_context(|| format!("Failed to read {}", config_path.display()))?;
    raw.parse::<DocumentMut>()
        .with_context(|| format!("Failed to parse {}", config_path.display()))
}

/// Delete the removed keys from the user config file at `config_path`.
///
/// The original is first copied, byte for byte, to the first free name among
/// `config.toml.bak`, `config.toml.bak.2`, … (an earlier backup is never
/// overwritten), then the edited file replaces it atomically. Comments, key
/// order and every key not on [`REMOVED_KEYS`] stay exactly as they were. A
/// file with nothing to remove is not written at all.
///
/// # Errors
///
/// Reading or parsing the file, writing the backup, or writing the result.
pub fn clean_removed_config_keys_at(config_path: &Path) -> Result<Cleanup> {
    super::config::with_persist_lock(|| {
        let mut cleanup = Cleanup {
            config_path: config_path.to_path_buf(),
            removed: Vec::new(),
            backup: None,
        };
        if !config_path.exists() {
            return Ok(cleanup);
        }
        let original = std::fs::read(config_path)
            .with_context(|| format!("Failed to read {}", config_path.display()))?;
        let mut doc = read_document(config_path)?;
        cleanup.removed = strip_removed_keys(&mut doc);
        if cleanup.removed.is_empty() {
            return Ok(cleanup);
        }
        let backup = free_backup_path(config_path);
        super::config::write_config_bytes(&backup, &original)
            .with_context(|| format!("Failed to write backup {}", backup.display()))?;
        cleanup.backup = Some(backup);
        super::config::write_config_bytes(config_path, doc.to_string().as_bytes())?;
        Ok(cleanup)
    })
}

/// [`clean_removed_config_keys_at`] on the user config file.
///
/// # Errors
///
/// Resolving the config path, plus [`clean_removed_config_keys_at`]'s.
pub fn clean_removed_config_keys() -> Result<Cleanup> {
    clean_removed_config_keys_at(&super::config::get_config_path()?)
}

fn free_backup_path(config_path: &Path) -> PathBuf {
    let mut name = config_path.as_os_str().to_owned();
    name.push(".bak");
    let first = PathBuf::from(&name);
    if !first.exists() {
        return first;
    }
    (2u32..)
        .map(|n| {
            let mut numbered = name.clone();
            numbered.push(format!(".{n}"));
            PathBuf::from(numbered)
        })
        .find(|candidate| !candidate.exists())
        .unwrap_or(first)
}

/// The lines `mermaid clean-config` prints for a finished cleanup.
#[must_use]
pub fn cleanup_report(cleanup: &Cleanup) -> String {
    let path = cleanup.config_path.display();
    if cleanup.removed.is_empty() {
        return format!("No removed config keys in {path}. Nothing to do.");
    }
    let mut report = format!("Deleted from {path}:\n{}", key_list(&cleanup.removed));
    if let Some(backup) = &cleanup.backup {
        report.push_str(&format!("\nBackup of the old file: {}", backup.display()));
    }
    report
}

fn key_list(keys: &[FoundKey]) -> String {
    let width = keys.iter().map(|k| k.path.len()).max().unwrap_or(0);
    keys.iter()
        .map(|k| format!("  {:width$}  (removed in {})", k.path, k.version))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Before the TUI starts: when the user config holds removed keys and both
/// stdin and stderr are a terminal, list them and ask `[y/N]`. Yes runs the
/// cleanup; anything else changes nothing (the load's warnings then repeat
/// the `mermaid clean-config` hint). Headless runs, pipes and scripts never
/// reach this. Never fails: a problem is printed and startup continues.
pub fn offer_removed_key_cleanup() {
    if !(std::io::stdin().is_terminal() && std::io::stderr().is_terminal()) {
        return;
    }
    let Ok(config_path) = super::config::get_config_path() else {
        return;
    };
    // A file that does not parse is reported by the load that follows.
    let Ok(found) = find_removed_config_keys(&config_path) else {
        return;
    };
    if found.is_empty() {
        return;
    }
    let mut stderr = std::io::stderr();
    let _ = write!(
        stderr,
        "mermaid: {} has settings this version no longer uses:\n{}\n\
         Delete them now? The old file is kept as a backup. [y/N]: ",
        config_path.display(),
        key_list(&found)
    );
    let _ = stderr.flush();
    let mut answer = String::new();
    if std::io::stdin().read_line(&mut answer).is_err() {
        return;
    }
    if !matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes") {
        return;
    }
    match clean_removed_config_keys_at(&config_path) {
        Ok(cleanup) => {
            if let Some(backup) = &cleanup.backup {
                eprintln!(
                    "mermaid: deleted {} key(s); backup at {}",
                    cleanup.removed.len(),
                    backup.display()
                );
            }
        },
        Err(e) => eprintln!(
            "mermaid: could not clean {}: {}",
            config_path.display(),
            mermaid_model::utils::redact_secrets(&format!("{e:#}"))
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn scratch(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "mermaid-removed-keys-{name}-{}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    /// Every entry must really be ignored by today's schema — a key that came
    /// back must leave the list, or the cleanup would delete a live setting.
    #[test]
    fn every_removed_key_is_unknown_to_the_current_schema() {
        for key in REMOVED_KEYS {
            let mut table = toml::Table::new();
            let segments: Vec<&str> = key.path.split('.').collect();
            let (last, parents) = segments.split_last().unwrap();
            let mut cursor = &mut table;
            for parent in parents {
                cursor = cursor
                    .entry((*parent).to_string())
                    .or_insert_with(|| toml::Value::Table(toml::Table::new()))
                    .as_table_mut()
                    .unwrap();
            }
            cursor.insert((*last).to_string(), toml::Value::Boolean(true));
            let mut ignored = Vec::new();
            let _: mermaid_domain::Config =
                serde_ignored::deserialize(toml::Value::Table(table), |p| {
                    ignored.push(p.to_string());
                })
                .unwrap_or_else(|e| panic!("{} fails to load: {e}", key.path));
            assert_eq!(ignored, vec![key.path.to_string()], "{}", key.path);
        }
    }

    #[test]
    fn removed_key_for_matches_the_key_and_what_is_below_it_only() {
        assert_eq!(removed_key_for("plan").unwrap().version, "0.28.0");
        assert_eq!(removed_key_for("plan.permissions").unwrap().path, "plan");
        assert!(removed_key_for("planner").is_none());
        assert!(removed_key_for("ui.theme").is_none());
        assert!(removed_key_for("typo_key").is_none());
        assert_eq!(
            removed_key_for("ollama.cloud_api_key").unwrap().version,
            "0.12.0"
        );
    }

    const OLD_CONFIG: &str = r#"# my settings
last_used_model = "ollama/qwen3:8b"
mystery = 1 # a spelling error stays

[default_model]
provider = "ollama" # keep me
name = "qwen3:8b"

# plan mode settings
[plan]
auto_approve = false

[plan.permissions]
web = true

[ui]
theme = "dark"
syntax_theme = "monokai"

[computer_use]
auto_screenshot = true

[profiles.ci]
non_interactive = { output_format = "json" }

[profiles.ci.default_model]
name = "x"
"#;

    #[test]
    fn cleanup_deletes_only_removed_keys_and_keeps_the_rest_verbatim() {
        let dir = scratch("clean");
        let path = dir.join("config.toml");
        std::fs::write(&path, OLD_CONFIG).unwrap();

        let cleanup = clean_removed_config_keys_at(&path).unwrap();
        let paths: Vec<&str> = cleanup.removed.iter().map(|k| k.path.as_str()).collect();
        assert_eq!(
            paths,
            vec![
                "plan",
                "computer_use",
                "ui.syntax_theme",
                "profiles.ci.non_interactive"
            ]
        );
        let backup = cleanup.backup.clone().unwrap();
        assert_eq!(backup, dir.join("config.toml.bak"));
        assert_eq!(std::fs::read_to_string(&backup).unwrap(), OLD_CONFIG);

        let cleaned = std::fs::read_to_string(&path).unwrap();
        assert_eq!(
            cleaned,
            r#"# my settings
last_used_model = "ollama/qwen3:8b"
mystery = 1 # a spelling error stays

[default_model]
provider = "ollama" # keep me
name = "qwen3:8b"

[ui]
theme = "dark"

[profiles.ci]

[profiles.ci.default_model]
name = "x"
"#
        );

        // Running again finds nothing, writes nothing, and makes no backup.
        let again = clean_removed_config_keys_at(&path).unwrap();
        assert!(again.removed.is_empty());
        assert!(again.backup.is_none());
        assert_eq!(std::fs::read_to_string(&path).unwrap(), cleaned);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn an_earlier_backup_is_never_overwritten() {
        let dir = scratch("backup");
        let path = dir.join("config.toml");
        std::fs::write(dir.join("config.toml.bak"), "older").unwrap();
        std::fs::write(&path, "[plan]\nmodel = \"x\"\n").unwrap();

        let cleanup = clean_removed_config_keys_at(&path).unwrap();
        assert_eq!(cleanup.backup.unwrap(), dir.join("config.toml.bak.2"));
        assert_eq!(
            std::fs::read_to_string(dir.join("config.toml.bak")).unwrap(),
            "older"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn find_reports_without_writing_and_an_absent_file_is_clean() {
        let dir = scratch("find");
        let path = dir.join("config.toml");
        assert!(find_removed_config_keys(&path).unwrap().is_empty());
        std::fs::write(&path, OLD_CONFIG).unwrap();
        assert_eq!(find_removed_config_keys(&path).unwrap().len(), 4);
        assert_eq!(std::fs::read_to_string(&path).unwrap(), OLD_CONFIG);
        assert!(!dir.join("config.toml.bak").exists());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn report_names_the_keys_and_the_backup() {
        let cleanup = Cleanup {
            config_path: PathBuf::from("/c/config.toml"),
            removed: vec![FoundKey {
                path: "plan".into(),
                version: "0.28.0",
            }],
            backup: Some(PathBuf::from("/c/config.toml.bak")),
        };
        let report = cleanup_report(&cleanup);
        assert!(report.contains("plan  (removed in 0.28.0)"), "{report}");
        assert!(report.contains("/c/config.toml.bak"), "{report}");
        let none = Cleanup {
            config_path: PathBuf::from("/c/config.toml"),
            removed: Vec::new(),
            backup: None,
        };
        assert!(cleanup_report(&none).contains("Nothing to do"));
    }
}