Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-codex-stale-auth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-ax/agents-desktop': patch
---

Clear stale Codex auth in the desktop app when no usable access token can be produced, preventing the UI from showing Codex as enabled while runs cannot authenticate.
31 changes: 28 additions & 3 deletions packages/agents-desktop/src/credentials/codex-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,44 @@ async function refreshCodexAuthIfNeeded(
return next
}

async function clearCodexAuth(
deps: Pick<
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Picked type is the most precise but also verbose and requires more maintenance (any time one of these props changes). I would keep it simple here: deps: CodexAuthDeps

CodexAuthDeps,
`settings` | `getSecretStore` | `saveSettings` | `markCredentialsDirty`
>
): Promise<void> {
deps.settings.codex = { enabled: false, source: null }
await deps.getSecretStore().delete(CODEX_AUTH_REF)
await deps.saveSettings()
deps.markCredentialsDirty()
}

export async function syncCodexEnvironment(
deps: Pick<CodexAuthDeps, `settings` | `getSecretStore`>
deps: Pick<
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before, simplify to: deps: CodexAuthDeps

CodexAuthDeps,
`settings` | `getSecretStore` | `saveSettings` | `markCredentialsDirty`
>
): Promise<void> {
process.env.ELECTRIC_CODEX_REQUIRE_OPT_IN = `1`
delete process.env.ELECTRIC_CODEX_ACCESS_TOKEN
const codex = deps.settings.codex ?? { enabled: false, source: null }
if (!codex.enabled || !codex.source) return

const stored = await loadStoredCodexAuth(deps)
if (!stored) return
const refreshed = await refreshCodexAuthIfNeeded(deps, stored)
const refreshed =
stored?.source === codex.source
? await refreshCodexAuthIfNeeded(deps, stored)
: null
if (refreshed?.access) {
process.env.ELECTRIC_CODEX_ACCESS_TOKEN = refreshed.access
return
}

// Avoid showing a false "Enabled" state when no usable access token can be
// produced. Desktop OAuth tokens should refresh through the OAuth token
// endpoint above; stale tokens imported from CLI/opencode auth files should be
// deleted rather than repeatedly trusted as an enabled desktop credential.
await clearCodexAuth(deps)
}

async function detectCodexSources(
Expand Down
Loading