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

GitHub aien-dev/aien-sovereign-core-mv#576 (code only)

closed derived effac95e6964a3b1 #576

Imported from GitHub aien-dev/aien-sovereign-core-mv#576 (code only)

GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed).

Base 26303a9ec008ae380c860095f275af12b0d443b7 · Head a4f3942309427cac43e87f9747cb779e3073e1cb

Apply it

git checkout -b patch-effac95e6964a3b1 26303a9ec008ae380c860095f275af12b0d443b7
curl -fsSL https://interplane.io/drakestapleton/aien-sovereign-core-mv/patches/effac95e6964a3b1.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
576
a4f3942309427cac43e87f9747cb779e3073e1cb
{"kind":"github-import","owner":"aien-dev","repo":"aien-sovereign-core-mv","number":576,"url":"https://github.com/aien-dev/aien-sovereign-core-mv/pull/576","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:33Z","metadata":"unavailable","state_derived":true}

Changes

diff --git a/.crumb b/.crumb
index f1d77dbe..caa30ae6 100644
--- a/.crumb
+++ b/.crumb
@@ -65,68 +65,68 @@
       "children": [
         {
           "digest": "4e7f370019e67f6c71565d1544530851eb1b3b1bed61c2712f4ac7d9614a89bf",
-          "last_commit": "6bbe2ec2",
+          "last_commit": "6bbe2ec",
           "name": "crates/aien-allen",
           "purpose": "ALLEN identity gate: resolve-only subject check, host pin record, deployment record (ADR 0035). "
         },
         {
           "digest": "40a231c50dba92af7de2527ab33879c5d1eaaa6e7ea5d6a5ebaa9d60c7542075",
-          "last_commit": "b9b52f9c",
+          "last_commit": "b9b52f9",
           "name": "crates/aien-allen-memory",
           "purpose": "ALLEN scoped memory store: scopes enforced at recall, inspect/correct/export, key-destroying for"
         },
         {
           "digest": "44dc482b8c9db8919a68ec69b019cd77967232a6ec8b1910c0dfe3cd4d71f141",
-          "last_commit": "b9b52f9c",
+          "last_commit": "b9b52f9",
           "name": "crates/aien-allen-profile",
           "purpose": "ALLEN persona profile v1: versioned, user-editable profile bound to the engaged ALLEN identity; "
         },
         {
           "digest": "f5571bcc0bb95b44dd8bf8f0b47ded3bcb1e1ee76c81b4ff5edb87ac237d66d8",
-          "last_commit": "f8eea420",
+          "last_commit": "f8eea42",
           "name": "crates/aien-cli",
           "purpose": ""
         },
         {
           "digest": "25fd9f8afbea19702750f8ddba2ef4aeafc5bc4515aa695340a5af6931989c03",
-          "last_commit": "8cca8020",
+          "last_commit": "8cca802",
           "name": "crates/aien-scheduler/src/bin",
           "purpose": ""
         },
         {
           "digest": "a0a981f6168d15e43a6b036c3aa82d44547aadfc3b6c62139a5a3afab0644514",
-          "last_commit": "8cca8020",
+          "last_commit": "8cca802",
           "name": "crates/spark-cockpit-rs",
           "purpose": ""
         },
         {
           "digest": "f38ccede5d794040581cf48d409424c923d705a2fc206140b52691ef7fb186ef",
-          "last_commit": "deff6f7a",
+          "last_commit": "deff6f7",
           "name": "scripts",
           "purpose": ""
         },
         {
           "digest": "75e439cb2ae120e1807af894b70b6454971028f5527f56451f9d6558f503c88b",
-          "last_commit": "fa73e778",
+          "last_commit": "fa73e77",
           "name": "skills",
           "purpose": ""
         },
         {
           "digest": "b95ea9baa15ce7ddc69a3ef4dc25cdcccba52ec1f5fd99e19f0f24af9181a306",
-          "last_commit": "247e0927",
+          "last_commit": "247e092",
           "name": "tools/aien-local-ci",
           "purpose": "Local CI sandbox exec contract (C8), aien-run user/caches install, Forgejo trigger hook, health "
         }
       ],
       "children_root": "sha256:7d8cf5937a13dd896fe511aee80a0873f4917a0a5eb07cbe0fe3690c0c878cbb",
