Skip to content

The agent that grows with you

fix: a restarted dashboard keeps its update action identity past the log tail

Teknium4bf62a398b5d
diff --git a/hermes_cli/web_routers/actions.py b/hermes_cli/web_routers/actions.py
index 050ee2dfd1d..f0c60579a02 100644
--- a/hermes_cli/web_routers/actions.py
+++ b/hermes_cli/web_routers/actions.py
@@ -147,6 +147,16 @@ def _latest_spawned_update_action_id(lines: List[str]) -> Optional[str]:
return None
+def _persisted_action_id(log_dir: Path, name: str) -> Optional[str]:
+ """Action id of the latest spawn of ``name`` from its sidecar (survives the log's tail
+ bound and rotation). A visible start header wins: a dashboard predating the sidecar wrote
+ only the header, and the newest header IS the latest spawn."""
+ with contextlib.suppress(OSError, UnicodeDecodeError):
+ value = (log_dir / f"{name}.action_id").read_text(encoding="utf-8-sig").strip()
+ return value if re.fullmatch(r"[0-9a-f]{32}", value) else None
+ return None
+
+
@router.post("/api/gateway/restart")
async def restart_gateway(profile: Optional[str] = None):
"""Kick off a ``hermes gateway restart`` in the background."""
@@ -374,7 +384,7 @@ async def get_action_status(name: str, lines: int = 200):
durable_update_action_id = _durable_completed_update_action_id(
_tail_lines(get_default_hermes_root() / "logs" / "update.log", 2000))
spawned_action_id = (_latest_spawned_update_action_id(_tail_lines(log_dir / log_file_name, 2000))
- or _ACTION_IDS.get(name))
+ or _persisted_action_id(log_dir, name) or _ACTION_IDS.get(name))
if durable_update_action_id != spawned_action_id:
# The root log is shared by every profile: another profile's (or an older) run's
# completion never certifies the action this dashboard started.
diff --git a/hermes_cli/web_server_gateway.py b/hermes_cli/web_server_gateway.py
index 9f4b5bb0c0a..2aa9d6a6146 100644
--- a/hermes_cli/web_server_gateway.py
+++ b/hermes_cli/web_server_gateway.py
@@ -462,6 +462,11 @@ def _spawn_hermes_action(
action_id = (env_overrides or {}).get("HERMES_ACTION_ID")
stamp = time.strftime('%Y-%m-%d %H:%M:%S') + (f" {action_id}" if action_id else "")
log_file.write(f"\n=== {name} started {stamp} ===\n".encode())
+ if action_id:
+ # ...and a sidecar keeps it once build output pushes the header past the status route's
+ # bounded tail, or the log rotates: a restarted dashboard still knows which action it ran.
+ from hermes_cli.runtime_state import _atomic_bytes
+ _atomic_bytes(_ACTION_LOG_DIR / f"{name}.action_id", action_id.encode("ascii"))
from hermes_cli._launchers import runtime_command
cmd = runtime_command(PROJECT_ROOT, subcommand)
diff --git a/tests/hermes_cli/test_web_server_update_status.py b/tests/hermes_cli/test_web_server_update_status.py
index e42a65d4d8b..fdab5db065c 100644
--- a/tests/hermes_cli/test_web_server_update_status.py
+++ b/tests/hermes_cli/test_web_server_update_status.py
@@ -87,6 +87,50 @@ class TestUpdateStatusRootLog:
assert own["exit_code"] == 0
assert own["action_id"] == b_id
+ @pytest.mark.parametrize("log_shape,b_outcome", [
+ ("short", "success"), ("long", "success"), ("rotated", "success"), ("long", "failed"), ("long", None)])
+ def test_restarted_dashboard_keeps_its_action_identity_past_the_log_tail(
+ self, monkeypatch, tmp_path, log_shape, b_outcome):
+ # The status route reads at most 2,000 log lines: build output (or rotation) pushing the
+ # start header out must not turn B's own success into exit null ("Action failed (exit ?)"),
+ # nor let profile A's newer latest receipt answer for B.
+ from hermes_cli import update_receipt
+
+ root, b_id, a_id = tmp_path / "root", "b" * 32, "a" * 32
+ (root / "logs").mkdir(parents=True)
+ b_logs = tmp_path / "b-logs"
+ monkeypatch.setattr(_web_server_gateway, "_ACTION_LOG_DIR", b_logs)
+ for registry in ("_ACTION_PROCS", "_ACTION_RESULTS", "_ACTION_COMMANDS", "_ACTION_IDS"):
+ monkeypatch.setattr(_web_server_gateway, registry, {})
+ monkeypatch.setattr(_web_server_gateway.subprocess, "Popen", lambda *a, **kw: types.SimpleNamespace(pid=7))
+ monkeypatch.setenv("HERMES_HOME", str(root / "profiles" / "b"))
+ _web_server_gateway._spawn_hermes_action(["update"], "hermes-update", env_overrides={"HERMES_ACTION_ID": b_id})
+ for profile, action_id, outcome in (("b", b_id, b_outcome), ("a", a_id, "success")):
+ if outcome:
+ monkeypatch.setenv("HERMES_HOME", str(root / "profiles" / profile))
+ monkeypatch.setenv("HERMES_ACTION_ID", action_id)
+ update_receipt.begin_update_receipt()
+ update_receipt.finalize_update_receipt(outcome)
+ monkeypatch.delenv("HERMES_ACTION_ID")
+ monkeypatch.setenv("HERMES_HOME", str(root / "profiles" / "b"))
+ log = b_logs / "hermes-update.log"
+ if log_shape == "long":
+ with log.open("a", encoding="utf-8") as handle:
+ handle.write("build output\n" * 2001)
+ elif log_shape == "rotated":
+ log.unlink()
+ if b_outcome == "success":
+ (root / "logs" / "update.log").write_text(f"=== hermes-update completed {b_id} ===\n", encoding="utf-8")
+ _web_server_gateway._ACTION_PROCS.clear() # dashboard restarted: in-memory registries lost
+ _web_server_gateway._ACTION_IDS.clear()
+
+ data = self.client.get("/api/actions/hermes-update/status?lines=2000").json()
+
+ assert data["running"] is False
+ assert data["exit_code"] == (0 if b_outcome == "success" else None)
+ assert data.get("action_id") == (b_id if b_outcome == "success" else None)
+ assert data["receipt"]["action_id"] == (b_id if b_outcome else a_id)
+
@pytest.mark.parametrize("b_outcome", [None, "failed", "success"])
def test_root_latest_receipt_of_another_profile_never_certifies_this_action(self, monkeypatch, tmp_path, b_outcome):
# The receipt store is root-wide too: A's later success in latest.json must not become B's