fix(codex-accounts): broadcast Codex account selection changes to windows (#17265)

Selecting, removing, or adding a managed Codex account from the CLI or a
paired phone updated settings without notifying listeners, so the desktop
status bar kept naming the previous account over the new account's usage.
Route every Codex selection write through CodexAccountSelection.saveSettings,
which passes notifyListeners: true, matching the Claude side.

Fixes #43107

Co-authored-by: Neil <neil@stably.ai>
This commit is contained in:
Tauri-EPO
2026-11-16 04:56:13 -08:00
committed by GitHub
co-authored by Neil
parent e6dacc7bf3
commit 2bc8fc1ad5
4 changed files with 123 additions and 19 deletions
@@ -147,7 +147,7 @@ export class CodexAccountRegistration {
)
// Restore this runtime's selection while retaining current validation of other runtimes.
this.dependencies.store.updateSettings({
this.dependencies.selection.saveSettings({
codexManagedAccounts: updatedAccounts,
activeCodexManagedAccountId: activeSelection.host,
activeCodexManagedAccountIdsByRuntime: activeSelection
@@ -232,7 +232,7 @@ export class CodexAccountRegistration {
const settings = this.dependencies.store.getSettings()
const selection = normalizeCodexRuntimeSelection(settings)
const targetSelection = getCodexSelectionTargetForAccount(account)
this.dependencies.store.updateSettings({
this.dependencies.selection.saveSettings({
codexManagedAccounts: [...settings.codexManagedAccounts, account],
activeCodexManagedAccountId: targetSelection.runtime === 'host' ? account.id : selection.host,
activeCodexManagedAccountIdsByRuntime: setSelectedCodexAccountIdForTarget(
@@ -247,7 +247,7 @@ export class CodexAccountRegistration {
this.dependencies.runtimeHome.syncForCurrentSelection(targetSelection)
} catch (error) {
// Restore settings before caller cleanup removes the failed account's home.
this.dependencies.store.updateSettings({
this.dependencies.selection.saveSettings({
codexManagedAccounts: settings.codexManagedAccounts,
activeCodexManagedAccountId: settings.activeCodexManagedAccountId,
activeCodexManagedAccountIdsByRuntime: settings.activeCodexManagedAccountIdsByRuntime
@@ -0,0 +1,92 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { installFakeAppEnvironment } from '../../../../config/scripts/test-runtime/environment/vitest-host-ports-setup'
import type { GlobalSettings } from '../../../shared/settings/global-settings-types'
import type { CodexAccountService as CodexAccountServiceInstance } from '../service'
import {
createManagedHome,
createRateLimits,
createRuntimeHome,
createSettings,
createStore,
registerCodexAccountsTestHomes,
testState
} from './service-test-harness'
import { createCodexAccountRecord } from '../runtime-home/runtime-home-service-test-harness'
vi.mock('electron', () => ({
app: {
getPath: () => testState.userDataDir
}
}))
beforeEach(() => installFakeAppEnvironment({ getPath: () => testState.userDataDir }))
vi.mock(import('node:os'), async (importOriginal) => {
const actual = await importOriginal()
return {
...actual,
homedir: () => testState.fakeHomeDir
}
})
describe('Codex account selection broadcast', () => {
registerCodexAccountsTestHomes()
// #43107: a switch from the CLI or a paired phone must reach every window's status bar.
it.each([
{
change: 'select',
apply: (service: CodexAccountServiceInstance) => service.selectAccount('b'),
expected: {
activeCodexManagedAccountId: 'b',
activeCodexManagedAccountIdsByRuntime: { host: 'b', wsl: {} }
}
},
{
change: 'remove',
apply: (service: CodexAccountServiceInstance) => service.removeAccount('a'),
expected: {
activeCodexManagedAccountId: null,
activeCodexManagedAccountIdsByRuntime: { host: null, wsl: {} }
}
}
])(
'tells settings listeners about a $change made outside the window',
async ({ apply, expected }) => {
const store = createStore(
createSettings({
codexManagedAccounts: [
createCodexAccountRecord(
'a',
'a@example.com',
'acct-a',
createManagedHome(testState.userDataDir, 'a')
),
createCodexAccountRecord(
'b',
'b@example.com',
'acct-b',
createManagedHome(testState.userDataDir, 'b')
)
],
activeCodexManagedAccountId: 'a',
activeCodexManagedAccountIdsByRuntime: { host: 'a', wsl: {} }
})
)
const broadcasts: Partial<GlobalSettings>[] = []
store.onSettingsChanged((updates) => broadcasts.push(updates))
const { CodexAccountService } = await import('../service')
const service = new CodexAccountService(
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: select/remove read only getSettings/updateSettings, which the harness store implements.
store as never,
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the harness stub implements the rate-limit members the service calls on a switch.
createRateLimits() as never,
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the harness stub implements the runtime-home members the service calls on a switch.
createRuntimeHome() as never
)
await apply(service)
expect(broadcasts).toContainEqual(expect.objectContaining(expected))
}
)
})
@@ -3,6 +3,7 @@ import type {
CodexRateLimitAccountsState,
CodexSystemDefaultIdentity
} from '../../../shared/agent-accounts/managed-account-types'
import type { GlobalSettings } from '../../../shared/settings/global-settings-types'
import { parseWslUncPath } from '../../../shared/file-access/wsl-paths'
import type { Store } from '../../persistence/loading-store/store'
import type { RateLimitService } from '../../rate-limits/service/rate-limit-service'
@@ -95,7 +96,7 @@ export class CodexAccountSelection {
null
)
this.dependencies.store.updateSettings({
this.saveSettings({
codexManagedAccounts: nextAccounts,
activeCodexManagedAccountId: nextActiveId,
activeCodexManagedAccountIdsByRuntime: nextSelection,
@@ -134,11 +135,7 @@ export class CodexAccountSelection {
normalizeManagedAccountDisplayName(displayName)
)
if (names) {
// Notify windows: their status-bar rows read names from settings.
this.dependencies.store.updateSettings(
{ managedAccountDisplayNames: names },
{ notifyListeners: true }
)
this.saveSettings({ managedAccountDisplayNames: names })
}
return this.snapshot()
}
@@ -169,7 +166,7 @@ export class CodexAccountSelection {
accountId,
effectiveTarget
)
this.dependencies.store.updateSettings({
this.saveSettings({
activeCodexManagedAccountId:
effectiveTarget?.runtime === 'wsl' ? nextSelection.host : accountId,
activeCodexManagedAccountIdsByRuntime: nextSelection
@@ -187,6 +184,11 @@ export class CodexAccountSelection {
return this.snapshot()
}
// Notify every window of changes made by the CLI, paired clients, or other windows.
saveSettings(patch: Partial<GlobalSettings>): void {
this.dependencies.store.updateSettings(patch, { notifyListeners: true })
}
// A `codex login` run inside a managed home swaps its account; label the row with what actually runs (#22284).
private adoptManagedHomeIdentities(): void {
const settings = this.dependencies.store.getSettings()
@@ -41,19 +41,29 @@ export function createSettings(overrides: Partial<GlobalSettings> = {}): GlobalS
export function createStore(settings: GlobalSettings) {
let resetLedger: CodexResetCreditAttemptLedger = { version: 1, attempts: [] }
const settingsListeners = new Set<(updates: Partial<GlobalSettings>) => void>()
return {
getSettings: vi.fn(() => settings),
updateSettings: vi.fn((updates: Partial<GlobalSettings>) => {
settings = {
...settings,
...updates,
notifications: {
...settings.notifications,
...updates.notifications
updateSettings: vi.fn(
(updates: Partial<GlobalSettings>, options?: { notifyListeners?: boolean }) => {
settings = {
...settings,
...updates,
notifications: {
...settings.notifications,
...updates.notifications
}
}
if (options?.notifyListeners) {
settingsListeners.forEach((listener) => listener(updates))
}
return settings
}
return settings
}),
),
onSettingsChanged: (listener: (updates: Partial<GlobalSettings>) => void) => {
settingsListeners.add(listener)
return () => settingsListeners.delete(listener)
},
getCodexResetCreditAttemptLedger: vi.fn(() => structuredClone(resetLedger)),
replaceCodexResetCreditAttemptLedgerAndFlush: vi.fn(
async (next: CodexResetCreditAttemptLedger) => {