| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- import { describe, expect, it } from "vitest";
- import {
- deriveWorkflowSchedulerState,
- type WorkflowEvent,
- type WorkflowGraph,
- type WorkflowGraphRecord,
- type WorkflowRunSnapshot,
- } from "@zcode/contracts";
- import { WorkflowGraphScheduler } from "../src/workflow/scheduler.js";
- import { DEFAULT_EXPERT_WORKFLOW_STRATEGY } from "../src/workflow/expert.js";
- describe("WorkflowGraphScheduler", () => {
- it("derives ready and blocked nodes from graph dependencies", () => {
- const state = deriveWorkflowSchedulerState({
- edges: [{ from: "a", to: "b" }],
- nodes: [
- { dependsOn: [], id: "a", kind: "task", status: "completed", title: "A" },
- { dependsOn: ["a"], id: "b", kind: "task", status: "pending", title: "B" },
- { dependsOn: ["missing"], id: "c", kind: "task", status: "pending", title: "C" },
- ],
- });
- expect(state.readyNodeIds).toEqual(["b"]);
- expect(state.blockedNodes).toEqual([{ blockedBy: ["missing"], nodeId: "c" }]);
- expect(state.counts).toMatchObject({
- blocked: 1,
- completed: 1,
- pending: 2,
- ready: 1,
- total: 3,
- });
- });
- it("dispatches independent ready nodes concurrently and waits for dependencies", async () => {
- const events: WorkflowEvent[] = [];
- const graphRecords: WorkflowGraphRecord[] = [];
- const started: string[] = [];
- const completed: string[] = [];
- let inFlight = 0;
- let maxInFlight = 0;
- let now = 0;
- const scheduler = new WorkflowGraphScheduler({
- appendEvent: async (event) => {
- events.push(event);
- },
- appendGraphRecord: async (_runId, record) => {
- graphRecords.push(record);
- },
- createActivityId: () => `act_${events.length}_${started.length}`,
- now: () => new Date(1_700_000_000_000 + now++),
- runner: {
- run: async ({ node }) => {
- started.push(node.id);
- inFlight++;
- maxInFlight = Math.max(maxInFlight, inFlight);
- await new Promise((resolve) => setTimeout(resolve, node.id === "c" ? 1 : 10));
- inFlight--;
- completed.push(node.id);
- return {
- response: `# ${node.id}`,
- sessionId: `sess_${node.id}`,
- };
- },
- },
- writeArtifact: async (_runId, relativePath) => ({
- path: relativePath,
- relativePath,
- }),
- writeSnapshot: async () => {},
- });
- const result = await scheduler.run({
- cwd: "/repo",
- executableNodeIds: ["a", "b", "c"],
- phase: "exec",
- snapshot: createSnapshot({
- edges: [
- { from: "a", to: "c" },
- { from: "b", to: "c" },
- ],
- nodes: [
- { dependsOn: [], id: "a", kind: "task", status: "pending", title: "A" },
- { dependsOn: [], id: "b", kind: "task", status: "pending", title: "B" },
- { dependsOn: ["a", "b"], id: "c", kind: "task", status: "pending", title: "C" },
- ],
- }),
- });
- expect(result.status).toBe("completed");
- expect(maxInFlight).toBe(2);
- expect(started.slice(0, 2).sort()).toEqual(["a", "b"]);
- expect(started.at(-1)).toBe("c");
- expect(completed.sort()).toEqual(["a", "b", "c"]);
- expect(result.snapshot.graph.nodes.map((node) => [node.id, node.status])).toEqual([
- ["a", "completed"],
- ["b", "completed"],
- ["c", "completed"],
- ]);
- expect(events.map((event) => event.type)).toEqual(
- expect.arrayContaining([
- "frontier_changed",
- "node_started",
- "node_completed",
- "executor_completed",
- ]),
- );
- expect(
- graphRecords.some(
- (record) =>
- record.recordType === "op" && record.nodeId === "c" && record.status === "completed",
- ),
- ).toBe(true);
- });
- it("retries failed nodes and pauses at the consecutive error threshold", async () => {
- const events: WorkflowEvent[] = [];
- const scheduler = new WorkflowGraphScheduler({
- appendEvent: async (event) => {
- events.push(event);
- },
- appendGraphRecord: async () => {},
- createActivityId: () => `act_${events.length}`,
- now: () => new Date("2026-05-05T00:00:00Z"),
- runner: {
- run: async () => {
- throw new Error("node failed");
- },
- },
- writeArtifact: async (_runId, relativePath) => ({
- path: relativePath,
- relativePath,
- }),
- writeSnapshot: async () => {},
- });
- const result = await scheduler.run({
- cwd: "/repo",
- executableNodeIds: ["flaky"],
- phase: "exec",
- snapshot: createSnapshot(
- {
- edges: [],
- nodes: [
- {
- dependsOn: [],
- id: "flaky",
- kind: "task",
- status: "pending",
- title: "Flaky",
- },
- ],
- },
- { maxConsecutiveErrors: 2 },
- ),
- });
- expect(result.status).toBe("paused");
- expect(result.reason).toBe("error_threshold");
- expect(result.snapshot.graph.nodes[0]).toMatchObject({
- attempts: 2,
- error: "node failed",
- status: "failed",
- });
- expect(events.filter((event) => event.type === "node_failed")).toHaveLength(2);
- expect(events.at(-1)).toMatchObject({
- type: "executor_paused",
- });
- });
- it("runs collection planners, expands the graph, and schedules new nodes", async () => {
- const events: WorkflowEvent[] = [];
- const graphRecords: WorkflowGraphRecord[] = [];
- const started: string[] = [];
- const plannerCollections: string[] = [];
- let now = 0;
- const scheduler = new WorkflowGraphScheduler({
- appendEvent: async (event) => {
- events.push(event);
- },
- appendGraphRecord: async (_runId, record) => {
- graphRecords.push(record);
- },
- createActivityId: () => `act_${events.length}_${started.length}_${plannerCollections.length}`,
- now: () => new Date(1_700_000_000_000 + now++),
- plannerRunner: {
- run: async ({ collection }) => {
- plannerCollections.push(collection.collectionId);
- if (plannerCollections.length === 1) {
- return {
- edges: [{ from: "seed", to: "follow_up" }],
- nodes: [
- {
- dependsOn: ["seed"],
- id: "follow_up",
- title: "Follow Up",
- },
- ],
- response:
- '{"nodes":[{"id":"follow_up","title":"Follow Up","dependsOn":["seed"]}],"edges":[{"from":"seed","to":"follow_up"}]}',
- sessionId: "planner-one",
- };
- }
- return {
- edges: [],
- exhausted: true,
- nodes: [],
- response: '{"nodes":[],"edges":[],"exhausted":true}',
- sessionId: "planner-two",
- };
- },
- },
- runner: {
- run: async ({ node }) => {
- started.push(node.id);
- return {
- response: `# ${node.id}`,
- sessionId: `sess_${node.id}`,
- };
- },
- },
- writeArtifact: async (_runId, relativePath) => ({
- path: relativePath,
- relativePath,
- }),
- writeSnapshot: async () => {},
- });
- const result = await scheduler.run({
- cwd: "/repo",
- executableNodeIds: ["seed"],
- phase: "exec",
- snapshot: createSnapshot(
- {
- collections: [
- {
- collectionId: "research",
- explorable: true,
- frontierTarget: 1,
- nodeIds: ["seed"],
- },
- ],
- edges: [],
- nodes: [
- {
- collectionId: "research",
- dependsOn: [],
- id: "seed",
- kind: "task",
- status: "completed",
- title: "Seed",
- },
- ],
- },
- { maxPlannerRuns: 3 },
- ),
- });
- expect(result.status).toBe("completed");
- expect(plannerCollections).toEqual(["research", "research"]);
- expect(started).toEqual(["follow_up"]);
- expect(result.snapshot.graph.nodes.map((node) => [node.id, node.status])).toEqual([
- ["seed", "completed"],
- ["follow_up", "completed"],
- ]);
- expect(result.snapshot.graph.collections?.[0]).toMatchObject({
- analyzedNodeIds: ["seed", "follow_up"],
- collectionId: "research",
- exhausted: true,
- nodeIds: ["seed", "follow_up"],
- plannerRuns: 2,
- status: "exhausted",
- });
- expect(events.map((event) => event.type)).toEqual(
- expect.arrayContaining([
- "planner_started",
- "planner_completed",
- "graph_expanded",
- "node_completed",
- "collection_exhausted",
- ]),
- );
- expect(
- graphRecords.some(
- (record) =>
- record.recordType === "op" &&
- record.type === "graph_expanded" &&
- record.collectionId === "research",
- ),
- ).toBe(true);
- });
- it("does not run a collection planner before seeded frontier has a completion", async () => {
- const plannerCalls: string[] = [];
- const started: string[] = [];
- const scheduler = new WorkflowGraphScheduler({
- appendEvent: async () => {},
- appendGraphRecord: async () => {},
- createActivityId: () => `act_${plannerCalls.length}_${started.length}`,
- now: () => new Date("2026-05-05T00:00:00Z"),
- plannerRunner: {
- run: async ({ collection }) => {
- plannerCalls.push(collection.collectionId);
- return {
- edges: [],
- exhausted: true,
- nodes: [],
- response: '{"nodes":[],"edges":[],"exhausted":true}',
- sessionId: "planner",
- };
- },
- },
- runner: {
- run: async ({ node }) => {
- expect(plannerCalls).toEqual([]);
- started.push(node.id);
- return {
- response: `# ${node.id}`,
- sessionId: `sess_${node.id}`,
- };
- },
- },
- writeArtifact: async (_runId, relativePath) => ({
- path: relativePath,
- relativePath,
- }),
- writeSnapshot: async () => {},
- });
- const result = await scheduler.run({
- cwd: "/repo",
- executableNodeIds: ["seed"],
- phase: "exec",
- snapshot: createSnapshot({
- collections: [
- {
- collectionId: "research",
- explorable: true,
- frontierTarget: 2,
- nodeIds: ["seed"],
- },
- ],
- edges: [],
- nodes: [
- {
- collectionId: "research",
- dependsOn: [],
- id: "seed",
- kind: "task",
- status: "pending",
- title: "Seed",
- },
- ],
- }),
- });
- expect(result.status).toBe("completed");
- expect(started).toEqual(["seed"]);
- expect(plannerCalls).toEqual(["research"]);
- });
- it("exhausts a collection instead of running past maxPlannerRuns", async () => {
- const events: WorkflowEvent[] = [];
- const scheduler = new WorkflowGraphScheduler({
- appendEvent: async (event) => {
- events.push(event);
- },
- appendGraphRecord: async () => {},
- createActivityId: () => `act_${events.length}`,
- now: () => new Date("2026-05-05T00:00:00Z"),
- plannerRunner: {
- run: async () => {
- throw new Error("planner should not run");
- },
- },
- runner: {
- run: async ({ node }) => ({
- response: `# ${node.id}`,
- sessionId: `sess_${node.id}`,
- }),
- },
- writeArtifact: async (_runId, relativePath) => ({
- path: relativePath,
- relativePath,
- }),
- writeSnapshot: async () => {},
- });
- const result = await scheduler.run({
- cwd: "/repo",
- executableNodeIds: ["seed"],
- phase: "exec",
- snapshot: createSnapshot(
- {
- collections: [
- {
- collectionId: "research",
- explorable: true,
- nodeIds: ["seed"],
- plannerRuns: 1,
- },
- ],
- edges: [],
- nodes: [
- {
- collectionId: "research",
- dependsOn: [],
- id: "seed",
- kind: "task",
- status: "completed",
- title: "Seed",
- },
- ],
- },
- { maxPlannerRuns: 1 },
- ),
- });
- expect(result.status).toBe("completed");
- expect(result.snapshot.graph.collections?.[0]).toMatchObject({
- collectionId: "research",
- exhausted: true,
- status: "exhausted",
- });
- expect(events.at(0)).toMatchObject({
- payload: {
- reason: "max_planner_runs",
- },
- type: "collection_exhausted",
- });
- });
- it("records planner failures without corrupting graph state", async () => {
- const events: WorkflowEvent[] = [];
- const scheduler = new WorkflowGraphScheduler({
- appendEvent: async (event) => {
- events.push(event);
- },
- appendGraphRecord: async () => {},
- createActivityId: () => `act_${events.length}`,
- now: () => new Date("2026-05-05T00:00:00Z"),
- plannerRunner: {
- run: async () => {
- throw new Error("planner failed");
- },
- },
- runner: {
- run: async ({ node }) => ({
- response: `# ${node.id}`,
- sessionId: `sess_${node.id}`,
- }),
- },
- writeArtifact: async (_runId, relativePath) => ({
- path: relativePath,
- relativePath,
- }),
- writeSnapshot: async () => {},
- });
- const result = await scheduler.run({
- cwd: "/repo",
- executableNodeIds: ["seed"],
- phase: "exec",
- snapshot: createSnapshot(
- {
- collections: [
- {
- collectionId: "research",
- explorable: true,
- nodeIds: ["seed"],
- },
- ],
- edges: [],
- nodes: [
- {
- collectionId: "research",
- dependsOn: [],
- id: "seed",
- kind: "task",
- status: "completed",
- title: "Seed",
- },
- ],
- },
- { maxConsecutiveErrors: 1 },
- ),
- });
- expect(result.status).toBe("completed");
- expect(result.snapshot.graph.nodes).toEqual([
- {
- collectionId: "research",
- dependsOn: [],
- id: "seed",
- kind: "task",
- status: "completed",
- title: "Seed",
- },
- ]);
- expect(result.snapshot.graph.collections?.[0]).toMatchObject({
- collectionId: "research",
- errorCount: 1,
- exhausted: true,
- status: "exhausted",
- });
- expect(events.map((event) => event.type)).toEqual(
- expect.arrayContaining(["planner_failed", "collection_exhausted"]),
- );
- });
- });
- function createSnapshot(
- graph: WorkflowGraph,
- options: { maxConsecutiveErrors?: number; maxPlannerRuns?: number } = {},
- ): WorkflowRunSnapshot {
- return {
- activities: [],
- artifacts: [],
- createdAt: "2026-05-05T00:00:00.000Z",
- cwd: "/repo",
- graph,
- kind: "expert",
- phaseOrder: ["exec"],
- phases: [{ phase: "exec", status: "pending" }],
- runId: "wf_scheduler_test",
- schemaVersion: 1,
- status: "running",
- strategy: {
- ...DEFAULT_EXPERT_WORKFLOW_STRATEGY,
- executor: {
- ...DEFAULT_EXPERT_WORKFLOW_STRATEGY.executor,
- maxConsecutiveErrors:
- options.maxConsecutiveErrors ??
- DEFAULT_EXPERT_WORKFLOW_STRATEGY.executor.maxConsecutiveErrors,
- maxPlannerRuns:
- options.maxPlannerRuns ?? DEFAULT_EXPERT_WORKFLOW_STRATEGY.executor.maxPlannerRuns,
- },
- },
- task: "ship workflow scheduler",
- updatedAt: "2026-05-05T00:00:00.000Z",
- };
- }
|