diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 70328f624..060669bbd 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -773,6 +773,40 @@ a quiet variable set in magpie's environment or in their Claude Code's reads no flags. An interactive run sends Claude Code's quota request as it starts, right before the turn's own. `TestClaudeRunReadsItsFlags` holds it. +A turn's run also waits on magpie's MCP calls for as long as the caller's +tool takes (`claudeToolWaitVars`): `CLAUDE_CODE_MCP_AUTO_BACKGROUND_MS=0` +and `CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT=0`. The caller runs the tools and +has its own timeouts. Interactive Claude Code 2.1.322 otherwise moves a call +still running after 120s to the background ("is still running after 120s. +It was moved to the background as task …") and its model goes on without +the result. The result then reaches the run while the model's next step is +being written, and the caller's turn got a reply with `finish_reason: +tool_calls` and no calls in it. The run went on making calls no client was +shown, each moved to the background 120s later, until it was killed (Pi: +29 such replies in 1341 turns after a tool of over 120s, none in 38735 +after a shorter one). Claude Code also aborts a call that sent nothing for +30 minutes ("sent no response or progress for 1800s"). Measured on +`claude -p` 2.1.322 with a stdio tool that sleeps (`-p` backgrounds only +with `CLAUDE_AUTO_BACKGROUND_TASKS=1`, interactive mode always): a 4s +background limit moves a 15s call to the background, `0` gets its result; +a 5s idle limit aborts a 70s call at the 30s check, `0` gets its result. A +value set in magpie's environment stays. `TestClaudeRunWaitsOnToolCalls` +holds it, by -p and by the interactive mode, and +`TestClaudeToolWaitVarsReachWSL` that a run in WSL is handed them. + +The same move delayed an answer. Pi's `ask_user_question` waited on its +user past 120s, was moved to the background, and the model wrote a bash +call past it that no client runs. The user's answer reached the run at once +but the model only once the bash call was moved to the background too, its +own 120s later (~113s after the answer, FunnyPartMent on X). Measured on +2.1.316 against a fake Anthropic API and a stdio tool that takes 10s: with +a 3s limit (interactive mode) or 8s (`-p`) the second call was written and +the result came only as a task notification once that call was moved too; +with `0` the model had the call's own result at 10.0s, by both, and no +second call was made. `0` also turns off the move Claude Code makes to hand over a message +that arrived mid-call (`isMcpCallMovableWithoutTimer` reads the same +value). + 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 f2b66acca..317081c60 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -826,7 +826,7 @@ func (c claudeCLI) asRoot(env []string) []string { // 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", "DO_NOT_TRACK", "DISABLE_ERROR_REPORTING", "DISABLE_AUTOUPDATER", "ANTHROPIC_BETAS", - "CLAUDE_CODE_EXTRA_BODY", "DISABLE_PROMPT_CACHING"} + "CLAUDE_CODE_EXTRA_BODY", "DISABLE_PROMPT_CACHING", "CLAUDE_CODE_MCP_AUTO_BACKGROUND_MS", "CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT"} // env is env for a run in config directory configDir ("" for the account // Claude Code is signed in to): for one in WSL, what of it goes in. @@ -4188,9 +4188,32 @@ func claudeRunEnv(env []string, ttl string) []string { out = append(out, k+"="+v) } } + for _, kv := range claudeToolWaitVars { + k, _, _ := strings.Cut(kv, "=") + if !slices.ContainsFunc(env, func(e string) bool { return strings.HasPrefix(e, k+"=") }) { + out = append(out, kv) + } + } return append(out, "DISABLE_AUTOUPDATER=1", "DISABLE_ERROR_REPORTING=1") } +// claudeToolWaitVars keep a turn's run waiting on magpie's MCP calls for as +// long as the caller's tool takes: the caller runs them and has its own +// timeouts. Claude Code otherwise moves a call still running after 120s to +// the background and the model goes on without the result, so the turn's +// reply is the model's next step, made before the result came, or a reply +// with no calls at all, and the run keeps making calls no one is shown +// (#6034). A call made past one still waiting on its user (Pi's +// ask_user_question) is one no client runs, and the answer, in hand at +// once, reached the model only when that call too was moved to the +// background, its 120s later (FunnyPartMent on X: ~113s). Set to 0, the +// timer is off and so is the move made to hand over a message that arrived +// mid-call (Claude Code 2.1.316's isMcpCallMovableWithoutTimer asks the +// same value). Claude Code also aborts a call that sent nothing for 30 +// minutes, a user's answer that took longer among them. A value set in +// magpie's environment stays. +var claudeToolWaitVars = []string{"CLAUDE_CODE_MCP_AUTO_BACKGROUND_MS=0", "CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT=0"} + type lockedWriter struct{ run *subscriptionRun } func (w *lockedWriter) Write(p []byte) (int, error) { diff --git a/internal/gateway/claude_tool_wait_test.go b/internal/gateway/claude_tool_wait_test.go new file mode 100644 index 000000000..68d54e965 --- /dev/null +++ b/internal/gateway/claude_tool_wait_test.go @@ -0,0 +1,80 @@ +package gateway + +import ( + "fmt" + "os" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/yetone/magpie/internal/claudetui" + "github.com/yetone/magpie/internal/testenv" +) + +// A turn's run waits on magpie's MCP calls for as long as the caller's tool +// takes, by -p and by the interactive mode: Claude Code neither moves one +// still running after 120s to the background nor aborts one silent for 30 +// minutes. A value the user set in magpie's environment stays as they set +// it. Moved to the background, a client's ask_user_question that waited on +// its user had a bash call written past it, and its answer then waited the +// bash call's own 120s to reach the model (#6034, FunnyPartMent on X). +func TestClaudeRunWaitsOnToolCalls(t *testing.T) { + for _, tui := range []bool{false, true} { + for _, c := range []struct { + name string + env map[string]string + want string + }{ + {name: "nothing set", want: "background 0 idle 0"}, + {name: "magpie's env", env: map[string]string{"CLAUDE_CODE_MCP_AUTO_BACKGROUND_MS": "600000"}, want: "background 600000 idle 0"}, + } { + t.Run(fmt.Sprintf("tui=%v/%s", tui, c.name), func(t *testing.T) { + for _, kv := range claudeToolWaitVars { + k, _, _ := strings.Cut(kv, "=") + t.Setenv(k, "") + os.Unsetenv(k) + } + for k, v := range c.env { + t.Setenv(k, v) + } + helped := filepath.Join(t.TempDir(), "helped") + if tui { + // the interactive mode, its helper a stand-in running + // the Claude Code it was handed + claudeTUIRuns = true + t.Cleanup(func() { claudeTUIRuns = false }) + helper := filepath.Join(t.TempDir(), "magpie") + testenv.Program(t, helper, "#!/bin/sh\n[ \"$1\" = \""+claudetui.Command+"\" ] || exit 9\necho start >> '"+helped+"'\nshift; claude=$1; shift\nexec \"$claude\" \"$@\"\n") + claudeTUIHelper = helper + t.Cleanup(func() { claudeTUIHelper = "" }) + } + h := newScriptHarness(t, "ok", `echo "background ${CLAUDE_CODE_MCP_AUTO_BACKGROUND_MS-unset} idle ${CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT-unset}" >> $log`) + h.ask("rules", "hello", "next") + runs := h.runs() + if len(runs) != 1 { + t.Fatalf("runs: %d", len(runs)) + } + if tui && helperStarts(helped) != 1 { + t.Fatalf("the helper started %d times", helperStarts(helped)) + } + for _, log := range runs { + if !strings.Contains(log, c.want) { + t.Fatalf("run's environment, want %q:\n%s", c.want, log) + } + } + }) + } + } +} + +// A run in WSL is handed the same variables: claudeEnvVars names them for +// wslrun's Env. +func TestClaudeToolWaitVarsReachWSL(t *testing.T) { + for _, kv := range claudeToolWaitVars { + k, _, _ := strings.Cut(kv, "=") + if !slices.Contains(claudeEnvVars, k) { + t.Fatalf("%s isn't in claudeEnvVars, so a run in WSL waits on its calls 120s at most", k) + } + } +}