spec-driven-docs 0.11.2

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
//! What a release asks of an instance that takes it.
//!
//! A changelog is written for a reader deciding whether to upgrade. A plan
//! is read by somebody who already decided. The two answer different
//! questions, so the bundle owes the second one as data a plan can filter
//! rather than prose an operator reads whole.
//!
//! One file per release that needs one, never per release. The index is
//! the complete coverage ledger from the capability floor onward, so a
//! missing prose file cannot strand an interval and a missing entry is a
//! release-time failure rather than a silent gap.

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::domain::version::CanonVersion;
use crate::plan::decision::{AnswerSchema, Choice, Decision, Selections};
use crate::plan::readiness::{Evaluation, Precondition, Requirement};

/// Where the ledger sits inside a bundle.
pub const INDEX_PATH: &str = "guidance/index.toml";

/// The ledger schema this engine reads.
pub const INDEX_SCHEMA: &str = "sdd.guidance-index/1";

/// The step-file schema this engine reads.
pub const SCHEMA: &str = "sdd.guidance/1";

/// The word an index entry uses when a release asks nothing.
pub const NONE: &str = "none";

/// Guidance this engine cannot read.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum GuidanceError {
    /// The bytes are not the shape they claim.
    #[error("{path} does not parse: {reason}")]
    Malformed {
        /// Which file.
        path: String,
        /// What the parser found.
        reason: String,
    },

    /// The file is written in a schema this engine does not read.
    #[error("{path} declares schema {found}, and this engine reads {expected}")]
    UnknownSchema {
        /// Which file.
        path: String,
        /// What it declares.
        found: String,
        /// What this engine reads.
        expected: String,
    },

    /// The file contradicts itself.
    #[error("{path} is inconsistent: {reason}")]
    Inconsistent {
        /// Which file.
        path: String,
        /// What is wrong.
        reason: String,
    },
}

/// What one release asks, in kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum StepKind {
    /// An adopted seed the projection now lands.
    SeedAdded,
    /// A rule ID that no longer resolves.
    RuleRetired,
    /// A managed file whose bytes changed.
    ManagedChanged,
    /// A key the project can now set.
    DeclarationKeyAdded,
    /// A gate whose judged set grew.
    GateWidened,
}

/// Who takes one step.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Actor {
    /// The plan's own operations already carry it.
    Plan,
    /// A person takes it.
    Operator,
}

/// The destination vocabulary a step filters against.
///
/// Closed, because a step naming a destination nothing recognizes cannot
/// be filtered and would reach every target.
pub const DESTINATIONS: &[&str] = &[
    "specs",
    "decisions",
    "reference",
    "guides",
    "declaration",
    "debt",
    "markdownlint",
    "hooks-config",
    "agents-digest",
];

/// One thing a release asks.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Step {
    /// A slug that stays put for the life of the release.
    pub id: String,
    /// What kind of change it is.
    pub kind: StepKind,
    /// Whether a target that ignores it fails a gate or loses a route.
    pub breaking: bool,
    /// Which destinations it applies to.
    pub destinations: Vec<String>,
    /// Who takes it.
    pub actor: Actor,
    /// The prose body, relative to the release's own directory.
    pub text: String,
}

impl Step {
    /// The decision identifier this step carries.
    ///
    /// Derived from the release and the step's own slug, never from
    /// display text, so a reworded body moves no plan's identity.
    #[must_use]
    pub fn decision_id(&self, release: &CanonVersion) -> String {
        format!("guidance:{release}:{}", self.id)
    }
}

/// What one release asks, whole.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Guidance {
    /// Always [`SCHEMA`] once parsed.
    pub schema: String,
    /// The release this describes.
    pub release: CanonVersion,
    /// Every step, in the order the release wrote them.
    pub steps: Vec<Step>,
}

