From 7134c7d5174535fbcbb8ff7f0e6c92ab1d6fe1b4 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 15 Nov 2026 21:32:41 +0000 Subject: [PATCH] test(ui-server): keep the real storage budget out of queue-adoption holds (#3561) Every write the dialogue journal tracks arms the real 10 s storage budget at submission. The queue-adoption cases submit up to 256 writes behind a native writer hold, and the worker drains them serially after release, so on a loaded host a write can stay pending past 10 s. The budget then withholds the owner and reports storage_indeterminate, and the case fails on whichever assertion reads the withheld callbacks or pages. The budget is not what these cases claim. storageDeadlineClock gains a manual mode, in which a registered budget fires only through expire() or captureLatest(), and counts registrations. The fixture installs it, and each case now asserts that budgets were armed and that every one was cleared by its write settling. Co-Authored-By: Claude Opus 5.5 --- .../tests/dialogue-queue-adoption.test.ts | 25 ++++++++++++++++--- .../tests/helpers/storage-deadline-clock.ts | 18 ++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/ui-server/tests/dialogue-queue-adoption.test.ts b/packages/ui-server/tests/dialogue-queue-adoption.test.ts index 2b36517981..9653b94a8f 100644 --- a/packages/ui-server/tests/dialogue-queue-adoption.test.ts +++ b/packages/ui-server/tests/dialogue-queue-adoption.test.ts @@ -10,6 +10,7 @@ import { createSessionCatalog } from "../src/ws/session-catalog.js"; import { DialogueJournal } from "../src/ws/dialogue.js"; import type { DialogueOwner } from "../src/ws/dialogue-storage.js"; import * as controls from "./helpers/dialogue-test-writes.js"; +import { storageDeadlineClock } from "./helpers/storage-deadline-clock.js"; type Queue = { count: number; bytes: number }; function queues(journal: DialogueJournal): Map { @@ -23,6 +24,12 @@ function evidence(sequence: number, text = "The mast is ready."): Extract db, undefined, undefined, { @@ -34,6 +41,11 @@ async function fixture() { const holds: { gate: Int32Array; done: Promise }[] = []; const release = () => { for (const { gate } of holds) { Atomics.store(gate, 0, 1); Atomics.notify(gate, 0); } }; return { db, writer, catalog, journal, failures, pages, release, control, + /** Every armed budget was cleared by its write settling, none by the clock. */ + budgetsSettled() { + expect(clock.registered, "STORAGE_BUDGET_ARMED_DURING_HOLD").toBeGreaterThan(0); + expect(clock.pending, "STORAGE_BUDGET_CLEARED_BY_SETTLEMENT").toBe(0); + }, async hold() { const gate = new Int32Array(new SharedArrayBuffer(8)); const done = control.call("hold", gate); @@ -46,9 +58,11 @@ async function fixture() { expect(writer.offloop).toBe(true); }, async close() { - release(); await Promise.all(holds.map(h => h.done)); await journal.settled(); - await catalog.settled!(); await control.close(); await writer.close(); db.close(); - rmSync(dir, { recursive: true, force: true }); + try { + release(); await Promise.all(holds.map(h => h.done)); await journal.settled(); + await catalog.settled!(); await control.close(); await writer.close(); db.close(); + rmSync(dir, { recursive: true, force: true }); + } finally { clock.restore(); } }, }; } @@ -81,6 +95,7 @@ for (const merge of [false, true]) test(`native adoption retires idle queue book expect(saved).toEqual([true, true]); expect(queues(f.journal).size).toBe(0); expect(f.db.query("SELECT closed FROM dialogue_conversations WHERE id='ithaca'").get()).toEqual({ closed: 1 }); + f.budgetsSettled(); } finally { await f.close(); } }); @@ -128,6 +143,7 @@ for (const bound of ["count", "bytes"] as const) test(`native alias adoption can expect(f.db.query("SELECT coverage FROM dialogue_owners").get()).toEqual({ coverage: "partial" }); expect((await f.catalog.dialogueRead!("odysseus", { sessionId: "voyage" }))!.coverage).toBe("partial"); await expect(f.journal.settled(true)).rejects.toThrow("pending writes exceeded"); + f.budgetsSettled(); } finally { f.release(); releaseCallbacks(); await f.close(); } }); @@ -165,6 +181,7 @@ for (const bound of ["count", "bytes"] as const) for (const merge of [false, tru expect(queues(f.journal).size, "ALL_CANONICAL_QUEUE_ENTRIES_RETIRE").toBe(0); expect(f.db.query("SELECT COUNT(*) AS n FROM dialogue_records").get()).toEqual({ n: count }); if (bound === "bytes") expect(count).toBeLessThan(CONVERSATION_LIMITS.maxPendingDialogueObservations); + f.budgetsSettled(); } finally { f.release(); releaseCallbacks(); await f.close(); } }); @@ -193,6 +210,7 @@ for (const merge of [false, true]) test(`native rejected writes retire drained q expect(f.db.query("SELECT COUNT(*) AS n FROM dialogue_records").get()).toEqual({ n: saved.filter(Boolean).length }); expect(f.pages.flatMap(page => page.records).map(row => row.sequence)).toEqual(Array.from({ length: saved.filter(Boolean).length }, (_, i) => i + 1)); expect((await f.catalog.dialogueRead!("odysseus", { sessionId: "voyage" }))!.coverage).toBe("partial"); + f.budgetsSettled(); } finally { await f.close(); } }); @@ -258,5 +276,6 @@ test(`fresh-owner ${route} shares ${bound} admission with its canonical target o expect(f.failures.length, "SETTLED_CALLBACKS_RESTORE_ADMISSION").toBe(failures); expect(f.db.query("SELECT COUNT(*) AS n FROM dialogue_records").get()).toEqual({ n: count + 100 }); expect(queues(f.journal).size).toBe(0); + f.budgetsSettled(); } finally { await f.close(); } }); diff --git a/packages/ui-server/tests/helpers/storage-deadline-clock.ts b/packages/ui-server/tests/helpers/storage-deadline-clock.ts index 571e1d0d6b..cf6b365d4c 100644 --- a/packages/ui-server/tests/helpers/storage-deadline-clock.ts +++ b/packages/ui-server/tests/helpers/storage-deadline-clock.ts @@ -1,20 +1,32 @@ import { STORAGE_SETTLEMENT_MS } from "../../src/db/settlement.js"; -/** Advance only the production storage budget; native I/O remains genuinely held. */ -export function storageDeadlineClock() { +/** + * Advance only the production storage budget; native I/O remains genuinely held. + * + * By default a registered budget still fires after its real interval. With + * `manual`, it fires only through `expire()` or `captureLatest()`. That suits a + * test whose claim is not the budget, but whose held writes take a + * load-dependent time to settle, so a slow host cannot expire them (#3561). + */ +export function storageDeadlineClock(options: { manual?: boolean } = {}) { const timeout = Object.getOwnPropertyDescriptor(globalThis, "setTimeout")!; const clear = Object.getOwnPropertyDescriptor(globalThis, "clearTimeout")!; const nativeSet = globalThis.setTimeout, nativeClear = globalThis.clearTimeout; const pending = new Map, () => void>(); + let registered = 0; Object.defineProperty(globalThis, "setTimeout", { ...timeout, value: (callback: (...args: unknown[]) => void, ms?: number, ...args: unknown[]) => { if (ms !== STORAGE_SETTLEMENT_MS) return nativeSet(callback, ms, ...args); - const timer = nativeSet(() => { pending.delete(timer); callback(...args); }, ms); + registered++; + // A manual budget keeps a real handle, so clearTimeout behaves natively, but its expiry does nothing. + const timer = options.manual ? nativeSet(() => {}, ms) : nativeSet(() => { pending.delete(timer); callback(...args); }, ms); pending.set(timer, () => callback(...args)); return timer; } }); Object.defineProperty(globalThis, "clearTimeout", { ...clear, value: (timer: ReturnType) => { pending.delete(timer); nativeClear(timer); } }); return { get pending() { return pending.size; }, + /** Every production budget armed since installation, settled or not. */ + get registered() { return registered; }, /** Capture one concrete deadline; cancellation must not expire a different timer. */ captureLatest() { const timer = [...pending.keys()].at(-1);