Skip to content

fix(sandbox): scope agent sandboxes by workspace - #96866

Closed
tayoun wants to merge 7 commits into
openclaw:mainfrom
tayoun:fix/sandbox-workspace-scope
Closed

tayoun wants to merge 7 commits into
openclaw:mainfrom
tayoun:fix/sandbox-workspace-scope

Conversation

@tayoun

@tayoun tayoun commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Agent-scoped sandboxes currently key reuse only by agent/session identity. In multi-tenant deployments, the same agent id can be used across separate customer workspaces, so a non-shared sandbox may be reused with the wrong workspace/container identity. This scopes non-shared sandbox lifetime keys by resolved workspace path while preserving shared sandbox behavior.

Summary

  • include the resolved workspace path hash in non-shared sandbox scope keys
  • preserve the workspace hash outside the readable slug truncation window for Docker, SSH, and sandbox skills paths
  • pass the resolved scope key through Docker sandbox creation so container names, labels, and registry entries stay workspace-specific
  • teach openclaw sandbox recreate --session <key> to match the new internal :workspace:<hash> session suffix only when it belongs to the requested raw session key
  • document the one-time upgrade behavior for old non-shared runtimes
  • cover session, shared, agent, Docker backend, SSH backend, and recreate filtering behavior with regression tests

Evidence

  • node scripts/run-vitest.mjs src/commands/sandbox.test.ts src/agents/sandbox.resolveSandboxContext.test.ts src/agents/sandbox/docker-backend.test.ts src/agents/sandbox/ssh-backend.test.ts passed locally
  • pnpm exec oxfmt --check --threads=1 src/commands/sandbox.ts src/commands/sandbox.test.ts passed locally
  • node scripts/run-oxlint.mjs src/commands/sandbox.ts src/commands/sandbox.test.ts passed locally
  • pnpm tsgo:core passed locally
  • pnpm tsgo:core:test passed locally
  • git diff --check passed locally

Real Behavior Proof

Fresh proof collected 2026-06-26 by exercising the exported production Docker backend path: ensureSandboxContainer(...) plus resolveSandboxScopeKey(...), using the same raw session key and two distinct workspace roots. The temporary proof script used the already-local Docker image pgvector/pgvector:pg16 only to avoid a registry pull; the OpenClaw container naming, label, config-hash, registry, and cleanup code paths were unchanged.

Command used:

  • node_modules/.bin/tsx tmp/proof-96866.mts

Redacted captured output:

{
  "rawSession": "agent:proof:session",
  "workspaceA": "<workspace-a>",
  "workspaceB": "<workspace-b>",
  "scopeA": "agent:proof:session:workspace:a9d219c1",
  "scopeB": "agent:proof:session:workspace:ebaf544b",
  "slugA": "agent-proof-session-workspace-a9d219c1-6c6522e6",
  "slugB": "agent-proof-session-workspace-ebaf544b-bae62cf4",
  "containerA": "/oc-proof-96866-agent-proof-session-workspace-a9d219c1-6c6522e6",
  "containerB": "/oc-proof-96866-agent-proof-session-workspace-ebaf544b-bae62cf4",
  "labelA": "agent:proof:session:workspace:a9d219c1",
  "labelB": "agent:proof:session:workspace:ebaf544b",
  "sameRawSession": true,
  "distinctScopeKeys": true,
  "distinctContainers": true,
  "labelsMatchScopeKeys": true
}

Observed result: the same raw agent/session in two workspace roots produced two distinct non-shared scope keys, two distinct Docker container names retaining the workspace hash, and Docker openclaw.sessionKey labels matching the workspace-scoped keys. The temporary containers were removed at the end of the proof run.

Notes

  • Existing pre-PR non-shared sandbox containers use the old scope key. After this change, they will be recreated once under workspace-scoped names and the old containers can be removed with openclaw sandbox recreate or aged out through the existing sandbox prune path.

@openclaw-barnacle openclaw-barnacle Bot added docker Docker and sandbox tooling agents Agent runtime and tooling size: S triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jun 25, 2026
@clawsweeper

clawsweeper Bot commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 26, 2026, 2:35 PM ET / 18:35 UTC.

Summary
The PR adds workspace-hashed non-shared sandbox scope keys across Docker, browser, SSH, skills workspace, and recreate paths, documents upgrade behavior, and adds regression coverage.

PR surface: Source +65, Tests +274, Docs +2. Total +341 across 13 files.

Reproducibility: yes. Current main is source-reproducible: non-shared sandbox scope keys and container names are derived without workspace identity, matching the co-hosted instance collision report.

Review metrics: 1 noteworthy metric.

  • Sandbox identity surfaces: 5 runtime identity/cleanup surfaces affected. Docker containers, browser containers, SSH runtime roots, skills workspace paths, and recreate filtering all consume the new workspace-scoped key, so upgrade behavior matters before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #51363
Summary: This PR is the current candidate fix for the canonical Docker sandbox container collision report; the earlier direct fix PR is closed unmerged and the broader multi-instance issue only partially overlaps.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Get maintainer sign-off on the documented one-time non-shared runtime reset before merge.