-      "compiled_at": "2026-10-16T15:26:31Z",
+      "compiled_at": "2026-10-16T18:41:20Z",
       "compiler": "crumb-compile/1",
-      "digest": "sha256:5c3262fb97f4051b7780f820f039da497a16ac9d1cf8d077e593d3b2e786ba89",
+      "digest": "sha256:ac1e99fc11afe5487f84f75e29cabf2c798acb8eec3bc2a641c1a10438934305",
       "evidence_files": 1578,
       "evidence_root": "sha256:135e27a1a0704c4a8371cd2c1eb555f2b5beec61c212949b029a5457418a7160",
       "files": 5889,
-      "generated_at_commit": "228809d2f65ded6e8bbc011bc817209d82255bb5",
-      "source_tree": "sha256:7bdcd0cf99ffd24054c17de9287bf4506d8059c855d566eab42a2280024ec2a2"
+      "generated_at_commit": "06203f89f4185a67568c4bb414f4f281f442a6f3",
+      "source_tree": "sha256:179fe9f154ea7186adc871469bd1e5d0c934c45600e7a16aaf05ff1e91197bb4"
     }
   },
   "history": [],
diff --git a/crates/aien-kv-store/src/host_fs.rs b/crates/aien-kv-store/src/host_fs.rs
index c6d57fea..61ec7eb2 100644
--- a/crates/aien-kv-store/src/host_fs.rs
+++ b/crates/aien-kv-store/src/host_fs.rs
@@ -13,10 +13,16 @@
 //! ```text
 //! <root>/
 //!   STORE_ID
+//!   LOCK
 //!   objects/00/ .. objects/ff/
 //!   tmp/
 //! ```
 //!
+//! `LOCK` carries an exclusive advisory lock (`flock`) for the store's
+//! lifetime: one process per store folder (sc#521). A second opener is
+//! refused before recovery, which would otherwise unlink the first process's
+//! in-flight temps and keep a second, drifting index of the same folder.
+//!
 //! All 256 shard directories and `tmp/` are created at [`HostFsStore::open`]
 //! with a parent-directory `fsync`; the hot path never creates a directory.
 
