Files
alzeo/switch-model.sh
T
Hasan BasbunarandClaude Opus 5.5 fd4c8ba913 Kolibri lane: the key on every route; installer, switch and uninstall hardened
vLLM 0.29 asks --api-key only under /v1, /v2, /inference and /cohere: /invocations
(SageMaker's route, which runs chat), /tokenize and /detokenize answered without it on
the engine's port (read in the image's source). vllm_engine_auth.RequireKey, loaded
with --middleware from a read-only mount, asks it everywhere but /health and /metrics,
as the SGLang lanes do.

Also found in review, each with a test that fails before: the installer reads the
journal whole before it looks for the key (piped into grep -q under pipefail, a long
journal read "not found"), follows a symlinked model cache to its disk, removes older
images once the unit names the new one, and proves the tool call at effort none (the
reasoning parser misses 1.9 to 3.5 % of such calls at high). --uninstall takes the
kolibri provider out of opencode (oc-merge-limits.py --remove-provider) and points a
default that named it at the text lane before. A switch takes a cached checkpoint only
when every shard its index names is there, and sizes opencode's compaction from
Kolibri's window, as install.sh now does when Kolibri boots the box. The engine
preflight no longer takes any command line holding "vllm/" for an engine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 06:16:59 +02:00

1144 lines
70 KiB
Bash
Executable File

