diff --git a/cli/src/engine-native/syncConcurrency.ts b/cli/src/engine-native/syncConcurrency.ts index 337410f1..f5e240e3 100644 --- a/cli/src/engine-native/syncConcurrency.ts +++ b/cli/src/engine-native/syncConcurrency.ts @@ -7,6 +7,16 @@ export type SyncConcurrency = 1 | 2 | 3; export type SyncTask = () => Promise; +type PoolState = + | { readonly kind: "accepting"; nextIndex: number } + | { readonly kind: "draining"; readonly index: number; readonly reason: unknown }; + +function drainAfterFailure(state: PoolState, index: number, reason: unknown): PoolState { + return state.kind === "draining" && state.index < index + ? state + : { kind: "draining", index, reason }; +} + /** * Run input-ordered tasks with bounded overlap. Once one task rejects, queued * tasks stay queued while already-started tasks drain; the earliest rejection @@ -17,27 +27,20 @@ export async function runBounded( concurrency: SyncConcurrency, ): Promise> { const results = new Array(tasks.length); - const failures = new Map(); - let nextIndex = 0; - let stopped = false; + const pool: { state: PoolState } = { state: { kind: "accepting", nextIndex: 0 } }; const workers = Array.from({ length: Math.min(concurrency, tasks.length) }, async () => { - for (;;) { - if (stopped || nextIndex >= tasks.length) return; - const index = nextIndex; - nextIndex += 1; + while (pool.state.kind === "accepting" && pool.state.nextIndex < tasks.length) { + const index = pool.state.nextIndex++; try { results[index] = await tasks[index]!(); - } catch (error) { - failures.set(index, error); - stopped = true; + } catch (reason) { + pool.state = drainAfterFailure(pool.state, index, reason); } } }); await Promise.all(workers); - for (let index = 0; index < tasks.length; index += 1) { - if (failures.has(index)) throw failures.get(index); - } + if (pool.state.kind === "draining") throw pool.state.reason; return results; } diff --git a/cli/test/unit/engine-di.test.ts b/cli/test/unit/engine-di.test.ts index 2b2ab3d1..c79ac634 100644 --- a/cli/test/unit/engine-di.test.ts +++ b/cli/test/unit/engine-di.test.ts @@ -2,26 +2,28 @@ import { describe, expect, it } from "vitest"; import { existsSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { setImmediate as nextTurn } from "node:timers/promises"; import { runBounded, runEngineNative } from "../../src/engine-native"; import { syncLockPath } from "../../src/engine-native/scratch"; import { makeEngineServices } from "../../src/engine-native/services"; describe("sync pipeline coordinator", () => { - function deferred() { - let resolve!: (value: T) => void; - let reject!: (error: unknown) => void; - const promise = new Promise((onResolve, onReject) => { - resolve = onResolve; - reject = onReject; - }); - return { promise, resolve, reject }; + function gatedFailure(reason: unknown) { + const { promise, resolve } = Promise.withResolvers(); + return { + release: resolve, + task: async () => { + await promise; + throw reason; + }, + }; } it("caps overlap and returns results in selection order", async () => { const names = ["first", "second", "third"] as const; - const controls = names.map(() => deferred()); - const thirdStarted = deferred(); + const controls = names.map(() => Promise.withResolvers()); + const thirdStarted = Promise.withResolvers(); const started: Array = []; let active = 0; let maxActive = 0; @@ -48,8 +50,8 @@ describe("sync pipeline coordinator", () => { }); it("runs the next pipeline only after the prior one settles at cap one", async () => { - const first = deferred(); - const secondStarted = deferred(); + const first = Promise.withResolvers(); + const secondStarted = Promise.withResolvers(); const started: Array = []; const run = runBounded( [ @@ -72,14 +74,14 @@ describe("sync pipeline coordinator", () => { }); it("drains started pipelines, stops queued work, and reports the first input failure", async () => { - const first = deferred(); - const second = deferred(); + const second = gatedFailure(new Error("second failed")); const firstFailure = new Error("first failed"); + const first = gatedFailure(firstFailure); let queued = false; const run = runBounded( [ - async () => await first.promise, - async () => await second.promise, + first.task, + second.task, async () => { queued = true; return "should not run"; @@ -88,11 +90,43 @@ describe("sync pipeline coordinator", () => { 2, ); const result = expect(run).rejects.toBe(firstFailure); - second.reject(new Error("second failed")); - first.reject(firstFailure); + second.release(); + first.release(); await result; expect(queued).toBe(false); }); + + it("keeps an undefined rejection when a later input fails during drain", async () => { + const failing = gatedFailure(undefined); + const active = gatedFailure(new Error("later input failed")); + const events: Array = []; + const run = runBounded( + [ + failing.task, + active.task, + async () => { + events.push("queued task started"); + }, + ], + 2, + ); + const settled = run.then( + () => events.push("fulfilled"), + (reason: unknown) => { + events.push("rejected"); + return reason; + }, + ); + failing.release(); + try { + await nextTurn(); + expect(events).toEqual([]); + } finally { + active.release(); + } + expect(await settled).toBeUndefined(); + expect(events).toEqual(["rejected"]); + }); }); describe("native CLI argument and output behavior", () => {