Skip to content

fix(hooks): propagate sessionKey in after_tool_call context#30511

Closed
scoootscooob wants to merge 1 commit intoopenclaw:mainfrom
scoootscooob:fix/after-tool-call-context
Closed

fix(hooks): propagate sessionKey in after_tool_call context#30511
scoootscooob wants to merge 1 commit intoopenclaw:mainfrom
scoootscooob:fix/after-tool-call-context

Conversation

@scoootscooob
Copy link
Contributor

@scoootscooob scoootscooob commented Mar 1, 2026

Summary

Fixes a bug where the after_tool_call plugin hook context was passing sessionKey: undefined even though the value is available on ctx.params. This prevented third-party plugins from accessing session-scoped context in their after_tool_call handlers.

  • Adds "sessionKey" to the ToolHandlerParams Pick type so ctx.params.sessionKey is accessible
  • Wires ctx.params.sessionKey into the after_tool_call hook context (replacing hardcoded undefined)
  • Updates test to assert sessionKey propagation

Follow-up: #30527 threads agentId through the session params chain (addressing the remaining agentId: undefined).

Motivation

Third-party plugins (e.g. openclaw-plimsoll-security) use ctx.sessionKey in after_tool_call hooks for per-session audit trails and security logging. Without this fix, sessionKey is always undefined, forcing plugins to fall back to hardcoded defaults.

Test plan

  • Updated wired-hooks-after-tool-call.test.ts — asserts context.sessionKey equals the provided value
  • All 3 tests pass
  • TypeScript compiles cleanly (tsc --noEmit)

Files changed

File Change
src/agents/pi-embedded-subscribe.handlers.types.ts Add "sessionKey" to ToolHandlerParams Pick
src/agents/pi-embedded-subscribe.handlers.tools.ts Wire ctx.params.sessionKey into hook context
src/plugins/wired-hooks-after-tool-call.test.ts Assert sessionKey propagation

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle bot added agents Agent runtime and tooling size: XS labels Mar 1, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 1, 2026

Greptile Summary

Fixed a bug where agentId and sessionKey were hardcoded as undefined in the after_tool_call hook context, even though both values were available in ctx.params.

  • Changed pi-embedded-subscribe.handlers.tools.ts to pass ctx.params.agentId and ctx.params.sessionKey instead of undefined
  • Updated test assertions in wired-hooks-after-tool-call.test.ts to verify both values are correctly propagated
  • This enables plugins to access session and agent context in after_tool_call handlers for use cases like per-session audit trails, security logging, and analytics

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is straightforward and correct - it simply passes through two existing context values instead of hardcoding them as undefined. The change is backward compatible since agentId and sessionKey are optional fields in the PluginHookToolContext type, and comprehensive test coverage validates the fix
  • No files require special attention

Last reviewed commit: eda0c32

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +429 to +430
agentId: ctx.params.agentId,
sessionKey: ctx.params.sessionKey,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@scoootscooob scoootscooob force-pushed the fix/after-tool-call-context branch from eda0c32 to 738dc8e Compare March 1, 2026 09:40
@scoootscooob scoootscooob changed the title fix(hooks): propagate agentId and sessionKey in after_tool_call context fix(hooks): propagate sessionKey in after_tool_call context Mar 1, 2026
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines 428 to +430
toolName,
agentId: undefined,
sessionKey: undefined,
sessionKey: ctx.params.sessionKey,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up PR submitted: #30527 threads sessionAgentId through SubscribeEmbeddedPiSessionParamsToolHandlerParamshandleToolExecutionEnd, so ctx.agentId now carries the resolved agent identity instead of undefined.

scoootscooob added a commit to scoootscooob/openclaw that referenced this pull request Mar 1, 2026
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>
@scoootscooob scoootscooob force-pushed the fix/after-tool-call-context branch from 738dc8e to c36cd4b Compare March 2, 2026 17:36
scoootscooob added a commit to scoootscooob/openclaw that referenced this pull request Mar 2, 2026
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>
@scoootscooob scoootscooob force-pushed the fix/after-tool-call-context branch from c36cd4b to 8752d93 Compare March 2, 2026 20:16
@vincentkoc vincentkoc self-assigned this Mar 2, 2026
vincentkoc pushed a commit that referenced this pull request Mar 2, 2026
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)
@vincentkoc
Copy link
Member

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

#32201 combines:

  • embedded after_tool_call session context propagation (sessionKey + agentId)
  • single-fire after_tool_call behavior (no duplicate adapter + embedded dispatch)
  • changelog credit for contributing authors

Link: #32201

vincentkoc added a commit that referenced this pull request Mar 2, 2026
#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>
@vincentkoc
Copy link
Member

Superseded by #32201, which is now merged.

This PR's sessionKey propagation work is included in the consolidated merge:
#32201

@vincentkoc vincentkoc closed this Mar 2, 2026
dawi369 pushed a commit to dawi369/davis that referenced this pull request Mar 3, 2026
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>
OWALabuy pushed a commit to kcinzgg/openclaw that referenced this pull request Mar 4, 2026
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>
zooqueen pushed a commit to hanzoai/bot that referenced this pull request Mar 6, 2026
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>
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