#!/usr/bin/env bash # apply-protected-cast.sh — RUNS ON DANKSERVER. Rewrite a recipe's # PROTECTED-BEGIN/END section from the campaign's OWN calibration grams: run # derive-protected-experts.py over the calib dir, splice its cast lines into # the marked section of the recipe in place, keep the pre-edit recipe in the # campaign trash. Refuses to run twice (marker text says derived), and refuses # when a requested target count was not reached. Run it on a COPY of the # template so the committed recipe stays the clean, reviewable baseline. set -euo pipefail SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CALIB=${1:-/home/dank/campaign/qwen38-w5nl64/calib} RECIPE=${2:-$SELF_DIR/recipes/qwen4exp-qwen38-w5nl64-bf16trunk.recipe} TARGET_COUNT=${3:-0} PY=${PY:-/home/dank/venvs/radcorpus/bin/python} DERIVE=${DERIVE:-$SELF_DIR/derive-protected-experts.py} TS=$(date +%Y%m%d-%H%M%S) # OUT and TRASH_DIR are overridable so the seam can drive the whole script in a # scratch directory; on the box they are the campaign's usual places. OUT=${OUT:-/tmp/protected-cast-$TS.txt} TRASH_DIR=${TRASH_DIR:-/home/dank/.AGENTTRASH} grep -q "derive-protected-experts.py rewrote" "$RECIPE" && { echo "recipe already carries derived cast lines; refusing to re-derive" >&2; exit 1; } grep -q "PROTECTED-BEGIN" "$RECIPE" || { echo "no PROTECTED markers in $RECIPE" >&2; exit 1; } ARGS=("$DERIVE" "$CALIB") if [ "$TARGET_COUNT" -gt 0 ]; then ARGS+=(--target-count "$TARGET_COUNT") fi "$PY" "${ARGS[@]}" > "$OUT" N=$(grep -c 'cast dtype=bf16' "$OUT") [ "$N" -ge 2 ] || { echo "derive produced $N cast lines; refusing (see $OUT)" >&2; exit 1; } if [ "$TARGET_COUNT" -gt 0 ] && [ "$N" -lt "$TARGET_COUNT" ]; then echo "derive reached $N protected experts, target was $TARGET_COUNT; refusing to convert" >&2 echo " (the bar sweep bottomed out: the recording is too thin or the target too high)" >&2 exit 1 fi echo "derived $N protected experts -> $OUT" mkdir -p "$TRASH_DIR" cp -f "$RECIPE" "$TRASH_DIR/$TS-$(basename "$RECIPE")" "$PY" - "$RECIPE" "$OUT" <<'PYEOF' import sys recipe, castfile = sys.argv[1], sys.argv[2] lines = open(recipe).read().splitlines() cast = [l for l in open(castfile).read().splitlines() if 'cast dtype=bf16' in l] out, insec = [], False for l in lines: if 'PROTECTED-BEGIN' in l: insec = True out.append(l) out.extend(cast) elif 'PROTECTED-END' in l: insec = False out.append(l) out.append('# (derive-protected-experts.py rewrote this section from the campaign grams)') elif not insec: out.append(l) open(recipe, 'w').write('\n'.join(out) + '\n') print(f'spliced {len(cast)} cast lines into {recipe}') PYEOF