ccd-cli 1.0.0-alpha.9

Bootstrap and validate Continuous Context Development repositories
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
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::path::Path;

use anyhow::Result;
use serde::Serialize;

use crate::paths::state::StateLayout;
use crate::state::compiled as compiled_state;
use crate::state::projection_metadata;
use crate::state::reference_paths::{collect_prose_path_candidates, extract_key_files_candidates};
use crate::state::runtime as runtime_state;

pub(crate) const REFERENTIAL_INTEGRITY_AXIS: &str = "referential_integrity";
pub(crate) const PROJECTION_FRESHNESS_AXIS: &str = "projection_freshness";
pub(crate) const CROSS_ARTIFACT_AGREEMENT_AXIS: &str = "cross_artifact_agreement";
pub(crate) const MEMORY_COHERENCE_AXIS: &str = "memory_coherence";

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ConsistencyStatus {
    Aligned,
    Drift,
    NoSignal,
}

impl ConsistencyStatus {
    pub(crate) fn is_drift(self) -> bool {
        matches!(self, Self::Drift)
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ConsistencyAggregateStatus {
    Aligned,
    Drift,
    NoSignal,
}

#[derive(Clone, Debug, Serialize)]
pub(crate) struct ConsistencyAxis {
    pub(crate) id: &'static str,
    pub(crate) status: ConsistencyStatus,
    pub(crate) summary: String,
    pub(crate) evidence: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) recommended_correction: Option<String>,
}

#[derive(Clone, Debug, Serialize)]
pub(crate) struct ConsistencyEvaluation {
    pub(crate) status: ConsistencyAggregateStatus,
    pub(crate) summary: String,
    pub(crate) evidence: Vec<String>,
    pub(crate) recommended_corrections: Vec<String>,
    pub(crate) axes: Vec<ConsistencyAxis>,
}

impl ConsistencyEvaluation {
    pub(crate) fn has_drift(&self) -> bool {
        matches!(self.status, ConsistencyAggregateStatus::Drift)
    }
}

pub(crate) struct ConsistencyInputs<'a> {
    pub(crate) repo_root: &'a Path,
    pub(crate) layout: &'a StateLayout,
    pub(crate) locality_id: &'a str,
    pub(crate) handoff: &'a runtime_state::RuntimeHandoffState,
    pub(crate) checkpoint: Option<&'a runtime_state::RuntimeCheckpointState>,
    pub(crate) include_checkpoint_references: bool,
    pub(crate) source_fingerprint: &'a str,
    pub(crate) current_digests: &'a compiled_state::ProjectionDigests,
    pub(crate) latest_observation: Option<&'a projection_metadata::ProjectionObservation>,
}

pub(crate) fn load_latest_projection_observation(
    layout: &StateLayout,
) -> Result<Option<projection_metadata::ProjectionObservation>> {
    Ok(projection_metadata::load_for_layout(layout)?
        .and_then(|metadata| metadata.observations.into_iter().last()))
}

pub(crate) fn evaluate(
    repo_root: &Path,
    layout: &StateLayout,
    locality_id: &str,
) -> Result<ConsistencyEvaluation> {
    let runtime = runtime_state::load_runtime_state(repo_root, layout, locality_id)?;
    let compiled =
        compiled_state::preview_for_target(&runtime, compiled_state::ProjectionTarget::Session)?;
    let current_digests = compiled
        .projection_digests
        .clone()
        .unwrap_or_else(|| compiled_state::compute_projection_digests(&compiled));
    let latest_observation = load_latest_projection_observation(layout)?;

    evaluate_with_inputs(ConsistencyInputs {
        repo_root,
        layout,
        locality_id,
        handoff: &runtime.state.handoff,
        checkpoint: runtime.recovery.state.checkpoint.as_ref(),
        include_checkpoint_references: true,
        source_fingerprint: &compiled.source_fingerprint,
        current_digests: &current_digests,
        latest_observation: latest_observation.as_ref(),
    })
}

