Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 100 additions & 1 deletion cli/src/__tests__/agents-invoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { vi, describe, it, expect, afterEach } from "vitest";
import { EventEmitter } from "node:events";
import { PassThrough, Writable } from "node:stream";
import { readFileSync, statSync } from "node:fs";
import path from "node:path";

const { mockSpawn, existsSyncDelegate } = vi.hoisted(() => ({
mockSpawn: vi.fn(),
Expand All @@ -16,7 +18,7 @@ vi.mock("node:fs", async () => {
return { ...actual, existsSync: existsSyncDelegate };
});

import { invokeAgent, type InvokeEvent } from "../agents-invoke.js";
import { invokeAgent, createMessageFile, type InvokeEvent } from "../agents-invoke.js";

function makeFakeChild() {
const stdout = new PassThrough();
Expand All @@ -32,6 +34,7 @@ function makeFakeChild() {
stdout,
stderr,
pid: 99999,
kill: () => true,
});

return { child, stdout, stderr, stdin };
Expand Down Expand Up @@ -528,3 +531,99 @@ describe("invokeAgent", () => {

});
});

describe("createMessageFile", () => {
it("returns distinct files with intact contents for invocations created in the same millisecond", () => {
const handles: ReturnType<typeof createMessageFile>[] = [];
for (let i = 0; i < 25; i++) {
handles.push(createMessageFile(`prompt-${i}`));
}

const paths = new Set(handles.map((h) => h.filePath));
expect(paths.size).toBe(handles.length);

handles.forEach((h, i) => {
expect(readFileSync(h.filePath, "utf8")).toBe(`prompt-${i}`);
});

for (const h of handles) {
h.cleanup();
h.cleanup();
}
const remaining = handles.filter(
(h) => statSync(path.dirname(h.filePath), { throwIfNoEntry: false }) !== undefined,
);
expect(remaining).toHaveLength(0);
});

it(
"creates the prompt file with restrictive permissions (not relying on umask)",
{ skip: process.platform === "win32" },
() => {
const h = createMessageFile("secret");
expect(statSync(path.dirname(h.filePath)).mode & 0o777).toBe(0o700);
expect(statSync(h.filePath).mode & 0o777).toBe(0o600);
h.cleanup();
},
);
});

describe("openclaw file-protocol prompt file concurrency", () => {
it("passes distinct prompt files to concurrent invocations and cleanup of one does not affect the other", async () => {
const messagePaths: string[] = [];
const invokeChildren: ReturnType<typeof makeFakeChild>["child"][] = [];
mockSpawn.mockImplementation((_bin: string, argv: string[]) => {
if (argv[0] === "agents" && argv[1] === "list") {
const listFake = makeFakeChild();
queueMicrotask(() => {
listFake.stdout.write("- main-agent\n");
listFake.stdout.end();
listFake.child.emit("close", 0);
});
return listFake.child;
}
const idx = argv.indexOf("--message-file");
if (idx !== -1) messagePaths.push(argv[idx + 1]);
const fake = makeFakeChild();
invokeChildren.push(fake.child);
return fake.child;
});

const streamA = invokeAgent({
agent: "openclaw",
prompt: "PROMPT-A",
binOverride: BIN_OVERRIDE,
});
const streamB = invokeAgent({
agent: "openclaw",
prompt: "PROMPT-B",
binOverride: BIN_OVERRIDE,
});

for (let i = 0; i < 200 && (messagePaths.length < 2 || invokeChildren.length < 2); i++) {
await new Promise((r) => setTimeout(r, 5));
}
const eventsA = collectStream(streamA);
const eventsB = collectStream(streamB);

expect(messagePaths).toHaveLength(2);
expect(messagePaths[0]).not.toBe(messagePaths[1]);
const contents = new Set(messagePaths.map((p) => readFileSync(p, "utf8")));
expect(contents).toEqual(new Set(["PROMPT-A", "PROMPT-B"]));
const contentBefore = messagePaths.map((p) => readFileSync(p, "utf8"));

invokeChildren[0].stdout.end();
invokeChildren[0].emit("close", 0);
await eventsA;

expect(statSync(messagePaths[1], { throwIfNoEntry: false })).toBeDefined();
expect(readFileSync(messagePaths[1], "utf8")).toBe(contentBefore[1]);

invokeChildren[1].stdout.end();
invokeChildren[1].emit("close", 0);
await eventsB;

expect(statSync(messagePaths[0], { throwIfNoEntry: false })).toBeUndefined();
expect(statSync(messagePaths[1], { throwIfNoEntry: false })).toBeUndefined();
});
});
6 changes: 3 additions & 3 deletions cli/src/agents-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path, { delimiter, join } from "node:path";
* Agent detection — adapted from next/src/lib/agents/detect.ts
*/

export type AgentProtocol = "stdin" | "argv" | "argv-message" | "acp" | "pi-rpc";
export type AgentProtocol = "stdin" | "argv" | "argv-message" | "acp" | "pi-rpc" | "file";

export type ModelOption = { id: string; label: string };

Expand Down Expand Up @@ -47,7 +47,7 @@ export const AGENTS: AgentDef[] = [
bin: "openclaw",
envOverride: "OPENCLAW_BIN",
vendor: "OpenClaw multi-channel agent gateway",
protocol: "argv-message",
protocol: "file",
fallbackModels: [
DEFAULT_MODEL,
{ id: "openrouter/anthropic/claude-opus-4.7", label: "Opus 4.7 (OpenRouter)" },
Expand Down Expand Up @@ -386,4 +386,4 @@ export function detectAgents(): DetectedAgent[] {
}
return { ...base, available: false };
});
}
}
34 changes: 31 additions & 3 deletions cli/src/agents-invoke.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
import { existsSync } from "node:fs";
import { existsSync, mkdtempSync, writeFileSync, rmSync } from "node:fs";
import path from "node:path";
import os from "node:os";
import { resolveOnPath, AGENTS, type AgentDef, type AgentProtocol } from "./agents-detect.js";

