#!/bin/sh # install — harness on this machine, from nothing. Linux or macOS, x64 or arm64. # # curl -fsSL get.harnessd.ai | sh -s -- --from HOST # scripts/install from a clone (clones nothing; uses it) # # get.harnessd.ai serves this file: main's copy, published by # scripts/publish-installer. The repository is private, so the script is # public and the code is not: --from HOST clones HOST:prg/harness and copies # HOST's model settings, over the ssh you already have. Without --from it # clones from GitHub, which needs a key GitHub knows, and leaves the model # settings to you. Rerunning is how you update. --help for the steps and the # options. # # POSIX sh, not bash: it has to run on a fresh Mac, whose /bin/bash is 3.2, and # when piped from ssh it is read by whatever `sh` is. Everything is inside # main(), which the shell must read to the end before it can run, so a command # that reads stdin cannot eat the rest of a piped script. set -eu usage() { cat <<'EOF' install — harness on this machine, from nothing (Linux or macOS). curl -fsSL get.harnessd.ai | sh -s -- [options] scripts/install [options] Each step is skipped when it is already done, so rerunning it updates: toolchain git (the Xcode Command Line Tools on macOS); Node >= 22.18 and Go, only if what is on PATH is missing or too old: downloaded, checksummed, unpacked under ~/.local/share/harness, linked into ~/.local/bin code clone into ~/prg/harness, or pull --ff-only; npm ci; build commands scripts/install-bin: harness, h, harness-cli, h-... in ~/bin PATH a marked block in your shell's startup file, if ~/bin (or ~/.local/bin) is missing from PATH; never written through a symlink, so a stowed dotfile is reported instead of edited env ~/.harness/env, mode 600, read by the daemon's service; --env-from HOST fills in the model settings from HOST daemon a systemd user unit (Linux) or a launchd agent (macOS); then waits for http://127.0.0.1:7777/healthz Options: --from HOST a machine that already runs harness, over ssh: clone its prg/harness and copy its model settings (the same as --repo HOST:prg/harness --env-from HOST) --repo URL clone from URL (default git@github.com:klutometis/harnessd.git) --dir DIR the checkout (default ~/prg/harness) --bin DIR where the commands go (default ~/bin) --env-from HOST copy model credentials and settings from HOST's login environment (over ssh) into ~/.harness/env --no-service install nothing that runs the daemon --no-modify-path leave shell startup files alone -h, --help this Environment: HARNESS_REPO, HARNESS_DIR, HARNESS_BIN, HARNESS_NODE_VERSION (e.g. v26.13.1; default: the newest LTS), HARNESS_STATE_DIR (default ~/.harness). EOF } say() { printf '%s\n' "$*"; } step() { printf '\n==> %s\n' "$*"; } warn() { printf 'install: warning: %s\n' "$*" >&2; } die() { printf 'install: %s\n' "$*" >&2 exit 1 } have() { command -v "$1" >/dev/null 2>&1; } # Run a step whose output only matters when it fails. quietly() { if ! out="$("$@" 2>&1 &2 die "failed: $*" fi } need_arg() { [ $# -ge 2 ] && [ -n "$2" ] || die "$1 needs an argument"; } # Dotted versions, numerically: is $1 >= $2? version_ge() { awk -v a="$1" -v b="$2" 'BEGIN { n = split(a, x, "."); m = split(b, y, "."); if (m > n) n = m for (i = 1; i <= n; i++) { if (x[i] + 0 > y[i] + 0) exit 0 if (x[i] + 0 < y[i] + 0) exit 1 } exit 0 }' } fetch() { # url dest if have curl; then curl -fsSL --retry 3 -o "$2" "$1" elif have wget; then wget -q -O "$2" "$1" else die "need curl or wget" fi } sha256() { if have sha256sum; then sha256sum "$1" | cut -d' ' -f1 else shasum -a 256 "$1" | cut -d' ' -f1 fi } as_root() { if [ "$(id -u)" = 0 ]; then "$@" elif have sudo; then sudo "$@" else die "need root (or sudo) for: $*" fi } # $HOME/... as "~/..." for messages, and as '$HOME/...' for files we write. # Display only: a literal ~ and $HOME, for messages and the PATH block. # shellcheck disable=SC2088 tilde() { case "$1" in "$HOME"/*) printf '~/%s' "${1#"$HOME"/}" ;; *) printf '%s' "$1" ;; esac; } # shellcheck disable=SC2016 homevar() { case "$1" in "$HOME"/*) printf '$HOME/%s' "${1#"$HOME"/}" ;; *) printf '%s' "$1" ;; esac; } # --------------------------------------------------------------------------- # platform detect_platform() { case "$(uname -s)" in Linux) OS=linux ;; Darwin) OS=darwin ;; *) die "unsupported OS: $(uname -s) (Linux and macOS only)" ;; esac arch="$(uname -m)" # A shell under Rosetta reports x86_64 on Apple Silicon. Native is better, # and the toolchains we download for it are arm64. if [ "$OS" = darwin ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then arch=arm64 fi case "$arch" in x86_64 | amd64) NODE_ARCH=x64 GO_ARCH=amd64 ;; aarch64 | arm64) NODE_ARCH=arm64 GO_ARCH=arm64 ;; *) die "unsupported CPU: $arch" ;; esac } # --------------------------------------------------------------------------- # toolchain ensure_prereqs() { if [ "$OS" = darwin ]; then # /usr/bin/git on a fresh Mac is a stub that opens an installer dialog. xcode-select -p >/dev/null 2>&1 || install_clt say "git $(git --version | cut -d' ' -f3) ($(command -v git))" return 0 fi missing="" have git || missing="$missing git" have curl || have wget || missing="$missing curl" have tar || missing="$missing tar" have gzip || missing="$missing gzip" # Node's official Linux builds link libatomic, which a minimal image (a # container, a fresh server) does not have; a desktop nearly always does. if ! { ldconfig -p 2>/dev/null | grep -q 'libatomic\.so\.1'; } && ! ls /lib*/libatomic.so.1 /usr/lib*/libatomic.so.1 /usr/lib/*/libatomic.so.1 >/dev/null 2>&1; then if have apt-get; then missing="$missing libatomic1"; elif have dnf; then missing="$missing libatomic"; fi fi # node-pty publishes prebuilt binaries for macOS and Windows only, so on # Linux `npm ci` compiles it, with node-gyp: python3, make and a C++ compiler. have python3 || missing="$missing python3" have make || missing="$missing make" if ! have g++ && ! have c++; then if have apt-get; then missing="$missing g++"; elif have dnf; then missing="$missing gcc-c++"; else missing="$missing gcc"; fi fi if [ -n "$missing" ]; then say "installing:$missing" if have pacman; then missing="$(printf '%s' "$missing" | sed 's/python3/python/')"; fi if have apt-get; then quietly as_root apt-get update -qq # shellcheck disable=SC2086 quietly as_root env DEBIAN_FRONTEND=noninteractive apt-get install -y -qq $missing ca-certificates elif have dnf; then # shellcheck disable=SC2086 quietly as_root dnf install -y -q $missing ca-certificates elif have pacman; then # shellcheck disable=SC2086 quietly as_root pacman -S --noconfirm --needed $missing ca-certificates else die "install$missing with your package manager, then rerun" fi fi say "git $(git --version | cut -d' ' -f3) ($(command -v git))" } # The Command Line Tools, headless, the way Homebrew's installer does it: the # marker file makes softwareupdate list the CLT, which it otherwise only # offers to the GUI prompt. install_clt() { step "Xcode Command Line Tools (git, clang): installing; this takes a few minutes" marker=/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress as_root touch "$marker" label="$(softwareupdate -l 2>/dev/null /dev/null 2>&1; then xcode-select --install >/dev/null 2>&1 || true die "softwareupdate did not offer the Command Line Tools; finish the dialog that just opened, then rerun" fi } # A link in ~/.local/bin to something we unpacked. Never over a name we did not # make: that is someone's own node, and they can keep it. link_tool() { # target name mkdir -p "$LOCALBIN" p="$LOCALBIN/$2" if [ -L "$p" ]; then case "$(readlink "$p")" in "$TOOLS"/*) ;; *) warn "$(tilde "$p") is a link to $(readlink "$p"); not replacing it" return 0 ;; esac elif [ -e "$p" ]; then warn "$(tilde "$p") exists and is not ours; not replacing it" return 0 fi ln -sfn "$1" "$p" LINKED_TOOLS=1 } node_ok() { have node || return 1 v="$(node -p process.versions.node 2>/dev/null /dev/null) at $(command -v node) is older than 22.18; installing a newer one" fi ver="${HARNESS_NODE_VERSION:-}" if [ -z "$ver" ]; then fetch https://nodejs.org/dist/index.json "$TMP/node-index.json" # One release per line, newest first; the first with an LTS codename. ver="$(sed -n '/"lts":"/{s/.*"version":"\(v[0-9.]*\)".*/\1/p;q;}' "$TMP/node-index.json")" [ -n "$ver" ] || die "could not find the current Node LTS in nodejs.org/dist/index.json" fi name="node-$ver-$OS-$NODE_ARCH" if [ ! -x "$TOOLS/$name/bin/node" ]; then say "downloading $name" fetch "https://nodejs.org/dist/$ver/SHASUMS256.txt" "$TMP/SHASUMS256.txt" fetch "https://nodejs.org/dist/$ver/$name.tar.gz" "$TMP/$name.tar.gz" want="$(awk -v f="$name.tar.gz" '$2 == f {print $1}' "$TMP/SHASUMS256.txt")" [ -n "$want" ] || die "$name.tar.gz is not in $ver's SHASUMS256.txt" [ "$(sha256 "$TMP/$name.tar.gz")" = "$want" ] || die "checksum mismatch: $name.tar.gz" mkdir -p "$TOOLS" tar -xzf "$TMP/$name.tar.gz" -C "$TMP" rm -rf "${TOOLS:?}/$name" mv "$TMP/$name" "$TOOLS/$name" fi if ! err="$("$TOOLS/$name/bin/node" --version 2>&1 /dev/null || true node_ok || die "installed $name, but \`node\` on PATH is $(command -v node || echo missing) ($(node --version 2>&1 &1 /dev/null || true have go || die "installed $name, but \`go\` is not on PATH" say "go $(go env GOVERSION &2 </dev/null 2>&1; then FRESH=0 if [ "$FROM_CHECKOUT" = 1 ]; then say "using $(tilde "$DIR") as it is" elif [ -n "$(git -C "$DIR" status --porcelain --untracked-files=no)" ]; then warn "$(tilde "$DIR") has uncommitted changes; not pulling" elif git -C "$DIR" pull --ff-only --quiet /dev/null)" ] || die "$DIR exists and is not a git checkout" mkdir -p "$(dirname "$DIR")" say "cloning $REPO into $(tilde "$DIR")" # accept-new: a first connection to github.com should not stop at a yes/no # prompt; a host key that has *changed* still refuses. GIT_SSH_COMMAND="${GIT_SSH_COMMAND:-ssh -o StrictHostKeyChecking=accept-new}" \ git clone --quiet "$REPO" "$DIR" >> harness >>>' "$rc"; then say "$(tilde "$rc") already has the harness PATH block" elif [ -L "$rc" ]; then say "$(tilde "$rc") is a symlink (stowed dotfiles?), so it is yours to edit, not mine. Add:" printf '%s\n' "$block" | sed 's/^/ /' else printf '\n%s\n' "$block" >>"$rc" say "added$need to PATH in $(tilde "$rc") (new terminals)" PATH_ADDED=1 fi } # --------------------------------------------------------------------------- # env # What decides which model answers and how: credentials, and every setting the # daemon checks against HARNESS_MODELS at boot. Partial is worse than none: an # allowlist copied without its HARNESS_LABEL_MODEL leaves the default label # model outside it, and the daemon refuses to start. ENV_VARS="ANTHROPIC_API_KEY CLAUDE_OAUTH_PROXY_URL CLAUDE_OAUTH_PROXY_TOKEN CHATGPT_PROXY_URL CHATGPT_PROXY_TOKEN HARNESS_PROVIDER HARNESS_MODEL HARNESS_MODELS HARNESS_LABEL_MODEL HARNESS_MACHINE_MODEL HARNESS_TOOL_LABELS HARNESS_EFFORT HARNESS_THINKING HARNESS_VERTEX_PROJECT VERTEX_LOCATION VOYAGE_API_KEY" ensure_env() { mkdir -p "$STATE" if [ ! -f "$ENVFILE" ]; then ( umask 077 cat >"$ENVFILE" <"$tmpenv.2" || true printf '%s="%s"\n' "$k" "$v" >>"$tmpenv.2" mv "$tmpenv.2" "$tmpenv" names="$names $k" done <"$ENVFILE" # through a symlink, if it is one rm -f "$tmpenv" say "copied from $ENV_FROM into $(tilde "$ENVFILE"):$names" } has_credentials() { grep -qE '^(ANTHROPIC_API_KEY|CLAUDE_OAUTH_PROXY_TOKEN)=.' "$ENVFILE" && return 0 grep -qE '^HARNESS_PROVIDER=.?vertex' "$ENVFILE" && return 0 [ -n "${ANTHROPIC_API_KEY:-}" ] && return 0 [ -f "$HOME/.claude/.credentials.json" ] && return 0 [ -n "${CLAUDE_CONFIG_DIR:-}" ] && return 0 return 1 } # --------------------------------------------------------------------------- # daemon port() { printf '%s' "${HARNESS_PORT:-7777}"; } LAUNCHD_LABEL="${HARNESS_LAUNCHD_LABEL:-com.danenberg.harnessd}" # Both services restart the daemon only when it fails: a clean stop stays # stopped, so a restart is the service's own verb. restart_hint() { if [ "$OS" = darwin ]; then printf 'launchctl kickstart -k gui/%s/%s' "$(id -u)" "$LAUNCHD_LABEL" elif have systemctl && systemctl --user show-environment >/dev/null 2>&1; then printf 'systemctl --user restart harnessd' else printf 'harnessd-start --stop, then harnessd-start once it has exited' fi } healthy() { if have curl; then curl -fsS -m 2 "http://127.0.0.1:$(port)/healthz" >/dev/null 2>&1 else wget -q -T 2 -O /dev/null "http://127.0.0.1:$(port)/healthz" 2>/dev/null fi } ensure_daemon() { if [ "$SERVICE" = 0 ]; then say "--no-service: start it yourself with harnessd-start" return 0 fi if healthy; then say "a daemon is already up on 127.0.0.1:$(port); not touching it" "$DIR/packages/daemon/bin/harnessd-start" --status &1 | sed 's/^/ /' || true return 0 fi if [ "$OS" = darwin ] || { have systemctl && systemctl --user show-environment >/dev/null 2>&1; }; then # Idempotent; refuses (with a diff) to replace a definition it did not # write, which is the dotfiles' own unit on a machine that has one. "$DIR/scripts/harnessd-install-service" /dev/null || true else systemctl --user start harnessd.service /dev/null | sed 's/^/ /' >&2 || true [ ! -f "$STATE/launchd.log" ] || tail -n 30 "$STATE/launchd.log" | sed 's/^/ /' >&2 exit 1 fi say "daemon up on 127.0.0.1:$(port) after ${i}s" } # --------------------------------------------------------------------------- main() { REPO="${HARNESS_REPO:-git@github.com:klutometis/harnessd.git}" DIR="${HARNESS_DIR:-$HOME/prg/harness}" BIN="${HARNESS_BIN:-$HOME/bin}" ENV_FROM="" FROM="" SERVICE=1 MODIFY_PATH=1 REPO_SET=0 DIR_SET=0 while [ $# -gt 0 ]; do case "$1" in --repo) need_arg "$@"; REPO="$2"; REPO_SET=1; shift ;; --repo=*) REPO="${1#--repo=}"; REPO_SET=1 ;; --dir) need_arg "$@"; DIR="$2"; DIR_SET=1; shift ;; --dir=*) DIR="${1#--dir=}"; DIR_SET=1 ;; --bin) need_arg "$@"; BIN="$2"; shift ;; --bin=*) BIN="${1#--bin=}" ;; --env-from) need_arg "$@"; ENV_FROM="$2"; shift ;; --env-from=*) ENV_FROM="${1#--env-from=}" ;; --from) need_arg "$@"; FROM="$2"; shift ;; --from=*) FROM="${1#--from=}" ;; --no-service) SERVICE=0 ;; --no-modify-path) MODIFY_PATH=0 ;; -h | --help) usage; exit 0 ;; *) die "unknown option: $1 (try --help)" ;; esac shift done # --from is shorthand; an explicit --repo or --env-from still wins. if [ -n "$FROM" ]; then [ "$REPO_SET" = 1 ] || { REPO="$FROM:prg/harness"; REPO_SET=1; } [ -n "$ENV_FROM" ] || ENV_FROM="$FROM" fi # Run as a file in a checkout (not piped; $0 is then just `sh`), with no # --repo or --dir: install that checkout, as it is. FROM_CHECKOUT=0 if [ "$REPO_SET" = 0 ] && [ "$DIR_SET" = 0 ] && [ -f "$0" ] && [ "$(basename "$0")" = install ] && root="$(cd "$(dirname "$0")/.." 2>/dev/null && pwd -P)" && [ -f "$root/tui/go.mod" ] && [ -f "$root/scripts/install-bin" ]; then DIR="$root" FROM_CHECKOUT=1 fi case "$DIR" in /*) ;; *) DIR="$(pwd)/$DIR" ;; esac case "$BIN" in /*) ;; *) BIN="$(pwd)/$BIN" ;; esac BIN="${BIN%/}" STATE="${HARNESS_STATE_DIR:-$HOME/.harness}" ENVFILE="$STATE/env" TOOLS="$HOME/.local/share/harness" LOCALBIN="$HOME/.local/bin" ORIG_PATH="$PATH" LINKED_TOOLS=0 PATH_ADDED=0 FRESH=0 SERVICE_KIND="" # Our own links go first for the rest of this run, whatever PATH says. PATH="$BIN:$LOCALBIN:$PATH" export PATH TMP="$(mktemp -d "${TMPDIR:-/tmp}/harness-install.XXXXXX")" trap 'rm -rf "$TMP"' EXIT trap 'exit 130' INT TERM detect_platform say "harness install: $OS-$NODE_ARCH, checkout $(tilde "$DIR"), commands $(tilde "$BIN")" step "toolchain" ensure_prereqs ensure_node step "code" ensure_code ensure_go ensure_build step "commands" ensure_commands ensure_path step "env" ensure_env step "daemon" ensure_daemon step "done" "$BIN/harness-cli" ls /dev/null 2>&1 || warn "harness-cli could not list sessions; see harnessd-start --status" say " checkout $(tilde "$DIR")" say " commands $(tilde "$BIN"): h (= harness), harness-cli, h-ls, h-send, ..." [ -z "$SERVICE_KIND" ] || say " daemon http://127.0.0.1:$(port) ($SERVICE_KIND; harnessd-start --status)" if ! has_credentials; then say "" say " No model credentials yet. One of:" say " harness-cli claude-auth login a Claude Pro/Max subscription" say " ANTHROPIC_API_KEY=... in $(tilde "$ENVFILE"), then: $(restart_hint)" say " rerun with --env-from HOST copy them from a machine that has them" fi if [ "$PATH_ADDED" = 1 ]; then say "" say " Open a new terminal (or: exec \$SHELL -l), then: h" else say "" say " Then: h" fi } main "$@"