Repository navigation
fix: keep a completed run's sandbox up until its last events reach the app server - #761
Conversation
… server The agent-server posts a partial batch of conversation events to the app server's webhook only after WebhookSpec.flush_delay (30 s) without a new event. complete_run deleted (or paused) the sandbox right away, so the last events of a run's conversation -- usually the agent's final reply and the finished state update -- never reached the app server. complete_run now books the deletion for the watchdog's due-sandbox sweep AUTOMATION_SANDBOX_EVENT_FLUSH_GRACE_SECONDS (default 35) after completion, and with AUTOMATION_SANDBOX_CLEANUP_DELAY_SECONDS set it pauses only after the grace period. Setting both to 0 restores immediate deletion. Fixes #616 Co-authored-by: openhands <openhands@all-hands.dev>
|
📁 PR Artifacts Notice This PR adds or changes temporary PR-specific documents under |
|
Warning Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it. |
|
@all-hands-bot Please review the latest head 30a46ce. All CI checks are green and the branch merges cleanly with main. Before/after evidence is in the PR description. This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig. |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Reviewed 30a46cecb1d940e39327e7cf456a2e7a74558ead against main (949c001), the linked issue #616, and the repository's review guidance. Scope is correct: this repository owns sandbox lifecycle orchestration, and #616 explicitly lists the service-level fix as one of the candidate locations; the issue is currently labeled ready-for-dev.
What I verified:
- Decision table in
complete_run(openhands/automation/router.py): traced every combination ofsandbox_cleanup_delay_secondsxsandbox_event_flush_grace_seconds. The default path (no delay, grace 35) now bookssandbox_cleanup_due_atfor the watchdog'scleanup_due_sandboxessweep instead of deleting at callback time; both settings at 0 preserve the immediate deletion; a delay still pauses, but only after the grace, with deletion booked atmax(delay, grace). There is no combination that spawns a teardown while also deferring, so no double-teardown path. - Deferred deletion is reachable and self-healing: the sweep runs in the watchdog loop whenever the service is not in local mode, and local runs carry
sandbox_id=Noneso nothing is ever booked that the sweep can't consume. The stamp is cleared after one attempt and the runtime's TTL reaper remains the backstop, so a crash no longer strands an orphaned sandbox the way the old fire-and-forget task could._spawn_teardown's strong reference fixes the weakref task-loss hazard for the remaining background paths. - Credentials: the deferred deletion needs no key at callback time;
CloudSandboxBackend.cleanup_after_verificationmints one via the service key at sweep time. Nothing new is persisted or logged. - Unchanged paths:
keep_alive=true, subject-owning runs, cancellation, dispatch-failure cleanup, and the watchdog's own terminal paths behave as before; the watchdog still only stamps on its own RUNNING-to-terminal write, andmark_stale_runsscans only RUNNING runs, so a callback-first completion can't be re-processed. - Tests: the ordering regression test asserts all three invariants (no deletion started, no pause started,
sandbox_cleanup_due_atmore than 30 s aftercompleted_at); the cleanup-delay test proves exactly one pause after the grace; the zero-grace test pins immediate deletion with no stamp; the retention tests cover subject-owning vs ordinary runs. Local test execution in this workspace isn't possible (no Docker daemon), so the greenunit-tests/windows-tests/backendruns on this exact head are the acceptance signal, corroborated by static verification and the PR's real-path simulation output for both the default andAUTOMATION_SANDBOX_CLEANUP_DELAY_SECONDSpaths, which show the final reply andfinishedstate update arriving before teardown on this branch and not on main. - Docs/config: the new
AUTOMATION_SANDBOX_EVENT_FLUSH_GRACE_SECONDSfield is documented inServiceSettings, and theAGENTS.mdsandbox-cleanup note now matches the implemented behavior. The.pr/issue-616/artifacts follow the repository's PR-artifacts policy.
No material correctness, security, compatibility, or acceptance-criterion defects found. The 5 s margin over the agent-server's 30 s flush_delay is deliberate and configurable; a deployment with a longer effective flush delay can raise the setting (or drop it to 0 once an SDK-side terminal flush ships), which the design explicitly supports.
APPROVED
|
🚀 Released in 1.33.0. |
Why
Fixes #616.
The agent-server sends conversation events to the app server's webhook in batches of 5. It posts a partial batch only after
WebhookSpec.flush_delay(30 s) passes with no new event.complete_rundeleted the run's sandbox, or paused it whensandbox_cleanup_delay_secondswas set, in the same second as the completion callback. The runner sends/completeabout 5 s after the conversation's last event. So whenever the last events don't fill a batch, the app server's copy of the run conversation stops before the agent's final reply and thefinishedstate update. A paused agent-server can't post either.The issue lists three places a fix could go. This PR fixes it in the service:
An SDK-side flush on terminal status would let the grace period drop to 0 later. The setting allows that.
Summary
AUTOMATION_SANDBOX_EVENT_FLUSH_GRACE_SECONDS, default 35 (the agent-server's 30 sflush_delayplus a 5 s margin).complete_runwithkeep_alivenot true:sandbox_cleanup_due_at = completed_at + grace, and the watchdog's existingcleanup_due_sandboxessweep deletes the sandbox. The deletion survives a service restart, unlike the previous fire-and-forget task. In practice the sandbox is deleted 35–95 s after completion, depending on the watchdog interval.sandbox_cleanup_delay_seconds: the pause runs after the grace period. Deletion is still booked atmax(delay, grace). If the pause task is lost, the sandbox only runs until that booked deletion.keep_alive=trueand subject-owning (continue_conversation) runs, cancellation, dispatch-failure cleanup, and the watchdog's own terminal paths. Those paths run at the run's timeout, long after the last event.AGENTS.md: the sandbox cleanup note now describes the grace period.How to Test
test_complete_run_keeps_sandbox_until_buffered_events_flushis the ordering regression test. After/complete, the test checks three things:sandbox_cleanup_due_atis more than 30 s aftercompleted_at.test_complete_run_with_cleanup_delay_pauses_and_stamps_due_at: no pause before the grace period ends, and exactly one pause after it.test_complete_run_without_flush_grace_deletes_at_once: with both settings at 0, the sandbox is deleted at once.tests/test_conversations.py: an ordinary run is booked for deletion, and a subject-owning run is not.End to end:
.pr/issue-616/demo_616.py, described in.pr/issue-616/README.md, is a simulation that needs no OHE install:WebhookSpec, with a mock LLM that makes one tool call and then sends a final reply. Its webhook goes to a fake app server.complete_runroute and the realcleanup_due_sandboxessweep (every 5 s) handle teardown.The simulation ran on both the default path and with
AUTOMATION_SANDBOX_CLEANUP_DELAY_SECONDS=600:On
main, the app server never getsMessageEvent(agent: final reply)orConversationStateUpdateEvent(execution_status=finished)on either path. On this branch, the last partial batch arrives about 25 s after/complete, and only then is the sandbox deleted (about 35 s) or paused.This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.