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

fix: drain the image-gen quota bus handler in tests instead of sleeping 20ms (#4788)

[._.]/ Adam Eivy24b4edd8b5bc
diff --git a/server/services/imageGenQuota.js b/server/services/imageGenQuota.js
index 869145d3d0..3bb110137c 100644
--- a/server/services/imageGenQuota.js
+++ b/server/services/imageGenQuota.js
@@ -164,6 +164,16 @@ export async function recordImageGenOutcome({ mode, ok, error = '', at = Date.no
let subscribed = false;
+// Single tail over every outcome the bus handlers have dispatched. An emitter
+// cannot await its listeners, so the handlers below are fire-and-forget and
+// nothing else can answer "has the render just announced on the bus actually
+// been written yet?". The recorder's write is real disk I/O — and on Windows
+// `atomicWrite` additionally sleeps between rename retries when the
+// destination is momentarily locked — so its latency is unbounded and a test
+// that sleeps a fixed number of milliseconds instead is a flake by
+// construction (#4788). Tests await this tail instead.
+let dispatched = Promise.resolve();
+
/**
* Subscribe the quota recorder to the image-generation bus. Called once at
* boot from `initMediaJobDependentHooks`. Idempotent.
@@ -175,8 +185,10 @@ let subscribed = false;
export function initImageGenQuotaHook() {
if (subscribed) return;
const note = (ok) => (payload) => {
- recordImageGenOutcome({ mode: payload?.mode, ok, error: payload?.error })
+ // Already caught, so the tail below can never reject or go unhandled.
+ const recorded = recordImageGenOutcome({ mode: payload?.mode, ok, error: payload?.error })
.catch((err) => console.error(`❌ Image-gen quota hook: ${err.message}`));
+ dispatched = dispatched.then(() => recorded);
};
imageGenEvents.on('completed', note(true));
imageGenEvents.on('failed', note(false));
@@ -186,6 +198,21 @@ export function initImageGenQuotaHook() {
/** Test-only: allow a suite to re-subscribe against fresh listeners. */
export function __resetImageGenQuotaHookForTests() {
subscribed = false;
+ dispatched = Promise.resolve();
+}
+
+/**
+ * Test-only: settle every render outcome the bus handlers have dispatched, so
+ * a suite can assert on the ledger the instant the recorder is done rather
+ * than guessing at a sleep. Loops until quiescent, so work a settling handler
+ * chains on is drained too.
+ */
+export async function __drainImageGenQuotaHookForTests() {
+ let seen;
+ do {
+ seen = dispatched;
+ await seen;
+ } while (dispatched !== seen);
}
/**
diff --git a/server/services/imageGenQuota.test.js b/server/services/imageGenQuota.test.js
index 31585a9402..24b489be0b 100644
--- a/server/services/imageGenQuota.test.js
+++ b/server/services/imageGenQuota.test.js
@@ -17,6 +17,7 @@ const {
isQuotaTrackedImageMode,
initImageGenQuotaHook,
__resetImageGenQuotaHookForTests,
+ __drainImageGenQuotaHookForTests,
} = await import('./imageGenQuota.js');
const { imageGenEvents } = await import('./imageGenEvents.js');
const { checkFabrication } = await import('./imageGen/fabricationGuard.js');
@@ -140,12 +141,19 @@ describe('isQuotaTrackedImageMode', () => {
describe('initImageGenQuotaHook', () => {
// The recorder rides the imageGenEvents bus rather than provider finalizers,
// so a backend that emits on the bus is tracked without touching its code.
- const flush = () => new Promise((resolve) => setTimeout(resolve, 20));
+ // An emitter cannot await its listeners, so the assertions below have to wait
+ // for the recorder's write by asking the hook when it is done. Sleeping a
+ // fixed 20ms instead is what made this suite flake on the Windows runner
+ // (#4788): the write is real disk I/O, and `atomicWrite` sleeps between
+ // rename retries there when the destination is briefly locked, so it can
+ // easily outlast any constant a test picks.
+ const flush = () => __drainImageGenQuotaHookForTests();
// The bus handler stamps its own `Date.now()`, so unlike the `getImageGenQuota`
// suite below there is no `now` to thread in — freeze the clock instead.
- // `shouldAdvanceTime` keeps `flush()`'s real 20ms timer working under the fake
- // clock; without it the flush promise never settles and the test times out.
+ // `shouldAdvanceTime` keeps the real timers the write path itself may use
+ // (`atomicWrite`'s Windows rename-retry sleep) advancing under the fake clock;
+ // without it such a write would never settle and the drain would hang.
beforeEach(() => {
vi.useFakeTimers({ shouldAdvanceTime: true });
vi.setSystemTime(FIXTURE_NOW);