spec-driven-docs 0.8.1

Spec-driven documentation: current specs, immutable decision records, and executable gates kept coherent for people and coding agents.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
//! Budget debt: the inherited violations a project carries, and only downward.
//!
//! A budget gate measures one document on one or more dimensions and fails
//! the measurement that exceeds its budget. A project adopting the convention
//! with a corpus written before it would fail its first commit on files
//! nobody touched, so it records each inherited violation here, per gate,
//! per path, per dimension. A numeric dimension carries a ceiling the gate
//! judges against instead of the budget. A boolean dimension is an exception
//! that clears once the condition is corrected.
//!
//! The file can only shrink. Nothing delivers it, nothing raises a ceiling,
//! and a recorded dimension that stops matching what the gate measures is a
//! failure naming the command that lowers it. The tightening is pure: it
//! takes the parsed debt and a set of measurements and returns the new debt,
//! so the gates and the command share one implementation and a test drives
//! it with no filesystem. What a gate measures is each gate's business.
//!
//! SATISFIES budget-debt:a-recorded-dimension-only-shrinks

use std::collections::BTreeMap;
use std::fmt::Write as _;

use camino::Utf8Path;

use crate::domain::gate_id::GateId;

/// Where an instance keeps its debt.
pub const DEBT_PATH: &str = ".spec-driven-docs/debt.yaml";
/// The flat list of exempt chapters an older instance carries.
pub const LEGACY_DEBT_PATH: &str = ".spec-driven-docs/chapter-size-debt.txt";
/// The debt file schema this binary reads and writes.
pub const SCHEMA_VERSION: u64 = 1;

/// The gates that measure a budget, in id order.
pub const BUDGET_GATES: &[GateId] = &[
    GateId::AdrWordCap,
    GateId::AgentsDigestSize,
    GateId::ChapterSizeCap,
    GateId::SpecSizeCap,
];

/// What a dimension measures.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
    /// A count judged against a budget, and against a ceiling once recorded.
    Count,
    /// A condition that either holds or does not.
    Flag,
}

/// One dimension a budget gate measures: its key in the file, its kind, and
/// the noun a finding prints after the number.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DimensionSpec {
    /// The key under a path entry.
    pub name: &'static str,
    /// Count or flag.
    pub kind: Kind,
    /// The noun a finding prints after a count.
    pub label: &'static str,
}

const fn dim(name: &'static str, kind: Kind, label: &'static str) -> DimensionSpec {
    DimensionSpec { name, kind, label }
}

const WORDS: &[DimensionSpec] = &[dim("words", Kind::Count, "words")];
const LINES: &[DimensionSpec] = &[dim("lines", Kind::Count, "lines")];
const SPEC: &[DimensionSpec] = &[
    dim("authored_lines", Kind::Count, "authored lines"),
    dim("missing_toc", Kind::Flag, "missing table of contents"),
];

/// The dimensions one budget gate measures, or none for a gate that measures
/// no budget.
#[must_use]
pub const fn dimensions(gate: GateId) -> &'static [DimensionSpec] {
    match gate {
        GateId::AdrWordCap => WORDS,
        GateId::AgentsDigestSize | GateId::ChapterSizeCap => LINES,
        GateId::SpecSizeCap => SPEC,
        _ => &[],
    }
}

fn dimension_spec(gate: GateId, name: &str) -> Option<&'static DimensionSpec> {
    dimensions(gate).iter().find(|spec| spec.name == name)
}

/// One recorded dimension.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Recorded {
    /// The gate fails above this, and at or below the budget the entry is
    /// stale.
    Ceiling(usize),
    /// The condition is accepted until it is corrected.
    Exception,
}

/// What a gate measured on one dimension of one path.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Measured {
    /// A count, with the budget the gate would judge it against.
    Count {
        /// What the gate counted.
        value: usize,
        /// The budget the specification states.
        budget: usize,
    },
    /// Whether the condition holds.
    Flag(bool),
}

/// One measurement: a gate, a path, a dimension, and what was found.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Measurement {
    /// The gate that measured.
    pub gate: GateId,
    /// The path, in the form the gate reports it.
    pub path: String,
    /// The dimension's key.
    pub dimension: &'static str,
    /// What was found.
    pub value: Measured,
}

