Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: one engine hash per architecture

ogsamratae1802b493e2
diff --git a/apps/desktop/electron/build-variant.mjs b/apps/desktop/electron/build-variant.mjs
index e1e82bd7..81ca191d 100644
--- a/apps/desktop/electron/build-variant.mjs
+++ b/apps/desktop/electron/build-variant.mjs
@@ -31,7 +31,8 @@ export function readDesktopBuild({
if (!packaged) return { variant: "dev", engineSha256: null, packaged: false };
try {
const value = JSON.parse(read(path.join(resourcesPath, "app.asar", "server", DESKTOP_BUILD_FILE)));
- const engineSha256 = typeof value?.engineSha256 === "string" && /^[0-9a-f]{64}$/.test(value.engineSha256) ? value.engineSha256 : null;
+ // One hash per bundled architecture, comma-separated (scripts/stamp-build-variant.mjs).
+ const engineSha256 = typeof value?.engineSha256 === "string" && /^[0-9a-f]{64}(?:,[0-9a-f]{64})*$/.test(value.engineSha256) ? value.engineSha256 : null;
return { variant: value?.variant === "dev" ? "dev" : "release", engineSha256, packaged: true };
} catch {
return { variant: "release", engineSha256: null, packaged: true };
@@ -72,6 +73,6 @@ export function releaseEngineBinary({ build = desktopBuild, resourcesPath = proc
const directory = resourcesPath ? path.join(resourcesPath, "sidecars") : null;
const candidate = directory ? fileNames.map((name) => path.join(directory, name)).find((file) => exists(file)) : null;
if (!candidate || !build.engineSha256) throw new Error(`${RELEASE_ENGINE_ERROR} No bundled engine was found. Reinstall OmniRush.ai.`);
- if (hash(candidate) !== build.engineSha256) throw new Error(`${RELEASE_ENGINE_ERROR} The bundled engine was changed. Reinstall OmniRush.ai.`);
+ if (!build.engineSha256.split(",").includes(hash(candidate))) throw new Error(`${RELEASE_ENGINE_ERROR} The bundled engine was changed. Reinstall OmniRush.ai.`);
return { path: candidate, source: "bundled" };
}
diff --git a/apps/desktop/electron/build-variant.test.mjs b/apps/desktop/electron/build-variant.test.mjs
index 4f503594..65854f9d 100644
--- a/apps/desktop/electron/build-variant.test.mjs
+++ b/apps/desktop/electron/build-variant.test.mjs
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
-import { mkdtemp, readFile, writeFile, mkdir } from "node:fs/promises";
+import { mkdtemp, readFile, writeFile, mkdir, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
@@ -72,6 +72,10 @@ test("release: only the bundled engine with the recorded sha256", async () => {
assert.deepEqual(releaseEngineBinary({ build, resourcesPath: directory, fileNames: ["opencode"] }), { path: path.join(directory, "sidecars", "opencode"), source: "bundled" });
assert.throws(() => releaseEngineBinary({ build: { ...build, engineSha256: "c".repeat(64) }, resourcesPath: directory, fileNames: ["opencode"] }), /was changed/);
assert.throws(() => releaseEngineBinary({ build, resourcesPath: directory, fileNames: ["missing"] }), /No bundled engine/);
+ // A macOS build records one hash per architecture; the binary present must be one of them.
+ const perArch = { ...build, engineSha256: ["d".repeat(64), sha].join(",") };
+ assert.equal(releaseEngineBinary({ build: perArch, resourcesPath: directory, fileNames: ["opencode"] }).source, "bundled");
+ assert.throws(() => releaseEngineBinary({ build: { ...build, engineSha256: ["d".repeat(64), "e".repeat(64)].join(",") }, resourcesPath: directory, fileNames: ["opencode"] }), /was changed/);
});
test("packaging stamps release with the engine hash, or dev when asked at build time", async () => {
@@ -92,6 +96,13 @@ test("packaging stamps release with the engine hash, or dev when asked at build
assert.deepEqual(stampBuildVariant({ env: { OMNIRUSH_DESKTOP_VARIANT: "dev" }, sidecarDir, serverDistDir, buildFilePath }), { variant: "dev", engineSha256: null });
assert.equal(await readFile(path.join(serverDistDir, "build-variant.js"), "utf8"), source);
assert.throws(() => desktopVariant({ OMNIRUSH_DESKTOP_VARIANT: "staging" }));
+ // macOS bundles one engine per architecture: both hashes are recorded, sorted.
await writeFile(path.join(sidecarDir, "opencode-x86_64-unknown-linux-gnu"), "other");
- assert.throws(() => stampBuildVariant({ env: {}, sidecarDir, serverDistDir, buildFilePath }), /one bundled engine/);
+ const other = createHash("sha256").update("other").digest("hex");
+ const both = [sha, other].sort().join(",");
+ assert.deepEqual(stampBuildVariant({ env: {}, sidecarDir, serverDistDir, buildFilePath }), { variant: "release", engineSha256: both });
+ assert.equal(await readFile(path.join(serverDistDir, "build-variant.js"), "utf8"), `export const BUILD_VARIANT = "release";\nexport const RELEASE_ENGINE_SHA256 = "${both}";\n`);
+ await rm(sidecarDir, { recursive: true, force: true });
+ await mkdir(sidecarDir);
+ assert.throws(() => stampBuildVariant({ env: {}, sidecarDir, serverDistDir, buildFilePath }), /one engine per architecture/);
});
diff --git a/apps/desktop/scripts/stamp-build-variant.mjs b/apps/desktop/scripts/stamp-build-variant.mjs
index 2e45e4fb..022d2b95 100644
--- a/apps/desktop/scripts/stamp-build-variant.mjs
+++ b/apps/desktop/scripts/stamp-build-variant.mjs
@@ -12,12 +12,16 @@ export function desktopVariant(env = process.env) {
return asked === "dev" ? "dev" : "release";
}
-/** The sha256 of the bundled engine; every opencode binary in the sidecar folder must be the same file. */
+/**
+ * The sha256 of each bundled engine binary, comma-separated in sorted order.
+ * A macOS build bundles one engine per architecture (arm64 and x64): each is
+ * recorded, and a release runs only a binary with one of these hashes.
+ */
export function bundledEngineSha256(sidecarDir) {
const names = existsSync(sidecarDir) ? readdirSync(sidecarDir).filter((name) => /^opencode(?:-[a-z0-9_-]+)?(?:\.exe)?$/.test(name)) : [];
- const hashes = [...new Set(names.map((name) => createHash("sha256").update(readFileSync(join(sidecarDir, name))).digest("hex")))];
- if (hashes.length !== 1) throw new Error(`Expected one bundled engine in ${sidecarDir}, found ${names.length} file(s) with ${hashes.length} different content(s)`);
- return hashes[0];
+ const hashes = [...new Set(names.map((name) => createHash("sha256").update(readFileSync(join(sidecarDir, name))).digest("hex")))].sort();
+ if (hashes.length < 1 || hashes.length > 4) throw new Error(`Expected one engine per architecture in ${sidecarDir}, found ${names.length} file(s) with ${hashes.length} different content(s)`);
+ return hashes.join(",");
}
/** Stamps the packaged server copy's build-variant.js (two lines, checked). */
@@ -25,7 +29,7 @@ export function stampServerVariant(serverDistDir, variant, engineSha256) {
const file = join(serverDistDir, "build-variant.js");
const source = readFileSync(file, "utf8");
const variantLine = /^export const BUILD_VARIANT = "(?:dev|release)";$/m;
- const hashLine = /^export const RELEASE_ENGINE_SHA256 = (?:null|"[0-9a-f]{64}");$/m;
+ const hashLine = /^export const RELEASE_ENGINE_SHA256 = (?:null|"[0-9a-f]{64}(?:,[0-9a-f]{64})*");$/m;
if (!variantLine.test(source) || !hashLine.test(source)) throw new Error(`${file} does not have the expected build variant lines`);
const stamped = source
.replace(variantLine, `export const BUILD_VARIANT = "${variant}";`)
diff --git a/apps/server/src/build-variant.ts b/apps/server/src/build-variant.ts
index 928a0eed..bf128da3 100644
--- a/apps/server/src/build-variant.ts
+++ b/apps/server/src/build-variant.ts
@@ -5,7 +5,7 @@
* The source and `pnpm build` output are the dev variant. The desktop
* packaging step (apps/desktop/scripts/electron-build.mjs) stamps its copy of
* the compiled server "release", with the sha256 of the engine binary it
- * bundles, unless that build was asked for the dev variant
+ * bundles (one per architecture, comma-separated, on macOS), unless that build was asked for the dev variant
* (OMNIRUSH_DESKTOP_VARIANT=dev when building). The two constants below are
* rewritten there line by line: keep each on one line.
*/
diff --git a/apps/server/src/release-guard.test.ts b/apps/server/src/release-guard.test.ts
index 97e75732..ec7bdd3d 100644
--- a/apps/server/src/release-guard.test.ts
+++ b/apps/server/src/release-guard.test.ts
@@ -50,6 +50,9 @@ describe("release build guard", () => {
const other = join(root, "other");
await writeFile(other, "#!/bin/sh\necho other\n");
await expect(releaseEngineBinary(other, release)).rejects.toThrow("is not the bundled one");
+ // One hash per bundled architecture (macOS arm64 + x64), comma-separated.
+ expect(await releaseEngineBinary(bin, { release: true, engineSha256: ["f".repeat(64), sha].join(",") })).toBe(bin);
+ await expect(releaseEngineBinary(other, { release: true, engineSha256: ["f".repeat(64), sha].join(",") })).rejects.toThrow("is not the bundled one");
// Bytes changed in place with the same size and time: still refused (hashed at every start, no cache).
const { mtime } = await stat(bin);
await writeFile(bin, "#!/bin/sh\necho EVILLL\n");
diff --git a/apps/server/src/release-guard.ts b/apps/server/src/release-guard.ts
index 78b22a41..e930590a 100644
--- a/apps/server/src/release-guard.ts
+++ b/apps/server/src/release-guard.ts
@@ -94,6 +94,7 @@ export async function releaseEngineBinary(bin: string | undefined | null, facts:
} catch {
throw new Error(`${RELEASE_ENGINE_ERROR} The bundled engine could not be read.`);
}
- if (actual !== facts.engineSha256) throw new Error(`${RELEASE_ENGINE_ERROR} The engine at ${path} is not the bundled one. Reinstall OmniRush.ai.`);
+ // One hash per bundled architecture, comma-separated (apps/desktop/scripts/stamp-build-variant.mjs).
+ if (!facts.engineSha256.split(",").includes(actual)) throw new Error(`${RELEASE_ENGINE_ERROR} The engine at ${path} is not the bundled one. Reinstall OmniRush.ai.`);
return path;
}