Skip to content

fix(ai-chat): preserve server-generated messages when client appends new messages - #1038

Merged
threepointone merged 1 commit into
mainfrom
fix/ai-chat-preserve-server-messages
Mar 1, 2026
Merged

fix(ai-chat): preserve server-generated messages when client appends new messages#1038
threepointone merged 1 commit into
mainfrom
fix/ai-chat-preserve-server-messages

Conversation

@threepointone

Copy link
Copy Markdown
Contributor

Problem

The _deleteStaleRows reconciliation in persistMessages (added in #1014 for regenerate support) was too aggressive. When a client sent a chat request with new messages that didn't include the full server history, it deleted server-generated assistant messages the client hadn't seen yet.

This caused the "multiple messages accumulate in conversation" e2e test to fail: the second request's persistMessages call deleted the first assistant message because it wasn't in the incoming message set.

Fix

Gate stale row deletion on whether the incoming messages are a subset of the server state. This distinguishes:

  • Regenerate/trim: all incoming IDs exist in server state → delete stale rows ✓
  • Append new messages: incoming set contains IDs unknown to server → skip deletion, preserve server messages ✓

Changes

  • packages/ai-chat/src/index.ts: Add isSubsetOfServer guard before stale row deletion
  • packages/ai-chat/src/tests/regenerate-message.test.ts: Add regression test

Testing

  • 33/33 e2e tests pass (including previously failing "multiple messages accumulate")
  • 291/291 unit tests pass (including all regenerate tests + new regression test)

@changeset-bot

changeset-bot Bot commented Mar 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 03fe9c9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/ai-chat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Mar 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/agents@1038
npm i https://pkg.pr.new/@cloudflare/ai-chat@1038
npm i https://pkg.pr.new/@cloudflare/codemode@1038
npm i https://pkg.pr.new/hono-agents@1038

commit: fd92a0f

@threepointone
threepointone merged commit e61cb4a into main Mar 1, 2026
3 checks passed
@threepointone
threepointone deleted the fix/ai-chat-preserve-server-messages branch March 1, 2026 16:24
@github-actions github-actions Bot mentioned this pull request Mar 1, 2026
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