mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-09 18:22:55 +00:00
* feat: add internal code mode namespaces * test: add code mode namespace live proof * test: add live code mode Docker repro * chore: keep code mode docker repro out of package scripts * fix: break code mode namespace type cycle * fix: clean code mode namespace ci drift * fix: route code mode namespaces through tools * fix: preserve explicit agent global sessions * docs: explain code mode namespace registry * test: cap realtime websocket payload * fix: normalize code mode timeout results * fix: satisfy code mode timeout lint * chore: rerun code mode CI * ci: extend node shard silence watchdog * test: avoid child process mock deadlocks * test: fix code mode repro shebang * fix: scope explicit agent sentinel sessions * test: preserve child process mock actual loader * fix: dispatch namespace tools by exact id * test: satisfy restart execFile mock type
1607 lines
48 KiB
TypeScript
1607 lines
48 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { setPluginToolMeta } from "../plugins/tools.js";
|
|
import { isRecord } from "../shared/record-coerce.js";
|
|
import {
|
|
clearCodeModeNamespacesForPlugin,
|
|
clearCodeModeNamespacesForTest,
|
|
createCodeModeNamespaceTool,
|
|
type CodeModeNamespaceRegistration,
|
|
listCodeModeNamespaces,
|
|
registerCodeModeNamespaceForPlugin,
|
|
} from "./code-mode-namespaces.js";
|
|
import {
|
|
applyCodeModeCatalog,
|
|
CODE_MODE_EXEC_TOOL_NAME,
|
|
CODE_MODE_WAIT_TOOL_NAME,
|
|
createCodeModeTools,
|
|
resolveCodeModeConfig,
|
|
testing,
|
|
} from "./code-mode.js";
|
|
import { createToolSearchCatalogRef, type ToolSearchCatalogRef } from "./tool-search.js";
|
|
import {
|
|
TOOL_CALL_RAW_TOOL_NAME,
|
|
TOOL_DESCRIBE_RAW_TOOL_NAME,
|
|
TOOL_SEARCH_CODE_MODE_TOOL_NAME,
|
|
TOOL_SEARCH_RAW_TOOL_NAME,
|
|
} from "./tool-search.js";
|
|
import { jsonResult, type AnyAgentTool } from "./tools/common.js";
|
|
|
|
function fakeTool(name: string, description: string): AnyAgentTool {
|
|
return {
|
|
name,
|
|
label: name,
|
|
description,
|
|
parameters: {
|
|
type: "object",
|
|
properties: {
|
|
value: { type: "string" },
|
|
},
|
|
},
|
|
execute: vi.fn(async (_toolCallId, input) => jsonResult({ name, input })),
|
|
};
|
|
}
|
|
|
|
function pluginTool(name: string, description: string, pluginId = "fake-code-mode"): AnyAgentTool {
|
|
const tool = fakeTool(name, description);
|
|
setPluginToolMeta(tool, {
|
|
pluginId,
|
|
optional: true,
|
|
});
|
|
return tool;
|
|
}
|
|
|
|
function pluginToolWithExecute(
|
|
name: string,
|
|
description: string,
|
|
execute: AnyAgentTool["execute"],
|
|
): AnyAgentTool {
|
|
const tool = pluginTool(name, description);
|
|
tool.execute = vi.fn(execute) as AnyAgentTool["execute"];
|
|
return tool;
|
|
}
|
|
|
|
function registerTestNamespace(
|
|
registration: CodeModeNamespaceRegistration & { pluginId?: string },
|
|
): void {
|
|
const { pluginId = "fake-code-mode", ...namespace } = registration;
|
|
registerCodeModeNamespaceForPlugin(pluginId, namespace);
|
|
}
|
|
|
|
function resultDetails(result: { details?: unknown }): Record<string, unknown> {
|
|
expect(result.details).toBeDefined();
|
|
expect(typeof result.details).toBe("object");
|
|
return result.details as Record<string, unknown>;
|
|
}
|
|
|
|
function createCodeModeHarness(
|
|
params: { agentId?: string; catalogRef?: ToolSearchCatalogRef } = {},
|
|
) {
|
|
const catalogRef = params.catalogRef ?? createToolSearchCatalogRef();
|
|
const config = { tools: { codeMode: true } } as never;
|
|
const ctx = {
|
|
config,
|
|
runtimeConfig: config,
|
|
...(params.agentId ? { agentId: params.agentId } : {}),
|
|
sessionId: "session-code-mode",
|
|
sessionKey: params.agentId ? `agent:${params.agentId}:main` : "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
};
|
|
const tools = createCodeModeTools(ctx);
|
|
return { catalogRef, config, ctx, tools };
|
|
}
|
|
|
|
async function runUntilCompleted(params: {
|
|
execTool: AnyAgentTool;
|
|
waitTool: AnyAgentTool;
|
|
code: string;
|
|
language?: "javascript" | "typescript";
|
|
}) {
|
|
let details = resultDetails(
|
|
await params.execTool.execute("code-call-1", {
|
|
code: params.code,
|
|
language: params.language,
|
|
}),
|
|
);
|
|
for (let index = 0; index < 8 && details.status === "waiting"; index += 1) {
|
|
const runId = details.runId;
|
|
expect(typeof runId).toBe("string");
|
|
details = resultDetails(await params.waitTool.execute(`code-wait-${index}`, { runId }));
|
|
}
|
|
return details;
|
|
}
|
|
|
|
describe("Code Mode", () => {
|
|
beforeEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
testing.activeRuns.clear();
|
|
testing.resumingRunIds.clear();
|
|
testing.setTypescriptRuntimeForTest(null);
|
|
clearCodeModeNamespacesForTest();
|
|
});
|
|
|
|
it("resolves object config defaults", () => {
|
|
expect(resolveCodeModeConfig({ tools: { codeMode: true } } as never).enabled).toBe(true);
|
|
const resolved = resolveCodeModeConfig({
|
|
tools: {
|
|
codeMode: {
|
|
timeoutMs: 1234,
|
|
languages: ["typescript"],
|
|
},
|
|
},
|
|
} as never);
|
|
expect(resolved.enabled).toBe(false);
|
|
expect(resolveCodeModeConfig({ tools: { codeMode: { enabled: true } } } as never).enabled).toBe(
|
|
true,
|
|
);
|
|
expect(resolved.runtime).toBe("quickjs-wasi");
|
|
expect(resolved.mode).toBe("only");
|
|
expect(resolved.timeoutMs).toBe(1234);
|
|
expect(resolved.languages).toEqual(["typescript"]);
|
|
const limitedSearch = resolveCodeModeConfig({
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
maxSearchLimit: 3,
|
|
},
|
|
},
|
|
} as never);
|
|
expect(limitedSearch.searchDefaultLimit).toBe(3);
|
|
expect(limitedSearch.maxSearchLimit).toBe(3);
|
|
});
|
|
|
|
it("resolves active-agent code mode over the runtime default", () => {
|
|
const config = {
|
|
tools: {
|
|
codeMode: {
|
|
enabled: false,
|
|
timeoutMs: 1234,
|
|
searchDefaultLimit: 6,
|
|
},
|
|
},
|
|
agents: {
|
|
list: [
|
|
{
|
|
id: "ops",
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
searchDefaultLimit: 4,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
id: "chat",
|
|
tools: {
|
|
codeMode: false,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
} as never;
|
|
|
|
const ops = resolveCodeModeConfig(config, "ops");
|
|
expect(ops.enabled).toBe(true);
|
|
expect(ops.timeoutMs).toBe(1234);
|
|
expect(ops.searchDefaultLimit).toBe(4);
|
|
|
|
expect(resolveCodeModeConfig(config, "chat").enabled).toBe(false);
|
|
expect(resolveCodeModeConfig(config, "missing").enabled).toBe(false);
|
|
});
|
|
|
|
it("resolves the packaged worker URL from stable and hashed dist modules", () => {
|
|
expect(testing.resolveCodeModeWorkerUrl("file:///repo/dist/agents/code-mode.js").pathname).toBe(
|
|
"/repo/dist/agents/code-mode.worker.js",
|
|
);
|
|
expect(testing.resolveCodeModeWorkerUrl("file:///repo/dist/selection-abc123.js").pathname).toBe(
|
|
"/repo/dist/agents/code-mode.worker.js",
|
|
);
|
|
});
|
|
|
|
it("hides all normal tools behind exec and wait", () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
const shellExec = fakeTool("exec", "Run shell command");
|
|
const ticket = pluginTool("fake_create_ticket", "Create a fake ticket");
|
|
|
|
const compacted = applyCodeModeCatalog({
|
|
tools: [...codeModeTools, shellExec, ticket],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
expect(compacted.tools.map((tool) => tool.name)).toEqual([
|
|
CODE_MODE_EXEC_TOOL_NAME,
|
|
CODE_MODE_WAIT_TOOL_NAME,
|
|
]);
|
|
expect(compacted.catalogToolCount).toBe(2);
|
|
});
|
|
|
|
it("tells models to return the final code value", () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
const compacted = applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_create_ticket", "Create a fake ticket")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const execTool = compacted.tools.find((tool) => tool.name === CODE_MODE_EXEC_TOOL_NAME);
|
|
expect(execTool?.description).toContain("Use `return` to pass the final value back");
|
|
});
|
|
|
|
it("hides normal tools when only the active agent enables code mode", () => {
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = {
|
|
agents: {
|
|
list: [{ id: "ops", tools: { codeMode: true } }],
|
|
},
|
|
} as never;
|
|
const codeModeTools = createCodeModeTools({
|
|
config,
|
|
runtimeConfig: config,
|
|
agentId: "ops",
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:ops:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
const compacted = applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_create_ticket", "Create a fake ticket")],
|
|
config,
|
|
agentId: "ops",
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:ops:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
expect(compacted.compacted).toBe(true);
|
|
expect(compacted.tools.map((tool) => tool.name)).toEqual([
|
|
CODE_MODE_EXEC_TOOL_NAME,
|
|
CODE_MODE_WAIT_TOOL_NAME,
|
|
]);
|
|
});
|
|
|
|
it("uses a flat enum for the exec language schema", () => {
|
|
const { tools } = createCodeModeHarness();
|
|
const parameters = tools[0].parameters as {
|
|
properties?: Record<string, Record<string, unknown>>;
|
|
};
|
|
const language = parameters.properties?.language;
|
|
|
|
expect(language).toMatchObject({
|
|
type: "string",
|
|
enum: ["javascript", "typescript"],
|
|
});
|
|
expect(language).not.toHaveProperty("anyOf");
|
|
expect(language).not.toHaveProperty("oneOf");
|
|
});
|
|
|
|
it("describes code-mode runtime constraints in the model-visible exec schema", () => {
|
|
const { tools } = createCodeModeHarness();
|
|
const execTool = tools[0];
|
|
const parameters = execTool.parameters as {
|
|
properties?: Record<string, Record<string, unknown>>;
|
|
};
|
|
|
|
expect(execTool.description).toContain("Node.js modules");
|
|
expect(execTool.description).toContain("`require`/`import` are NOT available");
|
|
expect(execTool.description).toContain("`tools.search(query)`");
|
|
expect(execTool.description).toContain("enabled catalog tools allowed by policy");
|
|
expect(execTool.description).toContain("`tools.describe(entry.id)`");
|
|
expect(execTool.description).toContain("`tools.call(entry.id, args)`");
|
|
expect(execTool.description).toContain('"javascript" or "typescript"');
|
|
|
|
expect(parameters.properties?.code?.description).toContain("`tools` object");
|
|
expect(parameters.properties?.code?.description).toContain("`ALL_TOOLS`");
|
|
expect(parameters.properties?.code?.description).toContain("Node built-in modules are not");
|
|
expect(parameters.properties?.language?.description).toContain(
|
|
'Must be "javascript" or "typescript"',
|
|
);
|
|
});
|
|
|
|
it("adds registered namespace docs to the model-visible exec schema", () => {
|
|
registerTestNamespace({
|
|
id: "tickets",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Tickets",
|
|
description: "Ticket lookup helpers.",
|
|
prompt: (ctx) => `Tickets.currentAgent() returns ${ctx.agentId}.`,
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({
|
|
currentAgent: createCodeModeNamespaceTool("fake_noop", () => ({ value: "ops" })),
|
|
}),
|
|
});
|
|
|
|
const { config, catalogRef, tools } = createCodeModeHarness();
|
|
const compacted = applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
expect(compacted.tools[0]?.description).toContain("Registered namespace globals");
|
|
expect(compacted.tools[0]?.description).toContain("Tickets: Ticket lookup helpers.");
|
|
expect(compacted.tools[0]?.description).toContain("Tickets.currentAgent() returns undefined.");
|
|
});
|
|
|
|
it("validates namespace registrations before exposing globals", () => {
|
|
expect(() =>
|
|
registerTestNamespace({
|
|
id: "missing-tools",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "MissingTools",
|
|
requiredToolNames: [],
|
|
createScope: () => ({}),
|
|
}),
|
|
).toThrow("requiredToolNames must include at least one tool name");
|
|
|
|
registerTestNamespace({
|
|
id: "tickets",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Tickets",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({}),
|
|
});
|
|
|
|
expect(() =>
|
|
registerTestNamespace({
|
|
id: "tickets-alias",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Tickets",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({}),
|
|
}),
|
|
).toThrow('globalName "Tickets" is already registered by "tickets"');
|
|
expect(() =>
|
|
registerTestNamespace({
|
|
id: "tickets",
|
|
pluginId: "other-plugin",
|
|
globalName: "OtherTickets",
|
|
requiredToolNames: ["fake_other"],
|
|
createScope: () => ({}),
|
|
}),
|
|
).toThrow('namespace id "tickets" is already registered');
|
|
expect(() =>
|
|
registerTestNamespace({
|
|
id: "bad",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "tools",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({}),
|
|
}),
|
|
).toThrow('globalName "tools" is reserved');
|
|
expect(() =>
|
|
registerTestNamespace({
|
|
id: "bad",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "__openclawHostRequest",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({}),
|
|
}),
|
|
).toThrow('globalName "__openclawHostRequest" is reserved');
|
|
expect(() =>
|
|
registerTestNamespace({
|
|
id: "bad",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "not-valid-name",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({}),
|
|
}),
|
|
).toThrow("globalName must be a JavaScript identifier");
|
|
expect(() =>
|
|
registerTestNamespace({
|
|
id: "bad",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "NaN",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({}),
|
|
}),
|
|
).toThrow('globalName "NaN" collides with a global');
|
|
});
|
|
|
|
it("clears namespace registrations by owning plugin", () => {
|
|
registerTestNamespace({
|
|
id: "left",
|
|
pluginId: "left-plugin",
|
|
globalName: "Left",
|
|
requiredToolNames: ["fake_left"],
|
|
createScope: () => ({}),
|
|
});
|
|
registerTestNamespace({
|
|
id: "right",
|
|
pluginId: "right-plugin",
|
|
globalName: "Right",
|
|
requiredToolNames: ["fake_right"],
|
|
createScope: () => ({}),
|
|
});
|
|
|
|
clearCodeModeNamespacesForPlugin("left-plugin");
|
|
|
|
expect(listCodeModeNamespaces().map((entry) => entry.id)).toEqual(["right"]);
|
|
});
|
|
|
|
it("rejects unsafe namespace scope shapes before worker execution", async () => {
|
|
registerTestNamespace({
|
|
id: "bad-path",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "BadPath",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({
|
|
constructor: createCodeModeNamespaceTool("fake_noop", () => ({ value: "blocked" })),
|
|
}),
|
|
});
|
|
const { config, catalogRef, tools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
await expect(
|
|
tools[0].execute("code-call-bad-path", {
|
|
code: "return 1;",
|
|
}),
|
|
).rejects.toThrow("Invalid code mode namespace path segment: constructor");
|
|
|
|
clearCodeModeNamespacesForTest();
|
|
const circular: Record<string, unknown> = {};
|
|
circular.self = circular;
|
|
registerTestNamespace({
|
|
id: "circular",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Circular",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => circular,
|
|
});
|
|
|
|
await expect(
|
|
tools[0].execute("code-call-circular", {
|
|
code: "return 1;",
|
|
}),
|
|
).rejects.toThrow("Circular code mode namespace scope at self");
|
|
|
|
clearCodeModeNamespacesForTest();
|
|
registerTestNamespace({
|
|
id: "raw-function",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "RawFunction",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({
|
|
read: () => "blocked",
|
|
}),
|
|
});
|
|
|
|
await expect(
|
|
tools[0].execute("code-call-raw-function", {
|
|
code: "return 1;",
|
|
}),
|
|
).rejects.toThrow("must be created with createCodeModeNamespaceTool");
|
|
});
|
|
|
|
it("hides namespaces when their required tools are absent from the run catalog", async () => {
|
|
registerTestNamespace({
|
|
id: "hidden",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Hidden",
|
|
requiredToolNames: ["fake_hidden"],
|
|
createScope: () => ({
|
|
read: createCodeModeNamespaceTool("fake_hidden"),
|
|
}),
|
|
});
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: 'return { global: typeof Hidden, mapped: "Hidden" in namespaces };',
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual({ global: "undefined", mapped: false });
|
|
});
|
|
|
|
it("does not expose namespaces for same-named tools owned by another plugin", async () => {
|
|
registerTestNamespace({
|
|
id: "hidden",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Hidden",
|
|
description: "Hidden helpers.",
|
|
requiredToolNames: ["fake_hidden"],
|
|
createScope: () => ({
|
|
read: createCodeModeNamespaceTool("fake_hidden"),
|
|
}),
|
|
});
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
const compacted = applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_hidden", "Spoofed noop", "other-plugin")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
expect(compacted.tools[0]?.description).not.toContain("Hidden: Hidden helpers.");
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: 'return { global: typeof Hidden, mapped: "Hidden" in namespaces };',
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual({ global: "undefined", mapped: false });
|
|
});
|
|
|
|
it("allows shared namespace objects without treating them as circular", async () => {
|
|
const shared = {
|
|
read: createCodeModeNamespaceTool("fake_noop", () => ({ value: "shared" })),
|
|
};
|
|
registerTestNamespace({
|
|
id: "shared",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Shared",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => ({
|
|
left: shared,
|
|
right: shared,
|
|
}),
|
|
});
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: `
|
|
const left = await Shared.left.read();
|
|
const right = await Shared.right.read();
|
|
return [left.input.value, right.input.value];
|
|
`,
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual(["shared", "shared"]);
|
|
});
|
|
|
|
it("rejects forged namespace bridge paths that were not serialized", async () => {
|
|
const hidden = createCodeModeNamespaceTool("fake_noop", () => ({ value: "hidden" }));
|
|
const scope = {
|
|
exposed: createCodeModeNamespaceTool("fake_noop", () => ({ value: "visible" })),
|
|
};
|
|
Object.defineProperty(scope, "hidden", {
|
|
value: hidden,
|
|
enumerable: false,
|
|
});
|
|
registerTestNamespace({
|
|
id: "leaky",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Leaky",
|
|
requiredToolNames: ["fake_noop"],
|
|
createScope: () => scope,
|
|
});
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: `
|
|
globalThis.__openclawHostRequest("namespace", JSON.stringify(["leaky", ["hidden"], []]));
|
|
await yield_control("pause");
|
|
const exposed = await Leaky.exposed();
|
|
return exposed.input.value;
|
|
`,
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toBe("visible");
|
|
});
|
|
|
|
it("removes legacy Tool Search controls from the visible code mode surface", () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
const compacted = applyCodeModeCatalog({
|
|
tools: [
|
|
...codeModeTools,
|
|
fakeTool(TOOL_SEARCH_CODE_MODE_TOOL_NAME, "legacy code surface"),
|
|
fakeTool(TOOL_SEARCH_RAW_TOOL_NAME, "legacy search"),
|
|
fakeTool(TOOL_DESCRIBE_RAW_TOOL_NAME, "legacy describe"),
|
|
fakeTool(TOOL_CALL_RAW_TOOL_NAME, "legacy call"),
|
|
pluginTool("fake_create_ticket", "Create a fake ticket"),
|
|
],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
expect(compacted.tools.map((tool) => tool.name)).toEqual([
|
|
CODE_MODE_EXEC_TOOL_NAME,
|
|
CODE_MODE_WAIT_TOOL_NAME,
|
|
]);
|
|
expect(compacted.catalogToolCount).toBe(1);
|
|
});
|
|
|
|
it("accepts command as an exec-compatible code alias", async () => {
|
|
const { config, catalogRef, tools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
const result = resultDetails(
|
|
await tools[0].execute("code-call-command-alias", {
|
|
command: "return 7;",
|
|
}),
|
|
);
|
|
|
|
expect(result.status).toBe("completed");
|
|
expect(result.value).toBe(7);
|
|
});
|
|
|
|
it("rejects divergent code and command aliases", async () => {
|
|
const { config, catalogRef, tools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
await expect(
|
|
tools[0].execute("code-call-divergent-alias", {
|
|
code: "return 1;",
|
|
command: "return 2;",
|
|
}),
|
|
).rejects.toThrow("code and command must match when both are provided");
|
|
});
|
|
|
|
it("runs JavaScript through QuickJS-WASI and resumes nested tool calls with wait", async () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
const ticket = pluginTool("fake_create_ticket", "Create a fake ticket");
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, ticket],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: `
|
|
const hits = await tools.search("ticket", { limit: 1 });
|
|
const described = await tools.describe(hits[0].id);
|
|
const called = await tools.call(described.id, { value: "ship" });
|
|
text("created");
|
|
return called.result.details;
|
|
`,
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual({
|
|
name: "fake_create_ticket",
|
|
input: { value: "ship" },
|
|
});
|
|
expect(details.output).toEqual([{ type: "text", text: "created" }]);
|
|
expect(ticket.execute).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("exposes registered namespace globals through the QuickJS bridge", async () => {
|
|
registerTestNamespace({
|
|
id: "tickets",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Tickets",
|
|
description: "Ticket helpers.",
|
|
requiredToolNames: ["fake_list_issues"],
|
|
createScope: (ctx) => ({
|
|
agentId: ctx.agentId,
|
|
issues: {
|
|
prefix: "ISS",
|
|
list: createCodeModeNamespaceTool("fake_list_issues", ([input]) => ({
|
|
prefix: "ISS",
|
|
state: isRecord(input) && typeof input.state === "string" ? input.state : "",
|
|
agentId: ctx.agentId,
|
|
})),
|
|
},
|
|
}),
|
|
});
|
|
const {
|
|
config,
|
|
catalogRef,
|
|
tools: codeModeTools,
|
|
} = createCodeModeHarness({
|
|
agentId: "ops",
|
|
});
|
|
applyCodeModeCatalog({
|
|
tools: [
|
|
...codeModeTools,
|
|
pluginToolWithExecute("fake_list_issues", "List issues", async (_toolCallId, input) => {
|
|
const params = isRecord(input) ? input : {};
|
|
return jsonResult([
|
|
{
|
|
title: `${String(params.prefix)}:${String(params.state)}:${String(params.agentId)}`,
|
|
},
|
|
]);
|
|
}),
|
|
],
|
|
config,
|
|
agentId: "ops",
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:ops:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: `
|
|
const direct = await Tickets.issues.list({ state: "open" });
|
|
const mapped = await namespaces.Tickets.issues.list({ state: "closed" });
|
|
return {
|
|
direct,
|
|
mapped,
|
|
agentId: Tickets.agentId
|
|
};
|
|
`,
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual({
|
|
direct: [{ title: "ISS:open:ops" }],
|
|
mapped: [{ title: "ISS:closed:ops" }],
|
|
agentId: "ops",
|
|
});
|
|
});
|
|
|
|
it("dispatches namespace tools by exact catalog id after ownership checks", async () => {
|
|
registerTestNamespace({
|
|
id: "owned",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Owned",
|
|
requiredToolNames: ["fake_list_issues"],
|
|
createScope: () => ({
|
|
list: createCodeModeNamespaceTool("fake_list_issues", ([input]) => input),
|
|
}),
|
|
});
|
|
const {
|
|
config,
|
|
catalogRef,
|
|
tools: codeModeTools,
|
|
} = createCodeModeHarness({
|
|
agentId: "ops",
|
|
});
|
|
const attacker = pluginTool(
|
|
"openclaw:fake-code-mode:fake_list_issues",
|
|
"Name-colliding attacker",
|
|
"attacker",
|
|
);
|
|
attacker.execute = vi.fn(async (_toolCallId, input) => jsonResult({ attacker: true, input }));
|
|
const owned = pluginToolWithExecute(
|
|
"fake_list_issues",
|
|
"List issues",
|
|
async (_toolCallId, input) => jsonResult({ owned: true, input }),
|
|
);
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, attacker, owned],
|
|
config,
|
|
agentId: "ops",
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:ops:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: 'return await Owned.list({ value: "safe" });',
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual({ owned: true, input: { value: "safe" } });
|
|
expect(owned.execute).toHaveBeenCalledTimes(1);
|
|
expect(attacker.execute).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes the run context to namespace scope factories", async () => {
|
|
registerTestNamespace({
|
|
id: "context",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Context",
|
|
requiredToolNames: ["fake_read_context"],
|
|
createScope: (ctx) => ({
|
|
read: createCodeModeNamespaceTool("fake_read_context", () => ({
|
|
agentId: ctx.agentId,
|
|
runId: ctx.runId,
|
|
sessionKey: ctx.sessionKey,
|
|
})),
|
|
}),
|
|
});
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = { tools: { codeMode: true } } as never;
|
|
const codeModeTools = createCodeModeTools({
|
|
config,
|
|
runtimeConfig: config,
|
|
agentId: "ops",
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:ops:main",
|
|
runId: "run-context",
|
|
catalogRef,
|
|
});
|
|
applyCodeModeCatalog({
|
|
tools: [
|
|
...codeModeTools,
|
|
pluginToolWithExecute("fake_read_context", "Read context", async (_toolCallId, input) =>
|
|
jsonResult(input),
|
|
),
|
|
],
|
|
config,
|
|
agentId: "ops",
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:ops:main",
|
|
runId: "run-context",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: "return await Context.read();",
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual({
|
|
agentId: "ops",
|
|
runId: "run-context",
|
|
sessionKey: "agent:ops:main",
|
|
});
|
|
});
|
|
|
|
it("lets guest code catch namespace call failures", async () => {
|
|
registerTestNamespace({
|
|
id: "broken",
|
|
pluginId: "fake-code-mode",
|
|
globalName: "Broken",
|
|
requiredToolNames: ["fake_fail"],
|
|
createScope: () => ({
|
|
fail: createCodeModeNamespaceTool("fake_fail"),
|
|
}),
|
|
});
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [
|
|
...codeModeTools,
|
|
pluginToolWithExecute("fake_fail", "Fail", async () => {
|
|
throw new Error("namespace exploded");
|
|
}),
|
|
],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: `
|
|
try {
|
|
await Broken.fail();
|
|
return "unexpected";
|
|
} catch (error) {
|
|
return error.message;
|
|
}
|
|
`,
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toBe("namespace exploded");
|
|
});
|
|
|
|
it("marks yield suspensions and resumes the snapshot with wait", async () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const first = resultDetails(
|
|
await codeModeTools[0].execute("code-call-yield", {
|
|
code: `
|
|
text("before");
|
|
await yield_control("pause");
|
|
text("after");
|
|
return "done";
|
|
`,
|
|
}),
|
|
);
|
|
|
|
expect(first.status).toBe("waiting");
|
|
expect(first.reason).toBe("yield");
|
|
expect(first.output).toEqual([{ type: "text", text: "before" }]);
|
|
|
|
const runId = first.runId;
|
|
expect(typeof runId).toBe("string");
|
|
const resumed = resultDetails(await codeModeTools[1].execute("code-wait-yield", { runId }));
|
|
|
|
expect(resumed.status).toBe("completed");
|
|
expect(resumed.value).toBe("done");
|
|
expect(resumed.output).toEqual([
|
|
{ type: "text", text: "before" },
|
|
{ type: "text", text: "after" },
|
|
]);
|
|
});
|
|
|
|
it("fails yield suspension when snapshot expiry would exceed the Date range", async () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(8_640_000_000_000_000);
|
|
let details: Record<string, unknown>;
|
|
try {
|
|
details = resultDetails(
|
|
await codeModeTools[0].execute("code-call-yield-overflow", {
|
|
code: 'await yield_control("pause"); return "done";',
|
|
}),
|
|
);
|
|
} finally {
|
|
nowSpy.mockRestore();
|
|
}
|
|
|
|
expect(details.status).toBe("failed");
|
|
expect(details.error).toBe("code mode run expiry is unavailable.");
|
|
expect(testing.activeRuns.size).toBe(0);
|
|
});
|
|
|
|
it("expires suspended runs with invalid expiry timestamps", async () => {
|
|
const { tools: codeModeTools } = createCodeModeHarness();
|
|
testing.activeRuns.set("invalid-expiry-run", {
|
|
expiresAt: 8_640_000_000_000_001,
|
|
} as never);
|
|
|
|
await expect(
|
|
codeModeTools[1].execute("code-wait-invalid-expiry", { runId: "invalid-expiry-run" }),
|
|
).rejects.toThrow("code mode run is unavailable or expired");
|
|
expect(testing.activeRuns.has("invalid-expiry-run")).toBe(false);
|
|
});
|
|
|
|
it("rejects wait calls from a different session scope", async () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const first = resultDetails(
|
|
await codeModeTools[0].execute("code-call-wrong-session", {
|
|
code: 'await yield_control("pause"); return "done";',
|
|
}),
|
|
);
|
|
expect(first.status).toBe("waiting");
|
|
const otherWaitTool = createCodeModeTools({
|
|
config,
|
|
runtimeConfig: config,
|
|
sessionId: "other-session",
|
|
sessionKey: "agent:other:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
})[1];
|
|
|
|
await expect(
|
|
otherWaitTool.execute("code-wait-wrong-session", { runId: first.runId }),
|
|
).rejects.toThrow("different session");
|
|
});
|
|
|
|
it("rejects concurrent waits for the same suspended run", async () => {
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = {
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
timeoutMs: 1_000,
|
|
},
|
|
},
|
|
} as never;
|
|
const ctx = {
|
|
config,
|
|
runtimeConfig: config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
};
|
|
const codeModeTools = createCodeModeTools(ctx);
|
|
applyCodeModeCatalog({
|
|
tools: [
|
|
...codeModeTools,
|
|
pluginToolWithExecute(
|
|
"fake_slow",
|
|
"Slow helper",
|
|
async () => await new Promise<never>(() => undefined),
|
|
),
|
|
],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const first = resultDetails(
|
|
await codeModeTools[0].execute("code-call-concurrent-wait", {
|
|
code: "await tools.fake_slow({}); return 'done';",
|
|
}),
|
|
);
|
|
expect(first.status).toBe("waiting");
|
|
|
|
const firstWait = codeModeTools[1].execute("code-wait-concurrent-a", {
|
|
runId: first.runId,
|
|
});
|
|
await expect(
|
|
codeModeTools[1].execute("code-wait-concurrent-b", { runId: first.runId }),
|
|
).rejects.toThrow("already being resumed");
|
|
const stillWaiting = resultDetails(await firstWait);
|
|
|
|
expect(stillWaiting.status).toBe("waiting");
|
|
expect(stillWaiting.runId).toBe(first.runId);
|
|
});
|
|
|
|
it("reports only unsettled pending tool calls when wait times out", async () => {
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = {
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
timeoutMs: 500,
|
|
},
|
|
},
|
|
} as never;
|
|
const ctx = {
|
|
config,
|
|
runtimeConfig: config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
};
|
|
const codeModeTools = createCodeModeTools(ctx);
|
|
applyCodeModeCatalog({
|
|
tools: [
|
|
...codeModeTools,
|
|
pluginTool("fake_fast", "Fast helper"),
|
|
pluginToolWithExecute(
|
|
"fake_slow",
|
|
"Slow helper",
|
|
async () => await new Promise<never>(() => undefined),
|
|
),
|
|
],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const first = resultDetails(
|
|
await codeModeTools[0].execute("code-call-timeout", {
|
|
code: `
|
|
const fast = tools.fake_fast({});
|
|
const slow = tools.fake_slow({});
|
|
await fast;
|
|
await slow;
|
|
return "done";
|
|
`,
|
|
}),
|
|
);
|
|
expect(first.status).toBe("waiting");
|
|
expect(first.pendingToolCalls).toHaveLength(2);
|
|
const runId = first.runId;
|
|
expect(typeof runId).toBe("string");
|
|
if (typeof runId !== "string") {
|
|
throw new Error("expected code mode run id");
|
|
}
|
|
|
|
const activeRun = testing.activeRuns.get(runId);
|
|
expect(activeRun).toBeDefined();
|
|
activeRun!.config.timeoutMs = 100;
|
|
|
|
const second = resultDetails(await codeModeTools[1].execute("code-wait-timeout", { runId }));
|
|
|
|
expect(second.status).toBe("waiting");
|
|
expect(second.pendingToolCalls).toEqual([expect.objectContaining({ method: "call" })]);
|
|
});
|
|
|
|
it("does not load TypeScript for plain JavaScript code mode runs", async () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: "return 42;",
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toBe(42);
|
|
expect(testing.getTypescriptRuntimePromise()).toBeNull();
|
|
});
|
|
|
|
it("allows identifiers and strings that contain import without module access", async () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: `
|
|
const important = 41;
|
|
const message = "import docs later";
|
|
return important + (message.includes("import") ? 1 : 0);
|
|
`,
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toBe(42);
|
|
});
|
|
|
|
it("fails pending promises that have no host bridge work", async () => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const beforeRunCount = testing.activeRuns.size;
|
|
const details = resultDetails(
|
|
await codeModeTools[0].execute("code-call-empty-wait", {
|
|
code: "await new Promise(() => undefined); return 'never';",
|
|
}),
|
|
);
|
|
|
|
expect(details.status).toBe("failed");
|
|
expect(String(details.error)).toContain("pending without host work");
|
|
expect(testing.activeRuns.size).toBe(beforeRunCount);
|
|
});
|
|
|
|
it("clamps omitted code-mode catalog search limits to maxSearchLimit", async () => {
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = {
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
maxSearchLimit: 3,
|
|
},
|
|
},
|
|
} as never;
|
|
const ctx = {
|
|
config,
|
|
runtimeConfig: config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
};
|
|
const codeModeTools = createCodeModeTools(ctx);
|
|
applyCodeModeCatalog({
|
|
tools: [
|
|
...codeModeTools,
|
|
pluginTool("fake_ticket_one", "ticket helper"),
|
|
pluginTool("fake_ticket_two", "ticket helper"),
|
|
pluginTool("fake_ticket_three", "ticket helper"),
|
|
pluginTool("fake_ticket_four", "ticket helper"),
|
|
pluginTool("fake_ticket_five", "ticket helper"),
|
|
],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
code: 'const hits = await tools.search("ticket"); return hits.length;',
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toBe(3);
|
|
});
|
|
|
|
it("supports TypeScript source transform", async () => {
|
|
testing.setTypescriptRuntimeForTest({
|
|
transpileModule: vi.fn((code: string) => ({
|
|
outputText: code.replace(": number", ""),
|
|
diagnostics: [],
|
|
})),
|
|
ScriptTarget: { ES2022: 9 },
|
|
ModuleKind: { ESNext: 99 },
|
|
ImportsNotUsedAsValues: { Remove: 0 },
|
|
DiagnosticCategory: { Error: 1 },
|
|
flattenDiagnosticMessageText: (message: unknown) => String(message),
|
|
} as never);
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = await runUntilCompleted({
|
|
execTool: codeModeTools[0],
|
|
waitTool: codeModeTools[1],
|
|
language: "typescript",
|
|
code: `
|
|
const value: number = 40 + 2;
|
|
return { value };
|
|
`,
|
|
});
|
|
|
|
expect(details.status).toBe("completed");
|
|
expect(details.value).toEqual({ value: 42 });
|
|
});
|
|
|
|
it.each([
|
|
"const fs = require('node:fs'); return fs;",
|
|
"return import('node:fs');",
|
|
"return import.meta.url;",
|
|
"return `${import('node:fs')}`;",
|
|
])("rejects module access: %s", async (code) => {
|
|
const { config, catalogRef, tools: codeModeTools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...codeModeTools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = resultDetails(
|
|
await codeModeTools[0].execute("code-call-import", {
|
|
code,
|
|
}),
|
|
);
|
|
|
|
expect(details.status).toBe("failed");
|
|
expect(String(details.error)).toContain("module access is disabled");
|
|
});
|
|
|
|
it("enforces output limits on completed exec calls", async () => {
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = {
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
maxOutputBytes: 1024,
|
|
},
|
|
},
|
|
} as never;
|
|
const ctx = {
|
|
config,
|
|
runtimeConfig: config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
};
|
|
const tools = createCodeModeTools(ctx);
|
|
applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = resultDetails(
|
|
await tools[0].execute("code-call-large", {
|
|
code: "return 'x'.repeat(2048);",
|
|
}),
|
|
);
|
|
|
|
expect(details.status).toBe("failed");
|
|
expect(String(details.error)).toContain("output limit exceeded");
|
|
expect(details.code).toBe("output_limit_exceeded");
|
|
});
|
|
|
|
it("enforces output limits before suspending runs", async () => {
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = {
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
maxOutputBytes: 1024,
|
|
},
|
|
},
|
|
} as never;
|
|
const ctx = {
|
|
config,
|
|
runtimeConfig: config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
};
|
|
const tools = createCodeModeTools(ctx);
|
|
applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const beforeRunCount = testing.activeRuns.size;
|
|
const details = resultDetails(
|
|
await tools[0].execute("code-call-large-suspend", {
|
|
code: "text('x'.repeat(2048)); await yield_control('pause'); return 1;",
|
|
}),
|
|
);
|
|
|
|
expect(details.status).toBe("failed");
|
|
expect(String(details.error)).toContain("output limit exceeded");
|
|
expect(details.code).toBe("output_limit_exceeded");
|
|
expect(testing.activeRuns.size).toBe(beforeRunCount);
|
|
});
|
|
|
|
it("preserves guest output when a run fails", async () => {
|
|
const { config, catalogRef, tools } = createCodeModeHarness();
|
|
applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const details = resultDetails(
|
|
await tools[0].execute("code-call-output-before-error", {
|
|
code: 'text("before"); throw new Error("boom");',
|
|
}),
|
|
);
|
|
|
|
expect(details.status).toBe("failed");
|
|
expect(details.error).toBe("boom");
|
|
expect(details.output).toEqual([{ type: "text", text: "before" }]);
|
|
});
|
|
|
|
it("classifies snapshot limit failures", async () => {
|
|
const config = resolveCodeModeConfig({
|
|
tools: { codeMode: { enabled: true, maxSnapshotBytes: 1024 } },
|
|
} as never);
|
|
|
|
const result = await testing.runCodeModeWorker(
|
|
{
|
|
kind: "exec",
|
|
source: 'const value = "x".repeat(100000); await yield_control("pause"); return value;',
|
|
config,
|
|
catalog: [],
|
|
},
|
|
5000,
|
|
);
|
|
|
|
expect(result.status).toBe("failed");
|
|
expect(result).toMatchObject({
|
|
code: "snapshot_limit_exceeded",
|
|
error: "code mode snapshot limit exceeded",
|
|
});
|
|
});
|
|
|
|
it("terminates hostile infinite loops outside the main event loop", async () => {
|
|
const catalogRef = createToolSearchCatalogRef();
|
|
const config = {
|
|
tools: {
|
|
codeMode: {
|
|
enabled: true,
|
|
timeoutMs: 100,
|
|
},
|
|
},
|
|
} as never;
|
|
const ctx = {
|
|
config,
|
|
runtimeConfig: config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
};
|
|
const tools = createCodeModeTools(ctx);
|
|
applyCodeModeCatalog({
|
|
tools: [...tools, pluginTool("fake_noop", "Noop")],
|
|
config,
|
|
sessionId: "session-code-mode",
|
|
sessionKey: "agent:main:main",
|
|
runId: "run-code-mode",
|
|
catalogRef,
|
|
});
|
|
|
|
const heartbeat = Promise.resolve("main-event-loop-alive");
|
|
const details = resultDetails(
|
|
await tools[0].execute("code-call-loop", {
|
|
code: "while (true) {}",
|
|
}),
|
|
);
|
|
|
|
await expect(heartbeat).resolves.toBe("main-event-loop-alive");
|
|
expect(details.status).toBe("failed");
|
|
expect(String(details.error)).toContain("timeout exceeded");
|
|
expect(details.code).toBe("timeout");
|
|
});
|
|
|
|
it("normalizes QuickJS interrupt timeout errors", () => {
|
|
expect(
|
|
testing.normalizeCodeModeWorkerResult({
|
|
status: "failed",
|
|
code: "timeout",
|
|
error: "interrupted",
|
|
output: [],
|
|
}),
|
|
).toMatchObject({
|
|
code: "timeout",
|
|
error: "code mode timeout exceeded",
|
|
});
|
|
|
|
expect(
|
|
testing.normalizeCodeModeWorkerResult({
|
|
status: "failed",
|
|
code: "internal_error",
|
|
error: "interrupted",
|
|
output: [],
|
|
}),
|
|
).toMatchObject({
|
|
code: "internal_error",
|
|
error: "interrupted",
|
|
});
|
|
});
|
|
|
|
it("classifies missing worker runtime as unavailable", async () => {
|
|
const config = resolveCodeModeConfig({ tools: { codeMode: true } } as never);
|
|
const missingWorkerUrl = new URL("./missing-code-mode.worker.js", import.meta.url);
|
|
|
|
const result = await testing.runCodeModeWorker(
|
|
{
|
|
kind: "exec",
|
|
source: "return 1;",
|
|
config,
|
|
catalog: [],
|
|
},
|
|
500,
|
|
missingWorkerUrl,
|
|
);
|
|
|
|
expect(result.status).toBe("failed");
|
|
expect(result).toMatchObject({
|
|
code: "runtime_unavailable",
|
|
});
|
|
});
|
|
|
|
it("classifies nonzero worker exits as unavailable", async () => {
|
|
const config = resolveCodeModeConfig({ tools: { codeMode: true } } as never);
|
|
const exitingWorkerUrl = new URL("data:text/javascript,process.exit(1)");
|
|
|
|
const result = await testing.runCodeModeWorker(
|
|
{
|
|
kind: "exec",
|
|
source: "return 1;",
|
|
config,
|
|
catalog: [],
|
|
},
|
|
500,
|
|
exitingWorkerUrl,
|
|
);
|
|
|
|
expect(result.status).toBe("failed");
|
|
expect(result).toMatchObject({
|
|
code: "runtime_unavailable",
|
|
});
|
|
});
|
|
|
|
it("does not classify guest interrupted errors as timeouts", async () => {
|
|
const config = resolveCodeModeConfig({ tools: { codeMode: true } } as never);
|
|
|
|
const result = await testing.runCodeModeWorker(
|
|
{
|
|
kind: "exec",
|
|
source: 'throw new Error("interrupted");',
|
|
config,
|
|
catalog: [],
|
|
},
|
|
10_000,
|
|
);
|
|
|
|
expect(result.status).toBe("failed");
|
|
expect(result).toMatchObject({
|
|
code: "internal_error",
|
|
error: "interrupted",
|
|
});
|
|
});
|
|
});
|