From 66cb72b8313b478c4a3dc0f215258962aa4e92e6 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Mon, 18 May 2026 13:56:11 +0900 Subject: [PATCH] fix(prompt-gate): bind session messages receiver --- src/hooks/shared/prompt-async-gate.test.ts | 44 ++++++++++++++++++++++ src/shared/prompt-async-gate.ts | 5 ++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/hooks/shared/prompt-async-gate.test.ts b/src/hooks/shared/prompt-async-gate.test.ts index c5055edd9..c45518534 100644 --- a/src/hooks/shared/prompt-async-gate.test.ts +++ b/src/hooks/shared/prompt-async-gate.test.ts @@ -242,6 +242,50 @@ describe("dispatchInternalPrompt shared gate behavior", () => { }) }) + test("#given SDK messages depends on its session receiver #when latest assistant waits on tools #then method binding is preserved and no prompt is sent", async () => { + // given + let promptCalls = 0 + const session = { + _client: { + messages: [ + { + info: { id: "msg_user", role: "user" }, + parts: [{ type: "text", text: "run work" }], + }, + { + info: { id: "msg_assistant", role: "assistant", finish: "tool-calls" }, + parts: [{ type: "tool_use", id: "toolu_pending", state: { status: "running" } }], + }, + ], + }, + async messages( + this: { _client: { messages: unknown[] } }, + _input: { path: { id: string }; query: { directory: string } }, + ) { + return { data: this._client.messages } + }, + async promptAsync() { + promptCalls += 1 + }, + } + const client = { session } + + // when + const result = await dispatchInternalPrompt({ + mode: "async", + client, + sessionID: "ses_bound_messages", + input: { path: { id: "ses_bound_messages" }, body: { parts: [] } }, + source: "test:bound-messages", + settleMs: 0, + postDispatchHoldMs: 0, + }) + + // then + expect(result.status).toBe("active") + expect(promptCalls).toBe(0) + }) + test("#given session.status reports busy #when an internal promptAsync is requested #then no prompt is sent", async () => { // given let promptCalls = 0 diff --git a/src/shared/prompt-async-gate.ts b/src/shared/prompt-async-gate.ts index 25b9c088c..db317e8ce 100644 --- a/src/shared/prompt-async-gate.ts +++ b/src/shared/prompt-async-gate.ts @@ -238,10 +238,11 @@ async function sessionLatestAssistantIsWaitingOnTools(args: { source: string timeoutMs: number }): Promise { - const messages = args.client.session?.messages - if (typeof messages !== "function") { + const session = args.client.session + if (typeof session?.messages !== "function") { return false } + const messages = session.messages.bind(session) try { const response = await withDispatchTimeout(