Why
Phase A (plans/model-intelligence-layer-phase-a.md, done) made the registry's execution tier the main session's model on every machine, which is Sonnet on Claude Code. Nothing enforces it. A session started by another session inherits that session's model, and the owner can pick any model from the menu; on 2026-11-22 the Phase A verify session ran on Opus for the first reason, and its model probe failed. The owner wants a session that starts on the wrong model to stop at once, say so, start a replacement on the right model with the same prompt, and archive itself.
Decisions (owner, 2026-11-22, in chat)
- D1. Mismatch means respawn. Every main session runs the registry's main tier. Work that needs the strong tier reaches it by delegating to a strong-tier agent, as MODELS.md already says; there is no sanctioned Opus main session.
- D2. A per-session override exists. The phrase
keep this model(case-insensitive) in the owner's first prompt records an override for that session id only. The same phrase in any later prompt also lifts a pending respawn, which is the owner's way out if a respawn stalls. - D3. Outside the desktop app the session blocks and shows the command.
- D4. A session started on Opus on purpose is respawned too. There is no exemption for child sessions; spawners start children on the registry's main id.
- D5. A child that is still wrong never respawns again. A mismatched session with
CLAUDE_CODE_CHILD_SESSION=1whose prompt came from a respawn (the instruction marks it with the linerespawned-by-model-guard) takes the exit-2 path instead, so a desktop app that ignored themodelargument produces one blocked session, never a loop.
Facts this design rests on
From <https://code.claude.com/docs/en/hooks>, read 2026-11-22:
- Only SessionStart receives
model, and it is optional (absent after/clearor on recovery). Its value is a full id such asclaude-opus-5. SessionStart also carriessource:startup,resume,clearorcompact. - Every event receives
session_id. - UserPromptSubmit exit 2 blocks the prompt and shows stderr to the user, not the model.
additionalContextreaches the model and is not shown as a chat message. - Hooks on one event run in parallel, so order in the settings file means nothing.
From this machine and the desktop app's own tool schemas, 2026-11-22 (not from the hooks page; the live acceptance below is what proves them):
- The desktop app sets
CLAUDE_CODE_ENTRYPOINT=claude-desktop; a session started by another session also hasCLAUDE_CODE_CHILD_SESSION=1. mcp__ccd_session__start_sessiontakesinitiation,context,cwd,model(a full id such asclaude-sonnet-5-5, same family as the parent or cheaper) andprompt.mcp__ccd_session_mgmt__archive_sessiontakessession_id: "self"and asks the owner to approve unless the session runs in auto or bypass mode.- Existing hooks deny a tool call by exiting 2 with the reason on stderr (
spawn-envelope.sh).
Mechanism
One script, claude-config:shared/tools/model-guard.sh <event>, with state in ${MODEL_GUARD_STATE_DIR:-$HOME/.claude/state/model-guard}/<session_id>.json. It reads session_id, source, model and prompt from stdin with sed patterns, never with bun or python, because it runs on every tool call. A fields-it-cannot-read payload is treated as "nothing to do".
Registry. shared/model-roles.json gains a full id beside each Claude Code alias, for example "execution": { "model": "sonnet", "id": "claude-sonnet-5-5", "verified": true }. The alias stays the pin the resolver checks; the id is what start_session needs. model-roles.ts main claude prints <alias> <id> for the main tier, tested first in model-roles.test.ts.
Matching. Strip any [...] suffix, split on -, and match when the alias is one of the tokens. claude-sonnet-5-5, sonnet and sonnet[1m] match sonnet; claude-opus-5-5 does not.
start, SessionStart.
source: startup: writes a fresh state file with the payload'smodel(orunknown) and the main tier's alias and id, read once withbun "$HOME/.claude/tools/model-roles.ts" main claude(the installed copy, whichlink-machine.shlinks to the repo). When bun is missing, it recordsunknownand prints one warning line, so the session runs unguarded and says so.resume,clearorcompact: leaves an existing state file alone, so thepromptedflag and any override survive and a long task is never respawned mid-way. With no state file, it does whatstartupdoes but setspromptedat once, so a resumed session is never respawned.- Every
startdeletes state files older than seven days.
prompt, UserPromptSubmit.
- No state file, an
unknownmodel, or a match: nothing. - A later prompt containing
keep this modelwhilerespawn-pending: clears pending, records the override, nothing else. - Otherwise, only on the first prompt (no
promptedflag; it is then set): - mismatch with
keep this model: records the override, nothing else; - mismatch with
CLAUDE_CODE_ENTRYPOINT=claude-desktop, unless it is a respawned child (D5): setsrespawn-pendingand prints the stdout JSON{"hookSpecificOutput": {"hookEventName": "UserPromptSubmit", "additionalContext": "..."}}, whose context tells the session to do three things and nothing else. First, callstart_sessionwithinitiation: "user_asked",context: "fresh",cwdset to the current directory,modelset to the registry id, andpromptset to the linerespawned-by-model-guardfollowed by the owner's prompt verbatim (the hook embeds it JSON-escaped). Second, tell the owner in one line that it started on the wrong model, which session took over, and that attachments or images in the prompt did not carry over and need re-adding there. Third, callarchive_sessionwithsession_id: "self"; - mismatch with any other entrypoint (the CLI, Nimbalyst, Codex, DSH): exit 2. Stderr says the session is on the wrong model, gives the restart command without the prompt (
claude --model sonnetfor Claude Code), and asks the owner to paste the prompt again in the new session. The prompt is never embedded in a command, so no shell quoting is involved.
tool, PreToolUse, no matcher. When the state file saysrespawn-pending, it exits 2 with the reason for every tool exceptmcp__ccd_session__start_session,mcp__ccd_session_mgmt__archive_sessionandToolSearch. Other PreToolUse hooks still run in parallel, which is harmless: a call one hook denies does not run. When the state file is absent or not pending, it exits after onesedand one file test.
If the session ignores the instruction, every tool stays denied, so it can do no work. The owner types keep this model to carry on, or archives it by hand. If start_session refuses, the session says so in one line and the owner restarts by hand. The manual way to clear a stuck session is to delete its state file; MODELS.md names the path.
Handoff. shared/commands/handoff.md gains one line: a successor session is started with model set to the registry's main id, so a handoff from an Opus session never produces an Opus successor that then respawns.
Out of scope: a model switch from the menu partway through a session (a later plan can use PreModelSwitch); Codex and DSH payloads, unchecked for a model field (verifier rows report UNSUPPORTED); subagents, which are pinned in their agent files; and the read-only list for built-in agents in spawn-envelope.sh, found during this review and left for its own small task.
Steps
All in claude-config, test first. A bun test drives the bash script through Git Bash, as spawn-envelope.test.ts does. Every test sets MODEL_GUARD_STATE_DIR to a temporary directory.
- Tests first in
model-roles.test.ts(themain claudecommand) andmodel-guard.test.ts: matching forclaude-sonnet-5-5,sonnet[1m]andclaude-opus-5-5;startupversusresume,clearandcompact, with and without a state file; the desktop respawn context carrying the prompt verbatim with quotes,$, backticks and newlines, and the registry id; the exit-2 path with the restart command and no prompt in it;keep this modelon the first prompt and on a later pending prompt; a second prompt never triggering; no state file andunknownnever triggering;tooldenying Edit, Bash and Agent while pending, allowing the three named tools, and allowing everything otherwise; bun missing atstart; and the seven-day cleanup. - Registry ids, the resolver command and the hook.
- Handoff line in
shared/commands/handoff.md. - Wiring. In
machines/tjakoens-windows-pc/settings.json, one entry per event, in the form every hook there already uses:"C:\\Program Files\\Git\\bin\\bash.exe" "C:\\Users\\tjako\\.claude\\tools\\model-guard.sh" start(andprompt,tool). On both MacBooks (machines/tjakoens-macbook-air/settings.jsonandmachines/tjakoens-macbook-pro/settings.json):bash "$HOME"/.claude/tools/model-guard.sh start(andprompt,tool). Addmodel-guard.shtoexpected-hooks.txt. The MacBook entries cannot be run from the Windows PC; the owner's next MODELS-VERIFY run there is their test. - Verifier.
model.guarded(mechanically enforced) inmil-verify.ts, tested first inmil-verify.test.ts. It takes the three model-guard command strings from the live settings, runs each withMODEL_GUARD_STATE_DIRset to a fresh temporary directory andsession_idmil-verify-fixture, feedsstartwithclaude-opus-5-5, a desktop firstprompt, thentoolfor Edit, and expectsrespawn-pendingand exit 2. It never touches the real state directory. - Run report at
claude-config:artifacts/runs/<date>-model-guard-respawn.md. - Standard. A short section in
standards/MODELS.md(the rule, the override, the stuck-session path, the attachment limit) and a probe instandards/MODELS-VERIFY.md; regenerate the index withbun run standards:indexin the same commit. This step runs in a portfolio session after the claude-config work is committed.
Acceptance
The build session proves: every test in steps 1 and 5 red first and green after; the full bun test shared/tools suite with no new failure; model-roles.ts check still clean.
After the owner pushes and applies settings, an acceptance session proves: mil-verify --harness claude shows model.guarded PASS; a desktop session opened on Opus with an ordinary prompt starts a Sonnet session with the same prompt and asks to archive itself; one opened on Opus with keep this model carries on; and a /compact in a guarded session does not respawn it.
Envelope
- Lane: gated. Hooks are reviewed by the owner before they go live.
- Scope cap: the
toucheslist. - Hard stops: no push; no change to
human-lane.shorspawn-envelope.sh; no session writes live settings; no Edge repository. - Ask-triggers: a test cannot be made red first; a gate goes red twice on the same cause; the live desktop behavior differs from the facts above.
Owner's answer
D4 was answered on 2026-11-22: respawn it too.
Resume card
- Stopped: All seven steps are done. The guard passed live acceptance on 2026-11-22 after six runs, and step 7 added the Model guard section to MODELS.md and a probe to MODELS-VERIFY.md.
- Next: The owner pushes claude-config and this repo.
- Waiting on: Nothing.
- Ended: done
- Left: 2026-11-22 by claude