Companion to the repo-doctor skill (the auditor that
scores a repo against this doctrine, plus the deep references on commenting, entry docs,
and monorepo structure). This file is the directive — how to write code, comments,
docs, and directory structure in every repo, on every task, so the work stays
navigable by the next agent session.
Every artifact you produce must pass the cold-agent test: a fresh session with zero conversation context must be able to find it, understand why it is the way it is, and modify it without breaking an invisible constraint. Session context dies at the end of every conversation — the repo is the only channel to your future self. Anything that lives only in the chat (a constraint you discovered, a format you invented, a reason you chose the weird option) is lost unless it lands in the code, a comment, or a doc in the same piece of work.
Three non-negotiables:
A 2026-07 audit of 10 active repos on this machine found the single best (GlyphWeb,
4.8/5) and one of the weakest (Simulacra, 3.2/5) were built by the same developer with
the same tooling in the same month. The difference was never effort — it was whether the
conventions below were applied as-you-go vs deferred. The recurring failures, every
one of them expensive for agents: 9,560-line files navigable only by grep; docs lagging
100+ commits behind code; idempotency-key formats with zero explanation two ADRs away
from their construction site; agents refactoring single-file apps into modules and
breaking file:// preview because nobody wrote down why it was one file. Enforcement
beats intention, and written doctrine is the first enforcement layer.
| Layer | Directive |
|---|---|
| Contract block (top of file) | Every substantive source file opens with 3–12 lines: what this module is, the invariants it enforces or relies on ("crash-safe + idempotent", "the ONLY module that touches D1", "no Math.random — deterministic replay"), and cross-refs to the doc/ADR that owns the reasoning. |
| WHY-only inline | Inline comments state constraints, invariants, units, ranges, and reasons — never narrate what the next line does. // order-independent so retries hash to the same key earns its place; // loop over the items never does. |
| Guard comments | Anything deliberately "wrong-looking" gets 2–5 lines starting with the constraint: why the file is one file, why the buffer is reused, why the allocation is hoisted, why the dependency is pinned. Name what breaks if it's "fixed". |
| Formats at the construction site | Wire formats, cache keys, hash inputs, magic strings: document the format grammar and the reason for each component where the string is built, even if an ADR also covers it. |
| Section markers | Files over ~400 lines get // === SECTION === markers and, over ~800, a top-of-file section map so an agent can jump instead of scroll. |
| Citations | Domain knowledge embedded in code (standards, papers, OSM way IDs, RFC numbers, spec sections) is cited inline — that's what makes domain code modifiable by a non-domain agent. |
| Docstrings/JSDoc | Public functions get them when the signature alone under-specifies: side effects, error behaviour, units, ordering guarantees. Private helpers with honest names don't need ceremony. |
What NOT to write: comments explaining what the code obviously does, comments addressed to the current reviewer ("fixed per feedback"), commented-out code, TODO without an owner-context ("TODO(cache): invalidate on rename — see #42" is fine; bare "TODO fix" is noise).
AGENTS.md per repo, and it is the single source of truth. It carries: what
the repo is (2–4 lines), run/test/check commands (exact, tested), a structure map
(folder → what lives there), conventions, and — mandatory — a Landmines section:
the specific things that break non-obviously (coupled golden fixtures, generated files
you must not hand-edit, ordering-sensitive registries). Landmines are the highest-value
lines in the file.docs/ and
link them. Setup guides for humans go in README or CONTRIBUTING, not AGENTS.md.src/xero/
beats src/utils2/.lint:db/"only this module touches
the DB" pattern — a 30-line check script makes a 9,000-line file safe).dev/; captured
verification images go to docs/screenshots/; generated artifacts are gitignored or
clearly marked generated ("built — don't hand-edit" header).test//tests/ per package, named for
the behaviour they defend. Adversarial tests are named for the adversary
(double-bill-nudge.test.ts, not billing2.test.ts) — the name tells the next agent
what evil the test blocks.check entry point (npm run check, just check, make
check) that gates typecheck + lint + tests + the repo's invariant scripts. If it
isn't one command, agents won't run it.docs/ with more than ~6 files gets an index (00_INDEX.md, or a PLAN.md that
serves the role): one line per doc — what it is, why you'd read it. Two rules that
keep an index alive: it carries its own maintenance instruction, and volatile lists
(ADRs, per-item inventories) are delegated to the filesystem ("see ls docs/adr/")
instead of hand-copied.docs(...) commits interleaved with feat(...) commits
is the healthy signature; a docs commit catching up 10 features later is the rot
signature.adr-ops skill): one decision
per record, rejected alternatives included. Everything else is a commit message or a
code comment — don't ceremonialise.The full playbook is repo-doctor's monorepo-structure reference. The directives:
check;
the root check fans out.repo-doctor flag what I just did? When in doubt, run it.Scratchpad experiments, one-shot probes, and generated/vendored code are exempt — but the moment a scratch artifact is committed to a repo, it either meets the doctrine or carries a header saying what it is and when it can be deleted. Solo creative projects (art, maps, one-off visualisations) may skip entry-doc ceremony, but the comment doctrine still applies — domain-heavy code with cited sources is precisely what keeps creative repos modifiable later.
~/.claude/skills/repo-doctor/SKILL.md — the auditor: scores any repo against this
doctrine; references own the depth (comment-doctrine, entry-docs, monorepo-structure,
scoring-rubric).~/.claude/skills/adr-ops/SKILL.md — decision records (when-to-write rule lives there).~/.claude/skills/doc-scanner/SKILL.md — reads/synthesizes/generates entry docs;
repo-doctor audits them.~/.claude/skills/refactor-ops/SKILL.md — the remediation path for monster files.naming-conventions.md — claude-mods component naming; this rule owns cross-repo
code/doc quality.