#!/bin/bash # # Grok unified build installer — https://x.ai/cli/unified/install.sh # # Installs the unified build (agent-host, grok with its agent-host daemon embedded) from a public # unified feed as `grok`: it replaces a stock grok's commands, never touches version.json, and # selects that feed's channel in config.toml so `grok update` updates from it. GROK_CHANNEL picks # the feed: stable reads cli/unified (the `unified` channel), which stable promotion publishes, and # alpha cli/unified-alpha (the `unified-alpha` channel), which the alpha release publishes. # # Env: GROK_CHANNEL (stable|alpha, default: stable) # # Usage: # curl -fsSL https://x.ai/cli/unified/install.sh | bash # the feed's latest # curl -fsSL https://x.ai/cli/unified/install.sh | bash -s 0.1.7567 # a published version # curl -fsSL https://x.ai/cli/unified/install.sh | GROK_CHANNEL=alpha bash # the alpha feed's latest # # It downloads the .zst (or .gz) copy when this machine can decode it, refuses a binary that does # not match its .sha256 or does not run as the version it was published as, keeps it as # $GROK_HOME/downloads/agent-host-- behind $GROK_HOME/bin/agent-host, points # $GROK_HOME/bin/grok and agent at that link (the layout `grok update --channel` leaves), and links # ~/.local/bin/agent-host, grok and agent to them; both grok and agent links, and config.toml, are # left alone while the Cursor CLI's grok-switch.json reads as a pending or active switch. Without a # switch, it links ~/.local/bin/cursor-agent to agent-host where nothing has that name. Builds: # linux-x86_64, linux-aarch64, macos-aarch64 and macos-x86_64 (older versions have only linux-x86_64 # and macos-aarch64); on Windows use `irm https://x.ai/cli/unified/install.ps1 | iex`. Written for # bash 3.2. set -euo pipefail TARGET="${1:-}" SEMVER='^[0-9]+\.[0-9]+\.[0-9]+$' die() { echo "Error: $*" >&2; exit 1; } if [ -n "$TARGET" ] && [[ ! "$TARGET" =~ $SEMVER ]]; then die "invalid version $TARGET (expected X.Y.Z)" fi DOWNLOADER="" if command -v curl >/dev/null 2>&1; then DOWNLOADER="curl" elif command -v wget >/dev/null 2>&1; then DOWNLOADER="wget" else die "either curl or wget is required but neither is installed" fi download_file() { local url="$1" output="${2:-}" if [ "$DOWNLOADER" = "curl" ]; then if [ -n "$output" ]; then curl -fsSL --retry 2 -o "$output" "$url" else curl -fsSL --retry 2 "$url" fi else if [ -n "$output" ]; then wget -q -O "$output" "$url" else wget -q -O - "$url" fi fi } # Parallel byte-range download, as install.sh's. Falls back to download_file whenever HEAD lacks # Content-Length, the file is small (<16 MiB), curl is unavailable, or any chunk fails. download_file_parallel() { local url="$1" output="$2" if [ "$DOWNLOADER" != "curl" ]; then download_file "$url" "$output" return fi local size size=$(curl -fsSL --head "$url" 2>/dev/null | awk -F'[: \r\n]+' 'tolower($1)=="content-length"{print $2; exit}') || size="" if [ -z "$size" ] || ! [ "$size" -ge 16777216 ] 2>/dev/null; then download_file "$url" "$output" return fi local n=8 local chunk_size=$(( (size + n - 1) / n )) local tmpdir tmpdir=$(mktemp -d 2>/dev/null) || { download_file "$url" "$output"; return; } local pids=() i start end for i in $(seq 0 $((n - 1))); do start=$((i * chunk_size)) end=$((start + chunk_size - 1)) [ "$end" -ge "$size" ] && end=$((size - 1)) curl -fsSL -r "${start}-${end}" -o "${tmpdir}/$(printf 'chunk.%03d' "$i")" "$url" & pids+=($!) done local all_ok=true pid for pid in "${pids[@]}"; do wait "$pid" || all_ok=false done if [ "$all_ok" = true ] && cat "${tmpdir}"/chunk.* > "$output" 2>/dev/null; then rm -rf "$tmpdir" return 0 fi rm -rf "$tmpdir" download_file "$url" "$output" } is_not_found() { local url="$1" code if [ "$DOWNLOADER" = "curl" ]; then code=$(curl -o /dev/null -sSL -w '%{http_code}' --head "$url" 2>/dev/null) || true else code=$(wget --server-response --spider "$url" 2>&1 | awk '/HTTP\//{print $2}' | tail -1) || true fi [ "$code" = "404" ] } # The binary is ~300 MiB; the cap only stops a decompression bomb from filling the disk. The # caller checks the decoded bytes against the plain object's .sha256 either way. fetch_compressed() { local url="$1" tmp="$2" out="$3" shift 3 is_not_found "$url" && return 1 download_file_parallel "$url" "$tmp" || return 1 local max=$((1024 * 1024 * 1024)) if (set -o pipefail; "$@" <"$tmp" 2>/dev/null | head -c "$max" >"$out"); then if [ -s "$out" ] && [ "$(wc -c <"$out" | tr -d ' ')" -lt "$max" ]; then return 0 fi fi rm -f "$out" return 1 } fetch_binary() { local base="$1" out="$2" tmp tmp=$(mktemp 2>/dev/null) || tmp="" if [ -n "$tmp" ]; then if command -v zstd >/dev/null 2>&1 && fetch_compressed "${base}.zst" "$tmp" "$out" zstd -q -dc; then rm -f "$tmp" return 0 fi if command -v gzip >/dev/null 2>&1 && fetch_compressed "${base}.gz" "$tmp" "$out" gzip -dc; then rm -f "$tmp" return 0 fi rm -f "$tmp" fi download_file_parallel "$base" "$out" } 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 return 1 fi } case "$(uname -s)" in Darwin) os="macos" ;; Linux) os="linux" ;; MINGW* | MSYS* | CYGWIN*) die "on Windows, run this in PowerShell instead: irm https://x.ai/cli/unified/install.ps1 | iex" ;; *) die "unsupported OS: $(uname -s)" ;; esac case "$(uname -m)" in x86_64|amd64) arch="x86_64" ;; arm64|aarch64) arch="aarch64" ;; *) arch="$(uname -m)" ;; esac # A Rosetta shell on Apple Silicon reports x86_64; the build is native arm64. if [ "$os" = "macos" ] && [ "$arch" = "x86_64" ]; then sysctl_bin="$(command -v sysctl || echo /usr/sbin/sysctl)" if [ "$("$sysctl_bin" -n hw.optional.arm64 2>/dev/null)" = "1" ]; then arch="aarch64" fi fi platform="${os}-${arch}" case "$platform" in linux-x86_64|linux-aarch64|macos-aarch64|macos-x86_64) ;; *) die "there is no unified build for $platform yet; there are linux-x86_64, linux-aarch64, macos-aarch64, macos-x86_64 and Windows builds" ;; esac CHANNEL="${GROK_CHANNEL:-stable}" case "$CHANNEL" in stable) FEED="unified" ;; alpha) FEED="unified-alpha" ;; *) echo "Invalid GROK_CHANNEL: '${CHANNEL}' (expected stable or alpha)" >&2 exit 1 ;; esac BASE_URL_PRIMARY="https://x.ai/cli/$FEED" BASE_URL_FALLBACK="https://storage.googleapis.com/grok-build-public-artifacts/cli/$FEED" # The channel config.toml records: the public feed, whichever feed this run reads. CHANNEL_TABLE="[cli.channels.$FEED] binary = \"agent-host\" sources = [ { url = \"$BASE_URL_PRIMARY\", auth = \"anonymous\" }, { url = \"$BASE_URL_FALLBACK\", auth = \"anonymous\" }, ]" # The same loopback-only override the binary's updater honors, for tests. case "${GROK_CLI_BASE_URL:-}" in *@*) ;; http://127.0.0.1:* | http://localhost:* | "http://[::1]:"*) BASE_URL_PRIMARY="${GROK_CLI_BASE_URL%/}" BASE_URL_FALLBACK="${GROK_CLI_BASE_URL%/}" ;; esac GROK_HOME_DIR="${GROK_HOME:-$HOME/.grok}" DOWNLOAD_DIR="$GROK_HOME_DIR/downloads" BIN_DIR="$GROK_HOME_DIR/bin" CONFIG_FILE="$GROK_HOME_DIR/config.toml" LINK_DIR="$HOME/.local/bin" # The Cursor CLI's getConfigDir(): CURSOR_CONFIG_DIR, else $XDG_CONFIG_HOME/cursor, else ~/.cursor. cursor_config_dir() { local explicit="${CURSOR_CONFIG_DIR:-}" xdg="${XDG_CONFIG_HOME:-}" if [ -n "${explicit//[[:space:]]/}" ]; then printf '%s' "$explicit" elif [ -n "${xdg//[[:space:]]/}" ]; then printf '%s/cursor' "$xdg" else printf '%s/.cursor' "$HOME" fi } # The Cursor CLI's switch record in the file $1, read as every Grok reader reads it: `JSON.parse`, # then a switch only for an object whose schema is the number 1, written `1`, and whose state is # "pending" or "active". Prints active, pending, other (JSON that records no switch) or invalid, and # nothing when it can't read the file (no od or awk, or either fails). The cases all readers agree # on are xai-grok-config/testdata/cursor-switch-records.tsv. cursor_switch_verdict() { command -v od >/dev/null && command -v awk >/dev/null || return 0 # awk reads the bytes as od prints them, 16 numbers to a line: no awk then meets a long line, # where some take time in proportion to the position for each character, or a NUL { LC_ALL=C od -A n -v -t u1 "$1" || echo failed; } | LC_ALL=C awk ' BEGIN { for (b = 32; b < 127; b++) chr[b] = sprintf("%c", b) for (b = 48; b < 58; b++) hex[b] = b - 48 for (b = 97; b < 103; b++) hex[b] = b - 87 for (b = 65; b < 71; b++) hex[b] = b - 55 accepts["zero"] = accepts["int"] = accepts["frac"] = accepts["exp"] = 1 expect = "value"; depth = 0; bad = 0; top = ""; tok = "" schema = ""; state = "\001"; key = "" } function closed() { expect = depth ? "comma" : "end" } # A value of kind at the top of the record, under key. function member(kind, text) { if (depth != 1 || stack[1] != "{") return if (key == "schema") schema = kind == "number" ? text : "" if (key == "state") state = kind == "string" ? text : "\001" } # The first 8 characters of a string or number: enough to tell the keys, states and schema # the record names, so a long one costs nothing. function keep(c) { if (length(text) < 8) text = text c } # The byte b inside a string, decoded far enough to compare with ASCII. function in_string(b) { if (escape == 1) { escape = 0 if (b == 34 || b == 92 || b == 47) keep(chr[b]) else if (b == 98 || b == 102 || b == 110 || b == 114 || b == 116) keep("\001") else if (b == 117) { escape = 2; code = 0 } else bad = 1 } else if (escape) { if (!(b in hex)) { bad = 1; return } code = code * 16 + hex[b] if (++escape == 6) { escape = 0; keep(code >= 32 && code < 127 ? chr[code] : "\001") } } else if (b == 34) { tok = "" if (is_key) { if (depth == 1) key = text; expect = "colon" } else { member("string", text); closed() } } else if (b == 92) escape = 1 else if (b < 32) bad = 1 else keep(b < 127 ? chr[b] : "\001") } # Whether the byte b goes on the number being read; a number that cannot end there is bad. function in_number(b, digit) { digit = b >= 48 && b <= 57 if (number == "start" && b == 45) number = "minus" else if ((number == "start" || number == "minus") && b == 48) number = "zero" else if ((number == "start" || number == "minus") && digit) number = "int" else if (number == "int" && digit) number = "int" else if ((number == "zero" || number == "int") && b == 46) number = "dot" else if ((number == "dot" || number == "frac") && digit) number = "frac" else if ((number == "zero" || number == "int" || number == "frac") && (b == 101 || b == 69)) number = "e" else if (number == "e" && (b == 43 || b == 45)) number = "sign" else if ((number == "e" || number == "sign" || number == "exp") && digit) number = "exp" else { if (!(number in accepts)) bad = 1 tok = ""; member("number", text); closed() return 0 } keep(chr[b]) return 1 } function byte(b) { if (tok == "string") { in_string(b); return } if (tok == "number" && in_number(b)) return if (tok == "literal") { if (b != word[at] + 0) { bad = 1; return } if (++at > letters) { tok = ""; member("literal", ""); closed() } return } if (bad || b == 32 || b == 9 || b == 10 || b == 13) return if (expect == "end") { bad = 1; return } if (expect == "colon") { if (b == 58) expect = "value"; else bad = 1; return } if (expect == "comma") { if (b == 44) expect = stack[depth] == "{" ? "key" : "value" else if ((b == 125 && stack[depth] == "{") || (b == 93 && stack[depth] == "[")) { depth--; closed() } else bad = 1 return } if (expect == "key" || expect == "key_or_close") { if (b == 125 && expect == "key_or_close") { depth--; closed(); return } if (b != 34) { bad = 1; return } tok = "string"; is_key = 1; escape = 0; text = "" return } # expect is value or value_or_close if (b == 93 && expect == "value_or_close") { depth--; closed(); return } if (depth == 0) top = b if (b == 123 || b == 91) { member("container", "") stack[++depth] = b == 123 ? "{" : "[" expect = b == 123 ? "key_or_close" : "value_or_close" } else if (b == 34) { tok = "string"; is_key = 0; escape = 0; text = "" } else if (b == 45 || (b >= 48 && b <= 57)) { tok = "number"; number = "start"; text = ""; in_number(b) } else if (b == 116 || b == 102 || b == 110) { # true, false or null, as bytes letters = split(b == 116 ? "116 114 117 101" : b == 102 ? "102 97 108 115 101" : "110 117 108 108", word, " ") tok = "literal"; at = 2 } else bad = 1 } $1 == "failed" { failed = 1 } { for (f = 1; f <= NF && !bad; f++) byte($f + 0) } END { if (failed) exit if (tok == "number" && !bad) byte(32) if (bad || tok != "" || expect != "end") print "invalid" else if (top == 123 && schema == "1" && (state == "active" || state == "pending")) print state else print "other" } ' } # Whether the Cursor CLI's switch record holds grok and agent, in $BIN_DIR and on PATH: only a # record that reads as a pending or active switch does. Anything else there holds nothing and gets a # note saying why, so a stale or foreign file never keeps grok and agent from an install. cursor_owns_names() { local dir path up why="" needs="" dir=$(cursor_config_dir) path="$dir/grok-switch.json" if [ -e "$dir" ] && [ ! -d "$dir" ]; then why="$dir is not a directory" elif [ ! -e "$path" ]; then # No record, unless a directory on the way to it can't be searched up=$dir until [ -e "$up" ] || [ "$up" = "$(dirname "$up")" ]; do up=$(dirname "$up"); done if [ -d "$up" ] && [ ! -x "$up" ]; then why="can't read $path"; fi elif [ -d "$path" ] || [ ! -r "$path" ]; then why="can't read $path" else case "$(cursor_switch_verdict "$path")" in active | pending) return 0 ;; invalid) why="$path is not JSON" ;; other) why="$path records no switch (schema 1, state pending or active)" ;; *) # A record that can't be read could be a switch: never take grok and agent over it command -v od >/dev/null || needs="od" command -v awk >/dev/null || needs="${needs:+$needs and }awk" if [ -n "$needs" ]; then echo "Error: can't read the Cursor CLI's switch record $path: reading it needs $needs. Install $needs and run this again, or delete $path if the Cursor CLI is no longer installed." >&2 else echo "Error: can't read the Cursor CLI's switch record $path. Run this again, or delete $path if the Cursor CLI is no longer installed." >&2 fi exit 1 ;; esac fi # A later read, immediately before the names are replaced, doesn't repeat the last note printed. if [ -n "$why" ] && [ "$why" != "${cursor_noted:-}" ]; then echo "Note: ignoring the Cursor CLI's switch record: $why." >&2 cursor_noted=$why fi return 1 } # Selects FEED's channel in config.toml and defines it as CHANNEL_TABLE, so `grok update` updates # this build, and records the standard installer as install.sh does: replaces `[cli] channel` and # `installer` and every `[cli.channels.unified]` and `[cli.channels.unified-alpha]` table, so a # switch between them leaves one definition, and keeps every other line. Like Grok's own writes, it # follows a linked config.toml and keeps its mode. # Returns 2, changing nothing, when the file isn't TOML this can edit, such as one that sets those # keys with dotted keys or inline tables, and 1 when it can't read or write the file. record_channel() { local file="$CONFIG_FILE" target hops=0 input=/dev/null status=0 while [ -L "$file" ]; do hops=$((hops + 1)) target=$(readlink "$file") && [ "$hops" -le 40 ] || return 1 case "$target" in /*) file=$target ;; *) file="$(dirname "$file")/$target" ;; esac done if [ -e "$file" ]; then [ -f "$file" ] && cp -p "$file" "$file.new.$$" || { rm -f "$file.new.$$"; return 1; } input=$file fi # A line-by-line reading of TOML: enough to tell table headers and keys from the lines of a # multi-line string or array. Exits 3 for a file it can't read that way. CHANNEL_TABLE="$CHANNEL_TABLE" LC_ALL=C awk -v q="'" -v feed="$FEED" ' BEGIN { at = "root"; open = ""; depth = 0; n = 0; np = 0; def = 0; cli = 0 } function blank(c) { return c == " " || c == "\t" } function is_feed(name) { return name == "unified" || name == "unified-alpha" } # A `[cli.channels.unified]` or `[cli.channels.unified-alpha]` table, or one under it function feed_table(k, p, end) { p = "cli" SUBSEP "channels" SUBSEP if (index(k, p) != 1) return 0 k = substr(k, length(p) + 1) if ((end = index(k, SUBSEP))) k = substr(k, 1, end - 1) return is_feed(k) } # The dotted key at s[i]: its parts joined by SUBSEP in key, the first in first, and i just # past it and the blanks after it. 0 when there is none. function read_key(s, c, part, parts, j) { key = ""; parts = 0 for (;;) { while (blank(substr(s, i, 1))) i++ c = substr(s, i, 1) if (c == "\"") { part = "" for (j = i + 1; j <= length(s) && (c = substr(s, j, 1)) != "\""; j++) { if (c == "\\") { part = part c; c = substr(s, ++j, 1) } part = part c } if (j > length(s)) return 0 i = j + 1 } else if (c == q) { j = index(substr(s, i + 1), q) if (!j) return 0 part = substr(s, i + 1, j - 1) i += j + 1 } else if (match(substr(s, i), /^[A-Za-z0-9_-]+/)) { part = substr(s, i, RLENGTH) i += RLENGTH } else return 0 if (!parts++) { key = part; first = part } else key = key SUBSEP part while (blank(substr(s, i, 1))) i++ if (substr(s, i, 1) != ".") return 1 i++ } } # Follows a value from s[i] to the end of the line: open is the multi-line string it leaves # open, and depth the arrays and inline tables. function scan(s, i, c, j, len) { len = length(s) while (i <= len) { c = substr(s, i, 1) if (open != "") { if (open == "\"\"\"" && c == "\\") i += 2 else if (substr(s, i, 3) == open) { open = ""; i += 3 } else i++ } else if (c == "#") return else if (substr(s, i, 3) == "\"\"\"" || substr(s, i, 3) == q q q) { open = substr(s, i, 3); i += 3 } else if (c == "\"") { for (i++; i <= len && (c = substr(s, i, 1)) != "\""; i++) if (c == "\\") i++ i++ } else if (c == q) { j = index(substr(s, i + 1), q) i = j ? i + j + 1 : len + 1 } else { if (c == "[" || c == "{") depth++ else if ((c == "]" || c == "}") && depth) depth-- i++ } } } # The rest of a value goes where the line that opened it went open != "" || depth { scan($0, 1) if (!dropping) out[++n] = $0 next } { dropping = 0 s = $0 while (blank(substr(s, 1, 1))) s = substr(s, 2) c = substr(s, 1, 1) } # Comments and blank lines after a dropped table may open the next one s == "" || c == "#" { if (at == "unified") pend[++np] = $0 else out[++n] = $0 next } c == "[" { array = substr(s, 2, 1) == "[" i = array ? 3 : 2 if (!read_key(s) || substr(s, i, array ? 2 : 1) != (array ? "]]" : "]")) { bad = 1; exit } if (feed_table(key)) { if (!def) def = ++n at = "unified"; np = 0 next } if (array && (key == "cli" || key == "cli" SUBSEP "channels")) { bad = 1; exit } for (k = 1; k <= np; k++) out[++n] = pend[k] np = 0 out[++n] = $0 if (key == "cli") { at = "cli"; cli = 1 out[++n] = "channel = \"" feed "\"" out[++n] = "installer = \"internal\"" } else if (key == "cli" SUBSEP "channels") at = "channels" else at = "other" next } { i = 1 if (!read_key(s) || substr(s, i, 1) != "=") { bad = 1; exit } scan(s, i + 1) if (at == "unified" || (at == "cli" && (key == "channel" || key == "installer"))) { dropping = 1; np = 0; next } if ((at == "root" && first == "cli") || (at == "cli" && first == "channels") || (at == "channels" && is_feed(first))) { bad = 1; exit } out[++n] = $0 } END { if (bad || open != "" || depth) exit 3 for (k = 1; k <= np; k++) out[++n] = pend[k] if (!def) { if (n && out[n] ~ /[^ \t]/) out[++n] = "" def = ++n } for (k = 1; k <= n; k++) { if (k != def) print out[k] else { if (!cli) print "[cli]\nchannel = \"" feed "\"\ninstaller = \"internal\"\n" print ENVIRON["CHANNEL_TABLE"] } } } ' "$input" >"$file.new.$$" || status=$? if [ "$status" -eq 0 ] && mv -f "$file.new.$$" "$file"; then return 0; fi rm -f "$file.new.$$" if [ "$status" -eq 3 ]; then return 2; fi return 1 } # The probe doubles as the pointer fetch, as in install.sh: x.ai first, then GCS directly. A pinned # version reads nothing from the pointer, so it installs even when the pointer is missing or bad. if [ -z "$TARGET" ]; then echo "Fetching latest ${CHANNEL} version..." >&2; fi BASE_URL="$BASE_URL_PRIMARY" latest=$(download_file "${BASE_URL}/latest" 2>/dev/null) || latest="" if [ -z "$latest" ] && [ "$BASE_URL_FALLBACK" != "$BASE_URL_PRIMARY" ]; then echo "Note: ${BASE_URL_PRIMARY} unreachable, falling back to direct GCS." >&2 BASE_URL="$BASE_URL_FALLBACK" latest=$(download_file "${BASE_URL}/latest" 2>/dev/null) || latest="" fi latest=$(printf '%s' "$latest" | tr -d '\r' | head -n1 | tr -d '[:space:]') [[ "$latest" =~ $SEMVER ]] || latest="" [ -n "$TARGET" ] || [ -n "$latest" ] \ || die "could not read the latest version from ${BASE_URL_PRIMARY}/latest or ${BASE_URL_FALLBACK}/latest" version="${TARGET:-$latest}" object="agent-host-${version}-${platform}" echo "Installing Grok $version ($platform)..." >&2 echo " Downloading grok ${version}..." >&2 work=$(mktemp -d "${TMPDIR:-/tmp}/grok-unified-install.XXXXXX") trap 'rm -rf "$work"' EXIT download_file "${BASE_URL}/${object}.sha256" "$work/$object.sha256" 2>/dev/null \ || die "$version has no $platform build at $BASE_URL (no ${object}.sha256)" want=$(cut -d' ' -f1 <"$work/$object.sha256" | tr -d '[:space:]') [ -n "$want" ] || die "${object}.sha256 is empty" fetch_binary "${BASE_URL}/${object}" "$work/$object" || die "could not download ${BASE_URL}/${object}" got=$(sha256_of "$work/$object") || die "neither sha256sum nor shasum is installed, so the download cannot be checked" [ "$want" = "$got" ] || die "$object is $got, but its .sha256 says $want; not installing" chmod 0755 "$work/$object" if [ "$os" = "macos" ]; then xattr -d com.apple.quarantine "$work/$object" 2>/dev/null || true; fi # `grok X.Y.Z ()`, or `grok X.Y.Z (, public )` for a build numbered # apart from its public release installed=$("$work/$object" --version /dev/null | head -1 | awk '{print $2}') || installed="" [ -n "$installed" ] || die "$object does not run on this machine; keeping the existing install" [ "$installed" = "$version" ] || die "$object runs as $installed, not $version; not installing" mkdir -p "$DOWNLOAD_DIR" "$BIN_DIR" install -m 0755 "$work/$object" "$DOWNLOAD_DIR/$object.new.$$" mv -f "$DOWNLOAD_DIR/$object.new.$$" "$DOWNLOAD_DIR/$object" # The updater's layout: a relative link survives a bind mount with another $HOME. ln -s "../downloads/$object" "$BIN_DIR/agent-host.new.$$" mv -f "$BIN_DIR/agent-host.new.$$" "$BIN_DIR/agent-host" mkdir -p "$LINK_DIR" ln -s "$BIN_DIR/agent-host" "$LINK_DIR/agent-host.new.$$" mv -f "$LINK_DIR/agent-host.new.$$" "$LINK_DIR/agent-host" # grok and agent run this build in place of a stock grok's: a sibling link, which the build reads # as its own install and so never hands back to the stock updater. cursor_names="" channel_note="" cursor_agent_note="" if cursor_owns_names; then cursor_names=1; fi if [ -z "$cursor_names" ]; then # A Cursor editor's `cursor agent` shim runs cursor-agent, which this build answers to. Only a # free name is taken, and not under a switch, whose hint runs the Cursor CLI's cursor-agent. The # test keeps ln from linking inside a directory of that name. if [ ! -e "$LINK_DIR/cursor-agent" ] && [ ! -L "$LINK_DIR/cursor-agent" ] && ln -s agent-host "$LINK_DIR/cursor-agent" 2>/dev/null; then cursor_agent_note=" Linked cursor-agent to this build, for the Cursor editor's 'cursor agent'." fi for name in grok agent; do rm -f "$BIN_DIR/$name.new.$$" "$LINK_DIR/$name.new.$$" ln -s agent-host "$BIN_DIR/$name.new.$$" mv -f "$BIN_DIR/$name.new.$$" "$BIN_DIR/$name" ln -s "$BIN_DIR/$name" "$LINK_DIR/$name.new.$$" mv -f "$LINK_DIR/$name.new.$$" "$LINK_DIR/$name" done # `grok update` keeps this build only on a channel config selects and defines: without the # definition it refuses, and without the selection it puts the public build back. if record_channel; then channel_note=" Selected the $FEED channel in $CONFIG_FILE, so 'grok update' updates this build." else case $? in 2) why="this installer can't edit it safely" ;; *) why="this installer couldn't write it" ;; esac channel_note="Note: left $CONFIG_FILE as it was: $why. Until it selects and defines the $FEED channel as below, 'grok update' can't update this build: [cli] channel = \"$FEED\" installer = \"internal\" $(printf '%s\n' "$CHANNEL_TABLE" | sed 's/^/ /')" fi fi echo " Unpacking the agent-host runtime..." >&2 "$DOWNLOAD_DIR/$object" local-admission unpack /dev/null 2>&1 \ || echo " Could not unpack the agent-host runtime now; the first launch will." >&2 # Writes the compile cache the daemon reads at its first start; without it, that start compiles. "$DOWNLOAD_DIR/$object" agent-host warm /dev/null 2>&1 || true path_note="" case ":$PATH:" in *":$LINK_DIR:"*) ;; *) case "$(basename "${SHELL:-}")" in zsh) rc="$HOME/.zshrc" line='export PATH="$HOME/.local/bin:$PATH"' ;; bash) if [ "$os" = "macos" ]; then rc="$HOME/.bash_profile"; else rc="$HOME/.bashrc"; fi line='export PATH="$HOME/.local/bin:$PATH"' ;; fish) rc="$HOME/.config/fish/config.fish" line='fish_add_path $HOME/.local/bin' ;; *) rc="$HOME/.profile" line='export PATH="$HOME/.local/bin:$PATH"' ;; esac # The marker the private installer writes too, so the line is added once. marker='# agent-host: ~/.local/bin on PATH' if ! grep -qF "$marker" "$rc" 2>/dev/null; then mkdir -p "$(dirname "$rc")" printf '\n%s\n%s\n' "$marker" "$line" >>"$rc" fi path_note=" Updated $LINK_DIR in PATH in $rc." ;; esac if [ -n "$cursor_names" ]; then echo "Grok $version installed to $BIN_DIR/agent-host" >&2 echo " Left grok and agent in $BIN_DIR and $LINK_DIR to the Cursor CLI, which switched them to agent-host." >&2 echo " To take them back, run 'cursor-agent update --channel cursor', or delete $(cursor_config_dir)/grok-switch.json if the Cursor CLI is no longer installed." >&2 else echo " Binary linked to $BIN_DIR/grok and $BIN_DIR/agent." >&2 echo "Grok $version installed to $BIN_DIR/grok" >&2 echo " Symlinked $LINK_DIR/grok -> $BIN_DIR/grok" >&2 echo " Symlinked $LINK_DIR/agent -> $BIN_DIR/agent" >&2 if [ -n "$cursor_agent_note" ]; then echo "$cursor_agent_note" >&2; fi echo "$channel_note" >&2 fi if [ -n "$path_note" ]; then echo "$path_note" >&2; fi echo "" >&2 if [ -n "$path_note" ]; then echo "Restart your terminal, then run 'grok' or 'agent' to get started!" >&2 else echo "Run 'grok' or 'agent' to get started!" >&2 fi