docs/context/ — and how the context-save skill keeps them currentclaude-shared@3827c28 · skill skills/context-save/ · hooks hooks/context-save-*.shCode shows what the system does today. It does not show why — which requirement each part fulfils, which decision ruled out the obvious alternative, which rule must never break. That why is the asset; the code is one implementation of it.
Every enhancement is built on top of the ones before it. A change made without knowing what the existing code is there for can break a requirement an earlier enhancement delivered — and nothing complains:
| Before changing code, a session asks | Without saved context | With it |
|---|---|---|
| Why is this code here? | guessed from the code | requirements.md names the requirement it fulfils |
| Can I simplify this? | looks redundant, gets removed | architecture.md records the decision and what it ruled out |
| What must still hold after my change? | whatever the tests happen to cover | invariants.md — INV-nn rules, each named by a test |
| Was this approach already tried? | tried again, fails again | the decision record says so |
context-save is how those decisions get written down — at the moment they are made, in the repo they govern.Having the context is not the same as obeying it. A rule can be in the prompt and still lose: two sources disagree and the model follows the wrong one (authority conflict), or a long session drowns it (attention decay). More documents do not fix this. Two things do.
Tests › Requirements (requirements.md) › Decision records and invariants (docs/adr/, invariants.md) › Code comments › Other context notes (architecture.md, operations.md, …)
context-save keeps the notes current| Kind of rule | Guardrail |
|---|---|
| Most rules | Unit and integration tests — known inputs, known outputs. The default |
| Forbidden pattern, required wrapper | Static checks (Semgrep): 3–10 cheap rules per project, zero tokens |
| High-risk output | Golden / approval tests — catch "looks cleaner, behaves differently", the classic AI refactor failure |
| Parsers, transforms, schedulers, state machines, pricing, serialization | Property-based tests (Hypothesis) — selectively, never the default |
Put the why in the test. A docstring that names the constraint and the decision behind it turns a failure into an explanation: the failing output tells the next session exactly which earlier requirement it just broke.
criticality.yml at the project root maps sensitive paths to their invariants, decision records and must-run tests, each with an authority tier. In the strategy, a PreToolUse hook injects them before an edit and asks what could this change break?
Everyday decisions follow the routing below: architecture.md or requirements.md. A separate record in docs/adr/ is only for catastrophic-trap decisions — the ones a future change could silently undo with no way back. About 3–5 in a project's lifetime.
docs/context/ to four files became a per-project option; the skill ships the eight (see How the set changed over time). On this host today: invariants.md with INV-nn anchors is in every default set; 4 projects keep decision records in docs/adr/ (cicd, research, aiagentchat, agents/memory); 1 project has a criticality.yml (agents/memory), where agent-memory recall drops a memory whose subject a higher-tier rule covers. The edit-time injection hook is not deployed anywhere yet.context-save writes the durable part of each session into the project's own repo, at the moments a session is about to lose it.
/context-savedocs/context/*.mdCLAUDE.md menu points to itCLAUDE.md holds directives — what to do and not do — and loads every session. docs/context/ holds knowledge — how the system works — and is read when a task needs it. A fact kept in both drifts; keep it in one./clear /compact /exit /quitwip.md snapshot once pressure passes 30%CLAUDE.md/context-save · Codex $context-save · Antigravity /context-save/qatestsdocs/context/ from templates/ (9 templates on disk: the 8 defaults plus gotchas.md)/context-save-upgrade instead/refocus-complete, codex-handoff, an orchestrator recording a worker's changes — behaves like a hook save: it skips the menu and slim steps.The set is not arbitrary: these are the questions someone would have to answer to rebuild the system. A fact that answers none of them probably does not belong in docs/context/.
| # | File | Answers |
|---|---|---|
| 1 | requirements.md | What must the system do? Business rules, workflows, product constraints |
| 2 | architecture.md | How do the parts connect? Components, boundaries, data flow |
| 3 | interfaces.md | What are the contracts? APIs, schemas, events, config surfaces |
| 4 | conventions.md | How is code written here? Patterns, naming, libraries, layout |
| 5 | security.md | Who may do what? Auth, secrets, trust boundaries |
| 6 | operations.md | How does it run? Deploy, containers, ports, env, networks |
| 7 | testing.md | How is it proven? Critical paths, regression checks, coverage gaps |
| 8 | invariants.md | What must never break? Hard rules with INV-nn anchors that tests cite |
agent-memory write --memory-kind gotcha --subject "<topic>" "<lesson>" — when the menu sets gotchas_via_memory: true or the CLI is on PATHdocs/context/gotchas.mdtroubleshooting skillTree (version: 2) | Holds | True until |
|---|---|---|
docs/context/ | how the application behaves | the code changes |
docs/runbooks/ | procedures a person runs, one file per subsystem | the procedure changes |
docs/releases/ | CI-parsed; never touched by this skill | — |
> Purpose: … header. invariants.md differs in kind: the other seven describe, it forbids.| When | Change | Why |
|---|---|---|
| 2026-03 | First versioned skill: eight files, the eighth was gotchas.md. The 200-line ceiling and the 75% design goal were already there | Questions someone must answer to rebuild the system |
| 2026-04-19 | The context strategy proposes cutting to four files (architecture, invariants, interfaces, operations), folding gotchas into invariants | Fewer documents, stronger authority |
| 2026-04-24 | invariants.md replaces gotchas.md as the eighth; gotchas move to agent-memory; each project picks its files from a menu in CLAUDE.md | Gotchas accrete like a log; invariants are rules a test can cite. The four-file cut became a per-project choice, not a global one |
| 2026-07-16 | The eight become an inherited default; projects declare only additions (extends:), files: can still trim | Re-declaring the list in every project let defaults drift, and a mistyped name silently dropped a file |
| 2026-09-05 | Version 2: docs/runbooks/ as a second tree, and the "not a doc" rule | A file can only lag live state, and procedures serve a different reader than behaviour |
| 2026-09-17 | Mechanical wip.md snapshot; pressure tiers lowered to 30 / 50 / 70% | A session hit 98% of its window before Stop blocked, and saved nothing |
| 2026-11-09 | A manual save writes a missing menu and slims a CLAUDE.md over 40,000 characters | Knowledge was piling up in the one file that loads every session |
Source: git history of skills/context-save/ in claude-shared (from 2026-06-14) and claudecodeconfig (before). The set will keep changing; the skill is authoritative, this table is history.
| What | Limit | Why |
|---|---|---|
Each docs/context/ or runbook file | Under 200 lines. Full means prune, not append | An unbounded file becomes an append-only log, and nobody reads a log. The prune forces the real work: deciding what still matters. A short file is also cheap enough to read before every change — the check the Why section depends on |
Project CLAUDE.md | Slim when over 40,000 characters, to about 30,000 | CLAUDE.md loads in full on every turn of every session; docs/context/ loads only when a task needs it. Claude Code warns once all loaded instruction files pass 150,000 characters, and the user and platform layers already take about 45,000 on an administrator account before any project file. Every extra character costs tokens on every turn and dilutes attention to the rules that matter |
| Each entry | One fact, one file, one self-contained bullet | A fact kept in two places disagrees with itself within months; a bullet that needs its neighbours cannot be pruned safely |
| Dates on entries | None | Git already records when. A dated line is perishable state, which does not belong in the file at all |
First question, always: does this stay true until someone changes the code or the procedure?
docs/runbooks/<subsystem>.md on a version: 2 project; otherwise operations.md.troubleshooting skill. A user's correction or preference → feedback memory in ~/.claude/projects/…/memory/. Compose, Dockerfile, deploy.sh, CI, dependency or SQL change → also docs/architecture/ if the project has it (architecture-docs skill).docs/context/ fact, not a runbook line.CLAUDE.md frontmatter--- context-save: extends: [signal-lifecycle, options-exec] # APPENDED to the default 8 files: [architecture, operations] # optional: REPLACES the default gotchas_via_memory: true # gotchas → agent-memory version: 2 # also maintain docs/runbooks/ routing: # optional topic → file map trading-rules: tradingrequirements.md ---
files: is present, which replaces them with a subset.extends: — the recommended knob. List only what you add.docs/context/*.md is maintained too. Still list it in extends:.version: 2, add every docs/runbooks/*.md.No frontmatter? The default set plus discovered files applies, and the next manual /context-save writes exactly that as the menu. Hooks never write it.
| Event | Script | Does |
|---|---|---|
| Stop · SubagentStop · PreCompact · SessionEnd | context-save-check.sh | Measures pressure and the diff; nudges or blocks (tiers below) |
| UserPromptSubmit | context-save-on-prompt.sh | On /clear /compact /exit /quit: blocks when the project has docs/context/, more than 15 lines of git diff HEAD and no doc update — unless a wip.md under 15 minutes old covers it |
| PreToolUse Bash|Edit|Write|NotebookEdit | context-save-wip-pretooluse.sh | Refreshes wip.md mid-turn at medium pressure or above, checked after every 250,000 bytes of transcript growth. Silent; never blocks |
| SessionStart | context-save-wip-resume.sh | Shows a leftover wip.md and asks the agent to fold it into a real save, then delete it |
| Signal | Pressure | Behaviour |
|---|---|---|
docs/context/ or docs/runbooks/ already touched | any | Soft reminder |
| Any session | medium >30% | wip.md written mechanically — zero model tokens |
| >15 diff lines, no doc touched | high >50% | Blocks stop; asks for the full save |
| >15 diff lines, no doc touched | critical >70% | Blocks stop; asks for one plain sentence — no skill load, no tool calls |
| >15 diff lines, no doc touched | low / medium | Strong reminder naming the 8 files |
| Minor or no code change | any | Soft reminder / checklist |
hooks/lib/context-pressure.shwip.mdA session reached 98% of a 1M window before Stop ever blocked: one autonomous turn chained dozens of tool calls, and Stop samples only between turns. The save it then asked for was too large for the room left, the next call failed with Prompt is too long, and nothing was saved.
So the hooks now write the snapshot themselves — branch, git status, recent commits, the last prompt — beside the transcript (~/.claude/projects/<slug>/wip.md), never in the repo. It is overwritten on each fire and never deleted automatically. Regression suite: hooks/test-context-save-wip.sh, 30 cases.
architecture.md / requirements.md · lessons → gotchas or troubleshooting · user corrections → feedback memory · code that moved architecture, interfaces, security or operations → the matching file · build and deploy changes → docs/architecture/./context-save, in ordergit fetch; is docs/ clean and current? Clean → write in the working tree. Behind upstream or already dirty → write anyway, then commit and push on context/<user>-<YYYY-MM-DD>.context-save: frontmatter → write extends: (only if CLAUDE.md is uncommitted-clean). A menu without version: 2 → a one-line v1 notice; v2 is never run for you.claudemd_slim.py measure; over 40,000 characters → slim first (next section)./qatests for just those gaps.gotchas.md; cross-project → troubleshooting.context/ branch.stash, checkout -- or reset another session's uncommitted docs/ edits — they go onto the context/ branch with yours.CLAUDE.md (manual only)CLAUDE.md loads every session; docs/context/ does not. A large CLAUDE.md is usually knowledge captured in the wrong place. Over 40,000 characters (wc -m), a manual save moves the descriptive part out, verbatim, to about 30,000.
> Purpose: … Moved verbatim from CLAUDE.md., added to extends:docs/context/Y" table, one-line forms of safety rules, and a moved-from → now-in table in docs/README.mdcontext/* branch already carrying a slim. Otherwise it skips and says why.# read-only helper SLIM=~/.claude/skills/claude-shared/skills/context-save/lib/claudemd_slim.py $SLIM measure # exit 1 = over 40,000 $SLIM sections --by-size # heading blocks, line ranges (fence-aware) $SLIM verify # lossless check vs HEAD $SLIM verify --base HEAD~1 # on the committed move
verify must exit 0: every removed line reappears once, in order. It fails on rewording, summarising, a dropped line, a reorder, or a receiver over 200 lines. Fix the move, never the check.verify, because it rewords on purpose.Helper tests: lib/test_claudemd_slim.py, 57 passing.
[PLANNED]INV-nn)CLAUDE.md directives- [IMPLEMENTED] PostgreSQL primary store - [PLANNED] Redis session cache - [DEPRECATED] MongoDB connector
| Command / path | What |
|---|---|
/context-save | Full save, plus menu and slim steps |
/context-save init | Scaffold docs/context/ for a new project |
/context-save-upgrade | Edit the menu, or migrate gotchas.md into agent-memory (one-time, idempotent) |
/context-save-upgrade v2 | Human-reviewed durable/perishable pass that adds docs/runbooks/. Needs a clean, current tree; never automatic |
claude-shared/skills/context-save/SKILL.md | The skill — authoritative over this page |
claude-shared/skills/context-save/templates/ | Scaffolding templates |
claude-shared/skills/context-save/lib/claudemd_slim.py | Slim helper: measure · sections · verify |
claude-shared/hooks/context-save-*.sh | The four hook scripts; registered in hooks/hooks.json |
claude-shared/hooks/lib/context-pressure.sh | Pressure tiers and the wip.md writer |
claude-shared/hooks/test-context-save-wip.sh | Hook regression suite |
gitpull or a deploy that hard-resets the tree destroys uncommitted context.wip.md is not a context file. It is a perishable snapshot outside the repo; fold it into a real save, then delete it.version: 2 is opt-in, human-reviewed, and never run by a hook.verify.SKILL.md wins — and this page is out of date.