234 lines
8.1 KiB
TypeScript
234 lines
8.1 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import type { Message } from "@earendil-works/pi-ai";
|
|
import { isFailedResult, isRunningResult, isSuccessfulResult } from "../src/job-state.ts";
|
|
import type { SubagentDetails, RunResult } from "../src/types.ts";
|
|
import {
|
|
PER_TASK_OUTPUT_CAP,
|
|
formatParallelSummary,
|
|
renderSubagentCall,
|
|
renderSubagentResult,
|
|
truncateParallelOutput,
|
|
type SubagentRenderTheme,
|
|
} from "../src/render.ts";
|
|
|
|
const theme: SubagentRenderTheme = {
|
|
fg: (_color, text) => text,
|
|
bold: (text) => text,
|
|
};
|
|
|
|
function runResult(overrides: Partial<RunResult> = {}): RunResult {
|
|
return {
|
|
agent: "scout",
|
|
agentSource: "user",
|
|
task: "inspect the code",
|
|
cwd: "/tmp",
|
|
exitCode: 0,
|
|
messages: [],
|
|
output: "done",
|
|
stderr: "",
|
|
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function details(results: RunResult[], overrides: Partial<SubagentDetails> = {}): SubagentDetails {
|
|
return {
|
|
mode: "parallel",
|
|
status: "running",
|
|
agentScope: "user",
|
|
projectAgentsDir: null,
|
|
projectAgentsDirs: [],
|
|
results,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function renderedText(component: { render(width: number): readonly string[] }, width = 200): string {
|
|
return component.render(width).join("\n");
|
|
}
|
|
|
|
test("queued and running tasks are neither failed nor successful", () => {
|
|
for (const status of ["queued", "running"] as const) {
|
|
const result = runResult({ status, exitCode: null, stopReason: "error", terminationSignal: "SIGTERM" });
|
|
assert.equal(isRunningResult(result), true, status);
|
|
assert.equal(isFailedResult(result), false, status);
|
|
assert.equal(isSuccessfulResult(result), false, status);
|
|
}
|
|
});
|
|
|
|
test("terminal failures come from status, exit code, stop reason, or a termination signal", () => {
|
|
for (const status of ["failed", "cancelled", "interrupted"] as const) {
|
|
const result = runResult({ status, exitCode: null });
|
|
assert.equal(isFailedResult(result), true, status);
|
|
assert.equal(isRunningResult(result), false, status);
|
|
assert.equal(isSuccessfulResult(result), false, status);
|
|
}
|
|
|
|
assert.equal(isFailedResult(runResult({ status: undefined, exitCode: 1 })), true);
|
|
assert.equal(isFailedResult(runResult({ status: undefined, exitCode: null })), true);
|
|
assert.equal(isFailedResult(runResult({ stopReason: "error" })), true);
|
|
assert.equal(isFailedResult(runResult({ stopReason: "aborted" })), true);
|
|
assert.equal(isFailedResult(runResult({ terminationSignal: "SIGTERM" })), true);
|
|
assert.equal(isFailedResult(runResult({ status: "succeeded" })), false);
|
|
assert.equal(isSuccessfulResult(runResult({ status: "succeeded" })), true);
|
|
});
|
|
|
|
test("parallel summary counts only settled successful tasks and labels running tasks", () => {
|
|
const summary = formatParallelSummary([
|
|
runResult({ agent: "one", status: "running", exitCode: null, output: "partial" }),
|
|
runResult({ agent: "two", status: "queued", exitCode: null, output: "" }),
|
|
runResult({ agent: "three", status: "succeeded", output: "finished" }),
|
|
runResult({ agent: "four", status: "failed", exitCode: 1, errorMessage: "boom" }),
|
|
]);
|
|
|
|
assert.match(summary, /^Parallel: 1\/4 succeeded/m);
|
|
assert.match(summary, /### \[one\] running\n\npartial/);
|
|
assert.match(summary, /### \[two\] running\n\n\(pending\)/);
|
|
assert.match(summary, /### \[three\] completed\n\nfinished/);
|
|
assert.match(summary, /### \[four\] failed\n\nboom/);
|
|
});
|
|
|
|
test("parallel summary truncates at the byte cap without splitting UTF-8", () => {
|
|
const original = "😀".repeat(PER_TASK_OUTPUT_CAP);
|
|
const truncated = truncateParallelOutput(original);
|
|
const [prefix, note] = truncated.split("\n\n[Output truncated: ");
|
|
|
|
assert.ok(Buffer.byteLength(prefix, "utf8") <= PER_TASK_OUTPUT_CAP);
|
|
assert.equal(prefix.includes("\uFFFD"), false);
|
|
assert.equal(original.startsWith(prefix), true);
|
|
|
|
const omitted = Buffer.byteLength(original, "utf8") - Buffer.byteLength(prefix, "utf8");
|
|
assert.equal(note, `${omitted} bytes omitted. Full output preserved in tool details.]`);
|
|
|
|
const small = "short output";
|
|
assert.equal(truncateParallelOutput(small), small);
|
|
});
|
|
|
|
test("parallel rendering shows running tasks without counting them as failures", () => {
|
|
const result = {
|
|
content: [{ type: "text" as const, text: "Parallel: 1/2 succeeded" }],
|
|
details: details(
|
|
[
|
|
runResult({ agent: "running-agent", status: "running", exitCode: null, output: "working" }),
|
|
runResult({ agent: "finished-agent", status: "succeeded", output: "finished" }),
|
|
],
|
|
{ status: "running" },
|
|
),
|
|
};
|
|
|
|
const text = renderedText(renderSubagentResult(result, { expanded: false }, theme));
|
|
assert.match(text, /1\/2 done, 1 running/);
|
|
assert.match(text, /running-agent/);
|
|
assert.match(text, /working/);
|
|
assert.match(text, /finished-agent/);
|
|
assert.doesNotMatch(text, /failed/);
|
|
assert.doesNotMatch(text, /✗/);
|
|
});
|
|
|
|
test("collapsed rendering shows only the latest activity for each running agent", () => {
|
|
const messages = [
|
|
{
|
|
role: "assistant",
|
|
content: [
|
|
{ type: "text", text: "earlier reasoning" },
|
|
{ type: "toolCall", id: "old", name: "bash", arguments: { command: "old-command" } },
|
|
],
|
|
},
|
|
{
|
|
role: "assistant",
|
|
content: [
|
|
{ type: "text", text: "latest reasoning" },
|
|
{ type: "toolCall", id: "new", name: "read", arguments: { path: "src/latest.ts" } },
|
|
],
|
|
},
|
|
] as unknown as Message[];
|
|
const result = {
|
|
content: [{ type: "text" as const, text: "partial" }],
|
|
details: details([
|
|
runResult({ status: "running", exitCode: null, messages, output: "working" }),
|
|
]),
|
|
};
|
|
|
|
const text = renderedText(renderSubagentResult(result, { expanded: false }, theme));
|
|
assert.match(text, /src\/latest\.ts/);
|
|
assert.match(text, /\[running\]/);
|
|
assert.doesNotMatch(text, /earlier reasoning|latest reasoning|old-command/);
|
|
});
|
|
|
|
test("expanded parallel rendering keeps live task status and tool details", () => {
|
|
const messages = [{
|
|
role: "assistant",
|
|
content: [{ type: "toolCall", id: "call", name: "bash", arguments: { command: "npm test" } }],
|
|
}] as unknown as Message[];
|
|
const result = {
|
|
content: [{ type: "text" as const, text: "partial" }],
|
|
details: details([runResult({ status: "running", exitCode: null, messages, output: "working" })]),
|
|
};
|
|
|
|
const text = renderedText(renderSubagentResult(result, { expanded: true }, theme));
|
|
assert.match(text, /\[running\]/);
|
|
assert.match(text, /npm test/);
|
|
assert.match(text, /working/);
|
|
});
|
|
|
|
test("expanded parallel rendering uses RunResult.output for final text", () => {
|
|
const result = {
|
|
content: [{ type: "text" as const, text: "ignored" }],
|
|
details: details(
|
|
[
|
|
runResult({
|
|
status: "succeeded",
|
|
task: "write the answer",
|
|
output: "final assistant output",
|
|
}),
|
|
],
|
|
{ status: "succeeded" },
|
|
),
|
|
};
|
|
|
|
const text = renderedText(renderSubagentResult(result, { expanded: true }, theme), 120);
|
|
assert.match(text, /write the answer/);
|
|
assert.match(text, /final assistant output/);
|
|
});
|
|
|
|
test("call previews preserve mode, scope, and task summaries", () => {
|
|
const chain = renderedText(
|
|
renderSubagentCall(
|
|
{
|
|
agentScope: "project",
|
|
chain: [
|
|
{ agent: "one", task: "first task" },
|
|
{ agent: "two", task: "second {previous} task" },
|
|
],
|
|
},
|
|
theme,
|
|
),
|
|
);
|
|
assert.match(chain, /chain \(2 steps\)/);
|
|
assert.match(chain, /\[project\]/);
|
|
assert.match(chain, /one/);
|
|
assert.match(chain, /second\s+task/);
|
|
|
|
const single = renderedText(renderSubagentCall({ agent: "scout", task: "inspect this" }, theme));
|
|
assert.match(single, /subagent/);
|
|
assert.match(single, /scout/);
|
|
assert.match(single, /inspect this/);
|
|
});
|
|
|
|
test("skipped tasks are terminal but neither failed nor successful", () => {
|
|
const result = runResult({ status: "skipped", exitCode: null, output: "" });
|
|
assert.equal(isRunningResult(result), false);
|
|
assert.equal(isFailedResult(result), false);
|
|
assert.equal(isSuccessfulResult(result), false);
|
|
});
|
|
|
|
test("parallel summary labels skipped tasks instead of counting them as completed", () => {
|
|
const summary = formatParallelSummary([
|
|
runResult({ agent: "one", status: "succeeded", output: "finished" }),
|
|
runResult({ agent: "two", status: "skipped", exitCode: null, output: "" }),
|
|
]);
|
|
assert.match(summary, /^Parallel: 1\/2 succeeded/m);
|
|
assert.match(summary, /### \[two\] skipped/);
|
|
});
|