Files
buzz/desktop/scripts/ensure-acp-tools.sh
Matt Toohey a29a3da6fe feat(desktop): pin and stage bundled ACP bridge tools
Buzz desktop spawned the claude-agent-acp and codex-acp bridges from
whatever the user had installed globally — an unpinned `npm install -g`
surface with no integrity checking, which produced stale-bridge drift
(the deprecated @zed-industries/codex-acp 0.16.x gate) and
missing-tool failures. Bundle both bridges as app resources instead,
pinned per target:

- desktop/acp-tools.lock.json pins @agentclientprotocol/claude-agent-acp
  0.58.1 and @agentclientprotocol/codex-acp 1.1.2 (npm `latest` at time
  of commit) for the four supported targets, with integrity hashes and
  Block Artifactory tarballs for the package, its claude-agent-sdk /
  @openai/codex dependency, and the per-target native package.
- desktop/scripts/update-acp-tools-lock.mjs regenerates the lock from
  the registry's `latest` dist-tags, failing loudly on any unresolvable
  package — never silently pinning an older version. Ranged
  dependencies (codex-acp's ^0.144.0) resolve to the highest matching
  version when `npm view` returns an array.
- desktop/scripts/ensure-acp-tools.sh installs the locked tools into a
  shared dev cache (~/Library/Caches/buzz-dev/acp-tools), validates
  versions + integrity against the lock, and stamps staged binaries
  next to the shared bin dir so any lock change — including a revert —
  forces a re-stage; binaries no longer in the lock are pruned.
- desktop/scripts/prepare-acp-tools-resource.sh stages the vendored npm
  trees + node wrapper shims into desktop/src-tauri/resources/acp,
  writes the node-runtime.json manifest for the app's Node.js doctor
  check, and ad-hoc signs every nested Mach-O (file(1) scan — the codex
  package vendors rg, zsh, and codex-code-mode-host beyond the main
  CLIs) so Gatekeeper doesn't kill them in local builds.
- desktop/scripts/lib/acp-node-wrapper.sh is the single wrapper-shim
  generator shared by both scripts, so the dev-cache and bundled
  wrappers (and the Node major they enforce) cannot drift.
- Wiring: `just dev` / `just staging` stage the resources and export
  BUZZ_ACP_TOOLS_DIR; `just desktop-release-build` stages per target
  before `tauri build`; `just bump-acp-tools` reruns the lock updater;
  tauri.conf.json bundles resources/acp; staged artifacts are
  gitignored with a .gitkeep placeholder.

Runtime resolution of the staged dir follows in the next commit. The
sprout-releases internal pipeline will need the same staging step
before its `tauri build`; that lives in a separate repo.

Ports the build-time half of the Staged implementation in
block/builderbot#876 (branch commits 16b115a7 bundle feature, f5c6bba9
re-stage after lock revert, 288fa7eb shared node wrapper lib, adea4017
node-runtime manifest, 5124302a sign all nested Mach-Os, de6a9782
ranged-dependency updater fix), itself ported from squareup/berd
f07df1d2 + 1db993fb + 24d7518b + 07087303 + 737e33a5.

Verified: update-acp-tools-lock.mjs regenerated the lock against the
Block registry (byte-identical pins to the donor lock; both packages
confirmed at npm `latest`); fresh stage installs both tools and the
staged wrappers report 0.58.1 / 1.1.2; no-op re-run performs zero npm
installs; a stamp/lock mismatch forces a re-stage and stray binaries
are pruned; all five staged Mach-Os pass `codesign --verify`; cargo
check on desktop/src-tauri passes with the new resources entry.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
2026-07-15 13:38:21 +10:00

