Skip to content

feat(PC-220): release idle managed compute after a profile's warm period - #196

Draft
au-re wants to merge 4 commits into
feature/pc-205-tenant-accessfrom
feature/pc-220-idle-reclaim
Draft

au-re wants to merge 4 commits into
feature/pc-205-tenant-accessfrom
feature/pc-220-idle-reclaim

Conversation

@au-re

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

Copy link
Copy Markdown
Contributor

Problem

  • Managed sessions keep their compute until they close. A chat that nobody uses still holds a VM and a capacity slot, so other owners wait or fail with capacity_exhausted.
  • With E2B (PC-212), a stopped VM can be paused and resumed in about 1.5 s, so holding idle compute has no benefit after a short warm period.
  • The capacity drain already exists, but only one path started it: a full pool picking an idle victim for a waiting session. Nothing released compute on time alone.
  • Ticket: PC-220 (part of PC-198).

Fix

  • A managed profile can set idleSeconds (its warm period). When a workspace on that profile has had no request activity for that long, PocketCoder drains it: the usual drain stops the compute and frees its capacity. Its sessions become suspended with the reason workspace_idle, and they place again when they get new work.
  • Profiles without idleSeconds keep today's behavior.
  • drainIdleCapacity (packages/db/src/modules/budgets/capacity-idle.ts) runs at the start of the existing session recovery tick. Under the capacity lock it selects launched, non-draining capacity past its warm period, skips workspaces that are not ready or still run an execution, and skips sessions with queued work.
  • idle_seconds is a new nullable column on managed_profiles (generated migration).
  • Verified:
    • Red: the two new tests in packages/server/src/sessions/shared-placement-flow.test.ts failed in CI before the fix (run 47228037007, units (4)), on the temporary branch test/pc-220-red (now deleted).
    • Green: bun run check and bun run typecheck pass locally.
    • The first CI run failed in units (4) and stores. The idle filter only accepted capacity in state launch_claimed, and the test fixture's capacity stays reserved. The filter now matches the idle victim: a ready workspace with no running execution. All 8 tests in shared-placement-flow.test.ts pass on the GCE test host.
    • Live: bun run example:e2e:e2b-idle-reclaim passed on the two-worker E2B cluster from PC-200 (revision 93116d8, profile idleSeconds: 60):
      • A gap of 30 s kept the VM. Five chats shared one VM.
      • The VM was released 60.3 s after the last activity. All five sessions became suspended / workspace_idle.
      • Another owner then got a new VM. The first chat resumed on a third VM with its ID, earlier token, edited file and attachment.
      • Turn latency: 2.7 s warm, 17.0 s for the first turn after the release (new VM plus restore).
  • Self-reviewed: the drain reuses the existing drainingAt path, so stopping and freeing the slot stays in one place. Busy workspaces are skipped rather than interrupted.

Background

  • The idle victim path (idleVictim) only drains when someone else is waiting. Reusing its drainingAt marker means the recovery tick and the launch flow handle idle drains with no new states.
  • lastUsedAt is already bumped on each request write, so it is the activity clock.
  • Alternative: let E2B's own sandbox timeout pause the VM. Rejected because PocketCoder would still count the slot as used and could not tell a paused VM from a busy one.

Downsides

  • The check runs once per recovery tick, so release can be late by up to one tick.
  • The first turn after a release pays the resume or restore cost.
  • Adds one migration (managed_profiles.idle_seconds).
  • The first turn after a release took 17 s in the live check, against 2.7 s on warm compute. Choose idleSeconds with that in mind.
Notes
  • Stacked on feat(PC-205): prove tenants on one controller cannot reach each other's sessions on E2B #195 (feature/pc-205-tenant-access).
  • Changeset: .changeset/session-idle-reclaim.md (CLI and SDK, minor).
  • Docs: docs/managed-consumers.md profile example and docs/managed-runtime.md warm-period paragraph.
  • Live check: examples/e2e/e2b-idle-reclaim.ts. It prints a timestamped mark at each step. Host samples were taken every 10 s on both workers. Each value below is the mean over the 30 s quiet window after a mark. The VM ran on worker A.
Step Guest CPU Guest memory used Worker A CPU Worker A hugepages in use (of 2,048 × 2 MiB) Firecracker RSS
No chats – – 0.4 % 0 0
One chat 2.9 % 407 MiB 9.4 % 383 (766 MiB) 3 MiB
Five chats 3.1 % 840 MiB 2.7 % 609 (1,218 MiB) 3 MiB
After release – – 0.6 % 0 0

Guest memory lives in the reserved hugepages, so the Firecracker RSS stays small. The host's ordinary memory use stayed at about 5.2 GiB, including the 4 GiB hugepage reservation.

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