Files
loki/scripts/hetzner/install-builder-agents.sh
CatoandClaude Opus 5.5 43e1782b22 fix(builder): five agent sessions at once, not eight; idle sealed sessions can be stopped to free memory (#1687)
* docs(handoff): #1678 merged; the box still drops requests and Loki's deploys do not land

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* docs(handoff): memory ceiling set at 10 GiB; Gemini spending cap in OrangeCat #1672

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): five agent sessions at once, not eight; idle sealed sessions can be stopped to free memory

Measured on the box on 2026-11-12: a sealed session holds about 1 GiB, not
the 0.4 GB the cap of eight was sized at. With seven or eight open the
builder held 6.3 to 8.3 GiB, the box had 0.6 GiB left, swap was full, and
the kernel had stopped 37 processes for memory, live sites and the gateway
among them.

- AGENT_MAX_OPEN_DEFAULT is 5. LOKI_AGENT_MAX_OPEN raises it where there is
  room.
- sealed-builds-ctl.sh stop-idle (sealed-builds.yml): stops the sealed
  sessions that relayed less than 512 bytes over two minutes, the line the
  idle rule already uses for "no work". A working agent prints far more
  (its spinner alone). runs=dry lists without stopping. idle_sessions is
  pinned by scripts/hetzner/test-sealed-builds-memory.sh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(spend): the platform's Gemini key is not used where nothing caps it

The audit after the Gemini spend (2026-11-12) found the same platform key in
three places with no cap that refuses a call:

- Loki's own chat chain called Google whenever GEMINI_API_KEY was in its
  env. Google is left out of CHAT_CHAIN (UNCAPPED_PAID_PROVIDERS) until Loki
  has a cap that refuses, like OrangeCat's reserve_vendor_call.
- install-builder-agents.sh copied OrangeCat's key into the builder's env
  for the Antigravity CLI. An agent CLI cannot be capped in code; the
  builder now uses Gemini only with a key of its own, from a Google project
  with a per-day request quota.
- openclaw-media.yml fell back to OrangeCat's key for the Telegram gateway's
  image and video reading. No fallback: with no key of its own, media
  understanding stays off.

Keys already copied on the box stop mattering when the platform key is
replaced; the new key is never copied anywhere.

scripts/test/chat-chain.ts pins all three.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): stop-idle stops only sessions whose project has no open run

Bytes are not the signal: on 2026-11-12 eight sealed sessions at their
prompt each relayed 3 to 10 KB a minute through podman, so a 512-byte line
would have stopped none, and an agent waiting on CI prints nothing at all.
A session whose project (or lane) has no open run in Loki's database is
between tasks; one that does is kept, and so is any session under fifteen
minutes old. between_tasks is pinned by test-sealed-builds-memory.sh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* fix(builder): between_tasks reads its list without a pipe into grep -q

scripts/test/no-grep-q-in-a-pipe.ts: grep -q exits at the first match and
can cut off the writer. A here-string has no writer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

* docs(handoff): the box recovered; spend caps; the owner's key steps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 16:39:19 +01:00

