Skip to content

feat(platform): delete sandbox workspaces whose owner is gone or unused - #4028

Merged
yannickmonney merged 5 commits into
mainfrom
feat/sandbox-workspace-cleanup
Oct 1, 2026
Merged

yannickmonney merged 5 commits into
mainfrom
feat/sandbox-workspace-cleanup

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Summary

A sandbox workspace (a project agent's persistent /agent directory) outlived everything. The idle reaper only stops a session and keeps its files, and the only thing that ever deleted a workspace was Destroy on Settings → Sandboxes. Deleting an agent, a project, a member or a whole organization left the workspaces on the host for good. For an organization, it also left its build and package caches and the gateway keys minted for its sessions. An erasure left the person's files in their agent workspaces, and nothing bounded how long an unused workspace of a live agent stays.

This PR deletes a workspace when its owner is gone or nobody has used it for the organization's window. It never deletes on a guess.

One retire path (domains/sandbox/workspace-cleanup.ts)

Each retirement runs under the session's lifecycle lock:

  1. It takes the decision again under the organization's admission lock, which every resume takes too.
  2. It ends the live incarnation: rows go expired and unpinned (on the spawner too), and gateway keys are revoked.
  3. The spawner deletes the workspace.
  4. It settles only the rows it claimed, and audits sandbox_workspace.deleted with the reason.

A turn that resumed the workspace before the claim keeps it. A turn after the claim starts a fresh incarnation (its create waits for the destroy), and that incarnation stays live. Busy, offline-device or failed spawner calls leave the workspace for the next attempt.

When a workspace goes

  • Its owner is deleted. A job is queued in the deleting transaction:

    • deleting a project agent or its project;
    • removing a member through any door (the shared membership cascade: members, SCIM, Better Auth's plugin door);
    • deleting an organization: revoke its gateway keys first, destroy its workspaces, then release its devices and tear it down on the spawner.

    A user's GDPR erasure deletes their member workspaces in the cascade itself. It is strict: if one could not be deleted, the receipt reads partial.

  • It is unused for the organization's window. A new governance policy sandbox_workspaces sets this: deleteUnused (on by default) and unusedDays (30 by default, 1–3650). Admins edit it in a new Workspace cleanup section on Settings → Sandboxes.

    • Pinned workspaces are kept, and an unpin starts the window over.
    • Nothing is deleted for being unused until a full window has passed since the rule took effect. That covers the upgrade itself, turning the rule on, and a shorter window. The start is recorded in migration 0145, both when the policy is saved and by the sweep, so this release never mass-deletes on arrival.
  • The hourly sweep finds it (sandbox.workspace_gc, minute 21). The sweep is the backstop for all of the above. That includes a departed member's workspaces, which it finds through the runs that person started; it never touches a member's or a schedule's.

    • It also inventories what the spawner physically holds: ended runs' leftovers, cut-off renders, and a deleted organization's leftovers.
    • A workspace no row names goes only when it is attributable to this deployment: a render's, or one whose recorded organization this deployment holds (unless its project agent still exists) or deleted. A spawner, a Kubernetes namespace or a shared Docker daemon can hold another deployment's workspaces and caches. Those, and workspaces with no recorded organization, are left alone and counted.
    • Each sweep deletes at most 50 workspaces and bounds its attempts per pass. It walks in a different order each hour, so workspaces that keep refusing never hold the rest back.
  • A legal hold keeps everything it covers on every path. An organization hold keeps all of its workspaces; a custodian hold keeps that person's own.

Sandbox service

  • GET /v1/workspaces lists every workspace the spawner holds and the organizations holding build helpers or package caches:
    • Docker: directories in both layouts. A stopped workspace names its organization through <root>/.owners/<id>.org, written at create beside the pin markers and outside the workspace.
    • Kubernetes: PVCs joined with Pods, now annotated with their organization.
  • DELETE /v1/sessions/:id?if_stopped=1 deletes only while no compute runs under the id and no create is in flight. if_idle=1 rides along for older spawners and devices. A destroy now reports destroyed: true for a stopped session's workspace alone.
  • Destroys of one id run one after another. A create waits up to 2 minutes for a destroy of its id, then answers 429 with retry-after.
  • DELETE /v1/organizations/:id removes what a deleted organization left: its sessions, its stopped workspaces, its build helpers (by id, ownership-checked, egress detached before the network goes) with their volumes, and its pip/npm/bun caches. It is idempotent.
  • Kubernetes RBAC: the spawner Role now needs list on persistentvolumeclaims (docs updated). Without it, the inventory answers 503 and only the orphan pass is skipped.

UI

  • Settings → Sandboxes gains the Workspace cleanup section (admins only).
  • Each hibernated agent workspace shows Deleted on {date} unless used again.
  • The audit log labels the new action, and the DSAR receipt names the Sandbox workspaces category.

Not covered

  • Gateway keys after a long gateway outage. A deleted organization's gateway keys are revoked by its retirement job, which retries for about 17 hours. A gateway that stays down past that leaves them. Before this change they were never revoked.
  • Old Docker directories and Kubernetes claims. Ones created before this change carry no organization record. A deleted organization's are reached through the session ids its rows named, but not once those rows are gone.
  • Migration number. feat(platform): hand a chat's work to a project agent in one step #4023 holds 0144; this PR uses 0145. The runner records migrations by name, so either merge order applies both.

Checklist

  • bun run check passes: format, lint, typecheck and tests were run per workspace (see the test plan).
  • bun run lint:sast passes: Opengrep over the 53 changed code files, 0 findings.
  • Translations updated in messages/{en,de,fr}.yml.
  • Docs updated in docs/{en,de,fr}/ (Sandboxes, project agents, tasks, members and roles, data subject requests, Kubernetes install), with a new pipeline-captured screenshot.
  • README.md / README.de.md / README.fr.md: N/A. The root READMEs don't describe sandbox internals; services/sandbox/README.md and its docs/ are updated.

Test plan

  • services/sandbox: bun test 630 pass. New tests cover:

    • the inventory and its organization records;
    • org teardown against a fake Docker CLI, including stopped workspaces;
    • the Kubernetes PVC inventory;
    • if_stopped;
    • create/destroy serialization and its bound.

    tsc and oxlint are clean.

  • Platform unit tests and the real-Postgres lane checkWorkspaceCleanup. The lane has 15 checks:

    • the rule-start guard and unused deletion;
    • a deleted agent's workspace, including pinned and warm;
    • the resume race;
    • a fresh incarnation during the deletion surviving it;
    • the page's dates;
    • an unpin restarting the window;
    • member removal by the job and by the sweep (never a member's or a schedule's);
    • a held member's workspace surviving its agent's deletion;
    • erasure while a turn runs;
    • an org hold until released;
    • inventory leftovers;
    • attribution of workspaces no row names (ours, foreign, deleted);
    • a workspace no row names deleted when stopped;
    • org-deletion capture, including healed workspaces and the slice barrier.

    Two of these checks fail against the code they replaced: the held-member check and the fresh-incarnation check.

  • Full suites: server + pii, the UI suite, and the docs and shared-package suites pass. tsc, oxlint, knip, oxfmt, lint:links, lint:manual and the integration-scope guard are clean.

  • Observed for real against a spawner running this branch on Docker:

    • create, then a cleanup destroy refused while the session runs;
    • after the idle stop, the directory is kept, and a cleanup destroy then deletes it;
    • org teardown removes a running session, its directory and its three cache volumes;
    • the platform sweep over a real database deletes a warm, pinned workspace of a deleted agent and audits it.
  • Browser: the Sandboxes page in en/de/fr:

    • the section and its validation;
    • per-row dates moving with the window;
    • shortening the window restamps it, a full window from the save;
    • turning the rule off and on starts a fresh window.
  • Scheduled: the dev stack's own worker ran sandbox.workspace_gc at minute 21 and retired a deleted agent's workspace (row destroyed, audited agent_deleted).

The spawner now reports and enforces what the platform's workspace
cleanup decides:

- GET /v1/workspaces lists every workspace the backend holds (Docker
  dirs in both layouts, Kubernetes PVCs) with its last change, whether
  compute runs for it, its pin and its organization, plus the
  organizations holding build helpers or package caches. A stopped
  Docker workspace names its organization in <root>/.owners/<id>.org;
  a PVC in its annotation.
- DELETE /v1/sessions/:id?if_stopped=1 deletes only while no compute
  runs under the id and no create is in flight. Destroys of one id are
  serialized; a create waits up to two minutes for one, then answers
  429. A destroy answers true for a stopped session's workspace alone.
- DELETE /v1/organizations/:id removes a deleted organization's
  sessions, stopped workspaces, build helpers (by id, ownership-checked,
  egress detached first) with their volumes, and package caches.

Kubernetes needs `list` on persistentvolumeclaims for the inventory.
A workspace outlived its owner: only an explicit Destroy ever deleted
one. Now one retire path deletes it: under the session's lifecycle
lock it decides again under the organization's admission lock, ends the
live incarnation, has the spawner delete it, settles only the rows it
claimed and audits `sandbox_workspace.deleted` with the reason.

- Owner deletion queues the deletion in the deleting transaction: a
  project agent or project, a member removed through any door, a whole
  organization (keys first, then workspaces, then its devices and the
  spawner teardown once every slice finished). An erasure deletes the
  person's member workspaces in its cascade, strictly.
- The new `sandbox_workspaces` governance policy deletes workspaces
  unused for its window (30 days by default). Nothing goes for disuse
  until a full window has passed since the rule took effect
  (migration 0145), or since an unpin.
- An hourly sweep backs all of it up, finds what the spawner holds that
  no live row owns, and deletes a workspace no row names only when it
  is attributable to this deployment. Legal holds keep what they cover.
Settings > Sandboxes gains a Workspace cleanup section for Owners and
Admins: delete unused workspaces (on by default) and the days without
use (1 to 3650, 30 by default), saved through the header's Save and
Discard. Each hibernated agent workspace shows the day it is deleted
unless used again. The audit log labels the deletion, and the erasure
receipt names the sandbox workspaces it counts. English, German and
French.
Settings boxes for a deleted agent's workspace, the cleanup section, the
dated rows, a removed member's workspaces and an organization's
deletion; a governance box for an erasure while a turn runs. The
automation reference names what the unit, real-Postgres and spawner
suites own.
The Sandboxes page documents the cleanup setting with its screenshot,
the dated rows, and why a workspace disappeared: its agent, project,
member or organization was removed, its owner's data was erased, or
nobody used it for the set days. Project agents, tasks, members and
data subject requests say what each removal deletes; the Kubernetes
install lists the claim permission the cleanup needs.
@yannickmonney
yannickmonney force-pushed the feat/sandbox-workspace-cleanup branch from e70ee46 to c559d1a Compare October 1, 2026 06:02
@yannickmonney
yannickmonney merged commit a703a95 into main Oct 1, 2026
92 of 93 checks passed
@yannickmonney
yannickmonney deleted the feat/sandbox-workspace-cleanup branch October 1, 2026 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant