gateway: a Claude run reads its flags, so Message Threads stop re-sending every image (nylesssssssss on X, #5963)

Every bridge run started Claude Code with
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1 (there since the bridge's
first commit, 702477525, not added for #5582). With it, or
DISABLE_TELEMETRY or DO_NOT_TRACK, Claude Code 2.1.316 reads none of its
GrowthBook flags (its privacy level must be "default"), and Message
Threads are on by one of them, tengu_curious_tower. So every turn sent
the whole history again, every image in it (#4496's slow turns).

A turn's run (claudeRunEnv) now leaves the quiet variables out, so Claude
Code reads the account's own flags as a plain claude does. Updates and
error reports stay off (DISABLE_AUTOUPDATER, DISABLE_ERROR_REPORTING,
which the flags don't need). A quiet variable the user set, in magpie's
environment or in their Claude Code's settings.json env, is passed as
they set it (their telemetry choice; the run reads no settings of its
own). WSL runs get DO_NOT_TRACK too. The one-off runs (model test,
warm-up, /usage) keep the switch: no history, and an interactive one
would send probeQuotaStatus's request as it starts (#5582).

The issue's patch set CLAUDE_CODE_TETHER_LIVE=1 instead. Not taken: in
2.1.316 that variable wins over the account's flag (ryr(): the env value
is returned before tengu_curious_tower is read), so it would turn threads
on for accounts Anthropic hasn't given them, or has turned them off for.
Here nothing is forced; an account without the flag goes whole, as its
own claude does. Nothing of magpie's sends a request; CLAUDE_CODE_ENTRYPOINT
is untouched.

Run:
- Real Team account (sandbox CLAUDE_CONFIG_DIR + HOME, the keychain item
  copied to the sandbox's name and deleted after, token not rotated),
  claude 2.1.316 -p stream-json, haiku, one process, a 3 MB noise PNG then
  three text turns, bytes to api.anthropic.com per turn through a
  counting CONNECT proxy:
    magpie's old env (NONESSENTIAL=1): 672, 667, 667, 668 KB
    this env (no quiet vars, updater + errors off): 737, 6.3, 2.6, 2.6 KB
  Turn 4 answered the codeword from turn 1 both times. The fresh config
  had no cached flags; after the run it had 1223, tengu_curious_tower true.
  Other traffic with this env: one connection to Datadog's log intake
  (1.5 KB), Claude Code's own.
- go test -count=1 -run 'Claude|WSL|Warm|Usage' ./internal/gateway on mote:
  ok (139s). The full package queued behind ten suites on mote's lock
  and was not run before the tag; the full suite runs after it.
- TestClaudeRunReadsItsFlags: red with the run back on cleanClaudeEnvTTL
  (3 of its 4 cases; the one where magpie's env sets them passes on both), green with it.
- gofmt; go vet darwin, linux (nogui), windows of internal/gateway.

Not tried: a run through a sandbox magpie end to end, the interactive
(TUI) mode with a real account (an image turn runs as -p anyway), WSL, an
account whose flag is off.
This commit is contained in:
yetone
2026-11-10 22:44:04 +08:00
parent e053c337aa
commit ee818e8bce
3 changed files with 126 additions and 2 deletions
@@ -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
+49 -2
View File
@@ -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) {
+58
View File
@@ -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)
}
}
})
}
}