Coding agent sessions
Give each session of a coding agent a sandbox of its own, started from a ready snapshot, that sleeps when idle
A coding agent's session needs a computer: the repository, its tools, a shell and somewhere to run the tests. Running each session in its own sandbox keeps sessions apart, lets one run for hours without your laptop, and lets you come back to it later where it stopped.
Make the session image once
Start a sandbox, install the agent's command-line tool and everything your repositories need, then take a snapshot named for it, such as agent-runner-v3. Every session starts from that snapshot, so it starts in seconds, ready to work.
Keep credentials out of the snapshot: every sandbox made from it would carry them. Hand each session the key it needs when it starts (Isolation and secrets).
One sandbox for each session
When a session starts, create a sandbox from the snapshot, labelled with the session, and start the agent as a detached command, so it keeps running when your connection drops:
const sandbox = await pod.sandboxes.create(
{
snapshot: RUNNER_SNAPSHOT_ID,
labels: { session: session.id },
end_user: session.userEndUserId,
env: { AGENT_TOKEN: session.agentToken },
},
{ idempotencyKey: `session-${session.id}` }
);
const agent = await sandbox.exec(["agent", "--task-file", "/work/task.md"], {
cwd: "/work",
detached: true,
});- The idempotency key makes the hook safe to run twice: a second start of the same session answers the same sandbox.
end_userlabels the sandbox with your user, so you can cap and read each user's sessions (Your users).- A detached command keeps running with nobody attached. Attach to its stream to watch it or type into it, or read its state to see whether it has finished.
To show the session's terminal in a browser, make a stream ticket on your server and hand the browser only the ticket, never your key.
Idle sessions sleep by themselves
You do not need a job that finds and ends idle sessions. A sandbox sleeps after its idle time, 15 minutes by default, and a sleeping sandbox costs only its disk. Set idle_s when you create it to change that. API calls, an attached terminal, network traffic and CPU use above a floor all count as use, so a working agent keeps its sandbox awake. A long quiet step, such as waiting on a person, can report activity to stay awake.
When the user comes back, wake the sandbox: its files are as they were, and its memory too where the sleep kept it.
When the session is over for good, delete the sandbox. If you want to keep its result, snapshot it first.
Many sessions from one point
To try several approaches to one task, run the session up to the point where they part, then fork it into up to 10 running copies and give each its own instructions.