magpie icon indicating copy to clipboard operation
magpie copied to clipboard

routing: forbiddenAlike only reads plain 403 — a request-level 5xx still rests every account of the provider in turn

Open MYCF711 opened this issue 1 month ago • 1 comments

What happens

forbiddenAlike / unforbid 已经解决了这类问题的前半段 —— 注释里就是同一个病:

Factory's 403 for a Claude Code wrapper it refused rested every account in turn, and the next requests found them all in backoff (plugins#243)

但判据 plainForbidden 要求 status == 403。当上游把「请求被拒」表达成 5xx(本次现场是插件把上游的 AIError:AI服务异常 归一成 503 upstream_unavailable)时,这条保护整个绕过:

agent A 的坏请求 → provider 的 3 个账号依次 503 → 各自 rest(backoff 递增)
                 → agent B 的正常请求发现这些号都在休息 → 只能用剩下的号或换 provider

路由日志 tries[] 里能看到同一个请求连续打三个不同账号、错误文本逐字相同:

"tries":[
 {"id":"<provider>","status":503,"ms":658,"fail":"other",
  "rest":{"why":"other","status":503,"by":"backoff","failures":1,"key":"<provider>@acct-A"}},
 {"id":"<provider>","status":503,"ms":395,"fail":"other", ... "key":"<provider>@acct-B"},
 {"id":"<provider>","status":503,"ms":532,"fail":"other", ... "key":"<provider>@acct-C"}]

当天该 provider 共 128 次 503 尝试,其中 127 次的错误文本逐字相同(同一个坏请求被反复换号重试)。三个账号各吃一次 503、各进 backoff,而它们本身凭证独立、余额充足。

Why

internal/gateway/routing.go:

func plainForbidden(c candidate, status int, body []byte) bool {
    return status == http.StatusForbidden && failureOf(c, status, body) == failOther && ...
}

internal/gateway/gateway.go 里只有 plain403 && len(alike) > 0 才走 unforbid。

Proposed

新增一条与 plainForbidden 平级的判据:

  • status ∈ {502, 503}
  • failureOf(...) == failOther
  • call.Error 与先前同组账号(同 provider、同 model、同 effort、不同账号)逐字相同
  • 命中则沿用现成的 unforbid 撤回休息、matesLast 把同族账号排后

不建议纳入 504:超时往往意味着「这个请求太重」,换号确实可能更快,不该撤回休息。也不建议放开到所有 5xx 而不比对文本:那会在上游整体故障时让账号一直空转。

Test

仿 rest_all_test.go 造 mock provider:两个 key 对同一请求回相同文本的 503,断言第一个 key 的 rest 被撤回、后续请求仍能打到它;再补一条「两个 key 回不同文本的 503」,断言两者都照常 rest。

环境

magpie 0.1.2901 / Windows。provider 与账号名已用 <provider> / acct-A/B/C 脱敏。

MYCF711 avatar Nov 04 '26 08:11 MYCF711

Implemented already — plainUnavailable in internal/gateway/routing.go covers exactly this: a 500/502/503 whose refusalWords (status + words, ids masked) match another account's in the same request is the request refused, unforbid takes the rest back, and internal/gateway/plain5xx_rest_test.go pins the matrix (alone / two alike + a third answers / nobody left / two in other words / provider down). The comment there names the same 503-AIError case. Closing as fixed.

MYCF711 avatar Nov 04 '26 12:11 MYCF711