fix(ultrawork): handle bun:sqlite import failure directly
Use the lazy bun:sqlite importer inside the deferred override microtask and keep the unavailable-runtime test on the rejected-import path. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test"
|
||||
import * as sharedModule from "../shared"
|
||||
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
|
||||
import * as loggerModule from "../shared/logger"
|
||||
import {
|
||||
scheduleDeferredModelOverride,
|
||||
__setBunSqliteImporterForTesting,
|
||||
__resetBunSqliteImporterForTesting,
|
||||
__setBunSqliteImporterForTesting,
|
||||
} from "./ultrawork-db-model-override"
|
||||
|
||||
function flushMicrotasks(depth: number): Promise<void> {
|
||||
@@ -25,22 +25,22 @@ describe("scheduleDeferredModelOverride bun:sqlite unavailable", () => {
|
||||
let logCalls: Array<[string, Record<string, unknown>?]> = []
|
||||
|
||||
beforeEach(() => {
|
||||
// Simulate non-Bun runtime (Node/Electron): bun:sqlite import returns null
|
||||
__setBunSqliteImporterForTesting(async () => null)
|
||||
|
||||
spyOn(sharedModule, "log").mockImplementation((message: string, metadata?: Record<string, unknown>) => {
|
||||
spyOn(loggerModule, "log").mockImplementation((message: string, metadata?: Record<string, unknown>) => {
|
||||
logCalls.push([message, metadata])
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
__resetBunSqliteImporterForTesting()
|
||||
mock.restore()
|
||||
logCalls = []
|
||||
})
|
||||
|
||||
test("#given non-Bun runtime #when scheduleDeferredModelOverride is called #then it returns without throwing", async () => {
|
||||
test("#given bun:sqlite import fails #when scheduleDeferredModelOverride is called #then it returns without throwing", async () => {
|
||||
//#given
|
||||
__setBunSqliteImporterForTesting(async () => {
|
||||
throw new Error("bun:sqlite unavailable")
|
||||
})
|
||||
|
||||
//#when
|
||||
expect(() => {
|
||||
scheduleDeferredModelOverride("msg_unavailable", {
|
||||
@@ -52,9 +52,9 @@ describe("scheduleDeferredModelOverride bun:sqlite unavailable", () => {
|
||||
await flushMicrotasks(5)
|
||||
|
||||
//#then
|
||||
const logMessages = logCalls.map(([msg]) => msg)
|
||||
expect(logMessages).toContain(
|
||||
"[ultrawork-db-override] bun:sqlite unavailable (non-Bun runtime), skipping deferred override",
|
||||
)
|
||||
expect(logCalls).toContainEqual([
|
||||
"[ultrawork-db-override] bun:sqlite unavailable (non-Bun runtime), skipping",
|
||||
undefined,
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,20 +6,16 @@ import { log } from "../shared"
|
||||
type BunDatabase = import("bun:sqlite").Database
|
||||
type SqliteModule = { Database: new (path: string) => BunDatabase }
|
||||
|
||||
/** @internal test-only seam: override to simulate non-Bun runtime */
|
||||
let _bunSqliteImporter: () => Promise<SqliteModule | null> = () =>
|
||||
import("bun:sqlite").catch(() => null) as Promise<SqliteModule | null>
|
||||
let bunSqliteImporter: () => Promise<SqliteModule> = () => import("bun:sqlite")
|
||||
|
||||
/** @internal test-only */
|
||||
export function __setBunSqliteImporterForTesting(
|
||||
impl: () => Promise<SqliteModule | null>,
|
||||
importer: () => Promise<SqliteModule>,
|
||||
): void {
|
||||
_bunSqliteImporter = impl
|
||||
bunSqliteImporter = importer
|
||||
}
|
||||
|
||||
/** @internal test-only */
|
||||
export function __resetBunSqliteImporterForTesting(): void {
|
||||
_bunSqliteImporter = () => import("bun:sqlite").catch(() => null) as Promise<SqliteModule | null>
|
||||
bunSqliteImporter = () => import("bun:sqlite")
|
||||
}
|
||||
|
||||
function getDbPath(): string {
|
||||
@@ -124,7 +120,6 @@ function retryViaMicrotask(
|
||||
* Session.updateMessage() to save the message first, then overwrites the model.
|
||||
*
|
||||
* Falls back to setTimeout(fn, 0) after 10 microtask attempts.
|
||||
*
|
||||
*/
|
||||
export function scheduleDeferredModelOverride(
|
||||
messageId: string,
|
||||
@@ -132,16 +127,14 @@ export function scheduleDeferredModelOverride(
|
||||
variant?: string,
|
||||
): void {
|
||||
queueMicrotask(async () => {
|
||||
// Lazy-load bun:sqlite so this module can be imported under Node/Electron
|
||||
// without crashing the ESM loader (bun: protocol is Bun-only).
|
||||
const sqliteModule = await _bunSqliteImporter()
|
||||
if (sqliteModule === null) {
|
||||
log("[ultrawork-db-override] bun:sqlite unavailable (non-Bun runtime), skipping deferred override")
|
||||
let DatabaseCtor: (new (path: string) => import("bun:sqlite").Database) | undefined
|
||||
try {
|
||||
DatabaseCtor = (await bunSqliteImporter()).Database
|
||||
} catch {
|
||||
log("[ultrawork-db-override] bun:sqlite unavailable (non-Bun runtime), skipping")
|
||||
return
|
||||
}
|
||||
|
||||
const { Database } = sqliteModule
|
||||
|
||||
const dbPath = getDbPath()
|
||||
if (!existsSync(dbPath)) {
|
||||
log("[ultrawork-db-override] DB not found, skipping deferred override")
|
||||
@@ -150,7 +143,7 @@ export function scheduleDeferredModelOverride(
|
||||
|
||||
let db: BunDatabase
|
||||
try {
|
||||
db = new Database(dbPath)
|
||||
db = new DatabaseCtor(dbPath)
|
||||
} catch (error) {
|
||||
log("[ultrawork-db-override] Failed to open DB, skipping deferred override", {
|
||||
messageId,
|
||||
|
||||
Reference in New Issue
Block a user