Skip to content

Commit 5d0933d

Browse files
codex: support session-isolated app-server clients
1 parent 5fb9c0c commit 5d0933d

50 files changed

Lines changed: 1927 additions & 195 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎extensions/codex/harness.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,23 @@ export function createCodexAppServerAgentHarness(options?: {
5555
return maybeCompactCodexAppServerSession(params, { pluginConfig: options?.pluginConfig });
5656
},
5757
reset: async (params) => {
58+
const { resolveCodexAppServerRuntimeOptions } = await import("./src/app-server/config.js");
59+
const appServer = resolveCodexAppServerRuntimeOptions({
60+
pluginConfig: options?.pluginConfig,
61+
});
62+
const isolationKey =
63+
appServer.clientIsolation === "session"
64+
? params.sandboxSessionKey?.trim() || params.sessionKey?.trim() || params.sessionId
65+
: undefined;
5866
if (params.sessionFile) {
59-
const { clearCodexAppServerBinding } = await import("./src/app-server/session-binding.js");
60-
await clearCodexAppServerBinding(params.sessionFile);
67+
const { clearAllCodexAppServerBindings } =
68+
await import("./src/app-server/session-binding.js");
69+
await clearAllCodexAppServerBindings(params.sessionFile);
70+
}
71+
if (isolationKey) {
72+
const { clearSharedCodexAppServerClientForIsolationKey } =
73+
await import("./src/app-server/shared-client.js");
74+
clearSharedCodexAppServerClientForIsolationKey(isolationKey);
6175
}
6276
},
6377
dispose: async () => {

‎extensions/codex/openclaw.plugin.json‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@
175175
"minimum": 1,
176176
"default": 60000
177177
},
178+
"turnTerminalIdleTimeoutMs": {
179+
"type": "number",
180+
"minimum": 1,
181+
"default": 1800000
182+
},
183+
"clientIsolation": {
184+
"type": "string",
185+
"enum": ["agent", "session"],
186+
"default": "agent"
187+
},
178188
"approvalPolicy": {
179189
"type": "string",
180190
"enum": ["never", "on-request", "on-failure", "untrusted"]
@@ -345,6 +355,16 @@
345355
"help": "Maximum quiet time after Codex accepts a turn or after a turn-scoped app-server request before OpenClaw interrupts the turn while waiting for turn/completed.",
346356
"advanced": true
347357
},
358+
"appServer.turnTerminalIdleTimeoutMs": {
359+
"label": "Turn Terminal Idle Timeout",
360+
"help": "Maximum quiet time during an accepted Codex app-server turn before OpenClaw interrupts the turn while waiting for a terminal event.",
361+
"advanced": true
362+
},
363+
"appServer.clientIsolation": {
364+
"label": "Client Isolation",
365+
"help": "Controls whether Codex app-server clients are shared per agent or isolated per OpenClaw session/topic.",
366+
"advanced": true
367+
},
348368
"appServer.approvalPolicy": {
349369
"label": "Approval Policy",
350370
"help": "Codex native approval policy sent to thread start, resume, and turns.",

‎extensions/codex/src/app-server/client-factory.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ export type CodexAppServerClientFactory = (
1111
authProfileId?: string,
1212
agentDir?: string,
1313
config?: AuthProfileOrderConfig,
14+
isolationKey?: string,
1415
) => Promise<CodexAppServerClient>;
1516

1617
export const defaultCodexAppServerClientFactory: CodexAppServerClientFactory = (
1718
startOptions,
1819
authProfileId,
1920
agentDir,
2021
config,
22+
isolationKey,
2123
) =>
2224
import("./shared-client.js").then(({ getSharedCodexAppServerClient }) =>
23-
getSharedCodexAppServerClient({ startOptions, authProfileId, agentDir, config }),
25+
getSharedCodexAppServerClient({ startOptions, authProfileId, agentDir, config, isolationKey }),
2426
);

‎extensions/codex/src/app-server/compact.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,14 @@ async function compactCodexNativeThread(
311311
options: { pluginConfig?: unknown; clientFactory?: CodexAppServerClientFactory } = {},
312312
): Promise<EmbeddedPiCompactResult | undefined> {
313313
const appServer = resolveCodexAppServerRuntimeOptions({ pluginConfig: options.pluginConfig });
314-
const binding = await readCodexAppServerBinding(params.sessionFile, { config: params.config });
314+
const appServerClientIsolationKey =
315+
appServer.clientIsolation === "session"
316+
? resolveCodexAppServerCompactionIsolationKey(params)
317+
: undefined;
318+
const binding = await readCodexAppServerBinding(params.sessionFile, {
319+
config: params.config,
320+
isolationKey: appServerClientIsolationKey,
321+
});
315322
if (!binding?.threadId) {
316323
return { ok: false, compacted: false, reason: "no codex app-server thread binding" };
317324
}
@@ -330,6 +337,7 @@ async function compactCodexNativeThread(
330337
requestedAuthProfileId ?? binding.authProfileId,
331338
params.agentDir,
332339
params.config,
340+
appServerClientIsolationKey,
333341
);
334342
const waiter = createCodexNativeCompactionWaiter(client, binding.threadId);
335343
let completion: CodexNativeCompactionCompletion;
@@ -376,6 +384,16 @@ async function compactCodexNativeThread(
376384
};
377385
}
378386

387+
function resolveCodexAppServerCompactionIsolationKey(
388+
params: CompactEmbeddedPiSessionParams,
389+
): string {
390+
return (
391+
params.sandboxSessionKey?.trim() ||
392+
params.sessionKey?.trim() ||
393+
params.sessionId
394+
).replace(/:run:[^:]+$/, "");
395+
}
396+
379397
function createCodexNativeCompactionWaiter(
380398
client: CodexAppServerClient,
381399
threadId: string,

‎extensions/codex/src/app-server/config.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ describe("Codex app-server config", () => {
6969
serviceTier: "flex",
7070
codeModeOnly: true,
7171
turnCompletionIdleTimeoutMs: 120_000,
72+
turnTerminalIdleTimeoutMs: 180_000,
73+
clientIsolation: "session",
7274
},
7375
},
7476
env: {
@@ -84,6 +86,8 @@ describe("Codex app-server config", () => {
8486
serviceTier: "flex",
8587
codeModeOnly: true,
8688
turnCompletionIdleTimeoutMs: 120_000,
89+
turnTerminalIdleTimeoutMs: 180_000,
90+
clientIsolation: "session",
8791
});
8892
expectFields(runtime.start, "runtime start", {
8993
transport: "websocket",
@@ -122,6 +126,12 @@ describe("Codex app-server config", () => {
122126
});
123127
});
124128

129+
it("keeps the terminal idle watchdog default aligned with the existing 30 minute runtime timeout", () => {
130+
const runtime = resolveRuntimeForTest();
131+
132+
expect(runtime.turnTerminalIdleTimeoutMs).toBe(30 * 60_000);
133+
});
134+
125135
it("normalizes legacy service tiers without discarding the rest of the config", () => {
126136
const runtime = resolveRuntimeForTest({
127137
pluginConfig: {

‎extensions/codex/src/app-server/config.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type CodexAppServerEffectiveApprovalPolicy =
3232
export type CodexAppServerSandboxMode = "read-only" | "workspace-write" | "danger-full-access";
3333
type CodexAppServerApprovalsReviewer = "user" | "auto_review" | "guardian_subagent";
3434
type CodexAppServerCommandSource = "managed" | "resolved-managed" | "config" | "env";
35+
export type CodexAppServerClientIsolation = "agent" | "session";
3536
export type CodexDynamicToolsLoading = "searchable" | "direct";
3637
export type CodexPluginDestructivePolicy = boolean;
3738

@@ -104,6 +105,8 @@ export type CodexAppServerRuntimeOptions = {
104105
codeModeOnly: boolean;
105106
requestTimeoutMs: number;
106107
turnCompletionIdleTimeoutMs: number;
108+
turnTerminalIdleTimeoutMs: number;
109+
clientIsolation: CodexAppServerClientIsolation;
107110
approvalPolicy: CodexAppServerEffectiveApprovalPolicy;
108111
sandbox: CodexAppServerSandboxMode;
109112
approvalsReviewer: CodexAppServerApprovalsReviewer;
@@ -131,6 +134,8 @@ export type CodexPluginConfig = {
131134
codeModeOnly?: boolean;
132135
requestTimeoutMs?: number;
133136
turnCompletionIdleTimeoutMs?: number;
137+
turnTerminalIdleTimeoutMs?: number;
138+
clientIsolation?: CodexAppServerClientIsolation;
134139
approvalPolicy?: CodexAppServerApprovalPolicy;
135140
sandbox?: CodexAppServerSandboxMode;
136141
approvalsReviewer?: CodexAppServerApprovalsReviewer;
@@ -151,6 +156,8 @@ export const CODEX_APP_SERVER_CONFIG_KEYS = [
151156
"codeModeOnly",
152157
"requestTimeoutMs",
153158
"turnCompletionIdleTimeoutMs",
159+
"turnTerminalIdleTimeoutMs",
160+
"clientIsolation",
154161
"approvalPolicy",
155162
"sandbox",
156163
"approvalsReviewer",
@@ -196,6 +203,7 @@ const codexAppServerApprovalPolicySchema = z.enum([
196203
]);
197204
const codexAppServerSandboxSchema = z.enum(["read-only", "workspace-write", "danger-full-access"]);
198205
const codexAppServerApprovalsReviewerSchema = z.enum(["user", "auto_review", "guardian_subagent"]);
206+
const codexAppServerClientIsolationSchema = z.enum(["agent", "session"]);
199207
const codexDynamicToolsLoadingSchema = z.enum(["searchable", "direct"]);
200208
const codexAppServerServiceTierSchema = z
201209
.preprocess(
@@ -259,6 +267,8 @@ const codexPluginConfigSchema = z
259267
codeModeOnly: z.boolean().optional(),
260268
requestTimeoutMs: z.number().positive().optional(),
261269
turnCompletionIdleTimeoutMs: z.number().positive().optional(),
270+
turnTerminalIdleTimeoutMs: z.number().positive().optional(),
271+
clientIsolation: codexAppServerClientIsolationSchema.optional(),
262272
approvalPolicy: codexAppServerApprovalPolicySchema.optional(),
263273
sandbox: codexAppServerSandboxSchema.optional(),
264274
approvalsReviewer: codexAppServerApprovalsReviewerSchema.optional(),
@@ -377,6 +387,11 @@ export function resolveCodexAppServerRuntimeOptions(
377387
config.turnCompletionIdleTimeoutMs,
378388
60_000,
379389
),
390+
turnTerminalIdleTimeoutMs: normalizePositiveNumber(
391+
config.turnTerminalIdleTimeoutMs,
392+
30 * 60_000,
393+
),
394+
clientIsolation: config.clientIsolation ?? "agent",
380395
approvalPolicy:
381396
resolveApprovalPolicy(config.approvalPolicy) ??
382397
resolveApprovalPolicy(env.OPENCLAW_CODEX_APP_SERVER_APPROVAL_POLICY) ??

‎extensions/codex/src/app-server/request.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export async function requestCodexAppServerJson<M extends CodexAppServerRequestM
2121
agentDir?: string;
2222
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
2323
isolated?: boolean;
24+
isolationKey?: string;
2425
}): Promise<CodexAppServerRequestResult<M>>;
2526
export async function requestCodexAppServerJson<T = JsonValue | undefined>(params: {
2627
method: string;
@@ -31,6 +32,7 @@ export async function requestCodexAppServerJson<T = JsonValue | undefined>(param
3132
agentDir?: string;
3233
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
3334
isolated?: boolean;
35+
isolationKey?: string;
3436
}): Promise<T>;
3537
export async function requestCodexAppServerJson<T = JsonValue | undefined>(params: {
3638
method: string;
@@ -41,19 +43,27 @@ export async function requestCodexAppServerJson<T = JsonValue | undefined>(param
4143
agentDir?: string;
4244
config?: Parameters<typeof resolveCodexAppServerAuthProfileIdForAgent>[0]["config"];
4345
isolated?: boolean;
46+
isolationKey?: string;
4447
}): Promise<T> {
4548
const timeoutMs = params.timeoutMs ?? 60_000;
4649
return await withTimeout(
4750
(async () => {
48-
const client = await (
49-
params.isolated ? createIsolatedCodexAppServerClient : getSharedCodexAppServerClient
50-
)({
51-
startOptions: params.startOptions,
52-
timeoutMs,
53-
authProfileId: params.authProfileId,
54-
agentDir: params.agentDir,
55-
config: params.config,
56-
});
51+
const client = await (params.isolated
52+
? createIsolatedCodexAppServerClient({
53+
startOptions: params.startOptions,
54+
timeoutMs,
55+
authProfileId: params.authProfileId,
56+
agentDir: params.agentDir,
57+
config: params.config,
58+
})
59+
: getSharedCodexAppServerClient({
60+
startOptions: params.startOptions,
61+
timeoutMs,
62+
authProfileId: params.authProfileId,
63+
agentDir: params.agentDir,
64+
config: params.config,
65+
isolationKey: params.isolationKey,
66+
}));
5767
try {
5868
return await client.request<T>(params.method, params.requestParams, { timeoutMs });
5969
} finally {

0 commit comments

Comments
 (0)