# CLAUDE.md — rules for all agents working on lucisv lucisv is a ruff-style Verilog/SystemVerilog formatter/linter in Rust. Read `README.md` for architecture and `TODO.md` for the backlog before doing anything. ## Hard invariants (never break these) 1. **Never corrupt code.** The formatter either produces verified-correct output or emits the input unchanged with an error. No partial output. 2. **Idempotent.** `format(format(x)) == format(x)`, always. 3. **Comments are never dropped.** 4. **Any widening of `verify.rs`'s forgiveness is guilty until proven innocent.** Every new or broadened normalization in the safety net ships WITH masked-corruption attack tests (crafted (input, corrupted output) pairs the normalization could plausibly forgive, asserted rejected) — written by the dev, re-attacked by QA. History: the one real corruption lucisv ever produced (macro-items, 2026-08-19) slipped through a normalization added without such tests. ## Simplicity first - Prefer the boring, obvious implementation. If you're building an abstraction for a second user that doesn't exist yet, stop. - Standard library first. Adding a dependency requires a written justification in the feature README. - Small functions, descriptive names, shallow nesting. Code should be readable top-to-bottom without jumping around. - Match the style of the surrounding code (comment density, naming, module layout). ## File and function size Files and functions are sized for a human reader, not a compiler. - **Target 300–600 production lines per file.** At 800, consider splitting. Above 1,200, an explicit reason not to split goes in the feature README. Test lines do not count, but a `#[cfg(test)]` block that dwarfs its file moves to its own module or `tests/` file. - **Functions: 10–50 lines.** Investigate anything over 80; anything over 150 needs a reason in a comment (a dispatch table over an enum is the usual one). - Looser for declarative tables, generated code, snapshots and repetitive tests. Complexity matters more than the count: 800 lines of grammar productions read more easily than 250 lines of state. - **The real test is one sentence.** A human should be able to say what a file is responsible for in one sentence and predict which parts a change touches. Several "and" clauses mean the file wants to be several modules. - A split is a **pure move**: public paths stay the same (re-export from the old `mod`), behaviour and output stay byte-identical, the test count does not drop. Measured, not asserted — the corpus and opentitan byte-identity gates are the proof. Measured 2026-09-02 (production lines): `vlog_parser/src/parser.rs` 8 786, `vlog_formatter/src/format.rs` 5 444, `vlog_parser/src/ast.rs` 3 313, `lucisv_server/src/server.rs` 2 559 (+6 300 test), `vlog_semantic/src/bind.rs` 2 378, `vlog_semantic/src/workspace.rs` 1 975, `vlog_refactor/src/ports.rs` 1 778, `lucisv_server/src/completion.rs` 1 716 — thirteen files over 800. Every one of them was where feature lanes collided, which is the other reason to split them. **Decomposed the same day by TODO §28** (four lanes, pure moves, output byte-identical): `parser/` (19 modules) and `ast/` (8), `format/` (15), `server/` (11 + tests) and `completion/` (6), `bind/` (8), `workspace/` (5), `ports/` (6), `rename/` (4). Measured after the round (non-blank, non-comment, outside tests): two files over 600 — `lucisv_server/src/perf.rs` 895 (one cohesive subsystem; the reason is in the decomposition-server README) and `vlog_parser/src/lexer.rs` 762 — and 183 under it. Nothing new may push a file back over the thresholds: a dev adding to a file near 600 puts the code in a new module. Re-measured 2026-09-07 at 0.49.0 (same rule, before `mod tests`): four files over 600 — `lucisv_server/src/state.rs` 958 (grew across §37–§49; a decomposition is filed as TODO §55: scope/widen, the document store, the source selectors are three sentences), `perf.rs` 900 (reasoned), `lexer.rs` 763, `vlog_formatter/src/format/stmt.rs` 604 (at the mark; the next statement arm goes in a new module) — and everything else under it. Re-measured 2026-09-23 for the MCP block (`crates/lucisv_mcp`, nine crates now): largest files `tools/read.rs` 405, `tools/item.rs` 379, `tools/item/verify.rs` 363, `tools/nav.rs` 340, `tools/diagnostics.rs` 330, `tools/interface.rs` 322, `tools/hierarchy.rs` 316 — everything under 600; the M3.5 engine lane left `vlog_refactor/src/rename/connections.rs` at 477 and `check/mod.rs` at 426, and `lucisv_server/src/state/mod.rs` at 985 (already over; TODO §55). Re-measured 2026-10-14 after TODO §55 (pure move, `docs/features/decomposition-state`): `state/mod.rs` 63, the largest `state/` file `discover.rs` 412 (`rescan.rs` 411); files over 600 workspace-wide: `lucisv_server/src/perf.rs` 907 (reasoned), `vlog_parser/src/lexer.rs` 771, `vlog_formatter/src/format/stmt.rs` 604. Re-measured 2026-11-19 at 0.66.0, after the overnight queue (TODO §71; non-blank, non-comment lines before `mod tests`): two files over 600 — `perf.rs` 907 (reasoned) and `lexer.rs` 771 — then `vlog_semantic/src/resolve.rs` 587 and `vlog_parser/src/parser/item.rs` 582, both at the mark: the next addition to either goes in a new module. The queue's new modules are all small (`server/ahead.rs`, `same_file.rs`, `completion/related.rs`, `server/viewport.rs`, `server/caret.rs`, `state/watched.rs`, `folding/directives.rs`, formatter `style.rs` / `auto.rs`); `server/dispatch.rs` is 503 and `server/index.rs` is where four of the eight features met. ## Rust standards - `cargo fmt` clean, `cargo clippy --all-targets` clean (no `#[allow]` without a comment explaining why), `cargo test` green — all three before declaring any work finished. **Before a release, and whenever the whole workspace is gated, run `tools/gates.sh`** (every gate as a separate guarded step with kept logs; `--only`/`--skip` for a subset) instead of a bare `cargo test --workspace`. - No `unwrap()`/`expect()`/`panic!` in library code paths; return `Result`. Tests and `main.rs` argument handling may use them. - Public items get a one-line doc comment. Doc comments say what/why, not how. - No `unsafe`. ## Document-based development Every feature lives in `docs/features//README.md`. That README is the single source of truth for the feature and must contain these sections: ```markdown # ## Goal — one paragraph, what and why ## Assumptions — everything you assumed that isn't enforced by code ## Handshakes — interfaces/contracts with other modules: what you consume from them, what you promise to them ## Status / TODO — checklist; keep current as you work ## Work log — dated entries by the dev agent: what was done, key decisions, deviations from plan ## QA log — dated entries by the QA agent: what was verified, how, and findings ## Review — reserved for the lead; do not write here ``` Rules: - **Dev agents**: create/update the feature README *as you work*, not at the end. Finished TODOs get checked off in the same README. If you change a handshake another module depends on, say so explicitly in Handshakes and in the work log. - **QA agents**: you verify, you don't redesign. Run the checks, try to break the feature, record everything in the QA log. File concrete findings (file:line, reproduction) — not opinions. Small unambiguous fixes (typos, missing test assertion) you may fix directly and note it. - **Review section** is written only by the lead agent after examining the work. Respond to review comments by fixing and logging in the work log, not by editing the Review section. ## Workflow - **Agent models**: the lead plans and reviews; coding goes to one dev agent and verification to one QA agent, **both Claude Opus 5.5** (`model: "opus"`). This holds for a single lane and for each of two parallel lanes. - **Refactor lanes** (pure moves, decompositions): the dev is **Claude Fable 5.2 at high reasoning effort** — the `refactor-dev` agent in `.claude/agents/`. Its brief says: no new tests beyond what the move forces, the gates are the proof, spend the effort on module boundaries, naming and readability. QA is unchanged (Opus 5.5). - **Escalation**: a first QA failure gets a normal Opus 5.5 fix round. When a fix round fails QA a **second** time on the same feature, redo the round with **Claude Fable 5.2 as both dev and QA** (`model: "fable"`). - Do **not** run `git commit` or `git push` — the lead reviews and commits all work. - Do not touch files outside your feature's stated scope. If you need a change in another module, write it as a request in your README's Handshakes section and stop. - New behavior needs tests in the same change. Every formatter change must keep the snapshot + idempotency suite green (`cargo test -p vlog_formatter`). - Update `TODO.md`: check items your feature completes (leave the item text intact). ## Runaway guard (never run a suite or sweep bare) On 2026-09-03 a background test run consumed 100 GB and crashed the machine. macOS enforces no memory cap, so the cap is ours: `tools/guard.sh [-m GB] [-t SECONDS] -- ` kills a command's process tree when its resident set or wall clock exceeds the limits (defaults 8 GB / 30 min). - **Every `cargo test`, every fuzzer, every corpus or opentitan sweep, and every `lucisv`/`lucisv-server` run over a tree goes through `tools/guard.sh`** — dev, QA and lead alike, foreground or background. - A guard kill is a finding: report the command and the limit it hit; do not raise the limit to make it pass. - The release gate is `tools/gates.sh`, which guards each step with its own limits and retries a killed step once (shown as `KILLED → retried`); do not wrap it in another `guard.sh`. ## Parallel lanes (the 2-agent loop) Up to **two dev/QA lanes may run concurrently** when — and only when — their features do not collide in `docs/topo.md`: no shared edge between them (no `──▶`, no `◀──▶`, and neither touching the same open handshake in §2.9), and disjoint file sets. `docs/topo.md` is the authority; if the collision question is not answerable from it, the features are not proven independent and run serially. Rules for a parallel wave: - **The lead plans the lanes** before anything is dispatched: one brief per lane with an explicit file-ownership list. That list is the merge contract — a lane touches nothing outside it. A needed change to a file the other lane owns (or to any unlisted file) is a Handshake request in the lane's README plus a flag in its report, never a silent edit; the lead rules on it at merge. - **Each lane works in its own git worktree** branched from the same trunk commit. Never two agents in one tree. Trunk stays untouched while lanes are in flight; the lead merges each lane back only after that lane's QA verdict and lead review, and re-runs the full gates on the merged tree before pushing. - **Roles per lane are unchanged**: one dev, then one QA — **both Claude Opus 5.5** (owner, 2026-11-09; QA was Sonnet before) — in the same worktree, then the lead's review — so at most 2 devs + 2 QA agents exist at once, and a lane's QA starts only when its own dev is done. A QA rework loops inside its lane without blocking the other lane. - **Tracking lives in `TODO.md`**: the lead writes the wave's section (one subsection per lane, each item naming its files) on trunk before branching; each lane ticks only its own lines. Shared-by-nature files (`TODO.md`, `docs/perf/README.md`) may be appended to by both lanes in their own sections — the lead resolves the trivially-parallel merge; anything worse than trivially-parallel means the lanes were not actually independent, which is a planning failure to record. - After merging, the lead updates `docs/topo.md` for any edge a lane added or amended, so the next wave plans against reality.