From a9f16031facdadab0780d8608784293ac0ff6edc Mon Sep 17 00:00:00 2001 From: "zhou.han" Date: Fri, 13 Nov 2026 11:40:05 +0800 Subject: [PATCH] kiro: finalize kiro-cli's SQLite statements so Windows can remove the file (#1067) kiro-cli's SQLite statements are finalized in readCLI and saveBack, and in the tests' fixtures, so Windows can remove the database between tests (EBUSY, #966). Run at 85bb47ff: packages/kiro on Windows 11 (Bun 1.4.2, temp HOME) went from 8 reds on main to 2; Linux (WSL Ubuntu 24.04) 183 pass, 0 fail; `bun scripts/check.mjs kiro` ok. The 2 left are the tests writing kiro-cli's database under ~/.local/share while the plugin reads %APPDATA% on Windows; fixed in the next commit. Fixes #966 (with the next commit). --- packages/kiro/accounts.test.mjs | 9 +++++++-- packages/kiro/builderid.test.mjs | 12 ++++++++++-- packages/kiro/index.mjs | 24 +++++++++++++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/packages/kiro/accounts.test.mjs b/packages/kiro/accounts.test.mjs index c80f038..5c57844 100644 --- a/packages/kiro/accounts.test.mjs +++ b/packages/kiro/accounts.test.mjs @@ -179,8 +179,13 @@ test("kiro-cli's sign-in, with no way known, keeps the bare email", async () => mkdirSync(cliDir(), { recursive: true }) const db = new Database(join(cliDir(), "data.sqlite3"), { create: true }) db.exec("CREATE TABLE auth_kv (key TEXT PRIMARY KEY, value TEXT)") - db.prepare("INSERT INTO auth_kv VALUES (?, ?)").run("kirocli:social:token", JSON.stringify({ access_token: "t", refresh_token: "r", - expires_at: new Date(Date.now() + 3600_000).toISOString(), region: "us-east-1", profile_arn: PROFILE })) + const stmt = db.prepare("INSERT INTO auth_kv VALUES (?, ?)") + try { + stmt.run("kirocli:social:token", JSON.stringify({ access_token: "t", refresh_token: "r", + expires_at: new Date(Date.now() + 3600_000).toISOString(), region: "us-east-1", profile_arn: PROFILE })) + } finally { + stmt.finalize() + } db.close() globalThis.fetch = async () => Response.json({ subscriptionInfo: { subscriptionTitle: "KIRO FREE" }, userInfo: { email: EMAIL } }) const saves = [] diff --git a/packages/kiro/builderid.test.mjs b/packages/kiro/builderid.test.mjs index b9c49b1..71969b9 100644 --- a/packages/kiro/builderid.test.mjs +++ b/packages/kiro/builderid.test.mjs @@ -122,8 +122,16 @@ test("kiro-cli's Builder ID sign-in (its start URL, or none) is asked with Build db.exec("CREATE TABLE auth_kv (key TEXT PRIMARY KEY, value TEXT)") const token = { access_token: "t", refresh_token: "r", expires_at: new Date(later()).toISOString(), region: "us-east-1", ...(start ? { start_url: start } : {}) } - db.prepare("INSERT INTO auth_kv VALUES (?, ?)").run("kirocli:odic:token", JSON.stringify(token)) - db.prepare("INSERT INTO auth_kv VALUES (?, ?)").run("kirocli:odic:device-registration", JSON.stringify({ client_id: "c", client_secret: "s" })) + const insert = (key, value) => { + const stmt = db.prepare("INSERT INTO auth_kv VALUES (?, ?)") + try { + stmt.run(key, value) + } finally { + stmt.finalize() + } + } + insert("kirocli:odic:token", JSON.stringify(token)) + insert("kirocli:odic:device-registration", JSON.stringify({ client_id: "c", client_secret: "s" })) db.close() const seen = kiro() const h = await hooks() diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index fa3110f..7405035 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -145,7 +145,14 @@ async function readCLI() { try { db = await openDB(path, true) const get = (key) => { - const row = db.prepare("SELECT value FROM auth_kv WHERE key = ?").get(key) + // Bun's SQLite keeps the file locked until a statement is finalized. + const stmt = db.prepare("SELECT value FROM auth_kv WHERE key = ?") + let row + try { + row = stmt.get(key) + } finally { + stmt.finalize?.() + } if (!row?.value) return null try { return JSON.parse(row.value) @@ -212,12 +219,23 @@ async function saveBack(c) { try { db = await openDB(cliDB(), false) db.exec("PRAGMA busy_timeout = 3000") - const row = db.prepare("SELECT value FROM auth_kv WHERE key = ?").get(c.dbKey) + const select = db.prepare("SELECT value FROM auth_kv WHERE key = ?") + let row + try { + row = select.get(c.dbKey) + } finally { + select.finalize?.() + } if (!row?.value) return const m = JSON.parse(row.value) Object.assign(m, { access_token: c.access, refresh_token: c.refresh, expires_at: expires }) if (c.profile && c.method === "social") m.profile_arn = c.profile - db.prepare("UPDATE auth_kv SET value = ? WHERE key = ?").run(JSON.stringify(m), c.dbKey) + const update = db.prepare("UPDATE auth_kv SET value = ? WHERE key = ?") + try { + update.run(JSON.stringify(m), c.dbKey) + } finally { + update.finalize?.() + } } catch { } finally { db?.close()