wdi-method 0.4.6 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/README.md +271 -252
  2. package/bin/wdi-method.js +1221 -1030
  3. package/kit/.constitution/method/README.md +73 -169
  4. package/kit/.constitution/{constitution.md → method/constitution.md} +10 -56
  5. package/kit/.constitution/{document → method/document}/architecture-guide.md +209 -209
  6. package/kit/.constitution/{document → method/document}/corpus-guide.md +4 -4
  7. package/kit/.constitution/{document → method/document}/delivery-flow-guide.md +338 -338
  8. package/kit/.constitution/{document → method/document}/sdd-guide.md +1 -1
  9. package/kit/.constitution/{document → method/document}/templates/cross-cutting.md +2 -2
  10. package/kit/.constitution/{document → method/document}/templates/design-system.md +67 -67
  11. package/kit/.constitution/{document → method/document}/templates/inventory.md +69 -69
  12. package/kit/.constitution/{document → method/document}/templates/sdd.md +1 -1
  13. package/kit/.constitution/{document → method/document}/templates/structure-codebase.md +129 -129
  14. package/kit/.constitution/{document → method/document}/templates/structure-document.md +90 -90
  15. package/kit/.constitution/{language-guide.md → method/language-guide.md} +2 -2
  16. package/kit/.constitution/{method-glossary.md → method/method-glossary.md} +169 -169
  17. package/kit/.constitution/{repo-guide.md → method/repo-guide.md} +120 -97
  18. package/kit/.constitution/method/scripts/__pycache__/validate.cpython-314.pyc +0 -0
  19. package/kit/.constitution/{scripts → method/scripts}/timeline.py +2 -2
  20. package/kit/.constitution/{scripts → method/scripts}/validate.py +1648 -1581
  21. package/kit/.constitution/{structure-guide.md → method/structure-guide.md} +3 -3
  22. package/kit/.constitution/method/why/README.md +169 -0
  23. package/kit/.constitution/method/{artifact-map.md → why/artifact-map.md} +5 -5
  24. package/kit/.constitution/method/{portability.md → why/portability.md} +6 -6
  25. package/kit/.constitution/method/{rationale.md → why/rationale.md} +5 -5
  26. package/kit/.constitution/project/README.md +68 -68
  27. package/kit/.constitution/project/constitution.md +64 -0
  28. package/kit/assets/bmad-custom/bmad-advanced-elicitation.toml +15 -15
  29. package/kit/assets/bmad-custom/bmad-architecture.toml +33 -33
  30. package/kit/assets/bmad-custom/bmad-code-review.toml +13 -13
  31. package/kit/assets/bmad-custom/bmad-correct-course.toml +28 -28
  32. package/kit/assets/bmad-custom/bmad-prd.toml +42 -42
  33. package/kit/assets/bmad-custom/bmad-product-brief.toml +34 -34
  34. package/kit/assets/bmad-custom/bmad-retrospective.toml +9 -9
  35. package/kit/assets/bmad-custom/bmad-spec.toml +16 -16
  36. package/kit/assets/bmad-custom/bmad-ux.toml +19 -19
  37. package/kit/skills/wdi-blueprint/SKILL.md +203 -203
  38. package/kit/skills/wdi-build/SKILL.md +244 -244
  39. package/kit/skills/wdi-component/SKILL.md +1 -1
  40. package/kit/skills/wdi-help/SKILL.md +98 -98
  41. package/kit/skills/wdi-init/SKILL.md +1 -1
  42. package/kit/skills/wdi-problem/SKILL.md +90 -90
  43. package/kit/skills/wdi-product/SKILL.md +127 -127
  44. package/kit/skills/wdi-reconcile/SKILL.md +2 -2
  45. package/kit/skills/wdi-report/SKILL.md +205 -205
  46. package/kit/skills/wdi-systematic-debugging/SKILL.md +193 -193
  47. package/kit/skills/wdi-ux/SKILL.md +145 -145
  48. package/kit-overlay/AGENTS.md +215 -212
  49. package/kit-overlay/README.md +24 -17
  50. package/kit-overlay/constitution.md +10 -56
  51. package/kit-overlay/portability.md +6 -6
  52. package/kit-overlay/repo-guide.md +120 -97
  53. package/package.json +4 -2
  54. package/scaffold/.control/questions/blocking.md +1 -1
  55. package/kit/.constitution/README.md +0 -66
  56. /package/kit/.constitution/{document → method/document}/bmad-guide.md +0 -0
  57. /package/kit/.constitution/{document → method/document}/bmad-skill-register.md +0 -0
  58. /package/kit/.constitution/{document → method/document}/brief-guide.md +0 -0
  59. /package/kit/.constitution/{document → method/document}/decision-guide.md +0 -0
  60. /package/kit/.constitution/{document → method/document}/prd-guide.md +0 -0
  61. /package/kit/.constitution/{document → method/document}/srs-guide.md +0 -0
  62. /package/kit/.constitution/{document → method/document}/templates/addendum.md +0 -0
  63. /package/kit/.constitution/{document → method/document}/templates/architecture-spine.md +0 -0
  64. /package/kit/.constitution/{document → method/document}/templates/brief.md +0 -0
  65. /package/kit/.constitution/{document → method/document}/templates/c4.md +0 -0
  66. /package/kit/.constitution/{document → method/document}/templates/contract.md +0 -0
  67. /package/kit/.constitution/{document → method/document}/templates/decision.md +0 -0
  68. /package/kit/.constitution/{document → method/document}/templates/flow.md +0 -0
  69. /package/kit/.constitution/{document → method/document}/templates/integration.md +0 -0
  70. /package/kit/.constitution/{document → method/document}/templates/lc.md +0 -0
  71. /package/kit/.constitution/{document → method/document}/templates/lifecycle.md +0 -0
  72. /package/kit/.constitution/{document → method/document}/templates/meeting.md +0 -0
  73. /package/kit/.constitution/{document → method/document}/templates/model.md +0 -0
  74. /package/kit/.constitution/{document → method/document}/templates/oq.md +0 -0
  75. /package/kit/.constitution/{document → method/document}/templates/prd.md +0 -0
  76. /package/kit/.constitution/{document → method/document}/templates/questions.md +0 -0
  77. /package/kit/.constitution/{document → method/document}/templates/rtr.md +0 -0
  78. /package/kit/.constitution/{document → method/document}/templates/rules.md +0 -0
  79. /package/kit/.constitution/{document → method/document}/templates/scn.md +0 -0
  80. /package/kit/.constitution/{document → method/document}/templates/srs.md +0 -0
  81. /package/kit/.constitution/{document → method/document}/templates/uc.md +0 -0
  82. /package/kit/.constitution/{document → method/document}/templates/ux.md +0 -0
  83. /package/kit/.constitution/{document → method/document}/ux-guide.md +0 -0
  84. /package/kit/.constitution/{scripts → method/scripts}/inventory.py +0 -0
  85. /package/kit/.constitution/{codebase/brownfield-guide.md → project/codebase-brownfield-guide.md} +0 -0
  86. /package/kit/.constitution/{codebase/conventions-guide.md → project/codebase-conventions-guide.md} +0 -0
  87. /package/kit/.constitution/{codebase/stack-guide.md → project/codebase-stack-guide.md} +0 -0