90 lines
4.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Box-side (run as root): give the cloud builder more than one engine.
#
# Until 2026-10-21 the builder had only Claude Code and Codex, so when both hit
# their quota the failover from #1367 had nowhere to go. This installs Cursor
# (cursor-agent), Grok and Antigravity (gemini) for the runner user into
# ~/.local/bin — already first on the runner unit's PATH, which is all the
# runner needs to report them installed (src/lib/agents/*.ts detectAvailable).
#
# Keys are left to the operator: GEMINI_API_KEY, XAI_API_KEY, CURSOR_API_KEY.
# The platform's Gemini key is NOT copied in. It used to be, from OrangeCat's
# env, and on 2026-11-12 that key spent with no cap until the API was switched
# off. An agent CLI cannot be capped in code, so the builder gets Gemini only
# through a key of its own in a Google project with a per-day request quota
# (the vendor refuses past it: the only hard cap a CLI can have).
# NO secret value is ever printed — this repo's Action logs are public.
#
# Usage: install-builder-agents.sh [--no-restart]
set -uo pipefail
RUNNER_USER="${RUNNER_USER:-ubuntu}"
RUNNER_HOME="/home/$RUNNER_USER"
RUNNER_ENV="${RUNNER_ENV:-/opt/loki/runner/.env}"
BIN="$RUNNER_HOME/.local/bin"
as_user() { sudo -u "$RUNNER_USER" -H env PATH="$BIN:/usr/local/bin:/usr/bin:/bin" bash -lc "$1" </dev/null; }
# key_present <file> <NAME> — set and non-empty, without reading it out.
key_present() { [ -r "$1" ] && grep -Eq "^(export )?$2=['\"]?[^'\"[:space:]]+" "$1"; }
if [ "${1:-}" != "--probe-only" ]; then
as_user "mkdir -p '$BIN'"
echo "→ Antigravity (gemini)"
as_user "npm install -g --prefix \$HOME/.local @google/gemini-cli >/dev/null 2>&1" || echo " ⚠ gemini install failed"
echo "→ Cursor (cursor-agent)"
as_user "curl -fsS https://cursor.com/install | bash >/dev/null 2>&1" || echo " ⚠ cursor install failed"
echo "→ Grok"
as_user "curl -fsSL https://x.ai/cli/install.sh | GROK_BIN_DIR=\$HOME/.local/bin bash >/dev/null 2>&1" || echo " ⚠ grok install failed"
if key_present "$RUNNER_ENV" GEMINI_API_KEY; then
echo "✓ GEMINI_API_KEY in runner env: it must be the builder's own key, with a daily quota in its Google project"
else
echo "Antigravity installed without a key: put the builder's own quota-capped key in $RUNNER_ENV to use it"
fi
fi
echo "── report"
for a in claude codex cursor-agent grok gemini; do
if [ -x "$BIN/$a" ] || as_user "command -v $a" >/dev/null 2>&1; then
v="$(as_user "timeout 20 $a --version 2>/dev/null | head -1" || true)"; echo "installed $a ${v}"
else echo "missing $a"; fi
done
for k in GEMINI_API_KEY XAI_API_KEY CURSOR_API_KEY; do
key_present "$RUNNER_ENV" "$k" && echo "key set $k" || echo "key unset $k"
done
# Usable = the model actually answers with the runner's own key.
if key_present "$RUNNER_ENV" GEMINI_API_KEY; then
# The env file is root-only (systemd reads it as root), so root loads it and
# hands the runner user just that one variable — as the runner itself does.
out="$( (set -a; . "$RUNNER_ENV"; set +a; cd /tmp && sudo -u "$RUNNER_USER" -H --preserve-env=GEMINI_API_KEY env PATH="$BIN:/usr/local/bin:/usr/bin:/bin" timeout 120 gemini --skip-trust -p 'Reply with exactly: PONG' </dev/null 2>&1) | tail -15)"
if case "$out" in *PONG*) true ;; *) false ;; esac; then echo "probe gemini ok"
else
# Show WHY, with any long token-looking string masked (public logs).
echo "probe gemini FAILED:"
printf '%s\n' "$out" | tr -d '\r' | sed -E 's/[A-Za-z0-9_-]{24,}/<masked>/g' | cut -c1-300 | sed 's/^/ /'
fi
fi
# What the RUNNER itself sees (its failover reads this, not the probe above):
# names on its PATH/env and its cached provider probes (secrets masked).
pid="$(systemctl show -p MainPID --value loki-box-runner 2>/dev/null)"
if [ -n "$pid" ] && [ "$pid" != 0 ] && [ -r "/proc/$pid/environ" ]; then
echo "── runner process (pid $pid)"
tr '\0' '\n' < "/proc/$pid/environ" | grep -E '^PATH=' | sed 's/^/ /'
for k in GEMINI_API_KEY GOOGLE_API_KEY XAI_API_KEY CURSOR_API_KEY HOME; do
tr '\0' '\n' < "/proc/$pid/environ" | grep -q "^$k=." && echo " env set $k" || echo " env unset $k"
done
fi
echo "── runner provider-probes.json"
cat "$RUNNER_HOME/.loki/provider-probes.json" 2>/dev/null | sed -E 's/[A-Za-z0-9_-]{24,}/<masked>/g' | tr ',' '\n' | sed 's/^/ /' || echo " (none)"
echo
case "${1:-}" in --no-restart|--probe-only) exit 0 ;; esac
echo "── scheduling drain-then-restart of runner units"
for u in loki-box-runner $(systemctl list-units 'loki-box-runner@*' --no-legend --plain 2>/dev/null | awk '{print $1}' | sed 's/\.service$//'); do
r="loki-runner-restart-${u//@/-}"
systemctl stop "$r.service" 2>/dev/null || true
systemctl reset-failed "$r.service" 2>/dev/null || true
systemd-run --unit="$r" --collect --quiet /bin/bash /opt/loki/runner/scripts/drain-and-restart-runner.sh "$u" && echo "✓ $u restart scheduled"
done