Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465#!/usr/bin/env bash# Bless a rules change: its ruleset version and every digest it pins.## Run this after the final rebase onto main and commit what it writes. It# reads main's ruleset version from the base ref, never from the branch, so# parallel PRs cannot hand out the same number. A branch that changes what a# mission produces goes to main's value plus one and its golden replay, pinned# bot outcomes, pinned encodings and schema-4 store mission rows are rewritten.# A data-only change, where only the pack snapshot moved, is re-blessed without# a bump. On a branch that leaves every digest alone it does nothing.## Usage: scripts/bless.sh [<base>]set -euo pipefail
cd "$(dirname "$0")/.."
base="${1:-origin/main}"ruleset_file="crates/game/mission/src/lib.rs"golden_dir="crates/protokill/tests/golden"
if [ "${1:-}" != "--self-test" ]; then if ! git rev-parse --verify --quiet "$base" >/dev/null; then echo "bless: $base does not resolve; run git fetch origin first" >&2 exit 1 fi if ! git merge-base --is-ancestor "$base" HEAD; then echo "bless: HEAD does not contain $base; rebase onto $base first" >&2 exit 1 fifi
read_ruleset() { python3 -c 'import re, sysmatch = re.search(r"pub const RULESET_VERSION: u32 = (\d+);", sys.stdin.read())if not match: sys.exit("no RULESET_VERSION to read")print(match.group(1))'}
write_ruleset() { python3 - "$ruleset_file" "$1" <<'PY'import reimport sys
path, version = sys.argv[1], sys.argv[2]with open(path) as source: text = source.read()text, count = re.subn( r"pub const RULESET_VERSION: u32 = \d+;", f"pub const RULESET_VERSION: u32 = {version};", text, count=1,)if count != 1: sys.exit("no RULESET_VERSION to write")with open(path, "w") as target: target.write(text)PY}
golden() { cargo test -p protokill --lib the_golden_replay_reproduces_its_pinned_digests "$@"}
bot_pins() { cargo test -p protokill --lib bot_outcomes_are_pinned "$@"}
encoding_pins() { cargo test -p protokill --lib digest_encodings_are_pinned "$@"}
store_missions() { cargo test -p protokill_server --test it \ store_fixture::a_store_written_at_schema_4_still_serves_its_missions "$@"}
store_schema() { cargo test -p protokill_server --test it \ store_fixture::the_store_fixture_stays_at_schema_4 "$@"}
# What a commit changes, from the base's golden suite and this build's. Reads# two directories of golden files and prints the kind of change and whether# RULESET_VERSION should move. Any golden whose outcome moves is the branch's# doing; a golden the branch adds is new coverage, not a rule change.decide() { python3 - "$1" "$2" <<'PY'import jsonimport osimport sys
old_dir, new_dir = sys.argv[1], sys.argv[2]outcomes = encoding = rules = data_only = False
for name in sorted(os.listdir(new_dir)): if not name.endswith(".json"): continue old_path = os.path.join(old_dir, name) # A golden the base does not have has nothing to compare with. if not os.path.exists(old_path): continue old = json.load(open(old_path)) new = json.load(open(os.path.join(new_dir, name))) moved = ( old["actions"] != new["actions"] or old["action_log_digest"] != new["action_log_digest"] or old["final_state_digest"] != new["final_state_digest"] ) if moved: outcomes = True elif old["encoding"] != new["encoding"]: encoding = True elif old == new: pass elif old["snapshot"] != new["snapshot"] and old["spec_digest"] == new["spec_digest"]: # A moved pack snapshot on its own is data, and data changes do not # bump the ruleset. data_only = True else: rules = True
if outcomes: print("outcomes") print(1)elif encoding: print("encoding") print(1)elif rules: print("rules") print(1)elif data_only: print("data-only") print(0)else: print("pinned values") print(0)PY}
logs="$(mktemp -d)"trap 'rm -rf "$logs"' EXITfailed=()
# Runs one test, keeping its output in the log directory. Prints a line here# and remembers a failure so the output can follow.run_test() { local name="$1" test="$2" if "$test" >"$logs/$test.log" 2>&1; then printf ' %-15s pass\n' "$name" >&2 else printf ' %-15s FAIL\n' "$name" >&2 failed+=("$test") fi}
failed_has() { local test for test in "${failed[@]}"; do [ "$test" = "$1" ] && return 0 done return 1}
show_log() { echo "--- $1 ---" >&2 tail -n 80 "$logs/$1" >&2 || true}
show_failures() { local test for test in "${failed[@]}"; do show_log "$test.log" done}
# Runs every test a rules change can move.all_pass() { failed=() run_test "golden" golden run_test "bot pins" bot_pins run_test "encoding pins" encoding_pins run_test "store missions" store_missions run_test "store schema" store_schema [ "${#failed[@]}" -eq 0 ]}
# A store failure beside passing pins is a regression the bless would hide:# stored rows that no longer read are not stale digests.store_regression() { failed_has store_missions && ! failed_has golden && ! failed_has bot_pins && ! failed_has encoding_pins}
self_test() { local root name expected got bad=0 root="$(mktemp -d)"
write_golden() { local path="$1" actions="$2" encoding="$3" snapshot="$4" spec="${5:-s}" printf '{"actions":["%s"],"action_log_digest":"a","final_state_digest":"b","encoding":%s,"snapshot":"%s","spec_digest":"%s"}\n' \ "$actions" "$encoding" "$snapshot" "$spec" >"$path" }
make_case() { mkdir -p "$root/$1/old" "$root/$1/new" }
check() { local name="$1" want="$2" got="$(decide "$root/$name/old" "$root/$name/new" | paste -sd ' ' -)" if [ "$got" != "$want" ]; then echo "self-test: $name: expected '$want', got '$got'" >&2 bad=1 fi }
make_case same write_golden "$root/same/old/a.json" 01 13 one write_golden "$root/same/new/a.json" 01 13 one check same "pinned values 0"
make_case snapshot write_golden "$root/snapshot/old/a.json" 01 13 one write_golden "$root/snapshot/new/a.json" 01 13 two check snapshot "data-only 0"
make_case outcomes write_golden "$root/outcomes/old/a.json" 01 13 one write_golden "$root/outcomes/new/a.json" 02 13 one check outcomes "outcomes 1"
make_case encoding write_golden "$root/encoding/old/a.json" 01 13 one write_golden "$root/encoding/new/a.json" 01 14 one check encoding "encoding 1"
make_case rules write_golden "$root/rules/old/a.json" 01 13 one old-spec write_golden "$root/rules/new/a.json" 01 13 one new-spec check rules "rules 1"
# A golden the branch adds is new coverage, not a rule change. make_case added write_golden "$root/added/old/a.json" 01 13 one write_golden "$root/added/new/a.json" 01 13 one write_golden "$root/added/new/b.json" 03 13 one check added "pinned values 0"
# One unchanged golden must not hide another's outcome change. make_case mixed write_golden "$root/mixed/old/a.json" 01 13 one write_golden "$root/mixed/old/b.json" 01 13 one write_golden "$root/mixed/new/a.json" 01 13 one write_golden "$root/mixed/new/b.json" 02 13 one check mixed "outcomes 1"
failed=(store_missions) store_regression || { echo "self-test: a store-only failure is a regression" >&2; bad=1; } failed=(store_missions golden) ! store_regression || { echo "self-test: a golden change may re-bless the store" >&2; bad=1; } rm -rf "$root" [ "$bad" -eq 0 ] && echo "bless: self-test passed" return "$bad"}
if [ "${1:-}" = "--self-test" ]; then self_test exitfi
main="$(git show "$base:$ruleset_file" | read_ruleset)"before="$(read_ruleset <"$ruleset_file")"if [ "$before" -ne "$main" ]; then echo "bless: RULESET_VERSION $before is not $base's $main; resetting before deciding" >&2 write_ruleset "$main"fi
printf 'bless: checking %s against %s\n' "$(git rev-parse --short HEAD)" "$base" >&2if all_pass; then if [ "$before" -ne "$main" ]; then echo "bless: no outcome change; RULESET_VERSION reset from $before to $main" else echo "bless: every digests test passes; nothing to do" fi exit 0fishow_failures
if failed_has store_schema; then echo "bless: the store fixture is not a schema-4 store; fix it by hand" >&2 exit 1fiif store_regression; then cat >&2 <<'MESSAGE'bless: the schema-4 store no longer reads its own rows while the golden, botand encoding pins still pass. That is a regression in what is stored, not astale digest, and re-blessing would hide it. Fix the store path, then run thisagain.MESSAGE exit 1fi
old_goldens="$(mktemp -d)"trap 'rm -rf "$logs" "$old_goldens"' EXIT# Measure the whole suite against the base, not the branch's copies: a stale# pinned value on the branch is not a rule change, and parallel PRs must not# chain bumps. A golden the base does not have is new coverage and is skipped.while IFS= read -r path; do [ -n "$path" ] || continue git show "$base:$path" >"$old_goldens/$(basename "$path")"done < <(git ls-tree -r --name-only "$base" -- "$golden_dir")
echo "bless: re-blessing the golden suite to decide what changed" >&2PROTOKILL_BLESS_GOLDEN=1 cargo test -p protokill --lib \ the_golden_replay_reproduces_its_pinned_digests -- --nocapture >"$logs/golden-bless.log" 2>&1 || { show_log "golden-bless.log"; exit 1; }
decision="$(decide "$old_goldens" "$golden_dir")"kind="$(printf '%s\n' "$decision" | sed -n 1p)"bump="$(printf '%s\n' "$decision" | sed -n 2p)"
if [ "$bump" -eq 1 ]; then target=$((main + 1))else target="$main"fiwrite_ruleset "$target"if [ "$before" -ne "$target" ]; then echo "bless: RULESET_VERSION $before -> $target ($kind change)" >&2else echo "bless: RULESET_VERSION stays at $target ($kind change)" >&2fi
# Bless again so the golden carries the version it will be judged at.PROTOKILL_BLESS_GOLDEN=1 cargo test -p protokill --lib \ the_golden_replay_reproduces_its_pinned_digests -- --nocapture >"$logs/golden-bless.log" 2>&1 || { show_log "golden-bless.log"; exit 1; }
bot_json="$( PROTOKILL_BLESS_GOLDEN=1 cargo test -p protokill --lib bot_outcomes_are_pinned \ -- --nocapture 2>"$logs/bot-bless.log" | sed -n 's/^PROTOKILL_BLESS_BOT_OUTCOMES=//p')"encoding_json="$( PROTOKILL_BLESS_GOLDEN=1 cargo test -p protokill --lib digest_encodings_are_pinned \ -- --nocapture 2>"$logs/encoding-bless.log" | sed -n 's/^PROTOKILL_BLESS_ENCODING=//p')"if [ -z "$bot_json" ]; then show_log "bot-bless.log" exit 1fiif [ -z "$encoding_json" ]; then show_log "encoding-bless.log" exit 1fi
python3 - "$bot_json" "$encoding_json" <<'PY'import jsonimport reimport sys
bot = json.loads(sys.argv[1])encoding = json.loads(sys.argv[2])
def patch(path, pattern, build): with open(path) as source: text = source.read() text, count = re.subn(pattern, build, text, count=1, flags=re.S) if count != 1: sys.exit(f"bless: cannot find the pinned values in {path}") with open(path, "w") as target: target.write(text)
def flag(value): return "true" if value else "false"
def bot_outcomes(match): lines = [] for max_floor, turns, game_over, stuck, floors in bot: floors_text = ", ".join( f"({floor}, {floor_turns}, {flag(cleared)})" for floor, floor_turns, cleared in floors ) lines.append( f" ({max_floor}, {turns}, {flag(game_over)}, " f"{flag(stuck)}, vec![{floors_text}])," ) return match.group(1) + "vec![\n" + "\n".join(lines) + match.group(2)
patch( "crates/protokill/src/bot.rs", r"(assert_eq!\(\s*observed,\s*)vec!\[.*?(\n\s*\])", bot_outcomes,)
def actions(match): values = ", ".join(f'"{action}"' for action in encoding["actions"]) return match.group(1) + "[" + values + "]"
patch( "crates/protokill/src/mission/tests.rs", r"(assert_eq!\(\s*hex,\s*)\[[^\]]*\]", actions,)
def spec_digest(match): digest = encoding["spec_digest"] # Split the hex so no piece reads as a float token to the architecture scan. parts = [digest[:40], digest[40:42], digest[42:]] body = ",\n".join(f' "{part}"' for part in parts) return match.group(1) + "concat!(\n" + body + "\n )"
patch( "crates/protokill/src/mission/tests.rs", r"(assert_eq!\(\s*spec_digest_text,\s*//[^\n]*\n\s*)concat!\(.*?\n\s*\)", spec_digest,)
for name, key in [ ("outcome_digest_text", "outcome_digest"), ("action_log_digest_text", "action_log_digest"), ("replay_digest_text", "replay_digest"),]: key_name = key
def value(match, key_name=key_name): return match.group(1) + f'"{encoding[key_name]}"'
patch( "crates/protokill/src/mission/tests.rs", rf'({name},\s*)"[0-9a-f]{{64}}"', value, )PY
echo "bless: re-blessing the schema-4 store fixture's mission rows" >&2PROTOKILL_BLESS_STORE=1 cargo test -p protokill_server --test it \ store_fixture::a_store_written_at_schema_4_still_serves_its_missions >"$logs/store-bless.log" 2>&1 || { show_log "store-bless.log"; exit 1; }
cargo fmt --all
if ! all_pass; then show_failures echo "bless: tests still fail after blessing; see above" >&2 exit 1fi
echo "bless: $kind change; RULESET_VERSION $before -> $target" >&2git diff --stat