withSandbox: each run's save drops the harness tool calls of earlier runs

Aperta
#2,059 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@tombeckenham ci sta già lavorando.

Dal 5/11/2026.

  • #2060 di @adelrodriguez — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

has-pr waiting-on: maintainer
TanStack AI version

@tanstack/ai-sandbox 1.1.0 and main (644a780b0). @tanstack/ai 1.1.0 and main. @tanstack/ai-persistence 1.0.1.

Framework/Library version

Server only. chat() in a Node 24 route handler. No client framework is involved.

Describe the bug and the steps to reproduce it

With withPersistence and withSandbox on one thread, run 2's save does not contain the harness tool calls of run 1. Only the tool calls of the last run survive. After a reload, every earlier run shows its text but none of its tool cards. docs/sandbox/events.md says that the tool cards come back when the user opens the thread again.

Steps:

  1. Use a harness adapter (for example claudeCodeText) with middleware: [withPersistence(persistence), withSandbox(sandbox, { instances })].
  2. Run chat() on thread t1. The harness runs one tool. The saved thread has the tool call and its result.
  3. Run chat() again on thread t1. The harness runs one more tool.
  4. Load thread t1 from the message store. Run 1's tool call and result are gone. Run 2's are there.

The middleware order does not change the result.

Suspected cause. withSandbox's onConfig returns { messages: stripObservedToolCalls(config.messages) }. Its comment says that ctx.messages keeps the recorded calls and that only the model request loses them. But applyMiddlewareConfig sets this.messages = config.messages and then middlewareCtx.messages = this.messages. So the stripped list replaces the transcript. The recorder's reconcile restores only the calls of the current run. Then withPersistence's onFinish saves ctx.messages without the earlier runs' calls.

The engine already keeps a separate list for the model request: this.providerMessages = config.providerMessages ?? config.messages.

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

https://github.com/adelrodriguez/ai/blob/08d45e633c1ce1f58e11da8d7367279709b32166/packages/ai-sandbox/tests/middleware-tool-history.test.ts#L381-L477

The test runs two chat() runs with a fixed harness stream, withPersistence(memoryPersistence()), and withSandbox over a fake provider. It fails on main in both middleware orders:

 × a later run keeps the tool calls of earlier runs (persistence first)
 × a later run keeps the tool calls of earlier runs (sandbox first)
AssertionError: expected undefined to be defined
  expect(pairOf(messages, 'call-run-1').call).toBeDefined()
 Tests  2 failed | 14 passed (16)
Do you intend to try to help solve this bug with your own PR?

Yes, I am also opening a PR that solves the problem along side this issue

Terms & Code of Conduct
  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

🤖 Created with Claude Opus 5.5 in T3 Code

Lingua principale
TypeScript
Stelle
3.3k
Fork
378
Merge medio
1g 2h
PR unite (30g)
254

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/ai

Tutte le issue di TanStack/ai

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.