memstead-base 0.27.1

Engine internals for Memstead — store, parser, validators, mem lifecycle, workspace policy. Published as a dependency of the memstead-cli and memstead-mcp products, not as a supported library — pre-1.0, no API stability promise.
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
//! The strict axis: `memstead health --strict` as the graph's referee.
//!
//! Strict evaluates one fixed set of health includes (the referee's set,
//! [`STRICT_INCLUDES`]) and reads three things off the composed report:
//!
//! * **Entity findings**, each a health condition on one entity
//!   ([`HEALTH_CONDITIONS`]): the three dangling conditions, an
//!   unresolved stub, an ungranted cross-mem edge, a missing required
//!   outgoing edge, an unsatisfied constraint, a section-format violation,
//!   a warn-level signal. Each may be **acknowledged**: a check record on
//!   that entity whose `finding.code` names the condition, with verdict
//!   `failed` and a method naming the owner and the plan that closes it.
//!   An acknowledged finding is reported and does not fail the run.
//! * **Configuration defects**, workspace- or mem-level and never
//!   acknowledgeable: a schema pin that disagrees with its mem, a rotted
//!   or drifted schema package, a mount that resolves to nothing, an
//!   unreadable anchors sidecar, a schema format defect.
//! * **Stale acknowledgements** ([`STALE_ACKNOWLEDGEMENT_CODE`]): an
//!   acknowledgement that still stands while its finding no longer occurs.
//!   That is a finding in its own right, so the acknowledged set shrinks
//!   with the repairs instead of rotting into an allowlist.
//!
//! The run fails on any unacknowledged finding, any configuration defect
//! and any stale acknowledgement. Stale entities, drifted anchors and
//! generation hints stay advisory: they are on the report, never on the
//! verdict.
//!
//! The finding-code namespace: a code spelled `UPPER_SNAKE` claims to
//! name an engine condition, so [`crate::Engine::record_check_with`]
//! refuses one that is not in [`HEALTH_CONDITIONS`]; a checker's own
//! vocabulary keeps any other spelling (`hidden-premise`) and never
//! acknowledges anything.

use std::collections::BTreeMap;

use serde::Serialize;

use crate::check::{CheckLedger, CheckRecord};

/// The includes strict evaluates, whatever `--include` says.
pub const STRICT_INCLUDES: &[&str] = &[
    "integrity",
    "anchors",
    "stale",
    "missing_required_outgoing",
    "constraints",
    "signals",
];

/// The code of a standing acknowledgement whose finding stopped occurring.
pub const STALE_ACKNOWLEDGEMENT_CODE: &str = "STALE_ACKNOWLEDGEMENT";

/// The condition codes an entity finding carries, and the only codes an
/// acknowledgement may name. The dangling conditions are the same three
/// [`crate::ops::DanglingLinkKind::ALL_CODES`] lists.
pub const HEALTH_CONDITIONS: &[&str] = &[
    "DANGLING_LINK_TARGET_MISSING",
    "DANGLING_LINK_NOT_RELATED",
    "DANGLING_RELATION_TARGET_MISSING",
    "UNRESOLVED_STUB",
    "CROSS_MEM_EDGE_UNGRANTED",
    "MISSING_REQUIRED_OUTGOING",
    "CONSTRAINT_UNSATISFIED",
    "SECTION_FORMAT_VIOLATION",
    "SIGNAL_WARN",
];

/// Configuration defects strict refuses on; never acknowledgeable because
/// none of them belongs to one entity.
pub const CONFIGURATION_CONDITIONS: &[&str] = &[
    "SCHEMA_AUTHORING_SOURCE_MISSING",
    "SCHEMA_AUTHORING_SOURCE_DIVERGED",
    "SCHEMA_PIN_MISMATCH",
    "SCHEMA_UNSTAMPED_SOURCE_ROT",
    "MOUNT_UNBACKED",
    "ANCHORS_SIDECAR_UNREADABLE",
    "SCHEMA_FORMAT_DEFECT",
];

/// Whether `code` is a health condition an acknowledgement may name.
pub fn is_health_condition(code: &str) -> bool {
    HEALTH_CONDITIONS.contains(&code)
}

/// Whether `code` is spelled in the engine's namespace: `UPPER_SNAKE`,
/// a letter first, three characters or more. A checker's own vocabulary
/// takes any other spelling.
pub fn is_engine_code_shape(code: &str) -> bool {
    code.len() >= 3
        && code.chars().next().is_some_and(|c| c.is_ascii_uppercase())
        && code
            .chars()
            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
}