impl Measurement {
    /// A count measurement.
    #[must_use]
    pub fn count(
        gate: GateId,
        path: impl Into<String>,
        dimension: &'static str,
        value: usize,
        budget: usize,
    ) -> Self {
        Self {
            gate,
            path: path.into(),
            dimension,
            value: Measured::Count { value, budget },
        }
    }

    /// A flag measurement.
    #[must_use]
    pub fn flag(
        gate: GateId,
        path: impl Into<String>,
        dimension: &'static str,
        holds: bool,
    ) -> Self {
        Self {
            gate,
            path: path.into(),
            dimension,
            value: Measured::Flag(holds),
        }
    }

    /// Whether the measurement violates the budget on its own.
    #[must_use]
    pub const fn violates(&self) -> bool {
        match self.value {
            Measured::Count { value, budget } => value > budget,
            Measured::Flag(holds) => holds,
        }
    }
}

/// The entries of the legacy chapter list, normalized, in file order.
///
/// One parser for the gate and the migration, so the migration can never
/// convert a line the gate did not honour. The gate never trimmed a line,
/// so a line with surrounding whitespace named no file and exempted
/// nothing; it is kept as it was written and matches nothing here either.
#[must_use]
pub fn legacy_list(text: &str) -> Vec<String> {
    text.lines()
        .filter(|entry| !entry.is_empty() && !entry.starts_with('#'))
        .map(normalize)
        .collect()
}

/// The form a path takes in the file: repository-relative, no `./`.
#[must_use]
pub fn normalize(path: &str) -> String {
    path.trim_start_matches("./").to_string()
}

/// A debt file that cannot be trusted, or a verb the state refuses.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum DebtError {
    /// The file is not YAML of this shape.
    #[error("{DEBT_PATH} does not parse: {0}")]
    Shape(String),
    /// One entry is not of this shape; the gate and path locate it.
    #[error("{DEBT_PATH}: {gate}: {path}: {detail}")]
    Malformed {
        /// The gate key the entry sits under.
        gate: String,
        /// The path key the entry sits under, or `-` for the gate itself.
        path: String,
        /// What is wrong.
        detail: String,
    },
    /// Both the legacy list and the dimensional file are present.
    #[error(
        "both {LEGACY_DEBT_PATH} and {DEBT_PATH} are present; run 'sdd debt migrate --apply' to finish the migration"
    )]
    TwoFormats,
    /// A baseline was requested over an existing debt file.
    #[error(
        "{DEBT_PATH} already exists and a baseline never widens it; fix the violation, or run 'sdd debt tighten --apply' where a recorded ceiling has slack"
    )]
    AlreadyBaselined,
    /// A baseline was requested while the legacy list is still in place.
    #[error("{LEGACY_DEBT_PATH} is present; run 'sdd debt migrate --apply' before a baseline")]
    LegacyBlocksBaseline,
    /// A migration was requested with no legacy list to migrate.
    #[error("{LEGACY_DEBT_PATH} is absent; there is no legacy list to migrate")]
    NothingToMigrate,
}

/// Which files are on disk.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Presence {
    /// `.spec-driven-docs/debt.yaml` exists.
    pub dimensional: bool,
    /// `.spec-driven-docs/chapter-size-debt.txt` exists.
    pub legacy: bool,
}

impl Presence {
    /// What an instance root carries.
    #[must_use]
    pub fn at(root: &Utf8Path) -> Self {
        Self {
            dimensional: root.join(DEBT_PATH).is_file(),
            legacy: root.join(LEGACY_DEBT_PATH).is_file(),
        }
    }
}

type Entries = BTreeMap<GateId, BTreeMap<String, BTreeMap<&'static str, Recorded>>>;

/// The recorded debt.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Debt {
    entries: Entries,
}

