Client tool continuation stalls when multiple tools complete in the same round #302
Send to my agentDisclosure#
This issue was written by an AI coding agent (Claude Code) on behalf of a developer who encountered this bug. The analysis below is based on source-level tracing of the @tanstack/ai-client ChatClient code and observed runtime behavior.
Versions#
@tanstack/ai: 0.5.1@tanstack/ai-client: 0.4.5@tanstack/ai-vue: 0.5.4- Framework: Vue 3.5 with Composition API
Description#
When an LLM response triggers multiple client-side tool calls in the same round (e.g., the model calls get_expenses and execute_sql simultaneously), the chat gets stuck after all tools complete. The model never produces its follow-up text response. The isLoading flag goes false with the last message part being a tool-result, but no continuation request is sent to the server.
Observed behavior#
- User sends a message like "How much did I spend this year on food?"
- Round 1: Model calls
get_expenses→ client executes → result returned → continuation fires correctly ✓ - Round 2: Model calls
get_expenses(drill-down) andexecute_sqlin the same response → both tools execute → both results appear in the UI → chat gets permanently stuck - The "Stop generating" button stays visible indefinitely
- No text response is ever produced by the model
- On page reload, the last tool's result shows as "loading" forever (it was never sent back to the server in a continuation request)
Expected behavior#
After all client tools in a round complete, a continuation request should be sent to the server so the model can produce its text response.
Root Cause Analysis#
The issue appears to be in ChatClient's interaction between drainPostStreamActions(), checkForContinuation(), and the shared postStreamActions queue.
The flow#
When multiple tools complete in the same round, each addToolResult() call queues a checkForContinuation() action onto postStreamActions:
postStreamActions = [checkForContinuation_A, checkForContinuation_B]
After the stream finalizes, streamResponse()'s finally block calls drainPostStreamActions():
// streamResponse() finally block:
this.setIsLoading(false)
await this.drainPostStreamActions() // processes the queue
The problem: nested drain steals actions#
drainPostStreamActions() runs checkForContinuation_A, which calls streamResponse() for the continuation (Round 3). Inside Round 3's streamResponse() finally block, drainPostStreamActions() is called again (nested). This inner drain finds checkForContinuation_B still in the shared postStreamActions array and processes it:
Outer drain: shift checkForContinuation_A → await it
checkForContinuation_A: continuationPending = true → await streamResponse() (Round 3)
Round 3 finally: drainPostStreamActions() (INNER DRAIN)
Inner drain: shift checkForContinuation_B → await it
checkForContinuation_B: sees continuationPending = true → RETURNS immediately (no-op)
Inner drain complete
Round 3 streamResponse() returns
checkForContinuation_A: continuationPending = false
Outer drain: postStreamActions is now empty → exits
If Round 3 itself involves tool calls (model calls another tool), the continuation from Round 3 is blocked because:
checkForContinuation_B(stolen by inner drain) seescontinuationPending = trueand returns without doing anything- The explicit continuation check at the end of
streamResponse()also seescontinuationPending = true - After
continuationPendingis finally set tofalse, no one callscheckForContinuation()again - The chat is stuck with tool results but no model continuation
Secondary issue: areAllToolsComplete() returns true for no-tool messages#
areAllToolsComplete(): boolean {
// ...
if (toolParts.length === 0) return true // ← always true for text-only messages
// ...
}
This means shouldAutoSend() returns true even after a text-only response, which can cause spurious continuation rounds when combined with the duplicate action problem.
Minimal reproduction scenario#
- Configure
useChatwith 2+ client tools (usingtoolDefinition().client()) - Set up a prompt that causes the LLM to call both tools in the same response
- Both tools execute and complete successfully
- Observe:
isLoadinggoesfalse, last part istool-result, but no continuation fires
Suggested fix#
A few possible approaches:
Option A: Deduplicate continuation actions#
Instead of queuing a checkForContinuation per tool result, use a single flag:
async addToolResult(result) {
this.processor.addToolResult(...)
if (this.isLoading) {
// Only queue ONE continuation check, not one per tool
if (!this.postStreamActions.some(a => a === this.checkForContinuation)) {
this.queuePostStreamAction(() => this.checkForContinuation())
}
return
}
await this.checkForContinuation()
}
Option B: Don't nest drains#
Prevent re-entrancy in drainPostStreamActions():
private draining = false
private async drainPostStreamActions(): Promise<void> {
if (this.draining) return // skip nested drain
this.draining = true
try {
while (this.postStreamActions.length > 0) {
const action = this.postStreamActions.shift()!
await action()
}
} finally {
this.draining = false
}
}
Option C: Post-continuation recheck#
After continuationPending is cleared, recheck if continuation is still needed:
private async checkForContinuation(): Promise<void> {
if (this.continuationPending || this.isLoading) return
if (this.shouldAutoSend()) {
this.continuationPending = true
try {
await this.streamResponse()
} finally {
this.continuationPending = false
// Recheck after clearing the flag
if (this.shouldAutoSend() && !this.isLoading) {
await this.checkForContinuation()
}
}
}
}
Option B seems cleanest as it prevents the root cause (nested drain stealing actions) without changing the continuation semantics.
mind trying this out and seeing if it solves it: https://github.com/TanStack/ai/pull/429
AlemTuzlak closed this
Sign in to join this conversation.
For others running into this, this is the
@tanstack+ai-client+0.4.3.patchwe're using in the meanwhile: