Skip to content

Sessions

Sessions are persistent processes running inside a sandbox. Unlike execStream, which tears down when the connection drops, a session keeps running. Any new client that attaches receives a replay of buffered output and can then interact with the live process.

This makes sessions ideal for interactive shells, long-running agents, and any workflow where continuity across reconnects matters.

const session = await sandbox.createSession({
name: 'shell',
command: 'bash',
workDir: '/workspace',
pty: true,
cols: 120,
rows: 40,
})
console.log(session.id)

On connect, the sandbox replays the in-memory output buffer so the client sees past output immediately. Multiple clients can attach to the same session simultaneously.

const attach = sandbox.attachSession(session.id, {
cols: 120,
rows: 40,
onStdout: chunk => process.stdout.write(chunk),
})
attach.write('echo hello\n')
const exit = await attach.done
console.log(exit.code)

Retrieve buffered output without opening a WebSocket connection.

const log = await sandbox.sessionLog(session.id)
console.log(log.toString())

The sandbox starts the process with either a PTY or a pipe pair. Output is written into a ring buffer in memory and fanned out to all currently attached clients. When a new client attaches, it reads the entire ring buffer before receiving live frames, giving a seamless replay experience.

Sessions are tied to the sandbox's runtime. If the sandbox is stopped and restarted, sessions do not persist across the restart.

execStreamSession
Process lifetimeTied to WebSocketIndependent of connection
ReconnectProcess killed on disconnectProcess keeps running
Multi-clientNoYes - concurrent attachment
Output replayNoYes - ring buffer
Use caseOne-shot commandsInteractive shells, agents