Risk before merge

  • [P1] Merging this intentionally gives existing non-shared Docker, browser, SSH, skills workspace, and registry identities a one-time reset, so existing runtime state may not be reused after upgrade.
  • [P1] The central scope-key change controls sandbox isolation boundaries; CI and focused proof cover the intended path, but maintainer review should still accept the identity policy before merge.
  • [P1] The canonical issue remains open until this PR lands, so closing or deprioritizing the issue before merge would be premature.

Maintainer options:

  1. Accept the documented non-shared reset (recommended)
    Maintainers can merge after explicitly accepting that old non-shared runtimes are recreated under workspace-scoped identities and cleaned up by recreate or prune.
  2. Require an upgrade migration
    If preserving old non-shared runtime state matters, add a doctor or migration path before merge instead of relying on one-time recreation.
  3. Split identity policy by backend
    If Docker, browser, SSH, and skills workspace identity should not move together, pause this PR and split the backend-specific policy decisions.

Next step before merge

  • No automated repair is needed; the remaining action is maintainer acceptance of the compatibility-sensitive non-shared sandbox identity reset.

Security
Cleared: No concrete security or supply-chain defect remains in the latest diff; the sandbox-boundary changes still need normal maintainer sign-off because of their compatibility impact.

Review details

Best possible solution:

Land the workspace-qualified non-shared scope-key fix once maintainers accept the documented one-time runtime reset and keep shared behavior stable.

Do we have a high-confidence way to reproduce the issue?

Yes. Current main is source-reproducible: non-shared sandbox scope keys and container names are derived without workspace identity, matching the co-hosted instance collision report.

Is this the best way to solve the issue?

Yes, with maintainer acceptance of the upgrade reset. Centralizing workspace identity in the non-shared scope key is the maintainable layer because Docker, browser, SSH, skills workspace, and recreate paths already consume that scope contract.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against e5123e44b0f5.

Label changes

Label justifications:

  • P1: The PR addresses a source-proven sandbox isolation bug that can make co-hosted instances share an exec container and operate on the wrong workspace.
  • merge-risk: 🚨 compatibility: The diff changes persistent runtime names and registry keys, which can make existing non-shared sandbox runtimes stop being reused after upgrade.
  • merge-risk: 🚨 session-state: Changing sandbox identity can hide, reset, or relocate sandbox workspace state across Docker, browser, SSH, and skills workspace paths.
  • merge-risk: 🚨 security-boundary: The changed scope-key and container-name logic is the boundary that prevents separate workspaces from sharing sandbox runtimes.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (terminal): The PR body includes redacted terminal output from a real Docker backend path showing distinct workspace-scoped scope keys, container names, and labels for one raw session in two workspace roots.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes redacted terminal output from a real Docker backend path showing distinct workspace-scoped scope keys, container names, and labels for one raw session in two workspace roots.
Evidence reviewed

PR surface:

Source +65, Tests +274, Docs +2. Total +341 across 13 files.

View PR surface stats
Area Files Added Removed Net
Source 7 94 29 +65
Tests 5 277 3 +274
Docs 1 2 0 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 13 373 32 +341

What I checked:

  • Current main source repro: Current main derives session scope from the raw session key and agent scope from only the agent id, with no workspace discriminator in the sandbox lifetime key. (src/agents/sandbox/shared.ts:32, e5123e44b0f5)
  • Current Docker naming path: Current main builds Docker container names from the scope key slug before workspaceDir participates in config hashing, so identical agent scope keys can address the same container. (src/agents/sandbox/docker.ts:600, e5123e44b0f5)
  • PR scope-key fix: PR head appends an eight-hex hash of the resolved workspace path to session and agent scope keys while preserving shared scope as shared. (src/agents/sandbox/shared.ts:69, 7cfdd2c546c6)
  • PR container-name repair: PR head uses a shared formatSandboxContainerName helper for Docker and browser names so the uniqueness suffix survives Docker's 63-character limit under normal prefixes. (src/agents/sandbox/shared.ts:37, 7cfdd2c546c6)
  • PR Docker threading: PR head passes the resolved scope key into Docker backend creation and uses it for container name, labels, and registry identity. (src/agents/sandbox/docker.ts:597, 7cfdd2c546c6)
  • PR recreate filtering: PR head teaches recreate-by-session to match a workspace-qualified stored session only when it starts with the requested raw session key and has an eight-hex workspace suffix. (src/commands/sandbox.ts:145, 7cfdd2c546c6)

