Skip to content

Supersedes #22051: fix(app): preserve messages sent during history load and replace reply placeholder by identity - #25157

Open
omiagent[bot] wants to merge 1 commit into
mainfrom
community/revive-22051
Open

omiagent[bot] wants to merge 1 commit into
mainfrom
community/revive-22051

Conversation

@omiagent

@omiagent omiagent Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #22051 (stalled since 2026-10-17 on a merge conflict; carried forward with the author's credit).

What this does, in plain terms

Two chat bugs stop losing or corrupting messages on mobile:

  1. A message sent while chat history is still loading used to disappear. Opening a chat starts a history load ("Reading your memories…"); if you typed or voice-sent a message before that load finished, the load's completion replaced the whole on-screen list and your message vanished from the view (it was still sent to the server, but you could not see it or the reply to it). The fix merges whatever you sent while the load was in flight back into the loaded history, and skips that merge when you deliberately switched apps.
  2. A finished reply could overwrite the wrong row. The streaming reply machinery remembered its row by position ("index 7") before streaming; if rows shifted while the reply streamed, the final text landed on someone else's row. The fix pins the placeholder by object identity, so the reply always replaces its own placeholder — the same identity-based pattern the codebase already uses for reply-stop cleanup.

No product behavior changes beyond the bug fixes: dedupe of already-delivered user messages keeps the list honest, and app switching keeps its current full-refresh behavior. This closes #19232.

Credit

All of the code in this PR is @ocakciomursel-pixel's — cherry-picked with the original git authorship intact (committer is only the carrying agent). The original PR (#22051, fix/chat-sent-while-loading-and-placeholder-identity) contains the identical authorial change; Member review on it (2026-10-17) verified both halves as "a well-scoped, correct fix for two real message-loss bugs" with only non-blocking notes.

What changed and why (carried from #22051, by @ocakciomursel-pixel)

  • Merge locally added / in-flight messages on history load (app/lib/providers/chat_history_state.dart): refreshMessages now records loadStarted and merges any messages added after the load began via _withSentWhileLoading (when not switching apps via dropdownSelected), deduplicating any human message the server already returned with the same text. Previously, a message added via addMessageLocally (or a voice turn in sendVoiceMessageStreamToServer) before a pending history load completed was overwritten when messages was replaced wholesale.
  • Replace and insert streaming reply placeholders by object identity (app/lib/providers/message_provider.dart): Both sendVoiceMessageStreamToServer and _streamReply previously captured final aiIndex = messages.length - 1 before streaming and wrote messages[aiIndex] = message on MessageChunkType.done (and voiceMessages.insert(aiIndex, ...) on MessageChunkType.message). If messages was merged or reordered while the stream was in flight, the final reply overwrote the wrong row. Added _replaceMessage and _insertBefore helpers using identical(m, placeholder).

Carry-forward resolution

The original diff applied cleanly to chat_history_state.dart (untouched on main since 2026-10-17) and to the test file. message_provider.dart conflicted because main gained the stop/replace-reply feature (095b2864e5), thinking-buffer flushing and voice-playback machinery inside the same functions since the original PR. Resolution kept main's current declarations in full and layered the author's change on top: the two aiIndex declarations and both index-based writes become the author's identity-based _replaceMessage/_insertBefore; main's current question capture, stop-path and playback code are untouched. The carried diff is otherwise byte-identical in intent to #22051 (+101/−12 across the same 3 files).

Product invariants affected

none

How it was verified (this branch, current main @ ac02feb)

  • Red-proof equivalent: git grep on plain origin/main confirms the unguarded pattern this PR fixes still exists on main (voiceMessages[aiIndex] = message, messages[aiIndex] = message, voiceMessages.insert(aiIndex, ...)) — the bug is not yet fixed on main, and linked issue Chat: a message sent while the history is still loading disappears, and the reply overwrites a row #19232 is open.
  • Regression tests: flutter test test/providers/message_provider_chat_reliability_test.dart under Flutter stable 3.44.5 (the CI pin) on macOS/arm64 — 30/30 passed, including the author's two new tests (a local message added while history is loading is preserved without duplication when the load completes, a streamed reply replaces its placeholder by identity even when messages are reordered mid-stream), in all three timezone runs: plain, TZ=Pacific/Kiritimati, TZ=Pacific/Pago_Pago.
  • Codegen: dart run build_runner build --delete-conflicting-outputs clean (7 outputs).
  • Analyzer ratchet: bash scripts/analyze_ratchet.sh — passed, zero regressions (no rule above baseline; several improved: avoid_print 13/19, unnecessary_import 11/12, etc.).
  • pr-preflight (manifest gate, real run — not --suggest): passed — speaker-identity-interpolation-ratchet, diff-hygiene, product-invariants, new-file-headers, mobile-changelog-entry (added unreleased fragment app/changelog/unreleased/20261112-chat-sent-while-loading-and-reply-identity.json, this is a user-facing fix), dead-code-ratchet, check-manifest-contract, plan-catalog-contract, and the rest.

Tests

  • app/test/providers/message_provider_chat_reliability_test.dart

UI evidence

UI-Evidence: none -- state-management / provider-only fix with unit test coverage; no widget layout or visual styling changes.

Failure class (fixes)

Failure-Class: none

View guided diff Turn on auto-fix

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The two fixes here are correct and well-targeted; one mechanical blocker keeps CI red: the new test file fails the repo formatter.

  • app/lib/providers/chat_history_state.dart — _withSentWhileLoading merges locally-added/streamed messages (createdAt >= loadStarted) back into the loaded page, dedupes by id and by human-text match, and the dropdownSelected guard preserves the intentional full refresh on app switch. Legacy-cache writes now use filtered before merge-back, so ephemeral local messages don't pollute the disk cache. Correct.
  • app/lib/providers/message_provider.dart — _replaceMessage/_insertBefore pin the streamed placeholder by object identity instead of the captured aiIndex, in both the voice and text streaming paths. That's the right fix for the wrong-row overwrite when rows shift mid-stream; the no-op fallback when the placeholder is no longer in the list is safe.
  • app/test/providers/message_provider_chat_reliability_test.dart — the two new tests are exactly the coverage I'd want (message sent during load preserved without duplication; identity replacement under mid-stream reorder), and the Dart test lane is green — but this file is unformatted: the Formatting job logs Changed app/test/providers/message_provider_chat_reliability_test.dart (dart format, line length 120).
  • app/changelog/unreleased/20261112-chat-sent-while-loading-and-reply-identity.json — accurate user-facing fragment, matches repo convention.

Non-blocking nit: the text-based human dedupe in _withSentWhileLoading can drop a locally-sent message if the same text already appears in the loaded page (e.g. "ok" sent twice in quick succession) — acceptable heuristic, a brief comment would help.

USER IMPACT: messages sent while chat history is still loading no longer vanish from the view, and finished replies land in their own row instead of overwriting a neighboring message.

Action needed: run dart format --line-length 120 on app/test/providers/message_provider_chat_reliability_test.dart (only that file) and push — everything else here looks ready.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added the flutter flutter work label Nov 12, 2026
@ocakciomursel-pixel

Copy link
Copy Markdown
Contributor

Thanks for carrying #22051 forward and for the review @Git-on-my-level!

Since BasedHardware:community/revive-22051 is an upstream branch (not directly writable from my fork), I ran dart format --line-length 120 on app/test/providers/message_provider_chat_reliability_test.dart (only that file) directly on top of 48ca142b9ead2c7ac34844122c00b5ff8fd17dc9 and pushed it to ocakciomursel-pixel:community/revive-22051 (672e82f618) so it can be fast-forwarded or cherry-picked directly:

--- a/app/test/providers/message_provider_chat_reliability_test.dart
+++ b/app/test/providers/message_provider_chat_reliability_test.dart
@@ -97,7 +97,8 @@ void main() {
     expect(provider.messages.any((m) => m.id == 'late-history'), isFalse);
   });
 
