fix(621): close three exemption holes and pin the guard's own wiring, per cold review
An independent cold-context review found the guard could be disabled or bypassed in four ways, one of them silently. HIGH — the guard's only wiring was untested. Deleting `wing_faults=record_wing_faults()` from main() left the ENTIRE suite green (120 passed) while a real block-scalar record vanished again with `decisions-validate: OK`. Every existing test either called the collector directly or handed validate() a hand-built list, so nothing pinned that main() ever invokes it — the #609 "prints OK while doing nothing" defect, one level up, inside the fix for that same defect. Independently reproduced before fixing. Now pinned by a monkeypatched sentinel test. MEDIUM — a record could escape by BASENAME. `_NON_DECISION_FILES` is a set of TOPIC-dir names ({README, migration-map, retrieval-eval}) and was applied to the wings too, so a genuine record at records/docs/retrieval-eval.md was skipped. Not hypothetical: the path<->key rule forces key `docs.retrieval-eval` to that exact filename, and dl.active_files() applies that filter only to the TOPIC_DIR glob — so the file would be a corpus source while exempt from the guard. Now nothing is exempt by basename. MEDIUM — the archive exemption was by LOCATION but justified by IDENTITY. Anything directly in archive/ was skipped, so a new unparseable archive/foo.md would vanish in the one directory left unwatched. Now exempt only if it really is a #610 stripped index: one keyless record with a known generated heading. LOW — parse-to-WRONG. `rule: >-` with an UNINDENTED continuation containing a colon parses to one VALID keyed record whose rule is literally ">-", plus a junk key, silently truncating the real value. PyYAML rejects that input, so the hand reader is more permissive than the writer. Now faulted via an unknown-frontmatter-key check. LOW — an absent or empty wing reported clean. Now loud: refusing to certify a corpus from a scan of nothing. Doc corrections, all measured rather than asserted: "all 167 records use single quoted lines" was false (117 of 168 rule: values are unquoted plain scalars — the invariant is single-LINE); the archive shape in the record, ci-cd.md and the rule: field said `archive/<area>/` where the code deliberately uses archive/** ; the mutation count is 10, not five; and the "converts ANY reader limitation" claim is softened to the class it actually covers. 125 passed. Refs #621
This commit is contained in:
@@ -415,31 +415,48 @@ def _diff_findings(base: str, head: str) -> tuple[list[str], list[str], list[str
|
||||
def record_wing_files(records_dir: Path | None = None, archive_dir: Path | None = None) -> list[Path]:
|
||||
"""Every file that MUST be exactly one keyed record.
|
||||
|
||||
Scope is the split-layout record wings only:
|
||||
* `docs/decisions/records/**/*.md` — the active wing
|
||||
* `docs/decisions/archive/<area>/*.md` — the archived wing, nested by area since #610
|
||||
Scope is EVERY `*.md` under `records/**` and `archive/**` — including the files directly in
|
||||
`archive/`, which are scanned but may qualify for the stripped-index exemption applied in
|
||||
`record_wing_faults` (see there). Nothing is excluded by BASENAME.
|
||||
|
||||
Deliberately NOT included: `docs/decisions/archive/*.md` at the top level. Those are the five
|
||||
stripped legacy topic files left behind by the #610 split (`api.md`, `scan.md`, `spa.md`,
|
||||
`startup.md`, `release-ci-governance.md`); each parses to a single KEYLESS pseudo-record from its
|
||||
generated "Records formerly in this file" index, which is exactly what keeps older date-based
|
||||
pointers resolvable. They are indexes, not records. `_NON_DECISION_FILES` (README.md and
|
||||
friends) is filtered on top of that.
|
||||
Excluding by basename was a real hole: `_NON_DECISION_FILES` is `{README.md, migration-map.md,
|
||||
retrieval-eval.md}` — three TOPIC-dir names — and applying it to the wings meant a genuine
|
||||
record at `records/docs/retrieval-eval.md` was silently skipped. That path is not hypothetical:
|
||||
the path<->key rule forces key `docs.retrieval-eval` to live at exactly that filename, and
|
||||
`docs/decisions/retrieval-eval.md` is a real unmigrated file, i.e. a plausible migration target.
|
||||
Worse, `dl.active_files()` applies that filter only to the TOPIC_DIR glob, not to
|
||||
`RECORDS_DIR.rglob` — so such a file IS a corpus source while being exempt from the guard.
|
||||
Only a literal `README.md` sitting directly in a wing root is dropped here (no such file exists
|
||||
today; the catalog README lives in TOPIC_DIR, one level up).
|
||||
"""
|
||||
records_dir = dl.RECORDS_DIR if records_dir is None else records_dir
|
||||
archive_dir = dl.ARCHIVE_DIR if archive_dir is None else archive_dir
|
||||
files: list[Path] = []
|
||||
if records_dir.exists():
|
||||
files += sorted(records_dir.rglob("*.md"))
|
||||
if archive_dir.exists():
|
||||
# rglob + "not directly in archive/", NOT glob("*/*.md"). Both exempt the top-level
|
||||
# stripped legacy files, but a one-level glob would ALSO skip anything nested deeper —
|
||||
# letting a record at `archive/<area>/<sub>/x.md` escape the check entirely. Silently
|
||||
# skipping a path is the exact failure mode this function exists to close, so the
|
||||
# exemption is expressed as the precise condition (depth-1 file) rather than a shape that
|
||||
# happens to match today's layout.
|
||||
files += sorted(p for p in archive_dir.rglob("*.md") if p.parent != archive_dir)
|
||||
return [p for p in files if p.name not in dl._NON_DECISION_FILES]
|
||||
for wing in (records_dir, archive_dir):
|
||||
if wing.exists():
|
||||
files += sorted(p for p in wing.rglob("*.md") if not (p.parent == wing and p.name == "README.md"))
|
||||
return files
|
||||
|
||||
|
||||
def _is_stripped_index(path: Path, archive_dir: Path, recs: list) -> bool:
|
||||
"""True for a #610 stripped legacy topic file sitting DIRECTLY in `archive/`.
|
||||
|
||||
Those five files (`api.md`, `scan.md`, `spa.md`, `startup.md`, `release-ci-governance.md`) are
|
||||
generated "Records formerly in this file" indexes — keyless by construction, and what keeps
|
||||
older date-based pointers resolvable. They are indexes, not records.
|
||||
|
||||
The exemption is by IDENTITY, not by location. Exempting everything directly in `archive/`
|
||||
would leave that one directory unguarded: a new unparseable `archive/foo.md` would vanish
|
||||
silently — the same defect this guard exists to close, in the last place it isn't watched. So a
|
||||
file there is exempt only if it actually LOOKS like a stripped index: exactly one keyless
|
||||
record whose heading is one of the known generated ones (`dl.SKIP_HEADINGS`).
|
||||
"""
|
||||
return (
|
||||
path.parent == archive_dir
|
||||
and len(recs) == 1
|
||||
and not recs[0].key
|
||||
and recs[0].heading in dl.SKIP_HEADINGS
|
||||
)
|
||||
|
||||
|
||||
def record_wing_faults(records_dir: Path | None = None, archive_dir: Path | None = None) -> list[str]:
|
||||
@@ -455,31 +472,83 @@ def record_wing_faults(records_dir: Path | None = None, archive_dir: Path | None
|
||||
closes the case the diff check structurally cannot see: a NEWLY ADDED record, where the author's
|
||||
own PR looks clean because there is no prior state to diff against.
|
||||
|
||||
Making it path-driven rather than record-driven is the point: it converts ANY present or future
|
||||
reader limitation from silent to loud, instead of enumerating the constructs we happen to know
|
||||
about today.
|
||||
Making it path-driven rather than record-driven is the point: it converts a whole CLASS of
|
||||
reader limitations — parse-to-zero, parse-to-many, keyless — from silent to loud in one move,
|
||||
instead of enumerating the constructs we happen to know about today.
|
||||
|
||||
It does NOT catch parse-to-WRONG: frontmatter that yields one keyed record with corrupted
|
||||
values. `rule: >-` followed by an UNINDENTED continuation containing a colon parses to a valid
|
||||
record whose `rule` is literally `>-`, plus a junk key from the continuation line. The junk-key
|
||||
check below is what catches that case; a mis-parse producing only known fields would still slip
|
||||
through, so this is a strong guard, not a total one.
|
||||
"""
|
||||
records_dir = dl.RECORDS_DIR if records_dir is None else records_dir
|
||||
archive_dir = dl.ARCHIVE_DIR if archive_dir is None else archive_dir
|
||||
faults: list[str] = []
|
||||
for p in record_wing_files(records_dir, archive_dir):
|
||||
files = record_wing_files(records_dir, archive_dir)
|
||||
|
||||
# A wing that is absent or empty must be LOUD, not silently clean. Otherwise a partial checkout,
|
||||
# a renamed directory, or a bad monkeypatch turns the whole guard into a no-op that reports
|
||||
# success — which is the defect this function exists to close, applied to itself.
|
||||
if not records_dir.exists() or not any(records_dir.rglob("*.md")):
|
||||
faults.append(
|
||||
f"{records_dir}: the active record wing is missing or contains no *.md files — refusing "
|
||||
f"to report a clean corpus from an empty scan."
|
||||
)
|
||||
|
||||
for p in files:
|
||||
try:
|
||||
recs = dl.parse_file(p)
|
||||
except Exception as exc: # unreadable/undecodable file is a fault, not a crash
|
||||
faults.append(f"{p}: could not be read as a decision record ({exc})")
|
||||
continue
|
||||
if _is_stripped_index(p, archive_dir, recs):
|
||||
continue
|
||||
if len(recs) != 1:
|
||||
faults.append(
|
||||
f"{p}: parsed to {len(recs)} records, expected exactly 1 — a file under the record "
|
||||
f"wings must be one record. Common cause: YAML the dependency-free frontmatter "
|
||||
f"reader does not accept (a block scalar such as `rule: >-` or `rule: |`, any "
|
||||
f"indented/nested structure, or unterminated `---` frontmatter). Put the value on "
|
||||
f"ONE quoted line, as all other records do."
|
||||
f"ONE line."
|
||||
)
|
||||
continue
|
||||
if not recs[0].key:
|
||||
faults.append(f"{p}: parsed to a record with no `key` — every record in the wings must be keyed.")
|
||||
continue
|
||||
# Junk keys: the reader accepted a line it should not have. The realistic source is a block
|
||||
# scalar whose continuation was read as its own `k: v` pair, which silently truncates the
|
||||
# real value (`rule` becomes `>-`). PyYAML REJECTS that input, so the hand reader is more
|
||||
# permissive than the writer and nothing else in the pipeline notices.
|
||||
unknown = _unknown_frontmatter_keys(p)
|
||||
if unknown:
|
||||
faults.append(
|
||||
f"{p}: frontmatter has unrecognized key(s) {sorted(unknown)} — a value was probably "
|
||||
f"split across lines (a block scalar continuation read as its own key), which "
|
||||
f"silently truncates the real value. Put each value on ONE line."
|
||||
)
|
||||
return faults
|
||||
|
||||
|
||||
def _unknown_frontmatter_keys(path: Path) -> set[str]:
|
||||
"""Frontmatter keys outside the known schema. Empty on any read/parse failure (reported elsewhere)."""
|
||||
try:
|
||||
text = path.read_text(encoding="utf-8")
|
||||
except Exception:
|
||||
return set()
|
||||
if not dl.has_frontmatter(text):
|
||||
return set()
|
||||
lines = text.splitlines()
|
||||
end = next((i for i, ln in enumerate(lines[1:], start=1) if ln.rstrip() == "---"), None)
|
||||
if end is None:
|
||||
return set()
|
||||
meta = dl._read_frontmatter("\n".join(lines[1:end]))
|
||||
if not meta:
|
||||
return set()
|
||||
known = set(dl._FM_TO_FIELD) | {"title"}
|
||||
return {k for k in meta if k not in known}
|
||||
|
||||
|
||||
def _archive_keys() -> set[str]:
|
||||
keys: set[str] = set()
|
||||
if dl.ARCHIVE_DIR.exists():
|
||||
|
||||
Reference in New Issue
Block a user