Repository navigation
Count a chat cancel once, and only while a response streams - #274
jerelvelarde wants to merge 1 commit into
Conversation
cancelStream() only checked activePromptId, which keeps its value after a response completes or fails. So New Session after a finished response recorded a user cancel in telemetry. It also didn't check for a cancel already under way: pressing Stop twice recorded two cancels, and a renderer change right after Stop replaced the user's cancel reason, so the chat showed the renderer note instead of the Stop note. Return early unless a response is still streaming and no cancel is under way. The first cancel's reason wins. activePromptId stays set after a response finishes: submitPrompt's catch block drops errors from a prompt that is no longer active, and validation errors are thrown after the stream finishes, so clearing it there would hide them.
There was a problem hiding this comment.
Code Review
This pull request refactors stream cancellation logic in ChatCoordinator to prevent redundant cancellations and handle cases where no response is streaming, accompanied by comprehensive unit tests. The review feedback highlights a critical issue in the new tests where the non-standard Vitest matcher toHaveBeenCalledExactlyOnceWith is used, which will cause runtime TypeErrors; it is recommended to split this into separate call count and argument assertions. Additionally, the custom createDeferred helper could be simplified using native Promise.withResolvers() if the environment supports it.
| expect(trackChatCancel).toHaveBeenCalledExactlyOnceWith({ | ||
| promptId: 'prompt-1', | ||
| turnIndex: 1, | ||
| pipelineStatus: PipelineStatus.RECEIVING_STREAM, | ||
| }); |
There was a problem hiding this comment.
The matcher toHaveBeenCalledExactlyOnceWith is not a standard Vitest or Jest matcher and will cause a runtime TypeError during test execution. In Vitest, you should assert the call count and the arguments separately using toHaveBeenCalledTimes(1) and toHaveBeenCalledWith(...) to match the established testing patterns in this codebase.
expect(trackChatCancel).toHaveBeenCalledTimes(1);
expect(trackChatCancel).toHaveBeenCalledWith({
promptId: 'prompt-1',
turnIndex: 1,
pipelineStatus: PipelineStatus.RECEIVING_STREAM,
});| expect(trackChatCancel).toHaveBeenCalledExactlyOnceWith({ | ||
| promptId: 'prompt-1', | ||
| turnIndex: 1, | ||
| pipelineStatus: PipelineStatus.RECEIVING_STREAM, | ||
| }); |
There was a problem hiding this comment.
The matcher toHaveBeenCalledExactlyOnceWith is not a standard Vitest or Jest matcher and will cause a runtime TypeError during test execution. In Vitest, you should assert the call count and the arguments separately using toHaveBeenCalledTimes(1) and toHaveBeenCalledWith(...) to match the established testing patterns in this codebase.
expect(trackChatCancel).toHaveBeenCalledTimes(1);
expect(trackChatCancel).toHaveBeenCalledWith({
promptId: 'prompt-1',
turnIndex: 1,
pipelineStatus: PipelineStatus.RECEIVING_STREAM,
});| function createDeferred<T>() { | ||
| let resolve!: (value: T) => void; | ||
| let reject!: (reason: unknown) => void; | ||
| const promise = new Promise<T>((onResolve, onReject) => { | ||
| resolve = onResolve; | ||
| reject = onReject; | ||
| }); | ||
| return {promise, resolve, reject}; | ||
| } |
There was a problem hiding this comment.
The problem
#269 sends every cancel through
ChatCoordinator.cancelStream(), which decides whether there isanything to cancel by checking
activePromptId. That ID keeps its value after a response finishesor fails. So New Session after a finished response records a user cancel (
trackChatCancel) for aresponse that had already ended. Gemini Code Assist pointed this out on #269.
Two more cases share the cause. Pressing Stop twice while a response is still connecting records two
cancels. And a renderer change right after Stop replaces the user's reason, so the chat says "This
response was stopped because the renderer changed" instead of "You stopped this response." The
telemetry only reaches analytics in builds that turn usage tracking on, but the wrong chat note shows
for everyone.
The approach
cancelStreamnow returns early unless a response is streaming and no cancel is already under way."Streaming" is
isProgrammaticStreamActive(), whichsubmitPromptsets and which is cleared oncethe response is written to the canvas, fails, or stops after a cancel. "Under way" is a non-null
cancelReason: a cancel was requested, but the response hasn't failed with the cancel error yet. Thefirst cancel's reason wins.
activePromptIdstays in the check because it narrows the type fortrackChatCancel. It is notcleared when the response finishes, which would have been the other fix:
submitPrompt's catchblock drops errors from a prompt whose ID no longer matches, and validation errors are thrown after
the stream ends, so clearing the ID there hides them. Two existing tests fail if it is cleared. The
comment above the guard says what each condition is for and why.
What is not covered
ChatState's own JSDoc for the streaming signal still calls it a lock against typing deadlocks.Only the coordinator's JSDoc changed.
setIntervalor build promises by hand are unchanged.takes place within one microtask, so the recording runs the new tests against fix(chat): keep the canvas and conversation when switching renderers #269's guard and this
one.
Verification
Six new tests in
chat-coordinator.spec.ts. Each fails on main's guard (37744e6, #269) and passes here:They reuse
createStreamAwaitingCancel, which gained aholdCanceloption so a test can releasethe cancel error when it chooses. The full shell suite passes: 1,799 tests,
with coverage. ESLint and Prettier pass on the changed files.
Merge notes
#216 adds half of this guard (
|| this.cancelReason !== null) in its first Slack commit. Whicheverof the two lands second keeps this condition.