Repository navigation
feat(platform): delete sandbox workspaces whose owner is gone or unused - #4028
Merged
Merged
Conversation
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
force-pushed
the
feat/sandbox-workspace-cleanup
branch
from
October 1, 2026 06:02
e70ee46 to
c559d1a
Compare
This was referenced Oct 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A sandbox workspace (a project agent's persistent
/agentdirectory) 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:
expiredand unpinned (on the spawner too), and gateway keys are revoked.sandbox_workspace.deletedwith 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:
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_workspacessets this:deleteUnused(on by default) andunusedDays(30 by default, 1–3650). Admins edit it in a new Workspace cleanup section on Settings → Sandboxes.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.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/workspaceslists every workspace the spawner holds and the organizations holding build helpers or package caches:<root>/.owners/<id>.org, written at create beside the pin markers and outside the workspace.DELETE /v1/sessions/:id?if_stopped=1deletes only while no compute runs under the id and no create is in flight.if_idle=1rides along for older spawners and devices. A destroy now reportsdestroyed: truefor a stopped session's workspace alone.retry-after.DELETE /v1/organizations/:idremoves 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.listonpersistentvolumeclaims(docs updated). Without it, the inventory answers 503 and only the orphan pass is skipped.UI
Not covered
0144; this PR uses0145. The runner records migrations by name, so either merge order applies both.Checklist
bun run checkpasses: format, lint, typecheck and tests were run per workspace (see the test plan).bun run lint:sastpasses: Opengrep over the 53 changed code files, 0 findings.messages/{en,de,fr}.yml.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.mdand itsdocs/are updated.Test plan
services/sandbox:bun test630 pass. New tests cover:if_stopped;tscandoxlintare clean.Platform unit tests and the real-Postgres lane
checkWorkspaceCleanup. The lane has 15 checks: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:manualand the integration-scope guard are clean.Observed for real against a spawner running this branch on Docker:
Browser: the Sandboxes page in en/de/fr:
Scheduled: the dev stack's own worker ran
sandbox.workspace_gcat minute 21 and retired a deleted agent's workspace (row destroyed, auditedagent_deleted).