The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): marker contract A1/A2/A3/A5 in the Python lock
Teknium90a9e26d425d
diff --git a/hermes_cli/update_lock.py b/hermes_cli/update_lock.pyindex 3bfde7e5ba2..6c6a3522c24 100644--- a/hermes_cli/update_lock.py+++ b/hermes_cli/update_lock.py@@ -21,8 +21,11 @@ Two artifacts, one authority each:from __future__ import annotations+import calendarimport loggingimport os+import re+import secretsimport subprocessimport sysimport time@@ -39,6 +42,10 @@ UPDATE_MARKER_MAX_AGE_SECONDS = 20 * 60# Clock skew allowed between a recorded and a probed process creation time (C1 rule 3).CREATE_TIME_TOLERANCE_SECONDS = 2.0+# A claim published by create-then-write (filesystems without hard links) is briefly empty; an+# empty marker this young is a claim in flight, not a dead one (contract A3).+EMPTY_MARKER_GRACE_SECONDS = 5.0+MARKER_NAME = ".hermes-update-in-progress"CHECKOUT_LOCK_NAME = ".hermes-update.lock"GIT_CHECKOUT_LOCK_NAME = "hermes-update.lock"@@ -168,7 +175,7 @@ def _stdlib_create_time(pid: int) -> float | None:"""Creation time in unix seconds without psutil (``-I -S`` children, early recovery).Same clock psutil reports: Linux ``starttime / CLK_TCK + btime``, macOS the kernel start- time (``ps -o lstart=``, second resolution — inside the 2 s tolerance), Windows the+ time (``ps -o lstart=`` in UTC, second resolution — inside the 2 s tolerance), Windows the``GetProcessTimes`` creation FILETIME."""try:@@ -182,14 +189,15 @@ def _stdlib_create_time(pid: int) -> float | None:with open("/proc/stat", "rb") as fh:btime = next(int(line.split()[1]) for line in fh if line.startswith(b"btime "))return btime + start_ticks / os.sysconf("SC_CLK_TCK")+ # UTC wall clock: a local-time lstart is ambiguous in the repeated DST hour.out = subprocess.run(["ps", "-o", "lstart=", "-p", str(pid)], capture_output=True, text=True,encoding="utf-8", errors="replace", timeout=5, stdin=subprocess.DEVNULL,- env={**os.environ, "LC_ALL": "C"},+ env={"PATH": os.environ.get("PATH") or "/bin:/usr/bin", "LC_ALL": "C", "TZ": "UTC0"},).stdout.strip()if not out:return None- return time.mktime(time.strptime(" ".join(out.split()), "%a %b %d %H:%M:%S %Y"))+ return float(calendar.timegm(time.strptime(" ".join(out.split()), "%a %b %d %H:%M:%S %Y")))except (OSError, ValueError, IndexError, StopIteration, subprocess.SubprocessError, AttributeError):return None@@ -212,14 +220,20 @@ def process_create_time(pid: int | None = None) -> float | None:return _stdlib_create_time(target)-def _identity_live(pid: int, create_time: float | None) -> bool:- """C1 rule 3 for one (pid, ct) identity: alive, not a zombie, and not a reused pid."""+def _identity_live(pid: int, create_time: float | None, age: float) -> bool:+ """C1 rule 3 + A1 for one (pid, ct) identity of a marker ``age`` seconds old.++ Alive (not a zombie) and, when a creation time was recorded, the same process: a matching+ creation time is live however old the marker is. Without that proof — a v1 marker, or a+ creation time we cannot read (Windows denies it for elevated/other-user pids) — the pid may+ be a reused one, so only the legacy age ceiling bounds it.+ """if not _pid_alive(pid):return False- if create_time is None:- return True- actual = process_create_time(pid)- return actual is None or abs(actual - create_time) <= CREATE_TIME_TOLERANCE_SECONDS+ actual = None if create_time is None else process_create_time(pid)+ if actual is None:+ return age <= UPDATE_MARKER_MAX_AGE_SECONDS+ return abs(actual - create_time) <= CREATE_TIME_TOLERANCE_SECONDSdef _identity_line(pid: int | None = None) -> str:@@ -408,10 +422,11 @@ class UpdateHolder:class _Marker:raw: bytespid: int- started_at: float | None+ started_at: int | Nonecreate_time: float | Nonedelegate_pid: int | Nonedelegate_create_time: float | None+ in_flight: bool = False # an empty marker younger than EMPTY_MARKER_GRACE_SECONDS@propertydef base(self) -> bytes:@@ -422,49 +437,42 @@ class _Marker:return time.time() - self.started_at if self.started_at is not None else float("inf")def owner_live(self) -> bool:- if self.create_time is None and self.age() > UPDATE_MARKER_MAX_AGE_SECONDS:- return False # v1 marker: pid-only, so only a ceiling can expose a reused pid- return _identity_live(self.pid, self.create_time)+ return self.started_at is not None and _identity_live(self.pid, self.create_time, self.age())def delegate_live(self) -> bool:- return self.delegate_pid is not None and _identity_live(self.delegate_pid, self.delegate_create_time)+ return self.delegate_pid is not None and self.started_at is not None \+ and _identity_live(self.delegate_pid, self.delegate_create_time, self.age())def live_pid(self) -> int | None:+ if self.in_flight:+ return 0if self.owner_live():return self.pidreturn self.delegate_pid if self.delegate_live() else None-def _parse_ct(text: str) -> float | None:- text = text.strip()- if not text.startswith("ct:"):- return None- try:- return float(text[3:])- except ValueError:- return None---def _parse_marker(raw: bytes) -> _Marker:- lines = raw.decode("utf-8-sig", errors="replace").splitlines()-- def field(index: int, cast):- try:- return cast(lines[index].strip())- except (IndexError, ValueError):- return None-- delegate_pid = delegate_ct = None- if len(lines) > 3 and lines[3].startswith("delegate:"):- head, _, tail = lines[3][len("delegate:"):].partition(" ")- with suppress(ValueError):- delegate_pid = int(head)- delegate_ct = _parse_ct(tail)- pid = field(0, int)+_INT_LINE = re.compile(r"[0-9]+", re.ASCII)+_CT_LINE = re.compile(r"ct:([0-9]+(?:\.[0-9]+)?)", re.ASCII)+_DELEGATE_LINE = re.compile(r"delegate:([0-9]+) ct:([0-9]+(?:\.[0-9]+)?)", re.ASCII)+++def _parse_marker(raw: bytes, *, mtime: float | None = None) -> _Marker:+ """Contract A2, positional and identical in every reader (Rust ``marker.rs``, Electron,+ the hand-off scripts): BOM and CRLF tolerated; line 1 pid and line 2 started_at are+ integers or the marker is MALFORMED (dead: ``started_at`` None); a bad line 3 makes it v1;+ a bad line 4 is ignored."""+ text = raw.decode("utf-8", errors="replace").removeprefix("\ufeff")+ lines = [line.removesuffix("\r").strip(" \t") for line in text.split("\n")]+ lines += [""] * (4 - len(lines))+ pid = int(lines[0]) if _INT_LINE.fullmatch(lines[0]) else -1+ started_at = int(lines[1]) if pid >= 0 and _INT_LINE.fullmatch(lines[1]) else None+ ct = _CT_LINE.fullmatch(lines[2])+ delegate = _DELEGATE_LINE.fullmatch(lines[3])+ in_flight = not raw and mtime is not None and time.time() - mtime < EMPTY_MARKER_GRACE_SECONDSreturn _Marker(- raw=raw, pid=pid if pid is not None else -1, started_at=field(1, float),- create_time=_parse_ct(lines[2]) if len(lines) > 2 else None,- delegate_pid=delegate_pid, delegate_create_time=delegate_ct,+ raw=raw, pid=pid, started_at=started_at, create_time=float(ct.group(1)) if ct else None,+ delegate_pid=int(delegate.group(1)) if delegate else None,+ delegate_create_time=float(delegate.group(2)) if delegate else None, in_flight=in_flight,)@@ -475,6 +483,36 @@ def _read_bytes(path: Path) -> bytes | None:return None+def _read_marker(path: Path) -> _Marker | None:+ raw = _read_bytes(path)+ if raw is None:+ return None+ mtime = None+ if not raw:+ with suppress(OSError):+ mtime = path.stat().st_mtime+ return _parse_marker(raw, mtime=mtime)+++def _tmp_sibling(path: Path) -> Path:+ return path.with_name(f"{path.name}.{os.getpid()}.{secrets.token_hex(4)}.tmp")+++def _sweep_dead_tmp_siblings(path: Path) -> None:+ """Reclaim ``<marker>.<pid>[.<token>].tmp`` files whose writer died between write and+ publish (contract m10): the pid is the first component after the marker name."""+ prefix = f"{path.name}."+ with suppress(OSError):+ for entry in path.parent.iterdir():+ name = entry.name+ if not (name.startswith(prefix) and name.endswith(".tmp")):+ continue+ owner = name[len(prefix):].split(".", 1)[0]+ if owner.isdigit() and int(owner) != os.getpid() and not _pid_alive(int(owner)):+ with suppress(OSError):+ entry.unlink()++def _compare_and_delete(path: Path, expected: bytes) -> bool:"""Delete ``path`` only while it still holds exactly ``expected`` (C1 rule 5)."""if _read_bytes(path) != expected:@@ -488,7 +526,7 @@ def _compare_and_swap(path: Path, expected: bytes, new: bytes) -> bool:"""Atomically replace ``path`` (tmp + ``os.replace``) only while it still holds ``expected``."""if _read_bytes(path) != expected:return False- tmp = path.with_name(f"{path.name}.{os.getpid()}.tmp")+ tmp = _tmp_sibling(path)try:with open(tmp, "wb") as fh:fh.write(new)@@ -507,12 +545,15 @@ def _compare_and_swap(path: Path, expected: bytes, new: bytes) -> bool:def _live_partners(marker: _Marker) -> list[int]:- """Live identities behind a marker: the owner, then the delegate.+ """Live identities behind a marker: the owner, then the delegate (``[0]`` for a claim+ still being written).A v1 marker naming our own pid is a killed update's claim whose pid this run inherited(containers restart pid numbering), not ours: without a creation time it cannot be toldapart, and nothing pre-writes a v1 marker for this process."""+ if marker.in_flight:+ return [0]partners = []if marker.owner_live() and not (marker.pid == os.getpid() and marker.create_time is None):partners.append(marker.pid)@@ -529,13 +570,12 @@ def read_live_update(*, path: Path | None = None) -> UpdateHolder | None:compare-and-delete so it can't strand future runs. Never raises."""marker = path or update_marker_path()- raw = _read_bytes(marker)- if raw is None:+ parsed = _read_marker(marker)+ if parsed is None:return None- parsed = _parse_marker(raw)live = parsed.live_pid()if live is None:- _compare_and_delete(marker, raw)+ _compare_and_delete(marker, parsed.raw)return Nonereturn UpdateHolder(pid=live, age_seconds=parsed.age() if parsed.started_at is not None else 0.0)@@ -551,7 +591,7 @@ def describe_holder(holder: UpdateHolder | None) -> str:)minutes, seconds = divmod(int(max(0 if holder is None else holder.age_seconds, 0)), 60)elapsed = f"{minutes}m {seconds}s" if minutes else f"{seconds}s"- who = f", process {holder.pid}" if holder else ""+ who = f", process {holder.pid}" if holder and holder.pid else ""return (f"✗ Another Hermes update is already running (started {elapsed} ago{who}).\n""\n"@@ -630,6 +670,23 @@ def _lock_holder(fd_or_path) -> UpdateHolder:return UpdateHolder(pid=max(parsed.pid, 0), age_seconds=parsed.age() if parsed.started_at else 0.0)+def _open_lock_file(path: Path) -> tuple[int | None, object]:+ """``(fd, True)`` read-write; ``(fd, False)`` read-only for an existing lock file we may not+ write (left root-owned by a ``sudo hermes update``: the kernel lock works on a read-only fd,+ contract A5); ``(None, reason)`` when neither opens."""+ binary = getattr(os, "O_BINARY", 0)+ try:+ return os.open(path, os.O_RDWR | os.O_CREAT | binary, 0o644), True+ except PermissionError as exc:+ denied = exc+ except OSError as exc:+ return None, exc.strerror or exc+ try:+ return os.open(path, os.O_RDONLY | binary), False+ except OSError:+ return None, denied.strerror or denied++def _acquire_checkout(install_root: Path) -> UpdateHolder | None:"""Take (or join, when inherited) the checkout lock; the refusal holder, else ``None``."""global _HELD@@ -641,19 +698,18 @@ def _acquire_checkout(install_root: Path) -> UpdateHolder | None:if inherited is not None:_HELD = {"path": str(path), "fd": inherited, "owned": False, "depth": 1}return None- try:- fd = os.open(path, os.O_RDWR | os.O_CREAT | getattr(os, "O_BINARY", 0), 0o644)- except OSError as exc:- return UpdateHolder(pid=0, age_seconds=0.0,- reason=f"{path} is not writable ({exc.strerror or exc})")+ fd, writable = _open_lock_file(path)+ if fd is None:+ return UpdateHolder(pid=0, age_seconds=0.0, reason=f"{path} is not writable ({writable})")try:if not _try_lock(fd):os.close(fd)return _lock_holder(path)- record = f"{os.getpid()}\n{int(time.time())}\n{_identity_line()}\n".encode()- os.lseek(fd, 0, os.SEEK_SET)- os.ftruncate(fd, 0)- os.write(fd, record)+ if writable is True:+ record = f"{os.getpid()}\n{int(time.time())}\n{_identity_line()}\n".encode()+ os.lseek(fd, 0, os.SEEK_SET)+ os.ftruncate(fd, 0)+ os.write(fd, record)except OSError as exc:_unlock(fd)os.close(fd)@@ -772,6 +828,39 @@ def update_in_progress(install_root: Path | str | None = None) -> bool:# --- the lock object ---------------------------------------------------------------------+def _publish_exclusive(path: Path, body: bytes) -> bool:+ """Contract A3: publish ``body`` at ``path`` only if nothing is there, never as an empty+ file a reader could judge dead. Write a private tmp sibling, then hard-link it into place+ (fails if the marker exists); a filesystem without hard links falls back to an exclusive+ create (readers grant a young empty marker EMPTY_MARKER_GRACE_SECONDS). False = taken."""+ tmp = _tmp_sibling(path)+ try:+ with open(tmp, "xb") as fh:+ fh.write(body)+ fh.flush()+ os.fsync(fh.fileno())+ try:+ os.link(tmp, path)+ return True+ except FileExistsError:+ return False+ except OSError as exc:+ logger.debug("No hard link for the update marker (%s); exclusive create instead", exc)+ finally:+ with suppress(OSError):+ tmp.unlink()+ try:+ fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0), 0o644)+ except FileExistsError:+ return False+ try:+ os.write(fd, body)+ os.fsync(fd)+ finally:+ os.close(fd)+ return True++class UpdateLock:"""Context manager owning the shared update marker (and, with ``install_root``, thecheckout lock) for this process.@@ -813,32 +902,27 @@ class UpdateLock:except OSError as exc:self.holder = UpdateHolder(pid=0, age_seconds=0.0, reason=f"{self.path.parent} is not writable ({exc})")return False+ _sweep_dead_tmp_siblings(self.path)body = f"{os.getpid()}\n{int(time.time())}\n{_identity_line()}\n".encode()for attempt in range(2):try:- fd = os.open(self.path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0), 0o644)- except FileExistsError:- raw = _read_bytes(self.path)- if raw is None:- continue # vanished between create and read: retry the create- existing = _parse_marker(raw)- if _live_partners(existing):- return self._adopt_or_refuse(existing)- if attempt == 0 and _compare_and_delete(self.path, raw):- continue- self.holder = UpdateHolder(pid=max(existing.pid, 0), age_seconds=0.0)- return False+ published = _publish_exclusive(self.path, body)except OSError as exc:self.holder = UpdateHolder(pid=0, age_seconds=0.0, reason=f"{self.path} is not writable ({exc})")return False- try:- os.write(fd, body)- os.fsync(fd)- finally:- os.close(fd)- self._written = body- self.acquired = True- return True+ if published:+ self._written = body+ self.acquired = True+ return True+ existing = _read_marker(self.path)+ if existing is None:+ continue # vanished between publish and read: retry+ if _live_partners(existing):+ return self._adopt_or_refuse(existing)+ if attempt == 0 and _compare_and_delete(self.path, existing.raw):+ continue+ self.holder = UpdateHolder(pid=max(existing.pid, 0), age_seconds=0.0)+ return Falseself.holder = read_live_update(path=self.path) or UpdateHolder(pid=0, age_seconds=0.0)return False@@ -846,14 +930,16 @@ class UpdateLock:"""C1 rule 4: a LIVE claim by us, an ancestor or the hand-off partner is run under."""partners = _live_partners(existing)if os.getpid() not in partners and not any(- p == _handoff_pid() or _is_ancestor_pid(p) for p in partners):+ p and (p == _handoff_pid() or _is_ancestor_pid(p)) for p in partners):self.holder = UpdateHolder(pid=partners[0], age_seconds=existing.age() if existing.started_at else 0.0)return False+ own = _identity_line()if os.getpid() not in partners and existing.delegate_pid not in partners \- and existing.create_time is not None:+ and existing.create_time is not None and own:# Rule 6: name ourselves as the delegate so the claim stays visible if the partner# (a hand-off script, the Tauri updater) dies while this update still runs.- delegated = existing.base + f"delegate:{os.getpid()} {_identity_line()}\n".encode()+ base = existing.base if existing.base.endswith(b"\n") else existing.base + b"\n"+ delegated = base + f"delegate:{os.getpid()} {own}\n".encode()if _compare_and_swap(self.path, existing.raw, delegated):self._written, self._delegate_base = delegated, existing.basereturn Truediff --git a/tests/hermes_cli/test_update_lock.py b/tests/hermes_cli/test_update_lock.pyindex 50130dc6751..38fb0e37b71 100644--- a/tests/hermes_cli/test_update_lock.py+++ b/tests/hermes_cli/test_update_lock.py@@ -252,16 +252,85 @@ def test_delegate_keeps_the_claim_live_after_the_partner_dies(marker, monkeypatc@pytest.mark.parametrize("body",- ["", "not-a-pid\n123\n", "\n\n", "12345"],- ids=["empty", "garbage-pid", "blank-lines", "no-start-time"],+ ["", "not-a-pid\n123\n", "\n\n", "12345", "{live}\n{now}.5\n", "1_0\n{now}\n", "{live}\nsoon\n"],+ ids=["empty", "garbage-pid", "blank-lines", "no-start-time", "fractional-start", "underscore-pid",+ "garbage-start"],)-def test_malformed_markers_never_block_an_update(marker, body):- marker.write_text(body, encoding="utf-8")+def test_malformed_markers_never_block_an_update(marker, body, other_pid):+ """Contract A2: line 1 and line 2 are integers or the marker is malformed, i.e. dead, in+ every reader (a fractional start time used to be live in Python and dead in the others)."""+ marker.write_text(body.format(live=other_pid, now=int(time.time())), encoding="utf-8")+ old = time.time() - 60+ os.utime(marker, (old, old)) # past the empty-file grace (A3)assert read_live_update(path=marker) is Noneassert UpdateLock(path=marker).acquire() is True+def test_marker_parse_follows_the_shared_contract(marker, other_pid):+ """Contract A2: BOM and CRLF are tolerated; a malformed ct line makes the marker v1 (so the+ age ceiling applies); only an exact line-4 delegate counts."""+ now, ct = int(time.time()), process_create_time(other_pid)+ live = {+ "bom": f"\ufeff{other_pid}\n{now}\nct:{ct:.3f}\n",+ "crlf": f"{other_pid}\r\n{now}\r\nct:{ct:.3f}\r\n",+ "bad-ct-fresh": f"{other_pid}\n{now}\nct:abc\n",+ }+ dead = {+ "bad-ct-aged": f"{other_pid}\n{now - UPDATE_MARKER_MAX_AGE_SECONDS - 300}\nct:abc\n",+ "spaced-delegate": f"{DEAD_PID}\n{now}\nct:1.000\ndelegate: {other_pid} ct:{ct:.3f}\n",+ "delegate-on-line-3": f"{DEAD_PID}\n{now}\ndelegate:{other_pid} ct:{ct:.3f}\n",+ }+ for name, body in {**live, **dead}.items():+ marker.write_text(body, encoding="utf-8", newline="")+ holder = read_live_update(path=marker)+ assert (holder is not None) == (name in live), name+++def test_fresh_empty_marker_is_a_claim_in_flight(marker):+ """Contract A3: a writer without hard links publishes create-then-write; an empty marker that+ young is someone's claim in progress, so it is neither deleted nor taken over."""+ marker.write_bytes(b"")++ holder = read_live_update(path=marker)+ lock = UpdateLock(path=marker)+ assert holder is not None and holder.pid == 0+ assert lock.acquire() is False and marker.read_bytes() == b""+++def test_claim_publishes_whole_and_reclaims_dead_writers_tmp_files(marker):+ """Contract A3 + litter: the claim lands complete (tmp + exclusive link) and leaves no tmp;+ a tmp left by a writer that died between write and publish is reclaimed, a live one is kept."""+ dead_tmp = marker.with_name(f"{marker.name}.{DEAD_PID}.tmp")+ mine = marker.with_name(f"{marker.name}.{os.getpid()}.ab12.tmp")+ dead_tmp.write_text("x")+ mine.write_text("x")++ lock = UpdateLock(path=marker)+ assert lock.acquire() is True+ assert marker.read_text(encoding="utf-8").startswith(f"{os.getpid()}\n")+ leftovers = sorted(p.name for p in marker.parent.iterdir() if p.name.endswith(".tmp"))+ assert leftovers == [mine.name]+ lock.release()+++def test_unreadable_creation_time_gets_the_v1_ceiling(marker, other_pid, monkeypatch):+ """Contract A1: a live pid whose creation time cannot be read (Windows denies it for+ elevated/other-user pids) is live only within the legacy ceiling: it may be a reused pid."""+ from hermes_cli import update_lock++ _claim_v2(marker, other_pid)+ fresh = marker.read_text(encoding="utf-8")+ monkeypatch.setattr(update_lock, "process_create_time", lambda pid=None: None)+ assert read_live_update(path=marker) is not None+ _claim_v2(marker, other_pid, started_at=time.time() - UPDATE_MARKER_MAX_AGE_SECONDS - 300)+ monkeypatch.undo()+ aged = marker.read_text(encoding="utf-8")+ assert read_live_update(path=marker) is not None, "a matching creation time is live at any age"+ monkeypatch.setattr(update_lock, "process_create_time", lambda pid=None: None)+ assert read_live_update(path=marker) is None and not marker.exists(), (fresh, aged)++def test_stale_marker_is_removed_on_read(marker):marker.write_text(f"{DEAD_PID}\n{int(time.time())}\n", encoding="utf-8")@@ -699,3 +768,27 @@ class TestAncestryUnderUnreadableProcesses:lock.release()assert marker.exists(), "the orchestrator still needs its marker"++[email protected]("posix")[email protected](hasattr(os, "geteuid") and os.geteuid() == 0, reason="root writes any file")+def test_unwritable_lock_file_still_locks_the_checkout(tmp_path):+ """Contract A5: a `sudo hermes update` leaves the lock file root-owned; later updates take the+ kernel lock on a read-only fd instead of refusing forever, and still exclude each other."""+ root = tmp_path / "install"+ root.mkdir()+ lock_file = checkout_lock_path(root)+ lock_file.write_text("0\n0\n")+ lock_file.chmod(0o444)++ first = UpdateLock(path=tmp_path / "a" / ".hermes-update-in-progress", install_root=root)+ assert first.acquire() is True, first.holder+ second = subprocess.run(+ [sys.executable, "-c",+ "import sys; from hermes_cli.update_lock import UpdateLock; "+ "sys.exit(0 if UpdateLock(path=__import__('pathlib').Path(sys.argv[1]), install_root=sys.argv[2]).acquire() else 3)",+ str(tmp_path / "b" / ".hermes-update-in-progress"), str(root)],+ cwd=REPO_ROOT, env={**os.environ, "PYTHONPATH": str(REPO_ROOT)}, stdin=subprocess.DEVNULL, timeout=60,+ )+ first.release()+ assert second.returncode == 3, "a second update ran while the read-only-locked checkout was held"