Recognize sign-in failures for every agent and fall through
Codex re-runs device login on revoked, expired, or reused sessions and token_revoked/401 errors, not only invalid_refresh_token. Claude and OpenCode credential rejections are classified too. Sign-in failures fall through to the next profile, and the failure comment names each tried profile with a fixed reason label.
This commit is contained in:
+21
-3
@@ -10,13 +10,31 @@ export function codexAuthentication(token?: string): CodexAuthentication {
|
||||
: { kind: "api-key", token };
|
||||
}
|
||||
|
||||
// Codex logs revoked, expired, or reused ChatGPT sign-ins and rejected API keys
|
||||
// on its own ERROR lines; tool output and the echoed prompt must not count.
|
||||
const CODEX_AUTH_FAILURE =
|
||||
/^(?:\S+\s+)?ERROR\b.*(?:\b401\b|unauthorized|invalid_refresh_token|refresh_token_(?:expired|reused|invalidated)|token_(?:revoked|expired)|invalid_api_key|(?:log|sign) in again)/im;
|
||||
|
||||
export function isCodexAuthFailure(error: string): boolean {
|
||||
return CODEX_AUTH_FAILURE.test(error);
|
||||
}
|
||||
|
||||
// Claude Code reports a rejected CLAUDE_CODE_OAUTH_TOKEN as an is_error
|
||||
// stream-json result, e.g. "Failed to authenticate. API Error: 401 OAuth
|
||||
// access token is invalid." with api_error_status 401.
|
||||
export function isClaudeAuthFailure(
|
||||
result: { api_error_status?: number | null; result?: string },
|
||||
): boolean {
|
||||
return result.api_error_status === 401 ||
|
||||
/failed to authenticate|oauth (?:access )?token (?:is invalid|has expired)|invalid api key|authentication_error|please run \/login|not logged in/i
|
||||
.test(result.result ?? "");
|
||||
}
|
||||
|
||||
export async function retryInvalidToken<
|
||||
T extends { status: { success: boolean }; error: string },
|
||||
>(attempt: () => Promise<T>, relogin: () => Promise<void>): Promise<T> {
|
||||
let result = await attempt();
|
||||
if (
|
||||
!result.status.success && result.error.includes("invalid_refresh_token")
|
||||
) {
|
||||
if (!result.status.success && isCodexAuthFailure(result.error)) {
|
||||
await relogin();
|
||||
result = await attempt();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user