Skip to content

fix(agents): preserve code mode hook context - #83481

Merged
steipete merged 1 commit into
mainfrom
fix/code-mode-hook-context
May 18, 2026
Merged

steipete merged 1 commit into
mainfrom
fix/code-mode-hook-context

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Summary

  • Thread hook context through Pi custom tool adaptation so top-level code-mode exec/wait dispatches keep agent/session/run/channel metadata.
  • Preserve the existing wrapped-tool dedupe path while adding coverage for unwrapped tool definitions.
  • Add a changelog entry for the plugin hook behavior fix.

Fixes #83387

Verification

  • node scripts/run-vitest.mjs src/agents/pi-tools.before-tool-call.integration.e2e.test.ts src/agents/code-mode.test.ts src/agents/tool-search.test.ts src/agents/pi-embedded-runner.splitsdktools.test.ts src/agents/pi-tool-definition-adapter.after-tool-call.test.ts
  • pnpm format:fix src/agents/pi-tool-definition-adapter.ts src/agents/pi-embedded-runner/tool-split.ts src/agents/pi-embedded-runner/run/attempt.ts src/agents/pi-embedded-runner/compact.ts src/agents/pi-tools.before-tool-call.integration.e2e.test.ts CHANGELOG.md
  • git diff --check
  • .agents/skills/autoreview/scripts/autoreview --mode auto

Real behavior proof

Behavior addressed: before_tool_call hooks now receive agent/session/run/channel context for top-level OpenClaw code-mode exec/wait dispatches instead of seeing an undefined ctx.agentId.
Real environment tested: local OpenClaw checkout on macOS with targeted Vitest coverage for the code-mode/tool-search/hook adapter path.
Exact steps or command run after this patch: node scripts/run-vitest.mjs src/agents/pi-tools.before-tool-call.integration.e2e.test.ts src/agents/code-mode.test.ts src/agents/tool-search.test.ts src/agents/pi-embedded-runner.splitsdktools.test.ts src/agents/pi-tool-definition-adapter.after-tool-call.test.ts
Evidence after fix: the new regression executes an unwrapped exec tool definition with hook context and asserts the plugin hook receives agentId, sessionKey, sessionId, runId, and channelId; the existing dedupe tests still prove wrapped tools fire once.
Observed result after fix: 7 test files passed, 95 tests passed; autoreview reported no accepted/actionable findings.
What was not tested: no live model/gateway run; the failing boundary is covered locally at the adapter and code-mode catalog/tool-search unit level.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels May 18, 2026
@clawsweeper

clawsweeper Bot commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge.

Workflow note: Future ClawSweeper reviews update this same comment in place.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

Summary
The PR threads HookContext through Pi tool-definition adaptation for code-mode exec/wait dispatches, adds regression coverage for unwrapped tool definitions, and records a changelog fix.

Reproducibility: yes. Source inspection shows outer code-mode controls reach toToolDefinitions without ctx on current main, and runBeforeToolCallHook only forwards agentId when args.ctx is present.

PR rating
Overall: 🐚 platinum hermit
Proof: 🌊 off-meta tidepool
Patch quality: 🐚 platinum hermit
Summary: The patch is focused and source-backed with targeted regression coverage, but merge readiness still depends on maintainer acceptance of the hook-policy compatibility change.

Rank-up moves:

  • Optionally add a redacted live code-mode hook log if maintainers want runtime proof beyond targeted tests.
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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
Not applicable: This maintainer-labeled PR includes targeted after-fix test output but no live model/gateway run; the external-contributor real-behavior proof gate is not applied here.

Risk before merge
Why this matters: - Merging changes the authorization/audit context seen by before_tool_call plugins for outer code-mode exec/wait, so plugins or workarounds keyed to missing or unknown agent context may need adjustment even though this restores the intended contract.

  • The PR proof is targeted test and autoreview output only; no live model or gateway code-mode run was attached.

Maintainer options:

  1. Land as hook-contract restoration (recommended)
    Maintainers can accept the compatibility risk because the patch restores the same run context already used by nested/catalog tool calls and preserves wrapped-tool dedupe.
  2. Ask for live hook proof first
    Before merge, ask for a redacted local or live code-mode run showing before_tool_call receives agent/session/run/channel context for outer exec or wait.

Next step before merge
No automated repair is needed; maintainer/security review should decide whether to land the focused auth-hook context change with the known compatibility risk.

Security
Cleared: The diff restores hook context used by authorization/audit plugins and does not introduce new dependencies, workflow permissions, secret handling, or supply-chain changes.

Review details

Best possible solution:

Land the focused context-threading fix after maintainer/security acceptance, and keep any code-vs-command input-shape alignment as a separate hook-contract decision.

Do we have a high-confidence way to reproduce the issue?

