Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions packages/cli/src/ui/hooks/useGeminiStream.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4915,6 +4915,77 @@ describe('useGeminiStream', () => {
expect(result.current.streamingState).toBe(StreamingState.Idle);
});

it('should drop queued tool calls when user cancels the turn', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Test only validates guard 1 (toolCallRequests.length = 0 on UserCancelled), not guard 2 (!signal.aborted).

When the stream yields ToolCallRequest → UserCancelled, the first guard empties the array. By the time execution reaches if (toolCallRequests.length > 0 && !signal.aborted), the length check is already false — !signal.aborted is never evaluated. This test would pass even if guard 2 were removed.

Guard 2 covers the scenario where the client-side abort fires but the stream drains without yielding UserCancelled (e.g., async iterator buffer race). Consider adding a complementary test:

it('should not schedule tool calls when signal is aborted without UserCancelled', async () => {
  mockSendMessageStream.mockReturnValue(
    (async function* () {
      yield {
        type: ServerGeminiEventType.ToolCallRequest,
        value: { callId: 'call_1', name: 'write_file', args: { path: 'a.txt' } },
      };
      // Stream ends normally — no UserCancelled event
    })(),
  );

  const { result } = renderTestHook();

  // Simulate client-side abort (e.g., user presses Escape in UI)
  // The abort signal should prevent scheduleToolCalls even though
  // no UserCancelled event was yielded by the stream.

  await act(async () => {
    await result.current.submitQuery('abort without cancel event');
  });

  expect(mockScheduleToolCalls).not.toHaveBeenCalled();
});

— qwen3.7-max via Qwen Code /review

mockSendMessageStream.mockReturnValue(
(async function* () {
yield {
type: ServerGeminiEventType.ToolCallRequest,
value: {
callId: 'call_cancelled',
name: 'write_file',
args: { path: 'cancelled.txt' },
},
};
yield { type: ServerGeminiEventType.UserCancelled };
})(),
);

const { result } = renderTestHook();

await act(async () => {
await result.current.submitQuery('cancel before tool dispatch');
});

expect(mockScheduleToolCalls).not.toHaveBeenCalled();
});

it('should not dispatch queued tool calls after the request is aborted', async () => {
let resolveStream!: () => void;
let toolCallQueued!: () => void;

const streamCanFinish = new Promise<void>((resolve) => {
resolveStream = resolve;
});
const toolCallWasQueued = new Promise<void>((resolve) => {
toolCallQueued = resolve;
});

mockSendMessageStream.mockReturnValue(
(async function* () {
yield {
type: ServerGeminiEventType.ToolCallRequest,
value: {
callId: 'call_aborted',
name: 'write_file',
args: { path: 'aborted.txt' },
},
};
toolCallQueued();
await streamCanFinish;
})(),
);

const { result } = renderTestHook();

let submitPromise!: Promise<void>;
await act(async () => {
submitPromise = result.current.submitQuery(
'abort before tool dispatch',
);
});

await toolCallWasQueued;

act(() => {
result.current.cancelOngoingRequest();
});

resolveStream();
await submitPromise;

expect(mockScheduleToolCalls).not.toHaveBeenCalled();
});

it('should reset thought to null when there is an error', async () => {
// Mock a stream that yields a thought then encounters an error
mockSendMessageStream.mockReturnValue(
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/ui/hooks/useGeminiStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,7 @@ export const useGeminiStream = (
break;
case ServerGeminiEventType.UserCancelled:
flushBufferedStreamEvents();
toolCallRequests.length = 0;
handleUserCancelledEvent(userMessageTimestamp);
break;
case ServerGeminiEventType.Error:
Expand Down Expand Up @@ -1663,7 +1664,7 @@ export const useGeminiStream = (
flushBufferedStreamEventsRef.current.delete(flushBufferedStreamEvents);
}
dualOutput?.finalizeAssistantMessage();
if (toolCallRequests.length > 0) {
if (toolCallRequests.length > 0 && !signal.aborted) {
scheduleToolCalls(toolCallRequests, signal);
}
return StreamProcessingStatus.Completed;
Expand Down
Loading