Files
lobster/test/openclaw_agent.test.ts
xingzhouandPeter Steinberger c440ca57d1 fix(runtime): cancelled workflows no longer continue external commands (#119)
* fix(runtime): stop process-backed work on cancellation

* fix(runtime): invalidate cancelled resume state

* Revert "fix(runtime): invalidate cancelled resume state"

This reverts commit 0f7d291f98.

* fix(runtime): narrow cancellation to safe child processes

* fix(runtime): stop after completed search cancellation

* fix(runtime): halt direct pipelines after cancellation

Preserve completed in-flight stage results while preventing later direct pipeline stages from starting after parent cancellation.

* fix(runtime): consume aborted approval resumes

* fix(runtime): cancelled workflows no longer continue external commands

* fix(workflow): propagate custom parent cancellation

* fix(runtime): preserve pre-aborted resume state

* fix(runtime): stop lazy handoff after cancellation

* fix(workflow): close remaining cancellation boundaries

* fix(runtime): preserve workflow resumes during setup cancellation

* fix(runtime): close final cancellation persistence gaps

* fix(runtime): stop lazy handoff after cancellation

* fix(runtime): interrupt blocked lazy handoff reads

* fix(runtime): terminate cancellation process trees

* fix(runtime): await process tree termination

* fix(runtime): terminate workflow process trees

* fix(runtime): bridge CLI cancellation

* fix(cli): preserve cancellation lifecycle

* fix(cli): abort stalled signal-aware commands

* fix(cli): release aborted interactive prompts

* fix(cli): preserve sequential prompt input

* fix(cli): preserve buffered prompt input

* fix(cli): handle prompt EOF after buffered input

* fix(runtime): preserve UTF-8 subprocess output

* fix(workflow): preserve retryable resume before execution

* fix(workflow): roll back cancelled resume replacement

* fix(state): roll back cancelled monitor snapshot

* fix(resume): preserve cancelled gate capabilities

* fix(resume): close cancellation rollback windows

* fix(resume): harden cancellation state cleanup

* fix(runtime): close resumed cancellation gaps

* fix(runtime): preserve cancellation cleanup

* fix(runtime): close cancellation lifecycle gaps

* fix(runtime): harden resumed cancellation boundaries

* fix(workflow): consume timed-out resume capabilities

* fix(workflow): preserve resume policy boundaries

* fix(runtime): preserve cancellation cleanup liveness

* fix(runtime): harden cancellation cleanup

* fix(runtime): stop lazy output after cancellation

* fix(runtime): settle cancellation cleanup

* fix(runtime): preserve safe input resumes

* fix(runtime): prevent consumed resume replays

* fix(runtime): serialize approval resume consumption

* fix(runtime): prevent concurrent safe gate forks

* fix(runtime): close cancellation review gaps

* fix(llm): restore cache after cancelled refresh

* fix(runtime): close remaining cancellation windows

* fix(runtime): preserve cancellation recovery invariants

* fix(runtime): prevent stale resume recovery

* fix(runtime): preserve resume claim recovery

* fix(runtime): retry pre-dispatch claims safely

* fix(state): synchronize rollback-safe reads

* fix(resume): discard cancelled pipeline successors

* fix(runtime): harden cancellation and state locking

* fix(runtime): recover durable cancellation failures

* fix(runtime): preserve legacy workflow cancellation

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-08-13 10:54:20 -07:00

202 lines
5.6 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { access, mkdtemp, readFile, rm } from "node:fs/promises";
import { readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import {
createOpenClawAgentCommand,
runOpenClawAgentCli,
} from "../src/commands/stdlib/openclaw_agent.js";
function streamOf(items: unknown[]) {
return (async function* () {
for (const item of items) yield item;
})();
}
async function fileExists(filePath: string) {
try {
await access(filePath);
return true;
} catch {
return false;
}
}
async function waitForFile(filePath: string, timeoutMs = 2000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (await fileExists(filePath)) return;
await new Promise((resolve) => setTimeout(resolve, 10));
}
throw new Error(`Timed out waiting for ${filePath}`);
}
function processIsRunning(pid: number) {
try {
const stat = readFileSync(`/proc/${pid}/stat`, "utf8");
const state = stat.slice(stat.lastIndexOf(")") + 2, stat.lastIndexOf(")") + 3);
return state !== "Z";
} catch {
return false;
}
}
test("openclaw.agent delegates agent, session, and model selection to OpenClaw", async () => {
const calls: Array<Record<string, unknown>> = [];
const cmd = createOpenClawAgentCommand(async (params) => {
calls.push(params);
return {
runId: "run-1",
status: "ok",
result: { payloads: [{ text: "done" }] },
};
});
const result = await cmd.run({
input: streamOf([{ path: "src/index.ts" }, "plain text"]),
args: {
_: [],
agent: "ops",
prompt: "Review this",
model: "openai/gpt-5.4",
"session-key": "incident-42",
thinking: "high",
timeout: 45,
},
ctx: { env: {}, cwd: "/tmp" },
});
const items: unknown[] = [];
for await (const item of result.output) items.push(item);
assert.deepEqual(items, [
{
runId: "run-1",
status: "ok",
result: { payloads: [{ text: "done" }] },
},
]);
assert.equal(calls.length, 1);
assert.deepEqual(calls[0]?.argv, [
"agent",
"--json",
"--message",
'Review this\n\nPipeline input (JSONL):\n{"path":"src/index.ts"}\n"plain text"',
"--agent",
"ops",
"--model",
"openai/gpt-5.4",
"--session-key",
"incident-42",
"--thinking",
"high",
"--timeout",
"45",
]);
});
test("openclaw.agent requires a message and agent or session target", async () => {
const cmd = createOpenClawAgentCommand(async () => ({}));
const ctx = { env: {}, cwd: "/tmp" };
await assert.rejects(
cmd.run({ input: streamOf([]), args: { _: [], agent: "main" }, ctx }),
/requires --prompt/,
);
await assert.rejects(
cmd.run({ input: streamOf([]), args: { _: [], prompt: "hello" }, ctx }),
/requires --agent/,
);
await assert.rejects(
cmd.run({
input: streamOf([]),
args: { _: [], agent: "main", prompt: "hello", timeout: 1.5 },
ctx,
}),
/non-negative integer/,
);
});
test("OpenClaw CLI runner parses structured JSON output", async () => {
const fixturePath = path.join(process.cwd(), "test", "fixtures", "mock-openclaw-agent.mjs");
const output = await runOpenClawAgentCli({
executable: process.execPath,
argv: [fixturePath, "agent", "--json", "--message", "hello"],
cwd: process.cwd(),
env: process.env,
});
assert.deepEqual(output, {
runId: "fixture-run",
status: "ok",
result: { payloads: [{ text: "fixture reply" }] },
});
});
test("OpenClaw CLI runner preserves the 10 MiB output limit", async () => {
await assert.rejects(
runOpenClawAgentCli({
executable: process.execPath,
argv: ["-e", "process.stdout.write('x'.repeat(10 * 1024 * 1024 + 1))"],
cwd: process.cwd(),
env: process.env,
}),
/openclaw\.agent output exceeded 10485760 bytes/,
);
});
test("OpenClaw CLI runner preserves workflow cancellation", async () => {
const fixturePath = path.join(process.cwd(), "test", "fixtures", "mock-openclaw-agent.mjs");
const controller = new AbortController();
const pending = runOpenClawAgentCli({
executable: process.execPath,
argv: [fixturePath, "--sleep"],
cwd: process.cwd(),
env: process.env,
signal: controller.signal,
});
controller.abort();
await assert.rejects(pending, (error: Error) => error.name === "AbortError");
});
test(
"OpenClaw CLI runner terminates descendant processes on cancellation",
{ skip: process.platform === "win32" },
async () => {
const dir = await mkdtemp(path.join(tmpdir(), "lobster-openclaw-agent-cancel-"));
try {
const fixturePath = path.join(process.cwd(), "test", "fixtures", "mock-openclaw-agent.mjs");
const descendantStarted = path.join(dir, "descendant-started");
const descendantCompleted = path.join(dir, "descendant-completed");
const controller = new AbortController();
const pending = runOpenClawAgentCli({
executable: process.execPath,
argv: [fixturePath, "--spawn-descendant", "--sleep"],
cwd: process.cwd(),
env: {
...process.env,
MOCK_OPENCLAW_AGENT_DESCENDANT_STARTED_FILE: descendantStarted,
MOCK_OPENCLAW_AGENT_DESCENDANT_COMPLETED_FILE: descendantCompleted,
},
signal: controller.signal,
});
await waitForFile(descendantStarted);
const descendantPid = Number(await readFile(descendantStarted, "utf8"));
controller.abort();
await assert.rejects(pending, (error: Error) => error.name === "AbortError");
await new Promise((resolve) => setTimeout(resolve, 700));
assert.equal(processIsRunning(descendantPid), false);
assert.equal(
await fileExists(descendantCompleted),
false,
"the OpenClaw child process must not outlive cancellation",
);
} finally {
await rm(dir, { recursive: true, force: true });
}
},
);