11import * as DateTime from "effect/DateTime" ;
22import * as Duration from "effect/Duration" ;
33import * as Effect from "effect/Effect" ;
4+ import * as FileSystem from "effect/FileSystem" ;
45import * as Layer from "effect/Layer" ;
56import * as Option from "effect/Option" ;
7+ import * as Path from "effect/Path" ;
68import * as Result from "effect/Result" ;
79import * as Schema from "effect/Schema" ;
10+ import * as Schedule from "effect/Schedule" ;
811import * as Scope from "effect/Scope" ;
912import * as Types from "effect/Types" ;
1013import * as ChildProcess from "effect/process/ChildProcess" ;
@@ -66,6 +69,9 @@ type CodexRateLimitsProbe =
6669 | { readonly failure : string } ;
6770
6871const CODEX_APP_SERVER_PROBE_FORCE_KILL_AFTER = "2 seconds" as const ;
72+ const CODEX_CATALOG_REFRESH_TIMEOUT = "3 seconds" as const ;
73+ const CODEX_CATALOG_REFRESH_POLL = "100 millis" as const ;
74+ const CODEX_CLIENT_NAME = "T3 Code" ;
6975
7076const CODEX_PRESENTATION = {
7177 displayName : "Codex" ,
@@ -347,11 +353,81 @@ const requestAllCodexModels = Effect.fn("requestAllCodexModels")(function* (
347353 return models ;
348354} ) ;
349355
356+ const decodeCodexModelsCacheStamp = Schema . decodeUnknownOption (
357+ Schema . fromJsonString ( Schema . Struct ( { fetched_at : Schema . String } ) ) ,
358+ ) ;
359+
360+ /** When Codex last wrote `models_cache.json`, in epoch millis; NaN when unreadable. */
361+ const readCodexModelsCacheFetchedAt = Effect . fn ( "readCodexModelsCacheFetchedAt" ) ( function * (
362+ codexHome : string ,
363+ ) {
364+ const fileSystem = yield * FileSystem . FileSystem ;
365+ const path = yield * Path . Path ;
366+ const text = yield * fileSystem
367+ . readFileString ( path . join ( codexHome , "models_cache.json" ) )
368+ . pipe ( Effect . option ) ;
369+ return Option . flatMap ( text , decodeCodexModelsCacheStamp ) . pipe (
370+ Option . flatMap ( ( stamp ) => DateTime . make ( stamp . fetched_at ) ) ,
371+ Option . match ( { onNone : ( ) => Number . NaN , onSome : DateTime . toEpochMillis } ) ,
372+ ) ;
373+ } ) ;
374+
375+ /**
376+ * Lists the ChatGPT model catalog as fetched for T3 Code.
377+ *
378+ * `model/list` serves `CODEX_HOME/models_cache.json` while it is under five
379+ * minutes old. Every Codex client on the machine shares that file, and the
380+ * backend tailors the catalog to each client's originator: the plain CLI does
381+ * not get Sol Ultrafast. So the first answer can be another client's catalog.
382+ * App-server refreshes the catalog online at startup, so wait until a write
383+ * newer than this process's spawn lands, then list again. If no refresh
384+ * lands in time, keep the first answer. Another client writing in the same
385+ * few hundred milliseconds can still win; the next probe corrects it.
386+ */
387+ const requestCodexChatGptModels = Effect . fn ( "requestCodexChatGptModels" ) ( function * ( input : {
388+ readonly client : CodexClient . CodexAppServerClient [ "Service" ] ;
389+ readonly codexHome : string ;
390+ readonly spawnedAtMs : number ;
391+ } ) {
392+ const models = yield * requestAllCodexModels ( input . client ) ;
393+ const refreshed = yield * readCodexModelsCacheFetchedAt ( input . codexHome ) . pipe (
394+ Effect . repeat ( {
395+ schedule : Schedule . spaced ( CODEX_CATALOG_REFRESH_POLL ) ,
396+ until : ( fetchedAtMs ) => fetchedAtMs >= input . spawnedAtMs ,
397+ } ) ,
398+ Effect . timeoutOption ( CODEX_CATALOG_REFRESH_TIMEOUT ) ,
399+ ) ;
400+ if ( Option . isNone ( refreshed ) ) {
401+ yield * Effect . logWarning (
402+ "Codex did not refresh its model catalog in time; using the cached catalog." ,
403+ ) ;
404+ return models ;
405+ }
406+ return yield * requestAllCodexModels ( input . client ) ;
407+ } ) ;
408+
409+ /**
410+ * Environment for every `codex app-server` T3 Code spawns.
411+ *
412+ * Codex sends its client name as the `originator` header, and the backend
413+ * tailors the model catalog to it. App-server starts its catalog refresh
414+ * before `initialize` names the client, so that first fetch goes out as the
415+ * plain CLI, misses tiers like Sol Ultrafast, and overwrites the shared
416+ * `models_cache.json`. Naming the originator at spawn closes that race.
417+ * A value the user already set wins.
418+ */
419+ export const codexAppServerEnvironment = (
420+ environment : NodeJS . ProcessEnv | undefined ,
421+ ) : NodeJS . ProcessEnv => ( {
422+ CODEX_INTERNAL_ORIGINATOR_OVERRIDE : CODEX_CLIENT_NAME ,
423+ ...environment ,
424+ } ) ;
425+
350426export function buildCodexInitializeParams ( ) : CodexSchema . V1InitializeParams {
351427 return {
352428 clientInfo : {
353- name : "T3 Code" ,
354- title : "T3 Code" ,
429+ name : CODEX_CLIENT_NAME ,
430+ title : CODEX_CLIENT_NAME ,
355431 version : packageJson . version ,
356432 } ,
357433 capabilities : {
@@ -382,7 +458,7 @@ export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(fu
382458 : undefined ;
383459 const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
384460 const environment = {
385- ...input . environment ,
461+ ...codexAppServerEnvironment ( input . environment ) ,
386462 ...( resolvedHomePath ? { CODEX_HOME : resolvedHomePath } : { } ) ,
387463 } ;
388464 const spawnCommand = yield * resolveSpawnCommand (
@@ -427,6 +503,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
427503 readonly environment ?: NodeJS . ProcessEnv ;
428504 readonly skipNativeUsage ?: boolean ;
429505} ) {
506+ const spawnedAtMs = DateTime . toEpochMillis ( yield * DateTime . now ) ;
430507 const { client, initialize } = yield * withCodexAppServerClient ( input ) ;
431508
432509 // Extract the version string after the first '/' in userAgent, up to the next space or the end
@@ -448,7 +525,9 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
448525 client . request ( "skills/list" , {
449526 cwds : [ input . cwd ] ,
450527 } ) ,
451- requestAllCodexModels ( client ) ,
528+ accountResponse . account ?. type === "chatgpt"
529+ ? requestCodexChatGptModels ( { client, codexHome : initialize . codexHome , spawnedAtMs } )
530+ : requestAllCodexModels ( client ) ,
452531 // Usage is an enrichment: a failure or a slow answer degrades to "no
453532 // usage this probe" rather than costing the account and models.
454533 input . skipNativeUsage
@@ -589,14 +668,14 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
589668 } ) => Effect . Effect <
590669 CodexAppServerProviderSnapshot ,
591670 CodexErrors . CodexAppServerError ,
592- ChildProcessSpawner . ChildProcessSpawner | Scope . Scope
671+ ChildProcessSpawner . ChildProcessSpawner | FileSystem . FileSystem | Path . Path | Scope . Scope
593672 > = probeCodexAppServerProvider ,
594673 environment ?: NodeJS . ProcessEnv ,
595674 managedAuth ?: ServerProvider [ "auth" ] ,
596675) : Effect . fn . Return <
597676 ServerProviderDraft ,
598677 ServerSettingsError ,
599- ChildProcessSpawner . ChildProcessSpawner
678+ ChildProcessSpawner . ChildProcessSpawner | FileSystem . FileSystem | Path . Path
600679> {
601680 const resolvedEnvironment = environment ?? process . env ;
602681 const checkedAt = DateTime . formatIso ( yield * DateTime . now ) ;
0 commit comments