Skip to content

fix(ai): preserve existing message ids on interrupt snapshots - #1116

Open
kolaworld wants to merge 2 commits into
TanStack:mainfrom
kolaworld:fix/1107-preserve-message-snapshot-id
Open

fix(ai): preserve existing message ids on interrupt snapshots#1116
kolaworld wants to merge 2 commits into
TanStack:mainfrom
kolaworld:fix/1107-preserve-message-snapshot-id

Conversation

@kolaworld

@kolaworld kolaworld commented Aug 15, 2026

Copy link
Copy Markdown

Keep ModelMessage.id on MESSAGES_SNAPSHOT so interrupt boundaries do not rewrite stable identity. Generate snapshot_<run>_<index> only when an id is missing.

Fixes #1107

🎯 Changes

  • Keep a stored ModelMessage.id on interrupt MESSAGES_SNAPSHOT events instead of always rewriting it to snapshot_<run>_<index>.
  • Generate snapshot_<run>_<index> only when a message has no id, matching the client snapshot normalizer.
  • That stops a stable user/assistant identity from looking like new input after a native interrupt, which breaks dedupe, reconciliation, and in-place rendering keyed on message.id.
  • Add a chat() regression that asserts user-1 and stream-assistant survive the snapshot, plus a patch changeset for @tanstack/ai.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes

    • Preserved existing message IDs in chat snapshots generated when an interaction is interrupted.
    • Assigned consistent fallback IDs to messages that do not already have one.
  • Tests

    • Added coverage for preserving existing IDs and generating deterministic IDs for messages without IDs.

Keep ModelMessage.id on MESSAGES_SNAPSHOT so interrupt boundaries do not rewrite stable identity. Generate snapshot_<run>_<index> only when an id is missing.

Fixes TanStack#1107
@coderabbitai

coderabbitai Bot commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ebcf428e-14f4-43f0-b238-1e0f02bed0fd

📥 Commits

Reviewing files that changed from the base of the PR and between 9a4124d and b6c9128.

📒 Files selected for processing (3)
  • .changeset/fix-1107-snapshot-message-ids.md
  • packages/ai/src/activities/chat/index.ts
  • packages/ai/tests/chat.test.ts

📝 Walkthrough

Walkthrough

Interrupt MESSAGES_SNAPSHOT events now preserve existing message IDs. Snapshots generate IDs from the run ID and message index only when messages do not already have IDs. Regression tests cover both behaviors.

Changes

Snapshot message ID preservation

Layer / File(s) Summary
Preserve and validate snapshot IDs
packages/ai/src/activities/chat/index.ts, packages/ai/tests/chat.test.ts, .changeset/fix-1107-snapshot-message-ids.md
buildMessagesSnapshotChunk keeps existing message IDs and generates fallback snapshot IDs for messages without IDs. Tests cover supplied user and assistant IDs and the fallback format. The changeset records the patch release.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to b6c91

The change preserves existing message identities at interrupt boundaries and only generates fallback IDs when needed; no actionable merge-blocking risk remains after normal checks and review.

Possibly related PRs

Suggested reviewers: alemtuzlak, tombeckenham, season179

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: preserving existing message IDs in interrupt snapshots.
Description check ✅ Passed The description includes the required changes, checklist, and release impact sections with relevant details and completed items.
Linked Issues check ✅ Passed The implementation, regression tests, fallback ID behavior, and changeset satisfy issue #1107 requirements.
Out of Scope Changes check ✅ Passed All changes directly support preserving message IDs and validating fallback snapshot IDs for issue #1107.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Labels

waiting-on: maintainer The ball is in the maintainers’ court

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MESSAGES_SNAPSHOT incorrectly replaces existing message ids

1 participant