/// The check record that acknowledges a finding, as the report carries it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Acknowledgement {
    /// Unix epoch seconds of the record.
    pub ts: u64,
    /// When the check was made, as the caller stated it; absent when
    /// nobody did.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub observed_at: Option<u64>,
    /// The caller-declared identity, when one was declared.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identity: Option<String>,
    /// The recorded actor (the transport).
    pub actor: String,
    /// The declared role, or `unspecified`.
    pub role: String,
    /// The owner and the closing plan, as the checker wrote them.
    pub method: String,
    /// The finding message the checker attached.
    pub message: String,
}

impl Acknowledgement {
    fn from_record(r: &CheckRecord) -> Self {
        Self {
            ts: r.ts,
            observed_at: r.observed_at,
            identity: r.identity.clone(),
            actor: r.actor.clone(),
            role: r.role.clone(),
            method: r.method.clone().unwrap_or_default(),
            message: r
                .finding
                .as_ref()
                .map(|f| f.message.clone())
                .unwrap_or_default(),
        }
    }
}

/// One entity finding on the strict axis.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrictFinding {
    /// One of [`HEALTH_CONDITIONS`].
    pub code: String,
    /// The mem-qualified entity id.
    pub entity: String,
    /// The mem the entity lives in.
    pub mem: String,
    /// What the report said about it, verbatim from the section it came from.
    pub detail: serde_json::Value,
    /// Whether a standing acknowledgement covers it.
    pub acknowledged: bool,
    /// The acknowledgement, when one stands.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub acknowledgement: Option<Acknowledgement>,
}

/// One configuration defect on the strict axis.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ConfigurationDefect {
    /// One of [`CONFIGURATION_CONDITIONS`].
    pub code: String,
    /// The mem it concerns, when one.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mem: Option<String>,
    /// The report's own row for it.
    pub detail: serde_json::Value,
}

/// A standing acknowledgement whose finding no longer occurs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StaleAcknowledgement {
    /// Always [`STALE_ACKNOWLEDGEMENT_CODE`].
    pub code: &'static str,
    /// The entity the acknowledgement names.
    pub entity: String,
    /// The condition it acknowledged.
    pub condition: String,
    /// The record itself, so the reader can find and withdraw it.
    pub acknowledgement: Acknowledgement,
}

/// The strict axis, as `health --strict` serves it under `strict`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct StrictAxis {
    /// The includes strict evaluated ([`STRICT_INCLUDES`]).
    pub evaluated: Vec<&'static str>,
    /// The condition vocabulary an acknowledgement may name.
    pub conditions: Vec<&'static str>,
    /// Every entity finding, acknowledged or not.
    pub findings: Vec<StrictFinding>,
    /// Configuration defects, never acknowledgeable.
    pub configuration: Vec<ConfigurationDefect>,
    /// Acknowledgements whose finding no longer occurs.
    pub stale_acknowledgements: Vec<StaleAcknowledgement>,
    /// Violations by code, in code order: unacknowledged findings,
    /// configuration defects, stale acknowledgements.
    pub violations_by_code: BTreeMap<String, usize>,
    /// The total the exit code turns on.
    pub violations: usize,
    /// Acknowledged findings, for the reader.
    pub acknowledged: usize,
}

impl StrictAxis {
    /// One line naming every violating code with its count, for the refusal.
    pub fn summary(&self) -> String {
        self.violations_by_code
            .iter()
            .map(|(code, n)| format!("{code}: {n}"))
            .collect::<Vec<_>>()
            .join(", ")
    }
}

/// A strict health run that found violations: the refusal a caller gating
/// on `--strict` raises, after the report.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("strict mode: {violations} violation(s) ({summary})")]
pub struct StrictViolations {
    pub violations: usize,
    pub violations_by_code: BTreeMap<String, usize>,
    pub acknowledged: usize,
    pub stale_acknowledgements: usize,
    summary: String,
}

