@@ -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+
73111const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const ;
74112const CODEX_CATALOG_REFRESH_TIMEOUT_MS = 3_000 ;
75113const CODEX_CONFIG_PROBE_TIMEOUT_MS = 3_000 ;
@@ -90,6 +128,7 @@ const CODEX_PRESENTATION = {
90128
91129export 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+
529596const 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