pub(crate) fn evaluate_with_inputs(inputs: ConsistencyInputs<'_>) -> Result<ConsistencyEvaluation> {
    summarize_axes(vec![
        evaluate_referential_integrity(
            inputs.repo_root,
            inputs.handoff,
            inputs.checkpoint,
            inputs.include_checkpoint_references,
        ),
        evaluate_projection_freshness(
            inputs.layout,
            inputs.source_fingerprint,
            inputs.latest_observation,
        ),
        evaluate_cross_artifact_agreement(
            inputs.layout,
            inputs.source_fingerprint,
            inputs.current_digests,
            inputs.latest_observation,
        ),
        evaluate_memory_coherence(inputs.layout, inputs.locality_id)?,
    ])
}

fn summarize_axes(axes: Vec<ConsistencyAxis>) -> Result<ConsistencyEvaluation> {
    let drifted = axes
        .iter()
        .filter(|axis| axis.status == ConsistencyStatus::Drift)
        .collect::<Vec<_>>();
    if !drifted.is_empty() {
        let mut recommended_corrections = Vec::new();
        for correction in drifted
            .iter()
            .filter_map(|axis| axis.recommended_correction.clone())
        {
            if !recommended_corrections.contains(&correction) {
                recommended_corrections.push(correction);
            }
        }
        return Ok(ConsistencyEvaluation {
            status: ConsistencyAggregateStatus::Drift,
            summary: format!(
                "{} consistency axis(es) need explicit repair before CCD continuity state is treated as clean.",
                drifted.len()
            ),
            evidence: drifted
                .iter()
                .flat_map(|axis| axis.evidence.clone())
                .collect(),
            recommended_corrections,
            axes,
        });
    }

    if axes
        .iter()
        .all(|axis| axis.status == ConsistencyStatus::NoSignal)
    {
        return Ok(ConsistencyEvaluation {
            status: ConsistencyAggregateStatus::NoSignal,
            summary: "CCD has no deterministic artifact-consistency signal beyond the currently available local state."
                .to_owned(),
            evidence: Vec::new(),
            recommended_corrections: Vec::new(),
            axes,
        });
    }

    Ok(ConsistencyEvaluation {
        status: ConsistencyAggregateStatus::Aligned,
        summary: "No confirmed artifact-consistency drift was detected from the available CCD-local signals."
            .to_owned(),
        evidence: Vec::new(),
        recommended_corrections: Vec::new(),
        axes,
    })
}

fn evaluate_referential_integrity(
    repo_root: &Path,
    handoff: &runtime_state::RuntimeHandoffState,
    checkpoint: Option<&runtime_state::RuntimeCheckpointState>,
    include_checkpoint_references: bool,
) -> ConsistencyAxis {
    let canonical_root = match repo_root.canonicalize() {
        Ok(root) => root,
        Err(_) => {
            return drift_axis(
                REFERENTIAL_INTEGRITY_AXIS,
                "Surfaced continuity references could not be validated against the repo root.",
                vec![format!(
                    "Could not canonicalize repo root for referential-integrity validation: {}",
                    repo_root.display()
                )],
                "Re-run the consistency audit from a valid repo checkout so CCD can validate surfaced path references against the repo root.",
            );
        }
    };

    let mut audit = ReferenceAuditSummary::default();
    audit_handoff_references(repo_root, &canonical_root, handoff, &mut audit);
    if include_checkpoint_references {
        audit_checkpoint_references(repo_root, &canonical_root, checkpoint, &mut audit);
    }

    let scope_label = if include_checkpoint_references {
        "current handoff or recovery checkpoint"
    } else {
        "current handoff"
    };

    if audit.checked == 0 {
        return no_signal_axis(
            REFERENTIAL_INTEGRITY_AXIS,
            format!("No surfaced repo-local path references were present in the {scope_label}."),
            Vec::new(),
        );
    }

    let mut evidence = vec![format!(
        "Audited {} surfaced path reference(s) against repo root `{}`.",
        audit.checked,
        repo_root.display()
    )];

    if audit.missing.is_empty() && audit.outside.is_empty() {
        evidence.push(
            "All audited path references resolved to repo-local files or directories.".to_owned(),
        );
        return aligned_axis(
            REFERENTIAL_INTEGRITY_AXIS,
            "Every surfaced path reference resolves to a repo-local path under the repo root.",
            evidence,
        );
    }

    for (artifact, entries) in &audit.missing {
        evidence.push(format!(
            "{}: {} missing path reference(s):",
            reference_artifact_label(artifact),
            entries.len()
        ));
        evidence.extend(
            entries
                .iter()
                .map(|path| format!("- {}", markdown_list_literal(path))),
        );
    }

    for (artifact, entries) in &audit.outside {
        evidence.push(format!(
            "{}: {} non-local path reference(s):",
            reference_artifact_label(artifact),
            entries.len()
        ));
        evidence.extend(
            entries
                .iter()
                .map(|(path, reason)| format!("- {} ({reason})", markdown_list_literal(path))),
        );
    }

    drift_axis(
        REFERENTIAL_INTEGRITY_AXIS,
        "Surfaced continuity artifacts still reference paths that are missing or live outside the repo root.",
        evidence,
        if include_checkpoint_references {
            "Rewrite the surfaced handoff or recovery state so its file references stay repo-local and still exist on disk, then persist the corrected continuity state before the next session inherits it."
        } else {
            "Rewrite the surfaced handoff so its file references stay repo-local and still exist on disk, then persist the corrected continuity state before the next session inherits it."
        },
    )
}

