Skip to main content

What You’ll Learn

  • Creating two sandboxes side by side
  • Writing a file in Sandbox A with sbx.files.write()
  • Confirming the file does NOT exist in Sandbox B with sbx.files.exists()
  • Reading file content with sbx.files.read() to verify contents
  • Writing a file in B and verifying A cannot see it (bidirectional isolation)
  • Proper cleanup of multiple sandboxes with try/finally

Prerequisites

This example is available in Python. TypeScript version coming soon.

Code Walkthrough

Create two sandboxes and keep references to both:
from declaw import Sandbox

sbx_a = Sandbox.create(template="base", timeout=300)
print(f"Sandbox A created: {sbx_a.sandbox_id}")

sbx_b = Sandbox.create(template="base", timeout=300)
print(f"Sandbox B created: {sbx_b.sandbox_id}")
Write a secret file in Sandbox A and confirm it exists there:
sbx_a.files.write("/tmp/secret.txt", "sandbox-a-secret")

exists_a = sbx_a.files.exists("/tmp/secret.txt")
assert exists_a, "File should exist in Sandbox A"

content_a = sbx_a.files.read("/tmp/secret.txt")
assert content_a == "sandbox-a-secret", "Content should match what was written"
Verify the same path does not exist in Sandbox B:
exists_b = sbx_b.files.exists("/tmp/secret.txt")
assert not exists_b, "File should NOT exist in Sandbox B"
Confirm isolation is bidirectional — write in B and check A cannot see it:
sbx_b.files.write("/tmp/b-only.txt", "sandbox-b-data")
exists_in_a = sbx_a.files.exists("/tmp/b-only.txt")
assert not exists_in_a, "File written in B should NOT exist in A"
Confirm the two sandboxes have different IDs:
assert sbx_a.sandbox_id != sbx_b.sandbox_id, "Sandbox IDs should differ"
Always clean up all sandboxes in finally:
try:
    # ... isolation tests ...
finally:
    sbx_a.kill()
    print(f"  Sandbox A ({sbx_a.sandbox_id}) killed.")
    sbx_b.kill()
    print(f"  Sandbox B ({sbx_b.sandbox_id}) killed.")

Expected Output

==================================================
Declaw Multi-Sandbox Isolation Example
==================================================

--- Creating Sandbox A ---
Sandbox A created: sbx_abc123

--- Creating Sandbox B ---
Sandbox B created: sbx_def456

--- Writing File in Sandbox A ---
  Wrote '/tmp/secret.txt' with content 'sandbox-a-secret' in Sandbox A.

--- Checking File in Sandbox A ---
  /tmp/secret.txt exists in A: True
  Content in A: sandbox-a-secret

--- Checking File in Sandbox B ---
  /tmp/secret.txt exists in B: False

--- Comparing Sandbox IDs ---
  Sandbox A ID: sbx_abc123
  Sandbox B ID: sbx_def456

--- Writing File in Sandbox B ---
  /tmp/b-only.txt exists in A: False

--- Isolation Verified ---
  Filesystem is fully isolated between sandboxes.

--- Cleaning Up ---
  Sandbox A (sbx_abc123) killed.
  Sandbox B (sbx_def456) killed.

==================================================
Done!
==================================================