#!/usr/bin/env bash# UPGRADE PROOF: the old side's writer, built from the core at an OLD sha (a build on the owner's phone), cached by# sha and by the writer's own hash. Prints the path of the test binary that holds `upgrade_old_side_writes_a_words_copy`.## scripts/upgrade-proof/old-core.sh <sha>## The tree at <sha> is a detached git worktree (its build script asks git for the commit), removed once built. The writer (crates/cotton-ffi/src/phone/# upgrade_writer.rs, as it is NOW) is copied in and registered, so one writer serves every sha; it uses only what every# phone build since 24 Nov has. Builds share one target dir under the cache, with CARGO_INCREMENTAL=0; once a sha's# binary is kept, its tree is removed. Cache: $COTTON_UPGRADE_CACHE, default <lane>/upgrade-cache beside the worktree.set -euo pipefailsha="${1:?usage: old-core.sh <sha>}"repo="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"cache="${COTTON_UPGRADE_CACHE:-$(dirname "$repo")/upgrade-cache}"writer="$repo/crates/cotton-ffi/src/phone/upgrade_writer.rs"full="$(git -C "$repo" rev-parse --verify "$sha^{commit}")"key="${full:0:12}-$(sha256sum "$writer" | cut -c1-12)"bin="$cache/bin/$key"if [ -x "$bin" ]; then echo "$bin"exit0fimkdir -p "$cache/bin""$cache/src"tree="$cache/src/${full:0:12}"git -C "$repo" worktree remove --force "$tree"2>/dev/null || rm -rf "${tree:?}"git -C "$repo" worktree prunegit -C "$repo" worktree add -q --detach "$tree""$full"cp "$writer""$tree/crates/cotton-ffi/src/phone/upgrade_writer.rs"if ! grep -q 'mod upgrade_writer;'"$tree/crates/cotton-ffi/src/phone/personal_object.rs"; then printf '\n#[cfg(test)]\n#[path = "upgrade_writer.rs"]\nmod upgrade_writer;\n' >> "$tree/crates/cotton-ffi/src/phone/personal_object.rs"fibuilt="$(cd "$tree/crates" && CARGO_INCREMENTAL=0 CARGO_TARGET_DIR="$cache/target" cargo test -q -p cotton-ffi --lib --no-run --message-format=json 2>"$cache/build-${full:0:12}.log" \ | python3 -c 'import json,sysfor line in sys.stdin: try: m = json.loads(line) except ValueError: continueif m.get("reason") == "compiler-artifact" and m.get("executable") and m.get("target", {}).get("name") == "cotton_ffi": print(m["executable"])' | tail -1)"if [ -z "$built" ] || [ ! -x "$built" ]; then echo "the core at ${full:0:12} did not build: $cache/build-${full:0:12}.log" >&2 tail -30"$cache/build-${full:0:12}.log" >&2exit1ficp "$built""$bin"git -C "$repo" worktree remove --force "$tree"echo "$bin"