Skip to content

Commit 2736ae1

Browse files
committed
fix(native-main): address latest review feedback
1 parent 42d63ef commit 2736ae1

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

‎src/providers/quota.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,10 +1102,10 @@ async function maybeFetchProviderQuota(
11021102
}
11031103

11041104
export async function fetchProviderQuotaReports(config: OcxConfig, forceRefresh = false): Promise<ProviderQuotaResponse> {
1105-
// A forced Pool refresh must share one account-list probe between the pre-signature and
1106-
// provider fetch. The commit-time signature still re-reads current state to reject races.
1107-
const prefetchedCodexSnapshot = forceRefresh && hasCodexPoolProvider(config)
1108-
? listCodexAuthAccountsSnapshot(config, true)
1105+
// A Pool report's cache signature and provider fetch must share one account snapshot.
1106+
// Preserve force semantics when deciding whether that snapshot refreshes upstream data.
1107+
const prefetchedCodexSnapshot = hasCodexPoolProvider(config)
1108+
? listCodexAuthAccountsSnapshot(config, forceRefresh)
11091109
: undefined;
11101110
const keyCandidate = cacheKeyWithAggregationState(config, prefetchedCodexSnapshot);
11111111
const key = typeof keyCandidate === "string" ? keyCandidate : await keyCandidate;

‎tests/keyring-smoke.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ describe("runKeyringSmoke", () => {
5858
test("zeros the buffer returned by the keyring", async () => {
5959
const entry = new MemoryKeyringEntry();
6060
const readback = Buffer.alloc(32, 0x5a);
61+
const generated = Buffer.alloc(32, 0x5a);
6162
entry.getSecret = async () => readback;
6263

6364
await runKeyringSmoke({
6465
createEntry: async () => entry,
65-
createRandomBytes: (size) => Buffer.alloc(size, 0x5a),
66+
createRandomBytes: () => generated,
6667
createId: () => "test-id",
6768
});
6869

6970
expect(readback.equals(Buffer.alloc(32))).toBe(true);
71+
expect(generated.equals(Buffer.alloc(32))).toBe(true);
7072
});
7173

7274
test("preserves a readback error when deletion returns false", async () => {
@@ -88,7 +90,8 @@ describe("runKeyringSmoke", () => {
8890
expect((error as Error).message).toContain("readback did not match");
8991
expect((error as Error & { cleanupError?: unknown }).cleanupError).toBeInstanceOf(Error);
9092
expect(Object.keys(error as Error)).toContain("cleanupError");
91-
expect(Bun.inspect(error)).toContain("could not delete the temporary entry");
93+
expect((error as Error & { cleanupError: Error }).cleanupError.message)
94+
.toContain("could not delete the temporary entry");
9295
expect(entry.deletes).toBe(1);
9396
expect(generated.equals(Buffer.alloc(32))).toBe(true);
9497
});

‎tests/provider-quota.test.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
1+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
22
import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
5+
import * as authApi from "../src/codex/auth-api";
56
import { clearAccountNeedsReauth, markAccountNeedsReauth } from "../src/codex/account-runtime-state";
67
import { clearMainAccountInfoCache } from "../src/codex/main-account-cache";
78
import { clearAccountQuota, updateAccountQuota } from "../src/codex/quota";
@@ -544,6 +545,35 @@ describe("fetchProviderQuotaReports", () => {
544545
expect(calls).toEqual(new Map([["chatgpt-main-account", 1], ["added-chatgpt-id", 1]]));
545546
});
546547

548+
test("one non-forced Pool refresh shares its probe snapshot before the commit recheck", async () => {
549+
saveCodexAccountCredential("added", {
550+
accessToken: "added-access", refreshToken: "added-refresh",
551+
expiresAt: Date.now() + 3600_000, chatgptAccountId: "added-chatgpt-id",
552+
});
553+
const config = testConfig();
554+
config.providers = { openai: config.providers.openai };
555+
config.codexAccounts = [{ id: "added", email: "[email protected]", plan: "prolite", isMain: false }];
556+
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
557+
const accountId = (init?.headers as Record<string, string> | undefined)?.["ChatGPT-Account-Id"];
558+
return new Response(JSON.stringify({
559+
plan_type: accountId === "added-chatgpt-id" ? "prolite" : "plus",
560+
rate_limit: { secondary_window: { used_percent: 25, reset_at: 1_999_000_000 } },
561+
}), { status: 200, headers: { "content-type": "application/json" } });
562+
}) as typeof fetch;
563+
564+
const snapshotSpy = spyOn(authApi, "listCodexAuthAccountsSnapshot");
565+
try {
566+
await fetchProviderQuotaReports(config);
567+
568+
// The cache-key and provider phases share call 1; call 2 is the intentional
569+
// post-probe commit-key recheck. Before the fix there were 3 calls.
570+
expect(snapshotSpy).toHaveBeenCalledTimes(2);
571+
expect(snapshotSpy.mock.calls.map(call => call[1] ?? false)).toEqual([false, false]);
572+
} finally {
573+
snapshotSpy.mockRestore();
574+
}
575+
});
576+
547577
test("all-excluded pool still returns a coverage-only OpenAI report", async () => {
548578
rmSync(join(codexHome, "auth.json"), { force: true });
549579
clearMainAccountInfoCache();

0 commit comments

Comments
 (0)