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 / scripts / test-install-release.sh

406 lines · 30162 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Exercise install.sh release mode offline. A fixture release is signed with a
18# throwaway key and served over file://. Checks:
19# 1. the key pinned inside install.sh equals docs/release/allowed_signers
20# 2. a valid signed release installs from a checkout
21# 3. a tampered archive installs nothing (checksum)
22# 4. a release signed by another key installs nothing (signature)
23# 5. a standalone copy of install.sh (no source tree, clean HOME) installs the
24# valid release offline when the signer file is given, imprint included
25# 6. the same standalone copy with no signer override falls back to the key
26# pinned in install.sh, so the fixture (throwaway key) is refused
27set -euo pipefail
28
29ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
30WORK="$(mktemp -d "${TMPDIR:-/tmp}/aien-test-install-release.XXXXXX")"
31[[ -n "${KEEP:-}" ]] || trap 'rm -rf "$WORK"' EXIT
32fail() { echo "FAIL: $*" >&2; [[ -z "${KEEP:-}" ]] || echo "WORK=$WORK" >&2; exit 1; }
33# kill9_at_point <pid>: kill -9 a paused installer exactly where it stands. The installer first,
34# then the `sleep` of its pause loop. Killing the sleep first (the old order) made install.sh exit
35# on its own under `set -e` (a killed child returns 137) through its EXIT trap, which is a clean
36# exit, not a kill at the point; and when that exit won the race, `kill -9` found no process and
37# failed the test (sc #452, 1 of 4 runs under load).
38kill9_at_point() {
39 local pid="$1" kids
40 kids="$(pgrep -P "$pid" 2>/dev/null || true)"
41 kill -9 "$pid" 2>/dev/null || true
42 # shellcheck disable=SC2086
43 [[ -z "$kids" ]] || kill -9 $kids 2>/dev/null || true
44 wait "$pid" 2>/dev/null || true
45}
46
47case "$(uname -s)" in Linux) OS=linux ;; Darwin) OS=macos ;; *) fail "unsupported OS" ;; esac
48case "$(uname -m)" in x86_64|amd64) ARCH=x86_64 ;; aarch64|arm64) ARCH=aarch64 ;; *) fail "unsupported arch" ;; esac
49[[ "$OS-$ARCH" != macos-aarch64 ]] || ARCH=arm64
50ASSET="sovereign-$OS-$ARCH.tar.gz"
51
52# 1. pinned keys: install.sh AIEN_RELEASE_SIGNER equals the key lines of docs/release/allowed_signers (both empty
53# while the placeholder stands); AIEN_INTERNAL_TEST_SIGNER equals docs/release/internal_test_signers; the
54# internal test key is never in allowed_signers (Drake ruling 1, sc#385: the old key is not reused)
55keylines() { grep -vE "^[[:space:]]*(#|\$)" "$1" || true; }
56pinned() { sed -n "s/^$1='\(.*\)'\$/\1/p" "$ROOT/install.sh"; }
57grep -qE "^AIEN_RELEASE_SIGNER=" "$ROOT/install.sh" || fail "install.sh has no AIEN_RELEASE_SIGNER line"
58[[ "$(pinned AIEN_RELEASE_SIGNER)" == "$(keylines "$ROOT/docs/release/allowed_signers")" ]] \
59 || fail "AIEN_RELEASE_SIGNER in install.sh differs from the key lines of docs/release/allowed_signers"
60INTERNAL="$(pinned AIEN_INTERNAL_TEST_SIGNER)"
61[[ -n "$INTERNAL" && "$INTERNAL" == "$(keylines "$ROOT/docs/release/internal_test_signers")" ]] \
62 || fail "AIEN_INTERNAL_TEST_SIGNER in install.sh differs from docs/release/internal_test_signers"
63! grep -qF "${INTERNAL#aien-release }" "$ROOT/docs/release/allowed_signers" || fail "the internal test key is in allowed_signers"
64if [[ -z "$(pinned AIEN_RELEASE_SIGNER)" ]]; then
65 grep -q "PLACEHOLDER" "$ROOT/docs/release/allowed_signers" || fail "allowed_signers holds no key and no PLACEHOLDER label"
66fi
67
68ssh-keygen -q -t ed25519 -N '' -C test -f "$WORK/key"
69ssh-keygen -q -t ed25519 -N '' -C other -f "$WORK/other"
70printf 'aien-release %s\n' "$(cut -d' ' -f1,2 "$WORK/key.pub")" > "$WORK/allowed_signers"
71
72make_release() { # dir signing-key [candidate] [variant] [dry: label the manifest dry-run]
73 local d="$1" k="$2" cand="${3:-CAND-1}" variant="${4:-a}" dry="${5:-}" f aien
74 rm -rf "$WORK/pkg"
75 mkdir -p "$d" "$WORK/pkg/bin" "$WORK/pkg/imprints/en2-trinity"
76 printf '#!/bin/sh\necho aien-%s\nexit 0\n' "$variant" > "$WORK/pkg/bin/aien"
77 printf '#!/bin/sh\nexit 0\n' > "$WORK/pkg/bin/cortex-rs"
78 printf 'fixture imprint\n' > "$WORK/pkg/imprints/en2-trinity/soul.md"
79 chmod 755 "$WORK/pkg/bin/"*
80 aien="$(sha256sum "$WORK/pkg/bin/aien" 2>/dev/null | cut -d' ' -f1 || shasum -a 256 "$WORK/pkg/bin/aien" | cut -d' ' -f1)"
81 {
82 printf 'schema = "AienReleaseV1"\ncandidate = "%s"\naien-cli-sha256 = "%s"\n' "$cand" "$aien"
83 [[ -z "$dry" ]] || printf 'dry-run = true\n'
84 printf '\n[files]\n'
85 (cd "$WORK/pkg" && find . -type f ! -name release.toml | LC_ALL=C sort | sed 's|^\./||') | while read -r f; do
86 printf '"%s" = "%s"\n' "$f" "$(sha256sum "$WORK/pkg/$f" | cut -d' ' -f1)"
87 done
88 } > "$WORK/pkg/release.toml"
89 tar -czf "$d/$ASSET" -C "$WORK/pkg" .
90 (cd "$d" && sha256sum "$ASSET" 2>/dev/null > SHA256SUMS.txt || shasum -a 256 "$ASSET" > SHA256SUMS.txt)
91 ssh-keygen -q -Y sign -f "$k" -n aien-release "$d/SHA256SUMS.txt"
92}
93
94run_install() { # release-dir bin-dir config-dir installer [VAR=value ...]
95 local rel="$1" bin="$2" cfg="$3" installer="$4"
96 shift 4
97 env AIEN_RELEASE_TAG=v0.0.0-test AIEN_RELEASE_BASE_URL="file://$rel" \
98 AIEN_BIN_DIR="$bin" AIEN_CONFIG_DIR="$cfg" AIEN_INSTALL_NO_PROFILE=1 "$@" \
99 bash "$installer" > "$WORK/log" 2>&1
100}
101
102# 2. valid release, from the checkout
103make_release "$WORK/good" "$WORK/key"
104run_install "$WORK/good" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" \
105 AIEN_SOURCE_DIR="$ROOT" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers" \
106 || { cat "$WORK/log" >&2; fail "valid signed release did not install"; }
107[[ -x "$WORK/bin-good/aien" && -x "$WORK/bin-good/cortex" ]] || fail "binaries missing after valid install"
108
109# 3. tampered archive
110make_release "$WORK/tamper" "$WORK/key"
111printf 'x' >> "$WORK/tamper/$ASSET"
112if run_install "$WORK/tamper" "$WORK/bin-tamper" "$WORK/cfg-tamper" "$ROOT/install.sh" \
113 AIEN_SOURCE_DIR="$ROOT" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers"; then
114 fail "tampered archive was installed"
115fi
116grep -q "checksum mismatch" "$WORK/log" || fail "tampered archive not rejected for checksum"
117[[ ! -e "$WORK/bin-tamper/aien" ]] || fail "tampered install left a binary"
118
119# 4. signature from another key
120make_release "$WORK/forged" "$WORK/other"
121if run_install "$WORK/forged" "$WORK/bin-forged" "$WORK/cfg-forged" "$ROOT/install.sh" \
122 AIEN_SOURCE_DIR="$ROOT" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers"; then
123 fail "release signed by another key was installed"
124fi
125grep -q "signature is NOT valid" "$WORK/log" || fail "forged signature not rejected"
126[[ ! -e "$WORK/bin-forged/aien" ]] || fail "forged install left a binary"
127
128# 5. standalone installer, no source tree, clean HOME, offline, signer file given
129mkdir -p "$WORK/standalone" "$WORK/home"
130cp "$ROOT/install.sh" "$WORK/standalone/install.sh"
131run_install "$WORK/good" "$WORK/bin-alone" "$WORK/cfg-alone" "$WORK/standalone/install.sh" \
132 HOME="$WORK/home" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers" \
133 || { cat "$WORK/log" >&2; fail "standalone offline install did not install"; }
134grep -q "Release mode without a source tree" "$WORK/log" || fail "standalone install used a source tree"
135[[ -x "$WORK/bin-alone/aien" && -x "$WORK/bin-alone/cortex" ]] || fail "binaries missing after standalone install"
136[[ -f "$WORK/cfg-alone/imprints/en2/soul.md" ]] || fail "imprint from the release archive was not installed"
137[[ -z "$(ls -A "$WORK/home")" ]] || fail "standalone install wrote into HOME: $(ls -A "$WORK/home")"
138
139# 6. standalone installer, no signer override: the pinned key refuses the fixture
140if run_install "$WORK/good" "$WORK/bin-pinned" "$WORK/cfg-pinned" "$WORK/standalone/install.sh" \
141 HOME="$WORK/home"; then
142 fail "fixture signed by a throwaway key was accepted by the pinned signer"
143fi
144grep -q "signature is NOT valid" "$WORK/log" || fail "pinned-signer fallback did not reject the fixture"
145[[ ! -e "$WORK/bin-pinned/aien" ]] || fail "pinned-signer refusal left a binary"
146
147
148# 7. upgrade: a newer candidate installs over the first; one previous release is kept; the
149# record names both; operator.toml is untouched
150SIGN_ENV=(AIEN_SOURCE_DIR="$ROOT" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers")
151live() { readlink "$1/releases/current"; }
152inst_args() { # bin cfg args... : run install.sh with arguments in explicit dirs
153 local bin="$1" cfg="$2"; shift 2
154 env AIEN_BIN_DIR="$bin" AIEN_CONFIG_DIR="$cfg" AIEN_INSTALL_NO_PROFILE=1 HOME="$WORK/home" \
155 bash "$ROOT/install.sh" "$@" > "$WORK/log" 2>&1
156}
157echo '# local edit' >> "$WORK/cfg-good/operator.toml"
158OP_BEFORE="$(sha256sum "$WORK/cfg-good/operator.toml" | cut -d' ' -f1)"
159ID1="$(live "$WORK/cfg-good")"
160[[ -n "$ID1" && -f "$WORK/cfg-good/installed.toml" ]] || fail "first install left no live release or record"
161[[ "$("$WORK/bin-good/aien")" == "aien-a" ]] || fail "first install does not run"
162make_release "$WORK/rel2" "$WORK/key" CAND-2 b
163run_install "$WORK/rel2" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" "${SIGN_ENV[@]}" \
164 || { cat "$WORK/log" >&2; fail "upgrade did not install"; }
165ID2="$(live "$WORK/cfg-good")"
166[[ "$ID2" != "$ID1" ]] || fail "upgrade did not change the live release"
167[[ "$("$WORK/bin-good/aien")" == "aien-b" ]] || fail "installed aien is not the upgraded one"
168grep -q "^previous = \"$ID1\"" "$WORK/cfg-good/installed.toml" || fail "record does not name the previous release"
169grep -q 'candidate = "CAND-2"' "$WORK/cfg-good/installed.toml" || fail "record lacks the new candidate"
170[[ "$(sha256sum "$WORK/cfg-good/operator.toml" | cut -d' ' -f1)" == "$OP_BEFORE" ]] || fail "upgrade changed operator.toml"
171[[ "$(ls "$WORK/cfg-good/releases" | grep -vc '^current$')" -eq 2 ]] || fail "expected exactly two releases kept"
172
173# 8. rollback returns to the first release and records it; a second rollback goes forward again
174inst_args "$WORK/bin-good" "$WORK/cfg-good" --rollback || { cat "$WORK/log" >&2; fail "rollback failed"; }
175[[ "$(live "$WORK/cfg-good")" == "$ID1" && "$("$WORK/bin-good/aien")" == "aien-a" ]] || fail "rollback did not restore the first release"
176grep -q '^last-action = "rollback"' "$WORK/cfg-good/installed.toml" || fail "rollback not recorded"
177grep -q "^previous = \"$ID2\"" "$WORK/cfg-good/installed.toml" || fail "record does not name the rolled-back-from release"
178inst_args "$WORK/bin-good" "$WORK/cfg-good" --rollback || fail "second rollback failed"
179[[ "$(live "$WORK/cfg-good")" == "$ID2" ]] || fail "second rollback did not swap forward"
180inst_args "$WORK/bin-good" "$WORK/cfg-good" --rollback || fail "third rollback failed"
181if inst_args "$WORK/bin-none" "$WORK/cfg-none" --rollback; then fail "rollback with nothing installed succeeded"; fi
182grep -q "no previous release" "$WORK/log" || fail "rollback with nothing installed not explained"
183
184# 9. downgrade guard: an older candidate is refused unless --allow-downgrade
185inst_args "$WORK/bin-good" "$WORK/cfg-good" --rollback >/dev/null || true # live = ID2 (CAND-2)
186[[ "$(live "$WORK/cfg-good")" == "$ID2" ]] || fail "setup for the downgrade case failed"
187make_release "$WORK/rel0" "$WORK/key" CAND-1 c
188if run_install "$WORK/rel0" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" "${SIGN_ENV[@]}"; then
189 fail "older candidate installed without --allow-downgrade"
190fi
191grep -q "allow-downgrade" "$WORK/log" || fail "downgrade refusal not explained"
192[[ "$(live "$WORK/cfg-good")" == "$ID2" ]] || fail "refused downgrade changed the live release"
193env AIEN_RELEASE_TAG=v0.0.0-test AIEN_RELEASE_BASE_URL="file://$WORK/rel0" AIEN_BIN_DIR="$WORK/bin-good" \
194 AIEN_CONFIG_DIR="$WORK/cfg-good" AIEN_INSTALL_NO_PROFILE=1 "${SIGN_ENV[@]}" \
195 bash "$ROOT/install.sh" --allow-downgrade > "$WORK/log" 2>&1 \
196 || { cat "$WORK/log" >&2; fail "downgrade with --allow-downgrade failed"; }
197
198# 10. package that does not match its own release.toml installs nothing
199make_release "$WORK/badpkg" "$WORK/key" CAND-9 d
200mkdir -p "$WORK/unp"; tar -xzf "$WORK/badpkg/$ASSET" -C "$WORK/unp"; printf 'x' >> "$WORK/unp/bin/cortex-rs"
201tar -czf "$WORK/badpkg/$ASSET" -C "$WORK/unp" .
202(cd "$WORK/badpkg" && sha256sum "$ASSET" > SHA256SUMS.txt && rm -f SHA256SUMS.txt.sig \
203 && ssh-keygen -q -Y sign -f "$WORK/key" -n aien-release SHA256SUMS.txt)
204LIVE_BEFORE="$(live "$WORK/cfg-good")"
205if run_install "$WORK/badpkg" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" "${SIGN_ENV[@]}"; then
206 fail "package with a file that does not match release.toml was installed"
207fi
208grep -q "does not match its release.toml" "$WORK/log" || fail "manifest mismatch not reported"
209[[ "$(live "$WORK/cfg-good")" == "$LIVE_BEFORE" ]] || fail "rejected package changed the live release"
210
211# 11. interrupted install: kill the installer mid-copy and before the swap; the live install is
212# still the old version, runs, and a rerun completes the upgrade
213make_release "$WORK/rel3" "$WORK/key" CAND-3 e
214for point in mid-copy after-copy before-swap; do
215 OLD="$(live "$WORK/cfg-good")"
216 env AIEN_TEST_PAUSE_AT="$point" AIEN_RELEASE_TAG=v0.0.0-test AIEN_RELEASE_BASE_URL="file://$WORK/rel3" \
217 AIEN_BIN_DIR="$WORK/bin-good" AIEN_CONFIG_DIR="$WORK/cfg-good" AIEN_INSTALL_NO_PROFILE=1 \
218 AIEN_SOURCE_DIR="$ROOT" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers" \
219 bash "$ROOT/install.sh" > "$WORK/log-int" 2>&1 &
220 IPID=$!
221 for _ in $(seq 1 100); do grep -q "paused at $point" "$WORK/log-int" 2>/dev/null && break; sleep 0.1; done
222 grep -q "paused at $point" "$WORK/log-int" || fail "installer never reached $point"
223 kill9_at_point "$IPID"
224 [[ "$(live "$WORK/cfg-good")" == "$OLD" ]] || fail "killed at $point changed the live release"
225 out="$("$WORK/bin-good/aien")"; [[ "$out" == "aien-b" || "$out" == "aien-c" ]] || fail "live install does not run after kill at $point ($out)"
226done
227# 11b. kill after the single rename (current already switched, links and record not yet written):
228# the new release is live and runs, the record still names the old one, a rerun repairs the record
229ID_NEW="$(sha256sum "$WORK/pkg/bin/aien" | cut -c1-12)"
230OLD="$(live "$WORK/cfg-good")"
231[[ "$OLD" != "$ID_NEW" ]] || fail "setup for the after-swap case failed"
232env AIEN_TEST_PAUSE_AT=after-swap AIEN_RELEASE_TAG=v0.0.0-test AIEN_RELEASE_BASE_URL="file://$WORK/rel3" \
233 AIEN_BIN_DIR="$WORK/bin-good" AIEN_CONFIG_DIR="$WORK/cfg-good" AIEN_INSTALL_NO_PROFILE=1 \
234 AIEN_SOURCE_DIR="$ROOT" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers" \
235 bash "$ROOT/install.sh" > "$WORK/log-int" 2>&1 &
236IPID=$!
237for _ in $(seq 1 100); do grep -q "paused at after-swap" "$WORK/log-int" 2>/dev/null && break; sleep 0.1; done
238grep -q "paused at after-swap" "$WORK/log-int" || fail "installer never reached after-swap"
239kill9_at_point "$IPID"
240[[ "$(live "$WORK/cfg-good")" == "$ID_NEW" ]] || fail "after-swap: the rename did not take effect"
241[[ "$("$WORK/bin-good/aien")" == "aien-e" ]] || fail "after-swap: the live release does not run"
242grep -q "^current = \"$OLD\"" "$WORK/cfg-good/installed.toml" || fail "after-swap: record unexpectedly updated before the kill"
243[[ -d "$WORK/cfg-good/releases/$OLD" ]] || fail "after-swap: the previous release was pruned before the record was written"
244run_install "$WORK/rel3" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" "${SIGN_ENV[@]}" \
245 || { cat "$WORK/log" >&2; fail "rerun after interruption failed"; }
246[[ "$("$WORK/bin-good/aien")" == "aien-e" ]] || fail "rerun did not install the new release"
247grep -q "^current = \"$ID_NEW\"" "$WORK/cfg-good/installed.toml" || fail "rerun after the after-swap kill did not repair the record"
248grep -q "^previous = \"$OLD\"" "$WORK/cfg-good/installed.toml" || fail "rerun after the after-swap kill lost the previous release"
249[[ -z "$(ls -A "$WORK/cfg-good/releases" | grep '^\.stage\.' || true)" ]] || fail "leftover stage after rerun"
250
251# 12. an existing release directory that is incomplete is never overwritten
252ID3="$(live "$WORK/cfg-good")"
253make_release "$WORK/rel4" "$WORK/key" CAND-4 f
254ID4="$(sha256sum "$WORK/pkg/bin/aien" | cut -c1-12)"
255mkdir -p "$WORK/cfg-good/releases/$ID4"; echo partial > "$WORK/cfg-good/releases/$ID4/junk"
256if run_install "$WORK/rel4" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" "${SIGN_ENV[@]}"; then
257 fail "install over an incomplete release directory succeeded"
258fi
259grep -q "was not overwritten" "$WORK/log" || fail "incomplete release directory not reported"
260[[ "$(live "$WORK/cfg-good")" == "$ID3" && -f "$WORK/cfg-good/releases/$ID4/junk" ]] || fail "refusal changed state"
261
262# 12b. kill -9 at the final switch itself. install.sh switches releases with one rename(2) of a
263# temp symlink over releases/current. After every kill the install is wholly old or wholly new:
264# it runs, its record is readable, and a rerun (or --rollback) finishes cleanly.
265kill_at() { # point release-dir [install.sh args...]: run install.sh held at point, then kill -9 it
266 local point="$1" rel="$2" ipid
267 shift 2
268 env AIEN_TEST_PAUSE_AT="$point" AIEN_RELEASE_TAG=v0.0.0-test AIEN_RELEASE_BASE_URL="file://$rel" \
269 AIEN_BIN_DIR="$WORK/bin-good" AIEN_CONFIG_DIR="$WORK/cfg-good" AIEN_INSTALL_NO_PROFILE=1 \
270 AIEN_SOURCE_DIR="$ROOT" AIEN_DRY_RUN_SIGNERS="$WORK/allowed_signers" HOME="$WORK/home" \
271 bash "$ROOT/install.sh" "$@" > "$WORK/log-int" 2>&1 &
272 ipid=$!
273 for _ in $(seq 1 100); do grep -q "paused at $point" "$WORK/log-int" 2>/dev/null && break; sleep 0.1; done
274 grep -q "paused at $point" "$WORK/log-int" || fail "installer never reached $point"
275 kill9_at_point "$ipid"
276}
277rec_cur() { sed -n 's/^current = "\(.*\)"$/\1/p' "$WORK/cfg-good/installed.toml"; }
278strays() { ls -A "$WORK/cfg-good/releases" | grep -c '^\.current\.new\.' || true; }
279
280# A. install killed after the temp link exists, before the rename: wholly old
281make_release "$WORK/rel5" "$WORK/key" CAND-5 g
282ID5="$(sha256sum "$WORK/pkg/bin/aien" | cut -c1-12)"
283ID_OLD="$(live "$WORK/cfg-good")"
284kill_at pre-rename "$WORK/rel5"
285[[ "$(live "$WORK/cfg-good")" == "$ID_OLD" && "$("$WORK/bin-good/aien")" == "aien-e" ]] || fail "pre-rename kill: install is not wholly old"
286[[ "$(strays)" -ge 1 ]] || fail "pre-rename: no temp link, the hold is not at the rename"
287[[ "$(rec_cur)" == "$ID_OLD" ]] || fail "pre-rename kill: record changed"
288run_install "$WORK/rel5" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" "${SIGN_ENV[@]}" \
289 || { cat "$WORK/log" >&2; fail "rerun after pre-rename kill failed"; }
290[[ "$(live "$WORK/cfg-good")" == "$ID5" && "$("$WORK/bin-good/aien")" == "aien-g" && "$(rec_cur)" == "$ID5" ]] || fail "rerun after pre-rename kill is not wholly new"
291[[ "$(strays)" -eq 0 ]] || fail "temp link left after rerun"
292
293# B. install killed after the rename and the links, before the record: wholly new, record repaired by rerun
294make_release "$WORK/rel6" "$WORK/key" CAND-6 h
295ID6="$(sha256sum "$WORK/pkg/bin/aien" | cut -c1-12)"
296kill_at after-links "$WORK/rel6"
297[[ "$(live "$WORK/cfg-good")" == "$ID6" && "$("$WORK/bin-good/aien")" == "aien-h" ]] || fail "after-links kill: install is not wholly new"
298[[ "$(rec_cur)" == "$ID5" && -d "$WORK/cfg-good/releases/$ID5" ]] || fail "after-links kill: record or previous release lost"
299run_install "$WORK/rel6" "$WORK/bin-good" "$WORK/cfg-good" "$ROOT/install.sh" "${SIGN_ENV[@]}" \
300 || { cat "$WORK/log" >&2; fail "rerun after after-links kill failed"; }
301[[ "$(rec_cur)" == "$ID6" ]] && grep -q "^previous = \"$ID5\"" "$WORK/cfg-good/installed.toml" || fail "rerun did not repair the record"
302
303# C. --rollback killed before its rename: wholly old (the release it was leaving); rerun completes
304kill_at pre-rename "$WORK/none" --rollback
305[[ "$(live "$WORK/cfg-good")" == "$ID6" && "$("$WORK/bin-good/aien")" == "aien-h" && "$(rec_cur)" == "$ID6" ]] || fail "rollback killed pre-rename: not wholly old"
306inst_args "$WORK/bin-good" "$WORK/cfg-good" --rollback || { cat "$WORK/log" >&2; fail "rollback rerun failed"; }
307[[ "$(live "$WORK/cfg-good")" == "$ID5" && "$("$WORK/bin-good/aien")" == "aien-g" && "$(rec_cur)" == "$ID5" ]] || fail "rollback rerun: not wholly rolled back"
308[[ "$(strays)" -eq 0 ]] || fail "temp link left after rollback rerun"
309
310# D. --rollback killed after its rename, before the record: wholly new (the target); rerun is safe and
311# leaves a consistent record; a further rollback still swaps back and verifies
312kill_at after-swap "$WORK/none" --rollback
313[[ "$(live "$WORK/cfg-good")" == "$ID6" && "$("$WORK/bin-good/aien")" == "aien-h" ]] || fail "rollback killed after-swap: not wholly switched"
314inst_args "$WORK/bin-good" "$WORK/cfg-good" --rollback || { cat "$WORK/log" >&2; fail "rollback rerun after after-swap kill failed"; }
315[[ "$(live "$WORK/cfg-good")" == "$(rec_cur)" ]] || fail "record and live release disagree after the rerun"
316[[ "$("$WORK/bin-good/aien")" == "aien-h" || "$("$WORK/bin-good/aien")" == "aien-g" ]] || fail "install does not run after the rerun"
317inst_args "$WORK/bin-good" "$WORK/cfg-good" --rollback || fail "rollback after recovery failed"
318[[ "$(live "$WORK/cfg-good")" == "$(rec_cur)" ]] || fail "record and live release disagree after a further rollback"
319
320# 13. release gate: the candidate must be named and omega.lock must be the candidate's omega commit
321GT="$WORK/gate"; mkdir -p "$GT/scripts" "$GT/release"
322cp "$ROOT/scripts/check-release-candidate.sh" "$GT/scripts/"; cp "$ROOT/release/candidate.toml" "$GT/release/"
323# A matching tree by construction: Cargo.lock sources at exactly the candidate's [pins], as omega.lock below is
324# the candidate's omega commit. The live Cargo.lock moves with dependency updates (sc#906 left one aien-protocols-mv
325# source) and is not CAND-4; pin mismatches are covered by scripts/test-release-checks.sh.
326pin() { sed -n '/^\[pins\]/,/^\[/p' "$GT/release/candidate.toml" | sed -n "s/^$1 *= *\"\([0-9a-f]*\)\".*/\1/p"; }
327for r in aien-protocols crumb-spec spark-crumbs; do
328 printf '[[package]]\nsource = "git+https://github.com/aien-dev/%s?rev=%s#%s"\n' "$r" "$(pin "$r")" "$(pin "$r")"
329done > "$GT/Cargo.lock"
330gate() { (cd "$GT" && bash scripts/check-release-candidate.sh "$@" > "$WORK/log-gate" 2>&1); }
331OM="$(sed -n 's/^omega-commit *= *"\(.*\)"$/\1/p' "$GT/release/candidate.toml")"
332echo "$OM" > "$GT/omega.lock"
333gate --model "$WORK/model.toml" || { cat "$WORK/log-gate" >&2; fail "gate refused a matching tree"; }
334grep -q '^\[model\]' "$WORK/model.toml" && grep -q '^model-safetensors-sha256' "$WORK/model.toml" || fail "gate did not extract the model table"
335echo 0000000000000000000000000000000000000000 > "$GT/omega.lock"
336if gate; then fail "gate accepted an omega.lock that is not the candidate's"; fi
337grep -q "cannot be released" "$WORK/log-gate" || fail "omega mismatch not explained"
338echo "$OM" > "$GT/omega.lock"
339sed -i 's/^candidate = .*/candidate = "unknown"/' "$GT/release/candidate.toml"
340if gate; then fail "gate accepted candidate unknown"; fi
341rm "$GT/release/candidate.toml"
342if gate; then fail "gate accepted a missing candidate file"; fi
343
344# 14. offline-signing handoff (sc#385): signature classes, labelled receipts, negative rows. Every key here is a
345# throwaway made above; nothing in this section touches a real key.
346rcpt() { sed -n "s/^ \"$2\": \"\{0,1\}\([^\",]*\)\"\{0,1\},\{0,1\}\$/\1/p" "$1/install-receipt.json"; }
347# A. a dry-run artifact (manifest labelled dry-run, signed by a throwaway key given explicitly) installs and is
348# recorded as signature_check dry-run-key in installed.toml and in install-receipt.json
349make_release "$WORK/dry" "$WORK/key" CAND-1 a dry
350run_install "$WORK/dry" "$WORK/bin-dry" "$WORK/cfg-dry" "$ROOT/install.sh" "${SIGN_ENV[@]}" \
351 || { cat "$WORK/log" >&2; fail "dry-run artifact did not install with the throwaway signer"; }
352grep -q '^signature-check = "dry-run-key"$' "$WORK/cfg-dry/installed.toml" || fail "installed.toml does not record dry-run-key"
353[[ "$(rcpt "$WORK/cfg-dry" signature_check)" == dry-run-key ]] || fail "install receipt does not record signature_check dry-run-key"
354[[ "$(rcpt "$WORK/cfg-dry" dry_run)" == true ]] || fail "install receipt does not record the dry-run manifest label"
355[[ "$(rcpt "$WORK/cfg-dry" artifact_sha256)" == "$(sha256sum "$WORK/dry/$ASSET" | cut -d' ' -f1)" ]] || fail "receipt artifact_sha256 is not the artifact's digest"
356[[ "$(rcpt "$WORK/cfg-dry" files_verified)" =~ ^[1-9][0-9]*$ ]] || fail "receipt files_verified is not a positive count"
357[[ "$(rcpt "$WORK/cfg-dry" cli_sha256)" =~ ^[0-9a-f]{64}$ ]] || fail "receipt cli_sha256 missing"
358grep -q 'signature_check: dry-run-key' "$WORK/log" || fail "installer output does not say dry-run-key"
359# B. one byte changed in the middle of the archive (same size): refused, names the checksum, nothing installed
360make_release "$WORK/flip" "$WORK/key" CAND-1 a dry
361SZ="$(wc -c < "$WORK/flip/$ASSET")"; OFF=$((SZ / 2))
362B="$(dd if="$WORK/flip/$ASSET" bs=1 skip="$OFF" count=1 2>/dev/null | od -An -tu1 | tr -d ' ')"
363printf "$(printf '\\%03o' $(( (B + 1) % 256 )))" | dd of="$WORK/flip/$ASSET" bs=1 seek="$OFF" count=1 conv=notrunc 2>/dev/null
364[[ "$(wc -c < "$WORK/flip/$ASSET")" == "$SZ" ]] || fail "byte flip changed the size"
365if run_install "$WORK/flip" "$WORK/bin-flip" "$WORK/cfg-flip" "$ROOT/install.sh" "${SIGN_ENV[@]}"; then fail "artifact with one byte changed was installed"; fi
366grep -qi "checksum" "$WORK/log" || fail "one-byte change not refused by name (checksum)"
367[[ ! -e "$WORK/bin-flip/aien" && ! -e "$WORK/cfg-flip/install-receipt.json" ]] || fail "one-byte refusal left a binary or a receipt"
368# C. one byte changed in the signed checksum list: refused, names the signature
369make_release "$WORK/flipsum" "$WORK/key" CAND-1 a dry
370C1="$(head -c1 "$WORK/flipsum/SHA256SUMS.txt")"; [[ "$C1" == 0 ]] && R=1 || R=0; sed -i "1s/^./$R/" "$WORK/flipsum/SHA256SUMS.txt"
371if run_install "$WORK/flipsum" "$WORK/bin-fs" "$WORK/cfg-fs" "$ROOT/install.sh" "${SIGN_ENV[@]}"; then fail "edited checksum list was installed"; fi
372grep -q "signature" "$WORK/log" || fail "edited checksum list not refused by name (signature)"
373[[ ! -e "$WORK/bin-fs/aien" ]] || fail "edited checksum list left a binary"
374# D. missing signature: refused, says unsigned, nothing installed
375make_release "$WORK/nosig" "$WORK/key" CAND-1 a dry
376rm -f "$WORK/nosig/SHA256SUMS.txt.sig"
377if run_install "$WORK/nosig" "$WORK/bin-ns" "$WORK/cfg-ns" "$ROOT/install.sh" "${SIGN_ENV[@]}"; then fail "release without a signature was installed"; fi
378grep -q "SHA256SUMS.txt.sig is missing" "$WORK/log" || fail "missing signature not refused by name"
379[[ ! -e "$WORK/bin-ns/aien" && ! -e "$WORK/cfg-ns/install-receipt.json" ]] || fail "missing-signature refusal left a binary or a receipt"
380# E. wrong key: a dry-run artifact signed by another throwaway key is refused
381make_release "$WORK/wrong" "$WORK/other" CAND-1 a dry
382if run_install "$WORK/wrong" "$WORK/bin-wk" "$WORK/cfg-wk" "$ROOT/install.sh" "${SIGN_ENV[@]}"; then fail "wrong-key artifact was installed"; fi
383grep -q "signature is NOT valid" "$WORK/log" || fail "wrong key not refused by name"
384# F. a dry-run artifact can never be recorded as a release. Simulate a pinned release key with a copy of install.sh
385# whose AIEN_RELEASE_SIGNER is the throwaway key: an unlabelled package signed by it records release (positive
386# control); a dry-run-labelled package signed by the same key is refused and leaves no receipt.
387mkdir -p "$WORK/pinned-sim"
388sed "s|^AIEN_RELEASE_SIGNER=.*|AIEN_RELEASE_SIGNER='$(cat "$WORK/allowed_signers")'|" "$ROOT/install.sh" > "$WORK/pinned-sim/install.sh"
389grep -q "^AIEN_RELEASE_SIGNER='aien-release ssh-ed25519 " "$WORK/pinned-sim/install.sh" || fail "release-key simulation did not apply"
390run_install "$WORK/good" "$WORK/bin-rel" "$WORK/cfg-rel" "$WORK/pinned-sim/install.sh" HOME="$WORK/home" \
391 || { cat "$WORK/log" >&2; fail "simulated release did not install"; }
392[[ "$(rcpt "$WORK/cfg-rel" signature_check)" == release ]] || fail "release-key signature not recorded as release (control)"
393if run_install "$WORK/dry" "$WORK/bin-dryrel" "$WORK/cfg-dryrel" "$WORK/pinned-sim/install.sh" HOME="$WORK/home"; then
394 fail "dry-run artifact signed by the release key was installed"
395fi
396grep -q "labelled dry-run" "$WORK/log" || fail "dry-run artifact under the release key not refused by name"
397[[ ! -e "$WORK/cfg-dryrel/install-receipt.json" ]] || fail "refused dry-run artifact left a receipt"
398# G. with the placeholder in place (no release key pinned) and no throwaway signer, nothing verifies as a release
399if [[ -z "$(pinned AIEN_RELEASE_SIGNER)" ]]; then
400 if run_install "$WORK/good" "$WORK/bin-ph" "$WORK/cfg-ph" "$ROOT/install.sh" HOME="$WORK/home"; then fail "placeholder accepted a release"; fi
401 grep -q "no public-release key is pinned" "$WORK/log" || fail "placeholder refusal does not say no release key is pinned"
402fi
403# H. a release signed by the internal test key would be labelled internal-test-key (classification is by the
404# verifying key; checked by grepping the installer, because that private key is not on this machine)
405grep -q 'SIG_CHECK=internal-test-key' "$ROOT/install.sh" || fail "install.sh has no internal-test-key class"
406echo "PASS: release gate fails closed, release install verifies signature, checksum and every package file, labels the signature class (release, internal-test-key, dry-run-key) in installed.toml and install-receipt.json, never records a dry-run artifact as a release, refuses a one-byte change, an edited checksum list, a missing signature and a wrong key, rejects tampered and forged releases, installs standalone offline, pinned key matches allowed_signers, upgrades atomically, keeps one previous release, rolls back, refuses downgrades, survives an interrupted install (including kill -9 just before the rename, just after it, and during rollback)"