fn evaluate_projection_freshness(
    layout: &StateLayout,
    current_source_fingerprint: &str,
    latest_observation: Option<&projection_metadata::ProjectionObservation>,
) -> ConsistencyAxis {
    let metadata_path = layout.state_db_path().display().to_string();
    let Some(latest_observation) = latest_observation else {
        return no_signal_axis(
            PROJECTION_FRESHNESS_AXIS,
            "No projection observation has been recorded yet for this clone-local state DB.",
            vec![format!(
                "Projection metadata is absent at {}.",
                metadata_path
            )],
        );
    };

    let evidence = vec![
        format!("Projection metadata path: {metadata_path}"),
        format!(
            "Latest recorded source fingerprint: {}",
            latest_observation.source_fingerprint
        ),
        format!("Current source fingerprint: {current_source_fingerprint}"),
    ];

    if latest_observation.source_fingerprint == current_source_fingerprint {
        return aligned_axis(
            PROJECTION_FRESHNESS_AXIS,
            "The latest recorded projection observation matches the current canonical source fingerprint.",
            evidence,
        );
    }

    drift_axis(
        PROJECTION_FRESHNESS_AXIS,
        "The latest recorded projection observation is stale relative to the current canonical continuity state.",
        evidence,
        "Record a fresh projection observation before relying on delta or projection telemetry derived from older continuity state.",
    )
}

