Skip to content

Commit 65347e0

Browse files
committed
fix(codex): renew managed credentials in active processes
1 parent 38c187b commit 65347e0

3 files changed

Lines changed: 198 additions & 12 deletions

File tree

‎apps/server/src/provider/CodexManagedRuntime.test.ts‎

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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.
22
import * as NodeOS from "node:os";
3+
import * as NodeChildProcess from "node:child_process";
4+
import * as NodeUtil from "node:util";
35
import * as NodeServices from "@effect/platform-node/NodeServices";
46
import { assert, it } from "@effect/vitest";
57
import { CodexSettings, EnvironmentId, ProviderInstanceId } from "@t3tools/contracts";
@@ -16,12 +18,28 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1618
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
1719
import * as CodexInstallation from "./CodexInstallation.ts";
1820
import { makeCodexManagedRuntime } from "./CodexManagedRuntime.ts";
21+
import { makeCodexManagedTokenSource } from "./CodexManagedTokenSource.ts";
1922
import * as ProviderCredentialStore from "./ProviderCredentialStore.ts";
2023
import { codexAppServerArgs } from "./codexLaunchArgs.ts";
2124
import { resolveManagedCodexHomeLayout } from "./CodexManagedHome.ts";
2225

2326
const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
2427
const 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+
);
2543
it.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
),