impl Guidance {
    /// Read one release's steps.
    ///
    /// # Errors
    ///
    /// [`GuidanceError`] for bytes that do not parse, a schema this engine
    /// does not read, a duplicate step identifier, an empty or unknown
    /// destination, or a body the bundle does not carry.
    pub fn parse(path: &str, bytes: &[u8], bodies: &[String]) -> Result<Self, GuidanceError> {
        let malformed = |reason: String| GuidanceError::Malformed {
            path: path.to_string(),
            reason,
        };
        let text = std::str::from_utf8(bytes).map_err(|source| malformed(source.to_string()))?;
        let held: Self = toml::from_str(text).map_err(|source| malformed(source.to_string()))?;
        if held.schema != SCHEMA {
            return Err(GuidanceError::UnknownSchema {
                path: path.to_string(),
                found: held.schema,
                expected: SCHEMA.to_string(),
            });
        }
        let inconsistent = |reason: String| GuidanceError::Inconsistent {
            path: path.to_string(),
            reason,
        };
        let mut seen: Vec<&str> = Vec::new();
        for step in &held.steps {
            if seen.contains(&step.id.as_str()) {
                return Err(inconsistent(format!("{} appears twice", step.id)));
            }
            seen.push(&step.id);
            if step.destinations.is_empty() {
                return Err(inconsistent(format!("{} names no destination", step.id)));
            }
            for destination in &step.destinations {
                if !DESTINATIONS.contains(&destination.as_str()) {
                    return Err(inconsistent(format!(
                        "{} names the destination {destination}, which is not one this engine filters against",
                        step.id
                    )));
                }
            }
            let body = format!("guidance/{}/{}", held.release, step.text);
            if !bodies.contains(&body) {
                return Err(inconsistent(format!(
                    "{} names the body {body}, which the bundle does not carry",
                    step.id
                )));
            }
        }
        Ok(held)
    }

    /// Every step that reaches one target's destinations.
    #[must_use]
    pub fn filtered(&self, held: &[String]) -> Vec<&Step> {
        self.steps
            .iter()
            .filter(|step| {
                step.destinations
                    .iter()
                    .any(|destination| held.contains(destination))
            })
            .collect()
    }
}

/// One release's coverage.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct IndexEntry {
    /// Which release.
    pub version: CanonVersion,
    /// The file that carries its steps, or [`NONE`].
    pub guidance: String,
}

/// The complete coverage ledger.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Index {
    /// Always [`INDEX_SCHEMA`] once parsed.
    pub schema: String,
    /// The lowest release this ledger covers.
    pub capability_floor: CanonVersion,
    /// One entry per covered release.
    pub releases: Vec<IndexEntry>,
}

impl Index {
    /// Read the ledger.
    ///
    /// # Errors
    ///
    /// [`GuidanceError`] for bytes that do not parse, a schema this engine
    /// does not read, or a release listed twice.
    pub fn parse(bytes: &[u8]) -> Result<Self, GuidanceError> {
        let malformed = |reason: String| GuidanceError::Malformed {
            path: INDEX_PATH.to_string(),
            reason,
        };
        let text = std::str::from_utf8(bytes).map_err(|source| malformed(source.to_string()))?;
        let held: Self = toml::from_str(text).map_err(|source| malformed(source.to_string()))?;
        if held.schema != INDEX_SCHEMA {
            return Err(GuidanceError::UnknownSchema {
                path: INDEX_PATH.to_string(),
                found: held.schema,
                expected: INDEX_SCHEMA.to_string(),
            });
        }
        let mut seen: Vec<CanonVersion> = Vec::new();
        for entry in &held.releases {
            if seen.contains(&entry.version) {
                return Err(GuidanceError::Inconsistent {
                    path: INDEX_PATH.to_string(),
                    reason: format!("{} appears twice", entry.version),
                });
            }
            seen.push(entry.version);
        }
        Ok(held)
    }

    /// What the ledger says about one release.
    #[must_use]
    pub fn entry(&self, version: CanonVersion) -> Option<&IndexEntry> {
        self.releases.iter().find(|entry| entry.version == version)
    }

    /// Every release in the half-open interval, in order.
    #[must_use]
    pub fn interval(&self, from: Option<CanonVersion>, to: CanonVersion) -> Vec<&IndexEntry> {
        let mut found: Vec<&IndexEntry> = self
            .releases
            .iter()
            .filter(|entry| from.is_none_or(|held| entry.version > held) && entry.version <= to)
            .collect();
        found.sort_by_key(|entry| entry.version);
        found
    }
}

