aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / scripts / pipeline-proofs / lib.sh
| 1 | # Shared helpers for the acceptance proofs 13-21 (sourced, not executed). |
| 2 | # Every proof: `source lib.sh; proof_begin <n> "<title>" "$1"` then assertions, then `proof_end`. |
| 3 | # Exit 0 = PROVEN, 1 = FAILED, 2 = usage, 3 = NOT_APPLICABLE to this repository. |
| 4 | # Each proof works on a temporary `git clone` of the repository given as $1; the source tree is never touched. |
| 5 | # Env: CRUMB (default crumb), KEEP=1 keeps the temp directory, RESULTS_LOG=<file> appends the PROOF line. |
| 6 | set -u |
| 7 | CRUMB=${CRUMB:-crumb} |
| 8 | export GIT_AUTHOR_NAME=proof GIT_AUTHOR_EMAIL=proof@example.invalid GIT_COMMITTER_NAME=proof GIT_COMMITTER_EMAIL=proof@example.invalid |
| 9 | export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=/dev/null |
| 10 | unset CRUMB_COORD_ROOT |
| 11 | |
| 12 | PROOF_N=""; PROOF_TITLE=""; SRC=""; TMP=""; FAILS=0; NUMBERS="" |
| 13 | |
| 14 | now_ms() { date +%s%3N; } |
| 15 | |
| 16 | proof_begin() { # proof_begin <n> <title> <repo-path> |
| 17 | PROOF_N=$1; PROOF_TITLE=$2 |
| 18 | [ -n "${3:-}" ] || { echo "usage: $(basename "$0") <repo-path>" >&2; exit 2; } |
| 19 | SRC=$(cd "$3" 2>/dev/null && git rev-parse --show-toplevel 2>/dev/null) || { echo "not a git checkout: $3" >&2; exit 2; } |
| 20 | TMP=$(mktemp -d "${TMPDIR:-/tmp}/proof${PROOF_N}.XXXXXX") |
| 21 | if [ "${KEEP:-0}" != 1 ]; then trap 'rm -rf "$TMP"' EXIT; fi |
| 22 | echo "PROOF $PROOF_N: $PROOF_TITLE" |
| 23 | echo " repo: $SRC at $(git -C "$SRC" rev-parse --short HEAD) ($(git -C "$SRC" ls-files '*.crumb' '**/.crumb' | wc -l) crumbs); temp: $TMP; load: $(cut -d' ' -f1 /proc/loadavg)" |
| 24 | } |
| 25 | |
| 26 | clone_repo() { # clone_repo <dst> : clone of SRC at its HEAD, with user config set |
| 27 | git clone -q --no-hardlinks "$SRC" "$1" || { echo "clone failed" >&2; exit 1; } |
| 28 | git -C "$1" config user.name proof; git -C "$1" config user.email proof@example.invalid |
| 29 | } |
| 30 | |
| 31 | measure() { # measure <label> <cmd...> -> sets MS, RC, OUT; prints one MEASURE line |
| 32 | local l=$1; shift |
| 33 | local t0 t1; t0=$(now_ms) |
| 34 | OUT=$("$@" 2>&1); RC=$? |
| 35 | t1=$(now_ms); MS=$((t1 - t0)) |
| 36 | echo " MEASURE $l: ${MS} ms (rc=$RC)" |
| 37 | NUMBERS="$NUMBERS $l=${MS}ms" |
| 38 | } |
| 39 | |
| 40 | note() { echo " $*"; } |
| 41 | num() { NUMBERS="$NUMBERS $1"; echo " NUMBER $1"; } |
| 42 | |
| 43 | check() { # check "<label>" <command...> (command's success = assertion holds) |
| 44 | local l=$1; shift |
| 45 | if "$@" >/dev/null 2>&1; then echo " ok $l"; else echo " FAIL $l"; FAILS=$((FAILS+1)); fi |
| 46 | } |
| 47 | check_eq() { # check_eq "<label>" <got> <want> |
| 48 | if [ "$2" = "$3" ]; then echo " ok $1 ($2)"; else echo " FAIL $1 (got '$2', want '$3')"; FAILS=$((FAILS+1)); fi |
| 49 | } |
| 50 | |
| 51 | crumb_dirs() { # crumb_dirs <repo> : directories holding a tracked .crumb, "." for the root |
| 52 | git -C "$1" ls-files '*.crumb' '**/.crumb' | sed 's#/\?\.crumb$##; s#^$#.#' |
| 53 | } |
| 54 | leaf_crumb_dir() { # deepest crumb directory other than the root that holds a tracked non-crumb file |
| 55 | local r=$1 d |
| 56 | for d in $(crumb_dirs "$r" | grep -v '^\.$' | awk -F/ '{print NF" "$0}' | sort -rn | cut -d' ' -f2); do |
| 57 | if git -C "$r" ls-files "$d" | grep -v '\.crumb$' | grep -q .; then echo "$d"; return 0; fi |
| 58 | done |
| 59 | return 1 |
| 60 | } |
| 61 | tracked_file_in() { # a tracked non-crumb file under <dir> |
| 62 | git -C "$1" ls-files "$2" | grep -v '\.crumb$' | grep -v '^\.crumbignore$' | head -1 |
| 63 | } |
| 64 | ancestors_with_crumbs() { # ancestors_with_crumbs <repo> <dir> : the dir, each ancestor with a crumb, and "." (space separated, sorted) |
| 65 | local r=$1 d=$2 out |
| 66 | out=". $d" |
| 67 | while [ "$d" != "." ] && [ "${d%/*}" != "$d" ]; do |
| 68 | d=${d%/*} |
| 69 | [ -f "$r/$d/.crumb" ] && out="$out $d" |
| 70 | done |
| 71 | echo "$out" | tr ' ' '\n' | sort -u | tr '\n' ' ' | sed 's/ $//' |
| 72 | } |
| 73 | stale_set() { # stale_set <repo> : sorted STALE/UNCOMPILED dirs from crumb status |
| 74 | (cd "$1" && $CRUMB status . | awk '$1=="STALE"||$1=="UNCOMPILED"{print $2}' | sort | tr '\n' ' ' | sed 's/ $//') |
| 75 | } |
| 76 | current_count() { (cd "$1" && $CRUMB status . | grep -c '^CURRENT'); } |
| 77 | |
| 78 | proof_end() { |
| 79 | if [ "$FAILS" -eq 0 ]; then |
| 80 | echo "PROOF $PROOF_N PROVEN:$NUMBERS" |
| 81 | [ -n "${RESULTS_LOG:-}" ] && echo "PROOF $PROOF_N PROVEN ($(basename "$SRC")):$NUMBERS" >> "$RESULTS_LOG" |
| 82 | exit 0 |
| 83 | else |
| 84 | echo "PROOF $PROOF_N FAILED ($FAILS assertion(s)):$NUMBERS" |
| 85 | [ -n "${RESULTS_LOG:-}" ] && echo "PROOF $PROOF_N FAILED ($(basename "$SRC")):$NUMBERS" >> "$RESULTS_LOG" |
| 86 | exit 1 |
| 87 | fi |
| 88 | } |
| 89 | proof_na() { echo "PROOF $PROOF_N NOT_APPLICABLE: $1"; [ -n "${RESULTS_LOG:-}" ] && echo "PROOF $PROOF_N NOT_APPLICABLE ($(basename "$SRC")): $1" >> "$RESULTS_LOG"; exit 3; } |