Skip to content

Commit 179cb1b

Browse files
committed
fix(cron): restrict scheduled authority to Codex owner
1 parent 46312a4 commit 179cb1b

9 files changed

Lines changed: 342 additions & 294 deletions

‎extensions/codex/src/app-server/dynamic-tool-build.test.ts‎

Lines changed: 45 additions & 202 deletions
Large diffs are not rendered by default.

‎extensions/codex/src/app-server/dynamic-tool-build.ts‎

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
type RuntimeToolSchemaDiagnostic,
2020
} from "openclaw/plugin-sdk/agent-harness-runtime";
2121
import { resolveAgentDir } from "openclaw/plugin-sdk/agent-runtime";
22-
import { runWithCronCreatorAuthorityCapabilityResolver } from "openclaw/plugin-sdk/codex-mcp-projection";
22+
import {
23+
resolveCodexScheduledToolProjectionFactory,
24+
runWithCronCreatorAuthorityCapabilityResolver,
25+
} from "openclaw/plugin-sdk/codex-mcp-projection";
2326
import { isToolAllowed } from "openclaw/plugin-sdk/sandbox";
2427
import {
2528
isCodexRemoteExecPlacementSandbox,
@@ -47,8 +50,9 @@ import {
4750
CODEX_GATEWAY_EXEC_DYNAMIC_TOOL_NAME,
4851
CODEX_GATEWAY_PROCESS_DYNAMIC_TOOL_NAME,
4952
CODEX_NODE_EXEC_DYNAMIC_TOOL_NAME,
50-
createExecAliasDynamicTool,
51-
createGatewayProcessAliasDynamicTool,
53+
createGatewayExecProjection,
54+
createGatewayProcessProjection,
55+
createNodeExecAliasDynamicTool,
5256
isCodexDynamicToolExcluded,
5357
} from "./shell-dynamic-tools.js";
5458
import { filterCodexVisionTools } from "./vision-tools.js";
@@ -614,24 +618,20 @@ function addGatewayShellDynamicToolsIfAvailable(
614618
const processAliasAvailable = Boolean(
615619
processTool && !processExcluded && !existingNames.has(CODEX_GATEWAY_PROCESS_DYNAMIC_TOOL_NAME),
616620
);
621+
const createProjection = resolveCodexScheduledToolProjectionFactory(
622+
input.params.hostCapabilities,
623+
);
624+
if (!createProjection) {
625+
return filteredTools;
626+
}
617627
const toolsToAppend = [
618-
createExecAliasDynamicTool(
619-
execTool,
620-
{
621-
host: "gateway",
622-
processAliasAvailable,
623-
...(input.sessionPermissionPolicy?.mode === "guarded" ? { ask: "always" } : {}),
624-
},
625-
input.params.hostCapabilities.sealScheduledToolProjection,
626-
),
628+
createGatewayExecProjection(createProjection, execTool, {
629+
processAliasAvailable,
630+
...(input.sessionPermissionPolicy?.mode === "guarded" ? { ask: "always" } : {}),
631+
}),
627632
];
628633
if (processAliasAvailable && processTool) {
629-
toolsToAppend.push(
630-
createGatewayProcessAliasDynamicTool(
631-
processTool,
632-
input.params.hostCapabilities.sealScheduledToolProjection,
633-
),
634-
);
634+
toolsToAppend.push(createGatewayProcessProjection(createProjection, processTool));
635635
}
636636
return [...filteredTools, ...toolsToAppend];
637637
}
@@ -929,10 +929,7 @@ function addNodeShellDynamicToolsIfNeeded(
929929
(tool) => normalizeCodexDynamicToolName(tool.name) === CODEX_NODE_EXEC_DYNAMIC_TOOL_NAME,
930930
)
931931
) {
932-
return [
933-
...filteredTools,
934-
createExecAliasDynamicTool(execTool, { host: "node", node: nodePolicy.node }),
935-
];
932+
return [...filteredTools, createNodeExecAliasDynamicTool(execTool, nodePolicy.node)];
936933
}
937934
return filteredTools;
938935
}
Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import type { EmbeddedRunAttemptParamsV2 as EmbeddedRunAttemptParams } from "openclaw/plugin-sdk/agent-harness-runtime";
2-
import { pinExecToolTarget } from "openclaw/plugin-sdk/codex-mcp-projection";
1+
import {
2+
pinExecToolTarget,
3+
type CodexScheduledToolProjectionFactory,
4+
} from "openclaw/plugin-sdk/codex-mcp-projection";
35
import type { CodexPluginConfig } from "./config.js";
46
import { normalizeCodexDynamicToolName } from "./dynamic-tool-profile.js";
57

