fix(hooks): propagate sessionKey in after_tool_call context#30511
fix(hooks): propagate sessionKey in after_tool_call context#30511scoootscooob wants to merge 1 commit intoopenclaw:mainfrom
Conversation
Greptile SummaryFixed a bug where
Confidence Score: 5/5
Last reviewed commit: eda0c32 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eda0c32c15
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| agentId: ctx.params.agentId, | ||
| sessionKey: ctx.params.sessionKey, |
There was a problem hiding this comment.
Add agent/session fields to tool handler params type
handleToolExecutionEnd now reads ctx.params.agentId and ctx.params.sessionKey, but ToolHandlerParams only picks runId, onBlockReplyFlush, onAgentEvent, and onToolResult from SubscribeEmbeddedPiSessionParams (src/agents/pi-embedded-subscribe.handlers.types.ts:133-136), so this change introduces TS2339 property errors and breaks type-checking for this file (reproducible with pnpm exec tsc -p tsconfig.json --noEmit). Please extend the handler param type (or switch to a context type that includes these fields) before using them here.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in the updated commit (738dc8e): added "sessionKey" to the ToolHandlerParams Pick type, so ctx.params.sessionKey is now type-safe. agentId is intentionally left as undefined since it does not exist on SubscribeEmbeddedPiSessionParams — see reply to the P2 comment below.
eda0c32 to
738dc8e
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 738dc8e3d0
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| toolName, | ||
| agentId: undefined, | ||
| sessionKey: undefined, | ||
| sessionKey: ctx.params.sessionKey, |
There was a problem hiding this comment.
Forward agentId to after_tool_call hook context
runAfterToolCall still sends agentId: undefined, so after_tool_call hooks cannot attribute tool executions to the originating agent in multi-agent/subagent runs. Fresh evidence in this commit is that the updated test context now includes agentId: "main", but this call site still drops it, leaving agent-scoped logging/policy hooks partially broken even after this fix.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid point. agentId is not available in handleToolExecutionEnd because SubscribeEmbeddedPiSessionParams does not carry it — the field does not exist on the type. The other after_tool_call call site in pi-tool-definition-adapter.ts also passes no agentId.
Threading agentId through would require adding it to SubscribeEmbeddedPiSessionParams, propagating it through the session setup chain, and updating all callers — a larger refactor that goes beyond this targeted fix.
This PR scopes to what is available today: sessionKey. A follow-up PR could add agentId to the session params for full agent-scoped context in both before_tool_call and after_tool_call hooks.
There was a problem hiding this comment.
Follow-up PR submitted: #30527 threads sessionAgentId through SubscribeEmbeddedPiSessionParams → ToolHandlerParams → handleToolExecutionEnd, so ctx.agentId now carries the resolved agent identity instead of undefined.
Follow-up to openclaw#30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
738dc8e to
c36cd4b
Compare
Follow-up to openclaw#30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The after_tool_call hook in handleToolExecutionEnd was passing `sessionKey: undefined` in the ToolContext, even though the value is available on ctx.params. This broke plugins that need session context in after_tool_call handlers (e.g., for per-session audit trails or security logging). - Add `sessionKey` to the `ToolHandlerParams` Pick type - Pass `ctx.params.sessionKey` through to the hook context - Add test assertion to prevent regression Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
c36cd4b to
8752d93
Compare
Follow-up to #30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit aad01ed)
|
Consolidating this work into #32201 so Option A lands as one mergeable unit. #32201 combines:
Link: #32201 |
#32201) * fix(hooks): deduplicate after_tool_call hook in embedded runs (cherry picked from commit c129a1a) * fix(hooks): propagate sessionKey in after_tool_call context The after_tool_call hook in handleToolExecutionEnd was passing `sessionKey: undefined` in the ToolContext, even though the value is available on ctx.params. This broke plugins that need session context in after_tool_call handlers (e.g., for per-session audit trails or security logging). - Add `sessionKey` to the `ToolHandlerParams` Pick type - Pass `ctx.params.sessionKey` through to the hook context - Add test assertion to prevent regression Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit b711738) * fix(hooks): thread agentId through to after_tool_call hook context Follow-up to #30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit aad01ed) * changelog: credit after_tool_call hook contributors * Update CHANGELOG.md * agents: preserve adjusted params until tool end * agents: emit after_tool_call with adjusted args * tests: cover adjusted after_tool_call params * tests: align adapter after_tool_call expectation --------- Co-authored-by: jbeno <jim@jimbeno.net> Co-authored-by: scoootscooob <zhentongfan@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
openclaw#32201) * fix(hooks): deduplicate after_tool_call hook in embedded runs (cherry picked from commit c129a1a) * fix(hooks): propagate sessionKey in after_tool_call context The after_tool_call hook in handleToolExecutionEnd was passing `sessionKey: undefined` in the ToolContext, even though the value is available on ctx.params. This broke plugins that need session context in after_tool_call handlers (e.g., for per-session audit trails or security logging). - Add `sessionKey` to the `ToolHandlerParams` Pick type - Pass `ctx.params.sessionKey` through to the hook context - Add test assertion to prevent regression Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit b711738) * fix(hooks): thread agentId through to after_tool_call hook context Follow-up to openclaw#30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit aad01ed) * changelog: credit after_tool_call hook contributors * Update CHANGELOG.md * agents: preserve adjusted params until tool end * agents: emit after_tool_call with adjusted args * tests: cover adjusted after_tool_call params * tests: align adapter after_tool_call expectation --------- Co-authored-by: jbeno <jim@jimbeno.net> Co-authored-by: scoootscooob <zhentongfan@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
openclaw#32201) * fix(hooks): deduplicate after_tool_call hook in embedded runs (cherry picked from commit c129a1a) * fix(hooks): propagate sessionKey in after_tool_call context The after_tool_call hook in handleToolExecutionEnd was passing `sessionKey: undefined` in the ToolContext, even though the value is available on ctx.params. This broke plugins that need session context in after_tool_call handlers (e.g., for per-session audit trails or security logging). - Add `sessionKey` to the `ToolHandlerParams` Pick type - Pass `ctx.params.sessionKey` through to the hook context - Add test assertion to prevent regression Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit b711738) * fix(hooks): thread agentId through to after_tool_call hook context Follow-up to openclaw#30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> (cherry picked from commit aad01ed) * changelog: credit after_tool_call hook contributors * Update CHANGELOG.md * agents: preserve adjusted params until tool end * agents: emit after_tool_call with adjusted args * tests: cover adjusted after_tool_call params * tests: align adapter after_tool_call expectation --------- Co-authored-by: jbeno <jim@jimbeno.net> Co-authored-by: scoootscooob <zhentongfan@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
openclaw#32201) * fix(hooks): deduplicate after_tool_call hook in embedded runs (cherry picked from commit c129a1a) * fix(hooks): propagate sessionKey in after_tool_call context The after_tool_call hook in handleToolExecutionEnd was passing `sessionKey: undefined` in the ToolContext, even though the value is available on ctx.params. This broke plugins that need session context in after_tool_call handlers (e.g., for per-session audit trails or security logging). - Add `sessionKey` to the `ToolHandlerParams` Pick type - Pass `ctx.params.sessionKey` through to the hook context - Add test assertion to prevent regression (cherry picked from commit b711738) * fix(hooks): thread agentId through to after_tool_call hook context Follow-up to openclaw#30511 — the after_tool_call hook context was passing `agentId: undefined` because SubscribeEmbeddedPiSessionParams did not carry the agent identity. This threads sessionAgentId (resolved in attempt.ts) through the session params into the tool handler context, giving plugins accurate agent-scoped context for both before_tool_call and after_tool_call hooks. Changes: - Add `agentId?: string` to SubscribeEmbeddedPiSessionParams - Add "agentId" to ToolHandlerParams Pick type - Pass `agentId: sessionAgentId` at the subscribeEmbeddedPiSession() call site in attempt.ts - Wire ctx.params.agentId into the after_tool_call hook context - Update tests to assert agentId propagation (cherry picked from commit aad01ed) * changelog: credit after_tool_call hook contributors * Update CHANGELOG.md * agents: preserve adjusted params until tool end * agents: emit after_tool_call with adjusted args * tests: cover adjusted after_tool_call params * tests: align adapter after_tool_call expectation --------- Co-authored-by: jbeno <jim@jimbeno.net> Co-authored-by: scoootscooob <zhentongfan@gmail.com>
Summary
Fixes a bug where the
after_tool_callplugin hook context was passingsessionKey: undefinedeven though the value is available onctx.params. This prevented third-party plugins from accessing session-scoped context in theirafter_tool_callhandlers."sessionKey"to theToolHandlerParamsPick type soctx.params.sessionKeyis accessiblectx.params.sessionKeyinto theafter_tool_callhook context (replacing hardcodedundefined)sessionKeypropagationFollow-up: #30527 threads
agentIdthrough the session params chain (addressing the remainingagentId: undefined).Motivation
Third-party plugins (e.g.
openclaw-plimsoll-security) usectx.sessionKeyinafter_tool_callhooks for per-session audit trails and security logging. Without this fix,sessionKeyis alwaysundefined, forcing plugins to fall back to hardcoded defaults.Test plan
wired-hooks-after-tool-call.test.ts— assertscontext.sessionKeyequals the provided valuetsc --noEmit)Files changed
src/agents/pi-embedded-subscribe.handlers.types.ts"sessionKey"toToolHandlerParamsPicksrc/agents/pi-embedded-subscribe.handlers.tools.tsctx.params.sessionKeyinto hook contextsrc/plugins/wired-hooks-after-tool-call.test.ts🤖 Generated with Claude Code