11// @effect -diagnostics nodeBuiltinImport:off - checks the host's default Codex directory without writing to it.
2+ import * as NodeURL from "node:url" ;
3+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
4+ import * as Stream from "effect/Stream" ;
5+ import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
6+ import * as CodexManagedTokenBroker from "./CodexManagedTokenBroker.ts" ;
27import * as NodeServices from "@effect/platform-node/NodeServices" ;
38import { assert , it } from "@effect/vitest" ;
49import { CodexSettings , EnvironmentId , ProviderInstanceId } from "@t3tools/contracts" ;
@@ -20,7 +25,10 @@ import { codexAppServerArgs } from "./codexLaunchArgs.ts";
2025
2126const encodeJson = Schema . encodeEffect ( Schema . fromJsonString ( Schema . Unknown ) ) ;
2227const decodeSettings = Schema . decodeSync ( CodexSettings ) ;
23- it . effect . each (
28+ const decodeCommand = Schema . decodeUnknownSync (
29+ Schema . fromJsonString ( Schema . Tuple ( [ Schema . String , Schema . Array ( Schema . String ) ] ) ) ,
30+ ) ;
31+ it . live . each (
2432 ( [ "managed" , "local" ] as const ) . flatMap ( ( source ) =>
2533 ( [ "primary" , "additional" ] as const ) . map ( ( account ) => ( { source, account } ) ) ,
2634 ) ,
@@ -37,6 +45,7 @@ it.effect.each(
3745 yield * fs . writeFileString ( path . join ( sharedHome , "auth.json" ) , "native-auth-unchanged" ) ;
3846 yield * fs . writeFileString ( path . join ( sharedHome , "config.toml" ) , "# shared config\n" ) ;
3947 const data = new Map < string , Uint8Array > ( ) ;
48+ let renewals = 0 ;
4049 const secrets = ServerSecretStore . ServerSecretStore . of ( {
4150 get : ( key ) => Effect . sync ( ( ) => Option . fromUndefinedOr ( data . get ( key ) ) ) ,
4251 set : ( key , value ) =>
@@ -87,10 +96,13 @@ it.effect.each(
8796 yield * store . set ( new TextEncoder ( ) . encode ( json ) ) ;
8897 const ambient = {
8998 CODEX_HOME : "/user/.codex" ,
99+ ACCESS_TOKEN : "ambient-bearer" ,
100+ T3CODE_HOME : path . join ( sharedHome , "wrong-server-home" ) ,
90101 OPENAI_API_KEY : "dummy-global-key" ,
91102 OPENAI_BASE_URL : "https://user-proxy.test" ,
92103 T3CODE_CODEX_LAUNCH_ARGS : "--config model_provider=global-proxy" ,
93- PATH : "/usr/bin" ,
104+ PATH : process . env . PATH ,
105+ SystemRoot : process . env . SystemRoot ,
94106 } ;
95107 const runtime = yield * makeCodexManagedRuntime ( {
96108 instanceId,
@@ -121,27 +133,78 @@ it.effect.each(
121133 ) ;
122134 assert . isFalse ( yield * fs . exists ( path . join ( effective . config . homePath , "auth.json" ) ) ) ;
123135 }
124- assert . strictEqual ( effective . environment . ACCESS_TOKEN , "dummy-owned-access" ) ;
136+ assert . isUndefined ( effective . environment . ACCESS_TOKEN ) ;
125137 assert . isUndefined ( effective . environment . OPENAI_API_KEY ) ;
126138 assert . isUndefined ( effective . environment . OPENAI_BASE_URL ) ;
127139 assert . isUndefined ( effective . environment . T3CODE_CODEX_LAUNCH_ARGS ) ;
128140 const args = codexAppServerArgs ( effective . config . launchArgs ) ;
129- assert . include (
130- args ,
131- 'model_providers.openai_token_sharing.base_url="https://api.openai.com/v1"' ,
132- ) ;
133- assert . include (
134- args ,
135- 'model_providers.openai_token_sharing.model_catalog_url="https://api.openai.com/v1/models"' ,
136- ) ;
141+ assert . include ( args , 'model_provider="openai_token_sharing"' ) ;
142+ const provider = args . find ( ( arg ) =>
143+ arg . startsWith ( "model_providers.openai_token_sharing=" ) ,
144+ ) ! ;
145+ assert . include ( provider , 'base_url="https://api.openai.com/v1"' ) ;
146+ assert . include ( provider , 'model_catalog_url="https://api.openai.com/v1/models"' ) ;
147+ assert . notInclude ( provider , "env_key" ) ;
148+ assert . include ( provider , "refresh_interval_ms=0" ) ;
149+ // regression: #16132 Codex fetches each token through this helper, so a running
150+ // session picks up renewals instead of keeping the token it launched with.
151+ if ( source === "managed" && account === "primary" ) {
152+ const commandMatch = / a u t h = \{ c o m m a n d = ( .* ) , a r g s = ( \[ .* \] ) , t i m e o u t _ m s = / u. exec ( provider ) ;
153+ assert . isNotNull ( commandMatch ) ;
154+ const [ command , helperArgs ] = decodeCommand (
155+ `[${ commandMatch ! [ 1 ] } ,${ commandMatch ! [ 2 ] } ]` ,
156+ ) ;
157+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner ;
158+ const runHelper = Effect . gen ( function * ( ) {
159+ const child = yield * spawner . spawn (
160+ ChildProcess . make ( command , helperArgs , {
161+ extendEnv : false ,
162+ env : effective . environment ,
163+ } ) ,
164+ ) ;
165+ const [ stdout , stderr , code ] = yield * Effect . all (
166+ [
167+ child . stdout . pipe ( Stream . decodeText ( ) , Stream . mkString ) ,
168+ child . stderr . pipe ( Stream . decodeText ( ) , Stream . mkString ) ,
169+ child . exitCode ,
170+ ] ,
171+ { concurrency : "unbounded" } ,
172+ ) ;
173+ return { stdout, stderr, code : Number ( code ) } ;
174+ } ) . pipe ( Effect . scoped ) ;
175+ assert . deepEqual ( yield * runHelper , {
176+ stdout : "dummy-owned-access" ,
177+ stderr : "" ,
178+ code : 0 ,
179+ } ) ;
180+ const saved = Option . getOrThrow ( yield * runtime . auth . read ) ;
181+ const expired = yield * encodeJson ( { ...saved , expiresAt : 0 } ) ;
182+ yield * store . set ( new TextEncoder ( ) . encode ( expired ) ) ;
183+ assert . deepEqual ( yield * runHelper , { stdout : "renewed-access" , stderr : "" , code : 0 } ) ;
184+ assert . equal ( renewals , 1 ) ;
185+ assert . equal (
186+ Option . getOrThrow ( yield * runtime . auth . read ) . refreshToken ,
187+ "renewed-refresh" ,
188+ ) ;
189+ assert . equal ( ( yield * runtime . resolve ) . revision , effective . revision ) ;
190+ const replaced = yield * encodeJson ( { ...saved , subject : "replacement-user" } ) ;
191+ yield * store . set ( new TextEncoder ( ) . encode ( replaced ) ) ;
192+ const refused = yield * runHelper ;
193+ assert . equal ( refused . code , 1 ) ;
194+ assert . equal ( refused . stdout , "" ) ;
195+ assert . isFalse ( yield * fs . exists ( ambient . T3CODE_HOME ) ) ;
196+ }
137197 assert . include ( args , "features.api_key_model_discovery=true" ) ;
138198 assert . include ( args , "features.multi_agent=false" ) ;
139199 assert . include ( args , "features.multi_agent_v2=false" ) ;
140200 assert . notInclude ( effective . config . launchArgs , "model_catalog_json" ) ;
141201 assert . notInclude ( effective . config . launchArgs , "x-openai-chatpass-test" ) ;
142- assert . include ( args , "model_providers.openai_token_sharing.supports_websockets=false" ) ;
143- assert . include ( args , "model_providers.openai_token_sharing.requires_openai_auth=false" ) ;
144- assert . notInclude ( effective . config . launchArgs , "dummy-owned-access" ) ;
202+ assert . include ( provider , "supports_websockets=false" ) ;
203+ assert . include ( provider , "requires_openai_auth=false" ) ;
204+ for ( const secret of [ "dummy-owned-access" , "dummy-refresh" , "ambient-bearer" ] ) {
205+ assert . notInclude ( effective . config . launchArgs , secret ) ;
206+ assert . notInclude ( JSON . stringify ( effective . environment ) , secret ) ;
207+ }
145208 assert . strictEqual ( ambient . CODEX_HOME , "/user/.codex" ) ;
146209 } ) . pipe ( Effect . scoped ) ;
147210 assert . strictEqual ( leases , 0 ) ;
@@ -151,6 +214,11 @@ it.effect.each(
151214 "native-auth-unchanged" ,
152215 ) ;
153216 } ) . pipe (
217+ Effect . provide ( CodexManagedTokenBroker . layer ) ,
218+ Effect . provideService ( HostProcess . Arguments , [
219+ process . execPath ,
220+ NodeURL . fileURLToPath ( new URL ( "../bin.ts" , import . meta. url ) ) ,
221+ ] ) ,
154222 Effect . provideService ( ServerSecretStore . ServerSecretStore , secrets ) ,
155223 Effect . provideService ( ServerEnvironment . ServerEnvironmentIdentity , {
156224 getEnvironmentId : Effect . succeed (
@@ -166,8 +234,22 @@ it.effect.each(
166234 [
167235 "https://auth.openai.com/.well-known/openid-configuration" ,
168236 "https://auth.openai.com/revoke" ,
237+ "https://auth.openai.com/api/accounts/oauth/token" ,
169238 ] . includes ( request . url ) ,
170239 ) ;
240+ if ( request . url . endsWith ( "/token" ) ) {
241+ renewals ++ ;
242+ return HttpClientResponse . fromWeb (
243+ request ,
244+ Response . json ( {
245+ access_token : "renewed-access" ,
246+ refresh_token : "renewed-refresh" ,
247+ token_type : "Bearer" ,
248+ expires_in : 3600 ,
249+ scope : "chatgpt.tokens.use.direct" ,
250+ } ) ,
251+ ) ;
252+ }
171253 return HttpClientResponse . fromWeb (
172254 request ,
173255 request . url . endsWith ( "/revoke" )
0 commit comments