@@ -548,6 +554,9 @@ struct Shared {
 pub struct HostFsStore {
     shared: Arc<Shared>,
     worker: Mutex<Option<JoinHandle<()>>>,
+    /// The open `LOCK` file whose `flock` makes this process the folder's
+    /// only opener; released when the store drops (after the worker joined).
+    _folder_lock: File,
 }
 
 static STORE_ID_COUNTER: AtomicU64 = AtomicU64::new(1);
@@ -702,6 +711,36 @@ fn create_private_file(path: &Path) -> std::io::Result<File> {
     Ok(f)
 }
 
+/// The lock file in the store root (sc#521).
+const LOCK_FILE: &str = "LOCK";
+
+/// Takes the folder's exclusive advisory lock: `root/LOCK` opened privately
+/// (no symlink followed, ours, 0600) and `flock(LOCK_EX | LOCK_NB)` on it.
+/// The lock belongs to the returned open file and ends when it closes. A
+/// folder held by another opener (another process, or a second store in
+/// this one: `flock` is per open file description) is refused with
+/// `TaskRejected` and a message that names the folder; any other failure is
+/// reported like every other store I/O error.
+fn take_folder_lock(root: &Path) -> Result<File, PlatformError> {
+    use std::os::unix::io::AsRawFd;
+    let path = root.join(LOCK_FILE);
+    let f = create_private_file(&path).map_err(|e| io_to_error(&e, "open LOCK"))?;
+    // SAFETY: `f` is an open descriptor this function owns; flock takes no
+    // memory and only sets the description's advisory lock.
+    let rc = unsafe { libc::flock(f.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
+    if rc == 0 {
+        return Ok(f);
+    }
+    let e = std::io::Error::last_os_error();
+    if e.raw_os_error() == Some(libc::EWOULDBLOCK) {
+        return Err(PlatformError::TaskRejected(format!(
+            "store folder {} is open in another process (LOCK held); one process per store folder",
+            root.display()
+        )));
+    }
+    Err(io_to_error(&e, "flock LOCK"))
+}
+
 /// `create_private_file` plus the bytes (fault injection, temps).
 fn write_private_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
     create_private_file(path)?.write_all(bytes)
@@ -3014,6 +3053,13 @@ impl HostFsStore {
             }
         }
 
+        // One process per store folder (sc#521), taken before recovery: the
+        // walk below unlinks every temp it finds, so a second opener would
+        // fail the first process's in-flight parks at their rename and keep
+        // its own drifting index and quota of the same folder.
+        let folder_lock = take_folder_lock(root)?;
+        stats.io_ops += 1;
+
         let alloc_unit = std::fs::metadata(&objects_dir)
             .map(|m| m.blksize())
             .unwrap_or(4096)
@@ -3145,6 +3191,7 @@ impl HostFsStore {
         Ok(Self {
             shared,
             worker: Mutex::new(handle),
+            _folder_lock: folder_lock,
         })
     }
 
diff --git a/crates/aien-kv-store/tests/host_fs_security_tests.rs b/crates/aien-kv-store/tests/host_fs_security_tests.rs
index 6a0659c3..b977198e 100644
--- a/crates/aien-kv-store/tests/host_fs_security_tests.rs
+++ b/crates/aien-kv-store/tests/host_fs_security_tests.rs
@@ -295,3 +295,45 @@ fn a_permissive_umask_does_not_widen_the_store() {
 fn a_narrow_umask_does_not_lock_the_store_out_of_itself() {
     run_under_umask(0o277);
 }
+
+// ---- one process per store folder (sc#521) --------------------------------
+
+/// `root/LOCK` is private like every other store file.
+#[test]
+fn the_lock_file_is_private_and_lives_in_the_root() {
+    let fx = Fx::new();
+    let _s = fx.open_with(|_| {});
+    let lock = fx.root().join("LOCK");
+    assert!(std::fs::symlink_metadata(&lock).unwrap().is_file());
+    assert_private(&lock);
+}
+
+/// A second opener of the same folder is refused with a message that names
+/// the folder, the first store keeps working, and the folder opens again
+/// once the first store is dropped. `flock` locks belong to the open file
+/// description, so a second `open` in this process is refused exactly as a
+/// second process would be.
+#[test]
+fn a_second_opener_of_the_same_folder_is_refused_until_the_first_closes() {
+    let fx = Fx::new();
+    let first = fx.open_with(|_| {});
+    assert_eq!(first.capacity().state, StoreState::Open);
+
+    let second = HostFsStore::open(fx.config());
+    match &second {
+        Err(PlatformError::TaskRejected(why)) => {
+            assert!(why.contains("another process"), "{why}");
+            assert!(why.contains(&fx.root().display().to_string()), "{why}");
+        }
+        Ok(_) => panic!("second opener must be refused, but it opened"),
+        Err(e) => panic!("second opener must be refused with TaskRejected, got {e:?}"),
+    }
+    // The refusal touched nothing the first store relies on.
+    assert_eq!(first.capacity().state, StoreState::Open);
+    assert!(fx.root().join("STORE_ID").is_file());
+
+    drop(second);
+    drop(first);
+    let again = fx.open_with(|_| {});
+    assert_eq!(again.capacity().state, StoreState::Open);
+}