fn evaluate_cross_artifact_agreement(
    layout: &StateLayout,
    current_source_fingerprint: &str,
    current_digests: &compiled_state::ProjectionDigests,
    latest_observation: Option<&projection_metadata::ProjectionObservation>,
) -> ConsistencyAxis {
    let metadata_path = layout.state_db_path().display().to_string();
    let Some(latest_observation) = latest_observation else {
        return no_signal_axis(
            CROSS_ARTIFACT_AGREEMENT_AXIS,
            "No projection observation is available yet, so cross-artifact agreement cannot be checked.",
            vec![format!(
                "Projection metadata is absent at {}.",
                metadata_path
            )],
        );
    };

    if latest_observation.source_fingerprint != current_source_fingerprint {
        return no_signal_axis(
            CROSS_ARTIFACT_AGREEMENT_AXIS,
            "Cross-artifact agreement is deferred until projection freshness is restored.",
            vec![format!(
                "Latest recorded fingerprint `{}` does not match current fingerprint `{}`.",
                latest_observation.source_fingerprint, current_source_fingerprint
            )],
        );
    }

    let Some(recorded_digests) = latest_observation.projection_digests.as_ref() else {
        return no_signal_axis(
            CROSS_ARTIFACT_AGREEMENT_AXIS,
            "The latest projection observation did not record projection digests, so cross-artifact agreement cannot be checked.",
            vec![format!(
                "Projection metadata path: {}",
                metadata_path
            )],
        );
    };

    let mut compared = 0usize;
    let mut mismatches = Vec::new();
    compare_digest_surface(
        "effective_memory",
        &recorded_digests.effective_memory,
        &current_digests.effective_memory,
        &mut compared,
        &mut mismatches,
    );
    compare_digest_surface(
        "handoff",
        &recorded_digests.handoff,
        &current_digests.handoff,
        &mut compared,
        &mut mismatches,
    );
    compare_digest_surface(
        "execution_gates",
        &recorded_digests.execution_gates,
        &current_digests.execution_gates,
        &mut compared,
        &mut mismatches,
    );
    compare_digest_surface(
        "escalation",
        &recorded_digests.escalation,
        &current_digests.escalation,
        &mut compared,
        &mut mismatches,
    );
    compare_digest_surface(
        "recovery",
        &recorded_digests.recovery,
        &current_digests.recovery,
        &mut compared,
        &mut mismatches,
    );
    compare_digest_surface(
        "git_state",
        &recorded_digests.git_state,
        &current_digests.git_state,
        &mut compared,
        &mut mismatches,
    );
    compare_digest_surface(
        "session_state",
        &recorded_digests.session_state,
        &current_digests.session_state,
        &mut compared,
        &mut mismatches,
    );

    if compared == 0 {
        return no_signal_axis(
            CROSS_ARTIFACT_AGREEMENT_AXIS,
            "The latest projection observation did not carry any non-empty surface digests that can be compared for agreement.",
            vec![format!(
                "Projection metadata path: {}",
                metadata_path
            )],
        );
    }

    let mut evidence = vec![
        format!("Projection metadata path: {metadata_path}"),
        format!("Compared {compared} recorded projection digest surface(s)."),
    ];

    if mismatches.is_empty() {
        return aligned_axis(
            CROSS_ARTIFACT_AGREEMENT_AXIS,
            "Fresh recorded projection digests agree with the current computed continuity surfaces.",
            evidence,
        );
    }

    evidence.extend(mismatches);
    drift_axis(
        CROSS_ARTIFACT_AGREEMENT_AXIS,
        "A fresh recorded projection disagrees with the current computed continuity surfaces.",
        evidence,
        "Rebuild the derived projection from canonical state and replace the conflicting recorded digests before relying on artifact agreement.",
    )
}

fn evaluate_memory_coherence(layout: &StateLayout, locality_id: &str) -> Result<ConsistencyAxis> {
    let profile_ledger = runtime_state::load_profile_memory_automation_ledger(layout)?;
    let locality_ledger = runtime_state::load_repo_memory_automation_ledger(layout, locality_id)?;
    let total_records = profile_ledger.len() + locality_ledger.len();
    if total_records == 0 {
        return Ok(no_signal_axis(
            MEMORY_COHERENCE_AXIS,
            "No recorded memory-automation signal is available yet for memory-coherence validation.",
            vec![
                format!(
                    "Profile runtime state path: {}",
                    layout.profile_runtime_state_path().display()
                ),
                format!(
                    "Locality runtime state path: {}",
                    layout.locality_runtime_state_path(locality_id)?.display()
                ),
            ],
        ));
    }

    let mut evidence = vec![format!(
        "Audited {} memory-automation record(s) across profile and locality runtime state.",
        total_records
    )];
    let mut contradictions = Vec::new();
    contradictions.extend(collect_memory_contradictions("profile", &profile_ledger));
    contradictions.extend(collect_memory_contradictions("locality", &locality_ledger));

    if contradictions.is_empty() {
        return Ok(aligned_axis(
            MEMORY_COHERENCE_AXIS,
            "Recorded memory-automation signals show no active contradiction count in remembered guidance.",
            evidence,
        ));
    }

    evidence.extend(contradictions);
    Ok(drift_axis(
        MEMORY_COHERENCE_AXIS,
        "Recorded memory-automation signals detected contradictory remembered guidance.",
        evidence,
        "Review the contradicting memory entries and compact, supersede, or rewrite them so surfaced continuity does not inherit conflicting guidance.",
    ))
}

fn compare_digest_surface(
    surface: &str,
    recorded: &str,
    current: &str,
    compared: &mut usize,
    mismatches: &mut Vec<String>,
) {
    if recorded.is_empty() || current.is_empty() {
        return;
    }
    *compared += 1;
    if recorded != current {
        mismatches.push(format!(
            "Surface `{surface}` digest mismatch: recorded `{recorded}`, current `{current}`."
        ));
    }
}

