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

One cargo target dir per head: a gate can never run another head's build (SC-TEST, PREFLIGHT, VERIFY-CLOSURE flakes; #452 cross-head half)

merged a03548f098f4350c #681 · opened 2026-10-19 15:30 UTC · closed 2026-10-19 18:47 UTC · merged 2026-10-19 18:47 UTC

Imported from GitHub aien-dev/aien-sovereign-core-mv#681, by drakestapleton

Labels: ready-to-merge

Base c17430dbb8358c8387384dd193479ef91c4fc3d6 · Head a04b69bf7702736ce4e97f84d67658f5cb7e9d88

Merged on GitHub as 17881bc10d8ed71bc14f68228660dda7f1293993; reachable from main 007ac4ac5ee5edc6f79f411b1706752293604044 (observed 2026-11-18T16:35:47Z)

drakestapleton wrote on 2026-10-19 15:30 UTC

What this fixes

Every head of a repo built into one shared cargo target dir (242 GB for sc). Cargo decides "already built" by file times and project-relative paths, so a test program built by another head a moment later counts as fresh, and a gate could run another head's code. This is the root cause of the intermittent SC-TEST, SC-AGENT-PREFLIGHT and SC-VERIFY-CLOSURE failures (for example real_digests_of_two_checked_in_weight_files failing with NotFound on 13 heads: its CARGO_MANIFEST_DIR pointed into another head's deleted worktree) and the cross-head half of the PREFILL-E2E-2 false FAIL (#452; the in-head half landed in #673).

  • aien-test worker gives each head its own target dir inside its trust class: target/<repo>/pr/<sha> or target/<repo>/main/<sha>.
  • The sandbox helper (aien-run-exec) refuses any target dir that is not this run's own; other heads' dirs are read-only to it.
  • wt.sh remove deletes the head's target dir with its checkout; gc.sh clears orphans and the old shared dir.
  • Docs: aien-local-ci README, LOCAL_VERIFICATION.md.

Evidence (Spark ~/workspace/gates-fix/evidence, report ~/handoffs/2026-10-19-gates-fix.md section 1)

  • tools/aien-local-ci/tests/per_head_target_test.sh: shared dir, head B ran head A's program (red); per-head dirs, head B ran its own (green).
  • Helper tests: old live helper 33/38 against the new rules, new helper 38/38 (run in a private view, live judges untouched).
  • aien-test unit tests pass; local_ci 32/32 with the new helper. SC-VERIFY-CLOSURE and SC-TEST pass in the per-head layout.

Pipeline note

Pipeline configuration change (rule A4): the desk runs the operator judge on the exact head. Goes live only after merge plus the deploy steps with the judge idle: install.sh (new helper), rebuild target-sc aien-test, restart workers when idle, then gc.sh --apply (never earlier: it removes the shared dir under live judges).

Apply it

git checkout -b patch-a03548f098f4350c c17430dbb8358c8387384dd193479ef91c4fc3d6
curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/a03548f098f4350c.patch | git am

Raw patch (mbox) · Diff

Provenance

This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record:

github-import 1
aien-dev/aien-sovereign-core-mv
681
a04b69bf7702736ce4e97f84d67658f5cb7e9d88
{"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":681,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/681","export_sha256":"d815604dc795b494a931d4c9d256159461266bed51d6da460421ed1d94dab90f","imported_utc":"2026-11-18T16:35:47Z"}

Changes

diff --git a/.crumb b/.crumb
index f1241b53..37fb3ad4 100644
--- a/.crumb
+++ b/.crumb
@@ -112,21 +112,21 @@
           "purpose": ""
         },
         {
-          "digest": "bb2a911a1a7d4088d5c767bba72d8248154b71fedd0d86102490a594a64a9947",
-          "last_commit": "24c29ae",
+          "digest": "afb0a808b1cb4f66c0a20b3c49080054c824c027a2e412d4e8a67cfa8482fc09",
+          "last_commit": "5cb704d",
           "name": "tools/aien-local-ci",
           "purpose": "Local CI sandbox exec contract (C8), aien-run user/caches install, Forgejo trigger hook, health "
         }
       ],
-      "children_root": "sha256:7233404baa7d5c3ce664e4abee8970212f5e6213399ba74d2ad7a65e3a5324d5",
-      "compiled_at": "2026-10-19T17:20:35Z",
+      "children_root": "sha256:dda1fdd0a03d2e5240273dcce56351592f5e4fddd4354f220899ad0032f1f989",
+      "compiled_at": "2026-10-19T18:07:31Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:ed210d92a91b1b826018fffd07efb5c8da54cfa5bc70911ca6c4a9dc9f74aa03",
+      "digest": "sha256:3904b58e0b7e3c56109963389b332413e4dcb48362c6f06fc4402277bd4dbe76",
       "evidence_files": 1578,
       "evidence_root": "sha256:135e27a1a0704c4a8371cd2c1eb555f2b5beec61c212949b029a5457418a7160",
-      "files": 5891,
-      "generated_at_commit": "40ed2a147cf53fa88af59fddb14a6007559d4bfd",
-      "source_tree": "sha256:228864541d5d655070f9ec7302cc5741bbd0198d569678411c0c009c92557898"
+      "files": 5892,
+      "generated_at_commit": "e34a12298408fa13e4cbc747341fa96bce2b55a5",
+      "source_tree": "sha256:b9bc52793e4c838b068eae124d2458f570dc80e3a416e61ebb61cfc01cce82a9"
     }
   },
   "history": [],
