Turn the subagent extension into the pi-subagent Pi package
Move the extension from ~/.pi/agent/extensions/subagent into this repo and package it for pi install: - Sources under src/, entry exposed via the pi manifest in package.json - Host packages (@earendil-works/*, typebox) stay peer dependencies - Tests, TLA+ spec, and research notes move along unchanged in behavior - Gates (.pi/verify.json): typecheck, tests, tlc, smoke — all green - Install: pi install git:git.rwiesner.com/alex/pi-subagent or local path
This commit is contained in:
1 parent
496e6334c9
commit
4deccb95ef
33 files changed
+7180
No files matched your search
@@ -0,0 +1,278 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { SubagentManager } from "../src/manager.ts";
|
||||
import { FakeRunner, invocation } from "./helpers.ts";
|
||||
|
||||
test("async single returns an ID before completion and later exposes its result", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner);
|
||||
|
||||
const job = manager.startAsync(invocation());
|
||||
assert.match(job.id, /^job_/);
|
||||
assert.equal(job.status, "running");
|
||||
assert.equal(runner.calls.length, 1);
|
||||
|
||||
runner.complete("task 1", "finished");
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
assert.equal(terminal.status, "succeeded");
|
||||
assert.equal(terminal.results.length, 1);
|
||||
assert.equal(terminal.results[0].output, "finished");
|
||||
assert.equal(terminal.results[0].status, "succeeded");
|
||||
});
|
||||
|
||||
test("parallel preserves partial results and does not finish while another task can run", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, { maxWorkers: 1 });
|
||||
|
||||
const job = manager.startAsync(invocation("parallel"));
|
||||
assert.equal(runner.calls.length, 1);
|
||||
|
||||
runner.complete("task 1", "bad", { exitCode: 1, errorMessage: "boom" });
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal(manager.get(job.id).status, "running");
|
||||
assert.equal(runner.calls.length, 2);
|
||||
|
||||
runner.complete("task 2", "good");
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
assert.equal(terminal.status, "failed");
|
||||
assert.deepEqual(terminal.results.map((result) => result.status), ["failed", "succeeded"]);
|
||||
assert.equal(terminal.results[0].errorMessage, "boom");
|
||||
assert.equal(terminal.results[1].output, "good");
|
||||
});
|
||||
|
||||
test("chain substitutes the previous output and starts only the next step", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner);
|
||||
const request = invocation("chain");
|
||||
request.tasks[1].task = "review {previous}";
|
||||
|
||||
const job = manager.startAsync(request);
|
||||
assert.equal(runner.calls.length, 1);
|
||||
assert.equal(manager.get(job.id).results.length, 1);
|
||||
|
||||
runner.complete("task 1", "first output");
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal(runner.calls.length, 2);
|
||||
assert.equal(runner.calls[1].task.task, "review first output");
|
||||
|
||||
runner.complete("review first output", "second output");
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
assert.equal(terminal.status, "succeeded");
|
||||
assert.deepEqual(terminal.results.map((result) => result.step), [1, 2]);
|
||||
});
|
||||
|
||||
test("cancel drains queued tasks, aborts running tasks, and is idempotent", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, { maxWorkers: 1 });
|
||||
const job = manager.startAsync(invocation("parallel"));
|
||||
|
||||
const cancelling = manager.cancel(job.id);
|
||||
assert.equal(cancelling.status, "cancelling");
|
||||
assert.deepEqual(cancelling.results.map((result) => result.status), ["running", "cancelled"]);
|
||||
assert.deepEqual(runner.aborted, ["task 1"]);
|
||||
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
assert.equal(terminal.status, "cancelled");
|
||||
assert.deepEqual(terminal.results.map((result) => result.status), ["cancelled", "cancelled"]);
|
||||
assert.equal(runner.calls.length, 1);
|
||||
assert.equal(manager.cancel(job.id).status, "cancelled");
|
||||
});
|
||||
|
||||
test("synchronous and async work share the global worker limit", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, { maxWorkers: 1 });
|
||||
const background = manager.startAsync(invocation());
|
||||
|
||||
let syncSettled = false;
|
||||
const sync = manager.runSync(invocation()).then((outcome) => {
|
||||
syncSettled = true;
|
||||
return outcome;
|
||||
});
|
||||
assert.equal(runner.calls.length, 1);
|
||||
assert.equal(syncSettled, false);
|
||||
|
||||
runner.complete("task 1", "async done");
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal((await manager.wait(background.id, 1)).status, "succeeded");
|
||||
assert.equal(runner.calls.length, 2);
|
||||
|
||||
runner.complete("task 1", "sync done");
|
||||
const outcome = await sync;
|
||||
assert.equal(outcome.status, "succeeded");
|
||||
assert.equal(outcome.results[0].output, "sync done");
|
||||
});
|
||||
|
||||
test("wait timeout and caller abort do not cancel the job", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner);
|
||||
const job = manager.startAsync(invocation());
|
||||
|
||||
const timedOut = await manager.wait(job.id, 0.01);
|
||||
assert.equal(timedOut.status, "running");
|
||||
assert.equal(timedOut.timedOut, true);
|
||||
assert.equal(manager.get(job.id).status, "running");
|
||||
|
||||
const controller = new AbortController();
|
||||
const waiting = manager.wait(job.id, 1, controller.signal);
|
||||
controller.abort();
|
||||
await assert.rejects(waiting, /Wait aborted/);
|
||||
assert.equal(manager.get(job.id).status, "running");
|
||||
assert.equal(runner.aborted.length, 0);
|
||||
|
||||
runner.complete("task 1");
|
||||
assert.equal((await manager.wait(job.id, 1)).status, "succeeded");
|
||||
});
|
||||
|
||||
test("shutdown interrupts work, drains the queue, awaits cleanup, and rejects admission", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, { maxWorkers: 1 });
|
||||
const job = manager.startAsync(invocation("parallel"));
|
||||
|
||||
let shutdownResolved = false;
|
||||
const shutdown = manager.shutdown().then(() => {
|
||||
shutdownResolved = true;
|
||||
});
|
||||
const interrupted = manager.get(job.id);
|
||||
assert.equal(interrupted.status, "interrupted");
|
||||
assert.deepEqual(interrupted.results.map((result) => result.status), ["interrupted", "interrupted"]);
|
||||
assert.deepEqual(runner.aborted, ["task 1"]);
|
||||
assert.equal(runner.calls.length, 1);
|
||||
|
||||
await shutdown;
|
||||
assert.equal(shutdownResolved, true);
|
||||
assert.throws(() => manager.startAsync(invocation()), /shutting down/);
|
||||
assert.equal(manager.shutdown instanceof Function, true);
|
||||
await manager.shutdown();
|
||||
});
|
||||
|
||||
test("sync abort kills work, rejects the call, and releases its worker", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, { maxWorkers: 1 });
|
||||
const controller = new AbortController();
|
||||
const updates: string[] = [];
|
||||
|
||||
const sync = manager.runSync(invocation(), {
|
||||
signal: controller.signal,
|
||||
onUpdate: (outcome) => updates.push(outcome.status),
|
||||
});
|
||||
controller.abort();
|
||||
await assert.rejects(sync, /aborted/i);
|
||||
assert.deepEqual(runner.aborted, ["task 1"]);
|
||||
assert.ok(updates.includes("running"));
|
||||
|
||||
const next = manager.startAsync(invocation());
|
||||
assert.equal(next.status, "running");
|
||||
runner.complete("task 1");
|
||||
assert.equal((await manager.wait(next.id, 1)).status, "succeeded");
|
||||
});
|
||||
|
||||
test("async snapshots cap output without mutating the full runner result", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner);
|
||||
const job = manager.startAsync(invocation());
|
||||
const output = "🙂ab".repeat(20000);
|
||||
|
||||
runner.complete("task 1", output);
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
const snapshot = terminal.results[0];
|
||||
assert.equal(snapshot.outputTruncated, true);
|
||||
assert.ok(Buffer.byteLength(snapshot.output, "utf8") <= 50 * 1024);
|
||||
assert.equal(output.startsWith(snapshot.output), true);
|
||||
assert.equal(snapshot.output.includes("\uFFFD"), false);
|
||||
assert.ok(snapshot.outputBytesOmitted > 0);
|
||||
});
|
||||
|
||||
test("active admission is capped and terminal history evicts oldest jobs only", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, { maxActiveJobs: 1, historyLimit: 1 });
|
||||
const first = manager.startAsync(invocation());
|
||||
assert.throws(() => manager.startAsync(invocation()), /Too many active async jobs/);
|
||||
|
||||
runner.complete("task 1", "first");
|
||||
assert.equal((await manager.wait(first.id, 1)).status, "succeeded");
|
||||
const second = manager.startAsync(invocation());
|
||||
assert.equal(manager.get(first.id).status, "succeeded");
|
||||
runner.complete("task 1", "second");
|
||||
assert.equal((await manager.wait(second.id, 1)).status, "succeeded");
|
||||
assert.throws(() => manager.get(first.id), /Unknown or evicted/);
|
||||
assert.equal(manager.get(second.id).results[0].output, "second");
|
||||
});
|
||||
|
||||
test("restore keeps terminal history, interrupts unfinished snapshots, and deduplicates IDs", () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner);
|
||||
const source = new SubagentManager(new FakeRunner());
|
||||
const running = source.startAsync(invocation());
|
||||
|
||||
manager.restore([running, running]);
|
||||
const restored = manager.get(running.id);
|
||||
assert.equal(restored.status, "interrupted");
|
||||
assert.equal(restored.results[0].status, "interrupted");
|
||||
assert.equal(manager.list().length, 1);
|
||||
assert.equal(runner.calls.length, 0);
|
||||
|
||||
const next = manager.startAsync(invocation());
|
||||
assert.notEqual(next.id, running.id);
|
||||
});
|
||||
|
||||
test("chain stops on the first failure without manufacturing later results", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner);
|
||||
const job = manager.startAsync(invocation("chain"));
|
||||
runner.complete("task 1", "bad", { exitCode: 1, errorMessage: "failed step" });
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
assert.equal(terminal.status, "failed");
|
||||
assert.equal(terminal.results.length, 1);
|
||||
assert.equal(runner.calls.length, 1);
|
||||
});
|
||||
|
||||
test("cancellation wins over a later successful process exit", async () => {
|
||||
const runner = new FakeRunner(false);
|
||||
const manager = new SubagentManager(runner);
|
||||
const job = manager.startAsync(invocation());
|
||||
manager.cancel(job.id);
|
||||
runner.complete("task 1", "too late", { exitCode: 0, stopReason: "stop" });
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
assert.equal(terminal.status, "cancelled");
|
||||
assert.equal(terminal.results[0].status, "cancelled");
|
||||
});
|
||||
|
||||
test("cancelled chain never starts a successor when the current step reports late success", async () => {
|
||||
const runner = new FakeRunner(false);
|
||||
const manager = new SubagentManager(runner);
|
||||
const job = manager.startAsync(invocation("chain"));
|
||||
manager.cancel(job.id);
|
||||
runner.complete("task 1", "late success", { exitCode: 0, stopReason: "stop" });
|
||||
const terminal = await manager.wait(job.id, 0.1);
|
||||
assert.equal(terminal.status, "cancelled");
|
||||
assert.equal(runner.calls.length, 1);
|
||||
assert.equal(terminal.results.length, 1);
|
||||
});
|
||||
|
||||
test("branch replacement removes terminal history without replacing active jobs", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner);
|
||||
const finished = manager.startAsync(invocation());
|
||||
runner.complete("task 1", "old branch");
|
||||
await manager.wait(finished.id, 1);
|
||||
const active = manager.startAsync(invocation());
|
||||
|
||||
manager.replaceHistory([]);
|
||||
assert.throws(() => manager.get(finished.id), /Unknown or evicted/);
|
||||
assert.equal(manager.get(active.id).status, "running");
|
||||
runner.complete("task 1", "active");
|
||||
assert.equal((await manager.wait(active.id, 1)).status, "succeeded");
|
||||
});
|
||||
|
||||
test("observer failures cannot strand jobs or workers", async () => {
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, { onJobChange: () => { throw new Error("observer"); } });
|
||||
const job = manager.startAsync(invocation());
|
||||
runner.complete("task 1");
|
||||
assert.equal((await manager.wait(job.id, 1)).status, "succeeded");
|
||||
|
||||
const sync = manager.runSync(invocation(), { onUpdate: () => { throw new Error("observer"); } });
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
runner.complete("task 1", "sync");
|
||||
assert.equal((await sync).status, "succeeded");
|
||||
});
|
||||
Reference in new issue
Block a user