Repository navigation
feat(sandbox): pause and delete idle sandboxes from the worker - #603
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
9a3da06 to
f3cb58e
Compare
bba6c7d to
3c16288
Compare
3c16288 to
672180b
Compare
✅ Replicated self-hosted validation — PASSTested Install / schema
No-regression e2e
Worker / sweep (verified locally)
Notes (not blockers for this PR)
Validation performed by an AI agent (OpenHands) on behalf of @aivong. |
Code review: feat(sandbox): pause and delete idle sandboxes from the workerHead: What I checked
Material findingsNone. Non-blocking notes
[RISK ASSESSMENT][Overall PR] This adds a new background process that deletes sandboxes and their workspaces, and it's on by default. Docker, E2B and k8s installs get it on upgrade once the worker is deployed: pause after 20 min idle or 12 h of running, delete after 10 days. It also caps foreground terminal commands at 18 minutes on those backends. The code is careful, but nobody has run the real pause, resume and delete paths on a backend the worker manages: the "A human has tested these changes" box is unchecked, and the self-hosted QA ran on runtime-api, where the worker does nothing by design. Recommendation: Do not auto-merge. Before merging, someone should confirm the product decision to delete workspaces by default after 10 days on self-hosted installs, and run the PR's manual test on at least Docker: pause after idle, resume, then delete with Key architectural insight: the worker locks rows with
This review was generated by an AI agent (OpenHands) on behalf of the user. |
✅ Local Docker sandbox validation: PASSI tested Setup
Results
|
Code review: feat(sandbox): pause and delete idle sandboxes from the workerHead: What I checked
Material findingsNone. Non-blocking notes
Status since the earlier review
[RISK ASSESSMENT][Overall PR] This adds a background process that deletes sandboxes and their workspaces, and it's on by default. On upgrade, once the worker is deployed, Docker, E2B and k8s installs get:
The code follows existing patterns and is well tested. The risk comes from destroying data by default on self-hosted installs. Recommendation: Do not auto-merge. Before merging, a product or self-hosted owner should explicitly confirm that workspaces should be deleted after 10 days by default, and decide whether this goes in the release notes. Verdict: COMMENT. There are no code defects, but a product decision is still open. Key architectural insight: The worker doesn't have its own copy of the state machine. It locks the row with
This review was generated by an AI agent (OpenHands) on behalf of the user. |
aivong-openhands
left a comment
There was a problem hiding this comment.
Following up on my two reviews above. None of the notes block this PR or the rest of the stack (#599–#603), so I've filed follow-ups to keep them from getting lost:
Stuck rows → #626. The Docker run reproduced this with a hung agent server: the row is never paused, and each check waits 15s on /health, holding a worker slot, until the 10-day delete. It isn't a regression, since nothing pauses Docker sandboxes on a timer today, and the fix is a small change to decide, so it fits better as its own PR.
Command cap on existing sandboxes → #627. Sandboxes created before the upgrade don't get OH_RUNTIME_IDLE_TIMEOUT_SECONDS (confirmed for Docker; the issue asks to check E2B and k8s), so the worker can pause one partway through a long foreground command. The issue covers a README note, plus release notes that call out the 10-day delete default. The mismatched-settings case I raised is already covered by the README's "same env as the app server" line, so I left it out.
How to Test step 1 → #625. This isn't from this PR: since #466, start_sandbox calls require_user_id, and OSS mode's DefaultUserAuth has no user id, so make run can't start Docker sandboxes on main either. make run-saas doesn't help, because local SaaS uses the remote runtime. The Docker validation run above, which used a workaround, is the record of the real pause, resume and delete paths. @jlav, the issue asks whether OSS mode was meant to keep working after #466.
10-day delete default. This is the one open question, and I no longer think it should hold up the merge, which changes my earlier "do not auto-merge" recommendation. As far as I can tell nothing deploys the worker yet (the self-hosted chart doesn't), so the default only reaches installs with the chart change that adds it. Before that chart change lands, a product or self-hosted owner should confirm that deleting workspaces after 10 days is the default we want. If not, defaulting OH_SANDBOX_LIFECYCLE_DELETE_AFTER_SECONDS to 0 is a one-line follow-up, and pausing stays on.
With these filed, I'm happy for the stack to merge.
This review was posted by an AI agent (OpenHands) on behalf of @aivong.
b8c567b to
23dafd4
Compare
23dafd4 to
3863048
Compare
|
🚀 Released in 1.69.0. |
HUMAN:
AGENT:
Why
Part 5 of 5 in the sandbox lifecycle stack.
runtime-api pauses and deletes the remote backend's sandboxes. Nothing does this for docker, E2B or k8s. Docker and k8s sandboxes run until someone pauses them, and none of the three are ever deleted.
Summary
Approach
Which backends take part. The worker only acts when the configured sandbox injector is a
ManagedSandboxServiceInjector. The docker, E2B and k8s injectors subclass it, and their services subclassManagedSandboxServicefrom part 1. The remote and process ones don't. A new backend opts in by subclassing both.When a sandbox starts or resumes:
mark_runningupdates the three columns part 1 adds to itsv1_remote_sandboxrow. It setslifecycle_statetorunning, and setsstate_changed_atandlast_active_atto now.state_changed_atis the last start, resume or pause.last_active_atis the latest agent activity the app has seen.OH_RUNTIME_IDLE_TIMEOUT_SECONDS, like runtime-api's pods do. The SDK caps a foreground command at 0.9 of that, so one long command doesn't look idle.The sweep, every minute (
sweepinlifecycle/tasks.py):sandbox_lifecycle:sweep. Its queueing lock keeps at most one waiting.lifecycle/sweep.py). It makes no provider calls and takes no locks. A row is a candidate when any of these hold:lifecycle_stateisrunning, and the later ofstate_changed_atandlast_active_atis older than the idle period.lifecycle_stateisrunning, andstate_changed_atis older than the max session.state_changed_atandlast_active_atis older than the delete period.sandbox_lifecycle:checkper candidate, with queueing locksandbox:<id>. A sandbox that already has a check waiting is skipped.The check, one per sandbox (
check_sandboxinlifecycle/tasks.py). It runs in one transaction, with a 120 second timeout.FOR UPDATE SKIP LOCKED. If the row is gone, or an API pause, resume or delete holds it, stop. A later sweep picks the sandbox up again.idle_timewith the service'sget_idle_time. It calls the agent server'sGET /server_info.decideinlifecycle/rules.py). It's a pure function, so it reads best next totest_sandbox_lifecycle_rules.py.pause_sandbox, the same code the API uses, and the check already holds its row lock. A delete callsdelete_sandbox. Anything else only updates the row.The decision in step 4:
state_changed_atis older than the max session. Ifidle_timecouldn't be read, do nothing. Otherwise the last activity is the latest ofstate_changed_at,last_active_atand now minusidle_time. Pause once that's older than the idle period. If it's newer thanlast_active_at, save it there.lifecycle_statesays running:mark_pausedsetslifecycle_statetopausedandstate_changed_atto now. E2B's owntimeout_secondstimer ends up here.state_changed_atandlast_active_atis older than the delete period.state_changed_atcounts as activity, so a sandbox stays active for a full idle period after it starts or resumes. The agent server's idle clock includes time spent paused, so a resumed sandbox would otherwise look idle at once. A failed read never leads to a delete. A failed check isn't retried, and the next sweep queues it again.Issue Number
How to Test
By hand:
OH_SANDBOX_LIFECYCLE_IDLE_SECONDS=120 make runand start a conversation.sandbox_lifecycle.pausedwithreason: idle, anddocker ps -ashows the container as Exited.Video/Screenshots
N/A
Type
Notes
OH_SANDBOX_LIFECYCLE_IDLE_SECONDS,OH_SANDBOX_LIFECYCLE_MAX_SESSION_SECONDSandOH_SANDBOX_LIFECYCLE_DELETE_AFTER_SECONDSset the rules. 0 turns a rule off.Enterprise server image for this PR: