Files
pi-subagent/test/workflow-template.test.ts
T

72 lines
3.2 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import {
MAX_EXPANDED_STEP_PROMPT_BYTES,
MAX_INTERPOLATED_STEP_OUTPUT_BYTES,
interpolateWorkflowTask,
workflowRunConcurrency,
} from "../src/workflow-run.ts";
// Independent of the exported constants so a changed budget fails the test.
const OUTPUT_BUDGET = 50 * 1024;
const PROMPT_BUDGET = 100 * 1024;
function context(args: Record<string, string> = {}, outputs: Record<string, string> = {}) {
return { args, outputs: new Map(Object.entries(outputs)) };
}
test("step outputs and args interpolate at release time in one pass", () => {
const template = "Audit {args.glob}: {steps.scan.output}\nThen {steps.scan.output}";
assert.equal(
interpolateWorkflowTask(template, context({ glob: "src/**" }, { scan: "a.ts\nb.ts" })),
"Audit src/**: a.ts\nb.ts\nThen a.ts\nb.ts",
);
assert.equal(interpolateWorkflowTask("no placeholders", context()), "no placeholders");
assert.equal(interpolateWorkflowTask("{previous} stays literal", context()), "{previous} stays literal");
});
test("substituted values are never re-expanded and placeholder-like text stays literal", () => {
const output = "keep {args.glob} and {steps.other.output} literal";
const args = { glob: "{steps.scan.output}", other: "$&" };
const result = interpolateWorkflowTask("{steps.scan.output}|{args.glob}|{args.other}", context(args, { scan: output }));
assert.equal(result, `${output}|{steps.scan.output}|$&`);
assert.equal(interpolateWorkflowTask("{item}", context()), "{item}");
});
test("a single output is bounded to the standard task-output budget with a marker", () => {
assert.equal(MAX_INTERPOLATED_STEP_OUTPUT_BYTES, OUTPUT_BUDGET);
const original = "😀a".repeat(OUTPUT_BUDGET);
const result = interpolateWorkflowTask("{steps.scan.output}", context({}, { scan: original }));
const [prefix, note] = result.split("\n\n[Output truncated: ");
assert.ok(Buffer.byteLength(prefix, "utf8") <= OUTPUT_BUDGET);
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]`);
});
test("the whole expanded brief is bounded to the prompt budget with a visible marker", () => {
assert.equal(MAX_EXPANDED_STEP_PROMPT_BYTES, PROMPT_BUDGET);
const piece = "x".repeat(49 * 1024);
const result = interpolateWorkflowTask(
"{steps.a.output}{steps.b.output}{steps.c.output}",
context({}, { a: piece, b: piece, c: piece }),
);
const [prefix, note] = result.split("\n\n[Prompt truncated: ");
assert.ok(Buffer.byteLength(prefix, "utf8") <= PROMPT_BUDGET);
assert.notEqual(note, undefined);
assert.match(note, /^\d+ bytes omitted\]$/);
const omitted = Buffer.byteLength(`${piece}${piece}${piece}`, "utf8") - Buffer.byteLength(prefix, "utf8");
assert.equal(note, `${omitted} bytes omitted]`);
assert.equal(prefix.includes("\uFFFD"), false);
});
test("the per-run reservation is min(maxWorkers - 1, 4)", () => {
assert.equal(workflowRunConcurrency(2), 1);
assert.equal(workflowRunConcurrency(3), 2);
assert.equal(workflowRunConcurrency(5), 4);
assert.equal(workflowRunConcurrency(32), 4);
});