fn collect_memory_contradictions(
    scope: &str,
    ledger: &runtime_state::RuntimeMemoryAutomationLedger,
) -> Vec<String> {
    ledger
        .iter()
        .filter(|(_, record)| record.contradiction_count > 0)
        .map(|(dedupe_key, record)| {
            format!(
                "{scope}: `{dedupe_key}` contradiction_count={} observations={} sessions={} localities={} structural_signal={}",
                record.contradiction_count,
                record.observation_count,
                record.observed_session_count(),
                record.observed_locality_count(),
                record.has_structural_signal
            )
        })
        .collect()
}

#[derive(Default)]
struct ReferenceAuditSummary {
    checked: usize,
    missing: BTreeMap<&'static str, BTreeSet<String>>,
    outside: BTreeMap<&'static str, BTreeMap<String, &'static str>>,
}

impl ReferenceAuditSummary {
    fn record_missing(&mut self, artifact: &'static str, path: &str) {
        self.missing
            .entry(artifact)
            .or_default()
            .insert(path.to_owned());
    }

    fn record_outside(&mut self, artifact: &'static str, path: &str, reason: &'static str) {
        self.outside
            .entry(artifact)
            .or_default()
            .insert(path.to_owned(), reason);
    }
}

fn audit_handoff_references(
    repo_root: &Path,
    canonical_root: &Path,
    handoff: &runtime_state::RuntimeHandoffState,
    audit: &mut ReferenceAuditSummary,
) {
    for item in handoff
        .key_files
        .iter()
        .filter(|item| item.lifecycle.is_active())
    {
        let candidates = extract_key_files_candidates(&item.text);
        if candidates.is_empty() {
            continue;
        }
        audit.checked = audit.checked.saturating_add(1);
        match classify_reference_entry(repo_root, canonical_root, &candidates) {
            ReferenceEntryOutcome::Resolved => {}
            ReferenceEntryOutcome::Missing => audit.record_missing("handoff", &item.text),
            ReferenceEntryOutcome::OutsideRepo(reason) => {
                audit.record_outside("handoff", &item.text, reason)
            }
        }
    }

    let mut prose_seen: HashSet<String> = HashSet::new();
    let prose_fields: [&[runtime_state::RuntimeHandoffItem]; 3] = [
        &handoff.immediate_actions,
        &handoff.operational_guardrails,
        &handoff.definition_of_done,
    ];
    for field in prose_fields {
        for item in field.iter().filter(|item| item.lifecycle.is_active()) {
            let mut tokens = Vec::new();
            collect_prose_path_candidates(&item.text, &mut tokens);
            for token in tokens {
                if !prose_seen.insert(token.clone()) {
                    continue;
                }
                audit.checked = audit.checked.saturating_add(1);
                match classify_reference(repo_root, canonical_root, &token) {
                    ReferenceOutcome::Resolved => {}
                    ReferenceOutcome::Missing => audit.record_missing("handoff", &token),
                    ReferenceOutcome::OutsideRepo { reason } => {
                        audit.record_outside("handoff", &token, reason)
                    }
                }
            }
        }
    }
}

fn audit_checkpoint_references(
    repo_root: &Path,
    canonical_root: &Path,
    checkpoint: Option<&runtime_state::RuntimeCheckpointState>,
    audit: &mut ReferenceAuditSummary,
) {
    let Some(checkpoint) = checkpoint else {
        return;
    };

    for entry in &checkpoint.key_files {
        let candidates = extract_key_files_candidates(entry);
        if candidates.is_empty() {
            continue;
        }
        audit.checked = audit.checked.saturating_add(1);
        match classify_reference_entry(repo_root, canonical_root, &candidates) {
            ReferenceEntryOutcome::Resolved => {}
            ReferenceEntryOutcome::Missing => {
                audit.record_missing("recovery_checkpoint", entry);
            }
            ReferenceEntryOutcome::OutsideRepo(reason) => {
                audit.record_outside("recovery_checkpoint", entry, reason);
            }
        }
    }

    let mut prose_seen: HashSet<String> = HashSet::new();
    for action in &checkpoint.immediate_actions {
        let mut tokens = Vec::new();
        collect_prose_path_candidates(action, &mut tokens);
        for token in tokens {
            if !prose_seen.insert(token.clone()) {
                continue;
            }
            audit.checked = audit.checked.saturating_add(1);
            match classify_reference(repo_root, canonical_root, &token) {
                ReferenceOutcome::Resolved => {}
                ReferenceOutcome::Missing => {
                    audit.record_missing("recovery_checkpoint", &token);
                }
                ReferenceOutcome::OutsideRepo { reason } => {
                    audit.record_outside("recovery_checkpoint", &token, reason);
                }
            }
        }
    }
}

