From 57a92598b175826086baae58982640992f4a0869 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Nov 2026 19:41:25 -0500 Subject: [PATCH 1/2] fix: auto-continue when a turn ends on reasoning or a tool message --- config.yaml | 2 +- openspec/specs/tui-streaming/spec.md | 25 +++++++++++--- src/stream/consume.js | 4 +-- src/tui/conversationArea.js | 26 +++++++++----- tests/unit/tui/conversationArea.test.js | 45 +++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 16 deletions(-) diff --git a/config.yaml b/config.yaml index d1ea211b..6ba0f463 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ agent: - autoContinueLimit: 1000 # max silent "Continue" nudges when a turn ends on reasoning + autoContinueLimit: 1000 # max silent "Continue" nudges when a turn ends on reasoning or a tool message bundledSkills: true # false = only 'reflection' loaded from .skills/ decision: baseUrl: "" # empty = tool not registered diff --git a/openspec/specs/tui-streaming/spec.md b/openspec/specs/tui-streaming/spec.md index 0f9fdf97..92d43353 100644 --- a/openspec/specs/tui-streaming/spec.md +++ b/openspec/specs/tui-streaming/spec.md @@ -233,23 +233,38 @@ The message list SHALL expose a method returning the ID of the last message in t - **WHEN** the message list is empty - **THEN** the method returns `undefined` -### Requirement: TUI SHALL auto-continue only when the last bubble ends on reasoning +### Requirement: TUI SHALL auto-continue only when the last bubble ends on reasoning or a tool message -The TUI SHALL dispatch a silent "Continue" user message only when the streaming assistant bubble is the last in the message list AND it ended with reasoning as its last event AND the auto-continue loop guard is not exhausted. +The TUI SHALL dispatch a silent "Continue" user message only when the streaming assistant bubble is the last in the message list AND its last event was reasoning or a tool message (`reasoning`, `tool`, or `on_tool_end`) AND the turn was not interrupted AND the auto-continue loop guard is not exhausted. #### Scenario: Last bubble ends on reasoning - **WHEN** the streaming bubble is the last in the list and its last event type is `reasoning` and the loop guard is not exhausted - **THEN** the TUI dispatches a silent "Continue" user message +#### Scenario: Last bubble ends on a tool message + +- **WHEN** the streaming bubble is the last in the list and its last event type is `tool` or `on_tool_end` and the loop guard is not exhausted +- **THEN** the TUI dispatches a silent "Continue" user message + #### Scenario: Earlier incomplete bubble does not trigger -- **WHEN** an earlier bubble is still streaming while a later bubble ends on reasoning +- **WHEN** an earlier bubble is still streaming while a later bubble ends on reasoning or a tool message - **THEN** the TUI does not dispatch a "Continue" for the earlier bubble -#### Scenario: Reasoning followed by a tool call does not trigger +#### Scenario: Interrupted turn does not trigger + +- **WHEN** the turn paused on an interrupt (e.g. `clarify`) +- **THEN** the TUI does not dispatch a "Continue" + +#### Scenario: Tool start does not trigger + +- **WHEN** the last event type is `on_tool_start` +- **THEN** the TUI does not dispatch a "Continue" + +#### Scenario: Turn ends on a message does not trigger -- **WHEN** the last event type is a tool event rather than `reasoning` +- **WHEN** the last event type is `message` - **THEN** the TUI does not dispatch a "Continue" #### Scenario: Loop guard exhausted diff --git a/src/stream/consume.js b/src/stream/consume.js index 1de4f12a..c76241e6 100644 --- a/src/stream/consume.js +++ b/src/stream/consume.js @@ -6,8 +6,8 @@ import { isSubagentNamespace } from "./namespace.js"; * collecting the orchestrator's own text and reasoning. * * Tracks the last non-subagent event type forwarded to the callback so the - * TUI can detect a turn that ended on reasoning (the model produced no - * message content) and nudge it with a silent "Continue". + * TUI can detect a turn that ended on reasoning or a tool message (the model + * produced no final message content) and nudge it with a silent "Continue". * * @param {AsyncIterable} stream - The agent stream (subgraphs mode) * @param {Function} [streamingCallback] - Callback for streamed chunks diff --git a/src/tui/conversationArea.js b/src/tui/conversationArea.js index 51588168..26c6ad5c 100644 --- a/src/tui/conversationArea.js +++ b/src/tui/conversationArea.js @@ -40,12 +40,17 @@ export async function countToolMessageTokens(model, text) { return 0; } +/** Last-event types that mean a turn ended without a final assistant message. */ +const AUTO_CONTINUE_EVENT_TYPES = new Set(["reasoning", "tool", "on_tool_end"]); + /** * Decide whether to dispatch a silent "Continue" after a turn ends. * - * The reasoning-only auto-continue fires only when the streaming bubble is the - * last in the message list AND it ended on reasoning AND the loop guard is not - * exhausted. Earlier incomplete bubbles must never trigger it. + * The auto-continue fires only when the streaming bubble is the last in the + * message list AND the turn ended on reasoning or a tool message (the model + * never produced a final response) AND the turn was not paused by an interrupt + * AND the loop guard is not exhausted. Earlier incomplete bubbles must never + * trigger it. * * @param {Object} params - Decision inputs * @param {string | undefined} params.lastEventType - The last non-subagent event type @@ -53,17 +58,20 @@ export async function countToolMessageTokens(model, text) { * @param {string | undefined} params.lastMessageId - The last message ID in the list * @param {number} params.autoContinueCount - Current auto-continue count * @param {number} params.autoContinueLimit - Max auto-continue count + * @param {boolean} [params.interrupted] - True when the turn paused on an interrupt * @returns {boolean} True when a silent "Continue" should be dispatched */ export function shouldAutoContinue({ lastEventType, + interrupted = false, streamingMsgId, lastMessageId, autoContinueCount, autoContinueLimit, }) { return ( - lastEventType === "reasoning" && + !interrupted && + AUTO_CONTINUE_EVENT_TYPES.has(lastEventType) && streamingMsgId === lastMessageId && autoContinueCount < autoContinueLimit ); @@ -493,15 +501,17 @@ const ConversationArea = forwardRef(function ConversationArea( completedToolCalls, ); - // Reasoning-only auto-continue: when the last bubble ends on reasoning - // (the model produced no message content), nudge it with a silent - // "Continue" so it produces an actual response. Scoped to the LAST - // bubble only — earlier incomplete bubbles must not trigger it. Bounded + // Auto-continue: when the last bubble ends on reasoning or a tool + // message (the model produced no final message content), nudge it with + // a silent "Continue" so it produces an actual response. Skipped for + // interrupted turns. Scoped to the LAST bubble only — earlier + // incomplete bubbles must not trigger it. Bounded // by config.agent.autoContinueLimit to prevent an infinite loop. const autoContinueLimit = config?.agent?.autoContinueLimit ?? 1000; if ( shouldAutoContinue({ lastEventType: response?.lastEventType, + interrupted: response?.interrupted, streamingMsgId: streamingMsgIdRef.current, lastMessageId: messageListRef.current?.getLastMessageId(), autoContinueCount: autoContinueCountRef.current, diff --git a/tests/unit/tui/conversationArea.test.js b/tests/unit/tui/conversationArea.test.js index 465c7118..6df2c84e 100644 --- a/tests/unit/tui/conversationArea.test.js +++ b/tests/unit/tui/conversationArea.test.js @@ -98,6 +98,51 @@ describe("shouldAutoContinue", () => { ); }); + it("returns true when the turn ends on a tool message", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + for (const lastEventType of ["tool", "on_tool_end"]) { + assert.strictEqual( + mod.shouldAutoContinue({ + lastEventType, + streamingMsgId: "3", + lastMessageId: "3", + autoContinueCount: 0, + autoContinueLimit: 1000, + }), + true, + ); + } + }); + + it("returns false when the turn ends on a tool start", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + assert.strictEqual( + mod.shouldAutoContinue({ + lastEventType: "on_tool_start", + streamingMsgId: "3", + lastMessageId: "3", + autoContinueCount: 0, + autoContinueLimit: 1000, + }), + false, + ); + }); + + it("returns false when the turn was interrupted", async () => { + const mod = await import("../../../src/tui/conversationArea.js"); + assert.strictEqual( + mod.shouldAutoContinue({ + lastEventType: "tool", + interrupted: true, + streamingMsgId: "3", + lastMessageId: "3", + autoContinueCount: 0, + autoContinueLimit: 1000, + }), + false, + ); + }); + it("returns false when lastEventType is undefined", async () => { const mod = await import("../../../src/tui/conversationArea.js"); assert.strictEqual( From 90200b55626cc6349aebebeec11eaad78ba83f46 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 17 Nov 2026 19:59:42 -0500 Subject: [PATCH 2/2] refactor: order shouldAutoContinue params to match its JSDoc --- src/tui/conversationArea.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tui/conversationArea.js b/src/tui/conversationArea.js index 26c6ad5c..6c7dd51a 100644 --- a/src/tui/conversationArea.js +++ b/src/tui/conversationArea.js @@ -63,11 +63,11 @@ const AUTO_CONTINUE_EVENT_TYPES = new Set(["reasoning", "tool", "on_tool_end"]); */ export function shouldAutoContinue({ lastEventType, - interrupted = false, streamingMsgId, lastMessageId, autoContinueCount, autoContinueLimit, + interrupted = false, }) { return ( !interrupted && @@ -511,11 +511,11 @@ const ConversationArea = forwardRef(function ConversationArea( if ( shouldAutoContinue({ lastEventType: response?.lastEventType, - interrupted: response?.interrupted, streamingMsgId: streamingMsgIdRef.current, lastMessageId: messageListRef.current?.getLastMessageId(), autoContinueCount: autoContinueCountRef.current, autoContinueLimit, + interrupted: response?.interrupted, }) ) { autoContinueCountRef.current += 1;