diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 7576892b1..a232d4da9 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -94,6 +94,21 @@ A run started anew still sends every image in the conversation. Replacing earlier images with a placeholder would change what the model sees, and magpie doesn't do it. +## Tool results another request answers first + +A request with tool results finds the run waiting on those calls (`match`) +and claims it (`claimResume`) until the reply it is resumed for ends. More +than one request can carry results for the same calls: Claude Code's fork +sub-agents each start from their lead's conversation as it stands, its +reply's calls answered with the placeholder "Fork started — processing in +background" and the fork's directive after, and they come at once with the +lead's own next turn; a client may also send a turn again while the first +is still answered. The first to claim the run goes on in it. Each of the +others, and one whose run has ended, is a conversation of its own from +there and gets a run started anew (`answeredElsewhere` in the log). It is +never refused: Claude Code doesn't retry a 409, and the agent that sent it +stops (ylorn on Discord). + ## The account's allowance on the reply (#1257) Claude Code tells a run's account allowance in its stream-json @@ -118,7 +133,7 @@ carries none. ## Verification ```sh -go test -tags nogui ./internal/gateway/ -run 'ClaudeRewritten|ClaudeSessionTempFiles|ClaudeLetGo|ClaudeSessionFiles|ClaudeOldSessions|ClaudeSubscriptionReplySaysTheAllowance|ClaudeAllowanceHeaders' -count=1 +go test -tags nogui ./internal/gateway/ -run 'ClaudeRewritten|ClaudeSessionTempFiles|ClaudeLetGo|ClaudeSessionFiles|ClaudeOldSessions|ClaudeSubscriptionReplySaysTheAllowance|ClaudeAllowanceHeaders|ClaudeForksAnsweringTheLeadsCalls' -count=1 ``` `claude_rewritten_test.go` has a case for each relaxation and one for each diff --git a/internal/gateway/automode_test.go b/internal/gateway/automode_test.go index 06da4c648..646bb27cf 100644 --- a/internal/gateway/automode_test.go +++ b/internal/gateway/automode_test.go @@ -12,6 +12,7 @@ import ( "runtime" "slices" "strings" + "sync/atomic" "testing" "time" @@ -453,7 +454,9 @@ func TestClaudeBridgeSafeguardWSLEnv(t *testing.T) { } // A context update and its tool-result handoff own the run together, through -// the reply's end. Another caller cannot update it or abort its owner. +// the reply's end. Another caller cannot update it or abort its owner: it is +// a conversation of its own from there (a fork sub-agent of the same lead), +// and gets a run of its own, not a 409 (ylorn on Discord). func TestClaudeBridgeSafeguardConcurrentHandoff(t *testing.T) { s := New() b := s.subscription @@ -491,21 +494,23 @@ func TestClaudeBridgeSafeguardConcurrentHandoff(t *testing.T) { t.Helper() body := []byte(`{"model":"claude-sonnet-5","max_tokens":64,"safeguards":[{"context":"B"}],"messages":[{"role":"assistant","content":"call"},{"role":"user","content":[{"type":"tool_result","tool_use_id":"` + id + `","content":"B"}]}]}`) done := make(chan int, 1) + var started atomic.Bool go func() { rec := httptest.NewRecorder() code, _ := s.serveSubscription(rec, httptest.NewRequest("POST", "/v1/messages", nil), provider.Anthropic, "Claude Code", req.Model, "", body, &Usage{}, func(context.Context, *Request) (*subscriptionRun, <-chan Event, error) { - return nil, nil, fmt.Errorf("must not start another run") + started.Store(true) + return nil, nil, fmt.Errorf("a run of its own") }) done <- code }() select { case code := <-done: - if code != 409 || run.closed || string(run.safeguards) != string(req.Safeguards) { - t.Fatalf("second caller: status %d, closed %v, context %s", code, run.closed, run.safeguards) + if code == 409 || !started.Load() || run.closed || string(run.safeguards) != string(req.Safeguards) { + t.Fatalf("second caller: status %d, own run %v, closed %v, context %s", code, started.Load(), run.closed, run.safeguards) } case <-time.After(2 * time.Second): - t.Fatal("second caller did not reject the claimed run") + t.Fatal("second caller was held on the claimed run") } } blocked("a") diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 12cf4c551..eeb7dfb55 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -2410,6 +2410,8 @@ const ( runExpired = "process expired" noRunWaiting = "no run waiting" accountChanged = "account changed" + // another request has the run's calls answered, or is answering them + answeredElsewhere = "answered by another request" ) // match is findRun, and how the results found their run (byExactID, @@ -3134,9 +3136,18 @@ func (s *Server) serveSubscription(w http.ResponseWriter, r *http.Request, from } run, results, how := s.subscription.match(req) + // Another request has the run's calls answered, or is answering them, + // or the run ended. Claude Code's fork sub-agents each start from their + // lead's conversation as it stands, a result for each of its calls and + // their directive after, so the lead's next turn and every fork it + // started answer the same calls at once; a client may also send a turn + // again while the first is still answered. The run takes one set of + // results only, and is not waiting for this request's once it is past + // the turn they were for: this request is its own conversation from + // here and gets a run of its own, told it whole. A 409 isn't retried, + // and the fork died with it (ylorn on Discord). if run != nil && !run.claimResume() { - msg := "the agent's turn is already being resumed" - return writeError(w, from, http.StatusConflict, msg), msg + run, how = nil, answeredElsewhere } // Tool-call IDs find the process that made them, independently of the // account routing selected. Continuing a different owner's process would diff --git a/internal/gateway/claude_subscription_fork_test.go b/internal/gateway/claude_subscription_fork_test.go new file mode 100644 index 000000000..5939349dd --- /dev/null +++ b/internal/gateway/claude_subscription_fork_test.go @@ -0,0 +1,188 @@ +package gateway + +import ( + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "regexp" + "runtime" + "strings" + "sync" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/testenv" +) + +// ylorn on Discord: a lead Claude Code routed by group/orchestrator to a +// Claude subscription starts sub-agents with run_in_background, and they +// died mid-run with "API Error: 409 the agent's turn is already being +// resumed". Claude Code's fork sub-agent (2.1.294's "Fork started — +// processing in background") starts from its lead's conversation as it +// stands: the lead's reply, all its tool calls, then a result for each of +// them, the same placeholder in every fork, and the fork's directive. Each +// fork's first request so answers the very calls the lead's run waits on, +// by their ids, as the lead's own next request does, and they come at once. +// Only one of them can be the run's next turn; each of the others is a +// conversation of its own from there, and is answered by a run of its own, +// told it whole, never turned away with a 409 Claude Code doesn't retry. +// Each reply is its own conversation's: no request is answered with what +// another one said. +func TestClaudeForksAnsweringTheLeadsCallsEachGetAnAnswer(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("a shell script stands in for Claude Code") + } + if _, err := exec.LookPath("curl"); err != nil { + t.Skip("the script makes its MCP calls with curl") + } + home := t.TempDir() + testenv.SetHome(t, home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + os.MkdirAll(filepath.Join(home, ".claude"), 0o755) + os.WriteFile(filepath.Join(home, ".claude", ".credentials.json"), mustJSON(map[string]any{"claudeAiOauth": map[string]any{ + "accessToken": "tok-lead", "refreshToken": "r-lead", "expiresAt": time.Now().Add(24 * time.Hour).UnixMilli(), "subscriptionType": "max"}}), 0o600) + os.WriteFile(filepath.Join(home, ".claude.json"), mustJSON(map[string]any{ + "oauthAccount": map[string]any{"emailAddress": "lead@example.com", "accountUuid": "u-lead"}}), 0o600) + + // Claude Code: a turn it is told from the start calls Agent three + // times, then makes the calls through its MCP server one after another, + // as Claude Code does, and says what their results said; a turn told + // with results in it says what those said. + dir := t.TempDir() + testenv.Program(t, filepath.Join(dir, "claude"), `#!/bin/sh +if [ "$1" = auth ]; then echo '{"loggedIn":true,"authMethod":"claude.ai","apiProvider":"firstParty","email":"lead@example.com","subscriptionType":"max"}'; exit 0; fi +cb=$(printf '%s\n' "$@" | grep -o 'http://[^"]*/_magpie/claude-mcp/[A-Za-z0-9_-]*' | head -1) +say() { + echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-opus-5-5","usage":{"input_tokens":1}}}}' + echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"heard:'"$1"'"}}}' + echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}}' + echo '{"type":"stream_event","event":{"type":"message_stop"}}' + echo '{"type":"result","subtype":"success","is_error":false,"result":""}' +} +marks() { grep -o 'directive [0-9]*\|agent launched' | sort -u | tr '\n' ','; } +while read -r line; do + case "$line" in + *control_request*) continue ;; + *"agent launched"*|*"Fork started"*) say "$(printf '%s' "$line" | marks)"; continue ;; + esac + echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-opus-5-5","usage":{"input_tokens":1}}}}' + for k in 1 2 3; do + echo '{"type":"stream_event","event":{"type":"content_block_start","index":'$k',"content_block":{"type":"tool_use","id":"toolu_L'$$'_'$k'","name":"mcp__magpie__Agent"}}}' + echo '{"type":"stream_event","event":{"type":"content_block_delta","index":'$k',"delta":{"type":"input_json_delta","partial_json":"{\"part\":'$k'}"}}}' + echo '{"type":"stream_event","event":{"type":"content_block_stop","index":'$k'}}' + done + echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"tool_use"},"usage":{"output_tokens":3}}}' + echo '{"type":"stream_event","event":{"type":"message_stop"}}' + got= + for k in 1 2 3; do + call='{"tool_call_id":"toolu_L'$$'_'$k'","name":"Agent","arguments":{"part":'$k'}}' + got="$got $(curl -s -X POST -H 'Content-Type: application/json' --data "$call" "$cb")" + done + say "$(printf '%s' "$got" | marks)" +done +`) + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + provider.ForgetAccounts() + t.Cleanup(provider.ForgetAccounts) + if err := provider.SaveGroup(provider.Group{ID: "orchestrator", Name: "Orchestrator", Members: []string{"claude/claude-opus-5-5"}}); err != nil { + t.Fatal(err) + } + s := New() + srv := httptest.NewServer(s.Handler()) + t.Cleanup(srv.Close) + t.Cleanup(s.subscription.abortAll) // before srv.Close: a run's MCP call holds a request open + t.Setenv("MAGPIE_ADDR", srv.Listener.Addr().String()) + + post := func(messages string) (int, string) { + res, err := http.Post(srv.URL+"/v1/messages", "application/json", strings.NewReader( + `{"model":"group/orchestrator","max_tokens":1000,"stream":true,"tools":[{"name":"Agent","input_schema":{"type":"object","properties":{"part":{"type":"integer"}}}}],"messages":`+messages+`}`)) + if err != nil { + return 0, err.Error() + } + defer res.Body.Close() + b, _ := io.ReadAll(res.Body) + return res.StatusCode, string(b) + } + + // the lead's conversation so far, as long as a lead's gets, which its + // run is checked against at each turn + lead := `{"role":"user","content":` + string(mustJSON("lead task: "+strings.Repeat("the codebase as read so far. ", 20000))) + `}` + code, body := post(`[` + lead + `]`) + ids := regexp.MustCompile(`toolu_L\d+_\d`).FindAllString(body, -1) + if code != 200 || len(ids) != 3 { + t.Fatalf("the lead's turn: %d, calls %v: %.400s", code, ids, body) + } + for deadline := time.Now().Add(5 * time.Second); ; { + s.subscription.mu.Lock() + waits := s.subscription.calls[ids[0]] != nil + s.subscription.mu.Unlock() + if waits { + break + } + if time.Now().After(deadline) { + t.Fatal("the lead's run never made its first call") + } + time.Sleep(5 * time.Millisecond) + } + var calls []string + for k, id := range ids { + calls = append(calls, fmt.Sprintf(`{"type":"tool_use","id":%q,"name":"Agent","input":{"part":%d}}`, id, k+1)) + } + reply := `{"role":"assistant","content":[` + strings.Join(calls, ",") + `]}` + results := func(text func(id string) string, after string) string { + var parts []string + for _, id := range ids { + parts = append(parts, fmt.Sprintf(`{"type":"tool_result","tool_use_id":%q,"content":[{"type":"text","text":%q}]}`, id, text(id))) + } + if after != "" { + parts = append(parts, fmt.Sprintf(`{"type":"text","text":%q}`, after)) + } + return `{"role":"user","content":[` + strings.Join(parts, ",") + `]}` + } + + // the lead's next turn, its sub-agents launched, and each fork's first + // request, all at once + const forks = 8 + asks := []string{`[` + lead + `,` + reply + `,` + results(func(id string) string { + return "Async agent launched successfully. agentId: a" + id[len(id)-1:] + }, "") + `]`} + want := []string{"heard:agent launched,"} + for k := 1; k <= forks; k++ { + asks = append(asks, `[`+lead+`,`+reply+`,`+results(func(string) string { + return "Fork started — processing in background" + }, fmt.Sprintf("directive %d: take part %d", k, k))+`]`) + want = append(want, fmt.Sprintf("heard:directive %d,", k)) + } + ready := make(chan struct{}) + codes, bodies := make([]int, len(asks)), make([]string, len(asks)) + var wg sync.WaitGroup + for n, msgs := range asks { + wg.Add(1) + go func() { + defer wg.Done() + <-ready + codes[n], bodies[n] = post(msgs) + }() + } + close(ready) + wg.Wait() + for n := range asks { + who := "the lead" + if n > 0 { + who = fmt.Sprintf("fork %d", n) + } + if codes[n] != 200 { + t.Errorf("%s: %d %.300s", who, codes[n], bodies[n]) + continue + } + if !strings.Contains(bodies[n], want[n]) { + t.Errorf("%s was answered %.600s, want %q", who, bodies[n], want[n]) + } + } +}