refactor(sandbox): normalize main session aliases
This commit is contained in:
@@ -71,4 +71,50 @@ describe("resolveSandboxContext", () => {
|
|||||||
|
|
||||||
vi.doUnmock("node:child_process");
|
vi.doUnmock("node:child_process");
|
||||||
}, 15_000);
|
}, 15_000);
|
||||||
|
|
||||||
|
it("treats main session aliases as main 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, resolveSandboxContext } =
|
||||||
|
await import("./sandbox.js");
|
||||||
|
|
||||||
|
const cfg: ClawdbotConfig = {
|
||||||
|
session: { mainKey: "work" },
|
||||||
|
agents: {
|
||||||
|
defaults: {
|
||||||
|
sandbox: { mode: "non-main", scope: "session" },
|
||||||
|
},
|
||||||
|
list: [{ id: "main" }],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await resolveSandboxContext({
|
||||||
|
config: cfg,
|
||||||
|
sessionKey: "main",
|
||||||
|
workspaceDir: "/tmp/clawdbot-test",
|
||||||
|
}),
|
||||||
|
).toBeNull();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
await ensureSandboxWorkspaceForSession({
|
||||||
|
config: cfg,
|
||||||
|
sessionKey: "work",
|
||||||
|
workspaceDir: "/tmp/clawdbot-test",
|
||||||
|
}),
|
||||||
|
).toBeNull();
|
||||||
|
|
||||||
|
expect(spawn).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
vi.doUnmock("node:child_process");
|
||||||
|
}, 15_000);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -19,12 +19,9 @@ import {
|
|||||||
loadConfig,
|
loadConfig,
|
||||||
STATE_DIR_CLAWDBOT,
|
STATE_DIR_CLAWDBOT,
|
||||||
} from "../config/config.js";
|
} from "../config/config.js";
|
||||||
|
import { resolveAgentMainSessionKey } from "../config/sessions.js";
|
||||||
import { PROVIDER_IDS } from "../providers/registry.js";
|
import { PROVIDER_IDS } from "../providers/registry.js";
|
||||||
import {
|
import { normalizeAgentId, normalizeMainKey } from "../routing/session-key.js";
|
||||||
buildAgentMainSessionKey,
|
|
||||||
normalizeAgentId,
|
|
||||||
normalizeMainKey,
|
|
||||||
} from "../routing/session-key.js";
|
|
||||||
import { defaultRuntime } from "../runtime.js";
|
import { defaultRuntime } from "../runtime.js";
|
||||||
import { resolveUserPath } from "../utils.js";
|
import { resolveUserPath } from "../utils.js";
|
||||||
import {
|
import {
|
||||||
@@ -558,12 +555,35 @@ function resolveMainSessionKeyForSandbox(params: {
|
|||||||
agentId: string;
|
agentId: string;
|
||||||
}): string {
|
}): string {
|
||||||
if (params.cfg?.session?.scope === "global") return "global";
|
if (params.cfg?.session?.scope === "global") return "global";
|
||||||
return buildAgentMainSessionKey({
|
return resolveAgentMainSessionKey({
|
||||||
|
cfg: params.cfg,
|
||||||
agentId: params.agentId,
|
agentId: params.agentId,
|
||||||
mainKey: normalizeMainKey(params.cfg?.session?.mainKey),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveComparableSessionKeyForSandbox(params: {
|
||||||
|
cfg?: ClawdbotConfig;
|
||||||
|
agentId: string;
|
||||||
|
sessionKey: string;
|
||||||
|
}): string {
|
||||||
|
const trimmed = params.sessionKey.trim();
|
||||||
|
if (!trimmed) return trimmed;
|
||||||
|
|
||||||
|
const mainKey = normalizeMainKey(params.cfg?.session?.mainKey);
|
||||||
|
const agentMainSessionKey = resolveAgentMainSessionKey({
|
||||||
|
cfg: params.cfg,
|
||||||
|
agentId: params.agentId,
|
||||||
|
});
|
||||||
|
const isMainAlias =
|
||||||
|
trimmed === "main" ||
|
||||||
|
trimmed === mainKey ||
|
||||||
|
trimmed === agentMainSessionKey;
|
||||||
|
|
||||||
|
if (params.cfg?.session?.scope === "global" && isMainAlias) return "global";
|
||||||
|
if (isMainAlias) return agentMainSessionKey;
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
export function resolveSandboxRuntimeStatus(params: {
|
export function resolveSandboxRuntimeStatus(params: {
|
||||||
cfg?: ClawdbotConfig;
|
cfg?: ClawdbotConfig;
|
||||||
sessionKey?: string;
|
sessionKey?: string;
|
||||||
@@ -584,7 +604,11 @@ export function resolveSandboxRuntimeStatus(params: {
|
|||||||
const sandboxCfg = resolveSandboxConfigForAgent(cfg, agentId);
|
const sandboxCfg = resolveSandboxConfigForAgent(cfg, agentId);
|
||||||
const mainSessionKey = resolveMainSessionKeyForSandbox({ cfg, agentId });
|
const mainSessionKey = resolveMainSessionKeyForSandbox({ cfg, agentId });
|
||||||
const sandboxed = sessionKey
|
const sandboxed = sessionKey
|
||||||
? shouldSandboxSession(sandboxCfg, sessionKey, mainSessionKey)
|
? shouldSandboxSession(
|
||||||
|
sandboxCfg,
|
||||||
|
resolveComparableSessionKeyForSandbox({ cfg, agentId, sessionKey }),
|
||||||
|
mainSessionKey,
|
||||||
|
)
|
||||||
: false;
|
: false;
|
||||||
return {
|
return {
|
||||||
agentId,
|
agentId,
|
||||||
@@ -1305,7 +1329,13 @@ export async function resolveSandboxContext(params: {
|
|||||||
cfg: params.config,
|
cfg: params.config,
|
||||||
agentId,
|
agentId,
|
||||||
});
|
});
|
||||||
if (!shouldSandboxSession(cfg, rawSessionKey, mainSessionKey)) return null;
|
const comparableSessionKey = resolveComparableSessionKeyForSandbox({
|
||||||
|
cfg: params.config,
|
||||||
|
agentId,
|
||||||
|
sessionKey: rawSessionKey,
|
||||||
|
});
|
||||||
|
if (!shouldSandboxSession(cfg, comparableSessionKey, mainSessionKey))
|
||||||
|
return null;
|
||||||
|
|
||||||
await maybePruneSandboxes(cfg);
|
await maybePruneSandboxes(cfg);
|
||||||
|
|
||||||
@@ -1388,7 +1418,13 @@ export async function ensureSandboxWorkspaceForSession(params: {
|
|||||||
cfg: params.config,
|
cfg: params.config,
|
||||||
agentId,
|
agentId,
|
||||||
});
|
});
|
||||||
if (!shouldSandboxSession(cfg, rawSessionKey, mainSessionKey)) return null;
|
const comparableSessionKey = resolveComparableSessionKeyForSandbox({
|
||||||
|
cfg: params.config,
|
||||||
|
agentId,
|
||||||
|
sessionKey: rawSessionKey,
|
||||||
|
});
|
||||||
|
if (!shouldSandboxSession(cfg, comparableSessionKey, mainSessionKey))
|
||||||
|
return null;
|
||||||
|
|
||||||
const agentWorkspaceDir = resolveUserPath(
|
const agentWorkspaceDir = resolveUserPath(
|
||||||
params.workspaceDir?.trim() || DEFAULT_AGENT_WORKSPACE_DIR,
|
params.workspaceDir?.trim() || DEFAULT_AGENT_WORKSPACE_DIR,
|
||||||
|
|||||||
Reference in New Issue
Block a user