Skip to main content

ev/
cmd.rs

1use crate::canonical::compute_id;
2use crate::store::Store;
3use crate::tick::{Check, Ground, Liveness, Tick};
4use crate::verify::verify;
5use std::path::Path;
6use std::process::ExitCode;
7
8/// Whether a triggering change landed after this ground's most recent run. Uses the latest
9/// receipt's commit + the binding's triggered_by paths. False when there is no receipt, no
10/// Test binding, or git can't tell (None ⇒ not evaluated).
11fn triggered_since(
12    repo: &std::path::Path,
13    ground: &crate::tick::Ground,
14    receipts: &[crate::receipt::Receipt],
15) -> bool {
16    use crate::tick::Check;
17    let triggered_by = match &ground.check {
18        Some(Check::Test { liveness, .. }) => &liveness.triggered_by,
19        _ => return false,
20    };
21    let latest = receipts.iter().max_by(|a, b| a.ran_at.cmp(&b.ran_at));
22    match latest {
23        Some(r) => crate::liveness::changed_since(repo, &r.commit, triggered_by).unwrap_or(false),
24        None => false,
25    }
26}
27
28pub fn init(repo: &Path) -> ExitCode {
29    let store = Store::at(repo);
30    match store.init() {
31        Ok(true) => {
32            println!("created .evolving/  (content-addressed chain + results cache)");
33            ExitCode::SUCCESS
34        }
35        Ok(false) => {
36            println!(".evolving/ already exists (no-op)");
37            ExitCode::SUCCESS
38        }
39        Err(e) => {
40            eprintln!("error: could not create .evolving/: {e}");
41            ExitCode::FAILURE
42        }
43    }
44}
45pub fn show(repo: &Path, id: &str) -> ExitCode {
46    let store = Store::at(repo);
47    let path = store.ticks_dir().join(id);
48    if !path.is_file() {
49        eprintln!("error: no tick with id {id}");
50        return ExitCode::FAILURE;
51    }
52    match std::fs::read_to_string(&path) {
53        Ok(text) => {
54            // print as-is (the on-disk pretty JSON: hashed payload + bookkeeping).
55            println!("{text}");
56            // surface the declared authority on its own line when present (boot-time read).
57            if let Ok(v) = serde_json::from_str::<serde_json::Value>(&text) {
58                if let Some(a) = v.get("authority").and_then(|x| x.as_str()) {
59                    println!("authority: {a}");
60                }
61                if let Some(j) = v.get("jurisdiction").and_then(|x| x.as_str()) {
62                    println!("jurisdiction: {j}");
63                }
64                if let Some(r) = v.get("round_id").and_then(|x| x.as_str()) {
65                    println!("round_id: {r}");
66                }
67            }
68            ExitCode::SUCCESS
69        }
70        Err(e) => {
71            eprintln!("error: reading {id}: {e}");
72            ExitCode::FAILURE
73        }
74    }
75}
76pub fn decide(repo: &Path, decision: Option<&str>, args: &[String]) -> ExitCode {
77    // clap fills the optional positional with the first token even when it is a flag (it carries
78    // allow_hyphen_values so a leading --from-git can reach us at all). A real decision never
79    // starts with '-', so a hyphen-leading "decision" is actually a flag: re-route it into args
80    // and leave the positional empty, letting the capture flag-loop own --from-git uniformly.
81    let (decision, args): (Option<&str>, Vec<String>) = match decision {
82        Some(d) if d.starts_with('-') => {
83            let mut v = vec![d.to_string()];
84            v.extend_from_slice(args);
85            (None, v)
86        }
87        other => (other, args.to_vec()),
88    };
89    match crate::capture::run(repo, decision, &args) {
90        Ok(t) => {
91            crate::events::append(&Store::at(repo), "decide", Some(&t.id), None);
92            println!("recorded {} ({} ground(s))", t.id, t.grounds.len());
93            ExitCode::SUCCESS
94        }
95        Err(e) => {
96            eprintln!("error: {e}");
97            ExitCode::FAILURE
98        }
99    }
100}
101
102pub fn guard(repo: &Path, a: crate::guard::GuardArgs) -> ExitCode {
103    match crate::guard::run(repo, a) {
104        Ok(t) => {
105            crate::events::append(&Store::at(repo), "guard", Some(&t.id), None);
106            println!("bound; wrote child {}", t.id);
107            ExitCode::SUCCESS
108        }
109        Err(e) => {
110            eprintln!("error: {e}");
111            ExitCode::FAILURE
112        }
113    }
114}
115
116pub fn verify_cmd(repo: &Path, self_test: bool) -> ExitCode {
117    if self_test {
118        return self_test_golden();
119    }
120    let store = Store::at(repo);
121    // Forward-compat: tolerated unknown top-level keys are warnings, never violations — they do
122    // not affect the verdict, but they keep a typo'd field name visible.
123    for w in crate::verify::unknown_key_warnings(&store).unwrap_or_default() {
124        eprintln!("{w}");
125    }
126    match verify(&store) {
127        Ok(v) if v.is_empty() => {
128            println!("✓ chain intact: every id == hash(payload), lineage forward-only");
129            println!("✓ every tick validates against the closed schema (R1) and check shape (R2)");
130            ExitCode::SUCCESS
131        }
132        Ok(v) => {
133            for line in &v {
134                println!("✗ {line}");
135            }
136            eprintln!("{} violation(s)", v.len());
137            ExitCode::FAILURE
138        }
139        Err(e) => {
140            eprintln!("error: reading store: {e}");
141            ExitCode::FAILURE
142        }
143    }
144}
145
146/// The latest ran_at across a ref's receipts (for the display line), if any.
147/// `receipts` is already scoped to one ref by `read_for`, so no filtering is needed.
148fn latest_ran_at(receipts: &[crate::receipt::Receipt]) -> Option<String> {
149    receipts.iter().map(|r| r.ran_at.clone()).max()
150}
151
152/// The evaluation context for one `ev check` / `ev reopen` invocation: the staleness reference
153/// (resolved per policy by the caller), the selected-list, the wall clock, and the staleness
154/// window. The I/O assembly lives here in the command layer so `verdict::verdict_for` stays pure.
155fn live_ctx(
156    store: &Store,
157    staleness_days: u64,
158    live_origin_sha: Option<String>,
159    attest: Option<Vec<String>>,
160) -> crate::verdict::Ctx {
161    crate::verdict::Ctx {
162        live_origin_sha,
163        selected: crate::selected::read(store).unwrap_or(None),
164        now_unix: time::OffsetDateTime::now_utc().unix_timestamp(),
165        staleness_secs: staleness_days as i64 * 86_400,
166        attest,
167    }
168}
169
170pub fn check(
171    repo: &Path,
172    exit_on_red: bool,
173    run: bool,
174    platform: &str,
175    offline: bool,
176    attest: Vec<String>,
177) -> ExitCode {
178    use crate::verdict::{verdict_for, Verdict};
179    let store = Store::at(repo);
180    if !store.exists() {
181        eprintln!("error: no .evolving/ store here — run `ev init` first");
182        return ExitCode::FAILURE;
183    }
184    let files = match store.read_all() {
185        Ok(f) => f,
186        Err(e) => {
187            eprintln!("error: reading store: {e}");
188            return ExitCode::FAILURE;
189        }
190    };
191    let config = crate::config::read(&store);
192
193    // --run pass: for every live Test-bound ground that declares this platform, run the
194    // bound ref locally and append a receipt for it (one local run = one platform receipt).
195    if run {
196        for (_filename, raw) in &files {
197            let t = match crate::tick::from_value(raw) {
198                Ok(t) => t,
199                Err(_) => continue,
200            };
201            if t.status != "live" {
202                continue;
203            }
204            for g in &t.grounds {
205                if let Some(Check::Test {
206                    reference,
207                    counter_test,
208                    liveness,
209                    ..
210                }) = &g.check
211                {
212                    if liveness.platforms.iter().any(|p| p == platform) {
213                        // run the bound check
214                        match crate::runner::run_check(
215                            repo,
216                            reference,
217                            platform,
218                            config.green_exit_code,
219                        ) {
220                            Ok(mut rc) => {
221                                // prove falsifiability: the counter-test must produce the OPPOSITE
222                                // result. A harvested binding (counter_test None) skips this step,
223                                // leaving falsifiable None — the existing default.
224                                if let Some(counter_test) = counter_test {
225                                    if let Ok(ct) = crate::runner::run_check(
226                                        repo,
227                                        counter_test,
228                                        platform,
229                                        config.green_exit_code,
230                                    ) {
231                                        rc.falsifiable = Some(rc.result != ct.result);
232                                    }
233                                }
234                                if let Err(e) = crate::receipt::append(&store, &rc) {
235                                    eprintln!(
236                                        "warning: could not write receipt for {reference:?}: {e}"
237                                    );
238                                }
239                            }
240                            Err(e) => eprintln!("warning: run failed for {reference:?}: {e}"),
241                        }
242                    }
243                }
244            }
245        }
246    }
247
248    let live_origin = crate::staleness::resolve(repo, &store, &config.staleness_ref, offline);
249    let attest = if attest.is_empty() {
250        None
251    } else {
252        Some(attest)
253    };
254    let ctx = live_ctx(&store, config.staleness_days, live_origin, attest);
255    let mut rows: Vec<String> = Vec::new();
256    let mut any_not_green = false;
257    // Harvested-binding honesty debt: N test bindings carry no counter-test (counter_test None) out
258    // of M total test bindings. Surfaced as a trailing line so the missing falsifiability proof is
259    // never silent — the verdict itself stays honest (a harvested green/red reads exactly as it ran).
260    let mut total_test_bindings = 0usize;
261    let mut harvested_unproven = 0usize;
262
263    for (filename, raw) in &files {
264        let t = match crate::tick::from_value(raw) {
265            Ok(t) => t,
266            Err(_) => continue, // ev verify owns schema errors; check skips unparsable ticks
267        };
268        if t.status != "live" {
269            continue;
270        }
271        let mut verdicts = Vec::with_capacity(t.grounds.len());
272        for g in &t.grounds {
273            // Receipts are read only for Test-bound grounds; person/unbound need none.
274            let receipts = match &g.check {
275                Some(Check::Test { reference, .. }) => {
276                    crate::receipt::read_for(&store, reference).unwrap_or_default()
277                }
278                _ => Vec::new(),
279            };
280            // verdict_for returns NotApplicable for any non-Test ground.
281            let ts = triggered_since(repo, g, &receipts);
282            let mut v = verdict_for(g, &receipts, &ctx, ts);
283            // LOCK 1 (gate-time, structural): a C/D-jurisdiction (detect-only) decision is
284            // structurally ungateable — map ANY not-green verdict to the non-gating Memo BEFORE the
285            // any_not_green writer below, so it can never flip --exit-on-red. Remapping every
286            // not-green at once is more robust than threading Memo through each gate site.
287            if matches!(t.jurisdiction.as_deref(), Some("C") | Some("D"))
288                && !matches!(v, Verdict::Green | Verdict::NotApplicable | Verdict::Exempt)
289            {
290                v = Verdict::Memo;
291            }
292            if !matches!(
293                v,
294                Verdict::Green | Verdict::NotApplicable | Verdict::Exempt | Verdict::Memo
295            ) {
296                any_not_green = true;
297            }
298            // Only Test-bound grounds appear in the printed set and the gate.
299            if let Some(Check::Test { counter_test, .. }) = &g.check {
300                total_test_bindings += 1;
301                let harvested = counter_test.is_none();
302                let mut detail = match &v {
303                    Verdict::NotRun { missing_platforms } => {
304                        format!("missing: {}", missing_platforms.join(", "))
305                    }
306                    Verdict::Stale { reason } => reason.clone(),
307                    _ => latest_ran_at(&receipts)
308                        .map(|ts| format!("ran {ts}"))
309                        .unwrap_or_else(|| "no receipt".into()),
310                };
311                // A harvested binding carries no counter-test, so its falsifiability was never
312                // proven; annotate the row honestly. The verdict is UNCHANGED — a passing harvested
313                // test still reads green (pass-green), a failing one still reads red (a real gate).
314                if harvested {
315                    harvested_unproven += 1;
316                    detail = format!("harvested — falsifiability not proven; {detail}");
317                    crate::events::append(&store, "harvested", Some(&t.id), Some(v.label()));
318                }
319                rows.push(format!(
320                    "{}\t{filename}\t{:?}\t({detail})",
321                    v.label(),
322                    g.claim
323                ));
324                crate::events::append(&store, "check", Some(&t.id), Some(v.label()));
325            }
326            verdicts.push((g, v));
327        }
328        // The per-host verdict-cache read contract for this tick (a hook reads it without shelling check).
329        let _ = crate::state::write_state(
330            &store,
331            &t.id,
332            &verdicts,
333            &config.staleness_ref,
334            ctx.live_origin_sha.as_deref(),
335        );
336    }
337
338    if rows.is_empty() {
339        println!("no test-bound grounds to check");
340    } else {
341        for r in &rows {
342            println!("{r}");
343        }
344        // The harvested-binding debt: how many of the test bindings have no counter-test (so their
345        // falsifiability is unproven). Pointed at `ev guard`, which is how a counter-test is added.
346        if harvested_unproven > 0 {
347            println!(
348                "harvested-unproven: {harvested_unproven} of {total_test_bindings} test bindings have no counter-test (run ev guard to add one)"
349            );
350        }
351        if !run {
352            // under --run the verdict itself carries falsifiability (an `unproven` row is a
353            // counter-test that did not flip); without it, point at the proof step rather than
354            // implying one already happened.
355            println!("note: run `ev check --run` to execute each counter-test and prove its falsifiability");
356        }
357    }
358    if exit_on_red && any_not_green {
359        return ExitCode::FAILURE;
360    }
361    ExitCode::SUCCESS
362}
363
364/// The parsed `ev migrate` invocation (built in main.rs from the clap subcommand).
365pub struct MigrateArgs {
366    pub sources: Vec<String>,
367    pub dry_run: bool,
368    pub reconcile: bool,
369    pub against: Option<String>,
370    pub blame: Option<String>,
371    pub bind_check: Option<String>,
372    pub platforms: Vec<String>,
373    pub triggered_by: Vec<String>,
374    pub surfaces: Vec<String>,
375    pub verified_at_sha: Option<String>,
376}
377
378/// Read a `<kind>:<path>` source spec, dispatch to the matching pure extractor, and return the
379/// extracted records. The kind names the substrate format; the path is read from disk here (the
380/// extractors themselves stay pure `&str -> Vec<MigrationRecord>`).
381fn extract_source(spec: &str) -> Result<Vec<crate::migrate::MigrationRecord>, String> {
382    let (kind, path) = spec
383        .split_once(':')
384        .ok_or_else(|| format!("--source expects <kind>:<path>, got {spec:?}"))?;
385    let text = std::fs::read_to_string(path).map_err(|e| format!("reading {path}: {e}"))?;
386    let recs = match kind {
387        "gitlog" => crate::migrate::extract_gitlog(&text),
388        "to-human" => crate::migrate::extract_to_human(&text),
389        "decisions-immutable" => crate::migrate::extract_decisions_immutable(&text),
390        "escalation" => crate::migrate::extract_escalation(&text),
391        other => {
392            return Err(format!(
393                "unknown source kind {other:?} (expected gitlog | to-human | decisions-immutable | escalation)"
394            ))
395        }
396    };
397    Ok(recs)
398}
399
400pub fn migrate(repo: &Path, a: MigrateArgs) -> ExitCode {
401    // --bind-check: harvest one existing test as a (counter-test-less) bound check and print it.
402    if let Some(selector) = &a.bind_check {
403        let sha = match crate::capture::resolve_sha(repo, &a.verified_at_sha) {
404            Ok(s) => s,
405            Err(e) => {
406                eprintln!("error: {e}");
407                return ExitCode::FAILURE;
408            }
409        };
410        match crate::migrate::bind_check(
411            selector.clone(),
412            sha,
413            a.platforms.clone(),
414            a.triggered_by.clone(),
415            a.surfaces.clone(),
416        ) {
417            Ok(Check::Test {
418                reference,
419                liveness,
420                ..
421            }) => {
422                println!(
423                    "harvested check (falsifiability not proven; no counter-test): {reference:?} on [{}] triggered-by [{}] surface [{}]",
424                    liveness.platforms.join(", "),
425                    liveness.triggered_by.join(", "),
426                    liveness.surfaces.join(", ")
427                );
428                return ExitCode::SUCCESS;
429            }
430            Ok(_) => unreachable!("bind_check yields a Test check"),
431            Err(e) => {
432                eprintln!("error: {e}");
433                return ExitCode::FAILURE;
434            }
435        }
436    }
437
438    // --reconcile --against <src>: join the source against the store and report the buckets.
439    if a.reconcile {
440        let against = match &a.against {
441            Some(s) => s,
442            None => {
443                eprintln!("error: --reconcile requires --against <kind>:<path>");
444                return ExitCode::FAILURE;
445            }
446        };
447        let recs = match extract_source(against) {
448            Ok(r) => r,
449            Err(e) => {
450                eprintln!("error: {e}");
451                return ExitCode::FAILURE;
452            }
453        };
454        match crate::migrate::reconcile(repo, &recs) {
455            Ok(rep) => {
456                println!(
457                    "reconcile: in-both {}, source-only {} (the capture gap), store-only {}, un-keyable {}",
458                    rep.in_both, rep.source_only, rep.store_only, rep.un_keyable
459                );
460                return ExitCode::SUCCESS;
461            }
462            Err(e) => {
463                eprintln!("error: {e}");
464                return ExitCode::FAILURE;
465            }
466        }
467    }
468
469    // The default action: backfill every --source into the ledger (idempotent).
470    if a.sources.is_empty() {
471        eprintln!("error: ev migrate needs at least one --source <kind>:<path> (or --reconcile / --bind-check)");
472        return ExitCode::FAILURE;
473    }
474    let mut records = Vec::new();
475    for spec in &a.sources {
476        match extract_source(spec) {
477            Ok(mut r) => records.append(&mut r),
478            Err(e) => {
479                eprintln!("error: {e}");
480                return ExitCode::FAILURE;
481            }
482        }
483    }
484    match crate::migrate::backfill(repo, records, a.blame.as_deref(), a.dry_run) {
485        Ok(s) => {
486            if !a.dry_run {
487                crate::events::append(&Store::at(repo), "migrate", None, None);
488            }
489            println!(
490                "{}imported {}, skipped {}, re-linked {}, {} source-only gap(s)",
491                if a.dry_run { "(dry-run) " } else { "" },
492                s.imported,
493                s.skipped,
494                s.relinked,
495                s.source_only_gaps
496            );
497            ExitCode::SUCCESS
498        }
499        Err(e) => {
500            eprintln!("error: {e}");
501            ExitCode::FAILURE
502        }
503    }
504}
505
506pub fn why(repo: &Path, selector: &str) -> ExitCode {
507    let store = Store::at(repo);
508    if !store.exists() {
509        eprintln!("error: no .evolving/ store here — run `ev init` first");
510        return ExitCode::FAILURE;
511    }
512    let files = match store.read_all() {
513        Ok(f) => f,
514        Err(e) => {
515            eprintln!("error: reading store: {e}");
516            return ExitCode::FAILURE;
517        }
518    };
519    let mut found = false;
520    for (filename, raw) in &files {
521        let t = match crate::tick::from_value(raw) {
522            Ok(t) => t,
523            Err(_) => continue,
524        };
525        if t.status != "live" {
526            continue;
527        }
528        for g in &t.grounds {
529            if let Some(Check::Test { reference, .. }) = &g.check {
530                if reference.as_str() == selector {
531                    found = true;
532                    println!(
533                        "{filename}\t{:?}\tguards: {:?} ({})",
534                        t.decision, g.claim, g.supports
535                    );
536                }
537            }
538        }
539    }
540    if !found {
541        eprintln!("{selector:?} guards nothing");
542        return ExitCode::FAILURE;
543    }
544    ExitCode::SUCCESS
545}
546
547/// List every decision in the ledger: id, status, decision (sorted by id, deterministic).
548pub fn list(repo: &Path) -> ExitCode {
549    let store = Store::at(repo);
550    if !store.exists() {
551        eprintln!("error: no .evolving/ store here — run `ev init` first");
552        return ExitCode::FAILURE;
553    }
554    let files = match store.read_all() {
555        Ok(f) => f,
556        Err(e) => {
557            eprintln!("error: reading store: {e}");
558            return ExitCode::FAILURE;
559        }
560    };
561    // One pre-rendered line per tick, keyed by id so the output is deterministic. The bookkeeping
562    // tags (authority, jurisdiction, round_id) are appended inline when present — same one-line shape as show.
563    let mut rows: Vec<String> = files
564        .iter()
565        .map(|(name, raw)| {
566            let line = match crate::tick::from_value(raw) {
567                Ok(t) => {
568                    let mut l = format!("{name}\t{}\t{:?}", t.status, t.decision);
569                    if let Some(a) = &t.authority {
570                        l.push_str(&format!("\tauthority={a}"));
571                    }
572                    if let Some(j) = &t.jurisdiction {
573                        l.push_str(&format!("\tjurisdiction={j}"));
574                    }
575                    if let Some(r) = &t.round_id {
576                        l.push_str(&format!("\tround_id={r}"));
577                    }
578                    l
579                }
580                Err(_) => format!("{name}\t?\t\"<unparseable>\""),
581            };
582            line
583        })
584        .collect();
585    rows.sort();
586    if rows.is_empty() {
587        println!("no decisions yet");
588        return ExitCode::SUCCESS;
589    }
590    for line in &rows {
591        println!("{line}");
592    }
593    ExitCode::SUCCESS
594}
595
596/// A decision is "load-bearing" iff any of its grounds closes a road (`supports` starts with
597/// `"rejected:"`). Those are the rulings a fresh agent must not re-walk, so they pin above the cap.
598/// Detectable straight from the tick — 0-network, no receipts, no git.
599fn load_bearing(t: &Tick) -> bool {
600    t.grounds
601        .iter()
602        .any(|g| g.supports.starts_with("rejected:"))
603}
604
605/// Boot-read: the live user-ruled decisions and the roads they rejected. A near-zero-cost,
606/// 0-network read (read_all only; no git, no receipts) for a fresh agent to load the
607/// decisions it must respect and the options it must not re-propose. Load-bearing rulings
608/// (those that closed a road) sort FIRST — pinned above the cap regardless of recency — then
609/// by recency (held_since), then id. Capped to the effective limit, with a remainder footer
610/// that counts how many hidden rulings closed a road so the elision stays visible.
611pub fn brief(repo: &Path, limit: Option<usize>) -> ExitCode {
612    let store = Store::at(repo);
613    if !store.exists() {
614        eprintln!("error: no .evolving/ store here — run `ev init` first");
615        return ExitCode::FAILURE;
616    }
617    let files = match store.read_all() {
618        Ok(f) => f,
619        Err(e) => {
620            eprintln!("error: reading store: {e}");
621            return ExitCode::FAILURE;
622        }
623    };
624    // The flag overrides config; 0 (here or in config) means "show all".
625    let limit = limit.unwrap_or(crate::config::read(&store).brief_limit);
626    // Keep only live, user-ruled decisions; carry the id so output is deterministic.
627    let mut kept: Vec<(String, Tick)> = files
628        .iter()
629        .filter_map(|(name, raw)| crate::tick::from_value(raw).ok().map(|t| (name.clone(), t)))
630        .filter(|(_, t)| t.status == "live" && t.authority.as_deref() == Some("user-ruled"))
631        .collect();
632    let lb = load_bearing;
633    // Load-bearing first (true > false, so descending pins them), then most-recent-first by
634    // held_since, then id descending — all deterministic.
635    kept.sort_by(|a, b| {
636        lb(&b.1)
637            .cmp(&lb(&a.1))
638            .then(b.1.held_since.cmp(&a.1.held_since))
639            .then(b.0.cmp(&a.0))
640    });
641    if kept.is_empty() {
642        println!("no user-ruled decisions");
643        return ExitCode::SUCCESS;
644    }
645    let total = kept.len();
646    // 0 means "show all"; otherwise cap at the limit (never past the end).
647    let n = if limit == 0 { total } else { limit.min(total) };
648    // Count load-bearing rulings about to be elided, before we truncate the shown set.
649    let dropped_lb = kept[n..].iter().filter(|(_, t)| lb(t)).count();
650    kept.truncate(n);
651    for (_id, t) in &kept {
652        println!("{}  [user-ruled]", t.decision);
653        for g in &t.grounds {
654            if let Some(option) = g.supports.strip_prefix("rejected:") {
655                println!("  rejected {option}: {}", g.claim);
656            }
657        }
658    }
659    if total > n {
660        let dropped = total - n;
661        let lb_clause = if dropped_lb > 0 {
662            format!(", {dropped_lb} with rejected roads")
663        } else {
664            String::new()
665        };
666        println!("… {dropped} more user-ruled decision(s){lb_clause} — `ev list` for all");
667    }
668    ExitCode::SUCCESS
669}
670
671/// Show the decision lineage from HEAD back to genesis (newest first).
672pub fn log(repo: &Path) -> ExitCode {
673    let store = Store::at(repo);
674    if !store.exists() {
675        eprintln!("error: no .evolving/ store here — run `ev init` first");
676        return ExitCode::FAILURE;
677    }
678    let mut id = match store.read_head() {
679        Ok(h) => h,
680        Err(e) => {
681            eprintln!("error: reading HEAD: {e}");
682            return ExitCode::FAILURE;
683        }
684    };
685    if id.is_empty() {
686        println!("no decisions yet");
687        return ExitCode::SUCCESS;
688    }
689    let mut seen = std::collections::HashSet::new();
690    while !id.is_empty() {
691        if !seen.insert(id.clone()) {
692            break; // cycle guard (a content-addressed chain can't cycle, but never loop)
693        }
694        match store.read_tick(&id) {
695            Ok(Some(t)) => {
696                println!("{}\t{}\t{:?}", t.id, t.status, t.decision);
697                id = t.parent_id;
698            }
699            Ok(None) => {
700                eprintln!("warning: {id} not found (broken lineage)");
701                break;
702            }
703            Err(e) => {
704                eprintln!("error: reading {id}: {e}");
705                return ExitCode::FAILURE;
706            }
707        }
708    }
709    ExitCode::SUCCESS
710}
711
712pub fn reopen(repo: &Path, id: &str) -> ExitCode {
713    let store = Store::at(repo);
714    let tick = match store.read_tick(id) {
715        Ok(Some(t)) => t,
716        Ok(None) => {
717            eprintln!("error: no tick with id {id}");
718            return ExitCode::FAILURE;
719        }
720        Err(e) => {
721            eprintln!("error: reading {id}: {e}");
722            return ExitCode::FAILURE;
723        }
724    };
725    let config = crate::config::read(&store);
726    let live_origin = crate::staleness::resolve(repo, &store, &config.staleness_ref, true);
727    let ctx = live_ctx(&store, config.staleness_days, live_origin, None);
728
729    crate::events::append(&store, "reopen", Some(id), None);
730    println!("decision {}: {:?}", tick.id, tick.decision);
731    if !tick.observe.is_empty() {
732        println!("observe: {:?}", tick.observe);
733    }
734    if let Some(a) = &tick.authority {
735        println!("authority: {a}");
736    }
737    if let Some(j) = &tick.jurisdiction {
738        println!("jurisdiction: {j}");
739    }
740    if let Some(r) = &tick.round_id {
741        println!("round_id: {r}");
742    }
743    for g in &tick.grounds {
744        match &g.check {
745            Some(Check::Test {
746                reference,
747                verified_at_sha,
748                ..
749            }) => {
750                let receipts = crate::receipt::read_for(&store, reference).unwrap_or_default();
751                let ts = triggered_since(repo, g, &receipts);
752                let v = crate::verdict::verdict_for(g, &receipts, &ctx, ts);
753                let now = v.label();
754                let short = &verified_at_sha[..verified_at_sha.len().min(8)];
755                println!(
756                    "  [{}] {:?} — test {:?} frozen@{short} now: {now}",
757                    g.supports, g.claim, reference
758                );
759            }
760            Some(Check::Person { reference }) => {
761                println!("  [{}] {:?} — person {:?}", g.supports, g.claim, reference);
762            }
763            None => {
764                println!("  [{}] {:?}", g.supports, g.claim);
765            }
766        }
767    }
768    ExitCode::SUCCESS
769}
770
771/// Reproduce the two frozen golden vectors; non-zero if either id drifts.
772fn self_test_golden() -> ExitCode {
773    let genesis = Tick {
774        id: String::new(),
775        parent_id: "".into(),
776        observe: "evaluating retrieval backend".into(),
777        decision: "freeze the retrieval schema for v2".into(),
778        grounds: vec![
779            Ground {
780                claim: "team still wants a frozen schema".into(),
781                supports: "chosen".into(),
782                check: Some(Check::Person {
783                    reference: "Q3 infra review".into(),
784                }),
785            },
786            Ground {
787                claim: "pgvector would lock our schema".into(),
788                supports: "rejected:pgvector".into(),
789                check: None,
790            },
791        ],
792        status: "live".into(),
793        held_since: "".into(),
794        blame: "Wang Yu".into(),
795        authority: None,
796        jurisdiction: None,
797        round_id: None,
798    };
799    let case1 = Tick {
800        id: String::new(),
801        parent_id: "7b21f0a4c8de".into(),
802        observe: "multi-pod restore-safety counter — chat-room R2289→R2290".into(),
803        decision: "restore-safety counter DB-backed; reject Redis".into(),
804        grounds: vec![
805            Ground {
806                claim: "Argus introduces no Redis; multi-pod coord via existing DB".into(),
807                supports: "chosen".into(),
808                check: Some(Check::Test {
809                    reference: "pytest tests/test_redis_absent.py".into(),
810                    verified_at_sha: "d308afac1b2c3d4e5f60718293a4b5c6d7e8f901".into(),
811                    counter_test: Some(
812                        "pytest tests/test_redis_absent.py::test_redis_injection_flips_red".into(),
813                    ),
814                    liveness: Liveness {
815                        platforms: vec!["linux-ci".into()],
816                        triggered_by: vec!["pyproject.toml".into()],
817                        surfaces: vec!["pyproject-deps".into()],
818                    },
819                }),
820            },
821            Ground {
822                claim: "team still wants 0-Redis posture".into(),
823                supports: "chosen".into(),
824                check: Some(Check::Person {
825                    reference: "Q3 infra review".into(),
826                }),
827            },
828            Ground {
829                claim: "Redis would add a new infra dependency".into(),
830                supports: "rejected:Redis".into(),
831                check: None,
832            },
833        ],
834        status: "live".into(),
835        held_since: "".into(),
836        blame: "Wang Yu".into(),
837        authority: None,
838        jurisdiction: None,
839        round_id: None,
840    };
841    // A harvested binding: case1's first ground with counter_test omitted (None). Pins that
842    // omit-on-None keeps every harvested id byte-stable — moving it would mean the payload changed.
843    let mut harvested = case1.clone();
844    if let Some(Check::Test { counter_test, .. }) = &mut harvested.grounds[0].check {
845        *counter_test = None;
846    }
847    let mut ok = true;
848    for (name, t, want) in [
849        ("genesis", &genesis, "e2b337f53a1f"),
850        ("case1", &case1, "638c47b0c9dd"),
851        ("harvested", &harvested, "0cf784b51331"),
852    ] {
853        let got = compute_id(t);
854        let pass = got == want;
855        ok &= pass;
856        println!(
857            "{} {name}: {got} (want {want})",
858            if pass { "✓" } else { "✗" }
859        );
860    }
861    if ok {
862        ExitCode::SUCCESS
863    } else {
864        ExitCode::FAILURE
865    }
866}