/// How much of an interval the ledger covers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Coverage {
    /// Every release in the interval has an entry.
    Complete,
    /// The recorded release predates the floor.
    Partial,
}

/// What the ledger says about one interval.
#[must_use]
pub fn coverage(index: &Index, from: Option<CanonVersion>) -> Coverage {
    match from {
        Some(recorded) if recorded < index.capability_floor => Coverage::Partial,
        _ => Coverage::Complete,
    }
}

/// The preconditions and decisions one interval's guidance carries.
#[derive(Debug, Clone, Default)]
pub struct Briefing {
    /// Everything that must hold first.
    pub preconditions: Vec<Precondition>,
    /// Everything the operator accepts knowingly.
    pub decisions: Vec<Decision>,
    /// Every step that reaches this target, as `<release>:<id>`.
    pub applicable: Vec<String>,
    /// How many steps the target's destinations excluded.
    pub excluded: usize,
}

/// Read one interval's guidance against one target.
///
/// A breaking step is a decision the operator accepts with the step's own
/// body in front of them. An additive step is reported and blocks nothing.
/// Partial coverage is a decision rather than a gap, because the operator
/// can still say they know what is missing.
#[must_use]
pub fn brief(
    index: &Index,
    files: &[(CanonVersion, Guidance)],
    from: Option<CanonVersion>,
    destinations: &[String],
    selections: &Selections,
) -> Briefing {
    let mut briefing = Briefing::default();
    for (release, guidance) in files {
        let applicable = guidance.filtered(destinations);
        briefing.excluded += guidance.steps.len() - applicable.len();
        for step in applicable {
            let id = step.decision_id(release);
            briefing.applicable.push(id.clone());
            if !step.breaking {
                continue;
            }
            let selected = selections.get(&id).cloned();
            briefing.decisions.push(Decision {
                question: format!("{release} asks: {}", step.id.replace('-', " ")),
                schema: AnswerSchema::Choice {
                    choices: vec![
                        Choice {
                            id: "accepted".to_string(),
                            consequence: format!(
                                "read guidance/{release}/{} and take the step",
                                step.text
                            ),
                        },
                        Choice {
                            id: "not-applicable".to_string(),
                            consequence: "this target does not carry what the step is about"
                                .to_string(),
                        },
                    ],
                },
                depends_on: Vec::new(),
                selected,
                id,
            });
        }
    }
    if coverage(index, from) == Coverage::Partial {
        let id = "guidance-coverage".to_string();
        let selected = selections.get(&id).cloned();
        briefing.preconditions.push(Precondition {
            id: "guidance-is-covered".to_string(),
            statement: "every release in the interval carries its guidance".to_string(),
            requirement: Requirement::DecisionRequired,
            evaluation: selected.as_ref().map_or_else(
                || Evaluation::NotObserved {
                    reason: format!(
                        "the target records a release below {}, which this engine does not brief",
                        index.capability_floor
                    ),
                },
                |_| Evaluation::Satisfied,
            ),
            resolved_by: Some(id.clone()),
            evidence_refs: vec!["release".to_string()],
        });
        briefing.decisions.push(Decision {
            question: format!(
                "guidance starts at {}; proceed without what came before?",
                index.capability_floor
            ),
            schema: AnswerSchema::Choice {
                choices: vec![
                    Choice {
                        id: "accepted".to_string(),
                        consequence: "the plan proceeds with the guidance it has".to_string(),
                    },
                    Choice {
                        id: "refuse".to_string(),
                        consequence: "nothing lands; read the changelog first".to_string(),
                    },
                ],
            },
            depends_on: Vec::new(),
            selected,
            id,
        });
    }
    for decision in &briefing.decisions {
        if decision.id == "guidance-coverage" {
            continue;
        }
        briefing.preconditions.push(Precondition {
            id: format!("step:{}", decision.id),
            statement: decision.question.clone(),
            requirement: Requirement::DecisionRequired,
            evaluation: decision.selected.as_ref().map_or_else(
                || Evaluation::Unsatisfied {
                    reason: "the operator has not accepted this step".to_string(),
                },
                |_| Evaluation::Satisfied,
            ),
            resolved_by: Some(decision.id.clone()),
            evidence_refs: vec!["release".to_string()],
        });
    }
    briefing
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::unwrap_used,
        reason = "a test panics as its failure signal, not as control flow"
    )]

    use super::*;

    fn version(value: &str) -> CanonVersion {
        value.parse().unwrap()
    }

    fn bodies() -> Vec<String> {
        vec!["guidance/0.7.0/one.md".to_string()]
    }

    const ONE: &str = r#"
