From b6569346053ff2ef633e9158dfd2d8c4f585aa2d Mon Sep 17 00:00:00 2001 From: Eduardo Marquez <55303379+DocksDocks@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:43:48 -0300 Subject: [PATCH 1/3] refactor: model worker admission and failure draining A failed worker closes admission. Keep only the lowest-index failure while active tasks drain, including when the reason is undefined. Verified: worker and tidy tests, isolated tidy CLI, check:types, targeted Oxlint and Bun formatting. --- cli/src/engine-native/syncConcurrency.ts | 29 +++++++++++++---------- cli/test/unit/engine-di.test.ts | 30 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/cli/src/engine-native/syncConcurrency.ts b/cli/src/engine-native/syncConcurrency.ts index 337410f10..f5e240e30 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 2b2ab3d17..9d7f30654 100644 --- a/cli/test/unit/engine-di.test.ts +++ b/cli/test/unit/engine-di.test.ts @@ -2,6 +2,7 @@ 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"; @@ -93,6 +94,35 @@ describe("sync pipeline coordinator", () => { await result; expect(queued).toBe(false); }); + + it("waits for active work before rejecting with an undefined reason", async () => { + const failing = deferred(); + const active = deferred(); + const events: Array = []; + const run = runBounded( + [ + () => failing.promise, + () => active.promise, + async () => { + events.push("queued task started"); + }, + ], + 2, + ); + const settled = run.then( + () => events.push("fulfilled"), + (reason: unknown) => { + expect(reason).toBeUndefined(); + events.push("rejected"); + }, + ); + failing.reject(undefined); + await nextTurn(); + expect(events).toEqual([]); + active.resolve(); + await settled; + expect(events).toEqual(["rejected"]); + }); }); describe("native CLI argument and output behavior", () => { From 37dfb4a0fe07a25b0f5a0251f1a74c700794dc69 Mon Sep 17 00:00:00 2001 From: Eduardo Marquez <55303379+DocksDocks@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:46:11 -0300 Subject: [PATCH 2/3] test: cover failure retention in both completion orders Observe settlement without asserting in a deferred rejection handler. A later input error must not replace the earlier undefined reason. Verified: worker and tidy tests, isolated tidy CLI, check:types, targeted Oxlint and Bun formatting. --- cli/test/unit/engine-di.test.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cli/test/unit/engine-di.test.ts b/cli/test/unit/engine-di.test.ts index 9d7f30654..bfb89226a 100644 --- a/cli/test/unit/engine-di.test.ts +++ b/cli/test/unit/engine-di.test.ts @@ -95,7 +95,7 @@ describe("sync pipeline coordinator", () => { expect(queued).toBe(false); }); - it("waits for active work before rejecting with an undefined reason", async () => { + it("keeps an undefined rejection when a later input fails during drain", async () => { const failing = deferred(); const active = deferred(); const events: Array = []; @@ -112,15 +112,18 @@ describe("sync pipeline coordinator", () => { const settled = run.then( () => events.push("fulfilled"), (reason: unknown) => { - expect(reason).toBeUndefined(); events.push("rejected"); + return reason; }, ); failing.reject(undefined); - await nextTurn(); - expect(events).toEqual([]); - active.resolve(); - await settled; + try { + await nextTurn(); + expect(events).toEqual([]); + } finally { + active.reject(new Error("later input failed")); + } + expect(await settled).toBeUndefined(); expect(events).toEqual(["rejected"]); }); }); From bb9b23bdffba12ff42f186ebf36420074a0b077d Mon Sep 17 00:00:00 2001 From: Eduardo Marquez <55303379+DocksDocks@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:48:29 -0300 Subject: [PATCH 3/3] test: reject only from started worker tasks Release a resolved gate and raise the failure inside the task. An unstarted fixture then cannot emit an unhandled rejection. Verified: worker and tidy tests, isolated tidy CLI, check:types, targeted Oxlint and Bun formatting. --- cli/test/unit/engine-di.test.ts | 49 +++++++++++++++++---------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/cli/test/unit/engine-di.test.ts b/cli/test/unit/engine-di.test.ts index bfb89226a..c79ac6340 100644 --- a/cli/test/unit/engine-di.test.ts +++ b/cli/test/unit/engine-di.test.ts @@ -9,20 +9,21 @@ 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; @@ -49,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( [ @@ -73,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"; @@ -89,20 +90,20 @@ 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 = deferred(); - const active = deferred(); + const failing = gatedFailure(undefined); + const active = gatedFailure(new Error("later input failed")); const events: Array = []; const run = runBounded( [ - () => failing.promise, - () => active.promise, + failing.task, + active.task, async () => { events.push("queued task started"); }, @@ -116,12 +117,12 @@ describe("sync pipeline coordinator", () => { return reason; }, ); - failing.reject(undefined); + failing.release(); try { await nextTurn(); expect(events).toEqual([]); } finally { - active.reject(new Error("later input failed")); + active.release(); } expect(await settled).toBeUndefined(); expect(events).toEqual(["rejected"]);