Skip to content

feat(PC-206): purge a managed session with every workspace it used - #197

Draft
au-re wants to merge 5 commits into
feature/pc-220-idle-reclaimfrom
feature/pc-206-session-purge
Draft

au-re wants to merge 5 commits into
feature/pc-220-idle-reclaimfrom
feature/pc-206-session-purge

Conversation

@au-re

@au-re au-re commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem

  • A managed session can be closed, but not deleted. After close, its old generations' workspaces, saved files (recovery points), attachments, generated files and history stay until separate retention removes them, and nothing proves they are gone.
  • With E2B and Rivet (PC-198), one chat can run on several generations of compute: the first VM, a replacement after VM loss, and shared VMs. A caller has no single call that removes all of them and reports when that is done.
  • A database restored from a backup that is older than a deletion could bring a deleted session back.
  • Ticket: PC-206 (part of PC-198).

Fix

  • POST /v1/sessions/{id}/purge (scope sessions:write, SDK client.sessions.purge(id)) closes the session first, so new turns, resumes and restores are refused. It then returns a purge receipt: 202 while pending, 200 once completed. Retries return the same receipt.
  • The first receipt already lists every workspace the session ever used: the compute created for it (found by its placement metadata, so an unbound launch counts), every shared workspace it was placed on, and every workspace its saved files came from. Each one gets the existing workspace purge, which proves the provider stopped and removes storage. A shared workspace that other sessions still use is drained first. Those sessions continue on new compute from their own saved files.
  • The receipt completes only after every listed workspace is purged, the closed session's recovery points and archives are swept, and its attachments, generated files, events and conversation content are deleted. The session row stays as a closed tombstone, so the session ID cannot be created again.
  • Each accepted purge is journaled (session_purged). Replaying it on a restored older database closes the session, deletes its content again and leaves the purge pending, so the controller finishes it again.
  • Code:
    • packages/db/src/modules/sessions/session-purge.ts: receipt, inventory, finish, replay.
    • packages/server/src/sessions/session-purge.ts: the worker, run on the scheduler interval.
    • drainForPurge in capacity-idle.ts, reusing the PC-220 drain.
    • New nullable managed_sessions.purge column (generated migration).
  • Verified:
    • Red: the new test failed in CI before the fix (run 47231684761, units (2): Cannot find module './session-purge') on the temporary branch test/pc-206-red, now deleted.
    • Green, run on the GCE test host because the remote test extension is not installed here:
      • packages/server/src/sessions/session-purge.test.ts: restore a session onto a second workspace, purge it, retry the purge, check that both workspaces are purged, the archive folder is empty, an old restore grant gets 401, history is empty, resume is refused and the completed receipt repeats.
      • packages/db/src/modules/sessions/session-purge.test.ts: replaying the journal record twice on an older database closes the session, removes its events and leaves the purge pending.
      • packages/server src/sessions, src/lifecycle, src/persistence/purge*, packages/db src/modules/sessions, src/recovery, src/modules/budgets, src/journal and packages/sdk all pass, except one test, native producer retains original results ... shared sessions. It times out after 5 s on that slower host, and it times out the same way on the parent branch without this change.
    • Live on the self-hosted E2B cluster (PC-200 workers, Rivet template from PC-204), examples/e2e/e2b-session-purge.ts at revision 9a2c1d3:
      • Session A saved a turn, its VM was killed, and its next turn ran on a new VM. Sibling B then joined A's second VM.
      • The purge was accepted at 00:25:35. The first receipt and its retry both listed the two workspaces. They were purged after 3 s and 5 s, and the receipt completed after 18 s.
      • Both VMs were gone, only B's saved files remained, A's history was empty and resume returned session.closed.
      • B was suspended, and its next turn repeated its token on a third VM.
    • bun run check and bun run typecheck pass locally. This PR's CI runs the full affected suites.
  • Self-reviewed:
    • The worker uses only existing paths: close, the workspace purge operation, the recovery point sweep, conversation deletion and the drain. No new stop or storage code was added.
    • Shared compute is drained, not purged directly, so running turns of other sessions are not cut off.

Background

  • The workspace purge already gives verifiable deletion with a journal, provider evidence and storage removal. A session purge is a set of workspace purges plus the session's own records.
  • Recovery points of closed sessions were already swept with their archives. The purge waits for that sweep instead of deleting archive files itself.
  • Workspace purge does not touch managed session conversations (conversationIds excludes them), so purging a shared workspace leaves other sessions' history intact.
  • Alternative: delete only the session's own directory inside a shared VM. Rejected: there is no producer call for it, and draining gives the same result through the tested path.

Downsides

  • Purging a session on shared compute moves its siblings to new compute. Their next turn pays the restore cost.
  • Busy shared compute and queued work delay the purge until the running turn ends.
  • The ordinary workspace purge needs a storage driver when the template declares persistence mounts. The E2B Rivet template declares none.
  • Request IDs and states stay as content-free tombstones, as with conversation deletion.
  • Adds one migration (managed_sessions.purge).
  • A workspace launched for the session after the purge was accepted is added to the receipt by the worker, so the list can grow while the purge is pending.
  • Not covered here: a full controller restore from an older backup in an isolated environment. This is tracked on PC-206.
Notes
  • Stacked on feat(PC-220): release idle managed compute after a profile's warm period #196 (feature/pc-220-idle-reclaim).
  • Changeset: .changeset/session-purge.md (CLI and SDK, minor).
  • Docs: docs/managed-session-work.md (new Purge section and route table), docs/cleanup.md, docs/api.md.
  • The archive upload helper and replacement launcher moved from recovery-points-flow.test.ts to recovery-fixture.test.ts so both tests share them.
  • storedInputPurge and workspaceInputPurge moved out of app.ts into attachments/purge-inputs.ts to keep composeServer under the function length limit.

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