Skip to content

fix(hooks): thread agentId into after_tool_call hook context#30527

Closed
scoootscooob wants to merge 2 commits intoopenclaw:mainfrom
scoootscooob:fix/after-tool-call-agent-id
Closed

fix(hooks): thread agentId into after_tool_call hook context#30527
scoootscooob wants to merge 2 commits intoopenclaw:mainfrom
scoootscooob:fix/after-tool-call-agent-id

Conversation

@scoootscooob
Copy link
Contributor

Summary

Follow-up to #30511 — threads agentId through the session params chain so the after_tool_call hook context receives the resolved agent identity instead of undefined.

  • Adds agentId?: string to SubscribeEmbeddedPiSessionParams
  • Adds "agentId" to the ToolHandlerParams Pick type
  • Passes sessionAgentId (resolved in attempt.ts via resolveSessionAgentIds()) at the subscribeEmbeddedPiSession() call site
  • Wires ctx.params.agentId into the after_tool_call hook context (replacing hardcoded undefined)

Motivation

Third-party plugins (e.g. openclaw-plimsoll-security) use ctx.agentId in both before_tool_call and after_tool_call hooks for agent-scoped audit trails and per-agent security policies. Without this fix, agentId is always undefined in the hook context, forcing plugins to fall back to sessionKey or a hardcoded default.

Test plan

  • Updated wired-hooks-after-tool-call.test.ts — asserts context.agentId equals the provided value
  • Added assertion that agentId is undefined when not provided (no-agentId path)
  • All 3 existing tests pass
  • TypeScript compiles cleanly (tsc --noEmit)

Files changed

File Change
src/agents/pi-embedded-subscribe.types.ts Add agentId?: string field
src/agents/pi-embedded-subscribe.handlers.types.ts Add "agentId" to ToolHandlerParams Pick
src/agents/pi-embedded-runner/run/attempt.ts Pass agentId: sessionAgentId to subscribeEmbeddedPiSession()
src/agents/pi-embedded-subscribe.handlers.tools.ts Wire ctx.params.agentId into hook context
src/plugins/wired-hooks-after-tool-call.test.ts Assert agentId propagation

Depends on #30511.

🤖 Generated with Claude Code

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 1, 2026

Greptile Summary

This PR successfully threads agentId through to the after_tool_call hook context, following the same pattern as the recent sessionKey propagation (commit 738dc8e).

  • Added agentId field to SubscribeEmbeddedPiSessionParams and ToolHandlerParams
  • Passes resolved sessionAgentId from attempt.ts through to subscribeEmbeddedPiSession()
  • Wires ctx.params.agentId into hook context, replacing hardcoded undefined
  • Test coverage verifies both presence and absence of agentId

This enables third-party plugins (like openclaw-plimsoll-security) to access agent identity for per-agent security policies and audit trails.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation follows an established pattern (sessionKey propagation from commit 738dc8e), has comprehensive test coverage including both positive and negative cases, is properly typed, and makes no breaking changes. The change is small, focused, and well-documented.
  • No files require special attention

Last reviewed commit: fa819f4

scoootscooob and others added 2 commits March 2, 2026 09:36
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>
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>
@vincentkoc
Copy link
Member

Consolidating this work into #32201 so Option A lands as one mergeable unit.

#32201 includes the embedded handler context propagation from this PR (sessionKey + agentId) and combines it with single-fire after_tool_call behavior.

Link: #32201

@vincentkoc
Copy link
Member

Superseded by #32201, which is now merged.

This PR's embedded agentId + sessionKey propagation is included in the consolidated merge:
#32201

@vincentkoc vincentkoc closed this Mar 2, 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 size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants