diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 876b44cf0..ca6f93027 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -589,6 +589,25 @@ interactive, so its `/usage` asks the usage endpoint alone setup-token, which magpie never reads, or an API key) gets the session's cost instead. `TestClaudeUsageStartsNoInteractiveRun` holds it. +A turn's run (`claudeRunEnv`) is not given +`CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC`, which the one-off runs above +keep (`cleanClaudeEnv`). With it, or `DISABLE_TELEMETRY` or +`DO_NOT_TRACK`, Claude Code 2.1.316 reads none of its feature flags, and +one of them (`tengu_curious_tower`) is Message Threads: a turn sends only +what came after the last reply. Without it every turn sent the whole +history again, every image in it (#5963, nylesssssssss: with a ~3 MB PNG, +672 KB on each of four turns; 737, 6, 3 and 3 KB with the flags read, on +a real Team account behind a counting proxy). Nothing forces threads: +`CLAUDE_CODE_TETHER_LIVE`, which turns them on whatever the account's +flag says, is never set, and an account Anthropic hasn't given threads +sends whole, as its own `claude` does. Updates and error reports stay off +(`DISABLE_AUTOUPDATER`, `DISABLE_ERROR_REPORTING`); Claude Code's own +telemetry goes as a plain `claude`'s does, unless the user turned it off: +a quiet variable set in magpie's environment or in their Claude Code's +`settings.json` env is given to the run as they set it, and the run then +reads no flags. An interactive run sends Claude Code's quota request as it +starts, right before the turn's own. `TestClaudeRunReadsItsFlags` holds it. + The helper still takes a typed `/usage`, and nothing of magpie's sends it now: it is told `{"type":"command","command":"/usage"}`, types it, waits for the panel's windows ("NN% used", `commandSettle` still) and closes it with diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index e9f81f741..3c446d7ef 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -781,7 +781,7 @@ func (c claudeCLI) asRoot(env []string) []string { // claudeEnvVars are the variables magpie sets for a Claude Code run, which // one in WSL is handed (wslrun's Env). var claudeEnvVars = []string{"ENABLE_CLAUDEAI_MCP_SERVERS", "DISABLE_AUTO_COMPACT", "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", - "DISABLE_TELEMETRY", "DISABLE_ERROR_REPORTING", "DISABLE_AUTOUPDATER", "ANTHROPIC_BETAS", + "DISABLE_TELEMETRY", "DO_NOT_TRACK", "DISABLE_ERROR_REPORTING", "DISABLE_AUTOUPDATER", "ANTHROPIC_BETAS", "CLAUDE_CODE_EXTRA_BODY", "DISABLE_PROMPT_CACHING"} // env is env for a run in config directory configDir ("" for the account @@ -1215,7 +1215,7 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, cmd = binary.command(context.Background(), args...) } cmd.Dir = work - env, err := binary.proxied(ctx, cleanClaudeEnvTTL(os.Environ(), prefix.runTTL())) + env, err := binary.proxied(ctx, claudeRunEnv(os.Environ(), prefix.runTTL())) if err != nil { cleanup() return nil, nil, err @@ -3752,6 +3752,53 @@ func cleanClaudeEnvTTL(env []string, ttl string) []string { "CLAUDE_CODE_PROMPT_CACHE_TTL="+ttl) } +// claudeQuietVars are the variables that keep Claude Code from fetching +// its feature flags: with any of them set (CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC, +// DISABLE_TELEMETRY or DO_NOT_TRACK; 2.1.316's privacy level, "default" only +// with none) it reads none, and every flag is its default. +var claudeQuietVars = []string{"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "DISABLE_TELEMETRY", "DO_NOT_TRACK"} + +// claudeRunEnv is the environment a bridge's run (a caller's turns) starts +// Claude Code in: cleanClaudeEnvTTL's, but with Claude Code's network as a +// plain claude has it, so it reads its feature flags for the account. One of +// them, tengu_curious_tower, is Message Threads: a turn sends only what came +// after the last reply instead of the whole history, every image in it again +// (#5963, nylesssssssss; 681KB a turn with a 3MB image, 3KB with threads). +// Nothing is forced: an account Anthropic hasn't given threads goes whole, +// as its own claude does. Updates and error reports stay off, which the +// flags don't need. The quiet variables stay as the user set them, in +// magpie's environment or in their Claude Code's settings.json env (the run +// reads no settings of the user's: --setting-sources ""), since they are the +// user's telemetry choice for Claude Code. +// +// The one-off runs (the model test, the warm-up, /usage) keep +// cleanClaudeEnv's CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: they have no +// history to send, and an interactive one would otherwise send a model +// request of its own as it starts (probeQuotaStatus, #5582). A turn's +// interactive run sends that request too, right before the turn's own, +// which opens the five-hour window anyway. +func claudeRunEnv(env []string, ttl string) []string { + out := slices.DeleteFunc(cleanClaudeEnvTTL(env, ttl), func(e string) bool { + k, _, _ := strings.Cut(e, "=") + return slices.Contains(claudeQuietVars, k) + }) + settings := filepath.Join(claudeConfigDir(), "settings.json") + for _, k := range claudeQuietVars { + v, _ := edit.GetJSON(settings, "env."+k) + if v == "" { + for _, e := range env { + if w, ok := strings.CutPrefix(e, k+"="); ok { + v = w + } + } + } + if v != "" { + out = append(out, k+"="+v) + } + } + return append(out, "DISABLE_AUTOUPDATER=1", "DISABLE_ERROR_REPORTING=1") +} + type lockedWriter struct{ run *subscriptionRun } func (w *lockedWriter) Write(p []byte) (int, error) { diff --git a/internal/gateway/claude_threads_test.go b/internal/gateway/claude_threads_test.go new file mode 100644 index 000000000..b4cf0475f --- /dev/null +++ b/internal/gateway/claude_threads_test.go @@ -0,0 +1,58 @@ +package gateway + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +// A turn's Claude Code runs with its network as a plain claude has it, so it +// reads the account's feature flags, Message Threads among them (#5963): +// with CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC, DISABLE_TELEMETRY or +// DO_NOT_TRACK set it reads none, and every turn sends the whole history, +// every image in it. Nothing forces threads (CLAUDE_CODE_TETHER_LIVE is +// never set). Updates and error reports stay off, and a quiet variable the +// user set, in magpie's environment or in their Claude Code's settings.json, +// stays as they set it. +func TestClaudeRunReadsItsFlags(t *testing.T) { + for _, c := range []struct { + name, settings string + env map[string]string + want string + }{ + {name: "nothing set", want: "traffic unset telemetry unset track unset tether unset updater 1 errors 1"}, + {name: "settings telemetry off", settings: `{"env":{"DISABLE_TELEMETRY":"1"}}`, want: "traffic unset telemetry 1 track unset tether unset updater 1 errors 1"}, + {name: "settings nonessential off", settings: `{"env":{"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC":"true"}}`, want: "traffic true telemetry unset track unset tether unset updater 1 errors 1"}, + {name: "magpie's env", env: map[string]string{"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1", "DO_NOT_TRACK": "1"}, want: "traffic 1 telemetry unset track 1 tether unset updater 1 errors 1"}, + } { + t.Run(c.name, func(t *testing.T) { + for _, k := range append(claudeQuietVars, "CLAUDE_CODE_TETHER_LIVE", "DISABLE_AUTOUPDATER", "DISABLE_ERROR_REPORTING") { + t.Setenv(k, "") + os.Unsetenv(k) + } + for k, v := range c.env { + t.Setenv(k, v) + } + h := newScriptHarness(t, "ok", `echo "traffic ${CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC-unset} telemetry ${DISABLE_TELEMETRY-unset} track ${DO_NOT_TRACK-unset} tether ${CLAUDE_CODE_TETHER_LIVE-unset} updater ${DISABLE_AUTOUPDATER-unset} errors ${DISABLE_ERROR_REPORTING-unset}" >> $log`) + if c.settings != "" { + if err := os.MkdirAll(h.config, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(h.config, "settings.json"), []byte(c.settings), 0o600); err != nil { + t.Fatal(err) + } + } + h.ask("rules", "hello", "next") + runs := h.runs() + if len(runs) != 1 { + t.Fatalf("runs: %d", len(runs)) + } + for _, log := range runs { + if !strings.Contains(log, c.want) { + t.Fatalf("run's environment, want %q:\n%s", c.want, log) + } + } + }) + } +}