omega-harness
Agent skills for operating, encoding and communicating in Omega (.om) and the AIEN repositories
Brought by drakestapleton
omega-harness / tools / install.sh
| 1 | #!/bin/sh |
| 2 | # install.sh: install omega-harness from Interplane main at a recorded head, or check an installation. |
| 3 | # |
| 4 | # tools/install.sh --skills DIR [--check] [--source URL] [--rev SHA] |
| 5 | # Copy every skills/<name>/ of main into DIR (other skills in DIR are left alone), remove a skill |
| 6 | # that the head recorded in DIR shipped and main no longer ships, and record the head in |
| 7 | # DIR/.installed-from-omega-harness. With --check, change nothing: exit 1 when the recorded head |
| 8 | # is not main's head (a skill main no longer ships then still sits there) or an installed skill |
| 9 | # differs from main's copy. A DIR with no recorded head is cleaned of retired skills only after |
| 10 | # one install has recorded its head there. |
| 11 | # tools/install.sh --omc OMC|auto [--check] [--source URL] [--rev SHA] [--overlay FILE] |
| 12 | # On a Linux AArch64 host: build tools/rules with OMC (its sha256 must be PIN's; `auto` takes |
| 13 | # seed/out/omc at PIN's omega-mv commit from the Interplane omega-mv repository), then install |
| 14 | # or check every skills directory and instruction file that FILE names (default |
| 15 | # ~/.config/omega-harness/local.rules; `skills DIR` and `target PROGRAM PATH` records). |
| 16 | # cap CHARS PATH limits a target in decoded characters. All caps are checked before installation. Each |
| 17 | # instruction file is rendered from rules/rules.txt plus FILE. A file kept by aien-keep (its |
| 18 | # path resolves under /srv/aien-keep/) is replaced only through `sudo aien-keep update` (the |
| 19 | # keeper keeps the old copy); any other through a rename, after a dated backup under |
| 20 | # ~/.local/state/omega-harness/backups/. The landing trigger is the landing tools' `land-hooks.sh` |
| 21 | # `harness` hook, which runs the landed head's own `tools/install.sh --omc auto --rev <landed head>` |
| 22 | # detached, one at a time, and posts any nonzero exit to the hive BOARD. This script itself writes |
| 23 | # a failure inside its install loop to the overlay's `alarm` file; earlier failures (clone, PIN, |
| 24 | # omc, overlay) are alarmed by the hook. Nothing runs on a clock or inside Radicle storage. |
| 25 | # |
| 26 | # SOURCE is the Interplane git repository of omega-harness. --rev |
| 27 | # installs that commit instead of main's head (the post-landing hook passes the landed head). |
| 28 | # The recorded install token still begins `rad:` plus the old repository id, so a check matches |
| 29 | # copies installed before the forge move. |
| 30 | # This file is invocation glue (clone, copy, compare, rename); what is generated and how is decided |
| 31 | # by tools/rules.om. Exit 0 installed or current, 1 a check found a difference, 2 usage or a failed step. |
| 32 | set -u |
| 33 | PATH=$HOME/.local/bin:$PATH |
| 34 | RID=z39gvwWsnkRZsAConQLjaKiNVLHfW |
| 35 | check=0 sdir= omc= overlay=$HOME/.config/omega-harness/local.rules source= rev= |
| 36 | while [ $# -gt 0 ]; do |
| 37 | case $1 in |
| 38 | --check) check=1 ;; |
| 39 | --rev) rev=${2:?}; shift ;; |
| 40 | --skills) sdir=${2:?}; shift ;; |
| 41 | --omc) omc=${2:?}; shift ;; |
| 42 | --overlay) overlay=${2:?}; shift ;; |
| 43 | --source) source=${2:?}; shift ;; |
| 44 | *) sed -n '2,30s/^# \{0,1\}//p' "$0" >&2; exit 2 ;; |
| 45 | esac |
| 46 | shift |
| 47 | done |
| 48 | [ -n "$sdir$omc" ] || { sed -n '2,30s/^# \{0,1\}//p' "$0" >&2; exit 2; } |
| 49 | if [ -z "$source" ]; then |
| 50 | source=https://interplane.io/projects/omega-harness/repo.git |
| 51 | fi |
| 52 | unset GIT_DIR GIT_NAMESPACE GIT_WORK_TREE GIT_INDEX_FILE |
| 53 | export GIT_TERMINAL_PROMPT=0 |
| 54 | tmp=$(mktemp -d) || exit 2 |
| 55 | trap 'rm -rf "$tmp"' EXIT |
| 56 | git clone -q --no-checkout "$source" "$tmp/h" || { echo "install: cannot clone $source" >&2; exit 2; } |
| 57 | [ -n "$rev" ] || rev=$(git -C "$tmp/h" rev-parse refs/remotes/origin/main) || exit 2 |
| 58 | git -C "$tmp/h" cat-file -e "$rev^{commit}" 2>/dev/null || |
| 59 | git -C "$tmp/h" fetch -q --upload-pack="git -c uploadpack.allowAnySHA1InWant=true upload-pack" origin "$rev" || |
| 60 | { echo "install: cannot fetch $rev from $source" >&2; exit 2; } |
| 61 | git -C "$tmp/h" checkout -q --detach "$rev" || { echo "install: $rev is not in $source" >&2; exit 2; } |
| 62 | head=$(git -C "$tmp/h" rev-parse HEAD) |
| 63 | mark="rad:$RID@$head" |
| 64 | rc=0 |
| 65 | |
| 66 | # retired DIR: the skills this repository shipped at DIR's recorded head that it no longer ships (what |
| 67 | # an earlier install put there and nobody else did); nothing when no head is recorded or it is unknown |
| 68 | retired() { |
| 69 | p=$(head -n 1 "$1/.installed-from-omega-harness" 2>/dev/null | sed -n "s/^rad:$RID@\([0-9a-f]\{40\}\)\$/\1/p") |
| 70 | [ -n "$p" ] && git -C "$tmp/h" cat-file -e "$p^{commit}" 2>/dev/null || return 0 |
| 71 | git -C "$tmp/h" ls-tree -d --name-only "$p" skills/ | while read -r o; do |
| 72 | n=$(basename "$o") |
| 73 | [ -d "$tmp/h/skills/$n" ] || echo "$n" |
| 74 | done |
| 75 | } |
| 76 | |
| 77 | # skills DIR: copy the repository's skills into DIR (or a staged copy of DIR), remove the skills an |
| 78 | # earlier install put there that the repository no longer ships, and mark the head |
| 79 | put_skills() { |
| 80 | for n in $(retired "$1"); do |
| 81 | rm -rf "${1:?}/$n" && echo "REMOVED $1/$n (no longer in the repository)" |
| 82 | done |
| 83 | for s in "$tmp/h"/skills/*/; do |
| 84 | n=$(basename "$s") |
| 85 | rm -rf "$1/.$n.new" && cp -R "$s" "$1/.$n.new" || return 1 |
| 86 | if [ -e "$1/$n" ] || [ -L "$1/$n" ]; then mv "$1/$n" "$1/.$n.old" || return 1; fi |
| 87 | mv "$1/.$n.new" "$1/$n" && rm -rf "$1/.$n.old" || return 1 |
| 88 | done |
| 89 | printf '%s\n' "$mark" > "$1/.installed-from-omega-harness" |
| 90 | } |
| 91 | |
| 92 | do_skills() { |
| 93 | d=$1 |
| 94 | if [ "$check" = 1 ]; then |
| 95 | got=$(head -n 1 "$d/.installed-from-omega-harness" 2>/dev/null) |
| 96 | if [ "$got" != "$mark" ]; then |
| 97 | echo "BEHIND $d: installed ${got:-nothing recorded}, main $mark"; rc=1; return |
| 98 | fi |
| 99 | for s in "$tmp/h"/skills/*/; do |
| 100 | n=$(basename "$s") |
| 101 | diff -rq "$s" "$d/$n" > /dev/null 2>&1 || { echo "MODIFIED $d/$n: differs from $mark"; rc=1; } |
| 102 | done |
| 103 | return |
| 104 | fi |
| 105 | case $(readlink -f "$d") in |
| 106 | /srv/aien-keep/*) |
| 107 | rm -rf "$tmp/stage" && mkdir -p "$tmp/stage" || { rc=2; return; } |
| 108 | cp -a "$(readlink -f "$d")/." "$tmp/stage/" || { rc=2; return; } |
| 109 | chmod -R u+w "$tmp/stage" |
| 110 | put_skills "$tmp/stage" || { echo "install: staging $d failed" >&2; rc=2; return; } |
| 111 | sudo -n aien-keep update "$d" "$tmp/stage" > /dev/null || { echo "install: aien-keep update $d failed" >&2; rc=2; return; } |
| 112 | ;; |
| 113 | *) |
| 114 | mkdir -p "$d" && put_skills "$d" || { echo "install: $d failed" >&2; rc=2; return; } |
| 115 | ;; |
| 116 | esac |
| 117 | echo "INSTALLED $d $mark" |
| 118 | } |
| 119 | |
| 120 | if [ -z "$omc" ]; then |
| 121 | do_skills "$sdir" |
| 122 | exit $rc |
| 123 | fi |
| 124 | |
| 125 | [ -f "$tmp/h/PIN" ] && [ -f "$tmp/h/tools/rules.om" ] || |
| 126 | { echo "install: $head has no PIN or tools/rules.om (install from a commit that has them)" >&2; exit 2; } |
| 127 | if [ "$omc" = auto ]; then |
| 128 | # the compiler PIN names, taken from the Interplane omega-mv repository |
| 129 | mvrev=$(sed -n 's/^omega-mv //p' "$tmp/h/PIN") |
| 130 | git clone -q --bare "https://interplane.io/projects/omega-mv/repo.git" "$tmp/mv" && mvs=$tmp/mv |
| 131 | git --git-dir "$mvs" cat-file blob "$mvrev:seed/out/omc" > "$tmp/omc" && chmod +x "$tmp/omc" || |
| 132 | { echo "install: cannot read seed/out/omc at omega-mv $mvrev" >&2; exit 2; } |
| 133 | omc=$tmp/omc |
| 134 | fi |
| 135 | pinned=$(sed -n 's/^omc-sha256 //p' "$tmp/h/PIN") |
| 136 | got=$(sha256sum "$omc" 2>/dev/null | cut -d' ' -f1) |
| 137 | [ -n "$pinned" ] && [ "$got" = "$pinned" ] || { echo "install: omc sha256 ${got:-unreadable}, PIN wants $pinned" >&2; exit 2; } |
| 138 | "$omc" < "$tmp/h/tools/rules.om" > "$tmp/rules" && chmod +x "$tmp/rules" || { echo "install: tools/rules.om does not compile" >&2; exit 2; } |
| 139 | [ -f "$overlay" ] || { echo "install: no overlay $overlay" >&2; exit 2; } |
| 140 | rhead=$(git -C "$tmp/h" log -1 --format=%H -- rules tools/rules.om) |
| 141 | "$tmp/rules" targets "$tmp/h/rules/rules.txt" "$overlay" > "$tmp/targets" || { cat "$tmp/targets" >&2; echo "install: rules targets failed" >&2; exit 2; } |
| 142 | alarm=$(sed -n 's/^alarm - //p' "$tmp/targets" | head -n 1) |
| 143 | "$tmp/rules" fit "$tmp/h/rules/rules.txt" "$overlay" "$rhead" > "$tmp/fit" || { |
| 144 | cat "$tmp/fit" >&2 |
| 145 | echo "install: target capacity check failed; nothing installed" >&2 |
| 146 | [ "$check" = 0 ] && [ -n "$alarm" ] && |
| 147 | echo "$(date -u +%FT%TZ) omega-harness: tools/install.sh at $head refused: $(grep -v ' ok$' "$tmp/fit" | head -n 1)" >> "$alarm" |
| 148 | exit 2 |
| 149 | } |
| 150 | while read -r kind prog path; do |
| 151 | case $kind in |
| 152 | skills) do_skills "$path" < /dev/null ;; |
| 153 | target) |
| 154 | "$tmp/rules" render "$tmp/h/rules/rules.txt" "$overlay" "$prog" "$rhead" "$path" > "$tmp/out" || { rc=2; continue; } |
| 155 | cmp -s "$tmp/out" "$path" && continue |
| 156 | if [ "$check" = 1 ]; then echo "DIFFERS $path: not the rendering of rules at $rhead"; rc=1; continue; fi |
| 157 | case $(readlink -f "$path") in |
| 158 | /srv/aien-keep/*) |
| 159 | sudo -n aien-keep update "$path" "$tmp/out" > /dev/null < /dev/null || { echo "install: aien-keep update $path failed" >&2; rc=2; continue; } ;; |
| 160 | *) |
| 161 | bk=${XDG_STATE_HOME:-$HOME/.local/state}/omega-harness/backups |
| 162 | mkdir -p "$bk" && [ ! -e "$path" ] || cp -p "$path" "$bk/$(printf '%s' "$path" | tr / _).$(date -u +%Y%m%dT%H%M%SZ)" || { rc=2; continue; } |
| 163 | cp "$tmp/out" "$path.generated-tmp" && mv "$path.generated-tmp" "$path" || { rc=2; continue; } ;; |
| 164 | esac |
| 165 | cmp -s "$tmp/out" "$path" || { echo "install: $path does not read back" >&2; rc=2; continue; } |
| 166 | echo "INSTALLED $path rules@$rhead" ;; |
| 167 | esac |
| 168 | done < "$tmp/targets" |
| 169 | if [ "$rc" = 2 ] && [ "$check" = 0 ] && [ -n "$alarm" ]; then |
| 170 | echo "$(date -u +%FT%TZ) omega-harness: tools/install.sh at $head FAILED (exit 2); rerun it to see why" >> "$alarm" |
| 171 | fi |
| 172 | exit $rc |