/// One change a tightening makes, or declines to make.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Change {
    /// A ceiling came down to the measurement.
    Lowered {
        /// The gate.
        gate: GateId,
        /// The path.
        path: String,
        /// The dimension.
        dimension: &'static str,
        /// The recorded ceiling.
        from: usize,
        /// The measurement it lowers to.
        to: usize,
    },
    /// A dimension left the file: within budget, corrected, or unmeasured.
    Removed {
        /// The gate.
        gate: GateId,
        /// The path.
        path: String,
        /// The dimension.
        dimension: &'static str,
        /// Why it left.
        reason: String,
    },
    /// A measurement rose above its ceiling; the entry stands and the gate
    /// fails on it.
    Grew {
        /// The gate.
        gate: GateId,
        /// The path.
        path: String,
        /// The dimension.
        dimension: &'static str,
        /// The recorded ceiling.
        ceiling: usize,
        /// The measurement above it.
        measured: usize,
    },
}

/// What a tightening produced.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tightened {
    /// The debt after tightening.
    pub debt: Debt,
    /// Every change, in gate, path, dimension order.
    pub changes: Vec<Change>,
}

impl Debt {
    /// Whether nothing is recorded.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.values().all(BTreeMap::is_empty)
    }

    /// Record one dimension.
    pub fn record(&mut self, gate: GateId, path: &str, dimension: &'static str, value: Recorded) {
        self.entries
            .entry(gate)
            .or_default()
            .entry(normalize(path))
            .or_default()
            .insert(dimension, value);
    }

    /// What is recorded for one dimension of one path.
    #[must_use]
    pub fn recorded(&self, gate: GateId, path: &str, dimension: &str) -> Option<Recorded> {
        self.entries
            .get(&gate)?
            .get(&normalize(path))?
            .get(dimension)
            .copied()
    }

    /// Every recorded dimension of one gate, as `(path, dimension, value)`.
    #[must_use]
    pub fn recorded_for(&self, gate: GateId) -> Vec<(String, &'static str, Recorded)> {
        self.entries
            .get(&gate)
            .into_iter()
            .flat_map(|paths| {
                paths.iter().flat_map(|(path, dims)| {
                    dims.iter()
                        .map(|(dimension, value)| (path.clone(), *dimension, *value))
                })
            })
            .collect()
    }

    /// The debt a set of measurements becomes: every violating dimension,
    /// at the measurement.
    #[must_use]
    pub fn baseline(measurements: &[Measurement]) -> Self {
        let mut debt = Self::default();
        for measurement in measurements {
            match measurement.value {
                Measured::Count { value, budget } if value > budget => {
                    debt.record(
                        measurement.gate,
                        &measurement.path,
                        measurement.dimension,
                        Recorded::Ceiling(value),
                    );
                }
                Measured::Flag(true) => {
                    debt.record(
                        measurement.gate,
                        &measurement.path,
                        measurement.dimension,
                        Recorded::Exception,
                    );
                }
                Measured::Count { .. } | Measured::Flag(false) => {}
            }
        }
        debt
    }

    /// The debt after every ceiling comes down to its measurement.
    ///
    /// A ceiling never rises and a cleared exception is never reinstated.
    /// A dimension within its budget, a corrected condition, and a path the
    /// gate no longer measures all leave the file. A measurement above its
    /// ceiling is reported and left alone, because the gate already fails on
    /// it and lowering is not the fix.
    #[must_use]
    pub fn tighten(&self, measurements: &[Measurement]) -> Tightened {
        let found: BTreeMap<(GateId, String, &str), Measured> = measurements
            .iter()
            .map(|m| ((m.gate, normalize(&m.path), m.dimension), m.value))
            .collect();
        let mut debt = Self::default();
        let mut changes = Vec::new();
        for (gate, paths) in &self.entries {
            for (path, dims) in paths {
                for (dimension, recorded) in dims {
                    let key = (*gate, path.clone(), *dimension);
                    let removed = |reason: &str| Change::Removed {
                        gate: *gate,
                        path: path.clone(),
                        dimension,
                        reason: reason.to_string(),
                    };
                    match (recorded, found.get(&key)) {
                        (_, None) => {
                            changes.push(removed("the gate measures no such path"));
                        }
                        (Recorded::Ceiling(_), Some(Measured::Count { value, budget }))
                            if value <= budget =>
                        {
                            changes.push(removed("within the budget"));
                        }
                        (Recorded::Ceiling(ceiling), Some(Measured::Count { value, .. })) => {
                            if value < ceiling {
                                changes.push(Change::Lowered {
                                    gate: *gate,
                                    path: path.clone(),
                                    dimension,
                                    from: *ceiling,
                                    to: *value,
                                });
                                debt.record(*gate, path, dimension, Recorded::Ceiling(*value));
                            } else {
                                if value > ceiling {
                                    changes.push(Change::Grew {
                                        gate: *gate,
                                        path: path.clone(),
                                        dimension,
                                        ceiling: *ceiling,
                                        measured: *value,
                                    });
                                }
                                debt.record(*gate, path, dimension, *recorded);
                            }
                        }
                        (Recorded::Exception, Some(Measured::Flag(false))) => {
                            changes.push(removed("corrected"));
                        }
                        (Recorded::Exception, Some(Measured::Flag(true))) => {
                            debt.record(*gate, path, dimension, *recorded);
                        }
                        (Recorded::Ceiling(_), Some(Measured::Flag(_)))
                        | (Recorded::Exception, Some(Measured::Count { .. })) => {
                            changes.push(removed("the dimension's kind changed"));
                        }
                    }
                }
            }
        }
        Tightened { debt, changes }
    }

    /// Parse a debt file.
    ///
    /// # Errors
    ///
    /// [`DebtError::Shape`] when the text is not a mapping of this schema,
    /// and [`DebtError::Malformed`] naming the gate and path of the first
    /// entry that is not of the expected shape. Neither falls back to the
    /// empty debt: a file that quietly stops applying turns a green commit
    /// into a false pass.
    pub fn parse(text: &str) -> Result<Self, DebtError> {
        let value: yaml_serde::Value =
            yaml_serde::from_str(text).map_err(|error| DebtError::Shape(error.to_string()))?;
        let Some(top) = value.as_mapping() else {
            return Err(DebtError::Shape(
                "the document is not a mapping".to_string(),
            ));
        };
        let mut debt = Self::default();
        let mut schema = None;
        for (key, value) in top {
            let Some(key) = key.as_str() else {
                return Err(DebtError::Shape(format!("a key is not a string: {key:?}")));
            };
            if key == "schema_version" {
                schema = value.as_u64();
                if schema != Some(SCHEMA_VERSION) {
                    return Err(DebtError::Shape(format!(
                        "schema_version must be {SCHEMA_VERSION}, found {value:?}"
                    )));
                }
                continue;
            }
            let Some(gate) = BUDGET_GATES.iter().copied().find(|g| g.to_string() == key) else {
                return Err(DebtError::Malformed {
                    gate: key.to_string(),
                    path: "-".to_string(),
                    detail: "not a budget gate; the budget gates are adr-word-cap, agents-digest-size, chapter-size-cap, and spec-size-cap".to_string(),
                });
            };
            parse_gate(&mut debt, gate, key, value)?;
        }
        if schema.is_none() {
            return Err(DebtError::Shape("schema_version is missing".to_string()));
        }
        Ok(debt)
    }

    /// Read the debt an instance carries.
    ///
    /// An absent file is the empty debt and never an error: nothing delivers
    /// the file, so absence is the ordinary state.
    ///
    /// # Errors
    ///
    /// [`DebtError::TwoFormats`] when the legacy list sits beside the file,
    /// and the parse errors of [`Self::parse`].
    pub fn read(root: &Utf8Path) -> Result<Self, DebtError> {
        let presence = Presence::at(root);
        if presence.dimensional && presence.legacy {
            return Err(DebtError::TwoFormats);
        }
        if !presence.dimensional {
            return Ok(Self::default());
        }
        let text = std::fs::read_to_string(root.join(DEBT_PATH))
            .map_err(|error| DebtError::Shape(error.to_string()))?;
        Self::parse(&text)
    }

    /// The file's text, in the one shape this binary writes.
    #[must_use]
    pub fn render(&self) -> String {
        let mut out = String::new();
        out.push_str("# Inherited budget violations this project carries.\n");
        out.push_str("#\n");
        out.push_str("# A ceiling is judged instead of the budget and only comes down: run\n");
        out.push_str(
            "# `sdd debt tighten --apply` after a document shrinks. An exception clears\n",
        );
        out.push_str("# once the condition is corrected. Nothing here can be widened.\n");
        let _ = writeln!(out, "schema_version: {SCHEMA_VERSION}");
        for (gate, paths) in &self.entries {
            if paths.is_empty() {
                continue;
            }
            let _ = writeln!(out, "\n{gate}:");
            for (path, dims) in paths {
                let _ = writeln!(out, "  {}:", quoted(path));
                for (dimension, recorded) in dims {
                    match recorded {
                        Recorded::Ceiling(ceiling) => {
                            let _ = writeln!(out, "    {dimension}:\n      ceiling: {ceiling}");
                        }
                        Recorded::Exception => {
                            let _ = writeln!(out, "    {dimension}: true");
                        }
                    }
                }
            }
        }
        out
    }
}

