#!/bin/bash # # Purpose : Node.js and Anthropic's sandbox runtime (srt) for the agent # sandbox. Source this file after lib/agents.sh; it uses # confirm_yes from configure. # # srt needs Node.js 22.12 or later, and Pop!_OS 24.04's is 18. So unless a # new enough one is installed already, the current long-term support release # is downloaded from nodejs.org into FC_TOOLS/node, checked against the # SHA-256 sums it publishes, and used only for srt: nothing in /usr or your # shell changes. # srt needs Node.js 22.12 or later node_ok() { local node version node="$(PATH="$FC_TOOLS/node/bin:$PATH" command -v node)" || return 1 version="$("$node" -p 'process.versions.node' 2> /dev/null)" || return 1 [ "$(printf '%s\n' 22.12.0 "$version" | sort -V | head -n 1)" = 22.12.0 ] } # download_checked download_checked() { local url="$1" sums_url="$2" name="$3" dest="$4" expected actual echo "Downloading $url" curl -fL --progress-bar --max-time 1800 -o "$dest" "$url" || return 1 expected="$(curl -fsL --max-time 30 "$sums_url" | awk -v f="$name" '$2 == f || $2 == "*" f { print $1; exit }')" actual="$(sha256sum "$dest" | cut -d' ' -f1)" if [ -z "$expected" ] || [ "$expected" != "$actual" ]; then echo -e "\e[31mThe download doesn't match the checksum its release publishes. Not installing it.\e[0m" rm -f "$dest" return 1 fi } install_node() { local tmp version arch name status=1 version="$(curl -fsL --max-time 30 https://nodejs.org/dist/index.json \ | jq -r '[.[] | select(.lts)][0].version // empty')" [ -n "$version" ] || { echo "Couldn't find Node.js' current release."; return 1; } case "$(uname -m)" in aarch64|arm64) arch=arm64 ;; *) arch=x64 ;; esac name="node-$version-linux-$arch.tar.xz" tmp="$(mktemp -d)" || return 1 if download_checked "https://nodejs.org/dist/$version/$name" \ "https://nodejs.org/dist/$version/SHASUMS256.txt" "$name" "$tmp/$name" \ && tar -xJf "$tmp/$name" -C "$tmp" && mkdir -p "$FC_TOOLS"; then rm -rf "$FC_TOOLS/node" && mv "$tmp/${name%.tar.xz}" "$FC_TOOLS/node" && status=0 fi rm -rf "$tmp" return $status } # srt from npm, into FC_TOOLS/srt. It has no install scripts of its own; the # ones npm would warn it blocked belong to a Windows-only package it # optionally depends on. install_srt() { local node_dir node_ok || install_node || return 1 node_dir="$(dirname "$(PATH="$FC_TOOLS/node/bin:$PATH" command -v node)")" echo "Installing the sandbox runtime (srt) from npm..." PATH="$node_dir:$PATH" "$node_dir/npm" install --global --prefix "$FC_TOOLS/srt" \ --no-fund --no-audit --ignore-scripts @anthropic-ai/sandbox-runtime > /dev/null } # Updating srt is installing it again srt_ensure() { srt_path > /dev/null && return 0 echo "The sandbox is Anthropic's sandbox runtime (srt), which runs on Node.js." node_ok || echo "This computer's Node.js is older than srt needs, so it gets its own, from nodejs.org." confirm_yes "Install it (into $FC_TOOLS)?" || return 1 install_srt }