fix(ai): preserve message roles during assistant reasoning #2262
main fromrefs/pull/2262/head·merged by AlemTuzlak View on GitHubA user message that arrives during streamed reasoning can appear as part of the assistant reply. Only assistant-role text starts now claim the assistant reasoning placeholder.
🎯 Changes#
- Keep streamed user and system messages under their own IDs and roles.
- Test the event sequence in
StreamProcessorand through a watchinguseChatclient. - Document how message IDs and roles work during streamed reasoning.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. A user or system message appears as assistant text if its start event arrives before the assistant starts its reply.
Cause. handleTextMessageStartEvent can assign the incoming ID to the assistant message created by reasoning. That message retains its assistant role.
Fix. Check the normalized role before assigning the incoming ID. The assistant reply can still take over the reasoning placeholder.
Possible alternatives#
- Insert each complete user message through the client API. Every client that streams follow-ups would need to implement that handling.
Testing#
Commands#
pnpm --filter @tanstack/ai exec vitest run tests/stream-processor.test.tspnpm --filter @tanstack/ai-e2e test:e2e tests/chat-client-watcher-status.spec.ts
Before the fix (92e8799) — both role assertions received assistant. The browser test could not find the user row.
StreamProcessor: 2 failed
Chromium: expected "Follow-up." in the user row; element not found
With the fix
StreamProcessor: 283 passed
Chromium watcher spec: 2 passed
The core build, type check, lint, and formatting checks also passed. The full local pnpm test:pr run passed on Linux with Node 24.22.1. Nx reported 494 successful tasks, and the declaration-file scan passed. The earlier macOS run timed out in ai-skills and ai-sandbox-local-process. Both suites passed on Linux.
Reproduce#
- Send
RUN_STARTEDandREASONING_MESSAGE_CONTENT. - Send a user message with its own ID using
TEXT_MESSAGE_START,CONTENT, andEND. - Send the assistant reply under a different ID.
Before the fix, the user text is part of an assistant message. With the fix, the user text has its own row. The reasoning and reply share the assistant row. The browser test runs this sequence without a model or credentials.
Risk / rollback#
The guard changes which message starts can claim an assistant placeholder. Revert the commit to restore the previous behavior.
Summary by CodeRabbit#
Bug Fixes
- Streamed user and system messages now remain separate from assistant reasoning and replies, preserving their roles and text in the transcript.
Documentation
- Clarified how message IDs and roles are represented in streamed chat events.
Tests
- Added coverage for streamed user and system messages, including follow-ups received while the assistant is reasoning.
View your CI Pipeline Execution ↗ for commit 22d0ae8b53c54806a1a572b1dd56859910dcabfd
nx affected --targets=test:sherif,test:knip,tes...nx run-many --targets=test:coverage --projects=...nx run-many --targets=build --exclude=examples/...💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗
☁️ Nx Cloud last updated this comment at
2026-11-30 09:36:37UTC