A codebase map for CLAUDE.md

Map a repository into a budgeted block you can paste into CLAUDE.md (commands, entry points, one line per module), fit it to a line budget by dropping detail before modules, and fail CI when a committed map goes stale. It puts the Repository Knowledge topic's codebase maps into practice.

  • Python 3.9+
  • standard library only
  • offline: common/samples/tiny-app and core-3/samples/CLAUDE.stale.md
python3 core-3/codebase_map.py
┌────────────────────────────────────────┐
│ Codebase map · common/samples/tiny-app │
└────────────────────────────────────────┘
[1] What was scanned
  files / python modules: 9 / 7 (skipped: hidden dirs, build output, caches)
  commands detected: test: python3 -m unittest, run: python3 -m tinyapp.cli
  entry points: python3 -m tinyapp.cli
[2] The block to paste into CLAUDE.md
<!-- codebase-map:start (generated by core-3/codebase_map.py; regenerate, don't hand-edit) -->
## Codebase map

- Test: `python3 -m unittest`
- Run: `python3 -m tinyapp.cli`

- Root: `CLAUDE.md`, `README.md`
- `tests/` 2 test file(s): `test_cart.py`, `test_pricing.py`
- `tinyapp/` — tinyapp: a deliberately small shopping-cart library used as the sample repo for the labs.
  - `cart.py` — The cart model: line items, subtotal, discount, tax and total. · `LineItem`, `Cart`
  - `cli.py` (entry: `python3 -m tinyapp.cli`) — Command-line receipt printer. · `parse_item`, `receipt`, `main`
  - `pricing.py` — Money, tax and discount rules. All amounts are integer cents to avoid float drift. · `TAX_BPS`, `DISCOUNTS`, `money`, `tax_for`, `apply_discount`
<!-- codebase-map:end -->

  detail level: 3 (names); body 11 line(s), budget 30
  size: ≈263 tokens (chars/2.5 estimate); the markers cost 0 (HTML comments are stripped)
  share of CLAUDE.md target: 11/200 lines
  Left out on purpose: the full file tree, dependency lists and architecture prose. Claude can derive
  those, and /doctor proposes trimming them. Keep conventions and pitfalls in CLAUDE.md by hand.
[3] Drift demo: a committed map that no longer matches the code
  core-3/samples/CLAUDE.stale.md: stale
--- CLAUDE.stale.md (committed map)
+++ fresh map
@@ -8,5 +8,4 @@
 - `tinyapp/` — tinyapp: a deliberately small shopping-cart library used as the sample repo for the labs.
   - `cart.py` — The cart model: line items, subtotal, discount, tax and total. · `LineItem`, `Cart`
   - `cli.py` (entry: `python3 -m tinyapp.cli`) — Command-line receipt printer. · `parse_item`, `receipt`, `main`
-  - `discounts.py` — Discount codes. · `DISCOUNTS`, `apply_discount`
-  - `pricing.py` — Money and tax rules. · `TAX_BPS`, `money`, `tax_for`
+  - `pricing.py` — Money, tax and discount rules. All amounts are integer cents to avoid float drift. · `TAX_BPS`, `DISCOUNTS`, `money`, `tax_for`, `apply_discount`
  In CI: python3 core-3/codebase_map.py --check CLAUDE.md  (exits 1 when stale)
  1. The block sits between HTML-comment markers. Block comments are stripped before CLAUDE.md is injected, so the markers cost no tokens and --check can find the block again.
  2. Two commands, the entry point and one line per module with its docstring and public names. No file tree: Claude can list files itself.
  3. Detail level 3 fits the 30-line budget in 11 lines. The size is the lab's estimate at 2.5 characters per token, not a token count.
  4. The drift demo: a committed map written before discounts.py was folded into pricing.py.
  5. The diff shows both halves of the drift: a module that no longer exists, and DISCOUNTS and apply_discount missing from the module that now has them.
  6. The CI form: exit 1 when the block is stale, so a wrong map fails the build instead of misleading Claude.
  • --max-lines 8: the map falls to detail level 1, module names only (- `tinyapp/`: `cart.py`, `cli.py`, `pricing.py`), in 6 lines.
  • --max-lines 5: level 0 and truncated, with … 2 more line(s) omitted to fit --max-lines 5. A map this small has stopped paying for itself.
  • --write also saves the block to core-3/out/codebase-map.md; then --check core-3/out/codebase-map.md prints fresh and exits 0.
  • Break it: --check core-3/samples/CLAUDE.stale.md prints stale and the diff, and exits 1.
  • --repo ~/code/myrepo maps your own repository, read-only.
  • The map is an index (commands, entry points, where each concern lives), not documentation. Its per-module lines sit close to the "file-by-file descriptions" the docs say to leave out, so keep it short and add the facts no generator can derive by hand.
  • Generated content in CLAUDE.md needs a drift check, or it ages into wrong instructions.

Create and validate a project skill

Three tools in one script: new scaffolds a skill folder, validate checks SKILL.md files against the documented frontmatter and 20 rules (S001–S020), and budget sizes the skill listing against 1% of the context window. It's the quality gate for the Library topic's personal and project skills.

  • Python 3.9+
  • standard library only
  • offline: the sample skills in core-3/samples/skills/
python3 core-3/skill_scaffold.py

The demo scaffolds discount-audit into core-3/out/skills/, validates it and the three samples, and prints the listing budget for all four.

┌───────────────────────┐
│ Skill scaffold · demo │
└───────────────────────┘
[1] Scaffold a project skill into core-3/out/skills/
  core-3/out/skills/discount-audit/SKILL.md
  core-3/out/skills/discount-audit/reference.md
  core-3/out/skills/discount-audit/scripts/helper.py

---
name: discount-audit
description: "Audit discount-code handling in tinyapp: list the codes, check case handling and propose missing tests. Use when asked about discount codes, coupons or apply_discount."
argument-hint: "[code]"
arguments: [code]
allowed-tools: Read Bash(python3 ${CLAUDE_SKILL_DIR}/scripts/helper.py *)
---

# discount-audit

Inputs: `$code`.

## Context

Python files and their sizes: !`python3 ${CLAUDE_SKILL_DIR}/scripts/helper.py "**/*.py"`

## Steps

1. State what done looks like before you start.
2. Do the work; keep changes inside the files the task names.
3. Show evidence (a command and its output), not a claim.

For conventions and an example of the expected output, see [reference.md](reference.md).

[2] Validate it, then the lab's sample skills
core-3/out/skills/discount-audit/SKILL.md  /discount-audit  errors=0 warnings=0
  INFO  S011 allowed-tools pre-approves; it doesn't restrict. Workspace trust doesn't gate it, so review it in repositories you didn't write
  INFO  S020 Claude Code-only fields (a claude.ai upload or the Skills API rejects them): argument-hint, arguments
  INJECT !`python3 ${CLAUDE_SKILL_DIR}/scripts/helper.py "**/*.py"` -> runs

core-3/samples/skills/Deploy_Prod/SKILL.md  /Deploy_Prod  errors=5 warnings=8
  ERROR S006 user-invocable: 'maybe' isn't a boolean (true/false, yes/no, on/off, 1/0)
  ERROR S008 context: 'forked' isn't recognized; the only value is 'fork'
  ERROR S009 effort: 'extreme'; options are low, medium, high, xhigh, max (availability depends on the model)
  ERROR S014 link to 'checklist.md', which doesn't exist next to SKILL.md
  ERROR S014 ${CLAUDE_SKILL_DIR}/scripts/tag.sh doesn't exist in the skill directory
  WARN  S003 unknown field 'allowed_tools' is ignored silently; did you mean 'allowed-tools'?
  WARN  S004 name 'Deploy_Prod' isn't portable: the Agent Skills spec allows 1-64 lowercase letters, digits and single hyphens (claude.ai uploads and the Skills API enforce it)
  WARN  S005 description 'Deploy' is too short to say what the skill does and when
  WARN  S008 background only applies with context: fork
  WARN  S011 allowed-tools: Bash pre-approves arbitrary commands for the invoking turn; name the exact commands instead
  WARN  S011 allowed-tools: Write(src/**): Write(...) path rules are accepted but never consulted; use Edit(...) instead
  WARN  S012 mentions deploy, push, release (keyword heuristic): if it has side effects, add disable-model-invocation: true so only you start it
  WARN  S016 metadata must be a map; Claude Code drops a value that isn't
  INFO  S011 allowed-tools pre-approves; it doesn't restrict. Workspace trust doesn't gate it, so review it in repositories you didn't write
  INFO  S020 Claude Code-only fields (a claude.ai upload or the Skills API rejects them): background, context, effort, user-invocable
  INJECT !`git branch --show-current` -> runs
  INJECT !`git log origin/main..HEAD --oneline` -> runs

core-3/samples/skills/everything-helper/SKILL.md  /everything-helper  errors=0 warnings=2
  WARN  S005 description + when_to_use is 2057 chars; the listing cuts it at 1536, so Claude never sees the last 521
  WARN  S012 mentions commit (keyword heuristic): if it has side effects, add disable-model-invocation: true so only you start it
  INFO  S005 description is 1959 chars; the spec allows at most 1024
  INFO  S020 Claude Code-only fields (a claude.ai upload or the Skills API rejects them): when_to_use

core-3/samples/skills/explain-pricing/SKILL.md  /explain-pricing  errors=0 warnings=2
  WARN  S015 !`python3 -m tinyapp.cli --region $region --code $code apple:120:3` isn't pre-approved, so outside auto mode the invocation aborts; add a narrow Bash(...) rule to allowed-tools
  WARN  S015 !`python3 -m tinyapp.cli --region $region --code $code apple:120:3` contains an argument placeholder; the docs don't say whether arguments are substituted before injected commands run, and if they are, typed text reaches a shell
  INFO  S011 allowed-tools: Grep does nothing on macOS, Linux and WSL, where Glob and Grep aren't in the default tool set (Claude searches with find and grep through Bash, which are read-only commands); allowed-tools doesn't bring them back. It matters only on Windows, in sessions started with them in --tools or --allowedTools, when a deny rule or --disallowedTools removes Bash, or in a subagent whose tools list names them without Bash
  INFO  S011 allowed-tools pre-approves; it doesn't restrict. Workspace trust doesn't gate it, so review it in repositories you didn't write
  INFO  S020 Claude Code-only fields (a claude.ai upload or the Skills API rejects them): argument-hint, arguments, when_to_use
  INJECT !`python3 -m tinyapp.cli --region $region --code $code apple:120:3` -> aborts outside auto mode (permission check: prompt)

[3] Skill listing budget for all of them
skill               desc chars shown/full  entry chars  ≈tokens
──────────────────  ─────────────────────  ───────────  ───────
/discount-audit     167/167                185          ≈74    
/Deploy_Prod        6/6                    21           ≈9     
/everything-helper  1536/2057              1557         ≈623   
/explain-pricing    251/251                270          ≈108   
  listing: 2033 chars ≈ 814 tokens (chars/2.5 estimate)
  budget: 0.01 x 1,000,000-token context window = 10,000
  share of budget: 8.1% if the budget is 10,000 tokens; 20.3% if it is 10,000 characters (the docs don't pin the unit)

Install (project scope):  mkdir -p <repo>/.claude/skills && cp -r core-3/out/skills/discount-audit <repo>/.claude/skills/
Install (personal scope): mkdir -p ~/.claude/skills && cp -r core-3/out/skills/discount-audit ~/.claude/skills/
  1. The injected command runs at invocation because allowed-tools pre-approves exactly that command, with ${CLAUDE_SKILL_DIR} substituted on both sides.
  2. Two typos: context: forked isn't a recognized value (the only one is fork), and allowed_tools with an underscore is an unknown field, ignored silently.
  3. A deploy skill Claude could start on its own. Workflows with side effects get disable-model-invocation: true, so only you run them.
  4. The listing truncates description plus when_to_use at 1,536 characters, so the last 521 never reach Claude. Put the use case first.
  5. An injected command that allowed-tools doesn't approve. Outside auto mode the invocation aborts with Shell command permission check failed.
  6. The docs call it a character budget that scales at 1% of the context window, so the lab prints both readings: 8.1% as tokens, 20.3% as characters.
  • new fix-issue --description "Fix a GitHub issue by number. Use when asked to fix #123." --manual --args issue: a manual-only skill with disable-model-invocation: true and a named issue argument, validated as soon as it's written. Run it twice and the second run refuses (exists; pass --force to replace it).
  • budget core-3/samples/skills --context-window 200000: on a 200K window the same three samples use 37.0% of the budget as tokens, or 92.4% as characters.
  • validate core-3/samples/skills/explain-pricing --strict exits 0: --strict fails on errors, and this skill has only warnings.
  • Break it: validate core-3/samples/skills --strict exits 1 on Deploy_Prod's five errors.
  • validate core-3/samples/skills ~/.claude/skills adds your personal skills to the check, read-only.
  • allowed-tools pre-approves tools for the invoking turn; it doesn't restrict which tools exist. Name exact commands, not Bash.
  • Frontmatter typos don't announce themselves: the lab reports allowed_tools as an unknown field that is ignored silently, so a validator is where you find them.
  • The listing budget is shared by every skill, so a long description costs the others their keywords.

Spec checkboxes to tasks with evidence

Turn a staged spec's checkboxes into TASKS.md and tasks.json, where an item counts as done only when its evidence holds: the named test passed in the log, a file contains the expected text, or a recorded output contains it. Then name the next actionable stage, so a fresh session knows where to resume. It's the Tasks topic's completion evidence, applied to a spec file.

  • Python 3.9+
  • standard library only
  • offline: core-3/samples/SPEC.md and a recorded test log
python3 core-3/spec_tasks.py

The recorded log is a real python3 -m unittest -v run on a copy of tiny-app with banker's rounding swapped into tax_for, so one checked item is wrong on purpose.

┌─────────────────────────────────────────────────────┐
│ Spec → tasks · Spec: tiny-app 0.3 pricing hardening │
└─────────────────────────────────────────────────────┘
[1] Inputs
  spec: core-3/samples/SPEC.md
  repo: common/samples/tiny-app
  evidence log: core-3/samples/evidence/unittest-v.log: 13 test result(s), 1 not ok
[2] Every item, judged by its evidence
id    box  status                item                                          evidence                                                                
────  ───  ────────────────────  ────────────────────────────────────────────  ────────────────────────────────────────────────────────────────────────
S1.1  [x]  VERIFIED              `money()` formats negative amounts with a l…  MoneyTest::test_negative ok in unittest-v.log                           
S1.2  [x]  EVIDENCE FAILS        Tax rounds half up (1000 cents in CA is 73,…  TaxTest::test_known_region_rounds_half_up FAIL in unittest-v.log        
S1.3  [x]  VERIFIED              Region codes are case-insensitive             TaxTest::test_region_is_case_insensitive ok in unittest-v.log           
S2.1  [x]  VERIFIED              Unknown codes give no discount                DiscountTest::test_unknown_code ok in unittest-v.log                    
S2.2  [x]  EVIDENCE FAILS        Discount codes are case-insensitive (`welco…  DiscountTest::test_lowercase_code isn't in unittest-v.log, and isn't de…
S2.3  [x]  CLAIMED, NO EVIDENCE  Tax applies after the discount                -                                                                       
S2.4  [ ]  READY TO CHECK        `apply_discount` stays exported from the pu…  tinyapp/__init__.py contains 'apply_discount'                           
S3.1  [ ]  READY TO CHECK        The receipt shows a discount line when a co…  evidence/cli-session.txt contains 'discount'; CliTest::test_receipt_tot…
S3.2  [ ]  OPEN                  `--list-codes` prints the known discount co…  -                                                                       
[3] Stages
  Stage 1: Money and tax: 2/3 verified
  Stage 2: Discounts: 1/4 verified
  Stage 3: Receipt: 0/2 verified
  next actionable stage: Stage 1: Money and tax
    S1.2 Tax rounds half up (1000 cents in CA is 73, not 72) (evidence fails)
  checked but not verified: S1.2, S2.2, S2.3
[4] Wrote core-3/out/TASKS.md and core-3/out/tasks.json (only verified items are ticked)
  1. Checked in the spec, but its rounding test FAILED in the log. A checkbox is a claim; the log is the evidence.
  2. Checked, and names a test that was never written.
  3. Checked, with no evidence named at all.
  4. Unchecked, but the evidence already holds. Evidence can lead the checkbox.
  5. Stage 1 isn't done, so it's still the next stage: what a fresh session should pick up first.
  6. TASKS.md ticks only verified items, whatever the spec says; tasks.json has the same data for scripts.

--run-tests regenerates the evidence by running the tests in a temp copy of the current code, which rounds half up:

┌─────────────────────────────────────────────────────┐
│ Spec → tasks · Spec: tiny-app 0.3 pricing hardening │
└─────────────────────────────────────────────────────┘
…
  evidence log: core-3/out/unittest-fresh.log (fresh run): 13 test result(s), 0 not ok
…
S1.1  [x]  VERIFIED              `money()` formats negative amounts with a l…  MoneyTest::test_negative ok in the fresh run                            
S1.2  [x]  VERIFIED              Tax rounds half up (1000 cents in CA is 73,…  TaxTest::test_known_region_rounds_half_up ok in the fresh run           
…
[3] Stages
  Stage 1: Money and tax: 3/3 verified ✓
  Stage 2: Discounts: 1/4 verified
  Stage 3: Receipt: 0/2 verified
  next actionable stage: Stage 2: Discounts
    S2.2 Discount codes are case-insensitive (`welcome10` works) (evidence fails)
    S2.3 Tax applies after the discount (claimed, no evidence)
    S2.4 `apply_discount` stays exported from the public API (ready to check)
  checked but not verified: S2.2, S2.3
…
  1. Fresh evidence: 13 results, none failing.
  2. The same checked item is now verified, because the code under test changed, not the spec.
  3. Stage 1 closes and the next actionable stage moves to Stage 2.
  • Break it: --strict exits 1, because S1.2, S2.2 and S2.3 are checked but not verified. Point a CI step at it.
  • --run-tests --strict still exits 1: S2.2 and S2.3 stay checked without passing evidence.
  • After any run, open core-3/out/TASKS.md: S2.2 is written as [ ] with the reason, although the spec has it ticked.
  • --spec my/SPEC.md --repo ~/code/myrepo --strict runs it on your own spec, read-only.
  • "Done" means the evidence holds now, not that someone ticked the box: the same spec reads differently against a different log.
  • As a gate, --strict belongs in CI. A Stop hook running it is only a nudge: with its stop_hook_active guard it blocks once, and a crashing one fails open.