Maintain coherent sessions under cancellation and changing screens.
1. Conversation ownership
Choose where history is stored and who can read it. Bound the number of retained turns or summarize older context. Separate conversations by user and clear them on account change.
2. Stream accumulation
Append chunks to an active response associated with a request ID. Distinguish stream termination from a completed answer. Do not save an interrupted fragment as an approved final record.
3. Lifecycle behavior
Collect work in an appropriate coroutine scope and decide whether leaving the screen cancels it. Restore durable user inputs after recreation rather than retaining arbitrary network objects in saved state.
Worked scenario
A user switches accounts while an answer streams. The old response must stop and must not appear in the new account history.
Apply it
Add streaming display and test screen exit, rapid resubmission and account change with a fake chunk source.
Check your understanding
Late chunks cannot update the wrong conversation. Explain the decision and show evidence from your implementation or design. If you cannot demonstrate it yet, revisit the relevant section before continuing.