Lukasz Boryn 2e9e6a7f44 feat(moor): graduate ferry decisions and collapse the moored ferry
Implements blueprint 003, phase 7's second half. moor gains two steps:
every resolved D-<n> that passes the warrant test graduates into the
why-register at the next free number, resolution body verbatim and
blocked-by edges dropped; then ferries/<topic>/ collapses into
ferries/<topic>.md carrying a sealed: pin (jj change id, git hash, ISO
date) read off @-, since the sealing commit cannot name itself.
Directory means open, file means moored, so `ls OKF/ferries/` finally
answers what deliberation is in flight.

Both guardrails that contradicted decisions 14/15 narrow rather than
gain exceptions: never collapse without a resolvable pin, never graduate
a decision that fails the warrant test. Done-criteria gate on okf-check,
deferred from blueprint 002 pending this rule.

okf-check learns the ferry shape rule — a page directly under ferries/
carries all three sealed: fields with a hex hash and an ISO date, and
may not coexist with its directory. Shape only: resolving the pin needs
the repo in hand, so refresh owns it, and a validator that cannot exit 0
on a tree copied outside its jj repo trains people to ignore it. That
split earns decision 017.

Verified against a throwaway scratch jj repo moored by hand per the
rewritten SKILL.md: blueprint 003's Verify block PASS, okf-check clean
on the fixture and on OKF/, moor at 106 lines and ROADMAP at 106.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-22 00:28:33 +02:00

