Skip to content

Commit 7cfdd2c

Browse files
committed
fix(sandbox): preserve scoped runtime identity
1 parent 33ac425 commit 7cfdd2c

6 files changed

Lines changed: 73 additions & 9 deletions

File tree

‎src/agents/sandbox.resolveSandboxContext.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ import path from "node:path";
55
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
66
import type { OpenClawConfig } from "../config/config.js";
77
import { registerSandboxBackend } from "./sandbox/backend.js";
8+
import {
9+
DEFAULT_SANDBOX_BROWSER_PREFIX,
10+
DEFAULT_SANDBOX_CONTAINER_PREFIX,
11+
} from "./sandbox/constants.js";
812
import { ensureSandboxWorkspaceForSession, resolveSandboxContext } from "./sandbox/context.js";
9-
import { resolveSandboxScopeKey, slugifySessionKey } from "./sandbox/shared.js";
13+
import {
14+
formatSandboxContainerName,
15+
resolveSandboxScopeKey,
16+
slugifySessionKey,
17+
} from "./sandbox/shared.js";
1018

1119
const updateRegistryMock = vi.hoisted(() => vi.fn());
1220
const syncSkillsToWorkspaceMock = vi.hoisted(() => vi.fn(async () => undefined));
@@ -72,6 +80,23 @@ describe("resolveSandboxScopeKey", () => {
7280
);
7381
});
7482

