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

Đang mở
#2,059 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@tombeckenham đang làm issue này rồi.

Từ ngày 5/11/2026.

  • #2060 của @adelrodriguez — đang mở

Đánh giá

Issue này chưa được đánh giá.

Mô tả

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

Ngôn ngữ chính
TypeScript
Star
3.3k
Fork
378
Merge trung bình
1 ngày 2 giờ
Pull request đã merge (30 ngày)
254

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TanStack/ai

Tất cả issue của TanStack/ai

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.