#!/usr/bin/env bash# A walk on a stale core is not evidence. This refuses one: it reads the build# stamp out of every slice of the xcframework (`crates/cotton-ffi/build.rs`)# and FAILS unless the core was built from exactly this checkout's crates/ —# the content, compared as one tree hash (`scripts/lib/crates-tree.sh`), so an# archive the shared cache built at another commit with the same crates/ passes,# and one built before the last change under crates/ (the core code, the# definitions and the Rust types the Swift definition types are made from)# fails. On 22 Oct 2026 a lane's deck screenshots ran on a core built before# the card landed, and nothing could tell.## bash scripts/core-fresh.sh check apps/cotton/.ffi/CottonFFI.xcframework# bash scripts/core-fresh.sh <archive or dir> check another xcframework or .a# bash scripts/core-fresh.sh --stamp …and print only the core's short commit,# for a capture's file name# bash scripts/core-fresh.sh --sidecar <dir> …and write <dir>/core-build.txt beside captures## Exit 0 fresh, 1 stale or unstamped, 2 usage.set -euo pipefailROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"TARGET="$ROOT/apps/cotton/.ffi/CottonFFI.xcframework"STAMP_ONLY=0SIDECAR=""while [ $# -gt 0 ]; docase"$1"in --stamp) STAMP_ONLY=1; shift ;; --sidecar) SIDECAR="${2:?--sidecar needs a directory}"; shift2 ;; -h|--help) sed -n '2,19p'"${BASH_SOURCE[0]}"; exit0 ;; -*) echo "core-fresh: unknown flag $1" >&2; exit2 ;; *) TARGET="$1"; shift ;;esacdone[ -e "$TARGET" ] || { echo "core-fresh: STALE — no core at $TARGET; build it with scripts/build-xcframework.sh" >&2; exit1; }# Every archive in it: each slice must be fresh, not just the first.archives=()if [ -d "$TARGET" ]; thenwhile IFS= read -r archive; do archives+=("$archive"); done < <(find "$TARGET" -type f -name 'libcotton_ffi*.a' | sort)else archives=("$TARGET")fi[ "${#archives[@]}" -gt 0 ] || { echo "core-fresh: STALE — $TARGET holds no libcotton_ffi archive (a stub?)" >&2; exit1; }# An XCFramework's slice is whole only with its headers beside the archive:# a create-xcframework cut short (the disk floor's watchdog, 8 Nov 2026) leaves# a stamped archive and no Headers, and the build then fails on every target.if [[ "$TARGET" == *.xcframework ]]; thenfor archive in"${archives[@]}"; do [ -d "$(dirname "$archive")/Headers" ] || { echo "core-fresh: STALE — $(dirname "$archive") has no Headers: a half-written XCFramework; rebuild with scripts/build-xcframework.sh" >&2; exit1; }donefiwant="$(bash "$ROOT/scripts/lib/crates-tree.sh" "$ROOT")"# THE CORE'S CRATES: cotton-ffi and every workspace crate it links, followed# through the path dependencies of each Cargo.toml (dev-dependencies never# reach the archive; an optional one counts, a superset only ever says stale).# The xcframework cache keys on the same closure (build-xcframework.sh,# THE KEY), so a change under a crate outside it (cotton-mac's tests,# 789da43115) leaves the cached core in place with its older stamp; without# this, that core was STALE here and every walk stopped until a phone crate moved.core_crates() {local todo=(cotton-ffi) seen=" " name depwhile [ "${#todo[@]}" -gt 0 ]; do name="${todo[0]}"; todo=("${todo[@]:1}")case"$seen"in *" $name "*) continue ;; esac seen+="$name " [ -f "$ROOT/crates/$name/Cargo.toml" ] || continuefor dep in $(awk ' /^\[/ { s = $0; gsub(/^\[|\].*$/, "", s); keep = (s ~ /^(target\..*\.)?(build-)?dependencies(\..*)?$/); next } keep && match($0, /path *= *"\.\.\/[^"]+"/) { p = substr($0, RSTART, RLENGTH); sub(/^.*"\.\.\//, "", p); sub(/".*$/, "", p); print p }' "$ROOT/crates/$name/Cargo.toml"); do todo+=("$dep"); donedone echo "$seen"}# Fresh though the stamp names other crates/: every path that differs lies# under a crate the core does not link. A workspace file (Cargo.toml,# Cargo.lock, rust-toolchain.toml) or a stamp tree this clone never saw is stale.outside_core() {local changed path top core changed="$(git -C "$ROOT" diff-tree -r --name-only "$1" "$2" 2>/dev/null)" || return1 [ -n "$changed" ] || return1 core="$(core_crates)"while IFS= read -r path; docase"$path"in */*) top="${path%%/*}" ;; *) return1 ;; esaccase"$core"in *" $top "*) return1 ;; esacdone <<<"$changed" echo "$changed" | cut -d/ -f1 | sort -u | paste -sd ' ' -}last="$(git -C "$ROOT" log -1 --format='%h %s' -- crates)"stale=0short=""for archive in"${archives[@]}"; do slice="${archive#"$TARGET"/}" stamp="$(LC_ALL=C grep -a -o -m1 -E 'cotton-core-build:[0-9a-f]{40}:[01]:[0-9a-f]{40}' "$archive" | head -1 || true)"if [ -z "$stamp" ]; then echo "core-fresh: STALE — $slice carries no build stamp: it was built before the stamp existed" >&2 stale=1; continuefi IFS=: read -r _ commit dirty tree <<<"$stamp" short="${commit:0:9}$([ "$dirty" = 1 ] && echo .dirty || true)"if [ "$tree" != "$want" ] && crates="$(outside_core "$tree" "$want")"; then [ "$STAMP_ONLY" = 1 ] || echo "core-fresh: $slice was built from $short; crates/ differ since only outside the core ($crates)" >&2elif [ "$tree" != "$want" ]; then echo "core-fresh: STALE — $slice was built from $short, whose crates/ differ from this checkout's (last change: $last); rebuild with scripts/build-xcframework.sh" >&2 stale=1fidone# THE KIT. A core built for one app's kit (COTTON_CORE_KIT, 15# Nov 2026) carries that kit's pictures alone and refuses any other app's# manifest, so it is no core for another app's walk. Its stamp names the kit# (`cotton-core-kit:<kit>;`, empty for the whole core, which serves every# app); a core from before the stamp is a whole one. The kit wanted is# COTTON_CORE_KIT here, as the build read it: unset is the whole core.for archive in"${archives[@]}"; do slice="${archive#"$TARGET"/}" built="$(LC_ALL=C grep -a -o -m1 -E 'cotton-core-kit:[a-z0-9_-]*;' "$archive" | head -1 || true)" built="${built#cotton-core-kit:}"; built="${built%;}"if [ "$built" != "${COTTON_CORE_KIT:-}" ]; thenif [ -n "$built" ]; then carries="the $built kit's pictures alone"; else carries="every kit's pictures"; fi echo "core-fresh: STALE — $slice carries $carries, and this build wants ${COTTON_CORE_KIT:-the whole core} (COTTON_CORE_KIT); rebuild with scripts/build-xcframework.sh" >&2 stale=1fidone[ "$stale" = 0 ] || exit1if [ -n "$SIDECAR" ]; then mkdir -p "$SIDECAR" printf 'core %s\ncrates %s\nhead %s\n'"$short""$want""$(git -C "$ROOT" rev-parse --short=9 HEAD)" > "$SIDECAR/core-build.txt"fiif [ "$STAMP_ONLY" = 1 ]; then echo "$short"; else echo "core-fresh: fresh — core $short matches this checkout's crates/"; fi