fix(ai-client): prevent drainPostStreamActions re-entrancy race condition#429
fix(ai-client): prevent drainPostStreamActions re-entrancy race condition#429AlemTuzlak wants to merge 5 commits intomainfrom
Conversation
…ueued actions When multiple client tools complete in the same round, each addToolResult() queues a checkForContinuation action. The first drain executes one action which calls streamResponse(), whose finally block calls drainPostStreamActions() again (nested). The inner drain steals the remaining actions, permanently stalling the conversation. Add a draining flag to skip nested drain calls. The outer drain processes all actions sequentially, preventing action theft. Also fix shouldAutoSend() to require at least one tool call in the last assistant message. Previously it returned true for text-only responses (areAllToolsComplete() returns true when toolParts.length === 0), causing the second queued checkForContinuation action to incorrectly trigger an extra continuation round and produce duplicate content. Fixes #302
📝 WalkthroughWalkthroughThis PR fixes a re-entrancy bug in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
🚀 Changeset Version Preview1 package(s) bumped directly, 9 bumped as dependents. 🟩 Patch bumps
|
|
View your CI Pipeline Execution ↗ for commit 3d06f8b
☁️ Nx Cloud last updated this comment at |
@tanstack/ai
@tanstack/ai-anthropic
@tanstack/ai-client
@tanstack/ai-code-mode
@tanstack/ai-code-mode-skills
@tanstack/ai-devtools-core
@tanstack/ai-elevenlabs
@tanstack/ai-event-client
@tanstack/ai-fal
@tanstack/ai-gemini
@tanstack/ai-grok
@tanstack/ai-groq
@tanstack/ai-isolate-cloudflare
@tanstack/ai-isolate-node
@tanstack/ai-isolate-quickjs
@tanstack/ai-ollama
@tanstack/ai-openai
@tanstack/ai-openrouter
@tanstack/ai-preact
@tanstack/ai-react
@tanstack/ai-react-ui
@tanstack/ai-solid
@tanstack/ai-solid-ui
@tanstack/ai-svelte
@tanstack/ai-vue
@tanstack/ai-vue-ui
@tanstack/preact-ai-devtools
@tanstack/react-ai-devtools
@tanstack/solid-ai-devtools
commit: |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/typescript/ai-client/tests/chat-client.test.ts`:
- Around line 11-14: The import specifiers in the import from
'../src/connection-adapters' are not alphabetized; reorder the named imports so
they are sorted alphabetically (e.g., place ConnectConnectionAdapter before
ConnectionAdapter) to satisfy the linter's sort-imports rule and update the
import line that currently lists ConnectionAdapter and ConnectConnectionAdapter.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 67771ad9-6029-4d21-a7c4-07a93e440236
📒 Files selected for processing (3)
.changeset/fix-drain-post-stream-reentrance.mdpackages/typescript/ai-client/src/chat-client.tspackages/typescript/ai-client/tests/chat-client.test.ts
| import type { | ||
| ConnectionAdapter, | ||
| ConnectConnectionAdapter, | ||
| } from '../src/connection-adapters' |
There was a problem hiding this comment.
Fix import member order to satisfy lint.
The import specifiers are not alphabetically sorted, which matches the reported sort-imports error.
🔧 Suggested fix
import type {
- ConnectionAdapter,
ConnectConnectionAdapter,
+ ConnectionAdapter,
} from '../src/connection-adapters'📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| import type { | |
| ConnectionAdapter, | |
| ConnectConnectionAdapter, | |
| } from '../src/connection-adapters' | |
| import type { | |
| ConnectConnectionAdapter, | |
| ConnectionAdapter, | |
| } from '../src/connection-adapters' |
🧰 Tools
🪛 ESLint
[error] 13-13: Member 'ConnectConnectionAdapter' of the import declaration should be sorted alphabetically.
(sort-imports)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/typescript/ai-client/tests/chat-client.test.ts` around lines 11 -
14, The import specifiers in the import from '../src/connection-adapters' are
not alphabetized; reorder the named imports so they are sorted alphabetically
(e.g., place ConnectConnectionAdapter before ConnectionAdapter) to satisfy the
linter's sort-imports rule and update the import line that currently lists
ConnectionAdapter and ConnectConnectionAdapter.
Summary
drainPostStreamActions()preventing nested drain calls from stealing queued actionsshouldAutoSend()guard requiring at least onetool-callpart before triggering auto-continuation (prevents spurious continuation after text-only responses)addToolResult()queues acheckForContinuationaction. Without the guard, the first action'sstreamResponse()→finally→drainPostStreamActions()(nested) steals remaining actions, permanently stalling the conversationFixes #302
Test plan
@tanstack/ai-clienttests passSummary by CodeRabbit