fn reference_artifact_label(artifact: &str) -> &'static str {
    match artifact {
        "handoff" => "Handoff",
        "recovery_checkpoint" => "Recovery checkpoint",
        _ => "Artifact",
    }
}

#[derive(Clone, Debug)]
enum ReferenceOutcome {
    Resolved,
    Missing,
    OutsideRepo { reason: &'static str },
}

fn classify_reference(
    repo_root: &Path,
    canonical_root: &Path,
    candidate: &str,
) -> ReferenceOutcome {
    let path = Path::new(candidate);
    if path.is_absolute() {
        return ReferenceOutcome::OutsideRepo {
            reason: "absolute path",
        };
    }
    if path
        .components()
        .any(|component| matches!(component, std::path::Component::ParentDir))
    {
        return ReferenceOutcome::OutsideRepo {
            reason: "parent-directory traversal escapes the repo root",
        };
    }
    let target = repo_root.join(path);
    if !target.exists() {
        return ReferenceOutcome::Missing;
    }
    let canonical_target = match target.canonicalize() {
        Ok(target) => target,
        Err(_) => return ReferenceOutcome::Missing,
    };
    if canonical_target.starts_with(canonical_root) {
        ReferenceOutcome::Resolved
    } else {
        ReferenceOutcome::OutsideRepo {
            reason: "symlink target escapes the repo root",
        }
    }
}

enum ReferenceEntryOutcome {
    Resolved,
    Missing,
    OutsideRepo(&'static str),
}

fn classify_reference_entry(
    repo_root: &Path,
    canonical_root: &Path,
    candidates: &[String],
) -> ReferenceEntryOutcome {
    let mut last_outside = None;
    for candidate in candidates {
        match classify_reference(repo_root, canonical_root, candidate) {
            ReferenceOutcome::Resolved => return ReferenceEntryOutcome::Resolved,
            ReferenceOutcome::Missing => {}
            ReferenceOutcome::OutsideRepo { reason } => last_outside = Some(reason),
        }
    }
    match last_outside {
        Some(reason) => ReferenceEntryOutcome::OutsideRepo(reason),
        None => ReferenceEntryOutcome::Missing,
    }
}

fn markdown_list_literal(text: &str) -> String {
    if text.contains('`') {
        text.to_owned()
    } else {
        format!("`{text}`")
    }
}

fn aligned_axis(
    id: &'static str,
    summary: impl Into<String>,
    evidence: Vec<String>,
) -> ConsistencyAxis {
    ConsistencyAxis {
        id,
        status: ConsistencyStatus::Aligned,
        summary: summary.into(),
        evidence,
        recommended_correction: None,
    }
}

fn no_signal_axis(
    id: &'static str,
    summary: impl Into<String>,
    evidence: Vec<String>,
) -> ConsistencyAxis {
    ConsistencyAxis {
        id,
        status: ConsistencyStatus::NoSignal,
        summary: summary.into(),
        evidence,
        recommended_correction: None,
    }
}

fn drift_axis(
    id: &'static str,
    summary: impl Into<String>,
    evidence: Vec<String>,
    recommended_correction: impl Into<String>,
) -> ConsistencyAxis {
    ConsistencyAxis {
        id,
        status: ConsistencyStatus::Drift,
        summary: summary.into(),
        evidence,
        recommended_correction: Some(recommended_correction.into()),
    }
}