interplane.Get Omega
Read in your language

aien

aien-dev/aien

Brought by drakestapleton

branch main Branches Tags History

aien / scripts / goal_microjobs / microjobs.sh

111 lines · 2975 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: AGPL-3.0-or-later
3# Allen #13 host glue for the seeded .om microjob kernel. Shell owns files, locks and
4# processes only (the seeded language has no file or argument API). Every admission
5# decision is made by the kernel: a proposed event is appended to the persistent
6# journal only when the kernel replays plan + journal + candidate with exit 0.
7# The journal is a local, owner-private file; it is not Cortex. The Omega Cortex
8# writer integration is pending (see docs/GOAL_MICROJOBS.md).
9set -euo pipefail
10
11usage() {
12 cat >&2 <<'EOF'
13usage: microjobs.sh init STATE_DIR PLAN_FILE
14 microjobs.sh status STATE_DIR
15 microjobs.sh propose STATE_DIR TYPE NODE ATTEMPT GEN ISSUER VERDICT D0 D1 D2 D3 TOK TIME_MS
16 microjobs.sh digest TEXT
17environment: GM_KERNEL = path to the kernel built by scripts/goal_microjobs/build.sh
18EOF
19 exit 64
20}
21
22kernel() {
23 : "${GM_KERNEL:?Set GM_KERNEL to the kernel built by scripts/goal_microjobs/build.sh}"
24 "$GM_KERNEL"
25}
26
27# digest TEXT: SHA-256 of TEXT as four big-endian 64-bit words in decimal
28digest_words() {
29 local hex
30 hex=$(printf '%s' "$1" | sha256sum | cut -c1-64)
31 printf '%u %u %u %u\n' "0x${hex:0:16}" "0x${hex:16:16}" "0x${hex:32:16}" "0x${hex:48:16}"
32}
33
34with_lock() {
35 local state=$1
36 exec 9>"$state/lock"
37 flock -n 9 || { echo 'microjobs: state is locked by another writer' >&2; exit 75; }
38}
39
40test $# -ge 1 || usage
41cmd=$1
42shift
43case "$cmd" in
44 digest)
45 test $# -eq 1 || usage
46 digest_words "$1"
47 ;;
48 init)
49 test $# -eq 2 || usage
50 state=$1 plan=$2
51 if test -e "$state/plan"; then
52 echo "microjobs: $state already holds an objective" >&2
53 exit 73
54 fi
55 umask 077
56 mkdir -p "$state"
57 with_lock "$state"
58 set +e
59 kernel < "$plan" > "$state/init.out"
60 rc=$?
61 set -e
62 if test "$rc" -ne 0; then
63 cat "$state/init.out"
64 rm -f "$state/init.out"
65 exit "$rc"
66 fi
67 cp "$plan" "$state/plan.tmp"
68 : > "$state/journal"
69 sync "$state/plan.tmp" "$state/journal"
70 mv "$state/plan.tmp" "$state/plan"
71 rm -f "$state/init.out"
72 sync "$state"
73 echo "INIT $state"
74 ;;
75 status)
76 test $# -eq 1 || usage
77 state=$1
78 cat "$state/plan" "$state/journal" | kernel
79 ;;
80 propose)
81 test $# -eq 13 || usage
82 state=$1
83 shift
84 for word in "$@"; do
85 case "$word" in
86 ''|*[!0-9]*) echo "microjobs: non-decimal field '$word'" >&2; exit 64 ;;
87 esac
88 done
89 with_lock "$state"
90 seq=$(( $(wc -l < "$state/journal") + 1 ))
91 cand="$state/journal.cand"
92 cp "$state/journal" "$cand"
93 printf '3 %s %s\n' "$seq" "$*" >> "$cand"
94 set +e
95 out=$(cat "$state/plan" "$cand" | kernel)
96 rc=$?
97 set -e
98 if test "$rc" -ne 0; then
99 rm -f "$cand"
100 printf 'REJECT %s\n' "$out"
101 exit "$rc"
102 fi
103 sync "$cand"
104 mv "$cand" "$state/journal"
105 sync "$state"
106 printf 'ACCEPT %s\n' "$seq"
107 ;;
108 *)
109 usage
110 ;;
111esac