Skip to content

The agent that grows with you

fix(cron): block taskkill gateway interpreter

KoNit-K339ee5b9dc75
diff --git a/cron/lifecycle_guard.py b/cron/lifecycle_guard.py
index 168801e29eb..b5a68288c73 100644
--- a/cron/lifecycle_guard.py
+++ b/cron/lifecycle_guard.py
@@ -60,6 +60,17 @@ _GATEWAY_LIFECYCLE_PATTERN = re.compile(
r"|(?:\bp?kill\b[^\n]*\bgateway\b[^\n]*\bhermes)"
)
+# Branch E: Windows image-name termination of the interpreter that runs the gateway. A gateway
+# process normally has the image name ``python.exe``; unlike a PID-targeted taskkill, ``/IM
+# python.exe`` terminates it even when the agent does not know its PID. Keep this tied to the
+# concrete taskkill command and its ``/IM`` option so other image names remain available to jobs.
+# The tokenized re-scan in ``contains_gateway_lifecycle_command`` applies this pattern after shell
+# quotes and escapes are resolved, just as it does for the other lifecycle branches (#113667).
+_TASKKILL_GATEWAY_INTERPRETER_PATTERN = re.compile(
+ r"(?i)(?<![/\w.\-])taskkill(?:\.exe)?\b"
+ r"(?=[^;\n|&]*(?:/im(?:\s+|:)\s*(?:['\"]?python\.exe['\"]?)(?=$|[\s,;|&\)])))"
+)
+
# Every branch uses `[^\n]*` between verb and label so matches cannot span unrelated lines. A POSIX
# backslash-newline continuation is therefore collapsed to a space before matching (as the shell
# does) rather than loosening `[^\n]*`.
@@ -268,7 +279,10 @@ def contains_gateway_lifecycle_command(text: str) -> bool:
text = strip_inert_heredoc_bodies(text)
normalized = _SHELL_LINE_CONTINUATION.sub(" ", text)
- if _GATEWAY_LIFECYCLE_PATTERN.search(normalized):
+ if (
+ _GATEWAY_LIFECYCLE_PATTERN.search(normalized)
+ or _TASKKILL_GATEWAY_INTERPRETER_PATTERN.search(normalized)
+ ):
return True
# Profile-flag form: blocked only when the named profile IS the one running the guard.
# Profile-flag form (#78028): `hermes -p <profile> gateway restart|stop` bypasses Branch A because the
@@ -289,10 +303,16 @@ def contains_gateway_lifecycle_command(text: str) -> bool:
# passes apply independently.
for segment in _iter_command_segments(normalized):
joined = " ".join(segment)
- if joined and _GATEWAY_LIFECYCLE_PATTERN.search(joined):
+ if joined and (
+ _GATEWAY_LIFECYCLE_PATTERN.search(joined)
+ or _TASKKILL_GATEWAY_INTERPRETER_PATTERN.search(joined)
+ ):
return True
stripped = _ARGV_LIST_PUNCTUATION.sub(" ", joined)
- if stripped != joined and _GATEWAY_LIFECYCLE_PATTERN.search(stripped):
+ if stripped != joined and (
+ _GATEWAY_LIFECYCLE_PATTERN.search(stripped)
+ or _TASKKILL_GATEWAY_INTERPRETER_PATTERN.search(stripped)
+ ):
return True
# The label may be built in an earlier `;`-segment, so no pass above sees verb + label together.
# Order-independent launchctl pass (#77083): a shell loop can build the gateway label from a variable
diff --git a/tests/hermes_cli/test_gateway_restart_loop.py b/tests/hermes_cli/test_gateway_restart_loop.py
index ce185516de3..7a727206c83 100644
--- a/tests/hermes_cli/test_gateway_restart_loop.py
+++ b/tests/hermes_cli/test_gateway_restart_loop.py
@@ -23,6 +23,26 @@ from hermes_cli.cron import cron_command
class TestGatewayLifecyclePattern:
"""Verify the regex catches gateway lifecycle commands."""
+ @pytest.mark.parametrize("text", [
+ "taskkill /F /IM python.exe",
+ "taskkill /IM python.exe /F",
+ 'taskkill /F /IM "python.exe"',
+ '"taskkill" /IM "python.exe" /F',
+ 'task"kill" /I"M" "python.exe" /F',
+ "taskkill.exe /T /IM python.exe",
+ 'subprocess.run(["taskkill", "/IM", "python.exe", "/F"])',
+ ])
+ def test_taskkill_gateway_interpreter_image_is_blocked(self, text):
+ """Image-name taskkill can terminate the gateway's Python process (#113667)."""
+ assert _contains_gateway_lifecycle_command(text), f"Should match: {text!r}"
+
+ @pytest.mark.parametrize("text", [
+ "taskkill /F /IM notepad.exe",
+ "taskkill /IM chrome.exe /F",
+ ])
+ def test_taskkill_other_image_names_are_allowed(self, text):
+ assert not _contains_gateway_lifecycle_command(text), f"Should NOT match: {text!r}"
+
@pytest.mark.parametrize("text", [
"hermes gateway restart",
"hermes gateway stop",