Files
coder/coderd
Thomas Kosiewski e3446c9c21 fix(coderd/x/chatd): drop late preview parts after a chat error (#30279)
When a generation attempt fails without a retry (`FinishError`), the chat moves to `error` but keeps its `history_version` and `generation_attempt`, and `retry_state` stays null. The stream loop therefore did not retire the attempt, and a part of the failed attempt that the relay delivered after the `error` event was still forwarded. The frontend clears its preview on `error` (`useChatStore.ts`), so the user saw a mid-message fragment appear under the error.

This is the error-path variant of the issue fixed in #30241 for retries. The stream loop now also retires the current attempt when a sync observes `status = error`, so its late parts are dropped. Every generation attempt increments `generation_attempt` before it creates its part episode (`beginGenerationAttempt`), so the next run after an error starts a fresh preview as before. Other statuses that leave `running` (`interrupting`, `requires_action`, `waiting`) are unchanged: the frontend does not clear the preview for them. The frontend contract is unchanged.

`TestStreamLoopLatePartAfterError` fails on main and passes with the fix; its subtests also cover the next attempt after an error and an interrupt that keeps streaming.

A separate docs commit updates step 7 of the sync algorithm in `ARCHITECTURE.md` and corrects the `preview_reset` description, which said it is emitted when the LLM call fails mid-way. The code emits it only when the history version changes or a new generation attempt starts.

The bug was found by a TLA+ model of the chat stream and confirmed with a Go reproduction.

Refs #30241

---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_
2026-10-02 13:02:41 +02:00
..
2026-10-01 11:49:21 -04:00