The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): the Tauri updater never reclaims a dead marker while the checkout lock is held (R6)
Teknium7d257fb619f0
diff --git a/apps/bootstrap-installer/src-tauri/Cargo.toml b/apps/bootstrap-installer/src-tauri/Cargo.tomlindex 6586140e652..a5ac70160be 100644--- a/apps/bootstrap-installer/src-tauri/Cargo.toml+++ b/apps/bootstrap-installer/src-tauri/Cargo.toml@@ -61,7 +61,9 @@ uuid = { version = "1", features = ["v4"] }[target.'cfg(windows)'.dependencies]windows-sys = { version = "0.59", features = ["Win32_Foundation",+ "Win32_Storage_FileSystem","Win32_System_Diagnostics_ToolHelp",+ "Win32_System_IO","Win32_System_Threading","Win32_System_Console","Win32_UI_WindowsAndMessaging",diff --git a/apps/bootstrap-installer/src-tauri/src/marker.rs b/apps/bootstrap-installer/src-tauri/src/marker.rsindex e972b517eec..51cdd5e1444 100644--- a/apps/bootstrap-installer/src-tauri/src/marker.rs+++ b/apps/bootstrap-installer/src-tauri/src/marker.rs@@ -81,6 +81,9 @@ const EMPTY_MARKER_GRACE: Duration = Duration::from_secs(5);pub(crate) struct MarkerOwner {pub(crate) pid: u32,pub(crate) age_secs: u64,+ /// R6: the marker's owner is dead but the install's checkout lock is still held (a process+ /// that update started is still mutating the checkout), so the marker is kept. pid is 0.+ pub(crate) held: bool,}/// Parsed marker body.@@ -273,12 +276,15 @@ fn marker_live_holder(record: &MarkerRecord, world: &World) -> Option<MarkerOwnereturn Some(MarkerOwner {pid: record.pid,age_secs,+ held: false,});}match record.delegate {- Some((pid, ct)) if identity_live(pid, Some(ct), age_secs, world) => {- Some(MarkerOwner { pid, age_secs })- }+ Some((pid, ct)) if identity_live(pid, Some(ct), age_secs, world) => Some(MarkerOwner {+ pid,+ age_secs,+ held: false,+ }),_ => None,}}@@ -448,7 +454,7 @@ fn replace_if_unchanged(path: &Path, expected: &[u8], body: &[u8]) -> std::io::R/// What is on disk at the marker path.enum MarkerState {Absent,- /// A live holder.+ /// A live holder, or a dead marker kept because its checkout lock is held (`held`, R6).Live(MarkerOwner),/// Dead / malformed / recycled / past the ceiling, compare-and-deleted./// Carries the error when the marker could not be read or removed (it@@ -460,7 +466,9 @@ enum MarkerState {/// (A7 rule 1), so the verdict and the delete are one critical section: a claim published by a/// lock-respecting process after our read can no longer be deleted on a stale verdict (R3)./// A 0-byte marker younger than `EMPTY_MARKER_GRACE` is a claim being written: live as pid 0.-fn inspect_marker_locked(path: &Path, world: &World) -> MarkerState {+/// R6: a dead marker is never deleted while `install_root`'s checkout lock is held; it is then+/// reported `held` (pid 0) — the update it names exited, but a process it started still runs.+fn inspect_marker_locked(path: &Path, install_root: &Path, world: &World) -> MarkerState {let raw = match std::fs::read(path) {Ok(raw) => raw,Err(err) if err.kind() == std::io::ErrorKind::NotFound => return MarkerState::Absent,@@ -473,12 +481,19 @@ fn inspect_marker_locked(path: &Path, world: &World) -> MarkerState {.map(|age| MarkerOwner {pid: 0,age_secs: age.as_secs(),+ held: false,})} else {parse_marker(&raw).and_then(|record| marker_live_holder(&record, world))};match live {Some(owner) => MarkerState::Live(owner),+ None if checkout_lock_held(install_root) => MarkerState::Live(MarkerOwner {+ pid: 0,+ age_secs: parse_marker(&raw)+ .map_or(0, |record| world.now.saturating_sub(record.started_at)),+ held: true,+ }),None => MarkerState::Dead(remove_if_unchanged(path, &raw).err()),}}@@ -496,11 +511,13 @@ fn empty_marker_age(path: &Path) -> Option<Duration> {}/// Read the marker (under its mutex) and report a live holder, if any; a dead marker is-/// removed. Test-only view of `inspect_marker_locked`.+/// removed. Test-only view of `inspect_marker_locked`; the install root is the marker's+/// `hermes-agent` sibling, as in production (`<HERMES_HOME>/hermes-agent`).#[cfg(test)]fn live_marker_owner(path: &Path) -> Option<MarkerOwner> {let _mutex = lock_marker(path).ok()?;- match inspect_marker_locked(path, &World::real()) {+ let install_root = path.with_file_name("hermes-agent");+ match inspect_marker_locked(path, &install_root, &World::real()) {MarkerState::Live(owner) => Some(owner),_ => None,}@@ -522,14 +539,149 @@ fn marker_owned_by_self(path: &Path) -> bool {/// The exit-2 heal decision (#75788), extracted so the contract is testable.///-/// True only when BOTH hold: the child exited with the concurrent-update-/// refusal code, AND the on-disk marker names THIS process. That combination-/// means the child refused over its own parent's claim — a stale checkout-/// without handoff recognition — so dropping the claim and retrying once is-/// safe. Any other owner (live foreign updater, garbage, missing marker) or-/// any other exit code must leave the refusal untouched.-pub(crate) fn should_heal_self_marker_refusal(exit_code: Option<i32>, marker_path: &Path) -> bool {- exit_code == Some(UPDATE_EXIT_CONCURRENT) && marker_owned_by_self(marker_path)+/// True only when ALL hold: the child exited with the concurrent-update+/// refusal code, the on-disk marker names THIS process, and nothing holds+/// `install_root`'s checkout lock. That combination means the child refused+/// over its own parent's claim — a stale checkout without handoff+/// recognition — so dropping the claim and retrying once is safe. Any other+/// owner (live foreign updater, garbage, missing marker), any other exit+/// code, or a held checkout lock (R6: another update's process is still+/// mutating the install, so the refusal is legitimate) must leave the+/// refusal untouched.+pub(crate) fn should_heal_self_marker_refusal(+ exit_code: Option<i32>,+ marker_path: &Path,+ install_root: &Path,+) -> bool {+ exit_code == Some(UPDATE_EXIT_CONCURRENT)+ && marker_owned_by_self(marker_path)+ && !checkout_lock_held(install_root)+}++/// `hermes_cli/update_lock.py::CHECKOUT_LOCK_NAME` (no checkout) / `GIT_CHECKOUT_LOCK_NAME`.+const CHECKOUT_LOCK_NAME: &str = ".hermes-update.lock";+const GIT_CHECKOUT_LOCK_NAME: &str = "hermes-update.lock";++/// `update_lock.py::_WINDOWS_LOCK_OFFSET`: the byte msvcrt locks, far past the holder record.+#[cfg(windows)]+const WINDOWS_LOCK_OFFSET: u32 = 1 << 20;++/// `update_lock.py::_git_common_dir`: the repository's common git dir, read from disk. `.git`+/// is the dir itself, or a `gitdir: <path>` file (linked worktree, submodule) whose target may+/// name the shared dir in `commondir`. `None` when `root` is no checkout.+fn git_common_dir(root: &Path) -> Option<PathBuf> {+ let read = |path: &Path| -> Option<String> {+ let text = std::fs::read_to_string(path).ok()?;+ Some(+ text.strip_prefix('\u{feff}')+ .unwrap_or(&text)+ .trim()+ .to_string(),+ )+ };+ let dot = root.join(".git");+ let mut gitdir = if dot.is_dir() {+ dot+ } else if dot.is_file() {+ // An absolute target replaces `root`.+ root.join(read(&dot)?.strip_prefix("gitdir:")?.trim())+ } else {+ return None;+ };+ let common = gitdir.join("commondir");+ if common.is_file() {+ gitdir = gitdir.join(read(&common)?);+ }+ Some(normalize_lexically(&gitdir))+}++/// `os.path.normpath`: drop `.` and fold `..` without touching the filesystem.+fn normalize_lexically(path: &Path) -> PathBuf {+ use std::path::Component;+ let mut out = PathBuf::new();+ for component in path.components() {+ match component {+ Component::CurDir => {}+ Component::ParentDir => match out.components().next_back() {+ Some(Component::Normal(_)) => {+ out.pop();+ }+ Some(Component::RootDir | Component::Prefix(_)) => {}+ _ => out.push(".."),+ },+ other => out.push(other.as_os_str()),+ }+ }+ out+}++/// `update_lock.py::checkout_lock_path`: the install's checkout kernel lock file.+fn checkout_lock_path(install_root: &Path) -> PathBuf {+ match git_common_dir(install_root) {+ Some(common) => common.join(GIT_CHECKOUT_LOCK_NAME),+ None => install_root.join(CHECKOUT_LOCK_NAME),+ }+}++/// `update_lock.py::checkout_lock_held`: true while some process holds the checkout kernel+/// lock. The probe takes the lock for one try on a fresh open and drops it; a missing file is+/// not held. This updater never takes the checkout lock itself, so it is never the holder.+#[cfg(unix)]+fn checkout_lock_held(install_root: &Path) -> bool {+ use std::os::unix::io::AsRawFd;++ let Ok(file) = std::fs::File::open(checkout_lock_path(install_root)) else {+ return false;+ };+ let fd = file.as_raw_fd();+ if unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) } == 0 {+ unsafe { libc::flock(fd, libc::LOCK_UN) };+ return false;+ }+ std::io::Error::last_os_error().raw_os_error() == Some(libc::EWOULDBLOCK)+}++/// Windows: msvcrt's `LK_NBLCK` is a `LockFile` byte-range lock on one byte at+/// `WINDOWS_LOCK_OFFSET`; `LockFileEx` on the same byte conflicts with it. As in `_try_lock`,+/// a lock that cannot be taken is held.+#[cfg(windows)]+fn checkout_lock_held(install_root: &Path) -> bool {+ use std::os::windows::fs::OpenOptionsExt;+ use std::os::windows::io::AsRawHandle;+ use windows_sys::Win32::Storage::FileSystem::{+ LockFileEx, UnlockFileEx, FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE,+ LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY,+ };+ use windows_sys::Win32::System::IO::OVERLAPPED;++ let Ok(file) = std::fs::OpenOptions::new()+ .read(true)+ .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)+ .open(checkout_lock_path(install_root))+ else {+ return false;+ };+ let handle = file.as_raw_handle();+ let at_offset = || unsafe {+ let mut overlapped: OVERLAPPED = std::mem::zeroed();+ overlapped.Anonymous.Anonymous.Offset = WINDOWS_LOCK_OFFSET;+ overlapped+ };+ unsafe {+ let mut overlapped = at_offset();+ let flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;+ if LockFileEx(handle, flags, 0, 1, 0, &mut overlapped) == 0 {+ return true;+ }+ let mut overlapped = at_offset();+ UnlockFileEx(handle, 0, 1, 0, &mut overlapped);+ }+ false+}++#[cfg(not(any(unix, windows)))]+fn checkout_lock_held(_install_root: &Path) -> bool {+ false}/// Process creation time as unix seconds, comparable with the `ct:` values@@ -815,9 +967,13 @@ impl UpdateMarkerGuard {/// claim re-published inside the same hold; a live foreign holder is `Busy`; a claim that is/// our exact incarnation is adopted.///+ /// R6: a dead marker is NOT removed while `install_root`'s checkout lock is held — a process+ /// the dead update started is still mutating the install — and the refusal is `Busy` with a+ /// `held` owner.+ ////// A marker that cannot be written at all is `Unwritable`: the update/// refuses rather than run unserialized against other updaters (m8).- pub(crate) fn acquire(path: PathBuf) -> Result<Self, AcquireError> {+ pub(crate) fn acquire(path: PathBuf, install_root: &Path) -> Result<Self, AcquireError> {let world = World::real();let mut body = format!("{}\n{}\n", world.pid, world.now);if let Some(ct) = world.ct {@@ -848,12 +1004,13 @@ impl UpdateMarkerGuard {return Err(AcquireError::Busy(MarkerOwner {pid: 0,age_secs: 0,+ held: false,}))}Err(err) => return Err(unwritable(&marker_mutex_path(&path), &err)),};let world = World::real();- match inspect_marker_locked(&path, &world) {+ match inspect_marker_locked(&path, install_root, &world) {MarkerState::Live(owner) if owner.pid == world.pid => return Ok(claimed(path)),MarkerState::Live(owner) => return Err(AcquireError::Busy(owner)),MarkerState::Dead(Some(err)) => return Err(unwritable(&path, &err)),@@ -872,6 +1029,7 @@ impl UpdateMarkerGuard {Err(AcquireError::Busy(MarkerOwner {pid: 0,age_secs: 0,+ held: false,}))}diff --git a/apps/bootstrap-installer/src-tauri/src/marker_tests.rs b/apps/bootstrap-installer/src-tauri/src/marker_tests.rsindex 2b4276d4599..d4ae1515f32 100644--- a/apps/bootstrap-installer/src-tauri/src/marker_tests.rs+++ b/apps/bootstrap-installer/src-tauri/src/marker_tests.rs@@ -9,7 +9,7 @@ fn update_marker_guard_writes_then_removes_on_drop() {let marker = dir.join(".hermes-update-in-progress");{- let _g = UpdateMarkerGuard::acquire(marker.clone())+ let _g = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("no live owner => acquire must succeed"));assert!(marker.exists(), "marker must exist while the guard is held");let body = std::fs::read_to_string(&marker).unwrap();@@ -45,7 +45,7 @@ fn update_marker_guard_drop_is_quiet_when_already_gone() {std::fs::create_dir_all(&dir).unwrap();let marker = dir.join(".hermes-update-in-progress");- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("no live owner => acquire must succeed"));// Simulate an external cleanup (e.g. the desktop pruned a marker it// judged stale) before our guard drops — Drop must not panic.@@ -100,7 +100,10 @@ fn acquire_refuses_while_a_live_updater_owns_the_marker() {.unwrap_or(0);std::fs::write(&marker, format!("{foreign_pid}\n{started_at}")).unwrap();- let owner = busy(UpdateMarkerGuard::acquire(marker.clone()));+ let owner = busy(UpdateMarkerGuard::acquire(+ marker.clone(),+ &install_root_of(&marker),+ ));assert_eq!(owner.pid, foreign_pid);// The refused guard must not delete the live owner's marker.@@ -124,7 +127,7 @@ fn own_pid_v1_prewrite_is_a_previous_incarnation_and_reclaimed() {let me = std::process::id();std::fs::write(&marker, format!("{me}\n{}", now_secs() - 2)).unwrap();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("a previous incarnation's claim never blocks us"));let record = parse_marker(&std::fs::read(&marker).unwrap()).unwrap();assert_eq!(record.pid, me);@@ -165,14 +168,19 @@ fn self_owned_marker_plus_exit_2_heals() {std::fs::write(&marker, v2_body(me, 123, ct_of(me))).unwrap();assert!(- should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT), &marker),+ should_heal_self_marker_refusal(+ Some(UPDATE_EXIT_CONCURRENT),+ &marker,+ &install_root_of(&marker)+ ),"a child refusing over OUR marker is the #75788 deadlock — must heal");// R12: our pid without our creation time is a previous incarnation's claim, not ours.std::fs::write(&marker, format!("{me}\n123\n")).unwrap();assert!(!should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT),- &marker+ &marker,+ &install_root_of(&marker)));let _ = std::fs::remove_dir_all(&dir);}@@ -186,7 +194,11 @@ fn foreign_owned_marker_never_heals() {std::fs::write(&marker, format!("{}\n123\n", foreign.id())).unwrap();assert!(- !should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT), &marker),+ !should_heal_self_marker_refusal(+ Some(UPDATE_EXIT_CONCURRENT),+ &marker,+ &install_root_of(&marker)+ ),"a foreign owner is a REAL concurrent update — the refusal must stand");let _ = foreign.kill();@@ -199,14 +211,22 @@ fn missing_or_garbage_marker_never_heals() {let dir = unique_tmp_dir("heal-garbage");let missing = dir.join("never-written");assert!(- !should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT), &missing),+ !should_heal_self_marker_refusal(+ Some(UPDATE_EXIT_CONCURRENT),+ &missing,+ &install_root_of(&missing)+ ),"no marker on disk = the child refused over something else entirely");let garbage = dir.join(".hermes-update-in-progress");std::fs::write(&garbage, "not-a-pid\n123\n").unwrap();assert!(- !should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT), &garbage),+ !should_heal_self_marker_refusal(+ Some(UPDATE_EXIT_CONCURRENT),+ &garbage,+ &install_root_of(&garbage)+ ),"an unparseable marker must not be treated as ours");let _ = std::fs::remove_dir_all(&dir);@@ -221,7 +241,7 @@ fn non_exit_2_outcomes_never_heal() {for code in [Some(0), Some(1), Some(3), None] {assert!(- !should_heal_self_marker_refusal(code, &marker),+ !should_heal_self_marker_refusal(code, &marker, &install_root_of(&marker)),"heal is exit-2-only; exit {code:?} must keep its normal path");}@@ -238,7 +258,7 @@ fn heal_end_to_end_marker_lifecycle() {let dir = unique_tmp_dir("heal-e2e");let marker = dir.join(".hermes-update-in-progress");- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("no live owner => acquire must succeed"));assert!(marker.exists(),@@ -248,7 +268,8 @@ fn heal_end_to_end_marker_lifecycle() {// Stale child refused over our claim:assert!(should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT),- &marker+ &marker,+ &install_root_of(&marker)));// The heal drops the claim exactly as run_update does:@@ -262,7 +283,8 @@ fn heal_end_to_end_marker_lifecycle() {// own exit 2, e.g. a genuinely still-running Hermes, stays terminal).assert!(!should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT),- &marker+ &marker,+ &install_root_of(&marker)));let _ = std::fs::remove_dir_all(&dir);}@@ -282,7 +304,7 @@ fn acquire_reclaims_a_marker_owned_by_a_dead_pid() {.unwrap_or(0);std::fs::write(&marker, format!("4294967294\n{started_at}")).unwrap();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("a dead owner must not block acquisition"));let body = std::fs::read_to_string(&marker).unwrap();assert_eq!(@@ -311,7 +333,7 @@ fn acquire_reclaims_a_marker_past_the_age_ceiling() {.saturating_sub(UPDATE_MARKER_MAX_AGE_SECS + 60);std::fs::write(&marker, format!("{}\n{long_ago}", std::process::id())).unwrap();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("a marker past the ceiling must be reclaimable"));drop(guard);let _ = std::fs::remove_dir_all(&dir);@@ -533,7 +555,7 @@ fn completed_update_releases_marker_before_guard_drop() {std::fs::create_dir_all(&dir).unwrap();let marker = dir.join(".hermes-update-in-progress");- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("no live owner => acquire must succeed"));guard.complete();@@ -575,7 +597,10 @@ fn v2_live_owner_is_never_aged_out() {std::fs::write(&marker, &body).unwrap();// a live v2 owner must not be reclaimed by age- let owner = busy(UpdateMarkerGuard::acquire(marker.clone()));+ let owner = busy(UpdateMarkerGuard::acquire(+ marker.clone(),+ &install_root_of(&marker),+ ));assert_eq!(owner.pid, foreign.id());assert_eq!(std::fs::read_to_string(&marker).unwrap(), body);let _ = foreign.kill();@@ -592,7 +617,7 @@ fn v2_marker_with_mismatched_creation_time_is_reclaimed() {let me = std::process::id();std::fs::write(&marker, v2_body(me, now_secs(), ct_of(me) + 100.0)).unwrap();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("a recycled-pid marker must be reclaimable"));let body = std::fs::read_to_string(&marker).unwrap();let record = parse_marker(body.as_bytes()).expect("reclaimed marker must parse");@@ -612,7 +637,8 @@ fn marker_claim_child_helper() {let Some(path) = std::env::var_os("HERMES_TEST_MARKER_CLAIM_PATH") else {return;};- let code = match UpdateMarkerGuard::acquire(PathBuf::from(path)) {+ let path = PathBuf::from(path);+ let code = match UpdateMarkerGuard::acquire(path.clone(), &install_root_of(&path)) {Ok(guard) if guard.claimed => {// Leave the claim on disk, as a still-running owner would.std::mem::forget(guard);@@ -648,7 +674,7 @@ fn second_claimant_process_is_refused_by_exclusive_publish() {// racing a fresh claim is refused and never truncates our bytes.let dir = unique_tmp_dir("marker-exclusive");let marker = dir.join(".hermes-update-in-progress");- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("fresh acquire must succeed"));let ours = std::fs::read(&marker).unwrap();@@ -691,7 +717,7 @@ fn compare_and_delete_spares_changed_bytes() {// The guard's release is compare-and-delete too: a marker whose// identity lines are no longer ours is someone else's claim.let mut foreign = spawn_foreign_holder();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("fresh acquire must succeed"));let theirs = v2_body(foreign.id(), now_secs(), ct_of(foreign.id()));std::fs::write(&marker, &theirs).unwrap();@@ -715,14 +741,14 @@ fn live_delegate_keeps_a_dead_owner_marker_live() {let owner = live_marker_owner(&marker).expect("live delegate keeps the marker live");assert_eq!(owner.pid, foreign.id());- assert!(UpdateMarkerGuard::acquire(marker.clone()).is_err());+ assert!(UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).is_err());assert_eq!(std::fs::read_to_string(&marker).unwrap(), body);std::fs::remove_file(&marker).unwrap();// A7 rule 5: our release with a LIVE delegate hands the claim to it (rewritten with the// delegate as owner, started_at and run line kept); a dead delegate's line never strands// our claim.- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("fresh acquire must succeed"));let mut ours = std::fs::read_to_string(&marker).unwrap();let started = ours.lines().nth(1).unwrap().to_string();@@ -748,7 +774,7 @@ fn live_delegate_keeps_a_dead_owner_marker_live() {let _ = foreign.wait();std::fs::remove_file(&marker).unwrap();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("fresh acquire must succeed"));let mut ours = std::fs::read_to_string(&marker).unwrap();ours.push_str(&delegate); // the delegate is dead now@@ -1000,7 +1026,14 @@ fn fresh_empty_marker_is_a_claim_in_flight() {let dir = unique_tmp_dir("marker-empty-fresh");let marker = dir.join(".hermes-update-in-progress");std::fs::write(&marker, "").unwrap();- assert_eq!(busy(UpdateMarkerGuard::acquire(marker.clone())).pid, 0);+ assert_eq!(+ busy(UpdateMarkerGuard::acquire(+ marker.clone(),+ &install_root_of(&marker)+ ))+ .pid,+ 0+ );assert_eq!(std::fs::read(&marker).unwrap(),b"",@@ -1017,7 +1050,7 @@ fn old_empty_marker_is_reclaimed_and_claim_leaves_no_tmp() {file.set_modified(SystemTime::now() - Duration::from_secs(10)).unwrap();drop(file);- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("a 10 s old empty marker is dead"));let body = std::fs::read_to_string(&marker).unwrap();assert!(body.starts_with(&format!("{}\n", std::process::id())));@@ -1043,7 +1076,7 @@ fn own_exact_v2_claim_is_adopted_verbatim_and_released() {let marker = dir.join(".hermes-update-in-progress");let body = v2_body(me, now_secs() - 25 * 60, ct_of(me));std::fs::write(&marker, &body).unwrap();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("own v2 marker must be adopted"));assert_eq!(std::fs::read_to_string(&marker).unwrap(), body);drop(guard);@@ -1068,7 +1101,10 @@ fn marker_under_a_regular_file_is_unwritable() {let not_a_dir = dir.join("home");std::fs::write(¬_a_dir, "x").unwrap();let marker = not_a_dir.join(".hermes-update-in-progress");- let msg = unwritable_message(UpdateMarkerGuard::acquire(marker.clone()));+ let msg = unwritable_message(UpdateMarkerGuard::acquire(+ marker.clone(),+ &install_root_of(&marker),+ ));assert!(msg.starts_with(&format!("Cannot lock this install for the update: {} is not writable (",marker.display()@@ -1092,7 +1128,7 @@ fn marker_in_a_read_only_dir_is_unwritable() {std::fs::write(&marker, body).unwrap();}std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o555)).unwrap();- let result = UpdateMarkerGuard::acquire(marker.clone());+ let result = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker));std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap();assert!(unwritable_message(result).contains("is not writable"),@@ -1124,7 +1160,7 @@ fn claim_sweeps_dead_claimants_tmp_litter() {{std::fs::write(dir.join(name), "").unwrap();}- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("fresh acquire must succeed"));for name in dead {assert!(!dir.join(name).exists(), "{name} belongs to a dead pid");@@ -1145,6 +1181,11 @@ fn busy(result: Result<UpdateMarkerGuard, AcquireError>) -> MarkerOwner {}}+/// The install root a marker guards, laid out as in production: `<HERMES_HOME>/hermes-agent`.+fn install_root_of(marker: &Path) -> PathBuf {+ marker.with_file_name("hermes-agent")+}+fn unique_tmp_dir(tag: &str) -> PathBuf {let base = std::env::temp_dir().join(format!("hermes-marker-test-{tag}-{}-{}",@@ -1158,6 +1199,182 @@ fn unique_tmp_dir(tag: &str) -> PathBuf {base}+// ---- R6: a dead marker is never reclaimed while the checkout lock is held ----++/// Take `lock`'s checkout kernel lock on its OWN open file description, as a process an+/// earlier `hermes update` started would; held until the returned file is dropped.+fn hold_checkout_lock(lock: &Path) -> std::fs::File {+ std::fs::create_dir_all(lock.parent().unwrap()).unwrap();+ let file = std::fs::OpenOptions::new()+ .read(true)+ .write(true)+ .create(true)+ .truncate(false)+ .open(lock)+ .unwrap();+ #[cfg(unix)]+ {+ use std::os::unix::io::AsRawFd;+ let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };+ assert_eq!(rc, 0, "the test takes the free checkout lock");+ }+ #[cfg(windows)]+ {+ use std::os::windows::io::AsRawHandle;+ use windows_sys::Win32::Storage::FileSystem::{+ LockFileEx, LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY,+ };+ use windows_sys::Win32::System::IO::OVERLAPPED;+ let ok = unsafe {+ let mut overlapped: OVERLAPPED = std::mem::zeroed();+ overlapped.Anonymous.Anonymous.Offset = WINDOWS_LOCK_OFFSET;+ let flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;+ LockFileEx(file.as_raw_handle(), flags, 0, 1, 0, &mut overlapped)+ };+ assert_ne!(ok, 0, "the test takes the free checkout lock");+ }+ file+}++/// Drop `holder` and wait until the checkout lock reads free. A process another test forks+/// in parallel shares the holder's open file description (and so the flock) until its exec+/// closes it; a probe that itself kept the lock would never read free.+fn release_checkout_lock(holder: std::fs::File, install: &Path) {+ drop(holder);+ let deadline = std::time::Instant::now() + Duration::from_secs(5);+ while checkout_lock_held(install) {+ assert!(+ std::time::Instant::now() < deadline,+ "the checkout lock never read free after its holder closed"+ );+ std::thread::sleep(Duration::from_millis(10));+ }+}++#[test]+fn checkout_lock_path_follows_the_git_common_dir() {+ // update_lock.py::checkout_lock_path: <common git dir>/hermes-update.lock, else+ // <root>/.hermes-update.lock.+ let dir = unique_tmp_dir("checkout-lock-path");+ let plain = dir.join("plain");+ std::fs::create_dir_all(&plain).unwrap();+ assert_eq!(+ checkout_lock_path(&plain),+ plain.join(".hermes-update.lock")+ );++ let repo = dir.join("repo");+ std::fs::create_dir_all(repo.join(".git").join("worktrees").join("wt")).unwrap();+ assert_eq!(+ checkout_lock_path(&repo),+ repo.join(".git").join("hermes-update.lock")+ );++ // A linked worktree: `.git` is a `gitdir:` file whose target names the shared dir.+ let worktree = dir.join("wt");+ std::fs::create_dir_all(&worktree).unwrap();+ std::fs::write(worktree.join(".git"), "gitdir: ../repo/.git/worktrees/wt\n").unwrap();+ std::fs::write(+ repo.join(".git")+ .join("worktrees")+ .join("wt")+ .join("commondir"),+ "../..\n",+ )+ .unwrap();+ assert_eq!(+ checkout_lock_path(&worktree),+ repo.join(".git").join("hermes-update.lock")+ );+ let _ = std::fs::remove_dir_all(&dir);+}++#[test]+fn checkout_lock_held_sees_another_open_file_holding_it() {+ let dir = unique_tmp_dir("checkout-lock-held");+ let install = dir.join("hermes-agent");+ std::fs::create_dir_all(install.join(".git")).unwrap();+ assert!(+ !checkout_lock_held(&install),+ "a missing lock file is not held"+ );+ let holder = hold_checkout_lock(&checkout_lock_path(&install));+ assert!(checkout_lock_held(&install));+ assert!(+ checkout_lock_held(&install),+ "a probe of a held lock leaves it held"+ );+ // The probe never keeps the lock: it reads free once the holder closes.+ release_checkout_lock(holder, &install);+ let _ = std::fs::remove_dir_all(&dir);+}++#[test]+fn dead_marker_is_never_reclaimed_while_the_checkout_lock_is_held() {+ // R6: the marker's owner is dead, but a process its update started still holds the+ // checkout lock (and still mutates the install). Neither a claimer nor a reader may+ // delete the marker then: the claim is refused as `held` and the bytes stay on disk.+ let dir = unique_tmp_dir("marker-dead-held");+ let marker = dir.join(".hermes-update-in-progress");+ let install = install_root_of(&marker);+ std::fs::create_dir_all(install.join(".git")).unwrap();+ let body = format!("2147483647\n{}\n", now_secs() - 90);+ std::fs::write(&marker, &body).unwrap();+ let holder = hold_checkout_lock(&install.join(".git").join("hermes-update.lock"));++ let owner = busy(UpdateMarkerGuard::acquire(marker.clone(), &install));+ assert!(owner.held, "the refusal names the held checkout");+ assert_eq!(owner.pid, 0);+ assert!(owner.age_secs >= 90, "aged by the dead marker's started_at");+ assert_eq!(+ std::fs::read_to_string(&marker).unwrap(),+ body,+ "a claimer never deletes a dead marker while the checkout lock is held"+ );+ let owner = live_marker_owner(&marker).expect("a reader reports the held checkout");+ assert!(owner.held);+ assert_eq!(+ std::fs::read_to_string(&marker).unwrap(),+ body,+ "a reader never deletes a dead marker while the checkout lock is held"+ );++ release_checkout_lock(holder, &install);+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install)+ .unwrap_or_else(|_| panic!("once the lock is free the dead marker is reclaimed"));+ assert_eq!(+ parse_marker(&std::fs::read(&marker).unwrap()).unwrap().pid,+ std::process::id()+ );+ drop(guard);+ assert!(!marker.exists());+ let _ = std::fs::remove_dir_all(&dir);+}++#[test]+fn self_owned_marker_never_heals_while_the_checkout_lock_is_held() {+ // An exit 2 over our own claim while some process holds the checkout lock is a real+ // concurrent update (R6): the refusal stands. The plain (no `.git`) lock path is used.+ let dir = unique_tmp_dir("heal-held");+ let marker = dir.join(".hermes-update-in-progress");+ let install = install_root_of(&marker);+ let me = std::process::id();+ std::fs::write(&marker, v2_body(me, 123, ct_of(me))).unwrap();+ let holder = hold_checkout_lock(&install.join(".hermes-update.lock"));++ assert!(+ !should_heal_self_marker_refusal(Some(UPDATE_EXIT_CONCURRENT), &marker, &install),+ "a refusal while the checkout lock is held is legitimate — never heal it"+ );+ release_checkout_lock(holder, &install);+ assert!(should_heal_self_marker_refusal(+ Some(UPDATE_EXIT_CONCURRENT),+ &marker,+ &install+ ));+ let _ = std::fs::remove_dir_all(&dir);+}+// ---- A7: marker mutex (rule 1) and the shared corpus (rule 7) ----/// Child half of `marker_mutations_wait_for_the_mutex_another_process_holds`: holds the@@ -1205,7 +1422,7 @@ fn marker_mutations_wait_for_the_mutex_another_process_holds() {std::thread::sleep(Duration::from_millis(10));}let started = std::time::Instant::now();- let guard = UpdateMarkerGuard::acquire(marker.clone())+ let guard = UpdateMarkerGuard::acquire(marker.clone(), &install_root_of(&marker)).unwrap_or_else(|_| panic!("the dead marker is reclaimed once the mutex is free"));assert!(started.elapsed() >= Duration::from_millis(900),diff --git a/apps/bootstrap-installer/src-tauri/src/update.rs b/apps/bootstrap-installer/src-tauri/src/update.rsindex 9a1cc3042e6..2606b60f584 100644--- a/apps/bootstrap-installer/src-tauri/src/update.rs+++ b/apps/bootstrap-installer/src-tauri/src/update.rs@@ -89,13 +89,11 @@ pub async fn start_update(app: AppHandle) -> Result<(), String> {}/// The refusal shown when a live update already holds the marker. pid 0 is-/// a claim still being published (see `marker::inspect_marker`).+/// a claim still being published (see `marker::inspect_marker_locked`), or,+/// with `held`, a dead update whose process still holds the checkout (R6).fn busy_update_message(owner: &MarkerOwner) -> String {let wait = "Wait for it to finish, or close the window or dashboard tab that \started it, then try again.";- if owner.pid == 0 {- return format!("Another Hermes update is starting right now. {wait}");- }let mins = owner.age_secs / 60;let secs = owner.age_secs % 60;let elapsed = if mins > 0 {@@ -103,6 +101,15 @@ fn busy_update_message(owner: &MarkerOwner) -> String {} else {format!("{secs}s")};+ if owner.held {+ return format!(+ "Another Hermes update is already running (started {elapsed} ago; its owner \+ exited but a process it started still holds the checkout). {wait}"+ );+ }+ if owner.pid == 0 {+ return format!("Another Hermes update is starting right now. {wait}");+ }format!("Another Hermes update is already running (PID {}, started {elapsed} ago). {wait}",owner.pid@@ -124,8 +131,10 @@ async fn run_update(app: AppHandle) -> Result<()> {// update_lock.py claims it too), so a live foreign owner means another// updater — most often a dashboard-spawned `hermes update` — is already// mutating this checkout. Refuse instead of running a second one over it.- let _update_marker = match UpdateMarkerGuard::acquire(crate::paths::update_in_progress_marker())- {+ let _update_marker = match UpdateMarkerGuard::acquire(+ crate::paths::update_in_progress_marker(),+ &install_root,+ ) {Ok(guard) => guard,Err(err) => {let msg = match err {@@ -273,6 +282,7 @@ async fn run_update(app: AppHandle) -> Result<()> {if legacy_install && should_heal_self_marker_refusal(update.exit_code,&crate::paths::update_in_progress_marker(),+ &install_root,) {emit_log(&app,