Skip to content

Commit af6bd0f

Browse files
committed
fix(gui): propagate health-only reauth into Providers attention state
Pass activePoolNeedsReauth through the Codex callback, honor health.status === reauth_required in the generic provider map and Codex controller, and add aggregate-path regressions so overview attention matches row CTAs.
1 parent fb9b64c commit af6bd0f

7 files changed

Lines changed: 150 additions & 18 deletions

‎gui/src/components/CodexAccountPool.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { CodexAccountResetModal } from "./codex-account-reset-modal";
1515
import { CodexAccountPoolLoadStates, CodexAccountPoolMainCard, CodexAccountPoolPageHead } from "./codex-account-pool-main-card";
1616
import { redeemResetCredit } from "./codex-account-pool-handlers";
1717
import type { CodexAccountEntry } from "./codex-account-pool-types";
18-
import { copyTextToClipboard, oauthHealthShowsReauth, type DoctorCopyFeedback } from "../oauth-health-display";
18+
import { accountNeedsReauth, copyTextToClipboard, type DoctorCopyFeedback } from "../oauth-health-display";
1919

2020
// Single definition lives with the controller that owns this data (WP3).
2121
export type { CodexAccountEntry } from "../hooks/useCodexAccountPool";
@@ -53,7 +53,7 @@ export default function CodexAccountPool({ apiBase, accountModeState = null, ban
5353
// but stays inert (no load, no polling) whenever a shared controller was injected.
5454
const ownController = useCodexAccountPool(apiBase, !injectedController);
5555
const controller = injectedController ?? ownController;
56-
const { accounts, activeId, loadState, switchingId, activeNeedsReauth, load } = controller;
56+
const { accounts, activeId, loadState, switchingId, load } = controller;
5757
const [confirm, setConfirm] = useState<CodexAccountEntry | null>(null);
5858
const [showAdd, setShowAdd] = useState(false);
5959
const [reauthId, setReauthId] = useState<string | null>(null);
@@ -110,12 +110,11 @@ export default function CodexAccountPool({ apiBase, accountModeState = null, ban
110110
const activePoolAccount = activeId && activeId !== "__main__"
111111
? accounts.find(a => a.id === activeId)
112112
: null;
113-
const activePoolNeedsReauth = Boolean(activePoolAccount?.needsReauth)
114-
|| oauthHealthShowsReauth(activePoolAccount?.health?.status);
113+
const activePoolNeedsReauth = accountNeedsReauth(activePoolAccount);
115114

116115
useEffect(() => {
117-
onActiveNeedsReauthChange?.(activeNeedsReauth);
118-
}, [activeNeedsReauth, onActiveNeedsReauthChange]);
116+
onActiveNeedsReauthChange?.(activePoolNeedsReauth);
117+
}, [activePoolNeedsReauth, onActiveNeedsReauthChange]);
119118

120119
const openReauth = useCallback((id: string) => {
121120
setReauthId(id);

‎gui/src/hooks/useCodexAccountPool.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useEffect, useRef, useState } from "react";
22
import type { AccountQuota } from "../codex-quota-utils";
3+
import { accountNeedsReauth } from "../oauth-health-display";
34

45
/**
56
* Codex account pool DATA layer (WP3 / 030_account_state_lift.md).
@@ -252,9 +253,8 @@ export function useCodexAccountPool(apiBase: string, enabled = true): CodexAccou
252253
? accounts.find(a => a.id === activeId)
253254
: null;
254255
const mainAccount = accounts.find(a => a.isMain);
255-
const activeNeedsReauth = activePoolAccount
256-
? Boolean(activePoolAccount.needsReauth)
257-
: Boolean(mainAccount?.needsReauth);
256+
// Include health-only reauth so Providers overview attention matches row CTAs.
257+
const activeNeedsReauth = accountNeedsReauth(activePoolAccount ?? mainAccount);
258258

259259
return {
260260
accounts,

‎gui/src/hooks/useProviderAccountPools.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useEffect, useMemo, useRef, useState, type MutableRefObject } from "react";
22
import type { AccountLoadState } from "../components/provider-workspace/types";
3+
import { accountNeedsReauth } from "../oauth-health-display";
34
import { oauthAccountDisplayLabel } from "../provider-workspace/auth";
45

56
export interface Config {
@@ -23,6 +24,20 @@ export interface OAuthAccount {
2324
}
2425
export interface ApiKeyEntry { id: string; label?: string; masked: string; active: boolean }
2526

27+
/** Pure aggregate map used by Providers overview / rail attention state. */
28+
export function buildActiveAccountNeedsReauthMap(
29+
accountSets: Record<string, { activeAccountId: string | null; accounts: OAuthAccount[] }>,
30+
codexActiveNeedsReauth = false,
31+
): Record<string, boolean> {
32+
const map: Record<string, boolean> = {};
33+
for (const [provider, set] of Object.entries(accountSets)) {
34+
const active = set.accounts.find(a => a.active) ?? set.accounts.find(a => a.id === set.activeAccountId);
35+
if (accountNeedsReauth(active)) map[provider] = true;
36+
}
37+
if (codexActiveNeedsReauth) map.openai = true;
38+
return map;
39+
}
40+
2641
export function useProviderAccountPools(deps: {
2742
apiBase: string;
2843
t: (key: string, ...args: unknown[]) => string;
@@ -211,15 +226,10 @@ export function useProviderAccountPools(deps: {
211226
return () => window.clearTimeout(timeout);
212227
}, [fetchKeyPools, keyCardProviders]);
213228

214-
const activeAccountNeedsReauth = useMemo(() => {
215-
const map: Record<string, boolean> = {};
216-
for (const [provider, set] of Object.entries(accountSets)) {
217-
const active = set.accounts.find(a => a.active) ?? set.accounts.find(a => a.id === set.activeAccountId);
218-
if (active?.needsReauth) map[provider] = true;
219-
}
220-
if (codexActiveNeedsReauth) map.openai = true;
221-
return map;
222-
}, [accountSets, codexActiveNeedsReauth]);
229+
const activeAccountNeedsReauth = useMemo(
230+
() => buildActiveAccountNeedsReauthMap(accountSets, codexActiveNeedsReauth),
231+
[accountSets, codexActiveNeedsReauth],
232+
);
223233

224234
return {
225235
accountSets, accountLoadStates, switchingAccount, openAccounts, keyPools, addingKeyFor, newKeyValue,

‎gui/src/oauth-health-display.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ export function oauthHealthShowsReauth(status: OAuthHealthStatus | undefined): b
4646
return status === "reauth_required";
4747
}
4848

49+
/**
50+
* Aggregate/row reauth gate: legacy `needsReauth` OR canonical health projection.
51+
* Health-only `reauth_required` must still demote Providers overview attention state.
52+
*/
53+
export function accountNeedsReauth(
54+
account: { needsReauth?: boolean; health?: { status?: OAuthHealthStatus } | null } | null | undefined,
55+
): boolean {
56+
return Boolean(account?.needsReauth) || oauthHealthShowsReauth(account?.health?.status);
57+
}
58+
4959
/** Cooldown: show wait copy; do not urge probing or immediate retry. */
5060
export function oauthHealthIsCooldown(status: OAuthHealthStatus | undefined): boolean {
5161
return status === "cooldown";
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { accountNeedsReauth } from "../src/oauth-health-display";
3+
import { buildActiveAccountNeedsReauthMap } from "../src/hooks/useProviderAccountPools";
4+
5+
/**
6+
* Aggregate-path regressions: health-only `reauth_required` must demote Providers
7+
* overview / rail attention the same way row CTAs already do. Fixtures intentionally
8+
* omit emails/tokens — never assert raw account identifiers into attention state.
9+
*/
10+
11+
describe("Codex callback aggregate path", () => {
12+
test("activePoolNeedsReauth (health-aware) is what the callback propagates", async () => {
13+
const pool = await Bun.file(new URL("../src/components/CodexAccountPool.tsx", import.meta.url)).text();
14+
expect(pool).toContain("accountNeedsReauth(activePoolAccount)");
15+
expect(pool).toContain("onActiveNeedsReauthChange?.(activePoolNeedsReauth)");
16+
expect(pool).toContain("[activePoolNeedsReauth, onActiveNeedsReauthChange]");
17+
// Legacy-only controller flag must not be the callback dependency.
18+
expect(pool).not.toContain("onActiveNeedsReauthChange?.(activeNeedsReauth)");
19+
expect(pool).not.toMatch(/\[activeNeedsReauth,\s*onActiveNeedsReauthChange\]/);
20+
});
21+
22+
test("shared Codex controller derives activeNeedsReauth from health as well", async () => {
23+
const hook = await Bun.file(new URL("../src/hooks/useCodexAccountPool.ts", import.meta.url)).text();
24+
expect(hook).toContain("accountNeedsReauth(activePoolAccount ?? mainAccount)");
25+
expect(hook).not.toMatch(/activePoolAccount\s*\?\s*Boolean\(activePoolAccount\.needsReauth\)/);
26+
});
27+
28+
test("health-only reauth_required marks the active Codex account", () => {
29+
// Mirrors CodexAccountPool's activePoolNeedsReauth / controller activeNeedsReauth.
30+
expect(accountNeedsReauth({
31+
needsReauth: undefined,
32+
health: { status: "reauth_required", reason: "refresh_failed" },
33+
})).toBe(true);
34+
expect(accountNeedsReauth({
35+
needsReauth: undefined,
36+
health: { status: "healthy" },
37+
})).toBe(false);
38+
});
39+
});
40+
41+
describe("generic provider map aggregate path", () => {
42+
test("health-only reauth_required on the active account demotes the provider", () => {
43+
const map = buildActiveAccountNeedsReauthMap({
44+
anthropic: {
45+
activeAccountId: "acct_active",
46+
accounts: [
47+
{
48+
id: "acct_active",
49+
active: true,
50+
health: { status: "reauth_required", reason: "refresh_failed" },
51+
},
52+
{
53+
id: "acct_other",
54+
active: false,
55+
needsReauth: true,
56+
},
57+
],
58+
},
59+
xai: {
60+
activeAccountId: "xai_ok",
61+
accounts: [
62+
{
63+
id: "xai_ok",
64+
active: true,
65+
health: { status: "healthy" },
66+
},
67+
],
68+
},
69+
});
70+
expect(map).toEqual({ anthropic: true });
71+
});
72+
73+
test("inactive-only reauth does not demote the provider", () => {
74+
const map = buildActiveAccountNeedsReauthMap({
75+
anthropic: {
76+
activeAccountId: "acct_active",
77+
accounts: [
78+
{ id: "acct_active", active: true, health: { status: "healthy" } },
79+
{ id: "acct_stale", active: false, health: { status: "reauth_required" } },
80+
],
81+
},
82+
});
83+
expect(map).toEqual({});
84+
});
85+
86+
test("legacy needsReauth and Codex overlay still mark providers", () => {
87+
const map = buildActiveAccountNeedsReauthMap(
88+
{
89+
kimi: {
90+
activeAccountId: "kimi_1",
91+
accounts: [{ id: "kimi_1", active: true, needsReauth: true }],
92+
},
93+
},
94+
true,
95+
);
96+
expect(map).toEqual({ kimi: true, openai: true });
97+
});
98+
});

‎gui/tests/oauth-health-display.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
doctorCopyButtonLabel,
55
formatOAuthHealthLabel,
66
formatOAuthHealthSummary,
7+
accountNeedsReauth,
78
oauthHealthBadgeClass,
89
oauthHealthBadgeTone,
910
oauthHealthIsCooldown,
@@ -42,6 +43,15 @@ describe("oauth health badge helpers", () => {
4243
expect(oauthHealthIsCooldown("healthy")).toBe(false);
4344
});
4445

46+
test("accountNeedsReauth combines legacy flag with health-only reauth", () => {
47+
expect(accountNeedsReauth(undefined)).toBe(false);
48+
expect(accountNeedsReauth({ needsReauth: true })).toBe(true);
49+
expect(accountNeedsReauth({ health: { status: "reauth_required" } })).toBe(true);
50+
expect(accountNeedsReauth({ needsReauth: false, health: { status: "healthy" } })).toBe(false);
51+
expect(accountNeedsReauth({ health: { status: "cooldown" } })).toBe(false);
52+
expect(accountNeedsReauth({ health: { status: "warning" } })).toBe(false);
53+
});
54+
4555
test("localizes labels and summaries from structured health", () => {
4656
expect(formatOAuthHealthLabel(t, { status: "healthy" })).toBeNull();
4757
expect(formatOAuthHealthLabel(t, { status: "cooldown", reason: "rate_limit", until: "2026-07-26T00:00:00.000Z" }))

‎tests/provider-workspace-auth.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ describe("workspace account integration seam", () => {
181181
Bun.file("gui/src/components/codex-account-pool-main-card.tsx").text(),
182182
]);
183183
expect(page).toContain("codexActiveNeedsReauth");
184+
expect(page).toContain("buildActiveAccountNeedsReauthMap");
184185
expect(page).toContain("map.openai = true");
185186

186187
// WP3: reauth health is DERIVED from the shared controller. The page used to keep a
@@ -191,6 +192,10 @@ describe("workspace account integration seam", () => {
191192
expect(page).not.toContain("codexReauthGenerationRef");
192193
expect(page).not.toContain("setCodexActiveNeedsReauth");
193194

195+
// Health-only reauth_required must reach both aggregate surfaces.
196+
expect(pool).toContain("onActiveNeedsReauthChange?.(activePoolNeedsReauth)");
197+
expect(hook).toContain("accountNeedsReauth(activePoolAccount ?? mainAccount)");
198+
expect(page).toContain("accountNeedsReauth(active)");
194199
// WP3: background refresh pauses through a token lease, not a boolean read of the
195200
// modal flag. Two holders must both release before polling resumes.
196201
expect(pool).toContain("controller.pauseRefresh()");

0 commit comments

Comments
 (0)