Skip to content

The agent that grows with you

fix(approval): anchor launchctl lookaheads to prevent GIL starvation

Hermes Patch Auditea870b4d3eac
diff --git a/tests/tools/test_approval_launchctl_performance.py b/tests/tools/test_approval_launchctl_performance.py
new file mode 100644
index 00000000000..d2340450d52
--- /dev/null
+++ b/tests/tools/test_approval_launchctl_performance.py
@@ -0,0 +1,18 @@
+"""Long non-launchctl inputs must not starve other Gateway threads.
+
+Subprocess timeout bounds regressions without hanging pytest on the GIL.
+"""
+import subprocess
+import sys
+
+
+def test_launchctl_guard_long_negative_input_is_bounded():
+ code = '''
+from tools.approval_detection import DANGEROUS_PATTERNS_COMPILED
+rules = [(rx, desc) for rx, desc in DANGEROUS_PATTERNS_COMPILED
+ if desc == "stop/restart hermes launchd service (kills running agents)"]
+assert len(rules) == 1
+rx = rules[0][0]
+assert rx.search("x" * 100_000) is None
+'''
+ subprocess.run([sys.executable, '-c', code], check=True, timeout=5)
diff --git a/tools/approval_detection.py b/tools/approval_detection.py
index 09a281b6f59..b03cd9ccd36 100644
--- a/tools/approval_detection.py
+++ b/tools/approval_detection.py
@@ -364,7 +364,9 @@ DANGEROUS_PATTERNS = [
# sequential match: a for-loop building the label from a list defined EARLIER (`for item in 'ai.hermes...'; do
# launchctl bootout "$label"`) never has "hermes" after the verb, and that slipped past and restarted 4 gateways
# with zero approval. Erring broad is correct for an approval gate: an extra prompt is cheap.
- (r'(?=[\s\S]*\blaunchctl\s+(?:stop|kickstart|bootout|unload|kill|disable|remove)\b)(?=[\s\S]*\b(?:hermes|ai\.hermes)\b)', "stop/restart hermes launchd service (kills running agents)"),
+ # Anchor whole-input lookaheads: re.search otherwise rescans every suffix of
+ # long non-matching commands, holding the GIL and starving Gateway threads.
+ (r'\A(?=[\s\S]*\blaunchctl\s+(?:stop|kickstart|bootout|unload|kill|disable|remove)\b)(?=[\s\S]*\b(?:hermes|ai\.hermes)\b)', "stop/restart hermes launchd service (kills running agents)"),
(rf'\b(cp|mv|install)\b.*\s{_SYSTEM_CONFIG_PATH}', "copy/move file into system config path"),
(rf'\b(cp|mv|install)\b.*\s["\']?{_PROJECT_SENSITIVE_WRITE_TARGET}["\']?{_COMMAND_TAIL}', "overwrite project env/config file"),
# cp/mv/install OVERWRITING a credential/SSH/shell-rc/Hermes file (key implant, login-time