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/main9335e23d3), 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 only9335e23d3, 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.
This commit is contained in:
@@ -346,8 +346,22 @@ Safeguards are taken: interactive Claude Code reads
|
||||
`CLAUDE_CODE_EXTRA_BODY` only as it starts (2.1.316: a later
|
||||
`apply_flag_settings` or a rewritten `--settings` file changes nothing it
|
||||
sends), so `startRun` puts them in its environment, and a turn whose
|
||||
safeguards changed starts a new run told the whole conversation
|
||||
(`setSafeguards`). Client cache marks are taken too, but not kept: the
|
||||
safeguards changed in what the user allows starts a new run told the
|
||||
whole conversation (`setSafeguards`). Claude Code's classifier context
|
||||
also carries parts that change on nearly every request, the ones it
|
||||
names as variable and sheds over its 256KB budget: `prior_turn_context`,
|
||||
`system_reminders`, `artifacts`, `text_provenance`, `shed`, Chrome's
|
||||
tabs, and git's status, remotes and error. `safeguardsKept` leaves those
|
||||
out of the comparison (git keeps cwd, root, branch and default branch),
|
||||
and a run whose turn differs only there goes on with the context it
|
||||
started with. Restarting for them began a new run for every turn and
|
||||
tool round, each writing the whole conversation to the cache again, and
|
||||
one message drained a Pro account's 5-hour window (xybio;
|
||||
`TestClaudeTUISafeguardsVaryingPartsKeepTheRun`). The cost: the server
|
||||
classifier sees those parts as they were when the run started, not
|
||||
current. A rules, permission-mode, auto-mode, folder or branch change
|
||||
still restarts. The `-p` run takes every change in place
|
||||
(`apply_flag_settings`). Client cache marks are taken too, but not kept: the
|
||||
helper pastes the words without their marks, so the run keeps Claude
|
||||
Code's own prompt caching (no `DISABLE_PROMPT_CACHING`, no client tail)
|
||||
and the reply reports every client mark as dropped
|
||||
|
||||
@@ -1445,6 +1445,51 @@ func claudeExtraBody(raw json.RawMessage, tail string) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// safeguardVaries is what Claude Code's classifier context carries that
|
||||
// changes from one request to the next, the parts 2.1.316 itself names as
|
||||
// variable and leaves out over its 256KB budget (its "shed" list): the
|
||||
// turns before (prior_turn_context), system-reminder ids, artifacts,
|
||||
// Chrome's tabs, where its text came from, and git's status and remotes.
|
||||
var safeguardVaries = []string{"prior_turn_context", "system_reminders", "artifacts", "text_provenance", "shed"}
|
||||
|
||||
// safeguardsKept is a safety field without the parts that vary
|
||||
// (safeguardVaries), for telling whether what it says the user allows
|
||||
// changed: the rules, the permission and auto mode, the folders, the
|
||||
// repository and branch. A field it can't read is kept whole, so any
|
||||
// change to it counts.
|
||||
func safeguardsKept(raw json.RawMessage) []byte {
|
||||
var guards []map[string]any
|
||||
if len(raw) == 0 || json.Unmarshal(raw, &guards) != nil {
|
||||
return raw
|
||||
}
|
||||
for _, g := range guards {
|
||||
ctx, ok := g["classifier_context"].(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
for _, k := range safeguardVaries {
|
||||
delete(ctx, k)
|
||||
}
|
||||
if nav, ok := ctx["chrome_navigation"].(map[string]any); ok {
|
||||
delete(nav, "tabs")
|
||||
}
|
||||
if git, ok := ctx["git_state"].(map[string]any); ok {
|
||||
kept := map[string]any{}
|
||||
for _, k := range []string{"cwd", "root", "branch", "default_branch"} {
|
||||
if v, ok := git[k]; ok {
|
||||
kept[k] = v
|
||||
}
|
||||
}
|
||||
ctx["git_state"] = kept
|
||||
}
|
||||
}
|
||||
b, err := json.Marshal(guards)
|
||||
if err != nil {
|
||||
return raw
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// setSafeguards updates the caller's safety context and opted-in cache tail.
|
||||
// It does not change the VPS's permission state.
|
||||
// A CLI unable to acknowledge the update is discarded by the caller; its old
|
||||
@@ -1467,7 +1512,18 @@ func (r *subscriptionRun) setSafeguards(req *Request) error {
|
||||
return errors.New("safety beta changed; restart Claude Code")
|
||||
}
|
||||
if r.tui {
|
||||
// it read them as it started (startRun), and reads them no more
|
||||
// It read them as it started (startRun), and reads them no more.
|
||||
// Claude Code's classifier context changes on nearly every
|
||||
// request in parts of its own (the tool ids of the turns before,
|
||||
// git status, system-reminder ids): restarting for those started
|
||||
// a new run for every tool round and every turn, each one
|
||||
// writing the whole conversation to the cache again, and one
|
||||
// message of xybio's spent a Pro account's 5-hour window. The run
|
||||
// goes on with what it started with unless a part that says what
|
||||
// the user allows changed.
|
||||
if bytes.Equal(safeguardsKept(r.safeguards), safeguardsKept(req.Safeguards)) {
|
||||
return nil
|
||||
}
|
||||
return errors.New("safeguards changed; restart Claude Code's interactive mode")
|
||||
}
|
||||
err := r.cliControl(map[string]any{"subtype": "apply_flag_settings", "settings": map[string]any{
|
||||
|
||||
@@ -78,6 +78,51 @@ func TestClaudeTUISafeguardsInEnv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Claude Code's auto-mode context changes on nearly every request in parts
|
||||
// of its own: the tool ids of the turns before, git's status, the
|
||||
// system-reminder ids. A turn whose safeguards differ only there goes on in
|
||||
// the interactive run; restarting for them began a new run for every turn
|
||||
// and tool round, each writing the whole conversation to the cache again,
|
||||
// and one message spent a Pro account's 5-hour window (xybio). A change to
|
||||
// what the user allows (the rules here) still starts a new run.
|
||||
func TestClaudeTUISafeguardsVaryingPartsKeepTheRun(t *testing.T) {
|
||||
h := tuiScriptHarness(t, `echo "extra ${CLAUDE_CODE_EXTRA_BODY-unset}" >> $log`)
|
||||
varied := func(n int, deny string) string {
|
||||
g := strings.Replace(reviewAsked, `"toolu_01Capture001"`, fmt.Sprintf(`"toolu_01Capture%03d"`, n), 1)
|
||||
g = strings.Replace(g, `"error":"not_a_repo"}`, fmt.Sprintf(`"error":null,"status":{"clean":false,"counts":{"modified":%d},"porcelain":" M f%d.go","truncated":false},"visibility":null,"truncated":false}`, n, n), 1)
|
||||
g = strings.Replace(g, `"prior_turn_context":[`, fmt.Sprintf(`"system_reminders":[{"id":"r%d"}],"shed":[],"prior_turn_context":[`, n), 1)
|
||||
g = strings.Replace(g, `"deny":[]`, `"deny":[`+deny+`]`, 1)
|
||||
return `"safeguards":` + g + `,`
|
||||
}
|
||||
said := []string{"a1"}
|
||||
for n := 1; n <= 4; n++ {
|
||||
h.extra = varied(n, "")
|
||||
h.ask("rules", said...)
|
||||
said = append(said, fmt.Sprintf("a%d", n+1))
|
||||
}
|
||||
if runs := h.runs(); len(runs) != 1 {
|
||||
t.Fatalf("4 turns whose safeguards differ only in Claude Code's varying parts started %d runs, want 1", len(runs))
|
||||
}
|
||||
h.extra = varied(5, `"Bash(rm:*)"`)
|
||||
h.ask("rules", said...)
|
||||
runs := h.runs()
|
||||
if len(runs) != 2 {
|
||||
t.Fatalf("a changed rule went on in %d runs, want a second", len(runs))
|
||||
}
|
||||
seen := false
|
||||
for _, log := range runs {
|
||||
if strings.Contains(log, `Bash(rm:*)`) {
|
||||
seen = true
|
||||
if !strings.Contains(log, "--resume ") {
|
||||
t.Fatalf("the new run wasn't given the whole conversation: %s", log)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !seen {
|
||||
t.Fatalf("no run has the changed rule: %v", runs)
|
||||
}
|
||||
}
|
||||
|
||||
// A client's own cache marks are taken with the interactive mode on: its
|
||||
// words are pasted without them (claudetui's userPieces), so Claude Code
|
||||
// keeps its own prompt caching, not turned off for marks it never sends,
|
||||
|
||||
Reference in New Issue
Block a user