@devries-cameron20 / t3code-1 / commits / c9dfc404cc
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): custom Codex providers no longer report native quota (#18622)

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

fixfon committed Nov 11, 2026 at 13:09 UTC c9dfc404ccde9a4c8be901b287026dd19e4ed19a
2 files changed +118 -29
apps/server/src/provider/CodexProvider.test.ts
+30 -1
@@ -1,6 +1,10 @@
1 import { assert, it } from "@effect/vitest";
2
3 -import { applyPreferredCodexDefaultModel, mapCodexModelCapabilities } from "./CodexProvider.ts";
3 +import {
4 + applyPreferredCodexDefaultModel,
5 + codexRoutesSpendNativeQuota,
6 + mapCodexModelCapabilities,
7 +} from "./CodexProvider.ts";
8
9 it("maps current Codex model capability fields", () => {
10 const capabilities = mapCodexModelCapabilities({
@@ -153,3 +157,28 @@ it("ignores custom models that shadow a preferred slug", () => {
157
158 assert.deepStrictEqual(models.find((model) => model.isDefault)?.slug, "gpt-5.4");
159 });
160 +
161 +it("reports native quota only for an OpenAI login on a native route", () => {
162 + const chatgpt = { type: "chatgpt", email: "a@example.com", 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
+88 -28
@@ -70,6 +70,44 @@ type CodexRateLimitsProbe =
70 }
71 | { readonly failure: string };
72
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 +
111 const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const;
112 const CODEX_CATALOG_REFRESH_TIMEOUT_MS = 3_000;
113 const CODEX_CONFIG_PROBE_TIMEOUT_MS = 3_000;
@@ -90,6 +128,7 @@ const CODEX_PRESENTATION = {
128
129 export interface CodexAppServerProviderSnapshot {
130 readonly account: CodexSchema.V2GetAccountResponse;
131 + readonly nativeUsage?: boolean;
132 readonly rateLimits?: CodexRateLimitsProbe;
133 readonly version: string | undefined;
134 readonly models: ReadonlyArray<ServerProviderModel> | undefined;
@@ -526,6 +565,34 @@ export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(fu
565 return { client, initialize };
566 });
567
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 +
596 const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(function* (input: {
597 readonly binaryPath: string;
598 readonly homePath?: string;
@@ -552,7 +619,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
619
620 // Optional discovery must finish before the outer timeout can discard the account result.
621 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(
623 [
624 withCodexDiscoveryDeadline(client.request("skills/list", { cwds: [input.cwd] }), deadlineMs),
625 accountResponse.account?.type === "chatgpt"
@@ -569,40 +636,32 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
636 deadlineMs,
637 CODEX_CONFIG_PROBE_TIMEOUT_MS,
638 ).pipe(
572 - Effect.map((response) => response?.config.service_tier ?? undefined),
639 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 + }),
657 ),
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 - ),
658 ],
659 { concurrency: "unbounded" },
660 );
661
662 return {
663 account: accountResponse,
664 + nativeUsage,
665 ...(rateLimits ? { rateLimits } : {}),
666 version,
667 models,
@@ -845,8 +904,9 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
904 const accountStatus = managedAuth
905 ? { status: "ready" as const, auth: managedAuth, message: undefined }
906 : accountProbeStatus(snapshot);
907 + // Sign-in guidance still applies when Codex itself says it needs an OpenAI login.
908 const usageLimits =
849 - snapshot.account.account?.type === "apiKey"
909 + !snapshot.nativeUsage && !accountStatus.message
910 ? makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" })
911 : snapshot.rateLimits === undefined || "failure" in snapshot.rateLimits
912 ? makeUnavailableUsageLimits({