#!/usr/bin/env bash
# Standalone npm 12/Grok reproduction. Requires Linux, Node and bootstrap npm.
# All installs and Grok state remain inside a disposable directory.
set -euo pipefail
root=$(mktemp -d /tmp/grok-npm12-repro.XXXXXX)
trap 'rm -rf "$root"' EXIT
bootstrap_npm=$(command -v npm)
node_dir=$(dirname "$(command -v node)")
mkdir -p "$root/home" "$root/prefix" "$root/cache" "$root/grokhome"
export HOME="$root/home" GROK_HOME="$root/grokhome" npm_config_cache="$root/cache"
unset npm_config_allow_scripts NPM_CONFIG_ALLOW_SCRIPTS NODE_AUTH_TOKEN NPM_TOKEN XAI_API_KEY
"$bootstrap_npm" install --prefix "$root/npm12" --ignore-scripts --no-audit --no-fund npm@12.2.0
export PATH="$root/prefix/bin:$root/npm12/node_modules/.bin:$node_dir:/usr/bin:/bin"
export npm_config_prefix="$root/prefix"
printf '\n=== toolchain ===\n'
node --version
npm --version
npm install -g @xai-official/grok@1.0.56 --allow-scripts=@xai-official/grok --no-audit --no-fund
printf '\n=== seed ===\n'
grok --version
printf '\n=== baseline: exits zero, binary stays old ===\n'
grok update
printf 'update_exit=%s\n' "$?"
node -p 'require(process.env.npm_config_prefix+"/lib/node_modules/@xai-official/grok/package.json").version'
grok --version
grok --version | head -1 | grep -F '1.0.56'
printf '\n=== control: same environment with PR allowlist ===\n'
npm_config_allow_scripts=@xai-official/grok grok update
printf 'update_exit=%s\n' "$?"
node -p 'require(process.env.npm_config_prefix+"/lib/node_modules/@xai-official/grok/package.json").version'
grok --version
grok --version | head -1 | grep -F '1.0.70'
