interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / tools / aien-local-ci / registry-refresh.sh

151 lines · 8624 bytes Raw Blame History
1#!/usr/bin/env bash
2# registry-refresh.sh: add to the judge's read-only cargo registry the crates a trusted Cargo.lock names
3# that it does not have yet (issue #622). The judge builds with `--locked --offline` against
4# /var/lib/aien-run/cargo/registry, which install.sh seeded once from the operator's ~/.cargo; nothing
5# refreshed it, so every PR that adds or bumps a crate failed on the judge. This is the operator-run refresh
6# step. The trust model is unchanged: the registry stays root-owned and read-only to aien-run, nothing here
7# touches the network, and the source of every byte is the operator's own cache.
8#
9# bash tools/aien-local-ci/registry-refresh.sh <trusted Cargo.lock> [--dry-run]
10#
11# For each crates.io package in the lockfile that is missing from the judge registry: copy the .crate from
12# the operator's ~/.cargo cache, refuse unless its sha256 equals the lockfile checksum, unpack that verified
13# .crate into registry/src the way cargo does (with its .cargo-ok marker), then copy the matching index-cache
14# entry. The unpacked source is required: the judge's CARGO_HOME is read-only, so its cargo cannot unpack a
15# .crate itself and fails with "failed to unpack package ... Permission denied". A crate whose .crate is
16# already in the judge cache but whose source was never unpacked (the state an older refresh left) gets its
17# source unpacked from the judge's own .crate after the same checksum check. Prints what it did and appends
18# one journal line so the next bundle can be traced to the registry state it ran with. Never deletes or
19# replaces an existing crate file or source directory.
20#
21# Test overrides (tests/registry_refresh_test.sh uses them on throwaway directories, no sudo):
22# AIEN_JUDGE_REGISTRY, AIEN_OPERATOR_REGISTRY, AIEN_JOURNAL, AIEN_INSTALL (the privileged copy command),
23# AIEN_AS_ROOT (the privileged prefix for the source tree copy; empty in tests).
24set -euo pipefail
25
26lock=${1:-}
27dry=0
28for a in "${@:2}"; do case $a in --dry-run) dry=1 ;; *) echo "unknown argument: $a" >&2; exit 2 ;; esac; done
29[[ -n $lock && -f $lock ]] || { echo "usage: registry-refresh.sh <trusted Cargo.lock> [--dry-run]" >&2; exit 2; }
30
31OP_HOME=${AIEN_OP_HOME:-$HOME}
32JUDGE=${AIEN_JUDGE_REGISTRY:-/var/lib/aien-run/cargo/registry}
33OPER=${AIEN_OPERATOR_REGISTRY:-$OP_HOME/.cargo/registry}
34JOURNAL=${AIEN_JOURNAL:-$OP_HOME/.local/state/aien-local-ci/journal.jsonl}
35INSTALL=${AIEN_INSTALL:-sudo -n install -o root -g root}
36AS_ROOT=${AIEN_AS_ROOT-sudo -n}
37CRATES_IO='registry+https://github.com/rust-lang/crates.io-index'
38
39say() { printf '[registry-refresh] %s\n' "$*"; }
40die() { echo "registry-refresh: $*" >&2; exit 1; }
41
42[[ -d $JUDGE/cache && -d $OPER/cache ]] || die "registry layout missing (judge $JUDGE, operator $OPER)"
43# The judge has one index directory (index.crates.io-<hash>). The operator's ~/.cargo may hold more than
44# one (a cargo release that changed the hash leaves the old one behind); only the judge's name matters,
45# because the judge's cargo never looks anywhere else, so the operator must have that same directory.
46idx_judge=$(ls "$JUDGE/cache")
47[[ $(wc -w <<<"$idx_judge") -eq 1 ]] || die "judge registry must hold exactly one index directory, found '$idx_judge'"
48[[ -d $OPER/cache/$idx_judge ]] || die "index directory mismatch: operator has no '$idx_judge' (has '$(ls "$OPER/cache")')"
49idx=$idx_judge
50SRC_DIR=$JUDGE/src/$idx
51
52# cargo's sparse-index cache path for a crate name: 1/<n>, 2/<n>, 3/<c>/<n>, <ab>/<cd>/<n> (lower case).
53index_rel() {
54 local n; n=$(printf '%s' "$1" | tr 'A-Z' 'a-z')
55 case ${#n} in
56 1) printf '1/%s' "$n" ;;
57 2) printf '2/%s' "$n" ;;
58 3) printf '3/%s/%s' "${n:0:1}" "$n" ;;
59 *) printf '%s/%s/%s' "${n:0:2}" "${n:2:2}" "$n" ;;
60 esac
61}
62
63# Lockfile packages from crates.io: "name version checksum" per line.
64packages() {
65 awk -v src="$CRATES_IO" '
66 function emit() { if (name != "" && source == src) print name, version, checksum }
67 /^\[\[package\]\]/ { emit(); name=""; version=""; source=""; checksum="" }
68 /^name = / { gsub(/"/, "", $3); name=$3 }
69 /^version = / { gsub(/"/, "", $3); version=$3 }
70 /^source = / { gsub(/"/, "", $3); source=$3 }
71 /^checksum = / { gsub(/"/, "", $3); checksum=$3 }
72 END { emit() }' "$lock"
73}
74
75STAGE=$(mktemp -d "${TMPDIR:-/tmp}/registry-refresh.XXXXXX")
76trap 'chmod -R u+w "$STAGE" 2>/dev/null; rm -rf "$STAGE"' EXIT
77
78# Unpack a verified .crate into $SRC_DIR/<name>-<version> the way cargo would, so the judge's read-only cargo
79# finds it already unpacked. Staged as the operator, copied as root under a dot name, made read-only, then
80# renamed into place in one step so a judge run never sees a half-written tree. Returns 1 (nothing written)
81# unless the archive holds exactly one top-level directory named <name>-<version>.
82unpack() {
83 local crate=$1 top=$2 s
84 s=$(mktemp -d "$STAGE/u.XXXXXX")
85 tar -xzf "$crate" -C "$s" --no-same-owner 2>/dev/null || return 1
86 [[ $(ls -A "$s") == "$top" && -d $s/$top && ! -L $s/$top ]] || return 1
87 # cargo writes this marker after a complete unpack and skips the unpack when it reads {"v":1}.
88 printf '{"v":1}' > "$s/$top/.cargo-ok"
89 [[ -e $SRC_DIR/$top || -e $SRC_DIR/.$top.tmp ]] && return 1
90 $AS_ROOT cp -R "$s/$top" "$SRC_DIR/.$top.tmp"
91 $AS_ROOT chmod -R a-w,a+rX "$SRC_DIR/.$top.tmp"
92 $AS_ROOT mv "$SRC_DIR/.$top.tmp" "$SRC_DIR/$top"
93}
94
95lock_digest=$(sha256sum "$lock" | cut -d' ' -f1)
96added=() unpacked=() missing_src=() refused=()
97total=0
98while read -r name version checksum; do
99 [[ -n $name ]] || continue
100 total=$((total + 1))
101 top="$name-$version"; file="$top.crate"
102 have_crate=0 have_src=0
103 [[ -e $JUDGE/cache/$idx/$file ]] && have_crate=1
104 [[ -e $SRC_DIR/$top/.cargo-ok ]] && have_src=1
105 (( have_crate && have_src )) && continue
106 if [[ -e $SRC_DIR/$top ]]; then refused+=("$file: $SRC_DIR/$top exists without .cargo-ok (half unpacked; not replaced)"); continue; fi
107 [[ -n $checksum ]] || { refused+=("$file: no checksum in lockfile"); continue; }
108 # The bytes to unpack: the judge's own .crate if it has one, otherwise the operator's.
109 if (( have_crate )); then src="$JUDGE/cache/$idx/$file"; else src="$OPER/cache/$idx/$file"; fi
110 if [[ ! -f $src ]]; then missing_src+=("$file"); continue; fi
111 have=$(sha256sum "$src" | cut -d' ' -f1)
112 if [[ $have != "$checksum" ]]; then refused+=("$file: sha256 $have, lockfile says $checksum"); continue; fi
113 if (( dry )); then
114 if (( have_crate )); then say "would unpack $top (judge has the .crate, no source)"; unpacked+=("$file")
115 else say "would add $file"; added+=("$file"); fi
116 continue
117 fi
118 unpack "$src" "$top" || { refused+=("$file: does not unpack to a single $top/ directory (nothing written)"); continue; }
119 if (( have_crate )); then say "unpacked $top from the judge's .crate (sha256 verified)"; unpacked+=("$file"); continue; fi
120 # Add only: check again right before the copy, then never overwrite.
121 [[ -e $JUDGE/cache/$idx/$file ]] || $INSTALL -m 0444 "$src" "$JUDGE/cache/$idx/$file"
122 rel=$(index_rel "$name")
123 if [[ -f $OPER/index/$idx/.cache/$rel ]]; then
124 $INSTALL -d -m 0755 "$JUDGE/index/$idx/.cache/$(dirname "$rel")"
125 # The index-cache entry lists every version cargo has seen; the operator's copy is a superset.
126 $INSTALL -m 0444 "$OPER/index/$idx/.cache/$rel" "$JUDGE/index/$idx/.cache/$rel"
127 else
128 say "note: no operator index-cache entry for $name (cargo will still find the .crate by its lockfile checksum)"
129 fi
130 say "added $file and its unpacked source (sha256 verified)"
131 added+=("$file")
132done < <(packages)
133
134(( total > 0 )) || die "no crates.io packages found in $lock"
135
136for m in "${missing_src[@]+"${missing_src[@]}"}"; do say "cannot add $m: not in the operator cache (fetch it as the operator first)"; done
137for r in "${refused[@]+"${refused[@]}"}"; do say "REFUSED $r"; done
138
139jlist() { printf '%s\n' "$@" | jq -R . | jq -sc 'map(select(. != ""))'; }
140if (( ! dry )); then
141 mkdir -p "$(dirname "$JOURNAL")"
142 jq -nc --arg utc "$(date -u +%Y-%m-%dT%H:%M:%SZ)" --arg action registry-refresh --arg lockfile "$lock" \
143 --arg lock_sha256 "$lock_digest" --arg packages "$total" \
144 --argjson added "$(jlist "${added[@]+"${added[@]}"}")" \
145 --argjson unpacked "$(jlist "${unpacked[@]+"${unpacked[@]}"}")" \
146 --argjson missing "$(jlist "${missing_src[@]+"${missing_src[@]}"}")" \
147 --argjson refused "$(jlist "${refused[@]+"${refused[@]}"}")" \
148 '$ARGS.named' >> "$JOURNAL"
149fi
150say "lockfile $lock_digest: $total crates.io packages, ${#added[@]} added, ${#unpacked[@]} unpacked, ${#missing_src[@]} not in operator cache, ${#refused[@]} refused"
151(( ${#refused[@]} == 0 )) || exit 1