Files
magpie/docs
yetone 3088872e63 claude: an interactive run goes on when only Claude Code's varying safety context changed (xybio)
xybio on Discord (Claude Pro, Claude Code through magpie, likely in auto
mode) saw it twice: right after the 5-hour window reset, one message took
the window from 100% to 0%. Their Usage page for the day (claude-opus-5-5):
- 62 requests and 17M tokens, almost all of it cache (input 102,
  output 31.3K);
- a 29.4% cache hit rate, so about 12M tokens were written to the cache,
  around 290K for each of the 41 that succeeded.
That is the whole conversation written again on almost every request.

The cause is in the bridge. With server review on, which it is for a Claude
account (AutoModeReviewed), every request carries Claude Code's
`safeguards`. The interactive mode's Claude Code reads
CLAUDE_CODE_EXTRA_BODY only as it starts, so setSafeguards restarted the run
whenever the field differed in any byte. A new run is told the whole
conversation, and that writes it all to the cache again.

The field's classifier_context differs on nearly every request. That is in
Claude Code's own code (2.1.316, Jmr):
- prior_turn_context grows with each tool round's tool_use_ids;
- git_state.status follows every edit;
- system_reminders, artifacts, text_provenance and the shed list vary too.
So every tool round and every turn of one message started a fresh run
(4 turns gave 4 runs in the new test on the old code), each paying a full
cache write of the history.

Change: in an interactive run, setSafeguards compares safeguardsKept forms.
That form leaves out the parts Claude Code itself names as variable and
sheds over its 256KB budget:
- prior_turn_context, system_reminders, artifacts, text_provenance, shed;
- Chrome's tabs;
- git_state except cwd, root, branch and default_branch.
When only those changed, the run goes on with the context it started with.
A change to the rules, permission or auto mode, folders, or the
repository and branch still restarts it. A field that isn't the expected
JSON is compared whole. The -p path is unchanged: it takes every change in
place through apply_flag_settings. The cost is that the server classifier
sees those varying parts as they were when the run started. This is noted
in docs/subsystems/claude-subscription-bridge.md.

Run at the pushed head (52d69934f, on origin/main 9335e23d3), on mote:
- go test -tags nogui -run 'TestClaudeTUI|TestClaudeBridgeSafeguard|
  TestClaude.*Safeguard|AutoMode' ./internal/gateway: ok.
- gofmt -l internal/gateway: clean.

Run at 4ff686b49, the same tree before the rebase (the rebase brought in
only 9335e23d3, outside these files):
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway: ok.
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway: one red,
  TestClosedBeforeSentEveryTimeSaysWhy (a bare EOF on the Codex
  closed-before-sent path, which this change doesn't touch). It passed
  30 of 30 alone. Filed as #5031.

Broken by hand:
- The comparison off (always restart):
  TestClaudeTUISafeguardsVaryingPartsKeepTheRun is red, "4 turns ...
  started 4 runs, want 1".
- The comparison always passing: the same test is red, "a changed rule
  went on in 1 runs", and so is TestClaudeTUISafeguardsInEnv.

Not tried:
- A real Claude account in auto mode through the interactive bridge, and
  the real 5h meter before and after.
- Whether xybio's windows also fell in the earlier cache regressions
  (v0.1.2677 to v0.1.2799). The local-classifier path, where each check is
  a one-off run that re-writes its transcript, is not changed here.
2026-11-03 14:58:57 +08:00
..