Repository navigation
fix(control-plane): roll a sandbox create back when its start record fails - #832
Conversation
POST /v1/sandboxes stores the sandbox (taking a live-sandbox slot and its name), audits it, then appends its `sandbox` start record to the template's chain. An append that threw answered 500 for a sandbox that existed: it counted against the caller's quota, held its name, and had no record on the chain, and the client, which never got the id, could not clean it up; a retry was then refused by the quota or by name_conflict. The create is now undone when that append fails: the record is marked deleted with no retention (the store's own atomic delete, which frees the slot and the name; the record is dropped at once), a `sandbox_create_rolled_back` audit line answers the `sandbox_created` one, and the append's error then answers as before (500 internal_error). A failed append leaves nothing behind, every sandbox keeps its start record, and the client can simply retry. Not undone: a lease the create's first use took (it has its own activate record and expires on its own; a retry uses it). Create does no worker I/O, mints no token and has an empty egress log, so nothing else needs undoing. A rollback whose delete fails too is logged with the id. Closes rossoctl#806 Assisted-By: Claude Code Signed-off-by: Rong Chang <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Summary
Rolls back a sandbox create when its sandbox start record append fails (#806): the record is marked deleted with zero retention via the store's existing atomic markDeleted, freeing the quota slot and name, a sandbox_create_rolled_back audit line is written, and the original append error still answers 500. Verified against origin/main (PR base == main tip 870a695): Redis DELETE script does ZREM live + DEL of the name key + EXPIRE rec 0, and the memory store purges at now >= deletedAt + 0, so both stores free slot and name immediately; ProvenanceStore.append moves the head last (tmp + rename), so a failed append leaves no half-written chain entry. New handler test and store-contract case (memory + real Redis) cover the path; the audit-decision enum in the design doc is updated. No blocking issues.
- nit: the commit subject
control-plane: roll a sandbox create back ...lacks the conventional type prefix used across main (e.g.fix(control-plane): ..., which the PR title already has). Advisory only.
Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (control-plane handlers, sandbox stores, provenance), Tests, Docs
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: passing (17/17 checks green, incl. DCO, CodeQL, lint, check, k8s-kind-e2e)
Takes upstream main at 1a7f41e (19 merges since 032c8f6): the sandbox idle lifetime across docs, control plane, clients and deploy (rossoctl#820, rossoctl#837, rossoctl#842, rossoctl#843), grant redemption (rossoctl#822), the lease-check log line (rossoctl#824), egress fixes (rossoctl#829, rossoctl#834), the mock wait (rossoctl#828), the deadline timer (rossoctl#835), the create rollback (rossoctl#832), multi refusals and the rossoctl#363 leftovers (rossoctl#833, rossoctl#838), the identity near-miss check (rossoctl#827), mocactl login errors (rossoctl#836), the ghostunnel probes (rossoctl#830), refusal-burst deferral (rossoctl#831), a sandbox-worker change (rossoctl#841) and the v0.11 doc truth (rossoctl#821). Conflicts (one): - deploy/sbx/README.md, the "Optional inputs" table. Upstream (rossoctl#843) rewrote the SBX_LIFETIME_DAYS row (the default idle lifetime, at most SBX_MAX_LIFETIME_DAYS) and added the SBX_MAX_LIFETIME_DAYS and SBX_LOWER_WORKSPACE_IDLE rows, which widened the table; the fork had added an SBX_GATE_RECORD_SCHEMA row at its end. Resolved by taking upstream's table whole and appending the fork's SBX_GATE_RECORD_SCHEMA row after SBX_PROFILE, then re-padding the table with prettier. The fork's env-file example line for SBX_GATE_RECORD_SCHEMA merged cleanly. Merged without markers, checked: setup.sh keeps the fork's SBX_GATE_RECORD_SCHEMA validation and its sbx-settings write (so it stays in the settings hash) beside upstream's SH_SANDBOX_MAX_LIFETIME_SECONDS; both ocp-sbx and ocp-combined patch-control-plane.yaml keep the optional SH_GATE_RECORD_SCHEMA from sbx-settings beside the new optional SH_SANDBOX_MAX_LIFETIME_SECONDS; setup.test.sh keeps the fork's cases beside upstream's. The fork's RUNBOOK, README and template-onboarding docs say nothing about sandbox lifetime, refusal bursts, login errors or the create rollback, so none needed a correction; GUIDE.md is unchanged and GUIDE.pdf is not regenerated. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Rong Chang <[email protected]>
Closes #806
The issue asked the provenance owners to choose between making the
sandboxstart record best effort (option 1), rolling the create back (option 2), or appending first (option 3). This PR takes option 2, rollback. If the maintainers would rather have best effort, the change is small: replace the rollback in thecatchwith a logged warning and answer 201.What changed and why
createSandbox(packages/control-plane/src/sandbox-handlers.ts) stores the sandbox, which takes a live-sandbox slot and its name, then writes thesandbox_createdaudit line, then appends thesandboxstart record (sandboxCreated). Before this change, an append that threw answered 500 for a sandbox that existed. It counted against the caller's quota, held its name, and had no record on its template's chain. The client never got the id, so it could not clean the sandbox up, and a retry was refused byquota_exceededorname_conflict.rollBackCreate) before the error answers:markDeleted(id, now, 0). That frees the quota slot and the name at once. Redis drops the record hash right away (EXPIRE 0), the memory store drops it on its next purge, andGET /v1/sandboxesno longer lists it. TheSandboxStoreinterface does not change.sandbox_create_rolled_backaudit line follows thesandbox_createdline, so the audit stream shows the record's whole life. The new decision is added to the enum indocs/specs/2026-10-20-audit-log-mirror-design.md. Prettier re-padded that table, which is why the doc diff is larger than one word.internal_error. The other provenance writers (the template catalog, the policy store, the members store) answer a failed append the same way, and they also leave nothing written.requireActive,first_use) is kept. It has its ownactivaterecord, a retry uses it, and it ends when it expires. Undoing it would need another append, on a store that just failed.GET /v1/sandboxesby the owner could see the sandbox. If the owner deletes it in that window, the rollback finds it already ended and does nothing.Tests
New
rolls a create back when its start record cannot be written, so a retry succeeds (#806)inpackages/control-plane/test/sandbox-handlers.test.ts. It uses a realProvenanceStorewhoseappendthrows, with one live sandbox allowed per user and a named sandbox. AfterPOST /v1/sandboxesit checks:CpError, so the server answers 500internal_error);GET /v1/sandboxeslists nothing;sandbox_createdthensandbox_create_rolled_back;With the append working again, a retry with the same name answers 201, the count is 1, and the chain holds that sandbox's
createdrecord.AssertionError: expected [ 'sbx-00000000000000000000000001' ] to deeply equal []at theGET /v1/sandboxescheck.New store contract case
drops a record deleted with no retention at once, freeing its slot and its nameintest/helpers/sandbox-store-contract.ts. It runs against both the memory store and a real Redis, and passes on both.@moca/control-plane, full suite: 78 files passed, 1 skipped (vault-store.live.test.ts); 1866 tests passed, 3 skipped.tsc --noEmitis clean.Out of scope
expiredend records (recordExpiries, called fromGETon the janitor's sandbox route) also append without a catch. A failure there answers the janitor's read with 500. No sandbox is left in a wrong state: expiry is computed, not stored, and the chain itself marks which expiries are already recorded, so the next sweep writes the missing record again. The cost is different: while appends keep failing, the janitor gets no list of workspaces to clean up. Whether that read should stay strict or record expiries best effort (asrolesNowrecords lapsed grants) is a separate choice, so this PR leaves it alone.Every commit is signed off (DCO).
Assisted-By: Claude Code