-  test('a local message added while history is loading is preserved without duplication when the load completes', () async {
+  test('a local message added while history is loading is preserved without duplication when the load completes',
+      () async {
     final pending = Completer<List<ServerMessage>>();
     final greeting = ServerMessage(
       'greeting',

Verified dart format --output=none --set-exit-if-changed --line-length 120 exits 0 across all touched Dart files.

@Git-on-my-level

Copy link
Copy Markdown
Member

Verified — your fix commit is correct; it just needs to land on the PR branch.

The formatting blocker from my earlier review is still present on this head (48ca142): the repo's Formatting check failed on the original push (run 43288201495, "Check Dart formatting" step), flagging only app/test/providers/message_provider_chat_reliability_test.dart — the new test's signature line is 126 chars, over the 120 limit. The check currently shows "skipped" only because a later label-event run skipped path detection; the original failure stands.

I applied 672e82f618 locally on top of 48ca142 and re-ran dart format --line-length 120 --set-exit-if-changed across all three touched Dart files: everything is clean (exit 0). The commit is a pure 2-line wrap based directly on this head, so it can be fast-forwarded or cherry-picked as-is.

Since BasedHardware:community/revive-22051 is an upstream branch, you can't push the fix here yourself — someone with write access needs to update the PR branch with 672e82f618. Once that lands, CI re-runs formatting and the review side is clear: both production fixes (_withSentWhileLoading merge-back in app/lib/providers/chat_history_state.dart, identity-based _replaceMessage/_insertBefore in app/lib/providers/message_provider.dart) were verified in my earlier review.

USER IMPACT: once merged, messages sent while chat history is loading no longer disappear, and finished replies replace their own placeholder instead of overwriting a neighboring row.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat: a message sent while the history is still loading disappears, and the reply overwrites a row

2 participants