gateway: a refused big conversation isn't told again to the Claude subscription's other accounts (Aisiverto on X)

Aisiverto on X: a refused turn of a ~900K-token conversation made magpie
switch accounts and send it again, "one user action becomes several
expensive requests". In the usage, pairs ~100ms apart: one read the whole
conversation from the cache, the other read ~26K and wrote ~877K.

The cause: a refusal goes to the next candidate under RefusalRetries
(unset: every one left), and matesFirst puts the same model's other
accounts first (#248, where a verified account may answer). For a Claude
subscription each other account is a new Claude Code told the whole
conversation, all of it written to that account's cache again, for a
refusal about the content that it most likely gives too.

Now, before a Claude subscription account's try, a conversation of
retellMost (100K) tokens or more by estimate (tooBigToRetell) leaves the
subscription's other accounts out if that account refuses
(otherThanClaudeAccounts). Other members are still asked; with none left
the refusal is the agent's, and it isn't handed over mid-stream to
nobody (handsOver is given last). A smaller conversation goes to the next
account as before. The size is measured only when another Claude
subscription account is left. gateway-routing.md says so beside
RefusalRetries.

Run:
- TestClaudeSubscriptionBigRefusalNotToldAgain (new; streamed and not):
  red on the old code (runs "own,tok-spare"), green with the fix, the
  agent getting the refusal. TestClaudeSubscriptionRefusalMovesToNextAccount
  (small conversation, spare still asked), TestRefusalRetries* and
  TestPolicyRefusal* green.
- Broken by hand: tooBigToRetell always false, red.
- On mote: gofmt -l clean; go vet, GOOS=linux go vet -tags nogui and
  GOOS=windows go vet of internal/gateway ok; go test -tags nogui
  -timeout 45m ./internal/gateway: ok (702s). The whole suite ran at the
  parent commit (its body); this commit touches internal/gateway only.

Not tried:
- The handsOver change: the test's refusal comes with no reasoning
  before it, so nothing is handed over, and taking the change out stays
  green. Not covered by a test.
- A large conversation where another member of the group is a Claude
  subscription of a different provider id: it is left out too, as every
  Claude subscription account is.
- A real Claude refusal on a real account.
This commit is contained in:
yetone
2026-11-01 14:28:40 +08:00
parent 2af4d8c62d
commit a5a9cdf7be
4 changed files with 85 additions and 2 deletions
File diff suppressed because one or more lines are too long
+34
View File
@@ -1927,6 +1927,40 @@ func matesFirst(left []candidate, c candidate) {
copy(left, append(mates, others...))
}
// retellMost is the size, in tokens, of a refused conversation past which
// another Claude subscription account isn't told it again: each is a new
// Claude Code that writes the whole of it to that account's cache.
const retellMost = 100_000
// claudeAccount is whether c is a Claude subscription account, served
// through Claude Code (serveClaudeSubscription).
func (c candidate) claudeAccount() bool {
return c.p.Account != nil && c.p.Account.Agent == "claude"
}
// tooBigToRetell is whether a conversation is too big to be told again to
// another Claude subscription account after one refused it (retellMost),
// and its size in tokens as estimate counts it.
func tooBigToRetell(from provider.Protocol, body []byte) (int, bool) {
req, err := parse(from, body)
if err != nil {
return 0, false
}
n := estimate(req)
return n, n >= retellMost
}
// otherThanClaudeAccounts is left without its Claude subscription accounts.
func otherThanClaudeAccounts(left []candidate) []candidate {
var kept []candidate
for _, x := range left {
if !x.claudeAccount() {
kept = append(kept, x)
}
}
return kept
}
// matesLast puts last, of the candidates left, the other keys or accounts
// of the member c is of: what turns one of them away is in the request, so
// it turns them all away the same, and the group is better spent on a
+19 -1
View File
@@ -2909,7 +2909,15 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// waited every request's whole thinking out.
hw.thinkingShown = !s.holdsThinking(c) || last
hw.ctx, hw.alive, hw.streams = r.Context(), kept, streams
if handsOver(streams, last, wantWhole, from, refusals) {
// a Claude subscription account's refusal of a big conversation
// isn't told again to the subscription's other accounts
// (tooBigToRetell), decided before the try, for its refusal not to
// be handed over to nobody
retell, noRetell := 0, false
if !last && c.claudeAccount() && slices.ContainsFunc(cands[i+1:], candidate.claudeAccount) {
retell, noRetell = tooBigToRetell(from, body)
}
if handsOver(streams, last || noRetell && len(otherThanClaudeAccounts(cands[i+1:])) == 0, wantWhole, from, refusals) {
// reasoning that went through as it came, and a refusal after
// it with nothing yet said: the next candidate answers in the
// same stream (refusalHandover; yhwangl on X)
@@ -3552,6 +3560,16 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// refusal be: nobody else is, and the agent has it now
cands = cands[:i+1]
last = true
} else if noRetell {
// another Claude subscription account is a new Claude Code
// told the whole conversation, a cache written again on that
// account, for a refusal about the request's content that it
// most likely gives too: a 900K conversation refused cost a
// Pro account most of its 5 hours (Aisiverto on X). Other
// members are still asked
cands = append(cands[:i+1], otherThanClaudeAccounts(cands[i+1:])...)
last = i == len(cands)-1
log.Printf("claude: %s refused a conversation of ~%dK tokens; the subscription's other accounts aren't told it again", c.label(), retell/1000)
}
}
if !last && hw.failed() && hw.refused {
+31
View File
@@ -1,6 +1,7 @@
package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
@@ -307,6 +308,36 @@ func TestClaudeSubscriptionRefusalMovesToNextAccount(t *testing.T) {
}
}
// A Claude subscription account refusing a conversation of ~110K tokens:
// the spare account isn't asked, as it would be a new Claude Code told the
// whole conversation, all of it written to that account's cache again, for
// a refusal about the content it most likely gives too. A 900K
// conversation refused on one Pro account and told again on the next cost
// the user most of a 5-hour window (Aisiverto on X). The refusal is the
// agent's, the one try on the Routing page.
func TestClaudeSubscriptionBigRefusalNotToldAgain(t *testing.T) {
log := claudeSubscriptionRefusing(t)
s := New()
big := strings.Repeat("the conversation so far ", 440_000/24)
for _, stream := range []bool{true, false} {
os.Remove(log)
body, _ := json.Marshal(map[string]any{"model": "claude/claude-sonnet-5", "max_tokens": 50, "stream": stream,
"messages": []map[string]any{{"role": "user", "content": big}}})
code, reply := sendTo(s, "/v1/messages", string(body))
b, _ := os.ReadFile(log)
if runs := strings.Join(strings.Fields(string(b)), ","); runs != "own" {
t.Fatalf("stream %v: runs %q, want the refusing account's alone (status %d)", stream, runs, code)
}
// the agent is told the refusal, not an empty reply
if strings.Contains(reply, "pong") || !strings.Contains(reply, "refusal") && !strings.Contains(reply, "Usage Policy") {
t.Fatalf("stream %v: status %d: %s", stream, code, reply)
}
if r := lastRoute(s); len(r.Tries) != 1 {
t.Fatalf("stream %v: tries: %+v", stream, r.Tries)
}
}
}
// Which errors are the safety filter's, and which are the request's own.
func TestPolicyRefusalShapes(t *testing.T) {
for body, want := range map[string]bool{