Repository navigation
feat(ai-sandbox-tangle): add Tangle sandbox provider #2122
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
Open
drewstone
wants to merge
9
commits into
TanStack:main
Choose a base branch
from
drewstone:feat/ai-sandbox-tangle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a7694b
feat(ai-sandbox-tangle): add Tangle sandbox provider
drewstone 3453a4c
chore(ai-sandbox-tangle): lockfile and formatting
drewstone 6c43ab9
docs(ai-sandbox-tangle): say env is not sent at create
drewstone ae34ef5
fix(ai-sandbox-tangle): survive dropped reads, fail on lost output, iβ¦
drewstone d367bab
fix(ai-sandbox-tangle): align re-reads by byte position, cap spawned β¦
drewstone 648abf9
test(ai-sandbox-tangle): check the aborted create was deleted and cleβ¦
drewstone 006e65b
fix(ai-sandbox-tangle): retry a fork parent's delete, start a server β¦
drewstone 7f72f36
fix(ai-sandbox-tangle): bound a fork parent's delete retry by time anβ¦
drewstone 7ff1edd
docs(ai-sandbox-tangle): state the measured fork-parent destroy time β¦
drewstone 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
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,16 @@ | ||
| --- | ||
| '@tanstack/ai-sandbox-tangle': minor | ||
| --- | ||
|
|
||
| Add `@tanstack/ai-sandbox-tangle`, a sandbox provider backed by Tangle cloud | ||
| sandboxes through `@tangle-network/sandbox`. It implements the | ||
| `SandboxProvider` / `SandboxHandle` contract: file reads and writes through the | ||
| sandbox file API, commands with separate stdout and stderr and real exit codes, | ||
| background processes with streamed output, public preview URLs through | ||
| `ports.connect`, `env` injection, and resume by id across stop. | ||
|
|
||
| `snapshot()` captures the workspace and `restoreSnapshot()` creates a new | ||
| sandbox from it. `fork()` branches the running sandbox into a new one. | ||
| `kill()` signals the process tree inside the sandbox, and an aborted `exec` | ||
| kills its process; both are measured against production. A | ||
| `network: 'deny'` policy creates the sandbox with egress blocked. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| <div align="center"> | ||
| <picture> | ||
| <source | ||
| media="(prefers-color-scheme: dark)" | ||
| srcset="https://tanstack.com/api/readme/ai.png?theme=dark" | ||
| /> | ||
| <source | ||
| media="(prefers-color-scheme: light)" | ||
| srcset="https://tanstack.com/api/readme/ai.png" | ||
| /> | ||
| <img | ||
| src="https://tanstack.com/api/readme/ai.png" | ||
| alt="TanStack AI" | ||
| width="900" | ||
| /> | ||
| </picture> | ||
| </div> | ||
|
|
||
| <br /> | ||
|
|
||
| # @tanstack/ai-sandbox-tangle | ||
|
|
||
| Tangle sandbox provider for [TanStack AI](https://tanstack.com/ai). Runs harness | ||
| adapters inside isolated [Tangle](https://tangle.tools) cloud sandboxes through | ||
| the uniform `SandboxHandle`: real filesystem, shell, background processes, | ||
| preview URLs, snapshots, and fork. | ||
|
|
||
| ## Install | ||
|
|
||
| ```bash | ||
| npm install @tanstack/ai @tanstack/ai-sandbox @tanstack/ai-sandbox-tangle | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| import { | ||
| defineSandbox, | ||
| defineWorkspace, | ||
| withSandbox, | ||
| } from '@tanstack/ai-sandbox' | ||
| import { tangleSandbox } from '@tanstack/ai-sandbox-tangle' | ||
|
|
||
| const sandbox = defineSandbox({ | ||
| id: 'agent', | ||
| provider: tangleSandbox({ | ||
| apiKey: process.env.TANGLE_API_KEY, // or set the env var and omit | ||
| }), | ||
| workspace: defineWorkspace({ source: { type: 'none' } }), | ||
| }) | ||
|
|
||
| // Then pass `withSandbox(sandbox)` as chat() middleware. | ||
| ``` | ||
|
|
||
| The API key falls back to the `TANGLE_API_KEY` environment variable when | ||
| `apiKey` is omitted. | ||
|
|
||
| ### End-to-end example | ||
|
|
||
| Using the provider directly through the uniform `SandboxHandle` (no harness / | ||
| `chat()` involved): | ||
|
|
||
| ```ts | ||
| import { tangleSandbox } from '@tanstack/ai-sandbox-tangle' | ||
|
|
||
| const provider = tangleSandbox() | ||
| const sbx = await provider.create({}) | ||
| try { | ||
| await sbx.fs.write('/workspace/hello.txt', 'hello from tangle') | ||
| console.log(await sbx.fs.read('/workspace/hello.txt')) | ||
|
|
||
| const run = await sbx.process.exec('node --version') | ||
| console.log('node', run.stdout.trim(), '(exit', run.exitCode, ')') | ||
|
|
||
| const server = await sbx.process.spawn('python3 -m http.server 3000') | ||
| const channel = await sbx.ports.connect(3000) | ||
| console.log('preview url:', channel.url) | ||
| await server.kill() | ||
| } finally { | ||
| await sbx.destroy() | ||
| } | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Option | Default | Notes | | ||
| | -------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | | ||
| | `apiKey` | `TANGLE_API_KEY` | Tangle API key. | | ||
| | `baseUrl` | `https://sandbox.tangle.tools` | Sandbox API URL. Must use HTTPS. | | ||
| | `environment` | platform default | A catalog environment or a container image, such as `node:22`. An image has a read-only root, and its `fork()` is slow. | | ||
| | `resources` | platform default | `{ cpuCores, memoryMB, diskGB }`. | | ||
| | `maxLifetimeSeconds` | platform default | Run time before the platform suspends the sandbox. The workspace is kept, and `resume` starts it again. | | ||
| | `idleTimeoutSeconds` | platform default | Idle time before the platform suspends the sandbox. | | ||
| | `egressPolicy` | `{ mode: 'open' }` | `open`, `strict` with `allowDomains`, or `blocked`. The platform default is `strict`. A `network: 'deny'` policy wins. | | ||
| | `workdir` | `/home/agent/workspace` | Where the `/workspace` virtual root maps. Keep it under `/home/agent` or `/tmp`. | | ||
| | `metadata` | none | Extra metadata on every created sandbox. The deterministic sandbox id from `ensure()` is stored under `tanstack-ai-key`. | | ||
|
|
||
| ## Capabilities | ||
|
|
||
| | Capability | Supported | Notes | | ||
| | --------------------- | --------- | ---------------------------------------------------------------------------------------------------------- | | ||
| | `fs` | β | Native file API for `read`, `readBytes`, and `write`; the rest runs as commands. `lstat` reports symlinks. | | ||
| | `exec` | β | Separate `stdout` and `stderr`, no run limit. Lost output is an error. An abort kills the process. | | ||
| | `env` | β | Env for each command, plus the `env.set` overlay. Nothing is sent at create. | | ||
| | `ports` | β | `ports.connect(port)` creates a preview link and returns its public HTTPS URL. | | ||
| | `snapshots` | β | `snapshot()` captures the workspace. `restoreSnapshot()` creates a new sandbox from it. | | ||
| | `durableFilesystem` | β | The workspace persists across stop and resume. | | ||
| | `backgroundProcesses` | β | `spawn()` returns a real pid and separate streams. Past 8 MiB a stream is cut and the process killed. | | ||
| | `writableStdin` | β | The process API takes no stdin stream; harnesses get their prompt from a file. | | ||
| | `killableProcesses` | β | `kill()` signals the process tree inside the sandbox, measured against a live sandbox. | | ||
| | `networkPolicy` | β | `policy.capabilities.network: 'deny'` maps to egress mode `blocked`. | | ||
| | `fork` | β | `fork()` branches the running sandbox into a new one. | | ||
|
|
||
| The native file calls reach the sandbox user's home (`/home/agent`) and | ||
| `/tmp`. Read other paths with `process.exec`. | ||
|
|
||
| A new sandbox is created without the account's stored secrets. A restore from | ||
| a snapshot inherits them, because the API refuses an isolated restore. | ||
|
|
||
| A parent cannot be destroyed while a fork of it exists. The API stops the | ||
| parent and refuses the delete. `destroy()` tries again for 30 to 40 seconds | ||
| (a refusal takes about 8 seconds), then rejects, and the parent stays stopped. | ||
| Destroy the forks first, or destroy them together with the parent. | ||
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.
Uh oh!
There was an error while loading. Please reload this page.