Repository navigation
fix(codex): list pool accounts without a native home #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b2a8fb3
fix(codex): list pool accounts without a native home
alexalok 09e092b
docs(codex): qualify main-account 401 reauthentication
alexalok 22a9f85
fix(codex): validate storage before missing-home fallback
alexalok b6f9a3b
docs(codex): translate headless account-list guidance
alexalok 2ef0c91
fix(codex): require proven absence for native-home fallback
alexalok 437939f
docs(codex): complete headless account-list translations
alexalok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
tests/codex-integration/codex-auth-headless-accounts.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { getCodexAccountCredential, saveCodexAccountCredential } from "../../src/codex/account-store"; | ||
| import { resetMainCodexAccountIdentityTrackingForTests } from "../../src/codex/account-lifecycle"; | ||
| import { clearAccountNeedsReauth, isAccountNeedsReauth, markAccountNeedsReauth } from "../../src/codex/account-runtime-state"; | ||
| import type { CodexAuthAccountDto } from "../../src/codex/auth-api/account-list"; | ||
| import { withNativeMainCredentialClaim } from "../../src/codex/auth-api/http"; | ||
| import { handleCodexAuthAPI } from "../../src/codex/auth-api/routes"; | ||
| import { | ||
| captureMainAccountIdentityGeneration, clearMainAccountInfoCache, | ||
| getMainAccountCredentialPresence, getMainAccountInfoCache, | ||
| } from "../../src/codex/main-account-cache"; | ||
| import { MAIN_CODEX_ACCOUNT_ID } from "../../src/codex/main-account"; | ||
| import { clearAccountQuota } from "../../src/codex/quota"; | ||
| import { resetQuotaQueryBackoffForTests } from "../../src/codex/quota-query-backoff"; | ||
| import { resetLifecycleDrainStateForTests } from "../../src/server/lifecycle"; | ||
| import type { OcxConfig } from "../../src/types"; | ||
| import { removeTreeWithRetry } from "../helpers/remove-tree"; | ||
|
|
||
| let root: string; | ||
| let nativeHome: string; | ||
| let previousOcxHome: string | undefined; | ||
| let previousCodexHome: string | undefined; | ||
| let originalFetch: typeof fetch; | ||
| const poolId = "headless-pool"; | ||
|
|
||
| function config(codexIntegration = false): OcxConfig { | ||
| return { | ||
| port: 10100, defaultProvider: "openai", providers: {}, | ||
| clientIntegrations: { codex: codexIntegration }, | ||
| codexAccounts: [{ id: poolId, email: "pool@example.test", plan: "plus", isMain: false }], | ||
| }; | ||
| } | ||
|
|
||
| function savePool() { | ||
| saveCodexAccountCredential(poolId, { | ||
| accessToken: "fixture-pool-access", refreshToken: "fixture-pool-refresh", | ||
| chatgptAccountId: "fixture-pool-identity", expiresAt: Date.now() + 3_600_000, | ||
| }); | ||
| return getCodexAccountCredential(poolId); | ||
| } | ||
|
|
||
| function usageResponse(): Response { | ||
| return Response.json({ | ||
| plan_type: "plus", | ||
| rate_limit: { primary_window: { used_percent: 17, limit_window_seconds: 18_000 } }, | ||
| }); | ||
| } | ||
|
|
||
| async function listAccounts(cfg: OcxConfig, method = "GET", suffix = "") { | ||
| const url = new URL(`http://localhost/api/codex-auth/accounts${suffix}`); | ||
| const response = await handleCodexAuthAPI(new Request(url, { method }), url, cfg); | ||
| expect(response?.status).toBe(200); | ||
| return (await response!.json() as { accounts: CodexAuthAccountDto[] }).accounts; | ||
| } | ||
|
|
||
| function resetRuntimeState() { | ||
| clearMainAccountInfoCache(); | ||
| clearAccountQuota(); | ||
| clearAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID); | ||
| clearAccountNeedsReauth(poolId); | ||
| resetMainCodexAccountIdentityTrackingForTests(); | ||
| resetQuotaQueryBackoffForTests(); | ||
| resetLifecycleDrainStateForTests(); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| root = mkdtempSync(join(tmpdir(), "ocx-headless-accounts-")); | ||
| nativeHome = join(root, "missing-codex-home"); | ||
| previousOcxHome = process.env.OPENCODEX_HOME; | ||
| previousCodexHome = process.env.CODEX_HOME; | ||
| process.env.OPENCODEX_HOME = join(root, "ocx"); | ||
| process.env.CODEX_HOME = nativeHome; | ||
| mkdirSync(process.env.OPENCODEX_HOME); | ||
| originalFetch = globalThis.fetch; | ||
| globalThis.fetch = (async (input: RequestInfo | URL) => { | ||
| const url = new URL(input instanceof Request ? input.url : String(input)); | ||
| if (url.origin !== "https://chatgpt.com" || !url.pathname.endsWith("/wham/usage")) { | ||
| throw new Error("unexpected fixture network request"); | ||
| } | ||
| return usageResponse(); | ||
| }) as typeof fetch; | ||
| resetRuntimeState(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.fetch = originalFetch; | ||
| resetRuntimeState(); | ||
| if (previousOcxHome === undefined) delete process.env.OPENCODEX_HOME; | ||
| else process.env.OPENCODEX_HOME = previousOcxHome; | ||
| if (previousCodexHome === undefined) delete process.env.CODEX_HOME; | ||
| else process.env.CODEX_HOME = previousCodexHome; | ||
| removeTreeWithRetry(root); | ||
| }); | ||
|
|
||
| describe("account listing without a native Codex home", () => { | ||
| for (const integration of [false, true]) { | ||
| for (const [method, suffix] of [["GET", ""], ["GET", "?refresh=1"], ["POST", "/refresh"]]) { | ||
| test(`${method} accounts${suffix}, integration=${integration}: stored pool remains available`, async () => { | ||
| const credential = savePool(); | ||
| const accounts = await listAccounts(config(integration), method, suffix); | ||
| expect(accounts).toHaveLength(2); | ||
| expect(accounts.find(account => account.id === poolId)).toMatchObject({ | ||
| hasCredential: true, needsReauth: false, plan: "plus", quota: { shortPercent: 17 }, | ||
| }); | ||
| expect(accounts.find(account => account.id === MAIN_CODEX_ACCOUNT_ID)).toMatchObject({ | ||
| hasCredential: false, needsReauth: false, quota: null, | ||
| }); | ||
| expect(isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)).toBe(false); | ||
| expect(getCodexAccountCredential(poolId)).toEqual(credential); | ||
| expect(existsSync(nativeHome)).toBe(false); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| test("an explicit non-directory CODEX_HOME still refuses account listing", async () => { | ||
| writeFileSync(nativeHome, "fixture-not-a-directory"); | ||
| const credential = savePool(); | ||
| await expect(listAccounts(config())).rejects.toThrow("not a directory"); | ||
| expect(getCodexAccountCredential(poolId)).toEqual(credential); | ||
| expect(readFileSync(nativeHome, "utf8")).toBe("fixture-not-a-directory"); | ||
| }); | ||
|
|
||
| test("a home removed during pool refresh does not fail final account projection", async () => { | ||
| mkdirSync(nativeHome); | ||
| savePool(); | ||
| globalThis.fetch = (async () => { | ||
| removeTreeWithRetry(nativeHome); | ||
| return usageResponse(); | ||
| }) as typeof fetch; | ||
| const accounts = await listAccounts(config()); | ||
| expect(accounts.find(account => account.id === poolId)).toMatchObject({ | ||
| hasCredential: true, needsReauth: false, quota: { shortPercent: 17 }, | ||
| }); | ||
| expect(existsSync(nativeHome)).toBe(false); | ||
| }); | ||
|
|
||
| test("disabled client integration still observes an existing native login", async () => { | ||
| mkdirSync(nativeHome); | ||
| const auth = JSON.stringify({ tokens: { | ||
| access_token: "fixture-main-access", account_id: "fixture-main-identity", | ||
| } }); | ||
| writeFileSync(join(nativeHome, "auth.json"), auth); | ||
| savePool(); | ||
| const accounts = await listAccounts(config()); | ||
| expect(accounts.find(account => account.id === MAIN_CODEX_ACCOUNT_ID)).toMatchObject({ | ||
| hasCredential: true, needsReauth: false, plan: "plus", quota: { shortPercent: 17 }, | ||
| }); | ||
| expect(readFileSync(join(nativeHome, "auth.json"), "utf8")).toBe(auth); | ||
| }); | ||
|
|
||
| test("a missing home preserves previously observed identity and reauth state", async () => { | ||
| mkdirSync(nativeHome); | ||
| writeFileSync(join(nativeHome, "auth.json"), JSON.stringify({ tokens: { | ||
| access_token: "fixture-main-access", account_id: "fixture-main-identity", | ||
| } })); | ||
| savePool(); | ||
| await listAccounts(config()); | ||
| markAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID); | ||
| const generation = captureMainAccountIdentityGeneration(); | ||
| const info = getMainAccountInfoCache(); | ||
| expect(info?.quota?.shortPercent).toBe(17); | ||
| expect(getMainAccountCredentialPresence()).toBe(true); | ||
| removeTreeWithRetry(nativeHome); | ||
|
|
||
| const accounts = await listAccounts(config(), "GET", "?refresh=1"); | ||
| expect(accounts.find(account => account.id === MAIN_CODEX_ACCOUNT_ID)).toMatchObject({ | ||
| hasCredential: true, needsReauth: true, quota: null, | ||
| }); | ||
| expect(accounts.find(account => account.id === poolId)).toMatchObject({ | ||
| hasCredential: true, needsReauth: false, quota: { shortPercent: 17 }, | ||
| }); | ||
| expect(captureMainAccountIdentityGeneration()).toBe(generation); | ||
| expect(getMainAccountInfoCache()).toEqual(info); | ||
| expect(getMainAccountCredentialPresence()).toBe(true); | ||
| expect(isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID)).toBe(true); | ||
| expect(existsSync(nativeHome)).toBe(false); | ||
| }); | ||
|
|
||
| test("an unsafe OpenCodex storage root still refuses account listing", async () => { | ||
| mkdirSync(nativeHome); | ||
| const unsafeRoot = join(root, "not-a-config-directory"); | ||
| writeFileSync(unsafeRoot, "fixture-unsafe-root"); | ||
| process.env.OPENCODEX_HOME = unsafeRoot; | ||
| await expect(listAccounts(config())).rejects.toMatchObject({ code: "PROFILE_STORAGE_UNSAFE" }); | ||
| expect(readFileSync(unsafeRoot, "utf8")).toBe("fixture-unsafe-root"); | ||
| }); | ||
|
|
||
| test("credential operations still refuse a missing native home before executing", async () => { | ||
| const mutationPath = join(root, "must-not-be-written"); | ||
| await expect((async () => withNativeMainCredentialClaim(async () => { | ||
| writeFileSync(mutationPath, "unexpected mutation"); | ||
| }))()).rejects.toMatchObject({ code: "CODEX_HOME_UNAVAILABLE" }); | ||
| expect(existsSync(mutationPath)).toBe(false); | ||
| expect(existsSync(nativeHome)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.