From 2bdc80eedc0e1cf6f9abe7fc16deb9ecf4532758 Mon Sep 17 00:00:00 2001 From: can1357 Date: Wed, 8 Apr 2026 12:52:32 +0200 Subject: [PATCH] fix: AsyncDrain double-allocation bug and history test singleton isolation --- packages/coding-agent/src/session/history-storage.ts | 8 ++++++-- .../test/history-storage-sqlite-compat.test.ts | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/coding-agent/src/session/history-storage.ts b/packages/coding-agent/src/session/history-storage.ts index 037270906..f1e9964ed 100644 --- a/packages/coding-agent/src/session/history-storage.ts +++ b/packages/coding-agent/src/session/history-storage.ts @@ -28,8 +28,7 @@ class AsyncDrain { push(value: T, hnd: (values: T[]) => Promise | void): Promise { let queue = this.#queue; if (!queue) { - queue = []; - this.#queue = []; + this.#queue = queue = []; this.#promise = new Promise((resolve, reject) => { const exec = () => { try { @@ -128,6 +127,11 @@ CREATE TRIGGER IF NOT EXISTS history_ai AFTER INSERT ON history BEGIN return HistoryStorage.#instance; } + /** @internal Reset the singleton — test-only. */ + static resetInstance(): void { + HistoryStorage.#instance = undefined; + } + #insertBatch(rows: Array>): void { this.#db.transaction((rows: Array>) => { for (const row of rows) { diff --git a/packages/coding-agent/test/history-storage-sqlite-compat.test.ts b/packages/coding-agent/test/history-storage-sqlite-compat.test.ts index 076dbc24c..01c4e956a 100644 --- a/packages/coding-agent/test/history-storage-sqlite-compat.test.ts +++ b/packages/coding-agent/test/history-storage-sqlite-compat.test.ts @@ -1,5 +1,5 @@ import { Database } from "bun:sqlite"; -import { afterEach, expect, it } from "bun:test"; +import { afterEach, beforeEach, expect, it } from "bun:test"; import * as fs from "node:fs/promises"; import * as os from "node:os"; import * as path from "node:path"; @@ -21,7 +21,12 @@ function readTableSql(dbPath: string, tableName: string): string | null { let tempDir = ""; +beforeEach(() => { + HistoryStorage.resetInstance(); +}); + afterEach(async () => { + HistoryStorage.resetInstance(); if (tempDir) { await fs.rm(tempDir, { recursive: true, force: true }); tempDir = "";