From 949000a36a3cb87fcdfc32548e29ef4ea5f4e499 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 22:12:32 +0800 Subject: [PATCH] claude: a request the interactive mode refuses is a 400 no account rests for MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A request Claude Code's interactive mode can't take (a JSON schema, a branch of a -p run's session, WSL Claude Code whose home wasn't found) was refused with a 502. The fallback asked every account of the subscription in turn and rested some for it: #4778 saw 15 tries across 3 accounts. The refusal is the same on every account, so it is now a 400 (tuiRefusal), which the fallback neither retries nor rests an account for. The message is reworded so that "Models" no longer stands just before the error's "invalid_request_error": "Settings › Models › Replies runs every request as -p\",\"type\":\"invalid_request_error" matched provider.ModelUnserved, which made the 400 retryable again. Run (mote, at this tree): go vet ./internal/gateway, GOOS=windows and GOOS=linux -tags nogui go vet; go test -run "TUI|Claude|Resume|Interactive|Refusal|Fallback" ./internal/gateway ok. gofmt -l clean. Broken by hand: with the 400 mapping taken out, TestClaudeTUIRefusalIsNoAccountsFault is red (502); with the old wording it was red on WSL (retryable true through ModelUnserved). Not tried: a refusal against real accounts. Not changed: a run that starts and fails (tuiNoStart) is still a 502, retried on the next account. Part of #4778. --- docs/subsystems/claude-subscription-bridge.md | 11 ++++++-- internal/gateway/claude_subscription.go | 22 +++++++++++++--- internal/gateway/claude_tui_refusal_test.go | 26 +++++++++++++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 internal/gateway/claude_tui_refusal_test.go diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 2078a5f44..80ec57898 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -261,8 +261,15 @@ plan counts a `-p` request as another client's, at about twice the interactive mode's, and a `-p` run starts without the interactive mode's cache. A run the interactive mode can't take is refused before any Claude Code starts (`startRun`), and the request gets an error naming why and -the setting (`claudeTUIErr`, `claudeNotTUIWhy`). That is when -(`claudeTUI`, `claudePrint`): +the setting (`claudeTUIErr`, `claudeNotTUIWhy`). The refusal is a 400 +(`tuiRefusal`): it is about the request or this computer, the same on +every account, so the fallback doesn't ask the next account and no +account rests for it. Until 11-01 it was a 502, which every account was +asked in turn (#4778: 15 tries across 3 accounts, some rested). Its +words avoid "Models" just before the error's type: "Models › +Replies…\",\"type\":\"invalid_request_error" read as a model not served +(`provider.ModelUnserved`), which made the 400 retryable again. That is +when (`claudeTUI`, `claudePrint`): - it is Claude Code in WSL whose home the probe didn't find, or, in the distro user's own folder, one Windows can't read (`wslread`); diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index ded95f88c..af58e4e31 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -752,7 +752,8 @@ func (b *subscriptionBridge) start(ctx context.Context, req *Request, model, con } var full *claudeFull var refused *cliSchemaRefused - if err != nil && from != nil && !errors.As(err, &full) && !errors.As(err, &refused) && ctx.Err() == nil { + var tui *tuiRefusal + if err != nil && from != nil && !errors.As(err, &full) && !errors.As(err, &refused) && !errors.As(err, &tui) && ctx.Err() == nil { log.Printf("claude: Claude Code could not go on from the conversation's saved session (%v); a new one is told the whole conversation", err) req.branch = nil return b.startRun(ctx, req, model, configDir, owner, nil) @@ -943,7 +944,7 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, } if !tui && code != "off" && code != "no-shim" { cleanup() - return nil, nil, claudeTUIErr(claudeNotTUIWhy(req, binary, configDir)) + return nil, nil, &tuiRefusal{claudeTUIErr(claudeNotTUIWhy(req, binary, configDir))} } noteClaudeRun(code) var cmd *exec.Cmd @@ -1365,9 +1366,21 @@ func claudeNotTUIWhy(req *Request, binary claudeCLI, configDir string) string { // the plan counts as another client's request and which starts without // the interactive mode's cache. func claudeTUIErr(why string) error { - return fmt.Errorf("Claude Code's interactive mode can't answer this request (%s), and magpie never runs it as claude -p instead; turning the interactive mode off in Settings › Models › Replies runs every request as -p", why) + // the settings come first: "Models" close before the error's + // "invalid_request_error" read as a model the account doesn't serve + // (provider.ModelUnserved), and the next account was asked + return fmt.Errorf("Claude Code's interactive mode, on in Settings › Models › Replies, can't answer this request (%s), and magpie never runs it as claude -p instead; turned off there, every request runs as -p", why) } +// tuiRefusal is a request the interactive mode refuses for what it asks, +// or for this computer: the same on every account, and no account's +// fault. It is answered 400, which no account rests for and which isn't +// asked of the next: a 502 was asked of every account in turn, 15 tries +// across 3, and rested some of them (#4778). +type tuiRefusal struct{ error } + +func (e *tuiRefusal) Unwrap() error { return e.error } + // cliControl shares the SDK reply dispatcher with rewind, so unrelated or // late control responses cannot acknowledge a safety-context update. func (r *subscriptionRun) cliControl(request map[string]any) error { @@ -5636,9 +5649,10 @@ func (s *Server) serveSubscription(w http.ResponseWriter, r *http.Request, from code := 502 var full *claudeFull var refused *cliSchemaRefused + var tui *tuiRefusal if errors.As(err, &full) { code = 503 - } else if errors.As(err, &refused) { + } else if errors.As(err, &refused) || errors.As(err, &tui) { code = 400 } return writeError(w, from, code, name+": "+err.Error()), err.Error() diff --git a/internal/gateway/claude_tui_refusal_test.go b/internal/gateway/claude_tui_refusal_test.go new file mode 100644 index 000000000..ff5f41f05 --- /dev/null +++ b/internal/gateway/claude_tui_refusal_test.go @@ -0,0 +1,26 @@ +package gateway + +import ( + "net/http/httptest" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// A request the interactive mode refuses is refused the same on every +// account: it is a 400 no account rests for and the next isn't asked, +// where a 502 was asked of every account in turn and rested some (#4778). +func TestClaudeTUIRefusalIsNoAccountsFault(t *testing.T) { + h, _ := tuiHelperHarness(t, "") + body := `{"model":"claude-sonnet-5","max_tokens":100,"messages":[{"role":"user","content":"sky?"}],"output_config":{"format":{"type":"json_schema","schema":{"type":"object","properties":{"color":{"type":"string"}}}}}}` + var u Usage + rec := httptest.NewRecorder() + code, msg := h.s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, h.p, "claude-sonnet-5", []byte(body), &u) + if code != 400 || !strings.Contains(msg, "a JSON schema") { + t.Fatalf("%d %.300s", code, msg) + } + if retryable(code, rec.Body.Bytes()) { + t.Fatalf("the refusal is asked of the next account: %s", rec.Body.String()) + } +}