interplane.Get Omega
Read in your language

aien

aien-dev/aien

Brought by drakestapleton

branch main Branches Tags History

aien / tests / goal_microjobs / aienet-m1 / sequence.sh

138 lines · 5571 bytes Raw Blame History
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.
6set -euo pipefail
7: "${STATE:?}" "${GM_KERNEL:?}" "${CANON:?}" "${RUN:?}" "${SERVE:?}"
8: "${KEYS:?}" "${WKEYS:?}" "${PINS:?}" "${WPINS:?}" "${RECOMPUTE:?}"
9: "${RECOMPUTE_SHA256:?}" "${LISTEN:?}"
10repo=${M1_REPO:-$(cd "$(dirname "$0")/../../.." && pwd)}
11driver="$repo/scripts/goal_microjobs/microjobs.sh"
12exec 8>"$STATE/sequence.lock"
13flock -n 8 || exit 75
14pause() {
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}
23status=$(bash "$driver" status "$STATE")
24while read -r tag id st gen attempts blocked ready effect; do
25 if test "$tag" = 101 && test "$id" = 1; then break; fi
26done <<< "$status"
27test "$tag" = 101 && test "$id" = 1
28if test "$st" = 4; then echo ALREADY_ACCEPTED; exit 0; fi
29test "$st" = 1 || test "$st" = 2 || test "$st" = 3
30art="$STATE/g$gen"
31mkdir -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"
42dd if="$art/emission.next" of="$art/document.next" bs=1 count=640 status=none
43dd if="$art/emission.next" of="$art/payload.next" bs=1 skip=640 count=15 status=none
44if test -e "$art/document"; then
45 cmp "$art/document" "$art/document.next"
46else
47 mv "$art/document.next" "$art/document"
48fi
49if test -e "$art/payload"; then
50 cmp "$art/payload" "$art/payload.next"
51else
52 mv "$art/payload.next" "$art/payload"
53fi
54sync "$art/document" "$art/payload" "$art"
55rm -f "$art/emission.next" "$art/document.next" "$art/payload.next"
56bundle="$art/job.an0b"
57persist() {
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
64persist
65pause persisted
66if test "$st" = 1; then
67 bash "$driver" propose "$STATE" 1 1 1 "$gen" 20 0 0 0 0 0 0 0
68fi
69pause dispatched
70# M1-MUTANT late-persist insertion point
71if test "${M1_STOP_AFTER_DISPATCH:-0}" = 1; then exit 0; fi
72dag=$(sha256sum "$art/document" | cut -d' ' -f1)
73out="$art/out"
74mkdir -p "$out"
75worker_pid=
76cleanup() {
77 if test -n "$worker_pid"; then kill "$worker_pid" 2>/dev/null || true; wait "$worker_pid" 2>/dev/null || true; fi
78}
79trap cleanup EXIT
80if ! 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=
96fi
97pause 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.
101current=$(bash "$driver" status "$STATE")
102while read -r tag id current_state current_gen rest; do
103 if test "$tag" = 101 && test "$id" = 1; then break; fi
104done <<< "$current"
105ingest_gen=$gen
106# M1-GUARD generation-match
107ingest_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"
113read -r -a trusted < "$art/ingest-effective"
114# M1-GUARD objective-pin
115objective_pin="${trusted[*]:2:4}"
116# M1-GUARD operand-pin
117operand_pin="${trusted[*]:35:4}"
118next=$(( $(wc -l < "$STATE/journal") + 1 ))
119if 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.
126mapfile -t events < "$art/events"
127test "${#events[@]}" = 2
128if 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"
131else
132 read -r _ _ rest <<< "${events[0]}"
133 bash "$driver" propose "$STATE" $rest
134fi
135pause resulted
136read -r _ _ rest <<< "${events[1]}"
137bash "$driver" propose "$STATE" $rest
138bash "$driver" status "$STATE"