Your agent sandbox needs an exit plan
Every agent sandbox demo looks the same. Create a box, run a command, print the output, done. Nobody shows the last ten minutes of the box's life, and that's the bit that bites you in week three.
Quick disclosure: I'm a founding member of Sandscale, so the code below uses its TypeScript SDK. The habits themselves work with any sandbox, including one you host.
Three ways a box ends badly
- Your script throws halfway through. The
stopcall was at the bottom, so it never ran, and the box just sits there. - The agent finishes and asks a human to approve something. The human goes to lunch, then a meeting. The box waits, idle, and still running.
- The run was fine, and tomorrow you want to continue from that exact state without redoing a twenty-minute install.
None of these is an isolation problem. You can have a perfect kernel boundary and still waste a lot of compute by being sloppy about how boxes end.
Timeout: a backstop, not a plan
create takes timeoutSeconds. The docs say the server default is 3600 and the max is 86400, and the quickstart says it stops the box if your script never does. That's the right mental model. It saves you when cleanup didn't run.
The catch: too short and you kill a long build halfway, and the agent sees a confusing failure. Too long and a crashed script turns into a day of billed time. I pick per task. Ten minutes for "run this test file", an hour for "install and run the suite", and anything longer should be a deliberate choice.
Stop in finally
Boring, and still the best pattern. Whoever creates the box stops it:
import { Sandscale } from '@sandscale/sdk'; const sandscale = new Sandscale(); // reads SANDSCALE_API_KEY const box = await sandscale.sandboxes.create({ timeoutSeconds: 600, labels: { task: 'run-tests' }, }); try { const run = await sandscale.sandboxes.run(box.id, 'echo hello', { region: box.region, onStdout: (t) => process.stdout.write(t), }); console.log('exit code', run.exitCode); } finally { await sandscale.sandboxes.stop(box.id, { region: box.region }); } Add the label even though it's optional. With thirty boxes in a list, task: run-tests tells you which are yours to worry about.
Pause while a human thinks
Stopping throws the state away, which is wrong if the agent should continue where it was. Leaving the box running is the pricey option. Pausing is the middle path: per the docs it keeps memory and files, and resuming brings the box back where it stopped.
On Sandscale a box counts against your hours while it runs, busy or idle. A paused one doesn't count. The pricing page's own example: a 2 vCPU box that runs for an hour and is paused the rest of the day uses 2 vCPU-hours.
await sandscale.sandboxes.pause(box.id, { region: box.region }); // ...a person approves, hours later... await sandscale.sandboxes.resume(box.id, { region: box.region }); The sandbox object also has a suspend field telling you whether pause and resume are possible right now. Check it instead of catching the error.
For a wait of days, I'd rather save a snapshot than leave a box paused that long. Check your plan's limits and the docs before relying on either.
Snapshot to branch
A snapshot saves the sandbox's /workspace, and create({ snapshotId }) starts new boxes from it. So the long install happens once and the next ten runs start from the result.
Pause means "keep everything as it is, I'm coming back". Snapshot means "remember these files, I want fresh boxes from here". A snapshot doesn't keep a running process, so a half-open dev server is a pause job. Snapshots also use storage, so look at your plan's allowance before taking hundreds.
What I'd do today
Three rules, and nothing more until something hurts:
- Every
creategets a timeout picked for the task. - Whoever creates a box stops it in a
finally. - Anything waiting on a human for more than a few minutes pauses first.
Labels and snapshots come later.
What I'm unsure about: who should own the pause call? The agent knows best when it's waiting. The orchestrator is easier to audit. I lean toward the orchestrator, but I can see the other side. What do you do?
Docs I used: sandboxes and the TypeScript SDK.
原文:https://www.a8q9.cn/en/article/your-agent-sandbox-needs-an-exit-plan-0ef59a.html