Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(kimi-coding): sign kc@ with only its own key after a rejected login
Jack Rudenkod3821f7cbecb
diff --git a/packages/cli/src/auth/credentials/composite-credential.ts b/packages/cli/src/auth/credentials/composite-credential.tsindex b4643fbe..2ab12096 100644--- a/packages/cli/src/auth/credentials/composite-credential.ts+++ b/packages/cli/src/auth/credentials/composite-credential.ts@@ -4,8 +4,10 @@** Used for OAuth-or-API-key providers (Codex, Kimi): the OAuth half is primary,* the API-key half is the fallback. A `fallbackSignal` lets the primary opt into- * a fallback by throwing a sentinel error message (e.g. Kimi throws- * "OAuth_FALLBACK_TO_API_KEY" when its refresh fails and an API key is present).+ * a fallback by throwing a sentinel error message (Kimi throws+ * "OAuth_FALLBACK_TO_API_KEY" when its login is rejected). The composite falls+ * through only when its fallback half is available, and otherwise rethrows the+ * sentinel's `cause` — the primary's own terminal error.*/import type {@@ -122,7 +124,15 @@ export class CompositeCredentialProvider implements CredentialProvider {} catch (e: any) {const signal = this.opts.fallbackSignal;if (signal && String(e?.message) === signal) {- return this.fallback.getRequestAuth(ctx);+ // Fall through ONLY to a fallback that has a credential of its own:+ // the api-key half always returns an artifact, so falling through+ // without asking sent an empty key (`x-api-key: ""`) and lost the+ // primary's message. With no key, the signal's cause (the primary's+ // own terminal error) is what the caller gets.+ if (await this.fallback.isAvailable({ allowOpPrompt: ctx.allowOpPrompt })) {+ return this.fallback.getRequestAuth(ctx);+ }+ throw e?.cause ?? e;}throw e;}diff --git a/packages/cli/src/auth/credentials/kimi-credential.ts b/packages/cli/src/auth/credentials/kimi-credential.tsindex 2209fa8b..758be7a3 100644--- a/packages/cli/src/auth/credentials/kimi-credential.ts+++ b/packages/cli/src/auth/credentials/kimi-credential.ts@@ -12,9 +12,13 @@* primary, but with DIFFERENT API-key fallback halves keyed on each product's* own env var. They MUST stay separate: aliasing kimi-coding onto the regular* Kimi credential makes the coding endpoint receive the regular Moonshot key- * (the wrong product's key) → 401. The OAuth refresh throws- * "OAuth_FALLBACK_TO_API_KEY" when it fails and an API key is present — the- * composite's `fallbackSignal` catches exactly that and falls through.+ * (the wrong product's key) → 401. When the token host REJECTS the login, the+ * OAuth refresh throws "OAuth_FALLBACK_TO_API_KEY" with the login instruction+ * as its cause. Each composite's `fallbackSignal` catches it and falls through+ * ONLY when its own product's key is present (kc@: KIMI_CODING_API_KEY, the+ * same flat-rate plan; kimi@: MOONSHOT_API_KEY / KIMI_API_KEY); otherwise the+ * caller gets the cause, "run `claudish login kimi`". A Moonshot key never+ * signs kc@.*/import { KimiOAuth } from "../kimi-oauth.js";diff --git a/packages/cli/src/auth/kimi-oauth.ts b/packages/cli/src/auth/kimi-oauth.tsindex 72263494..4414ef98 100644--- a/packages/cli/src/auth/kimi-oauth.ts+++ b/packages/cli/src/auth/kimi-oauth.ts@@ -590,20 +590,20 @@ export class KimiOAuth {}this.credentials = null;- // FIX H4: Check for API key fallback (FR5 priority)- if (- process.env.MOONSHOT_API_KEY ||- process.env.KIMI_API_KEY ||- process.env.KIMI_CODING_API_KEY- ) {- log("[KimiOAuth] Falling back to API key mode");- throw new Error("OAuth_FALLBACK_TO_API_KEY");- }-- // No API key available, throw error with instructions- throw terminalCredentialError(- `OAuth credentials invalid. Please re-login or set API key:\n - Run: claudish login kimi\n - Or set: export MOONSHOT_API_KEY='your-api-key'\n\nDetails: ${failure.message}`- );+ // The fallback SIGNAL, always, carrying the login instruction as its cause.+ // WHICH key may answer is not this login's decision: one KimiOAuth serves+ // both kimi@ (Moonshot, metered) and kc@ (the Coding Plan), and each+ // composite owns its own product's key (kimi-credential.ts). The composite+ // falls back only when ITS key is present, and otherwise throws the cause:+ // a kc@ user with no KIMI_CODING_API_KEY sees "run `claudish login kimi`",+ // and MOONSHOT_API_KEY never signs kc@.+ log("[KimiOAuth] Login rejected; the product's own API key may answer instead");+ throw new Error("OAuth_FALLBACK_TO_API_KEY", {+ cause: terminalCredentialError(+ `Your Kimi login was rejected (${failure.message}). Run \`claudish login kimi\` to sign in again.`,+ (failure as { upstreamStatus?: number }).upstreamStatus+ ),+ });}/**diff --git a/packages/cli/src/providers/transport/anthropic-compat.ts b/packages/cli/src/providers/transport/anthropic-compat.tsindex 2d45a393..86c1fe3e 100644--- a/packages/cli/src/providers/transport/anthropic-compat.ts+++ b/packages/cli/src/providers/transport/anthropic-compat.ts@@ -7,8 +7,6 @@*/import { credentials } from "../../auth/credentials/authority.js";-import { isTransientCredentialError } from "../../auth/credentials/credential-error.js";-import { classifyConnectionError } from "../../handlers/shared/connection-error.js";import type { RemoteProvider } from "../../handlers/shared/remote-provider-types.js";import { log } from "../../logger.js";import { getProviderByName } from "../provider-definitions.js";@@ -91,16 +89,21 @@ export class AnthropicProviderTransport implements ProviderTransport {}Object.assign(headers, auth.headers);} catch (e: any) {- // Swallowing everything here was the transport-catch bug (CLAUDE.md):- // the producer's structured error never reached ComposedHandler, and the- // request went out with `x-api-key: ""`, whose 401 became a terminal- // 400. Rethrow what the auth site must classify: a network fault (it- // recovers), a token host's 429/5xx (it retries), and any failure when- // there is no coding-plan key to fall back to (a dead login then says- // `claudish login kimi`). Only with a KIMI_CODING_API_KEY — the same- // flat-rate plan — does the request fall back to it.- if (classifyConnectionError(e) || isTransientCredentialError(e) || !this.apiKey) throw e;- log(`[${this.displayName}] OAuth path failed, falling back to API key: ${e.message}`);+ // Every failure is rethrown, for ComposedHandler's getHeaders site to+ // classify: a network fault (recovered), a token host's 429/5xx+ // (retried), a dead login ("run `claudish login kimi`", terminal).+ // Swallowing here was the transport-catch bug (CLAUDE.md): the request+ // went out with `x-api-key: ""` or a stale token.+ //+ // The fallback to the Coding Plan's own KIMI_CODING_API_KEY (the same+ // flat-rate plan) is decided where the key lives: the kimi-coding+ // composite falls through only when that key is present, and then+ // returns its artifact without throwing (composite-credential.ts). A+ // Moonshot key (MOONSHOT_API_KEY / KIMI_API_KEY, a metered product)+ // belongs to the `kimi` composite and never signs kc@. `this.apiKey`+ // is not consulted: for an OAuth user it is the construction-time token.+ log(`[${this.displayName}] OAuth path failed: ${e.message}`);+ throw e;}}