68
type OpenClawCodingToolsFactory =
79
(typeof import("openclaw/plugin-sdk/agent-harness"))["createOpenClawCodingTools"];
810
type OpenClawDynamicTool = ReturnType<OpenClawCodingToolsFactory>[number];
9-
type SealScheduledToolProjection = NonNullable<
10-
EmbeddedRunAttemptParams["hostCapabilities"]["sealScheduledToolProjection"]
11-
>;
12-
type ExecAliasParams =
13-
| { host: "gateway"; processAliasAvailable: boolean; ask?: "always" }
14-
| { host: "node"; node?: string };
1511

1612
export const CODEX_NODE_EXEC_DYNAMIC_TOOL_NAME = "node_exec";
1713
export const CODEX_GATEWAY_EXEC_DYNAMIC_TOOL_NAME = "gateway_exec";
@@ -30,64 +26,66 @@ export function isCodexDynamicToolExcluded(
3026
);
3127
}
3228

33-
export function createExecAliasDynamicTool(
29+
export function createNodeExecAliasDynamicTool(
3430
execTool: OpenClawDynamicTool,
35-
params: ExecAliasParams,
36-
sealScheduledToolProjection?: SealScheduledToolProjection,
31+
node?: string,
3732
): OpenClawDynamicTool {
38-
const pinnedNode = params.host === "node" ? params.node?.trim() : undefined;
39-
const nodeAlias = params.host === "node";
40-
const gatewayProcessAliasAvailable = params.host === "gateway" && params.processAliasAvailable;
41-
const name = nodeAlias ? CODEX_NODE_EXEC_DYNAMIC_TOOL_NAME : CODEX_GATEWAY_EXEC_DYNAMIC_TOOL_NAME;
42-
const description = nodeAlias
43-
? pinnedNode
44-
? "Run a shell command to completion on the OpenClaw configured remote node for this session. This tool always uses OpenClaw host=node internally and follows the existing node exec approval and allowlist policy. Remote-node background follow-up is unavailable. Use Codex's native shell for local app-server work."
45-
: "Run a shell command to completion on an OpenClaw remote node. Select the node by name or id when multiple nodes are available. This tool always uses OpenClaw host=node internally and follows the existing node exec approval and allowlist policy. Remote-node background follow-up is unavailable. Use Codex's native shell for local app-server work."
46-
: "Run a shell command through OpenClaw on the Gateway host for OpenClaw-managed Gateway environment access, including Secret Store agent-readable environment values and protected egress sentinels. Native Codex shell remains preferred for ordinary local work. This tool always uses OpenClaw host=gateway internally and follows Gateway exec approval and allowlist policy.";
47-
const followupText = nodeAlias
48-
? "Remote-node background follow-up is unavailable. Wait for the command to complete."
49-
: gatewayProcessAliasAvailable
50-
? "Use gateway_process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) for follow-up."
51-
: "Background session follow-up is unavailable because gateway_process is not exposed. Rerun without background=true and set yieldMs high enough to wait for completion.";
52-
const pinnedTool = pinExecToolTarget(
53-
execTool,
54-
nodeAlias
55-
? { host: "node", ...(pinnedNode ? { node: pinnedNode } : {}) }
56-
: { host: "gateway", ...(params.ask ? { ask: params.ask } : {}) },
57-
);
33+
const pinnedNode = node?.trim();
34+
const pinnedTool = pinExecToolTarget(execTool, {
35+
host: "node",
36+
...(pinnedNode ? { node: pinnedNode } : {}),
37+
});
5838
const execute: OpenClawDynamicTool["execute"] = async (toolCallId, args, signal, onUpdate) => {
5939
const result = await pinnedTool.execute(toolCallId, args, signal, onUpdate);
6040
return {
6141
...result,
6242
content: result.content.map((item) =>
6343
item.type === "text"
6444
? Object.assign({}, item, {
65-
text: item.text.replace(PROCESS_FOLLOWUP_TEXT, followupText),
45+
text: item.text.replace(
46+
PROCESS_FOLLOWUP_TEXT,
47+
"Remote-node background follow-up is unavailable. Wait for the command to complete.",
48+
),
6649
})
6750
: item,
6851
),
6952
};
7053
};
71-
const alias = {
54+
return {
7255
...pinnedTool,
73-
name,
74-
description,
56+
name: CODEX_NODE_EXEC_DYNAMIC_TOOL_NAME,
57+
description: pinnedNode
58+
? "Run a shell command to completion on the OpenClaw configured remote node for this session. This tool always uses OpenClaw host=node internally and follows the existing node exec approval and allowlist policy. Remote-node background follow-up is unavailable. Use Codex's native shell for local app-server work."
59+
: "Run a shell command to completion on an OpenClaw remote node. Select the node by name or id when multiple nodes are available. This tool always uses OpenClaw host=node internally and follows the existing node exec approval and allowlist policy. Remote-node background follow-up is unavailable. Use Codex's native shell for local app-server work.",
7560
execute,
7661
};
77-
return nodeAlias || !sealScheduledToolProjection
78-
? alias
79-
: sealScheduledToolProjection(execTool, alias);
8062
}
8163

82-
export function createGatewayProcessAliasDynamicTool(
64+
export function createGatewayExecProjection(
65+
createProjection: CodexScheduledToolProjectionFactory,
66+
execTool: OpenClawDynamicTool,
67+
params: { processAliasAvailable: boolean; ask?: "always" },
68+
): OpenClawDynamicTool {
69+
return createProjection(execTool, {
70+
kind: "exec",
71+
name: CODEX_GATEWAY_EXEC_DYNAMIC_TOOL_NAME,
72+
description:
73+
"Run a shell command through OpenClaw on the Gateway host for OpenClaw-managed Gateway environment access, including Secret Store agent-readable environment values and protected egress sentinels. Native Codex shell remains preferred for ordinary local work. This tool always uses OpenClaw host=gateway internally and follows Gateway exec approval and allowlist policy.",
74+
followupText: params.processAliasAvailable
75+
? "Use gateway_process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) for follow-up."
76+
: "Background session follow-up is unavailable because gateway_process is not exposed. Rerun without background=true and set yieldMs high enough to wait for completion.",
77+
...(params.ask ? { ask: params.ask } : {}),
78+
});
79+
}
80+
81+
export function createGatewayProcessProjection(
82+
createProjection: CodexScheduledToolProjectionFactory,
8383
processTool: OpenClawDynamicTool,
84-
sealScheduledToolProjection?: SealScheduledToolProjection,
8584
): OpenClawDynamicTool {
86-
const alias = {
87-
...processTool,
85+
return createProjection(processTool, {
86+
kind: "process",
8887
name: CODEX_GATEWAY_PROCESS_DYNAMIC_TOOL_NAME,
8988
description:
9089
"Manage background shell sessions in the existing per-session OpenClaw process scope: list, poll, log, write, send-keys, submit, paste, kill, clear, or remove. Use for gateway_exec follow-up; use native Codex shell session handling for ordinary local work.",
91-
};
92-
return sealScheduledToolProjection ? sealScheduledToolProjection(processTool, alias) : alias;
90+
});
9391
}

‎src/agents/exec-tool-target-pinning.ts‎

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,36 @@ const scheduledToolAuthorityCaptures = new WeakMap<
2828

2929
const EXEC_POLICY_PARAMETER_NAMES = new Set(["host", "security", "ask"]);
3030
const NODE_EXEC_PARAMETER_NAMES = new Set(["command", "workdir", "env", "timeoutSeconds", "node"]);
31+
const PROCESS_FOLLOWUP_TEXT =
32+
"Use process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) for follow-up.";
3133

3234
type PinnedExecToolTarget = { host: "gateway"; ask?: "always" } | { host: "node"; node?: string };
3335

34-
/** Core-only producer boundary for an alias of an exact host-created shell tool. */
35-
export function sealCronScheduledToolProjection(
36-
projectedTool: AnyAgentTool,
36+
export type CronScheduledToolProjectionRequest =
37+
| Readonly<{
38+
kind: "exec";
39+
name: string;
40+
description: string;
41+
followupText: string;
42+
ask?: "always";
43+
}>
44+
| Readonly<{ kind: "process"; name: string; description: string }>;
45+
46+
/** Constructs and seals an alias from an exact host-owned shell source. */
47+
export function createCronScheduledToolProjection(
48+
sourceTool: AnyAgentTool,
3749
assertActive: () => void,
3850
targetTool: "exec" | "process",
51+
projection: CronScheduledToolProjectionRequest,
3952
): AnyAgentTool {
4053
assertActive();
54+
if (projection.kind !== targetTool) {
55+
throw new Error("scheduled tool projection does not match its host-created source");
56+
}
57+
const projectedTool =
58+
projection.kind === "exec"
59+
? createScheduledExecProjection(sourceTool, projection)
60+
: { ...sourceTool, name: projection.name, description: projection.description };
4161
const binding: CronScheduledToolBinding =
4262
targetTool === "exec"
4363
? {
@@ -53,6 +73,39 @@ export function sealCronScheduledToolProjection(
5373
return projectedTool;
5474
}
5575

76+
function createScheduledExecProjection(
77+
sourceTool: AnyAgentTool,
78+
projection: Readonly<{
79+
name: string;
80+
description: string;
81+
followupText: string;
82+
ask?: "always";
83+
}>,
84+
): AnyAgentTool {
85+
const pinnedTool = pinExecToolTarget(sourceTool, {
86+
host: "gateway",
87+
...(projection.ask ? { ask: projection.ask } : {}),
88+
});
89+
return {
90+
...pinnedTool,
91+
name: projection.name,
92+
description: projection.description,
93+
execute: async (toolCallId, args, signal, onUpdate) => {
94+
const result = await pinnedTool.execute(toolCallId, args, signal, onUpdate);
95+
return {
96+
...result,
97+
content: result.content.map((item) =>
98+
item.type === "text"
99+
? Object.assign({}, item, {
100+
text: item.text.replace(PROCESS_FOLLOWUP_TEXT, projection.followupText),
101+
})
102+
: item,
103+
),
104+
};
105+
},
106+
};
107+
}
108+
56109
export function copyCronScheduledToolProjection(source: AnyAgentTool, target: AnyAgentTool): void {
57110
const projection = scheduledToolProjections.get(source);
58111
if (

‎src/agents/harness/host-capability-types.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ export type AgentHarnessHostCapabilities = Readonly<{
4747
options: AgentHarnessToolSurfaceOptions,
4848
bindingOptions?: Readonly<{ cwd?: string }>,
4949
) => AnyAgentTool[];
50-
/** Seals an alias only when its source came from this exact host-created surface. */
51-
sealScheduledToolProjection?: (
52-
sourceTool: AnyAgentTool,
53-
projectedTool: AnyAgentTool,
54-
) => AnyAgentTool;
5550
/** Core-owned byte binding for a native command approval, scoped to this admitted run. */
5651
prepareMutableFileApproval?: (request: { command: string; cwd?: string }) => Promise<
5752
| {

0 commit comments

Comments
 (0)