Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(modules): derive host key from target triple instead of runner

Steven Enamakel17b312084f3c
diff --git a/.github/workflows/build-desktop.yml b/.github/workflows/build-desktop.yml
index 2fa80c4378..19bbdadc9f 100644
--- a/.github/workflows/build-desktop.yml
+++ b/.github/workflows/build-desktop.yml
@@ -368,10 +368,11 @@ jobs:
# ---- Tauri build -------------------------------------------------------
# Downloaded and digest-verified here, once, so no installer fetches a
- # module at runtime. The host key follows the runner's OS/arch.
+ # module at runtime. The host key follows the build target, not the
+ # runner (the x86_64 macOS job may run on an arm64 runner).
- name: Bundle pinned native modules
shell: bash
- run: node scripts/release/stage-modules.mjs
+ run: node scripts/release/stage-modules.mjs --target "${{ matrix.settings.target }}"
# Vite is invoked via tauri.conf.json's beforeBuildCommand inside
# `cargo tauri build`, so all VITE_* env must be present here for the
diff --git a/.github/workflows/module-bundle.yml b/.github/workflows/module-bundle.yml
index 2588d20843..b98cf5671e 100644
--- a/.github/workflows/module-bundle.yml
+++ b/.github/workflows/module-bundle.yml
@@ -4,7 +4,7 @@ on:
pull_request:
paths:
- ".github/workflows/build-desktop.yml"
- - ".github/workflows/windows-module-bundle.yml"
+ - ".github/workflows/module-bundle.yml"
- "crates/openhuman-app/tauri.conf.json"
- "crates/openhuman-core/src/modules/**"
- "scripts/ci/self-hosted/test-module-assets.mjs"
@@ -21,7 +21,13 @@ jobs:
strategy:
fail-fast: false
matrix:
- os: [windows-latest, ubuntu-22.04, macos-latest]
+ include:
+ - { os: windows-latest, target: x86_64-pc-windows-msvc }
+ - { os: windows-latest, target: aarch64-pc-windows-msvc }
+ - { os: ubuntu-22.04, target: x86_64-unknown-linux-gnu }
+ - { os: ubuntu-22.04, target: aarch64-unknown-linux-gnu }
+ - { os: macos-latest, target: aarch64-apple-darwin }
+ - { os: macos-latest, target: x86_64-apple-darwin }
runs-on: ${{ matrix.os }}
timeout-minutes: 30
steps:
@@ -32,4 +38,4 @@ jobs:
- name: Check registry coverage
run: node --test scripts/__tests__/stage-modules.test.mjs
- name: Download and verify every bundled release
- run: node scripts/release/stage-modules.mjs
+ run: node scripts/release/stage-modules.mjs --target ${{ matrix.target }}
diff --git a/crates/openhuman-core/src/modules/ops.rs b/crates/openhuman-core/src/modules/ops.rs
index 9c2db98297..b3997d13bf 100644
--- a/crates/openhuman-core/src/modules/ops.rs
+++ b/crates/openhuman-core/src/modules/ops.rs
@@ -79,10 +79,12 @@ pub(crate) fn resolve_bundled_dir(
from_env: Option<PathBuf>,
exe_dir: Option<PathBuf>,
) -> Option<PathBuf> {
- let found = registered
- .or(from_env)
- .or_else(|| exe_dir.map(|dir| dir.join(BUNDLED_MODULES_DIR)))
- .filter(|dir| dir.is_dir());
+ // Each candidate must be a directory to win: a stale registered path or a
+ // mistyped env var must not hide a valid directory further down the list.
+ let found = [registered, from_env, exe_dir.map(|dir| dir.join(BUNDLED_MODULES_DIR))]
+ .into_iter()
+ .flatten()
+ .find(|dir| dir.is_dir());
if let Some(dir) = &found {
log::debug!("[modules] bundled release directory: {}", dir.display());
}
diff --git a/crates/openhuman-core/src/modules/ops_tests.rs b/crates/openhuman-core/src/modules/ops_tests.rs
index a0d2c481b7..50138cb89c 100644
--- a/crates/openhuman-core/src/modules/ops_tests.rs
+++ b/crates/openhuman-core/src/modules/ops_tests.rs
@@ -383,3 +383,18 @@ fn bundled_dir_ignores_paths_that_do_not_exist() {
);
assert_eq!(ops::resolve_bundled_dir(None, None, Some(root.path().into())), None);
}
+
+#[test]
+fn bundled_dir_skips_a_missing_candidate_for_a_valid_later_one() {
+ let root = tempfile::tempdir().unwrap();
+ let exe_dir = root.path().join("bin");
+ std::fs::create_dir_all(exe_dir.join("bundled-modules")).unwrap();
+ assert_eq!(
+ ops::resolve_bundled_dir(
+ Some(root.path().join("stale")),
+ Some(root.path().join("typo")),
+ Some(exe_dir.clone())
+ ),
+ Some(exe_dir.join("bundled-modules"))
+ );
+}
diff --git a/scripts/__tests__/stage-modules.test.mjs b/scripts/__tests__/stage-modules.test.mjs
index 5b21fad1ee..8abf206fad 100644
--- a/scripts/__tests__/stage-modules.test.mjs
+++ b/scripts/__tests__/stage-modules.test.mjs
@@ -2,6 +2,8 @@ import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
+import { createServer } from "node:http";
+import { gzipSync } from "node:zlib";
import { test } from "node:test";
import { readRegistrySource } from "../ci/self-hosted/test-module-assets.mjs";
@@ -9,6 +11,8 @@ import { parseAllList } from "../lib/module-pins.mjs";
import {
bundledAssets,
defaultHostKey,
+ download,
+ hostKeyForTarget,
extractWindowsZip,
} from "../release/stage-modules.mjs";
@@ -91,3 +95,55 @@ test("Windows extraction rejects an archive entry escaping its destination", {
);
assert.throws(() => readFileSync(join(root, "escape.txt")), { code: "ENOENT" });
});
+
+test("host keys follow the Rust target triple, not the runner", () => {
+ assert.equal(hostKeyForTarget("aarch64-apple-darwin"), "macos-15-arm64");
+ assert.equal(hostKeyForTarget("x86_64-apple-darwin"), "macos-15-x86_64");
+ assert.equal(hostKeyForTarget("x86_64-unknown-linux-gnu"), "ubuntu-22.04-x86_64");
+ assert.equal(hostKeyForTarget("aarch64-unknown-linux-gnu"), "ubuntu-22.04-arm64");
+ assert.equal(hostKeyForTarget("x86_64-pc-windows-msvc"), "windows-2022-x86_64");
+ assert.equal(hostKeyForTarget("aarch64-pc-windows-msvc"), "windows-11-arm64");
+ assert.throws(() => hostKeyForTarget("wasm32-unknown-unknown"), /no bundled modules/);
+});
+
+async function withServer(handler, run) {
+ const server = createServer(handler);
+ await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
+ try {
+ await run(`http://127.0.0.1:${server.address().port}/a`);
+ } finally {
+ server.closeAllConnections();
+ await new Promise((resolve) => server.close(resolve));
+ }
+}
+
+test("download keeps the archive bytes and refuses a content-encoded body", async () => {
+ const dir = mkdtempSync(join(tmpdir(), "openhuman-download-"));
+ const out = join(dir, "a.bin");
+ const raw = Buffer.from("raw archive bytes");
+ await withServer((req, res) => res.end(raw), async (url) => {
+ await download(url, out, 5000);
+ assert.deepEqual(readFileSync(out), raw);
+ });
+ await withServer(
+ (req, res) => {
+ res.setHeader("content-encoding", "gzip");
+ res.end(gzipSync(raw));
+ },
+ async (url) => {
+ await assert.rejects(download(url, out, 5000), /Content-Encoding gzip/);
+ },
+ );
+});
+
+test("a stalled download times out instead of hanging", async () => {
+ const dir = mkdtempSync(join(tmpdir(), "openhuman-download-"));
+ await withServer(
+ (req, res) => {
+ res.write("partial");
+ },
+ async (url) => {
+ await assert.rejects(download(url, join(dir, "a.bin"), 200), /download of/);
+ },
+ );
+});
diff --git a/scripts/release/package-cli-tarball.sh b/scripts/release/package-cli-tarball.sh
index fea4a2f506..dfecc8ba95 100755
--- a/scripts/release/package-cli-tarball.sh
+++ b/scripts/release/package-cli-tarball.sh
@@ -28,9 +28,9 @@ cp "$CORE_BIN_PATH" "$WORK/openhuman-core"
cp "$TUI_BIN_PATH" "$WORK/openhuman-tui"
chmod +x "$WORK/openhuman-core" "$WORK/openhuman-tui"
# Pinned native modules, staged at build time and found by the core beside its
-# binary; the target's host key comes from the runner, so build natively.
+# binary; the host key follows the tarball's target triple.
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
-node "$REPO_ROOT/scripts/release/stage-modules.mjs" --output "$WORK/bundled-modules"
+node "$REPO_ROOT/scripts/release/stage-modules.mjs" --target "$TARGET" --output "$WORK/bundled-modules"
tar -czf "$TARBALL" -C "$WORK" openhuman-core openhuman-tui bundled-modules
# openssl dgst works on both macOS and Linux
diff --git a/scripts/release/stage-modules.mjs b/scripts/release/stage-modules.mjs
index c8a366e9e1..8e56775136 100644
--- a/scripts/release/stage-modules.mjs
+++ b/scripts/release/stage-modules.mjs
@@ -6,7 +6,10 @@
// extracted library, which is what `modules::ops::set_bundled_releases_dir`
// (or `OPENHUMAN_BUNDLED_MODULES`) points at.
//
-// Usage: node scripts/release/stage-modules.mjs [--host-key KEY] [--output DIR]
+// Usage: node scripts/release/stage-modules.mjs [--target TRIPLE | --host-key KEY] [--output DIR]
+//
+// `--target` takes the Rust target triple being built, so a cross-built app
+// (x86_64 macOS on an arm64 runner) gets modules for the target, not the runner.
//
// One key per OS/arch is staged: the oldest published build, because tinybus
// tries every candidate for the host in order and skips any that is not
@@ -50,6 +53,19 @@ export function defaultHostKey(platform = process.platform, arch = process.arch)
}
}
+/** The registry host key to bundle for a Rust target triple. */
+export function hostKeyForTarget(triple) {
+ const arch = triple.split("-")[0];
+ const archKey = { x86_64: "x86_64", aarch64: "arm64" }[arch];
+ if (!archKey) throw new Error(`no bundled modules for target ${triple}`);
+ if (triple.includes("apple-darwin")) return `macos-15-${archKey}`;
+ if (triple.includes("linux")) return `ubuntu-22.04-${archKey}`;
+ if (triple.includes("windows")) {
+ return archKey === "arm64" ? "windows-11-arm64" : "windows-2022-x86_64";
+ }
+ throw new Error(`no bundled modules for target ${triple}`);
+}
+
export function bundledAssets(source, hostKey) {
const names = parseAllList(source);
const records = parseRecords(source);
@@ -123,12 +139,28 @@ export function extractArchive(archive, dir) {
execFileSync("tar", ["-xzf", archive, "-C", dir], { stdio: "pipe" });
}
-async function download(url, destination) {
+const DOWNLOAD_TIMEOUT_MS = 120_000;
+
+/**
+ * Fetch `url` to `destination` byte-for-byte. Identity encoding is requested
+ * and anything else is refused: `fetch` would transparently decode a
+ * `Content-Encoding: gzip` body, and the registry pin covers the archive's
+ * own bytes. Each attempt has a deadline that also covers the body.
+ */
+export async function download(url, destination, timeoutMs = DOWNLOAD_TIMEOUT_MS) {
let lastError;
for (let attempt = 1; attempt <= 3; attempt += 1) {
try {
- const response = await fetch(url, { redirect: "follow" });
+ const response = await fetch(url, {
+ redirect: "follow",
+ headers: { "accept-encoding": "identity" },
+ signal: AbortSignal.timeout(timeoutMs),
+ });
if (!response.ok) throw new Error(`HTTP ${response.status}`);
+ const encoding = response.headers.get("content-encoding");
+ if (encoding && encoding.toLowerCase() !== "identity") {
+ throw new Error(`unexpected Content-Encoding ${encoding}`);
+ }
writeFileSync(destination, Buffer.from(await response.arrayBuffer()));
return;
} catch (error) {
@@ -168,8 +200,9 @@ function option(name) {
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
+ const target = option("--target");
await stageModules({
- hostKey: option("--host-key") ?? defaultHostKey(),
+ hostKey: option("--host-key") ?? (target ? hostKeyForTarget(target) : defaultHostKey()),
output: option("--output") ? resolve(option("--output")) : OUTPUT,
});
}