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

test: drain cancelled process-audit test writes before removing the fixture root (#9622)

Adam Eivy20494e86d03c
diff --git a/scripts/persistentMindProcessAuditCleanup.test.js b/scripts/persistentMindProcessAuditCleanup.test.js
new file mode 100644
index 0000000000..76106640d6
--- /dev/null
+++ b/scripts/persistentMindProcessAuditCleanup.test.js
@@ -0,0 +1,55 @@
+import { expect, it } from 'vitest';
+import { spawnSync } from 'node:child_process';
+import { mkdtempSync, readFileSync, rmSync, writeFileSync, existsSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, dirname, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { createVitestTempFixture } from './lib/vitestTempRootFixture.js';
+
+// Pins the regression uniquely: a different worker's --bail=1 failure cancels a
+// process-audit test while its body still owns fixture writes (an archived job
+// and a ledger write) into the shared root that the suite's afterAll removes.
+it('drains process-audit fixture writes after another worker triggers fail-fast', () => {
+ const host = mkdtempSync(join(tmpdir(), 'pa-cancel-'));
+ try {
+ const fixture = join(host, 'fixture');
+ const ready = join(fixture, 'ready');
+ const finished = join(fixture, 'finished');
+ const { args, options } = createVitestTempFixture(host, 'server', '');
+ const sourcePath = fileURLToPath(new URL('../server/services/persistentMindProcessAudit.test.js', import.meta.url));
+ const header = "it('holds an ambiguous create durably instead of publishing twice', async () => {";
+ const source = readFileSync(sourcePath, 'utf8')
+ .replace(/(['"])(\.\.?\/[^'"]+)\1/g, (_, quote, specifier) => JSON.stringify(resolve(dirname(sourcePath), specifier)))
+ .replace(header, `${header.replace('async () =>', 'async ({ signal }) =>')}
+ const { writeFileSync } = await import('node:fs');
+ const cancelled = new Promise(resolve => signal.addEventListener('abort', () => setImmediate(resolve), { once: true }));
+ writeFileSync(${JSON.stringify(ready)}, 'ready');
+ await cancelled;
+ await addJob('agent-interrupted');
+ await next({ turnId: 'interrupted-turn' });
+ writeFileSync(${JSON.stringify(finished)}, 'finished');
+ return;`);
+ expect(source).toContain('await cancelled;');
+ writeFileSync(join(fixture, 'lifecycle.test.js'), source);
+ writeFileSync(join(fixture, 'failure.test.js'), `
+ import { it, expect } from 'vitest';
+ import { existsSync } from 'node:fs';
+ it('controlled sibling failure', async () => {
+ await expect.poll(() => existsSync(${JSON.stringify(ready)}), { timeout: 10000 }).toBe(true);
+ throw new Error('controlled sibling failure');
+ });
+ `);
+ const workerIndex = args.indexOf('--maxWorkers');
+ args[workerIndex + 1] = '2';
+ args.push('--bail=1');
+ const result = spawnSync(process.execPath, args, options);
+ const output = `${result.stdout}\n${result.stderr}`;
+ expect(result.error).toBeUndefined();
+ expect(result.status, output).toBe(1);
+ expect(output).toContain('controlled sibling failure');
+ expect(existsSync(finished), output).toBe(true);
+ expect(output).not.toMatch(/ENOTEMPTY|ENOENT|Unhandled|test temp leak:/);
+ } finally {
+ rmSync(host, { recursive: true, force: true });
+ }
+}, 25000);
diff --git a/server/services/persistentMindProcessAudit.test.js b/server/services/persistentMindProcessAudit.test.js
index 4cacd3a994..3fbb4fdb2d 100644
--- a/server/services/persistentMindProcessAudit.test.js
+++ b/server/services/persistentMindProcessAudit.test.js
@@ -1,4 +1,4 @@
-import { beforeEach, afterAll, expect, it, vi } from 'vitest';
+import { beforeEach, afterAll, expect, it as vitestIt, vi } from 'vitest';
import { join } from 'node:path';
import { mkdir, writeFile, rm } from 'node:fs/promises';
const mocks = vi.hoisted(() => ({ root: {}, apps: [], records: {}, dir: null, file: vi.fn(), list: vi.fn(), git: vi.fn() }));
@@ -43,7 +43,24 @@ beforeEach(async () => {
mocks.git.mockImplementation(async args => ({ stdout: args.slice(3).join('\0') + '\0', exitCode: 0 }));
await addJob('agent-example');
});
-afterAll(() => rm(mocks.dir, { recursive: true, force: true }));
+// Vitest's fail-fast cancellation rejects a test's wrapper while its async body
+// keeps writing into the shared fixture root. Own each body until it settles so
+// afterAll removes the root only after every write, and surface a body that
+// failed after vitest stopped listening instead of dropping it.
+const pendingBodies = new Set();
+const orphanedFailures = [];
+const it = (name, body) => vitestIt(name, context => {
+ const run = Promise.resolve().then(() => body(context));
+ const settled = run.then(() => {}, error => { if (context.signal.aborted) orphanedFailures.push(error); });
+ pendingBodies.add(settled);
+ settled.then(() => pendingBodies.delete(settled));
+ return run;
+});
+afterAll(async () => {
+ while (pendingBodies.size) await Promise.all(pendingBodies);
+ await rm(mocks.dir, { recursive: true, force: true });
+ if (orphanedFailures.length) throw new AggregateError(orphanedFailures, 'Cancelled process-audit test failed after cancellation');
+});
const next = (context = turn) => audit.nextProcessAuditBatch({ appId: 'example' }, context);
const finding = receipt => ({ appId: 'example', receiptId: receipt, outcome: 'finding', template: 'wrong-tool', targetAppId: 'slashdo', anchors: ['commands/next.md'] });