fix(sandbox): avoid sandboxing main DM sessions
This commit is contained in:
74
src/agents/sandbox.resolveSandboxContext.test.ts
Normal file
74
src/agents/sandbox.resolveSandboxContext.test.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
|
||||
describe("resolveSandboxContext", () => {
|
||||
it("does not sandbox the agent main session in non-main mode", async () => {
|
||||
vi.resetModules();
|
||||
|
||||
const spawn = vi.fn(() => {
|
||||
throw new Error("spawn should not be called");
|
||||
});
|
||||
vi.doMock("node:child_process", async (importOriginal) => {
|
||||
const actual =
|
||||
await importOriginal<typeof import("node:child_process")>();
|
||||
return { ...actual, spawn };
|
||||
});
|
||||
|
||||
const { resolveSandboxContext } = await import("./sandbox.js");
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
sandbox: { mode: "non-main", scope: "session" },
|
||||
},
|
||||
list: [{ id: "main" }],
|
||||
},
|
||||
};
|
||||
|
||||
const result = await resolveSandboxContext({
|
||||
config: cfg,
|
||||
sessionKey: "agent:main:main",
|
||||
workspaceDir: "/tmp/clawdbot-test",
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(spawn).not.toHaveBeenCalled();
|
||||
|
||||
vi.doUnmock("node:child_process");
|
||||
}, 15_000);
|
||||
|
||||
it("does not create a sandbox workspace for the agent main session in non-main mode", async () => {
|
||||
vi.resetModules();
|
||||
|
||||
const spawn = vi.fn(() => {
|
||||
throw new Error("spawn should not be called");
|
||||
});
|
||||
vi.doMock("node:child_process", async (importOriginal) => {
|
||||
const actual =
|
||||
await importOriginal<typeof import("node:child_process")>();
|
||||
return { ...actual, spawn };
|
||||
});
|
||||
|
||||
const { ensureSandboxWorkspaceForSession } = await import("./sandbox.js");
|
||||
|
||||
const cfg: ClawdbotConfig = {
|
||||
agents: {
|
||||
defaults: {
|
||||
sandbox: { mode: "non-main", scope: "session" },
|
||||
},
|
||||
list: [{ id: "main" }],
|
||||
},
|
||||
};
|
||||
|
||||
const result = await ensureSandboxWorkspaceForSession({
|
||||
config: cfg,
|
||||
sessionKey: "agent:main:main",
|
||||
workspaceDir: "/tmp/clawdbot-test",
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(spawn).not.toHaveBeenCalled();
|
||||
|
||||
vi.doUnmock("node:child_process");
|
||||
}, 15_000);
|
||||
});
|
||||
@@ -546,11 +546,22 @@ export function resolveSandboxConfigForAgent(
|
||||
function shouldSandboxSession(
|
||||
cfg: SandboxConfig,
|
||||
sessionKey: string,
|
||||
mainKey: string,
|
||||
mainSessionKey: string,
|
||||
) {
|
||||
if (cfg.mode === "off") return false;
|
||||
if (cfg.mode === "all") return true;
|
||||
return sessionKey.trim() !== mainKey.trim();
|
||||
return sessionKey.trim() !== mainSessionKey.trim();
|
||||
}
|
||||
|
||||
function resolveMainSessionKeyForSandbox(params: {
|
||||
cfg?: ClawdbotConfig;
|
||||
agentId: string;
|
||||
}): string {
|
||||
if (params.cfg?.session?.scope === "global") return "global";
|
||||
return buildAgentMainSessionKey({
|
||||
agentId: params.agentId,
|
||||
mainKey: normalizeMainKey(params.cfg?.session?.mainKey),
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveSandboxRuntimeStatus(params: {
|
||||
@@ -571,10 +582,7 @@ export function resolveSandboxRuntimeStatus(params: {
|
||||
});
|
||||
const cfg = params.cfg;
|
||||
const sandboxCfg = resolveSandboxConfigForAgent(cfg, agentId);
|
||||
const mainSessionKey = buildAgentMainSessionKey({
|
||||
agentId,
|
||||
mainKey: normalizeMainKey(cfg?.session?.mainKey),
|
||||
});
|
||||
const mainSessionKey = resolveMainSessionKeyForSandbox({ cfg, agentId });
|
||||
const sandboxed = sessionKey
|
||||
? shouldSandboxSession(sandboxCfg, sessionKey, mainSessionKey)
|
||||
: false;
|
||||
@@ -1293,8 +1301,11 @@ export async function resolveSandboxContext(params: {
|
||||
if (!rawSessionKey) return null;
|
||||
const agentId = resolveAgentIdFromSessionKey(rawSessionKey);
|
||||
const cfg = resolveSandboxConfigForAgent(params.config, agentId);
|
||||
const mainKey = normalizeMainKey(params.config?.session?.mainKey);
|
||||
if (!shouldSandboxSession(cfg, rawSessionKey, mainKey)) return null;
|
||||
const mainSessionKey = resolveMainSessionKeyForSandbox({
|
||||
cfg: params.config,
|
||||
agentId,
|
||||
});
|
||||
if (!shouldSandboxSession(cfg, rawSessionKey, mainSessionKey)) return null;
|
||||
|
||||
await maybePruneSandboxes(cfg);
|
||||
|
||||
@@ -1373,8 +1384,11 @@ export async function ensureSandboxWorkspaceForSession(params: {
|
||||
if (!rawSessionKey) return null;
|
||||
const agentId = resolveAgentIdFromSessionKey(rawSessionKey);
|
||||
const cfg = resolveSandboxConfigForAgent(params.config, agentId);
|
||||
const mainKey = normalizeMainKey(params.config?.session?.mainKey);
|
||||
if (!shouldSandboxSession(cfg, rawSessionKey, mainKey)) return null;
|
||||
const mainSessionKey = resolveMainSessionKeyForSandbox({
|
||||
cfg: params.config,
|
||||
agentId,
|
||||
});
|
||||
if (!shouldSandboxSession(cfg, rawSessionKey, mainSessionKey)) return null;
|
||||
|
||||
const agentWorkspaceDir = resolveUserPath(
|
||||
params.workspaceDir?.trim() || DEFAULT_AGENT_WORKSPACE_DIR,
|
||||
|
||||
Reference in New Issue
Block a user