1- // @effect -diagnostics nodeBuiltinImport:off - checks the host's default Codex directory without writing to it.
1+ // @effect -diagnostics nodeBuiltinImport:off globalFetchInEffect:off - checks the host's default Codex directory without writing to it.
22import * as NodeOS from "node:os" ;
3+ import * as NodeChildProcess from "node:child_process" ;
4+ import * as NodeUtil from "node:util" ;
35import * as NodeServices from "@effect/platform-node/NodeServices" ;
46import { assert , it } from "@effect/vitest" ;
57import { CodexSettings , EnvironmentId , ProviderInstanceId } from "@t3tools/contracts" ;
@@ -16,12 +18,28 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1618import * as ServerEnvironment from "../environment/ServerEnvironment.ts" ;
1719import * as CodexInstallation from "./CodexInstallation.ts" ;
1820import { makeCodexManagedRuntime } from "./CodexManagedRuntime.ts" ;
21+ import { makeCodexManagedTokenSource } from "./CodexManagedTokenSource.ts" ;
1922import * as ProviderCredentialStore from "./ProviderCredentialStore.ts" ;
2023import { codexAppServerArgs } from "./codexLaunchArgs.ts" ;
2124import { resolveManagedCodexHomeLayout } from "./CodexManagedHome.ts" ;
2225
2326const encodeJson = Schema . encodeEffect ( Schema . fromJsonString ( Schema . Unknown ) ) ;
2427const decodeSettings = Schema . decodeSync ( CodexSettings ) ;
28+ it . effect ( "closes the managed credential bridge with its provider scope" , ( ) =>
29+ Effect . gen ( function * ( ) {
30+ const source = yield * makeCodexManagedTokenSource (
31+ ProviderInstanceId . make ( "closed-codex" ) ,
32+ Effect . succeed ( { accessToken : "dummy-token" , clientId : "dummy-account" } ) ,
33+ ) . pipe ( Effect . scoped ) ;
34+ const reached = yield * Effect . promise ( ( ) =>
35+ fetch ( source . url ) . then (
36+ ( ) => true ,
37+ ( ) => false ,
38+ ) ,
39+ ) ;
40+ assert . isFalse ( reached ) ;
41+ } ) ,
42+ ) ;
2543it . effect ( "managed home defaults to the global Codex home and honors configured home paths" , ( ) =>
2644 Effect . gen ( function * ( ) {
2745 const path = yield * Path . Path ;
@@ -149,7 +167,54 @@ it.effect.each(
149167 ) ;
150168 assert . isFalse ( yield * fs . exists ( path . join ( effective . config . homePath , "auth.json" ) ) ) ;
151169 }
152- assert . strictEqual ( effective . environment . ACCESS_TOKEN , "dummy-owned-access" ) ;
170+ // Emulate the same app-server making later requests, without resolving
171+ // or restarting its runtime. Run its configured credential command.
172+ const requestToken = Effect . promise ( async ( ) => {
173+ const argv = codexAppServerArgs ( effective . config . launchArgs ) ;
174+ const prefix = "model_providers.openai_token_sharing.auth.args=" ;
175+ const commandArgs = argv . find ( ( value ) => value . startsWith ( prefix ) ) ;
176+ if ( commandArgs === undefined ) return effective . environment . ACCESS_TOKEN ;
177+ const { stdout } = await NodeUtil . promisify ( NodeChildProcess . execFile ) (
178+ process . execPath ,
179+ JSON . parse ( commandArgs . slice ( prefix . length ) ) ,
180+ { env : effective . environment , timeout : 25_000 } ,
181+ ) ;
182+ return stdout ;
183+ } ) ;
184+ assert . equal ( yield * requestToken , "dummy-owned-access" ) ;
185+ const expired = { ...JSON . parse ( json ) , expiresAt : 0 } ;
186+ yield * store . set ( new TextEncoder ( ) . encode ( JSON . stringify ( expired ) ) ) ;
187+ // A provider status read renews the saved token, as in the report.
188+ assert . equal ( ( yield * runtime . auth . access ) . accessToken , "dummy-renewed-access" ) ;
189+ assert . deepEqual (
190+ yield * Effect . all ( [ requestToken , requestToken ] , { concurrency : "unbounded" } ) ,
191+ [ "dummy-renewed-access" , "dummy-renewed-access" ] ,
192+ ) ;
193+ assert . isUndefined ( effective . environment . ACCESS_TOKEN ) ;
194+ assert . notInclude ( JSON . stringify ( effective . environment ) , "dummy-owned-access" ) ;
195+ const rejected = yield * Effect . promise ( ( ) =>
196+ fetch ( effective . environment . T3CODE_MANAGED_CODEX_AUTH_URL ! ) ,
197+ ) ;
198+ assert . equal ( rejected . status , 401 ) ;
199+ yield * store . set (
200+ new TextEncoder ( ) . encode (
201+ JSON . stringify ( {
202+ ...expired ,
203+ clientId : "another-account" ,
204+ expiresAt : ( yield * Clock . currentTimeMillis ) + 3_600_000 ,
205+ } ) ,
206+ ) ,
207+ ) ;
208+ const switched = yield * Effect . promise ( ( ) =>
209+ fetch ( effective . environment . T3CODE_MANAGED_CODEX_AUTH_URL ! , {
210+ headers : {
211+ Authorization : `Bearer ${ effective . environment . T3CODE_MANAGED_CODEX_AUTH_SECRET } ` ,
212+ "X-T3-Codex-Account" : effective . environment . T3CODE_MANAGED_CODEX_AUTH_ACCOUNT ! ,
213+ } ,
214+ } ) ,
215+ ) ;
216+ assert . equal ( switched . status , 409 ) ;
217+ assert . equal ( yield * Effect . promise ( ( ) => switched . text ( ) ) , "" ) ;
153218 assert . isUndefined ( effective . environment . OPENAI_API_KEY ) ;
154219 assert . isUndefined ( effective . environment . OPENAI_BASE_URL ) ;
155220 assert . isUndefined ( effective . environment . T3CODE_CODEX_LAUNCH_ARGS ) ;
@@ -192,19 +257,28 @@ it.effect.each(
192257 [
193258 "https://auth.openai.com/.well-known/openid-configuration" ,
194259 "https://auth.openai.com/revoke" ,
260+ "https://auth.openai.com/api/accounts/oauth/token" ,
195261 ] . includes ( request . url ) ,
196262 ) ;
197263 return HttpClientResponse . fromWeb (
198264 request ,
199265 request . url . endsWith ( "/revoke" )
200266 ? new Response ( null , { status : 200 } )
201- : Response . json ( {
202- issuer : "https://auth.openai.com" ,
203- authorization_endpoint : "https://auth.openai.com/api/accounts/authorize" ,
204- token_endpoint : "https://auth.openai.com/api/accounts/oauth/token" ,
205- jwks_uri : "https://auth.openai.com/jwks" ,
206- revocation_endpoint : "https://auth.openai.com/revoke" ,
207- } ) ,
267+ : request . url . endsWith ( "/token" )
268+ ? Response . json ( {
269+ access_token : "dummy-renewed-access" ,
270+ refresh_token : "dummy-rotated-refresh" ,
271+ expires_in : 3600 ,
272+ token_type : "Bearer" ,
273+ scope : "chatgpt.tokens.use.direct" ,
274+ } )
275+ : Response . json ( {
276+ issuer : "https://auth.openai.com" ,
277+ authorization_endpoint : "https://auth.openai.com/api/accounts/authorize" ,
278+ token_endpoint : "https://auth.openai.com/api/accounts/oauth/token" ,
279+ jwks_uri : "https://auth.openai.com/jwks" ,
280+ revocation_endpoint : "https://auth.openai.com/revoke" ,
281+ } ) ,
208282 ) ;
209283 } ) ,
210284 ) ,
0 commit comments