#!/usr/bin/env bash # hum installer. # # Builds + registers humd as a user service. Installs the orchd supervisor # from its GitHub release, pinned exactly and checksum-verified, for hives # to keep their own processes alive with; hum itself never calls it. The # release carries orch, which orchd reads every Orchfile through, and # orchd's rootstocks, so all of them are installed beside it. # `hum hive install` teaches humd a hive, and the hive enters and # runs its own bees. hum parses Orchfiles in-process with the `the-orch` # library, the same Orch version orch speaks. # # Idempotent — re-running upgrades in place. # # Two channels: # default the latest release tag — what ships, what others run # --edge the latest main HEAD — unreleased tip, for the machine # that lives on the edge # # Usage: # ./install # build + install the latest release tag # ./install --edge # build + install the latest main HEAD # ./install status # humd state + paths # ./install logs # tail humd logs # ./install uninstall # stop + remove humd unit (state preserved) # ./install purge # remove everything INCLUDING state set -euo pipefail XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}" XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}" HUM_CONFIG="$XDG_CONFIG_HOME/hum" HUM_DATA="$XDG_DATA_HOME/hum" HUM_STATE="$XDG_STATE_HOME/hum" HUM_BIN_ROOT="$HOME/.local" HUMD_BIN="$HUM_BIN_ROOT/bin/humd" HUMCTL_BIN="$HUM_BIN_ROOT/bin/humctl" HUM_CLI_BIN="$HUM_BIN_ROOT/bin/hum" ORCHD_BIN="$HUM_BIN_ROOT/bin/orchd" HUM_REPO_URL="${HUM_REPO_URL:-https://github.com/adiled/hum.git}" HUM_SRC="$HUM_DATA/src" ORCHD_MIN_VERSION="0.5.0" ORCHD_MAX_VERSION="0.5.0" ORCHD_RELEASES="${ORCHD_DOWNLOAD_URL:-https://github.com/adiled/orchd/releases/download/v$ORCHD_MIN_VERSION}" EDGE=0 declare -a REST=() for arg in "$@"; do case "$arg" in --edge) EDGE=1 ;; -h|--help) sed -n '2,24p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) REST+=("$arg") ;; esac done CMD="${REST[0]:-install}" OS="$(uname -s)" log() { printf '\033[1m[hum]\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m[hum]\033[0m %s\n' "$*" >&2; } fail() { printf '\033[1;31m[hum]\033[0m %s\n' "$*" >&2; exit 1; } version_gte() { printf '%s\n%s\n' "$2" "$1" | sort -V -C; } read_version() { "$1" --version 2>/dev/null | sed -n 's/.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' | head -1; } ensure_min_version() { local NAME="$1" BIN="$2" MIN="$3" if ! command -v "$BIN" >/dev/null 2>&1; then fail "$NAME not found on PATH."; fi local CUR; CUR="$(read_version "$BIN" || true)" if [ -z "$CUR" ]; then fail "$NAME found but couldn't read version."; fi if version_gte "$CUR" "$MIN"; then log "$NAME ${CUR} ≥ ${MIN} ✓" else fail "$NAME ${CUR} < ${MIN} — upgrade and re-run."; fi } ensure_orchd_api() { local BIN="$1" HELP MISSING="" HELP="$("$BIN" --help 2>/dev/null || true)" for sub in grow survey fell logs; do printf '%s' "$HELP" | grep -Eq "^ +${sub}( |$)" || MISSING="$MISSING $sub" done if [ -n "$MISSING" ]; then fail "orchd is missing the subcommand(s):$MISSING — the bundled hives call grow/fell. Update orchd (or re-run ./install)." fi log "orchd API: grow survey fell logs ✓" } ensure_prereqs() { if ! command -v cargo >/dev/null 2>&1; then if [ -x "$HUMD_BIN" ] && [ -x "$HUMCTL_BIN" ] && [ -x "$HUM_CLI_BIN" ] && [ -x "$ORCHD_BIN" ]; then warn "cargo not found; using pre-built binaries in $HUM_BIN_ROOT/bin" SKIP_BUILD=1 else fail "cargo (rustup) not found. Install rustup: https://rustup.rs" fi fi } ensure_identity() { mkdir -p "$HUM_STATE" local KEY="$HUM_STATE/humd.key" if [ -s "$KEY" ]; then log "humd identity: $KEY ✓"; return 0; fi log "minting humd identity at $KEY" dd if=/dev/urandom of="$KEY" bs=32 count=1 2>/dev/null chmod 600 "$KEY" } ensure_peers_config() { mkdir -p "$HUM_CONFIG" local PEERS="$HUM_CONFIG/peers.json" if [ -s "$PEERS" ]; then log "peers.json: $PEERS ✓"; return 0; fi echo "[]" > "$PEERS" log "wrote empty peers.json at $PEERS" } ensure_hum_config() { mkdir -p "$HUM_CONFIG" local CFG="$HUM_CONFIG/hum.json" if [ -s "$CFG" ]; then log "hum.json: $CFG ✓"; return 0; fi cat > "$CFG" </dev/null && cp "$PAGE" "$DIR/man1/hum.1" 2>/dev/null; then log "man page: $DIR/man1/hum.1" return 0 fi done warn "could not install the man page — copy $PAGE into a man1 directory on your man path" return 0 done } prepare_src() { local MODE="$1" LOCAL_SRC="" if [ -f "$(dirname "$0")/Cargo.toml" ] && grep -q '"humd"' "$(dirname "$0")/Cargo.toml"; then LOCAL_SRC="$(cd "$(dirname "$0")" && pwd)" fi if [ "$MODE" = edge ] && [ -n "$LOCAL_SRC" ]; then local BRANCH; BRANCH="$(git -C "$LOCAL_SRC" rev-parse --abbrev-ref HEAD 2>/dev/null || echo '?')" if [ "$BRANCH" != "main" ]; then fail "--edge builds main HEAD, but $LOCAL_SRC is on '$BRANCH'. Check out main, or drop --edge." fi if git -C "$LOCAL_SRC" diff --quiet && git -C "$LOCAL_SRC" diff --cached --quiet; then log "updating $LOCAL_SRC to origin/main" >&2 git -C "$LOCAL_SRC" pull --ff-only --quiet origin main >&2 || warn "could not fast-forward — building the local HEAD as-is" >&2 else warn "working tree has uncommitted changes — the build will include them" >&2 fi echo "$LOCAL_SRC" return 0 fi if [ ! -d "$HUM_SRC/.git" ]; then log "cloning $HUM_REPO_URL into $HUM_SRC" >&2 mkdir -p "$HUM_DATA" git clone --quiet "$HUM_REPO_URL" "$HUM_SRC" >&2 fi if [ "$(git -C "$HUM_SRC" rev-parse --is-shallow-repository 2>/dev/null || echo false)" = "true" ]; then git -C "$HUM_SRC" fetch --quiet --unshallow --tags --force origin >&2 else git -C "$HUM_SRC" fetch --quiet --tags --force origin >&2 fi if [ "$MODE" = edge ]; then log "checking out origin/main in $HUM_SRC" >&2 git -C "$HUM_SRC" checkout --quiet -B main origin/main >&2 git -C "$HUM_SRC" reset --quiet --hard origin/main >&2 else local TAG; TAG="$(git -C "$HUM_SRC" tag --sort=-v:refname | head -1)" [ -n "$TAG" ] || fail "no release tags found in $HUM_SRC" log "checking out $TAG in $HUM_SRC" >&2 git -C "$HUM_SRC" checkout --quiet --detach "$TAG" >&2 fi echo "$HUM_SRC" } orchd_target() { local ARCH SYS case "$(uname -m)" in x86_64|amd64) ARCH=x86_64 ;; arm64|aarch64) ARCH=aarch64 ;; *) return 1 ;; esac case "$OS" in Linux) SYS=linux ;; Darwin) SYS=macos ;; *) return 1 ;; esac case "$ARCH-$SYS" in x86_64-linux|aarch64-linux|aarch64-macos) echo "$ARCH-$SYS" ;; *) return 1 ;; esac } sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 else fail "need sha256sum or shasum to check the orchd download"; fi } fetch_orchd_release() { local TMP="$2" TARBALL="orchd-$1.tar.gz" curl -fsSL "$ORCHD_RELEASES/$TARBALL" -o "$TMP/$TARBALL" \ || fail "could not fetch $ORCHD_RELEASES/$TARBALL" curl -fsSL "$ORCHD_RELEASES/$TARBALL.sha256" -o "$TMP/$TARBALL.sha256" \ || fail "could not fetch $ORCHD_RELEASES/$TARBALL.sha256" local WANT GOT WANT="$(cut -d' ' -f1 < "$TMP/$TARBALL.sha256")" GOT="$(sha256_of "$TMP/$TARBALL")" [ "$WANT" = "$GOT" ] || fail "checksum mismatch for $TARBALL: expected $WANT, got $GOT" tar -xzf "$TMP/$TARBALL" -C "$TMP" || fail "could not unpack $TARBALL" { [ -x "$TMP/bin/orchd" ] && [ -x "$TMP/bin/orch" ]; } || fail "$TARBALL holds no bin/orchd and bin/orch" mkdir -p "$HUM_BIN_ROOT/bin" || fail "cannot make $HUM_BIN_ROOT/bin" local FILE NAME for FILE in "$TMP"/bin/*; do NAME="$(basename "$FILE")" { cp "$FILE" "$HUM_BIN_ROOT/bin/.$NAME.new" \ && chmod 755 "$HUM_BIN_ROOT/bin/.$NAME.new" \ && mv -f "$HUM_BIN_ROOT/bin/.$NAME.new" "$HUM_BIN_ROOT/bin/$NAME"; } \ || fail "cannot install $NAME into $HUM_BIN_ROOT/bin" done } install_orchd() { local TARGET if ! TARGET="$(orchd_target)"; then warn "orchd ${ORCHD_MIN_VERSION} ships no build for $(uname -m) $OS (only x86_64-linux, aarch64-linux, aarch64-macos)." warn "hum runs without it, but hives that keep processes alive need it: build it from https://github.com/adiled/orchd" return 0 fi log "installing orchd (bee supervisor) ${ORCHD_MIN_VERSION} for $TARGET, with orch and its rootstocks, pinned" ORCHD_TMP="$(mktemp -d)" trap 'rm -rf "$ORCHD_TMP"' EXIT fetch_orchd_release "$TARGET" "$ORCHD_TMP" rm -rf "$ORCHD_TMP" trap - EXIT ensure_min_version orchd "$ORCHD_BIN" "$ORCHD_MIN_VERSION" ensure_orchd_api "$ORCHD_BIN" local CUR; CUR="$(read_version "$ORCHD_BIN" || true)" if [ -n "$CUR" ] && ! version_gte "$ORCHD_MAX_VERSION" "$CUR"; then fail "orchd ${CUR} > pinned ${ORCHD_MAX_VERSION} — hum pins it exactly; update the pin in ./install" fi } build_binaries() { local SRC SRC="$(prepare_src "$( [ "$EDGE" = 1 ] && echo edge || echo stable )")" log "building Rust binaries from $SRC" cargo install --quiet --locked --path "$SRC/humd" --root "$HUM_BIN_ROOT" --force cargo install --quiet --locked --path "$SRC/humctl" --root "$HUM_BIN_ROOT" --force cargo install --quiet --locked --path "$SRC/hum" --root "$HUM_BIN_ROOT" --force install_orchd [ -x "$HUMD_BIN" ] || fail "humd not at $HUMD_BIN after build" [ -x "$HUMCTL_BIN" ] || fail "humctl not at $HUMCTL_BIN after build" [ -x "$HUM_CLI_BIN" ] || fail "hum not at $HUM_CLI_BIN after build" if orchd_target >/dev/null; then [ -x "$ORCHD_BIN" ] || fail "orchd not at $ORCHD_BIN after build" fi log "humd $HUMD_BIN ; humctl $HUMCTL_BIN ; hum $HUM_CLI_BIN ; orchd $ORCHD_BIN" } linux_unit_dir="${XDG_CONFIG_HOME}/systemd/user" darwin_unit_dir="$HOME/Library/LaunchAgents" trust_remote_nurses="${HUM_TRUST_REMOTE_NURSES:-0}" write_linux_unit() { mkdir -p "$linux_unit_dir" local TRUST_LINE="" if [ -f "$linux_unit_dir/humd.service" ]; then TRUST_LINE="$(grep -E '^Environment=' "$linux_unit_dir/humd.service" || true)" fi if [ -z "$TRUST_LINE" ] && [ "$trust_remote_nurses" != "0" ]; then TRUST_LINE="Environment=HUM_TRUST_REMOTE_NURSES=$trust_remote_nurses" fi cat > "$linux_unit_dir/humd.service" </dev/null 2>&1 || true systemctl --user restart humd.service } write_darwin_unit() { mkdir -p "$darwin_unit_dir" local TRUST_XML="" if [ -f "$darwin_unit_dir/humd.plist" ]; then TRUST_XML="$(awk '/EnvironmentVariables<\/key>/{f=1} f{print} /<\/dict>/{if(f){exit}}' "$darwin_unit_dir/humd.plist")" fi if [ -z "$TRUST_XML" ] && [ "$trust_remote_nurses" != "0" ]; then TRUST_XML=" EnvironmentVariables HUM_TRUST_REMOTE_NURSES$trust_remote_nurses " fi cat > "$darwin_unit_dir/humd.plist" < Labelhumd Program$HUMD_BIN RunAtLoad KeepAlive StandardOutPath$HOME/Library/Logs/sh.hum.humd.out.log StandardErrorPath$HOME/Library/Logs/sh.hum.humd.err.log $TRUST_XML EOF local uid; uid="$(id -u)" launchctl bootout "gui/$uid/humd" 2>/dev/null || true local i for i in $(seq 1 20); do launchctl print "gui/$uid/humd" >/dev/null 2>&1 || break sleep 0.25 done if ! launchctl bootstrap "gui/$uid" "$darwin_unit_dir/humd.plist" 2>/dev/null; then sleep 1 launchctl bootstrap "gui/$uid" "$darwin_unit_dir/humd.plist" \ || fail "could not register humd as a launchd agent ($darwin_unit_dir/humd.plist)" fi } start_daemons() { case "$OS" in Linux) write_linux_unit ;; Darwin) write_darwin_unit ;; *) warn "service install not supported on $OS — run '$HUMD_BIN' manually"; return 0 ;; esac log "humd registered + started ($OS)" sleep 1 } print_next_steps() { log "" log "humd is up. Install a hive; it enters and runs its own bee:" log " hum hive openai-server install # OpenAI inference" log " hum hive bash install # lend this host's shell to the blooms that build here" log " hum hive paid-oracle install # x402 paid price oracle" log "" log "Bee state: hum bee --list" log "humd state: hum status / hum doctor / humctl logs" check_path_hint } check_path_hint() { case ":$PATH:" in *":$HUM_BIN_ROOT/bin:"*) return 0 ;; esac log "" warn "$HUM_BIN_ROOT/bin is not on \$PATH." warn "Add this to your shell config (\$HOME/.bashrc, \$HOME/.zshrc, \$HOME/.profile):" warn " export PATH=\"\$HOME/.local/bin:\$PATH\"" warn "Then open a new shell. Until then, invoke binaries by absolute path:" warn " $HUM_CLI_BIN status" } show_status() { echo "humd binary: $HUMD_BIN" echo " version: $($HUMD_BIN --version 2>&1 || echo 'not installed')" echo "orchd binary: $ORCHD_BIN" echo " version: $($ORCHD_BIN --version 2>&1 || echo 'not installed')" echo " required: >= $ORCHD_MIN_VERSION (subcommands: grow survey fell logs)" echo "identity: $HUM_STATE/humd.key $([ -s "$HUM_STATE/humd.key" ] && echo ✓ || echo MISSING)" echo "peers.json: $HUM_CONFIG/peers.json $([ -s "$HUM_CONFIG/peers.json" ] && echo ✓ || echo MISSING)" echo "hum.json: $HUM_CONFIG/hum.json $([ -s "$HUM_CONFIG/hum.json" ] && echo ✓ || echo MISSING)" case "$OS" in Linux) systemctl --user status humd --no-pager 2>&1 | head -6 || true ;; Darwin) launchctl print "gui/$(id -u)/humd" 2>&1 | head -6 || true ;; esac if [ -x "$HUM_CLI_BIN" ]; then echo "--- bees ---" "$HUM_CLI_BIN" bee --list 2>&1 || true fi } show_logs() { if [ -x "$HUMCTL_BIN" ]; then "$HUMCTL_BIN" logs -n 100 else warn "humctl not installed" fi } uninstall() { case "$OS" in Linux) systemctl --user stop humd.service 2>/dev/null || true systemctl --user disable humd.service 2>/dev/null || true rm -f "$linux_unit_dir/humd.service" systemctl --user daemon-reload 2>/dev/null || true ;; Darwin) local uid; uid="$(id -u)" launchctl bootout "gui/$uid/humd" 2>/dev/null || true rm -f "$darwin_unit_dir/humd.plist" ;; esac rm -f "$HUMD_BIN" "$HUMCTL_BIN" "$HUM_CLI_BIN" rm -f /usr/local/share/man/man1/hum.1 "$XDG_DATA_HOME/man/man1/hum.1" log "uninstalled humd. orchd left in place. State preserved in $HUM_STATE + $HUM_CONFIG." log " run \`./install purge\` to remove state too." } purge() { uninstall rm -rf "$HUM_STATE" "$HUM_CONFIG" "$HUM_DATA" log "purged." } do_install() { log "hum installer" if [ "$EDGE" = 1 ]; then log "channel: edge — latest main HEAD" else log "channel: stable — latest release tag"; fi ensure_prereqs if [ "${SKIP_BUILD:-0}" != "1" ]; then build_binaries; fi install_man_page ensure_identity ensure_peers_config ensure_hum_config "$HUM_CLI_BIN" config >/dev/null 2>&1 || warn "hum config failed — run 'hum config' to see why" start_daemons log "" print_next_steps } case "$CMD" in install|"") do_install ;; status) show_status ;; logs) show_logs ;; uninstall) uninstall ;; purge) purge ;; *) fail "unknown command: $CMD (try: install, status, logs, uninstall, purge)" ;; esac