Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(kimi): keep a newer kimi-oauth.json when a refresh is rejected
Jack Rudenko9f4da7323917
diff --git a/packages/cli/src/auth/credential-file.ts b/packages/cli/src/auth/credential-file.tsindex 9ea00135..981acbe7 100644--- a/packages/cli/src/auth/credential-file.ts+++ b/packages/cli/src/auth/credential-file.ts@@ -253,6 +253,38 @@ export class CredentialFileState<T extends { access_token: string; refresh_token}}+ /**+ * A rejected login (Kimi): delete the file ONLY if it still holds `base`'s lineage.+ * A login another process wrote since `base` was read is never deleted, and an+ * unreadable file (possibly a newer login caught mid-write) is left alone.+ */+ async deleteIfUnchanged(+ base: T+ ): Promise<"deleted" | "newer-on-disk" | "removed" | "unreadable"> {+ const label = this.opts.label;+ const sync = await this.settle();+ if (sync === "removed") return "removed";+ if (sync === "unreadable") {+ log(`[${label}] login rejected, but the credential file is unreadable; not deleting it`);+ return "unreadable";+ }+ const onDisk = this.current;+ if (onDisk === null) return "removed";+ if (lineageOf(onDisk) !== lineageOf(base)) {+ log(`[${label}] login rejected, but the credential file changed on disk; not deleting it`);+ return "newer-on-disk";+ }+ const path = this.opts.path();+ try {+ unlinkSync(path);+ } catch (err) {+ if ((err as NodeJS.ErrnoException | null)?.code !== "ENOENT") return "unreadable";+ }+ this.current = null;+ this.recorded = { path, stamp: null };+ return "deleted";+ }+/** Forget the in-memory credentials (a logout in this process). */forget(): void {this.current = null;diff --git a/packages/cli/src/auth/kimi-oauth.ts b/packages/cli/src/auth/kimi-oauth.tsindex b17fbf50..51b593b5 100644--- a/packages/cli/src/auth/kimi-oauth.ts+++ b/packages/cli/src/auth/kimi-oauth.ts@@ -599,7 +599,7 @@ export class KimiOAuth {const grantRejected =status === 401 || status === 403 || (status === 400 && text.includes("invalid_grant"));if (!grantRejected) throw failure;- return this.rejectLogin(failure);+ return await this.rejectLogin(failure, current);}// A 2xx that is not JSON is a gateway answering for the token host:@@ -648,13 +648,13 @@ export class KimiOAuth {* login cannot recover, so it is deleted, and the caller gets the fallback* signal or the login instruction.*/- private rejectLogin(failure: Error): never {+ private async rejectLogin(failure: Error, rejected: KimiCredentials): Promise<never> {log(`[KimiOAuth] Refresh rejected: ${failure.message}`);- const credPath = kimiOAuthPath();- if (existsSync(credPath)) {- unlinkSync(credPath);- }- this.state.forget();+ // Delete only the login that was rejected. If another process logged in since+ // (`claudish login kimi` in another terminal), its file is kept, and+ // refreshAndCommit adopts it after this throw instead of answering with the+ // fallback. An unreadable file (possibly that login mid-write) is kept too.+ await this.state.deleteIfUnchanged(rejected);// The fallback SIGNAL, always, carrying the login instruction as its cause.// WHICH key may answer is not this login's decision: one KimiOAuth serves