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

Abierto
#2,059 0 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@tombeckenham ya está trabajando en esto.

Desde el 5/11/2026.

  • #2060 de @adelrodriguez — abierto

Evaluación

Este issue todavía no se ha evaluado.

Descripción

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

Lenguaje dominante
TypeScript
Estrellas
3.3k
Forks
378
Merge medio
1 d 2 h
PR fusionados (30 d)
254

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TanStack/ai

Todos los issues de TanStack/ai

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.