aien
aien-dev/aien
Brought by drakestapleton
aien / tests / goal_microjobs / aienet-m1 / sequence.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | # Migration adapter, aien-dev/aien#22 (shell retirement): files, locks and |
| 4 | # processes only. canon.om binds wire identities; kernel.om admits every event; |
| 5 | # OH verifies P-256 signatures and runs the independently pinned Omega verifier. |
| 6 | set -euo pipefail |
| 7 | : "${STATE:?}" "${GM_KERNEL:?}" "${CANON:?}" "${RUN:?}" "${SERVE:?}" |
| 8 | : "${KEYS:?}" "${WKEYS:?}" "${PINS:?}" "${WPINS:?}" "${RECOMPUTE:?}" |
| 9 | : "${RECOMPUTE_SHA256:?}" "${LISTEN:?}" |
| 10 | repo=${M1_REPO:-$(cd "$(dirname "$0")/../../.." && pwd)} |
| 11 | driver="$repo/scripts/goal_microjobs/microjobs.sh" |
| 12 | exec 8>"$STATE/sequence.lock" |
| 13 | flock -n 8 || exit 75 |
| 14 | pause() { |
| 15 | if test "${M1_PAUSE:-}" = "$1"; then |
| 16 | test -p "$STATE/pause.fifo" || mkfifo "$STATE/pause.fifo" |
| 17 | exec 9<>"$STATE/pause.fifo" |
| 18 | printf '%s\n' "$1" > "$STATE/paused" |
| 19 | while test -e "$STATE/paused"; do read -r -t 0.05 -u 9 _ || true; done |
| 20 | exec 9>&- |
| 21 | fi |
| 22 | } |
| 23 | status=$(bash "$driver" status "$STATE") |
| 24 | while read -r tag id st gen attempts blocked ready effect; do |
| 25 | if test "$tag" = 101 && test "$id" = 1; then break; fi |
| 26 | done <<< "$status" |
| 27 | test "$tag" = 101 && test "$id" = 1 |
| 28 | if test "$st" = 4; then echo ALREADY_ACCEPTED; exit 0; fi |
| 29 | test "$st" = 1 || test "$st" = 2 || test "$st" = 3 |
| 30 | art="$STATE/g$gen" |
| 31 | mkdir -p "$art" |
| 32 | # Kernel replay validates the journal; Omega derives the effective operand. |
| 33 | { |
| 34 | printf '1\n' |
| 35 | cat "$STATE/plan" "$STATE/journal" |
| 36 | } | "${RECOVERY:?}" > "$art/effective.next" |
| 37 | { |
| 38 | cat "$art/effective.next" |
| 39 | printf '101 1 %s %s %s %s %s %s\n' "$st" "$gen" "$attempts" "$blocked" "$ready" "$effect" |
| 40 | cat "$STATE/bindings/$gen" |
| 41 | } | "$CANON" emit > "$art/emission.next" |
| 42 | dd if="$art/emission.next" of="$art/document.next" bs=1 count=640 status=none |
| 43 | dd if="$art/emission.next" of="$art/payload.next" bs=1 skip=640 count=15 status=none |
| 44 | if test -e "$art/document"; then |
| 45 | cmp "$art/document" "$art/document.next" |
| 46 | else |
| 47 | mv "$art/document.next" "$art/document" |
| 48 | fi |
| 49 | if test -e "$art/payload"; then |
| 50 | cmp "$art/payload" "$art/payload.next" |
| 51 | else |
| 52 | mv "$art/payload.next" "$art/payload" |
| 53 | fi |
| 54 | sync "$art/document" "$art/payload" "$art" |
| 55 | rm -f "$art/emission.next" "$art/document.next" "$art/payload.next" |
| 56 | bundle="$art/job.an0b" |
| 57 | persist() { |
| 58 | if ! test -e "$bundle"; then |
| 59 | "$RUN" bundle --keys "$KEYS" --out "$bundle" --document "$art/document" --payload "$art/payload" |
| 60 | fi |
| 61 | sync "$bundle" "$art" |
| 62 | } |
| 63 | # M1-GUARD persist-before-dispatch |
| 64 | persist |
| 65 | pause persisted |
| 66 | if test "$st" = 1; then |
| 67 | bash "$driver" propose "$STATE" 1 1 1 "$gen" 20 0 0 0 0 0 0 0 |
| 68 | fi |
| 69 | pause dispatched |
| 70 | # M1-MUTANT late-persist insertion point |
| 71 | if test "${M1_STOP_AFTER_DISPATCH:-0}" = 1; then exit 0; fi |
| 72 | dag=$(sha256sum "$art/document" | cut -d' ' -f1) |
| 73 | out="$art/out" |
| 74 | mkdir -p "$out" |
| 75 | worker_pid= |
| 76 | cleanup() { |
| 77 | if test -n "$worker_pid"; then kill "$worker_pid" 2>/dev/null || true; wait "$worker_pid" 2>/dev/null || true; fi |
| 78 | } |
| 79 | trap cleanup EXIT |
| 80 | if ! test -f "$out/$dag.mjr1" || ! test -f "$out/$dag.mjv"; then |
| 81 | worker_args=(--connect "$LISTEN" --keys "$WKEYS" --pins "$WPINS" --state "$art/worker" --retry --connect-timeout 10) |
| 82 | if test -n "${M1_MISBEHAVE:-}"; then worker_args+=(--misbehave "$M1_MISBEHAVE"); fi |
| 83 | if test -n "${M1_TEST_WORKER:-}"; then |
| 84 | printf '%s\n' "${worker_args[@]}" > "$art/worker.args" |
| 85 | AN0_TEST_SERVE_ARGS="$art/worker.args" AN0_TEST_PAUSE="${M1_WORKER_PAUSE:-admitted}" AN0_TEST_READY="$art/worker.ready" \ |
| 86 | "$M1_TEST_WORKER" --ignored --exact tests::serve_crash_worker --nocapture > "$art/worker.log" 2>&1 & |
| 87 | else |
| 88 | "$SERVE" "${worker_args[@]}" > "$art/worker.log" 2>&1 & |
| 89 | fi |
| 90 | worker_pid=$! |
| 91 | printf '%s\n' "$worker_pid" > "$art/worker.pid" |
| 92 | "$RUN" run --resend "$bundle" --listen "$LISTEN" --keys "$KEYS" --pins "$PINS" --out "$out" \ |
| 93 | --recompute "$RECOMPUTE" --recompute-sha256 "$RECOMPUTE_SHA256" --accept-timeout 10 |
| 94 | wait "$worker_pid" |
| 95 | worker_pid= |
| 96 | fi |
| 97 | pause published |
| 98 | # M1-GUARD check-before-ingest |
| 99 | "$RUN" check --bundle "$bundle" --mjr1 "$out/$dag.mjr1" --mjv "$out/$dag.mjv" --keys "$KEYS" --pins "$PINS" |
| 100 | # An owner correction during network execution must win over the old response. |
| 101 | current=$(bash "$driver" status "$STATE") |
| 102 | while read -r tag id current_state current_gen rest; do |
| 103 | if test "$tag" = 101 && test "$id" = 1; then break; fi |
| 104 | done <<< "$current" |
| 105 | ingest_gen=$gen |
| 106 | # M1-GUARD generation-match |
| 107 | ingest_gen=$current_gen |
| 108 | # Recovery, not submitted wire bytes, supplies the objective and latest operand. |
| 109 | { |
| 110 | printf '1\n' |
| 111 | cat "$STATE/plan" "$STATE/journal" |
| 112 | } | "$RECOVERY" > "$art/ingest-effective" |
| 113 | read -r -a trusted < "$art/ingest-effective" |
| 114 | # M1-GUARD objective-pin |
| 115 | objective_pin="${trusted[*]:2:4}" |
| 116 | # M1-GUARD operand-pin |
| 117 | operand_pin="${trusted[*]:35:4}" |
| 118 | next=$(( $(wc -l < "$STATE/journal") + 1 )) |
| 119 | if test "$st" = 3; then next=$((next - 1)); fi |
| 120 | { |
| 121 | cat "$art/document" "$out/$dag.mjr1" "$out/$dag.mjv" |
| 122 | printf '5 1 %s 1 20 5 %s %s %s %s %s\n' "$ingest_gen" "$EXECUTOR_WORDS" "$VERIFIER_WORDS" "$next" "$objective_pin" "$operand_pin" |
| 123 | } | "$CANON" ingest > "$art/events" |
| 124 | # Each event is independently journaled via the kernel. Recovery from a torn |
| 125 | # pair proves the persisted RESULT is identical before proposing only VERIFY. |
| 126 | mapfile -t events < "$art/events" |
| 127 | test "${#events[@]}" = 2 |
| 128 | if test "$st" = 3; then |
| 129 | last=$(cat "$STATE/journal" | { mapfile -t lines; printf '%s\n' "${lines[-1]}"; }) |
| 130 | printf '2\n%s\n%s\n' "$last" "${events[0]}" | "$RECOVERY" |
| 131 | else |
| 132 | read -r _ _ rest <<< "${events[0]}" |
| 133 | bash "$driver" propose "$STATE" $rest |
| 134 | fi |
| 135 | pause resulted |
| 136 | read -r _ _ rest <<< "${events[1]}" |
| 137 | bash "$driver" propose "$STATE" $rest |
| 138 | bash "$driver" status "$STATE" |