Skip to content

Keep the reply text that had streamed when a build is cut off - #192

Closed
reynaldichernando wants to merge 2 commits into
mainfrom
claude/funny-mayer-f44e5t
Closed

reynaldichernando wants to merge 2 commits into
mainfrom
claude/funny-mayer-f44e5t

Conversation

@reynaldichernando

@reynaldichernando reynaldichernando commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Problem

The AI's text was saved to the chat history only around tool calls and at the end of a turn. When a reply was cut off partway, the text that had streamed stayed on screen but was never saved. It disappeared after a reload, and Resume or "continue" didn't send it to the AI, so the AI often wrote it again and it appeared twice.

A reply can be cut off in these ways, and each one lost the text.

  • The user clicks Stop or switches chats.
  • An error ends the turn, such as running out of credits partway through a reply.
  • Network errors use up the retries.
  • The stall watchdog's abort ends the stream without an error, so no retry runs.
  • The tab is closed or reloaded, or the phone kills it.

Fix

  • handleMessageStream.js. keepCutOffReply(context) saves the text that has streamed so far (saveCurrentMessage). The stream's finally calls it when the stream was aborted. It skips two cases.
    • Stall watchdog abort (_stallRecovery). The automatic retry removes the unsaved bubble and writes the reply again.
    • A newer turn has started in the same chat (Stop with queued messages). The late text would land after that turn's messages.
  • sendChatMessage in app.js.
    • The error-card catch calls it first, so the text sits ahead of the error card.
    • Out of retries, the retry loop keeps the text instead of removing the bubble.
    • The stall backstop (an abort that ended without an error) calls it once the stall flag is cleared.
    • The turn's turnSeq is passed on the stream context for the newer-turn check.
  • Reply checkpoints in app.js. While text streams, the turn is saved 3 seconds after its first unsaved chunk, right away when the page is hidden, and on pagehide. saveCurrentChat writes the uncommitted text after the history (historyWithPendingReply), so the chat reopens as interrupted with the text, and Resume continues after it. This adds at most one chat file write every 3 seconds, only while a reply is streaming. Text that was committed, or removed for a retry, before the timer fires is not saved again.

A closed tab can still lose up to the last 3 seconds of text. The pagehide save is fire-and-forget and may not finish before the page is gone.

Tests

  • scripts/test-stopped-reply-kept.mjs runs the real handleMessageStream.js, plus the stall backstop, the error-card catch and the reply-checkpoint blocks from app.js. It covers Stop, the watchdog cases, credit errors (stream cut short and error line), the retry give-up, the checkpoint timer and saving on page hide. It also checks that nothing is saved twice or for a turn a newer one replaced. The new checks fail on the code before this change.
  • scripts/test-chat-list-entry.mjs passes the new helper into its slice of saveCurrentChatUnlocked.
  • Added to npm test. The full suite and vite build pass.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A moderate active issue remains because the watchdog’s non-throwing abort path can return without persisting or explicitly discarding the partial streamed reply.

1 open issue
What changed in this PR

This pull request preserves partially streamed assistant replies when a build is stopped, allowing the saved text to survive reloads and be included when the build resumes.

Changes:

  • Saves buffered assistant text during aborted stream cleanup.
  • Passes the turn sequence into stream context to prevent stale replies from being persisted.
  • Adds regression coverage for stopped, stalled, superseded, empty, and normally completed replies.
File Description
src/​js/​handleMessageStream.js Adds persistence for partial assistant replies during aborted stream cleanup with safeguards for stalled and superseded turns.
src/​js/​app.js Passes the active turn sequence into the stream context for stale-turn protection.
scripts/​test-stopped-reply-kept.mjs Adds regression tests covering stopped, stalled, superseded, empty, and normally completed replies.
package.json Registers the new stopped-reply regression test in the main test suite.

Review effort: Lite

Comment thread src/js/handleMessageStream.js Resolved
claude and others added 2 commits November 24, 2026 02:33
The AI's text was saved only around tool calls and at the end of a turn.
A Stop in the middle of a reply left the sentence in progress on screen
but out of the history: it vanished on reload, and Resume didn't send it
to the AI, which then wrote it again. Now the stream commits that text
when it is aborted. Not for the stall watchdog's abort, whose retry
rewrites the reply, and not once a newer turn has started in the chat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FWCxozAPzozxrNQPgyy2WP
The first commit kept the text on Stop. A reply can also be cut off in
other ways, and each one lost the text that had streamed.

- Stall watchdog, quiet end. Its abort can end the stream without an
  AbortError, so no retry runs and the backstop offers Resume. The
  backstop now keeps the text (review comment on #192).
- Errors that end the turn, such as a 402 insufficient_funds line
  mid-reply. The text is kept ahead of the error card.
- Network errors past the retries. The text is kept for Resume instead
  of being dropped for a retry that never comes.
- A tab closed, reloaded or killed mid-reply. While text streams, the
  turn is saved 3 s after its first unsaved chunk, at once when the page
  is hidden, and on pagehide. The save writes the text after the history
  (historyWithPendingReply), so the chat reopens interrupted with it.

keepStoppedReply is now keepCutOffReply, and its abort check moved to
the stream's finally.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@reynaldichernando
reynaldichernando force-pushed the claude/funny-mayer-f44e5t branch from 1894b49 to 11c5e23 Compare November 23, 2026 20:10
@reynaldichernando reynaldichernando changed the title Keep the reply text that had streamed when the user stops a build Keep the reply text that had streamed when a build is cut off Nov 23, 2026
@reynaldichernando
reynaldichernando requested a lite review from Copilot November 24, 2026 01:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

An active retry/watchdog race can discard the streamed reply when waiting is canceled, so the partial bubble must be preserved until another attempt is confirmed.

2 open issues

Review effort: Lite

Comment thread src/js/app.js
// nothing will, so keep it as a Stop does. Resume continues
// after it.
if (!stallRecovery && attempt >= MAX_TURN_RETRIES) keepCutOffReply(context);
else removeUncommittedBubble(context);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants