III · Tool Use & EnvironmentEmerging

Forkable Agent Sandbox

also known as Branch-Context Exploration, Sandbox Checkpoint/Rollback, Copy-on-Write Agent Environment

Turn the agent's whole execution environment, including filesystem, memory and running processes, into a versioned object that can be snapshotted, forked into isolated branches, rolled back and selectively committed.

This pattern helps complete certain larger patterns —

  • used-byLanguage Agent Tree Search·Lift the agent loop into a search tree with a learned value function and backtracking.
  • used-byAdaptive Branching Tree Search·At each node of an inference-time search tree, use Thompson sampling to decide whether to deepen an existing answer or branch a fresh attempt, optionally choosing per-node which underlying LLM to invoke.

Context

An agent works inside a stateful environment: a container with a seeded database, a package tree that took minutes to install, a running development server, a logged-in browser or desktop session. Search strategies the agent depends on — tree search with backtracking, best-of-N sampling, reinforcement-learning rollouts, speculative execution of a risky command — all assume that a step can be tried and then unmade. Model outputs are trivial to discard because sampling is stateless, but the environment those outputs act on is not.

Problem

Real environments accumulate side effects that plain re-execution cannot undo. A migration has already altered the schema, a process has already written a lock file, a form has already been submitted in the browser session. Backtracking therefore means rebuilding the environment from scratch and replaying every earlier action, which is slow and not always faithful. Full-state duplication is the obvious alternative, but copying an entire sandbox costs hundreds of milliseconds to seconds per operation, which is far too slow to sit in the inner loop of a deep search or a large fan-out. So the agent either explores one path timidly or pays for a fresh environment per branch.

Forces

  • Search, best-of-N and rollout training all need cheap state restoration, while the environments worth searching in are exactly the ones that are expensive to rebuild.
  • Undo by re-execution is only faithful when every action is deterministic and reversible; a single external write, timestamp or random seed breaks the replay.
  • Full-state duplication is simple and correct but costs hundreds of milliseconds to seconds per checkpoint, which bottlenecks deep search and large-scale fan-outs; copy-on-write deltas cut that to milliseconds at the cost of a shared-page dependency between parent and branch.
  • Isolation and branching pull the same lever in opposite directions: containment keeps a bad path from damaging user state, while forking multiplies the number of live copies of that state that must be tracked, resource-limited and eventually reclaimed.
  • Anything a branch does outside the snapshot boundary — a payment, an email, an external API write — cannot be rolled back, so the versioning abstraction is exact inside the box and a lie outside it.

Example

An agent is fixing a failing test inside a container where a database has been seeded and a development server is already running. It wants to try three different repairs, but the first one runs a schema migration that the second one would need undone. Rather than rebuild the container three times, it snapshots the running sandbox, forks three branches from that snapshot, and keeps only the branch whose test suite goes green. The other two are discarded, and the migration they ran disappears with them.

Diagram

Solution

Therefore:

Give the sandbox a lifecycle rather than only a lifetime. A snapshot captures the complete machine state — filesystem, memory pages, process groups, and where it applies the GUI or browser session — as a restorable object. Forking that snapshot creates N branch contexts, each with an independent view of the filesystem and its own process group, sharing unmodified pages copy-on-write so a fork costs a delta rather than a full copy; measured implementations land in the low tens of milliseconds for a checkpoint and single-digit milliseconds for a rollback, which is what makes branching affordable inside a search loop. Each branch runs to a verdict: it commits, promoting its changes back into the parent, or it aborts and its state is discarded whole. When several siblings are exploring the same subproblem, the first successful commit wins and the runtime invalidates the rest, so no merge conflict has to be resolved by the model. Commit can be selective, promoting a chosen subset of changes rather than the whole branch, and contexts nest so a branch can itself fork for a sub-decision. Actions with effects outside the snapshot boundary are routed through a separate gate, because no rollback can retract them.

What this pattern forbids. Exploration may only happen inside a forked branch; a branch cannot write to the parent environment before it commits, at most one sibling commit is accepted and the rest must be aborted rather than merged, and actions whose effects escape the snapshot boundary must not be issued speculatively from inside a branch.

The smaller patterns that complete this one —

  • usesSandbox Isolation★★Run agent-emitted code or actions in a contained environment with restricted filesystem, network, and process privileges.

And the patterns that stand alongside it, or against it —

  • alternative-toDurable Workflow SnapshotCapture workflow execution state as a snapshot in a pluggable storage provider so a paused run can resume across deployments, process restarts, and host crashes.
  • alternative-toReplay / Time-Travel★★Re-run a past agent trace from any step with modified inputs/prompts/tools to debug or branch.
  • complementsShadow WorkspaceMirror the workspace into an isolated, version-controlled shadow where the agent makes and reverts edits, surfacing diffs for review and promoting only accepted changes to the real tree.
  • complementsSpeculative Agentic Actions·Predict the tool calls the agent is most likely to issue next and execute them preemptively on the current turn, then keep the results that the confirmed trajectory needs and discard the rest.
  • complementsClone Fan-Out Research·Spawn 100 or more identical, full-capability agent instances in parallel — each a complete general agent rather than a role-specialised worker — and aggregate their independent outputs into a single answer.
  • complementsSubagent IsolationRun subagents in isolated workspaces so their writes do not collide and parallelism is safe.
  • complementsCompensating Action★★Pair every irreversible-looking agent action with a compensating action that can undo or counteract it.
  • complementsAgent Resumption★★Persist agent execution state so a long-running run survives restarts, deploys, or user disconnects.

Neighbourhood

Click any neighbour to follow the language. Scroll to zoom, drag to pan.