interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / tools / candidate / parity / run.sh

109 lines · 6101 bytes Raw Blame History
1#!/usr/bin/env bash
2# tools/candidate/parity/run.sh: the byte-for-byte parity oracle of the candidate checkers.
3# usage: run.sh <check_candidate-bin> <check_pins-bin> [record|record-extensions]
4# Every directory under cases/candidate and cases/pins is one case: m.toml (plus amendments or a
5# files/ tree), `mode` (sha | shawords | emptysha | gh | files: how the checker reaches commits and
6# lock files, all offline stubs under mktemp) and an optional `args` file (one argument per line,
7# default m.toml). A case runs with its directory as the working directory; stdout and stderr are
8# captured in order through one pipe and the exit code is appended as "rc=N"; in gh mode the fake
9# gh's argv log follows. The expected files were recorded from the bash scripts this corpus
10# froze (scripts/check_candidate.sh and scripts/check_candidate_pins.sh at 1dd84d1) with the
11# word `record`; without it the outputs are compared and any difference is printed. A case with a
12# `limit` file is a named, documented difference (README "Limits"): it is shown, not counted as a
13# failure, and reported if it ever becomes identical.
14# Six candidate cases also assert the exact kernel-repository rename against the frozen oracle;
15# unlike a limit, any other byte difference (or the old repository name) fails.
16# A case with an `extension` file tests behaviour added after the bash was deleted (-mv sources and
17# in-tree contracts, DRA-46): its expected file comes from `record-extensions` (the .om, reviewed by
18# hand), it is counted on its own "extensions:" line and any difference fails; `record` skips it.
19set -u
20here="$(cd "$(dirname "$0")" && pwd)"
21cand="$1"; pins="$2"; mode="${3:-compare}"
22cand="$(cd "$(dirname "$cand")" && pwd)/$(basename "$cand")"
23pins="$(cd "$(dirname "$pins")" && pwd)/$(basename "$pins")"
24tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
25mkdir -p "$tmp/bin"
26cat > "$tmp/stub" <<'S'
27#!/usr/bin/env bash
28[ "$2" != "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ]
29S
30cat > "$tmp/stubw" <<'S'
31#!/usr/bin/env bash
32[ "$1" = "--flag" ] || exit 9
33[ "$3" != "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ]
34S
35cat > "$tmp/stubf" <<'S'
36#!/usr/bin/env bash
37cat "$PARITY_FILES/$1/$2/$3" 2>/dev/null || { echo "{\"message\":\"No commit found for the ref $2\"}"; exit 1; }
38S
39cat > "$tmp/stubfw" <<'S'
40#!/usr/bin/env bash
41[ "$1" = "--flag" ] || exit 9
42cat "$PARITY_FILES/$2/$3/$4" 2>/dev/null || { echo "{\"message\":\"No commit found for the ref $3\"}"; exit 1; }
43S
44cat > "$tmp/bin/gh" <<'S'
45#!/usr/bin/env bash
46echo "$*" >> "$GH_STUB_LOG"
47[ "$1" = "api" ] || exit 2
48sha="${2##*/}"
49if [ "$sha" = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef" ]; then echo "gh: Not Found (HTTP 404)" >&2; exit 1; fi
50echo "$sha"
51S
52chmod +x "$tmp/stub" "$tmp/stubw" "$tmp/stubf" "$tmp/stubfw" "$tmp/bin/gh"
53same=0; diff=0; limits=0; renames=0; xsame=0; xdiff=0
54one() { # one <group> <bin> <casedir>
55 local g="$1" bin="$2" d="$3" name; name="$(basename "$d")"
56 local m; m="$(cat "$d/mode")"
57 local args=(m.toml); [ -f "$d/args" ] && mapfile -t args < "$d/args"
58 : > "$tmp/gh.log"
59 ( cd "$d" && unset CAND_SHA_CHECK CAND_FILE_AT
60 case "$m" in
61 sha) export CAND_SHA_CHECK="$tmp/stub" ;;
62 shawords) export CAND_SHA_CHECK="$tmp/stubw --flag" ;;
63 emptysha) export CAND_SHA_CHECK="" PATH="$tmp/bin:$PATH" GH_STUB_LOG="$tmp/gh.log" ;;
64 gh) export PATH="$tmp/bin:$PATH" GH_STUB_LOG="$tmp/gh.log" ;;
65 files) export CAND_FILE_AT="$tmp/stubf" PARITY_FILES="$d/files" ;;
66 fileswords) export CAND_FILE_AT="$tmp/stubfw --flag" PARITY_FILES="$d/files" ;;
67 esac
68 "$bin" "${args[@]}" 2>&1; echo "rc=$?"
69 if [ -s "$tmp/gh.log" ]; then echo "-- gh calls --"; cat "$tmp/gh.log"; fi
70 ) | sed "s|$bin|<bin>|g" > "$tmp/out"
71 local exp="$here/expected/$g/$name.out"
72 if [ -f "$d/extension" ]; then
73 # An extension case (README "Extension cases"): behaviour the bash never had, so its expected
74 # file was recorded from the .om (record-extensions) and reviewed by hand. Counted apart from the
75 # frozen oracle; `record` (the bash oracle) never touches it.
76 if [ "$mode" = record ]; then return; fi
77 if [ "$mode" = record-extensions ]; then mkdir -p "$here/expected/$g"; cp "$tmp/out" "$exp"; xsame=$((xsame+1)); return; fi
78 if cmp -s "$tmp/out" "$exp"; then xsame=$((xsame+1))
79 else xdiff=$((xdiff+1)); echo "DIFF (extension): $g/$name"; diff "$exp" "$tmp/out" | sed 's/^/ /'; fi
80 return
81 fi
82 if [ "$mode" = record ]; then mkdir -p "$here/expected/$g"; cp "$tmp/out" "$exp"; same=$((same+1)); return; fi
83 case "$g/$name" in
84 candidate/gh-404-contract|candidate/gh-404|candidate/gh-good|candidate/gh-live-cand0|candidate/nonexistent-sha|candidate/sha-check-unset-empty)
85 # Transform only complete API/diagnostic lines, never manifest keys or arbitrary text.
86 sed -E \
87 -e 's|^(api repos/aien-dev/)aienos(/commits/[0-9a-f]{40} -q \.sha)$|\1aienux\2|' \
88 -e 's|^(FAIL: commit [0-9a-f]{40} not found in aien-dev/)aienos$|\1aienux|' \
89 "$exp" > "$tmp/renamed"
90 if ! cmp -s "$exp" "$tmp/renamed" && cmp -s "$tmp/out" "$tmp/renamed"; then
91 renames=$((renames+1)); echo "RENAME (exact delta): $g/$name"
92 else
93 diff=$((diff+1)); echo "DIFF (required exact naming delta): $g/$name"
94 diff "$tmp/renamed" "$tmp/out" | sed 's/^/ /'
95 fi
96 return ;;
97 esac
98 if cmp -s "$tmp/out" "$exp"; then
99 if [ -f "$d/limit" ]; then echo "LIMIT CLOSED (now identical, drop the limit file): $g/$name"; fi
100 same=$((same+1))
101 elif [ -f "$d/limit" ]; then
102 limits=$((limits+1)); echo "LIMIT (named, expected to differ): $g/$name: $(cat "$d/limit")"; diff "$exp" "$tmp/out" | sed 's/^/ /'
103 else diff=$((diff+1)); echo "DIFF: $g/$name"; diff "$exp" "$tmp/out" | sed 's/^/ /'; fi
104}
105for d in "$here"/cases/candidate/*/; do one candidate "$cand" "${d%/}"; done
106for d in "$here"/cases/pins/*/; do one pins "$pins" "${d%/}"; done
107echo "parity: $same identical, $renames exact naming deltas, $limits named limits, $diff different"
108echo "extensions: $xsame identical, $xdiff different"
109[ "$diff" -eq 0 ] && [ "$xdiff" -eq 0 ]