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: contain observation fixtures through cancellation (#13477)

Adam Eivy90a409f56c01
diff --git a/scripts/vitestTempRootCancellation.test.js b/scripts/vitestTempRootCancellation.test.js
index 81699c0428..d0444c0c92 100644
--- a/scripts/vitestTempRootCancellation.test.js
+++ b/scripts/vitestTempRootCancellation.test.js
@@ -1,5 +1,5 @@
import { afterAll, expect, it } from 'vitest';
-import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -102,3 +102,55 @@ afterAll(() => {
if (cleanupConfirmed) rmSync(host, { recursive: true, force: true });
}
}, 135000);
+
+// Regression: the observation suite's concurrent persistence test must remain
+// inside its mocks and drain both writes when a sibling cancels the pool.
+owned.it('settles concurrent observation writes on bail before removing the run root', async ({ signal }) => {
+ const host = mkdtempSync(join(tmpdir(), 'observation-cancel-'));
+ let cleanupConfirmed = true;
+ try {
+ const run = createVitestTempFixture(host, 'server', '');
+ const fixture = join(host, 'fixture');
+ const ready = join(fixture, 'ready');
+ const finished = join(fixture, 'finished');
+ const observationPath = fileURLToPath(new URL('../server/services/eidoverseObservationLedger.test.js', import.meta.url));
+ const source = readFileSync(observationPath, 'utf8')
+ .replace(/(['"])(\.\.?\/[^'"]+)\1/g, (_, _quote, specifier) => JSON.stringify(resolve(dirname(observationPath), specifier)))
+ .replace("import { mkdirSync, rmSync, writeFileSync }", "import { existsSync, mkdirSync, rmSync, writeFileSync }")
+ .replace(' await Promise.race([collected.promise, observations[1]]);', ` await Promise.race([collected.promise, observations[1]]);
+ writeFileSync(${JSON.stringify(ready)}, 'both-observations-collected');
+ await new Promise(resolve => {
+ signal.addEventListener('abort', resolve, { once: true });
+ if (signal.aborted) resolve();
+ });`)
+ .replace(' await Promise.allSettled(observations);', ` await Promise.allSettled(observations);
+ writeFileSync(${JSON.stringify(finished)}, JSON.stringify({
+ collected: collectSources.mock.calls.length,
+ controllers: listControllers.mock.calls.length,
+ foundations: listFoundations.mock.calls.length,
+ recipes: readRecipe.mock.calls.length,
+ cancelled: signal.aborted,
+ rootPresent: existsSync(dataRoot()),
+ }));`);
+ expect(source).toContain('both-observations-collected');
+ expect(source).toContain('cancelled: signal.aborted');
+ writeFileSync(join(fixture, 'lifecycle.test.js'), source);
+ writeFileSync(join(fixture, 'failure.test.js'), fixtureReadyFailureSource(ready));
+ run.args[run.args.indexOf('--maxWorkers') + 1] = '2';
+ run.args.push('--bail=1', '--testNamePattern', 'serializes concurrent observations|controlled sibling failure');
+ const result = await runFixtureToCompletion(run, 120000, { signal });
+ cleanupConfirmed = result.closeConfirmed;
+ if (!cleanupConfirmed) throw result.error;
+ if (signal.aborted) return;
+ const state = JSON.stringify({ ready: existsSync(ready), finished: existsSync(finished), status: result.status, error: result.error?.name ?? null });
+ expect(Boolean(result.error), state).toBe(false);
+ expect(result.status, state).toBe(1);
+ expect(result.stderr.includes('controlled sibling failure'), state).toBe(true);
+ expect(existsSync(finished), state).toBe(true);
+ expect(JSON.parse(readFileSync(finished, 'utf8'))).toEqual({ collected: 2, controllers: 2, foundations: 2, recipes: 2, cancelled: true, rootPresent: true });
+ expect(/test temp leak:|test process leak:|Unhandled|ENOTEMPTY|ENOENT/.test(result.stderr), state).toBe(false);
+ expect(readdirSync(host).filter(name => name.startsWith('pvt-')), state).toEqual([]);
+ } finally {
+ if (cleanupConfirmed) rmSync(host, { recursive: true, force: true });
+ }
+}, 135000);
diff --git a/server/services/eidoverseObservationLedger.test.js b/server/services/eidoverseObservationLedger.test.js
index 6db8593acc..faa26c8d76 100644
--- a/server/services/eidoverseObservationLedger.test.js
+++ b/server/services/eidoverseObservationLedger.test.js
@@ -7,15 +7,27 @@
* failing the whole tour, and an unreadable marker file never silently reports
* a settled world as brand new.
*/
-import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterAll, beforeEach, describe, expect, it as vitestIt, vi } from 'vitest';
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
-import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
+import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy, ownTestBodies } from '../lib/mockPathsDataRoot.js';
import { eidoversePeerId } from '../lib/eidoverseWorldSignals.js';
+const owned = ownTestBodies(vitestIt);
+const it = owned.it;
+
const dataRoot = () => lazyTempDataRoot('portos-eidoverse-observation-');
-vi.mock('../lib/fileUtils.js', async (importOriginal) => makePathsProxy(await importOriginal(), { dataRoot }));
+let beforeMarkerWrite = async () => {};
+vi.mock('../lib/fileUtils.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ return makePathsProxy(actual, { dataRoot, overrides: {
+ atomicWrite: async (...args) => {
+ await beforeMarkerWrite();
+ return actual.atomicWrite(...args);
+ },
+ } });
+});
const collectSources = vi.fn();
const listFoundations = vi.fn();
@@ -61,7 +73,9 @@ const peerSignal = () => ({
id: ALPHA_ID, label: 'Federated peer', status: 'active', enabled: true, fullSync: true, travelAvailable: true,
});
-beforeEach(() => {
+beforeEach(async () => {
+ await owned.drain();
+ beforeMarkerWrite = async () => {};
rmSync(dataRoot(), { recursive: true, force: true });
collectSources.mockReset().mockResolvedValue({ peers: [peerSignal()], agents: [], tasks: [] });
listFoundations.mockReset().mockResolvedValue({ counts: { vernacular: 0, baseline: 1, candidates: 0, inherited: 1 }, foundations: [inherited('peer:alpha:one')] });
@@ -70,7 +84,10 @@ beforeEach(() => {
listDestinations.mockReset().mockResolvedValue({ destinations: [{ peerId: ALPHA_ID, label: 'Peer' }] });
});
-afterAll(cleanupTempDataRoots);
+afterAll(async () => {
+ await owned.drain();
+ cleanupTempDataRoots();
+});
describe('the Eidoverse observation marker', () => {
it('carries what is new across the wake boundary the marker exists for', async () => {
@@ -141,15 +158,41 @@ describe('the Eidoverse observation marker', () => {
expect(observed.peers.map((peer) => peer.peerId)).toEqual([ALPHA_ID]);
});
- it('serializes concurrent observations so one marker write cannot lose the other', async () => {
- const observations = await Promise.all([
- observeEidoverseWorld({ now: () => '2026-09-16T12:00:00.000Z' }),
- observeEidoverseWorld({ now: () => '2026-09-16T12:00:01.000Z' }),
- ]);
-
- // Whichever ran second must have SEEN the first, not raced past it.
- expect(observations.filter((entry) => entry.changes.firstObservation)).toHaveLength(1);
- expect(observations.filter((entry) => !entry.changes.firstObservation)).toHaveLength(1);
+ it('serializes concurrent observations so one marker write cannot lose the other', async ({ signal }) => {
+ const writing = Promise.withResolvers();
+ const release = Promise.withResolvers();
+ const collected = Promise.withResolvers();
+ const observations = [];
+ const cancel = () => { writing.resolve(); collected.resolve(); release.resolve(); };
+ signal.addEventListener('abort', cancel, { once: true });
+ if (signal.aborted) cancel();
+ beforeMarkerWrite = () => { writing.resolve(); return release.promise; };
+ try {
+ observations.push(observeEidoverseWorld({ now: () => '2026-09-16T12:00:00.000Z' }));
+ await Promise.race([writing.promise, observations[0]]);
+ if (signal.aborted) return;
+ // Overlap the persistence boundary, after the first call's imports have
+ // settled. Vitest's manual mock callstack cannot isolate parallel dynamic
+ // imports: the second call otherwise reaches real host probes (#13477).
+ collectSources.mockImplementation(async () => {
+ collected.resolve();
+ return { peers: [peerSignal()], agents: [], tasks: [] };
+ });
+ observations.push(observeEidoverseWorld({ now: () => '2026-09-16T12:00:01.000Z' }));
+ await Promise.race([collected.promise, observations[1]]);
+ release.resolve();
+ const results = await Promise.all(observations);
+ expect(collectSources).toHaveBeenCalledTimes(2);
+ expect(listFoundations).toHaveBeenCalledTimes(2);
+ expect(listControllers).toHaveBeenCalledTimes(2);
+ expect(readRecipe).toHaveBeenCalledTimes(2);
+ expect(results.filter((entry) => entry.changes.firstObservation)).toHaveLength(1);
+ expect(results.filter((entry) => !entry.changes.firstObservation)).toHaveLength(1);
+ } finally {
+ release.resolve();
+ await Promise.allSettled(observations);
+ signal.removeEventListener('abort', cancel);
+ }
});
});