Repository navigation
fix(code-mode): align outer exec hook params - #83483
Conversation
|
Codex review: needs maintainer review before merge. Workflow note: Future ClawSweeper reviews update this same comment in place. How this review workflow works
Summary Reproducibility: yes. at source level. Current main's code-mode PR rating Rank-up moves:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. Real behavior proof Risk before merge
Maintainer options:
Next step before merge Security Review detailsBest possible solution: Land the additive alias/discriminator contract only if maintainers explicitly accept that broad Do we have a high-confidence way to reproduce the issue? Yes at source level. Current main's code-mode Is this the best way to solve the issue? Unclear until maintainer contract review. If maintainers want broad Label changes:
Label justifications:
Security concerns:
What I checked:
Likely related people:
Codex review notes: model gpt-5.5, reasoning high; reviewed against 504f0dfa36be. |
5c7c872 to
3b1bc5c
Compare
How to Keep Code-Mode Exec Distinct from Shell Exec PoliciesFour distinct tools that all involve "running code" reach the
A policy author writing Why this falls short of "Codex parity" — and whether that framing actually mattersWe initially framed the goal as parity with upstream Codex Code Mode. Source-grounded inspection shows upstream Codex Code Mode is a freeform custom tool (
A policy author on upstream Codex writes per-tool rules. The collision in the Openclaw row is what creates the Openclaw policy-author ergonomics gap — Code Mode and shell exec share a tool name, so users expect them to look similar to hook policies even though their payloads diverge. The honest framing is: does Openclaw want its users to be able to write one shell-shape policy that covers both Pi shell and Code Mode, or do we want them to write per-mechanism rules? This is a product judgment more than a parity argument. Options we consideredOption 1 — Do nothingLeave the divergent payloads as-is. Policy authors who want coverage across both runtimes write defensive checks like
Option 2 — Add
|
3b1bc5c to
b3bdeac
Compare
b3bdeac to
e8763b3
Compare
|
@clawsweeper re-review Current head |
|
🦞🧹 I asked ClawSweeper to review this item again. Re-review progress:
|
1738358 to
3b40c53
Compare
|
ClawSweeper PR egg 🔥 Warming up: real-behavior proof passed; findings, security review, or rank-up moves are still in progress. Hatch commandComment Hatchability rules:
What is this egg doing here?
|
|
Current-head proof refresh is now in the PR body for Maintainer signoff requested on the hook contract: this PR intentionally lets |
|
@clawsweeper ask teach me how code mode works here and how this pr changes current behavior and explain why it is broken |
|
🦞👀 I queued a lightweight read-only assist pass. It will post a separate answer comment and will not edit the durable ClawSweeper review comment or trigger close, merge, repair, label, or branch changes. Request: teach me how code mode works here and how this pr changes current behavior and explain why it is broken |
|
ClawSweeper assist: Code mode is an opt-in agent mode where the model sees only outer control tools, mainly The broken part is the hook contract for the outer code-mode This PR changes outer code-mode Evidence:
Suggested next action: Decide whether OpenClaw wants this contract: broad Source: #83483 (comment) |
|
ClawSweeper assist: Code mode makes the model call a small outer tool surface, mainly The breakage is in the hook-facing shape: outer code-mode This PR keeps Evidence:
Suggested next action: Decide whether OpenClaw wants broad Source: #83483 (comment) |
3b40c53 to
516c4c1
Compare
516c4c1 to
18ae53e
Compare
|
Landed via rebase onto
Thanks @Kaspre! |
Follow-up to #83387 after #83481 landed the active hook-context plumbing.
Problem
Code-mode exposes its outer
execcontrol tool through the Pi custom-tool adapter. On currentmain, the active hook context is already threaded into that outer dispatch viatoolHookContext, which givesbefore_tool_callhandlers the active agent/session/run identity.The remaining gap is that the outer code-mode
execinput is model-facingcode, while conventional exec-shaped hook policies inspectcommand. That makes it harder for one policy to reason consistently about outer code-modeexecand normal exec-shaped dispatches.Why this matters
Several code execution surfaces can reach hook policy with similar intent but different names and payload shapes:
execcodetoday,code+commandafter this PRexeccommandshell_command/unified_execcommandcode_mode_execwithexecaliascommandThe important OpenClaw-specific problem is that code-mode
execand Pi shellexecshare the same hook-facing tool name, but historically only shell-style exec exposedparams.command. That makes broadexec/commandpolicies easy to write but incomplete for code-mode cells.This PR keeps the model-facing code-mode tool named
exec, mirrors code-mode source intoparams.command, and addstoolKind: "code_mode_exec"so policy authors can choose either broad execution coverage or code-mode-specific handling.Compatibility note: existing policies that match
event.toolName === "exec"and inspectevent.params.commandmay now also evaluate outer code-mode cells. Policies intended to apply only to shell-style exec should excludeevent.toolKind === "code_mode_exec".Change and value
This PR preserves the current active hook-context path and aligns the outer code-mode
execinput contract with exec-shaped policy expectations:codeaccepted as the documented model-facing fieldcommandas an alias for code-modeexeccodeandcommandinbefore_tool_callhook payloads for marked code-mode control tools onlytoolKind: "code_mode_exec"andtoolInputKindmetadata so policies can distinguish code-mode cells from shell-styleexecafter_tool_callobserves the payload that actually executedcodeandcommandvalues before executionScope
Affected:
before_tool_call,after_tool_call, or trusted policies that inspect outer code-modeexecexecto expose an exec-compatiblecommandfield while still being distinguishable from shell execNot affected:
execbehavior, which remains unmarked by the code-mode discriminatorexeccodefield, which remains acceptedexeccontrol toolMaintainer decision needed
This PR intentionally makes outer code-mode
execvisible to policies that matchevent.toolName === "exec"and inspectevent.params.command. The opt-out / audit shape is the additive host-authored discriminator pair:toolKind: "code_mode_exec"andtoolInputKind: "javascript" | "typescript"when the language is known.Please confirm whether
commandplustoolKind/toolInputKindis the intended outer code-mode exec hook contract. If maintainers would rather require policy authors to handlecodedirectly, or rename the hook-facing tool instead of adding discriminators, this PR should change shape before merge.Verification
Current head:
3b40c538ccdcaff62f9a463e689e096285dab9f2.git diff --check origin/main...HEADtimeout 120s node_modules/.bin/oxfmt --check docs/plugins/hooks.md docs/reference/code-mode.md src/plugins/trusted-tool-policy.ts src/agents/code-mode-control-tools.ts src/agents/code-mode.ts src/agents/code-mode.test.ts src/agents/pi-tools.before-tool-call.ts src/agents/pi-tool-definition-adapter.ts src/agents/pi-tools.before-tool-call.integration.e2e.test.ts src/agents/pi-tool-definition-adapter.after-tool-call.test.ts src/agents/pi-tool-definition-adapter.after-tool-call.fires-once.test.ts src/plugins/hook-types.tstimeout 120s node_modules/.bin/oxlint docs/plugins/hooks.md docs/reference/code-mode.md src/plugins/trusted-tool-policy.ts src/agents/code-mode-control-tools.ts src/agents/code-mode.ts src/agents/code-mode.test.ts src/agents/pi-tools.before-tool-call.ts src/agents/pi-tool-definition-adapter.ts src/agents/pi-tools.before-tool-call.integration.e2e.test.ts src/agents/pi-tool-definition-adapter.after-tool-call.test.ts src/agents/pi-tool-definition-adapter.after-tool-call.fires-once.test.ts src/plugins/hook-types.tstimeout 180s node_modules/vitest/vitest.mjs run --config test/vitest/vitest.e2e.config.ts src/agents/pi-tools.before-tool-call.integration.e2e.test.ts -t "agent context to outer code-mode exec|trusted policy rewrites|single-alias" --reporter verbosepassed: 3 tests passed, 16 skipped by filtertimeout 220s node_modules/vitest/vitest.mjs run --config test/vitest/vitest.e2e.config.ts src/agents/pi-tools.before-tool-call.integration.e2e.test.ts --reporter verbosepassed: 19 tests passedtimeout 120s node_modules/vitest/vitest.mjs run --config test/vitest/vitest.contracts-plugin.config.ts src/plugins/contracts/host-hooks.contract.test.ts -t "trusted policy|derived paths" --reporter verbosepassed: 8 tests passed, 42 skipped by filtertimeout 120s node_modules/vitest/vitest.mjs run --config test/vitest/vitest.agents-core.config.ts src/agents/pi-tool-definition-adapter.after-tool-call.test.ts src/agents/pi-tool-definition-adapter.after-tool-call.fires-once.test.ts --reporter verbosepassed: 7 tests passedtimeout 60s node --import tsx /tmp/openclaw-code-mode-hook-proof.mjsReal behavior proof
Behavior or issue addressed: Outer code-mode
execnow exposes executable source under both the model-facingcodefield and the exec-shapedcommandalias when hooks inspect the dispatch. The same outer dispatch keeps the active agent/session/run/tool-call context from the #83481 hook-context path and includestoolKind: "code_mode_exec"plustoolInputKindwhen the input language is known, so shell-style exec policies can deliberately distinguish code-mode cells. Trusted-policy rewrites are normalized before later trusted policies and normal hooks observe params, including command-only rewrites and language changes. Reconciled adapter params are recorded soafter_tool_callobserves the payload that actually executed.Real environment tested: Local OpenClaw source worktree at head
3b40c538ccdcaff62f9a463e689e096285dab9f2, Node v26.1.0, pnpm 11.1.0, Linux x86_64. The runtime proof registered a real globalbefore_tool_callhook runner, sent the code-modeexeccontrol tool through the production Pi custom-tool adapter, and compared it with an unmarked plainexectool.Exact steps or command run after this patch: From
/tmp/openclaw-pr-83387, ran:Evidence after fix: Formatting and lint passed on the touched docs, source, and test files:
The affected integration file completed successfully:
The focused post-review regression check completed successfully and covers direct TypeScript input, trusted-policy command-only rewrites, trusted-policy language rewrites, and hook single-alias rewrites:
The focused contracts check for trusted-policy and derived-path behavior completed successfully:
The adapter after-hook focused tests completed successfully:
The runtime hook proof produced this redacted live hook observation:
{ "proof": "OpenClaw runtime before_tool_call hook observation", "head": "3b40c538ccdcaff62f9a463e689e096285dab9f2", "codeModeResult": { "status": "blocked", "deniedReason": "plugin-before-tool-call", "reason": "runtime proof captured code_mode_exec" }, "plainExecResult": { "status": "blocked", "deniedReason": "plugin-before-tool-call", "reason": "runtime proof captured plain_exec" }, "observations": [ { "event": { "toolName": "exec", "toolKind": "code_mode_exec", "toolInputKind": "javascript", "runId": "run-runtime-proof", "toolCallId": "proof-code-mode-exec", "params": { "hasCode": true, "hasCommand": true, "codeEqualsCommand": true, "code": "console.log('runtime proof');", "command": "console.log('runtime proof');", "language": "javascript" } }, "context": { "toolName": "exec", "toolKind": "code_mode_exec", "toolInputKind": "javascript", "agentId": "main", "sessionKey": "agent:main:runtime-proof", "sessionId": "session-runtime-proof", "runId": "run-runtime-proof", "toolCallId": "proof-code-mode-exec" } }, { "event": { "toolName": "exec", "toolKind": null, "toolInputKind": null, "runId": "run-runtime-proof", "toolCallId": "proof-plain-exec", "params": { "hasCode": false, "hasCommand": true, "codeEqualsCommand": false, "command": "echo runtime-proof" } }, "context": { "toolName": "exec", "toolKind": null, "toolInputKind": null, "agentId": "main", "sessionKey": "agent:main:runtime-proof", "sessionId": "session-runtime-proof", "runId": "run-runtime-proof", "toolCallId": "proof-plain-exec" } } ] }Observed result after fix: Hooks inspecting outer code-mode
execseecodeandcommandmirrored for code-only, command-only, malformed-counterpart, trusted-policy command-only rewrite, and hook single-alias rewrite inputs. Trusted-policy chains see normalized params and recomputed input kind between policies.after_tool_callsees reconciled adapter params for rewritten code-mode exec payloads. A plain unmarked tool namedexecstill reaches hooks withouttoolKind, so existing shell-style policies can distinguish the two paths instead of relying on tool name alone.What was not tested: I did not run a live model/gateway conversation. The runtime hook proof intentionally blocked before executing source so the evidence isolates the hook contract and does not depend on model output. Two earlier attempts to run these same affected standard-config tests through the top-level Vitest project config timed out during project startup, so I reran the affected files with their scoped configs instead.