/// A path as a YAML scalar that reads back as itself.
fn quoted(path: &str) -> String {
    format!("'{}'", path.replace('\'', "''"))
}

fn parse_gate(
    debt: &mut Debt,
    gate: GateId,
    key: &str,
    value: &yaml_serde::Value,
) -> Result<(), DebtError> {
    let malformed = |path: &str, detail: String| DebtError::Malformed {
        gate: key.to_string(),
        path: path.to_string(),
        detail,
    };
    if value.is_null() {
        return Ok(());
    }
    let Some(paths) = value.as_mapping() else {
        return Err(malformed("-", "not a mapping of paths".to_string()));
    };
    for (path, dims) in paths {
        let Some(path) = path.as_str() else {
            return Err(malformed(
                "-",
                format!("a path key is not a string: {path:?}"),
            ));
        };
        let Some(dims) = dims.as_mapping() else {
            return Err(malformed(path, "not a mapping of dimensions".to_string()));
        };
        for (name, recorded) in dims {
            let Some(name) = name.as_str() else {
                return Err(malformed(
                    path,
                    format!("a dimension key is not a string: {name:?}"),
                ));
            };
            let Some(spec) = dimension_spec(gate, name) else {
                let known: Vec<&str> = dimensions(gate).iter().map(|d| d.name).collect();
                return Err(malformed(
                    path,
                    format!(
                        "`{name}` is not a dimension of {gate}; it measures {}",
                        known.join(", ")
                    ),
                ));
            };
            let value = match spec.kind {
                Kind::Count => recorded
                    .as_mapping()
                    .and_then(|m| m.get("ceiling"))
                    .and_then(yaml_serde::Value::as_u64)
                    .and_then(|n| usize::try_from(n).ok())
                    .map(Recorded::Ceiling)
                    .ok_or_else(|| {
                        malformed(path, format!("`{name}` must carry `ceiling: <count>`"))
                    })?,
                Kind::Flag => match recorded.as_bool() {
                    Some(true) => Recorded::Exception,
                    _ => {
                        return Err(malformed(
                            path,
                            format!(
                                "`{name}` must be `true`; a corrected exception is removed rather than set false"
                            ),
                        ));
                    }
                },
            };
            debt.record(gate, path, spec.name, value);
        }
    }
    Ok(())
}

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

    const SAMPLE: &str = "schema_version: 1\n\nspec-size-cap:\n  _docs/specs/SPEC-legacy.md:\n    authored_lines:\n      ceiling: 417\n    missing_toc: true\n\nchapter-size-cap:\n  method/legacy.md:\n    lines:\n      ceiling: 417\n";

    fn sample() -> Debt {
        Debt::parse(SAMPLE).expect("the sample parses")
    }

    #[test]
    fn the_sample_parses_and_renders_back_to_itself() {
        let debt = sample();
        assert_eq!(
            debt.recorded(
                GateId::SpecSizeCap,
                "./_docs/specs/SPEC-legacy.md",
                "authored_lines"
            ),
            Some(Recorded::Ceiling(417))
        );
        assert_eq!(
            debt.recorded(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc"
            ),
            Some(Recorded::Exception)
        );
        assert_eq!(Debt::parse(&debt.render()).unwrap(), debt);
    }

    #[test]
    fn an_absent_file_is_the_empty_debt() {
        let dir = tempfile::tempdir().unwrap();
        let root = camino::Utf8PathBuf::from_path_buf(dir.path().to_path_buf()).unwrap();
        let debt = Debt::read(&root).expect("absence is not an error");
        assert!(debt.is_empty());
    }

    #[test]
    fn both_formats_present_is_an_error_naming_migrate() {
        let dir = tempfile::tempdir().unwrap();
        let root = camino::Utf8PathBuf::from_path_buf(dir.path().to_path_buf()).unwrap();
        std::fs::create_dir_all(root.join(".spec-driven-docs")).unwrap();
        std::fs::write(root.join(DEBT_PATH), SAMPLE).unwrap();
        std::fs::write(root.join(LEGACY_DEBT_PATH), "method/legacy.md\n").unwrap();
        let error = Debt::read(&root).unwrap_err();
        assert_eq!(error, DebtError::TwoFormats);
        assert!(error.to_string().contains("sdd debt migrate --apply"));
    }

    #[test]
    fn a_malformed_file_is_an_error_naming_the_gate_and_path() {
        let error = Debt::parse(
            "schema_version: 1\nchapter-size-cap:\n  method/a.md:\n    words:\n      ceiling: 3\n",
        )
        .unwrap_err();
        assert!(
            matches!(&error, DebtError::Malformed { gate, path, .. } if gate == "chapter-size-cap" && path == "method/a.md"),
            "{error}"
        );
        let error = Debt::parse("schema_version: 1\nno-personal-path:\n  a.md: {}\n").unwrap_err();
        assert!(matches!(&error, DebtError::Malformed { gate, .. } if gate == "no-personal-path"));
        let error =
            Debt::parse("schema_version: 1\nspec-size-cap:\n  a.md:\n    missing_toc: false\n")
                .unwrap_err();
        assert!(error.to_string().contains("must be `true`"), "{error}");
        assert!(matches!(
            Debt::parse("chapter-size-cap: {}\n").unwrap_err(),
            DebtError::Shape(_)
        ));
        assert!(matches!(
            Debt::parse("schema_version: 2\n").unwrap_err(),
            DebtError::Shape(_)
        ));
        assert!(matches!(
            Debt::parse("- a\n").unwrap_err(),
            DebtError::Shape(_)
        ));
    }

    #[test]
    fn baseline_records_every_violating_dimension_and_nothing_else() {
        let debt = Debt::baseline(&[
            Measurement::count(GateId::ChapterSizeCap, "./method/a.md", "lines", 250, 200),
            Measurement::count(GateId::ChapterSizeCap, "./method/b.md", "lines", 200, 200),
            Measurement::flag(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-a.md",
                "missing_toc",
                true,
            ),
            Measurement::flag(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-b.md",
                "missing_toc",
                false,
            ),
        ]);
        assert_eq!(
            debt.recorded(GateId::ChapterSizeCap, "method/a.md", "lines"),
            Some(Recorded::Ceiling(250))
        );
        assert_eq!(
            debt.recorded(GateId::ChapterSizeCap, "method/b.md", "lines"),
            None
        );
        assert_eq!(
            debt.recorded(GateId::SpecSizeCap, "_docs/specs/SPEC-a.md", "missing_toc"),
            Some(Recorded::Exception)
        );
        assert_eq!(
            debt.recorded(GateId::SpecSizeCap, "_docs/specs/SPEC-b.md", "missing_toc"),
            None
        );
    }

    #[test]
    fn tighten_lowers_a_ceiling_to_the_measurement() {
        let tightened = sample().tighten(&[
            Measurement::count(
                GateId::ChapterSizeCap,
                "./method/legacy.md",
                "lines",
                300,
                200,
            ),
            Measurement::count(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "authored_lines",
                417,
                300,
            ),
            Measurement::flag(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc",
                true,
            ),
        ]);
        assert_eq!(
            tightened
                .debt
                .recorded(GateId::ChapterSizeCap, "method/legacy.md", "lines"),
            Some(Recorded::Ceiling(300))
        );
        assert_eq!(
            tightened.changes,
            vec![Change::Lowered {
                gate: GateId::ChapterSizeCap,
                path: "method/legacy.md".to_string(),
                dimension: "lines",
                from: 417,
                to: 300,
            }]
        );
    }

    #[test]
    fn tighten_never_raises_a_ceiling() {
        let tightened = sample().tighten(&[
            Measurement::count(
                GateId::ChapterSizeCap,
                "method/legacy.md",
                "lines",
                500,
                200,
            ),
            Measurement::count(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "authored_lines",
                417,
                300,
            ),
            Measurement::flag(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc",
                true,
            ),
        ]);
        assert_eq!(
            tightened
                .debt
                .recorded(GateId::ChapterSizeCap, "method/legacy.md", "lines"),
            Some(Recorded::Ceiling(417)),
            "the ceiling moved on a document that grew"
        );
        assert!(matches!(
            tightened.changes.as_slice(),
            [Change::Grew {
                ceiling: 417,
                measured: 500,
                ..
            }]
        ));
    }

    #[test]
    fn tighten_clears_a_corrected_exception_and_never_reinstates_one() {
        let tightened = sample().tighten(&[
            Measurement::count(
                GateId::ChapterSizeCap,
                "method/legacy.md",
                "lines",
                417,
                200,
            ),
            Measurement::count(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "authored_lines",
                417,
                300,
            ),
            Measurement::flag(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc",
                false,
            ),
        ]);
        assert_eq!(
            tightened.debt.recorded(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc"
            ),
            None
        );
        // The condition returns. Tightening records nothing new: the gate
        // fails on it as a fresh violation, and only a baseline could have
        // accepted it.
        let again = tightened.debt.tighten(&[
            Measurement::count(
                GateId::ChapterSizeCap,
                "method/legacy.md",
                "lines",
                417,
                200,
            ),
            Measurement::count(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "authored_lines",
                417,
                300,
            ),
            Measurement::flag(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc",
                true,
            ),
        ]);
        assert_eq!(
            again.debt.recorded(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc"
            ),
            None
        );
        assert!(again.changes.is_empty());
    }

    #[test]
    fn a_ceiling_reached_by_the_budget_removes_the_entry() {
        let tightened = sample().tighten(&[
            Measurement::count(
                GateId::ChapterSizeCap,
                "method/legacy.md",
                "lines",
                200,
                200,
            ),
            Measurement::count(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "authored_lines",
                300,
                300,
            ),
            Measurement::flag(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc",
                true,
            ),
        ]);
        assert_eq!(
            tightened
                .debt
                .recorded(GateId::ChapterSizeCap, "method/legacy.md", "lines"),
            None
        );
        assert_eq!(
            tightened.debt.recorded(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "authored_lines"
            ),
            None
        );
        assert_eq!(
            tightened.debt.recorded(
                GateId::SpecSizeCap,
                "_docs/specs/SPEC-legacy.md",
                "missing_toc"
            ),
            Some(Recorded::Exception)
        );
    }

    #[test]
    fn an_unmeasured_path_leaves_the_file() {
        let tightened = sample().tighten(&[]);
        assert!(tightened.debt.is_empty());
        assert_eq!(tightened.changes.len(), 3);
        assert!(tightened.changes.iter().all(|change| matches!(
            change,
            Change::Removed { reason, .. } if reason == "the gate measures no such path"
        )));
    }

    #[test]
    fn the_legacy_list_is_read_as_written_and_never_trimmed() {
        assert_eq!(
            legacy_list("# exempt\nmethod/a.md\n./method/b.md\n\n method/c.md \n"),
            vec![
                "method/a.md".to_string(),
                "method/b.md".to_string(),
                " method/c.md ".to_string()
            ]
        );
    }

    #[test]
    fn an_empty_debt_renders_no_gate() {
        let rendered = Debt::default().render();
        assert!(rendered.contains("schema_version: 1"));
        assert!(!rendered.contains("chapter-size-cap"));
        assert!(Debt::parse(&rendered).unwrap().is_empty());
    }

    #[test]
    fn a_path_needing_quotes_reads_back() {
        let mut debt = Debt::default();
        debt.record(
            GateId::ChapterSizeCap,
            "./it's/*.md",
            "lines",
            Recorded::Ceiling(3),
        );
        let parsed = Debt::parse(&debt.render()).unwrap();
        assert_eq!(
            parsed.recorded(GateId::ChapterSizeCap, "it's/*.md", "lines"),
            Some(Recorded::Ceiling(3))
        );
    }
}