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.
A crate bump no longer fails on the read-only judge: the registry refresh also unpacks the crate source (#622)
merged83ab168883020280 #672 · opened 2026-10-18 21:37 UTC · closed 2026-10-30 11:50 UTC · merged 2026-10-30 11:50 UTC
Imported from GitHub aien-dev/aien-sovereign-core-mv#672, by drakestapleton
Base 620cf831698bbfbfdddc75979ebbc310810e090f · Head ffa66afd269533f4d11fce4f243b13835813355d
Merged on GitHub as 6c4fa26e3104f84127be602f261ba3de13d304fe; reachable from main 007ac4ac5ee5edc6f79f411b1706752293604044 (observed 2026-11-18T16:35:48Z)
drakestapleton wrote on 2026-10-18 21:37 UTC
Closes #622. Supersedes #648 (carries its commit unchanged, adds the missing unpack step on top).
Problem
The judge builds --locked --offline with a root-owned, read-only CARGO_HOME. Nothing refreshed its registry, so any crate bump failed (seen on #621, rustls 0.23.45 to 0.23.46).
#648 adds an operator refresh that copies the .crate and the index-cache entry. That is not enough. The judge's cargo also needs the crate unpacked in registry/src, and with a read-only CARGO_HOME it cannot unpack it itself. After #648's refresh the build still fails:
failed to unpack package `rustls v0.23.46`
failed to create directory `.../registry/src/index.crates.io-1949cf8c6b5b557f/rustls-0.23.46`
Permission denied (os error 13)
install.sh never hit this because it seeded the operator's already unpacked registry/src too (every one of the judge's 1333 .crate files has a matching src dir). The live judge only works for rustls 0.23.46 today because its source was unpacked by hand (src/.../rustls-0.23.46 is dated 2026-10-18 06:13, same as the .crate). #648's verification found "0 to add" on the live box for that reason, so its add path never met a real read-only cargo.
Change
registry-refresh.sh now, for each verified crate:
unpacks the .crate (only if it holds exactly one <name>-<version>/ directory) into registry/src, writes cargo's .cargo-ok marker ({"v":1}), copies it as root, makes it read-only (dirs 555, files 444), and renames it into place in one step;
repairs the state #648 would leave: a .crate the judge has with no source gets unpacked from the judge's own copy after the same sha256 check;
refuses and leaves alone: half-unpacked dirs (no .cargo-ok), archives with extra top-level entries, judge crates whose bytes don't match the lockfile;
journals an unpacked list next to added, missing, refused.
Evidence (all run on the Spark)
Repro: a copy of /var/lib/aien-run/cargo/registry rolled back to before the 2026-10-17 manual stopgap (rustls 0.23.46 .crate, source dir and new index entry removed; the old index entry restored from backup-2026-10-17-rustls), made read-only (chmod -R a-w), then cargo check --locked --offline with CARGO_HOME set to the copy, on a tiny crate pinned to rustls = "=0.23.46" (lockfile checksum 48e13bd8...). The live registry was not touched.
step
result
control: same setup, rustls =0.23.45
rc=0
no refresh (main has no refresh tool)
rc=101, failed to select a version ... rustls = "=0.23.46"
after #648's registry-refresh.sh
rc=101, failed to unpack package rustls v0.23.46 ... Permission denied
after this branch's script, fresh copy
rc=0 (1 added)
this branch's script on the state #648 left
rc=0 (1 unpacked)
second run
0 added, 0 unpacked, rc=0
bash tools/aien-local-ci/tests/registry_refresh_test.sh: 41 passed, 0 failed (fixtures are now real gzip .crate tarballs). The same test against #648's script: 27 passed, 14 failed (the unpack, repair and refusal cases).
crumb verify . with the pinned tool (aien-architecture 94fffbcf): OK, 16 crumbs current.
aien-local-ci plan --base origin/main: RUNNABLE, no GPU gates.
Limits
Not run against the live judge registry (the issue rule is to test on a copy). The first real use is the operator running it with sudo on a trusted lockfile.
Pipeline change under tools/aien-local-ci, so it needs an operator run.
The original registry-refresh functional patch from #648 is carried here with identical stable patch ID 3e9a974a3638d6e8403ede287b615eabe9a27d96; this successor adds the required registry/src unpack. The merge desk has closed #648 as superseded and transferred its unfinished work to this still-open PR.
This is a shared qualification prerequisite: integrate current main, run the registry refresh/unpack tests and required signed operator verification, then separately record installation on the actual judge. A merged installer change alone does not prove the judge cache is refreshed. Issue #622 remains pending until the successor and operational acceptance qualify.
Coordination: aien-dev/aien-architecture-mv#190 and inference release #724.
drakestapleton reviewed (COMMENTED) on 2026-10-30 11:17 UTC
Triage: technical findings.
This PR conflicts with main and needs a rebase.
It changes how the registry for the root-owned, read-only judge CARGO_HOME is refreshed and unpacked.
It supersedes #648.
Landing it should unblock crate bumps like #808.
Design decision (from Primate, relayed through Aien): yes, the judge CARGO_HOME change is OK.
Hardware evidence: n/a.
Triage only; no approval or merge implied.
cursor[bot] wrote on 2026-10-30 11:48 UTC
Merge of origin/main (620cf831) is on swarm/622-registry-refresh-unpack as ffa66afd (parents 3438c826 and 620cf831). PR #672 head is that SHA. GitHub reports mergeable: MERGEABLE, mergeStateStatus: CLEAN. The pull request was not merged.
Conflicts were only the two generated crumb files, .crumb and tools/aien-local-ci/.crumb. registry-refresh.sh, tests/registry_refresh_test.sh, and the README row merged cleanly. Main's local-ci argv fix (#733), alpha GPU-proof retention (#769), docs (#773, #782, #783), and om/residency (#779) came in with the merge.
Resolution: recompiled both crumbs with the pinned tool (aien-architecture 94fffbcf88492c5a1c54211b5126c8a6c370a98d). crumb verify . reported OK: 17 crumbs current before and after the merge commit. The local-ci crumb now has main's evidence set (12 evidence files) and the registry-refresh sources (40 tracked files: 38 from main plus registry-refresh.sh and its test). The root crumb records 8102 files and 3602 evidence files. Diff against origin/main is those two scripts, the README line, and the two crumbs. Deployment, credentials, and trusted Spark origins were left as on main.
Checks on ffa66afd:
AIEN Checks: pass (1m2s). Crumb verify and the portable smoke set passed. AIEN Alpha: skipping (pull request, not an alpha tag).
diff --git a/.crumb b/.crumb
index 6bfb0c7d..cdea2468 100644
--- a/.crumb
+++ b/.crumb
@@ -118,21 +118,21 @@
"purpose": ""
},
{
- "digest": "7e5a49f47b2cf3b202760e4ebda72417c314b841db70ff3b49d2c4ce03e53ec7",
- "last_commit": "00a55932",
+ "digest": "0aacc9353a76a564e51069e99c6f11fb84b293ecf9686b68fbd72755b7da564e",
+ "last_commit": "d752ac74",
"name": "tools/aien-local-ci",
"purpose": "Local CI sandbox exec contract (C8), aien-run user/caches install, Forgejo trigger hook, health "
}
],
- "children_root": "sha256:3b54a54aa0b1c75dac59ea3656c912bbf6951ae6e657b2068ea27ceb833c32f1",
- "compiled_at": "2026-10-30T05:45:27Z",
+ "children_root": "sha256:c589179e44042cf631cc69e57a96da852f2ddfb002f3ec3f511e184cb12251fb",
+ "compiled_at": "2026-10-30T11:33:42Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:512b90eb826e378a9c9bf3b290de14b8b37d24810d9e2d194b39d19fb1a0f546",
+ "digest": "sha256:791ee1a768dd67ce4a7cf063b230324d6f1c88127fc4e06d6ab8ddd5a2ac217f",
"evidence_files": 3602,
"evidence_root": "sha256:1b8069934a4cc67803874ebd2ad5de32671fae5ccce1a3ea70aeb532fe8ea86b",
- "files": 8100,
- "generated_at_commit": "7f0116843ea7e575b47029b2b8398de6e5b1ad5b",
- "source_tree": "sha256:94fe4b5960a6e646d85b3fa548f22cb2afee3b372df2cfdf4a462c536eb1dda0"
+ "files": 8102,
+ "generated_at_commit": "3438c82668b80fc4387e07786f92b747831f182d",
+ "source_tree": "sha256:49a2ec9d3e5bbe4d45bf5ec5bcf97052b7d37b2621e52cad003490f8510f134c"
}
},
"history": [],
diff --git a/tools/aien-local-ci/.crumb b/tools/aien-local-ci/.crumb
index 892ae174..16a492d1 100644
--- a/tools/aien-local-ci/.crumb
+++ b/tools/aien-local-ci/.crumb
@@ -3,14 +3,14 @@
"generated": {
"children": [],
"children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
- "compiled_at": "2026-10-30T05:15:32Z",
+ "compiled_at": "2026-10-30T11:33:42Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:7e5a49f47b2cf3b202760e4ebda72417c314b841db70ff3b49d2c4ce03e53ec7",
+ "digest": "sha256:0aacc9353a76a564e51069e99c6f11fb84b293ecf9686b68fbd72755b7da564e",
"evidence_files": 12,
"evidence_root": "sha256:223486aeb7e2dcf529b17591ce5667b2f2117b54e8ed3c5c0b43d580c01cf8a2",
- "files": 38,
- "generated_at_commit": "2015edda1c6ffe90880b77eb8fd847ce0242732e",
- "source_tree": "sha256:9dfe856f64cf8e24302b77691ab933f96deff61309b9ccdbaf93a2675be469fc"
+ "files": 40,
+ "generated_at_commit": "3438c82668b80fc4387e07786f92b747831f182d",
+ "source_tree": "sha256:4de50657e70658bc084f8850c3528b22070a77e31a8354225334c75d44ec1d7f"
}
},
"layer": "infra",
diff --git a/tools/aien-local-ci/README.md b/tools/aien-local-ci/README.md
index 49cedd42..8c94f44e 100644
--- a/tools/aien-local-ci/README.md
+++ b/tools/aien-local-ci/README.md
@@ -121,6 +121,7 @@ added by this lane.
| `/var/lib/aien-run` | root:aien-run 0750, ACL `u:drakestapleton:r-x` | HOME of aien-run; no skeleton dotfiles |
| `cargo/` | root 0755 | CARGO_HOME (cargo works with it read-only: `--locked --offline` builds need no writes there) |
| `cargo/registry`, `cargo/git` | root, read-only | seeded once from `~/.cargo` (1.9 GB + 1.3 GB; the git cache is needed because `Cargo.lock` has 3 git sources) |
+| `registry-refresh.sh <Cargo.lock>` | operator, sudo | adds to `cargo/registry` the crates.io packages a trusted lockfile names that the judge lacks: copied from `~/.cargo`, sha256 checked against the lockfile, unpacked into `registry/src` with cargo's `.cargo-ok` marker (the read-only judge cargo cannot unpack a `.crate` itself), index-cache entry copied, one journal line; a `.crate` the judge has without its source is unpacked after the same check; never deletes or replaces a crate or source directory (#622). `--dry-run` only lists |
| `cargo/bin` | root | the operator's `rustup` binary + proxy symlinks; no second rustup install |
| `rustup/` | root, read-only | `settings.toml` (default 1.98.1) + `toolchains/1.98.1-aarch64-unknown-linux-gnu` copied (631 MB), plus `toolchains/nightly-aarch64-unknown-linux-gnu` with miri for SC-MIRI |
| `.cache/miri` | root, read-only | miri sysroot built once by `install.sh` against the copied nightly (110 MB); `cargo +nightly miri setup` in the sandbox finds it and writes nothing (#600) |
diff --git a/tools/aien-local-ci/registry-refresh.sh b/tools/aien-local-ci/registry-refresh.sh
new file mode 100755
index 00000000..1e6cfedf
--- /dev/null
+++ b/tools/aien-local-ci/registry-refresh.sh
@@ -0,0 +1,149 @@
+#!/usr/bin/env bash
+# registry-refresh.sh: add to the judge's read-only cargo registry the crates a trusted Cargo.lock names
+# that it does not have yet (issue #622). The judge builds with `--locked --offline` against
+# /var/lib/aien-run/cargo/registry, which install.sh seeded once from the operator's ~/.cargo; nothing
+# refreshed it, so every PR that adds or bumps a crate failed on the judge. This is the operator-run refresh
+# step. The trust model is unchanged: the registry stays root-owned and read-only to aien-run, nothing here
+# touches the network, and the source of every byte is the operator's own cache.
+#
+# bash tools/aien-local-ci/registry-refresh.sh <trusted Cargo.lock> [--dry-run]
+#
+# For each crates.io package in the lockfile that is missing from the judge registry: copy the .crate from
+# the operator's ~/.cargo cache, refuse unless its sha256 equals the lockfile checksum, unpack that verified
+# .crate into registry/src the way cargo does (with its .cargo-ok marker), then copy the matching index-cache
+# entry. The unpacked source is required: the judge's CARGO_HOME is read-only, so its cargo cannot unpack a
+# .crate itself and fails with "failed to unpack package ... Permission denied". A crate whose .crate is
+# already in the judge cache but whose source was never unpacked (the state an older refresh left) gets its
+# source unpacked from the judge's own .crate after the same checksum check. Prints what it did and appends
+# one journal line so the next bundle can be traced to the registry state it ran with. Never deletes or
+# replaces an existing crate file or source directory.
+#
+# Test overrides (tests/registry_refresh_test.sh uses them on throwaway directories, no sudo):
+# AIEN_JUDGE_REGISTRY, AIEN_OPERATOR_REGISTRY, AIEN_JOURNAL, AIEN_INSTALL (the privileged copy command),
+# AIEN_AS_ROOT (the privileged prefix for the source tree copy; empty in tests).
+set -euo pipefail
+
+lock=${1:-}
+dry=0
+for a in "${@:2}"; do case $a in --dry-run) dry=1 ;; *) echo "unknown argument: $a" >&2; exit 2 ;; esac; done
+[[ -n $lock && -f $lock ]] || { echo "usage: registry-refresh.sh <trusted Cargo.lock> [--dry-run]" >&2; exit 2; }
+
+OP_HOME=${AIEN_OP_HOME:-$HOME}
+JUDGE=${AIEN_JUDGE_REGISTRY:-/var/lib/aien-run/cargo/registry}
+OPER=${AIEN_OPERATOR_REGISTRY:-$OP_HOME/.cargo/registry}
+JOURNAL=${AIEN_JOURNAL:-$OP_HOME/.local/state/aien-local-ci/journal.jsonl}
+INSTALL=${AIEN_INSTALL:-sudo -n install -o root -g root}
+AS_ROOT=${AIEN_AS_ROOT-sudo -n}
+CRATES_IO='registry+https://github.com/rust-lang/crates.io-index'
+
+say() { printf '[registry-refresh] %s\n' "$*"; }
+die() { echo "registry-refresh: $*" >&2; exit 1; }
+
+[[ -d $JUDGE/cache && -d $OPER/cache ]] || die "registry layout missing (judge $JUDGE, operator $OPER)"
+# One index directory on each side (index.crates.io-<hash>); the names must agree or the judge's cargo
+# would never look where we copy.
+idx_judge=$(ls "$JUDGE/cache"); idx_oper=$(ls "$OPER/cache")
+[[ $idx_judge == "$idx_oper" && $(wc -w <<<"$idx_judge") -eq 1 ]] || die "index directory mismatch: judge '$idx_judge' operator '$idx_oper'"
+idx=$idx_judge
+SRC_DIR=$JUDGE/src/$idx
+
+# cargo's sparse-index cache path for a crate name: 1/<n>, 2/<n>, 3/<c>/<n>, <ab>/<cd>/<n> (lower case).
+index_rel() {
+ local n; n=$(printf '%s' "$1" | tr 'A-Z' 'a-z')
+ case ${#n} in
+ 1) printf '1/%s' "$n" ;;
+ 2) printf '2/%s' "$n" ;;
+ 3) printf '3/%s/%s' "${n:0:1}" "$n" ;;
+ *) printf '%s/%s/%s' "${n:0:2}" "${n:2:2}" "$n" ;;
+ esac
+}
+
+# Lockfile packages from crates.io: "name version checksum" per line.
+packages() {
+ awk -v src="$CRATES_IO" '
+ function emit() { if (name != "" && source == src) print name, version, checksum }
+ /^\[\[package\]\]/ { emit(); name=""; version=""; source=""; checksum="" }
+ /^name = / { gsub(/"/, "", $3); name=$3 }
+ /^version = / { gsub(/"/, "", $3); version=$3 }
+ /^source = / { gsub(/"/, "", $3); source=$3 }
+ /^checksum = / { gsub(/"/, "", $3); checksum=$3 }
+ END { emit() }' "$lock"
+}
+
+STAGE=$(mktemp -d "${TMPDIR:-/tmp}/registry-refresh.XXXXXX")
+trap 'chmod -R u+w "$STAGE" 2>/dev/null; rm -rf "$STAGE"' EXIT
+
+# Unpack a verified .crate into $SRC_DIR/<name>-<version> the way cargo would, so the judge's read-only cargo
+# finds it already unpacked. Staged as the operator, copied as root under a dot name, made read-only, then
+# renamed into place in one step so a judge run never sees a half-written tree. Returns 1 (nothing written)
+# unless the archive holds exactly one top-level directory named <name>-<version>.
+unpack() {
+ local crate=$1 top=$2 s
+ s=$(mktemp -d "$STAGE/u.XXXXXX")
+ tar -xzf "$crate" -C "$s" --no-same-owner 2>/dev/null || return 1
+ [[ $(ls -A "$s") == "$top" && -d $s/$top && ! -L $s/$top ]] || return 1
+ # cargo writes this marker after a complete unpack and skips the unpack when it reads {"v":1}.
+ printf '{"v":1}' > "$s/$top/.cargo-ok"
+ [[ -e $SRC_DIR/$top || -e $SRC_DIR/.$top.tmp ]] && return 1
+ $AS_ROOT cp -R "$s/$top" "$SRC_DIR/.$top.tmp"
+ $AS_ROOT chmod -R a-w,a+rX "$SRC_DIR/.$top.tmp"
+ $AS_ROOT mv "$SRC_DIR/.$top.tmp" "$SRC_DIR/$top"
+}
+
+lock_digest=$(sha256sum "$lock" | cut -d' ' -f1)
+added=() unpacked=() missing_src=() refused=()
+total=0
+while read -r name version checksum; do
+ [[ -n $name ]] || continue
+ total=$((total + 1))
+ top="$name-$version"; file="$top.crate"
+ have_crate=0 have_src=0
+ [[ -e $JUDGE/cache/$idx/$file ]] && have_crate=1
+ [[ -e $SRC_DIR/$top/.cargo-ok ]] && have_src=1
+ (( have_crate && have_src )) && continue
+ if [[ -e $SRC_DIR/$top ]]; then refused+=("$file: $SRC_DIR/$top exists without .cargo-ok (half unpacked; not replaced)"); continue; fi
+ [[ -n $checksum ]] || { refused+=("$file: no checksum in lockfile"); continue; }
+ # The bytes to unpack: the judge's own .crate if it has one, otherwise the operator's.
+ if (( have_crate )); then src="$JUDGE/cache/$idx/$file"; else src="$OPER/cache/$idx/$file"; fi
+ if [[ ! -f $src ]]; then missing_src+=("$file"); continue; fi
+ have=$(sha256sum "$src" | cut -d' ' -f1)
+ if [[ $have != "$checksum" ]]; then refused+=("$file: sha256 $have, lockfile says $checksum"); continue; fi
+ if (( dry )); then
+ if (( have_crate )); then say "would unpack $top (judge has the .crate, no source)"; unpacked+=("$file")
+ else say "would add $file"; added+=("$file"); fi
+ continue
+ fi
+ unpack "$src" "$top" || { refused+=("$file: does not unpack to a single $top/ directory (nothing written)"); continue; }
+ if (( have_crate )); then say "unpacked $top from the judge's .crate (sha256 verified)"; unpacked+=("$file"); continue; fi
+ # Add only: check again right before the copy, then never overwrite.
+ [[ -e $JUDGE/cache/$idx/$file ]] || $INSTALL -m 0444 "$src" "$JUDGE/cache/$idx/$file"
+ rel=$(index_rel "$name")
+ if [[ -f $OPER/index/$idx/.cache/$rel ]]; then
+ $INSTALL -d -m 0755 "$JUDGE/index/$idx/.cache/$(dirname "$rel")"
+ # The index-cache entry lists every version cargo has seen; the operator's copy is a superset.
+ $INSTALL -m 0444 "$OPER/index/$idx/.cache/$rel" "$JUDGE/index/$idx/.cache/$rel"
+ else
+ say "note: no operator index-cache entry for $name (cargo will still find the .crate by its lockfile checksum)"
+ fi
+ say "added $file and its unpacked source (sha256 verified)"
+ added+=("$file")
+done < <(packages)
+
+(( total > 0 )) || die "no crates.io packages found in $lock"
+
+for m in "${missing_src[@]+"${missing_src[@]}"}"; do say "cannot add $m: not in the operator cache (fetch it as the operator first)"; done
+for r in "${refused[@]+"${refused[@]}"}"; do say "REFUSED $r"; done
+
+jlist() { printf '%s\n' "$@" | jq -R . | jq -sc 'map(select(. != ""))'; }
+if (( ! dry )); then
+ mkdir -p "$(dirname "$JOURNAL")"
+ jq -nc --arg utc "$(date -u +%Y-%m-%dT%H:%M:%SZ)" --arg action registry-refresh --arg lockfile "$lock" \
+ --arg lock_sha256 "$lock_digest" --arg packages "$total" \
+ --argjson added "$(jlist "${added[@]+"${added[@]}"}")" \
+ --argjson unpacked "$(jlist "${unpacked[@]+"${unpacked[@]}"}")" \
+ --argjson missing "$(jlist "${missing_src[@]+"${missing_src[@]}"}")" \
+ --argjson refused "$(jlist "${refused[@]+"${refused[@]}"}")" \
+ '$ARGS.named' >> "$JOURNAL"
+fi
+say "lockfile $lock_digest: $total crates.io packages, ${#added[@]} added, ${#unpacked[@]} unpacked, ${#missing_src[@]} not in operator cache, ${#refused[@]} refused"
+(( ${#refused[@]} == 0 )) || exit 1
diff --git a/tools/aien-local-ci/tests/registry_refresh_test.sh b/tools/aien-local-ci/tests/registry_refresh_test.sh
new file mode 100755
index 00000000..1e7e264b
--- /dev/null
+++ b/tools/aien-local-ci/tests/registry_refresh_test.sh
@@ -0,0 +1,135 @@
+#!/usr/bin/env bash
+# registry_refresh_test.sh: offline proofs for registry-refresh.sh on throwaway directories (no sudo, no network).
+# bash tools/aien-local-ci/tests/registry_refresh_test.sh
+set -euo pipefail
+HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+R=$HERE/../registry-refresh.sh
+T=$(mktemp -d)
+trap 'chmod -R u+w "$T" 2>/dev/null; rm -rf "$T"' EXIT
+pass=0 fail=0
+ok() { pass=$((pass + 1)); echo "ok $*"; }
+bad() { fail=$((fail + 1)); echo "FAIL $*"; }
+check() { local name=$1; shift; if "$@"; then ok "$name"; else bad "$name"; fi; }
+mode() { stat -c %a "$1" 2>/dev/null || stat -f %Lp "$1"; }
+
+IDX=index.crates.io-1949cf8c6b5b557f
+J=$T/judge O=$T/oper S=$T/judge/src/$IDX
+mkdir -p "$J/cache/$IDX" "$J/index/$IDX/.cache" "$S" "$O/cache/$IDX" "$O/index/$IDX/.cache/ru/st" "$O/index/$IDX/.cache/2" "$O/index/$IDX/.cache/3/a"
+export AIEN_JUDGE_REGISTRY=$J AIEN_OPERATOR_REGISTRY=$O AIEN_JOURNAL=$T/journal.jsonl AIEN_INSTALL="install" AIEN_AS_ROOT=
+
+# A real .crate: a gzip tar holding one <name>-<version>/ directory, like crates.io serves.
+# mk <dest dir> <name> <version>: writes the .crate, prints its sha256.
+mk() {
+ local d=$1 top=$2-$3 w; w=$(mktemp -d "$T/w.XXXXXX")
+ mkdir -p "$w/$top/src"
+ printf '[package]\nname = "%s"\nversion = "%s"\n' "$2" "$3" > "$w/$top/Cargo.toml"
+ printf 'pub fn f() {}\n' > "$w/$top/src/lib.rs"
+ tar -czf "$d/$top.crate" -C "$w" "$top"
+ sha256sum "$d/$top.crate" | cut -d' ' -f1
+}
+sha_rustls=$(mk "$O/cache/$IDX" rustls 0.23.46)
+sha_old=$(mk "$O/cache/$IDX" rustls 0.23.45)
+sha_ab=$(mk "$O/cache/$IDX" ab 1.0.0)
+sha_abc=$(mk "$O/cache/$IDX" abc 2.0.0)
+_=$(mk "$O/cache/$IDX" tampered 1.0.0)
+echo 'index entry rustls' > "$O/index/$IDX/.cache/ru/st/rustls"
+echo 'index entry ab' > "$O/index/$IDX/.cache/2/ab"
+echo 'index entry abc' > "$O/index/$IDX/.cache/3/a/abc"
+# The judge already has the old rustls, unpacked, with different bytes on purpose: it must never be replaced.
+printf 'judge copy, keep me' > "$J/cache/$IDX/rustls-0.23.45.crate"
+mkdir -p "$S/rustls-0.23.45"; printf '{"v":1}' > "$S/rustls-0.23.45/.cargo-ok"
+# The state an add-only refresh left behind: the .crate is in the judge cache but its source was never
+# unpacked, so the read-only judge cargo fails "failed to unpack package". It must be repaired.
+sha_half=$(mk "$J/cache/$IDX" half 1.0.0)
+
+pkg() { printf '[[package]]\nname = "%s"\nversion = "%s"\nsource = "registry+https://github.com/rust-lang/crates.io-index"\nchecksum = "%s"\n\n' "$@"; }
+{
+ printf 'version = 4\n\n'
+ pkg rustls 0.23.46 "$sha_rustls"
+ pkg rustls 0.23.45 "$sha_old"
+ pkg ab 1.0.0 "$sha_ab"
+ pkg abc 2.0.0 "$sha_abc"
+ pkg half 1.0.0 "$sha_half"
+ pkg notcached 0.1.0 0000000000000000000000000000000000000000000000000000000000000000
+ printf '[[package]]\nname = "aien-local"\nversion = "0.1.0"\n\n'
+ printf '[[package]]\nname = "gitdep"\nversion = "0.1.0"\nsource = "git+https://example.invalid/x#abc"\n\n'
+} > "$T/Cargo.lock"
+
+# 1 dry run adds nothing and names what it would add or unpack
+out=$(bash "$R" "$T/Cargo.lock" --dry-run 2>&1); rc=$?
+check "dry run exits 0" test $rc -eq 0
+check "dry run names rustls 0.23.46" grep -q 'would add rustls-0.23.46.crate' <<<"$out"
+check "dry run names the unpack-only repair" grep -q 'would unpack half-1.0.0' <<<"$out"
+check "dry run copies nothing" test ! -e "$J/cache/$IDX/rustls-0.23.46.crate"
+check "dry run unpacks nothing" test ! -e "$S/rustls-0.23.46" -a ! -e "$S/half-1.0.0"
+check "dry run writes no journal" test ! -e "$AIEN_JOURNAL"
+
+# 2 real run: adds the missing verified crates unpacked with their index entries, repairs the unpacked-less one,
+# keeps the existing one, reports the uncached one
+out=$(bash "$R" "$T/Cargo.lock" 2>&1); rc=$?
+check "real run exits 0" test $rc -eq 0
+check "rustls 0.23.46 added" cmp -s "$O/cache/$IDX/rustls-0.23.46.crate" "$J/cache/$IDX/rustls-0.23.46.crate"
+check "added crate is read-only" test "$(mode "$J/cache/$IDX/rustls-0.23.46.crate")" = 444
+check "added crate is unpacked into registry/src" grep -q 'version = "0.23.46"' "$S/rustls-0.23.46/Cargo.toml"
+check "unpacked source carries cargo's .cargo-ok marker" test "$(cat "$S/rustls-0.23.46/.cargo-ok")" = '{"v":1}'
+check "unpacked source file is read-only" test "$(mode "$S/rustls-0.23.46/src/lib.rs")" = 444
+check "unpacked source dir is read-only" test "$(mode "$S/rustls-0.23.46")" = 555
+check "no staging leftovers in registry/src" test -z "$(ls -A "$S" | grep '^\.' || true)"
+check "index entry copied (ru/st/rustls)" cmp -s "$O/index/$IDX/.cache/ru/st/rustls" "$J/index/$IDX/.cache/ru/st/rustls"
+check "two-letter name index path (2/ab)" test -f "$J/index/$IDX/.cache/2/ab"
+check "three-letter name index path (3/a/abc)" test -f "$J/index/$IDX/.cache/3/a/abc"
+check "two- and three-letter crates unpacked" test -f "$S/ab-1.0.0/.cargo-ok" -a -f "$S/abc-2.0.0/.cargo-ok"
+check "crate without source gets unpacked from the judge copy" test -f "$S/half-1.0.0/.cargo-ok" -a -f "$S/half-1.0.0/src/lib.rs"
+check "existing judge crate never replaced" test "$(cat "$J/cache/$IDX/rustls-0.23.45.crate")" = 'judge copy, keep me'
+check "uncached crate reported, not fatal" grep -q 'cannot add notcached-0.1.0.crate' <<<"$out"
+check "summary counts" grep -q '6 crates.io packages, 3 added, 1 unpacked, 1 not in operator cache, 0 refused' <<<"$out"
+check "journal line written" test "$(wc -l < "$AIEN_JOURNAL")" -eq 1
+check "journal names the lockfile digest" test "$(jq -r .lock_sha256 "$AIEN_JOURNAL")" = "$(sha256sum "$T/Cargo.lock" | cut -d' ' -f1)"
+check "journal lists the added crates" test "$(jq -r '.added|join(",")' "$AIEN_JOURNAL")" = 'rustls-0.23.46.crate,ab-1.0.0.crate,abc-2.0.0.crate'
+check "journal lists the unpacked crate" test "$(jq -r '.unpacked|join(",")' "$AIEN_JOURNAL")" = 'half-1.0.0.crate'
+
+# 3 second run is a no-op
+out=$(bash "$R" "$T/Cargo.lock" 2>&1); rc=$?
+check "second run exits 0 with 0 added, 0 unpacked" grep -q ', 0 added, 0 unpacked,' <<<"$out"
+
+# 4 a checksum mismatch is refused: nothing copied or unpacked, exit 1, named in the journal
+pkg tampered 1.0.0 1111111111111111111111111111111111111111111111111111111111111111 >> "$T/Cargo.lock"
+out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
+check "mismatch exits 1" test $rc -eq 1
+check "mismatch is named REFUSED" grep -q 'REFUSED tampered-1.0.0.crate' <<<"$out"
+check "mismatched crate not copied" test ! -e "$J/cache/$IDX/tampered-1.0.0.crate"
+check "mismatched crate not unpacked" test ! -e "$S/tampered-1.0.0"
+check "journal records the refusal" test "$(jq -r '.refused|length' <(tail -1 "$AIEN_JOURNAL"))" = 1
+
+# 5 an archive that is not exactly one <name>-<version>/ directory is refused before anything is written
+w=$(mktemp -d "$T/w.XXXXXX"); mkdir -p "$w/evil-1.0.0" "$w/other"; echo x > "$w/other/planted"
+tar -czf "$O/cache/$IDX/evil-1.0.0.crate" -C "$w" evil-1.0.0 other
+pkg evil 1.0.0 "$(sha256sum "$O/cache/$IDX/evil-1.0.0.crate" | cut -d' ' -f1)" >> "$T/Cargo.lock"
+out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
+check "bad archive exits 1" test $rc -eq 1
+check "bad archive is named REFUSED" grep -q 'REFUSED evil-1.0.0.crate: does not unpack' <<<"$out"
+check "bad archive: no crate, no source, no stray dir" test ! -e "$J/cache/$IDX/evil-1.0.0.crate" -a ! -e "$S/evil-1.0.0" -a ! -e "$S/other"
+
+# 6 a judge .crate whose bytes do not match the lockfile is refused, not replaced and not unpacked
+printf 'judge bytes are wrong' > "$J/cache/$IDX/wrongjudge-1.0.0.crate"
+pkg wrongjudge 1.0.0 2222222222222222222222222222222222222222222222222222222222222222 >> "$T/Cargo.lock"
+out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
+check "wrong judge crate is named REFUSED" grep -q 'REFUSED wrongjudge-1.0.0.crate: sha256' <<<"$out"
+check "wrong judge crate kept as is, not unpacked" test "$(cat "$J/cache/$IDX/wrongjudge-1.0.0.crate")" = 'judge bytes are wrong' -a ! -e "$S/wrongjudge-1.0.0"
+
+# 7 a half-unpacked source directory (no .cargo-ok) is refused, never replaced
+sha_part=$(mk "$O/cache/$IDX" part 1.0.0)
+mkdir -p "$S/part-1.0.0"; echo 'half written' > "$S/part-1.0.0/marker"
+pkg part 1.0.0 "$sha_part" >> "$T/Cargo.lock"
+out=$(bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
+check "half-unpacked dir is named REFUSED" grep -q 'REFUSED part-1.0.0.crate: .* exists without .cargo-ok' <<<"$out"
+check "half-unpacked dir left as is" test "$(cat "$S/part-1.0.0/marker")" = 'half written' -a ! -e "$S/part-1.0.0/.cargo-ok"
+
+# 8 index directory mismatch is fatal before any copy
+mkdir -p "$T/judge2/cache/index.crates.io-ffffffffffffffff"
+out=$(AIEN_JUDGE_REGISTRY=$T/judge2 bash "$R" "$T/Cargo.lock" 2>&1) && rc=0 || rc=$?
+check "index dir mismatch refused" test $rc -eq 1
+check "index dir mismatch message" grep -q 'index directory mismatch' <<<"$out"
+
+echo "registry_refresh_test: $pass passed, $fail failed"
+(( fail == 0 ))