The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): an oversized or live-SQLite cron script raises the named refusal, not the lifecycle verdict
teknium15ebbcbc8dc92
diff --git a/cron/lifecycle_guard.py b/cron/lifecycle_guard.pyindex 6b48263c1ad..168801e29eb 100644--- a/cron/lifecycle_guard.py+++ b/cron/lifecycle_guard.py@@ -915,17 +915,17 @@ def _sanitize_remote_script_text(return text, False-def _read_script_for_scanning(script_path: str) -> str:- """Read a cron script with the bounded scanner. Non-regular/oversized inputs fail closed via a- lifecycle-shaped sentinel; missing/unreadable paths stay empty so scheduler validation reports- them."""+def _read_script_for_scanning(script_path: str) -> tuple[str, Optional[str]]:+ """``(text, refusal)``: read a cron script with the bounded scanner. Non-regular/oversized/live+ SQLite inputs fail closed with a NAMED *refusal* (never a lifecycle-shaped verdict); missing or+ unreadable paths stay empty so scheduler validation reports them."""resolved = _resolve_script_path(script_path)if resolved is None:- return ""+ return "", Nonescript_text, unsafe = _read_referenced_script(resolved)if unsafe:- return "hermes gateway restart"- return script_text or ""+ return "", _unreadable_reason(resolved)+ return script_text or "", None# --- recursive walk ---------------------------------------------------------------------------@@ -1075,6 +1075,7 @@ def check_gateway_lifecycle(prompt: Optional[str], script: Optional[str] = None)propagate."""combined = prompt or ""python_script = False+ refusal: Optional[str] = Noneif script:resolved_script = _resolve_script_path(script)# Attribute the refusal correctly: not a lifecycle command, but a cloud path never opened.@@ -1093,15 +1094,16 @@ def check_gateway_lifecycle(prompt: Optional[str], script: Optional[str] = None)"(e.g. ~/.hermes/scripts/) and recreate the job.")python_script = resolved_script is not None and resolved_script.suffix == ".py"- script_text = _read_script_for_scanning(script)+ script_text, refusal = _read_script_for_scanning(script)if script_text:combined = f"{combined}\n{script_text}"- refusal: Optional[str] = None- if python_script:+ if refusal:+ unsafe = True+ elif python_script:# Python runs via the interpreter, never a POSIX shell, and the shell reference walk is a# false-positive generator on Python sources (pathlib "/" resolves to the filesystem root).- # The regex still scans the full text; non-regular/oversized files fail closed (sentinel).+ # The regex still scans the full text; non-regular/oversized files fail closed above (named refusal).# The data-exemption masker tokenizes with shlex, so it is charged against the walk budget.# The direct command regex below still scans the full text, so a literal `hermes gateway restart`# embedded in a .py script is still blocked. See #77131, #78398.diff --git a/tests/cron/test_lifecycle_guard_heredoc_walk.py b/tests/cron/test_lifecycle_guard_heredoc_walk.pyindex 061e72fada6..3b225798e03 100644--- a/tests/cron/test_lifecycle_guard_heredoc_walk.py+++ b/tests/cron/test_lifecycle_guard_heredoc_walk.py@@ -6,6 +6,8 @@ referenced scripts and ``sh -c`` payloads on the raw command, so a >1 MiB data pinside the Python body failed closed as an oversized "script"."""+import pytest+from cron.lifecycle_guard import contains_gateway_lifecycle_command_or_referenced_script as guard@@ -45,7 +47,7 @@ def test_inert_heredoc_body_script_path_still_read(tmp_path):"""Masking hides the body from the *executed* view only: a lifecycle script named inside aPython body is still handed to ``os.system`` at runtime, so its contents must still be read."""script = tmp_path / "restart.sh"- script.write_text("#!/bin/sh\nhermes gateway restart\n")+ script.write_text("#!/bin/sh\nhermes gateway restart\n", encoding="utf-8")command = f"python3 - <<'PY'\nimport os\nos.system('{script}')\nPY"assert guard(command, cwd=str(tmp_path)) is True@@ -58,9 +60,9 @@ def test_mentioned_data_file_that_cannot_be_scanned_is_not_a_verdict(tmp_path, mfrom hermes_cli.sqlite_safe_read import connect_trackedminified = tmp_path / "minified.json"- minified.write_text("[" + "1," * 40000 + "1]")+ minified.write_text("[" + "1," * 40000 + "1]", encoding="utf-8")notes = tmp_path / "notes.md"- notes.write_text("\n".join(f"| /opt/frag{i}/tool-{i}.md | note |" for i in range(70)))+ notes.write_text("\n".join(f"| /opt/frag{i}/tool-{i}.md | note |" for i in range(70)), encoding="utf-8")db = tmp_path / "state.db"conn = connect_tracked(db)monkeypatch.setattr(lifecycle_guard, "_MAX_LIFECYCLE_SCAN_REMOTE_READS", 8)@@ -72,7 +74,7 @@ def test_mentioned_data_file_that_cannot_be_scanned_is_not_a_verdict(tmp_path, mtry:for data in (minified, notes, db):- command = f"cd {tmp_path} && python3 - <<'PY'\nt = open('{data}').read()\nPY"+ command = f"cd {tmp_path} && python3 - <<'PY'\nt = open('{data}', encoding='utf-8').read()\nPY"assert guard(command, cwd=str(tmp_path), read_remote_script=remote) is False, data.nameassert remote_misses # the notes table was walked and its misses were bounded, not fatalunsafe, refusal = lifecycle_guard.scan_gateway_lifecycle(f"bash {minified}")@@ -89,6 +91,29 @@ def test_mentioned_script_with_lifecycle_command_still_blocks(tmp_path):import cron.lifecycle_guard as lifecycle_guardscript = tmp_path / "restart.sh"- script.write_text("#!/bin/sh\nhermes gateway restart\n")+ script.write_text("#!/bin/sh\nhermes gateway restart\n", encoding="utf-8")command = f"cd {tmp_path} && python3 - <<'PY'\nimport os\nos.system('{script}')\nPY"assert lifecycle_guard.scan_gateway_lifecycle(command, cwd=str(tmp_path)) == (True, None)++[email protected]("shape", ["oversized", "sqlite"])+def test_unscannable_own_cron_script_raises_named_refusal(tmp_path, shape):+ """A cron job whose OWN script is oversized or a live SQLite database fails closed with the+ named "could not scan" refusal, never the misattributed "contains a gateway lifecycle command"."""+ from cron.lifecycle_guard import GatewayLifecycleBlocked, check_gateway_lifecycle+ from hermes_cli.sqlite_safe_read import connect_tracked++ if shape == "oversized":+ script, conn = _big_file(tmp_path), None+ else:+ script = tmp_path / "state.db"+ conn = connect_tracked(script)+ try:+ with pytest.raises(GatewayLifecycleBlocked) as excinfo:+ check_gateway_lifecycle("run it", str(script))+ finally:+ if conn is not None:+ conn.close()+ message = str(excinfo.value)+ assert "could not scan" in message and str(script) in message+ assert "contains a gateway lifecycle command" not in message