|
| 1 | +# P3 Spike: Cloudflare Sandbox as L3 Remote Execution Tier |
| 2 | + |
| 3 | +## Scope |
| 4 | + |
| 5 | +This spike answers one question: |
| 6 | + |
| 7 | +**Can Cloudflare Sandboxes serve as a viable `L3` remote execution tier for StackMemory agents?** |
| 8 | + |
| 9 | +For this spike, `L3` means: |
| 10 | +- remote, isolated, browser-addressable execution |
| 11 | +- persistent enough to survive idle cycles via mounted storage or backups |
| 12 | +- suitable for repo-oriented agent work |
| 13 | + |
| 14 | +It does **not** mean: |
| 15 | +- replacing Postgres/SQLite as StackMemory's primary metadata database |
| 16 | +- replacing the hosted memory runtime |
| 17 | + |
| 18 | +## Why this is worth testing now |
| 19 | + |
| 20 | +Cloudflare's current platform shape materially changed: |
| 21 | +- Sandboxes are now generally available. |
| 22 | +- The SDK exposes commands, files, PTY terminals, Git workflows, file watching, mounted buckets, and backup/restore. |
| 23 | +- The platform is explicitly targeted at agentic workloads, CI/CD, and interactive development environments. |
| 24 | + |
| 25 | +That changes the answer from "interesting maybe later" to "build a real spike now". |
| 26 | + |
| 27 | +## Hypothesis |
| 28 | + |
| 29 | +Cloudflare Sandboxes are viable for StackMemory if all of the following are true: |
| 30 | + |
| 31 | +1. We can map one sandbox ID to one project/session cleanly. |
| 32 | +2. Browser terminal UX is good enough through WebSockets. |
| 33 | +3. Repo bootstrap plus restore beats repeated cold setup. |
| 34 | +4. Mounted storage and backups are good enough for persistence. |
| 35 | +5. Control-plane logic can stay in Workers without leaking secrets into the sandbox. |
| 36 | + |
| 37 | +## What the spike package implements |
| 38 | + |
| 39 | +See `packages/cloudflare-sandbox-spike/`. |
| 40 | + |
| 41 | +It provides: |
| 42 | +- Worker entrypoint |
| 43 | +- Sandbox binding |
| 44 | +- container image |
| 45 | +- websocket terminal route |
| 46 | +- Git checkout bootstrap route |
| 47 | +- command execution route |
| 48 | +- file read/write routes |
| 49 | +- mounted R2 route |
| 50 | +- backup/restore routes |
| 51 | + |
| 52 | +This is enough to validate the platform shape without dragging in full StackMemory runtime complexity. |
| 53 | + |
| 54 | +## Viability criteria |
| 55 | + |
| 56 | +The spike is a `GO` if we can demonstrate: |
| 57 | + |
| 58 | +1. **Bootstrapping** |
| 59 | + - clone a repo into `/workspace/repo` |
| 60 | + - run install/test/build commands |
| 61 | + |
| 62 | +2. **Interactive work** |
| 63 | + - connect browser terminal over websocket |
| 64 | + - keep shell state in a session |
| 65 | + |
| 66 | +3. **Persistence** |
| 67 | + - mount an R2 bucket into the sandbox filesystem |
| 68 | + - persist files across sandbox destruction |
| 69 | + - create and restore a workspace backup |
| 70 | + |
| 71 | +4. **Security model** |
| 72 | + - control secrets from the Worker side |
| 73 | + - avoid embedding live credentials directly into user-controlled code |
| 74 | + |
| 75 | +5. **Operational clarity** |
| 76 | + - one sandbox ID maps cleanly to project/session identity |
| 77 | + - cleanup lifecycle is explicit |
| 78 | + - failure modes are understandable |
| 79 | + |
| 80 | +## Non-goals |
| 81 | + |
| 82 | +- Multi-tenant billing |
| 83 | +- full StackMemory API integration |
| 84 | +- hosted retrieval/indexing layer |
| 85 | +- production authn/authz |
| 86 | +- scheduler and queue integration |
| 87 | +- fleet management |
| 88 | + |
| 89 | +## Current platform reading |
| 90 | + |
| 91 | +### What looks strong |
| 92 | + |
| 93 | +Cloudflare now has the pieces we actually need: |
| 94 | +- Sandboxes are GA and explicitly positioned for untrusted code execution and agent workflows. |
| 95 | +- Sandbox instances are Durable Objects under the hood, which gives a natural coordination identity. |
| 96 | +- Git operations are first-class. |
| 97 | +- PTY terminals are first-class. |
| 98 | +- Mounted S3-compatible buckets are first-class. |
| 99 | +- Backup/restore is first-class and specifically designed to avoid repeating clone/install/setup costs. |
| 100 | + |
| 101 | +### What still looks limiting |
| 102 | + |
| 103 | +- Sandbox containers are still ephemeral unless you add mounted storage or backups. |
| 104 | +- Backup/restore is production-only right now, not `wrangler dev`. |
| 105 | +- Preview URLs need custom domain setup; `.workers.dev` is not enough for exposed-port workflows. |
| 106 | +- This is an execution platform, not a relational memory/query platform. |
| 107 | + |
| 108 | +## Recommended production shape if this spike passes |
| 109 | + |
| 110 | +### Keep |
| 111 | +- StackMemory hosted runtime for: |
| 112 | + - projects |
| 113 | + - runs |
| 114 | + - frames |
| 115 | + - anchors |
| 116 | + - retrieval |
| 117 | + - search |
| 118 | + - orchestration |
| 119 | + |
| 120 | +### Add |
| 121 | +- Cloudflare Sandbox as: |
| 122 | + - per-project execution runtime |
| 123 | + - browser terminal endpoint |
| 124 | + - disposable worker session host |
| 125 | + |
| 126 | +### Store |
| 127 | +- R2 for: |
| 128 | + - mounted project persistence |
| 129 | + - backup archives |
| 130 | + - large artifacts and logs |
| 131 | + |
| 132 | +### Coordinate |
| 133 | +- Durable Objects for: |
| 134 | + - sandbox identity |
| 135 | + - session-to-sandbox routing |
| 136 | + - short-lived coordination state |
| 137 | + |
| 138 | +## Recommended production shape if this spike fails |
| 139 | + |
| 140 | +If terminal UX, bootstrap/restore latency, or operational complexity are poor, do not force it. |
| 141 | + |
| 142 | +Fallback: |
| 143 | +- keep execution local or VM-based |
| 144 | +- use Cloudflare only for edge API/control-plane pieces |
| 145 | +- do not contort StackMemory around a weak remote execution substrate |
| 146 | + |
| 147 | +## First decision after the spike |
| 148 | + |
| 149 | +At the end of P3, we should be able to say one of these clearly: |
| 150 | + |
| 151 | +1. **GO**: Cloudflare Sandbox is good enough for a real remote execution track. |
| 152 | +2. **PARTIAL GO**: good for ephemeral code execution, not good enough for long-lived interactive project sessions. |
| 153 | +3. **NO GO**: useful technology, wrong fit for StackMemory's execution model. |
| 154 | + |
| 155 | +## Deliverables |
| 156 | + |
| 157 | +- `packages/cloudflare-sandbox-spike/` |
| 158 | +- this decision note |
| 159 | +- a short benchmark/result note after hands-on validation |
0 commit comments