213 lines
7.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""Validate an OKF bundle. Usage: okf-check <okf-dir>
Errors (exit 1): missing type, missing warrant, duplicate answer,
unresolvable cite, page unreachable from index.md, register index line
disagreeing with its decision file, moored ferry with a malformed
`sealed:` pin or a surviving directory. Warnings (exit 0): page over the
110-line cap, page with no cites.
Pin *shape* only — whether the change id still resolves needs the repo in
hand, which is write-OKF refresh's job, not a validator that must exit 0
on an OKF tree copied outside its jj repo.
"""
import re
import sys
from pathlib import Path
CAP = 110
def frontmatter(text):
"""Parse the leading --- block. Returns {} when absent.
Handles scalars, inline lists (`answers: [a, b]`) and block lists
(`answers:` followed by indented `- ` items).
"""
if not text.startswith("---\n"):
return {}
end = text.find("\n---", 3)
if end == -1:
return {}
fields, key = {}, None
for line in text[4:end].splitlines():
item = re.match(r"\s+-\s+(.*)", line)
if item and key:
fields[key].append(unquote(item.group(1)))
continue
pair = re.match(r"([A-Za-z_-]+):\s*(.*)", line)
if not pair:
continue
key, raw = pair.group(1), pair.group(2).strip()
if raw.startswith("[") and raw.endswith("]"):
fields[key] = [unquote(v) for v in raw[1:-1].split(",") if v.strip()]
elif raw:
fields[key] = unquote(raw)
key = None
else:
fields[key] = []
return fields
def unquote(value):
value = value.strip()
if len(value) > 1 and value[0] == value[-1] and value[0] in "\"'":
return value[1:-1]
return value
def links(text):
"""Markdown link targets, minus anchors and external URLs."""
out = []
for target in re.findall(r"\]\(([^)]+)\)", text):
target = target.split("#")[0].strip()
if target and "://" not in target:
out.append(target)
return out
def reachable(okf, pages):
"""Pages reachable from index.md, transitively through index pages.
A ferry's decision files are covered by their MAP.md, which is the
single home for them by layout contract.
"""
seen, frontier = set(), [okf / "index.md"]
while frontier:
page = frontier.pop()
if page in seen or not page.exists():
continue
seen.add(page)
if page.name not in ("index.md", "MAP.md"):
continue
for target in links(page.read_text()):
frontier.append((page.parent / target).resolve())
for page in pages:
parent_map = page.parent / "MAP.md"
if parent_map in seen:
seen.add(page)
return seen
def cite_resolves(cite, page, okf):
path = cite.split("#")[0].split(":")[0].strip()
if not path:
return False
roots = [page.parent, okf, okf.parent]
return any((root / path).exists() for root in roots)
def pin(text):
"""Fields of `sealed:`, the one nested block the frontmatter carries."""
head = text.partition("\n---")[0] if text.startswith("---\n") else ""
block = re.search(r"^sealed:\n((?:[ \t]+.*\n?)+)", head, re.M)
return dict(re.findall(r"([a-z]+):\s*(\S+)", block.group(1))) if block else {}
def check_ferries(okf, errors):
"""A page directly under ferries/ is a moored ferry: pinned, collapsed."""
ferries = okf / "ferries"
if not ferries.is_dir():
return
for page in sorted(ferries.glob("*.md")):
rel = page.relative_to(okf.parent)
if (ferries / page.stem).is_dir():
errors.append(f"{rel}: coexists with ferries/{page.stem}/ — open or moored, never both")
fields = pin(page.read_text())
missing = [f for f in ("change", "commit", "date") if f not in fields]
if missing:
errors.append(f"{rel}: sealed: missing {', '.join(missing)}")
continue
if not re.fullmatch(r"[0-9a-f]{8,64}", fields["commit"]):
errors.append(f"{rel}: sealed: commit is not a hex hash: {fields['commit']}")
if not re.fullmatch(r"\d{4}-\d{2}-\d{2}", fields["date"]):
errors.append(f"{rel}: sealed: date is not ISO 8601: {fields['date']}")
def check_register(okf, warrants, errors):
"""Register index lines must quote their decision's own question."""
index = okf / "decisions" / "index.md"
if not index.exists():
return
for line in index.read_text().splitlines():
if not line.startswith("- "):
continue
target = links(line)
fields = line.split(" — ")
if not target or len(fields) < 2:
errors.append(f"{index}: malformed register line: {line}")
continue
page = (index.parent / target[0]).resolve()
answers = warrants.get(page, {}).get("answers") or []
if not answers:
errors.append(f"{index}: line points at a page with no answers: {target[0]}")
elif fields[1].strip() != answers[0]:
errors.append(
f"{index}: line question disagrees with {target[0]}: "
f"{fields[1].strip()!r} vs {answers[0]!r}"
)
def main(argv):
if len(argv) != 2:
print("usage: okf-check <okf-dir>", file=sys.stderr)
return 2
okf = Path(argv[1]).resolve()
if not okf.is_dir():
print(f"not a directory: {okf}", file=sys.stderr)
return 2
pages = sorted(p.resolve() for p in okf.rglob("*.md"))
index = okf / "index.md"
errors, warnings, warrants, answered = [], [], {}, {}
seen = reachable(okf, pages)
for page in pages:
text = page.read_text()
fields = frontmatter(text)
warrants[page] = fields
rel = page.relative_to(okf.parent)
if page not in seen:
errors.append(f"{rel}: not reachable from index.md")
if page == index:
continue
if not fields.get("type"):
errors.append(f"{rel}: missing type:")
if len(text.splitlines()) > CAP:
warnings.append(f"{rel}: over the {CAP}-line cap")
# The warrant contract covers durable synthesis pages; blueprints
# and ferries carry their own frontmatter contracts.
if {"blueprints", "ferries"} & set(rel.parts) or page.name == "index.md":
continue
if not fields.get("purpose"):
errors.append(f"{rel}: missing purpose:")
if not fields.get("answers"):
errors.append(f"{rel}: missing answers:")
for answer in fields.get("answers") or []:
if answer in answered:
errors.append(f"{rel}: answers a question {answered[answer]} owns: {answer}")
else:
answered[answer] = rel
cites = fields.get("cites") or []
if not cites:
warnings.append(f"{rel}: no cites:")
for cite in cites:
if not cite_resolves(cite, page, okf):
errors.append(f"{rel}: unresolvable cite: {cite}")
check_ferries(okf, errors)
check_register(okf, warrants, errors)
for line in warnings:
print(f"WARN {line}")
for line in errors:
print(f"ERROR {line}")
print(f"{len(errors)} error(s), {len(warnings)} warning(s)")
return 1 if errors else 0
if __name__ == "__main__":
sys.exit(main(sys.argv))