Skip to content

Commit fb6a9ef

Browse files
committed
fix(server): treat an unreadable Codex route as unknown for quota
When `config/read` failed, the probe fell back to "no override", which `reportsNativeUsage` read as the built-in OpenAI route. A custom provider that requires OpenAI auth but points at a non-OpenAI `base_url` could then publish the stored ChatGPT login's quota. A failed read now yields an unknown route, so the quota is not fetched and usage is reported unavailable. A config that reads fine but sets no override keeps reporting native usage as before.
1 parent a6904a7 commit fb6a9ef

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎apps/server/src/provider/CodexProvider.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,16 @@ const isOpenAiEndpoint = (baseUrl: string): boolean => {
101101
* it is `false` for a custom provider even when a ChatGPT login is stored, and
102102
* a custom provider can set it `true` while routing to a non-OpenAI `base_url`.
103103
* Either way that login's quota is not what the provider's traffic spends. API
104-
* keys have no subscription windows.
104+
* keys have no subscription windows. A `null` `baseUrl` means the route could
105+
* not be read, so the quota cannot be attributed to it.
105106
*/
106107
const reportsNativeUsage = (
107108
account: CodexSchema.V2GetAccountResponse,
108-
baseUrl: string | undefined,
109+
baseUrl: string | null | undefined,
109110
): boolean =>
110111
account.requiresOpenaiAuth &&
111112
account.account?.type !== "apiKey" &&
112-
(baseUrl === undefined || isOpenAiEndpoint(baseUrl));
113+
(baseUrl === undefined || (baseUrl !== null && isOpenAiEndpoint(baseUrl)));
113114

114115
const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const;
115116
const CODEX_CATALOG_REFRESH_TIMEOUT = "3 seconds" as const;
@@ -124,7 +125,7 @@ const CODEX_PRESENTATION = {
124125

125126
export interface CodexAppServerProviderSnapshot {
126127
readonly account: CodexSchema.V2GetAccountResponse;
127-
readonly baseUrl?: string;
128+
readonly baseUrl?: string | null;
128129
readonly rateLimits?: CodexRateLimitsProbe;
129130
readonly version: string | undefined;
130131
readonly models: ReadonlyArray<ServerProviderModel>;
@@ -521,10 +522,9 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
521522
} satisfies CodexAppServerProviderSnapshot;
522523
}
523524

524-
// An unreadable config leaves the account alone to decide.
525525
const baseUrl = yield* client.request("config/read", {}).pipe(
526526
Effect.map((response) => activeProviderBaseUrl(response.config)),
527-
Effect.orElseSucceed(() => undefined),
527+
Effect.orElseSucceed(() => null),
528528
);
529529

530530
const [skillsResponse, models, rateLimits] = yield* Effect.all(

‎apps/server/src/provider/CodexProvider.usage.test.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ readline.createInterface({ input: process.stdin }).on("line", (line) => {
3131
} else if (method === "account/read") {
3232
write({ id, result: { account: { type: "chatgpt", email: "[email protected]", planType: "pro" }, requiresOpenaiAuth: true } });
3333
} else if (method === "config/read") {
34-
write({ id, result: { config: JSON.parse(process.env.FAKE_CODEX_CONFIG), origins: {} } });
34+
if (process.env.FAKE_CODEX_CONFIG === undefined) {
35+
write({ id, error: { code: -32603, message: "config unreadable" } });
36+
} else {
37+
write({ id, result: { config: JSON.parse(process.env.FAKE_CODEX_CONFIG), origins: {} } });
38+
}
3539
} else if (method === "account/rateLimits/read") {
3640
fs.writeFileSync(path.join(process.env.CODEX_HOME, "rate-limits-requested"), "");
3741
write({ id, result: { rateLimits: { primary: { usedPercent: 90, windowDurationMins: 10080, resetsAt: 1789436313 } } } });
@@ -47,7 +51,8 @@ readline.createInterface({ input: process.stdin }).on("line", (line) => {
4751

4852
const decodeCodexSettings = Schema.decodeSync(CodexSettings);
4953

50-
const usageFor = Effect.fn("usageFor")(function* (config: Record<string, unknown>) {
54+
/** A `config` of `undefined` makes the fake CLI fail `config/read`. */
55+
const usageFor = Effect.fn("usageFor")(function* (config: Record<string, unknown> | undefined) {
5156
const fs = yield* FileSystem.FileSystem;
5257
const path = yield* Path.Path;
5358
const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-usage-" });
@@ -58,7 +63,10 @@ const usageFor = Effect.fn("usageFor")(function* (config: Record<string, unknown
5863
const provider = yield* checkCodexProviderStatus(
5964
decodeCodexSettings({ binaryPath, homePath: dir }),
6065
undefined,
61-
{ ...process.env, FAKE_CODEX_CONFIG: JSON.stringify(config) },
66+
{
67+
...process.env,
68+
...(config === undefined ? {} : { FAKE_CODEX_CONFIG: JSON.stringify(config) }),
69+
},
6270
);
6371
return {
6472
usageLimits: provider.usageLimits,
@@ -112,4 +120,13 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("Codex native usage
112120
}
113121
}).pipe(Effect.scoped),
114122
);
123+
124+
it.effect("skips the ChatGPT quota when the routing config cannot be read", () =>
125+
Effect.gen(function* () {
126+
const { usageLimits, fetched } = yield* usageFor(undefined);
127+
assert.strictEqual(fetched, false);
128+
assert.deepStrictEqual(usageLimits?.windows, []);
129+
assert.strictEqual(usageLimits?.unavailable?.reason, "unsupported");
130+
}).pipe(Effect.scoped),
131+
);
115132
});

0 commit comments

Comments
 (0)