Skip to content

Commit f27b4de

Browse files
committed
fix(codex): expose sandbox shell tools for ssh backends
1 parent c49d909 commit f27b4de

3 files changed

Lines changed: 268 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Docs: https://docs.openclaw.ai
6262
- Plugins/xAI: echo PKCE challenge fields during OAuth authorization-code token exchange for xAI token-endpoint compatibility. (#83499) Thanks @fuller-stack-dev.
6363
- Codex app-server: hydrate current inbound image attachments before queued runs so Responses-backed agents receive Discord and other channel images as native vision input. Fixes #83466. Thanks @iannwu.
6464
- Codex app-server: keep native code mode available without forcing code-mode-only so OpenClaw dynamic tool turns complete through the app-server tool bridge. Fixes #83109. Thanks @daswass.
65+
- Codex app-server: expose OpenClaw's sandbox-routed shell as `sandbox_exec`/`sandbox_process` for non-Docker sandbox backends so SSH sandbox agents keep a correctly routed shell path without shadowing Codex native shell. Fixes #80322. Thanks @keramblock.
6566
- Release stability: recover stale session diagnostics and Codex OAuth fallback state so stuck runs and reused refresh tokens clear without blocking follow-up work. (#83503) Thanks @100yenadmin.
6667
- Messages/TTS: apply TTS directives before message-tool sends reach core, gateway, or plugin delivery so opt-in message-tool rooms and proactive sends attach voice notes instead of leaking raw tags. Fixes #81598. Thanks @CG-Intelligence-Agent-Jack and @CoronovirusG10.
6768
- Messages/Codex: keep Codex direct/source chats on message-tool visible delivery by default while documenting and testing `messages.visibleReplies: "automatic"` as the old-mode opt-out; channel wildcard model overrides now apply to direct chats before harness delivery defaults.

‎extensions/codex/src/app-server/run-attempt.test.ts‎

Lines changed: 186 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import * as authBridge from "./auth-bridge.js";
3232
import { resolveCodexAppServerEnvApiKeyCacheKey } from "./auth-bridge.js";
3333
import type { CodexAppServerClientFactory } from "./client-factory.js";
3434
import { readCodexPluginConfig, resolveCodexAppServerRuntimeOptions } from "./config.js";
35-
import { CODEX_OPENCLAW_DYNAMIC_TOOL_NAMESPACE } from "./dynamic-tools.js";
35+
import {
36+
CODEX_OPENCLAW_DYNAMIC_TOOL_NAMESPACE,
37+
createCodexDynamicToolBridge,
38+
} from "./dynamic-tools.js";
3639
import * as elicitationBridge from "./elicitation-bridge.js";
3740
import {
3841
buildCodexPluginAppCacheKey,
@@ -432,9 +435,14 @@ function createNamedDynamicTool(
432435
};
433436
}
434437

435-
function createRuntimeDynamicTool(name: string) {
438+
type RuntimeDynamicToolForTest = Parameters<
439+
typeof createCodexDynamicToolBridge
440+
>[0]["tools"][number];
441+
442+
function createRuntimeDynamicTool(name: string): RuntimeDynamicToolForTest {
436443
return {
437444
name,
445+
label: name,
438446
description: `${name} test tool`,
439447
parameters: {
440448
type: "object",
@@ -445,7 +453,7 @@ function createRuntimeDynamicTool(name: string) {
445453
content: [{ type: "text" as const, text: `${name} done` }],
446454
details: {},
447455
})),
448-
} as never;
456+
};
449457
}
450458

451459
function createPluginAppConfigPatch() {
@@ -663,6 +671,177 @@ describe("runCodexAppServerAttempt", () => {
663671
expect(testing.resolveCodexDynamicToolsLoading({}, privateQaCodexEnv)).toBe("direct");
664672
});
665673

674+
it("exposes OpenClaw sandbox shell tools under distinct names for non-Docker sandbox backends", async () => {
675+
testing.setOpenClawCodingToolsFactoryForTests(() => [
676+
createRuntimeDynamicTool("read"),
677+
createRuntimeDynamicTool("write"),
678+
createRuntimeDynamicTool("edit"),
679+
createRuntimeDynamicTool("apply_patch"),
680+
createRuntimeDynamicTool("exec"),
681+
createRuntimeDynamicTool("process"),
682+
createRuntimeDynamicTool("message"),
683+
]);
684+
const sessionFile = path.join(tempDir, "session.jsonl");
685+
const workspaceDir = path.join(tempDir, "workspace");
686+
const params = createParams(sessionFile, workspaceDir);
687+
params.disableTools = false;
688+
const sandboxSessionKey = params.sessionKey;
689+
if (!sandboxSessionKey) {
690+
throw new Error("createParams must provide a sessionKey for Codex dynamic tool tests.");
691+
}
692+
693+
const tools = await testing.buildDynamicTools({
694+
params,
695+
resolvedWorkspace: workspaceDir,
696+
effectiveWorkspace: workspaceDir,
697+
sandboxSessionKey,
698+
sandbox: { enabled: true, backendId: "ssh" } as never,
699+
runAbortController: new AbortController(),
700+
sessionAgentId: "main",
701+
pluginConfig: {},
702+
onYieldDetected: () => undefined,
703+
});
704+
705+
expect(tools.map((tool) => tool.name)).toEqual(["message", "sandbox_exec", "sandbox_process"]);
706+
expect(tools.find((tool) => tool.name === "sandbox_exec")?.description).toContain(
707+
"configured sandbox backend",
708+
);
709+
expect(tools.find((tool) => tool.name === "sandbox_process")?.description).toContain(
710+
"sandbox_exec sessions",
711+
);
712+
});
713+
714+
it("does not expose sandbox shell tools unless non-Docker sandbox routing is available", async () => {
715+
testing.setOpenClawCodingToolsFactoryForTests(() => [
716+
createRuntimeDynamicTool("exec"),
717+
createRuntimeDynamicTool("process"),
718+
createRuntimeDynamicTool("message"),
719+
]);
720+
const sessionFile = path.join(tempDir, "session.jsonl");
721+
const workspaceDir = path.join(tempDir, "workspace");
722+
const params = createParams(sessionFile, workspaceDir);
723+
params.disableTools = false;
724+
const sandboxSessionKey = params.sessionKey;
725+
if (!sandboxSessionKey) {
726+
throw new Error("createParams must provide a sessionKey for Codex dynamic tool tests.");
727+
}
728+
729+
const dockerTools = await testing.buildDynamicTools({
730+
params,
731+
resolvedWorkspace: workspaceDir,
732+
effectiveWorkspace: workspaceDir,
733+
sandboxSessionKey,
734+
sandbox: { enabled: true, backendId: "docker" } as never,
735+
runAbortController: new AbortController(),
736+
sessionAgentId: "main",
737+
pluginConfig: {},
738+
onYieldDetected: () => undefined,
739+
});
740+
const disabledSandboxTools = await testing.buildDynamicTools({
741+
params,
742+
resolvedWorkspace: workspaceDir,
743+
effectiveWorkspace: workspaceDir,
744+
sandboxSessionKey,
745+
sandbox: { enabled: false, backendId: "ssh" } as never,
746+
runAbortController: new AbortController(),
747+
sessionAgentId: "main",
748+
pluginConfig: {},
749+
onYieldDetected: () => undefined,
750+
});
751+
752+
expect(dockerTools.map((tool) => tool.name)).toEqual(["message"]);
753+
expect(disabledSandboxTools.map((tool) => tool.name)).toEqual(["message"]);
754+
});
755+
756+
it("does not expose sandbox_exec without a matching process follow-up tool", async () => {
757+
testing.setOpenClawCodingToolsFactoryForTests(() => [
758+
createRuntimeDynamicTool("exec"),
759+
createRuntimeDynamicTool("message"),
760+
]);
761+
const sessionFile = path.join(tempDir, "session.jsonl");
762+
const workspaceDir = path.join(tempDir, "workspace");
763+
const params = createParams(sessionFile, workspaceDir);
764+
params.disableTools = false;
765+
const sandboxSessionKey = params.sessionKey;
766+
if (!sandboxSessionKey) {
767+
throw new Error("createParams must provide a sessionKey for Codex dynamic tool tests.");
768+
}
769+
770+
const tools = await testing.buildDynamicTools({
771+
params,
772+
resolvedWorkspace: workspaceDir,
773+
effectiveWorkspace: workspaceDir,
774+
sandboxSessionKey,
775+
sandbox: { enabled: true, backendId: "ssh" } as never,
776+
runAbortController: new AbortController(),
777+
sessionAgentId: "main",
778+
pluginConfig: {},
779+
onYieldDetected: () => undefined,
780+
});
781+
782+
expect(tools.map((tool) => tool.name)).toEqual(["message"]);
783+
});
784+
785+
it("honors Codex dynamic tool excludes for sandbox shell exposure", async () => {
786+
testing.setOpenClawCodingToolsFactoryForTests(() => [
787+
createRuntimeDynamicTool("exec"),
788+
createRuntimeDynamicTool("process"),
789+
createRuntimeDynamicTool("message"),
790+
]);
791+
const sessionFile = path.join(tempDir, "session.jsonl");
792+
const workspaceDir = path.join(tempDir, "workspace");
793+
const params = createParams(sessionFile, workspaceDir);
794+
params.disableTools = false;
795+
const sandboxSessionKey = params.sessionKey;
796+
if (!sandboxSessionKey) {
797+
throw new Error("createParams must provide a sessionKey for Codex dynamic tool tests.");
798+
}
799+
800+
for (const excludedToolName of ["sandbox_exec", "process"]) {
801+
const tools = await testing.buildDynamicTools({
802+
params,
803+
resolvedWorkspace: workspaceDir,
804+
effectiveWorkspace: workspaceDir,
805+
sandboxSessionKey,
806+
sandbox: { enabled: true, backendId: "ssh" } as never,
807+
runAbortController: new AbortController(),
808+
sessionAgentId: "main",
809+
pluginConfig: { codexDynamicToolsExclude: [excludedToolName] },
810+
onYieldDetected: () => undefined,
811+
});
812+
813+
expect(tools.map((tool) => tool.name)).toEqual(["message"]);
814+
}
815+
});
816+
817+
it("points yielded sandbox_exec follow-up guidance at sandbox_process", async () => {
818+
const execTool = createRuntimeDynamicTool("exec");
819+
vi.mocked(execTool.execute).mockResolvedValueOnce({
820+
content: [
821+
{
822+
type: "text",
823+
text: "Command still running (session exec-1, pid 123). Use process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) for follow-up.",
824+
},
825+
],
826+
details: { status: "running" },
827+
});
828+
const processTool = createRuntimeDynamicTool("process");
829+
const tools = testing.addSandboxShellDynamicToolsIfAvailable([], [execTool, processTool], {
830+
sandbox: { enabled: true, backendId: "ssh" },
831+
pluginConfig: {},
832+
} as never);
833+
834+
const sandboxExec = tools.find((tool) => tool.name === "sandbox_exec");
835+
const result = await sandboxExec?.execute("call-1", {}, undefined);
836+
837+
expect(result?.content).toEqual([
838+
{
839+
type: "text",
840+
text: "Command still running (session exec-1, pid 123). Use sandbox_process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) for follow-up.",
841+
},
842+
]);
843+
});
844+
666845
it("starts Codex threads without duplicate OpenClaw workspace tools by default", async () => {
667846
const sessionFile = path.join(tempDir, "session.jsonl");
668847
const workspaceDir = path.join(tempDir, "workspace");
@@ -947,13 +1126,15 @@ describe("runCodexAppServerAttempt", () => {
9471126
});
9481127

9491128
it("normalizes Codex dynamic toolsAllow entries before filtering", () => {
950-
const tools = ["exec", "apply_patch", "read", "message"].map((name) => ({ name }));
1129+
const tools = ["exec", "sandbox_exec", "sandbox_process", "apply_patch", "read", "message"].map(
1130+
(name) => ({ name }),
1131+
);
9511132

9521133
expect(
9531134
testing
9541135
.filterCodexDynamicToolsForAllowlist(tools, [" BASH ", "apply-patch", "READ"])
9551136
.map((tool) => tool.name),
956-
).toEqual(["exec", "apply_patch", "read"]);
1137+
).toEqual(["exec", "sandbox_exec", "sandbox_process", "apply_patch", "read"]);
9571138
});
9581139

9591140
it("treats an explicit empty Codex dynamic toolsAllow as no tools", () => {

‎extensions/codex/src/app-server/run-attempt.ts‎

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ type OpenClawCodingToolsOptions = NonNullable<
198198
>;
199199
type OpenClawCodingToolsFactory =
200200
(typeof import("openclaw/plugin-sdk/agent-harness"))["createOpenClawCodingTools"];
201+
type OpenClawDynamicTool = ReturnType<OpenClawCodingToolsFactory>[number];
201202
type CodexBootstrapContext = Awaited<ReturnType<typeof resolveBootstrapContextForRun>>;
202203
type CodexBootstrapFile = CodexBootstrapContext["bootstrapFiles"][number];
203204
type CodexSystemPromptReport = NonNullable<EmbeddedRunAttemptResult["systemPromptReport"]>;
@@ -3127,7 +3128,11 @@ async function buildDynamicTools(input: DynamicToolBuildParams) {
31273128
input.runAbortController.abort("sessions_yield");
31283129
},
31293130
});
3130-
const codexFilteredTools = filterCodexDynamicTools(allTools, input.pluginConfig);
3131+
const codexFilteredTools = addSandboxShellDynamicToolsIfAvailable(
3132+
filterCodexDynamicTools(allTools, input.pluginConfig),
3133+
allTools,
3134+
input,
3135+
);
31313136
const visionFilteredTools = filterToolsForVisionInputs(codexFilteredTools, {
31323137
modelHasVision,
31333138
hasInboundImages: (params.images?.length ?? 0) > 0,
@@ -3187,6 +3192,72 @@ function disableCodexPluginThreadConfig(pluginConfig?: unknown): CodexPluginConf
31873192
};
31883193
}
31893194

3195+
function addSandboxShellDynamicToolsIfAvailable(
3196+
filteredTools: OpenClawDynamicTool[],
3197+
allTools: OpenClawDynamicTool[],
3198+
input: DynamicToolBuildParams,
3199+
): OpenClawDynamicTool[] {
3200+
if (
3201+
!shouldExposeSandboxExecDynamicTool(input) ||
3202+
isSandboxShellDynamicToolExcluded(input.pluginConfig)
3203+
) {
3204+
return filteredTools;
3205+
}
3206+
const execTool = allTools.find((tool) => normalizeCodexDynamicToolName(tool.name) === "exec");
3207+
const processTool = allTools.find(
3208+
(tool) => normalizeCodexDynamicToolName(tool.name) === "process",
3209+
);
3210+
if (!execTool || !processTool) {
3211+
return filteredTools;
3212+
}
3213+
const sandboxExecTool: OpenClawDynamicTool = {
3214+
...execTool,
3215+
name: "sandbox_exec",
3216+
description:
3217+
"Run a shell command through OpenClaw's configured sandbox backend for this session. Use only when the command must execute in the OpenClaw sandbox backend, such as an SSH-backed sandbox. Use Codex's native shell for normal local workspace commands.",
3218+
execute: async (toolCallId, args, signal, onUpdate) => {
3219+
const result = await execTool.execute(toolCallId, args, signal, onUpdate);
3220+
return {
3221+
...result,
3222+
content: result.content.map((item) =>
3223+
item.type === "text"
3224+
? Object.assign({}, item, {
3225+
text: item.text.replace(
3226+
"Use process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) for follow-up.",
3227+
"Use sandbox_process (list/poll/log/write/send-keys/submit/paste/kill/clear/remove) for follow-up.",
3228+
),
3229+
})
3230+
: item,
3231+
),
3232+
};
3233+
},
3234+
};
3235+
const sandboxProcessTool: OpenClawDynamicTool = {
3236+
...processTool,
3237+
name: "sandbox_process",
3238+
description:
3239+
"Manage sandbox_exec sessions that were started through OpenClaw's configured sandbox backend for this session: list, poll, log, write, send-keys, submit, paste, kill, clear, or remove. Use only for sandbox_exec follow-up; use Codex's native shell session handling for normal native shell commands.",
3240+
};
3241+
return [...filteredTools, sandboxExecTool, sandboxProcessTool];
3242+
}
3243+
3244+
function shouldExposeSandboxExecDynamicTool(input: DynamicToolBuildParams): boolean {
3245+
const backendId = input.sandbox?.enabled ? input.sandbox.backendId.trim().toLowerCase() : "";
3246+
return Boolean(backendId && backendId !== "docker");
3247+
}
3248+
3249+
function isSandboxShellDynamicToolExcluded(config: CodexPluginConfig): boolean {
3250+
return (config.codexDynamicToolsExclude ?? []).some((name) => {
3251+
const normalized = normalizeCodexDynamicToolName(name);
3252+
return (
3253+
normalized === "exec" ||
3254+
normalized === "sandbox_exec" ||
3255+
normalized === "process" ||
3256+
normalized === "sandbox_process"
3257+
);
3258+
});
3259+
}
3260+
31903261
function filterCodexDynamicToolsForAllowlist<T extends { name: string }>(
31913262
tools: T[],
31923263
toolsAllow?: string[],
@@ -3203,7 +3274,14 @@ function filterCodexDynamicToolsForAllowlist<T extends { name: string }>(
32033274
const allowSet = new Set(
32043275
toolsAllow.map((name) => normalizeCodexDynamicToolName(name)).filter(Boolean),
32053276
);
3206-
return tools.filter((tool) => allowSet.has(normalizeCodexDynamicToolName(tool.name)));
3277+
return tools.filter((tool) => {
3278+
const normalized = normalizeCodexDynamicToolName(tool.name);
3279+
return (
3280+
allowSet.has(normalized) ||
3281+
(normalized === "sandbox_exec" && allowSet.has("exec")) ||
3282+
(normalized === "sandbox_process" && (allowSet.has("exec") || allowSet.has("process")))
3283+
);
3284+
});
32073285
}
32083286

32093287
function hasWildcardCodexToolsAllow(toolsAllow: string[]): boolean {
@@ -4366,6 +4444,7 @@ export const testing = {
43664444
buildDeveloperInstructions,
43674445
filterCodexDynamicTools,
43684446
buildDynamicTools,
4447+
addSandboxShellDynamicToolsIfAvailable,
43694448
filterCodexDynamicToolsForAllowlist,
43704449
filterToolsForVisionInputs,
43714450
hasWildcardCodexToolsAllow,

0 commit comments

Comments
 (0)