diff --git a/src/hooks/session-notification-input-needed.test.ts b/src/hooks/session-notification-input-needed.test.ts index 9a3f3db43..5e8552907 100644 --- a/src/hooks/session-notification-input-needed.test.ts +++ b/src/hooks/session-notification-input-needed.test.ts @@ -78,7 +78,7 @@ describe("session-notification input-needed events", () => { await hook({ event: { - type: "permission.ask", + type: "permission.asked", properties: { sessionID, }, diff --git a/src/plugin/tool-execute-before-session-notification.test.ts b/src/plugin/tool-execute-before-session-notification.test.ts new file mode 100644 index 000000000..390f1fa88 --- /dev/null +++ b/src/plugin/tool-execute-before-session-notification.test.ts @@ -0,0 +1,33 @@ +const { describe, expect, test, spyOn } = require("bun:test") + +const sessionState = require("../features/claude-code-session-state") +const { createToolExecuteBeforeHandler } = require("./tool-execute-before") + +describe("createToolExecuteBeforeHandler session notification sessionID", () => { + test("uses main session fallback when input sessionID is empty", async () => { + const mainSessionID = "ses_main" + const getMainSessionIDSpy = spyOn(sessionState, "getMainSessionID").mockReturnValue(mainSessionID) + + let capturedSessionID: string | undefined + const hooks = { + sessionNotification: async (input) => { + capturedSessionID = input.event.properties?.sessionID + }, + } + + const handler = createToolExecuteBeforeHandler({ + ctx: { client: { session: { messages: async () => ({ data: [] }) } } }, + hooks, + }) + + await handler( + { tool: "question", sessionID: "", callID: "call_q" }, + { args: { questions: [{ question: "Continue?", options: [{ label: "Yes" }] }] } }, + ) + + expect(getMainSessionIDSpy).toHaveBeenCalled() + expect(capturedSessionID).toBe(mainSessionID) + }) +}) + +export {} diff --git a/src/plugin/tool-execute-before.ts b/src/plugin/tool-execute-before.ts index e2742baaf..65ebaed63 100644 --- a/src/plugin/tool-execute-before.ts +++ b/src/plugin/tool-execute-before.ts @@ -37,11 +37,12 @@ export function createToolExecuteBeforeHandler(args: { || normalizedToolName === "ask_user_question" || normalizedToolName === "askuserquestion" ) { + const sessionID = input.sessionID || getMainSessionID() await hooks.sessionNotification?.({ event: { type: "tool.execute.before", properties: { - sessionID: input.sessionID, + sessionID, tool: input.tool, args: output.args, },