gateway: a group's trusted Claude, GPT and Gemini thinking streams as it comes, a refusal or error after it going to the next member (#4166)
A try whose model's vendor may refuse after its reasoning (#248) had its reasoning held while another candidate could answer, whatever the member had answered lately. Through a routing group Claude Code saw a Claude member's thinking only with its text, all at once, and never its "thinking with <effort> effort". waynesguan measured it on one WorkBuddy account: `workbuddy claude-opus-5.5` alone, first thinking at 3.66s; through the group, all 67 thinking deltas with the text at 17.25s. Ported from waynesguan's 71aacaa (onf8987ce61): the trust #4512 gave DeepSeek now covers Claude, GPT and Gemini, but only where a refusal after the reasoning can be handed to the next candidate in the same stream (handsOver, refusalHandover). Once a member's thinking has gone on to an answer thoughtCleanMost (3) times running, its reasoning goes as it comes; a refusal after it, nothing yet said, goes to the next member, whose reply follows as the same one; the refusal takes the trust back. Where no handover is possible (no stream, a Gemini-protocol agent, a compaction wanted whole, refusals asked again enough, the last candidate) it is held as before. holdsThinking is decided after hw.over. Added in review: the port alone lost failover for errors. passOver handed over only a refusal; a retryable error after shown reasoning (Anthropic's mid-stream overloaded_error) counted as the answer begun and reached the agent, the next member never asked, where the held stream had failed over. passOver now hands a retryable error over too (handOver(status, msg, refused=false)): the try fails with its status and words, as a held one did, and the next member's stream follows. A non-retryable error (a request at fault) still reaches the agent. This also applies to GLM's and trusted DeepSeek's shown reasoning, which had the same gap. Tests: - TestTrustedThinkingFailsOverOnEveryPath: claude (Anthropic upstream), gpt and gemini (Chat upstream) members x refused / overloaded / invalid x /v1/messages, /v1/chat/completions, /v1/responses, 27 cases; spliced stream checked per protocol (one message_start, block indexes, sequence numbers, one [DONE]). - TestClaudeThinkingStreamsOnceTrusted, TestClaudeTrustedThinkingStreamsThroughGroup (waynesguan's), TestClaudeTrustedThinkingErrorFailsOver. Broken by hand: - main's fallback.go + gateway.go: the 18 refused/overloaded cases, TestClaudeThinkingStreamsOnceTrusted ("a trusted Claude member's reasoning is still held") and ...ThroughGroup ("first thought only 1.541µs before the text") FAIL. - port without the error handover: the 9 overloaded cases and TestClaudeTrustedThinkingErrorFailsOver FAIL (the agent got "Overloaded", b asked 0 times). Run (mote, via mote-run): go test ./internal/gateway/ at 741f7a8f2 (877s) ok but TestClosedBeforeSentEveryTimeSaysWhy (bare EOF, Codex closed-unsent path, already #5031, not touched here); after rebase one7b0c398e: -run 'Thinking|Refusal|Handover|ObjectRoot|DeepSeek' ok; gofmt -l clean; go vet ./internal/gateway/, GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... ok. Full suite runs after push; its reds are filed separately. Not tried: a real refusal or overloaded_error after thinking from a real account (can't be triggered at will); Claude Code against a real group with this build; Windows/WSL runs (vet only). Co-authored-by: waynesguan <wenwenguan@tencent.com>
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -2631,8 +2631,8 @@ func refusesAfterThinking(model string) bool {
|
||||
// deepseekAfterThinking tells whether the model is DeepSeek's, the one
|
||||
// vendor whose reasoning the refusal hold lets go once the member has
|
||||
// gone on from its thinking to an answer enough times running
|
||||
// (holdsThinking): Claude's, GPT's and Gemini's is held whatever the
|
||||
// member has answered lately (#248).
|
||||
// (holdsThinking) whether or not a refusal after it can be handed over:
|
||||
// Claude's, GPT's and Gemini's only where it can (#248, #4166).
|
||||
func deepseekAfterThinking(model string) bool {
|
||||
m := strings.ToLower(model)
|
||||
if i := strings.LastIndex(m, "/"); i >= 0 {
|
||||
@@ -2642,8 +2642,8 @@ func deepseekAfterThinking(model string) bool {
|
||||
}
|
||||
|
||||
// thoughtCleanMost is how many thinking replies in a row that went on to
|
||||
// an answer trust a DeepSeek member with its reasoning as it comes
|
||||
// (holdsThinking): its filter refuses after thinking hardly ever (#4512:
|
||||
// an answer trust a member with its reasoning as it comes
|
||||
// (holdsThinking): DeepSeek's filter refuses after thinking hardly ever (#4512:
|
||||
// one content_filter in 33,245 group requests over 7 days), and held,
|
||||
// every one of those requests kept the agent waiting its whole thinking
|
||||
// out. A refusal of its own takes the trust back (thoughtRefused).
|
||||
@@ -2653,13 +2653,16 @@ const thoughtCleanMost = 3
|
||||
// refusal after it while another candidate could answer. DeepSeek's is,
|
||||
// until the member's thinking has gone on to an answer thoughtCleanMost
|
||||
// times running; past that it goes through as it comes, GLM's way.
|
||||
// Claude's, GPT's and Gemini's is held whatever the member answered
|
||||
// lately (#248).
|
||||
func (s *Server) holdsThinking(c candidate) bool {
|
||||
// Claude's, GPT's and Gemini's is trusted the same way only where a
|
||||
// refusal after it is handed to the next candidate in the same stream
|
||||
// (handsOver, refusalHandover), and is held whatever the member answered
|
||||
// lately where it isn't (#248): told to the agent, such a refusal would
|
||||
// end the turn with nothing said (#4166).
|
||||
func (s *Server) holdsThinking(c candidate, handsOver bool) bool {
|
||||
if !refusesAfterThinking(c.model) {
|
||||
return false
|
||||
}
|
||||
if !deepseekAfterThinking(c.model) {
|
||||
if !deepseekAfterThinking(c.model) && !handsOver {
|
||||
return true
|
||||
}
|
||||
s.thoughtMu.Lock()
|
||||
@@ -2670,7 +2673,7 @@ func (s *Server) holdsThinking(c candidate) bool {
|
||||
// thoughtAnswered counts a thinking reply that went on to an answer, for
|
||||
// holdsThinking's trust to run out on.
|
||||
func (s *Server) thoughtAnswered(c candidate) {
|
||||
if !deepseekAfterThinking(c.model) {
|
||||
if !refusesAfterThinking(c.model) {
|
||||
return
|
||||
}
|
||||
s.thoughtMu.Lock()
|
||||
@@ -2686,7 +2689,7 @@ func (s *Server) thoughtAnswered(c candidate) {
|
||||
// is held again for the next requests, until it has answered
|
||||
// thoughtCleanMost times running.
|
||||
func (s *Server) thoughtRefused(c candidate) {
|
||||
if !deepseekAfterThinking(c.model) {
|
||||
if !refusesAfterThinking(c.model) {
|
||||
return
|
||||
}
|
||||
s.thoughtMu.Lock()
|
||||
|
||||
@@ -556,9 +556,10 @@ type Server struct {
|
||||
// (UnixNano, 0 for never): while that is recent, a group skips it for
|
||||
// such a request when someone is left after it (antigravityForeseen)
|
||||
agTurnedAway atomic.Int64
|
||||
// thoughtMu keeps the count of each DeepSeek member's thinking replies
|
||||
// that went on to an answer in a row, by "<provider>/<model>"
|
||||
// (holdsThinking, #4512)
|
||||
// thoughtMu keeps the count of each member's thinking replies that
|
||||
// went on to an answer in a row, by "<provider>/<model>", for a
|
||||
// vendor that may refuse after its thinking (holdsThinking, #4512,
|
||||
// #4166)
|
||||
thoughtMu sync.Mutex
|
||||
thoughtClean map[string]int
|
||||
}
|
||||
@@ -3205,7 +3206,11 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
// thoughtCleanMost times running (holdsThinking, #4512): its
|
||||
// filter refuses after thinking hardly ever, and held, the agent
|
||||
// waited every request's whole thinking out.
|
||||
hw.thinkingShown = !s.holdsThinking(c) || last
|
||||
// Claude's, GPT's and Gemini's go as they come the same way while
|
||||
// a refusal after them can still be handed over (hw.over): held,
|
||||
// a group's Claude showed Claude Code its thinking only with its
|
||||
// text, all at once, and never its "thinking with <effort>
|
||||
// effort" (#4166). Set below, once hw.over is.
|
||||
hw.ctx, hw.alive, hw.streams = r.Context(), kept, streams
|
||||
// a Claude subscription account's refusal of a big conversation
|
||||
// isn't told again to the subscription's other accounts
|
||||
@@ -3221,6 +3226,8 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
// same stream (refusalHandover; yhwangl on X)
|
||||
hw.over = &refusalHandover{proto: from, seq: -1}
|
||||
}
|
||||
// the reasoning's hold, above, once the handover is known
|
||||
hw.thinkingShown = !s.holdsThinking(c, hw.over != nil) || last
|
||||
report := cacheReport
|
||||
if c.p.Account == nil || c.p.Account.Agent != "claude" {
|
||||
report = nil
|
||||
|
||||
@@ -92,7 +92,20 @@ func (h *holdWriter) passOver(b []byte) {
|
||||
switch kind, status, msg := streamEvent(ev); kind {
|
||||
case eventRefusal, eventRefusalError:
|
||||
o.pending = nil
|
||||
h.handOver(status, msg)
|
||||
h.handOver(status, msg, true)
|
||||
return
|
||||
case eventError:
|
||||
// a failure the next candidate may answer, nothing yet said
|
||||
// (Anthropic's overloaded_error after its thinking): handed
|
||||
// over as a refusal is, as it went to the next candidate
|
||||
// while the reasoning was held (#4166). One that isn't
|
||||
// retryable is the agent's, as before.
|
||||
if !retryable(status, []byte(msg)) {
|
||||
o.spoke = true
|
||||
break
|
||||
}
|
||||
o.pending = nil
|
||||
h.handOver(status, msg, false)
|
||||
return
|
||||
case eventThinking, eventLead, eventBuffering:
|
||||
o.track(ev)
|
||||
@@ -124,8 +137,10 @@ func (h *holdWriter) out(b []byte) {
|
||||
// what went before it, an Anthropic block still open is stopped, and the
|
||||
// try fails as a refusal with nothing said does, for the next candidate's
|
||||
// stream to follow in the same response. Its account doesn't rest: the
|
||||
// request was refused, not the account (#248).
|
||||
func (h *holdWriter) handOver(status int, msg string) {
|
||||
// request was refused, not the account (#248). A retryable error handed
|
||||
// over (refused false) fails the try as one held would have, its status
|
||||
// and its words for the account's rest.
|
||||
func (h *holdWriter) handOver(status int, msg string, refused bool) {
|
||||
o := h.over
|
||||
if o.proto == provider.Anthropic && len(o.open) > 0 {
|
||||
open := make([]int, 0, len(o.open))
|
||||
@@ -140,7 +155,7 @@ func (h *holdWriter) handOver(status int, msg string) {
|
||||
}
|
||||
h.flush()
|
||||
h.handedOver, h.passing = true, false
|
||||
h.refused, h.failure, h.failMsg = true, status, msg
|
||||
h.refused, h.failure, h.failMsg = refused, status, msg
|
||||
// the refusal's own end (message_stop, [DONE]) wasn't sent: the
|
||||
// reply isn't whole, and the next one's stream follows without its
|
||||
// 200 and headers again
|
||||
|
||||
@@ -274,3 +274,110 @@ func TestSensitiveContentRefusals(t *testing.T) {
|
||||
t.Error("Zhipu's sensitive is not the filter's stop")
|
||||
}
|
||||
}
|
||||
|
||||
// A group's Claude, GPT or Gemini member trusted with its reasoning
|
||||
// (holdsThinking, #4166) lets it through as it comes, and what ends the
|
||||
// try before a word of the answer still goes to the next member, as it
|
||||
// did while the reasoning was held (#248): a refusal and a retryable
|
||||
// error alike, whose answer follows the reasoning in the same stream, as
|
||||
// one reply, for Claude Code, Codex and a Chat client. An error the next
|
||||
// member wouldn't answer otherwise (a request at fault) is the agent's.
|
||||
func TestTrustedThinkingFailsOverOnEveryPath(t *testing.T) {
|
||||
chatAnswered := sse(
|
||||
`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"hm"}}]}`,
|
||||
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":"from a"}}]}`,
|
||||
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":5,"total_tokens":8}}`,
|
||||
`data: [DONE]`)
|
||||
chatThought := strings.Join(strings.SplitAfter(glmThoughtThenSensitive, "\n\n")[:2], "")
|
||||
anthropicThought := strings.Join(strings.SplitAfter(glmAnthropicThoughtThenRefused, "\n\n")[:4], "")
|
||||
anthropicError := func(typ, msg string) string {
|
||||
return anthropicThought + sse(`event: error`+"\n"+`data: {"type":"error","error":{"type":"`+typ+`","message":"`+msg+`"}}`)
|
||||
}
|
||||
for _, up := range []struct {
|
||||
name, model string
|
||||
proto provider.Protocol
|
||||
answered string
|
||||
refused string
|
||||
overloaded string
|
||||
invalid string
|
||||
}{
|
||||
{"claude", "claude-opus-5-5", provider.Anthropic, anthropicThoughtThenAnswered, glmAnthropicThoughtThenRefused,
|
||||
anthropicError("overloaded_error", "Overloaded"), anthropicError("invalid_request_error", "invalid tool schema")},
|
||||
{"gpt", "gpt-5.5", provider.Chat, chatAnswered, chatThoughtThenFiltered,
|
||||
chatThought + sse(`data: {"error":{"message":"Overloaded","type":"server_error"}}`),
|
||||
chatThought + sse(`data: {"error":{"message":"invalid tool schema","type":"invalid_request_error"}}`)},
|
||||
{"gemini", "gemini-3-pro", provider.Chat, chatAnswered, chatThoughtThenFiltered,
|
||||
chatThought + sse(`data: {"error":{"message":"Overloaded","type":"server_error"}}`),
|
||||
chatThought + sse(`data: {"error":{"message":"invalid tool schema","type":"invalid_request_error"}}`)},
|
||||
} {
|
||||
for _, end := range []string{"refused", "overloaded", "invalid"} {
|
||||
for _, client := range []struct {
|
||||
name, path, ask string
|
||||
check func(t *testing.T, body string)
|
||||
}{
|
||||
{"messages", "/v1/messages", claudeCodeAsk, checkAnthropicSpliced},
|
||||
{"chat", "/v1/chat/completions", chatStreamAsk, checkChatSpliced},
|
||||
{"responses", "/v1/responses", codexAsk, checkResponsesSpliced},
|
||||
} {
|
||||
t.Run(up.name+" "+end+" to "+client.name, func(t *testing.T) {
|
||||
fresh(t)
|
||||
last := map[string]string{"refused": up.refused, "overloaded": up.overloaded, "invalid": up.invalid}[end]
|
||||
a := &scripted{replies: []reply{
|
||||
{200, "text/event-stream", up.answered},
|
||||
{200, "text/event-stream", up.answered},
|
||||
{200, "text/event-stream", up.answered},
|
||||
{200, "text/event-stream", last},
|
||||
}}
|
||||
scriptedOn(t, "a", up.proto, a)
|
||||
pa, err := provider.Find("a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pa.Models = []string{up.model}
|
||||
if err := provider.Save(*pa); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
||||
scriptedOn(t, "b", provider.Anthropic, b)
|
||||
refusalGroup(t, "a/"+up.model, "b/m")
|
||||
s := New()
|
||||
for i := range thoughtCleanMost {
|
||||
if code, body := sendTo(s, client.path, client.ask); code != 200 || !strings.Contains(body, "from a") {
|
||||
t.Fatalf("warm-up %d: %d %s (a %d)", i+1, code, body, a.n)
|
||||
}
|
||||
}
|
||||
code, body := sendTo(s, client.path, client.ask)
|
||||
// trusted: the reasoning went through as it came
|
||||
if code != 200 || a.n != thoughtCleanMost+1 || !strings.Contains(body, "first thought") {
|
||||
t.Fatalf("%d (a %d, b %d): %s", code, a.n, b.n, body)
|
||||
}
|
||||
r := lastRoute(s)
|
||||
if end == "invalid" {
|
||||
if b.n != 0 || !strings.Contains(body, "invalid tool schema") || len(r.Tries) != 1 {
|
||||
t.Fatalf("a request at fault isn't the agent's (b %d): %s %+v", b.n, body, r.Tries)
|
||||
}
|
||||
return
|
||||
}
|
||||
if b.n != 1 || !strings.Contains(body, "from b") {
|
||||
t.Fatalf("the next member didn't answer (b %d): %s", b.n, body)
|
||||
}
|
||||
for _, w := range []string{"content_filter", `"refusal"`, "Overloaded", "overloaded"} {
|
||||
if strings.Contains(body, w) {
|
||||
t.Fatalf("the failure (%s) reached the agent: %s", w, body)
|
||||
}
|
||||
}
|
||||
client.check(t, body)
|
||||
if len(r.Tries) != 2 || r.Tries[1].Fail != "" {
|
||||
t.Fatalf("tries: %+v", r.Tries)
|
||||
}
|
||||
if end == "refused" && (r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil) {
|
||||
t.Fatalf("refused try: %+v", r.Tries[0])
|
||||
}
|
||||
if end == "overloaded" && r.Tries[0].Status != 529 {
|
||||
t.Fatalf("overloaded try: %+v", r.Tries[0])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,6 +307,190 @@ func TestDeepSeekRefusalHoldsATrustedMemberAgain(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A group's Claude member is trusted as a DeepSeek one is: once its
|
||||
// thinking has gone on to an answer thoughtCleanMost times running, its
|
||||
// reasoning goes as it comes while a refusal after it is still handed to
|
||||
// the next member in the same stream (refusalHandover). Held, Claude Code
|
||||
// on a group of one model's accounts saw the thinking only with the text,
|
||||
// all at once, and never its "thinking with <effort> effort" (#4166). A
|
||||
// refusal takes the trust back, as DeepSeek's does.
|
||||
func TestClaudeThinkingStreamsOnceTrusted(t *testing.T) {
|
||||
fresh(t)
|
||||
a := &scripted{replies: []reply{
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", anthropicThoughtThenRefused},
|
||||
{200, "text/event-stream", anthropicThoughtThenRefused},
|
||||
}}
|
||||
scriptedOn(t, "a", provider.Anthropic, a)
|
||||
pa, err := provider.Find("a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pa.Models = []string{"claude-opus-5-5"}
|
||||
if err := provider.Save(*pa); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}, {200, "text/event-stream", anthropicAnswer}}}
|
||||
scriptedOn(t, "b", provider.Anthropic, b)
|
||||
refusalGroup(t, "a/claude-opus-5-5", "b/m")
|
||||
s := New()
|
||||
if !s.holdsThinking(candidate{p: *pa, model: "claude-opus-5-5"}, true) {
|
||||
t.Fatal("an untried Claude member's reasoning isn't held")
|
||||
}
|
||||
for i := 0; i < thoughtCleanMost; i++ {
|
||||
if code, body := sendTo(s, "/v1/messages", claudeCodeAsk); code != 200 || !strings.Contains(body, "from a") || strings.Contains(body, "from b") {
|
||||
t.Fatalf("warm-up %d: %d %s (a %d, b %d)", i+1, code, body, a.n, b.n)
|
||||
}
|
||||
}
|
||||
// trusted only where a refusal can still be handed over: where it
|
||||
// can't, it would reach the agent and end the turn with nothing said
|
||||
if s.holdsThinking(candidate{p: *pa, model: "claude-opus-5-5"}, true) {
|
||||
t.Fatal("a trusted Claude member's reasoning is still held")
|
||||
}
|
||||
if !s.holdsThinking(candidate{p: *pa, model: "claude-opus-5-5"}, false) {
|
||||
t.Fatal("a trusted Claude member's reasoning goes through with no handover behind it")
|
||||
}
|
||||
// trusted, the refused reply's reasoning went through as it came, and
|
||||
// the refusal after it, nothing yet said, went to the next member,
|
||||
// whose answer follows in the same stream, as one reply
|
||||
code, body := sendTo(s, "/v1/messages", claudeCodeAsk)
|
||||
if code != 200 || !strings.Contains(body, "The user wants a plan for") || !strings.Contains(body, "from b") || strings.Contains(body, `"refusal"`) ||
|
||||
strings.Count(body, "event: message_start") != 1 || a.n != thoughtCleanMost+1 || b.n != 1 {
|
||||
t.Fatalf("trusted refusal: %d %s (a %d, b %d)", code, body, a.n, b.n)
|
||||
}
|
||||
if r := lastRoute(s); len(r.Tries) != 2 || r.Tries[0].Fail != failRefused || r.Tries[0].Rest != nil || r.Tries[1].Fail != "" {
|
||||
t.Fatalf("tries: %+v", r.Tries)
|
||||
}
|
||||
// and the member is held again: the same refusal goes to the next
|
||||
// member with its reasoning shown to nobody
|
||||
code, body = sendTo(s, "/v1/messages", claudeCodeAsk)
|
||||
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, "The user wants a plan for") || a.n != thoughtCleanMost+2 || b.n != 2 {
|
||||
t.Fatalf("held again: %d %s (a %d, b %d)", code, body, a.n, b.n)
|
||||
}
|
||||
}
|
||||
|
||||
// Trusted, a group's Claude member's thinking reaches the agent as it
|
||||
// comes, the text a good while after the first thought (#4166).
|
||||
func TestClaudeTrustedThinkingStreamsThroughGroup(t *testing.T) {
|
||||
fresh(t)
|
||||
a := &scripted{replies: []reply{
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
}}
|
||||
scriptedOn(t, "a", provider.Anthropic, a)
|
||||
pa, err := provider.Find("a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pa.Models = []string{"claude-opus-5-5"}
|
||||
if err := provider.Save(*pa); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
scriptedOn(t, "b", provider.Anthropic, &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}})
|
||||
refusalGroup(t, "a/claude-opus-5-5", "b/m")
|
||||
s := New()
|
||||
for i := 0; i < thoughtCleanMost; i++ {
|
||||
if code, body := sendTo(s, "/v1/messages", claudeCodeAsk); code != 200 || !strings.Contains(body, "from a") {
|
||||
t.Fatalf("warm-up %d: %d %s (a %d)", i+1, code, body, a.n)
|
||||
}
|
||||
}
|
||||
up := &slowThinker{events: glmThinksSlowly, gap: 200 * time.Millisecond, done: make(chan time.Time, 1)}
|
||||
vendor := httptest.NewServer(up)
|
||||
t.Cleanup(vendor.Close)
|
||||
pa, err = provider.Find("a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pa.Anthropic = vendor.URL
|
||||
if err := provider.Save(*pa); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
gw := httptest.NewServer(s.Handler())
|
||||
t.Cleanup(gw.Close)
|
||||
res, err := http.Post(gw.URL+"/v1/messages", "application/json", strings.NewReader(claudeCodeAsk))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
var firstThought, firstText time.Time
|
||||
rd := bufio.NewReader(res.Body)
|
||||
for {
|
||||
ln, err := rd.ReadString('\n')
|
||||
if firstThought.IsZero() && strings.Contains(ln, `"thinking_delta"`) {
|
||||
firstThought = time.Now()
|
||||
}
|
||||
if firstText.IsZero() && strings.Contains(ln, `"text_delta"`) {
|
||||
firstText = time.Now()
|
||||
}
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-up.done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("the vendor never finished")
|
||||
}
|
||||
if firstThought.IsZero() || firstText.IsZero() {
|
||||
t.Fatal("no thinking or no text reached the client")
|
||||
}
|
||||
// three thoughts 200ms apart come before the text
|
||||
if firstText.Sub(firstThought) < 300*time.Millisecond {
|
||||
t.Fatalf("thinking held: first thought only %v before the text", firstText.Sub(firstThought))
|
||||
}
|
||||
if r := lastRoute(s); len(r.Tries) != 1 {
|
||||
t.Fatalf("the trusted member's reply was asked again: %+v", r.Tries)
|
||||
}
|
||||
}
|
||||
|
||||
// Trusted, a group's Claude member whose stream fails after its thinking
|
||||
// went through, nothing yet said (Anthropic's overloaded_error), still
|
||||
// fails over: the next member answers in the same stream, as one reply,
|
||||
// as it did while the thinking was held (#4166). Let through, the error
|
||||
// reached Claude Code and the group's next member was never asked.
|
||||
func TestClaudeTrustedThinkingErrorFailsOver(t *testing.T) {
|
||||
fresh(t)
|
||||
overloaded := sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m1","role":"assistant","model":"m","content":[],"usage":{"input_tokens":2}}}`,
|
||||
`event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":"","signature":""}}`,
|
||||
`event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":"The user wants a plan for"}}`,
|
||||
`event: error`+"\n"+`data: {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}`)
|
||||
a := &scripted{replies: []reply{
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", anthropicThoughtThenAnswered},
|
||||
{200, "text/event-stream", overloaded},
|
||||
}}
|
||||
scriptedOn(t, "a", provider.Anthropic, a)
|
||||
pa, err := provider.Find("a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pa.Models = []string{"claude-opus-5-5"}
|
||||
if err := provider.Save(*pa); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := &scripted{replies: []reply{{200, "text/event-stream", anthropicAnswer}}}
|
||||
scriptedOn(t, "b", provider.Anthropic, b)
|
||||
refusalGroup(t, "a/claude-opus-5-5", "b/m")
|
||||
s := New()
|
||||
for i := 0; i < thoughtCleanMost; i++ {
|
||||
if code, body := sendTo(s, "/v1/messages", claudeCodeAsk); code != 200 || !strings.Contains(body, "from a") {
|
||||
t.Fatalf("warm-up %d: %d %s (a %d)", i+1, code, body, a.n)
|
||||
}
|
||||
}
|
||||
code, body := sendTo(s, "/v1/messages", claudeCodeAsk)
|
||||
if code != 200 || !strings.Contains(body, "from b") || strings.Contains(body, "Overloaded") ||
|
||||
strings.Count(body, "event: message_start") != 1 || b.n != 1 {
|
||||
t.Fatalf("error after trusted thinking: %d %s (a %d, b %d)", code, body, a.n, b.n)
|
||||
}
|
||||
if r := lastRoute(s); len(r.Tries) != 2 || r.Tries[0].Status != 529 || r.Tries[1].Fail != "" {
|
||||
t.Fatalf("tries: %+v", r.Tries)
|
||||
}
|
||||
}
|
||||
|
||||
// A try that is asked again asks the same fake vendor again: a reply
|
||||
// that only reasoned is asked again in place (#667), and a full run's
|
||||
// state may have any ask retried. The fake's done holds one send, and
|
||||
|
||||
Reference in New Issue
Block a user