-
-
Notifications
You must be signed in to change notification settings - Fork 297
feat(ai-sandbox): SandboxInstanceStore instance durability (BYO) #988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
326d2e5
feat(ai-sandbox): durable sandbox instance store + shared lock tokens…
AlemTuzlak b2ccb26
fix(sandbox): align instance durability with main patterns
tombeckenham d57ad22
ci: apply automated fixes
autofix-ci[bot] 2d8e35f
refactor(sandbox): take the instance store as a withSandbox option
AlemTuzlak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| '@tanstack/ai-sandbox': minor | ||
| --- | ||
|
|
||
| Durable **sandbox instance** resume for multi-process / multi-replica deploy. | ||
|
|
||
| - **`SandboxInstanceStore` / `SandboxInstanceRecord` / `InMemorySandboxInstanceStore` / `SandboxInstanceStoreCapability`** in `@tanstack/ai-sandbox` | ||
| - **`withSandbox(sandbox, { instances, locks? })`** takes the store directly, so it cannot be mis-ordered (in-memory fallback when absent). `SandboxInstanceStoreCapability` + `provideSandboxInstanceStore` remain for ambient/platform wiring; an explicit option wins over the bus. | ||
| - **`defineSandboxInstanceStore`** for inline BYO typing (same pattern as `defineLock` / `defineMessageStore`) | ||
| - Pair multi-instance with **`withLocks`** from `@tanstack/ai/locks` (distributed lock) | ||
| - Independent of chat persistence — compose both when the app needs transcript durability _and_ instance reuse | ||
| - Conformance: `runSandboxInstanceStoreConformance` from `@tanstack/ai-sandbox/testkit` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| --- | ||
| title: Sandbox Instance Durability | ||
| id: sandbox-durability | ||
| order: 9 | ||
| description: "Durable sandbox instance resume across processes with a SandboxInstanceStore passed to withSandbox." | ||
| --- | ||
|
|
||
| Your agent runs behind more than one server instance, or at the edge. A run | ||
| spins up a sandbox, clones the repo, installs deps, does its work. The next run | ||
| for the same thread should pick that sandbox back up. Instead it builds a fresh | ||
| one every time and pays the whole cold-start cost again. | ||
|
|
||
| [Lifecycle & Snapshots](./lifecycle) already knows how to resume, but its | ||
| bookkeeping is in-memory, so it only holds within one process. The moment a run | ||
| lands on a different replica (or a fresh isolate), that instance has never seen | ||
| the sandbox and re-creates it. | ||
|
|
||
| **Sandbox instance durability** is runtime placement — not chat history. It is | ||
| owned by `@tanstack/ai-sandbox`, independent of `@tanstack/ai-persistence` | ||
| (transcript / runs / interrupts). You may share a database with chat stores, but | ||
| you compose a separate middleware. | ||
|
|
||
| Two pieces: | ||
|
|
||
| - **`SandboxInstanceStore`**: map of compound key → provider sandbox id (+ | ||
| optional snapshot). Durable, shared across instances. | ||
| - **`LockStore`** (from `@tanstack/ai/locks`): mutual exclusion around resume-or-create. | ||
| Multi-instance needs a distributed lock. See [Locks](../advanced/locks). | ||
|
|
||
| ## Wire it up | ||
|
|
||
| Hand the store to `withSandbox`. The lock is a separate middleware because other | ||
| middleware can share it, and it must come **before** `withSandbox`. | ||
|
|
||
| ```ts | ||
| import { chat } from '@tanstack/ai' | ||
| import { InMemoryLockStore, withLocks } from '@tanstack/ai/locks' | ||
| import { grokBuildText } from '@tanstack/ai-grok-build' | ||
| import { | ||
| InMemorySandboxInstanceStore, | ||
| defineSandbox, | ||
| defineWorkspace, | ||
| withSandbox, | ||
| } from '@tanstack/ai-sandbox' | ||
| import type { ModelMessage } from '@tanstack/ai' | ||
|
|
||
| // Single-process: in-memory is fine for local dev. | ||
| // Multi-instance: your durable SandboxInstanceStore + distributed LockStore. | ||
| const instanceStore = new InMemorySandboxInstanceStore() | ||
| const messages: Array<ModelMessage> = [{ role: 'user', content: 'hi' }] | ||
|
|
||
| const sandbox = defineSandbox({ | ||
| id: 'repo', | ||
| provider: { | ||
| name: 'example', | ||
| capabilities: () => ({ | ||
| fs: true, | ||
| exec: true, | ||
| env: true, | ||
| ports: false, | ||
| backgroundProcesses: false, | ||
| writableStdin: false, | ||
| snapshots: false, | ||
| networkPolicy: false, | ||
| durableFilesystem: false, | ||
| fork: false, | ||
| }), | ||
| create: () => { | ||
| throw new Error('example provider — wire a real SandboxProvider') | ||
| }, | ||
| resume: () => Promise.resolve(null), | ||
| destroy: () => Promise.resolve(), | ||
| }, | ||
| workspace: defineWorkspace({ source: { type: 'none' } }), | ||
| }) | ||
|
|
||
| chat({ | ||
| adapter: grokBuildText('grok-build'), | ||
| messages, | ||
| middleware: [ | ||
| withLocks(new InMemoryLockStore()), | ||
| withSandbox(sandbox, { instances: instanceStore }), | ||
| ], | ||
| }) | ||
| ``` | ||
|
|
||
| With `reuse: 'thread'` (the default), the first run creates and records the | ||
| instance. A later run for the same `threadId` resumes it when the store (and | ||
| lock) are shared across processes. | ||
|
|
||
| Single sandbox, nothing else sharing the lock? Pass both as options and skip the | ||
| extra middleware: | ||
|
|
||
| ```ts | ||
| import { InMemoryLockStore } from '@tanstack/ai/locks' | ||
| import { withSandbox } from '@tanstack/ai-sandbox' | ||
| import { instanceStore } from './instance-store' | ||
| import { sandbox } from './sandbox' | ||
|
|
||
| const middleware = [ | ||
| withSandbox(sandbox, { | ||
| instances: instanceStore, | ||
| locks: new InMemoryLockStore(), // multi-replica: a distributed LockStore | ||
| }), | ||
| ] | ||
| ``` | ||
|
|
||
| Optional chat persistence is independent: | ||
|
|
||
| ```ts | ||
| import { withLocks, InMemoryLockStore } from '@tanstack/ai/locks' | ||
| import { withPersistence, memoryPersistence } from '@tanstack/ai-persistence' | ||
| import { withSandbox } from '@tanstack/ai-sandbox' | ||
| import { instanceStore } from './instance-store' | ||
| import { sandbox } from './sandbox' | ||
|
|
||
| const middleware = [ | ||
| withPersistence(memoryPersistence()), // chat state only | ||
| withLocks(new InMemoryLockStore()), // multi-replica: distributed LockStore | ||
| withSandbox(sandbox, { instances: instanceStore }), | ||
| ] | ||
| ``` | ||
|
|
||
| ## Implement `SandboxInstanceStore` | ||
|
|
||
| ```ts | ||
| import { | ||
| defineSandboxInstanceStore, | ||
| } from '@tanstack/ai-sandbox' | ||
| import type { SandboxInstanceRecord } from '@tanstack/ai-sandbox' | ||
|
|
||
| export const instanceStore = defineSandboxInstanceStore({ | ||
| async get(_key) { | ||
| return null | ||
| }, | ||
| async upsert(_record: SandboxInstanceRecord) { | ||
| // insert or FULLY replace by record.key | ||
| }, | ||
| async delete(_key) { | ||
| // no-op if missing | ||
| }, | ||
| }) | ||
| ``` | ||
|
|
||
| ### Invariants (conformance) | ||
|
|
||
| ```ts | ||
| import { | ||
| runSandboxInstanceStoreConformance, | ||
| } from '@tanstack/ai-sandbox/testkit' | ||
| import type { SandboxInstanceStore } from '@tanstack/ai-sandbox' | ||
|
|
||
| declare const instanceStore: SandboxInstanceStore | ||
|
|
||
| runSandboxInstanceStoreConformance('my-instance-store', () => instanceStore) | ||
| ``` | ||
|
|
||
| | Method | Invariant | | ||
| | --- | --- | | ||
| | `get` | Missing key → `null` (not throw). | | ||
| | `upsert` | **Full replace** by `record.key`. Omitted optionals clear prior values. | | ||
| | `delete` | Missing key is a **no-op**. | | ||
| | timestamps | `updatedAt` is epoch **milliseconds**. | | ||
|
|
||
| ### Suggested schema (SQLite) | ||
|
|
||
| ```sql | ||
| CREATE TABLE IF NOT EXISTS sandbox_instances ( | ||
| key text PRIMARY KEY NOT NULL, | ||
| provider text NOT NULL, | ||
| provider_sandbox_id text NOT NULL, | ||
| latest_snapshot_id text, | ||
| thread_id text NOT NULL, | ||
| latest_run_id text, | ||
| updated_at integer NOT NULL | ||
| ); | ||
| ``` | ||
|
|
||
| You can put this table next to chat tables in the same DB — that is an **app** | ||
| choice, not a requirement of `@tanstack/ai-persistence`. | ||
|
|
||
| ## Locks | ||
|
|
||
| A durable instance map without a distributed lock is still wrong across | ||
| replicas: two concurrent runs for one thread both find no record and both | ||
| create. Pair the store with a lock, either `withLocks` from | ||
| `@tanstack/ai/locks` or the `locks` option above. Full guide: | ||
| [Locks](../advanced/locks). | ||
|
|
||
| ## See also | ||
|
|
||
| - [Locks](../advanced/locks) | ||
| - [Lifecycle](./lifecycle) | ||
| - [Persistence overview](../persistence/overview) — chat state only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use
workspace:*for the internal peer dependency.@tanstack/aiusesworkspace:^, which does not follow the required internal dependency protocol.Proposed fix
As per coding guidelines, “Use the
workspace:*protocol for internal package dependencies inpackage.json.”📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Coding guidelines