diff --git a/tools/aien-local-ci/.crumb b/tools/aien-local-ci/.crumb
index 752ad287..d952c33a 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-19T00:25:49Z",
+      "compiled_at": "2026-10-19T15:30:18Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:bb2a911a1a7d4088d5c767bba72d8248154b71fedd0d86102490a594a64a9947",
+      "digest": "sha256:afb0a808b1cb4f66c0a20b3c49080054c824c027a2e412d4e8a67cfa8482fc09",
       "evidence_files": 0,
       "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
-      "files": 17,
-      "generated_at_commit": "f23e1cbd1827d99d525a19052b6d8ae993951ad6",
-      "source_tree": "sha256:dc61b4ef713230725436ddaf2cfb6a6f3958daa16ea8a5a5d7727428519a3768"
+      "files": 18,
+      "generated_at_commit": "4ec0f1844bdd5ba2be58c5daee12381c1834a708",
+      "source_tree": "sha256:0645ea9517501bac03ea694e6e2588bd05684dfe29b5b11b7943e81bbdfb97e8"
     }
   },
   "layer": "infra",
diff --git a/tools/aien-local-ci/README.md b/tools/aien-local-ci/README.md
index 7a5edee4..f2e89f70 100644
--- a/tools/aien-local-ci/README.md
+++ b/tools/aien-local-ci/README.md
@@ -56,8 +56,8 @@ Argument rules (exit 2 when broken):
 * `--cwd` absolute, existing, canonical (no symlink, no `..`, no trailing slash). Inside `/var/lib/aien-run` only the
   home itself or `wt/<repo>/<40-hex sha>[/...]`.
 * `--env K=V`: plain names; `PATH`, `HOME`, `CARGO_HOME`, `RUSTUP_HOME`, `LD_*`, `BASH_ENV`, `ENV` refused; each name at
-  most once. `CARGO_TARGET_DIR` must be exactly `/var/lib/aien-run/target/<repo>/pr` or `.../main` (trust class, A2);
-  it is created on demand (parent root-owned, class dir aien-run).
+  most once. `CARGO_TARGET_DIR` must be exactly `/var/lib/aien-run/target/<repo>/pr/<sha40>` or `.../main/<sha40>` (trust class, A2),
+  the worktree's own repo and sha when `--cwd` is in one; it is created on demand (parents root-owned, head dir aien-run).
 * `PROGRAM` may not contain `=` or start with `-` (otherwise `env` would read it as one more variable assignment).
 * The wrapper refuses to run if the home, `cargo`, `cargo/bin`, `rustup`, `target`, `/usr/local/bin` or `/usr/bin`
   is not root-owned or is group/other-writable.
@@ -117,7 +117,7 @@ added by this lane.
 | `mirror/<repo>.git` | drakestapleton 0755 | bare mirror of the trusted clone, refreshed by `wt.sh sync`; unwritable by aien-run |
 | `wt/<repo>/<sha>` | root dir drakestapleton:aien-run 1775, `.git` root 0644, files aien-run | checkouts; aien-run can add files, cannot replace `.git` or write the mirror |
 | `target/`, `target/<repo>/` | root 0755 | parents cannot be swapped by aien-run |
