From b7117384fc1a09d60b25db0f80847a3519ddb3c3 Mon Sep 17 00:00:00 2001 From: scoootscooob Date: Sun, 1 Mar 2026 01:27:23 -0800 Subject: [PATCH] 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 --- src/agents/pi-embedded-subscribe.handlers.tools.ts | 2 +- src/agents/pi-embedded-subscribe.handlers.types.ts | 2 +- src/plugins/wired-hooks-after-tool-call.e2e.test.ts | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/agents/pi-embedded-subscribe.handlers.tools.ts b/src/agents/pi-embedded-subscribe.handlers.tools.ts index 18dc11193f0..e41346a9e24 100644 --- a/src/agents/pi-embedded-subscribe.handlers.tools.ts +++ b/src/agents/pi-embedded-subscribe.handlers.tools.ts @@ -427,7 +427,7 @@ export async function handleToolExecutionEnd( .runAfterToolCall(hookEvent, { toolName, agentId: undefined, - sessionKey: undefined, + sessionKey: ctx.params.sessionKey, }) .catch((err) => { ctx.log.warn(`after_tool_call hook failed: tool=${toolName} error=${String(err)}`); diff --git a/src/agents/pi-embedded-subscribe.handlers.types.ts b/src/agents/pi-embedded-subscribe.handlers.types.ts index d5c725528c8..bd6c95b8190 100644 --- a/src/agents/pi-embedded-subscribe.handlers.types.ts +++ b/src/agents/pi-embedded-subscribe.handlers.types.ts @@ -132,7 +132,7 @@ export type EmbeddedPiSubscribeContext = { */ export type ToolHandlerParams = Pick< SubscribeEmbeddedPiSessionParams, - "runId" | "onBlockReplyFlush" | "onAgentEvent" | "onToolResult" + "runId" | "onBlockReplyFlush" | "onAgentEvent" | "onToolResult" | "sessionKey" >; export type ToolHandlerState = Pick< diff --git a/src/plugins/wired-hooks-after-tool-call.e2e.test.ts b/src/plugins/wired-hooks-after-tool-call.e2e.test.ts index 8ec506a5d33..5471e2d0dff 100644 --- a/src/plugins/wired-hooks-after-tool-call.e2e.test.ts +++ b/src/plugins/wired-hooks-after-tool-call.e2e.test.ts @@ -114,7 +114,9 @@ describe("after_tool_call hook wiring", () => { const event = firstCall?.[0] as | { toolName?: string; params?: unknown; error?: unknown; durationMs?: unknown } | undefined; - const context = firstCall?.[1] as { toolName?: string } | undefined; + const context = firstCall?.[1] as + | { toolName?: string; agentId?: string; sessionKey?: string } + | undefined; expect(event).toBeDefined(); expect(context).toBeDefined(); if (!event || !context) { @@ -125,6 +127,7 @@ describe("after_tool_call hook wiring", () => { expect(event.error).toBeUndefined(); expect(typeof event.durationMs).toBe("number"); expect(context.toolName).toBe("read"); + expect(context.sessionKey).toBe("test-session"); }); it("includes error in after_tool_call event on tool failure", async () => {