Open History to see the last commit
1---
2title: Sandbox Instance Durability
3id: sandbox-durability
4order: 9
5description: "Resume the same sandbox from any server process. Pass a SandboxInstanceStore and a lock to withSandbox."
6---
7
8Durability stores the sandbox for a thread. The next run on another server reuses it and skips the cold start.
9
10[Lifecycle & Snapshots](./lifecycle) can resume a sandbox, but it keeps its records in memory, in one process. A `SandboxInstanceStore` keeps those records in shared storage, so every server finds the same sandbox.
11
12## Wire it up
13
14You need two pieces:
15
16- **`SandboxInstanceStore`**: maps the compound sandbox key to the provider sandbox id (and an optional snapshot). It must be shared across servers.
17- **`LockStore`** from `@tanstack/ai/locks`: makes sure that only one run at a time does resume-or-create. Across servers you need a distributed lock. See [Locks](../advanced/locks).
18
19This sample uses the in-memory versions, which work in one process for local dev:
20
21```ts
22import { chat } from '@tanstack/ai'
23import { InMemoryLockStore, withLocks } from '@tanstack/ai/locks'
24import { GROK_CLI_INSTALL_COMMAND, grokBuildText } from '@tanstack/ai-grok-build'
25import {
26 createSecrets,
27 defineSandbox,
28 defineWorkspace,
29 githubRepo,
30 InMemorySandboxInstanceStore,
31 withSandbox,
32} from '@tanstack/ai-sandbox'
33import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
34
35// Production: your durable store and a distributed LockStore.
36const instances = new InMemorySandboxInstanceStore()
37const locks = new InMemoryLockStore()
38
39const sandbox = defineSandbox({
40 id: 'repo-agent',
41 provider: dockerSandbox({ image: 'node:22' }),
42 workspace: defineWorkspace({
43 source: githubRepo({ repo: 'owner/app' }),
44 setup: [GROK_CLI_INSTALL_COMMAND],
45 secrets: createSecrets({ XAI_API_KEY: process.env.XAI_API_KEY ?? '' }),
46 }),
47})
48
49const stream = chat({
50 threadId: 'thread-1',
51 adapter: grokBuildText('grok-build'),
52 messages: [{ role: 'user', content: 'hi' }],
53 middleware: [
54 // The lock comes BEFORE withSandbox.
55 withLocks(locks),
56 withSandbox(sandbox, { instances }),
57 ],
58})
59```
60
61With `reuse: 'thread'` (the default), the first run creates the sandbox and records it. A later run for the same `threadId` resumes it, on any server that shares the store and the lock.
62
63For production, replace both in-memory stores. To write your own `SandboxInstanceStore`, see [Build a Sandbox Adapter](../persistence/build-a-sandbox-adapter).
64
65## How it works
66
67- Instance durability is about where the sandbox runs. It is not chat history. `@tanstack/ai-sandbox` owns it, separate from `@tanstack/ai-persistence` (transcripts, runs, interrupts). You can use the same database for both, but each has its own middleware.
68- It does not copy the workspace into your storage. When a new sandbox must rebuild finished files, artifacts, and the conversation, use [Keep Files After Reload](./portable-snapshots-configure).
69- It does not keep the agent's output. To read a run's events from another process, see [The Run Journal](./journal).
70
71The two work together. A resumed sandbox still holds the journal of every **durable** run that ran in it, under `/tmp/tanstack-runs`. A run is durable when `withSandbox` gets both `runs` and `durability`. A run without that opt-in leaves no journal file.
72
73## Other ways to wire it
74
75If nothing else shares the lock, pass both as options and skip `withLocks`:
76
77```ts
78import { InMemoryLockStore } from '@tanstack/ai/locks'
79import { withSandbox } from '@tanstack/ai-sandbox'
80import { instances } from './instance-store'
81import { sandbox } from './sandbox'
82
83const middleware = [
84 withSandbox(sandbox, {
85 instances,
86 locks: new InMemoryLockStore(), // multi-server: a distributed LockStore
87 }),
88]
89```
90
91Chat persistence is a separate middleware. Add it next to the sandbox:
92
93```ts
94import { withLocks, InMemoryLockStore } from '@tanstack/ai/locks'
95import { withPersistence, memoryPersistence } from '@tanstack/ai-persistence'
96import { withSandbox } from '@tanstack/ai-sandbox'
97import { instances } from './instance-store'
98import { sandbox } from './sandbox'
99
100const middleware = [
101 withPersistence(memoryPersistence()), // chat state only
102 withLocks(new InMemoryLockStore()), // multi-server: a distributed LockStore
103 withSandbox(sandbox, { instances }),
104]
105```
106
107## Advanced
108
109### Why you need a lock
110
111A shared instance store without a distributed lock is still wrong across servers. Two runs for one thread can start at the same time. Both find no record, and both create a sandbox.
112
113Pair the store with a lock: use `withLocks` from `@tanstack/ai/locks`, or the `locks` option. See [Locks](../advanced/locks).
114
115### Implement `SandboxInstanceStore`
116
117The store has three methods: `get`, `upsert`, and `delete`. Each has a rule that a conformance suite checks for you. [Build a Sandbox Adapter](../persistence/build-a-sandbox-adapter) shows the code, the table, and the suite. It also helps you choose how much a sandboxed run leaves behind.
118
119### Related pages
120
121- [Takeover & Detached Runs](./takeover): survive a client disconnect. A detached run keeps its sandbox up. A later server finds it again with `RunStore.sandboxKey` and `RunStore.detachedSince`.
122- [Persistence overview](../persistence/overview): chat state only.