interplane-mv
INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.
Brought by drakestapleton
interplane-mv / adapters / aien / scripts / test-fix-the-test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Fix-the-test slice gate (VAC M3b) for the INTERPLANE AIEN adapter. |
| 3 | # |
| 4 | # Runs the live row `slice1_fix_the_test_lands_and_tests_pass` against a real `aien-cli daemon` |
| 5 | # (~6.5 min on the CPU reference backend): the fixture's `make test` fails, a SCRIPTED proposal |
| 6 | # carrying the corrected file goes through the daemon's approved ledger, `make test` passes. The |
| 7 | # exported bundle is then judged by the offline provenance verifier and tampered copies must each |
| 8 | # be refused with their specific code. It cannot pass vacuously: a missing row, a failed test, a |
| 9 | # missing bundle or an unexpected verdict fails it. |
| 10 | # |
| 11 | # Expected verdict for a scripted run (never `PASS complete`, the model turn is scripted): |
| 12 | # daemon reports compose_native (sovereign-core #346 or later): |
| 13 | # PASS_LABELLED_INCOMPLETE missing=link:model_turn effect=aien-ledger-slice/1:strong proposal=scripted_turn |
| 14 | # daemon without the field (older build): the same with missing=link:model_turn,link:native |
| 15 | # |
| 16 | # Inputs (environment): AIEN_BIN (required), OUT (required, fresh), AIEN_LEDGER_MODEL_DIR (default: |
| 17 | # the unsloth Llama-3.2-1B-Instruct snapshot), SOVEREIGN_CORE_REV (caller-asserted), OMEGA_LOCK_REV. |
| 18 | # No process is killed here; the daemon is the test's own. |
| 19 | set -euo pipefail |
| 20 | |
| 21 | here="$(cd "$(dirname "$0")" && pwd)" |
| 22 | adapter="$(cd "$here/.." && pwd)" |
| 23 | repo="$(cd "$adapter/../.." && pwd)" |
| 24 | die() { echo "SLICE GATE FAIL: $*" >&2; exit 1; } |
| 25 | |
| 26 | : "${AIEN_BIN:?AIEN_BIN must name the aien-cli binary}" |
| 27 | : "${OUT:?OUT must name the output directory}" |
| 28 | [ -x "$AIEN_BIN" ] || die "AIEN_BIN not executable: $AIEN_BIN" |
| 29 | command -v jq >/dev/null || die "jq is required" |
| 30 | command -v make >/dev/null || die "make is required" |
| 31 | model_dir="${AIEN_LEDGER_MODEL_DIR:-$HOME/.cache/huggingface/hub/models--unsloth--Llama-3.2-1B-Instruct/snapshots/5a8abab4a5d6f164389b1079fb721cfab8d7126c}" |
| 32 | for f in model.safetensors tokenizer.json config.json; do |
| 33 | [ -f "$model_dir/$f" ] || die "model dir lacks $f: $model_dir" |
| 34 | done |
| 35 | if command -v quietlock >/dev/null && ! quietlock check >/dev/null 2>&1; then |
| 36 | die "quiet flag is held (quietlock check): no long run now" |
| 37 | fi |
| 38 | mkdir -p "$OUT" |
| 39 | OUT="$(cd "$OUT" && pwd)" |
| 40 | [ ! -e "$OUT/rows" ] || die "$OUT/rows exists: use a fresh OUT (a sealed run is never overwritten)" |
| 41 | started="$(date -u +%FT%TZ)" |
| 42 | |
| 43 | # 1. The live row. |
| 44 | log="$OUT/live.log" |
| 45 | set +e |
| 46 | (cd "$adapter" && AIEN_BIN="$AIEN_BIN" AIEN_LEDGER_MODEL_DIR="$model_dir" LEDGER_OUT="$OUT/rows" \ |
| 47 | cargo test --locked --test compose_ledger -- --ignored --test-threads=1 slice1_) >"$log" 2>&1 |
| 48 | rc=$? |
| 49 | set -e |
| 50 | [ "$rc" -eq 0 ] || { tail -5 "$log" >&2; die "live row exited $rc (log $log)"; } |
| 51 | grep -q '^test slice1_fix_the_test_lands_and_tests_pass \.\.\. ok$' "$log" || die "the slice row did not run and pass" |
| 52 | [ "$(grep -c '^test .* \.\.\. FAILED$' "$log" || true)" -eq 0 ] || die "a live test failed" |
| 53 | |
| 54 | # 2. The verifier on the exported bundle. |
| 55 | bundle="$OUT/rows/slice1/bundle" |
| 56 | [ -f "$bundle/COMPANION.json" ] || die "the row exported no bundle: $bundle/COMPANION.json" |
| 57 | verify() { (cd "$repo/provenance" && cargo run -q --locked --release -- verify "$1" 2>&1 || true) | tail -1; } |
| 58 | strong="effect=aien-ledger-slice/1:strong proposal=scripted_turn" |
| 59 | native_claimed="$(jq -r '.aien.native.claimed // "absent"' "$bundle/COMPANION.json")" |
| 60 | case "$native_claimed" in |
| 61 | true) want="PASS_LABELLED_INCOMPLETE missing=link:model_turn $strong"; native_label="proven: daemon reports compose_native=true and the verifier checked the retained report";; |
| 62 | false) want="PASS_LABELLED_INCOMPLETE missing=link:model_turn,link:native $strong"; native_label="link:native missing: the daemon reports a STUB compose library";; |
| 63 | absent) want="PASS_LABELLED_INCOMPLETE missing=link:model_turn,link:native $strong"; native_label="link:native missing: this daemon build does not report compose_native";; |
| 64 | *) die "unreadable aien.native.claimed: $native_claimed";; |
| 65 | esac |
| 66 | verdict="$(verify "$bundle")" |
| 67 | [ "$verdict" = "$want" ] || die "unexpected verdict on the slice bundle: $verdict (wanted: $want)" |
| 68 | before_exit="$(jq -r .test_exit_before "$OUT/rows/slice1/receipt.json")" |
| 69 | after_exit="$(jq -r .test_exit_after "$OUT/rows/slice1/receipt.json")" |
| 70 | [ "$before_exit" != "0" ] && [ "$before_exit" != "null" ] || die "test exit before the fix was $before_exit (must be non-zero)" |
| 71 | [ "$after_exit" = "0" ] || die "test exit after the fix was $after_exit (must be 0)" |
| 72 | |
| 73 | # 3. Tamper checks on hard-linked copies (a changed file is always written fresh, never in place). |
| 74 | neg="$OUT/negative" |
| 75 | mkdir -p "$neg" |
| 76 | fork() { rm -rf "$neg/$1"; cp -al "$bundle" "$neg/$1"; } |
| 77 | manifest() { # bundle jq-filter [jq args...]: rewrite COMPANION.json |
| 78 | local d="$1" f="$2"; shift 2 |
| 79 | jq "$@" "$f" "$d/COMPANION.json" >"$d/COMPANION.tmp" && mv "$d/COMPANION.tmp" "$d/COMPANION.json" |
| 80 | } |
| 81 | recpath() { jq -r ".records.$2.path" "$1/COMPANION.json"; } |
| 82 | restamp() { # bundle record: set the manifest's sha256 and size to the file as it is now |
| 83 | local d="$1" n="$2" p; p="$(recpath "$d" "$n")" |
| 84 | manifest "$d" '.records[$n].sha256=$s | .records[$n].bytes=$b' --arg n "$n" \ |
| 85 | --arg s "$(sha256sum "$d/$p" | cut -d' ' -f1)" --argjson b "$(wc -c <"$d/$p")" |
| 86 | } |
| 87 | editrec() { # bundle record jq-filter [jq args...]: edit a JSON record as a fresh file, restamp |
| 88 | local d="$1" n="$2" f="$3" p; shift 3; p="$(recpath "$d" "$n")" |
| 89 | jq "$@" "$f" "$d/$p" >"$d/$p.new"; rm "$d/$p"; mv "$d/$p.new" "$d/$p" |
| 90 | restamp "$d" "$n" |
| 91 | } |
| 92 | expect() { # name bundle code |
| 93 | local v; v="$(verify "$2")" |
| 94 | case "$v" in "FAIL $3:"*) echo "negative $1: $v";; *) die "negative $1: wanted FAIL $3, got: $v";; esac |
| 95 | echo "$1 $v" >>"$neg/verdicts.txt" |
| 96 | } |
| 97 | : >"$neg/verdicts.txt" |
| 98 | fork control |
| 99 | [ "$(verify "$neg/control")" = "$verdict" ] || die "negative control: an untouched copy no longer verifies" |
| 100 | echo "control $verdict" >>"$neg/verdicts.txt" |
| 101 | other="9999999999999999999999999999999999999999999999999999999999999999" |
| 102 | fork exit_code; editrec "$neg/exit_code" test_run_record '.exit_code = 1'; expect changed_exit_code "$neg/exit_code" test_run_mismatch |
| 103 | fork stdout_digest; editrec "$neg/stdout_digest" test_run_record '.stdout_sha256 = $o' --arg o "$other"; expect swapped_stdout_digest "$neg/stdout_digest" test_run_mismatch |
| 104 | fork blob_after; editrec "$neg/blob_after" test_run_record '.target_blob_sha256_after = $o' --arg o "$other"; expect wrong_target_blob "$neg/blob_after" test_run_mismatch |
| 105 | fork dropped; manifest "$neg/dropped" 'del(.test_run)'; expect dropped_test_run "$neg/dropped" test_run_missing |
| 106 | fork pin; editrec "$neg/pin" source_pin '.target_blob_sha256 = $o' --arg o "$other"; expect tampered_source_pin "$neg/pin" source_pin_mismatch |
| 107 | fork scope; editrec "$neg/scope" task '.target_path = "test/test_clamp.c"'; expect write_outside_task_scope "$neg/scope" task_scope_mismatch |
| 108 | fork forged_native |
| 109 | if [ "$native_claimed" = "true" ]; then |
| 110 | # a native claim whose evidence (the retained ComposeRecall report) is removed |
| 111 | manifest "$neg/forged_native" 'del(.records.compose_recall)' |
| 112 | else |
| 113 | # native:true asserted over a stub or absent recall report, with the missing-label edited to match |
| 114 | manifest "$neg/forged_native" '.aien.native = {claimed: true, omega_sha: "6c6180cf378075b61291f4565d226eba38b4decd"} | .completeness.missing -= ["link:native"]' |
| 115 | fi |
| 116 | expect forged_native "$neg/forged_native" native_claim_contradicted |
| 117 | |
| 118 | # 4. Receipt. |
| 119 | finished="$(date -u +%FT%TZ)" |
| 120 | omega="${OMEGA_LOCK_REV:-}" |
| 121 | if [ -z "$omega" ] && [ -n "${AIEN_OMEGA_COMPOSE_DIR:-}" ]; then |
| 122 | omega="$(git -C "$AIEN_OMEGA_COMPOSE_DIR" rev-parse HEAD 2>/dev/null || true)" |
| 123 | fi |
| 124 | dirty=false; [ -z "$(git -C "$repo" status --porcelain -- adapters provenance 2>/dev/null)" ] || dirty=true |
| 125 | jq -n \ |
| 126 | --arg adapter_commit "$(git -C "$repo" rev-parse HEAD)" --argjson adapter_dirty "$dirty" \ |
| 127 | --arg bin_sha "$(sha256sum "$AIEN_BIN" | cut -d' ' -f1)" \ |
| 128 | --arg verifier_sha "$(sha256sum "${CARGO_TARGET_DIR:-$repo/provenance/target}/release/interplane-provenance" 2>/dev/null | cut -d' ' -f1)" \ |
| 129 | --arg sc_rev "${SOVEREIGN_CORE_REV:-UNVERIFIED}" --arg omega "${omega:-UNVERIFIED}" \ |
| 130 | --arg verdict "$verdict" --arg native_label "$native_label" \ |
| 131 | --argjson run "$(jq '{task_id, source_commit, tree_commit_after, test_exit_before, test_exit_after, target_blob_sha256_before, target_blob_sha256_after}' "$OUT/rows/slice1/receipt.json")" \ |
| 132 | --argjson native "$(jq '.aien.native // null' "$bundle/COMPANION.json")" \ |
| 133 | --arg companion_sha "$(sha256sum "$bundle/COMPANION.json" | cut -d' ' -f1)" \ |
| 134 | --arg negatives "$(cat "$neg/verdicts.txt")" --arg started "$started" --arg finished "$finished" \ |
| 135 | '{kind: "interplane-aien-fix-the-test-receipt", adapter_commit: $adapter_commit, adapter_dirty: $adapter_dirty, |
| 136 | aien_cli_sha256: $bin_sha, verifier_sha256: $verifier_sha, sovereign_core_rev: $sc_rev, |
| 137 | omega_lock: $omega, backend: "CPU reference (as printed in the daemon log)", |
| 138 | task: $run, native: $native, native_label: $native_label, |
| 139 | verdict: $verdict, bundle_companion_sha256: $companion_sha, |
| 140 | negative_checks: ($negatives | split("\n") | map(select(length>0))), |
| 141 | started: $started, finished: $finished, |
| 142 | limits: ["the model turn is scripted: proposal authorship is not claimed", |
| 143 | "the test run is harness evidence, not a daemon effect; the daemon only wrote the file", |
| 144 | "records are unsigned exports; the verifier cannot tell an export from a hand-written file", |
| 145 | "desk MAC for ComposeAuthorize stays default-off (not changed here)", |
| 146 | "make exits 2 (not 1) on a failing recipe: the gate requires non-zero before and 0 after"]}' >"$OUT/receipt.json" |
| 147 | echo "SLICE GATE PASS: verdict: $verdict; receipt $OUT/receipt.json" |