Skip to content

The agent that grows with you

fix(tui_gateway): turn-marker edits are serialized across backends

Maciej Lisiewski656554cd63e6
diff --git a/tests/tui_gateway/_turn_marker_backend.py b/tests/tui_gateway/_turn_marker_backend.py
new file mode 100644
index 00000000000..e8c2d459a32
--- /dev/null
+++ b/tests/tui_gateway/_turn_marker_backend.py
@@ -0,0 +1,20 @@
+"""Sibling-backend child for the turn-marker cross-process race test (spawned, so importable)."""
+
+from __future__ import annotations
+
+
+def record_after_parent_moves(home: str, session_key: str, loaded, proceed) -> None:
+ """Start a turn on *session_key*, pausing between the marker file's load and its store until
+ *proceed* is set — the window a second backend on the same HERMES_HOME races into."""
+ from tui_gateway import turn_marker
+
+ original_load = turn_marker._load
+
+ def paused_load(path):
+ entries = original_load(path)
+ loaded.set()
+ proceed.wait(30)
+ return entries
+
+ turn_marker._load = paused_load
+ turn_marker.record_turn_start(home, session_key, "second prompt")
diff --git a/tests/tui_gateway/test_auto_continue.py b/tests/tui_gateway/test_auto_continue.py
index cf4e291a6c3..d3a7cf968d7 100644
--- a/tests/tui_gateway/test_auto_continue.py
+++ b/tests/tui_gateway/test_auto_continue.py
@@ -21,6 +21,7 @@ time is positive proof the turn never finished. Contract pinned here:
from __future__ import annotations
+import multiprocessing
import os
import subprocess
import sys
@@ -135,6 +136,36 @@ def test_marker_survives_corrupt_sidecar(tmp_path):
assert read_turn_marker(tmp_path, "abc")["prompt"] == "prompt"
+def test_sibling_backend_record_does_not_resurrect_a_cleared_marker(tmp_path, monkeypatch):
+ """Two backends on one HERMES_HOME: B loads the marker file to start S2, A clears its concluded
+ S1, then B stores — B's stale snapshot must not bring S1 back (resume would re-run a finished turn)."""
+ from hermes_cli import auth
+ from tests.tui_gateway._turn_marker_backend import record_after_parent_moves
+
+ record_turn_start(tmp_path, "S1", "first prompt")
+ context = multiprocessing.get_context("spawn")
+ loaded, proceed = context.Event(), context.Event()
+ backend_b = context.Process(target=record_after_parent_moves, args=(str(tmp_path), "S2", loaded, proceed))
+ backend_b.start()
+ assert loaded.wait(timeout=30)
+
+ original_kernel_lock = auth._kernel_lock
+
+ def lock_releasing_b(lock_file, exclusive):
+ # A is about to wait on B's lock: let B finish so the wait ends instead of deadlocking the test.
+ proceed.set()
+ return original_kernel_lock(lock_file, exclusive)
+
+ monkeypatch.setattr(auth, "_kernel_lock", lock_releasing_b)
+ clear_turn_marker(tmp_path, "S1")
+ proceed.set()
+ backend_b.join(timeout=30)
+
+ assert backend_b.exitcode == 0
+ assert read_turn_marker(tmp_path, "S1") is None
+ assert read_turn_marker(tmp_path, "S2")["prompt"] == "second prompt"
+
+
def _patch_local_interrupt(monkeypatch, session):
monkeypatch.setattr(server, "_tts_stream_stop", lambda: None)
monkeypatch.setattr(server, "_sess_nowait", lambda params, rid: (session, None))
diff --git a/tui_gateway/turn_marker.py b/tui_gateway/turn_marker.py
index cb3d96db0b9..26b8d616e76 100644
--- a/tui_gateway/turn_marker.py
+++ b/tui_gateway/turn_marker.py
@@ -26,12 +26,21 @@ _MAX_ENTRIES = 32
_MAX_PROMPT_CHARS = 64_000
_lock = threading.Lock()
+_file_lock_holder = threading.local()
def _marker_path(home: Path | str) -> Path:
return Path(home) / "desktop" / "interrupted_turns.json"
+def _cross_process_lock(path: Path):
+ """Sibling backends on one HERMES_HOME (#94778) don't share ``_lock``; without this flock one's stale
+ load → store resurrects a marker the other just cleared (a finished turn auto-continues again)."""
+ from hermes_cli.auth import _file_lock
+ return _file_lock(path.with_name(f"{path.name}.lock"), _file_lock_holder, 5.0,
+ f"Timed out waiting for the turn-marker lock ({path})")
+
+
def _started_at(entry: dict) -> float:
return float(entry.get("started_at") or 0)
@@ -103,8 +112,8 @@ def _store(path: Path, entries: dict[str, dict]) -> None:
def _update(home: Path | str, session_key: str, mutate, what: str) -> None:
"""Load → ``mutate(entries)`` → store under the lock; ``mutate`` returns None to skip the write."""
try:
- with _lock:
- path = _marker_path(home)
+ path = _marker_path(home)
+ with _lock, _cross_process_lock(path):
entries = mutate(_load(path))
if entries is not None:
_store(path, entries)