‎apps/server/src/provider/CodexManagedRuntime.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import * as Effect from "effect/Effect";
44
import * as FileSystem from "effect/FileSystem";
55
import * as Path from "effect/Path";
66
import * as Schema from "effect/Schema";
7+
import * as Scope from "effect/Scope";
78
import * as ServerConfig from "../config.ts";
89
import * as CodexInstallation from "./CodexInstallation.ts";
10+
import {
11+
makeCodexManagedTokenSource,
12+
managedCodexTokenCommand,
13+
} from "./CodexManagedTokenSource.ts";
914
import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts";
1015
import { materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts";
1116

@@ -22,12 +27,15 @@ const managedCodexLaunchArgs = [
2227
'model_providers.openai_token_sharing.base_url="https://api.openai.com/v1"',
2328
'model_providers.openai_token_sharing.model_catalog_url="https://api.openai.com/v1/models"',
2429
"features.api_key_model_discovery=true",
25-
'model_providers.openai_token_sharing.env_key="ACCESS_TOKEN"',
30+
`model_providers.openai_token_sharing.auth.command=${JSON.stringify(process.execPath)}`,
31+
`model_providers.openai_token_sharing.auth.args=${JSON.stringify(["-e", managedCodexTokenCommand])}`,
32+
"model_providers.openai_token_sharing.auth.timeout_ms=25000",
33+
"model_providers.openai_token_sharing.auth.refresh_interval_ms=30000",
2634
'model_providers.openai_token_sharing.wire_api="responses"',
2735
"model_providers.openai_token_sharing.requires_openai_auth=false",
2836
"model_providers.openai_token_sharing.supports_websockets=false",
2937
]
30-
.map((value) => `-c '${value}'`)
38+
.map((value) => `-c '${value.replaceAll("'", "'\"'\"'")}'`)
3139
.join(" ");
3240

3341
export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(function* (options: {
@@ -47,6 +55,11 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
4755
config.devUrl ?? `http://localhost:${config.port}`,
4856
).toString(),
4957
});
58+
const scope = yield* Scope.Scope;
59+
const tokenSource = yield* makeCodexManagedTokenSource(options.instanceId, auth.access).pipe(
60+
Effect.provideService(Scope.Scope, scope),
61+
Effect.cached,
62+
);
5063
const homeLayout = yield* resolveManagedCodexHomeLayout(
5164
config.stateDir,
5265
options.instanceId,
@@ -65,6 +78,7 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
6578
),
6679
);
6780
const credentials = yield* auth.access;
81+
const source = yield* tokenSource;
6882
yield* materializeCodexShadowHome(homeLayout).pipe(
6983
Effect.provideService(FileSystem.FileSystem, fs),
7084
Effect.provideService(Path.Path, path),
@@ -90,9 +104,13 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
90104
// Ambient CLI overrides cannot redirect a T3-owned token to a different provider.
91105
const environment: NodeJS.ProcessEnv = {
92106
...options.environment,
93-
ACCESS_TOKEN: credentials.accessToken,
107+
T3CODE_MANAGED_CODEX_AUTH_URL: source.url,
108+
T3CODE_MANAGED_CODEX_AUTH_SECRET: source.secret,
109+
T3CODE_MANAGED_CODEX_AUTH_ACCOUNT: credentials.clientId,
110+
...(process.versions.electron ? { ELECTRON_RUN_AS_NODE: "1" } : {}),
94111
CODEX_HOME: homePath,
95112
};
113+
delete environment.ACCESS_TOKEN;
96114
delete environment.T3CODE_CODEX_LAUNCH_ARGS;
97115
delete environment.OPENAI_API_KEY;
98116
delete environment.OPENAI_BASE_URL;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - scope-owned loopback bridge for Codex's credential command.
2+
import * as NodeCrypto from "node:crypto";
3+
import * as NodeHttp from "node:http";
4+
import { ProviderSetupError, type ProviderInstanceId } from "@t3tools/contracts";
5+
import * as Effect from "effect/Effect";
6+
7+
// Codex invokes this command again when its cached bearer expires or receives a 401.
8+
// The child receives a bridge credential, never a fixed copy of the provider token.
9+
export const managedCodexTokenCommand = `
10+
fetch(process.env.T3CODE_MANAGED_CODEX_AUTH_URL, {
11+
headers: { Authorization: "Bearer " + process.env.T3CODE_MANAGED_CODEX_AUTH_SECRET,
12+
"X-T3-Codex-Account": process.env.T3CODE_MANAGED_CODEX_AUTH_ACCOUNT },
13+
}).then(async response => {
14+
if (!response.ok) throw new Error();
15+
process.stdout.write(await response.text());
16+
}).catch(() => {
17+
process.stderr.write("Could not renew managed Codex credentials.\\n");
18+
process.exitCode = 1;
19+
});
20+
`;
21+
22+
export const makeCodexManagedTokenSource = Effect.fn("makeCodexManagedTokenSource")(function* (
23+
instanceId: ProviderInstanceId,
24+
access: Effect.Effect<
25+
{ readonly accessToken: string; readonly clientId: string },
26+
ProviderSetupError
27+
>,
28+
) {
29+
const context = yield* Effect.context<never>();
30+
const runPromise = Effect.runPromiseWith(context);
31+
const secret = NodeCrypto.randomBytes(32).toString("base64url");
32+
const server = yield* Effect.acquireRelease(
33+
Effect.tryPromise({
34+
try: () =>
35+
new Promise<NodeHttp.Server>((resolve, reject) => {
36+
const server = NodeHttp.createServer((request, response) => {
37+
if (request.method !== "GET" || request.url !== "/token") {
38+
response.writeHead(404).end();
39+
return;
40+
}
41+
if (request.headers.authorization !== `Bearer ${secret}`) {
42+
response.writeHead(401).end();
43+
return;
44+
}
45+
const abort = new AbortController();
46+
response.once("close", () => abort.abort());
47+
void runPromise(access, { signal: abort.signal }).then(
48+
(credentials) => {
49+
if (response.destroyed) return;
50+
if (request.headers["x-t3-codex-account"] !== credentials.clientId) {
51+
response.writeHead(409).end();
52+
return;
53+
}
54+
if (!response.destroyed)
55+
response
56+
.writeHead(200, {
57+
"content-type": "text/plain",
58+
"cache-control": "no-store",
59+
})
60+
.end(credentials.accessToken);
61+
},
62+
() => {
63+
if (!response.destroyed) response.writeHead(503).end();
64+
},
65+
);
66+
});
67+
server.once("error", reject);
68+
server.listen(0, "127.0.0.1", () => resolve(server));
69+
}),
70+
catch: () =>
71+
new ProviderSetupError({
72+
instanceId,
73+
operation: "runtime",
74+
detail: "Could not start the managed Codex credential bridge.",
75+
}),
76+
}),
77+
(server) =>
78+
Effect.promise(
79+
() =>
80+
new Promise<void>((resolve) => {
81+
server.closeAllConnections();
82+
server.close(() => resolve());
83+
}),
84+
),
85+
);
86+
const address = server.address();
87+
if (address === null || typeof address === "string")
88+
return yield* new ProviderSetupError({
89+
instanceId,
90+
operation: "runtime",
91+
detail: "Could not resolve the managed Codex credential bridge.",
92+
});
93+
return { url: `http://127.0.0.1:${address.port}/token`, secret };
94+
});

0 commit comments

Comments
 (0)