impl StrictViolations {
    pub fn code(&self) -> &'static str {
        "HEALTH_STRICT_VIOLATIONS"
    }

    pub fn details(&self) -> serde_json::Value {
        serde_json::json!({
            "violations": self.violations,
            "violations_by_code": self.violations_by_code,
            "acknowledged": self.acknowledged,
            "stale_acknowledgements": self.stale_acknowledgements,
        })
    }

    /// The refusal for a strict axis as the health report serves it
    /// (`strict` in the report JSON), when it counts any violation.
    pub fn from_axis_json(axis: &serde_json::Value) -> Option<Self> {
        let violations = axis["violations"].as_u64().unwrap_or(0) as usize;
        if violations == 0 {
            return None;
        }
        let violations_by_code: BTreeMap<String, usize> = axis["violations_by_code"]
            .as_object()
            .map(|m| {
                m.iter()
                    .map(|(k, v)| (k.clone(), v.as_u64().unwrap_or(0) as usize))
                    .collect()
            })
            .unwrap_or_default();
        let summary = violations_by_code
            .iter()
            .map(|(code, n)| format!("{code}: {n}"))
            .collect::<Vec<_>>()
            .join(", ");
        Some(StrictViolations {
            violations,
            violations_by_code,
            acknowledged: axis["acknowledged"].as_u64().unwrap_or(0) as usize,
            stale_acknowledgements: axis["stale_acknowledgements"]
                .as_array()
                .map_or(0, |a| a.len()),
            summary,
        })
    }
}

/// The mem half of a mem-qualified entity id (`mem--slug`, where the mem
/// may carry slashes but never a double dash).
fn mem_of(entity_id: &str) -> &str {
    entity_id.split_once("--").map_or(entity_id, |(m, _)| m)
}

