fix(presence): pgrep False-Positive + DDP 429-Verbesserung (V8.8)
Bug 1 — pgrep argv-basierter Scanner: - _pgrep_hermes_subagents() nutzt jetzt argv-basierte Erkennung statt Substring-Suche in der kompletten cmdline - 'chat' und '--profile' muessen eigene argv-Elemente sein - argv[0] basename pruefen: 'hermes', 'python', 'python3' - python: '-m hermes_cli.main' oder hermes-Skript-Pfad in argv - Gateway-PID aus gateway_state.json ausschliessen - Neue _read_gateway_pid() Hilfsmethode - 6 neue False-Positive-Tests (bash grep, snap-path, gateway) Bug 2 — DDP 429 Circuit-Breaker + Pulse-Interval: - Circuit-Breaker threshold: 3 → 1 (sofort nach erstem 429) - DDP-Timeout: 2.0s → 1.0s (schneller fail) - Mode-abhaengige Pulse-Intervalle: away=3.0s, busy=1.5s - set_pulse_target() passt dynamisch das Interval an Tests: 144/144 passed
This commit is contained in:
@@ -162,21 +162,25 @@ class TestHoneymoonExpiredWithBgTasks:
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
class TestPgrepHermesSubagents:
|
||||
"""Test 6: _pgrep_hermes_subagents() erkennt hermes chat --profile Prozesse."""
|
||||
"""Test 6: _pgrep_hermes_subagents() erkennt hermes chat --profile Prozesse.
|
||||
|
||||
def test_pgrep_finds_hermes_chat_profile_process(self):
|
||||
"""_pgrep_hermes_subagents() findet einen simulierten hermes-Prozess."""
|
||||
V8.8: argv-basierter Scanner — verhindert False-Positives auf
|
||||
Shell-Befehle die 'hermes'+'chat'+'--profile' als Text enthalten.
|
||||
"""
|
||||
|
||||
def test_pgrep_finds_hermes_binary_chat_profile(self):
|
||||
"""_pgrep_hermes_subagents() findet 'hermes chat --profile rainer'."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
own_pid = os.getpid()
|
||||
|
||||
# Mock /proc: nur ein simulierter hermes chat --profile Prozess
|
||||
# Mock /proc: ein echter hermes chat --profile Prozess
|
||||
mock_proc_entries = ["1", "2", str(own_pid), "99999", "12345"]
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
"""Simuliere /proc/<pid>/cmdline."""
|
||||
if pid_dir == "12345":
|
||||
return b"python\x00hermes\x00chat\x00--profile\x00miguel\x00--model\x00deepseek\x00"
|
||||
# argv: ["hermes", "chat", "--profile", "rainer"]
|
||||
return b"hermes\x00chat\x00--profile\x00rainer\x00"
|
||||
elif pid_dir == str(own_pid):
|
||||
return b"python\x00pytest\x00"
|
||||
else:
|
||||
@@ -184,9 +188,7 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
with patch("os.listdir", return_value=mock_proc_entries):
|
||||
with patch("builtins.open") as mock_open:
|
||||
# Simuliere open() für /proc/<pid>/cmdline
|
||||
def open_side_effect(path, mode="r"):
|
||||
# Extrahiere PID aus Pfad: /proc/<pid>/cmdline
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 3 and parts[1] == "proc":
|
||||
pid_dir = parts[2]
|
||||
@@ -197,11 +199,178 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
# Mock _read_gateway_pid → None (kein Gateway)
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
|
||||
# Sollte 1 Prozess finden (PID 12345, nicht own_pid)
|
||||
assert count == 1, f"Expected 1, got {count}"
|
||||
|
||||
def test_pgrep_finds_python_m_hermes_cli_chat_profile(self):
|
||||
"""_pgrep_hermes_subagents() findet 'python -m hermes_cli.main chat --profile miguel'."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
own_pid = os.getpid()
|
||||
mock_proc_entries = ["1", str(own_pid), "12345"]
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
if pid_dir == "12345":
|
||||
# argv: ["python", "-m", "hermes_cli.main", "chat", "--profile", "miguel"]
|
||||
return b"python\x00-m\x00hermes_cli.main\x00chat\x00--profile\x00miguel\x00"
|
||||
elif pid_dir == str(own_pid):
|
||||
return b"python\x00pytest\x00"
|
||||
return b"bash\x00"
|
||||
|
||||
with patch("os.listdir", return_value=mock_proc_entries):
|
||||
with patch("builtins.open") as mock_open:
|
||||
def open_side_effect(path, mode="r"):
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 3 and parts[1] == "proc":
|
||||
pid_dir = parts[2]
|
||||
mock_file = MagicMock()
|
||||
mock_file.__enter__.return_value.read.return_value = mock_cmdline_read(pid_dir)
|
||||
return mock_file
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
|
||||
assert count == 1, f"Expected 1, got {count}"
|
||||
|
||||
def test_pgrep_false_positive_bash_grep(self):
|
||||
"""V8.8: 'bash -c grep hermes.*chat.*--profile' → KEIN match (False-Positive)."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
own_pid = os.getpid()
|
||||
mock_proc_entries = ["1", str(own_pid), "12345"]
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
if pid_dir == "12345":
|
||||
# argv: ["bash", "-c", "grep hermes.*chat.*--profile /var/log/..."]
|
||||
return b"bash\x00-c\x00grep hermes.*chat.*--profile /var/log/syslog\x00"
|
||||
elif pid_dir == str(own_pid):
|
||||
return b"python\x00pytest\x00"
|
||||
return b"bash\x00"
|
||||
|
||||
with patch("os.listdir", return_value=mock_proc_entries):
|
||||
with patch("builtins.open") as mock_open:
|
||||
def open_side_effect(path, mode="r"):
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 3 and parts[1] == "proc":
|
||||
pid_dir = parts[2]
|
||||
mock_file = MagicMock()
|
||||
mock_file.__enter__.return_value.read.return_value = mock_cmdline_read(pid_dir)
|
||||
return mock_file
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
|
||||
assert count == 0, f"Expected 0 (false positive), got {count}"
|
||||
|
||||
def test_pgrep_false_positive_hermes_snap_path(self):
|
||||
"""V8.8: '/tmp/hermes-snap-xxx bash -c ...' → KEIN match (False-Positive)."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
own_pid = os.getpid()
|
||||
mock_proc_entries = ["1", str(own_pid), "12345"]
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
if pid_dir == "12345":
|
||||
# argv: ["/tmp/hermes-snap-xxx", "bash", "-c", "hermes chat --profile"]
|
||||
return b"/tmp/hermes-snap-abc123\x00bash\x00-c\x00hermes chat --profile rainer\x00"
|
||||
elif pid_dir == str(own_pid):
|
||||
return b"python\x00pytest\x00"
|
||||
return b"bash\x00"
|
||||
|
||||
with patch("os.listdir", return_value=mock_proc_entries):
|
||||
with patch("builtins.open") as mock_open:
|
||||
def open_side_effect(path, mode="r"):
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 3 and parts[1] == "proc":
|
||||
pid_dir = parts[2]
|
||||
mock_file = MagicMock()
|
||||
mock_file.__enter__.return_value.read.return_value = mock_cmdline_read(pid_dir)
|
||||
return mock_file
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
|
||||
assert count == 0, f"Expected 0 (false positive), got {count}"
|
||||
|
||||
def test_pgrep_false_positive_gateway_process(self):
|
||||
"""V8.8: Gateway 'python -m hermes_cli.main gateway run' → KEIN match."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
own_pid = os.getpid()
|
||||
mock_proc_entries = ["1", str(own_pid), "12345"]
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
if pid_dir == "12345":
|
||||
# argv: ["python", "-m", "hermes_cli.main", "gateway", "run"]
|
||||
return b"python\x00-m\x00hermes_cli.main\x00gateway\x00run\x00"
|
||||
elif pid_dir == str(own_pid):
|
||||
return b"python\x00pytest\x00"
|
||||
return b"bash\x00"
|
||||
|
||||
with patch("os.listdir", return_value=mock_proc_entries):
|
||||
with patch("builtins.open") as mock_open:
|
||||
def open_side_effect(path, mode="r"):
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 3 and parts[1] == "proc":
|
||||
pid_dir = parts[2]
|
||||
mock_file = MagicMock()
|
||||
mock_file.__enter__.return_value.read.return_value = mock_cmdline_read(pid_dir)
|
||||
return mock_file
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
|
||||
assert count == 0, f"Expected 0 (gateway, no chat), got {count}"
|
||||
|
||||
def test_pgrep_excludes_gateway_pid(self):
|
||||
"""V8.8: Gateway-PID aus gateway_state.json wird ausgeschlossen."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
|
||||
own_pid = os.getpid()
|
||||
gateway_pid = 12345
|
||||
mock_proc_entries = ["1", str(own_pid), str(gateway_pid)]
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
if pid_dir == str(gateway_pid):
|
||||
# Sieht aus wie hermes chat --profile, ist aber Gateway
|
||||
return b"hermes\x00chat\x00--profile\x00miguel\x00"
|
||||
elif pid_dir == str(own_pid):
|
||||
return b"python\x00pytest\x00"
|
||||
return b"bash\x00"
|
||||
|
||||
with patch("os.listdir", return_value=mock_proc_entries):
|
||||
with patch("builtins.open") as mock_open:
|
||||
def open_side_effect(path, mode="r"):
|
||||
parts = path.split("/")
|
||||
if len(parts) >= 3 and parts[1] == "proc":
|
||||
pid_dir = parts[2]
|
||||
mock_file = MagicMock()
|
||||
mock_file.__enter__.return_value.read.return_value = mock_cmdline_read(pid_dir)
|
||||
return mock_file
|
||||
raise FileNotFoundError(path)
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=gateway_pid):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
|
||||
assert count == 0, f"Expected 0 (gateway excluded), got {count}"
|
||||
|
||||
def test_pgrep_returns_zero_when_no_hermes_processes(self):
|
||||
"""_pgrep_hermes_subagents() → 0 wenn keine hermes-Prozesse."""
|
||||
from plugin.archivar.presence_engine import DetectionTimer
|
||||
@@ -227,7 +396,8 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
assert count == 0, f"Expected 0, got {count}"
|
||||
|
||||
def test_pgrep_excludes_own_pid(self):
|
||||
@@ -240,7 +410,7 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
# Eigener Prozess hat hermes chat --profile in cmdline
|
||||
return b"python\x00hermes\x00chat\x00--profile\x00miguel\x00"
|
||||
return b"hermes\x00chat\x00--profile\x00miguel\x00"
|
||||
|
||||
with patch("os.listdir", return_value=mock_proc_entries):
|
||||
with patch("builtins.open") as mock_open:
|
||||
@@ -255,7 +425,8 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
# Eigene PID wird ausgeschlossen → 0
|
||||
assert count == 0, f"Expected 0 (own PID excluded), got {count}"
|
||||
|
||||
@@ -268,9 +439,9 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
def mock_cmdline_read(pid_dir):
|
||||
if pid_dir == "55555":
|
||||
return b"python\x00hermes\x00serve\x00" # hermes aber kein chat
|
||||
return b"hermes\x00serve\x00" # hermes binary aber kein chat
|
||||
elif pid_dir == "66666":
|
||||
return b"python\x00chat\x00--profile\x00" # chat aber kein hermes
|
||||
return b"python\x00chat\x00--profile\x00" # chat+profile aber argv0=python ohne -m hermes_cli
|
||||
elif pid_dir == str(own_pid):
|
||||
return b"python\x00pytest\x00"
|
||||
return b"bash\x00"
|
||||
@@ -288,7 +459,8 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
# Keiner der Prozesse hat hermes+chat+--profile → 0
|
||||
assert count == 0, f"Expected 0, got {count}"
|
||||
|
||||
@@ -317,7 +489,8 @@ class TestPgrepHermesSubagents:
|
||||
|
||||
mock_open.side_effect = open_side_effect
|
||||
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
with patch.object(DetectionTimer, '_read_gateway_pid', return_value=None):
|
||||
count = DetectionTimer._pgrep_hermes_subagents()
|
||||
# PermissionError → Prozess wird ignoriert, count=0
|
||||
assert count == 0, f"Expected 0, got {count}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user