vitrify-cli 0.5.0

vitrify: Rust CLI suite, orchestrator, and harness integration for spec-driven development (SDD)
Documentation
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! `vitrify probe` — probe this machine again and bring the probe record in
//! `~/.vitrify/config.toml` up to date. It prints what it found and what it
//! would change, and asks before it writes (`-y` answers for you); it
//! changes nothing else in the file. A home `vitrify init` has not set up is
//! refused.
//!
//! The probing is shared with `vitrify init`, which records its first probe
//! without asking: git, each harness CLI's one streaming headless spelling,
//! and each role that adds flags. Probes spawn in their own process group so
//! a hung probe is killable; absent harness CLIs are "untested", never an
//! error.

use std::collections::BTreeMap;
use std::io::{BufRead, Read, Write};
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

use vitrify_core::CoreError;
use vitrify_core::harness::{self, Profiles, Role};

use crate::home::{self, Origin};
use crate::out::{J, diag, emit};

/// How every message names the file, whatever `VITRIFY_HOME` says.
pub const CONFIG_LABEL: &str = "~/.vitrify/config.toml";

/// The candidate spelling table: one streaming spelling per harness. Each
/// writes the session as it happens — events line by line — so a worker's
/// log holds everything up to the moment it stops, however it stops. A
/// harness that does not accept its spelling fails.
const HARNESSES: [(&str, &[&[&str]]); 3] = [
    (
        "claude",
        &[&[
            "claude",
            "-p",
            "--output-format",
            "stream-json",
            "--verbose",
        ]],
    ),
    ("codex", &[&["codex", "exec", "--json"]]),
    ("opencode", &[&["opencode", "run", "--format", "json"]]),
];

const PROBE_PROMPT: &str = "Reply with exactly: OK";

fn probe_timeout() -> Duration {
    let secs = std::env::var("VITRIFY_PROBE_TIMEOUT_SECS")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(60u64);
    Duration::from_secs(secs)
}

enum ProbeResult {
    /// exit status 0 within the timeout, non-empty output
    Ok,
    Failed,
    TimedOut,
    Missing,
}

/// Run argv + prompt in its own process group; SIGKILL the group on timeout.
fn probe(argv: &[&str], timeout: Duration) -> ProbeResult {
    let mut cmd = Command::new(argv[0]);
    cmd.args(&argv[1..])
        .arg(PROBE_PROMPT)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .process_group(0);
    let mut child = match cmd.spawn() {
        Ok(c) => c,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return ProbeResult::Missing,
        Err(_) => return ProbeResult::Failed,
    };
    let pid = child.id();
    let start = Instant::now();
    loop {
        match child.try_wait() {
            Ok(Some(status)) => {
                let mut out = String::new();
                if let Some(mut stdout) = child.stdout.take() {
                    let _ = stdout.read_to_string(&mut out);
                }
                return if status.success() && !out.trim().is_empty() {
                    ProbeResult::Ok
                } else {
                    ProbeResult::Failed
                };
            }
            Ok(None) => {
                if start.elapsed() > timeout {
                    crate::gate::kill_group(pid);
                    let _ = child.wait();
                    return ProbeResult::TimedOut;
                }
                std::thread::sleep(Duration::from_millis(50));
            }
            Err(_) => return ProbeResult::Failed,
        }
    }
}

fn version_of(bin: &str) -> Option<String> {
    let out = Command::new(bin)
        .arg("--version")
        .stdin(Stdio::null())
        .output()
        .ok()?;
    if !out.status.success() {
        return None;
    }
    String::from_utf8(out.stdout)
        .ok()
        .and_then(|s| s.lines().next().map(str::trim).map(str::to_string))
}

pub(crate) struct HarnessReport {
    name: &'static str,
    present: bool,
    version: Option<String>,
    headless: Option<Vec<String>>,
    failure: Option<String>,
}

pub(crate) struct CheckLine {
    pub status: &'static str, // "ok" | "FAIL" | "untested" | "note" | "change"
    pub text: String,
}

impl CheckLine {
    pub fn new(status: &'static str, text: impl Into<String>) -> CheckLine {
        CheckLine {
            status,
            text: text.into(),
        }
    }
}

/// What one probe of the machine found: the lines to print, and the record
/// to keep in config.toml.
pub(crate) struct Probed {
    pub checks: Vec<CheckLine>,
    harnesses: Vec<HarnessReport>,
    git_present: bool,
    git_version: Option<String>,
}

