# Core 3 labs: knowledge systems for Claude Code

Companion labs for **Core 3: Knowledge Systems for Claude Code**. Three scripts that keep repository knowledge small, checkable and current: a codebase map you can paste into CLAUDE.md and test for drift, a skill scaffolder and validator built on the documented frontmatter, and a spec-to-tasks tool that counts an item as done only when its evidence holds.

Everything runs offline with Python 3.9+ and the standard library, from `labs/`. No lab calls a model. Each one writes only to `core-3/out/` (or a temp dir), and none of them writes into the sample repo or your repo unless you pass `--dest`.

| Lab | What it does | Session topic | Docs it encodes |
|---|---|---|---|
| `codebase_map.py` | Builds a compact map block for CLAUDE.md (commands, entry points, one line per module with its docstring and public names), fits it to a line budget by dropping detail, wraps it in HTML-comment markers, and `--check` reports drift against a committed block. | Repository knowledge: codebase maps, CLAUDE.md design | [memory](https://code.claude.com/docs/en/memory): under 200 lines, block HTML comments stripped, `/doctor` trims derivable content |
| `skill_scaffold.py` | `new` scaffolds `.claude/skills/<name>/` (SKILL.md, reference.md, optional bundled script pre-approved in `allowed-tools`); `validate` checks SKILL.md against the frontmatter table and 20 rules (S001-S020); `budget` sizes the skill listing against 1% of the context window. | Library: personal and project skills | [skills](https://code.claude.com/docs/en/skills), [settings-reference](https://code.claude.com/docs/en/settings-reference) (`skillListingBudgetFraction`, `skillListingMaxDescChars`), [Agent Skills spec](https://agentskills.io/specification) |
| `spec_tasks.py` | Parses a spec's stages and checkboxes, verifies each item's evidence (`test:`, `file:`, `log:`) against a unittest log and the repo, writes `out/TASKS.md` and `out/tasks.json`, and names the next actionable stage. `--run-tests` regenerates the evidence in a temp copy. | Specifications and tasks: task lists, stage splitting, completion evidence | [effective harnesses for long-running agents](https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents) (feature list with `passes`), [best practices](https://code.claude.com/docs/en/best-practices) (give Claude a check it can run) |

Shared helpers: `common/frontmatter.py` splits and parses frontmatter and coerces booleans and tool lists (PyYAML isn't in the standard library), and `core-1/_perms.py` checks `allowed-tools` rules and injected commands with the same engine `core-1/mode_simulator.py` uses.

## Commands

```bash
# Codebase map
python3 core-3/codebase_map.py                         # map common/samples/tiny-app, then a drift demo
python3 core-3/codebase_map.py --max-lines 8           # watch detail levels drop to fit the budget
python3 core-3/codebase_map.py --repo ~/code/myrepo --write
python3 core-3/codebase_map.py --check path/to/CLAUDE.md   # exit 1 when the block is stale or missing

# Skills
python3 core-3/skill_scaffold.py                       # demo: scaffold, validate the demo and the samples, budget
python3 core-3/skill_scaffold.py new fix-issue --description "Fix a GitHub issue by number. Use when asked to fix #123." --manual --args issue
python3 core-3/skill_scaffold.py new repo-tour --description "..." --fork --agent Explore --with-script --dest /tmp/skills
python3 core-3/skill_scaffold.py validate core-3/samples/skills ~/.claude/skills --strict
python3 core-3/skill_scaffold.py budget core-3/samples/skills --context-window 200000

# Spec to tasks
python3 core-3/spec_tasks.py                           # recorded evidence log (a branch with a rounding regression)
python3 core-3/spec_tasks.py --run-tests               # fresh evidence from the current code
python3 core-3/spec_tasks.py --spec my/SPEC.md --repo ~/code/myrepo --strict
```

To install a scaffolded skill: `mkdir -p <repo>/.claude/skills && cp -r core-3/out/skills/<name> <repo>/.claude/skills/` (project scope), or copy it into `~/.claude/skills/` (personal scope).

## Output excerpts

### `codebase_map.py`

The default run maps tiny-app at detail level 3 in 11 lines (excerpt):

```text
<!-- 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
```

With `--max-lines 9` it drops to module names:

```text
## 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/`: `cart.py`, `cli.py`, `pricing.py`
<!-- codebase-map:end -->

  detail level: 1 (module names); body 6 line(s), budget 9
```

The drift demo compares the fresh map with `samples/CLAUDE.stale.md`, whose block predates folding `discounts.py` into `pricing.py`:

```text
[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 @@
...
-  - `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`
```

### `skill_scaffold.py`

The scaffolded demo skill validates cleanly, and its injected command runs because `allowed-tools` approves exactly that command:

```text
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
```

The deliberately flawed sample (excerpt):

```text
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  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
```

A mostly good skill whose injected command isn't pre-approved:

```text
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
  INJECT !`python3 -m tinyapp.cli --region $region --code $code apple:120:3` -> aborts outside auto mode (permission check: prompt)
```

The listing budget for the demo skill plus the samples:

```text
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)
```

### `spec_tasks.py`

Against the recorded log (`samples/evidence/unittest-v.log`, a real `python3 -m unittest -v` run on a copy of tiny-app with banker's rounding swapped into `tax_for`, temp path replaced):

```text
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
```

With `--run-tests` the same spec reads differently, because the current code rounds half up:

```text
  evidence log: core-3/out/unittest-fresh.log (fresh run): 13 test result(s), 0 not ok
...
[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
```

## Samples

| File | What it is |
|---|---|
| `samples/CLAUDE.stale.md` | A tiny-app CLAUDE.md whose map block is out of date, for the drift demo |
| `samples/skills/Deploy_Prod/` | A flawed skill: typo'd and invalid fields, broad grants, side effects without `disable-model-invocation`, broken links |
| `samples/skills/explain-pricing/` | A reasonable skill with one problem: an injected command `allowed-tools` doesn't approve |
| `samples/skills/everything-helper/` | A 1,959-character description (2,057 with when_to_use) that the listing cuts at 1,536 |
| `samples/SPEC.md` | A three-stage spec with evidence annotations |
| `samples/evidence/unittest-v.log` | Recorded unittest output from a branch with a rounding regression |
| `samples/evidence/cli-session.txt` | A recorded CLI receipt (real output of the command it shows) |

## Notes on what the docs leave open

- **Listing budget units.** The skills page says the listing has a "character budget" that "scales at 1% of the model's context window", and `skillListingBudgetFraction` "reserves 1% of the context window", which is measured in tokens. `budget` reports both readings rather than picking one.
- **Arguments inside `!` commands.** The docs say `!` substitution "runs once over the original file" but don't say whether argument placeholders are expanded before injected commands run. The validator warns when a `!` command contains one, because if they are, typed text reaches a shell.
- **How description and when_to_use are joined** in the listing isn't published; the validator adds their lengths, and `budget` adds 4 characters per entry for separators, labelled as an estimate.
