Create the session ID in your backend. Pass it to the worker as
SESSION_ID. Keep the repository
ID, branch name, and current commit SHA in the same session record.Create the session branch
Create an ephemeral branch from the repository’s normal default branch. One session branch holds every state of one session. Run this code in a trusted backend service. SetPIERRE_PRIVATE_KEY to the private key in PEM
format from the Code Storage dashboard. Do not give this key to a sandbox.
TypeScript
baseRef can also be a full commit SHA. Use a full SHA when the session must start from an exact
state.
Save each state
Add one commit for each state that another process must read or restore. Each commit inherits unchanged files from the prior state, so send only changed or deleted paths.TypeScript
expectedHeadSha lets the write succeed only if the branch still points to headSha. If another
writer changed the branch, the write fails with the precondition_failed reason. Stop the old
writer and reload the session record.
Store headSha in the backend session record after each successful write. See
createCommit() for file and stream inputs.
Exchange commit pointers
Pass a pointer instead of file contents. We recommend a repository ID, a commit SHA, and an optional path.TypeScript
getFileStream() for files and
getCommitDiff() for changes.
Confine a sandbox to its own session
A sandbox never holds your private key. Give each sandbox a credential with a short TTL. UserefPolicies to confine that credential to the session’s own ref. The sandbox can then fast-forward
its own session branch only. It cannot change a normal branch or another session branch.
A backend service that writes states through the API needs none of this. It signs each request with
the private key and never hands a credential out.
Connect a Sandbox shows the URLs and
the Git setup. See Ephemeral Namespace for namespace details and
Branch Protection for refPolicies rules.
Next steps
- Resume Sandbox Work restores the latest state after a sandbox stops.
- Run Parallel Attempts creates several isolated branches from one state.
- Show Live Diffs renders the state as it changes.