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 / m5-propose-and-judge.sh
| 1 | #!/usr/bin/env bash |
| 2 | # VAC M5 hook: `m5-propose-and-judge.sh <task-workspace> <out-dir>`, run by the live row |
| 3 | # `m5_rsi_judged_change_lands_and_rolls_back`. Writes <out>/proposal.json, receipt.json and |
| 4 | # policy.json. It decides nothing: the harness checks the receipt before anything reaches the |
| 5 | # daemon, and the daemon's normal approval applies after that. |
| 6 | # |
| 7 | # 1. The RSI engine runs as RSI_USER on a read-only copy of the workspace and prints one proposal. |
| 8 | # 2. A proposal for any file other than README.md is refused here. |
| 9 | # 3. The operator builds the candidate tree: the parent copy with the proposed README. |
| 10 | # 4. The judge runs as JUDGE_USER on its own copies (parent, candidate, holdouts, policy, its own |
| 11 | # binary) inside its mode-700 home, builds both trees itself into a fresh target directory, |
| 12 | # runs the holdouts in the jail and signs a version 2 receipt with its own key. |
| 13 | # |
| 14 | # M5_PROPOSAL_FILE=<file>: judge a model-turn proposal instead of running `spark-rsi propose`. |
| 15 | # Environment: SPARK_RSI_DIR (holds release `spark-rsi` and `spark-rsi-judge`), M5_POLICY (the |
| 16 | # pinned policy file), M5_HOLDOUTS (holdout suite directory); JUDGE_USER, RSI_USER, JUDGE_HOME. |
| 17 | # Negative tests only (the gate script uses these to make counterfeit evidence, never the row): |
| 18 | # M5_SUBJECT_OVERRIDE=<file> judges that file as the README instead of the proposal; |
| 19 | # M5_JUDGE_KEY_OVERRIDE=<file> signs with that key instead of the judge's own; |
| 20 | # M5_RECEIPT_V1=1 asks for a version 1 receipt (no binding). All three are ignored unless |
| 21 | # M5_GATE_NEGATIVE=1 (the gate sets it for its negatives; the live row strips all four). |
| 22 | # Exit 0 when a receipt was written (admitted or not: the harness reads it), non-zero otherwise. |
| 23 | set -euo pipefail |
| 24 | die() { echo "M5 HOOK FAIL: $*" >&2; exit 1; } |
| 25 | ws="${1:?workspace}"; out="${2:?out dir}" |
| 26 | : "${SPARK_RSI_DIR:?}" "${M5_POLICY:?}" "${M5_HOLDOUTS:?}" |
| 27 | judge_user="${JUDGE_USER:-aien-judge}" |
| 28 | rsi_user="${RSI_USER:-aien-rsi}" |
| 29 | judge_home="${JUDGE_HOME:-/var/lib/aien-judge}" |
| 30 | target="README.md" |
| 31 | as_judge() { sudo -n -u "$judge_user" "$@"; } |
| 32 | command -v jq >/dev/null || die "jq is required" |
| 33 | if [ "${M5_GATE_NEGATIVE:-0}" != 1 ]; then |
| 34 | unset M5_SUBJECT_OVERRIDE M5_JUDGE_KEY_OVERRIDE M5_RECEIPT_V1 |
| 35 | fi |
| 36 | # The judged parent is the workspace's commit, exported by git itself (so ignored, untracked or |
| 37 | # flagged files in the folder never reach the judge). Git runs with no hook or fsmonitor program |
| 38 | # from the workspace's own config. |
| 39 | g() { git -c core.fsmonitor= -c core.hooksPath=/dev/null -c core.attributesFile=/dev/null -C "$ws" "$@"; } |
| 40 | [ "$(g rev-parse --show-toplevel 2>/dev/null)" = "$(cd "$ws" && pwd -P)" ] \ |
| 41 | || die "workspace is not the top of its own git repository: refused" |
| 42 | ws_status="$(g status --porcelain --untracked-files=all)" || die "cannot read the workspace status" |
| 43 | [ -z "$ws_status" ] || die "workspace has uncommitted changes: the judged parent would not be its commit" |
| 44 | parent_commit="$(g rev-parse --verify 'HEAD^{commit}')" || die "workspace has no commit" |
| 45 | # Attributes could make the export differ from the commit (export-ignore, export-subst). |
| 46 | tree_files="$(g ls-tree -r --name-only "$parent_commit")" || die "cannot list the task commit" |
| 47 | ! grep -Eq '(^|/)\.gitattributes$' <<<"$tree_files" || die "the task commit carries .gitattributes: refused" |
| 48 | [ ! -s "$(g rev-parse --path-format=absolute --git-path info/attributes)" ] || die "the workspace has info/attributes: refused" |
| 49 | |
| 50 | export_commit() { # dst: the parent commit's tree, exactly |
| 51 | mkdir -p "$1"; g archive --format=tar "$parent_commit" | tar -C "$1" -xf - |
| 52 | } |
| 53 | copy_tree() { # src dst: a staged tree |
| 54 | mkdir -p "$2"; tar -C "$1" -cf - . | tar -C "$2" -xf - |
| 55 | } |
| 56 | |
| 57 | stage="$(mktemp -d /tmp/aien-m5.XXXXXX)" |
| 58 | trap 'chmod -R u+w "$stage" 2>/dev/null; rm -rf "$stage"' EXIT |
| 59 | chmod 755 "$stage" |
| 60 | mkdir "$stage/bin" |
| 61 | cp "$SPARK_RSI_DIR/spark-rsi" "$stage/bin/spark-rsi" |
| 62 | export_commit "$stage/view" |
| 63 | [ -z "$(find "$stage/view" -type l -print -quit)" ] || die "the task commit contains a symlink: refused" |
| 64 | chmod -R a+rX,a-w "$stage/view" "$stage/bin" |
| 65 | |
| 66 | # 1. Propose. Normally the RSI account, from a read-only view. With M5_PROPOSAL_FILE (the model-turn |
| 67 | # row) the proposal is the whole-file change a real model turn produced, written by the harness as |
| 68 | # a proposal.json-shaped file. Every later step is the same: the target check, the candidate tree |
| 69 | # and the judge, which builds and evaluates the exact bytes on holdouts the proposer cannot read. |
| 70 | if [ -n "${M5_PROPOSAL_FILE:-}" ]; then |
| 71 | [ -f "$M5_PROPOSAL_FILE" ] || die "M5_PROPOSAL_FILE is not a file: $M5_PROPOSAL_FILE" |
| 72 | cp "$M5_PROPOSAL_FILE" "$out/proposal.json" |
| 73 | else |
| 74 | sudo -n -u "$rsi_user" env -i HOME=/nonexistent PATH=/usr/bin:/bin \ |
| 75 | "$stage/bin/spark-rsi" propose "$stage/view" >"$out/proposal.json" \ |
| 76 | || die "spark-rsi propose failed" |
| 77 | fi |
| 78 | # 2. Only the task's file. |
| 79 | got="$(jq -r '.target_file // "none"' "$out/proposal.json")" |
| 80 | [ "$got" = "$target" ] || die "RSI proposed a change to $got; only $target is allowed: refused" |
| 81 | |
| 82 | # 3. Candidate = parent + the proposed file, byte for byte. |
| 83 | copy_tree "$stage/view" "$stage/candidate" |
| 84 | chmod -R u+w "$stage/candidate" |
| 85 | jq -e '.proposed_patch | type == "string"' "$out/proposal.json" >/dev/null \ |
| 86 | || die "the proposal carries no whole-file content" |
| 87 | jq -j '.proposed_patch' "$out/proposal.json" >"$stage/candidate/$target" |
| 88 | if [ -n "${M5_SUBJECT_OVERRIDE:-}" ]; then cp "$M5_SUBJECT_OVERRIDE" "$stage/candidate/$target"; fi |
| 89 | chmod -R a+rX "$stage/candidate" |
| 90 | |
| 91 | # 4. The judge, on its own copies. |
| 92 | run="m5-$(date -u +%Y%m%dT%H%M%SZ)-$$" |
| 93 | jdir="$judge_home/m5/$run" |
| 94 | as_judge mkdir -p "$jdir/parent" "$jdir/candidate" "$jdir/holdouts" "$jdir/out" |
| 95 | send() { tar -C "$1" -cf - . | as_judge tar -C "$2" -xf -; } |
| 96 | send "$stage/view" "$jdir/parent" |
| 97 | send "$stage/candidate" "$jdir/candidate" |
| 98 | send "$M5_HOLDOUTS" "$jdir/holdouts" |
| 99 | as_judge sh -c 'cat > "$1"' _ "$jdir/policy.json" <"$M5_POLICY" |
| 100 | as_judge sh -c 'cat > "$1" && chmod 700 "$1"' _ "$jdir/spark-rsi-judge" <"$SPARK_RSI_DIR/spark-rsi-judge" |
| 101 | candidate_id="$(jq -r .id "$out/proposal.json")" |
| 102 | keyfile="$judge_home/judge.key" |
| 103 | if [ -n "${M5_JUDGE_KEY_OVERRIDE:-}" ]; then |
| 104 | keyfile="$jdir/override.key" |
| 105 | as_judge sh -c 'umask 077; cat > "$1"' _ "$keyfile" <"$M5_JUDGE_KEY_OVERRIDE" |
| 106 | fi |
| 107 | bind=(--subject-path "$target" --policy-file "$jdir/policy.json") |
| 108 | want='.format_version == 2 and (.signature | type == "string")' |
| 109 | if [ "${M5_RECEIPT_V1:-0}" = 1 ]; then |
| 110 | bind=(); want='(.format_version // 1) == 1 and (.signature | type == "string")' |
| 111 | fi |
| 112 | set +e |
| 113 | as_judge env -i HOME="$judge_home" RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}" \ |
| 114 | CARGO_HOME="$judge_home/.cargo" PATH="${CARGO_BIN_DIR:-$HOME/.cargo/bin}:/usr/bin:/bin" \ |
| 115 | "$jdir/spark-rsi-judge" --cycle-id "$run" --candidate-id "$candidate_id" \ |
| 116 | --parent-id "$parent_commit" --candidate-path "$jdir/candidate" --parent-path "$jdir/parent" \ |
| 117 | --holdouts-dir "$jdir/holdouts" --output-dir "$jdir/out" \ |
| 118 | --signing-key-file "$keyfile" "${bind[@]}" --executable rsi-dashes --build-release \ |
| 119 | >"$out/receipt.json" 2>"$out/judge.stderr" |
| 120 | rc=$? |
| 121 | set -e |
| 122 | jq -e "$want" "$out/receipt.json" >/dev/null 2>&1 \ |
| 123 | || die "the judge wrote no receipt of the expected version (exit $rc): $(tail -3 "$out/judge.stderr")" |
| 124 | cp "$M5_POLICY" "$out/policy.json" |
| 125 | echo "M5 HOOK: judge exit $rc, receipt $out/receipt.json (judge dir $jdir)" |