impl Probed {
    pub fn failed(&self) -> bool {
        self.checks.iter().any(|c| c.status == "FAIL")
    }
}

/// Probe git, each harness CLI, and each role of a working harness that adds
/// flags (its profile read from `config_text`); report the home's content.
/// The home must exist: `init` creates it first, `probe` refuses without it.
pub(crate) fn probe_machine(
    home_dir: &Path,
    version: &str,
    config_text: &str,
) -> Result<Probed, CoreError> {
    let timeout = probe_timeout();
    let mut checks: Vec<CheckLine> = Vec::new();

    // git
    let git_version = version_of("git");
    let git_present = git_version.is_some();
    checks.push(match &git_version {
        Some(v) => CheckLine::new("ok", format!("git present ({v})")),
        None => CheckLine::new("FAIL", "git not found on PATH"),
    });

    // harness probes
    let mut harnesses: Vec<HarnessReport> = Vec::new();
    for (name, spellings) in HARNESSES {
        let hv = version_of(name);
        if hv.is_none() && Command::new(name).arg("--help").output().is_err() {
            checks.push(CheckLine::new(
                "untested",
                format!("harness {name}: CLI not on PATH"),
            ));
            harnesses.push(HarnessReport {
                name,
                present: false,
                version: None,
                headless: None,
                failure: None,
            });
            continue;
        }
        let mut recorded: Option<Vec<String>> = None;
        let mut failure = None;
        for argv in spellings.iter() {
            match probe(argv, timeout) {
                ProbeResult::Ok => {
                    recorded = Some(argv.iter().map(|s| s.to_string()).collect());
                    break;
                }
                ProbeResult::Missing => {
                    failure = Some(format!("harness {name}: CLI not on PATH"));
                    break;
                }
                ProbeResult::TimedOut => {
                    failure = Some(format!(
                        "harness {name}: probe timed out after {}s ({})",
                        timeout.as_secs(),
                        argv.join(" ")
                    ));
                }
                ProbeResult::Failed => {
                    failure = Some(format!(
                        "harness {name}: headless probe failed — vitrify needs `{}`; update {name} until it accepts it",
                        argv.join(" ")
                    ));
                }
            }
        }
        match (&recorded, &failure) {
            (Some(argv), _) => checks.push(CheckLine::new(
                "ok",
                format!("harness {name}: headless = {}", argv.join(" ")),
            )),
            (None, Some(f)) if f.ends_with("not on PATH") => {
                checks.push(CheckLine::new("untested", f.clone()));
            }
            (None, Some(f)) => checks.push(CheckLine::new("FAIL", f.clone())),
            (None, None) => {}
        }
        let present =
            recorded.is_some() || !failure.as_deref().unwrap_or("").ends_with("not on PATH");
        harnesses.push(HarnessReport {
            name,
            present,
            version: hv,
            headless: recorded,
            failure,
        });
    }

    // the home's content: materialized, restored, refreshed — and overrides
    let states = home::ensure_materialized(home_dir, version)?;
    let overrides: Vec<_> = states
        .iter()
        .filter(|s| s.origin == Origin::Override)
        .collect();
    checks.push(CheckLine::new(
        "ok",
        format!(
            "~/.vitrify materialized ({} assets, {} overridden)",
            states.len(),
            overrides.len()
        ),
    ));
    for s in &overrides {
        checks.push(CheckLine::new(
            "note",
            if s.stale_against_embedded {
                format!(
                    "override: {} (forked from another shipped version — stale against embedded)",
                    s.rel_path
                )
            } else {
                format!("override: {}", s.rel_path)
            },
        ));
    }

    // per-role model/effort profiles for each working harness
    match harness::parse(config_text, CONFIG_LABEL) {
        Ok(profiles) => {
            // a role that adds flags gets its own real one-shot probe, so a
            // model the account cannot reach fails here, not mid-run
            for h in harnesses.iter() {
                let Some(recorded) = &h.headless else {
                    continue;
                };
                let profile = profiles.get(h.name);
                for role in Role::ALL {
                    if !profile.is_own(role) {
                        continue; // retries run as the worker does
                    }
                    let label = format!("harness {} {}", h.name, role.as_str());
                    let described = profile.describe(role);
                    if profile.role_args(role).is_empty() {
                        checks.push(CheckLine::new("ok", format!("{label}: {described}")));
                        continue;
                    }
                    let argv = profiles.argv_for(h.name, recorded, role);
                    let argv_ref: Vec<&str> = argv.iter().map(String::as_str).collect();
                    checks.push(match probe(&argv_ref, timeout) {
                        ProbeResult::Ok => CheckLine::new("ok", format!("{label}: {described}")),
                        ProbeResult::TimedOut => CheckLine::new(
                            "FAIL",
                            format!(
                                "{label}: probe timed out after {}s ({})",
                                timeout.as_secs(),
                                argv.join(" ")
                            ),
                        ),
                        ProbeResult::Failed | ProbeResult::Missing => CheckLine::new(
                            "FAIL",
                            format!(
                                "{label}: probe failed ({}) — fix [harness.{}.{}] in {CONFIG_LABEL}",
                                argv.join(" "),
                                h.name,
                                role.as_str()
                            ),
                        ),
                    });
                }
            }
        }
        Err(e) => {
            let msg = e.to_string();
            checks.push(CheckLine::new(
                "FAIL",
                msg.strip_prefix("error: ").unwrap_or(&msg),
            ));
        }
    }

    Ok(Probed {
        checks,
        harnesses,
        git_present,
        git_version,
    })
}

