Skip to content

fix(harness): reconcile the sandbox container by name before creating one - #3890

Open
wahllllll wants to merge 3 commits into
agentscope-ai:mainfrom
wahllllll:fix/issue-3644-sandbox-container-reconcile
Open

wahllllll wants to merge 3 commits into
agentscope-ai:mainfrom
wahllllll:fix/issue-3644-sandbox-container-reconcile

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

Problem

DockerSandbox derives the container name from the session id, but decides whether a container is usable only from the persisted containerId. When the two stop agreeing the code concludes "container missing" and issues docker run with a name the daemon still claims:

docker run failed (exit=125): Conflict. The container name "/agentscope-sandbox-<session>"
is already in use by container "live-id".

Nothing corrects the state, so every later turn repeats the same decision and the sandbox session can never start again. The realistic trigger is docker run succeeding while persisting the returned id fails — the container exists under the deterministic name, but no id was ever recorded.

The probe made this worse by collapsing "the daemon says this does not exist" and "the probe could not answer" into one UNKNOWN state, so a failed inspect was treated as evidence that the container was gone.

Change

  1. Reconcile by name before creating. When no usable id is persisted, the sandbox asks the daemon about agentscope-sandbox-<sessionId> first: a running container is adopted (its id recorded), a stopped one is started, and only a name the daemon does not know leads to docker run.
  2. Tell "missing" from "unanswered". inspectContainerState now distinguishes MISSING (the daemon explicitly reports no such object) from UNKNOWN (transport failure). A stale id is cleared only in the MISSING case; on UNKNOWN the persisted id is kept, because a probe that cannot answer is not evidence of absence — the next command surfaces the transport failure instead of the sandbox silently building a second container.
  3. One seam for the CLI. inspect and container creation each used to build their own ProcessBuilder with their own stream draining, while start/stop/rm went through runDockerCliBlocking. Everything now goes through runDockerCli, which returns the exit code plus both streams; runDockerCliBlocking keeps its contract on top of it. This is what makes the lifecycle testable without a live daemon.

shutdown() is untouched: docker rm on a container that is already gone is already tolerated.

Tests

DockerSandboxContainerReconcileTest runs the lifecycle against a fake daemon that refuses a claimed name the way the real CLI does:

Case Expectation
persisted id stale, name held and running adopt it, never create
persisted id stale, name held and stopped start it, never create
persisted id stale, name free create
probe cannot answer keep the persisted id, never create

Against the previous code the first three fail with the reported exit=125 conflict; the fourth creates a duplicate container under the claimed name.

Verification

  • mvn -B -pl agentscope-harness "-Dtest=DockerSandbox*" test: 30 tests, 0 failures (9 daemon-backed integration tests skipped as before).
  • mvn -B -pl agentscope-harness test: full module suite.
  • spotless:check: BUILD SUCCESS.

Addresses #3644.

DockerSandbox reached the daemon from four places: inspect and container
creation each built their own ProcessBuilder with its own stream draining, while
start/stop/rm went through runDockerCliBlocking. Only the blocking helper was
overridable, so the container lifecycle could not be exercised without a live
daemon.

Every call now goes through runDockerCli, which returns the exit code plus both
streams, and runDockerCliBlocking keeps its contract on top of it. Behaviour is
unchanged; this is the seam the container reconciliation needs.
… one

The container name is derived from the session id, so the daemon can still hold a
container under that name while no usable id is persisted: docker run can succeed
with only persisting its id failing. doEnsureContainerRunning only ever looked at
the persisted id, concluded "container missing", and issued docker run with a name
the daemon had already claimed. That fails with exit 125, nothing corrects the
state, and every later turn repeats the same decision, so the session can never
start again.

The probe also conflated "the daemon says the container does not exist" with "the
probe could not answer", so a transient inspect failure was read as evidence that
the container was gone.

Before creating anything the sandbox now reconciles with the daemon by name: a
container found there is adopted or started and its id recorded, and only a name
the daemon does not know results in docker run. A probe that cannot answer no
longer destroys the persisted id, because it is not evidence of absence; the next
command reports the transport failure instead.

DockerSandboxContainerReconcileTest drives the lifecycle through a fake daemon
that refuses a claimed name like the real CLI. Against the previous code the three
reconcile cases fail with "docker run failed (exit=125): ... container name
\"/agentscope-sandbox-wedge\" is already in use", which is the reported symptom.
@juruoO-o

Copy link
Copy Markdown
Collaborator

[P2] Container adoption reports success even when it did not obtain a usable container ID.

adoptContainerByName() first inspects the name, then adoptContainer() performs a second inspect through resolveContainerId(). If the first probe succeeds but the second fails or returns no ID, resolveContainerId() returns null. adoptContainer() leaves the ID unset, and its caller still returns true. The prior ID may already have been cleared by doEnsureContainerRunning(); the initial no-ID recovery case also has no fallback. super.start() then reaches workspace setup with a null ID and builds docker exec <null> mkdir ..., so the claimed successful reconciliation actually fails.

Please require a usable ID before reporting adoption success, propagate an actionable probe failure, or obtain ID and running state in a single inspect response. Add a regression where the name/state probe succeeds and the subsequent ID probe fails, exercising the public start path rather than only the private reconciliation helper.

Reviewed head: 2cb444c6a0f6a2de57854823eefa166f1281f4db. Static source review; local Java/Maven tests were not run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 紧急不重要

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants