Skip to content

The agent that grows with you

fix(update): a whole-second marker creation time in our own second is still us

etherneta28a5d03a9fa
diff --git a/hermes_cli/update_lock.py b/hermes_cli/update_lock.py
index 725e2ab13ae..cfc438e73c7 100644
--- a/hermes_cli/update_lock.py
+++ b/hermes_cli/update_lock.py
@@ -47,6 +47,9 @@ CREATE_TIME_TOLERANCE_SECONDS = 2.0
# Our own creation time re-probed by the same clock: only the marker's 3-decimal rounding differs,
# while two processes are at least one scheduler tick (10 ms) apart.
_OWN_CREATE_TIME_EPSILON = 0.005
+# macOS shells read a creation time from `ps -o lstart` and write it as whole seconds (`ct:N.000`),
+# truncated. A whole-second claim inside the second we started in is still our incarnation.
+_WHOLE_SECOND_CT = 1.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).
@@ -295,7 +298,11 @@ def _incarnation(pid: int, recorded: float | None, w: _World) -> bool | None:
if pid == w.pid: # we are alive by definition: only the incarnation is in question
if w.ct is None:
return recorded is None
- return recorded is not None and abs(w.ct - recorded) <= _OWN_CREATE_TIME_EPSILON
+ if recorded is None:
+ return False
+ if abs(w.ct - recorded) <= _OWN_CREATE_TIME_EPSILON:
+ return True
+ return recorded.is_integer() and 0 <= w.ct - recorded < _WHOLE_SECOND_CT
if not w.alive(pid):
return False
actual = None if recorded is None else w.ct_of(pid)
diff --git a/tests/hermes_cli/test_update_lock.py b/tests/hermes_cli/test_update_lock.py
index c9a8d449840..f3df6eb1057 100644
--- a/tests/hermes_cli/test_update_lock.py
+++ b/tests/hermes_cli/test_update_lock.py
@@ -25,6 +25,7 @@ from pathlib import Path
import pytest
+from hermes_cli import update_lock
from hermes_cli.update_lock import (
HANDOFF_PID_ENV,
UPDATE_MARKER_MAX_AGE_SECONDS,
@@ -979,3 +980,23 @@ def test_checkout_lock_never_writes_through_a_link_to_a_file_outside_the_install
assert lock.acquire() is False
assert lock.holder is not None and lock.holder.reason
assert outside.read_bytes() == b"PRECIOUS USER DATA"
+
+
+def test_whole_second_creation_time_in_our_own_second_is_us(marker):
+ """The macOS hand-off shell writes `ct:<secs>.000` (ps lstart has no sub-second part) for the
+ update child it names as delegate. That is our incarnation, so the child adopts the claim
+ instead of refusing its own hand-off as "another update"."""
+ import math
+
+ own = process_create_time()
+ assert update_lock.incarnation_live(os.getpid(), float(math.floor(own))) is True
+
+ parent = os.getppid()
+ marker.write_text(
+ f"{parent}\n{int(time.time())}\nct:{math.floor(process_create_time(parent))}.000\n"
+ f"delegate:{os.getpid()} ct:{math.floor(own)}.000\n",
+ encoding="utf-8", newline="",
+ )
+ assert update_lock.judge_marker(marker.read_bytes())[0] == "ours"
+ # A whole second from an EARLIER second is a killed update's pid reused: still not us.
+ assert update_lock.incarnation_live(os.getpid(), float(math.floor(own)) - 1) is False