#!/usr/bin/env bash # apply-protected-cast.sh — RUNS ON DANKSERVER. Rewrite the recipe's # PROTECTED-BEGIN/END section from Swift'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). set -euo pipefail CALIB=${1:-/home/dank/corpora/calib} RECIPE=${2:-/home/dank/serverAppConfigs-swift15/scripts/recipes/qwen4exp-swift15-w4dn8-i8-hc8m.recipe} PY=/home/dank/venvs/radcorpus/bin/python DERIVE=/home/dank/serverAppConfigs-swift15/scripts/derive-protected-experts.py TS=$(date +%Y%m%d-%H%M%S) 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; } "$PY" "$DERIVE" "$CALIB" > "/tmp/protected-cast-$TS.txt" N=$(grep -c 'cast dtype=bf16' "/tmp/protected-cast-$TS.txt") [ "$N" -ge 2 ] || { echo "derive produced $N cast lines; refusing" >&2; exit 1; } echo "derived $N protected experts -> /tmp/protected-cast-$TS.txt" cp -f "$RECIPE" "/home/dank/.AGENTTRASH/$TS-qwen4exp-swift15-w4dn8-i8-hc8m.recipe" "$PY" - "$RECIPE" "/tmp/protected-cast-$TS.txt" <<'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 Swift\'s own 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