fix(603): close four defects found in adversarial review of the stale-after fields
1. `date.fromisoformat` is not a YYYY-MM-DD validator. On Python >= 3.11 it also
accepts ISO basic format ("20270101") and week dates ("2027-W01-1"), so a
malformed-looking value passed the blocking check — and which forms parse
depends on the interpreter, meaning the same corpus could validate differently
on a dev machine and on the runner (pr-checks.yml pins only python-version
'3.x'). Knock-on: the catalog's Review-due section sorts on the raw STRING, so
an accepted "20270101" sorted AFTER "2027-01-15" ('-' < '0'), contradicting the
section's own "sorted soonest-first" text. Gate on ^\d{4}-\d{2}-\d{2}$ first,
which fixes both — a fixed-width zero-padded form makes string sort == date sort.
2. A present-but-empty `stale-after:` was collapsed to None by `or None` in the
parser and then skipped by a truthiness guard in the validator, so it passed as
"absent" — a field that silently never fires, which is the exact failure mode
the blocking check exists to prevent. Keep "" distinct from None and test with
`is not None`.
3. `test_catalog_is_date_independent` was partly vacuous: with no date in either
render, both sides were trivially equal after the .replace(). It did still catch
an injected clock-derived marker, but it passed with the feature deleted. Assert
the dates are present.
4. The malformed-date check ran only over the active set, exempting archive
records. Staleness is moot there, but a typo is still a typo — check both wings.
Adds regression tests for each, plus a Review-due row for a topic-file record
(pinning the `../decisions.md` vs bare-filename link forms).
This commit is contained in:
@@ -84,7 +84,11 @@ def parse_text(text: str, source: Path) -> list[Record]:
|
||||
rec.since = meta.get("since") or None
|
||||
rec.supersedes = meta.get("supersedes") or None
|
||||
rec.superseded_by = meta.get("superseded-by") or None
|
||||
rec.stale_after = meta.get("stale-after") or None
|
||||
# NOT `or None`: a present-but-empty `stale-after:` must stay "" so the validator can
|
||||
# tell it from an absent field and reject it. Collapsing the two would let a blank or
|
||||
# truncated value through as "absent" — a field that silently never fires, which is
|
||||
# exactly what the blocking format check exists to prevent.
|
||||
rec.stale_after = meta.get("stale-after")
|
||||
# The metadata block is contiguous: scan only until the first blank line, so a
|
||||
# bolded **Rule:** appearing later inside rationale prose can't overwrite the real one.
|
||||
for bl2 in body_lines[k + 1 :]:
|
||||
|
||||
@@ -11,6 +11,7 @@ parse issues), matching the old guard.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from datetime import date
|
||||
@@ -27,14 +28,27 @@ REQUIRED_META = ("key", "status", "since", "supersedes", "superseded_by", "signa
|
||||
EDIT_TOKEN = "[decisions-edit]" # noqa: S105 (a commit-message marker, not a credential)
|
||||
|
||||
|
||||
# `date.fromisoformat` is NOT a YYYY-MM-DD validator: on Python >= 3.11 it also accepts ISO basic
|
||||
# format ("20270101") and week dates ("2027-W01-1"), and which of those parse depends on the
|
||||
# interpreter version — so the same corpus could validate differently on a dev machine and on the
|
||||
# runner (pr-checks.yml pins only `python-version: '3.x'`). Gate on the extended calendar form first.
|
||||
# The catalog's Review-due section additionally sorts on the raw STRING, which is only equivalent to
|
||||
# sorting by date because this regex forces a fixed-width zero-padded form.
|
||||
_STALE_AFTER_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
|
||||
|
||||
|
||||
def _parse_stale_after(value: str | None) -> date | None:
|
||||
"""`stale-after` as a date, or None if absent/malformed. Callers distinguish the two."""
|
||||
if not value:
|
||||
"""`stale-after` as a date, or None if absent, empty, or malformed.
|
||||
|
||||
Callers must NOT use a None return to mean "absent" — an empty or malformed value returns None
|
||||
too. Distinguish on the raw field with `is not None` (absent stays None; present-but-empty is "").
|
||||
"""
|
||||
if not value or not _STALE_AFTER_RE.match(value.strip()):
|
||||
return None
|
||||
try:
|
||||
return date.fromisoformat(value.strip())
|
||||
except ValueError:
|
||||
return None
|
||||
return None # e.g. 2027-02-30 — well-shaped but not a real date
|
||||
|
||||
|
||||
def stale_records(records, today: date) -> list[tuple[str, str]]:
|
||||
@@ -89,7 +103,7 @@ def validate(
|
||||
errs.append(f"{r.heading}: missing required metadata {f}")
|
||||
# `stale-after` is OPTIONAL, but a malformed one is a defect in the commit that wrote it —
|
||||
# it would silently never fire. Absent is fine; unparseable is not.
|
||||
if r.stale_after and _parse_stale_after(r.stale_after) is None:
|
||||
if r.stale_after is not None and _parse_stale_after(r.stale_after) is None:
|
||||
errs.append(f"{r.heading}: stale-after {r.stale_after!r} is not a YYYY-MM-DD date")
|
||||
if r.status == "active":
|
||||
active_by_key[r.key] = active_by_key.get(r.key, 0) + 1
|
||||
@@ -104,6 +118,10 @@ def validate(
|
||||
for r in archive_records:
|
||||
if r.status == "active":
|
||||
errs.append(f"{r.heading}: active record must not live under docs/decisions/archive/")
|
||||
# An archived record's staleness is moot, but a malformed date there is still a typo worth
|
||||
# catching — and checking both wings keeps the field's contract symmetric.
|
||||
if r.stale_after is not None and _parse_stale_after(r.stale_after) is None:
|
||||
errs.append(f"{r.heading}: stale-after {r.stale_after!r} is not a YYYY-MM-DD date")
|
||||
|
||||
for key, n in active_by_key.items():
|
||||
if n > 1:
|
||||
|
||||
@@ -87,6 +87,22 @@ def test_catalog_is_date_independent():
|
||||
time-dependent render would drift red on a calendar boundary with no commit touching it."""
|
||||
past = bc.render_catalog([_r("a.b", stale_after="2020-01-01")])
|
||||
future = bc.render_catalog([_r("a.b", stale_after="2099-01-01")])
|
||||
# Guard against vacuity: without this the assertion below passes trivially on a renderer that
|
||||
# emits no dates at all (i.e. with the whole feature deleted).
|
||||
assert "2020-01-01" in past and "2099-01-01" in future
|
||||
# A long-past and a far-future date must produce byte-identical output apart from the date
|
||||
# itself: no stale/fresh verdict, no marker, nothing derived from the clock.
|
||||
assert past.replace("2020-01-01", "D") == future.replace("2099-01-01", "D")
|
||||
|
||||
|
||||
def test_review_due_row_links_correctly_from_a_topic_file():
|
||||
"""Review-due rows reuse _rel(), so a topic-file record must not get decisions.md's `../` form."""
|
||||
out = bc.render_catalog(
|
||||
[
|
||||
_r("t.opic", source=Path("docs/decisions/workflow-process.md"), stale_after="2027-01-01"),
|
||||
_r("m.ain", source=Path("docs/decisions.md"), stale_after="2027-01-02"),
|
||||
]
|
||||
)
|
||||
due = out.split("## Review due")[1]
|
||||
assert "(workflow-process.md#" in due
|
||||
assert "(../decisions.md#" in due
|
||||
|
||||
@@ -47,3 +47,15 @@ def test_optional_fields_default_to_none_when_absent():
|
||||
r = next(r for r in recs if r.key == "ci.runner-placement")
|
||||
assert r.stale_after is None
|
||||
assert r.sources is None
|
||||
|
||||
|
||||
def test_empty_stale_after_parses_as_empty_string_not_none():
|
||||
"""Absent vs present-but-empty must stay distinguishable for the validator."""
|
||||
text = (
|
||||
"## H\n"
|
||||
"`key: a.b` · `status: active` · `since: 2026-01-01` · `stale-after:` "
|
||||
"· `supersedes: none` · `superseded-by: none`\n"
|
||||
"**Rule:** r\n"
|
||||
)
|
||||
r = dl.parse_text(text, Path("fake.md"))[0]
|
||||
assert r.stale_after == ""
|
||||
|
||||
@@ -456,3 +456,25 @@ def test_stale_records_ignores_malformed_date():
|
||||
def test_sources_is_optional_and_never_required():
|
||||
assert _v([_rec(key="a.b")]) == []
|
||||
assert _v([_rec(key="a.b", sources="ci run 1234 · docs/ci-cd.md")]) == []
|
||||
|
||||
|
||||
def test_empty_stale_after_is_malformed_not_absent():
|
||||
"""A blank/truncated value must not pass as 'absent' — it would silently never fire."""
|
||||
errs = _v([_rec(key="a.b", stale_after="")])
|
||||
assert any("not a YYYY-MM-DD date" in e for e in errs)
|
||||
|
||||
|
||||
def test_noncanonical_iso_forms_are_rejected():
|
||||
"""date.fromisoformat accepts these on py>=3.11; the field's contract is YYYY-MM-DD only.
|
||||
|
||||
Accepting them would also break the catalog's Review-due sort, which orders on the raw string.
|
||||
"""
|
||||
for bad in ("20270101", "2027-W01-1", "2027-01-01T00:00:00", " 2027-01-01 extra"):
|
||||
errs = _v([_rec(key="a.b", stale_after=bad)])
|
||||
assert any("not a YYYY-MM-DD date" in e for e in errs), bad
|
||||
|
||||
|
||||
def test_malformed_stale_after_on_an_archive_record_is_caught():
|
||||
arch = _rec(key="a.old", status="superseded", superseded_by="a.b", stale_after="soon")
|
||||
errs = _v([_rec(key="a.b", supersedes="a.old")], archive_records=[arch], archive_keys={"a.old"})
|
||||
assert any("not a YYYY-MM-DD date" in e for e in errs)
|
||||
|
||||
Reference in New Issue
Block a user