// ------------------------------------------------------------ the record

/// A TOML basic string.
fn toml_str(s: &str) -> String {
    let mut out = String::from("\"");
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\t' => out.push_str("\\t"),
            c if c.is_control() => out.push_str(&format!("\\u{:04X}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// The `[probe]` tables for what `p` found, as config.toml carries them.
pub(crate) fn render_record(version: &str, p: &Probed) -> String {
    let mut s = String::from("[probe]\n");
    s.push_str("# What `vitrify init` and `vitrify probe` found on this machine.\n");
    s.push_str("# Re-run `vitrify probe` rather than edit these tables.\n");
    s.push_str(&format!("vitrify_version = {}\n", toml_str(version)));
    s.push_str(&format!(
        "git = {{ present = {}, version = {} }}\n",
        p.git_present,
        toml_str(p.git_version.as_deref().unwrap_or(""))
    ));
    for h in &p.harnesses {
        s.push_str(&format!("\n[probe.{}]\n", h.name));
        s.push_str(&format!("present = {}\n", h.present));
        if let Some(v) = &h.version {
            s.push_str(&format!("version = {}\n", toml_str(v)));
        }
        if let Some(argv) = &h.headless {
            let quoted: Vec<String> = argv.iter().map(|a| toml_str(a)).collect();
            s.push_str(&format!("headless = [{}]\n", quoted.join(", ")));
        }
    }
    s
}

/// The key of a table header line (`[a.b]`, `[[a]]`), dotted and unquoted;
/// `None` for any other line.
fn header_key(line: &str) -> Option<String> {
    let t = line.trim_start();
    let inner = t.strip_prefix("[[").or_else(|| t.strip_prefix('['))?;
    let end = inner.find(']')?;
    Some(
        inner[..end]
            .split('.')
            .map(|part| part.trim().trim_matches('"').trim_matches('\''))
            .collect::<Vec<_>>()
            .join("."),
    )
}

fn is_probe_key(key: &str) -> bool {
    key == "probe" || key.starts_with("probe.")
}

/// `text` with every `[probe]` / `[probe.*]` table replaced by `block`, put
/// where the first one was — or at the end, when there is none. Every other
/// line is kept byte for byte, comments included.
pub(crate) fn replace_record(text: &str, block: &str) -> String {
    let mut out = String::new();
    let mut in_record = false;
    let mut placed = false;
    // the delimiter of the multi-line string a line is inside, if any
    let mut open: Option<&str> = None;
    for line in text.split_inclusive('\n') {
        let header = if open.is_none() {
            header_key(line)
        } else {
            None
        };
        for delim in ["\"\"\"", "'''"] {
            if (open.is_none() || open == Some(delim)) && line.matches(delim).count() % 2 == 1 {
                open = if open.is_some() { None } else { Some(delim) };
            }
        }
        if let Some(key) = header {
            if is_probe_key(&key) {
                if !placed {
                    out.push_str(block);
                    placed = true;
                }
                in_record = true;
                continue;
            }
            if in_record && !out.ends_with("\n\n") {
                out.push('\n');
            }
            in_record = false;
        }
        if !in_record {
            out.push_str(line);
        }
    }
    if !placed {
        if !out.is_empty() && !out.ends_with('\n') {
            out.push('\n');
        }
        if !out.is_empty() && !out.ends_with("\n\n") {
            out.push('\n');
        }
        out.push_str(block);
    }
    out
}

/// The probe record of a parsed config.toml, flattened to `probe.<path>` →
/// the value as a person reads it; empty when there is none.
fn flatten_record(table: &toml::Table) -> BTreeMap<String, String> {
    fn walk(prefix: &str, v: &toml::Value, out: &mut BTreeMap<String, String>) {
        match v {
            toml::Value::Table(t) => {
                for (k, v) in t {
                    walk(&format!("{prefix}.{k}"), v, out);
                }
            }
            toml::Value::String(s) => {
                out.insert(prefix.to_string(), s.clone());
            }
            toml::Value::Array(a) => {
                let parts: Vec<String> = a
                    .iter()
                    .map(|x| match x {
                        toml::Value::String(s) => s.clone(),
                        other => other.to_string(),
                    })
                    .collect();
                out.insert(prefix.to_string(), parts.join(" "));
            }
            other => {
                out.insert(prefix.to_string(), other.to_string());
            }
        }
    }
    let mut out = BTreeMap::new();
    if let Some(record) = table.get("probe") {
        walk("probe", record, &mut out);
    }
    out
}

/// One difference between the recorded probe and the new one.
pub(crate) struct Change {
    pub key: String,
    pub from: Option<String>,
    pub to: Option<String>,
}

impl Change {
    fn line(&self) -> String {
        let show = |v: &Option<String>| v.clone().unwrap_or_else(|| "(none)".to_string());
        format!("{}: {} → {}", self.key, show(&self.from), show(&self.to))
    }
}

fn changes(old: &BTreeMap<String, String>, new: &BTreeMap<String, String>) -> Vec<Change> {
    let mut keys: Vec<&String> = old.keys().chain(new.keys()).collect();
    keys.sort();
    keys.dedup();
    keys.into_iter()
        .filter(|k| old.get(*k) != new.get(*k))
        .map(|k| Change {
            key: k.clone(),
            from: old.get(k).cloned(),
            to: new.get(k).cloned(),
        })
        .collect()
}

fn parse_config(text: &str) -> Result<toml::Table, CoreError> {
    text.parse::<toml::Table>().map_err(|e| {
        CoreError::refusal(
            2,
            format!(
                "error: unparseable {CONFIG_LABEL}: {e} → fix the line it names, or move the file aside and run vitrify init"
            ),
        )
    })
}

/// `text` with its probe record replaced by `block`, checked: everything
/// outside the record parses back unchanged, and the record is `block`.
pub(crate) fn updated_text(text: &str, block: &str) -> Result<String, CoreError> {
    let new_text = replace_record(text, block);
    let refuse = |why: &str| {
        CoreError::refusal(
            1,
            format!(
                "error: cannot update the [probe] tables of {CONFIG_LABEL} in place ({why}) → move them to the end of the file and run vitrify probe again"
            ),
        )
    };
    let mut before = parse_config(text)?;
    let mut after = new_text
        .parse::<toml::Table>()
        .map_err(|_| refuse("the result does not parse"))?;
    let expected = block
        .parse::<toml::Table>()
        .map_err(|_| refuse("the record does not parse"))?;
    if after.remove("probe") != expected.get("probe").cloned() {
        return Err(refuse("the record did not land whole"));
    }
    before.remove("probe");
    if before != after {
        return Err(refuse("a setting would change"));
    }
    Ok(new_text)
}

/// Write config.toml whole: a temporary file beside it, then a rename.
pub(crate) fn write_config(home_dir: &Path, text: &str) -> Result<(), CoreError> {
    let path = home::config_path(home_dir);
    let tmp = home_dir.join("config.toml.tmp");
    std::fs::write(&tmp, text).map_err(|source| CoreError::Io {
        path: tmp.clone(),
        source,
    })?;
    std::fs::rename(&tmp, &path).map_err(|source| CoreError::Io { path, source })
}

// ------------------------------------------------------------ the command

fn plural(n: usize) -> String {
    if n == 1 {
        "1 change".to_string()
    } else {
        format!("{n} changes")
    }
}

enum Answer {
    Yes,
    No,
    /// stdin closed before an answer: nobody is there to ask
    NoOne,
}

fn ask(n: usize) -> Answer {
    {
        let mut err = std::io::stderr().lock();
        let _ = write!(err, "apply {} to {CONFIG_LABEL}? [y/N] ", plural(n));
        let _ = err.flush();
    }
    let mut line = String::new();
    match std::io::stdin().lock().read_line(&mut line) {
        Ok(0) | Err(_) => {
            diag("");
            Answer::NoOne
        }
        Ok(_) => match line.trim().to_ascii_lowercase().as_str() {
            "y" | "yes" => Answer::Yes,
            _ => Answer::No,
        },
    }
}

/// The JSON shared by `init --json` and `probe --json`.
pub(crate) fn json_of(checks: &[CheckLine], p: &Probed, failed: bool) -> Vec<(String, J)> {
    let jchecks: Vec<J> = checks
        .iter()
        .map(|c| {
            J::Obj(vec![
                ("status".into(), J::s(c.status)),
                ("text".into(), J::s(c.text.clone())),
            ])
        })
        .collect();
    let jh: Vec<J> = p
        .harnesses
        .iter()
        .map(|h| {
            J::Obj(vec![
                ("name".into(), J::s(h.name)),
                ("present".into(), J::Bool(h.present)),
                ("version".into(), J::opt_str(h.version.clone())),
                (
                    "headless".into(),
                    match &h.headless {
                        Some(argv) => J::Arr(argv.iter().map(|a| J::s(a.clone())).collect()),
                        None => J::Null,
                    },
                ),
                ("failure".into(), J::opt_str(h.failure.clone())),
            ])
        })
        .collect();
    vec![
        ("green".into(), J::Bool(!failed)),
        ("checks".into(), J::Arr(jchecks)),
        ("harnesses".into(), J::Arr(jh)),
    ]
}

pub fn run(json: bool, assume_yes: bool) -> Result<u8, CoreError> {
    let Some(home_dir) = super::home_dir() else {
        return Err(CoreError::refusal(
            1,
            "error: cannot resolve the vitrify home (set VITRIFY_HOME or HOME)",
        ));
    };
    home::require_set_up(&home_dir)?;
    let path = home::config_path(&home_dir);
    let text = vitrify_core::model::read_text(&path)?;
    let table = parse_config(&text)?;
    let version = super::brief::VERSION;

    let mut checks = vec![CheckLine::new("ok", format!("vitrify version {version}"))];
    let probed = probe_machine(&home_dir, version, &text)?;
    let failed = probed.failed();
    let block = render_record(version, &probed);
    let new_text = updated_text(&text, &block)?;
    let found = changes(
        &flatten_record(&table),
        &flatten_record(&parse_config(&new_text)?),
    );
    checks.extend(
        probed
            .checks
            .iter()
            .map(|c| CheckLine::new(c.status, c.text.clone())),
    );
    if !json {
        for c in &checks {
            emit(&format!("{}: {}", c.status, c.text));
        }
    }

    let mut result: Vec<CheckLine> = found
        .iter()
        .map(|c| CheckLine::new("change", c.line()))
        .collect();
    if !json {
        for c in &result {
            emit(&format!("{}: {}", c.status, c.text));
        }
    }
    let applied = if found.is_empty() {
        result.push(CheckLine::new("ok", format!("{CONFIG_LABEL}: no changes")));
        false
    } else {
        if json && !assume_yes {
            // the changes go where the question goes
            for c in &result {
                diag(&format!("{}: {}", c.status, c.text));
            }
        }
        let answer = if assume_yes {
            Answer::Yes
        } else {
            ask(found.len())
        };
        match answer {
            Answer::Yes => {
                write_config(&home_dir, &new_text)?;
                result.push(CheckLine::new(
                    "ok",
                    format!("updated {CONFIG_LABEL} ({})", plural(found.len())),
                ));
                true
            }
            Answer::No => {
                result.push(CheckLine::new(
                    "note",
                    format!("{CONFIG_LABEL}: nothing changed"),
                ));
                false
            }
            Answer::NoOne => {
                result.push(CheckLine::new(
                    "note",
                    format!(
                        "{CONFIG_LABEL}: nothing changed — no answer on stdin; vitrify probe -y applies without asking"
                    ),
                ));
                false
            }
        }
    };
    let declined = !found.is_empty() && !applied;

    if json {
        let all: Vec<CheckLine> = checks
            .into_iter()
            .chain(result.into_iter().filter(|c| c.status != "change"))
            .collect();
        let mut obj = json_of(&all, &probed, failed);
        obj.push((
            "changes".into(),
            J::Arr(
                found
                    .iter()
                    .map(|c| {
                        J::Obj(vec![
                            ("key".into(), J::s(c.key.clone())),
                            ("from".into(), J::opt_str(c.from.clone())),
                            ("to".into(), J::opt_str(c.to.clone())),
                        ])
                    })
                    .collect(),
            ),
        ));
        obj.push(("applied".into(), J::Bool(applied)));
        emit(&J::Obj(obj).dumps_indent2());
    } else {
        for c in result.iter().filter(|c| c.status != "change") {
            emit(&format!("{}: {}", c.status, c.text));
        }
    }
    Ok(if failed || declined { 2 } else { 0 })
}

/// The headless spelling the probe recorded for one harness; the
/// orchestrator uses ONLY recorded spellings.
pub fn recorded_headless(home_dir: &Path, harness: &str) -> Option<Vec<String>> {
    let text = std::fs::read_to_string(home::config_path(home_dir)).ok()?;
    let table: toml::Table = text.parse().ok()?;
    let h = table.get("probe")?.as_table()?.get(harness)?.as_table()?;
    let argv = h.get("headless")?.as_array()?;
    Some(
        argv.iter()
            .filter_map(|v| v.as_str().map(str::to_string))
            .collect(),
    )
}

/// The role profiles in force for a run: `~/.vitrify/config.toml`, with a
/// repository's `.vitrify/config.toml` overriding it key by key.
pub fn role_profiles(home_dir: &Path, repo_root: Option<&Path>) -> Result<Profiles, CoreError> {
    let layers = vitrify_core::config::layers(repo_root, Some(home_dir))?;
    harness::from_layers(&layers)
}

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

    const BLOCK: &str = "[probe]\nvitrify_version = \"1\"\n\n[probe.claude]\npresent = true\n";

    #[test]
    fn the_record_is_appended_when_there_is_none() {
        let text = "schema = 1\n\n[run]\nworker_cap = 4 # mine\n";
        let out = replace_record(text, BLOCK);
        assert_eq!(out, format!("{text}\n{BLOCK}"));
        assert_eq!(updated_text(text, BLOCK).unwrap(), out);
    }

    #[test]
    fn only_the_record_is_replaced_in_place() {
        let text = "# top\nschema = 1\n\n[run]\nworker_cap = 2 # mine\n\n[probe]\nvitrify_version = \"0\"\n\n[probe.codex]\npresent = false\n\n[lint]\n# keep\nreadset_budget_bytes = 9\n";
        let out = updated_text(text, BLOCK).unwrap();
        assert_eq!(
            out,
            "# top\nschema = 1\n\n[run]\nworker_cap = 2 # mine\n\n[probe]\nvitrify_version = \"1\"\n\n[probe.claude]\npresent = true\n\n[lint]\n# keep\nreadset_budget_bytes = 9\n"
        );
    }

    #[test]
    fn a_header_inside_a_multi_line_string_is_not_a_table() {
        let text = "note = \"\"\"\n[probe]\n\"\"\"\n[run]\nworker_cap = 1\n";
        let out = updated_text(text, BLOCK).unwrap();
        assert!(out.starts_with(text), "{out}");
    }

    #[test]
    fn changes_name_what_moved() {
        let old: toml::Table = "[probe.claude]\npresent = true\nversion = \"1\"\n"
            .parse()
            .unwrap();
        let new: toml::Table =
            "[probe.claude]\npresent = true\nversion = \"2\"\nheadless = [\"claude\", \"-p\"]\n"
                .parse()
                .unwrap();
        let lines: Vec<String> = changes(&flatten_record(&old), &flatten_record(&new))
            .iter()
            .map(Change::line)
            .collect();
        assert_eq!(
            lines,
            [
                "probe.claude.headless: (none) → claude -p",
                "probe.claude.version: 1 → 2"
            ]
        );
        assert!(changes(&flatten_record(&new), &flatten_record(&new)).is_empty());
    }

    #[test]
    fn strings_are_escaped() {
        assert_eq!(toml_str("a\"b\\c"), "\"a\\\"b\\\\c\"");
    }
}