Skip to content

Commit f5cf9a0

Browse files
fix(server): renew active managed Codex turns through native auth (pingdotgg#21787)
1 parent 478fdfc commit f5cf9a0

10 files changed

Lines changed: 287 additions & 51 deletions

‎apps/server/src/bin.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Every ACP agent spawns `t3 acp-mcp-bridge` while opening its session, and terminal-fallback
33
* agents run `t3 acp-mcp-call` per tool call, so their startup sits on first-message latency.
4-
* Both dispatch here before the full CLI module graph (seconds of evaluation) loads; everything
5-
* else defers to the real CLI in ./binCli.ts.
4+
* They and Codex's managed-token helper dispatch before the full CLI module graph (seconds
5+
* of evaluation) loads; everything else defers to the real CLI in ./binCli.ts.
66
*/
77
import { isEntrypoint } from "./entrypoint.ts";
88

@@ -17,7 +17,10 @@ if (
1717
// Guard sockets before either dispatch path can load outbound-capable modules.
1818
const { ignoreMacosTypeOfServiceEinval } = await import("./outboundHttp.ts");
1919
ignoreMacosTypeOfServiceEinval();
20-
if (command === "acp-mcp-bridge" || command === "acp-mcp-call") {
20+
if (command === "__managed-chatgpt-token") {
21+
const { runManagedChatGptTokenCli } = await import("./cli/managedChatGptToken.ts");
22+
await runManagedChatGptTokenCli(process.argv.slice(3));
23+
} else if (command === "acp-mcp-bridge" || command === "acp-mcp-call") {
2124
const { runAcpMcpCliFastPath } = await import("./mcp/AcpMcpStdioBridge.ts");
2225
await runAcpMcpCliFastPath(command, process.argv.slice(3));
2326
} else {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Lean self-invoked local IPC client; no auth/store graph.
2+
import * as NodeNet from "node:net";
3+
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
5+
import * as Schema from "effect/Schema";
6+
import * as Protocol from "../provider/CodexManagedTokenProtocol.ts";
7+
8+
const decodeArguments = Schema.decodeUnknownEffect(
9+
Schema.Tuple([
10+
Schema.NonEmptyString,
11+
Protocol.Request.fields.instanceId,
12+
Protocol.Request.fields.environmentId,
13+
Protocol.Request.fields.profileKey,
14+
]),
15+
);
16+
const encodeRequest = Schema.encodeSync(Schema.fromJsonString(Protocol.Request));
17+
const decodeResponse = Schema.decodeUnknownEffect(Schema.fromJsonString(Protocol.Response), {
18+
onExcessProperty: "error",
19+
});
20+
21+
/** Codex's auth.command protocol is a bare token on stdout, never a CLI diagnostic. */
22+
export async function runManagedChatGptTokenCli(args: ReadonlyArray<string>): Promise<void> {
23+
const program = Effect.gen(function* () {
24+
const [endpoint, instanceId, environmentId, profileKey] = yield* decodeArguments(args);
25+
const socket = yield* Effect.acquireRelease(
26+
Effect.sync(() => NodeNet.createConnection(endpoint)),
27+
(socket) => Effect.sync(() => socket.destroy()),
28+
);
29+
socket.on("error", () => {});
30+
socket.once("connect", () =>
31+
socket.write(encodeRequest({ version: 1, instanceId, environmentId, profileKey }) + "\n"),
32+
);
33+
return yield* Protocol.readFrame(
34+
socket,
35+
Protocol.RESPONSE_BYTES,
36+
Protocol.COMMAND_TIMEOUT_MS,
37+
).pipe(Effect.flatMap(decodeResponse));
38+
}).pipe(Effect.scoped);
39+
const result = await Effect.runPromiseExit(program);
40+
if (Exit.isFailure(result)) {
41+
process.stderr.write(
42+
"Could not access the managed ChatGPT connection. Reconnect in T3 Code.\n",
43+
);
44+
process.exitCode = 1;
45+
return;
46+
}
47+
process.stdout.write(result.value.token);
48+
}

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

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
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";
27
import * as NodeServices from "@effect/platform-node/NodeServices";
38
import { assert, it } from "@effect/vitest";
49
import { CodexSettings, EnvironmentId, ProviderInstanceId } from "@t3tools/contracts";
@@ -20,7 +25,10 @@ import { codexAppServerArgs } from "./codexLaunchArgs.ts";
2025

2126
const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
2227
const 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 = /auth=\{command=(.*),args=(\[.*\]),timeout_ms=/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")

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

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import * as FileSystem from "effect/FileSystem";
55
import * as Path from "effect/Path";
66
import * as Schema from "effect/Schema";
77
import * as ServerConfig from "../config.ts";
8+
import * as CodexManagedTokenBroker from "./CodexManagedTokenBroker.ts";
9+
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
10+
import { resolveSelfInvocation, selfInvocationArgs } from "@t3tools/shared/nodeRuntime";
811
import * as CodexInstallation from "./CodexInstallation.ts";
912
import { makeCodexChatGptAuth } from "./CodexChatGptAuth.ts";
13+
import { COMMAND_TIMEOUT_MS } from "./CodexManagedTokenProtocol.ts";
1014
import { materializeCodexShadowHome } from "./Drivers/CodexHomeLayout.ts";
1115

1216
export interface CodexEffectiveRuntime {
@@ -15,21 +19,17 @@ export interface CodexEffectiveRuntime {
1519
readonly revision: string;
1620
}
1721
const decodeSettings = Schema.decodeSync(CodexSettings);
18-
const managedCodexLaunchArgs = [
19-
'model_provider="openai_token_sharing"',
20-
'model_providers.openai_token_sharing.name="OpenAI Token Sharing"',
21-
'model_providers.openai_token_sharing.base_url="https://api.openai.com/v1"',
22-
'model_providers.openai_token_sharing.model_catalog_url="https://api.openai.com/v1/models"',
23-
"features.api_key_model_discovery=true",
24-
"features.multi_agent=false",
25-
"features.multi_agent_v2=false",
26-
'model_providers.openai_token_sharing.env_key="ACCESS_TOKEN"',
27-
'model_providers.openai_token_sharing.wire_api="responses"',
28-
"model_providers.openai_token_sharing.requires_openai_auth=false",
29-
"model_providers.openai_token_sharing.supports_websockets=false",
30-
]
31-
.map((value) => `-c '${value}'`)
32-
.join(" ");
22+
const managedCodexLaunchArgs = (command: string, args: ReadonlyArray<string>) =>
23+
[
24+
'model_provider="openai_token_sharing"',
25+
// Codex persists this provider ID in rollouts and needs it unchanged to resume.
26+
`model_providers.openai_token_sharing={name="OpenAI Token Sharing",base_url="https://api.openai.com/v1",model_catalog_url="https://api.openai.com/v1/models",wire_api="responses",requires_openai_auth=false,supports_websockets=false,auth={command=${JSON.stringify(command)},args=${JSON.stringify(args)},timeout_ms=${COMMAND_TIMEOUT_MS},refresh_interval_ms=0}}`,
27+
"features.api_key_model_discovery=true",
28+
"features.multi_agent=false",
29+
"features.multi_agent_v2=false",
30+
]
31+
.map((value) => `-c ${JSON.stringify(value)}`)
32+
.join(" ");
3333

3434
export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(function* (options: {
3535
readonly instanceId: ProviderInstanceId;
@@ -41,6 +41,10 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
4141
const config = yield* ServerConfig.ServerConfig;
4242
const fs = yield* FileSystem.FileSystem;
4343
const path = yield* Path.Path;
44+
const broker = yield* CodexManagedTokenBroker.CodexManagedTokenBroker;
45+
const environmentIdentity = yield* ServerEnvironment.ServerEnvironmentIdentity;
46+
const environmentId = yield* environmentIdentity.getEnvironmentId;
47+
const invocation = yield* resolveSelfInvocation();
4448
const auth = yield* makeCodexChatGptAuth({
4549
instanceId: options.instanceId,
4650
defaultReturnUrl: new URL(
@@ -55,6 +59,19 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
5559
);
5660
const homePath = homeLayout.effectiveHomePath ?? homeLayout.sharedHomePath;
5761
const resolve = Effect.gen(function* () {
62+
// Establish renewal availability before auth.access can consume a refresh token.
63+
const endpoint = yield* broker.endpoint.pipe(
64+
Effect.mapError(
65+
(cause) =>
66+
new ProviderSetupError({
67+
instanceId: options.instanceId,
68+
operation: "runtime",
69+
detail:
70+
"Could not prepare the private managed Codex token listener. Restart T3 and retry.",
71+
cause,
72+
}),
73+
),
74+
);
5875
const executable = yield* installation.acquire().pipe(
5976
Effect.mapError(
6077
() =>
@@ -66,6 +83,14 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
6683
),
6784
);
6885
const credentials = yield* auth.access;
86+
const profileKey = yield* auth.profileKey(credentials);
87+
const authArgs = selfInvocationArgs(invocation, [
88+
"__managed-chatgpt-token",
89+
endpoint,
90+
options.instanceId,
91+
environmentId,
92+
profileKey,
93+
]);
6994
yield* materializeCodexShadowHome(homeLayout).pipe(
7095
Effect.provideService(FileSystem.FileSystem, fs),
7196
Effect.provideService(Path.Path, path),
@@ -91,9 +116,9 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
91116
// Ambient CLI overrides cannot redirect a T3-owned token to a different provider.
92117
const environment: NodeJS.ProcessEnv = {
93118
...options.environment,
94-
ACCESS_TOKEN: credentials.accessToken,
95119
CODEX_HOME: homePath,
96120
};
121+
delete environment.ACCESS_TOKEN;
97122
delete environment.T3CODE_CODEX_LAUNCH_ARGS;
98123
delete environment.OPENAI_API_KEY;
99124
delete environment.OPENAI_BASE_URL;
@@ -103,10 +128,10 @@ export const makeCodexManagedRuntime = Effect.fn("makeCodexManagedRuntime")(func
103128
setupMode: "managed",
104129
binaryPath: executable.executablePath,
105130
homePath,
106-
launchArgs: managedCodexLaunchArgs,
131+
launchArgs: managedCodexLaunchArgs(invocation.command, authArgs),
107132
}),
108133
environment,
109-
revision: credentials.accessToken,
134+
revision: profileKey,
110135
} satisfies CodexEffectiveRuntime;
111136
});
112137
return { auth, resolve, installation, homePath, homeLayout };

0 commit comments

Comments
 (0)