schema = "sdd.guidance/1"
release = "0.7.0"

[[steps]]
id = "one"
kind = "rule-retired"
breaking = true
destinations = ["specs"]
actor = "operator"
text = "one.md"
"#;

    #[test]
    fn a_step_declares_every_part_and_parses() {
        let held = Guidance::parse("guidance/0.7.0.toml", ONE.as_bytes(), &bodies()).unwrap();
        assert_eq!(held.release, version("0.7.0"));
        assert_eq!(held.steps[0].kind, StepKind::RuleRetired);
        assert_eq!(held.steps[0].actor, Actor::Operator);
        assert!(held.steps[0].breaking);
    }

    #[test]
    fn a_decision_id_derives_from_the_release_and_the_step_id() {
        let held = Guidance::parse("guidance/0.7.0.toml", ONE.as_bytes(), &bodies()).unwrap();
        assert_eq!(
            held.steps[0].decision_id(&version("0.7.0")),
            "guidance:0.7.0:one"
        );
        // A body rewritten under the same name keeps the identifier: the
        // decision is the step, not the prose that explains it.
        let reworded = ONE.replace("kind = \"rule-retired\"", "kind = \"gate-widened\"");
        let again = Guidance::parse("guidance/0.7.0.toml", reworded.as_bytes(), &bodies()).unwrap();
        assert_eq!(
            again.steps[0].decision_id(&version("0.7.0")),
            held.steps[0].decision_id(&version("0.7.0"))
        );
    }

    #[test]
    fn guidance_refuses_what_it_cannot_filter_or_resolve() {
        let cases = [
            (
                ONE.replace("kind = \"rule-retired\"", "kind = \"invented\""),
                "parse",
            ),
            (
                ONE.replace("destinations = [\"specs\"]", "destinations = []"),
                "destination",
            ),
            (
                ONE.replace("destinations = [\"specs\"]", "destinations = [\"nowhere\"]"),
                "destination",
            ),
            (
                ONE.replace("text = \"one.md\"", "text = \"absent.md\""),
                "body",
            ),
            (format!("{ONE}extra = 1\n"), "parse"),
            (format!("{ONE}{ONE}"), "parse"),
        ];
        for (text, _) in cases {
            assert!(
                Guidance::parse("guidance/0.7.0.toml", text.as_bytes(), &bodies()).is_err(),
                "{text}"
            );
        }
    }

    #[test]
    fn a_duplicate_step_identifier_refuses() {
        let doubled = format!(
            "{ONE}\n[[steps]]\nid = \"one\"\nkind = \"seed-added\"\nbreaking = false\ndestinations = [\"specs\"]\nactor = \"plan\"\ntext = \"one.md\"\n"
        );
        let error =
            Guidance::parse("guidance/0.7.0.toml", doubled.as_bytes(), &bodies()).unwrap_err();
        assert!(error.to_string().contains("twice"), "{error}");
    }

    #[test]
    fn a_step_is_filtered_against_the_targets_destinations() {
        let held = Guidance::parse("guidance/0.7.0.toml", ONE.as_bytes(), &bodies()).unwrap();
        assert_eq!(held.filtered(&["specs".to_string()]).len(), 1);
        assert_eq!(held.filtered(&["debt".to_string()]).len(), 0);
    }

    fn index() -> Index {
        Index::parse(
            br#"
schema = "sdd.guidance-index/1"
capability_floor = "0.6.6"

[[releases]]
version = "0.6.6"
guidance = "none"

[[releases]]
version = "0.7.0"
guidance = "0.7.0.toml"

[[releases]]
version = "0.7.1"
guidance = "none"
"#,
        )
        .unwrap()
    }

    #[test]
    fn the_interval_is_derived_from_the_ledger_alone() {
        let held = index();
        let found: Vec<String> = held
            .interval(Some(version("0.6.6")), version("0.7.1"))
            .iter()
            .map(|entry| entry.version.to_string())
            .collect();
        assert_eq!(found, ["0.7.0", "0.7.1"]);
        assert_eq!(held.interval(None, version("0.6.6")).len(), 1);
        assert_eq!(held.entry(version("0.7.0")).unwrap().guidance, "0.7.0.toml");
        assert_eq!(held.entry(version("0.7.1")).unwrap().guidance, NONE);
        assert!(held.entry(version("9.9.9")).is_none());
    }

    #[test]
    fn a_release_below_the_floor_is_partial_coverage() {
        let held = index();
        assert_eq!(coverage(&held, Some(version("0.6.5"))), Coverage::Partial);
        assert_eq!(coverage(&held, Some(version("0.6.6"))), Coverage::Complete);
        assert_eq!(coverage(&held, None), Coverage::Complete);
    }

    #[test]
    fn an_additive_upgrade_needs_no_decision_and_a_breaking_one_does() {
        let additive = ONE.replace("breaking = true", "breaking = false");
        let held = Guidance::parse("guidance/0.7.0.toml", additive.as_bytes(), &bodies()).unwrap();
        let briefing = brief(
            &index(),
            &[(version("0.7.0"), held)],
            Some(version("0.6.6")),
            &["specs".to_string()],
            &Selections::new(),
        );
        assert!(briefing.decisions.is_empty());
        assert_eq!(briefing.applicable, ["guidance:0.7.0:one"]);

        let breaking = Guidance::parse("guidance/0.7.0.toml", ONE.as_bytes(), &bodies()).unwrap();
        let briefing = brief(
            &index(),
            &[(version("0.7.0"), breaking.clone())],
            Some(version("0.6.6")),
            &["specs".to_string()],
            &Selections::new(),
        );
        assert_eq!(briefing.decisions.len(), 1);
        assert_eq!(briefing.preconditions.len(), 1);
        assert_eq!(
            briefing.preconditions[0].verdict(),
            crate::plan::readiness::Readiness::NeedsDecision
        );

        let mut selections = Selections::new();
        selections.insert("guidance:0.7.0:one".to_string(), "accepted".to_string());
        let briefing = brief(
            &index(),
            &[(version("0.7.0"), breaking)],
            Some(version("0.6.6")),
            &["specs".to_string()],
            &selections,
        );
        assert_eq!(
            briefing.preconditions[0].verdict(),
            crate::plan::readiness::Readiness::Ready
        );
    }

    #[test]
    fn a_filtered_out_step_is_counted_rather_than_hidden() {
        let held = Guidance::parse("guidance/0.7.0.toml", ONE.as_bytes(), &bodies()).unwrap();
        let briefing = brief(
            &index(),
            &[(version("0.7.0"), held)],
            Some(version("0.6.6")),
            &["debt".to_string()],
            &Selections::new(),
        );
        assert_eq!(briefing.excluded, 1);
        assert!(briefing.applicable.is_empty());
        assert!(briefing.decisions.is_empty());
    }

    #[test]
    fn partial_coverage_is_a_decision_a_selection_resolves() {
        let briefing = brief(
            &index(),
            &[],
            Some(version("0.6.5")),
            &["specs".to_string()],
            &Selections::new(),
        );
        assert_eq!(briefing.preconditions.len(), 1);
        assert_eq!(
            briefing.preconditions[0].verdict(),
            crate::plan::readiness::Readiness::NeedsDecision
        );
        let mut selections = Selections::new();
        selections.insert("guidance-coverage".to_string(), "accepted".to_string());
        let briefing = brief(
            &index(),
            &[],
            Some(version("0.6.5")),
            &["specs".to_string()],
            &selections,
        );
        assert_eq!(
            briefing.preconditions[0].verdict(),
            crate::plan::readiness::Readiness::Ready
        );
    }
}