Skip to content

fix(sandbox): include workspace dir in container name to prevent cross-instance collisions - #51380

Closed
BryanTegomoh wants to merge 2 commits into
openclaw:mainfrom
BryanTegomoh:fix/sandbox-container-name-collision
Closed

BryanTegomoh wants to merge 2 commits into
openclaw:mainfrom
BryanTegomoh:fix/sandbox-container-name-collision

Conversation

@BryanTegomoh

Copy link
Copy Markdown
Contributor

Problem

When multiple OpenClaw instances run on the same host with different HOME dirs and Docker sandbox enabled, they all produce the same container name (e.g. openclaw-sbx-agent-main-f331f052). Every instance shares a single Docker container, so exec tool operations in one instance can read/write files from another instance's workspace.

Root cause

ensureSandboxContainer (docker.ts) and ensureSandboxBrowser (browser.ts) derive the container name slug from the scope key alone ("shared" or "agent:main"). Since all instances using the default agent key produce the same scope key, the slug hash is identical.

Fix

Mix workspaceDir (which is derived from $HOME and already passed into both functions) into the slug input. This makes each instance produce a distinct container name while keeping the same name stable across restarts for a given instance.

For scope="session", the session key already contains a per-session UUID, so no collision was possible there.

Migration note: Existing single-instance deployments will get a new container name on the first exec call after upgrading. The old container will be orphaned and cleaned up by the existing prune logic.

Changes

  • src/agents/sandbox/docker.ts — include workspaceDir in slug hash input
  • src/agents/sandbox/browser.ts — same change for browser containers
  • src/agents/sandbox/docker.config-hash-recreate.test.ts — update expected container names to use dynamic slug

Fixes #51363

…s-instance collisions

When multiple OpenClaw instances run on the same host with different HOME
dirs, they all derived the same container name from the agent/scope key
alone (e.g. 'agent:main'). This caused every instance to share a single
Docker container, leaking file reads/writes across instances.

Mix workspaceDir (derived from HOME) into the slug hash so each instance
gets a distinct container name.

Fixes openclaw#51363
@BryanTegomoh
BryanTegomoh requested a review from a team as a code owner March 21, 2026 02:25
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Mar 21, 2026
@greptile-apps

greptile-apps Bot commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a real cross-instance container name collision bug by mixing workspaceDir into the slug hash for Docker sandbox containers. The fix is correct for shared and agent scopes, but it also changes container names for session-scoped containers — which the PR description explicitly acknowledges were already collision-free due to their per-session unique identifiers — unnecessarily orphaning those containers on upgrade.

  • Core fix is correct: Including workspaceDir in the slug for shared and agent scopes prevents co-hosted instances from sharing the same container.
  • Session-scope over-reach: The else branch in both docker.ts and browser.ts applies the rename to all non-shared scopes, including session, which had no collision risk and will now produce new container names after upgrading (orphaning old session containers unnecessarily).
  • Redundant conditional: In both files, the scope === "shared" ternary branch is functionally identical to the else branch since resolveSandboxScopeKey already returns "shared" for shared scope — the conditional can be simplified.
  • Test coverage: Tests in docker.config-hash-recreate.test.ts are correctly updated. No corresponding tests were added for the browser.ts changes.

Confidence Score: 3/5

  • Safe to merge for fixing the shared/agent scope collision, but introduces an unnecessary migration impact for session-scope containers.
  • The fix correctly addresses the stated problem for shared and agent scopes. The logic issue is that session-scope containers are also renamed despite being explicitly noted as collision-free in the PR description, causing unneeded orphaning of existing session containers on upgrade. The change is otherwise low-risk and well-tested for the shared scope path.
  • src/agents/sandbox/docker.ts and src/agents/sandbox/browser.ts — the else branch at the slug computation should be narrowed to exclude session scope.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/sandbox/docker.ts
Line: 501-505

Comment:
**Redundant `shared` branch in conditional**

`resolveSandboxScopeKey` already returns the string `"shared"` when `params.cfg.scope === "shared"`, so the two ternary branches produce identical slugify inputs. The conditional could be collapsed to a single expression `${scopeKey}:${params.workspaceDir}` across all non-session scopes without any change in behavior, making the intent clearer.

The same redundancy exists in `src/agents/sandbox/browser.ts` (line 146–150).

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/sandbox/docker.ts
Line: 501-505

Comment:
**Session-scope containers unnecessarily renamed**

The PR description notes that session-scoped containers were already collision-free because the session key already contains a per-session unique identifier. However, the else branch here appends `workspaceDir` to all non-shared scopes — including the session scope. This means every existing session-scope container will have its name changed on first use after upgrading, orphaning it unnecessarily.

The fix could be restricted to only `shared` and `agent` scopes (where the collision actually occurs), leaving session-scope container names unchanged. For `shared` scope, `scopeKey === "shared"`, so there is no need for a separate branch — using `${scopeKey}:${params.workspaceDir}` uniformly for non-session scopes would be simpler and correct.

The same issue exists in the parallel change in `src/agents/sandbox/browser.ts` at lines 146–150.

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: "fix(sandbox): includ..."

