Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(errors): advance past an opencode-zen-go 401 for an uncarried model

Jack Rudenko5d57a9e2ec6d
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index cc8c2469..6af16953 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -90,6 +90,7 @@ import {
import {
authVerdictHeaders,
classifyAuthFailure,
+ isCredentialRejection,
respondAuthFailure,
} from "./shared/auth-failure.js";
import { sseResponseToJson } from "./shared/collect-sse-message.js";
@@ -1875,7 +1876,13 @@ export class ComposedHandler implements ModelHandler {
retryResp.status,
errorText
);
- if (isSubscriptionProvider(this.provider.name) || transportTerminal === true) {
+ // The subscription stop is for a credential rejection only; a model
+ // or allowance miss keeps its pass-through route (`isCredentialRejection`).
+ if (
+ (isSubscriptionProvider(this.provider.name) &&
+ isCredentialRejection(retryResp.status, errorText)) ||
+ transportTerminal === true
+ ) {
const hint = getRecoveryHint(
retryResp.status,
errorText,
@@ -2141,9 +2148,11 @@ export class ComposedHandler implements ModelHandler {
// (`auth-failure.ts`): the next hop would bill the user per token for a
// login they can fix. A spent allowance (402/429) is not a credential
// failure and still advances, with its warning.
+ // A model the provider does not carry (Zen Go's 401 "Model <id> is not
+ // supported") is NOT a credential rejection and keeps advancing.
const verdictHeaders =
- (response.status === 401 || response.status === 403) &&
- isSubscriptionProvider(this.provider.name)
+ isSubscriptionProvider(this.provider.name) &&
+ isCredentialRejection(response.status, errorText)
? authVerdictHeaders("terminal")
: undefined;
// Carry the ORIGINAL upstream status as a structured field so
diff --git a/packages/cli/src/handlers/fallback-handler.ts b/packages/cli/src/handlers/fallback-handler.ts
index e5fa2199..9838b537 100644
--- a/packages/cli/src/handlers/fallback-handler.ts
+++ b/packages/cli/src/handlers/fallback-handler.ts
@@ -13,7 +13,7 @@ import type { Context } from "hono";
import { logStderr } from "../logger.js";
import { ComposedHandler } from "./composed-handler.js";
import { extractUpstreamStatus } from "./shared/anthropic-error.js";
-import { authVerdict } from "./shared/auth-failure.js";
+import { authVerdict, isCredentialRejection } from "./shared/auth-failure.js";
import { hasQuotaExhaustionWording } from "./shared/quota-exhaustion.js";
import { isClaudishConnectionVerdict, isRecoveryHoldResponse } from "./shared/recovery-marker.js";
import { isSubscriptionProvider } from "./shared/remote-provider-types.js";
@@ -297,7 +297,12 @@ export function isRetryableError(
// Asked of the uid — a display name is never a subscription.
if (providerId && isSubscriptionProvider(providerId)) {
const recovered = status === 400 ? extractUpstreamStatus(errorBody) : undefined;
- if (status === 401 || recovered === 401) return false;
+ // A credential rejection only: a 401 that says the MODEL is not carried
+ // (OpenCode Zen Go's "Model <id> is not supported") is a per-model miss and
+ // advances, as it always did (`isCredentialRejection`).
+ if ((status === 401 || recovered === 401) && isCredentialRejection(401, errorBody)) {
+ return false;
+ }
}
// ── Recover the ORIGINAL status from a remapped terminal error (#148) ──────
diff --git a/packages/cli/src/handlers/shared/auth-failure.ts b/packages/cli/src/handlers/shared/auth-failure.ts
index 2d1ed6e7..2a775d71 100644
--- a/packages/cli/src/handlers/shared/auth-failure.ts
+++ b/packages/cli/src/handlers/shared/auth-failure.ts
@@ -40,6 +40,8 @@ import type { Context } from "hono";
import type { ContentfulStatusCode } from "hono/utils/http-status";
import { wrapAnthropicError } from "./anthropic-error.js";
import { classifyConnectionError } from "./connection-error.js";
+import { hasModelUnsupportedWording } from "./model-unsupported.js";
+import { isQuotaExhaustionError } from "./quota-exhaustion.js";
/** The header name. Referenced by name in no other file — import this. */
export const AUTH_VERDICT_HEADER = "x-claudish-auth-verdict";
@@ -71,6 +73,56 @@ export type AuthFailure =
| { kind: "terminal"; message: string; upstreamStatus?: number }
| { kind: "unknown"; message: string };
+/**
+ * Whether an upstream 401/403 rejects the CREDENTIAL — a dead, revoked or
+ * unsubscribed login — rather than this one request.
+ *
+ * The status alone does not say. Two answers arrive under it that are about
+ * the model or the allowance, and both must keep advancing the routing chain
+ * exactly as they did before the subscription stop existed:
+ *
+ * - a model the provider does not carry. OpenCode Zen Go answers
+ * `401 "Model deepseek-v4-pro-0813 is not supported"` (measured 2026-08-18,
+ * the same key serving `kimi-k3` with 200) and
+ * `401 "Model grok-4.6 is not supported for format oa-compat"`
+ * (ai-docs/reports/grok-46-zen-go-misroute-was-stale-902-20260907.md). A
+ * catalog that drifts behind the plan sends such a model there, and the
+ * next hop serves it. Read with `hasModelUnsupportedWording`, the one
+ * predicate the recovery hint and the probe state already share;
+ * - a spent allowance (`403 "You've reached your usage limit for this
+ * billing cycle"`, Kimi Code), read with `isQuotaExhaustionError`, which
+ * advances with its per-token warning;
+ * - a model the account may not use HERE. Zen Go answers
+ * `403 {"error":{"type":"RegionError","message":"The latest version of this
+ * model is only available hosted in China and requires explicit opt in: …"}}`
+ * (measured 2026-08-19, `model-unsupported.ts`). Read from the provider's
+ * own error TYPE, which names a region rather than a credential.
+ *
+ * Only what is left is a credential verdict that stops the chain.
+ */
+export function isCredentialRejection(status: number, errorBody: string): boolean {
+ if (status !== 401 && status !== 403) return false;
+ if (hasModelUnsupportedWording(errorBody)) return false;
+ if (isQuotaExhaustionError(status, errorBody)) return false;
+ if (/region/i.test(providerErrorType(errorBody) ?? "")) return false;
+ return true;
+}
+
+/**
+ * The provider's own `error.type` in a raw upstream JSON error body. Read at
+ * the sites that hold the upstream body (the generic remap, settleAuthRetry);
+ * claudish's remapped 400 does not carry it, and FallbackHandler's own
+ * subscription rule covers 401 only, so a 403 RegionError advances there too.
+ */
+function providerErrorType(errorBody: string): string | undefined {
+ try {
+ const type = (JSON.parse(errorBody) as { error?: { type?: unknown } })?.error?.type;
+ return typeof type === "string" ? type : undefined;
+ } catch {
+ return undefined;
+ }
+}
+
/** The fields a credential path sets on what it throws. */
interface ErrorFields {
message?: unknown;