Yes. Source inspection shows outer code-mode controls reach toToolDefinitions without ctx on current main, and runBeforeToolCallHook only forwards agentId when args.ctx is present.

Is this the best way to solve the issue?

Yes for the minimum bug. Passing the already-built run HookContext through splitSdkTools into the adapter is narrower and more maintainable than asking plugins to recover agent identity from sessionKey; the input-shape concern should remain separate.

Label justifications:

  • P1: The linked regression affects per-agent authorization and audit hooks for code-mode tool execution in real plugin workflows.
  • merge-risk: 🚨 compatibility: External before_tool_call plugins may change policy decisions once outer code-mode exec/wait starts carrying real agent and session context.

What I checked:

  • Current main adapter omits hook context on unwrapped ToolDefinition calls: On current main, toToolDefinitions calls runBeforeToolCallHook with toolName, params, and toolCallId only; unwrapped visible controls therefore cannot supply agent/session/run/channel context to before_tool_call hooks. (src/agents/pi-tool-definition-adapter.ts:241, 56024b782814)
  • Run context already exists before code-mode controls are exposed: runEmbeddedAttempt builds catalogToolHookContext with agentId, config, cwd, sessionKey, sessionId, runId, channelId, trace, loopDetection, and onToolOutcome, but the current splitSdkTools call does not pass it into the adapter. (src/agents/pi-embedded-runner/run/attempt.ts:1680, 56024b782814)
  • Code-mode control tools remain visible while nested catalog tools are wrapped: applyToolCatalogCompaction keeps visible code-mode controls unwrapped while cataloging other tools with toolHookContext, matching the linked issue's distinction between outer exec/wait and nested tool calls. (src/agents/tool-search.ts:1105, 56024b782814)
  • PR diff passes context into the missing adapter path: The PR adds an optional HookContext argument to toToolDefinitions, forwards it from splitSdkTools, and supplies catalogToolHookContext from the main run path plus analogous compaction context. (src/agents/pi-tool-definition-adapter.ts:245, 7a9c49f34c25)
  • Regression coverage targets the missing unwrapped-tool path: The PR adds a before_tool_call regression that executes an unwrapped exec ToolDefinition and asserts the hook context includes agentId, sessionKey, sessionId, runId, and channelId while existing dedupe coverage remains in place. (src/agents/pi-tools.before-tool-call.integration.e2e.test.ts:316, 7a9c49f34c25)
  • Linked issue discussion confirms security-sensitive source reproduction: The linked issue's ClawSweeper review kept it open as source-reproducible and security-sensitive because before_tool_call policy plugins can see undefined agent context on outer code-mode exec dispatches.

Likely related people:

  • steipete: Remote commit history shows steipete introduced generic OpenClaw code mode and made recent code-mode hardening changes on the central files touched by this PR. (role: introduced behavior / recent feature owner; confidence: high; commits: 0844e771a8df, 8985cd596a86, 0db097936568; files: src/agents/code-mode.ts, src/agents/tool-search.ts)
  • joshavant: Recent history on before_tool_call policy enforcement and local blame for the current checkout point to adjacent maintenance of the hook-policy surface. (role: recent adjacent owner; confidence: medium; commits: e57b137aef41, 395bd578d2; files: src/agents/pi-tools.before-tool-call.ts, src/agents/pi-tool-definition-adapter.ts, src/agents/pi-embedded-runner/run/attempt.ts)
  • 100yenadmin: The generic plugin host-hook contract commit touched the before_tool_call contract path that this PR relies on for ctx.agentId forwarding. (role: hook contract introducer; confidence: medium; commits: 1adaa28dc86b; files: src/agents/pi-tools.before-tool-call.ts, src/plugins/host-tool-param-parsers.ts, src/plugins/contracts/host-hooks.contract.test.ts)
  • jalehman: The host-hook contract history names jalehman as reviewer/co-author context for the hook surface, so they are a useful secondary routing candidate if maintainers want contract review. (role: reviewer / adjacent hook-contract owner; confidence: low; commits: 1adaa28dc86b; files: src/agents/pi-tools.before-tool-call.ts, src/plugins/contracts/host-hooks.contract.test.ts)

Codex review notes: model gpt-5.5, reasoning high; reviewed against 56024b782814.

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels May 18, 2026
@steipete steipete added the proof: override Maintainer override for the external PR real behavior proof gate. label May 18, 2026
@steipete
steipete merged commit 69aec10 into main May 18, 2026
154 of 165 checks passed
@steipete
steipete deleted the fix/code-mode-hook-context branch May 18, 2026 07:00
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
galiniliev pushed a commit to galiniliev/openclaw that referenced this pull request May 25, 2026
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. proof: override Maintainer override for the external PR real behavior proof gate. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

code-mode: ctx.agentId undefined in before_tool_call for outer code-mode exec dispatch

1 participant