Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): classify Codex refresh failures as the Codex client does (#739)
Alfredo Lopezf2a5bb465288
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex e09311723..8090d616a 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.616",+ "version": "0.1.617","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.616",+ "version": "0.1.617","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex 9e41085c9..9e658aedb 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.616",+ "version": "0.1.617","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/services/codexSubscriptionOAuth.ts b/control-api/src/services/codexSubscriptionOAuth.tsindex e9fb0bb50..467725dc3 100644--- a/control-api/src/services/codexSubscriptionOAuth.ts+++ b/control-api/src/services/codexSubscriptionOAuth.ts@@ -773,8 +773,7 @@ async function exchangeRefreshToken(client_id: deps.clientId,})if (!result.ok) {- const error = readUpstreamErrorCode(result.body)- if ((result.status === 400 || result.status === 401) && error === 'invalid_grant') {+ if (isPermanentRefreshFailure(result.status, readUpstreamErrorCode(result.body))) {const latest = await loadCodexSubscriptionSecrets(deps.db,deps.encryptionKey,@@ -786,7 +785,7 @@ async function exchangeRefreshToken(if (lockChanged || revisionChanged) {throw new CodexSubscriptionOAuthError('stale_revision',- 'invalid_grant observed after a lost refresh race'+ 'refresh rejection observed after a lost refresh race')}const marked = await markCodexRefreshRejected(@@ -800,7 +799,7 @@ async function exchangeRefreshToken(if (!marked) {throw new CodexSubscriptionOAuthError('stale_revision',- 'invalid_grant observed after the grant changed'+ 'refresh rejection observed after the grant changed')}// The row is now `reauth_required`. The throw below must not read as@@ -842,6 +841,27 @@ function decodeDeviceAuthHandle(value: string | undefined): CodexDeviceAuthHandlreturn null}+const PERMANENT_REFRESH_ERROR_CODES = new Set([+ 'refresh_token_expired',+ 'refresh_token_reused',+ 'refresh_token_invalidated',+])++/**+ * The rule the official Codex client applies (`classify_refresh_token_failure`+ * in openai/codex `codex-rs/login/src/auth/manager.rs`): any 401, one of the+ * refresh-token codes at any status, or a 400 `invalid_grant`, compared+ * case-insensitively. Every other failure is transient.+ */+function isPermanentRefreshFailure(status: number, upstreamCode: string): boolean {+ const code = upstreamCode.toLowerCase()+ return (+ status === 401 ||+ PERMANENT_REFRESH_ERROR_CODES.has(code) ||+ (status === 400 && code === 'invalid_grant')+ )+}+function readUpstreamErrorCode(body: Record<string, unknown>): string {const error = body.errorif (typeof error === 'string') return error