Part 3: A Copy per User
Start a user’s sandbox from Warm
Section titled “Start a user’s sandbox from Warm” const userSandbox = Effect.fn("userSandbox")(function* (user: string) { const [existing] = yield* sandboxes.list({ metadata: { user }, state: ["running", "paused"] }); return existing !== undefined ? yield* sandboxes.connect(existing.sandboxID, { timeout: 600 }) : yield* sandboxes.create({ metadata: { user }, timeout: 600, autoPause: true }); });A user’s first request starts their sandbox from Warm, with the repository already cloned. Its metadata finds it again on their next request.
Their changes stay in their copy
Section titled “Their changes stay in their copy” const match = /^\/users\/([a-z0-9-]+)\/files\/(.+)$/.exec(url.pathname); if (match !== null) { const sandbox = yield* userSandbox(match[1]!); if (request.method === "PUT") yield* sandbox.files.write(`/home/user/repo/${match[2]}`, yield* request.text); const content = yield* sandbox.files.readText(`/home/user/repo/${match[2]}`); yield* sandbox.pause(); return yield* HttpServerResponse.json({ content }); }Each user edits their own copy of the repository. Warm never changes, so the next sandbox started from it doesn’t see their edits.
Pause between requests
Section titled “Pause between requests”pause keeps the sandbox’s memory, files and processes, and stops billing it. connect resumes it where it stopped on the user’s next request. autoPause pauses it too when its timeout runs out, instead of killing it.
pnpm alchemy deploycurl -X PUT "$URL/users/ana/files/notes.md" -d 'hello'curl "$URL/users/ana/files/notes.md"bun alchemy deploycurl -X PUT "$URL/users/ana/files/notes.md" -d 'hello'curl "$URL/users/ana/files/notes.md"npx alchemy deploycurl -X PUT "$URL/users/ana/files/notes.md" -d 'hello'curl "$URL/users/ana/files/notes.md"yarn alchemy deploycurl -X PUT "$URL/users/ana/files/notes.md" -d 'hello'curl "$URL/users/ana/files/notes.md"Part 4 changes the template.