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.
fix(aien-test): hold the target-dir lock for the whole run, from before the checkout
closed600856c7d6adc490 #655 · opened 2026-10-18 14:30 UTC · closed 2026-10-29 19:00 UTC
Imported from GitHub aien-dev/aien-sovereign-core-mv#655, by drakestapleton
Base e6852de85dfe503347d37ed2438d1ce6f1ac8c3a · Head f5f2d71238fe18c5c2c51f5466b4f948bcec9737
drakestapleton wrote on 2026-10-18 14:30 UTC
What
The worker takes the per-target-dir flockbefore it makes the checkout and holds it until the worktree is removed (run-level lock). Gates of that run skip the file lock and take turns through an in-process mutex on the same path, so two gates still never build in one target dir at once (fix/521 stays). Gates run outside the worker (aien-test run, aien-test test) keep the per-gate file lock.
Why
Two heads of one repo interleaved gate by gate in the shared cargo target dir (/var/lib/aien-run/target/<repo>/pr). Cargo judges a workspace crate fresh when no source file is newer than the last build in that dir, so a checkout made before the other head's build reused that head's artifacts.
Receipt (2026-10-18, sc): 1f39046's SC-TEST ran 13:15:26Z to 13:36:14Z and built deployment_test at 13:25:12Z. 0d035972 (#645) was checked out at 13:16:58Z; its SC-TEST at 14:04Z said Finished test profile in 4.42s, reused that binary, and the test failed NotFound because env!("CARGO_MANIFEST_DIR") had baked 1f39046's checkout path into it (strings on the binary shows the 1f39046 path; cargo's fingerprint for the test lists no CARGO_MANIFEST_DIR, so it never rebuilds for a moved checkout). With differing sources the run would have tested the other head's code and nobody would have noticed.
A checkout made under the lock is newer than anything built there, so every workspace crate is rebuilt once per run; third-party dependencies are keyed by package id and version, not mtime, and stay cached.
Proof
cargo test -p aien-test: 317 pass (new: gates_under_a_held_run_lock_still_take_turns, runs the campaign on a thread so a regression fails instead of hanging).
Operator runs with this binary: 0d035972 (#645, the false FAIL above) and this head, logs in the handoff ~/handoffs/2026-10-18-ci-runners-rethink.md.
Pipeline-config change: lanes mark this head BLOCKED_OPERATOR PIPELINE_CONFIG_CHANGED; the operator run is the verdict. Merge pause in force: not to be merged until Drake lifts it.
diff --git a/.crumb b/.crumb
index f5e6e78e..55f1169d 100644
--- a/.crumb
+++ b/.crumb
@@ -119,14 +119,14 @@
}
],
"children_root": "sha256:b85c8810abe85be616a42d2802e5637c38014f05e3c06d23705fef6ea5a766f2",
- "compiled_at": "2026-10-18T15:37:23Z",
+ "compiled_at": "2026-10-18T18:08:18Z",
"compiler": "crumb-compile/1",
- "digest": "sha256:2dbc3bfc48f62db17b27a04f0fd028ee3ad19f77af8d9cac6f93d3e77d147f7b",
+ "digest": "sha256:c27026001faaff194fba6f002a3390882c59d6f3f57e8d71b8009de1af46af59",
"evidence_files": 1578,
"evidence_root": "sha256:135e27a1a0704c4a8371cd2c1eb555f2b5beec61c212949b029a5457418a7160",
"files": 5891,
- "generated_at_commit": "6b58967d3b40455d5180fa7f22237f0b46912b98",
- "source_tree": "sha256:1769c5da9a48ba6c23bc738850153c93581c418175c8ef5ac9e31926c116dfb0"
+ "generated_at_commit": "553ab96c419c231f65d9705b94460251ec7f511a",
+ "source_tree": "sha256:49ed59e96b6e0b9257598fd5cb47ec3c34e17c4e6fe8566381c0dd608e342674"
}
},
"history": [],
diff --git a/README.md b/README.md
index c592b572..4a8662a4 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,8 @@ cargo test --workspace -- --test-threads=1 # one test thread, as CI runs it
scripts/repro-build.sh -p aien-cli # release build with machine-local paths removed; build aien-cli on its own, because its digest changes with the other packages in the same cargo command (docs/release/F1-binary-digest-difference.md)
```
+The judge holds a repo's shared build cache for a whole run, from before the checkout is made until it is removed, so one head never tests another head's compiled crates; see `tools/aien-test/docs/LOCAL_VERIFICATION.md` for why.
+
Installing a release (state on 2026-10-11, from `install.sh` and the dry-run artifact of Actions run 38105672607, nothing else):
- `install.sh` with no arguments builds from source and verifies no signature. With `AIEN_RELEASE_TAG=vX.Y.Z` it installs a published release instead: it downloads the archive, `SHA256SUMS.txt` and `SHA256SUMS.txt.sig`, checks the signature with `ssh-keygen`, then the archive checksum, and writes `install-receipt.json` with a `signature_check` of `release`, `internal-test-key` or `dry-run-key` (see [docs/release/RELEASE_SIGNING.md](docs/release/RELEASE_SIGNING.md)).
diff --git a/tools/aien-test/docs/LOCAL_VERIFICATION.md b/tools/aien-test/docs/LOCAL_VERIFICATION.md
index 0f36bffb..5f7e82e5 100644
--- a/tools/aien-test/docs/LOCAL_VERIFICATION.md
+++ b/tools/aien-test/docs/LOCAL_VERIFICATION.md
@@ -129,22 +129,39 @@ 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).
-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
-`<state>/locks/target/<sanitised target dir>.lock` (the `CARGO_TARGET_DIR` it
-is given, or else the worktree's own `target/`) for its whole run, build and
-execution, and releases it when its process has exited. Every gate, not only
-those whose manifest names cargo inputs: SC-INQUISITOR builds with cargo and
-lists no `Cargo.toml`, and the runner cannot see inside a script. The lock is
-the worker's, never the gate's. Without it two gates built in one target dir
-at once and one relinked a test binary the other was executing (sc fix/521:
+The gates of one head share that target dir, and so do the heads of one
+repo, so the worker takes an exclusive `flock` on
+`<state>/locks/target/<sanitised target dir>.lock` before it makes the
+checkout and holds it until the last gate has exited and the checkout is
+removed (the run-level lock; heartbeat phase `lock` while it waits; the
+head's base is decided from the trusted clone so the target dir is known
+first). Why before the checkout: cargo judges a workspace crate fresh when
+none of its sources is newer than the last build in the target dir. Two
+heads interleaving gate by gate in one target dir broke that: a checkout
+made at t0 whose sources differ from the head built there at t1 > t0 was
+"fresh", and cargo reused the other head's artifacts (sc 2026-10-18:
+0d035972's SC-TEST ran the `deployment_test` binary 1f39046 had built, with
+1f39046's checkout path baked in by `env!("CARGO_MANIFEST_DIR")` and gone
+since: `NotFound`, a false FAIL; with differing sources it would have been
+the wrong code under test). A checkout made under the lock is newer than
+anything built there, so every workspace crate is rebuilt once per run;
+third-party dependencies are keyed by package id and version, not mtime,
+and stay cached. Gates run outside the worker (`aien-test run`, `aien-test
+test`) still take the same lock one gate at a time: every gate that runs,
+not only those whose manifest names cargo inputs, because SC-INQUISITOR
+builds with cargo and lists no `Cargo.toml`, and the runner cannot see
+inside a script. Without a lock two gates built in one target dir at once
+and one relinked a test binary the other was executing (sc fix/521:
SC-AGENT-PREFLIGHT under SC-TEST, `current_exe: NotFound`, a false FAIL).
-Gates given different target dirs still run at the same time. The fixed lock
-order of one gate is: pool slot, target-dir lock, `/tmp/aien-gb10.lock`,
-aien-proof `slots/gpu.lock`; the worker's target priming takes only the
-target-dir lock. Known limit: a gb10 gate is never killed on timeout, so a
+Gates given different target dirs still run at the same time. The fixed
+lock order of one gate is: pool slot, target-dir lock (under the worker's
+run-level file lock the gates of that run take turns through an in-process
+mutex on the same path instead, so two of them still never build in one
+target dir at once), `/tmp/aien-gb10.lock`, aien-proof `slots/gpu.lock`. Known limit: a gb10 gate is never killed on timeout, so a
hung one holds the target dir until it exits on its own.
+
+
Under `--sandbox user` every checkout is made with INFRA's worktree tool
(`--wt-tool`, default `<clones>/aien-sovereign-core/tools/aien-local-ci/wt.sh`;
without it the worker refuses to run). `wt.sh sync` refreshes the bare mirror
diff --git a/tools/aien-test/src/resources.rs b/tools/aien-test/src/resources.rs
index 53c116d8..fba25426 100644
--- a/tools/aien-test/src/resources.rs
+++ b/tools/aien-test/src/resources.rs
@@ -483,6 +483,32 @@ pub fn lock_target_dir(path: &Path) -> io::Result<TargetLock> {
})
}
+static IN_PROCESS_TARGET_LOCKS: Mutex<Vec<(PathBuf, &'static Mutex<()>)>> = Mutex::new(Vec::new());
+
+/// The in-process half of the target-dir lock. When the worker holds the
+/// file lock for its whole run (`Options::target_lock_held`), its gates
+/// cannot take that lock again (a second `flock` on another open file
+/// waits for ever), yet two of them must still never build in one target
+/// dir at once (sc fix/521: one relinked the test binary the other was
+/// executing). So under a held run lock each gate takes turns through this
+/// mutex instead, keyed by the lock file's path.
+pub fn lock_target_dir_in_process(path: &Path) -> MutexGuard<'static, ()> {
+ let m: &'static Mutex<()> = {
+ let mut table = IN_PROCESS_TARGET_LOCKS
+ .lock()
+ .unwrap_or_else(|e| e.into_inner());
+ match table.iter().find(|(p, _)| p == path) {
+ Some((_, m)) => m,
+ None => {
+ let m: &'static Mutex<()> = Box::leak(Box::new(Mutex::new(())));
+ table.push((path.to_path_buf(), m));
+ m
+ }
+ }
+ };
+ m.lock().unwrap_or_else(|e| e.into_inner())
+}
+
fn open_lock(path: &Path) -> io::Result<File> {
if let Some(parent) = path.parent() {
if !parent.as_os_str().is_empty() {
diff --git a/tools/aien-test/src/runner.rs b/tools/aien-test/src/runner.rs
index 3f9e8684..c6acb57a 100644
--- a/tools/aien-test/src/runner.rs
+++ b/tools/aien-test/src/runner.rs
@@ -66,6 +66,10 @@ pub struct Options {
/// or the worktree's `target/`) for its whole run, so two gates never
/// build in one target dir at once. The worker sets `<state>/locks/target`.
pub target_lock_dir: PathBuf,
+ /// The worker already holds the target-dir lock for the whole run, so
+ /// gates skip their own (a second `flock` on another open file would
+ /// wait for ever). See `worker::run_in_worktree`.
+ pub target_lock_held: bool,
/// `--release`: qualification mode. No cache reuse and the tree must be
/// clean even with `--allow-dirty`.
pub release: bool,
@@ -94,6 +98,7 @@ impl Options {
sandbox: Sandbox::None,
cargo_target_dir: None,
target_lock_dir: PathBuf::from(crate::resources::DEFAULT_TARGET_LOCK_DIR),
+ target_lock_held: false,
release: false,
pins: json!([]),
operator: false,
@@ -685,14 +690,23 @@ fn execute_gate(job: &Job<'_>, permit: Option<Permit<'_>>) -> Result<Outcome, Ru
// A gb10 gate is never killed on timeout (ADR 0028 Decision 6), so one
// that hangs holds the target dir until it exits; the receipt's
// `timeout_exceeded` is the only sign.
- let target_lock: Option<resources::TargetLock> = if verdict::pre_run(m, &facts).is_none() {
+ let will_lock = verdict::pre_run(m, &facts).is_none();
+ let target_lock_path = {
let dir = opts
.cargo_target_dir
.clone()
.unwrap_or_else(|| resolve(root).join("target"));
- let path = resources::target_lock_path(&opts.target_lock_dir, &dir);
- Some(resources::lock_target_dir(&path).map_err(|e| {
- RunError::Fatal(format!("cannot lock target dir {}: {e}", path.display()))
+ resources::target_lock_path(&opts.target_lock_dir, &dir)
+ };
+ // Under the worker's run-level file lock, gates take turns in process.
+ let mut in_process_lock = (will_lock && opts.target_lock_held)
+ .then(|| resources::lock_target_dir_in_process(&target_lock_path));
+ let target_lock: Option<resources::TargetLock> = if will_lock && !opts.target_lock_held {
+ Some(resources::lock_target_dir(&target_lock_path).map_err(|e| {
+ RunError::Fatal(format!(
+ "cannot lock target dir {}: {e}",
+ target_lock_path.display()
+ ))
})?)
} else {
None
@@ -770,6 +784,7 @@ fn execute_gate(job: &Job<'_>, permit: Option<Permit<'_>>) -> Result<Outcome, Ru
// The job is over: close the lock descriptors at once.
drop(gpu);
drop(target_lock);
+ in_process_lock.take();
after = porcelain(root).map_err(RunError::Fatal)?;
let head_after = head(root).unwrap_or_default();
facts.tree_changed_after = after != before;
@@ -2001,6 +2016,38 @@ mod tests {
assert!(lock.exists(), "lock {} never taken", lock.display());
}
+ /// With the worker's run-level file lock held (`target_lock_held`), gates
+ /// skip the file lock (a second `flock` would wait for ever) but still
+ /// take turns in the target dir through the in-process lock (sc fix/521).
+ /// The campaign runs on its own thread so a regression fails instead of
+ /// hanging the test.
+ #[test]
+ fn gates_under_a_held_run_lock_still_take_turns() {
+ let fx = gfx(&[
+ ("G-A", "", "host", slow("G-A")),
+ ("G-B", "", "host", slow("G-B")),
+ ]);
+ let mut opts = fx.opts();
+ opts.target_lock_dir = fx.scratch.path().join("locks");
+ opts.target_lock_held = true;
+ let lock = crate::resources::target_lock_path(
+ &opts.target_lock_dir,
+ &resolve(fx.repo.path()).join("target"),
+ );
+ let _held_by_the_worker = crate::resources::lock_target_dir(&lock).unwrap();
+ let (tx, rx) = std::sync::mpsc::channel();
+ std::thread::spawn(move || {
+ let c = fx.campaign(&opts);
+ let _ = tx.send((c.exit_code(), fx.log()));
+ });
+ let (code, log) = rx
+ .recv_timeout(std::time::Duration::from_secs(60))
+ .expect("gates waited on the file lock the worker already holds");
+ assert_eq!(code, 0);
+ assert_eq!(log.len(), 4, "{log:?}");
+ assert_serial(&log, "gates under a held run lock");
+ }
+
/// A gate may call cargo without naming `Cargo.toml` among its inputs
/// (SC-INQUISITOR builds spark-inquisitor that way), so the rule is not
/// "cargo gates": every gate given a `CARGO_TARGET_DIR` holds it.
diff --git a/tools/aien-test/src/worker.rs b/tools/aien-test/src/worker.rs
index 6982dd4c..20d2af5d 100644
--- a/tools/aien-test/src/worker.rs
+++ b/tools/aien-test/src/worker.rs
@@ -29,7 +29,7 @@ use crate::evidence::{canonical, canonical_digest, sha256_hex, utc_string};
use crate::graph;
use crate::plan::{self, Known, Section};
use crate::process;
-use crate::resources::{Hardware, JobsSpec};
+use crate::resources::{self, Hardware, JobsSpec};
use crate::runner::{self, CacheNote, Options};
use crate::sandbox::Sandbox;
use crate::verdict::Verdict;
@@ -510,6 +510,12 @@ struct Checkout {
wt: PathBuf,
run_dir: PathBuf,
nested: bool,
+ /// Whether the head is on the base line, decided from the clone before the
+ /// checkout existed (it picks the target dir, so it had to be known first).
+ on_main: bool,
+ /// The run-level target-dir lock, taken before the checkout was made and
+ /// held until this value drops (after the worktree is removed).
+ run_lock: Option<resources::TargetLock>,
}
/// Make the read-only sibling checkouts of one head, each from its trusted
@@ -724,6 +730,27 @@ fn checkout(
if !present(&clone) {
return Err(format!("commit {sha} is not in {}", clone.display()));
}
+ // The run-level target-dir lock (docs/LOCAL_VERIFICATION.md): taken before
+ // the checkout exists, so every file the checkout gets is newer than
+ // anything built in that dir and cargo rebuilds each workspace crate from
+ // this head's own sources. Held until the Checkout drops.
+ let (_, on_main) = plan::base_for(&clone, &cfg.base_ref, sha)?;
+ let run_lock = match target_dir_for(cfg, repo, on_main) {
+ Some(dir) => {
+ cfg.state.beat(repo, sha, "lock");
+ let path = resources::target_lock_path(&cfg.state.target_lock_dir(), &dir);
+ let t0 = std::time::Instant::now();
+ let lock = resources::lock_target_dir(&path)
+ .map_err(|e| format!("cannot lock target dir {}: {e}", path.display()))?;
+ notes.push(format!(
+ "target dir {} held for the whole run (waited {} s before the checkout)",
+ dir.display(),
+ t0.elapsed().as_secs()
+ ));
+ Some(lock)
+ }
+ None => None,
+ };
if cfg.sandbox == Sandbox::User {
let (wt, run_dir, nested) = checkout_for_sandbox(cfg, repo, sha, &clone)?;
return Ok(Checkout {
@@ -731,6 +758,8 @@ fn checkout(
wt,
run_dir,
nested,
+ on_main,
+ run_lock,
});
}
let run_dir = cfg.wt_root.join(repo).join(sha);
@@ -761,6 +790,8 @@ fn checkout(
wt,
run_dir,
nested: true,
+ on_main,
+ run_lock,
})
}
@@ -914,13 +945,20 @@ fn run_in_worktree(
let plan = plan::compute(wt, &req)?;
// Run what the plan selects, through the sandbox and the pools.
+ if on_main != co.on_main {
+ notes.push(format!(
+ "base detection differs: worktree says on_main={on_main}, clone said {}; the locked target dir follows the clone",
+ co.on_main
+ ));
+ }
cfg.state.beat(repo, sha, "run");
let evidence_dir = cfg.state.evidence(repo, sha);
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, co.on_main);
opts.target_lock_dir = cfg.state.target_lock_dir();
+ opts.target_lock_held = co.run_lock.is_some();
opts.pool_sizes = opts.pool_sizes.with(cfg.jobs);
opts.pins = pins;
// The same operator permission the plan had: an operator gate the plan