interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / scripts / crumb-parity.sh

114 lines · 4296 bytes Raw Blame History
1#!/usr/bin/env bash
2# Parity check between synapse-crumb and the reference crumb.c (ADR 0004).
3#
4# Builds tools/crumb/crumb.c from aien-architecture at the pinned commit with `cc` only,
5# copies the fixture tree (crates/synapse-crumb/tests/fixtures/tree, where `dot.crumb`
6# stands for `.crumb` so this repository's own crumb tooling never reads the fixtures)
7# into a throwaway Git repository, runs `validate`, `sniff`, and `explain`, and compares
8# the normalised output with the golden files the Rust parity tests read.
9#
10# Usage: scripts/crumb-parity.sh [--update]
11# --update rewrite the golden files instead of comparing.
12# Env: AIEN_ARCHITECTURE_REPO path to an aien-architecture clone
13# (default: $HOME/workspace/aien-architecture).
14#
15# Dependencies: bash, git, cc, sha256sum, diff, coreutils. No jq, no Python.
16set -euo pipefail
17
18PIN_COMMIT=29780e56c9e6b9111d884e0d4b146737228c19f6
19PIN_SOURCE_SHA256=ef0f6566fb838917a750eb898f66596b76aa15a53d7d61bedac03c4c963ee147
20TARGETS=(. d1 d1/nested d1/nested/deeper d2 d3 badjson gap gap/inner unknown
21 bom scalar latin1 nuljunk dupkey surrogate)
22
23here=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)
24fixtures="$here/crates/synapse-crumb/tests/fixtures/tree"
25golden="$here/crates/synapse-crumb/tests/golden"
26arch="${AIEN_ARCHITECTURE_REPO:-$HOME/workspace/aien-architecture}"
27update=0
28case "${1:-}" in
29 --update) update=1 ;;
30 "") ;;
31 *) echo "usage: $0 [--update]" >&2; exit 2 ;;
32esac
33
34work=$(mktemp -d)
35trap 'rm -rf "$work"' EXIT
36
37# 1. Pinned source and build.
38git -C "$arch" cat-file -e "$PIN_COMMIT^{commit}" \
39 || { echo "FAIL: $arch lacks commit $PIN_COMMIT (git fetch it first)" >&2; exit 1; }
40git -C "$arch" show "$PIN_COMMIT:tools/crumb/crumb.c" > "$work/crumb.c"
41src_sha=$(sha256sum "$work/crumb.c" | cut -d' ' -f1)
42if [ "$src_sha" != "$PIN_SOURCE_SHA256" ]; then
43 echo "FAIL: crumb.c sha256 $src_sha != pinned $PIN_SOURCE_SHA256" >&2; exit 1
44fi
45cc -O2 -o "$work/crumb" "$work/crumb.c"
46bin_sha=$(sha256sum "$work/crumb" | cut -d' ' -f1)
47cc_ver=$(cc --version | head -n1)
48echo "crumb.c $PIN_COMMIT sha256 $src_sha"
49echo "binary sha256 $bin_sha ($cc_ver)"
50
51# 2. Fixture tree in a throwaway Git repository (bounds explain's upward walk at its root).
52mkdir -p "$work/tree"
53root=$(cd "$work/tree" && pwd -P)
54while IFS= read -r -d '' f; do
55 rel=${f#"$fixtures"/}
56 dest="$root/$rel"
57 [ "$(basename "$rel")" = dot.crumb ] && dest="$(dirname "$dest")/.crumb"
58 mkdir -p "$(dirname "$dest")"
59 cp "$f" "$dest"
60done < <(find "$fixtures" -type f -print0)
61git -C "$root" init -q
62export CRUMB_COORD_ROOT="$work/coord"
63mkdir -p "$CRUMB_COORD_ROOT"
64
65norm() { sed "s#$root#<ROOT>#g"; }
66out="$work/out"
67mkdir -p "$out"
68
69# 3. validate: whole tree. Exit 1 is expected (the fixtures include invalid crumbs).
70set +e
71"$work/crumb" validate "$root" > "$out/validate.raw"
72validate_rc=$?
73set -e
74{ norm < "$out/validate.raw"; echo "exit $validate_rc"; } > "$out/validate.txt"
75
76# 4. sniff: keep the directory's own .crumb lines (coordination state is not compared).
77: > "$out/sniff.txt"
78for t in "${TARGETS[@]}"; do
79 echo "### sniff $t" >> "$out/sniff.txt"
80 "$work/crumb" sniff parity "$root/$t" 2>/dev/null \
81 | sed '/^shared coordination:/,$d' | norm >> "$out/sniff.txt"
82done
83
84# 5. explain: keep NEAREST CRUMB and INHERITED INVARIANTS.
85: > "$out/explain.txt"
86for t in "${TARGETS[@]}"; do
87 echo "### explain $t" >> "$out/explain.txt"
88 "$work/crumb" explain "$root/$t" 2>/dev/null \
89 | sed -n '/^== NEAREST CRUMB ==$/,/^== SHARED COORDINATION ==$/p' \
90 | sed '$d' | norm >> "$out/explain.txt"
91done
92
93printf 'commit %s\ncrumb.c sha256 %s\n' "$PIN_COMMIT" "$PIN_SOURCE_SHA256" > "$out/PIN"
94
95if [ "$update" = 1 ]; then
96 mkdir -p "$golden"
97 cp "$out/validate.txt" "$out/sniff.txt" "$out/explain.txt" "$out/PIN" "$golden/"
98 # Build record: informational, differs by compiler, never compared.
99 printf 'binary sha256 %s\ncompiler %s\n' "$bin_sha" "$cc_ver" > "$golden/BUILD.txt"
100 echo "UPDATED golden files in $golden"
101 exit 0
102fi
103
104status=0
105for f in PIN validate.txt sniff.txt explain.txt; do
106 if diff -u "$golden/$f" "$out/$f"; then
107 echo "PASS $f"
108 else
109 echo "FAIL $f differs from golden" >&2
110 status=1
111 fi
112done
113[ "$status" = 0 ] && echo "PARITY OK: crumb.c output matches the golden files"
114exit "$status"