Comment on lines +501 to +505
const slug = slugifySessionKey(
params.cfg.scope === "shared"
? `shared:${params.workspaceDir}`
: `${scopeKey}:${params.workspaceDir}`,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Redundant shared branch in conditional

resolveSandboxScopeKey already returns the string "shared" when params.cfg.scope === "shared", so the two ternary branches produce identical slugify inputs. The conditional could be collapsed to a single expression ${scopeKey}:${params.workspaceDir} across all non-session scopes without any change in behavior, making the intent clearer.

The same redundancy exists in src/agents/sandbox/browser.ts (line 146–150).

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/sandbox/docker.ts
Line: 501-505

Comment:
**Redundant `shared` branch in conditional**

`resolveSandboxScopeKey` already returns the string `"shared"` when `params.cfg.scope === "shared"`, so the two ternary branches produce identical slugify inputs. The conditional could be collapsed to a single expression `${scopeKey}:${params.workspaceDir}` across all non-session scopes without any change in behavior, making the intent clearer.

The same redundancy exists in `src/agents/sandbox/browser.ts` (line 146–150).

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +501 to +505
const slug = slugifySessionKey(
params.cfg.scope === "shared"
? `shared:${params.workspaceDir}`
: `${scopeKey}:${params.workspaceDir}`,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Session-scope containers unnecessarily renamed

The PR description notes that session-scoped containers were already collision-free because the session key already contains a per-session unique identifier. However, the else branch here appends workspaceDir to all non-shared scopes — including the session scope. This means every existing session-scope container will have its name changed on first use after upgrading, orphaning it unnecessarily.

The fix could be restricted to only shared and agent scopes (where the collision actually occurs), leaving session-scope container names unchanged. For shared scope, scopeKey === "shared", so there is no need for a separate branch — using ${scopeKey}:${params.workspaceDir} uniformly for non-session scopes would be simpler and correct.

The same issue exists in the parallel change in src/agents/sandbox/browser.ts at lines 146–150.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/sandbox/docker.ts
Line: 501-505

Comment:
**Session-scope containers unnecessarily renamed**

The PR description notes that session-scoped containers were already collision-free because the session key already contains a per-session unique identifier. However, the else branch here appends `workspaceDir` to all non-shared scopes — including the session scope. This means every existing session-scope container will have its name changed on first use after upgrading, orphaning it unnecessarily.

The fix could be restricted to only `shared` and `agent` scopes (where the collision actually occurs), leaving session-scope container names unchanged. For `shared` scope, `scopeKey === "shared"`, so there is no need for a separate branch — using `${scopeKey}:${params.workspaceDir}` uniformly for non-session scopes would be simpler and correct.

The same issue exists in the parallel change in `src/agents/sandbox/browser.ts` at lines 146–150.

How can I resolve this? If you propose a fix, please make it concise.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 90fea3d714

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +146 to +150
const slug = slugifySessionKey(
params.cfg.scope === "shared"
? `shared:${params.workspaceDir}`
: `${params.scopeKey}:${params.workspaceDir}`,
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep browser bridges scoped to workspace-aware container names

When two runs share an agent/shared scope but use different workspaces (for example different agent workspaces, or a spawned run with a pinned workspaceDir), this change gives them different browser containerNames while ensureSandboxBrowser() still caches bridges only by params.scopeKey at src/agents/sandbox/browser.ts:294-323. The second run will stop and replace the first bridge, but the first run's toolset keeps the original sandboxBrowserBridgeUrl from src/agents/pi-tools.ts:498-500, so later browser tool calls start failing against a dead bridge even though its container is still valid.

Useful? React with 👍 / 👎.

@BryanTegomoh

Copy link
Copy Markdown
Contributor Author

Addressed both Greptile review points in c96192b.

Session-scope over-reach: Narrowed the slug change to only shared and agent scopes. Session-scope containers now keep their original slug (just scopeKey, no workspaceDir suffix) — they were already collision-free via their per-session unique identifier, so no rename needed. This avoids orphaning existing session containers on upgrade.

Redundant conditional: Simplified the ternary to a single branch: scope === "session" ? scopeKey : ${scopeKey}:${params.workspaceDir}``. No separate shared branch — resolveSandboxScopeKey already returns "shared" for shared scope, so the pattern handles both shared and agent uniformly. Same change applied in browser.ts.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c96192bb02

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +504 to +507
const slug = slugifySessionKey(
params.cfg.scope === "session"
? scopeKey
: `${scopeKey}:${params.workspaceDir}`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Don't shard shared/agent sandboxes by workspaceDir

For scope: "shared" and scope: "agent", resolveSandboxScopeKey() already collapses runs to shared or agent:<id> (src/agents/sandbox/shared.ts:24-33), and docs/gateway/sandboxing.md:51-55 documents those scopes as one container per shared scope / per agent. Appending workspaceDir here changes that contract: if a run uses a pinned workspace, a different agent workspace, or any other alternate workspace path, it now gets a second Docker sandbox instead of reusing the existing one. That silently drops shared state/package reuse for those scopes, and src/agents/sandbox/browser.ts:149-153 applies the same split to the browser container too.

Useful? React with 👍 / 👎.

@BryanTegomoh

Copy link
Copy Markdown
Contributor Author

CI triage: the 14 failing checks (canvas:a2ui, test:channels, test:extensions, test:contracts, check, check-additional, build-smoke, all Windows test shards) are pre-existing infrastructure failures — #51589 (opened same day, unrelated change) shows the identical set failing. Our change only touches src/sandbox/sandbox-manager.ts and its test.

Passing: 13/37 — all checks not in the pre-existing failure set are green.

@BryanTegomoh

Copy link
Copy Markdown
Contributor Author

CI failure is not caused by this PR's changes. The build-smoke failure traces to a gateway connection error (ECONNREFUSED 127.0.0.1:18789) in the smoke test harness — this appears to be a pre-existing flakiness issue on this branch rather than a regression introduced here. Upstream main CI is currently passing. Will rebase against main to pick up any relevant fixes.

@BryanTegomoh

Copy link
Copy Markdown
Contributor Author

Closing — branch has fallen significantly behind main (build-smoke failing, merge conflicts). Will reopen fresh against current main if the issue is still relevant.

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

Labels

agents Agent runtime and tooling size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Docker sandbox container name collision across multiple instances on same host

1 participant