The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(left-core): kept notices survive a racing append and are kept once
Teknium4aee230d0dc0
diff --git a/hermes_cli/left_core_migration.py b/hermes_cli/left_core_migration.pyindex a2e7e506db6..b201b355006 100644--- a/hermes_cli/left_core_migration.py+++ b/hermes_cli/left_core_migration.py@@ -716,41 +716,53 @@ def _notice_file(home: Path) -> Path:return home / "cache" / "left-core-notices.jsonl"+@contextmanager+def _notices_locked(home: Path):+ """The cross-process lock every append and claim of *home*'s kept notices holds: an append never lands+ in a file a claimant already read, and a claimant never reads a half-written line."""+ import threading+ from hermes_cli.auth import _file_lock+ path = _notice_file(home)+ # A fresh holder: the helper's per-thread reentrancy must never skip one home's lock inside another's.+ with _file_lock(path.with_name(f"{path.name}.lock"), threading.local(), 5.0,+ f"Timed out waiting for the left-core notice lock ({path})"):+ yield path++def _queue_notice(home: Path, message: str) -> None:"""Keep *message* for *home*'s first agent in ANY process (:func:`_claim_notices`): the process that- produced it (a CLI resolving the provider, a gateway start) often runs no agent for that home itself."""+ produced it (a CLI resolving the provider, a gateway start) often runs no agent for that home itself.+ A line already kept is not kept again: every start with ``allow_lazy_installs`` off reports the same row."""import json- path = _notice_file(home)+ line = json.dumps(message)try:- path.parent.mkdir(parents=True, exist_ok=True)- with open(path, "a", encoding="utf-8") as fh: # one short append per line: concurrent writers interleave- fh.write(json.dumps(message) + "\n")- except OSError as exc:+ with _notices_locked(home) as path:+ if path.exists() and line in path.read_text(encoding="utf-8-sig").splitlines():+ return+ with open(path, "a", encoding="utf-8") as fh:+ fh.write(line + "\n")+ except OSError as exc: # unwritable home or lock timeout: the outcome is still loggedlogger.debug("left-core notice not kept for %s: %s", home, exc)def _claim_notices(home: Path) -> list[str]:- """Take every notice kept for *home*, exactly once across processes: the rename is the claim, so of- several agents starting at once only one gets (and delivers) them."""+ """Take every notice kept for *home*, exactly once across processes: read and removed under the notice+ lock, so of several agents starting at once only one gets (and delivers) them. A crash before the+ removal leaves them for the next claimant rather than losing them."""import json- import threading- path = _notice_file(home)- claimed = path.with_name(f"{path.name}.{os.getpid()}.{threading.get_ident()}")- try:- os.replace(path, claimed)- except OSError: # nothing kept, or another process claimed it first+ if not _notice_file(home).exists(): # the common case: no lock takenreturn []try:- lines = claimed.read_text(encoding="utf-8-sig").splitlines()- except OSError:- lines = []- finally:- claimed.unlink(missing_ok=True)+ with _notices_locked(home) as path:+ lines = path.read_text(encoding="utf-8-sig").splitlines()+ path.unlink()+ except OSError: # another claimant took them first, or the lock timed out (they stay kept)+ return []messages = []- for line in lines:+ for line in dict.fromkeys(lines):try:messages.append(str(json.loads(line)))- except ValueError: # a torn write: skip that line only+ except ValueError: # a line from outside Hermes: skip it onlycontinuereturn messages@@ -1092,10 +1104,15 @@ def recover_at_startup(*, say: Optional[Callable[[str], None]] = None) -> list[shome = Path(get_hermes_home())key = hermes_home_key(home)+ delivered: set[str] = set()+def deliver(message: str) -> None:if say is None:_queue_notice(home, message)return+ if message in delivered: # a kept copy of what this attempt reports again (allow_lazy_installs off)+ return+ delivered.add(message)try:say(message)except Exception:diff --git a/tests/hermes_cli/test_left_core_provider_runtime.py b/tests/hermes_cli/test_left_core_provider_runtime.pyindex 64aa8b69654..bbc4c3bec0a 100644--- a/tests/hermes_cli/test_left_core_provider_runtime.py+++ b/tests/hermes_cli/test_left_core_provider_runtime.py@@ -118,6 +118,46 @@ def test_doctor_installs_nothing_and_names_the_install(tmp_path, monkeypatch, inassert "hermes plugins install github-copilot" in capsys.readouterr().out+def test_an_append_racing_a_claim_is_delivered_not_lost(tmp_path, monkeypatch):+ """A writer that opened the notice file before a claimant took it: its line still reaches a claimant."""+ import builtins+ import threading+ home = _copilot_home(tmp_path, monkeypatch)+ lcm._queue_notice(home, "first")+ opened, go = threading.Event(), threading.Event()++ def held_open(*args, **kwargs): # the writer has its handle and stalls before writing+ handle = builtins.open(*args, **kwargs)+ opened.set()+ go.wait(10)+ return handle+ monkeypatch.setattr(lcm, "open", held_open, raising=False)+ writer = threading.Thread(target=lcm._queue_notice, args=(home, "second"))+ writer.start()+ assert opened.wait(10)+ claimed: list[str] = []+ claimant = threading.Thread(target=lambda: claimed.extend(lcm._claim_notices(home)))+ claimant.start()+ claimant.join(0.5) # a claim that can run now (no lock) finishes here, before the writer writes+ go.set()+ writer.join(10)+ claimant.join(10)+ assert sorted(claimed + lcm._claim_notices(home)) == ["first", "second"]+++def test_repeated_starts_keep_one_copy_of_the_same_notice(tmp_path, monkeypatch, installs):+ """With allow_lazy_installs off every gateway start reports the same row: the chat agent gets it once."""+ home = _copilot_home(tmp_path, monkeypatch, "model:\n provider: copilot\nsecurity:\n allow_lazy_installs: false\n")+ for _ in range(3):+ monkeypatch.setattr(lcm, "_attempted", set()) # three gateway starts, no agent in between+ lcm.recover_at_startup()+ assert len(lcm._notice_file(home).read_text(encoding="utf-8").splitlines()) == 1+ monkeypatch.setattr(lcm, "_attempted", set())+ said: list[str] = []+ lcm.recover_at_startup(say=said.append) # a terminal's start: the kept copy, then its own report+ assert len(said) == 1 and "allow_lazy_installs is off" in said[0]++def test_doctor_in_a_terminal_front_end_leaves_kept_notices_for_the_next_agent(tmp_path, monkeypatch, installs,capsys):"""The classic CLI runs doctor in-process (console engine): a read-only diagnostic neither prints nor