export type InvokeOpts = {
Expand Down Expand Up @@ -185,6 +186,18 @@ function envFor(agent: string): NodeJS.ProcessEnv {
return base;
}

export function createMessageFile(prompt: string): {filePath: string; cleanup: () => void;} {
const dir = mkdtempSync(path.join(os.tmpdir(), "html-anything-agent-message-"));
const filePath = path.join(dir, "message.txt");
writeFileSync(filePath, prompt, { encoding: "utf8", mode: 0o600 });
const cleanup = () => {
try {
rmSync(dir, { recursive: true, force: true });
} catch {}
};
return { filePath, cleanup };
}

// ─── stdout parser ────────────────────────────────────────────────────

type AgentParse =
Expand Down Expand Up @@ -458,11 +471,13 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {
const env = envFor(opts.agent);
const promptViaArgv = def.protocol === "argv";
const promptViaMessageFlag = def.protocol === "argv-message";
const promptViaFileFlag = def.protocol === "file";

return new ReadableStream<InvokeEvent>({
async start(controller) {
let closed = false;
let child: ChildProcessWithoutNullStreams | null = null;
let messageFile: { filePath: string; cleanup: () => void } | undefined;

const safeEnqueue = (ev: InvokeEvent) => {
if (closed) return;
Expand All @@ -479,6 +494,12 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {
controller.close();
} catch {}
};
const cleanupMessageFile = () => {
if (messageFile) {
messageFile.cleanup();
messageFile = undefined;
}
};

let argv: string[];
try {
Expand All @@ -504,7 +525,10 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {
}
if (promptViaArgv) argv = [...argv, opts.prompt];
if (promptViaMessageFlag) argv = [...argv, "--message", opts.prompt];

if (promptViaFileFlag) {
messageFile = createMessageFile(opts.prompt);
argv = [...argv, "--message-file", messageFile.filePath];
}
try {
child = spawn(bin, argv, {
cwd: opts.cwd ?? process.cwd(),
Expand All @@ -517,6 +541,7 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {
type: "error",
message: err instanceof Error ? err.message : String(err),
});
cleanupMessageFile();
safeClose();
return;
}
Expand Down Expand Up @@ -563,6 +588,7 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {

child.on("error", (err) => {
safeEnqueue({ type: "error", message: err.message });
cleanupMessageFile();
safeClose();
});

Expand Down Expand Up @@ -604,13 +630,15 @@ export function invokeAgent(opts: InvokeOpts): ReadableStream<InvokeEvent> {
}
}
safeEnqueue({ type: "done", code });
cleanupMessageFile();
safeClose();
});

const onAbort = () => {
try {
child?.kill("SIGTERM");
} catch {}
cleanupMessageFile();
safeClose();
};
opts.signal?.addEventListener("abort", onAbort, { once: true });
Expand All @@ -626,4 +654,4 @@ function errorStream(message: string): ReadableStream<InvokeEvent> {
controller.close();
},
});
}
}
136 changes: 136 additions & 0 deletions next/src/lib/agents/__tests__/invoke.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { describe, it, expect, beforeAll, afterAll } from "vitest";
import { chmodSync, mkdtempSync, writeFileSync, readFileSync, rmSync, statSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { invokeAgent, createMessageFile, type InvokeEvent } from "../invoke";

const BIN_OVERRIDE = process.execPath;

describe("createMessageFile", () => {
it("returns distinct files with intact contents for invocations created in the same millisecond", () => {
const handles: ReturnType<typeof createMessageFile>[] = [];
for (let i = 0; i < 25; i++) {
handles.push(createMessageFile(`prompt-${i}`));
}

const paths = new Set(handles.map((h) => h.filePath));
expect(paths.size).toBe(handles.length);

handles.forEach((h, i) => {
expect(readFileSync(h.filePath, "utf8")).toBe(`prompt-${i}`);
});

for (const h of handles) {
h.cleanup();
h.cleanup();
}
const remaining = handles.filter(
(h) => statSync(path.dirname(h.filePath), { throwIfNoEntry: false }) !== undefined,
);
expect(remaining).toHaveLength(0);
});

it(
"creates the prompt file with restrictive permissions (not relying on umask)",
{ skip: process.platform === "win32" },
() => {
const h = createMessageFile("secret");
expect(statSync(path.dirname(h.filePath)).mode & 0o777).toBe(0o700);
expect(statSync(h.filePath).mode & 0o777).toBe(0o600);
h.cleanup();
},
);
});

describe("openclaw file-protocol prompt file concurrency", () => {
let stubDir: string;
let stubBin: string;

beforeAll(() => {
stubDir = mkdtempSync(path.join(os.tmpdir(), "html-anything-agent-stub-"));
writeFileSync(
path.join(stubDir, "stub.js"),
[
`const { readFileSync } = require("node:fs");`,
`const argv = process.argv.slice(2);`,
`const idx = argv.indexOf("--message-file");`,
`let content = "";`,
`let file = "";`,
`if (idx !== -1) {`,
` file = argv[idx + 1];`,
` try {`,
` content = readFileSync(file, "utf8");`,
` } catch {`,
` content = "STUB_READ_ERROR";`,
` }`,
`}`,
`process.stdout.write(JSON.stringify({`,
` meta: { finalAssistantVisibleText: content, agentMeta: { sessionId: file } },`,
`}));`,
``,
].join("\n"),
);
if (process.platform === "win32") {
stubBin = path.join(stubDir, "stub.cmd");
writeFileSync(stubBin, `@echo off\r\nnode "%~dp0stub.js" %*\r\n`);
} else {
stubBin = path.join(stubDir, "stub.sh");
writeFileSync(stubBin, `#!/bin/sh\nexec node "$(dirname "$0")/stub.js" "$@"\n`);
chmodSync(stubBin, 0o755);
}
});

afterAll(() => {
rmSync(stubDir, { recursive: true, force: true });
});

it("passes distinct prompt files to concurrent invocations and cleanup of one does not affect the other", async () => {
const streamA = invokeAgent({
agent: "openclaw",
prompt: "PROMPT-A",
binOverride: stubBin,
});
const streamB = invokeAgent({
agent: "openclaw",
prompt: "PROMPT-B",
binOverride: stubBin,
});

const [eventsA, eventsB] = await Promise.all([
collectStream(streamA),
collectStream(streamB),
]);

const deltaA = eventsA.find((e): e is Extract<InvokeEvent, { type: "delta" }> => e.type === "delta");
const deltaB = eventsB.find((e): e is Extract<InvokeEvent, { type: "delta" }> => e.type === "delta");
expect(deltaA).toBeDefined();
expect(deltaB).toBeDefined();
expect([deltaA!.text, deltaB!.text].sort()).toEqual(["PROMPT-A", "PROMPT-B"]);

const sessionA = eventsA.find(
(e): e is Extract<InvokeEvent, { type: "meta" }> => e.type === "meta" && e.key === "session",
);
const sessionB = eventsB.find(
(e): e is Extract<InvokeEvent, { type: "meta" }> => e.type === "meta" && e.key === "session",
);
expect(sessionA).toBeDefined();
expect(sessionB).toBeDefined();
expect(sessionA!.value).not.toBe(sessionB!.value);

expect(statSync(path.dirname(sessionA!.value as string), { throwIfNoEntry: false })).toBeUndefined();
expect(statSync(path.dirname(sessionB!.value as string), { throwIfNoEntry: false })).toBeUndefined();
});
});

async function collectStream(
stream: ReadableStream<InvokeEvent>,
): Promise<InvokeEvent[]> {
const events: InvokeEvent[] = [];
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) events.push(value);
}
return events;
}
Loading