Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

[issue-11641] Drain cancelled completion-order test bodies (#11643)

[._.]/e375b1b04113
diff --git a/scripts/ci-test-plan.js b/scripts/ci-test-plan.js
index f11376cc8d..540eeadb1e 100644
--- a/scripts/ci-test-plan.js
+++ b/scripts/ci-test-plan.js
@@ -146,6 +146,10 @@ export const WINDOWS_RISK_RULES = [
// — green everywhere CI looked. These modules decide containment and worktree
// identity, so they need a real Windows run.
/^server\/lib\/(?:pathSafety|worktreeOwnership)(?:\.test)?\.js$/,
+ // Fail-fast fixture removal raced continuing writes on Windows (#11641).
+ // Exercise the ownership helper and affected fixture on the real filesystem.
+ /^server\/lib\/mockPathsDataRoot(?:\.test)?\.js$/,
+ /^server\/services\/cosAgentIndex\.completionOrder\.test\.js$/,
// Same class, different mechanism: staticImportGraph keys its module map by
// string-concatenating entry names with "/" but resolves edge targets with
// path.relative, which answers with "\\" on Windows. Every edge into a
@@ -938,7 +942,12 @@ export function buildCiTestPlan(changedFiles, {
smoke: hasServerSource,
windows,
windowsMode,
- windowsFiles: windows ? windowsContractTests(trackedSet) : [],
+ windowsFiles: windows ? uniqueSorted([
+ ...windowsContractTests(trackedSet),
+ // Test-only changes have no source path for `vitest related` to follow.
+ // A risk tag must select the changed contract itself as well as baseline.
+ ...directTests.filter(path => isServerRunnerFile(path) && hasWindowsRiskFile([path])),
+ ]) : [],
windowsSources: windowsMode === 'related' ? serverSources : [],
}, { appRouteOnly });
}
diff --git a/scripts/ci-test-plan.test.js b/scripts/ci-test-plan.test.js
index 3321964c06..78fda2f396 100644
--- a/scripts/ci-test-plan.test.js
+++ b/scripts/ci-test-plan.test.js
@@ -1412,6 +1412,8 @@ describe('Windows escalation on a full plan (#7440)', () => {
'scripts/ps1-bom.test.js',
'server/lib/bufferedSpawn.js',
'server/lib/pathSafety.js',
+ 'server/lib/mockPathsDataRoot.js',
+ 'server/services/cosAgentIndex.completionOrder.test.js',
'server/lib/staticImportGraph.js',
'server/services/agentImportCycles.test.js',
'server/services/worktreeManager.js',
@@ -1455,6 +1457,21 @@ describe('Windows escalation on a full plan (#7440)', () => {
expect(plan(['server/services/auth.js']).windows).toBe(false);
expect(plan(['server/lib/bufferedSpawn.js']).windows).toBe(true);
});
+
+ it('selects the changed cancellation fixtures explicitly on Windows without widening unrelated tests (#11641)', () => {
+ const fixtures = ['server/lib/mockPathsDataRoot.test.js', 'server/services/cosAgentIndex.completionOrder.test.js'];
+ const unrelated = 'server/services/exampleUnrelated.test.js';
+ const trackedFiles = [...TRACKED, ...WINDOWS_CONTRACT_TESTS, ...fixtures, unrelated];
+ for (const changedFiles of [fixtures, ...fixtures.map(path => [path])]) {
+ const selected = buildCiTestPlan(changedFiles, { trackedFiles, clientDependencies: [] });
+ expect(selected.full).toBe(false);
+ expect(selected.windowsMode).toBe('files');
+ expect(selected.windowsFiles).toEqual([...new Set([...WINDOWS_CONTRACT_TESTS, ...changedFiles])].sort());
+ expect(selected.windowsFiles).not.toContain(unrelated);
+ }
+ const helper = 'server/lib/mockPathsDataRoot.js';
+ expect(buildCiTestPlan([helper], { trackedFiles: [...trackedFiles, helper], clientDependencies: [] }).windows).toBe(true);
+ });
});
});
diff --git a/server/lib/mockPathsDataRoot.test.js b/server/lib/mockPathsDataRoot.test.js
index 925932a212..8e581f7aa3 100644
--- a/server/lib/mockPathsDataRoot.test.js
+++ b/server/lib/mockPathsDataRoot.test.js
@@ -4,7 +4,101 @@ import { posixPath } from './testHelper.js';
import { existsSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join, sep } from 'path';
-import { createTempDataRoot, makePathsProxy, mockNoPeers, mockNoPeerSync, mockPathsDataRoot } from './mockPathsDataRoot.js';
+import { mkdir, writeFile, rm } from 'node:fs/promises';
+import { createTempDataRoot, makePathsProxy, mockNoPeers, mockNoPeerSync, mockPathsDataRoot, ownTestBodies } from './mockPathsDataRoot.js';
+
+describe('ownTestBodies cancellation ordering (#11641)', () => {
+ // These controls also write real fixtures: own their outer Vitest bodies,
+ // not only the simulated cancelled bodies they exercise below.
+ const controls = ownTestBodies(it);
+ afterAll(() => controls.drain());
+ const lateWriteControl = tracked => async () => {
+ const root = createTempDataRoot('portos-owned-body-');
+ const controller = new AbortController();
+ const entered = Promise.withResolvers();
+ const release = Promise.withResolvers();
+ let registered;
+ const owned = ownTestBodies((_name, body) => { registered = body; });
+ const writeLate = async () => {
+ entered.resolve();
+ await release.promise;
+ await mkdir(root, { recursive: true });
+ await writeFile(join(root, 'projection.json'), '{}');
+ };
+ owned.it('held write', writeLate);
+ const body = tracked ? registered({ signal: controller.signal }) : writeLate();
+ const cancelled = new Promise(resolve => controller.signal.addEventListener('abort', () => resolve('cancelled'), { once: true }));
+ // Model the runner wrapper settling on cancellation, while the real body
+ // is deterministically held at a write boundary rather than on a timer.
+ const wrapper = Promise.race([body, cancelled]);
+ let removing = false;
+ let cleanup;
+ try {
+ await entered.promise;
+ controller.abort();
+ expect(await wrapper).toBe('cancelled');
+ cleanup = (async () => {
+ if (tracked) await owned.drain();
+ removing = true;
+ await rm(root, { recursive: true, force: true });
+ })();
+ if (tracked) {
+ // Flush a resolved drain's continuation: a broken, early-resolving
+ // drain must fail here rather than race the filesystem assertions.
+ await Promise.resolve();
+ expect(removing).toBe(false);
+ expect(existsSync(root)).toBe(true);
+ } else {
+ await cleanup;
+ expect(existsSync(root)).toBe(false);
+ }
+ release.resolve();
+ await body;
+ await cleanup;
+ // Unowned cancellation recreates the removed fixture; ownership makes
+ // the same write finish before removal, leaving nothing behind.
+ expect(existsSync(join(root, 'projection.json'))).toBe(!tracked);
+ expect(existsSync(root)).toBe(!tracked);
+ } finally {
+ release.resolve();
+ // Preserve any assertion/body error while still settling both pieces
+ // of owned work before the unconditional fixture removal.
+ await Promise.allSettled([body, cleanup]);
+ await rm(root, { recursive: true, force: true });
+ }
+ };
+ for (const tracked of [false, true]) {
+ controls.it(`bounds the late-write cleanup race with ownership=${tracked}`, lateWriteControl(tracked));
+ }
+
+ controls.it('reports a body failure after cancellation and still permits finally cleanup', async () => {
+ const root = createTempDataRoot('portos-owned-failure-');
+ const controller = new AbortController();
+ const entered = Promise.withResolvers(), release = Promise.withResolvers();
+ let registered;
+ const owned = ownTestBodies((_name, body) => { registered = body; });
+ const failure = new Error('synthetic late projection failure');
+ owned.it('held failure', async () => { entered.resolve(); await release.promise; throw failure; });
+ const observed = registered({ signal: controller.signal }).catch(error => error);
+ let cleanup;
+ try {
+ await entered.promise;
+ controller.abort();
+ cleanup = (async () => {
+ try { await owned.drain(); } finally { await rm(root, { recursive: true, force: true }); }
+ })();
+ const rejected = expect(cleanup).rejects.toMatchObject({ errors: [failure] });
+ release.resolve();
+ expect(await observed).toBe(failure);
+ await rejected;
+ expect(existsSync(root)).toBe(false);
+ } finally {
+ release.resolve();
+ await Promise.allSettled([observed, cleanup]);
+ await rm(root, { recursive: true, force: true });
+ }
+ });
+});
describe('mockPathsDataRoot', () => {
describe('createTempDataRoot', () => {
diff --git a/server/services/cosAgentIndex.completionOrder.test.js b/server/services/cosAgentIndex.completionOrder.test.js
index bb831422e7..f3c07db64e 100644
--- a/server/services/cosAgentIndex.completionOrder.test.js
+++ b/server/services/cosAgentIndex.completionOrder.test.js
@@ -5,9 +5,13 @@
* "read the whole day, then sort it" shows up as a day scan that a warm index
* must never perform.
*/
-import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterAll, beforeEach, describe, expect, it as vitestIt, vi } from 'vitest';
import { mkdir, writeFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
+import { ownTestBodies } from '../lib/mockPathsDataRoot.js';
+
+const owned = ownTestBodies(vitestIt);
+const it = owned.it;
const fixture = await vi.hoisted(async () => {
const { mkdtemp } = await import('node:fs/promises');
@@ -79,13 +83,22 @@ async function seedBusyDay() {
for (let i = DAY_SIZE - ON_DISK; i < DAY_SIZE; i += 1) await writeArchive(recordAt(i));
}
-beforeEach(() => {
+beforeEach(async () => {
+ // Cancellation/timeouts can settle Vitest's wrapper before the body stops
+ // writing. Finish that work before resetting its shared modules and fixture.
+ await owned.drain();
io.metadata = 0;
io.dayScans = 0;
vi.resetModules();
});
-afterAll(() => rm(fixture, { recursive: true, force: true }));
+afterAll(async () => {
+ try {
+ await owned.drain();
+ } finally {
+ await rm(fixture, { recursive: true, force: true });
+ }
+});
describe('bounded paging over a busy archive day (#7968)', () => {
it('reads one metadata record per returned row and never scans the day', async () => {