Skip to content

fix(control-plane): roll a sandbox create back when its start record fails - #832

Merged
webchang merged 1 commit into
rossoctl:mainfrom
webchang:pr/i806-rollback-create
Nov 16, 2026
Merged

webchang merged 1 commit into
rossoctl:mainfrom
webchang:pr/i806-rollback-create

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #806

The issue asked the provenance owners to choose between making the sandbox start 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 the catch with 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 the sandbox_created audit line, then appends the sandbox start 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 by quota_exceeded or name_conflict.
  • Now a failed append undoes the create (rollBackCreate) before the error answers:
    • The record is marked deleted with no retention, using the store's own atomic 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, and GET /v1/sandboxes no longer lists it. The SandboxStore interface does not change.
    • A sandbox_create_rolled_back audit line follows the sandbox_created line, so the audit stream shows the record's whole life. The new decision is added to the enum in docs/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.
    • The append's own error then propagates unchanged: a 500 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.
  • Why rollback: it keeps the invariant that every sandbox has a start record (template lifecycle §7.1), so the verifier needs no exception. The client also gets an honest answer it can retry. Best effort would allow sandboxes with no chain record. Append first would leave a start record for a sandbox that a quota or name refusal never stored, unless it also wrote an end record.
  • What is not undone, and why:
    • A lease that the create's first use took (requireActive, first_use) is kept. It has its own activate record, a retry uses it, and it ends when it expires. Undoing it would need another append, on a store that just failed.
    • Create does no worker I/O, mints no token, and has an empty egress log, so none of those need undoing.
    • If the rollback's delete also fails (for example, the store is down), the failure is logged with the sandbox id. The sandbox then stays, as it did before this change, until its owner deletes it or it expires. The client still gets the 500.
    • For the short time between the store write and the rollback, a concurrent GET /v1/sandboxes by 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) in packages/control-plane/test/sandbox-handlers.test.ts. It uses a real ProvenanceStore whose append throws, with one live sandbox allowed per user and a named sandbox. After POST /v1/sandboxes it checks:

    • the append's error is thrown (not a CpError, so the server answers 500 internal_error);
    • GET /v1/sandboxes lists nothing;
    • the caller's live-sandbox count is still 0;
    • the audit stream holds sandbox_created then sandbox_create_rolled_back;
    • the chain is empty.

    With the append working again, a retry with the same name answers 201, the count is 1, and the chain holds that sandbox's created record.

    • Before the fix (fix reverted): the test fails with AssertionError: expected [ 'sbx-00000000000000000000000001' ] to deeply equal [] at the GET /v1/sandboxes check.
    • With the fix: it passes.
  • New store contract case drops a record deleted with no retention at once, freeing its slot and its name in test/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 --noEmit is clean.

Out of scope

  • The janitor's expired end records (recordExpiries, called from GET on 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 (as rolesNow records lapsed grants) is a separate choice, so this PR leaves it alone.

Every commit is signed off (DCO).

Assisted-By: Claude Code

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]>
@webchang
webchang requested a review from pdettori as a code owner November 16, 2026 20:33
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 16, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@webchang
webchang merged commit 9fe44e5 into rossoctl:main Nov 16, 2026
17 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 17, 2026
webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

control-plane: POST /v1/sandboxes answers 500 for a sandbox that exists when its provenance append fails

3 participants