fix(codex): show live usage on the active account row, including System default (#20842)

The Codex switcher only rendered usage bars on inactive account rows from
the per-account cache, so the active row (and System default, which never
has a managed account id) showed nothing. Render the live Codex usage on
the active row when this desktop owns the accounts; a remote owner's usage
belongs to that host's target and stays off desktop-selected rows.

Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
moonrailgun
2026-10-26 15:40:01 -07:00
committed by GitHub
co-authored by Neil
parent 8291297b0b
commit 037cf27429
2 changed files with 54 additions and 4 deletions
@@ -250,7 +250,13 @@ export function CodexSwitcherMenu({
</span>
) : null}
</div>
{inactiveUsage?.isFetching && !inactiveUsage.rateLimits ? (
{/* Why: rows follow this desktop's codexTarget, so a remote owner's usage could land on the wrong row. */}
{target.active && !hasActiveRuntimeEnvironment ? (
<InlineUsageBars
limits={codex}
isFetching={codex.status === 'fetching'}
/>
) : inactiveUsage?.isFetching && !inactiveUsage.rateLimits ? (
<InlineUsageSkeleton />
) : showSignInAction ? (
<InlineUsageSignInAction
@@ -1,6 +1,6 @@
// @vitest-environment happy-dom
/** Exercise real inline sign-in wiring through reauthentication, restart prompts and success/failure feedback. */
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { GlobalSettings } from '../../../../shared/global-settings-types'
@@ -9,6 +9,7 @@ import type {
CodexRateLimitAccountsState
} from '../../../../shared/managed-account-types'
import type { ProviderRateLimits } from '../../../../shared/rate-limit-types'
import type { RuntimeClientTarget } from '../../../../shared/runtime-client-target'
const markLiveCodexSessionsForRestart = vi.fn(async () => {})
const toastSuccess = vi.fn()
@@ -18,6 +19,7 @@ const fetchSettings = vi.fn(async () => {})
const reauthenticate = vi.fn(async (_args: unknown) => codexSnapshot(null))
let storeSettings: GlobalSettings
let accountOwner: RuntimeClientTarget
function codexAccount(id: string, updatedAt: number): CodexManagedAccountSummary {
return {
@@ -86,6 +88,11 @@ vi.mock('@/runtime/runtime-provider-accounts-client', () => ({
selectClaudeProviderAccount: vi.fn(async () => ({ accounts: [], activeAccountId: null }))
}))
vi.mock('@/runtime/use-worktree-runtime-target', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useActiveAccountOwner: () => ({ target: accountOwner, wslHostDistro: null })
}))
vi.mock('@/lib/windows-terminal-capabilities', () => ({
useWindowsTerminalCapabilities: () => ({ wslDistros: [], isLoading: false }),
getWindowsTerminalCapabilityOwnerKey: () => 'local'
@@ -165,11 +172,21 @@ const codexProvider: ProviderRateLimits = {
updatedAt: 1
} as unknown as ProviderRateLimits
async function renderSwitcherAndOpenAccounts(summaryLabel: string): Promise<void> {
const liveUsage: ProviderRateLimits = {
...codexProvider,
status: 'ok',
error: null,
weekly: { usedPercent: 11, windowMinutes: 10_080, resetsAt: null, resetDescription: null }
}
async function renderSwitcherAndOpenAccounts(
summaryLabel: string,
codex = codexProvider
): Promise<void> {
const { CodexSwitcherMenu } = await import('./StatusBar')
render(
React.createElement(CodexSwitcherMenu, {
codex: codexProvider,
codex,
compact: false,
iconOnly: false
})
@@ -182,6 +199,7 @@ describe('status bar Codex sign-in action', () => {
beforeEach(() => {
vi.clearAllMocks()
storeSettings = settingsWithActive(null)
accountOwner = { kind: 'local' }
Object.defineProperty(window, 'api', {
configurable: true,
writable: true,
@@ -193,6 +211,32 @@ describe('status bar Codex sign-in action', () => {
cleanup()
})
it.each([null, 'account-1'])(
'shows live usage in the active account row, including system default: %s',
async (activeAccountId) => {
storeSettings = settingsWithActive(activeAccountId)
await renderSwitcherAndOpenAccounts(
activeAccountId ? 'account-1@example.com' : 'System default',
liveUsage
)
const activeRow = screen.getByRole('menuitem', { name: /Active/ })
expect(within(activeRow).getByText('11% used wk')).toBeTruthy()
}
)
it("keeps a remote owner's usage off rows picked by this desktop's runtime target", async () => {
accountOwner = { kind: 'environment', environmentId: 'remote-1' }
const { CodexSwitcherMenu } = await import('./StatusBar')
render(
React.createElement(CodexSwitcherMenu, { codex: liveUsage, compact: false, iconOnly: false })
)
fireEvent.click(screen.getByText('System default'))
const activeRow = await screen.findByRole('menuitem', { name: /Active/ })
expect(within(activeRow).queryByText('11% used wk')).toBeNull()
})
it.each([null, 'Enterprise'])(
'selects the exact same-email account when workspace labels collide: %s',
async (workspaceLabel) => {