From 45cf11bb7368c96ab494030255e45308f881894a Mon Sep 17 00:00:00 2001 From: Claudia Date: Tue, 17 Feb 2026 16:20:05 +0100 Subject: [PATCH] fix: debounce race condition in tests, .catch() on LLM batch - Added FactStore.flush() for immediate write (no debounce) - beforeEach flushes pending writes before cleanup - 3/3 consecutive runs: 83/83 pass, 0 fail - Added .catch() on processLlmBatchWhenReady() in hooks.ts (H1) --- src/fact-store.ts | 10 ++++++++++ src/hooks.ts | 3 ++- test/fact-store.test.ts | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/fact-store.ts b/src/fact-store.ts index 61d2799..e80e5ed 100644 --- a/src/fact-store.ts +++ b/src/fact-store.ts @@ -33,6 +33,16 @@ export class FactStore { ); } + /** + * Immediately flushes any pending debounced writes. + * Useful in tests and before shutdown to ensure data is persisted. + */ + public async flush(): Promise { + if (this.isLoaded) { + await this.persist(); + } + } + /** * Loads facts from the `facts.json` file into the in-memory store. * If the file doesn't exist, it initializes an empty store. diff --git a/src/hooks.ts b/src/hooks.ts index 42213d4..f614772 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -98,7 +98,8 @@ export class HookManager { if (this.llmEnhancer) { const messageId = `msg-${Date.now()}`; this.llmEnhancer.addToBatch({ id: messageId, text }); - this.processLlmBatchWhenReady(); + this.processLlmBatchWhenReady().catch(err => + this.logger.error('LLM batch processing failed', err as Error)); } } diff --git a/test/fact-store.test.ts b/test/fact-store.test.ts index 2d08449..131a45b 100644 --- a/test/fact-store.test.ts +++ b/test/fact-store.test.ts @@ -23,6 +23,11 @@ describe('FactStore', () => { after(async () => await fs.rm(testDir, { recursive: true, force: true })); beforeEach(async () => { + // Flush any pending debounced writes from the previous test + // to prevent stale data from bleeding across test boundaries. + if (factStore) { + await factStore.flush(); + } const filePath = path.join(testDir, 'facts.json'); try { await fs.unlink(filePath); } catch (e: unknown) { if ((e as NodeJS.ErrnoException).code !== 'ENOENT') throw e;