Repository navigation
fix(sandbox): include workspace dir in container name to prevent cross-instance collisions - #51380
BryanTegomoh wants to merge 2 commits into
Conversation
…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
Greptile SummaryThis PR fixes a real cross-instance container name collision bug by mixing
Confidence Score: 3/5
Prompt To Fix All With AIThis 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..." |
| const slug = slugifySessionKey( | ||
| params.cfg.scope === "shared" | ||
| ? `shared:${params.workspaceDir}` | ||
| : `${scopeKey}:${params.workspaceDir}`, | ||
| ); |
There was a problem hiding this 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).
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.| const slug = slugifySessionKey( | ||
| params.cfg.scope === "shared" | ||
| ? `shared:${params.workspaceDir}` | ||
| : `${scopeKey}:${params.workspaceDir}`, | ||
| ); |
There was a problem hiding this 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.
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.There was a problem hiding this comment.
💡 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".
| const slug = slugifySessionKey( | ||
| params.cfg.scope === "shared" | ||
| ? `shared:${params.workspaceDir}` | ||
| : `${params.scopeKey}:${params.workspaceDir}`, | ||
| ); |
There was a problem hiding this comment.
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 👍 / 👎.
|
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: |
There was a problem hiding this comment.
💡 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".
| const slug = slugifySessionKey( | ||
| params.cfg.scope === "session" | ||
| ? scopeKey | ||
| : `${scopeKey}:${params.workspaceDir}`, |
There was a problem hiding this comment.
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 👍 / 👎.
|
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 Passing: 13/37 — all checks not in the pre-existing failure set are green. |
|
CI failure is not caused by this PR's changes. The |
|
Closing — branch has fallen significantly behind main (build-smoke failing, merge conflicts). Will reopen fresh against current main if the issue is still relevant. |
Problem
When multiple OpenClaw instances run on the same host with different
HOMEdirs 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, soexectool operations in one instance can read/write files from another instance's workspace.Root cause
ensureSandboxContainer(docker.ts) andensureSandboxBrowser(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$HOMEand 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
execcall after upgrading. The old container will be orphaned and cleaned up by the existing prune logic.Changes
src/agents/sandbox/docker.ts— includeworkspaceDirin slug hash inputsrc/agents/sandbox/browser.ts— same change for browser containerssrc/agents/sandbox/docker.config-hash-recreate.test.ts— update expected container names to use dynamic slugFixes #51363