2 min read
kevin-wiki / scripts/agent-docs
Purpose
The Agent-Docs Mesh kit — the engine that scaffolds, adopts, and health-checks
the layered agent documentation (AGENTS.md + per-directory SKILL.md, with
memory.md + RESOLVER.md coming in later plans) for any repo. It detects a
repo's stack and structure, renders/refreshes machine-derived auto-blocks
without ever clobbering human prose/fill-slots, and runs a docs-doctor.
Mental model & key files
detect/— pure, synchronous detectors (StackDetector,DirtreeDetector) each extendBaseDetectorand contribute part of aRepoProfile; merged indetect/index.ts.render/—markers.ts(idempotent auto-block engine),templates/(module- drivenAGENTS.md+SKILL.md),scaffold.ts(non-destructive plan: refresh auto-blocks, append missing ones, leave prose untouched).doctor/—BaseCheckregistry (structure,safety,links) run bydoctor/index.ts; mirrors the shape of the wiki'sscripts/doctor.ts.lib/—repo-walk.ts(significant-dir detection),write-plan.ts(apply planned writes).index.ts— CLI:init|scaffold|doctor.
Patterns to follow / invariants
- TDD with
node:test+assert/strict; runnpm run test:agent-docs. - ESM: local relative imports use a
.jsextension (NodeNext). - Auto-blocks (
<!-- agent-docs:auto:<id> ... -->) are always regenerated; fill-slots / prose are never overwritten. This is the core safety property. - A significant dir = depth ≤ 2 from the run root AND ≥ 3 source files.
Missing per-dir
SKILL.mdis a warn; onlyAGENTS.md+ rootSKILL.mdare hard-required. - Symlinks (and dangling links) are skipped in the walk — never followed.
- Extend, don't fork: a new signal → subclass
BaseDetector; a new health check → subclassBaseCheck.
Common tasks → first action
- Add a doctor check → create
doctor/checks/<name>.tsextendingBaseCheck, register it in theCHECKSarray indoctor/index.ts, add a red→green test. - Add a detector → create
detect/<name>-detector.tsextendingBaseDetector, register inDETECTORSindetect/index.ts. - Change generated doc shape → edit
render/templates/agents.ts/skill.ts. - Run against a repo →
node --import tsx scripts/agent-docs/index.ts <init|scaffold|doctor> <root> [--json].
Gotchas
significantDirsdepth is measured from the run root — for a monorepo, enroll each package separately so its modules sit at depth ≤ 2.upsertAutoBlockis a no-op if the marker is absent;mergeAgentsappends missing blocks under the "Agent-docs (auto-maintained)" section instead.reportDoctorwrites.agent-docs/doctor-results.jsononly if.agent-docs/already exists (so a pre-enrollmentdoctorrun is read-only).parseCliArgsstrips flags before the repo root —doctor --json(no root) defaults tocwd. Other repos usescripts/run-agent-docs.tsshim → canonical kit.