Skip to content

Commit c9dfc40

Browse files
fixfonclaudejuliusmarminge
authored
fix(server): custom Codex providers no longer report native quota (#18622)
Co-authored-by: Claude Sonnet 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 25f4c9d commit c9dfc40

2 files changed

Lines changed: 118 additions & 29 deletions

File tree

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { assert, it } from "@effect/vitest";
22

3-
import { applyPreferredCodexDefaultModel, mapCodexModelCapabilities } from "./CodexProvider.ts";
3+
import {
4+
applyPreferredCodexDefaultModel,
5+
codexRoutesSpendNativeQuota,
6+
mapCodexModelCapabilities,
7+
} from "./CodexProvider.ts";
48

59
it("maps current Codex model capability fields", () => {
610
const capabilities = mapCodexModelCapabilities({
@@ -153,3 +157,28 @@ it("ignores custom models that shadow a preferred slug", () => {
153157

154158
assert.deepStrictEqual(models.find((model) => model.isDefault)?.slug, "gpt-5.4");
155159
});
160+
161+
it("reports native quota only for an OpenAI login on a native route", () => {
162+
const chatgpt = { type: "chatgpt", email: "[email protected]", planType: "pro" } as const;
163+
const hub = {
164+
model_provider: "hub",
165+
model_providers: { hub: { base_url: "https://hub.example.com/v1" } },
166+
};
167+
const cases = [
168+
["default route", chatgpt, true, {}, true],
169+
["openai base url", chatgpt, true, { openai_base_url: "https://chatgpt.com/x" }, true],
170+
["openai proxy url", chatgpt, true, { openai_base_url: "https://proxy.example.com" }, false],
171+
["custom provider", chatgpt, true, hub, false],
172+
["unknown provider id", chatgpt, true, { model_provider: "missing" }, false],
173+
["unreadable config", chatgpt, true, null, false],
174+
["login not required", chatgpt, false, {}, false],
175+
["api key", { type: "apiKey" }, true, {}, false],
176+
] as const;
177+
for (const [name, account, requiresOpenaiAuth, config, expected] of cases) {
178+
assert.strictEqual(
179+
codexRoutesSpendNativeQuota({ account, requiresOpenaiAuth }, config),
180+
expected,
181+
name,
182+
);
183+
}
184+
});

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

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,44 @@ type CodexRateLimitsProbe =
7070
}
7171
| { readonly failure: string };
7272

73+
const CodexRoutingConfig = Schema.Struct({
74+
model_provider: Schema.optionalKey(Schema.NullOr(Schema.String)),
75+
openai_base_url: Schema.optionalKey(Schema.NullOr(Schema.String)),
76+
model_providers: Schema.optionalKey(
77+
Schema.Record(
78+
Schema.String,
79+
Schema.Struct({ base_url: Schema.optionalKey(Schema.NullOr(Schema.String)) }),
80+
),
81+
),
82+
});
83+
const decodeCodexRoutingConfig = Schema.decodeUnknownOption(CodexRoutingConfig);
84+
85+
function routesToNativeOpenai(config: unknown): boolean {
86+
const decoded = decodeCodexRoutingConfig(config);
87+
if (Option.isNone(decoded)) return false;
88+
const routing = decoded.value;
89+
const providerId = routing.model_provider ?? "openai";
90+
const provider = routing.model_providers?.[providerId];
91+
if (providerId !== "openai" && !provider) return false;
92+
const baseUrl =
93+
providerId === "openai" ? routing.openai_base_url || undefined : provider?.base_url;
94+
// A provider without an override uses Codex's auth-dependent OpenAI default.
95+
if (baseUrl == null) return true;
96+
if (!URL.canParse(baseUrl)) return false;
97+
const url = new URL(baseUrl);
98+
return (
99+
url.protocol === "https:" &&
100+
(url.hostname === "api.openai.com" || url.hostname === "chatgpt.com")
101+
);
102+
}
103+
104+
// A stored login does not mean this provider's traffic spends that login's quota.
105+
export const codexRoutesSpendNativeQuota = (
106+
account: CodexSchema.V2GetAccountResponse,
107+
config: unknown,
108+
): boolean =>
109+
account.requiresOpenaiAuth && account.account?.type !== "apiKey" && routesToNativeOpenai(config);
110+
73111
const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const;
74112
const CODEX_CATALOG_REFRESH_TIMEOUT_MS = 3_000;
75113
const CODEX_CONFIG_PROBE_TIMEOUT_MS = 3_000;
@@ -90,6 +128,7 @@ const CODEX_PRESENTATION = {
90128

91129
export interface CodexAppServerProviderSnapshot {
92130
readonly account: CodexSchema.V2GetAccountResponse;
131+
readonly nativeUsage?: boolean;
93132
readonly rateLimits?: CodexRateLimitsProbe;
94133
readonly version: string | undefined;
95134
readonly models: ReadonlyArray<ServerProviderModel> | undefined;
@@ -526,6 +565,34 @@ export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(fu
526565
return { client, initialize };
527566
});
528567

568+
// Usage is an enrichment: a failure or a slow answer degrades to "no usage this
569+
// probe" rather than costing the account and models.
570+
const readCodexRateLimits = (
571+
client: CodexClient.CodexAppServerClient["Service"],
572+
deadlineMs: number,
573+
) =>
574+
withCodexDiscoveryDeadline(
575+
client.request("account/rateLimits/read", null),
576+
deadlineMs,
577+
RATE_LIMITS_PROBE_TIMEOUT_MS,
578+
).pipe(
579+
Effect.map((response): CodexRateLimitsProbe =>
580+
response
581+
? {
582+
snapshot: response.rateLimits,
583+
rateLimitsByLimitId: response.rateLimitsByLimitId,
584+
resetCredits: response.rateLimitResetCredits,
585+
accountId: response.accountId,
586+
}
587+
: { failure: "Codex did not answer the usage request." },
588+
),
589+
Effect.catch((error) =>
590+
Effect.logDebug("Codex rate-limit read failed.", { cause: error }).pipe(
591+
Effect.as<CodexRateLimitsProbe>({ failure: codexRateLimitsFailureMessage(error) }),
592+
),
593+
),
594+
);
595+
529596
const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(function* (input: {
530597
readonly binaryPath: string;
531598
readonly homePath?: string;
@@ -552,7 +619,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
552619

553620
// Optional discovery must finish before the outer timeout can discard the account result.
554621
const deadlineMs = spawnedAtMs + CODEX_AUTH_PROBE_TIMEOUT_MS - CODEX_DISCOVERY_MARGIN_MS;
555-
const [skillsResponse, models, serviceTier, rateLimits] = yield* Effect.all(
622+
const [skillsResponse, models, { serviceTier, nativeUsage, rateLimits }] = yield* Effect.all(
556623
[
557624
withCodexDiscoveryDeadline(client.request("skills/list", { cwds: [input.cwd] }), deadlineMs),
558625
accountResponse.account?.type === "chatgpt"
@@ -569,40 +636,32 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
569636
deadlineMs,
570637
CODEX_CONFIG_PROBE_TIMEOUT_MS,
571638
).pipe(
572-
Effect.map((response) => response?.config.service_tier ?? undefined),
573639
Effect.catch(() => Effect.logDebug("Codex config read failed.").pipe(Effect.as(undefined))),
640+
Effect.flatMap((config) => {
641+
// Only a confirmed native route may probe or publish the login's quota.
642+
const nativeUsage =
643+
!input.skipNativeUsage &&
644+
config !== undefined &&
645+
codexRoutesSpendNativeQuota(accountResponse, config.config);
646+
const readUsage: Effect.Effect<CodexRateLimitsProbe | undefined> = nativeUsage
647+
? readCodexRateLimits(client, deadlineMs)
648+
: Effect.undefined;
649+
return readUsage.pipe(
650+
Effect.map((rateLimits) => ({
651+
serviceTier: config?.config.service_tier ?? undefined,
652+
nativeUsage,
653+
rateLimits,
654+
})),
655+
);
656+
}),
574657
),
575-
// Usage is an enrichment: a failure or a slow answer degrades to "no
576-
// usage this probe" rather than costing the account and models.
577-
input.skipNativeUsage
578-
? Effect.undefined
579-
: withCodexDiscoveryDeadline(
580-
client.request("account/rateLimits/read", null),
581-
deadlineMs,
582-
RATE_LIMITS_PROBE_TIMEOUT_MS,
583-
).pipe(
584-
Effect.map((response): CodexRateLimitsProbe =>
585-
response
586-
? {
587-
snapshot: response.rateLimits,
588-
rateLimitsByLimitId: response.rateLimitsByLimitId,
589-
resetCredits: response.rateLimitResetCredits,
590-
accountId: response.accountId,
591-
}
592-
: { failure: "Codex did not answer the usage request." },
593-
),
594-
Effect.catch((error) =>
595-
Effect.logDebug("Codex rate-limit read failed.", { cause: error }).pipe(
596-
Effect.as<CodexRateLimitsProbe>({ failure: codexRateLimitsFailureMessage(error) }),
597-
),
598-
),
599-
),
600658
],
601659
{ concurrency: "unbounded" },
602660
);
603661

604662
return {
605663
account: accountResponse,
664+
nativeUsage,
606665
...(rateLimits ? { rateLimits } : {}),
607666
version,
608667
models,
@@ -845,8 +904,9 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
845904
const accountStatus = managedAuth
846905
? { status: "ready" as const, auth: managedAuth, message: undefined }
847906
: accountProbeStatus(snapshot);
907+
// Sign-in guidance still applies when Codex itself says it needs an OpenAI login.
848908
const usageLimits =
849-
snapshot.account.account?.type === "apiKey"
909+
!snapshot.nativeUsage && !accountStatus.message
850910
? makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" })
851911
: snapshot.rateLimits === undefined || "failure" in snapshot.rateLimits
852912
? makeUnavailableUsageLimits({

0 commit comments

Comments
 (0)