magpie icon indicating copy to clipboard operation
magpie copied to clipboard

gateway: ZCode 405 (code 3012, failRestricted) aborts at tries=1 without failing over because retryable() omits failRestricted / 405

Open ArmedHelicopter opened this issue 1 week ago • 2 comments

Environment

  • magpie version: 0.1.4777 (Windows 11 amd64)
  • Where: Gateway (group/agentcompass, order routing)
  • Provider / Model: zcode/GLM-5.4 (ZCode plugin)

What happened

When zcode/GLM-5.4 returned 405 (code 3012 — Start Plan account suspended), Magpie did not fail over to any of the other providers in the routing group (TRIES: 1), and returned the 405 directly to Codex.

This happened repeatedly in routing/2026-11-25.jsonl (e.g. 08:47:42, 09:47:15, 10:47:37, ..., 18:16:38):

{
  "time": "2026-11-25T18:16:38.4822686-08:00",
  "status": 405,
  "model": "group/agentcompass",
  "tries": [
    {
      "model": "GLM-5.4",
      "status": 405,
      "error": "ZCode: Your account has been suspended for misusing the Start Plan, such as proxying it to non-ZCode clients. Please contact support if you believe this is a mistake. (405, code 3012) — ZCode's risk control blocked this account for unusual activity (code 3012); it rests, and the group's other providers are used. If the ZCode app fails on this account too, only ZCode support can lift the block"
    }
  ]
}

Notice the error message itself says "it rests, and the group's other providers are used", yet tries has length 1 and no other provider in group/pool-code / group/agentcompass was tried.

Root Cause in Source

  1. internal/gateway/routing.go:599-600 (failure) classifies restrictedSaid(405, body) (matching provider.ZCodeRestrictedHint) as failRestricted, so ZCode rests for restrictedRest (2 * time.Hour, routing.go:283).
  2. However, retryable(status int, body []byte) in internal/gateway/fallback.go:2162-2205:
    • Lists 401, 402, 403, 404, 408, 429, >= 500, modelRetired, provider.EdgeBlocked, failCredit, claudeOffWords, refusedWords — but omits 405 and omits restrictedSaid / failRestricted!
    • As a result, retryable(405, body) returns false, immediately aborting the fallback loop at TRIES: 1 instead of trying the next provider in the group.
  3. Additionally, for a permanent/account-suspension 3012 (account has been suspended), a flat 2-hour restrictedRest causes it to wake up and fail with 405 (TRIES: 1) again every 2 hours.

Expected Behavior

  1. retryable(status, body) in internal/gateway/fallback.go should return true when restrictedSaid(status, body) (or failure(status, raw) == failRestricted) is true (so 405 from ZCode 3012 / WorkBuddy 11140 actually fails over to the group's other providers as the error text promises).
  2. Consider a longer or escalating rest (like creditLadder) for failRestricted when the vendor explicitly reports account suspension (code 3012).

ArmedHelicopter avatar Nov 26 '26 02:11 ArmedHelicopter

Fix branch: https://github.com/qq01-hub/magpie/tree/fix/restricted-4xx-failover (dec5bb5d3)

Root cause: ZCode's newer 405 (code 3012) suspension message no longer contains "request has been blocked", so provider.EdgeBlocked returns false, and retryable() did not check restrictedSaid(). That caused holdWriter.failed() to return false (aborting routing groups at tries=1) and decideFailed() to skip resting.

Changes:

  • Add case restrictedSaid(status, body): return true in internal/gateway/fallback.go (retryable), keeping riskRefused() skipping sibling accounts of the restricted provider and keeping plain 405 / 200 non-retryable.
  • Decode body with wordsOf() in restrictedRefused() (internal/gateway/routing.go) so JSON-escaped hints match consistently with failure() and retryable().
  • Add TestRestricted4xxFailsOver in internal/gateway/zcode_blocked_test.go.

Verification (Windows 11 amd64, Go 1.26.8):

  • Before fix: TestRestricted4xxFailsOver failed (retryable(405, ZCodeRestrictedHint) = false).
  • After fix: targeted suite (TestRestricted4xxFailsOver|TestZCodeStartPlanBlockRestsRestricted|TestZCodeRiskControlNotToldAsFirewall|TestRiskRefusal|TestWorkBuddyRestricted|TestOnlyWorkBuddyRestricted|TestRestrictedLoneAccount|TestSuspendedAccounts|TestDecide) passed -count=5; go vet passed on Windows/Linux/macOS (-race not run, no cgo compiler locally).

qq01-hub avatar Nov 26 '26 03:11 qq01-hub

Fixed in v0.1.4840. A group now fails over when ZCode answers 405 with code 3012.

ZCode's newer suspension text ("Your account has been suspended for misusing the Start Plan…") no longer says "request has been blocked", so retryable() didn't treat the 405 as one another provider could take. Now any 4xx that carries magpie's restricted-account hint is retryable:

  • the ZCode account rests as restricted;
  • the group's next member answers;
  • ZCode's other accounts are still skipped for that request, since the vendor's risk control reads the request too.

A plain 405 still goes back to the agent.

Thank you @qq01-hub for the fix and the root cause. Your commit (36b76f775) landed as you wrote it, under your name. A follow-up (da02781fa) adds a test of this exact case through the ZCode plugin, with your trace's words. That test fails on the old code, and it also fails with the new retryable case removed by hand.

Not changed: your third point, a longer rest for a suspended account. It still rests 2 hours, so every 2 hours one request tries it once and then fails over.

@ArmedHelicopter, I'll leave this open until it works for you on group/agentcompass. After updating, the routing log should show two tries for these requests, not one.

yetone avatar Nov 26 '26 08:11 yetone