/// Read the strict axis off a composed health report (every include in
/// [`STRICT_INCLUDES`] already rendered into `report`) and the workspace
/// check ledger. `mem_filter` scopes the stale-acknowledgement walk the
/// way the report itself was scoped.
pub fn compose_strict_axis(
    report: &serde_json::Map<String, serde_json::Value>,
    ledger: Option<&CheckLedger>,
    mem_filter: Option<&str>,
) -> StrictAxis {
    let mut findings: Vec<StrictFinding> = Vec::new();
    let mut configuration: Vec<ConfigurationDefect> = Vec::new();

    let arr = |key: &str| -> Vec<serde_json::Value> {
        report
            .get(key)
            .and_then(|v| v.as_array())
            .cloned()
            .unwrap_or_default()
    };
    let s = |v: &serde_json::Value, key: &str| -> String {
        v.get(key)
            .and_then(|x| x.as_str())
            .unwrap_or_default()
            .to_string()
    };

    // Integrity findings: the consistency codes only; the conformance
    // axis reports beside them and stays advisory here, as it always was.
    for f in arr("findings") {
        let code = s(&f, "code");
        if is_health_condition(&code) {
            let id = s(&f, "id");
            findings.push(StrictFinding {
                mem: mem_of(&id).to_string(),
                code,
                entity: id,
                detail: f.get("detail").cloned().unwrap_or(serde_json::Value::Null),
                acknowledged: false,
                acknowledgement: None,
            });
        } else if code == "ANCHORS_SIDECAR_UNREADABLE" {
            configuration.push(ConfigurationDefect {
                code,
                mem: f
                    .get("detail")
                    .and_then(|d| d.get("mem"))
                    .and_then(|m| m.as_str())
                    .map(str::to_string),
                detail: f,
            });
        }
    }
    for m in arr("missing_required_outgoing") {
        let id = s(&m, "id");
        findings.push(StrictFinding {
            code: "MISSING_REQUIRED_OUTGOING".to_string(),
            mem: s(&m, "mem"),
            entity: id,
            detail: m.get("missing").cloned().unwrap_or(serde_json::Value::Null),
            acknowledged: false,
            acknowledgement: None,
        });
    }
    for c in arr("constraints") {
        let id = s(&c, "id");
        let mem = s(&c, "mem");
        if let Some(v) = c.get("violations").and_then(|x| x.as_array())
            && !v.is_empty()
        {
            findings.push(StrictFinding {
                code: "CONSTRAINT_UNSATISFIED".to_string(),
                mem: mem.clone(),
                entity: id.clone(),
                detail: serde_json::Value::Array(v.clone()),
                acknowledged: false,
                acknowledgement: None,
            });
        }
        if let Some(fv) = c.get("format_violations").and_then(|x| x.as_array())
            && !fv.is_empty()
        {
            findings.push(StrictFinding {
                code: "SECTION_FORMAT_VIOLATION".to_string(),
                mem,
                entity: id,
                detail: serde_json::Value::Array(fv.clone()),
                acknowledged: false,
                acknowledgement: None,
            });
        }
    }
    for d in arr("schema_format_defects") {
        configuration.push(ConfigurationDefect {
            code: "SCHEMA_FORMAT_DEFECT".to_string(),
            mem: d.get("mem").and_then(|m| m.as_str()).map(str::to_string),
            detail: d,
        });
    }
    if let Some(entities) = report
        .get("signals")
        .and_then(|sg| sg.get("entities"))
        .and_then(|e| e.as_array())
    {
        for e in entities {
            // A row lists every signal above `none`; the entity is a
            // warn-level finding when any of them sits at `warn`.
            let warn: Vec<serde_json::Value> = e
                .get("signals")
                .and_then(|x| x.as_array())
                .into_iter()
                .flatten()
                .filter(|sg| s(sg, "level") == "warn")
                .cloned()
                .collect();
            if !warn.is_empty() {
                let id = s(e, "id");
                findings.push(StrictFinding {
                    code: "SIGNAL_WARN".to_string(),
                    mem: mem_of(&id).to_string(),
                    entity: id,
                    detail: serde_json::Value::Array(warn),
                    acknowledged: false,
                    acknowledgement: None,
                });
            }
        }
    }
    if let Some(mems) = report.get("anchors").and_then(|a| a.as_object()) {
        for (mem, row) in mems {
            if let Some(cond) = row.get("condition").filter(|c| !c.is_null()) {
                configuration.push(ConfigurationDefect {
                    code: "ANCHORS_SIDECAR_UNREADABLE".to_string(),
                    mem: Some(mem.clone()),
                    detail: cond.clone(),
                });
            }
        }
    }
    for w in arr("warnings") {
        let code = s(&w, "code");
        if CONFIGURATION_CONDITIONS.contains(&code.as_str()) {
            configuration.push(ConfigurationDefect {
                mem: w
                    .get("details")
                    .and_then(|d| d.get("mem"))
                    .and_then(|m| m.as_str())
                    .map(str::to_string),
                code,
                detail: w,
            });
        }
    }
    // One row per configuration defect: the anchors axis and the integrity
    // findings both carry an unreadable sidecar.
    configuration.dedup_by(|a, b| a.code == b.code && a.mem == b.mem);

    // Standing acknowledgements: the newest record per (entity, condition)
    // that carries a health-condition finding, standing while its verdict
    // is `failed` — a later `ok` on the same condition withdraws it.
    let mut standing: BTreeMap<(String, String), Acknowledgement> = BTreeMap::new();
    let mut withdrawn: BTreeMap<(String, String), ()> = BTreeMap::new();
    if let Some(ledger) = ledger {
        for rec in ledger.in_standing_order() {
            let Some(f) = &rec.finding else { continue };
            if !is_health_condition(&f.code) {
                continue;
            }
            let key = (rec.entity.clone(), f.code.clone());
            if rec.verdict == "failed" {
                withdrawn.remove(&key);
                standing.insert(key, Acknowledgement::from_record(&rec));
            } else {
                standing.remove(&key);
                withdrawn.insert(key, ());
            }
        }
    }

    for f in &mut findings {
        if let Some(ack) = standing.get(&(f.entity.clone(), f.code.clone())) {
            f.acknowledged = true;
            f.acknowledgement = Some(ack.clone());
        }
    }
    let present: std::collections::BTreeSet<(String, String)> = findings
        .iter()
        .map(|f| (f.entity.clone(), f.code.clone()))
        .collect();
    let mut stale_acknowledgements: Vec<StaleAcknowledgement> = standing
        .into_iter()
        .filter(|(key, _)| !present.contains(key))
        .filter(|((entity, _), _)| mem_filter.is_none_or(|m| mem_of(entity) == m))
        .map(|((entity, condition), ack)| StaleAcknowledgement {
            code: STALE_ACKNOWLEDGEMENT_CODE,
            entity,
            condition,
            acknowledgement: ack,
        })
        .collect();
    stale_acknowledgements
        .sort_by(|a, b| (&a.entity, &a.condition).cmp(&(&b.entity, &b.condition)));
    findings.sort_by(|a, b| (&a.entity, &a.code).cmp(&(&b.entity, &b.code)));
    configuration.sort_by(|a, b| (&a.code, &a.mem).cmp(&(&b.code, &b.mem)));

    let mut violations_by_code: BTreeMap<String, usize> = BTreeMap::new();
    for f in findings.iter().filter(|f| !f.acknowledged) {
        *violations_by_code.entry(f.code.clone()).or_default() += 1;
    }
    for c in &configuration {
        *violations_by_code.entry(c.code.clone()).or_default() += 1;
    }
    if !stale_acknowledgements.is_empty() {
        violations_by_code.insert(
            STALE_ACKNOWLEDGEMENT_CODE.to_string(),
            stale_acknowledgements.len(),
        );
    }
    let violations = violations_by_code.values().sum();
    let acknowledged = findings.iter().filter(|f| f.acknowledged).count();

    StrictAxis {
        evaluated: STRICT_INCLUDES.to_vec(),
        conditions: HEALTH_CONDITIONS.to_vec(),
        findings,
        configuration,
        stale_acknowledgements,
        violations_by_code,
        violations,
        acknowledged,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::check::{CheckFinding, CheckRecord};
    use tempfile::TempDir;

    fn record(entity: &str, verdict: &str, code: &str, ts: u64) -> CheckRecord {
        CheckRecord {
            ts,
            observed_at: None,
            entity: entity.to_string(),
            verdict: verdict.to_string(),
            method: Some("owner: the work-down lane; plan: repairs".to_string()),
            entity_hash: "h".to_string(),
            actor: "cli".to_string(),
            client: None,
            role: "checker".to_string(),
            identity: Some("checker-1".to_string()),
            kind: None,
            schema_ref: None,
            finding: Some(CheckFinding {
                code: code.to_string(),
                section: None,
                message: "known open".to_string(),
                evidence: None,
            }),
            renamed_from: None,
            carried_from: None,
            checked_write: None,
        }
    }

    fn report_with(findings: serde_json::Value) -> serde_json::Map<String, serde_json::Value> {
        let v = serde_json::json!({
            "findings": findings,
            "missing_required_outgoing": [{"id": "m--needs-edge", "mem": "m", "missing": [{"cardinality": "at_least_one"}]}],
            "constraints": [],
            "signals": {"counts": {"warn": 1}, "entities": [{"id": "m--loud", "signals": [{"name": "attack_load", "value": 3, "level": "warn", "contributors": []}]}]},
            "anchors": {"m": {"condition": null}},
            "warnings": [{"code": "MOUNT_UNBACKED", "details": {"mem": "ghost", "reason": "missing_ref"}}],
        });
        v.as_object().cloned().unwrap()
    }

    /// The namespace rule: upper-snake claims an engine condition, any other
    /// spelling is the checker's own.
    #[test]
    fn engine_code_shape_and_conditions() {
        assert!(is_engine_code_shape("MISSING_REQUIRED_OUTGOING"));
        assert!(is_engine_code_shape("NOT_A_CONDITION"));
        assert!(!is_engine_code_shape("hidden-premise"));
        assert!(!is_engine_code_shape("Ab"));
        assert!(is_health_condition("DANGLING_LINK_NOT_RELATED"));
        assert!(!is_health_condition("STALE_ACKNOWLEDGEMENT"));
        for code in crate::ops::DanglingLinkKind::ALL_CODES {
            assert!(is_health_condition(code), "{code}");
        }
    }

    /// Unacknowledged findings, a configuration defect and a stale
    /// acknowledgement each count; an acknowledged finding does not; a
    /// later `ok` withdraws an acknowledgement.
    #[test]
    fn axis_counts_unacknowledged_configuration_and_stale() {
        let tmp = TempDir::new().unwrap();
        let ledger = CheckLedger::for_workspace(tmp.path());
        // Acknowledges the missing edge (stands), acknowledges a dangling
        // link on an entity that has since been repaired (stale), and
        // acknowledged then withdrew a signal warning (counts again).
        ledger
            .record(&record(
                "m--needs-edge",
                "failed",
                "MISSING_REQUIRED_OUTGOING",
                1,
            ))
            .unwrap();
        ledger
            .record(&record(
                "m--repaired",
                "failed",
                "DANGLING_LINK_TARGET_MISSING",
                2,
            ))
            .unwrap();
        ledger
            .record(&record("m--loud", "failed", "SIGNAL_WARN", 3))
            .unwrap();
        ledger
            .record(&record("m--loud", "ok", "SIGNAL_WARN", 4))
            .unwrap();

        let report = report_with(serde_json::json!([
            {"id": "m--linker", "axis": "consistency", "code": "DANGLING_LINK_TARGET_MISSING", "detail": {"target_id": "m--nowhere"}},
            {"id": "m--linker", "axis": "conformance", "code": "UNKNOWN_SECTION", "detail": {}},
        ]));
        let axis = compose_strict_axis(&report, Some(&ledger), None);

        assert_eq!(axis.evaluated, STRICT_INCLUDES.to_vec());
        assert_eq!(axis.findings.len(), 3, "{:?}", axis.findings);
        let by_entity: BTreeMap<&str, &StrictFinding> = axis
            .findings
            .iter()
            .map(|f| (f.entity.as_str(), f))
            .collect();
        assert!(by_entity["m--needs-edge"].acknowledged);
        assert_eq!(
            by_entity["m--needs-edge"]
                .acknowledgement
                .as_ref()
                .unwrap()
                .identity
                .as_deref(),
            Some("checker-1")
        );
        assert!(!by_entity["m--linker"].acknowledged);
        assert!(!by_entity["m--loud"].acknowledged, "a later ok withdraws");
        assert_eq!(axis.stale_acknowledgements.len(), 1);
        assert_eq!(axis.stale_acknowledgements[0].entity, "m--repaired");
        assert_eq!(
            axis.stale_acknowledgements[0].condition,
            "DANGLING_LINK_TARGET_MISSING"
        );
        assert_eq!(axis.configuration.len(), 1);
        assert_eq!(axis.configuration[0].code, "MOUNT_UNBACKED");
        assert_eq!(axis.acknowledged, 1);
        let expect: BTreeMap<String, usize> = [
            ("DANGLING_LINK_TARGET_MISSING", 1),
            ("MOUNT_UNBACKED", 1),
            ("SIGNAL_WARN", 1),
            ("STALE_ACKNOWLEDGEMENT", 1),
        ]
        .into_iter()
        .map(|(k, v)| (k.to_string(), v))
        .collect();
        assert_eq!(axis.violations_by_code, expect);
        assert_eq!(axis.violations, 4);
        assert!(axis.summary().contains("STALE_ACKNOWLEDGEMENT: 1"));

        // A mem filter drops stale acknowledgements of other mems.
        let scoped = compose_strict_axis(&report, Some(&ledger), Some("other"));
        assert!(scoped.stale_acknowledgements.is_empty());
    }

    /// "A later ok withdraws" means the later act, not the later line:
    /// an ok made after the acknowledgement but arriving before it (a
    /// transport merge) still withdraws it, and an ok made before it does
    /// not.
    #[test]
    fn withdrawal_follows_the_time_of_the_act_not_the_line() {
        let tmp = TempDir::new().unwrap();
        let ledger = CheckLedger::for_workspace(tmp.path());
        ledger
            .record(&record("m--loud", "ok", "SIGNAL_WARN", 4))
            .unwrap();
        ledger
            .record(&record("m--loud", "failed", "SIGNAL_WARN", 3))
            .unwrap();
        ledger
            .record(&record("m--quiet", "ok", "SIGNAL_WARN", 1))
            .unwrap();
        ledger
            .record(&record("m--quiet", "failed", "SIGNAL_WARN", 2))
            .unwrap();
        let report = report_with(serde_json::json!([
            {"id": "m--loud", "axis": "signals", "code": "SIGNAL_WARN", "detail": {}},
            {"id": "m--quiet", "axis": "signals", "code": "SIGNAL_WARN", "detail": {}},
        ]));
        let axis = compose_strict_axis(&report, Some(&ledger), None);
        let by: BTreeMap<&str, bool> = axis
            .findings
            .iter()
            .map(|f| (f.entity.as_str(), f.acknowledged))
            .collect();
        assert_eq!(by.get("m--loud"), Some(&false), "{:?}", axis.findings);
        assert_eq!(by.get("m--quiet"), Some(&true), "{:?}", axis.findings);
    }

    /// No ledger, no findings: a clean axis with zero violations.
    #[test]
    fn clean_axis_is_empty() {
        let report = serde_json::json!({"findings": [], "warnings": []});
        let axis = compose_strict_axis(report.as_object().unwrap(), None, None);
        assert_eq!(axis.violations, 0);
        assert!(axis.findings.is_empty());
        assert!(axis.summary().is_empty());
    }
}