83+
it("keeps workspace uniqueness suffixes inside default container name limits", () => {
84+
const first = "agent:poly:msteams:channel-with-shared-readable-prefix-alpha:workspace:1234abcd";
85+
const second = "agent:poly:msteams:channel-with-shared-readable-prefix-beta:workspace:1234abcd";
86+
87+
const dockerFirst = formatSandboxContainerName(DEFAULT_SANDBOX_CONTAINER_PREFIX, first);
88+
const dockerSecond = formatSandboxContainerName(DEFAULT_SANDBOX_CONTAINER_PREFIX, second);
89+
const browserFirst = formatSandboxContainerName(DEFAULT_SANDBOX_BROWSER_PREFIX, first);
90+
const browserSecond = formatSandboxContainerName(DEFAULT_SANDBOX_BROWSER_PREFIX, second);
91+
92+
for (const name of [dockerFirst, dockerSecond, browserFirst, browserSecond]) {
93+
expect(name.length).toBeLessThanOrEqual(63);
94+
expect(name).toMatch(/-workspace-1234abcd-[a-f0-9]{8}$/);
95+
}
96+
expect(dockerFirst).not.toBe(dockerSecond);
97+
expect(browserFirst).not.toBe(browserSecond);
98+
});
99+
75100
it("scopes session sandboxes by workspace", () => {
76101
const first = resolveSandboxScopeKey("session", "agent:poly:msteams:channel-1", {
77102
workspaceDir: "/tmp/openclaw-customers/atica/openclaw/agents/poly/workspace",

‎src/agents/sandbox/browser.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
issueNoVncObserverToken,
5252
} from "./novnc-auth.js";
5353
import { readBrowserRegistry, updateBrowserRegistry } from "./registry.js";
54-
import { resolveSandboxAgentId, slugifySessionKey } from "./shared.js";
54+
import { formatSandboxContainerName, resolveSandboxAgentId } from "./shared.js";
5555
import { isToolAllowed } from "./tool-policy.js";
5656
import type { SandboxBrowserContext, SandboxConfig } from "./types.js";
5757
import { validateNetworkMode } from "./validate-sandbox-security.js";
@@ -231,9 +231,10 @@ export async function ensureSandboxBrowser(params: {
231231
return null;
232232
}
233233

234-
const slug = params.cfg.scope === "shared" ? "shared" : slugifySessionKey(params.scopeKey);
235-
const name = `${params.cfg.browser.containerPrefix}${slug}`;
236-
const containerName = name.slice(0, 63);
234+
const containerName = formatSandboxContainerName(
235+
params.cfg.browser.containerPrefix,
236+
params.scopeKey,
237+
);
237238
const state = await dockerContainerState(containerName);
238239
const browserImage = params.cfg.browser.image ?? DEFAULT_SANDBOX_BROWSER_IMAGE;
239240
const cdpSourceRange = normalizeOptionalString(params.cfg.browser.cdpSourceRange);

‎src/agents/sandbox/docker.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ import {
179179
} from "./config-hash.js";
180180
import { DEFAULT_SANDBOX_IMAGE } from "./constants.js";
181181
import { readRegistryEntry, updateRegistry } from "./registry.js";
182-
import { resolveSandboxAgentId, resolveSandboxScopeKey, slugifySessionKey } from "./shared.js";
182+
import {
183+
formatSandboxContainerName,
184+
resolveSandboxAgentId,
185+
resolveSandboxScopeKey,
186+
} from "./shared.js";
183187
import type { SandboxConfig, SandboxDockerConfig, SandboxWorkspaceAccess } from "./types.js";
184188
import { validateSandboxSecurity } from "./validate-sandbox-security.js";
185189
import {
@@ -603,9 +607,7 @@ export async function ensureSandboxContainer(params: {
603607
resolveSandboxScopeKey(params.cfg.scope, params.sessionKey, {
604608
workspaceDir: params.agentWorkspaceDir,
605609
});
606-
const slug = params.cfg.scope === "shared" ? "shared" : slugifySessionKey(scopeKey);
607-
const name = `${params.cfg.docker.containerPrefix}${slug}`;
608-
const containerName = name.slice(0, 63);
610+
const containerName = formatSandboxContainerName(params.cfg.docker.containerPrefix, scopeKey);
609611
const readOnlyWorkspaceSkillMounts = resolveReadOnlyWorkspaceSkillMounts({
610612
workspaceDir: params.workspaceDir,
611613
agentWorkspaceDir: params.agentWorkspaceDir,

‎src/agents/sandbox/shared.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { resolveUserPath } from "../../utils.js";
1010
import { resolveAgentIdFromSessionKey } from "../agent-scope.js";
1111
import { hashTextSha256 } from "./hash.js";
1212

13+
const DOCKER_NAME_MAX_LENGTH = 63;
14+
1315
/** Converts an arbitrary session key into a bounded filesystem/container-safe slug. */
1416
export function slugifySessionKey(value: string) {
1517
const trimmed = value.trim() || "session";
@@ -32,6 +34,30 @@ export function slugifySessionKey(value: string) {
3234
return `${base}-${hash}`;
3335
}
3436

37+
/** Formats a Docker/container runtime name while preserving the unique slug suffix. */
38+
export function formatSandboxContainerName(prefix: string, scopeKey: string) {
39+
const slug = scopeKey.trim() === "shared" ? "shared" : slugifySessionKey(scopeKey);
40+
const name = `${prefix}${slug}`;
41+
if (name.length <= DOCKER_NAME_MAX_LENGTH) {
42+
return name;
43+
}
44+
45+
const suffix =
46+
slug.match(/-workspace-[a-f0-9]{8}-[a-f0-9]{8}$/i)?.[0] ??
47+
slug.match(/-[a-f0-9]{8}$/i)?.[0] ??
48+
`-${hashTextSha256(slug).slice(0, 8)}`;
49+
const slugBudget = DOCKER_NAME_MAX_LENGTH - prefix.length;
50+
if (slugBudget > suffix.length) {
51+
const baseBudget = slugBudget - suffix.length;
52+
const base = slug.slice(0, baseBudget).replace(/-+$/g, "") || "session".slice(0, baseBudget);
53+
return `${prefix}${base}${suffix}`;
54+
}
55+
56+
const fallbackSuffix = `-${hashTextSha256(name).slice(0, 8)}`;
57+
const prefixBudget = Math.max(0, DOCKER_NAME_MAX_LENGTH - fallbackSuffix.length);
58+
return `${prefix.slice(0, prefixBudget).replace(/-+$/g, "")}${fallbackSuffix}`;
59+
}
60+
3561
/** Resolves the per-session sandbox workspace directory under the configured sandbox root. */
3662
export function resolveSandboxWorkspaceDir(root: string, sessionKey: string) {
3763
const resolvedRoot = resolveUserPath(root);

‎src/agents/sandbox/ssh-backend.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ describe("ssh sandbox backend", () => {
213213
expect(commandParams.remoteCommand).toContain("/remote/openclaw/openclaw-ssh-agent-worker");
214214
});
215215

216+
it("preserves the legacy shared SSH runtime id", () => {
217+
const runtimePaths = resolveSshRuntimePaths("/remote/openclaw", "shared");
218+
219+
expect(runtimePaths.runtimeId).toBe("openclaw-ssh-shared-8198076c");
220+
expect(runtimePaths.runtimeRootDir).toBe("/remote/openclaw/openclaw-ssh-shared-8198076c");
221+
});
222+
216223
it("removes runtimes by deleting the remote scope root", async () => {
217224
await sshSandboxBackendManager.removeRuntime({
218225
entry: {

‎src/agents/sandbox/ssh-backend.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,5 +444,8 @@ export function resolveSshRuntimePaths(
444444
}
445445

446446
function buildSshSandboxRuntimeId(scopeKey: string): string {
447+
if ((scopeKey.trim() || "session") === "shared") {
448+
return "openclaw-ssh-shared-8198076c";
449+
}
447450
return `openclaw-ssh-${slugifySessionKey(scopeKey)}`;
448451
}

0 commit comments

Comments
 (0)