Skip to content

Commit c1be956

Browse files
committed
fix(codex): close pause sentinel gaps
1 parent c38617a commit c1be956

4 files changed

Lines changed: 61 additions & 7 deletions

File tree

‎gui/src/hooks/useCodexAccountPool.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ export function useCodexAccountPool(apiBase: string, enabled = true): CodexAccou
282282
activeCodexAccountId?: string | null;
283283
};
284284
const pausedIds = new Set(result.pausedAccountIds ?? []);
285-
setAccounts(current => current.map(account => pausedIds.has(account.id) ? { ...account, paused: true } : account));
285+
setAccounts(current => current.map(account => (
286+
pausedIds.has(account.id) || (pausedIds.has("__main__") && account.isMain)
287+
? { ...account, paused: true }
288+
: account
289+
)));
286290
const nextActiveId = result.activeCodexAccountId ?? null;
287291
pendingActiveIdRef.current = { id: nextActiveId };
288292
setActiveId(nextActiveId);

‎gui/tests/codex-account-pool-behaviour.test.tsx‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ let accounts: unknown[] = [];
2424
let threshold = 80;
2525
let nextAccountsResponseGate: Promise<void> | null = null;
2626
let pauseResponseActiveId: string | null = null;
27+
let bulkPausedAccountIds: string[] = ["a2"];
28+
let bulkResponseActiveId: string | null = null;
2729

2830
beforeEach(() => {
2931
previous = Object.fromEntries(globals.map((k) => [k, Reflect.get(globalThis, k)])) as typeof previous;
@@ -40,6 +42,8 @@ beforeEach(() => {
4042
calls = [];
4143
nextAccountsResponseGate = null;
4244
pauseResponseActiveId = null;
45+
bulkPausedAccountIds = ["a2"];
46+
bulkResponseActiveId = null;
4347
accounts = [{ id: "a1", email: "account-one", isMain: true, paused: false, hasCredential: true, quota: null }];
4448
Object.defineProperty(globalThis, "fetch", {
4549
configurable: true,
@@ -57,14 +61,20 @@ beforeEach(() => {
5761
return { ok: true, json: async () => ({ activeCodexAccountId: pauseResponseActiveId }) } as unknown as Response;
5862
}
5963
if (path === "codex-auth/accounts/pause-exhausted") {
64+
const pausedIds = new Set(bulkPausedAccountIds);
6065
accounts = accounts.map(account => (
61-
typeof account === "object" && account !== null && "id" in account && account.id === "a2"
66+
typeof account === "object" && account !== null && "id" in account
67+
&& (pausedIds.has(String(account.id)) || (pausedIds.has("__main__") && "isMain" in account && account.isMain === true))
6268
? { ...account, paused: true }
6369
: account
6470
));
6571
return {
6672
ok: true,
67-
json: async () => ({ pausedAccountIds: ["a2"], pausedCount: 1, activeCodexAccountId: null }),
73+
json: async () => ({
74+
pausedAccountIds: bulkPausedAccountIds,
75+
pausedCount: bulkPausedAccountIds.length,
76+
activeCodexAccountId: bulkResponseActiveId,
77+
}),
6878
} as unknown as Response;
6979
}
7080
if (path.startsWith("codex-auth/accounts")) {
@@ -208,6 +218,31 @@ test("bulk pausing writes one endpoint and updates every returned account", asyn
208218
expect(seen.current!.pausingExhausted).toBe(false);
209219
});
210220

221+
test("bulk pausing translates the main sentinel to its distinct account row", async () => {
222+
accounts = [
223+
{ id: "a1", email: "main", isMain: true, paused: false, hasCredential: true, quota: null },
224+
{ id: "a2", email: "pool", isMain: false, paused: false, hasCredential: true, quota: null },
225+
];
226+
bulkPausedAccountIds = ["__main__"];
227+
bulkResponseActiveId = "a2";
228+
const seen = await mountController();
229+
let releaseReload!: () => void;
230+
nextAccountsResponseGate = new Promise<void>(resolve => { releaseReload = resolve; });
231+
232+
await act(async () => {
233+
expect(await seen.current!.pauseExhaustedAccounts()).toEqual({ ok: true, pausedCount: 1 });
234+
});
235+
236+
expect(seen.current!.accounts.find(account => account.isMain)?.id).toBe("a1");
237+
expect(seen.current!.accounts.find(account => account.isMain)?.paused).toBe(true);
238+
expect(seen.current!.activeId).toBe("a2");
239+
240+
await act(async () => {
241+
releaseReload();
242+
await new Promise((resolve) => setTimeout(resolve, 30));
243+
});
244+
});
245+
211246
test("two pause holders both have to release before polling resumes", async () => {
212247
const seen = await mountController();
213248
const controller = seen.current!;

‎src/codex/auth-api.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -752,15 +752,16 @@ export async function handleCodexAuthAPI(
752752
let body: { accountId: string | null };
753753
try { body = (await req.json()) as typeof body; } catch { return jsonResponse({ error: "Invalid JSON" }, 400); }
754754
const runtimeConfig = getRuntimeConfig(config);
755-
if (body.accountId != null && isCodexAccountPaused(runtimeConfig, body.accountId)) {
755+
const targetAccountId = body.accountId ?? MAIN_CODEX_ACCOUNT_ID;
756+
if (isCodexAccountPaused(runtimeConfig, targetAccountId)) {
756757
return jsonResponse({ error: "Account is paused" }, 409);
757758
}
758-
if (body.accountId != null && body.accountId !== MAIN_CODEX_ACCOUNT_ID) {
759-
const exists = (runtimeConfig.codexAccounts ?? []).some(a => a.id === body.accountId);
759+
if (targetAccountId !== MAIN_CODEX_ACCOUNT_ID) {
760+
const exists = (runtimeConfig.codexAccounts ?? []).some(a => a.id === targetAccountId);
760761
if (!exists) return jsonResponse({ error: "Account not found" }, 400);
761762
}
762763
runtimeConfig.activeCodexAccountId = body.accountId ?? undefined;
763-
resetCodexRoutingForManualSelection(body.accountId ?? MAIN_CODEX_ACCOUNT_ID);
764+
resetCodexRoutingForManualSelection(targetAccountId);
764765
saveRuntimeConfig(config, runtimeConfig);
765766
return jsonResponse({ ok: true, activeCodexAccountId: body.accountId, appliesImmediately: true });
766767
}

‎tests/codex-auth-api.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,20 @@ describe("codex-auth API", () => {
13401340
expect(config.pausedCodexAccountIds).toEqual([MAIN_CODEX_ACCOUNT_ID]);
13411341
});
13421342

1343+
test("PUT /api/codex-auth/active rejects null when the effective main account is paused", async () => {
1344+
const config = makeConfig({ pausedCodexAccountIds: [MAIN_CODEX_ACCOUNT_ID] });
1345+
const req = new Request("http://localhost/api/codex-auth/active", {
1346+
method: "PUT",
1347+
headers: { "Content-Type": "application/json" },
1348+
body: JSON.stringify({ accountId: null }),
1349+
});
1350+
const resp = await handleCodexAuthAPI(req, new URL(req.url), config);
1351+
1352+
expect(resp!.status).toBe(409);
1353+
expect(await resp!.json()).toEqual({ error: "Account is paused" });
1354+
expect(config.activeCodexAccountId).toBeUndefined();
1355+
});
1356+
13431357
test("resuming restores eligibility and manual activation rejects paused accounts", async () => {
13441358
const config = makeConfig({
13451359
codexAccounts: [{ id: "work", email: "[email protected]", isMain: false }],

0 commit comments

Comments
 (0)