fix(521): bound _rationale strip window; restore REQUIRED_META; fail-open _run; archive-placement invariant

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-21 02:58:58 +02:00
co-authored by Claude Opus 4.8
parent 9266437d68
commit 7b8ac751d7
2 changed files with 169 additions and 27 deletions
+38 -25
View File
@@ -18,7 +18,7 @@ from pathlib import Path
import scripts.decisions_lib as dl # noqa: E402 (run with PYTHONPATH=. or as module)
SKIP_HEADINGS = {"Index", "Active catalog"}
REQUIRED_META = ("key", "status")
REQUIRED_META = ("key", "status", "since", "supersedes", "superseded_by")
EDIT_TOKEN = "[decisions-edit]" # noqa: S105 (a commit-message marker, not a credential)
@@ -28,8 +28,9 @@ def _key_of(ref: str | None) -> str | None:
return ref.split("@", 1)[0].strip()
def validate(records, *, archive_keys, catalog_ok, budget_ok, removed, rewritten) -> list[str]:
def validate(records, *, archive_keys, catalog_ok, budget_ok, removed, rewritten, archive_records=None) -> list[str]:
errs: list[str] = []
archive_records = archive_records or []
decision_recs = [r for r in records if r.heading not in SKIP_HEADINGS and r.status != "legacy-unmigrated"]
active_by_key: dict[str, int] = {}
@@ -46,6 +47,14 @@ def validate(records, *, archive_keys, catalog_ok, budget_ok, removed, rewritten
errs.append(f"{r.heading}: missing required metadata {f}")
if r.status == "active":
active_by_key[r.key] = active_by_key.get(r.key, 0) + 1
if r.status in ("superseded", "retired"):
errs.append(
f"{r.heading}: status {r.status} but still in the active set — relocate to docs/decisions/archive/"
)
for r in archive_records:
if r.status == "active":
errs.append(f"{r.heading}: active record must not live under docs/decisions/archive/")
for key, n in active_by_key.items():
if n > 1:
@@ -76,7 +85,10 @@ def validate(records, *, archive_keys, catalog_ok, budget_ok, removed, rewritten
def _run(args: list[str]) -> str | None:
r = subprocess.run(args, capture_output=True, text=True)
try:
r = subprocess.run(args, capture_output=True, text=True, timeout=30)
except (OSError, subprocess.SubprocessError):
return None
return r.stdout if r.returncode == 0 else None
@@ -127,28 +139,24 @@ def _archive_records_at(ref: str):
def _rationale(rec) -> str:
"""Record body with the metadata block stripped, whitespace-normalized — the prose we protect."""
keep = []
seen_meta = False
for line in rec.body.splitlines():
s = line.strip()
if not seen_meta and (
s.startswith("`key:")
or s.startswith("**Rule:**")
or s.startswith("**Signals:**")
or s.startswith("**Mechanics:**")
):
seen_meta = True
continue
if seen_meta and (
s.startswith("**Rule:**")
or s.startswith("**Signals:**")
or s.startswith("**Mechanics:**")
or s.startswith("`key:")
):
continue
keep.append(s)
return "\n".join(x for x in keep if x)
"""Record body with the contiguous top metadata block stripped, whitespace-normalized.
Only the block from the first non-blank line (when it is the `key:` meta line) up to the first
following blank line is metadata. A **Rule:**/**Signals:**/**Mechanics:**/`key:` line appearing
later in rationale prose is prose, not metadata (mirrors decisions_lib's contiguous-block rule).
"""
lines = rec.body.splitlines()
start = 0
while start < len(lines) and not lines[start].strip():
start += 1
if start < len(lines) and lines[start].strip().startswith("`key:"):
end = start + 1
while end < len(lines) and lines[end].strip():
end += 1
rest = lines[:start] + lines[end:]
else:
rest = lines
return "\n".join(s for s in (ln.strip() for ln in rest) if s)
def _diff_findings(base: str, head: str) -> tuple[list[str], list[str]]:
@@ -221,6 +229,10 @@ def main(argv=None) -> int:
args = ap.parse_args(argv)
records = dl.all_active_records()
archive_records = []
if dl.ARCHIVE_DIR.exists():
for f in dl.ARCHIVE_DIR.glob("*.md"):
archive_records += dl.parse_file(f)
removed, rewritten = _diff_findings(args.base, args.head) if args.base and args.head else ([], [])
errs = validate(
records,
@@ -229,6 +241,7 @@ def main(argv=None) -> int:
budget_ok=_budget_ok(args.budget),
removed=removed,
rewritten=rewritten,
archive_records=archive_records,
)
unmigrated = [r for r in records if r.status == "legacy-unmigrated" and r.heading not in SKIP_HEADINGS]