Emit job events, checkpoint at terminal, and bound the run wait
This commit is contained in:
1 parent
bf70be5729
commit
df929590e8
17 files changed
+501
-166
No files matched your search
+43
-32
@@ -2,8 +2,8 @@ import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import type { Message } from "@earendil-works/pi-ai";
|
||||
import { SubagentManager } from "../src/manager.ts";
|
||||
import type { JobSnapshot } from "../src/types.ts";
|
||||
import { FakeRunner, invocation, result as makeResult } from "./helpers.ts";
|
||||
import type { JobActivityEvent, JobEvent, JobSnapshot } from "../src/types.ts";
|
||||
import { FakeRunner, invocation, result as makeResult, waitForSnapshot } from "./helpers.ts";
|
||||
|
||||
test("async single returns an ID before completion and later exposes its result", async () => {
|
||||
const runner = new FakeRunner();
|
||||
@@ -15,7 +15,7 @@ test("async single returns an ID before completion and later exposes its result"
|
||||
assert.equal(runner.calls.length, 1);
|
||||
|
||||
runner.complete("task 1", "finished");
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 1);
|
||||
assert.equal(terminal.status, "succeeded");
|
||||
assert.equal(terminal.results.length, 1);
|
||||
assert.equal(terminal.results[0].output, "finished");
|
||||
@@ -24,26 +24,33 @@ test("async single returns an ID before completion and later exposes its result"
|
||||
|
||||
test("async progress exposes bounded activity without persisting every message", async () => {
|
||||
const persisted: JobSnapshot[] = [];
|
||||
const progress: JobSnapshot[] = [];
|
||||
const events: JobEvent[] = [];
|
||||
const progress: JobActivityEvent[] = [];
|
||||
const runner = new FakeRunner();
|
||||
const manager = new SubagentManager(runner, {
|
||||
onJobChange: (job) => persisted.push(job),
|
||||
onJobActivity: (job) => progress.push(job),
|
||||
onJobEvent: (event) => {
|
||||
events.push(event);
|
||||
if (event.kind !== "changed") persisted.push(manager.get(event.jobId));
|
||||
},
|
||||
onJobActivity: (event) => progress.push(event),
|
||||
});
|
||||
const job = manager.startAsync(invocation());
|
||||
const persistedBeforeProgress = persisted.length;
|
||||
const pending = runner.calls[0];
|
||||
const messages = [{
|
||||
const message = {
|
||||
role: "assistant",
|
||||
content: [{ type: "toolCall", id: "call-1", name: "bash", arguments: { command: "npm test" } }],
|
||||
timestamp: 100,
|
||||
}] as unknown as Message[];
|
||||
} as unknown as Message;
|
||||
|
||||
pending.options.onUpdate?.({
|
||||
...makeResult(pending.task, "working"),
|
||||
status: "running",
|
||||
exitCode: null,
|
||||
messages,
|
||||
result: {
|
||||
...makeResult(pending.task, "working"),
|
||||
status: "running",
|
||||
exitCode: null,
|
||||
messages: [message],
|
||||
},
|
||||
message,
|
||||
});
|
||||
|
||||
const current = manager.get(job.id);
|
||||
@@ -52,11 +59,15 @@ test("async progress exposes bounded activity without persisting every message",
|
||||
{ kind: "toolCall", summary: "$ npm test", timestamp: 100 },
|
||||
]);
|
||||
assert.equal(progress.length, 1);
|
||||
assert.equal(persisted.length, persistedBeforeProgress);
|
||||
assert.equal(progress[0].jobId, job.id);
|
||||
assert.equal(progress[0].message, message);
|
||||
assert.equal(persisted.length, persistedBeforeProgress, "a message delta is not a persistence event");
|
||||
assert.deepEqual(events.map((event) => event.kind), ["started", "changed"]);
|
||||
|
||||
runner.complete("task 1", "finished");
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 1);
|
||||
assert.equal(terminal.results[0].activity?.[0].summary, "$ npm test");
|
||||
assert.deepEqual(events.map((event) => event.kind), ["started", "changed", "changed", "finalized"]);
|
||||
assert.equal(persisted.at(-1)?.results[0].activity?.[0].summary, "$ npm test");
|
||||
});
|
||||
|
||||
@@ -73,7 +84,7 @@ test("parallel preserves partial results and does not finish while another task
|
||||
assert.equal(runner.calls.length, 2);
|
||||
|
||||
runner.complete("task 2", "good");
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
const terminal = await waitForSnapshot(manager, 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");
|
||||
@@ -96,7 +107,7 @@ test("chain substitutes the previous output and starts only the next step", asyn
|
||||
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);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 1);
|
||||
assert.equal(terminal.status, "succeeded");
|
||||
assert.deepEqual(terminal.results.map((result) => result.step), [1, 2]);
|
||||
});
|
||||
@@ -111,7 +122,7 @@ test("cancel drains queued tasks, aborts running tasks, and is idempotent", asyn
|
||||
assert.deepEqual(cancelling.results.map((result) => result.status), ["running", "cancelled"]);
|
||||
assert.deepEqual(runner.aborted, ["task 1"]);
|
||||
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 1);
|
||||
assert.equal(terminal.status, "cancelled");
|
||||
assert.deepEqual(terminal.results.map((result) => result.status), ["cancelled", "cancelled"]);
|
||||
assert.equal(runner.calls.length, 1);
|
||||
@@ -132,7 +143,7 @@ test("the manager defaults to sixteen shared workers", async () => {
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal(runner.calls.length, 17);
|
||||
runner.complete("task 17");
|
||||
assert.equal((await manager.wait(job.id, 1)).status, "succeeded");
|
||||
assert.equal((await waitForSnapshot(manager, job.id, 1)).status, "succeeded");
|
||||
});
|
||||
|
||||
test("synchronous and async work share the global worker limit", async () => {
|
||||
@@ -150,7 +161,7 @@ test("synchronous and async work share the global worker limit", async () => {
|
||||
|
||||
runner.complete("task 1", "async done");
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
assert.equal((await manager.wait(background.id, 1)).status, "succeeded");
|
||||
assert.equal((await waitForSnapshot(manager, background.id, 1)).status, "succeeded");
|
||||
assert.equal(runner.calls.length, 2);
|
||||
|
||||
runner.complete("task 1", "sync done");
|
||||
@@ -164,7 +175,7 @@ test("wait timeout and caller abort do not cancel the job", async () => {
|
||||
const manager = new SubagentManager(runner);
|
||||
const job = manager.startAsync(invocation());
|
||||
|
||||
const timedOut = await manager.wait(job.id, 0.01);
|
||||
const timedOut = await waitForSnapshot(manager, job.id, 0.01);
|
||||
assert.equal(timedOut.status, "running");
|
||||
assert.equal(timedOut.timedOut, true);
|
||||
assert.equal(manager.get(job.id).status, "running");
|
||||
@@ -177,7 +188,7 @@ test("wait timeout and caller abort do not cancel the job", async () => {
|
||||
assert.equal(runner.aborted.length, 0);
|
||||
|
||||
runner.complete("task 1");
|
||||
assert.equal((await manager.wait(job.id, 1)).status, "succeeded");
|
||||
assert.equal((await waitForSnapshot(manager, job.id, 1)).status, "succeeded");
|
||||
});
|
||||
|
||||
test("shutdown interrupts work, drains the queue, awaits cleanup, and rejects admission", async () => {
|
||||
@@ -220,7 +231,7 @@ test("sync abort kills work, rejects the call, and releases its worker", async (
|
||||
const next = manager.startAsync(invocation());
|
||||
assert.equal(next.status, "running");
|
||||
runner.complete("task 1");
|
||||
assert.equal((await manager.wait(next.id, 1)).status, "succeeded");
|
||||
assert.equal((await waitForSnapshot(manager, next.id, 1)).status, "succeeded");
|
||||
});
|
||||
|
||||
test("async snapshots cap output without mutating the full runner result", async () => {
|
||||
@@ -230,7 +241,7 @@ test("async snapshots cap output without mutating the full runner result", async
|
||||
const output = "🙂ab".repeat(20000);
|
||||
|
||||
runner.complete("task 1", output);
|
||||
const terminal = await manager.wait(job.id, 1);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 1);
|
||||
const snapshot = terminal.results[0];
|
||||
assert.equal(snapshot.outputTruncated, true);
|
||||
assert.ok(Buffer.byteLength(snapshot.output, "utf8") <= 50 * 1024);
|
||||
@@ -246,11 +257,11 @@ test("active admission is capped and terminal history evicts oldest jobs only",
|
||||
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");
|
||||
assert.equal((await waitForSnapshot(manager, 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.equal((await waitForSnapshot(manager, second.id, 1)).status, "succeeded");
|
||||
assert.throws(() => manager.get(first.id), /Unknown or evicted/);
|
||||
assert.equal(manager.get(second.id).results[0].output, "second");
|
||||
});
|
||||
@@ -277,7 +288,7 @@ test("chain stops on the first failure without manufacturing later results", asy
|
||||
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);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 1);
|
||||
assert.equal(terminal.status, "failed");
|
||||
assert.equal(terminal.results.length, 1);
|
||||
assert.equal(runner.calls.length, 1);
|
||||
@@ -289,7 +300,7 @@ test("cancellation wins over a later successful process exit", async () => {
|
||||
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);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 1);
|
||||
assert.equal(terminal.status, "cancelled");
|
||||
assert.equal(terminal.results[0].status, "cancelled");
|
||||
});
|
||||
@@ -300,7 +311,7 @@ test("cancelled chain never starts a successor when the current step reports lat
|
||||
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);
|
||||
const terminal = await waitForSnapshot(manager, job.id, 0.1);
|
||||
assert.equal(terminal.status, "cancelled");
|
||||
assert.equal(runner.calls.length, 1);
|
||||
assert.equal(terminal.results.length, 1);
|
||||
@@ -311,22 +322,22 @@ test("branch replacement removes terminal history without replacing active jobs"
|
||||
const manager = new SubagentManager(runner);
|
||||
const finished = manager.startAsync(invocation());
|
||||
runner.complete("task 1", "old branch");
|
||||
await manager.wait(finished.id, 1);
|
||||
await waitForSnapshot(manager, 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");
|
||||
assert.equal((await waitForSnapshot(manager, 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 manager = new SubagentManager(runner, { onJobEvent: () => { throw new Error("observer"); } });
|
||||
const job = manager.startAsync(invocation());
|
||||
runner.complete("task 1");
|
||||
assert.equal((await manager.wait(job.id, 1)).status, "succeeded");
|
||||
assert.equal((await waitForSnapshot(manager, job.id, 1)).status, "succeeded");
|
||||
|
||||
const sync = manager.runSync(invocation(), { onUpdate: () => { throw new Error("observer"); } });
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
|
||||
Reference in new issue
Block a user