Skip to main content
Use this pattern when a person or an agent must continue work in a new sandbox. The new sandbox restores the last commit and continues on the same ephemeral branch. This guide uses the branch layout from Store Session State.

Load the last state

Store the repository ID, branch name, and current commit SHA in your backend. Update this record after each successful state write. When your backend starts a replacement worker, pass the stored values as environment variables. PIERRE_PRIVATE_KEY holds the private key for your Code Storage organization. Keep this key in trusted server code.
TypeScript
The current SDK has no direct branch lookup method. If your backend loses the tip SHA, call listBranches() and follow nextCursor until you find the branch.

Restore the files

Use an archive when the sandbox only needs a file tree.
TypeScript
Use Git when the sandbox must create commits. Use a normal read URL to fetch a full SHA. The SHA must remain reachable from an ephemeral ref.
TypeScript
Pass readUrl, sessionBranch, and tipSha to the new sandbox as READ_URL, SESSION_BRANCH, and TIP_SHA.
Use the full 40-character SHA that the API returns. Do not use an abbreviated SHA or a revision expression. See getArchiveStream() for archive filters. See Connect a Sandbox for the separate push URL.

Reject stale writes

Write the next state only when the branch still has the tip that you restored.
TypeScript
If another sandbox wrote first, this call fails with the precondition_failed reason. Stop the old sandbox. Do not retry until your backend assigns a new owner. For Git pushes, keep no-force-push on the session branch. An old checkout cannot replace the new commits with a non-fast-forward push.

Record the handoff

Store nextState.commitSha as the current tip. Record which person, agent, and sandbox own the session. Use short URL lifetimes. Mint a new write URL for the replacement sandbox. Do not reuse the old sandbox URL.