Files
magpie/internal/gateway/claude_cut_run_test.go
T
yetone a8e98e1abb gateway: a Claude Code run that exits mid-reply is a failure, not a 200 (#4822)
A Claude subscription run whose Claude Code exited after its reply began,
with no message_stop nor a result (only the message_start, or part of the
text), closed its segment as if the reply had ended. The client was
answered 200 with nothing or the words so far, and the agent took it as a
finished turn.

- readOutput: at the end of Claude Code's output, a reply begun on the
  open segment (KStart, tracked in emit as unended until a KStop or
  KError) ends with a KError, 502, "Claude Code exited before it ended
  its reply" and the end of its stderr. -p and the interactive mode share
  this path.
- relay, not streamed: any error is a status, words said before it too
  (was: a 200 with the partial reply), as a translated reply cut short is
  (errEndedShort). The try's usage is kept as before (d2a83de61).
- Streamed with nothing sent yet: the head's KError is a status, as
  before, so the spare answers by the existing failover rules, and the
  cut try keeps its handed_on usage row. Streamed after text: the
  protocol's error event (Anthropic `error`, chat's error chunk with no
  [DONE], Responses' response.failed), no end event, no other run.
- A reply that ended with message_stop, or whose stop was told, is
  unchanged.
- The tests' fake Claude Code (claudeSubscriptionFirstSays) answers
  control requests, so it runs in the interactive mode too.

TestClaudeSubscriptionRunExitedMidReplyFails: message_start then exit and
text then exit, on /v1/messages, /v1/chat/completions and /v1/responses,
streamed and not, -p and interactive (24 cases): the spare's answer and
the cut try's handed_on row (900 cache write) where nothing was sent, the
error event and no end where text was streamed.

Run, at this tree on origin/main 89ffc58f5:
- mote: go test -run TestClaudeSubscriptionRunExitedMidReplyFails
  ./internal/gateway ok (24/24); go vet -tags nogui ./internal/gateway
  for darwin, linux and windows ok; gofmt -l clean.
- mote: go test -tags nogui -timeout 45m ./internal/gateway
  ./internal/claudetui: claudetui ok; gateway red on
  TestCodexPoolSaysTheClientsVersion alone (#4807, another test's Codex
  identity, open and passes alone; this change doesn't touch Codex).
- Full suite (go test -tags nogui -timeout 45m ./..., Linux, wsl-run)
  was still running at push time; internal/agent's install-detection
  reds there are #4033. Its result goes on #4822.
Broken by hand: with claude_subscription.go at origin/main, all 24 cases
red (200, runs "own", partial or empty reply). With the relay change
alone reverted, the 6 text/not-streamed cases red (200 partial).
Not tried: a real Claude Code whose upstream cuts the stream and exits
mid-reply. A non-streamed reply cut after text now goes on to the spare
account, which is billed for the conversation again (the client gets a
whole answer instead of a cut one, as for translated providers).
2026-11-02 04:38:57 +08:00

113 lines
5.1 KiB
Go

package gateway
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/claudetui"
"github.com/yetone/magpie/internal/testenv"
"github.com/yetone/magpie/internal/usage"
)
// A Claude subscription run whose Claude Code exits after its reply
// began, with no message_stop nor a result, is a failure the agent sees,
// in every protocol a Claude subscription is served on, by -p and by the
// interactive mode. It was a 200 with nothing, or the words so far, as a
// reply that ended (#4822). Exited after its message_start alone, the
// spare account answers, as for any failure before the answer. Exited
// part way through the text, a stream already sent has the protocol's
// error event and no end, and the account isn't asked again; a reply not
// streamed has had nothing sent, and is a failure the spare answers.
func TestClaudeSubscriptionRunExitedMidReplyFails(t *testing.T) {
start := ` echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m1","model":"claude-sonnet-5","usage":{"input_tokens":3,"cache_creation_input_tokens":900}}}}'
`
text := start + ` echo '{"type":"stream_event","event":{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}}'
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"half an ans"}}}'
`
asks := []struct{ path, body string }{
{"/v1/messages", `{"model":"claude/claude-sonnet-5","max_tokens":50,"stream":%v,"messages":[{"role":"user","content":"ping"}]}`},
{"/v1/chat/completions", `{"model":"claude/claude-sonnet-5","stream":%v,"messages":[{"role":"user","content":"ping"}]}`},
{"/v1/responses", `{"model":"claude/claude-sonnet-5","stream":%v,"input":[{"role":"user","content":[{"type":"input_text","text":"ping"}]}]}`},
}
// what each protocol's stream ends a reply with, and fails one with
ends := map[string][2]string{
"/v1/messages": {"event: message_stop", "event: error"},
"/v1/chat/completions": {"data: [DONE]", `"error":{`},
"/v1/responses": {"response.completed", "response.failed"},
}
for _, tui := range []bool{false, true} {
for _, cut := range []string{"start", "text"} {
for _, ask := range asks {
for _, stream := range []bool{true, false} {
t.Run(fmt.Sprintf("tui=%v/%s/%s/stream=%v", tui, cut, ask.path, stream), func(t *testing.T) {
first := start
if cut == "text" {
first = text
}
log := claudeSubscriptionFirstSays(t, first+" exit 0\n")
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 = "" })
}
s := New()
t.Cleanup(s.subscription.abortAll)
code, reply := sendTo(s, ask.path, fmt.Sprintf(ask.body, stream))
b, _ := os.ReadFile(log)
runs := strings.Join(strings.Fields(string(b)), ",")
if tui {
if n := helperStarts(helped); n != strings.Count(runs, ",")+1 {
t.Fatalf("the helper started %d times for runs %q", n, runs)
}
}
if cut == "text" && stream {
// the words so far went out; the reply fails
// where it was cut, and no other run tells it
// again under them
end := ends[ask.path]
if code != 200 || !strings.Contains(reply, "half an ans") || !strings.Contains(reply, end[1]) ||
strings.Contains(reply, end[0]) || strings.Contains(reply, "pong") || runs != "own" {
t.Fatalf("status %d (runs %q):\n%s", code, runs, reply)
}
if !strings.Contains(reply, "exited before it ended its reply") {
t.Fatalf("the error doesn't say why:\n%s", reply)
}
return
}
// nothing has reached the client: the spare answers
if code != 200 || !strings.Contains(reply, "pong") || strings.Contains(reply, "half an ans") || runs != "own,tok-spare" {
t.Fatalf("status %d (runs %q):\n%s", code, runs, reply)
}
r := lastRoute(s)
if len(r.Tries) != 2 || r.Tries[0].Status < 500 || !strings.Contains(r.Tries[0].Error, "exited before it ended its reply") {
t.Fatalf("tries: %+v", r.Tries)
}
// what the cut try billed keeps its row (d2a83de61)
var handed []usage.Record
for deadline := time.Now().Add(10 * time.Second); len(handed) == 0 && time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) {
for _, u := range usage.Load(time.Time{}) {
if u.RouteID == r.ID && u.HandedOn {
handed = append(handed, u)
}
}
}
if len(handed) != 1 || handed[0].ProviderAccount != "me@example.com" || handed[0].CacheWrite != 900 {
t.Fatalf("the cut try's row: %+v", handed)
}
})
}
}
}
}
}