Skip to content

The agent that grows with you

fix(cron): -f patterns must match the gateway cmdline, not any "hermes" substring; ledger names agent-issued kills

teknium15d027bbd06b4
diff --git a/cron/lifecycle_guard.py b/cron/lifecycle_guard.py
index 7565b02d0f1..d687a4a4b4d 100644
--- a/cron/lifecycle_guard.py
+++ b/cron/lifecycle_guard.py
@@ -93,6 +93,19 @@ _ERE_WILDCARD_ONLY = re.compile(r"^[.*+?$\s]*$")
_NAME_KILLERS = frozenset({"pkill", "killall", "taskkill", "stop-process"})
_NAME_ENUMERATORS = frozenset({"pgrep", "pidof", "get-process"})
_KILL_VERB_RE = re.compile(r"(?i)\b(?:kill|taskkill|stop-process)\b")
+# A `-f` pattern that does not start with the interpreter reaches the gateway cmdline
+# (`python -m hermes_cli.main gateway run` / `hermes gateway run`) only through its own tokens;
+# an unrelated script that merely contains "hermes" (`hermes-polis/run.sh`, `my_hermes_bot.py`)
+# cannot match it. Same hermes+gateway pairing as Branch D, plus the module path.
+_GATEWAY_CMDLINE_TOKEN_RE = re.compile(r"(?i)hermes_cli|\bhermes\b[^\n]*\bgateway\b|\bgateway\b[^\n]*\bhermes\b")
+# Rejection text for Branch E, shared by every tool surface that runs the guard so the agent is
+# pointed at the ownership-scoped route (proc_* id / explicit PID) rather than the shell.
+HOST_INTERPRETER_KILL_REJECTION = (
+ "Blocked: this command kills every process whose image/name matches the Python "
+ "interpreter, which is the process hosting this gateway (and this command). "
+ "Stop only the process you own instead: process(action=\"kill\", session_id=\"proc_…\") "
+ "for a background job Hermes started, or kill/taskkill by its explicit PID."
+)
def _is_interpreter_image(value: str, *, substring: bool = False) -> bool:
@@ -126,7 +139,7 @@ def _pattern_reaches_host_interpreter(pattern: str, *, full_cmdline: bool, exact
rest = head[match.start():] + " " + rest
head = head[: match.start()]
if not _is_interpreter_image(head, substring=not exact):
- return full_cmdline and "hermes" in core.lower()
+ return full_cmdline and bool(_GATEWAY_CMDLINE_TOKEN_RE.search(core))
return not rest.strip() or bool(_ERE_WILDCARD_ONLY.match(rest)) or "hermes" in rest.lower()
diff --git a/gateway/lifecycle_ledger.py b/gateway/lifecycle_ledger.py
index d3e33c204c3..e3f30a3030d 100644
--- a/gateway/lifecycle_ledger.py
+++ b/gateway/lifecycle_ledger.py
@@ -213,7 +213,8 @@ def _report_unclean_exit(evidence: Dict[str, Any], home: Optional[Path]) -> None
_append_exit_diag({"ts": _now_iso(), "tag": "gateway.previous_unclean_exit", "pid": os.getpid(), **evidence}, home)
logger.warning(
"Previous gateway life (pid=%s, started_at=%s) exited UNCLEANLY (no exit path ran — SIGKILL / OOM / "
- "VM death). last_heartbeat_at=%s last_mem=%s suspected_oom=%s",
+ "VM death, or a process kill issued by the agent or one of its descendants, e.g. a pkill/taskkill of "
+ "the host interpreter image; see #113667). last_heartbeat_at=%s last_mem=%s suspected_oom=%s",
evidence.get("prior_pid"), evidence.get("prior_started_at"), evidence.get("last_heartbeat_at"),
evidence.get("last_heartbeat_mem"), evidence.get("suspected_oom", False),
)
diff --git a/tests/gateway/test_lifecycle_ledger.py b/tests/gateway/test_lifecycle_ledger.py
index 54127a6928d..cdb07b352bb 100644
--- a/tests/gateway/test_lifecycle_ledger.py
+++ b/tests/gateway/test_lifecycle_ledger.py
@@ -143,6 +143,16 @@ def test_record_startup_persists_unclean_report_and_reclaims(tmp_path: Path) ->
assert sentinel["pid"] == os.getpid()
+def test_unclean_report_names_agent_issued_kill_as_a_cause(tmp_path: Path, caplog) -> None:
+ """The cause family must not read as OS-only: an agent/descendant `pkill` of the host interpreter
+ leaves the identical evidence (no exit path ran) and is the operator's first thing to rule out (#113667)."""
+ _write_sentinel(tmp_path, {"phase": "running", "pid": _DEAD_PID, "start_time": 1000.0})
+ with caplog.at_level("WARNING", logger="gateway.lifecycle_ledger"):
+ assert record_startup(home=tmp_path) is not None
+ message = next(r.getMessage() for r in caplog.records if "exited UNCLEANLY" in r.getMessage())
+ assert "kill issued by the agent" in message and "OOM" in message
+
+
def test_record_startup_carries_unclean_flags_onto_new_sentinel(
tmp_path: Path,
) -> None:
diff --git a/tests/hermes_cli/test_gateway_restart_loop.py b/tests/hermes_cli/test_gateway_restart_loop.py
index 54c625acb14..ca2e000cf3e 100644
--- a/tests/hermes_cli/test_gateway_restart_loop.py
+++ b/tests/hermes_cli/test_gateway_restart_loop.py
@@ -68,6 +68,9 @@ class TestGatewayLifecyclePattern:
"pkill -u alice chrome",
"pkill -t pts/1 vim",
"pkill -f 'python mt_add_paused.py --go'",
+ # Unrelated scripts that merely contain "hermes" cannot match the gateway cmdline.
+ "pkill -f 'hermes-polis/run.sh'",
+ "pkill -f my_hermes_bot.py",
"pgrep python",
"hermes.exe gateway start",
"my-hermes.exe gateway restart",
@@ -941,7 +944,7 @@ class TestLifecycleGuardModule:
contains_gateway_lifecycle_command_or_referenced_script,
)
script = tmp_path / "restart.sh"
- script.write_text("#!/bin/bash\nhermes gateway restart\n")
+ script.write_text("#!/bin/bash\nhermes gateway restart\n", encoding="utf-8")
assert (
contains_gateway_lifecycle_command_or_referenced_script(f". {script}")
is True
@@ -971,7 +974,7 @@ class TestLifecycleGuardModule:
contains_gateway_lifecycle_command_or_referenced_script,
)
script = tmp_path / "restart.sh"
- script.write_text("#!/bin/bash\nhermes gateway restart\n")
+ script.write_text("#!/bin/bash\nhermes gateway restart\n", encoding="utf-8")
assert (
contains_gateway_lifecycle_command_or_referenced_script(f"source {script}")
is True
@@ -984,7 +987,7 @@ class TestLifecycleGuardModule:
contains_gateway_lifecycle_command_or_referenced_script,
)
script = tmp_path / "activate.sh"
- script.write_text("#!/bin/bash\nexport PATH=/usr/bin:$PATH\n")
+ script.write_text("#!/bin/bash\nexport PATH=/usr/bin:$PATH\n", encoding="utf-8")
assert (
contains_gateway_lifecycle_command_or_referenced_script(f". {script}")
is False