Skip to content

Commit d1c0204

Browse files
committed
fix(codex): preserve paired-node agent ownership
1 parent cc9883a commit d1c0204

6 files changed

Lines changed: 199 additions & 38 deletions

File tree

‎extensions/codex/index.test.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Codex tests cover index plugin behavior.
22
import fs from "node:fs";
3+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
34
import { createTestPluginApi } from "openclaw/plugin-sdk/plugin-test-api";
45
import { describe, expect, it, vi } from "vitest";
56
import openAIPlugin from "../openai/index.js";
@@ -17,6 +18,12 @@ import { CODEX_SUPERVISION_COMPAT_TOOL_NAMES } from "./src/supervision-tools.js"
1718

1819
const runCodexAppServerAttemptMock = vi.hoisted(() => vi.fn());
1920
const runCodexAppServerSideQuestionMock = vi.hoisted(() => vi.fn());
21+
const explicitAgentConfig = {
22+
agents: {
23+
ownership: "explicit",
24+
entries: { main: {}, clawblocker: {}, blockdigest: {} },
25+
},
26+
} as OpenClawConfig;
2027

2128
function createCodexTestRuntime(
2229
current?: () => unknown,
@@ -55,7 +62,7 @@ describe("codex plugin", () => {
5562
expect(manifest.providers).toBeUndefined();
5663
});
5764

58-
it("does not open plugin state while registering with the base runtime", () => {
65+
it("does not select an agent or open plugin state while registering", () => {
5966
const openSyncKeyedStore = vi.fn(() => {
6067
throw new Error("openSyncKeyedStore is only available through the plugin runtime proxy");
6168
});
@@ -66,7 +73,7 @@ describe("codex plugin", () => {
6673
id: "codex",
6774
name: "Codex",
6875
source: "test",
69-
config: {},
76+
config: explicitAgentConfig,
7077
pluginConfig: {},
7178
runtime: { state: { openSyncKeyedStore } } as never,
7279
}),
@@ -75,6 +82,38 @@ describe("codex plugin", () => {
7582
expect(openSyncKeyedStore).not.toHaveBeenCalled();
7683
});
7784

85+
it("registers request-scoped surfaces with explicit multi-agent ownership", () => {
86+
const registerAgentHarness = vi.fn();
87+
const registerNodeHostCommand = vi.fn();
88+
const registerSessionCatalog = vi.fn();
89+
90+
expect(() =>
91+
plugin.register(
92+
createTestPluginApi({
93+
id: "codex",
94+
name: "Codex",
95+
source: "test",
96+
config: explicitAgentConfig,
97+
pluginConfig: {},
98+
runtime: createCodexTestRuntime(() => explicitAgentConfig),
99+
registerAgentHarness,
100+
registerNodeHostCommand,
101+
registerSessionCatalog,
102+
}),
103+
),
104+
).not.toThrow();
105+
106+
expect(registerAgentHarness).toHaveBeenCalledOnce();
107+
expect(registerSessionCatalog).toHaveBeenCalledOnce();
108+
expect(registerNodeHostCommand.mock.calls.map(([command]) => command.command)).toEqual(
109+
expect.arrayContaining([
110+
"codex.appServer.threads.list.v1",
111+
"codex.appServer.thread.turns.list.v1",
112+
"codex.terminal.resume.v1",
113+
]),
114+
);
115+
});
116+
78117
it("proactively monitors an explicitly configured remote websocket app-server", () => {
79118
const registerService = vi.fn();
80119

@@ -222,7 +261,7 @@ describe("codex plugin", () => {
222261
id: "codex",
223262
name: "Codex",
224263
source: "test",
225-
config: {},
264+
config: explicitAgentConfig,
226265
pluginConfig: { sessionCatalog: { enabled: false } },
227266
runtime: createCodexTestRuntime(),
228267
registerAgentHarness,

‎extensions/codex/index.ts‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { resolveSessionAgentIds } from "openclaw/plugin-sdk/agent-runtime";
21
/**
32
* Bundled Codex plugin entry: app-server harness, media understanding,
43
* migration provider, CLI-session commands, and binding hooks.
@@ -133,11 +132,6 @@ export default definePluginEntry({
133132
getPluginConfig: resolveCurrentPluginConfig,
134133
getRuntimeConfig: resolveCurrentConfig,
135134
});
136-
const nodeSessionCatalogControl = sessionCatalogControlFactory.forRequest(
137-
resolveSessionAgentIds({
138-
config: resolveCurrentConfig() ?? (api.config as OpenClawConfig),
139-
}).sessionAgentId,
140-
);
141135
const sessionCatalogEnabled =
142136
readCodexPluginConfig(resolveCurrentPluginConfig()).sessionCatalog?.enabled !== false;
143137
if (sessionCatalogEnabled) {
@@ -148,10 +142,13 @@ export default definePluginEntry({
148142
getPluginConfig: resolveCurrentPluginConfig,
149143
getRuntimeConfig: resolveCurrentConfig,
150144
});
151-
for (const command of createCodexSessionCatalogNodeHostCommands(nodeSessionCatalogControl, {
152-
getPluginConfig: resolveCurrentPluginConfig,
153-
getRuntimeConfig: resolveCurrentConfig,
154-
})) {
145+
for (const command of createCodexSessionCatalogNodeHostCommands(
146+
sessionCatalogControlFactory,
147+
{
148+
getPluginConfig: resolveCurrentPluginConfig,
149+
getRuntimeConfig: () => resolveCurrentConfig() ?? (api.config as OpenClawConfig),
150+
},
151+
)) {
155152
api.registerNodeHostCommand(command);
156153
}
157154
}

‎extensions/codex/src/session-catalog-node-continue.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function canContinueCodexOnNode(node: CatalogNode): boolean {
8181
}
8282

8383
export async function listPairedNode(params: {
84+
agentId: string;
8485
runtime: PluginRuntime;
8586
node: CatalogNode;
8687
query: CodexSessionCatalogParams;
@@ -111,6 +112,7 @@ export async function listPairedNode(params: {
111112
nodeId: params.node.nodeId,
112113
command: CODEX_APP_SERVER_THREADS_LIST_COMMAND,
113114
params: {
115+
agentId: params.agentId,
114116
cursor: params.query.cursors?.[hostId],
115117
limit: params.query.limitPerHost,
116118
searchTerm: params.query.search,
@@ -177,6 +179,7 @@ async function requireNodeForCodexContinue(params: {
177179
}
178180

179181
async function resolveNodeCodexRecord(params: {
182+
agentId: string;
180183
runtime: PluginRuntime;
181184
nodeId: string;
182185
threadId: string;
@@ -188,6 +191,7 @@ async function resolveNodeCodexRecord(params: {
188191
nodeId: params.nodeId,
189192
command: CODEX_APP_SERVER_THREADS_LIST_COMMAND,
190193
params: {
194+
agentId: params.agentId,
191195
limit: CODEX_SESSION_CATALOG_MAX_PAGE_LIMIT,
192196
...(cursor ? { cursor } : {}),
193197
},
@@ -233,6 +237,7 @@ function requireContinuableNodeRecord(record: CodexSessionCatalogSession): void
233237
}
234238

235239
async function readNodeCodexHistory(params: {
240+
agentId: string;
236241
runtime: PluginRuntime;
237242
nodeId: string;
238243
record: CodexSessionCatalogSession;
@@ -241,6 +246,7 @@ async function readNodeCodexHistory(params: {
241246
nodeId: params.nodeId,
242247
command: CODEX_APP_SERVER_THREAD_TURNS_LIST_COMMAND,
243248
params: {
249+
agentId: params.agentId,
244250
threadId: params.record.threadId,
245251
limit: MAX_TRANSCRIPT_PAGE_LIMIT,
246252
},
@@ -279,6 +285,7 @@ async function continueNodeCodexSessionInner(params: {
279285
hostId: params.hostId,
280286
});
281287
const record = await resolveNodeCodexRecord({
288+
agentId: params.agentId,
282289
runtime: params.api.runtime,
283290
nodeId,
284291
threadId: params.threadId,
@@ -301,6 +308,7 @@ async function continueNodeCodexSessionInner(params: {
301308
disposition = "existing";
302309
} else {
303310
const history = await readNodeCodexHistory({
311+
agentId: params.agentId,
304312
runtime: params.api.runtime,
305313
nodeId,
306314
record,

‎extensions/codex/src/session-catalog-terminal.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ async function findCatalogEligibleThread(
135135
}
136136

137137
export function createCodexTerminalNodeHostCommand(
138-
control: CodexSessionCatalogControl,
138+
bindRequest: (paramsJSON?: string | null) => {
139+
agentId: string;
140+
control: CodexSessionCatalogControl;
141+
paramsJSON: string;
142+
},
139143
configSources: CodexTerminalConfigSources,
140144
): OpenClawPluginNodeHostCommand {
141145
return {
@@ -155,7 +159,8 @@ export function createCodexTerminalNodeHostCommand(
155159
if (!io) {
156160
throw new Error("Codex terminal command requires duplex transport");
157161
}
158-
const resume = decodeNodePtyResumeParams(paramsJSON, (value) => {
162+
const request = bindRequest(paramsJSON);
163+
const resume = decodeNodePtyResumeParams(request.paramsJSON, (value) => {
159164
if (
160165
typeof value !== "string" ||
161166
!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu.test(value)
@@ -164,7 +169,7 @@ export function createCodexTerminalNodeHostCommand(
164169
}
165170
return value;
166171
});
167-
const record = await requireCatalogEligibleThread(control, resume.threadId);
172+
const record = await requireCatalogEligibleThread(request.control, resume.threadId);
168173
const resolution = resolveNodeHostExecutable("codex", {
169174
env: process.env,
170175
pathEnv: process.env.PATH ?? process.env.Path ?? "",
@@ -179,7 +184,12 @@ export function createCodexTerminalNodeHostCommand(
179184
file: resolution.executable,
180185
args: ["resume", resume.threadId],
181186
cwd: record.cwd,
182-
env: { CODEX_HOME: resolveCodexCatalogTerminalHome(configSources) },
187+
env: {
188+
CODEX_HOME: resolveCodexCatalogTerminalHome({
189+
...configSources,
190+
agentId: request.agentId,
191+
}),
192+
},
183193
cols: resume.cols,
184194
rows: resume.rows,
185195
},
@@ -191,6 +201,7 @@ export function createCodexTerminalNodeHostCommand(
191201
}
192202

193203
async function resolveNodeCatalogEligibleThread(params: {
204+
agentId: string;
194205
runtime: PluginRuntime;
195206
nodeId: string;
196207
threadId: string;
@@ -203,6 +214,7 @@ async function resolveNodeCatalogEligibleThread(params: {
203214
nodeId: params.nodeId,
204215
command: CODEX_APP_SERVER_THREADS_LIST_COMMAND,
205216
params: {
217+
agentId: params.agentId,
206218
limit: CODEX_SESSION_CATALOG_MAX_PAGE_LIMIT,
207219
...(cursor ? { cursor } : {}),
208220
},
@@ -276,6 +288,7 @@ export async function openCodexCatalogTerminal(
276288
throw new CatalogParamsError("paired-node Codex terminal is unavailable");
277289
}
278290
const record = await resolveNodeCatalogEligibleThread({
291+
agentId: params.agentId,
279292
runtime: params.api.runtime,
280293
nodeId,
281294
threadId: params.threadId,
@@ -285,7 +298,7 @@ export async function openCodexCatalogTerminal(
285298
kind: "node",
286299
nodeId,
287300
command: CODEX_TERMINAL_RESUME_COMMAND,
288-
paramsJSON: JSON.stringify({ threadId: params.threadId }),
301+
paramsJSON: JSON.stringify({ agentId: params.agentId, threadId: params.threadId }),
289302
...(record.cwd ? { cwd: record.cwd } : {}),
290303
title,
291304
};

0 commit comments

Comments
 (0)