Lukasz Boryn e55f6e319b feat(write-OKF): rescan the page plan on refresh
Refresh proposes a plan delta instead of scanning once at birth: every
scan candidate and every modules/ or analyses/ line already in index.md
sorts into new, standing, or unwarranted, and the user locks the delta as
they lock the plan at scaffold. Jurisdiction stops at what a scan can
see.

Removal had no ruling, so it becomes decision 018: an unwarranted page is
deleted only when wholly derived, superseded otherwise. A wholly derived
page loses nothing a rescan cannot rebuild; for the rest 015 holds, and
history is the archive of record.

okf-scan gains the two clauses 005's weave flagged, both reproduced
first: a directory matched by name alone with a candidate beneath it
proposes nothing, and an unreadable directory yields no evidence rather
than raising and exiting 1.

Implements blueprint 006; ROADMAP gains phase 8, paid for by collapsing
the execution-order note.

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

101 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Propose OKF page candidates from a repo's shape. Usage: okf-scan <repo-root>
One candidate per line on stdout, index.md-ready:
<dir>/<name>.md — <evidence path>
Every line names the disk evidence that warranted it; no evidence, no
line. The scan proposes only — it never writes a page, never edits
index.md, never culls or ranks. Contract: ../taxonomy-scan.md
"""
import sys
from pathlib import Path
NOTEBOOKS = {".ipynb", ".Rmd", ".qmd"}
CODE = {".py", ".R", ".r", ".js", ".jsx", ".ts", ".tsx", ".go", ".rs",
".java", ".rb", ".c", ".h", ".cc", ".cpp", ".hpp", ".sh", ".sql"}
SOURCE_ROOTS = ("src", "lib", "R")
def directories(root):
"""Every directory under root, minus dot-directories and its own OKF/."""
for path in sorted(root.rglob("*")):
parts = path.relative_to(root).parts
if not path.is_dir() or parts[0] == "OKF":
continue
if any(part.startswith(".") for part in parts):
continue
yield path
def entries(directory):
"""Files and directories inside, or none — a permission bit is not a plan."""
try:
return sorted(directory.iterdir())
except OSError:
return []
def analysis_evidence(directory):
"""A directory holding a notebook, or one named analysis/analyses.
The notebook wins as evidence when both hold — a file is the more
concrete warrant.
"""
for entry in entries(directory):
if entry.is_file() and entry.suffix in NOTEBOOKS:
return entry
return directory if directory.name in ("analysis", "analyses") else None
def module_evidence(directory, root):
"""A directory holding code, one level under the root or a source root."""
if str(directory.relative_to(root).parent) not in (".", *SOURCE_ROOTS):
return None
for entry in entries(directory):
if entry.is_file() and entry.suffix in CODE:
return entry
return None
def main(argv):
if len(argv) != 2:
print("usage: okf-scan <repo-root>", file=sys.stderr)
return 2
root = Path(argv[1]).resolve()
if not root.is_dir():
print(f"not a directory: {root}", file=sys.stderr)
return 2
plan, containers, warranted = {}, {}, []
for directory in directories(root):
evidence, page = analysis_evidence(directory), "analyses"
if evidence is None:
evidence, page = module_evidence(directory, root), "modules"
if evidence is None:
continue
page = f"{page}/{directory.name}.md"
if page in plan:
continue
# Matched by name alone: a container, warranted only until a
# member speaks for itself.
if evidence is directory:
containers[page] = directory
else:
warranted.append(directory)
rel = evidence.relative_to(root)
plan[page] = f"{rel}/" if evidence.is_dir() else str(rel)
for page, container in containers.items():
if any(container in member.parents for member in warranted):
del plan[page]
for page in sorted(plan):
print(f"{page} — {plan[page]}")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))