Repository navigation
fix(platform): destroy the container of a failed agent session - #3505
Merged
Merged
Conversation
A fresh agent session whose spawner create failed was marked `failed` without asking the spawner to destroy what it may already have made. A create cut short between Docker's create and start leaves its container in state `created`, and nothing ever removed it: the row is never reconciled, resumed or listed, and the spawner only adopts running session containers. The leaked container kept its runtime image, and with it a whole superseded release, alive through every later deploy. ensureAgentSession now calls sessionDestroy best-effort before the row reads `failed`, while the still-`creating` row holds the owner's slot, so no fresh create of the same deterministic id can start under the destroy. A destroy failure is logged and never masks the create's error. The resume path is unchanged: a failed resume releases its slot and never destroys the standing workspace.
Nothing ever collected a `failed` session row: reconcile walks creating/active/degraded rows and reclaim walks stopped/expired ones. A destroy the failing turn could not complete (a spawner being rolled, a wedged daemon) therefore left the container and its host workspace for good. The sandbox sweep gains a COLLECT pass over `failed` rows without `destroyed_at_ms`, past a ten-minute grace, in the same fair walk as the other spawner-facing passes. Session ids are deterministic and the next turn inserts a fresh row under the same id, so a row whose id a newer or live incarnation carries is settled without a spawner call. Otherwise the spawner destroys the session only when idle (`if_idle`), as for the ended-run reclaim; busy and erroring sessions wait for a later tick. The row is stamped by primary key and keeps its `failed` status: markSessionDestroyed would settle every row and revoke every token under the session id, a live successor's included. The real-Postgres checkWatchdogs lane drives the pass with a scripted spawner, and the manual automation register records the coverage.
A sibling turn of the same owner can resume the still-creating row and create or adopt the session itself; an unconditional destroy on this turn's failure would kill that turn's running exec. The failure path now destroys only an idle session (if_idle), which still covers a container cut short before it started, and leaves a busy one to the watchdog's collect pass.
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.
Problem
When a fresh agent session's spawner create failed,
ensureAgentSessionmarked theapp.sandbox_sessionsrowfailedand rethrew. It never asked the spawner to destroy what it might already have made. A create cut short between Docker's create and start leaves its container in statecreated, and nothing ever removed it:creating/active/degradedrows and reclaim walksstopped/expiredones, so no pass reached afailedrow;failedisn't a live status, so the Sandboxes page never listed the row and nobody could destroy it by hand;As #3494 reports, the leaked container kept its runtime image, and with it a whole superseded release (about 12.5 GB), alive through every later deploy. Its host workspace directory stayed too.
Fix
backend/core/node_only/sandbox/agent_session.ts): the fresh-row catch now callssessionDestroyIfIdle(sessionId)before the row is markedfailed. Only an idle session goes: a sibling turn of the same owner can resume the still-creatingrow and create or adopt the session itself, and its running exec must never die for this turn's failure (a busy answer is logged and left to the sweep). A container that never started runs no exec, so the leak from Sandbox: an agent session that fails after its container was created leaves the container behind #3494 is still destroyed. The call is best-effort: a failure is logged withconsole.warnand never masks the create's error. The destroy runs while the still-creatingrow holds the owner's slot, so no fresh create of the same deterministic id can start underneath it. The resume path is unchanged: a failed resume releases its slot and never destroys the standing workspace.backend/domains/sandbox/watchdogs.ts): the 5-minute sandbox sweep gains a pass overfailedrows withdestroyed_at_ms IS NULL, once they are past a 10-minute grace. It uses the same fair walk and visit stamps as the other spawner-facing passes.pa-<agentId>,wf-<runId>-…), and the next turn inserts a new row under the same id. A failed row whose id a newer row (any status) or a live row (any age) also carries is stamped without a spawner call: whatever the spawner holds under that id belongs to the newer incarnation. This check runs per row, just before the spawner call, and is deployment-wide, because the spawner's session namespace is.?if_idle=1, as the ended-run reclaim does. A busy session or a spawner error leaves the row for a later tick.failed, which records what happened to that incarnation.markSessionDestroyedwould settle every row and revoke every token under the session id, a live successor's included. A failed incarnation never minted credentials (the hosts mint only afterensureAgentSessionreturns), so its docstring now names this exception.SandboxWatchdogResult.collectedreports the count, and thewatchdog.sandboxjob logs it.sandbox_sessions_reconcile_order; the successor check usessandbox_sessions_session_id.session_constants.tsno longer credits a non-existent "owner-cascade teardown" with reapingfailedrows.Proof
agent_session.test.ts(both owner types):createand beforesetSessionStatus(failed);failed, and the original error survives;failed.watchdogs.test.ts:markSessionDestroyed;failedrows, grace, fair order) is pinned;skipReconcileskips the new pass.checkWatchdogslane 3c inbackend:integration, with a scripted spawner:wd-collect-live=failed/stamped, and the spawner was asked;wd-collect-reused=failed/stampedwith no spawner call, while the newerstoppedincarnation staysunstampedand its token stays live;watchdogs.tsthe same lane FAILS (after 8 ticks every row is still unstamped).ITEST_LANES=checkWatchdogson PostgreSQL 16.14 + pgvector: 23/24. The one failure is the boot-timeknowledge BM25 indexes verified at bootcheck, because that database had no ParadeDBpg_search; it is unrelated to this change.cache miss, executing):tsc --noEmitpasses.render_fetch.test.ts×3,openai-compat.test.ts×1). They fail identically on untouchedorigin/mainin that sandbox and pass in CI.oxlint --type-aware --type-checkis clean on every changed file, the hosts that callensureAgentSession, and the touched directories. The whole-workspacetsc/lintdoes not fit that sandbox's 4 GiB, which is why CI's run is quoted above.oxfmt --check,bun run lint:manual, and opengrep on the changed files (0 findings) all pass.docker ps -a --filter name=tale-sbx --filter status=created --format '{{.Names}} {{.Status}} {{.Image}}'on a Docker host has not been run, because this environment has no Docker daemon. After deploy it should list no new entries. Existing leftovers offailedrows are collected by the sweep within a few ticks (25 rows per tick). A container with no platform row at all stays out of reach and needs a manualdocker rm.Notes for review
DELETE /v1/sessions/:idalso removes the host workspace directory, as Sandbox: an agent session that fails after its container was created leaves the container behind #3494 asks ("container and workspace"). A directory there can only be a leftover of an incarnation the platform already ended (a destroyed, expired or failed row), because a live or hibernated row sends the turn down the resume path.creating, and end up creating the session itself; the first turn's catch then flips that row tofailed. Both destroys (the catch's and the sweep's) areif_idle, so neither kills a session that turn is executing in; the flip itself is the pre-existing part.Out of scope
render_fetch.ts) has the same shape: a failed create skips the spawner destroy and the row is flipped todestroyed, so the sweep never sees it. Its batch key can repeat on a job retry, so a 409 there needs its own analysis.Closes #3494