364 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
app_root="$(cd "$script_dir/.." && pwd)"
lock_file="${ACP_TOOLS_LOCK_FILE:-$app_root/acp-tools.lock.json}"
# shellcheck source=lib/acp-node-wrapper.sh
source "$script_dir/lib/acp-node-wrapper.sh"
usage() {
cat <<'USAGE'
Usage: desktop/scripts/ensure-acp-tools.sh [--target <target-triple>] [--print-bin-dir]
Installs the ACP bridge tools pinned in acp-tools.lock.json into the shared
Buzz dev cache. The lockfile is target-specific; only entries matching the
requested target are prepared. Each tool is installed as a vendored npm
package tree with a small executable wrapper, validated against the locked
versions and integrity hashes.
Environment variables:
ACP_TOOLS_LOCK_FILE lockfile path (default: desktop/acp-tools.lock.json)
ACP_TOOLS_CACHE_DIR cache dir override
USAGE
}
default_cache_root() {
if [[ -n "${XDG_CACHE_HOME:-}" ]]; then
printf '%s/buzz-dev/acp-tools\n' "$XDG_CACHE_HOME"
return
fi
case "$(uname -s)" in
Darwin) printf '%s/Library/Caches/buzz-dev/acp-tools\n' "$HOME" ;;
*) printf '%s/.cache/buzz-dev/acp-tools\n' "$HOME" ;;
esac
}
target=""
print_bin_dir=0
while [[ $# -gt 0 ]]; do
case "$1" in
--target)
target="${2:-}"
[[ -n "$target" ]] || { echo "--target requires a value" >&2; exit 1; }
shift 2
;;
--print-bin-dir)
print_bin_dir=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
if [[ -z "$target" ]]; then
target="$(rustc -vV | sed -n 's|host: ||p')"
fi
if [[ -z "$target" ]]; then
echo "Could not determine rust host target. Pass --target explicitly." >&2
exit 1
fi
cache_root="${ACP_TOOLS_CACHE_DIR:-$(default_cache_root)}"
bin_dir="$cache_root/bin/$target"
if [[ ! -f "$lock_file" ]]; then
echo "ACP tools lockfile not found: $lock_file" >&2
exit 1
fi
require_tool() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Required tool missing: $1" >&2
exit 1
fi
}
require_tool node
require_tool npm
lock_entries="$(node - "$lock_file" "$target" <<'NODE'
const fs = require("node:fs");
const [lockFile, target] = process.argv.slice(2);
const data = JSON.parse(fs.readFileSync(lockFile, "utf8"));
const entries = (data.tools ?? []).filter((tool) => tool.target === target);
function requireString(entry, field) {
if (typeof entry[field] !== "string" || entry[field].trim() === "") {
throw new Error(`Invalid ACP tool lock entry for ${entry.id ?? "(unknown)"}: missing ${field}`);
}
}
for (const entry of entries) {
if (entry.source !== "npm") {
throw new Error(`Invalid ACP tool lock entry for ${entry.id}: unsupported source ${entry.source}`);
}
for (const field of [
"id",
"binary",
"target",
"package",
"version",
"integrity",
"tarball",
"npmOs",
"npmCpu",
"dependencyPackage",
"dependencyVersion",
"dependencyIntegrity",
"dependencyTarball",
"nativePackage",
"nativePackageName",
"nativeVersion",
"nativeIntegrity",
"nativeTarball",
"nativeExecutable",
]) {
requireString(entry, field);
}
}
process.stdout.write(JSON.stringify(entries));
NODE
)"
entry_count="$(node -e 'process.stdout.write(String(JSON.parse(process.argv[1]).length))' "$lock_entries")"
mkdir -p "$bin_dir"
if [[ "$entry_count" == "0" ]]; then
find "$bin_dir" -type f -delete
if [[ "$print_bin_dir" == "1" ]]; then
printf '%s\n' "$bin_dir"
else
echo "No ACP tools locked for target $target."
fi
exit 0
fi
validate_npm_install() {
local install_dir="$1"
local package="$2"
local version="$3"
local integrity="$4"
local dependency_package="$5"
local dependency_version="$6"
local dependency_integrity="$7"
local native_package="$8"
local native_package_name="$9"
local native_version="${10}"
local native_integrity="${11}"
local native_executable="${12}"
local claude_code_version="${13}"
node - "$install_dir" "$package" "$version" "$integrity" "$dependency_package" "$dependency_version" "$dependency_integrity" "$native_package" "$native_package_name" "$native_version" "$native_integrity" "$native_executable" "$claude_code_version" <<'NODE'
const fs = require("node:fs");
const path = require("node:path");
const [
installDir,
packageName,
expectedVersion,
expectedIntegrity,
dependencyPackageName,
expectedDependencyVersion,
expectedDependencyIntegrity,
nativePackageName,
expectedNativePackageName,
expectedNativeVersion,
expectedNativeIntegrity,
nativeExecutable,
expectedClaudeCodeVersion,
] = process.argv.slice(2);
function packagePath(name, ...segments) {
return path.join(installDir, "node_modules", ...name.split("/"), ...segments);
}
function readJson(file) {
return JSON.parse(fs.readFileSync(file, "utf8"));
}
function assertEqual(actual, expected, label) {
if (actual !== expected) {
throw new Error(`${label}: expected ${expected}, got ${actual}`);
}
}
function packageLockEntry(lock, packageName) {
const suffix = `node_modules/${packageName}`;
const match = Object.entries(lock.packages ?? {}).find(([key]) => key === suffix || key.endsWith(`/${suffix}`));
if (!match) {
throw new Error(`package-lock entry not found for ${packageName}`);
}
return match[1];
}
const packageJson = readJson(packagePath(packageName, "package.json"));
assertEqual(packageJson.name, packageName, `${packageName} name`);
assertEqual(packageJson.version, expectedVersion, `${packageName} version`);
const lock = readJson(path.join(installDir, "package-lock.json"));
assertEqual(packageLockEntry(lock, packageName).integrity, expectedIntegrity, `${packageName} integrity`);
const dependencyPackageJson = readJson(packagePath(dependencyPackageName, "package.json"));
assertEqual(
dependencyPackageJson.name,
dependencyPackageName,
`${dependencyPackageName} name`,
);
assertEqual(
dependencyPackageJson.version,
expectedDependencyVersion,
`${dependencyPackageName} version`,
);
if (expectedClaudeCodeVersion && expectedClaudeCodeVersion !== "null") {
assertEqual(
dependencyPackageJson.claudeCodeVersion,
expectedClaudeCodeVersion,
`${dependencyPackageName} claudeCodeVersion`,
);
}
assertEqual(
packageLockEntry(lock, dependencyPackageName).integrity,
expectedDependencyIntegrity,
`${dependencyPackageName} integrity`,
);
const nativePackageJson = readJson(packagePath(nativePackageName, "package.json"));
assertEqual(
nativePackageJson.name,
expectedNativePackageName,
`${nativePackageName} package name`,
);
assertEqual(
nativePackageJson.version,
expectedNativeVersion,
`${nativePackageName} version`,
);
fs.accessSync(packagePath(nativePackageName, nativeExecutable), fs.constants.X_OK);
assertEqual(
packageLockEntry(lock, nativePackageName).integrity,
expectedNativeIntegrity,
`${nativePackageName} integrity`,
);
NODE
}
node -e '
const entries = JSON.parse(process.argv[1]);
for (const entry of entries) {
console.log([
entry.id,
entry.binary,
entry.package,
entry.version,
entry.integrity,
entry.tarball,
entry.npmOs,
entry.npmCpu,
entry.npmLibc ?? "",
entry.nodeEngine ?? ">=22",
entry.dependencyPackage,
entry.dependencyVersion,
entry.dependencyIntegrity,
entry.dependencyTarball,
entry.nativePackage,
entry.nativePackageName,
entry.nativeVersion,
entry.nativeIntegrity,
entry.nativeTarball,
entry.nativeExecutable,
entry.claudeCodeVersion ?? "",
].join("\x1f"));
}
' "$lock_entries" | while IFS=$'\x1f' read -r id binary package version integrity tarball npm_os npm_cpu npm_libc node_engine dependency_package dependency_version dependency_integrity dependency_tarball native_package native_package_name native_version native_integrity native_tarball native_executable claude_code_version; do
[[ -n "$id" ]] || continue
tool_dir="$cache_root/$target/$id/$version"
install_dir="$tool_dir/npm"
package_dir="$install_dir/node_modules/$package"
entrypoint="$package_dir/dist/index.js"
native_binary="$install_dir/node_modules/$native_package/$native_executable"
staged_bin="$bin_dir/$binary"
# The staged output is shared across lock versions, so its freshness stamp
# must live next to it, not in the per-version tool_dir: a per-version stamp
# stays self-consistent after a lock revert and would skip re-staging.
stamp="$staged_bin.stamp"
if [[ -x "$staged_bin" && -f "$stamp" && -f "$entrypoint" && -x "$native_binary" ]]; then
# shellcheck disable=SC1090
source "$stamp"
if [[ "${STAMP_PACKAGE:-}" == "$package" && "${STAMP_VERSION:-}" == "$version" && "${STAMP_INTEGRITY:-}" == "$integrity" && "${STAMP_DEPENDENCY_PACKAGE:-}" == "$dependency_package" && "${STAMP_DEPENDENCY_VERSION:-}" == "$dependency_version" && "${STAMP_DEPENDENCY_INTEGRITY:-}" == "$dependency_integrity" && "${STAMP_NATIVE_PACKAGE:-}" == "$native_package" && "${STAMP_NATIVE_PACKAGE_NAME:-}" == "$native_package_name" && "${STAMP_NATIVE_VERSION:-}" == "$native_version" && "${STAMP_NATIVE_INTEGRITY:-}" == "$native_integrity" && "${STAMP_NATIVE_EXECUTABLE:-}" == "$native_executable" ]]; then
continue
fi
fi
echo "Installing ACP tool $id $version from npm for $target..." >&2
rm -rf "$install_dir"
mkdir -p "$install_dir" "$bin_dir"
npm_args=(
install
--prefix "$install_dir"
--omit=dev
--include=optional
--ignore-scripts
--no-audit
--no-fund
--os "$npm_os"
--cpu "$npm_cpu"
)
if [[ -n "$npm_libc" ]]; then
npm_args+=(--libc "$npm_libc")
fi
npm_args+=("$package@$version")
npm "${npm_args[@]}" >&2
validate_npm_install "$install_dir" "$package" "$version" "$integrity" "$dependency_package" "$dependency_version" "$dependency_integrity" "$native_package" "$native_package_name" "$native_version" "$native_integrity" "$native_executable" "$claude_code_version"
write_node_wrapper "$staged_bin" "$entrypoint" "$node_engine"
{
printf 'STAMP_TARGET=%q\n' "$target"
printf 'STAMP_PACKAGE=%q\n' "$package"
printf 'STAMP_VERSION=%q\n' "$version"
printf 'STAMP_INTEGRITY=%q\n' "$integrity"
printf 'STAMP_TARBALL=%q\n' "$tarball"
printf 'STAMP_NPM_OS=%q\n' "$npm_os"
printf 'STAMP_NPM_CPU=%q\n' "$npm_cpu"
printf 'STAMP_NPM_LIBC=%q\n' "$npm_libc"
printf 'STAMP_NODE_ENGINE=%q\n' "$node_engine"
printf 'STAMP_DEPENDENCY_PACKAGE=%q\n' "$dependency_package"
printf 'STAMP_DEPENDENCY_VERSION=%q\n' "$dependency_version"
printf 'STAMP_DEPENDENCY_INTEGRITY=%q\n' "$dependency_integrity"
printf 'STAMP_DEPENDENCY_TARBALL=%q\n' "$dependency_tarball"
printf 'STAMP_CLAUDE_CODE_VERSION=%q\n' "$claude_code_version"
printf 'STAMP_NATIVE_PACKAGE=%q\n' "$native_package"
printf 'STAMP_NATIVE_PACKAGE_NAME=%q\n' "$native_package_name"
printf 'STAMP_NATIVE_VERSION=%q\n' "$native_version"
printf 'STAMP_NATIVE_INTEGRITY=%q\n' "$native_integrity"
printf 'STAMP_NATIVE_TARBALL=%q\n' "$native_tarball"
printf 'STAMP_NATIVE_EXECUTABLE=%q\n' "$native_executable"
printf 'STAMP_BINARY=%q\n' "$binary"
} > "$stamp"
done
# bin_dir is prepended to the agent spawn PATH and the desktop's command
# resolution sweep, so binaries (and stamps) for tools no longer in the lock
# must be pruned, not just left behind.
locked_binaries="$(node -e '
const entries = JSON.parse(process.argv[1]);
for (const entry of entries) console.log(entry.binary);
' "$lock_entries" | sort -u)"
find "$bin_dir" -type f -print0 | while IFS= read -r -d '' staged_file; do
name="$(basename "$staged_file")"
if ! printf '%s\n' "$locked_binaries" | grep -Fxq -- "${name%.stamp}"; then
rm -f -- "$staged_file"
fi
done
if [[ "$print_bin_dir" == "1" ]]; then
printf '%s\n' "$bin_dir"
fi