#!/usr/bin/env bash
# Surgical target-model switch on a live install, between these targets:
#
# ./switch-model.sh stock # RadixArk/Qwen3.8-27B-NVFP4 (SGLang)
# ./switch-model.sh uncensored # edp1096/Huihui-...-abliterated-NVFP4 (SGLang)
# ./switch-model.sh fp8 # Qwen/Qwen3.8-27B-FP8 (SGLang)
# ./switch-model.sh uncensored-fp8 # edp1096/Huihui-...-abliterated-FP8 (SGLang)
# ./switch-model.sh flash # RadixArk/Qwen3.8-Flash-Next-NVFP4 (SGLang)
# ./switch-model.sh flash-nvda # nvidia/Qwen3.8-Flash-Next-NVFP4, ModelOpt mixed precision
# ./switch-model.sh flash-uncensored # the abliterated build of the same tree
# ./switch-model.sh image # Qwen/Qwen-Image-2.1 (SGLang Diffusion, own venv)
# ./switch-model.sh image-turbo # Qwen/Qwen-Image-2.1-Turbo, its eight-step distillation
# ./switch-model.sh video # MiniMaxAI/MiniMax-H3 (SGLang Diffusion, own venv)
# ./switch-model.sh kolibri # Aleph-Alpha/Kolibri-1, the official FP8 (vLLM + Aleph Alpha's plugin)
# ./switch-model.sh kolibri-nvfp4 # Quantinine/Kolibri-1-NVFP4A16-FP8, its experts in NVFP4
#
# Within the 27B lane (stock, uncensored, fp8, uncensored-fp8) it does what it
# always did:
# 1. downloads the checkpoint into $HF_CACHE (resumable, pinned revision);
# 2. applies the 1M YaRN config patch ONLY if the installed unit uses it;
# 3. regenerates the patched chat template from the target's own snapshot;
# 4. rewrites ONLY --model-path/--revision in the qwen38-sglang unit.
#
# Across lanes (27B <-> flash) both stacks must already be installed once
# (each by its own ./install.sh run: the switch is surgical, it does not
# download images or build overlays). It then re-verifies the checkpoint,
# regenerates the target's template, flips which unit is enabled at boot,
# and points the opencode default model at the target.
#
# The DFlash2 drafter (27B) and the NEXTN/MTP head (flash) are lossless speculative
# paths: drafts are verified against the target, quality is the target's own.
# The switch takes effect on the engine's NEXT restart: this script never starts, stops
# or restarts an engine, and prints the exact commands instead. The services around it: it
# restarts opencode-web when the limits it reads change (a switch across lanes), and, on a
# box whose proxy unit is from before v1.18.7, the proxy, the old way of moving its
# ceiling (since v1.18.7 the ceiling follows the lane that serves). For the 27B lane it
# rewrites --model-path, --revision and the KV cache line of the unit.
set -euo pipefail
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
die() { printf '\n\033[1;31mERROR:\033[0m %s\n' "$*" >&2; exit 1; }
# A download says its bytes every 10 s (hf-progress.py), and with no terminal on stdout (the
# cockpit's Load runs this as a job) snapshot_download's own bars go: they count files, a
# 53 GB safetensors the same as a 2 KB config, and in a job each redraw is one more line.
DL_NO_BARS=""; [ -t 1 ] || DL_NO_BARS=1
_ENV_HF_CACHE="${HF_CACHE:-}" # before the pins below give it install.sh's default
# No target, no switch: run bare, this used to switch the box to stock, re-enabling the
# 27B lane and restarting what follows it, where the usage was all anyone wanted (found
# in review, 2026-09-24). MODEL_CHOICE= still names one, as install.sh takes it.
CHOICE="${1:-${MODEL_CHOICE:-}}"
case "$CHOICE" in stock|uncensored|fp8|uncensored-fp8|flash|flash-nvda|flash-uncensored|image|image-turbo|video|kolibri|kolibri-nvfp4) ;; *) printf 'usage: ./switch-model.sh <stock|uncensored|fp8|uncensored-fp8|flash|flash-nvda|flash-uncensored|image|image-turbo|video|kolibri|kolibri-nvfp4>\n' >&2; [ -z "$CHOICE" ] && exit 2; die "unknown target: $CHOICE" ;; esac
PINS="$(grep -E '^(IMAGE|STOCK_REPO|STOCK_REV|UNC_REPO|UNC_REV|FP8_REPO|FP8_REV|UNCFP8_REPO|UNCFP8_REV|FLASH_REPO|FLASH_REV|FLASH_NVDA_REPO|FLASH_NVDA_REV|FLASH_UNC_REPO|FLASH_UNC_REV|FLASH_IMAGE|FLASH_SERVE_IMAGE|SERVE_IMAGE|MODEL_CHOICE|HF_CACHE|CONFIG_DIR)=' "$REPO_DIR/install.sh" || true)"
# A count of matched lines was the old check, and adding a pin broke both scripts
# at once (it did, on 2026-09-08). What matters is not how many lines matched but
# whether every name this script goes on to use is defined, so that is what is
# asserted, and a failure says which one.
eval "$PINS"
for _v in IMAGE SERVE_IMAGE STOCK_REPO STOCK_REV UNC_REPO UNC_REV FP8_REPO FP8_REV UNCFP8_REPO UNCFP8_REV FLASH_REPO FLASH_REV FLASH_NVDA_REPO FLASH_NVDA_REV FLASH_UNC_REPO FLASH_UNC_REV FLASH_IMAGE FLASH_SERVE_IMAGE MODEL_CHOICE HF_CACHE CONFIG_DIR; do
eval "[ -n \"\${$_v:-}\" ]" || die "install.sh no longer defines $_v (repo layout changed?)"
done
unset _v
IMAGE_UNIT="/etc/systemd/system/qwen38-image.service"
IMAGE_UNIT_NAME="qwen38-image.service"
# The image unit is rewritten between its two checkpoints (image, image-turbo) with sudo,
# so it is staged at a fixed path the cockpit's exact-argv sudoers allowlist names, as the
# 27B unit is below.
IMG_STAGE="$CONFIG_DIR/qwen38-image.service.switch-stage"
VIDEO_UNIT="/etc/systemd/system/qwen38-video.service"
VIDEO_UNIT_NAME="qwen38-video.service"
# Free GB under a path's filesystem, whole numbers, "" when df cannot answer. Every
# branch's room check reads through this, so it lives before the branches run.
dl_free_gb(){ local p="${1:-$HF_CACHE}"; while [ ! -e "$p" ]; do p="$(dirname "$p")"; done
{ df -BG --output=avail "$p" 2>/dev/null || true; } | tail -1 | tr -dc '0-9'; }
# qwen38-llamacpp.service is the fifth serving unit: no installer here enables it,
# but every lane template lists it in Conflicts=, so a box where an operator enabled
# it by hand would boot it beside the lane just switched to, one of the two boot
# jobs quietly removed from the transaction (found in the branch's reviews,
# 2026-09-28). Disabling it keeps "exactly one serving unit enabled" true; a box
# with an older sudoers file refuses that one line, and the switch must not die for
# it, because the Conflicts= line already loses it the boot either way.
disable_rollback_lane(){
if [ -f /etc/systemd/system/qwen38-llamacpp.service ] && systemctl is-enabled --quiet qwen38-llamacpp.service 2>/dev/null; then
echo "disabling qwen38-llamacpp.service at boot (unit file kept)"
sudo systemctl disable qwen38-llamacpp.service \
|| echo " (not disabled: this box's sudoers file predates the line; the boot conflict resolves against it anyway)"
fi
}
# ── The Kolibri lane: a text lane on vLLM, switched to the way the image lane is ────
# For the proxy and its clients Kolibri is a text lane like the 27B and the flash: the same
# port, the same key. But nothing in the four hundred lines below is about it: they rewrite
# SGLang units, patch Qwen chat templates and move Qwen prompt ceilings. So it is handled
# here, whole, and exits, with the image lane's contract: verify the checkpoint (the first
# switch to a variant fetches it), name it in the unit, make it the one unit enabled at
# boot, never start or stop anything, print the exact commands.
KOLIBRI_UNIT="/etc/systemd/system/qwen38-kolibri.service"
KOLIBRI_UNIT_NAME="qwen38-kolibri.service"
# Rewritten with sudo between its two checkpoints, so staged at a fixed path the cockpit's
# exact-argv sudoers allowlist names, as the image unit is.
KOL_STAGE="$CONFIG_DIR/qwen38-kolibri.service.switch-stage"
if [ "$CHOICE" = "kolibri" ] || [ "$CHOICE" = "kolibri-nvfp4" ]; then
[ -f "$KOLIBRI_UNIT" ] || die "the Kolibri lane is not installed on this box (no $KOLIBRI_UNIT). Install it once: ./install.sh --with-kolibri (about 102 GB)"
# The lane's two checkpoints and their pinned revisions, from install-kolibri.sh, the one
# place they are declared.
eval "$(grep -E '^(KOLIBRI_FP8_REPO|KOLIBRI_FP8_REV|KOLIBRI_NVFP4_REPO|KOLIBRI_NVFP4_REV)=' "$REPO_DIR/install-kolibri.sh" || true)"
for _v in KOLIBRI_FP8_REPO KOLIBRI_FP8_REV KOLIBRI_NVFP4_REPO KOLIBRI_NVFP4_REV; do
eval "[ -n \"\${$_v:-}\" ]" || die "install-kolibri.sh no longer defines $_v (repo layout changed?)"
done
unset _v
# Each checkpoint's memory share, the same as install-kolibri.sh's (it says why).
if [ "$CHOICE" = "kolibri-nvfp4" ]; then
KOL_MODEL="$KOLIBRI_NVFP4_REPO"; KOL_REV="$KOLIBRI_NVFP4_REV"; KOL_GMU=0.55; KOL_NEED_GB=46
else
KOL_MODEL="$KOLIBRI_FP8_REPO"; KOL_REV="$KOLIBRI_FP8_REV"; KOL_GMU=0.80; KOL_NEED_GB=79
fi
KOL_IMAGE="$(grep -m1 -oE 'qwen38-kolibri:[^ ]+' "$KOLIBRI_UNIT" || true)"
[ -n "$KOL_IMAGE" ] && docker image inspect "$KOL_IMAGE" >/dev/null 2>&1 \
|| die "the Kolibri lane's image (${KOL_IMAGE:-none named in its unit}) is missing: re-run ./install-kolibri.sh --no-smoke, it builds it again"
KOL_HF="$(grep -m1 -oE -- '-v [^ :]+:/root/\.cache/huggingface' "$KOLIBRI_UNIT" | sed -e 's/^-v //' -e 's|:/root/\.cache/huggingface$||' || true)"
KOL_HF="${KOL_HF:-$HF_CACHE}"
printf '\n\033[1;36m── Verifying %s @ %s in the cache (resumable)\033[0m\n' "$KOL_MODEL" "${KOL_REV:0:12}"
KOL_TOKEN_ARGS=()
# by name: docker takes the value from its own environment, so the token is in no argv
if [ -n "${HF_TOKEN:-}" ]; then export HF_TOKEN; KOL_TOKEN_ARGS=(-e HF_TOKEN); fi
KOL_PROGRESS_ARGS=()
if [ -f "$REPO_DIR/hf-progress.py" ]; then
KOL_PROGRESS_ARGS=(-v "$REPO_DIR/hf-progress.py:/opt/qwen38/hf-progress.py:ro" -e QWEN38_REPO_DIR=/opt/qwen38)
fi
# In the lane's own image, as the text lanes fetch theirs in their serving image; --init
# for the same reason as below: a cockpit job stopped past its timeout stops this too.
docker run --rm -i --init --network host --user "$(id -u):$(id -g)" --entrypoint python3 \
-e HF_HOME=/hf -e HF_HUB_DOWNLOAD_TIMEOUT=30 -e HF_HUB_DISABLE_XET=1 \
-e HF_HUB_DISABLE_PROGRESS_BARS="$DL_NO_BARS" \
-e KOL_MODEL="$KOL_MODEL" -e KOL_REV="$KOL_REV" \
-e KOL_NEED_GB="$KOL_NEED_GB" -e KOL_FREE_GB="$(dl_free_gb "$KOL_HF")" \
"${KOL_TOKEN_ARGS[@]}" "${KOL_PROGRESS_ARGS[@]}" \
-v "$KOL_HF":/hf "$KOL_IMAGE" - <<'PYKOL' || die "the Kolibri checkpoint could not be verified or fetched (re-run to resume; set HF_TOKEN if it stalls)"
import contextlib
import importlib.util
import json
import os
import time
from huggingface_hub import constants, snapshot_download
def reporting(*args, **kwargs):
"""hf-progress.py's byte count while the rest comes; a progress line is never worth a download."""
try:
spec = importlib.util.spec_from_file_location(
"hf_progress", os.path.join(os.environ["QWEN38_REPO_DIR"], "hf-progress.py"))
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod.reporting(*args, **kwargs)
except Exception:
return contextlib.nullcontext()
def complete(path):
"""Every shard the index names is in the snapshot (a file appears there only once its
blob is whole), as install.sh's ckpt_cached reads it: huggingface_hub's own offline
check sees a missing file only when it cached the repo's tree, which a download made
with another tool or an older hub never wrote, and vLLM then dies at every start."""
try:
with open(os.path.join(path, "model.safetensors.index.json")) as f:
shards = set(json.load(f)["weight_map"].values())
except (OSError, ValueError, KeyError, TypeError, AttributeError):
shards = {"model.safetensors"}
return bool(shards) and all(os.path.exists(os.path.join(path, s)) for s in shards)
repo, rev = os.environ["KOL_MODEL"], os.environ["KOL_REV"]
try:
path = snapshot_download(repo, revision=rev, local_files_only=True)
if not complete(path):
raise FileNotFoundError(f"a shard the index of {repo} names is missing")
except Exception:
# Refused before the first byte rather than stopped by a full disk halfway. What the
# cache already holds of the repo comes off the need: a fetch that stopped resumes.
blobs = os.path.join(constants.HF_HUB_CACHE, "models--" + repo.replace("/", "--"), "blobs")
have = sum(e.stat().st_size for e in os.scandir(blobs) if e.is_file()) if os.path.isdir(blobs) else 0
free = int(os.environ.get("KOL_FREE_GB") or 0)
need = max(int(os.environ["KOL_NEED_GB"]) - have // 10**9, 1)
if free and free < need:
raise SystemExit(f"{free} GB free in the cache, and {repo} needs about {need} GB more: free some space first")
print("not complete in the cache, fetching the rest", flush=True)
with reporting(repo, revision=rev):
for attempt in range(1, 6): # a new attempt keeps every file already whole
try:
path = snapshot_download(repo, revision=rev)
break
except Exception as e:
if attempt == 5:
raise
print(f"download interrupted ({type(e).__name__}), resuming ({attempt}/5)...", flush=True)
time.sleep(10)
print(path)
PYKOL
# The unit names one checkpoint, its revision and its memory share, on one line each; a
# switch rewrites those and nothing else.
KOL_CUR="$(grep -m1 -oE -- "[^ ]+ --revision [0-9a-f]{40}" "$KOLIBRI_UNIT" || true)"
KOL_CUR_GMU="$(grep -m1 -oE -- "--gpu-memory-utilization [0-9.]+" "$KOLIBRI_UNIT" || true)"
[ -n "$KOL_CUR" ] && [ -n "$KOL_CUR_GMU" ] || die "the Kolibri unit names no checkpoint or memory share where this expects it (hand-edited unit?): re-run ./install-kolibri.sh --no-smoke"
KOL_WANT="$KOL_MODEL --revision $KOL_REV"; KOL_WANT_GMU="--gpu-memory-utilization $KOL_GMU"
KOL_REWRITTEN=""
if [ "$KOL_CUR" != "$KOL_WANT" ] || [ "$KOL_CUR_GMU" != "$KOL_WANT_GMU" ]; then
KOL_TEXT="$(cat "$KOLIBRI_UNIT")"
KOL_TEXT="${KOL_TEXT//"$KOL_CUR"/"$KOL_WANT"}"
printf '%s\n' "${KOL_TEXT//"$KOL_CUR_GMU"/"$KOL_WANT_GMU"}" > "$KOL_STAGE"
if ! grep -q -- "$KOL_WANT " "$KOL_STAGE" || ! grep -q -- "$KOL_WANT_GMU " "$KOL_STAGE"; then
rm -f "$KOL_STAGE"; die "the Kolibri unit's rewrite to $KOL_MODEL failed (hand-edited unit?): re-run ./install-kolibri.sh --no-smoke"
fi
diff "$KOL_STAGE" "$KOLIBRI_UNIT" || true # show exactly what changes
sudo install -m 644 "$KOL_STAGE" "$KOLIBRI_UNIT"
rm -f "$KOL_STAGE"
KOL_REWRITTEN=1
fi
# Which lane this box served at boot, written down before it is disabled: removing the
# Kolibri lane later gives the boot back to it (install-kolibri.sh --uninstall).
for TEXT_UNIT_NAME in qwen38-sglang.service qwen38-flash.service qwen38-image.service qwen38-video.service; do
if systemctl is-enabled --quiet "$TEXT_UNIT_NAME" 2>/dev/null; then
printf '%s\n' "$TEXT_UNIT_NAME" > "$CONFIG_DIR/lane-before-kolibri"
fi
done
# The loop variable ends in UNIT_NAME on purpose: CI checks every
# `sudo systemctl <verb> "$..UNIT_NAME"` against the cockpit's sudoers allowlist.
for TEXT_UNIT_NAME in qwen38-sglang.service qwen38-flash.service qwen38-image.service qwen38-video.service; do
if systemctl is-enabled --quiet "$TEXT_UNIT_NAME" 2>/dev/null; then
echo "disabling $TEXT_UNIT_NAME at boot (unit file kept for switching back)"
sudo systemctl disable "$TEXT_UNIT_NAME"
fi
done
disable_rollback_lane
sudo systemctl enable "$KOLIBRI_UNIT_NAME" >/dev/null 2>&1 || sudo systemctl enable "$KOLIBRI_UNIT_NAME"
sudo systemctl daemon-reload
RUNNING=""
for u in qwen38-sglang.service qwen38-flash.service qwen38-image.service qwen38-video.service qwen38-llamacpp.service; do
systemctl is-active --quiet "$u" 2>/dev/null && RUNNING="$u"
done
printf '\n\033[1;32mSwitch queued: %s (%s)\033[0m\n' "$KOL_MODEL" "$KOLIBRI_UNIT_NAME"
if [ -n "$RUNNING" ]; then
echo "Effective after: sudo systemctl stop $RUNNING && sudo systemctl start $KOLIBRI_UNIT_NAME (or next reboot)"
elif systemctl is-active --quiet "$KOLIBRI_UNIT_NAME" 2>/dev/null; then
if [ -n "$KOL_REWRITTEN" ]; then
echo "Effective after: sudo systemctl restart $KOLIBRI_UNIT_NAME (it serves the previous checkpoint until then)"
else
echo "Its unit already named this checkpoint. Started before it did, the lane serves what it loaded: sudo systemctl restart $KOLIBRI_UNIT_NAME"
fi
else
echo "Effective after: sudo systemctl start $KOLIBRI_UNIT_NAME (or next reboot; ready in about 10 min)"
fi
# opencode follows the lane, as on a text switch: the kolibri provider (added here to a
# config written before the lane was installed) becomes the default of the generated
# config and of the one opencode reads, and the server that reads them restarts when they
# changed. A box installed with --no-opencode is left alone.
if [ -f "$CONFIG_DIR/opencode.off" ]; then
echo "opencode integration is off on this box (./install.sh --no-opencode): default model left alone"
else
KOL_OC_SUM="$({ cat "$CONFIG_DIR/opencode.json" "$HOME/.config/opencode/opencode.json" 2>/dev/null || true; } | sha256sum)"
KOL_PAIR="$("$REPO_DIR/oc-limits.sh" "$CHOICE")" || KOL_PAIR=""
KOL_NAME="$(python3 "$REPO_DIR/oc-point-default.py" --label "$CHOICE" 262144 || true)"
KOL_PORT="$({ grep -m1 -oE 'PROXY_PORT=[0-9]+|keepalive-proxy.py [0-9]+' /etc/systemd/system/qwen38-keepalive.service 2>/dev/null || true; } | grep -oE '[0-9]+$' || true)"
KOL_SRC="$(mktemp)"
if [ -n "$KOL_PAIR" ] && python3 "$REPO_DIR/oc-kolibri.py" "http://127.0.0.1:${KOL_PORT:-30001}/v1" "$CONFIG_DIR/api-key" \
"${KOL_PAIR%% *}" "$(echo "$KOL_PAIR" | cut -d' ' -f2)" "${KOL_NAME:-Kolibri-1 (local, 262K)}" > "$KOL_SRC"; then
# The compaction block is global: sized from Kolibri's window as every text switch sizes
# it from its own. Left as the Qwen lane wrote it (132,000 on a 27B in 1M mode), opencode
# kept almost all of a session verbatim against Kolibri's threshold and compacted again
# every step or two (found in review, 2026-10-14). pi's keepRecentTokens follows (pi-gen).
KOL_KEEP="$("$REPO_DIR/oc-limits.sh" --preserve "${KOL_PAIR%% *}")" || KOL_KEEP=""
for OC_JSON in "$CONFIG_DIR/opencode.json" "$HOME/.config/opencode/opencode.json"; do
[ -f "$OC_JSON" ] || continue
python3 "$REPO_DIR/oc-merge-limits.py" "$OC_JSON" --add-providers "$KOL_SRC" >/dev/null || true
python3 "$REPO_DIR/oc-point-default.py" "$OC_JSON" kolibri "$CHOICE" 262144 \
|| echo "NOTE: could not update $OC_JSON (hand-edited?); set its \"model\" field yourself"
[ -n "$KOL_KEEP" ] && { python3 "$REPO_DIR/oc-merge-limits.py" "$OC_JSON" --compaction "$KOL_KEEP" \
|| echo "NOTE: could not update the compaction block in $OC_JSON; check it by hand ($KOL_KEEP)"; }
done
fi
rm -f "$KOL_SRC"
if [ -d "$CONFIG_DIR/pi" ] || [ -d "$CONFIG_DIR/omp" ]; then
python3 "$REPO_DIR/pi-gen.py" --config "$CONFIG_DIR/opencode.json" --out "$CONFIG_DIR" \
|| echo "NOTE: could not regenerate the pi/omp artifacts; run ./pi-gen.py by hand"
fi
if [ "$KOL_OC_SUM" != "$({ cat "$CONFIG_DIR/opencode.json" "$HOME/.config/opencode/opencode.json" 2>/dev/null || true; } | sha256sum)" ] \
&& systemctl list-unit-files opencode-web.service >/dev/null 2>&1 && systemctl is-active --quiet opencode-web.service; then
sudo systemctl restart opencode-web.service \
&& echo "opencode-web.service restarted so it reads the new default" \
|| echo "NOTE: could not restart opencode-web.service; restart it by hand"
fi
fi
echo "Clients reach it through the proxy on :30001 with the box's key, as model kolibri-1."
echo "Switch back: ./switch-model.sh stock (or any other target)"
exit 0
fi
# ── The image lane: a third lane, switched to the same way as the other two ─────
# Same contract as a cross-lane switch: verify the checkpoint, make it the one unit
# enabled at boot, never start or stop anything, print the exact commands. What it
# does NOT share with the text lanes is everything that is about text: there is no
# serving image to inspect (it runs from its own venv), nothing in the unit to rewrite but
# its --model-path (between the lane's checkpoints), no prompt ceiling on the proxy (the
# proxy is a text door and nothing reaches this lane through it), and no opencode default
# to move (opencode is a text client, and pointing it at an image lane would break every
# session it opens). So it is handled here, whole, and exits, instead of threading
# "unless image" through four hundred lines written for SGLang's text server.
if [ "$CHOICE" = "image" ] || [ "$CHOICE" = "image-turbo" ]; then
[ -f "$IMAGE_UNIT" ] || die "the image lane is not installed on this box (no $IMAGE_UNIT). Install it once: ./install.sh --with-image (40 GB)"
IMG_WD="$(grep -m1 -E '^WorkingDirectory=' "$IMAGE_UNIT" | cut -d= -f2- || true)"
IMG_PY="${IMG_WD:-$HOME/.local/share/qwen38-image}/venv/bin/python"
[ -x "$IMG_PY" ] || die "the image lane's runtime is missing ($IMG_PY): re-run ./install-image.sh, it resumes"
# The lane's two checkpoints and their pinned revisions, from install-image.sh, the one
# place they are declared.
eval "$(grep -E '^(IMAGE_MODEL_PIN|IMAGE_MODEL_PIN_REV|IMAGE_TURBO_PIN|IMAGE_TURBO_PIN_REV)=' "$REPO_DIR/install-image.sh" || true)"
for _v in IMAGE_MODEL_PIN IMAGE_MODEL_PIN_REV IMAGE_TURBO_PIN IMAGE_TURBO_PIN_REV; do
eval "[ -n \"\${$_v:-}\" ]" || die "install-image.sh no longer defines $_v (repo layout changed?)"
done
unset _v
IMG_CUR="$(grep -oE -- '--model-path [^ ]+' "$IMAGE_UNIT" | head -1 | cut -d' ' -f2 || true)"
IMG_CUR="${IMG_CUR:-$IMAGE_MODEL_PIN}"
# image-turbo serves the Turbo; image serves the base, or the checkpoint the unit was
# installed with (IMAGE_MODEL=) when it is neither, written down on the way to the Turbo
# so that the way back finds it.
IMG_BEFORE_TURBO="$CONFIG_DIR/image-model-before-turbo"
if [ "$CHOICE" = "image-turbo" ]; then
IMG_MODEL="$IMAGE_TURBO_PIN"
elif [ "$IMG_CUR" = "$IMAGE_TURBO_PIN" ]; then
IMG_MODEL="$(cat "$IMG_BEFORE_TURBO" 2>/dev/null || true)"; IMG_MODEL="${IMG_MODEL:-$IMAGE_MODEL_PIN}"
else
IMG_MODEL="$IMG_CUR"
fi
# The revision this box serves of that checkpoint, as the video lane reads it: the unit
# names the repo with HF_HUB_OFFLINE=1, so its refs/main IS the served commit, and one an
# install made with IMAGE_MODEL_REV= stays served. The installer's pin answers when the ref
# is missing or holds no commit, and main for a checkpoint of the unit's own.
IMG_HF="$(grep -m1 -E '^Environment=HF_HOME=' "$IMAGE_UNIT" | cut -d= -f3- || true)"
IMG_MODEL_DIR="${IMG_HF:-$HF_CACHE}/hub/models--${IMG_MODEL//\//--}"
IMG_REV=""
if [ -f "$IMG_MODEL_DIR/refs/main" ]; then IMG_REV="$(cat "$IMG_MODEL_DIR/refs/main")"; fi
[[ "$IMG_REV" =~ ^[0-9a-f]{40}$ ]] || IMG_REV=""
if [ -z "$IMG_REV" ]; then
case "$IMG_MODEL" in
"$IMAGE_MODEL_PIN") IMG_REV="$IMAGE_MODEL_PIN_REV" ;;
"$IMAGE_TURBO_PIN") IMG_REV="$IMAGE_TURBO_PIN_REV" ;;
*) IMG_REV=main ;;
esac
fi
if [ "$IMG_MODEL" = "$IMAGE_TURBO_PIN" ]; then
# The Turbo samples on the eight-step grid its model_index.json carries, which only a
# runtime with sgl-project/sglang#43391 follows; one without it loads the weights and
# samples on a uniform schedule instead, answering 200 with worse images.
IMG_WHY="$("$IMG_PY" "$REPO_DIR/image-sglang/turbo-grid-check.py" 2>&1)" \
|| die "the image lane's runtime cannot sample on the Turbo's eight-step grid (${IMG_WHY:-no reason given}). Update it first: ./install.sh (or ./install-image.sh), then switch again."
fi
printf '\n\033[1;36m── Verifying %s @ %s in the cache (resumable)\033[0m\n' "$IMG_MODEL" "$IMG_REV"
# The same two lessons the text lanes learned: Xet stalls on this box, and an
# unauthenticated pull gets throttled. local_files_only first, so a complete cache
# answers without touching the network at all. A revision is fetched as such and
# refs/main pointed at it when it had none, because the unit runs offline by name, which
# resolves refs/main: fetched at main, a push upstream would have changed what the lane
# serves (the same lesson as install-image.sh, 2026-09-24).
HF_HOME="${IMG_HF:-$HF_CACHE}" HF_HUB_DOWNLOAD_TIMEOUT=30 HF_HUB_DISABLE_XET=1 IMG_MODEL="$IMG_MODEL" IMG_REV="$IMG_REV" \
IMG_NEED_GB=34 IMG_FREE_GB="$(dl_free_gb "${IMG_HF:-$HF_CACHE}")" \
HF_HUB_DISABLE_PROGRESS_BARS="$DL_NO_BARS" QWEN38_REPO_DIR="$REPO_DIR" \
"$IMG_PY" - <<'PYIMG' || die "the image checkpoint could not be verified or fetched (re-run to resume; set HF_TOKEN if it stalls)"
import contextlib
import importlib.util
import os
from huggingface_hub import snapshot_download
def reporting(*args, **kwargs):
"""hf-progress.py's byte count while the rest comes; a progress line is never worth a download."""
try:
spec = importlib.util.spec_from_file_location(
"hf_progress", os.path.join(os.environ["QWEN38_REPO_DIR"], "hf-progress.py"))
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod.reporting(*args, **kwargs)
except Exception:
return contextlib.nullcontext()
repo, rev = os.environ["IMG_MODEL"], os.environ["IMG_REV"]
try:
path = snapshot_download(repo, revision=rev, local_files_only=True)
except Exception:
# Both checkpoints of this lane are about 33 GB; refused before the first byte rather
# than stopped by a full disk halfway. What the cache already holds of the repo comes
# off the need: a fetch that stopped resumes from there.
from huggingface_hub import constants
blobs = os.path.join(constants.HF_HUB_CACHE, "models--" + repo.replace("/", "--"), "blobs")
have = sum(e.stat().st_size for e in os.scandir(blobs) if e.is_file()) if os.path.isdir(blobs) else 0
free = int(os.environ.get("IMG_FREE_GB") or 0)
need = max(int(os.environ["IMG_NEED_GB"]) - have // 2**30, 1)
if free and free < need:
raise SystemExit(f"{free} GB free in the cache, and {repo} needs about {need} GB more: free some space first")
print("not complete in the cache, fetching the rest", flush=True)
with reporting(repo, revision=rev):
path = snapshot_download(repo, revision=rev)
print(path)
sha = os.path.basename(path.rstrip("/"))
if rev != "main" and sha == rev:
ref = os.path.join(os.path.dirname(os.path.dirname(path.rstrip("/"))), "refs", "main")
os.makedirs(os.path.dirname(ref), exist_ok=True)
old = open(ref).read().strip() if os.path.exists(ref) else ""
if old != sha:
with open(ref, "w") as f:
f.write(sha)
print(f"refs/main -> {sha[:12]}" + (f" (was {old[:12]})" if old else ""), flush=True)
PYIMG
# The unit names one checkpoint; a switch between the lane's two rewrites that name and
# nothing else.
IMG_REWRITTEN=""
if [ "$IMG_CUR" != "$IMG_MODEL" ]; then
IMG_UNIT_TEXT="$(cat "$IMAGE_UNIT")"
printf '%s\n' "${IMG_UNIT_TEXT//"--model-path $IMG_CUR "/"--model-path $IMG_MODEL "}" > "$IMG_STAGE"
if ! grep -q -- "--model-path $IMG_MODEL " "$IMG_STAGE"; then
rm -f "$IMG_STAGE"; die "the image unit's rewrite to $IMG_MODEL failed (hand-edited unit?): re-run ./install-image.sh"
fi
diff "$IMG_STAGE" "$IMAGE_UNIT" || true # show exactly what changes
sudo install -m 644 "$IMG_STAGE" "$IMAGE_UNIT"
rm -f "$IMG_STAGE"
IMG_REWRITTEN=1
if [ "$IMG_MODEL" = "$IMAGE_TURBO_PIN" ] && [ "$IMG_CUR" != "$IMAGE_MODEL_PIN" ]; then
printf '%s\n' "$IMG_CUR" > "$IMG_BEFORE_TURBO"
else
rm -f "$IMG_BEFORE_TURBO"
fi
fi
# Which text lane this box served, written down before it is disabled: install.sh
# updates that lane on a later run, and enablement cannot tell it once both are off.
for TEXT_UNIT_NAME in qwen38-flash.service qwen38-sglang.service qwen38-video.service qwen38-kolibri.service; do
if systemctl is-enabled --quiet "$TEXT_UNIT_NAME" 2>/dev/null; then
printf '%s\n' "$TEXT_UNIT_NAME" > "$CONFIG_DIR/lane-before-image"
fi
done
# The loop variable ends in UNIT_NAME on purpose: CI checks every
# `sudo systemctl <verb> "$..UNIT_NAME"` against the cockpit's sudoers allowlist,
# and a variable called $u would have walked straight past it.
for TEXT_UNIT_NAME in qwen38-sglang.service qwen38-flash.service qwen38-video.service qwen38-kolibri.service; do
if systemctl is-enabled --quiet "$TEXT_UNIT_NAME" 2>/dev/null; then
echo "disabling $TEXT_UNIT_NAME at boot (unit file kept for switching back)"
sudo systemctl disable "$TEXT_UNIT_NAME"
fi
done
disable_rollback_lane
sudo systemctl enable "$IMAGE_UNIT_NAME" >/dev/null 2>&1 || sudo systemctl enable "$IMAGE_UNIT_NAME"
sudo systemctl daemon-reload
RUNNING=""
for u in qwen38-sglang.service qwen38-flash.service qwen38-video.service qwen38-kolibri.service qwen38-llamacpp.service; do
systemctl is-active --quiet "$u" 2>/dev/null && RUNNING="$u"
done
printf '\n\033[1;32mSwitch queued: %s (%s)\033[0m\n' "$IMG_MODEL" "$IMAGE_UNIT_NAME"
if [ -n "$RUNNING" ]; then
echo "Effective after: sudo systemctl stop $RUNNING && sudo systemctl start $IMAGE_UNIT_NAME (or next reboot)"
elif systemctl is-active --quiet "$IMAGE_UNIT_NAME" 2>/dev/null; then
if [ -n "$IMG_REWRITTEN" ]; then
echo "Effective after: sudo systemctl restart $IMAGE_UNIT_NAME (it serves the previous checkpoint until then)"
else
echo "Its unit already named this checkpoint. Started before it did, the lane serves what it loaded: sudo systemctl restart $IMAGE_UNIT_NAME"
fi
else
echo "Effective after: sudo systemctl start $IMAGE_UNIT_NAME (or next reboot; ready in about 70 s)"
fi
echo "The proxy on :30001 and opencode are text clients and were left as they are."
echo "Switch back: ./switch-model.sh stock (or any text target)"
exit 0
fi
# ── The video lane: a fourth lane, switched to the same way as the image one ───
# Same contract as the image lane: verify the checkpoint, make it the one unit
# enabled at boot, never start or stop anything, print the exact commands. Like the
# image lane it shares nothing with the text lanes: no serving image to inspect (it
# runs from its own venv), no unit to rewrite (one checkpoint, one variant), no
# prompt ceiling on the proxy (the proxy is a text door and nothing reaches this
# lane through it), and no opencode default to move (opencode is a text client).
# Handled here, whole, and exits.
if [ "$CHOICE" = "video" ]; then
[ -f "$VIDEO_UNIT" ] || die "the video lane is not installed on this box (no $VIDEO_UNIT). Install it once: ./install.sh --with-video (about 165 GB of headroom)"
VID_WD="$(grep -m1 -E '^WorkingDirectory=' "$VIDEO_UNIT" | cut -d= -f2- || true)"
VID_PY="${VID_WD:-$HOME/.local/share/qwen38-video}/venv/bin/python"
[ -x "$VID_PY" ] || die "the video lane's runtime is missing ($VID_PY): re-run ./install-video.sh, it resumes"
VID_MODEL="$(grep -oE -- '--model-path [^ ]+' "$VIDEO_UNIT" | head -1 | cut -d' ' -f2 || true)"
VID_MODEL="${VID_MODEL:-MiniMaxAI/MiniMax-H3}"
VID_HF="$(grep -m1 -E '^Environment=HF_HOME=' "$VIDEO_UNIT" | cut -d= -f3- || true)"
printf '\n\033[1;36m── Verifying %s in the cache (resumable)\033[0m\n' "$VID_MODEL"
# Only the partition the installed unit serves is verified and fetched, by the same
# allowlist as install-video.sh: a complete fl2va cache answers without touching the
# network, and without it a switch re-downloaded the whole repo (Ref2VA included)
# onto a full disk (reference box, 2026-09-26).
VID_VARIANT="$(grep -oE -- '--model-variant [^ ]+' "$VIDEO_UNIT" | head -1 | cut -d' ' -f2 || true)"
VID_VARIANT="${VID_VARIANT:-fl2va}"
if [ "$VID_MODEL" = "MiniMaxAI/MiniMax-H3" ]; then
if [ "$VID_VARIANT" = "fl2va" ]; then VID_ALLOW="FL2VA/* model_index.json modular_model_index.json scheduler/* audio_scheduler/* *.md LICENSE*"
else VID_ALLOW="Ref2VA/* model_index.json modular_model_index.json scheduler/* audio_scheduler/* *.md LICENSE*"; fi
else VID_ALLOW="";
fi
# Xet stalls on this box, and an unauthenticated pull gets throttled.
# Fetch the revision this box actually serves, never the upstream main tip: the
# unit names the repo with HF_HUB_OFFLINE=1, so refs/main IS the served commit.
# A download without revision resolves main over the network, writes refs/main
# at the tip BEFORE the first byte is downloaded (huggingface_hub writes the ref
# before fetching: _snapshot_download.py), and resumes that half-fetched tip on
# the next boot attempt: a switched-off pin, ~145 GB of another commit, a dead
# offline unit. refs/main says what to fetch; the installer's pin answers when
# the ref is missing (a download by sha wrote no ref).
VID_MODEL_DIR="${VID_HF:-$HF_CACHE}/hub/models--${VID_MODEL//\//--}"
VID_REV=""
if [ -f "$VID_MODEL_DIR/refs/main" ]; then VID_REV="$(cat "$VID_MODEL_DIR/refs/main")"; fi
[[ "$VID_REV" =~ ^[0-9a-f]{40}$ ]] || VID_REV=""
if [ -z "$VID_REV" ]; then
if [ "$VID_MODEL" = "MiniMaxAI/MiniMax-H3" ]; then
VID_REV="$(grep -m1 -oE '^VIDEO_MODEL_PIN_REV="[0-9a-f]{40}"' "$REPO_DIR/install-video.sh" | cut -d'"' -f2 || true)"
else VID_REV="main"; fi
fi
[ -n "$VID_REV" ] || die "no refs/main under $VID_MODEL_DIR and install-video.sh no longer defines its pin: re-run ./install-video.sh, it resumes"
# Room first, the law the text branch learned on 2026-09-24 and this branch
# skipped: the cockpit's Load journey runs this under a serving engine, and a
# 145 GB weights fetch onto a fuller disk is a failure of everything on it
# (found in the branch's reviews, 2026-09-28). What the cache already holds
# comes off the need, so a complete cache (this box's normal state) needs only
# the floor and downloads nothing; the number is install-video.sh's own.
VID_WANT_GB="$(grep -m1 -oE '^WEIGHTS_GB=[0-9]+' "$REPO_DIR/install-video.sh" | cut -d= -f2 || true)"
VID_WANT_GB="${VID_WANT_GB:-145}"
VID_HAVE_B="$({ du -s --apparent-size -B1 "$VID_MODEL_DIR/blobs" 2>/dev/null || true; } | cut -f1)"
VID_WANT_GB=$((VID_WANT_GB - ${VID_HAVE_B:-0} / 1073741824)); [ "$VID_WANT_GB" -ge 10 ] || VID_WANT_GB=10
VID_FREE_GB="$(dl_free_gb "${VID_HF:-$HF_CACHE}")"
[ "${VID_FREE_GB:-0}" -ge "$VID_WANT_GB" ] \
|| die "not enough room for $VID_MODEL under HF_HOME=${VID_HF:-$HF_CACHE}: ${VID_FREE_GB:-?} GB free, about $VID_WANT_GB GB needed (nothing was changed). Free some space first, or point HF_HOME at a bigger disk."
HF_HOME="${VID_HF:-$HF_CACHE}" HF_HUB_DOWNLOAD_TIMEOUT=30 HF_HUB_DISABLE_XET=1 VID_MODEL="$VID_MODEL" VID_ALLOW="$VID_ALLOW" VID_REV="$VID_REV" \
HF_HUB_DISABLE_PROGRESS_BARS="$DL_NO_BARS" QWEN38_REPO_DIR="$REPO_DIR" \
"$VID_PY" - <<'PYVID' || die "the video checkpoint could not be verified or fetched (re-run to resume; set HF_TOKEN if it stalls; if the pinned revision was removed upstream, ./install-video.sh VIDEO_MODEL_REV=main re-pins it)"
import contextlib
import importlib.util
import os
import re
from huggingface_hub import snapshot_download
def reporting(*args, **kwargs):
"""hf-progress.py's byte count while the rest comes; a progress line is never worth a download."""
try:
spec = importlib.util.spec_from_file_location(
"hf_progress", os.path.join(os.environ["QWEN38_REPO_DIR"], "hf-progress.py"))
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod.reporting(*args, **kwargs)
except Exception:
return contextlib.nullcontext()
repo = os.environ["VID_MODEL"]
rev = os.environ["VID_REV"]
allow = [p for p in os.environ["VID_ALLOW"].split() if p] or None
try:
# local_files_only first, so a complete cache answers without touching the
# network at all; a full sha resolves straight from snapshots/, refs or no refs.
path = snapshot_download(repo, revision=rev, local_files_only=True, allow_patterns=allow)
except Exception:
print("not complete in the cache, fetching the rest", flush=True)
with reporting(repo, revision=rev, allow_patterns=allow):
path = snapshot_download(repo, revision=rev, allow_patterns=allow)
print(path)
# refs/main is what the offline unit resolves, and a download by sha never writes
# it: create it when missing, repair it when it holds no commit hash (a corrupt ref
# strands the unit offline whatever just succeeded here), never move a valid one
# the installer or the pin watch will answer for. This runs on the local-cache
# success too: that is the common path, and the ref can be exactly as absent there.
if len(rev) == 40:
ref = os.path.join(os.path.dirname(os.path.dirname(path.rstrip("/"))), "refs", "main")
cur = open(ref).read().strip() if os.path.exists(ref) else ""
if cur != rev and not re.fullmatch(r"[0-9a-f]{40}", cur):
os.makedirs(os.path.dirname(ref), exist_ok=True)
with open(ref, "w") as f:
f.write(rev)
print(f" refs/main -> {rev[:12]}", flush=True)
PYVID
# Which lane this box served, written down before it is disabled: install.sh
# updates that lane on a later run, and enablement cannot tell it once all are off.
for OTHER_UNIT_NAME in qwen38-flash.service qwen38-sglang.service qwen38-image.service qwen38-kolibri.service; do
if systemctl is-enabled --quiet "$OTHER_UNIT_NAME" 2>/dev/null; then
printf '%s\n' "$OTHER_UNIT_NAME" > "$CONFIG_DIR/lane-before-video"
fi
done
# The loop variable ends in UNIT_NAME on purpose: CI checks every
# `sudo systemctl <verb> "$..UNIT_NAME"` against the cockpit's sudoers allowlist,
# and a variable called $u would have walked straight past it.
for OTHER_UNIT_NAME in qwen38-sglang.service qwen38-flash.service qwen38-image.service qwen38-kolibri.service; do
if systemctl is-enabled --quiet "$OTHER_UNIT_NAME" 2>/dev/null; then
echo "disabling $OTHER_UNIT_NAME at boot (unit file kept for switching back)"
sudo systemctl disable "$OTHER_UNIT_NAME"
fi
done
disable_rollback_lane
sudo systemctl enable "$VIDEO_UNIT_NAME" >/dev/null 2>&1 || sudo systemctl enable "$VIDEO_UNIT_NAME"
sudo systemctl daemon-reload
RUNNING=""
for u in qwen38-sglang.service qwen38-flash.service qwen38-image.service qwen38-kolibri.service qwen38-llamacpp.service; do
systemctl is-active --quiet "$u" 2>/dev/null && RUNNING="$u"
done
printf '\n\033[1;32mSwitch queued: %s (%s)\033[0m\n' "$VID_MODEL" "$VIDEO_UNIT_NAME"
if [ -n "$RUNNING" ]; then
echo "Effective after: sudo systemctl stop $RUNNING && sudo systemctl start $VIDEO_UNIT_NAME (or next reboot)"
else
echo "Effective after: sudo systemctl start $VIDEO_UNIT_NAME (or next reboot; ready in about 12 min)"
fi
echo "The proxy on :30001 and opencode are text clients and were left as they are."
echo "Switch back: ./switch-model.sh stock (or any text target, or image)"
exit 0
fi
SGL_UNIT="/etc/systemd/system/qwen38-sglang.service"
FLASH_UNIT="/etc/systemd/system/qwen38-flash.service"
case "$CHOICE" in
stock) TARGET_REPO="$STOCK_REPO"; TARGET_REV="$STOCK_REV"; TARGET_LANE=27b ;;
uncensored) TARGET_REPO="$UNC_REPO"; TARGET_REV="$UNC_REV"; TARGET_LANE=27b ;;
fp8) TARGET_REPO="$FP8_REPO"; TARGET_REV="$FP8_REV"; TARGET_LANE=27b ;;
uncensored-fp8) TARGET_REPO="$UNCFP8_REPO"; TARGET_REV="$UNCFP8_REV"; TARGET_LANE=27b ;;
flash) TARGET_REPO="$FLASH_REPO"; TARGET_REV="$FLASH_REV"; TARGET_LANE=flash ;;
flash-nvda) TARGET_REPO="$FLASH_NVDA_REPO"; TARGET_REV="$FLASH_NVDA_REV"; TARGET_LANE=flash ;;
flash-uncensored) TARGET_REPO="$FLASH_UNC_REPO"; TARGET_REV="$FLASH_UNC_REV"; TARGET_LANE=flash ;;
esac
# The pins come from install.sh through eval, so a pin renamed or emptied there
# would otherwise reach the download step as an empty repo or revision. CI
# guards the names; this guards the values on a box that already diverged.
[ -n "${TARGET_REPO:-}" ] && [ -n "${TARGET_REV:-}" ] \
|| die "target '$CHOICE' resolved to an empty checkpoint or revision; install.sh is missing its pin"
if [ "$TARGET_LANE" = "27b" ]; then
[ -f "$SGL_UNIT" ] || die "the 27B stack is not installed on this box (no $SGL_UNIT). Install it once first: MODEL_CHOICE=$CHOICE ./install.sh"
TARGET_UNIT="$SGL_UNIT"; TARGET_UNIT_NAME="qwen38-sglang.service"
OTHER_UNIT="$FLASH_UNIT"; OTHER_UNIT_NAME="qwen38-flash.service"
INVOCATION="$SGL_UNIT"
PIN_IMAGE="$SERVE_IMAGE"
else
[ -f "$FLASH_UNIT" ] || die "the Flash-Next stack is not installed on this box (no $FLASH_UNIT). Install it once first: MODEL_CHOICE=$CHOICE ./install.sh (~126 GB download, no image build)"
TARGET_UNIT="$FLASH_UNIT"; TARGET_UNIT_NAME="qwen38-flash.service"
OTHER_UNIT="$SGL_UNIT"; OTHER_UNIT_NAME="qwen38-sglang.service"
INVOCATION="$CONFIG_DIR/launch-flash.sh"
PIN_IMAGE="$FLASH_SERVE_IMAGE"
fi
# The cache the installed engine mounts, read back the way install.sh does (CUR_HF),
# not the default: a box installed with HF_CACHE=/data/hf and switched from the cockpit,
# which passes no HF_CACHE, downloaded and patched into ~/.cache/huggingface while its
# unit kept mounting /data/hf, and the next start found no checkpoint (found in review,
# 2026-09-24). Both text lanes mount the same cache; an explicit HF_CACHE still wins.
if [ -z "$_ENV_HF_CACHE" ]; then
for _inv in "$INVOCATION" "$SGL_UNIT" "$CONFIG_DIR/launch-flash.sh"; do
_hf="$(grep -oE -- '-v [^ :]+:/root/\.cache/huggingface' "$_inv" 2>/dev/null | head -1 \
| sed -e 's/^-v //' -e 's|:/root/\.cache/huggingface$||' || true)"
if [ -n "$_hf" ]; then
[ "$_hf" != "$HF_CACHE" ] && echo "using the HF cache the installed engine mounts: $_hf"
HF_CACHE="$_hf"
break
fi
done
unset _inv _hf
fi
# The image whose presence decides a switch is the one the installed invocation
# will actually run, not the base it may have been built from. Since v1.8 a box
# can serve the pinned official image and keep no base image at all, and both
# base images are re-pullable, so gating on a base turned a working switch into
# a false "run install.sh once". Read it the way the cockpit does: the line just
# above the one that calls the server.
unit_image() {
[ -f "$1" ] || return 0
awk '/sglang\.launch_server/ { print prev; exit } { prev = $0 }' "$1" \
| sed -e 's/[[:space:]]*\\$//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
# Point the flash launcher at another checkpoint of the same lane: the model
# path, the revision, and the one flag line that belongs to the checkpoint.
#
# That line is REPLACED WHOLE, not patched in place. Patching fragments left one
# checkpoint's draft quantization standing behind the other's scheme (seen live
# on 2026-09-08 switching nvda -> flash), because the two strings hold a
# different number of flags. The rendered line always ends with the FP4 GEMM
# backend, so rebuilding it from the indent is exact.
#
# Tested by CI against both input shapes, sourced out of this file so a copy of
# the logic cannot drift from it.
# Point the 27B unit at another checkpoint of the same lane: the model path, the
# revision, and the KV cache dtype, which belongs to the checkpoint too.
#
# That last one is why this is a function and not two sed expressions. Until
# 2026-09-08 the switch rewrote only the path and the revision, so a switch from
# an NVFP4 target to Qwen's FP8 release left the unit with NO --kv-cache-dtype:
# that checkpoint carries no KV scales, "auto" falls back to a bf16 KV cache, and
# the pool halves (this repo measured 771,139 tokens against 382,706 on the same
# 1M unit). In the other direction the flag stayed on an NVFP4 target, which is a
# no-op there (both NVFP4 exports declare kv_cache_quant_algo: FP8, checked) but
# is what left this box reporting a drift for days.
#
# The fraction on that line is the box's own choice (0.50 native, 0.76 for 1M,
# and a hand-tuned value must survive a switch), so it is read from the unit and
# written back unchanged. Tested by CI against both directions and both modes,
# sourced out of this file so no copy of the logic can drift from it.
rewrite_27b_unit() { # $1 unit, $2 repo, $3 revision, $4 kv args ("" or "--kv-cache-dtype fp8_e4m3 ")
awk -v repo="$2" -v rev="$3" -v kv="$4" '
{
line = $0
sub(/--model-path [A-Za-z0-9_.\/-]+/, "--model-path " repo, line)
sub(/--revision [A-Za-z0-9]+/, "--revision " rev, line)
if (match(line, /--mem-fraction-static [0-9.]+/)) {
frac = substr(line, RSTART, RLENGTH)
match(line, /^ */)
line = substr(line, 1, RLENGTH) frac " " kv "\\"
}
print line
}
' "$1"
}
rewrite_flash_launcher() { # $1 launcher, $2 repo, $3 revision, $4 quant args
awk -v repo="$2" -v rev="$3" -v quant="$4" '
{
line = $0
sub(/--model-path [A-Za-z0-9_.\/-]+/, "--model-path " repo, line)
sub(/--revision [A-Za-z0-9]+/, "--revision " rev, line)
if (line ~ /^ *(--quantization|--moe-runner-backend)/) {
match(line, /^ */)
line = substr(line, 1, RLENGTH) quant "--fp4-gemm-backend flashinfer_cutlass \\"
}
print line
}
' "$1"
}
DL_IMAGE="$(unit_image "$INVOCATION")"
case "$DL_IMAGE" in
*:*) ;; # name:tag or name@sha256:... both match
*) DL_IMAGE="$PIN_IMAGE" ;; # unreadable invocation: fall back to the repo's pin
esac
docker image inspect "$DL_IMAGE" >/dev/null 2>&1 \
|| die "the image this box would serve is not present ($DL_IMAGE): run MODEL_CHOICE=$CHOICE ./install.sh once, the switch stays surgical"
if [ "$TARGET_LANE" = "27b" ]; then
CUR="$(grep -oE -- '--model-path [^ ]+' "$TARGET_UNIT" | head -1 | cut -d' ' -f2 || true)"
if [ "$CUR" = "$TARGET_REPO" ] && ! systemctl is-enabled --quiet "$OTHER_UNIT_NAME" 2>/dev/null; then
echo "unit already points at $TARGET_REPO"
else
echo "switching: ${CUR:-<none>} -> $TARGET_REPO @ $TARGET_REV"
fi
else
echo "switching the serving lane to Flash-Next: $TARGET_REPO @ $TARGET_REV"
fi
# 1) checkpoint in cache (docker + pinned image python, same as install.sh step 4)
printf '\n\033[1;36m── Verifying/downloading %s @ %s (resumable)\033[0m\n' "$TARGET_REPO" "$TARGET_REV"
# Room first, as install.sh does: a cockpit button can start this, and a download that
# fills the disk under a serving engine is a failure of everything on it, while the old
# message blamed an HF_TOKEN (found in review, 2026-09-24). What the cache already holds
# for the target comes off the lane's need (install.sh's numbers: 45 GB for a 27B
# checkpoint and its caches, 180 for flash).
DL_REPO_CACHE="$HF_CACHE/hub/models--${TARGET_REPO//\//--}"
DL_NEED_GB=45; [ "$TARGET_LANE" = "flash" ] && DL_NEED_GB=180
DL_HAVE_B="$({ du -s --apparent-size -B1 "$DL_REPO_CACHE/blobs" 2>/dev/null || true; } | cut -f1)"
DL_NEED_GB=$((DL_NEED_GB - ${DL_HAVE_B:-0} / 1073741824)); [ "$DL_NEED_GB" -ge 10 ] || DL_NEED_GB=10
DL_FREE_GB="$(dl_free_gb)"
[ "${DL_FREE_GB:-0}" -ge "$DL_NEED_GB" ] \
|| die "not enough room for $TARGET_REPO under HF_CACHE=$HF_CACHE: ${DL_FREE_GB:-?} GB free, about $DL_NEED_GB GB needed (nothing was changed). Free some space first."
DL_TOKEN_ARGS=()
# by name: docker takes the value from its own environment, so the token is not in the
# docker client's argv, which any local user can read in /proc (found in review, 2026-09-24)
if [ -n "${HF_TOKEN:-}" ]; then export HF_TOKEN; DL_TOKEN_ARGS=(-e HF_TOKEN); fi
# The byte count reads hf-progress.py in the container. Mounted only when it is there: a
# bind of a missing file makes docker create a root-owned directory in its place.
DL_PROGRESS_ARGS=()
if [ -f "$REPO_DIR/hf-progress.py" ]; then
DL_PROGRESS_ARGS=(-v "$REPO_DIR/hf-progress.py:/opt/qwen38/hf-progress.py:ro" -e QWEN38_REPO_DIR=/opt/qwen38)
fi
# --init: the cockpit stops a switch that overruns its job timeout with a TERM to the
# whole process group, and docker run passes it on to the container, where python3 as
# PID 1 has no handler for it and ignores it. Measured on the reference box: without
# --init the container outlived the TERM and then a SIGKILL of its docker client, and
# went on downloading with nobody attached; with it, both were gone within 6 s.
docker run --rm -i --init --network host --user "$(id -u):$(id -g)" \
--entrypoint python3 \
-e HF_HOME=/hf -e HF_HUB_DOWNLOAD_TIMEOUT=30 -e HF_HUB_DISABLE_XET=1 \
-e HF_HUB_DISABLE_PROGRESS_BARS="$DL_NO_BARS" \
-e MODEL_REPO="$TARGET_REPO" -e MODEL_REV="$TARGET_REV" \
"${DL_TOKEN_ARGS[@]}" "${DL_PROGRESS_ARGS[@]}" \
-v "$HF_CACHE":/hf \
"$DL_IMAGE" - <<'PYEOF' || die "download failed ($(dl_free_gb) GB left under $HF_CACHE; if that is near 0 the disk filled up, otherwise re-run to resume: HuggingFace throttles unauthenticated downloads, set HF_TOKEN=<your token> if it stalls)"
import contextlib
import importlib.util
import os
import time
from huggingface_hub import constants, snapshot_download
def reporting(*args, **kwargs):
"""hf-progress.py's byte count while the checkpoint comes; never worth a download."""
try:
spec = importlib.util.spec_from_file_location(
"hf_progress", os.path.join(os.environ["QWEN38_REPO_DIR"], "hf-progress.py"))
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod.reporting(*args, **kwargs)
except Exception:
return contextlib.nullcontext()
def download(repo, rev):
"""snapshot_download, with the one exception to the Xet ban above.
The classic CDN refuses any single file over MAX_HTTP_DOWNLOAD_SIZE
(50,000,000,000 bytes) and says so in a ValueError naming hf_xet. One target
ships such a file: nvidia/Qwen3.8-Flash-Next-NVFP4 carries its n-gram table
as one 53.7 GB safetensors. Without this, that target cannot be fetched at
all: the resume loop retries the refusal four times and the run dies under a
message naming four causes that are all the wrong one, over a traceback that
does name hf_xet (seen 2026-09-18, 74 GB of the 124 left in cache). Xet is
turned back on only for the repo the CDN just refused, and turned off again
right after, so every other file keeps the transfer path that measured
89 MB/s against its 0-8.
"""
try:
return snapshot_download(repo, revision=rev)
except Exception as e:
if "too large to be downloaded" not in str(e) or not constants.HF_HUB_DISABLE_XET:
raise
print("a file is over the classic CDN limit; retrying this repo with Xet", flush=True)
constants.HF_HUB_DISABLE_XET = False
try:
return snapshot_download(repo, revision=rev)
finally:
constants.HF_HUB_DISABLE_XET = True
print("──", os.environ["MODEL_REPO"], "@", os.environ["MODEL_REV"], flush=True)
with reporting(os.environ["MODEL_REPO"], revision=os.environ["MODEL_REV"]):
# a new attempt keeps every file already whole; one cut off mid-way starts over (the
# huggingface_hub of the pinned images, 1.30 and 1.33, keeps no partial file)
for attempt in range(1, 6):
try:
path = download(os.environ["MODEL_REPO"], os.environ["MODEL_REV"])
break
except Exception as e:
if attempt == 5:
raise
print(f"download interrupted ({type(e).__name__}), resuming ({attempt}/5)...", flush=True)
time.sleep(10)
# Same guarantee as install.sh: offline serving resolves "main" via refs/main,
# which a pinned-sha download never writes. Write it once, never overwrite.
sha = os.path.basename(path.rstrip("/"))
ref = os.path.join(os.path.dirname(os.path.dirname(path.rstrip("/"))), "refs", "main")
if len(sha) == 40 and not os.path.exists(ref):
os.makedirs(os.path.dirname(ref), exist_ok=True)
with open(ref, "w") as f:
f.write(sha)
print("checkpoint ready", flush=True)
PYEOF
# 2) 1M YaRN patch on the target's cached config.json (27B pair only, and only
# if its unit uses it; flash serves its native window)
if [ "$TARGET_LANE" = "27b" ] && grep -q -- '--context-length 1010000' "$TARGET_UNIT"; then
python3 "$REPO_DIR/patch-yarn.py" "$HF_CACHE" "$TARGET_REPO" "$TARGET_REV" || die "YaRN patch failed"
elif [ "$TARGET_LANE" = "27b" ]; then
# A native unit crashes at load on a YaRN-patched config, and a target this box served
# while it was in 1m keeps its patch in the cache after the box went back to native
# (install.sh restores the target it installs, not the others). So the target's config
# is restored here, idempotently; the restore refuses, before the unit is touched, when
# a patched config lost its backup (found in review, 2026-09-24).
echo "unit does not use the 1M context flag: restoring the target's native config if it carries YaRN"
python3 "$REPO_DIR/patch-yarn.py" --restore "$HF_CACHE" "$TARGET_REPO" "$TARGET_REV" \
|| die "the target's config carries the 1M YaRN patch and could not be restored (the switch was NOT applied to the unit yet)"
fi
# 2b) regenerate the patched chat template FROM the target's own snapshot.
# One template file per engine; the served template always follows the served
# model. Read at service startup only, so writing it now is safe.
TEMPLATE_OUT="$CONFIG_DIR/chat-template-sglang.jinja"
[ "$TARGET_LANE" = "flash" ] && TEMPLATE_OUT="$CONFIG_DIR/chat-template-flashnext.jinja"
python3 "$REPO_DIR/patch-template.py" "$HF_CACHE" "$TEMPLATE_OUT" "$TARGET_REV" "$TARGET_REPO" \
|| die "template patch failed for $TARGET_REPO (the switch was NOT applied to the unit yet)"
# 3) point the target unit at the exact checkpoint. For the 27B pair, rewrite
# ONLY --model-path/--revision (the draft's own revision flag has a
# different name and is never touched). For flash, refresh --revision to
# the repo pin. The 27B unit is installed with sudo, so it is staged at a
# FIXED path the cockpit's exact-argv sudoers allowlist pins (a mktemp name
# cannot be pinned); the user-owned flash launcher keeps a temp file.
TMP_UNIT="$(mktemp)"
STAGE_UNIT="$CONFIG_DIR/qwen38-sglang.service.switch-stage"
STAGE_CEIL="$CONFIG_DIR/keepalive-ceiling.conf.switch-stage"
trap 'rm -f "$TMP_UNIT" "$STAGE_UNIT" "$STAGE_CEIL"' EXIT
if [ "$TARGET_LANE" = "27b" ]; then
# The KV cache dtype follows the checkpoint, exactly as install.sh decides it:
# Qwen's FP8 release carries no KV scales and must ask for fp8 explicitly, the
# NVFP4 exports declare it in their own quant config and must not be forced.
# CI asserts these two strings still match install.sh's KV_CACHE_ARGS.
case "$CHOICE" in
fp8|uncensored-fp8) NEW_KV="--kv-cache-dtype fp8_e4m3 " ;;
*) NEW_KV="" ;;
esac
rewrite_27b_unit "$TARGET_UNIT" "$TARGET_REPO" "$TARGET_REV" "$NEW_KV" > "$STAGE_UNIT"
grep -q -- "--model-path $TARGET_REPO" "$STAGE_UNIT" || die "unit rewrite failed"
KV_SEEN="$(grep -c -- '--kv-cache-dtype fp8_e4m3' "$STAGE_UNIT" || true)"
case "$CHOICE" in
fp8|uncensored-fp8)
[ "$KV_SEEN" -eq 1 ] || die "the rewritten unit lost the fp8 KV cache this target needs (it costs half the pool); re-run MODEL_CHOICE=$CHOICE ./install.sh" ;;
*)
[ "$KV_SEEN" -eq 0 ] || die "the rewritten unit still forces an fp8 KV cache; re-run MODEL_CHOICE=$CHOICE ./install.sh" ;;
esac
grep -qE -- '--mem-fraction-static [0-9.]+' "$STAGE_UNIT" || die "the rewritten unit lost its memory fraction"
if grep -q -- '--revision ' "$TARGET_UNIT"; then
grep -q -- "--revision $TARGET_REV" "$STAGE_UNIT" || die "unit revision rewrite failed"
fi
diff "$STAGE_UNIT" "$TARGET_UNIT" || true # show exactly what changes
sudo install -m 644 "$STAGE_UNIT" "$TARGET_UNIT"
else
# Flash keeps its serving flags in a plain launch script; the unit just
# points at it. Refresh --revision there.
FLASH_LAUNCH="$CONFIG_DIR/launch-flash.sh"
[ -f "$FLASH_LAUNCH" ] || die "flash launch script missing ($FLASH_LAUNCH): re-run MODEL_CHOICE=flash ./install.sh"
# The two flash checkpoints differ in more than their name: the RadixArk
# export is plain NVFP4 and declares it, NVIDIA's is a mixed-precision export
# that resolves its own scheme and needs the MoE runner pinned. So the switch
# rewrites the model path, the revision and that one flag pair together.
# These two strings are install.sh's FLASH_QUANT_ARGS, and CI asserts they
# still match it character for character. They carry BOTH the target's scheme
# and the draft's, because both belong to the checkpoint: the NVFP4 tree's
# in-checkpoint MTP tensors are BF16, NVIDIA's are fp8 block-scaled.
case "$CHOICE" in
flash-nvda) NEW_QUANT="--moe-runner-backend flashinfer_cutlass " ;;
*) NEW_QUANT="--quantization modelopt_fp4 --speculative-draft-model-quantization unquant " ;;
esac
rewrite_flash_launcher "$FLASH_LAUNCH" "$TARGET_REPO" "$TARGET_REV" "$NEW_QUANT" > "$TMP_UNIT"
grep -qF -- "$NEW_QUANT--fp4-gemm-backend" "$TMP_UNIT" \
|| die "the launcher's quantization line did not take this checkpoint's flags; re-run MODEL_CHOICE=$CHOICE ./install.sh"
grep -q -- "--model-path $TARGET_REPO " "$TMP_UNIT" || die "the flash launch script does not serve $TARGET_REPO (hand-edited?); re-run MODEL_CHOICE=$CHOICE ./install.sh"
grep -q -- "--revision $TARGET_REV" "$TMP_UNIT" || die "launch script revision rewrite failed"
bash -n "$TMP_UNIT" || die "rewritten launch script does not parse"
diff "$TMP_UNIT" "$FLASH_LAUNCH" || true # show exactly what changes
install -m 755 "$TMP_UNIT" "$FLASH_LAUNCH"
# A switch changes the checkpoint, never the serving image: only install.sh builds
# that. So a box that pulled a repo whose image pin moved would keep serving the old
# one, silently, and the image is where the kernel fixes live (v1.6: the merged
# sm_121 kernel). Say so instead of letting it pass.
# Since v1.8 the launcher names the pinned official image (a digest); a box
# installed with OVERLAY_FLASH=1 before v1.18.7 retired it names a local
# qwen38-flash:tag, and is told the same thing: adopt the pin.
# Same class as OC_WINDOW below: the empty case is handled three lines down
# ([ -n "$LAUNCH_IMAGE" ]), and without || true set -e never lets it get there.
LAUNCH_IMAGE="$(grep -oE '^[[:space:]]*(qwen38-flash:[A-Za-z0-9._-]+|lmsysorg/sglang@sha256:[0-9a-f]{64})' "$FLASH_LAUNCH" | tr -d '[:space:]' | head -1 || true)"
WANT_IMAGE="${FLASH_IMAGE:-}"
if [ -n "$LAUNCH_IMAGE" ] && [ -n "$WANT_IMAGE" ] && [ "$LAUNCH_IMAGE" != "$WANT_IMAGE" ]; then
printf '\n\033[1;33mWARNING:\033[0m this box serves flash from %s, but this repo pins %s.\n' \
"$LAUNCH_IMAGE" "$WANT_IMAGE"
printf ' The serving image is where the kernel fixes live, and only the installer adopts it.\n'
printf ' Run ./install.sh to adopt the pinned image before relying on this lane.\n'
fi
fi
# 4) exactly one serving unit enabled at boot; the opencode default model
# follows the switch (providers themselves are kept as installed).
if [ -f "$OTHER_UNIT" ] && systemctl is-enabled --quiet "$OTHER_UNIT_NAME" 2>/dev/null; then
echo "disabling $OTHER_UNIT_NAME at boot (unit file kept for switching back)"
sudo systemctl disable "$OTHER_UNIT_NAME"
fi
# The image lane is the third serving unit, the video lane the fourth. Leaving either
# enabled while a text lane is made the boot lane would bring two engines up at the
# next reboot.
if [ -f "$IMAGE_UNIT" ] && systemctl is-enabled --quiet "$IMAGE_UNIT_NAME" 2>/dev/null; then
echo "disabling $IMAGE_UNIT_NAME at boot (unit file kept for switching back)"
sudo systemctl disable "$IMAGE_UNIT_NAME"
fi
if [ -f "$VIDEO_UNIT" ] && systemctl is-enabled --quiet "$VIDEO_UNIT_NAME" 2>/dev/null; then
echo "disabling $VIDEO_UNIT_NAME at boot (unit file kept for switching back)"
sudo systemctl disable "$VIDEO_UNIT_NAME"
fi
# The Kolibri lane is the fifth: the same reason.
if [ -f "$KOLIBRI_UNIT" ] && systemctl is-enabled --quiet "$KOLIBRI_UNIT_NAME" 2>/dev/null; then
echo "disabling $KOLIBRI_UNIT_NAME at boot (unit file kept for switching back)"
sudo systemctl disable "$KOLIBRI_UNIT_NAME"
fi
disable_rollback_lane
sudo systemctl enable "$TARGET_UNIT_NAME" >/dev/null 2>&1 || sudo systemctl enable "$TARGET_UNIT_NAME"
sudo systemctl daemon-reload
# 4b) the keepalive proxy's one-prompt ceiling follows the lane (v1.5.6 contract, see
# install.sh: flash 250000 tokens by default, the 27B lane none), applied now so the
# proxy matches the lane that serves after the restart below. Written as a systemd
# drop-in, never by editing the unit in place: an in-place sed needs a sudoers
# wildcard to stay valid, and a wildcard on sed is root (its w command writes any
# file). The drop-in overrides the unit's Environment line; install.sh bakes the
# ceiling into the unit and removes this drop-in, so either path converges.
KA_UNIT="/etc/systemd/system/qwen38-keepalive.service"
if [ -f "$KA_UNIT" ] && grep -q '^Environment=FLASH_PROMPT_CEILING_TOKENS=' "$KA_UNIT"; then
# The proxy of this unit follows the lane that serves by itself (v6.25): nothing to move,
# and nothing to restart. Moving it here applied the target's ceiling while the old lane
# still served (a 27B serving 1M refused every prompt past 250,000 until the next boot),
# and the restart cut every stream in flight through :30001 (found in review, 2026-09-24).
echo "keepalive proxy: its one-prompt ceiling follows the lane that serves; left as it is"
elif [ -f "$KA_UNIT" ]; then
# A unit from before v1.18.7, whose proxy only knows one ceiling: the old way, until a
# ./install.sh writes the new unit.
CEIL=0
[ "$TARGET_LANE" = "flash" ] && CEIL="${PROMPT_CEILING_TOKENS:-250000}"
[[ "$CEIL" =~ ^[0-9]+$ ]] || die "PROMPT_CEILING_TOKENS must be a number (got '$CEIL')"
printf '[Service]\nEnvironment=PROMPT_CEILING_TOKENS=%s\n' "$CEIL" > "$STAGE_CEIL"
sudo install -m 644 -D "$STAGE_CEIL" "/etc/systemd/system/qwen38-keepalive.service.d/ceiling.conf"
sudo systemctl daemon-reload
sudo systemctl restart qwen38-keepalive.service
echo "keepalive proxy: one-prompt ceiling ${CEIL} tokens for the $TARGET_LANE lane"
fi
# Both files when present: the generated artifact (CONFIG_DIR) and the config
# opencode actually reads (~/.config/opencode). Updating only the artifact left
# the real default on the previous lane (seen on the reference box 2026-08-30).
# A box installed with --no-opencode (marker file) is left entirely alone.
if [ -f "$CONFIG_DIR/opencode.off" ]; then
echo "opencode integration is off on this box (./install.sh --no-opencode): default model left alone"
else
# The limits follow the target, not just the model name. opencode compacts when a
# conversation reaches limit.context, and a limit larger than the lane can serve
# means the proxy refuses the request with a 400 before opencode ever compacts:
# a hard failure mid-session. Switching a 27B box in 1M mode (700,000) to the
# flash lane (200,000 ceiling) is exactly that, and until 2026-09-08 the switch
# left the old numbers in place. The table is oc-limits.sh, shared with
# install.sh so the two cannot disagree; the tier or context mode is read from
# the invocation this switch just wrote, which is what the box will serve.
# The label is read but not used here: the picker name is set by the python
# block below, from the same table's LABEL dict, so it is read into a throwaway.
# what the opencode server reads, before this switch writes any of it (see 4d); a missing
# file is part of the answer, not a failure (install.sh's first installs died on the same
# line under pipefail)
oc_configs_sum(){ { cat "$CONFIG_DIR/opencode.json" "$HOME/.config/opencode/opencode.json" 2>/dev/null || true; } | sha256sum; }
SW_OC_SUM_BEFORE="$(oc_configs_sum)"
if read -r SW_CTX SW_OUT _SW_LABEL \
<<<"$("$REPO_DIR/oc-limits.sh" "$CHOICE" --from "$INVOCATION")" \
&& [ -n "${SW_CTX:-}" ]; then
if [ "$TARGET_LANE" = "flash" ]; then SW_PROV=flashnext; SW_MODEL=qwen3.8-flash-next
else SW_PROV=qwen38; SW_MODEL=qwen3.8-27b; fi
# The compaction block is global, so it is sized from the target's context and
# rewritten with the limits: a switch that moved the threshold and left the old
# preserve_recent_tokens behind would compact the new lane against the old
# lane's window. Same table, same call as install.sh.
SW_KEEP="$("$REPO_DIR/oc-limits.sh" --preserve "$SW_CTX")" || SW_KEEP=""
for OC_JSON in "$CONFIG_DIR/opencode.json" "$HOME/.config/opencode/opencode.json"; do
[ -f "$OC_JSON" ] || continue
python3 "$REPO_DIR/oc-merge-limits.py" "$OC_JSON" "$SW_PROV" "$SW_MODEL" "$SW_CTX" "$SW_OUT" \
|| echo "NOTE: could not update the limits in $OC_JSON; check them by hand ($SW_CTX/$SW_OUT)"
[ -n "$SW_KEEP" ] && { python3 "$REPO_DIR/oc-merge-limits.py" "$OC_JSON" --compaction "$SW_KEEP" \
|| echo "NOTE: could not update the compaction block in $OC_JSON; check it by hand ($SW_KEEP)"; }
done
else
echo "NOTE: oc-limits.sh gave no limits for $CHOICE; the opencode limits were left as they were"
fi
for OC_JSON in "$CONFIG_DIR/opencode.json" "$HOME/.config/opencode/opencode.json"; do
[ -f "$OC_JSON" ] || continue
# The window is read from the INVOCATION, not from the unit: on the flash lane
# the unit is a thin pointer at the launcher and carries no --context-length,
# so reading the unit labelled every flash switch "(local)" instead of
# "(local, 262K)". Default model and picker name share one table in
# oc-point-default.py with install.sh, so the three spellings cannot drift.
# || true, and it is the whole reason a switch to a native 27B target died
# here on 2026-09-14: a native unit carries NO --context-length (262144 is the
# engine default), grep exits 1 on zero matches, and under set -e with
# pipefail that killed the script silently, three lines before the end. The
# lane had already been swapped and enabled, so the box served the 27B while
# opencode's default model still pointed at flashnext: a switch that reported
# failure and had in fact done nine tenths of the job. install.sh carries the
# same pipeline and was fixed the same way on 2026-09-12; this copy was not.
OC_WINDOW="$(grep -oE -- '--context-length [0-9]+' "$INVOCATION" 2>/dev/null | awk '{print $2}' | head -1 || true)"
python3 "$REPO_DIR/oc-point-default.py" "$OC_JSON" "$TARGET_LANE" "$CHOICE" "$OC_WINDOW" \
|| echo "NOTE: could not update $OC_JSON (hand-edited?); set its \"model\" field yourself"
done
# 4d) the opencode server, if this box runs one, is restarted so the numbers above
# actually reach it. `opencode serve` reads opencode.json ONCE at startup and
# never again: measured 2026-09-13, the file on disk said 225,000 while the
# running server still answered 175,000 on /config. Every switch before this
# rewrote the limits correctly and left the Agent tab compacting against the
# previous lane's window, which is a silent version of the mid-session 400
# this whole table exists to prevent.
# Only when they changed: a switch between two targets of one lane (stock to
# uncensored) writes the same numbers, and the restart ended the turn the Agent tab
# was in all the same (found in review, 2026-09-24).
if systemctl list-unit-files opencode-web.service >/dev/null 2>&1 \
&& systemctl is-active --quiet opencode-web.service; then
if [ "$(oc_configs_sum)" != "$SW_OC_SUM_BEFORE" ]; then
sudo systemctl restart opencode-web.service \
&& echo "opencode-web.service restarted so it reads the new limits" \
|| echo "NOTE: could not restart opencode-web.service; restart it by hand or the Agent view keeps the old limits"
else
echo "opencode-web.service kept: the limits it reads did not change"
fi
fi
fi
# 4f) pi and omp (docs/pi.md): the same dynamic the block above gives opencode.
# Their configs carry no second table of names and limits: pi-gen.py
# regenerates them from the opencode.json this switch just rewrote, as
# artifacts under ~/.config/qwen38 (pi/ JSON, omp/ YAML converted from it).
# No agent directory is ever written: the agents' own files are the user's
# copies, so re-copy them after a switch to pick up the new lane. Only on a
# box that asked for them once (./pi-gen.py made the directories): a switch
# writes no file for an agent nobody uses.
if [ -d "$CONFIG_DIR/pi" ] || [ -d "$CONFIG_DIR/omp" ]; then
python3 "$REPO_DIR/pi-gen.py" --config "$CONFIG_DIR/opencode.json" --out "$CONFIG_DIR" \
|| echo "NOTE: could not regenerate the pi/omp artifacts; run ./pi-gen.py by hand"
fi
RUNNING=""
systemctl is-active --quiet "$OTHER_UNIT_NAME" 2>/dev/null && RUNNING="$OTHER_UNIT_NAME"
systemctl is-active --quiet "$IMAGE_UNIT_NAME" 2>/dev/null && RUNNING="$IMAGE_UNIT_NAME"
systemctl is-active --quiet "$VIDEO_UNIT_NAME" 2>/dev/null && RUNNING="$VIDEO_UNIT_NAME"
systemctl is-active --quiet "$KOLIBRI_UNIT_NAME" 2>/dev/null && RUNNING="$KOLIBRI_UNIT_NAME"
systemctl is-active --quiet qwen38-llamacpp.service 2>/dev/null && RUNNING="qwen38-llamacpp.service"
printf '\n\033[1;32mSwitch queued: %s (%s)\033[0m\n' "$TARGET_REPO" "$TARGET_UNIT_NAME"
if [ -n "$RUNNING" ]; then
echo "Effective after: sudo systemctl stop $RUNNING && sudo systemctl start $TARGET_UNIT_NAME (or next reboot)"
else
echo "Effective after: sudo systemctl restart $TARGET_UNIT_NAME (or next reboot)"
fi
# The limits written above are the ones this mode DESERVES, not the ones this
# box can serve: the pool is only known once the engine has booted, and it moves
# with the checkpoint and the image. install.sh fits them at install time; a
# switch cannot, because the engine it just pointed at is not up yet. Say so,
# or the operator learns it from a proxy 400 late in a session (or, on a box
# with the cockpit, from its banner: this is the "Fit the limits to this
# engine" button). Measured three times on 2026-09-18 while testing switches.
echo "Then fit them: python3 oc-fit-limits.py (the limits above are this target's"
echo " nominal pair; the pool is only known once the engine is up)"
# The first target a hint names is a standard one: the project advises the standard
# checkpoints. The abliterated targets stay named, last in the parentheses, so that they are
# known without ever being the default. This line named `uncensored` first after every
# switch to stock.
case "$CHOICE" in
stock) echo "Switch back: ./switch-model.sh flash (or fp8, image, video, uncensored)" ;;
uncensored) echo "Switch back: ./switch-model.sh stock (or fp8, flash)" ;;
fp8) echo "Switch back: ./switch-model.sh stock (or flash, uncensored, uncensored-fp8)" ;;
uncensored-fp8) echo "Switch back: ./switch-model.sh fp8 (or stock, flash, uncensored)" ;;
flash) echo "Switch back: ./switch-model.sh stock (or fp8, flash-nvda, uncensored, flash-uncensored)" ;;
flash-nvda) echo "Switch back: ./switch-model.sh flash (or stock, fp8, flash-uncensored, uncensored)" ;;
flash-uncensored) echo "Switch back: ./switch-model.sh flash (or flash-nvda, stock, fp8, uncensored)" ;;
esac