Skip to content

fix(chat): preserve created_at so stream replay can't reorder messages on reconnect - #3956

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/decopilot-stream-reorder-on-reconnect
Jun 16, 2026
Merged

pedrofrxncx merged 1 commit into
mainfrom
fix/decopilot-stream-reorder-on-reconnect

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jun 16, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up fix to #3951 (the diagnostics, now merged). The decopilot-stream-diag logs from #3951 confirmed the publish path is healthy (35/35 chunks delivered, 0 errors) — the bug is client-side ordering.

Symptom

Reload mid-run → turn-1's assistant response renders under turn-2's user message; turn-1's own slot shows "No response was generated". A second reload fixes it.

Cause

On reload mid-run the client reconnects, the thread is in_progress, so /stream uses deliverPolicy: "all" and replays the whole per-thread subject (an already-persisted run + the next one) as timestamp-less streamed UIMessages. upsertById then lets a replayed (timestamp-less) message overwrite its DB-seeded row, which carried the authoritative top-level created_at. The timestamp is stripped → readTimestamp returns +Infinity → the message sorts to the end → renders under the next turn's user message, original slot empty. A refresh fixes it only because, with no active run, the reconnect uses deliverPolicy: "new" and replays nothing.

Fix

thread-connection.ts — preserveCreatedAt: when an upsert/merge replacement lacks created_at but the prior row has one, keep it. A genuine persisted update (incoming has created_at) still overwrites. Applied in upsertById (stream commit path) and mergeAndSort (defensive). Exported upsertById; added 3 unit tests.

Testing

bun test thread-connection.test.ts — 45 pass (+3). bun run check clean on changed files; bun run fmt applied.

🤖 Generated with Claude Code


Summary by cubic

Preserves created_at on upsert/merge so replayed stream chunks don’t reorder messages after a reconnect. Fix prevents assistant replies from appearing under the next user turn and avoids the “No response was generated” placeholder.

  • Bug Fixes
    • Keep the prior row’s created_at when the incoming message has none (replay on reconnect); applied in upsertById and mergeAndSort, while persisted updates still overwrite.
    • Export upsertById and add 3 unit tests (append, preserve, overwrite).

Written for commit 0b0ee52. Summary will update on new commits.

Review in cubic

… seeded message

deliverPolicy:"all" on /stream reconnect replays an already-persisted run as
timestamp-less streamed UIMessages. Upserting one over its DB-seeded row (which
carries the authoritative top-level created_at) stripped the timestamp, so
readTimestamp returned +Infinity and the message sorted to the END of the list
— it rendered under the NEXT turn's user message and left its own slot empty
("No response was generated"). A refresh fixed it only because, with no active
run, the reconnect uses deliverPolicy:"new" and replays nothing.

Add preserveCreatedAt: when an upsert/merge replacement lacks created_at but the
prior row has one, keep it. A genuine persisted update (incoming has created_at)
still overwrites. Applied in both upsertById (stream commit path) and
mergeAndSort (defensive). Export upsertById + tests.
@pedrofrxncx
pedrofrxncx enabled auto-merge (squash) June 16, 2026 19:36
@pedrofrxncx
pedrofrxncx merged commit f34d5ea into main Jun 16, 2026
14 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/decopilot-stream-reorder-on-reconnect branch June 16, 2026 19:39
decocms Bot pushed a commit that referenced this pull request Jun 16, 2026
PR: #3956 fix(chat): preserve created_at so stream replay can't reorder messages on reconnect
Bump type: patch

- decocms (apps/mesh/package.json): 3.28.2 -> 3.28.3
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.

1 participant