Move Execution, InternalTask, and the status predicates into job-state.ts; the supervisor now creates, schedules, and settles shared rows only through its host. Concentrate cancellation and shutdown settlement in one supervisor policy, make restored rows honest (no fabricated AgentConfig), and thread load-time cap limits through WorkflowRunSpec so release enforcement and the review card share one value. Agent discovery is memoized per cwd within a prepare call.
73 lines
3.4 KiB
TypeScript
73 lines
3.4 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import fc from "fast-check";
|
|
import { SubagentManager } from "../src/manager.ts";
|
|
import { isTerminalJob } from "../src/job-state.ts";
|
|
import type { JobSnapshot, JobStatus } from "../src/types.ts";
|
|
import { FakeRunner, invocation } from "./helpers.ts";
|
|
|
|
type Action =
|
|
| { type: "start"; mode: "single" | "parallel" | "chain" }
|
|
| { type: "cancel"; index: number }
|
|
| { type: "complete"; fail: boolean };
|
|
|
|
const action = fc.oneof(
|
|
fc.record({ type: fc.constant("start"), mode: fc.constantFrom("single", "parallel", "chain") }),
|
|
fc.record({ type: fc.constant("cancel"), index: fc.integer({ min: 0, max: 7 }) }),
|
|
fc.record({ type: fc.constant("complete"), fail: fc.boolean() }),
|
|
) as fc.Arbitrary<Action>;
|
|
|
|
function checkInvariants(manager: SubagentManager, ids: string[], runner: FakeRunner, previous: Map<string, JobSnapshot>, shuttingDown: boolean): void {
|
|
assert.ok(runner.calls.filter((call) => !call.settled).length <= 2, "global worker bound");
|
|
for (const id of ids) {
|
|
const current = manager.get(id);
|
|
const old = previous.get(id);
|
|
if (old && isTerminalJob(old.status)) assert.equal(current.status, old.status, `terminal job ${id} changed`);
|
|
for (let index = 0; index < current.results.length; index++) {
|
|
const task = current.results[index];
|
|
const before = old?.results[index];
|
|
if (before && ["succeeded", "failed", "cancelled", "interrupted"].includes(before.status)) {
|
|
assert.equal(task.status, before.status, `terminal task ${id}/${index} changed`);
|
|
}
|
|
if (current.mode === "chain" && index > 0) {
|
|
assert.equal(current.results[index - 1].status, "succeeded", "chain step ran before predecessor succeeded");
|
|
}
|
|
}
|
|
if (current.status === "cancelling") assert.equal(current.results.some((task) => task.status === "queued"), false);
|
|
if (shuttingDown) assert.equal(isTerminalJob(current.status), true);
|
|
previous.set(id, current);
|
|
}
|
|
}
|
|
|
|
test("generated start/complete/cancel/shutdown sequences preserve scheduler and lifecycle invariants", async () => {
|
|
await fc.assert(fc.asyncProperty(fc.array(action, { minLength: 1, maxLength: 30 }), async (actions) => {
|
|
const runner = new FakeRunner(false);
|
|
const manager = new SubagentManager(runner, { maxWorkers: 2 });
|
|
const ids: string[] = [];
|
|
const previous = new Map<string, JobSnapshot>();
|
|
let shuttingDown = false;
|
|
|
|
for (const step of actions) {
|
|
if (step.type === "start" && ids.length < 8 && !shuttingDown) {
|
|
ids.push(manager.startAsync(invocation(step.mode)).id);
|
|
} else if (step.type === "cancel" && ids.length > 0 && !shuttingDown) {
|
|
manager.cancel(ids[step.index % ids.length]);
|
|
} else if (step.type === "complete") {
|
|
const pending = runner.calls.find((call) => !call.settled);
|
|
if (pending) runner.complete(pending.task.task, "generated", step.fail ? { exitCode: 1, errorMessage: "generated failure" } : {});
|
|
}
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
checkInvariants(manager, ids, runner, previous, shuttingDown);
|
|
}
|
|
|
|
shuttingDown = true;
|
|
const shutdown = manager.shutdown();
|
|
for (const pending of runner.calls.filter((call) => !call.settled)) {
|
|
runner.complete(pending.task.task, "late completion", { exitCode: 0 });
|
|
}
|
|
await shutdown;
|
|
checkInvariants(manager, ids, runner, previous, shuttingDown);
|
|
assert.throws(() => manager.startAsync(invocation()), /shutting down/);
|
|
}), { numRuns: 100 });
|
|
});
|