@@ -1,1581 +1,1648 @@
1
- #!/usr/bin/env -S uv run --script
2
- # /// script
3
- # requires-python = ">=3.11"
4
- # dependencies = ["pyyaml>=6"]
5
- # ///
6
- """validate — V1..V27 plus the .control/generated/ generator.
7
-
8
- Two modes:
9
- validate --check exit non-zero if anything is red; writes nothing
10
- validate --generate rewrite .control/generated/ (and still runs --check)
11
-
12
- Determinism is the contract: two runs over the same data MUST produce the same result.
13
- That is why there is no unordered iteration, and the one time-dependent input
14
- (--asof, used by V14) is stated explicitly instead of being taken silently from the wall clock.
15
-
16
- What is NOT done here: the time dimension from git. `generated/timeline` and
17
- `generated/report` belong to wdi-report. See 08-project-management.md.
18
- """
19
-
20
- from __future__ import annotations
21
-
22
- import argparse
23
- import datetime as dt
24
- import re
25
- import subprocess
26
- import sys
27
- from dataclasses import dataclass, field
28
- from pathlib import Path
29
-
30
- import yaml
31
-
32
- REGISTRY = "control/registry" # tidied up in resolve(); '.control' is what is actually used
33
- GENERATED_ORDER = ["components", "risks", "dag", "rtm", "status"]
34
-
35
- # Pages read by HUMANS, not machines: written as real markdown tables, not yaml
36
- # in a fence. All three are named in §22 and each has one clear reader.
37
- GENERATED_PAGES = ["decisions", "blueprint", "estimate"]
38
-
39
- MODES = ("catalog", "outline", "guarded", "deep")
40
-
41
- # Keywords that make a component "sensitive" for V23. Matched against `risk_note`, which is PROSE in
42
- # whatever `policy.doc_language` the product chose so the set is the UNION of both languages rather
43
- # than a translation. It leans toward disclosing more, which is what this check is for: it discloses,
44
- # it does not judge. Deliberately short.
45
- SENSITIVE_MARKERS = (
46
- # English
47
- "money", "payment", "personal data", "pii",
48
- "irreversible", "cannot be undone", "contractual", "contract", "integration",
49
- # Bahasa Indonesia
50
- "uang", "pembayaran", "data pribadi",
51
- "tak-terbalikkan", "tak terbalikkan", "tidak dapat dibatalkan",
52
- "kontraktual", "kontrak", "integrasi",
53
- )
54
-
55
-
56
- # ---------------------------------------------------------------- infrastructure
57
-
58
-
59
- @dataclass(frozen=True)
60
- class Finding:
61
- vid: str
62
- subject: str
63
- message: str
64
-
65
- @property
66
- def sort_key(self) -> tuple[int, str, str]:
67
- digits = "".join(ch for ch in self.vid if ch.isdigit())
68
- return (int(digits or 0), self.subject, self.message)
69
-
70
-
71
- @dataclass
72
- class Result:
73
- findings: list[Finding] = field(default_factory=list)
74
- skipped: dict[str, str] = field(default_factory=dict)
75
-
76
- def fail(self, vid: str, subject: str, message: str) -> None:
77
- self.findings.append(Finding(vid, subject, message))
78
-
79
- def skip(self, vid: str, why: str) -> None:
80
- self.skipped[vid] = why
81
-
82
- @property
83
- def red(self) -> list[str]:
84
- return sorted({f.vid for f in self.findings})
85
-
86
-
87
- def load_yaml(path: Path) -> dict:
88
- if not path.exists():
89
- return {}
90
- data = yaml.safe_load(path.read_text(encoding="utf-8"))
91
- return data if isinstance(data, dict) else {}
92
-
93
-
94
- def rows(data: dict, key: str) -> list[dict]:
95
- """Registry list, always sorted by id so the output is deterministic."""
96
- value = data.get(key) or []
97
- if not isinstance(value, list):
98
- return []
99
- items = [v for v in value if isinstance(v, dict)]
100
- return sorted(items, key=lambda r: str(r.get("id", "")))
101
-
102
-
103
- FM = re.compile(r"\A---\s*\n(.*?)\n---\s*(\n|\Z)", re.S)
104
-
105
-
106
- class Dumper(yaml.SafeDumper):
107
- """No anchors/aliases: output MUST be readable and diffable line by line."""
108
-
109
- def ignore_aliases(self, data) -> bool: # noqa: ARG002
110
- return True
111
-
112
-
113
- def dump(payload: dict) -> str:
114
- return yaml.dump(payload, Dumper=Dumper, allow_unicode=True, sort_keys=False,
115
- default_flow_style=False, width=100)
116
-
117
-
118
- def frontmatter(path: Path) -> dict | None:
119
- """None if the file does not exist; {} if it exists but has no frontmatter."""
120
- if not path.exists():
121
- return None
122
- match = FM.match(path.read_text(encoding="utf-8", errors="replace"))
123
- if not match:
124
- return {}
125
- data = yaml.safe_load(match.group(1))
126
- return data if isinstance(data, dict) else {}
127
-
128
-
129
- def git(root: Path, *args: str) -> str | None:
130
- try:
131
- out = subprocess.run(
132
- ["git", "-C", str(root), *args],
133
- capture_output=True, text=True, timeout=30, check=False,
134
- )
135
- except (OSError, subprocess.SubprocessError):
136
- return None
137
- return out.stdout.strip() if out.returncode == 0 else None
138
-
139
-
140
- # ------------------------------------------------------------------- loading
141
-
142
-
143
- @dataclass
144
- class Corpus:
145
- root: Path
146
- requirements: dict
147
- usecases: dict
148
- decisions: dict
149
- risks: dict
150
- components: dict
151
- waves: dict
152
- defects: dict
153
- index: dict
154
-
155
- @classmethod
156
- def load(cls, root: Path) -> "Corpus":
157
- reg = root / ".control" / "registry"
158
- return cls(
159
- root=root,
160
- requirements=load_yaml(reg / "requirements.yaml"),
161
- usecases=load_yaml(reg / "usecases.yaml"),
162
- decisions=load_yaml(reg / "decisions.yaml"),
163
- risks=load_yaml(reg / "risks.yaml"),
164
- components=load_yaml(reg / "components.yaml"),
165
- waves=load_yaml(reg / "waves.yaml"),
166
- defects=load_yaml(reg / "defects.yaml"),
167
- index=load_yaml(reg / "index.yaml"),
168
- )
169
-
170
- # --- shortcuts used repeatedly
171
- @property
172
- def goals(self) -> list[dict]:
173
- return rows(self.requirements, "goals")
174
-
175
- @property
176
- def caps(self) -> list[dict]:
177
- return rows(self.requirements, "capabilities")
178
-
179
- @property
180
- def frs(self) -> list[dict]:
181
- return rows(self.requirements, "functional")
182
-
183
- @property
184
- def nfrs(self) -> list[dict]:
185
- return rows(self.requirements, "nonfunctional")
186
-
187
- @property
188
- def ucs(self) -> list[dict]:
189
- return rows(self.usecases, "usecases")
190
-
191
- @property
192
- def decs(self) -> list[dict]:
193
- return rows(self.decisions, "decisions")
194
-
195
- def mode_of(self, pc: dict) -> str:
196
- """Per-component `mode` wins over the global one; with neither, default `catalog`."""
197
- own = str(pc.get("mode") or "").strip()
198
- if own:
199
- return own
200
- return str(self.index.get("mode") or "").strip() or "catalog"
201
-
202
- @property
203
- def lcs(self) -> list[dict]:
204
- return rows(self.components, "logical_components")
205
-
206
- @property
207
- def pcs(self) -> list[dict]:
208
- return rows(self.components, "product_components")
209
-
210
- @property
211
- def wave_list(self) -> list[dict]:
212
- return rows(self.waves, "waves")
213
-
214
- @property
215
- def defect_list(self) -> list[dict]:
216
- return rows(self.defects, "defects")
217
-
218
- def stories(self) -> list[tuple[dict, dict, dict]]:
219
- """(wave, epic, story) sorted by id at each level."""
220
- out = []
221
- for wave in self.wave_list:
222
- for epic in sorted(wave.get("epics") or [], key=lambda e: str(e.get("id", ""))):
223
- if not isinstance(epic, dict):
224
- continue
225
- for story in sorted(epic.get("stories") or [], key=lambda s: str(s.get("id", ""))):
226
- if isinstance(story, dict):
227
- out.append((wave, epic, story))
228
- return out
229
-
230
-
231
- def listy(row: dict, key: str) -> list[str]:
232
- value = row.get(key) or []
233
- if isinstance(value, str):
234
- return [value]
235
- return [str(v) for v in value if v is not None]
236
-
237
-
238
- # ------------------------------------------------------------------ validators
239
-
240
-
241
- def v1(c: Corpus, r: Result) -> None:
242
- """Every BG has >=1 FR through its CAP, OR states its reason in `no_fr`.
243
-
244
- A goal MAY be satisfied by an **invariant** rather than a feature. `BG-6` — the data and
245
- deployment foundation can be extended without being torn down is measured by two architectural
246
- properties that its own `measure` names, and no `FR` can carry it without being invented. Demanding
247
- one `FR` there produces a false promise, and a false promise is more expensive than a finding.
248
-
249
- The escape MUST carry a reason, not a boolean — the same shape as `no_uc` on `FR` (V2).
250
- """
251
- cap_by_goal: dict[str, list[str]] = {}
252
- for cap in c.caps:
253
- cap_by_goal.setdefault(str(cap.get("goal", "")), []).append(str(cap.get("id")))
254
- fr_caps = {str(fr.get("capability", "")) for fr in c.frs}
255
- for goal in c.goals:
256
- gid = str(goal.get("id"))
257
- reachable = [cid for cid in cap_by_goal.get(gid, []) if cid in fr_caps]
258
- if reachable:
259
- continue
260
- if str(goal.get("no_fr") or "").strip():
261
- continue
262
- r.fail("V1", gid, "has no FR through its CAP and states no reason in `no_fr`")
263
-
264
-
265
- def v2(c: Corpus, r: Result) -> None:
266
- covered = {fr for uc in c.ucs for fr in listy(uc, "satisfies")}
267
- for fr in c.frs:
268
- fid = str(fr.get("id"))
269
- if fid in covered:
270
- continue
271
- if str(fr.get("no_uc") or "").strip():
272
- continue
273
- r.fail("V2", fid, "has no UC and states no reason in `no_uc`")
274
-
275
-
276
- def v3(c: Corpus, r: Result) -> None:
277
- """A UC on a component that a wave has ALREADY touched MUST be scheduled to a story.
278
-
279
- The old shape demanded this of EVERY UC, at any time. Before the first wave that meant the
280
- entire catalogue was reported red 56 findings out of 62, and those 56 were the correct state,
281
- not drift: a story is born in a wave, and there was no wave yet. A validator that drowns six real
282
- findings under fifty-six expected ones stops being read, and a validator that is not read
283
- guards nothing.
284
-
285
- What is guarded now is the actual omission: a wave touches a component, and a UC of that
286
- component is left behind without a story. Full coverage of the whole catalogue is a G5 question,
287
- and `wdi-build` owns it the same way V12 was shifted to wave closing.
288
- """
289
- scheduled = {uc for _, _, s in c.stories() for uc in listy(s, "satisfies")}
290
- touched = {str(s.get("component")) for _, _, s in c.stories() if s.get("component")}
291
- if not c.wave_list:
292
- r.skip("V3", "no wave yet, so no story yet — every unscheduled UC is the correct "
293
- "state. Full catalogue coverage is checked at G5")
294
- return
295
- for uc in c.ucs:
296
- uid = str(uc.get("id"))
297
- if uid in scheduled or str(uc.get("component")) not in touched:
298
- continue
299
- r.fail("V3", uid, f"component `{uc.get('component')}` has already been touched by a wave, "
300
- f"but this UC is not scheduled to any story")
301
-
302
-
303
- def v4(c: Corpus, r: Result) -> None:
304
- for _, _, story in c.stories():
305
- if not [t for t in listy(story, "tests") if t.strip()]:
306
- r.fail("V4", str(story.get("id")), "has not one named test")
307
-
308
-
309
- def v5(c: Corpus, r: Result) -> None:
310
- """Every NFR has an enforcer, OR states its reason in `no_enforcer`.
311
-
312
- Two NFRs in this repo cannot have an enforcer, and both are valid: one has already been
313
- **retired**, and the other states of itself that it is a **design measure, not a gate**. Demanding
314
- a test for both produces a test that cannot fail, and a test that cannot fail is theater.
315
- """
316
- for nfr in c.nfrs:
317
- if [e for e in listy(nfr, "enforced_by") if e.strip()]:
318
- continue
319
- if str(nfr.get("no_enforcer") or "").strip():
320
- continue
321
- r.fail("V5", str(nfr.get("id")),
322
- "has no enforcer in `enforced_by` and states no reason in `no_enforcer`")
323
-
324
-
325
- def v6(c: Corpus, r: Result) -> None:
326
- defined: set[str] = set()
327
- for group in (c.goals, c.caps, c.frs, c.nfrs, c.ucs, c.decs, c.lcs, c.pcs,
328
- rows(c.requirements, "journeys"), rows(c.risks, "risks"), c.defect_list):
329
- defined |= {str(row.get("id")) for row in group if row.get("id") is not None}
330
- for wave in c.wave_list:
331
- defined.add(str(wave.get("id")))
332
- for _, epic, story in c.stories():
333
- defined.add(str(epic.get("id")))
334
- defined.add(str(story.get("id")))
335
-
336
- refs: list[tuple[str, str]] = []
337
- for cap in c.caps:
338
- refs.append((str(cap.get("id")), str(cap.get("goal", ""))))
339
- refs += [(str(cap.get("id")), d) for d in listy(cap, "depends_on")]
340
- for fr in c.frs:
341
- refs.append((str(fr.get("id")), str(fr.get("capability", ""))))
342
- for nfr in c.nfrs:
343
- refs.append((str(nfr.get("id")), str(nfr.get("goal", ""))))
344
- for uc in c.ucs:
345
- refs += [(str(uc.get("id")), f) for f in listy(uc, "satisfies")]
346
- for dec in c.decs:
347
- refs += [(str(dec.get("id")), s) for s in listy(dec, "serves")]
348
- for defect in c.defect_list:
349
- refs += [(str(defect.get("id")), v) for v in listy(defect, "violates")]
350
- for _, _, story in c.stories():
351
- refs += [(str(story.get("id")), u) for u in listy(story, "satisfies")]
352
- refs += [(str(story.get("id")), d) for d in listy(story, "depends_on")]
353
-
354
- for owner, target in sorted(set(refs)):
355
- if target and target not in defined:
356
- r.fail("V6", owner, f"points to `{target}` which does not exist in any registry")
357
-
358
-
359
- def _cycles(graph: dict[str, list[str]]) -> list[str]:
360
- state: dict[str, int] = {}
361
- bad: list[str] = []
362
-
363
- def walk(node: str) -> None:
364
- state[node] = 1
365
- for nxt in sorted(graph.get(node, [])):
366
- if state.get(nxt) == 1:
367
- bad.append(node)
368
- elif state.get(nxt) is None and nxt in graph:
369
- walk(nxt)
370
- state[node] = 2
371
-
372
- for node in sorted(graph):
373
- if state.get(node) is None:
374
- walk(node)
375
- return sorted(set(bad))
376
-
377
-
378
- def v7(c: Corpus, r: Result) -> None:
379
- caps = {str(x.get("id")): listy(x, "depends_on") for x in c.caps}
380
- for node in _cycles(caps):
381
- r.fail("V7", node, "is part of a `depends_on` cycle among CAPs")
382
- stories = {str(s.get("id")): listy(s, "depends_on") for _, _, s in c.stories()}
383
- for node in _cycles(stories):
384
- r.fail("V7", node, "is part of a `depends_on` cycle among stories")
385
-
386
-
387
- def v8(c: Corpus, r: Result) -> None:
388
- """Every `applied` decision names a non-empty `touches`.
389
-
390
- Replaces the old shape "every accepted decision serves >=1 FR/NFR". A decision like
391
- "the filter MUST work like this" serves no FR at all, and that is VALID — it is exactly
392
- decisions like that which most need remembering, and the old rule discarded them.
393
- """
394
- for dec in c.decs:
395
- if str(dec.get("status")) != "applied":
396
- continue
397
- if not [x for x in listy(dec, "touches") if str(x).strip()]:
398
- r.fail("V8", str(dec.get("id")),
399
- "is applied but `touches` is empty — an application with no file trace")
400
-
401
-
402
- def v9(c: Corpus, r: Result) -> None:
403
- passed = {str(g) for g in (c.index.get("gates_passed") or [])}
404
- for path in sorted(c.root.glob(".what/**/*.md")) + sorted(c.root.glob(".how/**/*.md")):
405
- fm = frontmatter(path) or {}
406
- if str(fm.get("status")) != "locked":
407
- continue
408
- gate = str(fm.get("locked_at_gate") or "")
409
- if gate not in passed:
410
- rel = path.relative_to(c.root).as_posix()
411
- r.fail("V9", rel, f"is locked but gate `{gate or '?'}` is not recorded as passed")
412
-
413
-
414
- def v11(c: Corpus, r: Result) -> None:
415
- per_wave: dict[str, list[dict]] = {}
416
- for wave, _, story in c.stories():
417
- per_wave.setdefault(str(wave.get("id")), []).append(story)
418
-
419
- for wid in sorted(per_wave):
420
- items = per_wave[wid]
421
- edges = {str(s.get("id")): set(listy(s, "depends_on")) for s in items}
422
-
423
- def reaches(a: str, b: str, seen: set[str] | None = None) -> bool:
424
- seen = seen or set()
425
- if a in seen:
426
- return False
427
- seen.add(a)
428
- if b in edges.get(a, set()):
429
- return True
430
- return any(reaches(n, b, seen) for n in sorted(edges.get(a, set())))
431
-
432
- for i, left in enumerate(items):
433
- for right in items[i + 1:]:
434
- lid, rid = str(left.get("id")), str(right.get("id"))
435
- shared = sorted(set(listy(left, "touches")) & set(listy(right, "touches")))
436
- if not shared:
437
- continue
438
- if reaches(lid, rid) or reaches(rid, lid):
439
- continue
440
- r.fail("V11", f"{lid} + {rid}",
441
- f"share touches {shared} with no depends_on relation — MUST NOT run in parallel")
442
-
443
-
444
- def v12(c: Corpus, r: Result) -> None:
445
- """LC registration is checked when a wave CLOSES, not before a story goes `ready-for-dev`.
446
-
447
- The old shape demanded the answer when the information was thinnest. At wave closing,
448
- every `touches` already has an area and every boundary already has a name.
449
- """
450
- areas = {str(lc.get("area")) for lc in c.lcs if lc.get("area")}
451
- lcs_per_pc: dict[str, int] = {}
452
- for lc in c.lcs:
453
- lcs_per_pc[str(lc.get("component"))] = lcs_per_pc.get(str(lc.get("component")), 0) + 1
454
- pc_by_id = {str(x.get("id")): x for x in c.pcs}
455
-
456
- seen: set[tuple[str, str]] = set()
457
- for wave, _, story in c.stories():
458
- if str(wave.get("status")) != "closed":
459
- continue
460
- for area in listy(story, "touches"):
461
- if area not in areas:
462
- r.fail("V12", str(story.get("id")),
463
- f"its wave is already closed, but `{area}` is not registered as an `area` "
464
- f"in components.yaml")
465
- pid = str(story.get("component") or "")
466
- row = pc_by_id.get(pid)
467
- if row is None or (str(wave.get("id")), pid) in seen:
468
- continue
469
- seen.add((str(wave.get("id")), pid))
470
- if c.mode_of(row) in ("guarded", "deep") and not lcs_per_pc.get(pid):
471
- r.fail("V12", f"{wave.get('id')} / {pid}",
472
- f"wave closed and component with mode `{c.mode_of(row)}` has not one "
473
- f"`LC` registered")
474
-
475
-
476
- LENS_BY_RISK = {
477
- "low": {"edge-case-hunter"},
478
- "medium": {"edge-case-hunter"},
479
- "high": set(),
480
- }
481
- FRONTMATTER_KEYS = ("reviewed:", "date:", "sha:", "lenses:", "updated:")
482
-
483
-
484
- def _reviewed_ok(r: Result, rel: str, block: object, need: set[str]) -> None:
485
- if not isinstance(block, dict) or not block.get("sha") or not block.get("date"):
486
- r.fail("V13", rel, "carries no `reviewed` trace with a date and sha")
487
- return
488
- lenses = {str(x) for x in (block.get("lenses") or [])}
489
- if not lenses:
490
- r.fail("V13", rel, "the `reviewed` trace names not one lens")
491
- missing = sorted(need - lenses)
492
- if missing:
493
- r.fail("V13", rel,
494
- f"lenses {missing} MUST be included that is what the component's `risk_accepted` demands")
495
-
496
-
497
- def _only_reviewed_block(diff: str) -> bool:
498
- """True if a commit's diff on one file ONLY touches the `reviewed:` block.
499
-
500
- This is the OQ-146 fix. The old V13 compared `sha` against the last commit that changed
501
- the file but the commit that WRITES the `reviewed:` block always changes the file, and
502
- writing its own hash into a git commit is cryptographically impossible. As a result every
503
- artifact that had just been stamped immediately read as "stale review", forever.
504
- """
505
- touched = [ln for ln in diff.splitlines()
506
- if ln[:1] in "+-" and not ln.startswith("+++") and not ln.startswith("---")]
507
- if not touched:
508
- return True
509
- for ln in touched:
510
- body = ln[1:].strip()
511
- if not body or body.startswith("#"):
512
- continue
513
- if not body.startswith(FRONTMATTER_KEYS):
514
- return False
515
- return True
516
-
517
-
518
- def _stale_since(c: Corpus, rel: str, sha: str) -> str | None:
519
- """First commit after `sha` that changes this file for a reason other than a review stamp."""
520
- log = git(c.root, "log", "--format=%H", f"{sha}..HEAD", "--", rel)
521
- if not log:
522
- return None
523
- for head in log.splitlines():
524
- head = head.strip()
525
- if not head:
526
- continue
527
- diff = git(c.root, "show", "--format=", "--unified=0", head, "--", rel)
528
- if diff is None:
529
- return head
530
- if _only_reviewed_block(diff):
531
- continue
532
- return head
533
- return None
534
-
535
-
536
- def v13(c: Corpus, r: Result) -> None:
537
- """Review trace follows review INTENSITY, not document depth.
538
-
539
- Narrowed to components with `risk_accepted` `low` or `medium`. At `high` the owner has already
540
- stated they accept the risk, and demanding a trace there is bookkeeping with no buyer.
541
- """
542
- watched = [pc for pc in c.pcs
543
- if str(pc.get("risk_accepted") or "").strip() in ("low", "medium")]
544
- if not watched:
545
- r.skip("V13", "no component with risk_accepted low or medium — nothing to guard")
546
- targets: list[tuple[Path, set[str]]] = []
547
- if watched:
548
- targets.append((c.root / ".how/_platform/ARCHITECTURE-SPINE.md", set()))
549
- for pc in watched:
550
- pid = str(pc.get("id"))
551
- need = LENS_BY_RISK.get(str(pc.get("risk_accepted")).strip(), set())
552
- # The SRS exists and is meaningful at EVERY mode: it carries the Actor Register and UC
553
- # Catalogue, and both are born at G3, which the depth knob does not touch.
554
- targets.append((c.root / f".what/{pid}/SRS-{pid}.md", need))
555
- # The SDD is guarded only when it HAS content worth guarding. Two states exempt it, and
556
- # both are FINISHED states, not neglected ones:
557
- # mode: catalog the skeleton is its final form; G4 is skipped there
558
- # g4_passed not set G4 has not run yet, so not one section is written
559
- # Demanding a review trace on a file whose content is 13 lines of template comments is
560
- # theater — exactly the ceremony this redesign cut, and a review that cannot fail proves
561
- # nothing. Once G4 passes, the demand comes back and it is meaningful.
562
- passed = str(pc.get("g4_passed") or "").strip().lower()
563
- if c.mode_of(pc) != "catalog" and passed not in ("", "false", "no", "belum"):
564
- targets.append((c.root / f".how/{pid}/SDD-{pid}.md", need))
565
-
566
- for path, need in targets:
567
- fm = frontmatter(path)
568
- if fm is None:
569
- continue # not born yet not V13's business
570
- rel = path.relative_to(c.root).as_posix()
571
- _reviewed_ok(r, rel, fm.get("reviewed"), need)
572
- block = fm.get("reviewed")
573
- if isinstance(block, dict) and block.get("sha"):
574
- stale = _stale_since(c, rel, str(block["sha"]))
575
- if stale:
576
- r.fail("V13", rel,
577
- f"changed at {stale[:7]} after being reviewed at {str(block['sha'])[:7]} — "
578
- f"stale review")
579
-
580
- for wave in c.wave_list:
581
- if not wave.get("epics"):
582
- continue
583
- _reviewed_ok(r, f"waves.yaml:{wave.get('id')}", wave.get("spec_reviewed"),
584
- {"edge-case-hunter"})
585
-
586
-
587
- def cap_stories(c: Corpus) -> dict[str, list[dict]]:
588
- """CAP -> story, traced through CAP -> FR -> UC -> story. No git, no timeline."""
589
- frs_of: dict[str, list[str]] = {}
590
- for fr in c.frs:
591
- frs_of.setdefault(str(fr.get("capability", "")), []).append(str(fr.get("id")))
592
- ucs_of: dict[str, list[str]] = {}
593
- for uc in c.ucs:
594
- for fid in listy(uc, "satisfies"):
595
- ucs_of.setdefault(fid, []).append(str(uc.get("id")))
596
- out: dict[str, list[dict]] = {}
597
- for cap in c.caps:
598
- cid = str(cap.get("id"))
599
- wanted = {u for fid in frs_of.get(cid, []) for u in ucs_of.get(fid, [])}
600
- out[cid] = [s for _, _, s in c.stories()
601
- if wanted & set(listy(s, "satisfies"))]
602
- return out
603
-
604
-
605
- def v14(c: Corpus, r: Result, asof: dt.date) -> None:
606
- """Overdue-ness is computed from the registry itself — the timeline only reinforces, never gates."""
607
- by_cap = cap_stories(c)
608
- timeline = load_yaml(c.root / ".control/generated/timeline.yaml")
609
- listed = {str(row.get("id")) for row in rows(timeline, "capabilities")
610
- if str(row.get("state")) == "overdue"} if timeline else None
611
- if listed is None:
612
- r.skip("V14", "generated/timeline.yaml does not exist yet — overdue-ness is still computed "
613
- "from the registry, but its presence in generated/report is not checked")
614
-
615
- for cap in c.caps:
616
- cid = str(cap.get("id"))
617
- end = str(cap.get("planned_end") or "")
618
- if not end:
619
- continue
620
- try:
621
- due = dt.date.fromisoformat(end)
622
- except ValueError:
623
- r.fail("V14", cid, f"`planned_end` `{end}` is not an ISO date")
624
- continue
625
- items = by_cap.get(cid, [])
626
- closed = bool(items) and all(_story_status(c, s) == "done" for s in items)
627
- if closed or due >= asof:
628
- continue
629
- late = (asof - due).days
630
- if listed is not None and cid not in listed:
631
- r.fail("V14", cid, f"{late} days overdue with nothing delivered, and not flagged "
632
- f"`overdue` in generated/timeline")
633
- else:
634
- r.fail("V14", cid, f"{late} days overdue with nothing closed")
635
-
636
-
637
- def v15(c: Corpus, r: Result) -> None:
638
- for cap in c.caps:
639
- if not str(cap.get("goal") or "").strip():
640
- r.fail("V15", str(cap.get("id")), "does not point to a `goal`")
641
- for fr in c.frs:
642
- if not str(fr.get("capability") or "").strip():
643
- r.fail("V15", str(fr.get("id")), "does not point to a `capability`")
644
-
645
-
646
- def v16(c: Corpus, r: Result) -> None:
647
- for path in sorted((c.root / ".control/memlog").glob("*.md")):
648
- fm = frontmatter(path) or {}
649
- rel = path.relative_to(c.root).as_posix()
650
- artifact = str(fm.get("artifact") or "")
651
- if not artifact:
652
- r.fail("V16", rel, "has no `artifact:` in frontmatter")
653
- elif not (c.root / artifact).exists():
654
- r.fail("V16", rel, f"`artifact:` points to `{artifact}` which does not exist")
655
- for layer in (".what", ".how"):
656
- for stray in sorted(c.root.glob(f"{layer}/**/.memlog.md")):
657
- r.fail("V16", stray.relative_to(c.root).as_posix(),
658
- "a memlog MUST NOT live inside the corpus")
659
-
660
-
661
- def v17(c: Corpus, r: Result) -> None:
662
- for wave in c.wave_list:
663
- wid = str(wave.get("id"))
664
- if not str(wave.get("release") or "").strip():
665
- r.fail("V17", wid, "does not name a `release`")
666
- slugs = listy(wave, "prd")
667
- if not slugs:
668
- r.fail("V17", wid, "does not name a `prd`")
669
- for slug in slugs:
670
- if not (c.root / ".what/_prd" / slug).is_dir():
671
- r.fail("V17", wid, f"`prd: {slug}` has no folder .what/_prd/{slug}/")
672
-
673
-
674
- def v18(c: Corpus, r: Result) -> None:
675
- for _, _, story in c.stories():
676
- sid = str(story.get("id"))
677
- folder = str(story.get("spec_folder") or "").strip()
678
- if not folder:
679
- r.fail("V18", sid, "does not name a `spec_folder`")
680
- continue
681
- matches = sorted((c.root / folder / "stories").glob(f"{sid}-*.md"))
682
- if not matches:
683
- r.fail("V18", sid, f"has no story file in {folder}stories/")
684
- continue
685
- fm = frontmatter(matches[0]) or {}
686
- if not str(fm.get("status") or "").strip():
687
- r.fail("V18", sid, "story file has no `status` in frontmatter")
688
-
689
-
690
- def v19(c: Corpus, r: Result) -> None:
691
- """The retrospective archive is tied to WAVE SIZE, not to `mode`.
692
-
693
- Mandatory on wave `L`; advisory on `S` and `M`. Document depth and volume of work are two
694
- different things, and demanding a retrospective for a three-story wave is ceremony.
695
- """
696
- names = [x.name for x in sorted((c.root / ".control/reports").glob("RTR-*"))]
697
- advisory: list[str] = []
698
- for wave in c.wave_list:
699
- if str(wave.get("status")) != "closed":
700
- continue
701
- wid = str(wave.get("id"))
702
- if any(wid in name for name in names):
703
- continue
704
- if str(wave.get("size")).upper() == "L":
705
- r.fail("V19", wid, "wave `L` closed without an `RTR-` in .control/reports/")
706
- else:
707
- advisory.append(wid)
708
- if advisory:
709
- r.skip("V19", "advisory — wave S/M closed without an RTR-: " + ", ".join(sorted(advisory)))
710
- else:
711
- r.skip("V19", "only the RTR- line item is checked mechanically; the rest of the distillation is guarded by wdi-build")
712
-
713
-
714
- PLATFORM = "_platform"
715
- CROSS_CUTTING = ".how/_platform/cross-cutting.md"
716
- PLATFORM_DATA_HEADING = "Milik platform"
717
-
718
-
719
- def v21(c: Corpus, r: Result) -> None:
720
- """One domain entity has EXACTLY ONE owner authorized to write it.
721
-
722
- The owner is a Product Component, OR `_platform` for an entity with no single component
723
- promise behind it. Semantic collisions across PRDs have already happened for real: one
724
- component took a business-rule numbering range from a shared global sequence. Two `FR`s
725
- that both claim write authority over the same entity, with neither pointing at the other,
726
- are a defect the moment they are written.
727
-
728
- `_platform` is NOT a Product Component and therefore has no `mode`, `risk_accepted`, SRS,
729
- or G4. It is a home for ownership, not a domain slice and so it does not become a dumping
730
- ground, every entity it claims MUST be explained in `cross-cutting.md`: if the platform
731
- owns the data, the platform documents it.
732
- """
733
- owner: dict[str, str] = {}
734
- for pc in c.pcs:
735
- pid = str(pc.get("id"))
736
- for entity in listy(pc, "owns"):
737
- if entity in owner and owner[entity] != pid:
738
- r.fail("V21", entity,
739
- f"claimed as `owns` by both `{owner[entity]}` and `{pid}` one entity MUST "
740
- f"have exactly one owner")
741
- else:
742
- owner.setdefault(entity, pid)
743
-
744
- platform = listy(c.components, "platform_owns")
745
- for entity in platform:
746
- if entity in owner:
747
- r.fail("V21", entity,
748
- f"claimed as `platform_owns` and also as `owns` by `{owner[entity]}` — "
749
- f"`{PLATFORM}` is not a second path for an entity that already has an owner")
750
- else:
751
- owner[entity] = PLATFORM
752
-
753
- _platform_documented(c, r, platform + _platform_inventory_rows(c))
754
-
755
- cap_home = {str(x.get("id")): str(x.get("component") or "") for x in c.caps}
756
- for fr in c.frs:
757
- fid = str(fr.get("id"))
758
- home = str(fr.get("component") or cap_home.get(str(fr.get("capability", "")), ""))
759
- for entity in listy(fr, "writes"):
760
- own = owner.get(entity)
761
- if not own or not home or own == home:
762
- continue
763
- if own == PLATFORM:
764
- # The platform has no `FR`, so there is nothing a `defers_to` could point to. What
765
- # stands in for "one writer" here is ONE DOCUMENTED FORM, and that is what
766
- # _platform_documented checks above.
767
- continue
768
- if not [d for d in listy(fr, "defers_to") if str(d).strip()]:
769
- r.fail("V21", fid,
770
- f"promises to write `{entity}` which `{own}` owns, without `defers_to` "
771
- f"pointing to an `FR` owned by that owner")
772
-
773
-
774
- def _platform_inventory_rows(c: Corpus) -> list[str]:
775
- """Inventory rows owned by `_platform`, read from `platform_rows:` in each inventory.
776
-
777
- `_platform` is a valid value at EVERY ownership position, so the guard applies at every
778
- position too: whatever it owns MUST be documented in `cross-cutting.md`.
779
- """
780
- out: list[str] = []
781
- for kind in ("db", "api", "screen"):
782
- path = c.root / f".how/_platform/inventory-{kind}.md"
783
- fm = frontmatter(path)
784
- if not fm:
785
- continue
786
- out += [str(x) for x in (fm.get("platform_rows") or [])]
787
- return out
788
-
789
-
790
- def _platform_documented(c: Corpus, r: Result, entities: list[str]) -> None:
791
- """Every entity with `platform_owns` MUST be named in `cross-cutting.md`.
792
-
793
- Skipped while the file does not yet carry that section: `cross-cutting.md` is a G3 output, and
794
- an artifact the next gate will produce MUST NOT be reported missing.
795
- """
796
- if not entities:
797
- return
798
- path = c.root / CROSS_CUTTING
799
- text = path.read_text(encoding="utf-8", errors="replace") if path.exists() else ""
800
- if PLATFORM_DATA_HEADING.lower() not in text.lower():
801
- r.skip("V21", f"`{CROSS_CUTTING}` has no `{PLATFORM_DATA_HEADING}` section yet "
802
- f"{len(entities)} entities with platform_owns are not documented yet: "
803
- + ", ".join(sorted(entities)))
804
- return
805
- for entity in sorted(entities):
806
- if entity not in text:
807
- r.fail("V21", entity,
808
- f"claimed as `platform_owns` but not named in `{CROSS_CUTTING}` "
809
- f"a platform that owns data MUST document it")
810
-
811
-
812
- def v22(c: Corpus, r: Result) -> None:
813
- """A wave MUST NOT touch a component whose G4 has not passed and whose mode is not catalog.
814
-
815
- `catalog` skips G4 on purpose, so it is not an exception it is part of the rule.
816
- """
817
- pc_by_id = {str(x.get("id")): x for x in c.pcs}
818
- seen: set[tuple[str, str]] = set()
819
- for wave, _, story in c.stories():
820
- pid = str(story.get("component") or "")
821
- row = pc_by_id.get(pid)
822
- if row is None:
823
- continue
824
- key = (str(wave.get("id")), pid)
825
- if key in seen:
826
- continue
827
- seen.add(key)
828
- mode = c.mode_of(row)
829
- if mode == "catalog":
830
- continue
831
- if mode not in MODES:
832
- r.fail("V22", pid, f"`mode: {mode}` is not one of {list(MODES)}")
833
- continue
834
- passed = row.get("g4_passed")
835
- if not passed or str(passed).strip().lower() in ("false", "no", "belum"):
836
- r.fail("V22", f"{wave.get('id')} / {pid}",
837
- f"wave touches a component with mode `{mode}` whose `g4_passed` has not been set")
838
-
839
-
840
- def v23(c: Corpus, r: Result) -> None:
841
- """`risk_accepted: high` on a sensitive component demands a `DEC-` in `risk_accepted_by`.
842
-
843
- On a component that touches nothing on that list, `high` is FREE. The control is
844
- disclosure, not veto — the owner may still choose quickly, just not without knowing what
845
- they are wagering.
846
- """
847
- known = {str(x.get("id")) for x in c.decs}
848
- for pc in c.pcs:
849
- pid = str(pc.get("id"))
850
- if str(pc.get("risk_accepted") or "").strip() != "high":
851
- continue
852
- note = str(pc.get("risk_note") or "").lower()
853
- hits = sorted({m for m in SENSITIVE_MARKERS if m in note})
854
- if not hits:
855
- continue
856
- ref = str(pc.get("risk_accepted_by") or "").strip()
857
- if not ref:
858
- r.fail("V23", pid,
859
- f"`risk_accepted: high` while `risk_note` mentions {hits}, without "
860
- f"`risk_accepted_by` pointing to a risk-acceptance `DEC-`")
861
- elif ref not in known:
862
- r.fail("V23", pid, f"`risk_accepted_by: {ref}` does not exist in decisions.yaml")
863
-
864
-
865
- def v20(c: Corpus, r: Result) -> None:
866
- needs_link = {"requirement", "architecture"}
867
- for defect in c.defect_list:
868
- did = str(defect.get("id"))
869
- cause = str(defect.get("root_cause") or "")
870
- if cause not in needs_link:
871
- continue
872
- if not listy(defect, "violates"):
873
- r.fail("V20", did, f"has `root_cause` `{cause}` but `violates` is empty")
874
- if str(defect.get("status")) == "fixed" and not str(defect.get("decision") or "").strip():
875
- r.fail("V20", did,
876
- f"closed as fixed with root_cause `{cause}` without an accompanying `DEC-`")
877
-
878
-
879
- # Files that DESCRIBE the past, not STATE what currently holds. A dangling citation here is
880
- # not a finding — corpus-guide.md owns that rule, and rewriting it would falsify history.
881
- PAST_RECORD = (
882
- ".control/memlog/",
883
- ".control/decisions/",
884
- ".control/questions/answered.md",
885
- ".control/reports/",
886
- )
887
- # Corpus that §25 freezes as-is. Its citation of a now-retired prototype is authorized by DEC-016.
888
- FROZEN = (".what/",)
889
- # A path a run WILL PRODUCE, not one a document cites as existing. A rule stating "this pass's
890
- # memlog lands at X" names a DESTINATION; demanding X already exist would demand the run has already
891
- # happened.
892
- DESTINATION = (
893
- ".control/memlog/",
894
- ".control/meetings/",
895
- ".control/reports/",
896
- "_bmad-output/",
897
- )
898
-
899
- CITE_RE = re.compile(
900
- r"`((?:\.constitution|\.control|\.what|\.how|_bmad-output|\.work|src|web|public|deploy)"
901
- r"/[A-Za-z0-9_./-]+\.(?:md|yaml|yml|py|go|tsx|ts|js|mjs|sql|html|css|json))`")
902
-
903
-
904
- def v24(c: Corpus, r: Result) -> None:
905
- """A path citation inside a document that STATES what currently holds MUST resolve.
906
-
907
- This is the mechanical half of `wdi-reconcile`'s Evidence check, and it is the only way to know
908
- that a migration stayed complete. Its failure class is distinctive: a file gets deleted or moved,
909
- while the routing line that points at it stays behind no other validator sees it, because no
910
- id moved.
911
-
912
- Deliberately SKIPPED: files that describe the past, and corpus that has been frozen. A `DEC-`
913
- Trace that names material that has since been retired describes what was read on that date;
914
- reporting it would demand history be rewritten to match the present.
915
- """
916
- scanned = 0
917
- for path in sorted(c.root.rglob("*.md")) + sorted(c.root.rglob("*.yaml")):
918
- rel = path.relative_to(c.root).as_posix()
919
- if rel.startswith((".git/", "node_modules/", "_bmad-output/", ".claude/skills/bmad-")):
920
- continue
921
- if rel.startswith(PAST_RECORD) or rel.startswith(FROZEN):
922
- continue
923
- scanned += 1
924
- text = path.read_text(encoding="utf-8", errors="replace")
925
- for cited in sorted(set(CITE_RE.findall(text))):
926
- if "<" in cited or "{" in cited:
927
- continue # placeholder, not a path
928
- if cited.startswith(DESTINATION):
929
- continue
930
- if not (c.root / cited).exists():
931
- r.fail("V24", rel, f"cites `{cited}` which does not exist")
932
- if not scanned:
933
- r.skip("V24", "no file was scanned")
934
-
935
-
936
- CTR_HEADING = re.compile(r"^###\s+(.+?)\s*$", re.M)
937
-
938
-
939
- def map_container_headings(root: Path) -> list[str] | None:
940
- """Heading `### x` under `## Containers` in the code map. None if the map does not exist."""
941
- path = root / ".control" / "structure-codebase.md"
942
- if not path.exists():
943
- return None
944
- text = path.read_text(encoding="utf-8", errors="replace")
945
- start = text.find("\n## Containers")
946
- if start < 0:
947
- return []
948
- rest = text[start + 1:]
949
- nxt = re.search(r"^##\s+(?!#)", rest[len("## Containers"):], re.M)
950
- if nxt:
951
- rest = rest[:len("## Containers") + nxt.start()]
952
- return [m.group(1).strip().strip("`") for m in CTR_HEADING.finditer(rest)]
953
-
954
-
955
- def v25(c: Corpus, r: Result) -> None:
956
- """A container's `built` and its four consequences, plus the PC x container matrix.
957
-
958
- A container EXISTS inside the boundary whether or not we write its content, and that is what
959
- used to make the rule unsatisfiable: `structure-guide.md` demands every code-map heading match
960
- the registry, while a database or web server MUST be registered and MUST NOT have a heading.
961
- `built` separates the two, and this check is what makes that separation hold instead of the
962
- argument being repeated on every project. `DEC-017` records its definition.
963
-
964
- Anything whose runtime we do not deploy is an external system: it lives in C4 L1 and MUST NOT
965
- be registered here at all — its absence from the registry is the check.
966
- """
967
- containers = rows(c.components, "containers")
968
- if not containers:
969
- r.skip("V25", "`containers:` is not registered yet")
970
- return
971
-
972
- built: dict[str, bool] = {}
973
- for ctr in containers:
974
- cid = str(ctr.get("id") or "").strip()
975
- if not cid:
976
- r.fail("V25", "containers", "a container has no `id`")
977
- continue
978
- flag = ctr.get("built")
979
- if not isinstance(flag, bool):
980
- r.fail("V25", cid, "`built` MUST be a bool — true if we write its content, false if someone else implements it")
981
- continue
982
- built[cid] = flag
983
-
984
- # (1) code-map heading = EXACTLY a container with `built: true`
985
- headings = map_container_headings(c.root)
986
- if headings is None:
987
- r.fail("V25", ".control/structure-codebase.md", "the code map does not exist, so container headings cannot be compared")
988
- else:
989
- for h in headings:
990
- if h not in built:
991
- r.fail("V25", f"code map §{h}", "heading is not a registered container — register it, or it is not a container")
992
- elif not built[h]:
993
- r.fail("V25", f"code map §{h}", "`built: false` MUST NOT have a heading — there is no code of ours inside it")
994
- for cid, flag in sorted(built.items()):
995
- if flag and cid not in headings:
996
- r.fail("V25", cid, "`built: true` MUST have a heading in the code map")
997
-
998
- # (2) `built: false` MUST NOT be used by an LC, and (3) MUST NOT appear in a PC's `containers:`
999
- for lc in c.lcs:
1000
- ctr = str(lc.get("container") or "").strip()
1001
- if ctr and built.get(ctr) is False:
1002
- r.fail("V25", str(lc.get("id") or "LC-?"), f"names container `{ctr}` which is `built: false`")
1003
- elif ctr and ctr not in built:
1004
- r.fail("V25", str(lc.get("id") or "LC-?"), f"names container `{ctr}` which is not registered")
1005
-
1006
- # (4) PC x container matrix — this field is its SSOT, and it MUST be complete at G3
1007
- for pc in c.pcs:
1008
- pid = str(pc.get("id") or "?")
1009
- listed = listy(pc, "containers")
1010
- if not listed:
1011
- r.fail("V25", pid, "`containers:` is empty — every PC MUST live in at least one container (a G3 debt)")
1012
- continue
1013
- for ctr in listed:
1014
- if ctr not in built:
1015
- r.fail("V25", pid, f"`containers:` names `{ctr}` which is not registered")
1016
- elif not built[ctr]:
1017
- r.fail("V25", pid, f"`containers:` names `{ctr}` which is `built: false` the data lives there by definition, so the row tells us nothing")
1018
-
1019
- # (5) L3 — only for `built: true`, and only ones that hold more than one PC
1020
- pcs_per: dict[str, list[str]] = {}
1021
- for pc in c.pcs:
1022
- for ctr in listy(pc, "containers"):
1023
- pcs_per.setdefault(ctr, []).append(str(pc.get("id") or "?"))
1024
- for path in sorted((c.root / ".how" / "_platform").glob("c4-l3-*.md")):
1025
- cid = path.name[len("c4-l3-"):-len(".md")]
1026
- if cid not in built:
1027
- r.fail("V25", path.relative_to(c.root).as_posix(),
1028
- f"L3 for `{cid}` which is not a registered container")
1029
- elif not built[cid]:
1030
- r.fail("V25", path.relative_to(c.root).as_posix(),
1031
- f"`{cid}` `built: false` MUST NOT have an L3 — not one box inside it is ours to draw")
1032
- for cid, pids in sorted(pcs_per.items()):
1033
- if built.get(cid) and len(pids) > 1:
1034
- l3 = c.root / ".how" / "_platform" / f"c4-l3-{cid}.md"
1035
- if not l3.exists():
1036
- r.fail("V25", cid, f"holds {len(pids)} PCs, so `c4-l3-{cid}.md` MUST exist")
1037
-
1038
-
1039
- UC_ROW_RE = re.compile(r"^\|\s*(UC-\d+)\s*\|([^\n]*)$", re.M)
1040
-
1041
- # The `critical` column value is machine-matched, so it is machine-facing and its canonical form
1042
- # is English `yes`. `ya` is still accepted: a corpus that wrote it before this rule took effect
1043
- # MUST NOT be forced to migrate just so a regex can be tidier. The word boundary keeps `ya` from
1044
- # matching inside other words.
1045
- CRITICAL_YES = re.compile(r"\b(yes|ya)\b", re.I)
1046
-
1047
-
1048
- def v26(c: Corpus, r: Result) -> None:
1049
- """The UC catalogue in every SRS MUST agree with `usecases.yaml`both its id AND its `critical`.
1050
-
1051
- This is the most expensive gap this pass closes, because it is the only one that **had already
1052
- happened and no validator saw it.** Step 16 re-derived `critical` in the registry with a
1053
- narrowed definition — money, personal data, irreversible action — and the seven catalogue tables
1054
- in the SRS did not follow along. Twenty-six rows disagreed, and the disagreement was only
1055
- discovered when a human read the sentence "nine of these are critical" in SRS-admin while the
1056
- registry held three.
1057
-
1058
- The registry is the SSOT. The table in the SRS is the catalogue's permanent home for a reader,
1059
- and two homes for one fact are only safe if something compares them. This is what compares them.
1060
-
1061
- What is NOT checked here: title and actor. Both are prose, and prose with different words is
1062
- not prose with a different meaningcomparing them would report style as a defect.
1063
- """
1064
- reg = {str(uc.get("id")): bool(uc.get("critical")) for uc in c.ucs}
1065
- reg_pc = {str(uc.get("id")): str(uc.get("component") or "") for uc in c.ucs}
1066
- checked = 0
1067
- for pc in c.pcs:
1068
- pid = str(pc.get("id"))
1069
- path = c.root / f".what/{pid}/SRS-{pid}.md"
1070
- if not path.exists():
1071
- continue
1072
- checked += 1
1073
- text = path.read_text(encoding="utf-8", errors="replace")
1074
- seen: set[str] = set()
1075
- for match in UC_ROW_RE.finditer(text):
1076
- uid = match.group(1)
1077
- cells = [x.strip() for x in match.group(2).split("|")]
1078
- if len(cells) < 4:
1079
- continue
1080
- seen.add(uid)
1081
- if uid not in reg:
1082
- r.fail("V26", f"{pid}/{uid}", "is in the SRS catalogue but not in `usecases.yaml`")
1083
- continue
1084
- if reg_pc[uid] != pid:
1085
- r.fail("V26", f"{pid}/{uid}",
1086
- f"the registry places it in `{reg_pc[uid]}`, not in this component")
1087
- marked = CRITICAL_YES.search(cells[3]) is not None
1088
- if marked != reg[uid]:
1089
- r.fail("V26", f"{pid}/{uid}",
1090
- f"`critical` in the SRS {'yes' if marked else 'no'}, "
1091
- f"in the registry {'yes' if reg[uid] else 'no'}")
1092
- for uid, owner in sorted(reg_pc.items()):
1093
- if owner == pid and uid not in seen:
1094
- r.fail("V26", f"{pid}/{uid}", "is in `usecases.yaml` but not in the SRS catalogue")
1095
- if not checked:
1096
- r.skip("V26", "no SRS could be read")
1097
-
1098
-
1099
- def v27(c: Corpus, r: Result) -> None:
1100
- """Every file in the custom room MUST declare itself, and a rebuttal MUST have a decision.
1101
-
1102
- The `.constitution/project/` room exists so product-specific rules have a home that `update`
1103
- does not overwrite and `promote` does not publish. The cost that comes with it: it is also the
1104
- easiest place to break a generic rule without a trace. Its frontmatter is what holds that back.
1105
-
1106
- A file here MAY narrow or add without naming anything. To REBUT a generic rule it MUST name it
1107
- in `overrides:` and carry a `decision:` because a method that can be rebutted without a
1108
- decision stops being trustworthy in the next repo.
1109
-
1110
- The room's `README.md` is skipped: it is authored in the package, not in the product.
1111
- """
1112
- room = c.root / ".constitution" / "project"
1113
- if not room.is_dir():
1114
- r.skip("V27", "the `.constitution/project/` room does not exist yet it is seeded at install")
1115
- return
1116
- files = [p for p in sorted(room.rglob("*.md")) if p.name != "README.md"]
1117
- if not files:
1118
- r.skip("V27", "the `.constitution/project/` room is empty, and that is a valid state "
1119
- "a generic rule MUST NOT be moved here just to give the room content")
1120
- return
1121
- dec_ids = {str(d.get("id")) for d in c.decs}
1122
- for path in files:
1123
- rel = path.relative_to(c.root).as_posix()
1124
- fm = frontmatter(path)
1125
- if fm is None:
1126
- r.fail("V27", rel, "has no frontmatter")
1127
- continue
1128
- if str(fm.get("scope") or "").strip() != "project":
1129
- r.fail("V27", rel, "`scope:` MUST contain exactly `project`")
1130
- if not str(fm.get("purpose") or "").strip():
1131
- r.fail("V27", rel, "`purpose:` is empty — one line: what this rule guards")
1132
- over = str(fm.get("overrides") or "").strip()
1133
- dec = str(fm.get("decision") or "").strip()
1134
- if over:
1135
- if not (c.root / over).exists():
1136
- r.fail("V27", rel, f"`overrides:` points to `{over}` which does not exist — "
1137
- f"the rebutted rule may already be gone")
1138
- if not dec:
1139
- r.fail("V27", rel, "rebuts a generic rule without `decision:` — "
1140
- "a rebuttal MUST have a `DEC-` that decided it")
1141
- elif dec not in dec_ids:
1142
- r.fail("V27", rel, f"`decision: {dec}` is not registered in decisions.yaml")
1143
- elif dec:
1144
- r.fail("V27", rel, "`decision:` is set without `overrides:` — "
1145
- "name which rule is rebutted, or drop `decision:`")
1146
-
1147
-
1148
- def run_checks(c: Corpus, asof: dt.date) -> Result:
1149
- r = Result()
1150
- for fn in (v1, v2, v3, v4, v5, v6, v7, v8, v9, v11, v12, v13, v15, v16, v17, v18, v19, v20,
1151
- v21, v22, v23, v24, v25, v26, v27):
1152
- fn(c, r)
1153
- v14(c, r, asof)
1154
- return r
1155
-
1156
-
1157
- # ------------------------------------------------------------------ generator
1158
-
1159
-
1160
- def _story_status(c: Corpus, story: dict) -> str:
1161
- folder = str(story.get("spec_folder") or "").strip()
1162
- if not folder:
1163
- return "unknown"
1164
- matches = sorted((c.root / folder / "stories").glob(f"{story.get('id')}-*.md"))
1165
- if not matches:
1166
- return "unknown"
1167
- return str((frontmatter(matches[0]) or {}).get("status") or "unknown")
1168
-
1169
-
1170
- def gen_components(c: Corpus) -> dict:
1171
- return {
1172
- "product_components": [
1173
- {"id": pc.get("id"), "name": pc.get("name"),
1174
- "containers": listy(pc, "containers"),
1175
- "logical_components": sorted(
1176
- str(lc.get("id")) for lc in c.lcs
1177
- if str(lc.get("component")) == str(pc.get("id")))}
1178
- for pc in c.pcs
1179
- ],
1180
- "logical_components": [
1181
- {"id": lc.get("id"), "type": lc.get("type"), "component": lc.get("component"),
1182
- "area": lc.get("area"), "owner": lc.get("owner")}
1183
- for lc in c.lcs
1184
- ],
1185
- }
1186
-
1187
-
1188
- def gen_risks(c: Corpus) -> dict:
1189
- return {"risks": [
1190
- {"id": x.get("id"), "impact": x.get("impact"), "likelihood": x.get("likelihood"),
1191
- "owner": x.get("owner"), "status": x.get("status"),
1192
- "pivot_trigger": x.get("pivot_trigger")}
1193
- for x in rows(c.risks, "risks") if str(x.get("status")) != "closed"
1194
- ]}
1195
-
1196
-
1197
- def gen_dag(c: Corpus) -> dict:
1198
- out = []
1199
- per_wave: dict[str, list[dict]] = {}
1200
- for wave, _, story in c.stories():
1201
- per_wave.setdefault(str(wave.get("id")), []).append(story)
1202
- for wid in sorted(per_wave):
1203
- items = per_wave[wid]
1204
- done: set[str] = set()
1205
- pending = {str(s.get("id")): set(listy(s, "depends_on")) for s in items}
1206
- waves_out = []
1207
- while pending:
1208
- ready = sorted(k for k, deps in pending.items() if not (deps - done))
1209
- if not ready: # cycle V7 has already reported it
1210
- waves_out.append({"blocked": sorted(pending)})
1211
- break
1212
- waves_out.append({"parallel": ready})
1213
- done |= set(ready)
1214
- for k in ready:
1215
- pending.pop(k)
1216
- out.append({"wave": wid, "order": waves_out})
1217
- return {"dag": out}
1218
-
1219
-
1220
- def gen_rtm(c: Corpus) -> dict:
1221
- cap_goal = {str(x.get("id")): str(x.get("goal", "")) for x in c.caps}
1222
- ucs_for_fr: dict[str, list[str]] = {}
1223
- for uc in c.ucs:
1224
- for fr in listy(uc, "satisfies"):
1225
- ucs_for_fr.setdefault(fr, []).append(str(uc.get("id")))
1226
- stories_for_uc: dict[str, list[tuple[dict, dict]]] = {}
1227
- for wave, _, story in c.stories():
1228
- for uc in listy(story, "satisfies"):
1229
- stories_for_uc.setdefault(uc, []).append((wave, story))
1230
- decs_for: dict[str, list[str]] = {}
1231
- for dec in c.decs:
1232
- for target in listy(dec, "serves"):
1233
- decs_for.setdefault(target, []).append(str(dec.get("id")))
1234
-
1235
- lines = []
1236
- for fr in c.frs:
1237
- fid = str(fr.get("id"))
1238
- cap = str(fr.get("capability", ""))
1239
- base = {"BG": cap_goal.get(cap, ""), "CAP": cap, "FR": fid,
1240
- "DEC": sorted(decs_for.get(fid, []))}
1241
- ucs = sorted(ucs_for_fr.get(fid, []))
1242
- if not ucs:
1243
- exempt = bool(str(fr.get("no_uc") or "").strip())
1244
- lines.append({**base, "UC": "", "story": "", "wave": "", "release": "",
1245
- "test": [], "status": "", "green": False,
1246
- "exempt": exempt,
1247
- "broken_at": "no_uc" if exempt else "UC"})
1248
- continue
1249
- for uid in ucs:
1250
- pairs = sorted(stories_for_uc.get(uid, []), key=lambda p: str(p[1].get("id")))
1251
- if not pairs:
1252
- lines.append({**base, "UC": uid, "story": "", "wave": "", "release": "",
1253
- "test": [], "status": "", "green": False, "exempt": False,
1254
- "broken_at": "story"})
1255
- continue
1256
- for wave, story in pairs:
1257
- status = _story_status(c, story)
1258
- tests = listy(story, "tests")
1259
- broken = ""
1260
- if not tests:
1261
- broken = "test"
1262
- elif status != "done":
1263
- broken = "status"
1264
- lines.append({**base, "UC": uid, "story": str(story.get("id")),
1265
- "wave": str(wave.get("id")), "release": str(wave.get("release", "")),
1266
- "test": tests, "status": status, "exempt": False,
1267
- "green": broken == "", "broken_at": broken})
1268
- return {"rtm": lines}
1269
-
1270
-
1271
- def gen_status(c: Corpus, rtm: dict, result: Result) -> dict:
1272
- lines = rtm.get("rtm") or []
1273
- counted = [line for line in lines if not line.get("exempt")]
1274
- exempt = len(lines) - len(counted)
1275
- green = sum(1 for line in counted if line.get("green"))
1276
- per_wave = []
1277
- for wave in c.wave_list:
1278
- wid = str(wave.get("id"))
1279
- items = [s for w, _, s in c.stories() if str(w.get("id")) == wid]
1280
- done = sum(1 for s in items if _story_status(c, s) == "done")
1281
- per_wave.append({"wave": wid, "status": wave.get("status"),
1282
- "stories_done": done, "stories_total": len(items),
1283
- "work_progress": _pct(done, len(items))})
1284
- applicable = 26 # V1..V27 minus V10, which was retired
1285
- return {
1286
- "promise_progress": _pct(green, len(counted)),
1287
- "rtm_rows": {"green": green, "counted": len(counted),
1288
- "excluded_no_uc": exempt},
1289
- "work_progress": per_wave,
1290
- "gate_readiness": _pct(applicable - len(result.red), applicable),
1291
- "validators_red": result.red,
1292
- "validators_skipped": dict(sorted(result.skipped.items())),
1293
- "open_questions": _question_budget(c),
1294
- }
1295
-
1296
-
1297
- def _question_budget(c: Corpus) -> dict:
1298
- """Counts of all four question lists, compared against the budget in index.yaml.
1299
-
1300
- The budget is NOT a hard gate. It is reported when a batch exceeds it, because a larger
1301
- batch is a signal about the pass, not about the corpus.
1302
- """
1303
- budget = c.index.get("question_budget") or {}
1304
- out: dict[str, object] = {}
1305
- for name in ("blocking", "assumptions", "external", "answered"):
1306
- path = c.root / ".control/questions" / f"{name}.md"
1307
- rows_n = 0
1308
- if path.exists():
1309
- rows_n = sum(1 for line in path.read_text(encoding="utf-8", errors="replace").splitlines()
1310
- if line.startswith("| OQ-"))
1311
- out[name] = rows_n
1312
- cap_block = budget.get("blocking_per_component")
1313
- if cap_block and c.pcs:
1314
- allowed = int(cap_block) * len(c.pcs)
1315
- out["blocking_budget"] = allowed
1316
- out["blocking_over_budget"] = out["blocking"] > allowed
1317
- cap_assume = budget.get("assumptions_per_gate")
1318
- if cap_assume:
1319
- out["assumptions_budget_per_gate"] = int(cap_assume)
1320
- return out
1321
-
1322
-
1323
- def _pct(part: int, total: int) -> str:
1324
- return "n/a" if total == 0 else f"{round(100 * part / total)}%"
1325
-
1326
-
1327
- def as_markdown(name: str, payload: dict) -> str:
1328
- body = dump(payload)
1329
- return (f"# {name}\n\n"
1330
- f"> Generated by `.constitution/scripts/validate --generate`. "
1331
- f"MUST NOT be hand-edited.\n\n"
1332
- f"```yaml\n{body}```\n")
1333
-
1334
-
1335
- # ------------------------------------------------------- pages for humans
1336
-
1337
- PAGE_HEADER = ("> Generated by `.constitution/scripts/validate --generate`. "
1338
- "MUST NOT be hand-edited.\n")
1339
-
1340
-
1341
- def _section(path: Path, heading: str) -> str:
1342
- """Extract one `## <heading>` section from a markdown file, as-is."""
1343
- if not path.exists():
1344
- return ""
1345
- lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
1346
- out: list[str] = []
1347
- inside = False
1348
- for line in lines:
1349
- if line.startswith("## "):
1350
- if inside:
1351
- break
1352
- inside = line[3:].strip().lower().startswith(heading.lower())
1353
- continue
1354
- if inside:
1355
- out.append(line)
1356
- return "\n".join(out).strip("\n")
1357
-
1358
-
1359
- def _body(path: Path) -> str:
1360
- """File content without frontmatter and without template comments."""
1361
- if not path.exists():
1362
- return ""
1363
- text = path.read_text(encoding="utf-8", errors="replace")
1364
- match = FM.match(text)
1365
- if match:
1366
- text = text[match.end():]
1367
- while "<!--" in text and "-->" in text:
1368
- head, _, rest = text.partition("<!--")
1369
- _, _, tail = rest.partition("-->")
1370
- text = head + tail
1371
- return text.strip("\n")
1372
-
1373
-
1374
- def page_decisions(c: Corpus) -> str:
1375
- """Flat table of every `DEC-`. This is what replaces looking up decisions through the memlog."""
1376
- rows_out = ["| id | Title | Status | Type | Touches | File |",
1377
- "| --- | --- | --- | --- | --- | --- |"]
1378
- for dec in c.decs:
1379
- touches = ", ".join(f"`{x}`" for x in listy(dec, "touches")) or "—"
1380
- rows_out.append(
1381
- f"| `{dec.get('id')}` | {_cell(dec.get('title'))} | `{dec.get('status', '')}` "
1382
- f"| {dec.get('type') or '—'} | {touches} | `{dec.get('file', '')}` |")
1383
- counts: dict[str, int] = {}
1384
- for dec in c.decs:
1385
- key = str(dec.get("status"))
1386
- counts[key] = counts.get(key, 0) + 1
1387
- tally = " · ".join(f"{k}: {v}" for k, v in sorted(counts.items())) or "no decisions yet"
1388
- return ("# decisions\n\n" + PAGE_HEADER +
1389
- "\nDecisions are no longer looked up through the memlog — the memlog goes back to being just a pass log.\n"
1390
- f"\n**{len(c.decs)} decisions** {tally}.\n\n" + "\n".join(rows_out) + "\n")
1391
-
1392
-
1393
- def page_blueprint(c: Corpus) -> str:
1394
- """One-page roll-up reviewed at G3. Seven files become one read.
1395
-
1396
- The UC catalogue, actor list, and domain model stay put in their own component's kernel as
1397
- their permanent home. This is their view. One fact, one home, one view.
1398
- """
1399
- parts = ["# blueprint\n", PAGE_HEADER,
1400
- "\nThis is what the owner reads at **G3 Blueprint**, instead of seven files. Its "
1401
- "content is affected by neither `mode` nor `risk_accepted`.\n"]
1402
-
1403
- crit = sum(1 for uc in c.ucs if uc.get("critical"))
1404
- parts.append(f"\n## Use case catalogue\n\n**{len(c.ucs)} use cases**, {crit} marked "
1405
- f"`critical`.\n")
1406
- parts.append("| id | Use case | Component | Satisfies | critical |")
1407
- parts.append("| --- | --- | --- | --- | --- |")
1408
- for uc in c.ucs:
1409
- sat = ", ".join(f"`{x}`" for x in listy(uc, "satisfies")) or "—"
1410
- flag = "yes" if uc.get("critical") else "no"
1411
- parts.append(f"| `{uc.get('id')}` | {_cell(uc.get('title'))} | "
1412
- f"`{uc.get('component', '')}` | {sat} | {flag} |")
1413
-
1414
- parts.append("\n## Actor list\n")
1415
- for pc in c.pcs:
1416
- pid = str(pc.get("id"))
1417
- block = _section(c.root / f".what/{pid}/SRS-{pid}.md", "Actor Register")
1418
- parts.append(f"\n### {pid} — {pc.get('name', '')}\n")
1419
- parts.append(_demote(block) if block
1420
- else "_no § Actor Register in this component's SRS yet._")
1421
-
1422
- parts.append("\n## Domain model\n")
1423
- for pc in c.pcs:
1424
- pid = str(pc.get("id"))
1425
- block = _body(c.root / f".what/{pid}/03-domain/domain-model.md")
1426
- parts.append(f"\n### {pid}\n")
1427
- parts.append(_demote(block) if block else "_no `03-domain/domain-model.md` yet._")
1428
-
1429
- parts.append("\n## Three inventories\n")
1430
- for kind, name in (("db", "table"), ("api", "endpoint"), ("screen", "screen")):
1431
- block = _body(c.root / f".how/_platform/inventory-{kind}.md")
1432
- parts.append(f"\n### List of {name}s — `inventory-{kind}.md`\n")
1433
- parts.append(_demote(block) if block else f"_no `inventory-{kind}.md` yet._")
1434
-
1435
- return "\n".join(parts) + "\n"
1436
-
1437
-
1438
- def _cell(value: object, limit: int = 110) -> str:
1439
- """One table row, shortened. The full-length source stays in the registry — this is just a view."""
1440
- text = " ".join(str(value or "").split()).replace("|", "\\|")
1441
- return text if len(text) <= limit else text[: limit - 1].rstrip() + "…"
1442
-
1443
-
1444
- def _demote(block: str, by: int = 2) -> str:
1445
- """Demote the heading level of inlined content, so it does not clash with the roll-up's own structure."""
1446
- out = []
1447
- for line in block.splitlines():
1448
- stripped = line.lstrip()
1449
- if stripped.startswith("#"):
1450
- hashes = len(stripped) - len(stripped.lstrip("#"))
1451
- out.append("#" * min(6, hashes + by) + stripped[hashes:])
1452
- else:
1453
- out.append(line)
1454
- return "\n".join(out)
1455
-
1456
-
1457
- def page_estimate(c: Corpus) -> str:
1458
- """Table of CANDIDATE tasks. One row per `FR`, since that is a wave's ideal shape."""
1459
- mode_of = {str(pc.get("id")): c.mode_of(pc) for pc in c.pcs}
1460
- risk_of = {str(pc.get("id")): (str(pc.get("risk_accepted") or "—"),
1461
- str(pc.get("risk_note") or "—")) for pc in c.pcs}
1462
- cap_by_id = {str(x.get("id")): x for x in c.caps}
1463
- fr_per_cap: dict[str, int] = {}
1464
- for fr in c.frs:
1465
- key = str(fr.get("capability", ""))
1466
- fr_per_cap[key] = fr_per_cap.get(key, 0) + 1
1467
-
1468
- have_mandays = any(x.get("estimate_mandays") for x in c.caps)
1469
- parts = ["# estimate\n", PAGE_HEADER,
1470
- "\n**THIS IS AN ESTIMATE, FORWARD-LOOKING.** Every row below is a **candidate** "
1471
- "task; the wave in `waves.yaml` is the real one. One row MAY become one wave, and three "
1472
- "neighboring rows MAY be merged into one — that merge is a human decision made when the "
1473
- "wave is opened.\n"]
1474
- if not have_mandays:
1475
- parts.append("\n**With no `estimate_mandays` on a single `CAP`**, the Load column is empty and "
1476
- "this output is only as good as a T-shirt-size estimate. It MUST be reported as such.\n")
1477
-
1478
- parts.append("\n| Task | FR | Epic | mode | Exposure | Load | Priority | Depends on | Release |")
1479
- parts.append("| --- | --- | --- | --- | --- | --- | --- | --- | --- |")
1480
- for fr in c.frs:
1481
- cap_id = str(fr.get("capability", ""))
1482
- cap = cap_by_id.get(cap_id, {})
1483
- pid = str(fr.get("component") or cap.get("component") or "")
1484
- risk, note = risk_of.get(pid, ("", ""))
1485
- exposure = "not set yet" if risk == "—" else f"`{risk}` — {_cell(note, 60)}"
1486
- mandays = cap.get("estimate_mandays")
1487
- share = "—"
1488
- if mandays:
1489
- try:
1490
- share = f"{float(mandays) / max(1, fr_per_cap.get(cap_id, 1)):.1f}"
1491
- except (TypeError, ValueError):
1492
- share = ""
1493
- deps = ", ".join(f"`{x}`" for x in listy(cap, "depends_on")) or "—"
1494
- parts.append(
1495
- f"| {_cell(fr.get('text') or fr.get('title'))} | `{fr.get('id')}` | `{pid or '—'}` "
1496
- f"| `{mode_of.get(pid, 'catalog')}` | {exposure} | {share} "
1497
- f"| {cap.get('priority', '—')} | {deps} | {cap.get('target_release', '—')} |")
1498
- return "\n".join(parts) + "\n"
1499
-
1500
-
1501
- def generate(c: Corpus, result: Result) -> list[Path]:
1502
- out_dir = c.root / ".control" / "generated"
1503
- out_dir.mkdir(parents=True, exist_ok=True)
1504
- rtm = gen_rtm(c)
1505
- payloads = {
1506
- "components": gen_components(c),
1507
- "risks": gen_risks(c),
1508
- "dag": gen_dag(c),
1509
- "rtm": rtm,
1510
- "status": gen_status(c, rtm, result),
1511
- }
1512
- written = []
1513
- for name in GENERATED_ORDER:
1514
- payload = payloads[name]
1515
- yaml_path = out_dir / f"{name}.yaml"
1516
- yaml_path.write_text(dump(payload), encoding="utf-8")
1517
- md_path = out_dir / f"{name}.md"
1518
- md_path.write_text(as_markdown(name, payload), encoding="utf-8")
1519
- written += [yaml_path, md_path]
1520
-
1521
- # Three pages for HUMANS: real markdown tables, with no .yaml twin. What people read is
1522
- # not wrapped in a yaml fence, and no machine reader demands a second version of it.
1523
- for name, render in (("decisions", page_decisions),
1524
- ("blueprint", page_blueprint),
1525
- ("estimate", page_estimate)):
1526
- page = out_dir / f"{name}.md"
1527
- page.write_text(render(c), encoding="utf-8")
1528
- written.append(page)
1529
- return written
1530
-
1531
-
1532
- # ------------------------------------------------------------------------ CLI
1533
-
1534
-
1535
- def main(argv: list[str] | None = None) -> int:
1536
- parser = argparse.ArgumentParser(
1537
- prog="validate", description="V1..V27 and the .control/generated/ generator")
1538
- parser.add_argument("--check", action="store_true",
1539
- help="check only; exit non-zero if anything is red")
1540
- parser.add_argument("--generate", action="store_true",
1541
- help="rewrite .control/generated/ (still runs the check first)")
1542
- parser.add_argument("--root", default=".", help="repo root (default: current directory)")
1543
- parser.add_argument("--asof", default=None,
1544
- help="reference date for V14, format YYYY-MM-DD (default: today). "
1545
- "Stated explicitly so a run can be repeated exactly")
1546
- args = parser.parse_args(argv)
1547
-
1548
- if not args.check and not args.generate:
1549
- args.check = True
1550
-
1551
- root = Path(args.root).resolve()
1552
- if not (root / ".control" / "registry").is_dir():
1553
- print(f"validate: {root} has no .control/registry/ — wrong repo root?", file=sys.stderr)
1554
- return 2
1555
-
1556
- asof = dt.date.fromisoformat(args.asof) if args.asof else dt.date.today()
1557
- corpus = Corpus.load(root)
1558
- result = run_checks(corpus, asof)
1559
-
1560
- if args.generate:
1561
- for path in generate(corpus, result):
1562
- print(f" wrote {path.relative_to(root).as_posix()}")
1563
-
1564
- if result.findings:
1565
- print(f"\nRED — {len(result.findings)} findings across {len(result.red)} validators\n")
1566
- for finding in sorted(result.findings, key=lambda f: f.sort_key):
1567
- print(f" {finding.vid:<4} {finding.subject}: {finding.message}")
1568
- else:
1569
- print("\nGREEN no findings")
1570
-
1571
- if result.skipped:
1572
- print("\nSkipped:")
1573
- for vid, why in sorted(result.skipped.items()):
1574
- print(f" {vid:<4} {why}")
1575
-
1576
- print(f"\nV14 reference date: {asof.isoformat()}")
1577
- return 1 if result.findings else 0
1578
-
1579
-
1580
- if __name__ == "__main__":
1581
- raise SystemExit(main())
1
+ #!/usr/bin/env -S uv run --script
2
+ # /// script
3
+ # requires-python = ">=3.11"
4
+ # dependencies = ["pyyaml>=6"]
5
+ # ///
6
+ """validate — V1..V27 plus the .control/generated/ generator.
7
+
8
+ Two modes:
9
+ validate --check exit non-zero if anything is red; writes nothing
10
+ validate --generate rewrite .control/generated/ (and still runs --check)
11
+
12
+ Determinism is the contract: two runs over the same data MUST produce the same result.
13
+ That is why there is no unordered iteration, and the one time-dependent input
14
+ (--asof, used by V14) is stated explicitly instead of being taken silently from the wall clock.
15
+
16
+ What is NOT done here: the time dimension from git. `generated/timeline` and
17
+ `generated/report` belong to wdi-report. See 08-project-management.md.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import argparse
23
+ import datetime as dt
24
+ import os
25
+ import re
26
+ import subprocess
27
+ import sys
28
+ from dataclasses import dataclass, field
29
+ from pathlib import Path
30
+
31
+ import yaml
32
+
33
+ REGISTRY = "control/registry" # tidied up in resolve(); '.control' is what is actually used
34
+ GENERATED_ORDER = ["components", "risks", "dag", "rtm", "status"]
35
+
36
+ # Pages read by HUMANS, not machines: written as real markdown tables, not yaml
37
+ # in a fence. All three are named in §22 and each has one clear reader.
38
+ GENERATED_PAGES = ["decisions", "blueprint", "estimate"]
39
+
40
+ MODES = ("catalog", "outline", "guarded", "deep")
41
+
42
+ # Keywords that make a component "sensitive" for V23. Matched against `risk_note`, which is PROSE in
43
+ # whatever `policy.doc_language` the product chose so the set is the UNION of both languages rather
44
+ # than a translation. It leans toward disclosing more, which is what this check is for: it discloses,
45
+ # it does not judge. Deliberately short.
46
+ SENSITIVE_MARKERS = (
47
+ # English
48
+ "money", "payment", "personal data", "pii",
49
+ "irreversible", "cannot be undone", "contractual", "contract", "integration",
50
+ # Bahasa Indonesia
51
+ "uang", "pembayaran", "data pribadi",
52
+ "tak-terbalikkan", "tak terbalikkan", "tidak dapat dibatalkan",
53
+ "kontraktual", "kontrak", "integrasi",
54
+ )
55
+
56
+
57
+ # ---------------------------------------------------------------- infrastructure
58
+
59
+
60
+ @dataclass(frozen=True)
61
+ class Finding:
62
+ vid: str
63
+ subject: str
64
+ message: str
65
+
66
+ @property
67
+ def sort_key(self) -> tuple[int, str, str]:
68
+ digits = "".join(ch for ch in self.vid if ch.isdigit())
69
+ return (int(digits or 0), self.subject, self.message)
70
+
71
+
72
+ @dataclass
73
+ class Result:
74
+ findings: list[Finding] = field(default_factory=list)
75
+ skipped: dict[str, str] = field(default_factory=dict)
76
+
77
+ def fail(self, vid: str, subject: str, message: str) -> None:
78
+ self.findings.append(Finding(vid, subject, message))
79
+
80
+ def skip(self, vid: str, why: str) -> None:
81
+ self.skipped[vid] = why
82
+
83
+ @property
84
+ def red(self) -> list[str]:
85
+ return sorted({f.vid for f in self.findings})
86
+
87
+
88
+ def load_yaml(path: Path) -> dict:
89
+ if not path.exists():
90
+ return {}
91
+ data = yaml.safe_load(path.read_text(encoding="utf-8"))
92
+ return data if isinstance(data, dict) else {}
93
+
94
+
95
+ def rows(data: dict, key: str) -> list[dict]:
96
+ """Registry list, always sorted by id so the output is deterministic."""
97
+ value = data.get(key) or []
98
+ if not isinstance(value, list):
99
+ return []
100
+ items = [v for v in value if isinstance(v, dict)]
101
+ return sorted(items, key=lambda r: str(r.get("id", "")))
102
+
103
+
104
+ FM = re.compile(r"\A---\s*\n(.*?)\n---\s*(\n|\Z)", re.S)
105
+
106
+
107
+ class Dumper(yaml.SafeDumper):
108
+ """No anchors/aliases: output MUST be readable and diffable line by line."""
109
+
110
+ def ignore_aliases(self, data) -> bool: # noqa: ARG002
111
+ return True
112
+
113
+
114
+ def dump(payload: dict) -> str:
115
+ return yaml.dump(payload, Dumper=Dumper, allow_unicode=True, sort_keys=False,
116
+ default_flow_style=False, width=100)
117
+
118
+
119
+ def frontmatter(path: Path) -> dict | None:
120
+ """None if the file does not exist; {} if it exists but has no frontmatter."""
121
+ if not path.exists():
122
+ return None
123
+ match = FM.match(path.read_text(encoding="utf-8", errors="replace"))
124
+ if not match:
125
+ return {}
126
+ data = yaml.safe_load(match.group(1))
127
+ return data if isinstance(data, dict) else {}
128
+
129
+
130
+ def git(root: Path, *args: str) -> str | None:
131
+ try:
132
+ out = subprocess.run(
133
+ ["git", "-C", str(root), *args],
134
+ capture_output=True, text=True, timeout=30, check=False,
135
+ )
136
+ except (OSError, subprocess.SubprocessError):
137
+ return None
138
+ return out.stdout.strip() if out.returncode == 0 else None
139
+
140
+
141
+ # ------------------------------------------------------------------- loading
142
+
143
+
144
+ @dataclass
145
+ class Corpus:
146
+ root: Path
147
+ requirements: dict
148
+ usecases: dict
149
+ decisions: dict
150
+ risks: dict
151
+ components: dict
152
+ waves: dict
153
+ defects: dict
154
+ index: dict
155
+
156
+ @classmethod
157
+ def load(cls, root: Path) -> "Corpus":
158
+ reg = root / ".control" / "registry"
159
+ return cls(
160
+ root=root,
161
+ requirements=load_yaml(reg / "requirements.yaml"),
162
+ usecases=load_yaml(reg / "usecases.yaml"),
163
+ decisions=load_yaml(reg / "decisions.yaml"),
164
+ risks=load_yaml(reg / "risks.yaml"),
165
+ components=load_yaml(reg / "components.yaml"),
166
+ waves=load_yaml(reg / "waves.yaml"),
167
+ defects=load_yaml(reg / "defects.yaml"),
168
+ index=load_yaml(reg / "index.yaml"),
169
+ )
170
+
171
+ # --- shortcuts used repeatedly
172
+ @property
173
+ def goals(self) -> list[dict]:
174
+ return rows(self.requirements, "goals")
175
+
176
+ @property
177
+ def caps(self) -> list[dict]:
178
+ return rows(self.requirements, "capabilities")
179
+
180
+ @property
181
+ def frs(self) -> list[dict]:
182
+ return rows(self.requirements, "functional")
183
+
184
+ @property
185
+ def nfrs(self) -> list[dict]:
186
+ return rows(self.requirements, "nonfunctional")
187
+
188
+ @property
189
+ def ucs(self) -> list[dict]:
190
+ return rows(self.usecases, "usecases")
191
+
192
+ @property
193
+ def decs(self) -> list[dict]:
194
+ return rows(self.decisions, "decisions")
195
+
196
+ def mode_of(self, pc: dict) -> str:
197
+ """Per-component `mode` wins over the global one; with neither, default `catalog`."""
198
+ own = str(pc.get("mode") or "").strip()
199
+ if own:
200
+ return own
201
+ return str(self.index.get("mode") or "").strip() or "catalog"
202
+
203
+ @property
204
+ def lcs(self) -> list[dict]:
205
+ return rows(self.components, "logical_components")
206
+
207
+ @property
208
+ def pcs(self) -> list[dict]:
209
+ return rows(self.components, "product_components")
210
+
211
+ @property
212
+ def wave_list(self) -> list[dict]:
213
+ return rows(self.waves, "waves")
214
+
215
+ @property
216
+ def defect_list(self) -> list[dict]:
217
+ return rows(self.defects, "defects")
218
+
219
+ def stories(self) -> list[tuple[dict, dict, dict]]:
220
+ """(wave, epic, story) — sorted by id at each level."""
221
+ out = []
222
+ for wave in self.wave_list:
223
+ for epic in sorted(wave.get("epics") or [], key=lambda e: str(e.get("id", ""))):
224
+ if not isinstance(epic, dict):
225
+ continue
226
+ for story in sorted(epic.get("stories") or [], key=lambda s: str(s.get("id", ""))):
227
+ if isinstance(story, dict):
228
+ out.append((wave, epic, story))
229
+ return out
230
+
231
+
232
+ def listy(row: dict, key: str) -> list[str]:
233
+ value = row.get(key) or []
234
+ if isinstance(value, str):
235
+ return [value]
236
+ return [str(v) for v in value if v is not None]
237
+
238
+
239
+ # ------------------------------------------------------------------ validators
240
+
241
+
242
+ def v1(c: Corpus, r: Result) -> None:
243
+ """Every BG has >=1 FR through its CAP, OR states its reason in `no_fr`.
244
+
245
+ A goal MAY be satisfied by an **invariant** rather than a feature. `BG-6` the data and
246
+ deployment foundation can be extended without being torn down is measured by two architectural
247
+ properties that its own `measure` names, and no `FR` can carry it without being invented. Demanding
248
+ one `FR` there produces a false promise, and a false promise is more expensive than a finding.
249
+
250
+ The escape MUST carry a reason, not a boolean — the same shape as `no_uc` on `FR` (V2).
251
+ """
252
+ cap_by_goal: dict[str, list[str]] = {}
253
+ for cap in c.caps:
254
+ cap_by_goal.setdefault(str(cap.get("goal", "")), []).append(str(cap.get("id")))
255
+ fr_caps = {str(fr.get("capability", "")) for fr in c.frs}
256
+ for goal in c.goals:
257
+ gid = str(goal.get("id"))
258
+ reachable = [cid for cid in cap_by_goal.get(gid, []) if cid in fr_caps]
259
+ if reachable:
260
+ continue
261
+ if str(goal.get("no_fr") or "").strip():
262
+ continue
263
+ r.fail("V1", gid, "has no FR through its CAP and states no reason in `no_fr`")
264
+
265
+
266
+ def v2(c: Corpus, r: Result) -> None:
267
+ covered = {fr for uc in c.ucs for fr in listy(uc, "satisfies")}
268
+ for fr in c.frs:
269
+ fid = str(fr.get("id"))
270
+ if fid in covered:
271
+ continue
272
+ if str(fr.get("no_uc") or "").strip():
273
+ continue
274
+ r.fail("V2", fid, "has no UC and states no reason in `no_uc`")
275
+
276
+
277
+ def v3(c: Corpus, r: Result) -> None:
278
+ """A UC on a component that a wave has ALREADY touched MUST be scheduled to a story.
279
+
280
+ The old shape demanded this of EVERY UC, at any time. Before the first wave that meant the
281
+ entire catalogue was reported red 56 findings out of 62, and those 56 were the correct state,
282
+ not drift: a story is born in a wave, and there was no wave yet. A validator that drowns six real
283
+ findings under fifty-six expected ones stops being read, and a validator that is not read
284
+ guards nothing.
285
+
286
+ What is guarded now is the actual omission: a wave touches a component, and a UC of that
287
+ component is left behind without a story. Full coverage of the whole catalogue is a G5 question,
288
+ and `wdi-build` owns it — the same way V12 was shifted to wave closing.
289
+ """
290
+ scheduled = {uc for _, _, s in c.stories() for uc in listy(s, "satisfies")}
291
+ touched = {str(s.get("component")) for _, _, s in c.stories() if s.get("component")}
292
+ if not c.wave_list:
293
+ r.skip("V3", "no wave yet, so no story yet — every unscheduled UC is the correct "
294
+ "state. Full catalogue coverage is checked at G5")
295
+ return
296
+ for uc in c.ucs:
297
+ uid = str(uc.get("id"))
298
+ if uid in scheduled or str(uc.get("component")) not in touched:
299
+ continue
300
+ r.fail("V3", uid, f"component `{uc.get('component')}` has already been touched by a wave, "
301
+ f"but this UC is not scheduled to any story")
302
+
303
+
304
+ def v4(c: Corpus, r: Result) -> None:
305
+ for _, _, story in c.stories():
306
+ if not [t for t in listy(story, "tests") if t.strip()]:
307
+ r.fail("V4", str(story.get("id")), "has not one named test")
308
+
309
+
310
+ def v5(c: Corpus, r: Result) -> None:
311
+ """Every NFR has an enforcer, OR states its reason in `no_enforcer`.
312
+
313
+ Two NFRs in this repo cannot have an enforcer, and both are valid: one has already been
314
+ **retired**, and the other states of itself that it is a **design measure, not a gate**. Demanding
315
+ a test for both produces a test that cannot fail, and a test that cannot fail is theater.
316
+ """
317
+ for nfr in c.nfrs:
318
+ if [e for e in listy(nfr, "enforced_by") if e.strip()]:
319
+ continue
320
+ if str(nfr.get("no_enforcer") or "").strip():
321
+ continue
322
+ r.fail("V5", str(nfr.get("id")),
323
+ "has no enforcer in `enforced_by` and states no reason in `no_enforcer`")
324
+
325
+
326
+ def v6(c: Corpus, r: Result) -> None:
327
+ defined: set[str] = set()
328
+ for group in (c.goals, c.caps, c.frs, c.nfrs, c.ucs, c.decs, c.lcs, c.pcs,
329
+ rows(c.requirements, "journeys"), rows(c.risks, "risks"), c.defect_list):
330
+ defined |= {str(row.get("id")) for row in group if row.get("id") is not None}
331
+ for wave in c.wave_list:
332
+ defined.add(str(wave.get("id")))
333
+ for _, epic, story in c.stories():
334
+ defined.add(str(epic.get("id")))
335
+ defined.add(str(story.get("id")))
336
+
337
+ refs: list[tuple[str, str]] = []
338
+ for cap in c.caps:
339
+ refs.append((str(cap.get("id")), str(cap.get("goal", ""))))
340
+ refs += [(str(cap.get("id")), d) for d in listy(cap, "depends_on")]
341
+ for fr in c.frs:
342
+ refs.append((str(fr.get("id")), str(fr.get("capability", ""))))
343
+ for nfr in c.nfrs:
344
+ refs.append((str(nfr.get("id")), str(nfr.get("goal", ""))))
345
+ for uc in c.ucs:
346
+ refs += [(str(uc.get("id")), f) for f in listy(uc, "satisfies")]
347
+ for dec in c.decs:
348
+ refs += [(str(dec.get("id")), s) for s in listy(dec, "serves")]
349
+ for defect in c.defect_list:
350
+ refs += [(str(defect.get("id")), v) for v in listy(defect, "violates")]
351
+ for _, _, story in c.stories():
352
+ refs += [(str(story.get("id")), u) for u in listy(story, "satisfies")]
353
+ refs += [(str(story.get("id")), d) for d in listy(story, "depends_on")]
354
+
355
+ for owner, target in sorted(set(refs)):
356
+ if target and target not in defined:
357
+ r.fail("V6", owner, f"points to `{target}` which does not exist in any registry")
358
+
359
+
360
+ def _cycles(graph: dict[str, list[str]]) -> list[str]:
361
+ state: dict[str, int] = {}
362
+ bad: list[str] = []
363
+
364
+ def walk(node: str) -> None:
365
+ state[node] = 1
366
+ for nxt in sorted(graph.get(node, [])):
367
+ if state.get(nxt) == 1:
368
+ bad.append(node)
369
+ elif state.get(nxt) is None and nxt in graph:
370
+ walk(nxt)
371
+ state[node] = 2
372
+
373
+ for node in sorted(graph):
374
+ if state.get(node) is None:
375
+ walk(node)
376
+ return sorted(set(bad))
377
+
378
+
379
+ def v7(c: Corpus, r: Result) -> None:
380
+ caps = {str(x.get("id")): listy(x, "depends_on") for x in c.caps}
381
+ for node in _cycles(caps):
382
+ r.fail("V7", node, "is part of a `depends_on` cycle among CAPs")
383
+ stories = {str(s.get("id")): listy(s, "depends_on") for _, _, s in c.stories()}
384
+ for node in _cycles(stories):
385
+ r.fail("V7", node, "is part of a `depends_on` cycle among stories")
386
+
387
+
388
+ def v8(c: Corpus, r: Result) -> None:
389
+ """Every `applied` decision names a non-empty `touches`.
390
+
391
+ Replaces the old shape "every accepted decision serves >=1 FR/NFR". A decision like
392
+ "the filter MUST work like this" serves no FR at all, and that is VALID it is exactly
393
+ decisions like that which most need remembering, and the old rule discarded them.
394
+ """
395
+ for dec in c.decs:
396
+ if str(dec.get("status")) != "applied":
397
+ continue
398
+ if not [x for x in listy(dec, "touches") if str(x).strip()]:
399
+ r.fail("V8", str(dec.get("id")),
400
+ "is applied but `touches` is empty — an application with no file trace")
401
+
402
+
403
+ def v9(c: Corpus, r: Result) -> None:
404
+ passed = {str(g) for g in (c.index.get("gates_passed") or [])}
405
+ for path in sorted(c.root.glob(".what/**/*.md")) + sorted(c.root.glob(".how/**/*.md")):
406
+ fm = frontmatter(path) or {}
407
+ if str(fm.get("status")) != "locked":
408
+ continue
409
+ gate = str(fm.get("locked_at_gate") or "")
410
+ if gate not in passed:
411
+ rel = path.relative_to(c.root).as_posix()
412
+ r.fail("V9", rel, f"is locked but gate `{gate or '?'}` is not recorded as passed")
413
+
414
+
415
+ def v11(c: Corpus, r: Result) -> None:
416
+ per_wave: dict[str, list[dict]] = {}
417
+ for wave, _, story in c.stories():
418
+ per_wave.setdefault(str(wave.get("id")), []).append(story)
419
+
420
+ for wid in sorted(per_wave):
421
+ items = per_wave[wid]
422
+ edges = {str(s.get("id")): set(listy(s, "depends_on")) for s in items}
423
+
424
+ def reaches(a: str, b: str, seen: set[str] | None = None) -> bool:
425
+ seen = seen or set()
426
+ if a in seen:
427
+ return False
428
+ seen.add(a)
429
+ if b in edges.get(a, set()):
430
+ return True
431
+ return any(reaches(n, b, seen) for n in sorted(edges.get(a, set())))
432
+
433
+ for i, left in enumerate(items):
434
+ for right in items[i + 1:]:
435
+ lid, rid = str(left.get("id")), str(right.get("id"))
436
+ shared = sorted(set(listy(left, "touches")) & set(listy(right, "touches")))
437
+ if not shared:
438
+ continue
439
+ if reaches(lid, rid) or reaches(rid, lid):
440
+ continue
441
+ r.fail("V11", f"{lid} + {rid}",
442
+ f"share touches {shared} with no depends_on relation — MUST NOT run in parallel")
443
+
444
+
445
+ def v12(c: Corpus, r: Result) -> None:
446
+ """LC registration is checked when a wave CLOSES, not before a story goes `ready-for-dev`.
447
+
448
+ The old shape demanded the answer when the information was thinnest. At wave closing,
449
+ every `touches` already has an area and every boundary already has a name.
450
+ """
451
+ areas = {str(lc.get("area")) for lc in c.lcs if lc.get("area")}
452
+ lcs_per_pc: dict[str, int] = {}
453
+ for lc in c.lcs:
454
+ lcs_per_pc[str(lc.get("component"))] = lcs_per_pc.get(str(lc.get("component")), 0) + 1
455
+ pc_by_id = {str(x.get("id")): x for x in c.pcs}
456
+
457
+ seen: set[tuple[str, str]] = set()
458
+ for wave, _, story in c.stories():
459
+ if str(wave.get("status")) != "closed":
460
+ continue
461
+ for area in listy(story, "touches"):
462
+ if area not in areas:
463
+ r.fail("V12", str(story.get("id")),
464
+ f"its wave is already closed, but `{area}` is not registered as an `area` "
465
+ f"in components.yaml")
466
+ pid = str(story.get("component") or "")
467
+ row = pc_by_id.get(pid)
468
+ if row is None or (str(wave.get("id")), pid) in seen:
469
+ continue
470
+ seen.add((str(wave.get("id")), pid))
471
+ if c.mode_of(row) in ("guarded", "deep") and not lcs_per_pc.get(pid):
472
+ r.fail("V12", f"{wave.get('id')} / {pid}",
473
+ f"wave closed and component with mode `{c.mode_of(row)}` has not one "
474
+ f"`LC` registered")
475
+
476
+
477
+ LENS_BY_RISK = {
478
+ "low": {"edge-case-hunter"},
479
+ "medium": {"edge-case-hunter"},
480
+ "high": set(),
481
+ }
482
+ FRONTMATTER_KEYS = ("reviewed:", "date:", "sha:", "lenses:", "updated:")
483
+
484
+
485
+ def _reviewed_ok(r: Result, rel: str, block: object, need: set[str]) -> None:
486
+ # str() before the truth test: an unquoted sha of all digits — `0000000`, and roughly one
487
+ # short sha in twenty-seven is all digits — is read by YAML as the INTEGER 0, which is falsy.
488
+ # The old test then reported "carries no reviewed trace" about a file that plainly carries one,
489
+ # which is the worst kind of finding: correct-looking, and wrong.
490
+ if not isinstance(block, dict):
491
+ r.fail("V13", rel, "carries no `reviewed` trace with a date and sha")
492
+ return
493
+ # NOT `block.get("sha") or ""` — for the integer 0 that yields "" and reintroduces the very
494
+ # bug this guards. `.get(key, "")` returns the 0, and str(0) is "0", which is truthy.
495
+ if not str(block.get("sha", "")).strip() or not str(block.get("date", "")).strip():
496
+ r.fail("V13", rel, "carries no `reviewed` trace with a date and sha")
497
+ return
498
+ lenses = {str(x) for x in (block.get("lenses") or [])}
499
+ if not lenses:
500
+ r.fail("V13", rel, "the `reviewed` trace names not one lens")
501
+ missing = sorted(need - lenses)
502
+ if missing:
503
+ r.fail("V13", rel,
504
+ f"lenses {missing} MUST be included — that is what the component's `risk_accepted` demands")
505
+
506
+
507
+ def _only_reviewed_block(diff: str) -> bool:
508
+ """True if a commit's diff on one file ONLY touches the `reviewed:` block.
509
+
510
+ This is the OQ-146 fix. The old V13 compared `sha` against the last commit that changed
511
+ the file but the commit that WRITES the `reviewed:` block always changes the file, and
512
+ writing its own hash into a git commit is cryptographically impossible. As a result every
513
+ artifact that had just been stamped immediately read as "stale review", forever.
514
+ """
515
+ touched = [ln for ln in diff.splitlines()
516
+ if ln[:1] in "+-" and not ln.startswith("+++") and not ln.startswith("---")]
517
+ if not touched:
518
+ return True
519
+ for ln in touched:
520
+ body = ln[1:].strip()
521
+ if not body or body.startswith("#"):
522
+ continue
523
+ if not body.startswith(FRONTMATTER_KEYS):
524
+ return False
525
+ return True
526
+
527
+
528
+ def _stale_since(c: Corpus, rel: str, sha: str) -> str | None:
529
+ """First commit after `sha` that changes this file for a reason other than a review stamp."""
530
+ log = git(c.root, "log", "--format=%H", f"{sha}..HEAD", "--", rel)
531
+ if not log:
532
+ return None
533
+ for head in log.splitlines():
534
+ head = head.strip()
535
+ if not head:
536
+ continue
537
+ diff = git(c.root, "show", "--format=", "--unified=0", head, "--", rel)
538
+ if diff is None:
539
+ return head
540
+ if _only_reviewed_block(diff):
541
+ continue
542
+ return head
543
+ return None
544
+
545
+
546
+ def v13(c: Corpus, r: Result) -> None:
547
+ """Review trace follows review INTENSITY, not document depth.
548
+
549
+ Narrowed to components with `risk_accepted` `low` or `medium`. At `high` the owner has already
550
+ stated they accept the risk, and demanding a trace there is bookkeeping with no buyer.
551
+ """
552
+ watched = [pc for pc in c.pcs
553
+ if str(pc.get("risk_accepted") or "").strip() in ("low", "medium")]
554
+ if not watched:
555
+ r.skip("V13", "no component with risk_accepted low or medium nothing to guard")
556
+ targets: list[tuple[Path, set[str]]] = []
557
+ if watched:
558
+ targets.append((c.root / ".how/_platform/ARCHITECTURE-SPINE.md", set()))
559
+ for pc in watched:
560
+ pid = str(pc.get("id"))
561
+ need = LENS_BY_RISK.get(str(pc.get("risk_accepted")).strip(), set())
562
+ # The SRS exists and is meaningful at EVERY mode: it carries the Actor Register and UC
563
+ # Catalogue, and both are born at G3, which the depth knob does not touch.
564
+ targets.append((c.root / f".what/{pid}/SRS-{pid}.md", need))
565
+ # The SDD is guarded only when it HAS content worth guarding. Two states exempt it, and
566
+ # both are FINISHED states, not neglected ones:
567
+ # mode: catalog the skeleton is its final form; G4 is skipped there
568
+ # g4_passed not set G4 has not run yet, so not one section is written
569
+ # Demanding a review trace on a file whose content is 13 lines of template comments is
570
+ # theater — exactly the ceremony this redesign cut, and a review that cannot fail proves
571
+ # nothing. Once G4 passes, the demand comes back and it is meaningful.
572
+ passed = str(pc.get("g4_passed") or "").strip().lower()
573
+ if c.mode_of(pc) != "catalog" and passed not in ("", "false", "no", "belum"):
574
+ targets.append((c.root / f".how/{pid}/SDD-{pid}.md", need))
575
+
576
+ for path, need in targets:
577
+ fm = frontmatter(path)
578
+ if fm is None:
579
+ continue # not born yet — not V13's business
580
+ rel = path.relative_to(c.root).as_posix()
581
+ _reviewed_ok(r, rel, fm.get("reviewed"), need)
582
+ block = fm.get("reviewed")
583
+ if isinstance(block, dict) and block.get("sha"):
584
+ stale = _stale_since(c, rel, str(block["sha"]))
585
+ if stale:
586
+ r.fail("V13", rel,
587
+ f"changed at {stale[:7]} after being reviewed at {str(block['sha'])[:7]} — "
588
+ f"stale review")
589
+
590
+ for wave in c.wave_list:
591
+ if not wave.get("epics"):
592
+ continue
593
+ _reviewed_ok(r, f"waves.yaml:{wave.get('id')}", wave.get("spec_reviewed"),
594
+ {"edge-case-hunter"})
595
+
596
+
597
+ def cap_stories(c: Corpus) -> dict[str, list[dict]]:
598
+ """CAP -> story, traced through CAP -> FR -> UC -> story. No git, no timeline."""
599
+ frs_of: dict[str, list[str]] = {}
600
+ for fr in c.frs:
601
+ frs_of.setdefault(str(fr.get("capability", "")), []).append(str(fr.get("id")))
602
+ ucs_of: dict[str, list[str]] = {}
603
+ for uc in c.ucs:
604
+ for fid in listy(uc, "satisfies"):
605
+ ucs_of.setdefault(fid, []).append(str(uc.get("id")))
606
+ out: dict[str, list[dict]] = {}
607
+ for cap in c.caps:
608
+ cid = str(cap.get("id"))
609
+ wanted = {u for fid in frs_of.get(cid, []) for u in ucs_of.get(fid, [])}
610
+ out[cid] = [s for _, _, s in c.stories()
611
+ if wanted & set(listy(s, "satisfies"))]
612
+ return out
613
+
614
+
615
+ def v14(c: Corpus, r: Result, asof: dt.date) -> None:
616
+ """Overdue-ness is computed from the registry itself — the timeline only reinforces, never gates."""
617
+ by_cap = cap_stories(c)
618
+ timeline = load_yaml(c.root / ".control/generated/timeline.yaml")
619
+ listed = {str(row.get("id")) for row in rows(timeline, "capabilities")
620
+ if str(row.get("state")) == "overdue"} if timeline else None
621
+ if listed is None:
622
+ r.skip("V14", "generated/timeline.yaml does not exist yet — overdue-ness is still computed "
623
+ "from the registry, but its presence in generated/report is not checked")
624
+
625
+ for cap in c.caps:
626
+ cid = str(cap.get("id"))
627
+ end = str(cap.get("planned_end") or "")
628
+ if not end:
629
+ continue
630
+ try:
631
+ due = dt.date.fromisoformat(end)
632
+ except ValueError:
633
+ r.fail("V14", cid, f"`planned_end` `{end}` is not an ISO date")
634
+ continue
635
+ items = by_cap.get(cid, [])
636
+ closed = bool(items) and all(_story_status(c, s) == "done" for s in items)
637
+ if closed or due >= asof:
638
+ continue
639
+ late = (asof - due).days
640
+ if listed is not None and cid not in listed:
641
+ r.fail("V14", cid, f"{late} days overdue with nothing delivered, and not flagged "
642
+ f"`overdue` in generated/timeline")
643
+ else:
644
+ r.fail("V14", cid, f"{late} days overdue with nothing closed")
645
+
646
+
647
+ def v15(c: Corpus, r: Result) -> None:
648
+ for cap in c.caps:
649
+ if not str(cap.get("goal") or "").strip():
650
+ r.fail("V15", str(cap.get("id")), "does not point to a `goal`")
651
+ for fr in c.frs:
652
+ if not str(fr.get("capability") or "").strip():
653
+ r.fail("V15", str(fr.get("id")), "does not point to a `capability`")
654
+
655
+
656
+ def v16(c: Corpus, r: Result) -> None:
657
+ for path in sorted((c.root / ".control/memlog").glob("*.md")):
658
+ fm = frontmatter(path) or {}
659
+ rel = path.relative_to(c.root).as_posix()
660
+ artifact = str(fm.get("artifact") or "")
661
+ if not artifact:
662
+ r.fail("V16", rel, "has no `artifact:` in frontmatter")
663
+ elif not (c.root / artifact).exists():
664
+ r.fail("V16", rel, f"`artifact:` points to `{artifact}` which does not exist")
665
+ for layer in (".what", ".how"):
666
+ for stray in sorted(c.root.glob(f"{layer}/**/.memlog.md")):
667
+ r.fail("V16", stray.relative_to(c.root).as_posix(),
668
+ "a memlog MUST NOT live inside the corpus")
669
+
670
+
671
+ def v17(c: Corpus, r: Result) -> None:
672
+ for wave in c.wave_list:
673
+ wid = str(wave.get("id"))
674
+ if not str(wave.get("release") or "").strip():
675
+ r.fail("V17", wid, "does not name a `release`")
676
+ slugs = listy(wave, "prd")
677
+ if not slugs:
678
+ r.fail("V17", wid, "does not name a `prd`")
679
+ for slug in slugs:
680
+ if not (c.root / ".what/_prd" / slug).is_dir():
681
+ r.fail("V17", wid, f"`prd: {slug}` has no folder .what/_prd/{slug}/")
682
+
683
+
684
+ def v18(c: Corpus, r: Result) -> None:
685
+ for _, _, story in c.stories():
686
+ sid = str(story.get("id"))
687
+ folder = str(story.get("spec_folder") or "").strip()
688
+ if not folder:
689
+ r.fail("V18", sid, "does not name a `spec_folder`")
690
+ continue
691
+ matches = sorted((c.root / folder / "stories").glob(f"{sid}-*.md"))
692
+ if not matches:
693
+ r.fail("V18", sid, f"has no story file in {folder}stories/")
694
+ continue
695
+ fm = frontmatter(matches[0]) or {}
696
+ if not str(fm.get("status") or "").strip():
697
+ r.fail("V18", sid, "story file has no `status` in frontmatter")
698
+
699
+
700
+ def v19(c: Corpus, r: Result) -> None:
701
+ """The retrospective archive is tied to WAVE SIZE, not to `mode`.
702
+
703
+ Mandatory on wave `L`; advisory on `S` and `M`. Document depth and volume of work are two
704
+ different things, and demanding a retrospective for a three-story wave is ceremony.
705
+ """
706
+ names = [x.name for x in sorted((c.root / ".control/reports").glob("RTR-*"))]
707
+ advisory: list[str] = []
708
+ for wave in c.wave_list:
709
+ if str(wave.get("status")) != "closed":
710
+ continue
711
+ wid = str(wave.get("id"))
712
+ if any(wid in name for name in names):
713
+ continue
714
+ if str(wave.get("size")).upper() == "L":
715
+ r.fail("V19", wid, "wave `L` closed without an `RTR-` in .control/reports/")
716
+ else:
717
+ advisory.append(wid)
718
+ if advisory:
719
+ r.skip("V19", "advisory wave S/M closed without an RTR-: " + ", ".join(sorted(advisory)))
720
+ else:
721
+ r.skip("V19", "only the RTR- line item is checked mechanically; the rest of the distillation is guarded by wdi-build")
722
+
723
+
724
+ PLATFORM = "_platform"
725
+ CROSS_CUTTING = ".how/_platform/cross-cutting.md"
726
+ # The section heading V21 looks for. A heading a SCRIPT matches is a machine-facing key, and
727
+ # `language-guide.md` says a key is always English — so the template writes the English one and
728
+ # this is what a new corpus carries. The Indonesian form is kept as a READER-side alias, exactly
729
+ # like `yes|ya`: a corpus written before this MUST NOT be migrated for a regex.
730
+ PLATFORM_DATA_HEADINGS = ("Platform-owned", "Milik platform")
731
+ PLATFORM_DATA_HEADING = PLATFORM_DATA_HEADINGS[0]
732
+
733
+
734
+ def v21(c: Corpus, r: Result) -> None:
735
+ """One domain entity has EXACTLY ONE owner authorized to write it.
736
+
737
+ The owner is a Product Component, OR `_platform` for an entity with no single component
738
+ promise behind it. Semantic collisions across PRDs have already happened for real: one
739
+ component took a business-rule numbering range from a shared global sequence. Two `FR`s
740
+ that both claim write authority over the same entity, with neither pointing at the other,
741
+ are a defect the moment they are written.
742
+
743
+ `_platform` is NOT a Product Component and therefore has no `mode`, `risk_accepted`, SRS,
744
+ or G4. It is a home for ownership, not a domain slice — and so it does not become a dumping
745
+ ground, every entity it claims MUST be explained in `cross-cutting.md`: if the platform
746
+ owns the data, the platform documents it.
747
+ """
748
+ owner: dict[str, str] = {}
749
+ for pc in c.pcs:
750
+ pid = str(pc.get("id"))
751
+ for entity in listy(pc, "owns"):
752
+ if entity in owner and owner[entity] != pid:
753
+ r.fail("V21", entity,
754
+ f"claimed as `owns` by both `{owner[entity]}` and `{pid}` — one entity MUST "
755
+ f"have exactly one owner")
756
+ else:
757
+ owner.setdefault(entity, pid)
758
+
759
+ platform = listy(c.components, "platform_owns")
760
+ for entity in platform:
761
+ if entity in owner:
762
+ r.fail("V21", entity,
763
+ f"claimed as `platform_owns` and also as `owns` by `{owner[entity]}` — "
764
+ f"`{PLATFORM}` is not a second path for an entity that already has an owner")
765
+ else:
766
+ owner[entity] = PLATFORM
767
+
768
+ _platform_documented(c, r, platform + _platform_inventory_rows(c))
769
+
770
+ cap_home = {str(x.get("id")): str(x.get("component") or "") for x in c.caps}
771
+ for fr in c.frs:
772
+ fid = str(fr.get("id"))
773
+ home = str(fr.get("component") or cap_home.get(str(fr.get("capability", "")), ""))
774
+ for entity in listy(fr, "writes"):
775
+ own = owner.get(entity)
776
+ if not own or not home or own == home:
777
+ continue
778
+ if own == PLATFORM:
779
+ # The platform has no `FR`, so there is nothing a `defers_to` could point to. What
780
+ # stands in for "one writer" here is ONE DOCUMENTED FORM, and that is what
781
+ # _platform_documented checks above.
782
+ continue
783
+ if not [d for d in listy(fr, "defers_to") if str(d).strip()]:
784
+ r.fail("V21", fid,
785
+ f"promises to write `{entity}` which `{own}` owns, without `defers_to` "
786
+ f"pointing to an `FR` owned by that owner")
787
+
788
+
789
+ def _platform_inventory_rows(c: Corpus) -> list[str]:
790
+ """Inventory rows owned by `_platform`, read from `platform_rows:` in each inventory.
791
+
792
+ `_platform` is a valid value at EVERY ownership position, so the guard applies at every
793
+ position too: whatever it owns MUST be documented in `cross-cutting.md`.
794
+ """
795
+ out: list[str] = []
796
+ for kind in ("db", "api", "screen"):
797
+ path = c.root / f".how/_platform/inventory-{kind}.md"
798
+ fm = frontmatter(path)
799
+ if not fm:
800
+ continue
801
+ out += [str(x) for x in (fm.get("platform_rows") or [])]
802
+ return out
803
+
804
+
805
+ def _platform_documented(c: Corpus, r: Result, entities: list[str]) -> None:
806
+ """Every entity with `platform_owns` MUST be named in `cross-cutting.md`.
807
+
808
+ Skipped while the file does not yet carry that section: `cross-cutting.md` is a G3 output, and
809
+ an artifact the next gate will produce MUST NOT be reported missing.
810
+ """
811
+ if not entities:
812
+ return
813
+ path = c.root / CROSS_CUTTING
814
+ text = path.read_text(encoding="utf-8", errors="replace") if path.exists() else ""
815
+ if not any(h.lower() in text.lower() for h in PLATFORM_DATA_HEADINGS):
816
+ r.skip("V21", f"`{CROSS_CUTTING}` has no `{PLATFORM_DATA_HEADING}` section yet — "
817
+ f"{len(entities)} entities with platform_owns are not documented yet: "
818
+ + ", ".join(sorted(entities)))
819
+ return
820
+ for entity in sorted(entities):
821
+ if entity not in text:
822
+ r.fail("V21", entity,
823
+ f"claimed as `platform_owns` but not named in `{CROSS_CUTTING}` — "
824
+ f"a platform that owns data MUST document it")
825
+
826
+
827
+ def v22(c: Corpus, r: Result) -> None:
828
+ """A wave MUST NOT touch a component whose G4 has not passed and whose mode is not catalog.
829
+
830
+ `catalog` skips G4 on purpose, so it is not an exception — it is part of the rule.
831
+ """
832
+ pc_by_id = {str(x.get("id")): x for x in c.pcs}
833
+ seen: set[tuple[str, str]] = set()
834
+ for wave, _, story in c.stories():
835
+ pid = str(story.get("component") or "")
836
+ row = pc_by_id.get(pid)
837
+ if row is None:
838
+ continue
839
+ key = (str(wave.get("id")), pid)
840
+ if key in seen:
841
+ continue
842
+ seen.add(key)
843
+ mode = c.mode_of(row)
844
+ if mode == "catalog":
845
+ continue
846
+ if mode not in MODES:
847
+ r.fail("V22", pid, f"`mode: {mode}` is not one of {list(MODES)}")
848
+ continue
849
+ passed = row.get("g4_passed")
850
+ if not passed or str(passed).strip().lower() in ("false", "no", "belum"):
851
+ r.fail("V22", f"{wave.get('id')} / {pid}",
852
+ f"wave touches a component with mode `{mode}` whose `g4_passed` has not been set")
853
+
854
+
855
+ def v23(c: Corpus, r: Result) -> None:
856
+ """`risk_accepted: high` on a sensitive component demands a `DEC-` in `risk_accepted_by`.
857
+
858
+ On a component that touches nothing on that list, `high` is FREE. The control is
859
+ disclosure, not veto the owner may still choose quickly, just not without knowing what
860
+ they are wagering.
861
+ """
862
+ known = {str(x.get("id")) for x in c.decs}
863
+ for pc in c.pcs:
864
+ pid = str(pc.get("id"))
865
+ if str(pc.get("risk_accepted") or "").strip() != "high":
866
+ continue
867
+ note = str(pc.get("risk_note") or "").lower()
868
+ hits = sorted({m for m in SENSITIVE_MARKERS if m in note})
869
+ if not hits:
870
+ continue
871
+ ref = str(pc.get("risk_accepted_by") or "").strip()
872
+ if not ref:
873
+ r.fail("V23", pid,
874
+ f"`risk_accepted: high` while `risk_note` mentions {hits}, without "
875
+ f"`risk_accepted_by` pointing to a risk-acceptance `DEC-`")
876
+ elif ref not in known:
877
+ r.fail("V23", pid, f"`risk_accepted_by: {ref}` does not exist in decisions.yaml")
878
+
879
+
880
+ def v20(c: Corpus, r: Result) -> None:
881
+ needs_link = {"requirement", "architecture"}
882
+ for defect in c.defect_list:
883
+ did = str(defect.get("id"))
884
+ cause = str(defect.get("root_cause") or "")
885
+ if cause not in needs_link:
886
+ continue
887
+ if not listy(defect, "violates"):
888
+ r.fail("V20", did, f"has `root_cause` `{cause}` but `violates` is empty")
889
+ if str(defect.get("status")) == "fixed" and not str(defect.get("decision") or "").strip():
890
+ r.fail("V20", did,
891
+ f"closed as fixed with root_cause `{cause}` without an accompanying `DEC-`")
892
+
893
+
894
+ # Files that DESCRIBE the past, not STATE what currently holds. A dangling citation here is
895
+ # not a finding — corpus-guide.md owns that rule, and rewriting it would falsify history.
896
+ PAST_RECORD = (
897
+ ".control/memlog/",
898
+ ".control/decisions/",
899
+ ".control/questions/answered.md",
900
+ ".control/reports/",
901
+ )
902
+ # Corpus that §25 freezes as-is. Its citation of a now-retired prototype is authorized by DEC-016.
903
+ FROZEN = (".what/",)
904
+ # Derived output. A finding here is UNACTIONABLE by construction — the folder MUST NOT be written
905
+ # by hand, so nobody may fix it where it is reported. It also renders registry values inside
906
+ # backticks, which makes a frozen `DEC-` `touches:` entry look like a live citation: the 0.5.0
907
+ # layout move surfaced three of those, all of them correct history. Fix the source or leave it.
908
+ DERIVED = (".control/generated/",)
909
+ # A path a run WILL PRODUCE, not one a document cites as existing. A rule stating "this pass's
910
+ # memlog lands at X" names a DESTINATION; demanding X already exist would demand the run has already
911
+ # happened.
912
+ DESTINATION = (
913
+ ".control/memlog/",
914
+ ".control/meetings/",
915
+ ".control/reports/",
916
+ "_bmad-output/",
917
+ )
918
+
919
+ CITE_RE = re.compile(
920
+ r"`((?:\.constitution|\.control|\.what|\.how|_bmad-output|\.work|src|web|public|deploy)"
921
+ r"/[A-Za-z0-9_./-]+\.(?:md|yaml|yml|py|go|tsx|ts|js|mjs|sql|html|css|json))`")
922
+
923
+
924
+ # Directories that MUST be pruned DURING traversal, not filtered afterwards.
925
+ #
926
+ # The old form was `c.root.rglob("*.md")` plus a `rel.startswith(...)` filter, and it had two faults
927
+ # that only showed up on a real machine:
928
+ #
929
+ # The filter ran too late. rglob had already walked in, so a dangling symlink inside
930
+ # node_modules an npm workspace link left behind by an abandoned git worktree — raised
931
+ # FileNotFoundError and took the whole run down. A validator that CRASHES on somebody's build
932
+ # output reports nothing about the corpus at all.
933
+ #
934
+ # `node_modules/` matched only at the ROOT. `web/node_modules/` sailed straight through, which is
935
+ # where a monorepo actually keeps it.
936
+ PRUNE_DIRS = frozenset({
937
+ ".git", "node_modules", "__pycache__", ".venv", "venv", "dist", "build",
938
+ ".pytest_cache", ".mypy_cache", ".ruff_cache", ".next", ".turbo", ".idea", ".vscode",
939
+ "worktrees", # .claude/worktrees/ another checkout's tree is not this corpus
940
+ })
941
+
942
+
943
+ def _walk_corpus(root: Path, suffixes: tuple[str, ...]) -> list[Path]:
944
+ """Every file under `root` with one of `suffixes`, sorted, pruning PRUNE_DIRS as it goes.
945
+
946
+ Sorted because determinism is this script's contract: two runs over the same tree MUST report the
947
+ same thing in the same order.
948
+ """
949
+ out: list[Path] = []
950
+ for dirpath, dirnames, filenames in os.walk(root, onerror=lambda _e: None):
951
+ dirnames[:] = sorted(d for d in dirnames if d not in PRUNE_DIRS)
952
+ for name in filenames:
953
+ if name.endswith(suffixes):
954
+ out.append(Path(dirpath) / name)
955
+ return sorted(out)
956
+
957
+
958
+ def v24(c: Corpus, r: Result) -> None:
959
+ """A path citation inside a document that STATES what currently holds MUST resolve.
960
+
961
+ This is the mechanical half of `wdi-reconcile`'s Evidence check, and it is the only way to know
962
+ that a migration stayed complete. Its failure class is distinctive: a file gets deleted or moved,
963
+ while the routing line that points at it stays behind — no other validator sees it, because no
964
+ id moved.
965
+
966
+ Deliberately SKIPPED: files that describe the past, corpus that has been frozen, and derived
967
+ output. A `DEC-` Trace that names material that has since been retired describes what was read on
968
+ that date; reporting it would demand history be rewritten to match the present. Derived output is
969
+ skipped for a second reason on top of that: it MUST NOT be edited by hand, so a finding reported
970
+ there names a file nobody is allowed to fix.
971
+ """
972
+ scanned = 0
973
+ for path in _walk_corpus(c.root, (".md", ".yaml")):
974
+ rel = path.relative_to(c.root).as_posix()
975
+ if rel.startswith(("_bmad-output/", ".claude/skills/bmad-")):
976
+ continue
977
+ if rel.startswith(PAST_RECORD) or rel.startswith(FROZEN) or rel.startswith(DERIVED):
978
+ continue
979
+ scanned += 1
980
+ text = path.read_text(encoding="utf-8", errors="replace")
981
+ for cited in sorted(set(CITE_RE.findall(text))):
982
+ if "<" in cited or "{" in cited:
983
+ continue # placeholder, not a path
984
+ if cited.startswith(DESTINATION):
985
+ continue
986
+ if not (c.root / cited).exists():
987
+ r.fail("V24", rel, f"cites `{cited}` which does not exist")
988
+ if not scanned:
989
+ r.skip("V24", "no file was scanned")
990
+
991
+
992
+ CTR_HEADING = re.compile(r"^###\s+(.+?)\s*$", re.M)
993
+
994
+
995
+ def map_container_headings(root: Path) -> list[str] | None:
996
+ """Heading `### x` under `## Containers` in the code map. None if the map does not exist."""
997
+ path = root / ".control" / "structure-codebase.md"
998
+ if not path.exists():
999
+ return None
1000
+ text = path.read_text(encoding="utf-8", errors="replace")
1001
+ start = text.find("\n## Containers")
1002
+ if start < 0:
1003
+ return []
1004
+ rest = text[start + 1:]
1005
+ nxt = re.search(r"^##\s+(?!#)", rest[len("## Containers"):], re.M)
1006
+ if nxt:
1007
+ rest = rest[:len("## Containers") + nxt.start()]
1008
+ return [m.group(1).strip().strip("`") for m in CTR_HEADING.finditer(rest)]
1009
+
1010
+
1011
+ def v25(c: Corpus, r: Result) -> None:
1012
+ """A container's `built` and its four consequences, plus the PC x container matrix.
1013
+
1014
+ A container EXISTS inside the boundary whether or not we write its content, and that is what
1015
+ used to make the rule unsatisfiable: `structure-guide.md` demands every code-map heading match
1016
+ the registry, while a database or web server MUST be registered and MUST NOT have a heading.
1017
+ `built` separates the two, and this check is what makes that separation hold instead of the
1018
+ argument being repeated on every project. `DEC-017` records its definition.
1019
+
1020
+ Anything whose runtime we do not deploy is an external system: it lives in C4 L1 and MUST NOT
1021
+ be registered here at all — its absence from the registry is the check.
1022
+ """
1023
+ containers = rows(c.components, "containers")
1024
+ if not containers:
1025
+ r.skip("V25", "`containers:` is not registered yet")
1026
+ return
1027
+
1028
+ built: dict[str, bool] = {}
1029
+ for ctr in containers:
1030
+ cid = str(ctr.get("id") or "").strip()
1031
+ if not cid:
1032
+ r.fail("V25", "containers", "a container has no `id`")
1033
+ continue
1034
+ flag = ctr.get("built")
1035
+ if not isinstance(flag, bool):
1036
+ r.fail("V25", cid, "`built` MUST be a bool — true if we write its content, false if someone else implements it")
1037
+ continue
1038
+ built[cid] = flag
1039
+
1040
+ # (1) code-map heading = EXACTLY a container with `built: true`
1041
+ headings = map_container_headings(c.root)
1042
+ if headings is None:
1043
+ r.fail("V25", ".control/structure-codebase.md", "the code map does not exist, so container headings cannot be compared")
1044
+ else:
1045
+ for h in headings:
1046
+ if h not in built:
1047
+ r.fail("V25", f"code map §{h}", "heading is not a registered container — register it, or it is not a container")
1048
+ elif not built[h]:
1049
+ r.fail("V25", f"code map §{h}", "`built: false` MUST NOT have a heading there is no code of ours inside it")
1050
+ for cid, flag in sorted(built.items()):
1051
+ if flag and cid not in headings:
1052
+ r.fail("V25", cid, "`built: true` MUST have a heading in the code map")
1053
+
1054
+ # (2) `built: false` MUST NOT be used by an LC, and (3) MUST NOT appear in a PC's `containers:`
1055
+ for lc in c.lcs:
1056
+ ctr = str(lc.get("container") or "").strip()
1057
+ if ctr and built.get(ctr) is False:
1058
+ r.fail("V25", str(lc.get("id") or "LC-?"), f"names container `{ctr}` which is `built: false`")
1059
+ elif ctr and ctr not in built:
1060
+ r.fail("V25", str(lc.get("id") or "LC-?"), f"names container `{ctr}` which is not registered")
1061
+
1062
+ # (4) PC x container matrixthis field is its SSOT, and it MUST be complete at G3
1063
+ for pc in c.pcs:
1064
+ pid = str(pc.get("id") or "?")
1065
+ listed = listy(pc, "containers")
1066
+ if not listed:
1067
+ r.fail("V25", pid, "`containers:` is empty — every PC MUST live in at least one container (a G3 debt)")
1068
+ continue
1069
+ for ctr in listed:
1070
+ if ctr not in built:
1071
+ r.fail("V25", pid, f"`containers:` names `{ctr}` which is not registered")
1072
+ elif not built[ctr]:
1073
+ r.fail("V25", pid, f"`containers:` names `{ctr}` which is `built: false` — the data lives there by definition, so the row tells us nothing")
1074
+
1075
+ # (5) L3 — only for `built: true`, and only ones that hold more than one PC
1076
+ pcs_per: dict[str, list[str]] = {}
1077
+ for pc in c.pcs:
1078
+ for ctr in listy(pc, "containers"):
1079
+ pcs_per.setdefault(ctr, []).append(str(pc.get("id") or "?"))
1080
+ for path in sorted((c.root / ".how" / "_platform").glob("c4-l3-*.md")):
1081
+ cid = path.name[len("c4-l3-"):-len(".md")]
1082
+ if cid not in built:
1083
+ r.fail("V25", path.relative_to(c.root).as_posix(),
1084
+ f"L3 for `{cid}` which is not a registered container")
1085
+ elif not built[cid]:
1086
+ r.fail("V25", path.relative_to(c.root).as_posix(),
1087
+ f"`{cid}` `built: false` MUST NOT have an L3 — not one box inside it is ours to draw")
1088
+ for cid, pids in sorted(pcs_per.items()):
1089
+ if built.get(cid) and len(pids) > 1:
1090
+ l3 = c.root / ".how" / "_platform" / f"c4-l3-{cid}.md"
1091
+ if not l3.exists():
1092
+ r.fail("V25", cid, f"holds {len(pids)} PCs, so `c4-l3-{cid}.md` MUST exist")
1093
+
1094
+
1095
+ UC_ROW_RE = re.compile(r"^\|\s*(UC-\d+)\s*\|([^\n]*)$", re.M)
1096
+
1097
+ # The `critical` column value is machine-matched, so it is machine-facing and its canonical form
1098
+ # is English `yes`. `ya` is still accepted: a corpus that wrote it before this rule took effect
1099
+ # MUST NOT be forced to migrate just so a regex can be tidier. The word boundary keeps `ya` from
1100
+ # matching inside other words.
1101
+ CRITICAL_YES = re.compile(r"\b(yes|ya)\b", re.I)
1102
+
1103
+
1104
+ def v26(c: Corpus, r: Result) -> None:
1105
+ """The UC catalogue in every SRS MUST agree with `usecases.yaml` — both its id AND its `critical`.
1106
+
1107
+ This is the most expensive gap this pass closes, because it is the only one that **had already
1108
+ happened and no validator saw it.** Step 16 re-derived `critical` in the registry with a
1109
+ narrowed definition — money, personal data, irreversible action — and the seven catalogue tables
1110
+ in the SRS did not follow along. Twenty-six rows disagreed, and the disagreement was only
1111
+ discovered when a human read the sentence "nine of these are critical" in SRS-admin while the
1112
+ registry held three.
1113
+
1114
+ The registry is the SSOT. The table in the SRS is the catalogue's permanent home for a reader,
1115
+ and two homes for one fact are only safe if something compares them. This is what compares them.
1116
+
1117
+ What is NOT checked here: title and actor. Both are prose, and prose with different words is
1118
+ not prose with a different meaning comparing them would report style as a defect.
1119
+ """
1120
+ reg = {str(uc.get("id")): bool(uc.get("critical")) for uc in c.ucs}
1121
+ reg_pc = {str(uc.get("id")): str(uc.get("component") or "") for uc in c.ucs}
1122
+ checked = 0
1123
+ for pc in c.pcs:
1124
+ pid = str(pc.get("id"))
1125
+ path = c.root / f".what/{pid}/SRS-{pid}.md"
1126
+ if not path.exists():
1127
+ continue
1128
+ checked += 1
1129
+ text = path.read_text(encoding="utf-8", errors="replace")
1130
+ seen: set[str] = set()
1131
+ for match in UC_ROW_RE.finditer(text):
1132
+ uid = match.group(1)
1133
+ cells = [x.strip() for x in match.group(2).split("|")]
1134
+ if len(cells) < 4:
1135
+ continue
1136
+ seen.add(uid)
1137
+ if uid not in reg:
1138
+ r.fail("V26", f"{pid}/{uid}", "is in the SRS catalogue but not in `usecases.yaml`")
1139
+ continue
1140
+ if reg_pc[uid] != pid:
1141
+ r.fail("V26", f"{pid}/{uid}",
1142
+ f"the registry places it in `{reg_pc[uid]}`, not in this component")
1143
+ marked = CRITICAL_YES.search(cells[3]) is not None
1144
+ if marked != reg[uid]:
1145
+ r.fail("V26", f"{pid}/{uid}",
1146
+ f"`critical` in the SRS {'yes' if marked else 'no'}, "
1147
+ f"in the registry {'yes' if reg[uid] else 'no'}")
1148
+ for uid, owner in sorted(reg_pc.items()):
1149
+ if owner == pid and uid not in seen:
1150
+ r.fail("V26", f"{pid}/{uid}", "is in `usecases.yaml` but not in the SRS catalogue")
1151
+ if not checked:
1152
+ r.skip("V26", "no SRS could be read")
1153
+
1154
+
1155
+ def v27(c: Corpus, r: Result) -> None:
1156
+ """Every file in the custom room MUST declare itself, and a rebuttal MUST have a decision.
1157
+
1158
+ The `.constitution/project/` room exists so product-specific rules have a home that `update`
1159
+ does not overwrite and `promote` does not publish. The cost that comes with it: it is also the
1160
+ easiest place to break a generic rule without a trace. Its frontmatter is what holds that back.
1161
+
1162
+ A file here MAY narrow or add without naming anything. To REBUT a generic rule it MUST name it
1163
+ in `overrides:` and carry a `decision:` — because a method that can be rebutted without a
1164
+ decision stops being trustworthy in the next repo.
1165
+
1166
+ Four files in the room are STRUCTURAL and are skipped, because they are not ad-hoc rules and
1167
+ carry their own frontmatter conventions instead:
1168
+
1169
+ README.md authored in the package, not in the product
1170
+ constitution.md Articles 1, 2, 5 — carries `status:`, and Article 4 governs it
1171
+ codebase-*-guide.md the stack, conventions, and brownfield guides — `status:` plus
1172
+ `ratified_by:`, and they are filled by a wave's distillation
1173
+
1174
+ Demanding `scope:` and `purpose:` of those would be demanding a declaration of files whose
1175
+ role is already fixed by the layout. What V27 exists to guard is the file somebody ADDS.
1176
+ """
1177
+ room = c.root / ".constitution" / "project"
1178
+ if not room.is_dir():
1179
+ r.skip("V27", "the `.constitution/project/` room does not exist yet — it is seeded at install")
1180
+ return
1181
+ structural = {"README.md", "constitution.md"}
1182
+ files = [p for p in sorted(room.rglob("*.md"))
1183
+ if p.name not in structural and not p.name.startswith("codebase-")]
1184
+ if not files:
1185
+ r.skip("V27", "the `.constitution/project/` room is empty, and that is a valid state — "
1186
+ "a generic rule MUST NOT be moved here just to give the room content")
1187
+ return
1188
+ dec_ids = {str(d.get("id")) for d in c.decs}
1189
+ for path in files:
1190
+ rel = path.relative_to(c.root).as_posix()
1191
+ fm = frontmatter(path)
1192
+ if fm is None:
1193
+ r.fail("V27", rel, "has no frontmatter")
1194
+ continue
1195
+ if str(fm.get("scope") or "").strip() != "project":
1196
+ r.fail("V27", rel, "`scope:` MUST contain exactly `project`")
1197
+ if not str(fm.get("purpose") or "").strip():
1198
+ r.fail("V27", rel, "`purpose:` is empty — one line: what this rule guards")
1199
+ over = str(fm.get("overrides") or "").strip()
1200
+ dec = str(fm.get("decision") or "").strip()
1201
+ if over:
1202
+ if not (c.root / over).exists():
1203
+ r.fail("V27", rel, f"`overrides:` points to `{over}` which does not exist — "
1204
+ f"the rebutted rule may already be gone")
1205
+ if not dec:
1206
+ r.fail("V27", rel, "rebuts a generic rule without `decision:` — "
1207
+ "a rebuttal MUST have a `DEC-` that decided it")
1208
+ elif dec not in dec_ids:
1209
+ r.fail("V27", rel, f"`decision: {dec}` is not registered in decisions.yaml")
1210
+ elif dec:
1211
+ r.fail("V27", rel, "`decision:` is set without `overrides:` — "
1212
+ "name which rule is rebutted, or drop `decision:`")
1213
+
1214
+
1215
+ def run_checks(c: Corpus, asof: dt.date) -> Result:
1216
+ r = Result()
1217
+ for fn in (v1, v2, v3, v4, v5, v6, v7, v8, v9, v11, v12, v13, v15, v16, v17, v18, v19, v20,
1218
+ v21, v22, v23, v24, v25, v26, v27):
1219
+ fn(c, r)
1220
+ v14(c, r, asof)
1221
+ return r
1222
+
1223
+
1224
+ # ------------------------------------------------------------------ generator
1225
+
1226
+
1227
+ def _story_status(c: Corpus, story: dict) -> str:
1228
+ folder = str(story.get("spec_folder") or "").strip()
1229
+ if not folder:
1230
+ return "unknown"
1231
+ matches = sorted((c.root / folder / "stories").glob(f"{story.get('id')}-*.md"))
1232
+ if not matches:
1233
+ return "unknown"
1234
+ return str((frontmatter(matches[0]) or {}).get("status") or "unknown")
1235
+
1236
+
1237
+ def gen_components(c: Corpus) -> dict:
1238
+ return {
1239
+ "product_components": [
1240
+ {"id": pc.get("id"), "name": pc.get("name"),
1241
+ "containers": listy(pc, "containers"),
1242
+ "logical_components": sorted(
1243
+ str(lc.get("id")) for lc in c.lcs
1244
+ if str(lc.get("component")) == str(pc.get("id")))}
1245
+ for pc in c.pcs
1246
+ ],
1247
+ "logical_components": [
1248
+ {"id": lc.get("id"), "type": lc.get("type"), "component": lc.get("component"),
1249
+ "area": lc.get("area"), "owner": lc.get("owner")}
1250
+ for lc in c.lcs
1251
+ ],
1252
+ }
1253
+
1254
+
1255
+ def gen_risks(c: Corpus) -> dict:
1256
+ return {"risks": [
1257
+ {"id": x.get("id"), "impact": x.get("impact"), "likelihood": x.get("likelihood"),
1258
+ "owner": x.get("owner"), "status": x.get("status"),
1259
+ "pivot_trigger": x.get("pivot_trigger")}
1260
+ for x in rows(c.risks, "risks") if str(x.get("status")) != "closed"
1261
+ ]}
1262
+
1263
+
1264
+ def gen_dag(c: Corpus) -> dict:
1265
+ out = []
1266
+ per_wave: dict[str, list[dict]] = {}
1267
+ for wave, _, story in c.stories():
1268
+ per_wave.setdefault(str(wave.get("id")), []).append(story)
1269
+ for wid in sorted(per_wave):
1270
+ items = per_wave[wid]
1271
+ done: set[str] = set()
1272
+ pending = {str(s.get("id")): set(listy(s, "depends_on")) for s in items}
1273
+ waves_out = []
1274
+ while pending:
1275
+ ready = sorted(k for k, deps in pending.items() if not (deps - done))
1276
+ if not ready: # cycle — V7 has already reported it
1277
+ waves_out.append({"blocked": sorted(pending)})
1278
+ break
1279
+ waves_out.append({"parallel": ready})
1280
+ done |= set(ready)
1281
+ for k in ready:
1282
+ pending.pop(k)
1283
+ out.append({"wave": wid, "order": waves_out})
1284
+ return {"dag": out}
1285
+
1286
+
1287
+ def gen_rtm(c: Corpus) -> dict:
1288
+ cap_goal = {str(x.get("id")): str(x.get("goal", "")) for x in c.caps}
1289
+ ucs_for_fr: dict[str, list[str]] = {}
1290
+ for uc in c.ucs:
1291
+ for fr in listy(uc, "satisfies"):
1292
+ ucs_for_fr.setdefault(fr, []).append(str(uc.get("id")))
1293
+ stories_for_uc: dict[str, list[tuple[dict, dict]]] = {}
1294
+ for wave, _, story in c.stories():
1295
+ for uc in listy(story, "satisfies"):
1296
+ stories_for_uc.setdefault(uc, []).append((wave, story))
1297
+ decs_for: dict[str, list[str]] = {}
1298
+ for dec in c.decs:
1299
+ for target in listy(dec, "serves"):
1300
+ decs_for.setdefault(target, []).append(str(dec.get("id")))
1301
+
1302
+ lines = []
1303
+ for fr in c.frs:
1304
+ fid = str(fr.get("id"))
1305
+ cap = str(fr.get("capability", ""))
1306
+ base = {"BG": cap_goal.get(cap, ""), "CAP": cap, "FR": fid,
1307
+ "DEC": sorted(decs_for.get(fid, []))}
1308
+ ucs = sorted(ucs_for_fr.get(fid, []))
1309
+ if not ucs:
1310
+ exempt = bool(str(fr.get("no_uc") or "").strip())
1311
+ lines.append({**base, "UC": "", "story": "", "wave": "", "release": "",
1312
+ "test": [], "status": "", "green": False,
1313
+ "exempt": exempt,
1314
+ "broken_at": "no_uc" if exempt else "UC"})
1315
+ continue
1316
+ for uid in ucs:
1317
+ pairs = sorted(stories_for_uc.get(uid, []), key=lambda p: str(p[1].get("id")))
1318
+ if not pairs:
1319
+ lines.append({**base, "UC": uid, "story": "", "wave": "", "release": "",
1320
+ "test": [], "status": "", "green": False, "exempt": False,
1321
+ "broken_at": "story"})
1322
+ continue
1323
+ for wave, story in pairs:
1324
+ status = _story_status(c, story)
1325
+ tests = listy(story, "tests")
1326
+ broken = ""
1327
+ if not tests:
1328
+ broken = "test"
1329
+ elif status != "done":
1330
+ broken = "status"
1331
+ lines.append({**base, "UC": uid, "story": str(story.get("id")),
1332
+ "wave": str(wave.get("id")), "release": str(wave.get("release", "")),
1333
+ "test": tests, "status": status, "exempt": False,
1334
+ "green": broken == "", "broken_at": broken})
1335
+ return {"rtm": lines}
1336
+
1337
+
1338
+ def gen_status(c: Corpus, rtm: dict, result: Result) -> dict:
1339
+ lines = rtm.get("rtm") or []
1340
+ counted = [line for line in lines if not line.get("exempt")]
1341
+ exempt = len(lines) - len(counted)
1342
+ green = sum(1 for line in counted if line.get("green"))
1343
+ per_wave = []
1344
+ for wave in c.wave_list:
1345
+ wid = str(wave.get("id"))
1346
+ items = [s for w, _, s in c.stories() if str(w.get("id")) == wid]
1347
+ done = sum(1 for s in items if _story_status(c, s) == "done")
1348
+ per_wave.append({"wave": wid, "status": wave.get("status"),
1349
+ "stories_done": done, "stories_total": len(items),
1350
+ "work_progress": _pct(done, len(items))})
1351
+ applicable = 26 # V1..V27 minus V10, which was retired
1352
+ return {
1353
+ "promise_progress": _pct(green, len(counted)),
1354
+ "rtm_rows": {"green": green, "counted": len(counted),
1355
+ "excluded_no_uc": exempt},
1356
+ "work_progress": per_wave,
1357
+ "gate_readiness": _pct(applicable - len(result.red), applicable),
1358
+ "validators_red": result.red,
1359
+ "validators_skipped": dict(sorted(result.skipped.items())),
1360
+ "open_questions": _question_budget(c),
1361
+ }
1362
+
1363
+
1364
+ def _question_budget(c: Corpus) -> dict:
1365
+ """Counts of all four question lists, compared against the budget in index.yaml.
1366
+
1367
+ The budget is NOT a hard gate. It is reported when a batch exceeds it, because a larger
1368
+ batch is a signal about the pass, not about the corpus.
1369
+ """
1370
+ budget = c.index.get("question_budget") or {}
1371
+ out: dict[str, object] = {}
1372
+ for name in ("blocking", "assumptions", "external", "answered"):
1373
+ path = c.root / ".control/questions" / f"{name}.md"
1374
+ rows_n = 0
1375
+ if path.exists():
1376
+ rows_n = sum(1 for line in path.read_text(encoding="utf-8", errors="replace").splitlines()
1377
+ if line.startswith("| OQ-"))
1378
+ out[name] = rows_n
1379
+ cap_block = budget.get("blocking_per_component")
1380
+ if cap_block and c.pcs:
1381
+ allowed = int(cap_block) * len(c.pcs)
1382
+ out["blocking_budget"] = allowed
1383
+ out["blocking_over_budget"] = out["blocking"] > allowed
1384
+ cap_assume = budget.get("assumptions_per_gate")
1385
+ if cap_assume:
1386
+ out["assumptions_budget_per_gate"] = int(cap_assume)
1387
+ return out
1388
+
1389
+
1390
+ def _pct(part: int, total: int) -> str:
1391
+ return "n/a" if total == 0 else f"{round(100 * part / total)}%"
1392
+
1393
+
1394
+ def as_markdown(name: str, payload: dict) -> str:
1395
+ body = dump(payload)
1396
+ return (f"# {name}\n\n"
1397
+ f"> Generated by `.constitution/method/scripts/validate.py --generate`. "
1398
+ f"MUST NOT be hand-edited.\n\n"
1399
+ f"```yaml\n{body}```\n")
1400
+
1401
+
1402
+ # ------------------------------------------------------- pages for humans
1403
+
1404
+ PAGE_HEADER = ("> Generated by `.constitution/method/scripts/validate.py --generate`. "
1405
+ "MUST NOT be hand-edited.\n")
1406
+
1407
+
1408
+ def _section(path: Path, heading: str) -> str:
1409
+ """Extract one `## <heading>` section from a markdown file, as-is."""
1410
+ if not path.exists():
1411
+ return ""
1412
+ lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
1413
+ out: list[str] = []
1414
+ inside = False
1415
+ for line in lines:
1416
+ if line.startswith("## "):
1417
+ if inside:
1418
+ break
1419
+ inside = line[3:].strip().lower().startswith(heading.lower())
1420
+ continue
1421
+ if inside:
1422
+ out.append(line)
1423
+ return "\n".join(out).strip("\n")
1424
+
1425
+
1426
+ def _body(path: Path) -> str:
1427
+ """File content without frontmatter and without template comments."""
1428
+ if not path.exists():
1429
+ return ""
1430
+ text = path.read_text(encoding="utf-8", errors="replace")
1431
+ match = FM.match(text)
1432
+ if match:
1433
+ text = text[match.end():]
1434
+ while "<!--" in text and "-->" in text:
1435
+ head, _, rest = text.partition("<!--")
1436
+ _, _, tail = rest.partition("-->")
1437
+ text = head + tail
1438
+ return text.strip("\n")
1439
+
1440
+
1441
+ def page_decisions(c: Corpus) -> str:
1442
+ """Flat table of every `DEC-`. This is what replaces looking up decisions through the memlog."""
1443
+ rows_out = ["| id | Title | Status | Type | Touches | File |",
1444
+ "| --- | --- | --- | --- | --- | --- |"]
1445
+ for dec in c.decs:
1446
+ touches = ", ".join(f"`{x}`" for x in listy(dec, "touches")) or "—"
1447
+ rows_out.append(
1448
+ f"| `{dec.get('id')}` | {_cell(dec.get('title'))} | `{dec.get('status', '')}` "
1449
+ f"| {dec.get('type') or '—'} | {touches} | `{dec.get('file', '')}` |")
1450
+ counts: dict[str, int] = {}
1451
+ for dec in c.decs:
1452
+ key = str(dec.get("status"))
1453
+ counts[key] = counts.get(key, 0) + 1
1454
+ tally = " · ".join(f"{k}: {v}" for k, v in sorted(counts.items())) or "no decisions yet"
1455
+ return ("# decisions\n\n" + PAGE_HEADER +
1456
+ "\nDecisions are no longer looked up through the memlog — the memlog goes back to being just a pass log.\n"
1457
+ f"\n**{len(c.decs)} decisions** — {tally}.\n\n" + "\n".join(rows_out) + "\n")
1458
+
1459
+
1460
+ def page_blueprint(c: Corpus) -> str:
1461
+ """One-page roll-up reviewed at G3. Seven files become one read.
1462
+
1463
+ The UC catalogue, actor list, and domain model stay put in their own component's kernel as
1464
+ their permanent home. This is their view. One fact, one home, one view.
1465
+ """
1466
+ parts = ["# blueprint\n", PAGE_HEADER,
1467
+ "\nThis is what the owner reads at **G3 Blueprint**, instead of seven files. Its "
1468
+ "content is affected by neither `mode` nor `risk_accepted`.\n"]
1469
+
1470
+ crit = sum(1 for uc in c.ucs if uc.get("critical"))
1471
+ parts.append(f"\n## Use case catalogue\n\n**{len(c.ucs)} use cases**, {crit} marked "
1472
+ f"`critical`.\n")
1473
+ parts.append("| id | Use case | Component | Satisfies | critical |")
1474
+ parts.append("| --- | --- | --- | --- | --- |")
1475
+ for uc in c.ucs:
1476
+ sat = ", ".join(f"`{x}`" for x in listy(uc, "satisfies")) or "—"
1477
+ flag = "yes" if uc.get("critical") else "no"
1478
+ parts.append(f"| `{uc.get('id')}` | {_cell(uc.get('title'))} | "
1479
+ f"`{uc.get('component', '')}` | {sat} | {flag} |")
1480
+
1481
+ parts.append("\n## Actor list\n")
1482
+ for pc in c.pcs:
1483
+ pid = str(pc.get("id"))
1484
+ block = _section(c.root / f".what/{pid}/SRS-{pid}.md", "Actor Register")
1485
+ parts.append(f"\n### {pid} — {pc.get('name', '')}\n")
1486
+ parts.append(_demote(block) if block
1487
+ else "_no § Actor Register in this component's SRS yet._")
1488
+
1489
+ parts.append("\n## Domain model\n")
1490
+ for pc in c.pcs:
1491
+ pid = str(pc.get("id"))
1492
+ block = _body(c.root / f".what/{pid}/03-domain/domain-model.md")
1493
+ parts.append(f"\n### {pid}\n")
1494
+ parts.append(_demote(block) if block else "_no `03-domain/domain-model.md` yet._")
1495
+
1496
+ parts.append("\n## Three inventories\n")
1497
+ for kind, name in (("db", "table"), ("api", "endpoint"), ("screen", "screen")):
1498
+ block = _body(c.root / f".how/_platform/inventory-{kind}.md")
1499
+ parts.append(f"\n### List of {name}s — `inventory-{kind}.md`\n")
1500
+ parts.append(_demote(block) if block else f"_no `inventory-{kind}.md` yet._")
1501
+
1502
+ return "\n".join(parts) + "\n"
1503
+
1504
+
1505
+ def _cell(value: object, limit: int = 110) -> str:
1506
+ """One table row, shortened. The full-length source stays in the registry — this is just a view."""
1507
+ text = " ".join(str(value or "").split()).replace("|", "\\|")
1508
+ return text if len(text) <= limit else text[: limit - 1].rstrip() + "…"
1509
+
1510
+
1511
+ def _demote(block: str, by: int = 2) -> str:
1512
+ """Demote the heading level of inlined content, so it does not clash with the roll-up's own structure."""
1513
+ out = []
1514
+ for line in block.splitlines():
1515
+ stripped = line.lstrip()
1516
+ if stripped.startswith("#"):
1517
+ hashes = len(stripped) - len(stripped.lstrip("#"))
1518
+ out.append("#" * min(6, hashes + by) + stripped[hashes:])
1519
+ else:
1520
+ out.append(line)
1521
+ return "\n".join(out)
1522
+
1523
+
1524
+ def page_estimate(c: Corpus) -> str:
1525
+ """Table of CANDIDATE tasks. One row per `FR`, since that is a wave's ideal shape."""
1526
+ mode_of = {str(pc.get("id")): c.mode_of(pc) for pc in c.pcs}
1527
+ risk_of = {str(pc.get("id")): (str(pc.get("risk_accepted") or ""),
1528
+ str(pc.get("risk_note") or "—")) for pc in c.pcs}
1529
+ cap_by_id = {str(x.get("id")): x for x in c.caps}
1530
+ fr_per_cap: dict[str, int] = {}
1531
+ for fr in c.frs:
1532
+ key = str(fr.get("capability", ""))
1533
+ fr_per_cap[key] = fr_per_cap.get(key, 0) + 1
1534
+
1535
+ have_mandays = any(x.get("estimate_mandays") for x in c.caps)
1536
+ parts = ["# estimate\n", PAGE_HEADER,
1537
+ "\n**THIS IS AN ESTIMATE, FORWARD-LOOKING.** Every row below is a **candidate** "
1538
+ "task; the wave in `waves.yaml` is the real one. One row MAY become one wave, and three "
1539
+ "neighboring rows MAY be merged into one — that merge is a human decision made when the "
1540
+ "wave is opened.\n"]
1541
+ if not have_mandays:
1542
+ parts.append("\n**With no `estimate_mandays` on a single `CAP`**, the Load column is empty and "
1543
+ "this output is only as good as a T-shirt-size estimate. It MUST be reported as such.\n")
1544
+
1545
+ parts.append("\n| Task | FR | Epic | mode | Exposure | Load | Priority | Depends on | Release |")
1546
+ parts.append("| --- | --- | --- | --- | --- | --- | --- | --- | --- |")
1547
+ for fr in c.frs:
1548
+ cap_id = str(fr.get("capability", ""))
1549
+ cap = cap_by_id.get(cap_id, {})
1550
+ pid = str(fr.get("component") or cap.get("component") or "")
1551
+ risk, note = risk_of.get(pid, ("—", "—"))
1552
+ exposure = "not set yet" if risk == "" else f"`{risk}` — {_cell(note, 60)}"
1553
+ mandays = cap.get("estimate_mandays")
1554
+ share = "—"
1555
+ if mandays:
1556
+ try:
1557
+ share = f"{float(mandays) / max(1, fr_per_cap.get(cap_id, 1)):.1f}"
1558
+ except (TypeError, ValueError):
1559
+ share = "—"
1560
+ deps = ", ".join(f"`{x}`" for x in listy(cap, "depends_on")) or "—"
1561
+ parts.append(
1562
+ f"| {_cell(fr.get('text') or fr.get('title'))} | `{fr.get('id')}` | `{pid or '—'}` "
1563
+ f"| `{mode_of.get(pid, 'catalog')}` | {exposure} | {share} "
1564
+ f"| {cap.get('priority', '—')} | {deps} | {cap.get('target_release', '—')} |")
1565
+ return "\n".join(parts) + "\n"
1566
+
1567
+
1568
+ def generate(c: Corpus, result: Result) -> list[Path]:
1569
+ out_dir = c.root / ".control" / "generated"
1570
+ out_dir.mkdir(parents=True, exist_ok=True)
1571
+ rtm = gen_rtm(c)
1572
+ payloads = {
1573
+ "components": gen_components(c),
1574
+ "risks": gen_risks(c),
1575
+ "dag": gen_dag(c),
1576
+ "rtm": rtm,
1577
+ "status": gen_status(c, rtm, result),
1578
+ }
1579
+ written = []
1580
+ for name in GENERATED_ORDER:
1581
+ payload = payloads[name]
1582
+ yaml_path = out_dir / f"{name}.yaml"
1583
+ yaml_path.write_text(dump(payload), encoding="utf-8")
1584
+ md_path = out_dir / f"{name}.md"
1585
+ md_path.write_text(as_markdown(name, payload), encoding="utf-8")
1586
+ written += [yaml_path, md_path]
1587
+
1588
+ # Three pages for HUMANS: real markdown tables, with no .yaml twin. What people read is
1589
+ # not wrapped in a yaml fence, and no machine reader demands a second version of it.
1590
+ for name, render in (("decisions", page_decisions),
1591
+ ("blueprint", page_blueprint),
1592
+ ("estimate", page_estimate)):
1593
+ page = out_dir / f"{name}.md"
1594
+ page.write_text(render(c), encoding="utf-8")
1595
+ written.append(page)
1596
+ return written
1597
+
1598
+
1599
+ # ------------------------------------------------------------------------ CLI
1600
+
1601
+
1602
+ def main(argv: list[str] | None = None) -> int:
1603
+ parser = argparse.ArgumentParser(
1604
+ prog="validate", description="V1..V27 and the .control/generated/ generator")
1605
+ parser.add_argument("--check", action="store_true",
1606
+ help="check only; exit non-zero if anything is red")
1607
+ parser.add_argument("--generate", action="store_true",
1608
+ help="rewrite .control/generated/ (still runs the check first)")
1609
+ parser.add_argument("--root", default=".", help="repo root (default: current directory)")
1610
+ parser.add_argument("--asof", default=None,
1611
+ help="reference date for V14, format YYYY-MM-DD (default: today). "
1612
+ "Stated explicitly so a run can be repeated exactly")
1613
+ args = parser.parse_args(argv)
1614
+
1615
+ if not args.check and not args.generate:
1616
+ args.check = True
1617
+
1618
+ root = Path(args.root).resolve()
1619
+ if not (root / ".control" / "registry").is_dir():
1620
+ print(f"validate: {root} has no .control/registry/ — wrong repo root?", file=sys.stderr)
1621
+ return 2
1622
+
1623
+ asof = dt.date.fromisoformat(args.asof) if args.asof else dt.date.today()
1624
+ corpus = Corpus.load(root)
1625
+ result = run_checks(corpus, asof)
1626
+
1627
+ if args.generate:
1628
+ for path in generate(corpus, result):
1629
+ print(f" wrote {path.relative_to(root).as_posix()}")
1630
+
1631
+ if result.findings:
1632
+ print(f"\nRED — {len(result.findings)} findings across {len(result.red)} validators\n")
1633
+ for finding in sorted(result.findings, key=lambda f: f.sort_key):
1634
+ print(f" {finding.vid:<4} {finding.subject}: {finding.message}")
1635
+ else:
1636
+ print("\nGREEN — no findings")
1637
+
1638
+ if result.skipped:
1639
+ print("\nSkipped:")
1640
+ for vid, why in sorted(result.skipped.items()):
1641
+ print(f" {vid:<4} {why}")
1642
+
1643
+ print(f"\nV14 reference date: {asof.isoformat()}")
1644
+ return 1 if result.findings else 0
1645
+
1646
+
1647
+ if __name__ == "__main__":
1648
+ raise SystemExit(main())