From 242635d0b75234785ee1b9382e78c3fc4c69f7ed Mon Sep 17 00:00:00 2001 From: trmosala Date: Sun, 8 Nov 2026 13:04:36 +0200 Subject: [PATCH] fix(cm): stop V1 history migration crashing desktop startup The desktop sidecar runs the published Bun-only V1 migration under Electron's Node, where its synthetic-ID hashing called Bun.CryptoHasher and threw "Bun is not defined" for real histories, killing the sidecar before the window could load. Swap that one call to node:crypto inside the migration module only, and guard that it stays the sole Bun API. Co-Authored-By: Claude Opus 5.5 --- packages/cm-v2/src/runtime.ts | 11 +++++++++++ packages/cm-v2/src/v1-migration-shim.test.ts | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 packages/cm-v2/src/v1-migration-shim.test.ts diff --git a/packages/cm-v2/src/runtime.ts b/packages/cm-v2/src/runtime.ts index a79c35a4ed..482408d561 100644 --- a/packages/cm-v2/src/runtime.ts +++ b/packages/cm-v2/src/runtime.ts @@ -25,6 +25,17 @@ export async function listen(input: import("./server").ListenOptions) { return nextResolve(specifier, { ...context, parentURL: import.meta.url }) return nextResolve(specifier, context) }, + load(url, context, nextLoad) { + const result = nextLoad(url, context) + if (!url.endsWith("/database/v1-migration.bun.js")) return result + // shortcut: the Bun migration's only Bun API is CryptoHasher for synthetic IDs; drop when 2.2.x stops needing Bun. + const source = typeof result.source === "string" ? result.source : new TextDecoder().decode(result.source) + return { + ...result, + source: `import { createHash as __cmCreateHash } from "node:crypto";\n${source.replaceAll("new Bun.CryptoHasher(", "__cmCreateHash(")}`, + shortCircuit: true, + } + }, }) installed = true } diff --git a/packages/cm-v2/src/v1-migration-shim.test.ts b/packages/cm-v2/src/v1-migration-shim.test.ts new file mode 100644 index 0000000000..987cbc10ef --- /dev/null +++ b/packages/cm-v2/src/v1-migration-shim.test.ts @@ -0,0 +1,11 @@ +import { expect, test } from "bun:test" +import { readFileSync } from "node:fs" +import { fileURLToPath } from "node:url" + +// runtime.ts runs the Bun-only V1 migration under Electron's Node by swapping its one Bun API. +test("the published V1 migration needs only the CryptoHasher shim under Node", () => { + const file = fileURLToPath(import.meta.resolve("@opencode/core/database/v1-migration.bun")) + const source = readFileSync(file, "utf8") + expect(source).toContain("new Bun.CryptoHasher(") + expect(source.replaceAll("new Bun.CryptoHasher(", "").match(/\bBun\./g)).toBeNull() +})