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>
This commit is contained in:
scoootscooob
2026-03-01 01:27:23 -08:00
parent eb35fb745d
commit b7117384fc
3 changed files with 6 additions and 3 deletions

View File

@@ -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)}`);

View File

@@ -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<

View File

@@ -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 () => {