Auto mode's safeguards change with every tool batch: prior_turn_context
grows by the turn before. An interactive Claude Code run read them once,
from CLAUDE_CODE_EXTRA_BODY in its environment as it started. So a tool
loop either started the run again (the whole conversation written to the
cache again), or kept it and sent the classifier the context it started
with, which is not what real Claude Code sends.
Each turn's safeguards now reach the running run:
- The bridge writes the extra body to <run tmp>/guard.json and hands its
path to the helper as MAGPIE_CLAUDE_GUARD (mapped for WSL in newTap).
- tap.js, on each /v1/messages request whose string body has a
safeguards key, reads the file and puts its safeguards in the body. It
also sets CLAUDE_CODE_EXTRA_BODY to the file's text, so Claude Code's
own later reads agree. The first time, it writes guard.json.live.
- setSafeguards rewrites the file (temp and rename, retried for Windows)
instead of restarting, once .live exists. Without .live (a Claude Code
that doesn't load the tap), it falls back to the old rule: keep the run
when only the varying parts changed, otherwise restart. A beta change,
or safeguards appearing or going, still restarts.
The tap changes only the safeguards value; any other byte of the body is
Claude Code's own. Bodies without safeguards, count_tokens and non-string
bodies are passed through untouched.
The tool-results path logged "process expired" alone while the run's end
read "magpie ended it". It now aborts the run with the reason and logs
"tool results process expired: <reason>". continueWith's "not waiting for
tool results" does the same.
Tests:
- TestClaudeTUIToolLoopCarriesEachBatchsSafeguards: three tool batches
with growing prior_turn_context, then the answer. Checks one run and
that each request carried its own batch's safeguards.
- TestClaudeTUIToolResultsLogWhyTheRunEnded: no tap and the rules change.
Checks that the log has the reason.
- TestTapPutsTheGuardFilesSafeguards (bun and node): the rewrite, the
bodies left alone, the env, and .live.
- TestTapEnv: MAGPIE_CLAUDE_GUARD is passed through, WSL-mapped.
Run (worktree at c360d9d30 + this change):
- mote: go test -run 'TestClaudeTUI|TestClaudeBridgeSafeguard|
TestClaudeToolResults|TestClaudeNoRunSaysWhy' ./internal/gateway ok
(73 PASS); go test -run TestTap ./internal/claudetui ok (bun and
node); go vet ./internal/claudetui ./internal/gateway on darwin, linux
and windows ok; gofmt -l internal clean.
- Full suite, go test -timeout 45m ./..., ran on the Linux box (wsl-run,
magpie-desktop), not mote. mote had two other sessions' GUI suites
sitting at 0% CPU for 3h42m at load 300, and one suite per machine.
Result: 80 packages ok, internal/claudetui ok. Red:
- TestGrokInstalled (#3548)
- TestCodexBodyLimitFromMemory (#5118)
- TestMoveCutShort (#5119)
- TestCCListingKeepsItsNames (#4925)
All four are red the same way run alone at origin/main d8b3749ef on
that box. Root, internal/cli and internal/gui don't build there
without GTK4 (#5120).
- Real use, macOS:
- Sandbox HOME and CLAUDE_CONFIG_DIR, the owner's Max account copied
to the sandbox dir's keychain item and deleted after. The refresh
token didn't rotate.
- A gateway on 127.0.0.1:3496; magpie built -tags dev at c360d9d30
(before) and with this change (after).
- Client: real interactive Claude Code 2.1.316 in tmux,
--permission-mode auto, sonnet 5.5 through the gateway.
- One turn, three sequential Bash calls (echo one, ls -la, echo three),
four upstream requests.
- Before: one run (pid 49488), cache_write 23005/86/162/86 (the first
request cold). Its CLAUDE_CODE_EXTRA_BODY stayed the first request's
safeguards, with no prior_turn_context, so requests 2 to 4 sent the
classifier stale context.
- After: one run (pid 64000), cache_read 23005/23091/23091/23253,
cache_write 0/0/162/86. guard.json.live was written; the tap logged
3 "guard":"set" (requests 2 to 4), and guard.json ended with 3
prior_turn_context entries.
Broken by hand:
- The old claude_subscription.go makes both gateway tests red ("request
0 carried ..."; the log said "tool results process expired" alone).
- tap.js without the rewrite makes TestTapPutsTheGuardFilesSafeguards
red in bun and node.
Not tried:
- A tool loop whose rules change mid-loop with a real client (restart
path; only the fake covers it).
- Linux or WSL with a real Claude Code (the TUI isn't used on Windows).
- Comparing the before/after upstream bodies byte for byte (the tap
logs that it set the value, not the bodies).
- The gateway package as its own -timeout 45m run on macOS: only the
targeted tests ran on mote, and the full package ran on Linux.