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

Prove "nothing happened" by awaiting completion, not by sleeping past it

[._.]/ Adam Eivy428665050c02
diff --git a/client/src/components/musicVideo/ShotFinalRender.test.jsx b/client/src/components/musicVideo/ShotFinalRender.test.jsx
index 14a3486b8d..44b86d3f6f 100644
--- a/client/src/components/musicVideo/ShotFinalRender.test.jsx
+++ b/client/src/components/musicVideo/ShotFinalRender.test.jsx
@@ -95,9 +95,17 @@ describe('ShotFinalRender', () => {
expect(await screen.findByText('Shot 3: the selected clip file is missing')).toBeTruthy();
unmount();
getMusicVideoShotRenders.mockClear();
- const { container } = render(<ShotFinalRender project={{ ...project, composition: { mode: 'concat' } }} scene={project.scenes[0]} onChange={() => {}} fieldId={fieldId} />);
- await new Promise((r) => setTimeout(r, 700));
- expect(getMusicVideoShotRenders).not.toHaveBeenCalled();
- expect(container.innerHTML).toBe('');
+ // The read is debounced 600ms; fake time runs past that debounce exactly
+ // instead of sleeping and hoping a loaded worker kept up (#12047).
+ vi.useFakeTimers();
+ try {
+ const { container } = render(<ShotFinalRender project={{ ...project, composition: { mode: 'concat' } }} scene={project.scenes[0]} onChange={() => {}} fieldId={fieldId} />);
+ await act(async () => { await vi.advanceTimersByTimeAsync(700); });
+ expect(vi.getTimerCount()).toBe(0);
+ expect(getMusicVideoShotRenders).not.toHaveBeenCalled();
+ expect(container.innerHTML).toBe('');
+ } finally {
+ vi.useRealTimers();
+ }
});
});
diff --git a/scripts/ci-test-plan.js b/scripts/ci-test-plan.js
index 075be20f13..83c7c2cbd6 100644
--- a/scripts/ci-test-plan.js
+++ b/scripts/ci-test-plan.js
@@ -351,6 +351,9 @@ export const ALWAYS_RUN_TESTS = [
// Whole-tree scanner: a renamed export in any module leaves a dead key in some
// suite's vi.mock factory, with no import edge from that suite to this guard (#11891).
'server/mockContractConventions.test.js',
+ // Whole-tree scanner over git-tracked tests (#12047): any suite can add a
+ // real sleep before a not-called assertion with no import edge to this guard.
+ 'server/negativeProofSleepConventions.test.js',
// Whole-tree scanner: any server file can spawn a child and await before
// attaching its error/exit listener (#11761), with no import edge to this file.
'server/childListenerOrderConventions.test.js',
diff --git a/server/lib/managedDaemon.idle.test.js b/server/lib/managedDaemon.idle.test.js
index cba896399a..006457012b 100644
--- a/server/lib/managedDaemon.idle.test.js
+++ b/server/lib/managedDaemon.idle.test.js
@@ -178,13 +178,21 @@ describe('managedDaemon idle reaper', () => {
});
it('stops sweeping once disarmed', async () => {
- const stop = vi.fn().mockResolvedValue(undefined);
- // Window 0 so a stray sweep can't stop it — this asserts the timer, not the policy.
- registerIdleDaemon({ name: 'daemon-a', getIdleMs: () => 0, stop });
- startIdleReaper({ intervalMs: 5 });
- stopIdleReaper();
- await new Promise((r) => setTimeout(r, 25));
- expect(stop).not.toHaveBeenCalled();
+ // Fake time makes "five intervals passed" exact rather than a real sleep a
+ // loaded worker can outrun (#12047).
+ vi.useFakeTimers();
+ try {
+ const stop = vi.fn().mockResolvedValue(undefined);
+ // Window 0 so a stray sweep can't stop it — this asserts the timer, not the policy.
+ registerIdleDaemon({ name: 'daemon-a', getIdleMs: () => 0, stop });
+ startIdleReaper({ intervalMs: 5 });
+ stopIdleReaper();
+ await vi.advanceTimersByTimeAsync(25);
+ expect(vi.getTimerCount()).toBe(0);
+ expect(stop).not.toHaveBeenCalled();
+ } finally {
+ vi.useRealTimers();
+ }
});
});
});
diff --git a/server/negativeProofSleepConventions.test.js b/server/negativeProofSleepConventions.test.js
new file mode 100644
index 0000000000..5c2d5d6b91
--- /dev/null
+++ b/server/negativeProofSleepConventions.test.js
@@ -0,0 +1,129 @@
+/**
+ * Repo-wide guard: a negative proof may not rest on a real sleep.
+ *
+ * await new Promise((r) => setTimeout(r, 30));
+ * expect(writeMock).not.toHaveBeenCalled();
+ *
+ * That passes whenever the handler is merely slower than the sleep — on a
+ * loaded CI worker the asserted-absent call lands after the assertion, so the
+ * test is green against the exact regression it exists to catch, while every
+ * run pays the sleep (#12047). The deterministic shapes are: await the
+ * handler's own completion signal (`hook.__testing.settled()` on the media-job
+ * hooks), drive the clock with fake timers (`vi.advanceTimersByTimeAsync`), or
+ * wait for a positive signal with `vi.waitFor` and only then assert the negative.
+ *
+ * ## Burn-down
+ *
+ * `BURN_DOWN` names the test files that still carry the pattern. It only
+ * shrinks: a file that no longer matches must leave the list (the stale-entry
+ * assertion fails otherwise), and a new site anywhere fails outright.
+ *
+ * ## What the scan sees
+ *
+ * A one-line real sleep (`await new Promise(<fn> => setTimeout(<fn>, <literal>))`)
+ * followed, before the enclosing test ends and within the next few lines, by
+ * `.not.toHaveBeenCalled` / `toHaveBeenCalledTimes(0)`. A sleep bound to a
+ * helper (`await sleep(30)`) or a negative assertion further away is not seen;
+ * the guard errs toward silence rather than guessing at intent.
+ */
+
+import { describe, it, expect } from 'vitest';
+import { execFileSync } from 'node:child_process';
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const ROOT = fileURLToPath(new URL('..', import.meta.url));
+// This file spells the pattern out in its docs and recognizer fixtures.
+const SELF = 'server/negativeProofSleepConventions.test.js';
+
+const BURN_DOWN = [
+ 'client/src/components/meatspace/post/PostSessionLauncher.test.jsx',
+ 'server/services/agentCliSpawning.test.js',
+ 'server/services/backupScheduler.test.js',
+ 'server/services/creativeDirectorSceneImageHook.test.js',
+ 'server/services/fableLoomSceneImageHook.test.js',
+ 'server/services/fableLoomSceneVideoHook.test.js',
+ 'server/services/mediaJobImageHook.test.js',
+ 'server/services/musicVideo/autonomousService.test.js',
+ 'server/services/runner.test.js',
+ 'server/services/writersRoomSceneImageHook.test.js',
+];
+
+const REAL_SLEEP = /await\s+new\s+Promise\s*\(\s*\(?\s*\w*\s*\)?\s*=>\s*setTimeout\s*\(\s*\w+\s*,\s*\d+\s*\)\s*\)/;
+const NEGATIVE_CALL = /\.not\.toHaveBeenCalled\b|toHaveBeenCalledTimes\(\s*0\s*\)/;
+const TEST_BOUNDARY = /^\s*(?:it|test|describe)(?:\.\w+)*\(|^\s*}\)?;?\s*$/;
+const LOOKAHEAD_LINES = 5;
+
+/** `file:line` for every real sleep that a negative call assertion follows. */
+export function scanNegativeProofSleeps(source, file) {
+ const lines = source.split('\n');
+ const hits = [];
+ for (let i = 0; i < lines.length; i += 1) {
+ if (!REAL_SLEEP.test(lines[i])) continue;
+ for (let j = i + 1; j < Math.min(lines.length, i + 1 + LOOKAHEAD_LINES); j += 1) {
+ if (TEST_BOUNDARY.test(lines[j])) break;
+ if (NEGATIVE_CALL.test(lines[j])) { hits.push(`${file}:${i + 1}`); break; }
+ }
+ }
+ return hits;
+}
+
+function trackedTests() {
+ return execFileSync('git', ['ls-files', '-z', '--cached', '--others', '--exclude-standard', 'server', 'scripts', 'client/src', 'lib', 'autofixer'], { cwd: ROOT, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 })
+ .split('\0').filter((f) => /\.test\.[cm]?jsx?$/.test(f) && f !== SELF);
+}
+
+describe('negative proofs do not rest on real sleeps (#12047)', () => {
+ it('flags a sleep followed by a not-called assertion and nothing else', () => {
+ const flagged = [
+ " await new Promise((r) => setTimeout(r, 30));",
+ ' expect(setPortraitMedia).not.toHaveBeenCalled();',
+ ].join('\n');
+ expect(scanNegativeProofSleeps(flagged, 'probe.test.js')).toEqual(['probe.test.js:1']);
+
+ const zeroTimes = [
+ ' await new Promise((resolve) => setTimeout(resolve, 700));',
+ ' expect(container.innerHTML).toBe("");',
+ ' expect(fetchMock).toHaveBeenCalledTimes(0);',
+ ].join('\n');
+ expect(scanNegativeProofSleeps(zeroTimes, 'probe.test.js')).toEqual(['probe.test.js:1']);
+
+ const positive = [
+ ' await new Promise((r) => setTimeout(r, 30));',
+ ' expect(attachMedia).toHaveBeenCalledWith("a");',
+ ' });',
+ ' it("other", () => {',
+ ' expect(other).not.toHaveBeenCalled();',
+ ].join('\n');
+ expect(scanNegativeProofSleeps(positive, 'probe.test.js')).toEqual([]);
+
+ const fakeTimers = [
+ ' await vi.advanceTimersByTimeAsync(50);',
+ ' expect(stop).not.toHaveBeenCalled();',
+ ].join('\n');
+ expect(scanNegativeProofSleeps(fakeTimers, 'probe.test.js')).toEqual([]);
+ });
+
+ it('keeps the pattern out of every test file not on the burn-down list', () => {
+ const files = trackedTests();
+ expect(files.length).toBeGreaterThan(100);
+ const burnDown = new Set(BURN_DOWN);
+ const offenders = files
+ .filter((f) => !burnDown.has(f))
+ .flatMap((f) => scanNegativeProofSleeps(readFileSync(join(ROOT, f), 'utf8'), f));
+ expect(
+ offenders,
+ 'await the handler\'s completion signal, drive fake timers, or vi.waitFor a positive signal before the negative assertion. Do NOT add to BURN_DOWN — it only shrinks.',
+ ).toEqual([]);
+ });
+
+ it('drops burn-down entries once their file is clean', () => {
+ const files = new Set(trackedTests());
+ const stale = BURN_DOWN.filter((f) => !files.has(f)
+ || scanNegativeProofSleeps(readFileSync(join(ROOT, f), 'utf8'), f).length === 0);
+ expect(stale, 'these files no longer carry the pattern — remove them from BURN_DOWN').toEqual([]);
+ expect([...BURN_DOWN].sort()).toEqual(BURN_DOWN);
+ expect(new Set(BURN_DOWN).size).toBe(BURN_DOWN.length);
+ });
+});
diff --git a/server/services/catalogImageAttachHook.js b/server/services/catalogImageAttachHook.js
index b45ee2d2ed..dd5192029f 100644
--- a/server/services/catalogImageAttachHook.js
+++ b/server/services/catalogImageAttachHook.js
@@ -125,4 +125,5 @@ export const __testing = {
hook.__testing.reset();
portraitGuard.clear();
},
+ settled: hook.__testing.settled,
};
diff --git a/server/services/catalogImageAttachHook.test.js b/server/services/catalogImageAttachHook.test.js
index ef4998f474..b832080afd 100644
--- a/server/services/catalogImageAttachHook.test.js
+++ b/server/services/catalogImageAttachHook.test.js
@@ -93,8 +93,8 @@ describe('catalogImageAttachHook', () => {
it('is idempotent — skips when the filename is already attached (client won the race)', async () => {
listMediaForIngredient.mockResolvedValue([{ mediaKey: 'dup.png', kind: 'portrait' }]);
mediaJobEvents.emit('completed', completedImageJob({ catalogAttach: { ingredientId: 'ing-5' } }, 'dup.png'));
- // Give the async handler a chance to run, then assert no second write.
- await new Promise((r) => setTimeout(r, 30));
+ // The handler has finished deciding; only now is "no write" a proof.
+ await hook.__testing.settled();
expect(setPortraitMedia).not.toHaveBeenCalled();
expect(attachMedia).not.toHaveBeenCalled();
});
@@ -108,7 +108,7 @@ describe('catalogImageAttachHook', () => {
{ mediaKey: 'dup.png', kind: 'reference' },
]);
mediaJobEvents.emit('completed', completedImageJob({ catalogAttach: { ingredientId: 'ing-8' } }, 'dup.png'));
- await new Promise((r) => setTimeout(r, 30));
+ await hook.__testing.settled();
expect(setPortraitMedia).not.toHaveBeenCalled();
expect(attachMedia).not.toHaveBeenCalled();
});
@@ -151,7 +151,7 @@ describe('catalogImageAttachHook', () => {
it('skips attaching when the target ingredient was deleted before the render completed', async () => {
getIngredient.mockResolvedValue(null); // soft- or hard-deleted: getIngredient filters deleted=false
mediaJobEvents.emit('completed', completedImageJob({ catalogAttach: { ingredientId: 'ing-gone' } }, 'orphan.png'));
- await new Promise((r) => setTimeout(r, 30));
+ await hook.__testing.settled();
expect(listMediaForIngredient).not.toHaveBeenCalled();
expect(setPortraitMedia).not.toHaveBeenCalled();
expect(attachMedia).not.toHaveBeenCalled();
@@ -160,7 +160,7 @@ describe('catalogImageAttachHook', () => {
it('ignores jobs without a catalogAttach tag', async () => {
mediaJobEvents.emit('completed', completedImageJob({ universeRun: { collectionId: 'c1' } }, 'x.png'));
mediaJobEvents.emit('completed', completedImageJob({}, 'y.png'));
- await new Promise((r) => setTimeout(r, 30));
+ await hook.__testing.settled();
expect(listMediaForIngredient).not.toHaveBeenCalled();
expect(attachMedia).not.toHaveBeenCalled();
expect(setPortraitMedia).not.toHaveBeenCalled();
@@ -169,7 +169,7 @@ describe('catalogImageAttachHook', () => {
it('ignores non-image jobs and jobs with no result filename', async () => {
mediaJobEvents.emit('completed', { kind: 'video', params: { catalogAttach: { ingredientId: 'ing-6' } }, result: { filename: 'v.mp4' } });
mediaJobEvents.emit('completed', { kind: 'image', params: { catalogAttach: { ingredientId: 'ing-6' } }, result: {} });
- await new Promise((r) => setTimeout(r, 30));
+ await hook.__testing.settled();
expect(listMediaForIngredient).not.toHaveBeenCalled();
});
@@ -250,7 +250,7 @@ describe('catalogImageAttachHook', () => {
listMediaForIngredient.mockRejectedValueOnce(new Error('db down'));
// Must not throw / leave an unhandled rejection.
mediaJobEvents.emit('completed', completedImageJob({ catalogAttach: { ingredientId: 'ing-7' } }, 'z.png'));
- await new Promise((r) => setTimeout(r, 30));
+ await hook.__testing.settled();
expect(setPortraitMedia).not.toHaveBeenCalled();
expect(attachMedia).not.toHaveBeenCalled();
});
diff --git a/server/services/creativeDirectorMusicBedHook.test.js b/server/services/creativeDirectorMusicBedHook.test.js
index 927c8e67c4..8a333bc577 100644
--- a/server/services/creativeDirectorMusicBedHook.test.js
+++ b/server/services/creativeDirectorMusicBedHook.test.js
@@ -77,25 +77,25 @@ describe('creativeDirectorMusicBedHook', () => {
it('ignores a completed job with no creativeDirectorMusicBed tag', async () => {
mediaJobEvents.emit('completed', completedAudioJob({ params: { prompt: 'plain audio render' } }));
- await new Promise((r) => setTimeout(r, 20));
+ await hook.__testing.settled();
expect(updateProject).not.toHaveBeenCalled();
});
it('ignores a non-audio job kind (other hooks own those)', async () => {
mediaJobEvents.emit('completed', { kind: 'image', params: { creativeDirectorMusicBed: tag() }, result: { filename: 'img.png' } });
- await new Promise((r) => setTimeout(r, 20));
+ await hook.__testing.settled();
expect(updateProject).not.toHaveBeenCalled();
});
it('skips a tag missing projectId', async () => {
mediaJobEvents.emit('completed', completedAudioJob({ params: { creativeDirectorMusicBed: {} } }));
- await new Promise((r) => setTimeout(r, 20));
+ await hook.__testing.settled();
expect(updateProject).not.toHaveBeenCalled();
});
it('skips a completed job whose result carries no filename', async () => {
mediaJobEvents.emit('completed', completedAudioJob({ params: { creativeDirectorMusicBed: tag() }, result: { filename: undefined } }));
- await new Promise((r) => setTimeout(r, 20));
+ await hook.__testing.settled();
expect(updateProject).not.toHaveBeenCalled();
});
@@ -103,7 +103,7 @@ describe('creativeDirectorMusicBedHook', () => {
getProject.mockRejectedValueOnce(Object.assign(new Error('Project not found'), { status: 404 }));
mediaJobEvents.emit('completed', completedAudioJob({ params: { creativeDirectorMusicBed: tag() } }));
await waitFor(() => getProject.mock.calls.length > 0);
- await new Promise((r) => setTimeout(r, 20));
+ await hook.__testing.settled();
// No throw escapes the listener; nothing further to assert beyond "didn't crash".
expect(updateProject).not.toHaveBeenCalled();
});
@@ -112,7 +112,7 @@ describe('creativeDirectorMusicBedHook', () => {
getProject.mockImplementationOnce(async (id) => ({ id, musicBed: { filename: 'already-there.wav' } }));
mediaJobEvents.emit('completed', completedAudioJob({ params: { creativeDirectorMusicBed: tag() } }));
await waitFor(() => getProject.mock.calls.length > 0);
- await new Promise((r) => setTimeout(r, 20));
+ await hook.__testing.settled();
expect(updateProject).not.toHaveBeenCalled();
});
diff --git a/server/services/mediaJobImageHook.js b/server/services/mediaJobImageHook.js
index f36c3900cb..8a016fcbfd 100644
--- a/server/services/mediaJobImageHook.js
+++ b/server/services/mediaJobImageHook.js
@@ -27,7 +27,10 @@
* whose `job.queuedAt` is older than the newest already applied to that
* scene slot — read+written inside the serialize section so it never races);
* - best-effort error logging (a bookkeeping miss is logged, never thrown);
- * - `__testing.reset()` (off the listener, clear the queue + guard).
+ * - `__testing.reset()` (off the listener, clear the queue + guard) and
+ * `__testing.settled()` (resolves once every completion the listener has
+ * accepted has finished, so a test proves "nothing was written" without a
+ * real sleep — #12047).
*
* What each hook supplies via config:
* - `label` — used in the failure / crash log lines.
@@ -104,6 +107,9 @@ export function createMediaJobImageHook(config) {
let completedHandler = null;
let terminalHandlers = null;
+ // Every accepted completion's work, so tests can await the outcome instead
+ // of sleeping past it. Entries remove themselves when they settle.
+ const inFlight = new Set();
// The tag → identity half of `decode`, shared with the terminal path (a
// failed job carries no result to extract).
@@ -138,7 +144,7 @@ export function createMediaJobImageHook(config) {
completedHandler = (job) => {
// Admitted synchronously (before any await) so the attach — a durable
// row write that points at the render's file — is never split by a backup cut.
- void withBackupAssetPublication(async () => {
+ const work = withBackupAssetPublication(async () => {
const ctx = decode(job);
if (!ctx) return;
const sKey = sceneKey ? sceneKey(ctx) : null;
@@ -169,6 +175,8 @@ export function createMediaJobImageHook(config) {
// Last-resort net for synchronous throws (unexpected job shape, etc).
console.log(`⚠️ ${label} hook crashed: ${err?.message || err}`);
});
+ inFlight.add(work);
+ work.finally(() => inFlight.delete(work));
};
mediaJobEvents.on('completed', completedHandler);
@@ -204,6 +212,11 @@ export function createMediaJobImageHook(config) {
serialize.clear();
guard.clear();
},
+ // Loops because a settling attach may not be the last: a completion
+ // emitted while an earlier one was in flight joins the set late.
+ settled: async () => {
+ while (inFlight.size > 0) await Promise.allSettled([...inFlight]);
+ },
};
return { init, __testing };