-| `target/<repo>/{pr,main}` | aien-run | per trust class (A2); a run can write only the one it names; `main` is wiped after FAIL/timeout by the engine |
+| `target/<repo>/{pr,main}/<sha40>` | aien-run | one per head inside its trust class (A2); a run can write only its own; `wt.sh remove` deletes it with the worktree (a shared dir let cargo link one head's build into another, sc #605) |
 
 ## Trigger file contract (A3)
 
diff --git a/tools/aien-local-ci/aien-run-exec b/tools/aien-local-ci/aien-run-exec
index 91ac56ab..e3728a4f 100755
--- a/tools/aien-local-ci/aien-run-exec
+++ b/tools/aien-local-ci/aien-run-exec
@@ -35,7 +35,9 @@
 #             writable one.
 #   --env     K=V with a plain name; PATH, HOME, CARGO_HOME, RUSTUP_HOME, LD_*, BASH_ENV, ENV refused;
 #             each name at most once. CARGO_TARGET_DIR, if given, must be exactly
-#             /var/lib/aien-run/target/<repo>/pr or .../main (the trust class, A2); it is created if missing.
+#             /var/lib/aien-run/target/<repo>/pr/<sha40> or .../main/<sha40>: the head's own dir inside its
+#             trust class (A2), created if missing. With --cwd in a worktree, <repo> and <sha40> must be that
+#             worktree's. One dir per head: a shared dir let cargo link one head's build into another (sc #605).
 #   PROGRAM   may not contain '=' or start with '-' (so it can never be read as an extra environment
 #             assignment by `env`). A bare name resolves only through the sandbox PATH
 #             /var/lib/aien-run/cargo/bin:/usr/local/bin:/usr/bin:/bin (all root-owned, checked at start);
@@ -96,7 +98,7 @@ case "$profile" in host|qemu|gb10) ;; *) die "--profile must be host|qemu|gb10:
 # --cwd: canonical, and inside the sandbox home only the home itself or a worktree.
 [[ $cwd == /* && -d $cwd ]] || die "--cwd must be an existing absolute directory: $cwd"
 [[ $(realpath -e -- "$cwd") == "$cwd" ]] || die "--cwd must be canonical (no symlinks, no trailing slash): $cwd"
-wtroot=""
+wtroot=""; repo=""; sha=""
 case "$cwd" in
   "$RUN_HOME") ;;
   "$RUN_HOME"/wt/*/*)
@@ -106,7 +108,8 @@ case "$cwd" in
   "$RUN_HOME"/*) die "--cwd inside $RUN_HOME must be the home itself or inside wt/<repo>/<sha>: $cwd" ;;
 esac
 
-# --env: plain names, refused names, no duplicates, CARGO_TARGET_DIR only as target/<repo>/pr|main.
+# --env: plain names, refused names, no duplicates, CARGO_TARGET_DIR only as target/<repo>/pr|main/<sha40>
+# (the worktree's own repo and sha when --cwd is in one).
 tdir=""; seen=" "
 for kv in "${envs[@]}"; do
   [[ $kv =~ ^[A-Za-z_][A-Za-z0-9_]*= ]] || die "--env needs K=V with a valid name: $kv"
@@ -118,9 +121,12 @@ for kv in "${envs[@]}"; do
   seen+="$k "
   if [[ $k == CARGO_TARGET_DIR ]]; then
     tdir=${kv#*=}
-    trepo=${tdir#"$RUN_HOME"/target/}; trepo=${trepo%/*}
-    [[ $tdir == "$RUN_HOME/target/$trepo/pr" || $tdir == "$RUN_HOME/target/$trepo/main" ]] && plain_name "$trepo" \
-      || die "CARGO_TARGET_DIR must be $RUN_HOME/target/<repo>/pr or .../main: $tdir"
+    trel=${tdir#"$RUN_HOME"/target/}; IFS=/ read -r trepo tclass tsha textra <<<"$trel"
+    [[ $tdir == "$RUN_HOME/target/$trepo/$tclass/$tsha" && -z $textra && ( $tclass == pr || $tclass == main ) \
+       && $tsha =~ ^[0-9a-f]{40}$ ]] && plain_name "$trepo" \
+      || die "CARGO_TARGET_DIR must be $RUN_HOME/target/<repo>/pr/<sha40> or .../main/<sha40>: $tdir"
+    [[ -z $sha || ( $trepo == "$repo" && $tsha == "$sha" ) ]] \
+      || die "CARGO_TARGET_DIR must be this worktree's own ($repo $sha): $tdir"
   fi
 done
 getent passwd "$RUN_USER" >/dev/null || die "user $RUN_USER does not exist (run tools/aien-local-ci/install.sh)"
@@ -133,10 +139,11 @@ for p in "$RUN_HOME" "$RUN_HOME/cargo" "$RUN_HOME/cargo/bin" "$RUN_HOME/rustup"
   (( (8#$m & 8#022) == 0 )) || die "unsafe host layout: $p is group/other-writable ($m)"
 done
 
-# Target dir of the requested trust class: created on demand (parent root-owned, the class dir aien-run).
+# The head's target dir: created on demand (missing parents root-owned, the head's dir aien-run). Only that one
+# dir is mounted writable below, so a run can never touch another head's build or the other trust class.
 if [[ -n $tdir ]]; then
   if [[ ! -d $tdir ]]; then
-    install -d -o root -g root -m 0755 "${tdir%/*}"
+    [[ -d ${tdir%/*} ]] || install -d -o root -g root -m 0755 "${tdir%/*}"
     install -d -o "$RUN_USER" -g "$RUN_USER" -m 0755 "$tdir"
   fi
   [[ $(realpath -e -- "$tdir") == "$tdir" ]] || die "CARGO_TARGET_DIR must be canonical: $tdir"
diff --git a/tools/aien-local-ci/gc.sh b/tools/aien-local-ci/gc.sh
index 5c953c10..110f96ac 100755
--- a/tools/aien-local-ci/gc.sh
+++ b/tools/aien-local-ci/gc.sh
@@ -7,10 +7,10 @@
 #   * worktrees under /var/lib/aien-run/wt/<repo>/<sha> older than N days (default 3, by mtime of the root dir)
 #     are removed and `git worktree prune` is run on the mirror. A worktree some process still has as its cwd
 #     (a running check) is skipped.
-#   * target dirs /var/lib/aien-run/target/<repo>/{pr,main}: the per-repo total is capped at G GB (default 120 GB,
-#     about 1/3 of the free disk on 2026-10-11; one sovereign-core all-targets dev build is ~30 GB). When over quota
-#     the `pr` dir is wiped first (shared cache, rebuilt on the next PR), then `main` (A2 says `main` is wiped after
-#     any FAIL/timeout anyway). Wiping never touches cargo/registry, cargo/git or rustup (root-owned, read-only).
+#   * target dirs /var/lib/aien-run/target/<repo>/{pr,main}/<sha40>: one per head (sc #605), removed with the head's
+#     worktree by wt.sh remove. gc removes head dirs whose worktree is gone (a worker died mid-run) and the old
+#     shared cache (anything else under pr/ or main/). The per-repo total is reported against G GB (default 120 GB;
+#     one sovereign-core all-targets dev build is ~30 GB). Never touches cargo/registry, cargo/git or rustup.
 #   * stale trigger files older than 7 days are reported, not deleted (the worker owns them).
 set -euo pipefail
 RUN_HOME=/var/lib/aien-run
@@ -39,19 +39,31 @@ for d in "$RUN_HOME"/wt/*/*/; do
   act git -C "$RUN_HOME/mirror/$repo.git" worktree prune
 done
 
-echo "== target quota ${quota_gb} GB per repo"
+echo "== target dirs: one per head (target/<repo>/pr|main/<sha40>), removed with the worktree by wt.sh remove"
+# Left behind only when a worker died mid-run: a head dir whose worktree is gone, older than an hour.
+# Anything else directly under pr/ or main/ is the old shared cache (before sc #605 every head of a repo built
+# in one dir, which let cargo link one head's build into another); nothing reads it any more.
+for c in "$RUN_HOME"/target/*/pr "$RUN_HOME"/target/*/main; do
+  [[ -d $c ]] || continue
+  repo=$(basename "$(dirname "$c")")
+  while IFS= read -r -d '' e; do
+    n=${e##*/}
+    if [[ $n =~ ^[0-9a-f]{40}$ ]]; then
+      [[ -d $RUN_HOME/wt/$repo/$n ]] && continue
+      (( now - $(stat -c %Y "$e") >= 3600 )) || continue
+      echo "orphan head dir: $e"
+    else
+      echo "old shared cache: $e"
+    fi
+    act sudo -n rm -rf "$e"
+  done < <(sudo -n find "$c" -mindepth 1 -maxdepth 1 -print0)
+done
+
+echo "== target quota ${quota_gb} GB per repo (report; head dirs live only as long as their run)"
 for r in "$RUN_HOME"/target/*/; do
   [[ -d $r ]] || continue
   used_gb=$(( $(sudo -n du -sk "$r" | cut -f1) / 1024 / 1024 ))
-  printf '%s: %s GB\n' "$r" "$used_gb"
-  (( used_gb > quota_gb )) || continue
-  for sub in pr main; do
-    [[ -d $r/$sub ]] || continue
-    act sudo -n find "$r/$sub" -mindepth 1 -delete
-    (( apply )) || break
-    used_gb=$(( $(sudo -n du -sk "$r" | cut -f1) / 1024 / 1024 ))
-    (( used_gb > quota_gb )) || break
-  done
+  printf '%s: %s GB%s\n' "$r" "$used_gb" "$( (( used_gb > quota_gb )) && echo ' OVER QUOTA: heads running in parallel, see wt.sh list')"
 done
 
 echo "== stale triggers (>7 days, report only)"
diff --git a/tools/aien-local-ci/tests/per_head_target_test.sh b/tools/aien-local-ci/tests/per_head_target_test.sh
new file mode 100755
index 00000000..8c7cc0ff
--- /dev/null
+++ b/tools/aien-local-ci/tests/per_head_target_test.sh
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+# tests/per_head_target_test.sh: why every head gets its own CARGO_TARGET_DIR (sc #605 flakes, #452 signature).
+#
+# Cargo calls a workspace crate fresh when its outputs are newer than its sources. It compares file times and a
+# path RELATIVE to the workspace, never the absolute checkout path. Two heads that share one target dir therefore
+# share binaries: when head A builds after head B was checked out, head B's gate links and runs head A's build,
+# with head A's CARGO_MANIFEST_DIR baked in. Seen on the Spark as SC-VERIFY-CLOSURE "…/wt/aien-sovereign-core/
+# 2692c1e8…/tests/fixtures/c-cycle: No such file" while judging 9b8e09eb, and as PREFILL-E2E-2 running a mutant.
+#
+# Leg 1 (shared dir, the old layout): must show head B running head A's binary. If it does not, this cargo no
+#        longer has the hazard and the test says so (exit 1) instead of passing silently.
+# Leg 2 (one dir per head, the new layout): head B must run its own build.
+# Needs only cargo; no sandbox, no network.
+set -uo pipefail
+R=$(mktemp -d); trap 'rm -rf "$R"' EXIT
+pass=0; fail=0
+ok()  { pass=$((pass+1)); printf 'PASS %s\n' "$1"; }
+bad() { fail=$((fail+1)); printf 'FAIL %s: %s\n' "$1" "$2"; }
+mk() {  # mk <dir>: one probe crate whose test prints and checks the checkout path baked in at build time
+  mkdir -p "$1/src"
+  printf '[package]\nname = "probe"\nversion = "0.1.0"\nedition = "2021"\n\n[workspace]\n' > "$1/Cargo.toml"
+  cat > "$1/src/lib.rs" <<'EOF'
+pub fn dir() -> &'static str { env!("CARGO_MANIFEST_DIR") }
+#[test]
+fn where_am_i() {
+    println!("BUILT_IN={}", dir());
+    assert!(std::path::Path::new(dir()).exists(), "baked path gone: {}", dir());
+}
+EOF
+}
+leg() {  # leg <name> <target-of-A> <target-of-B>: B checked out first, A built after it, A's checkout removed
+  local n=$1 ta=$2 tb=$3
+  mk "$R/$n/headB"; sleep 1.1; mk "$R/$n/headA"
+  (cd "$R/$n/headA" && CARGO_TARGET_DIR=$ta cargo test -q --offline -- --nocapture >/dev/null 2>&1) || { bad "$n" "head A build failed"; return; }
+  rm -rf "$R/$n/headA"
+  (cd "$R/$n/headB" && CARGO_TARGET_DIR=$tb cargo test -q --offline -- --nocapture 2>&1) | grep -o 'BUILT_IN=[^ ]*' | head -1
+}
+got=$(leg shared "$R/shared/t" "$R/shared/t")
+[[ $got == "BUILT_IN=$R/shared/headA" ]] && ok "shared target dir: head B ran head A's binary (the hazard is real)" \
+  || bad "shared hazard not reproduced" "$got"
+got=$(leg perhead "$R/perhead/t-A" "$R/perhead/t-B")
+[[ $got == "BUILT_IN=$R/perhead/headB" ]] && ok "one target dir per head: head B ran its own build" \
+  || bad "per-head" "$got"
+printf '%d passed, %d failed\n' "$pass" "$fail"
+(( fail == 0 ))
diff --git a/tools/aien-local-ci/tests/run.sh b/tools/aien-local-ci/tests/run.sh
index 9a4abba7..ff99eeff 100755
--- a/tools/aien-local-ci/tests/run.sh
+++ b/tools/aien-local-ci/tests/run.sh
@@ -1,7 +1,7 @@
 #!/usr/bin/env bash
 # tests/run.sh: proves the C8 exec contract on this host. Run as drakestapleton (uses sudo -n). Exit 1 on any FAIL.
 set -uo pipefail
-X=/usr/local/sbin/aien-run-exec
+X=${AIEN_EXEC:-/usr/local/sbin/aien-run-exec}   # AIEN_EXEC: test a candidate helper before install.sh installs it
 RUN_HOME=/var/lib/aien-run
 OP_HOME=/home/drakestapleton
 pass=0; fail=0
@@ -69,17 +69,19 @@ sudo -n -u aien-run ls "$OP_HOME/workspace" >/dev/null 2>&1 && bad "workspace" "
 out=$(run --cwd /tmp --timeout 60 --profile host -- cargo --version 2>&1)
 [[ $out == cargo\ 1.98.1* ]] && ok "cargo as aien-run: $out" || bad "cargo" "$out"
 
-# 12 crash recovery: kill -9 the running child; exit non-zero, no strays in the scope
+# 12 crash recovery: kill -9 the running child; exit non-zero, no strays in the scope. Only this test's scope
+#    counts (its unit is named after the helper pid): a judge running at the same time holds scopes of its own.
 sudo -n "$X" --cwd /tmp --timeout 120 --profile host -- sh -c 'sleep 300 & exec sleep 200' >/dev/null 2>&1 &
 bg=$!
 sleep 2
 victim=$(pgrep -u aien-run -f '^sleep 200$' | head -1)
+helper=$(pgrep -P "$bg" | head -1)
 [[ -n $victim ]] && sudo -n kill -9 "$victim"
 wait $bg; rc=$?
 sleep 1
 strays=$(pgrep -u aien-run -f '^sleep 300$' | wc -l)
-scopes=$(systemctl list-units --no-legend --plain 'aien-run-exec-*.scope' 2>/dev/null | wc -l)
-[[ $rc -ne 0 && $strays -eq 0 && $scopes -eq 0 ]] && ok "crash recovery: rc=$rc, strays=0, scopes=0" || bad "crash recovery" "rc=$rc strays=$strays scopes=$scopes"
+scopes=$(systemctl list-units --no-legend --plain "aien-run-exec-${helper:-none}-*.scope" 2>/dev/null | wc -l)
+[[ -n $helper && $rc -ne 0 && $strays -eq 0 && $scopes -eq 0 ]] && ok "crash recovery: rc=$rc, strays=0, scopes=0" || bad "crash recovery" "helper=${helper:-not found} rc=$rc strays=$strays scopes=$scopes"
 
 # 13 worktree protection (if a worktree exists; also used by the tests below)
 # Only worktrees made by wt.sh (root-owned .git pointing into the mirror). Other lanes may be creating and removing
@@ -129,7 +131,9 @@ fi
 
 # ---- review fixes (INFRA-2): nothing a check writes can reach a later check ----
 refused() { [[ $1 -eq 2 ]]; }   # exit 2 = refused by aien-run-exec before anything ran
-T_PR="$RUN_HOME/target/aien-sovereign-core/pr"; T_MAIN="$RUN_HOME/target/aien-sovereign-core/main"
+T_SHA=0000000000000000000000000000000000000a15; T_SHA2=0000000000000000000000000000000000000b15
+T_PR="$RUN_HOME/target/aien-sovereign-core/pr/$T_SHA"; T_MAIN="$RUN_HOME/target/aien-sovereign-core/main/$T_SHA"
+T_PR2="$RUN_HOME/target/aien-sovereign-core/pr/$T_SHA2"
 
 # 14b the operator's ~/models is visible read-only at /var/lib/aien-run/models (model gates), never writable
 if [[ -d $HOME/models && -d /var/lib/aien-run/models ]]; then
@@ -155,12 +159,14 @@ for p in host gb10; do
   [[ $out == "done " ]] && ok "$p: sandbox home, CARGO_HOME, registry, proxies, .gitconfig all read-only" || bad "$p home read-only" "$out"
 done
 
-# 15 trust classes: a PR run can write target/<repo>/pr only, never main; a main run never pr; a run whose cwd is not
-#    in a worktree can write no worktree
-out=$(run --cwd /tmp --timeout 20 --profile host --env CARGO_TARGET_DIR="$T_PR" -- sh -c "touch $T_PR/.p && rm $T_PR/.p && echo pr-rw; touch $T_MAIN/.p 2>/dev/null && echo MAIN-WRITABLE; echo end" 2>&1 | tr '\n' ' ')
-[[ $out == "pr-rw end " ]] && ok "class pr: target/pr writable, target/main read-only" || bad "class pr" "$out"
+# 15 trust classes and heads: a PR run can write its own target/<repo>/pr/<sha> only, never main and never another
+#    head's dir; a main run never pr; a run whose cwd is not in a worktree can write no worktree
+sudo -n install -d -o aien-run -g aien-run -m 0755 "$T_PR2"
+out=$(run --cwd /tmp --timeout 20 --profile host --env CARGO_TARGET_DIR="$T_PR" -- sh -c "touch $T_PR/.p && rm $T_PR/.p && echo pr-rw; touch $T_MAIN/.p 2>/dev/null && echo MAIN-WRITABLE; touch $T_PR2/.p 2>/dev/null && echo OTHER-HEAD-WRITABLE; echo end" 2>&1 | tr '\n' ' ')
+[[ $out == "pr-rw end " ]] && ok "class pr: own head dir writable, main and another head's dir read-only" || bad "class pr" "$out"
 out=$(run --cwd /tmp --timeout 20 --profile gb10 --env CARGO_TARGET_DIR="$T_MAIN" -- sh -c "touch $T_MAIN/.p && rm $T_MAIN/.p && echo main-rw; touch $T_PR/.p 2>/dev/null && echo PR-WRITABLE; echo end" 2>&1 | tr '\n' ' ')
-[[ $out == "main-rw end " ]] && ok "class main (gb10): target/main writable, target/pr read-only" || bad "class main" "$out"
+[[ $out == "main-rw end " ]] && ok "class main (gb10): own head dir writable, target/pr read-only" || bad "class main" "$out"
+sudo -n rm -rf "$T_PR" "$T_MAIN" "$T_PR2"
 if [[ -n $wt ]]; then
   out=$(run --cwd /tmp --timeout 20 --profile host -- sh -c "touch $wt/.p 2>/dev/null && echo WT-WRITABLE; echo end" 2>&1 | tr '\n' ' ')
   [[ $out == "end " ]] && ok "a run outside a worktree cannot write any worktree" || bad "foreign worktree" "$out"
@@ -171,7 +177,23 @@ run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR=/tmp/t -- true
 run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/../target/x/main" -- true >/dev/null 2>&1; r2=$?
 run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/aien-sovereign-core/other" -- true >/dev/null 2>&1; r3=$?
 run --cwd /tmp --timeout 5 --profile host --env FOO=1 --env FOO=2 -- true >/dev/null 2>&1; r4=$?
-refused $r1 && refused $r2 && refused $r3 && refused $r4 && ok "CARGO_TARGET_DIR outside target/<repo>/pr|main and duplicate --env refused" || bad "env refusals" "$r1 $r2 $r3 $r4"
+# the old shared class dir, a head dir with a bad sha, and a dir one level too deep
+run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/aien-sovereign-core/pr" -- true >/dev/null 2>&1; r5=$?
+run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/aien-sovereign-core/pr/abc" -- true >/dev/null 2>&1; r6=$?
+run --cwd /tmp --timeout 5 --profile host --env CARGO_TARGET_DIR="$T_PR/x" -- true >/dev/null 2>&1; r7=$?
+refused $r1 && refused $r2 && refused $r3 && refused $r4 && refused $r5 && refused $r6 && refused $r7 \
+  && ok "CARGO_TARGET_DIR other than target/<repo>/pr|main/<sha40> (incl. the old shared dir) and duplicate --env refused" \
+  || bad "env refusals" "$r1 $r2 $r3 $r4 $r5 $r6 $r7"
+if [[ -n $wt ]]; then
+  # inside a worktree the target dir must be that head's own: another head's dir is refused
+  wrel=${wt#"$RUN_HOME"/wt/}; wrepo=${wrel%%/*}; wsha=${wrel#*/}; wsha=${wsha%%/*}
+  had_class=0; [[ -d $RUN_HOME/target/$wrepo/pr ]] && had_class=1
+  run --cwd "$wt" --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/$wrepo/pr/$T_SHA2" -- true >/dev/null 2>&1; r1=$?
+  run --cwd "$wt" --timeout 5 --profile host --env CARGO_TARGET_DIR="$RUN_HOME/target/$wrepo/pr/$wsha" -- true >/dev/null 2>&1; r2=$?
+  sudo -n rm -rf "$RUN_HOME/target/$wrepo/pr/$wsha"
+  (( had_class )) || sudo -n rmdir "$RUN_HOME/target/$wrepo/pr" 2>/dev/null || true
+  refused $r1 && [[ $r2 -eq 0 ]] && ok "in a worktree only that head's own target dir is accepted" || bad "head-bound target dir" "$r1 $r2"
+fi
 run --cwd "$RUN_HOME/target" --timeout 5 --profile host -- true >/dev/null 2>&1; r1=$?
 run --cwd "$RUN_HOME/cargo" --timeout 5 --profile host -- true >/dev/null 2>&1; r2=$?
 run --cwd "$RUN_HOME/wt/../wt" --timeout 5 --profile host -- true >/dev/null 2>&1; r3=$?
diff --git a/tools/aien-local-ci/wt.sh b/tools/aien-local-ci/wt.sh
index 8bd27639..02322e24 100755
--- a/tools/aien-local-ci/wt.sh
+++ b/tools/aien-local-ci/wt.sh
@@ -7,7 +7,8 @@
 #   wt.sh sibling <repo> <sha40> <dir> <source-repo> <source-sha>
 #                                              read-only checkout of <source-repo> at <source-sha> beside it, at
 #                                              /var/lib/aien-run/wt/<repo>/<sha40>/<dir> (what ../<dir> means there)
-#   wt.sh remove  <repo> <sha>                 remove /var/lib/aien-run/wt/<repo>/<sha> with every sibling in it
+#   wt.sh remove  <repo> <sha>                 remove /var/lib/aien-run/wt/<repo>/<sha> with every sibling in it,
+#                                              and the head's target dirs /var/lib/aien-run/target/<repo>/pr|main/<sha>
 #   wt.sh list                                 list run directories with age
 #
 # Why a mirror: the operator's clones live under ~/workspace (0700), which aien-run must not read. The mirror is a
@@ -80,6 +81,14 @@ case "$cmd" in
     name_ok "$repo" && [[ $sha =~ ^[0-9a-f]{7,40}$ ]] || die "bad repo or sha"
     d=$(ls -d "$WT/$repo/$sha"* 2>/dev/null | head -1); [[ -n $d ]] || die "no worktree for $sha"
     sudo -n rm -rf "$d"
+    # The head's own cargo target dirs (one per head and trust class, see aien-run-exec) go with it: nothing
+    # reuses them, and a dev build of sovereign-core is ~30 GB.
+    full=${d##*/}
+    if [[ $full =~ ^[0-9a-f]{40}$ ]]; then
+      for t in "$BASE/target/$repo/pr/$full" "$BASE/target/$repo/main/$full"; do
+        if [[ -d $t ]]; then sudo -n rm -rf "$t"; printf 'removed %s\n' "$t"; fi
+      done
+    fi
     # The checkout and its siblings were worktrees of (possibly) several mirrors.
     for m in "$MIRROR"/*.git; do [[ -d $m ]] && git -C "$m" worktree prune; done
     printf 'removed %s\n' "$d"
@@ -90,5 +99,5 @@ case "$cmd" in
       printf '%s  %s days\n' "$d" "$(( ( $(date +%s) - $(stat -c %Y "$d") ) / 86400 ))"
     done
     ;;
-  *) sed -n '2,11p' "$0" >&2; exit 2 ;;
+  *) sed -n '2,12p' "$0" >&2; exit 2 ;;
 esac
diff --git a/tools/aien-test/docs/LOCAL_VERIFICATION.md b/tools/aien-test/docs/LOCAL_VERIFICATION.md
index 0f36bffb..a72173e4 100644
--- a/tools/aien-test/docs/LOCAL_VERIFICATION.md
+++ b/tools/aien-test/docs/LOCAL_VERIFICATION.md
@@ -108,7 +108,7 @@ What the helper requires (INFRA, read from the installed script): `--cwd`
 canonical, and inside `/var/lib/aien-run` only a run directory
 `wt/<repo>/<40-hex>` or below it; host and qemu checks get a private `/tmp`, so a tree
 under `/tmp` is invisible to them; `CARGO_TARGET_DIR` only as
-`/var/lib/aien-run/target/<repo>/pr` or `.../main` (created on demand).
+`/var/lib/aien-run/target/<repo>/pr/<sha40>` or `.../main/<sha40>`, the head's own (created on demand).
 Inside a run everything is read-only except the run's own run directory (file
 ownership inside it still applies: siblings are not writable), its one
 target dir and a private `/tmp`. host and qemu checks have no network but
@@ -126,8 +126,10 @@ otherwise. `--sandbox none` runs gates as the worker's own user, who can read
 the signing key: the worker refuses it (exit 2) unless `--dev-unsandboxed` is
 given, and refuses it with `--github` in every case. The library has the same
 rule (`Config.allow_unsandboxed`, default false). Under `--sandbox user` the
-worker passes `CARGO_TARGET_DIR=/var/lib/aien-run/target/<repo>/main` for a
-head already on the base line and `.../pr` for any other head (A2).
+worker passes `CARGO_TARGET_DIR=/var/lib/aien-run/target/<repo>/main/<sha>` for a
+head already on the base line and `.../pr/<sha>` for any other head (A2). One dir
+per head: in a shared dir cargo linked one head's build into another (sc #605);
+`wt.sh remove` deletes the dir with the worktree.
 
 The gates of one head share that target dir, so the runner holds it for one
 gate at a time: every gate that runs takes an exclusive `flock` on
@@ -414,7 +416,7 @@ unmapped path, no blocked gate, no problem line.
   <repo>/                                the detached checkout under test
   <dir>/                                 a read-only sibling checkout (../<dir> from the checkout)
 <clones>/aien-sovereign-core/tools/aien-local-ci/siblings   the operator's sibling map
-/var/lib/aien-run/target/<repo>/pr|main  cargo target dir per trust class (sandbox user)
+/var/lib/aien-run/target/<repo>/pr|main/<sha>  cargo target dir per head and trust class (sandbox user)
 /usr/local/sbin/aien-run-exec            the exec helper (INFRA)
 ```
 
diff --git a/tools/aien-test/src/worker.rs b/tools/aien-test/src/worker.rs
index 6982dd4c..ae694857 100644
--- a/tools/aien-test/src/worker.rs
+++ b/tools/aien-test/src/worker.rs
@@ -221,7 +221,8 @@ pub struct Config {
     /// worker user, who can read the signing key). Never with `github`.
     pub allow_unsandboxed: bool,
     /// Per-repository cargo target directories by trust class
-    /// (`<target_root>/<repo>/main|pr`), used under `Sandbox::User`.
+    /// (`<target_root>/<repo>/main|pr/<sha>`, one per head), used under
+    /// `Sandbox::User`.
     pub target_root: PathBuf,
     /// INFRA's worktree tool (`tools/aien-local-ci/wt.sh`). Under
     /// `Sandbox::User` every checkout is made and removed with it; without it
@@ -919,7 +920,7 @@ fn run_in_worktree(
     fs::create_dir_all(&evidence_dir).map_err(|e| format!("cannot create evidence dir: {e}"))?;
     let mut opts = Options::new(evidence_dir.clone());
     opts.sandbox = cfg.sandbox;
-    opts.cargo_target_dir = target_dir_for(cfg, repo, on_main);
+    opts.cargo_target_dir = target_dir_for(cfg, repo, sha, on_main);
     opts.target_lock_dir = cfg.state.target_lock_dir();
     opts.pool_sizes = opts.pool_sizes.with(cfg.jobs);
     opts.pins = pins;
@@ -1151,18 +1152,24 @@ fn run_in_worktree(
 }
 
 /// `CARGO_TARGET_DIR` for the gates of one head. Under the user sandbox it is
-/// the INFRA target dir of the head's trust class (A2):
-/// `<target_root>/<repo>/main` for a head on the base line, `.../pr`
-/// otherwise. The exec helper accepts only exactly those two paths under
-/// `/var/lib/aien-run/target` and creates the class dir on demand. Never the
-/// worker's own target dir, which `aien-run` cannot write. Without the
+/// the head's OWN dir inside its trust class (A2):
+/// `<target_root>/<repo>/main/<sha>` for a head on the base line,
+/// `.../pr/<sha>` otherwise. Never shared between heads: cargo calls a
+/// workspace crate fresh by file times and a path relative to the workspace,
+/// so with one dir per class a head that built after this one was checked out
+/// had its binaries linked and run here, with its checkout path baked in
+/// (sc #605: SC-TEST, SC-AGENT-PREFLIGHT, SC-VERIFY-CLOSURE "No such file" in
+/// another head's worktree; #452: PREFILL-E2E-2 running a mutant). The exec
+/// helper accepts only this shape, with `<sha>` equal to the worktree's, and
+/// creates it on demand; `wt.sh remove` deletes it with the worktree. Never
+/// the worker's own target dir, which `aien-run` cannot write. Without the
 /// sandbox, the worker's own `CARGO_TARGET_DIR`.
-fn target_dir_for(cfg: &Config, repo: &str, on_main: bool) -> Option<PathBuf> {
+fn target_dir_for(cfg: &Config, repo: &str, sha: &str, on_main: bool) -> Option<PathBuf> {
     match cfg.sandbox {
         Sandbox::None => std::env::var_os("CARGO_TARGET_DIR").map(PathBuf::from),
         Sandbox::User => {
             let class = if on_main { "main" } else { "pr" };
-            Some(cfg.target_root.join(repo).join(class))
+            Some(cfg.target_root.join(repo).join(class).join(sha))
         }
     }
 }
@@ -1481,6 +1488,37 @@ pub fn run_loop(cfg: &Config, iterations: Option<u64>) -> usize {
 mod tests {
     use super::*;
 
+    #[test]
+    fn every_head_gets_its_own_target_dir_inside_its_trust_class() {
+        let mut cfg = Config::new(
+            StateDir::new(Path::new("/s")),
+            Path::new("/k"),
+            Path::new("/c"),
+            Path::new("/w"),
+        );
+        cfg.sandbox = Sandbox::User;
+        let (a, b) = ("a".repeat(40), "b".repeat(40));
+        let pr_a = target_dir_for(&cfg, "aien-sovereign-core", &a, false).unwrap();
+        let pr_b = target_dir_for(&cfg, "aien-sovereign-core", &b, false).unwrap();
+        let main_a = target_dir_for(&cfg, "aien-sovereign-core", &a, true).unwrap();
+        assert_eq!(
+            pr_a,
+            Path::new(TARGET_ROOT)
+                .join("aien-sovereign-core/pr")
+                .join(&a)
+        );
+        assert_eq!(
+            main_a,
+            Path::new(TARGET_ROOT)
+                .join("aien-sovereign-core/main")
+                .join(&a)
+        );
+        // Two heads never share a dir (cargo would link one head's build into
+        // the other, sc #605), and a head's pr dir is never its main dir (A2).
+        assert_ne!(pr_a, pr_b);
+        assert_ne!(pr_a, main_a);
+    }
+
     #[test]
     fn pin_list_selects_the_repository_and_refuses_bad_lines() {
         let sha = "353b7d4be3f00169545caec1615cffa61f7d311a";