Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai): stop server tool event polling from leaking memory #2086

Mergedefemaer(GitHub profile, opens in a new tab)merged 8 commits intomain fromrefs/pull/2086/headmerged by tombeckenham View on GitHub

Showing 22 changed files with 15 additions and 0 deletions (loading 18).

x
0 of 22 viewed, 22 left
8 additions and 0 deletions.changeset/loop-stopped-signal.mdAdded
View file
@@ -0,0 +1,8 @@
+---
+'@tanstack/ai': minor
+'@tanstack/ai-harness': minor
+---
+​
+A run that the agent loop stops before the model finished now says so. When `agentLoopStrategy` (for example `maxIterations`) or a middleware `onShouldContinue` returns `false` while tool results wait for the next model call, `chat()` ends the stream with a `RUN_FINISHED` that has `metadata.tanstack.loopStopped: true`, and the last stored assistant message gets the same field. `finishReason` stays the reason of the last model call (`tool_calls`, or `length` for a cut answer). A run that the model ends has no `loopStopped`.
+​
+In `@tanstack/ai-harness`, a turn that the loop stops settles with `{ outcome: 'completed', loopStopped: true }`. `session.settled()`, the `harness.input.settled` event of `createHarnessClient().events()`, and the transcript all carry it.
5 additions and 0 deletions.changeset/tool-event-polling-leak.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/ai': patch
+---
+​
+Stop a long-running server tool from leaking memory. While a tool ran, the event poll subscribed to the tool's promise again every 10 ms and kept every subscription until the tool finished. Memory grew by about 100 MB per hour while a tool stayed pending. The poll now subscribes once.
2 additions and 0 deletionsdocs/api/ai.mdModified
View file
@@ -259,6 +259,8 @@
​
A strategy that goes on after a `length` answer makes the model send the [cut tool call](../chat/agentic-cycle#when-the-output-limit-cuts-a-tool-call) again.
​
+When a strategy stops the loop while tool results wait for the model, the last `RUN_FINISHED` has `metadata.tanstack.loopStopped: true`. See [Know when the loop stopped the run](../chat/agentic-cycle#know-when-the-loop-stopped-the-run).
+​
## Response helpers
​
Each helper turns a `chat()` stream into a response for one transport. Pick the one that matches the client connection adapter. See [Transports](../transports/overview).