Likely related people:

  • steipete: History shows repeated sandbox Docker/browser, SSH, security hardening, and CLI refactor work in the owner boundary this PR changes. (role: sandbox feature and security contributor; confidence: high; commits: 7066d5e192a6, 14b6eea6e30e, c070be1bc4d0; files: src/agents/sandbox/docker.ts, src/agents/sandbox/browser.ts, src/agents/sandbox/ssh-backend.ts)
  • vincentkoc: Recent and older history touches Docker sandbox environment handling, backend type seams, and changelog/release context around the sandbox surface. (role: adjacent sandbox backend contributor; confidence: medium; commits: 7d6af7e154c9, bd33a340fba0, b48291e01eca; files: src/agents/sandbox/docker.ts, src/agents/sandbox/backend.ts, src/agents/sandbox/ssh.ts)
  • renaudcerrato: The latest substantive SSH sandbox backend work changed workdir validation and backend contract behavior in the same remote runtime path. (role: recent SSH sandbox contributor; confidence: medium; commits: 95b97e5b0b5e; files: src/agents/sandbox/ssh-backend.ts, src/agents/sandbox/ssh-backend.test.ts)
  • sheeek: Earlier history introduced and refactored the sandbox CLI command surface that this PR updates for recreate filtering. (role: sandbox command history contributor; confidence: medium; commits: 5e78d5a21f4b, 1c757ae35eda, b0c97d6178d3; files: src/commands/sandbox.ts, src/cli/sandbox-cli.ts)
  • mushuiyu886: Current blame for the sandbox shared naming, Docker/browser identity, and recreate filtering files points to a recent broad rewrite authored by this account and committed by obviyus. (role: recent current-line contributor; confidence: medium; commits: 4bd68aef657e; files: src/agents/sandbox/shared.ts, src/agents/sandbox/docker.ts, src/agents/sandbox/browser.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 25, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 25, 2026
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime commands Command implementations labels Jun 26, 2026
@tayoun

tayoun commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Jun 26, 2026
@tayoun

tayoun commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Updated the session matcher follow-up at 33ac425ede and refreshed the PR body proof.

Changes:

  • createSessionMatcher now requires a stored workspace-qualified key to start with the requested raw session key before accepting the :workspace:<8-hex> suffix
  • added same-length unrelated-key regressions for container and browser recreate filtering

Verification:

  • node scripts/run-vitest.mjs src/commands/sandbox.test.ts src/agents/sandbox.resolveSandboxContext.test.ts src/agents/sandbox/docker-backend.test.ts src/agents/sandbox/ssh-backend.test.ts
  • pnpm exec oxfmt --check --threads=1 src/commands/sandbox.ts src/commands/sandbox.test.ts
  • node scripts/run-oxlint.mjs src/commands/sandbox.ts src/commands/sandbox.test.ts
  • pnpm tsgo:core
  • pnpm tsgo:core:test
  • git diff --check

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jun 26, 2026
@tayoun
tayoun marked this pull request as ready for review June 26, 2026 18:05
@tayoun
tayoun requested a review from a team as a code owner June 26, 2026 18:05
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Jun 26, 2026
@tayoun

tayoun commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

I addressed the two sandbox identity findings:

  • added prefix-aware Docker/browser runtime name formatting so the workspace/full-key uniqueness suffix stays inside Docker’s 63-character limit under the default Docker and browser prefixes
  • preserved the legacy shared SSH runtime id (openclaw-ssh-shared-8198076c) while keeping the new workspace-qualified behavior for non-shared scope keys

Added focused regressions for default-prefix container collisions and shared SSH id stability.

Verification passed locally:

  • node scripts/run-vitest.mjs src/commands/sandbox.test.ts src/agents/sandbox.resolveSandboxContext.test.ts src/agents/sandbox/docker-backend.test.ts src/agents/sandbox/ssh-backend.test.ts src/agents/sandbox/browser.create.test.ts
  • pnpm exec oxfmt --check --threads=1 src/commands/sandbox.ts src/commands/sandbox.test.ts src/agents/sandbox/shared.ts src/agents/sandbox/ssh-backend.ts src/agents/sandbox/ssh-backend.test.ts src/agents/sandbox/docker.ts src/agents/sandbox/browser.ts src/agents/sandbox.resolveSandboxContext.test.ts src/agents/sandbox/docker-backend.test.ts src/agents/sandbox/browser.create.test.ts
  • node scripts/run-oxlint.mjs src/commands/sandbox.ts src/commands/sandbox.test.ts src/agents/sandbox/shared.ts src/agents/sandbox/ssh-backend.ts src/agents/sandbox/ssh-backend.test.ts src/agents/sandbox/docker.ts src/agents/sandbox/browser.ts src/agents/sandbox.resolveSandboxContext.test.ts src/agents/sandbox/docker-backend.test.ts src/agents/sandbox/browser.create.test.ts
  • pnpm tsgo:core
  • pnpm tsgo:core:test
  • git diff --check

@clawsweeper

clawsweeper Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jun 26, 2026
@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Jul 12, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #clawtributors on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

@vincentkoc

Copy link
Copy Markdown
Member

Superseded by #115766, landed as 30346f9.

A replacement PR was necessary because this branch was already closed and maintainerCanModify=false, while the landed diff needed current-main integration and an additional sandbox-explain regression update. @tayoun's contribution is preserved through the landed commit's co-author credit.

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

Labels

agents Agent runtime and tooling commands Command implementations docker Docker and sandbox tooling docs Improvements or additions to documentation gateway Gateway runtime merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M stale Marked as stale due to inactivity status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants