Skip to content

fix(app): resolve message identity by id when creation time shifts - #41429

Open
leepokai wants to merge 1 commit into
anomalyco:devfrom
leepokai:message-identity
Open

fix(app): resolve message identity by id when creation time shifts#41429
leepokai wants to merge 1 commit into
anomalyco:devfrom
leepokai:message-identity

Conversation

@leepokai

@leepokai leepokai commented Aug 9, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #41428

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

#41001 (9113255) started ordering stored messages by creation time and switched message lookups to an exact match on time.created + id. But the same message can legitimately appear with two different time.created values: the optimistic user message is stamped with the client's Date.now() while the server persists the same message id with its own clock. Any skew makes the keys differ, so the message.updated handler misses the existing row and splices the server copy in as a second row with the same id. The stale row sticks around until a full refetch: the fork dialog lists the prompt twice, message.removed deletes only the first match and leaves a ghost row, and mergeOptimisticPage never runs its confirmed/observed bookkeeping for the time-shifted item.

The fix keeps the creation-time ordering from #41001 but treats the id as the message's identity when the exact key misses: the message.updated handlers (server-session.ts, global-sync event-reducer.ts) drop any same-id row before inserting the update at its sorted position, and both mergeOptimisticPage implementations count a same-id row as found so no duplicate is inserted and confirmation fires. Per-session arrays are small, so the extra linear scan only runs on the rare key miss.

How did you verify your code works?

From packages/app:

  • Added six tests covering the streaming update (no refresh), the optimistic submit/confirm flow, message.removed after a time-shifted update, refresh-time optimistic confirmation, and the same-id case in both mergeOptimisticPages. Without the fix they fail (bun test --conditions=solid --preload ./happydom.ts src/context/server-session.test.ts src/context/global-sync/event-reducer.test.ts src/context/sync-optimistic.test.ts → 96 pass, 6 fail); with it all pass (102 pass, 0 fail).
  • bun test --conditions=solid --preload ./happydom.ts src/context → 316 pass, 0 fail.
  • bun run test:unit → 724 pass, 0 fail.
  • bun run typecheck → clean.
  • test:browser / Playwright e2e were not run locally; this is a state-store change covered by the unit tests above.

Screenshots / recordings

N/A — state-store fix, unit-tested.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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.

Submitted prompt is stored twice in the app message store (fork dialog duplicates, undo leaves a ghost)

1 participant