Skip to content

Commit 0be58ff

Browse files
fixfonclaude
andcommitted
fix(server): custom Codex providers no longer report native quota
A Codex instance routed through a custom model_provider (for example a CLIProxyAPI hub) reports `requiresOpenaiAuth: false`, but the app-server can still answer `account/rateLimits/read` from a ChatGPT login stored in CODEX_HOME. That quota is not what the provider's traffic spends, yet it was published as the instance's native limits, so Usage drew the same subscription once per instance or environment next to the hub's rows. Codex usage is now "unsupported" unless the provider authenticates through OpenAI (`requiresOpenaiAuth`) and is not an API key, and the usage read is skipped in those cases instead of being fetched and discarded. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 58d2532 commit 0be58ff

2 files changed

Lines changed: 60 additions & 16 deletions

File tree

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ type CodexRateLimitsProbe =
6868
}
6969
| { readonly failure: string };
7070

71+
/**
72+
* Whether `account/rateLimits/read` describes quota this provider draws on.
73+
* `requiresOpenaiAuth` follows the configured `model_provider`, not the login:
74+
* it is `false` for a custom provider even when a ChatGPT login is stored, and
75+
* that stored login's quota is not what the provider's traffic spends. API keys
76+
* have no subscription windows at all.
77+
*/
78+
const reportsNativeUsage = (account: CodexSchema.V2GetAccountResponse): boolean =>
79+
account.requiresOpenaiAuth && account.account?.type !== "apiKey";
80+
7181
const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const;
7282
const CODEX_CATALOG_REFRESH_TIMEOUT = "3 seconds" as const;
7383
const CODEX_CATALOG_REFRESH_POLL = "100 millis" as const;
@@ -530,7 +540,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
530540
: requestAllCodexModels(client),
531541
// Usage is an enrichment: a failure or a slow answer degrades to "no
532542
// usage this probe" rather than costing the account and models.
533-
input.skipNativeUsage
543+
input.skipNativeUsage || !reportsNativeUsage(accountResponse)
534544
? Effect.succeed(undefined)
535545
: client.request("account/rateLimits/read", null).pipe(
536546
Effect.map((response): CodexRateLimitsProbe => ({
@@ -757,21 +767,20 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
757767
const accountStatus = managedAuth
758768
? { status: "ready" as const, auth: managedAuth, message: undefined }
759769
: accountProbeStatus(snapshot);
760-
const usageLimits =
761-
snapshot.account.account?.type === "apiKey"
762-
? makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" })
763-
: snapshot.rateLimits === undefined || "failure" in snapshot.rateLimits
764-
? makeUnavailableUsageLimits({
765-
checkedAt,
766-
reason: "probeFailed",
767-
...(snapshot.rateLimits ? { message: snapshot.rateLimits.failure } : {}),
768-
})
769-
: codexRateLimitsToLimits({
770-
snapshot: snapshot.rateLimits.snapshot,
771-
rateLimitsByLimitId: snapshot.rateLimits.rateLimitsByLimitId,
772-
resetCredits: snapshot.rateLimits.resetCredits,
773-
checkedAt,
774-
});
770+
const usageLimits = !reportsNativeUsage(snapshot.account)
771+
? makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" })
772+
: snapshot.rateLimits === undefined || "failure" in snapshot.rateLimits
773+
? makeUnavailableUsageLimits({
774+
checkedAt,
775+
reason: "probeFailed",
776+
...(snapshot.rateLimits ? { message: snapshot.rateLimits.failure } : {}),
777+
})
778+
: codexRateLimitsToLimits({
779+
snapshot: snapshot.rateLimits.snapshot,
780+
rateLimitsByLimitId: snapshot.rateLimits.rateLimitsByLimitId,
781+
resetCredits: snapshot.rateLimits.resetCredits,
782+
checkedAt,
783+
});
775784

776785
return buildServerProvider({
777786
presentation: CODEX_PRESENTATION,

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,41 @@ it.layer(
547547
}),
548548
);
549549

550+
it.effect("publishes native quota only when Codex authenticates through OpenAI", () =>
551+
Effect.gen(function* () {
552+
const rateLimits = {
553+
snapshot: {
554+
primary: { usedPercent: 90, windowDurationMins: 10080, resetsAt: 1789436313 },
555+
},
556+
rateLimitsByLimitId: null,
557+
resetCredits: null,
558+
};
559+
const usageFor = (account: CodexAppServerProviderSnapshot["account"]) =>
560+
checkCodexProviderStatus(defaultCodexSettings, () =>
561+
Effect.succeed(makeCodexProbeSnapshot({ account, rateLimits })),
562+
).pipe(Effect.map((status) => status.usageLimits));
563+
564+
const native = yield* usageFor({
565+
account: { type: "chatgpt", email: "[email protected]", planType: "pro" },
566+
requiresOpenaiAuth: true,
567+
});
568+
assert.strictEqual(native?.windows.length, 1);
569+
assert.strictEqual(native?.unavailable, undefined);
570+
571+
// A custom model_provider reports `requiresOpenaiAuth: false`, yet the
572+
// CLI can still answer the usage read from a stored ChatGPT login that
573+
// the provider's traffic never draws on.
574+
for (const account of [
575+
{ account: null, requiresOpenaiAuth: false },
576+
{ account: { type: "apiKey" as const }, requiresOpenaiAuth: false },
577+
]) {
578+
const custom = yield* usageFor(account);
579+
assert.deepStrictEqual(custom?.windows, []);
580+
assert.strictEqual(custom?.unavailable?.reason, "unsupported");
581+
}
582+
}),
583+
);
584+
550585
it.effect("returns an Amazon Bedrock label for codex Bedrock auth", () =>
551586
Effect.gen(function* () {
552587
const status = yield* checkCodexProviderStatus(defaultCodexSettings, () =>

0 commit comments

Comments
 (0)