claude: a client's tool call is waited for however long it takes, never moved to the background at 120s (#6034, FunnyPartMent on X)
#6034 (Suge8) and FunnyPartMent on X: interactive Claude Code moves an
MCP call still running after 120s to the background (2.1.316's
getMcpAutoBackgroundMs: 120000 by default, behind
tengu_mcp_auto_background) and tells the model "It was moved to the
background as task ... You can keep working in the meantime". Every
client tool magpie hands the bridge is such a call.
- Pi's long tool runs: the model wrote its next step without the result,
and the turn's reply was a tool_calls finish with no calls; the run
went on calling tools no one was shown.
- Pi's ask_user_question waiting on its user: the model wrote a bash
call past it that no client runs. The answer reached the run at once
but the model only when that bash call was moved to the background
too, its own 120s later (~113s after the answer).
Change (Suge8's branch fix/claude-mcp-wait, ported): claudeRunEnv sets
CLAUDE_CODE_MCP_AUTO_BACKGROUND_MS=0 and CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT=0
(claudeToolWaitVars) unless magpie's environment sets them, for -p and
the interactive mode alike, and claudeEnvVars hands both to a run in WSL.
0 also turns off the move made to hand over a message that arrived
mid-call (isMcpCallMovableWithoutTimer reads the same value), and the
idle limit no longer aborts an answer that took over 30 minutes. One-off
runs (model test, warm-up, /usage) make no MCP calls and are unchanged.
The subsystem page says why, with the measurements.
Reproduced (Claude Code 2.1.316, sandbox HOME and CLAUDE_CONFIG_DIR, a
fake Anthropic API on 127.0.0.1 and a stdio MCP server whose "wait"
answers after 10s and "bash" after 100s; no real account):
- interactive mode, limit 3000: wait moved at 3s, the model called
bash, bash moved at 3s, the result came only as a task notification.
- -p with CLAUDE_AUTO_BACKGROUND_TASKS=1, limit 8000: wait moved at
8.0s, bash called, next model request at 16.0s, 6s after the result
was in (the 113s, scaled).
- limit 0, both modes: the model's next request at 10.0s carried
"late-result", no bash call made.
Run (on mica, Go 1.26.8, at this change on 53db3e389; the four tests
above again at this head, rebased on 7707ba9ad: ok):
- red on the old code: with claudeRunEnv's append taken out,
TestClaudeRunWaitsOnToolCalls fails all four subtests (tui=false and
tui=true, "run's environment, want \"background 0 idle 0\"");
with the two names out of claudeEnvVars TestClaudeToolWaitVarsReachWSL
fails ("CLAUDE_CODE_MCP_AUTO_BACKGROUND_MS isn't in claudeEnvVars").
Both pass with the change, beside TestClaudeRunReadsItsFlags and
TestCleanClaudeEnvRemovesGatewayOverrides.
- gofmt -l empty; go vet, GOOS=linux go vet -tags nogui and GOOS=windows
go vet of internal/gateway; go build, GOOS=linux go build -tags nogui,
GOOS=windows go build: ok.
- go vet ./... (3 OSes) and go test -tags nogui -timeout 45m ./... in a
sandbox HOME: running on mica at push time; result goes on #6034.
Not tried: a real Claude subscription turn from Pi through a rebuilt
magpie with a tool of over 120s; the real 120s default (measured with
3s and 8s limits).
Co-authored-by: Suge8 <nnyless@foxmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user