feat: V8.7 pgrep-basierte Subagent-Erkennung ersetzt subagent_status.json

- adapter.py: register()-Block entfernt (wurde bei jedem Turn aufgerufen)
- subagent_tracker.py: register()/complete() zu no-ops, Modul bleibt kompatibel
- presence_engine.py: _pgrep_hermes_subagents() scannt /proc/*/cmdline
  nach 'hermes chat --profile' Prozessen (exkl. eigene PID, <1ms)
- DetectionTimer.check(): pgrep als vierte Quelle (nach PID-Sentinel,
  gateway_state, plugin_state), ersetzt subagent_status.json
- _stale_recovery() entfernt (pgrep braucht kein Cleanup)
- _read_subagent_count() delegiert an _pgrep_hermes_subagents()
- subprocess-Import entfernt
- 1434 Tests grün (test_subagent_tracker, test_subagent_status_presence,
  test_presence_engine alle angepasst)
This commit is contained in:
Bernd (Hermes)
2026-08-02 13:24:10 +02:00
parent 06e8d832df
commit 30015f0cfe
6 changed files with 418 additions and 984 deletions
+68 -138
View File
@@ -936,16 +936,14 @@ class TestPulseControllerLastSentPhase:
class TestDetectionTimerPgrepPrimary:
"""Test 20: DetectionTimer — gateway_state PRIMARY, pgrep SECONDARY (P1b)."""
"""Test 20: DetectionTimer — gateway_state PRIMARY, pgrep SECONDARY (V8.7)."""
def test_pgrep_finds_other_agents(self):
"""gateway_state=1 (nicht >1) → pgrep-Fallback findet anderen Prozess → result=True."""
"""gateway_state=1 (nicht >1) → pgrep findet anderen Prozess → result=True."""
from plugin.archivar.presence_engine import DetectionTimer
import tempfile
import json
own_pid = os.getpid()
# gateway_state mit active_agents=1 (nicht >1 → fällt auf pgrep zurück)
state = {"active_agents": 1, "profiles": ["default"]}
with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="w") as f:
@@ -953,20 +951,14 @@ class TestDetectionTimerPgrepPrimary:
state_path = f.name
try:
# Mock pgrep: findet eigenen + fremden Prozess
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n99999\n",
stderr="",
returncode=0,
)
# Mock _pgrep_hermes_subagents: findet 2 Prozesse
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=2):
dt = DetectionTimer(
gateway_state_path=state_path,
detection_interval=0.1,
)
result = dt.check()
# gateway_state=1 → pgrep-Fallback findet anderen Prozess → True
# gateway_state=1 → pgrep findet 2 Prozesse → True
assert result is True
finally:
os.unlink(state_path)
@@ -984,8 +976,8 @@ class TestDetectionTimerPgrepPrimary:
state_path = f.name
try:
# Mock pgrep: sollte NICHT aufgerufen werden
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
# Mock _pgrep_hermes_subagents: sollte NICHT aufgerufen werden
with patch.object(DetectionTimer, '_pgrep_hermes_subagents') as mock_pgrep:
dt = DetectionTimer(
gateway_state_path=state_path,
detection_interval=0.1,
@@ -994,50 +986,34 @@ class TestDetectionTimerPgrepPrimary:
# gateway_state=3 > 1 → PRIMARY entscheidet → True
assert result is True
# pgrep wurde NICHT aufgerufen (gateway_state hat entschieden)
mock_run.assert_not_called()
mock_pgrep.assert_not_called()
finally:
os.unlink(state_path)
class TestDetectionTimerPgrepNoGatewayState:
"""Test 21: DetectionTimer pgrep-Fallback funktioniert auch ohne gateway_state.json (P1b)."""
"""Test 21: DetectionTimer pgrep funktioniert auch ohne gateway_state.json (V8.7)."""
def test_pgrep_works_without_gateway_state(self):
"""Wenn gateway_state.json nicht existiert, pgrep-Fallback greift."""
"""Wenn gateway_state.json nicht existiert, pgrep greift."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
# Mock pgrep: findet anderen Prozess
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n99999\n",
stderr="",
returncode=0,
)
# Mock _pgrep_hermes_subagents: findet 2 Prozesse
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=2):
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
)
result = dt.check()
# pgrep findet anderen Prozess → True
# pgrep findet 2 Prozesse → True
assert result is True
def test_pgrep_returns_bool(self):
"""pgrep liefert immer bool zurück."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
# Mock pgrep: findet nur eigenen Prozess
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n",
stderr="",
returncode=0,
)
# Mock _pgrep_hermes_subagents: 0 Prozesse
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=0):
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
@@ -1047,26 +1023,19 @@ class TestDetectionTimerPgrepNoGatewayState:
class TestDetectionTimerIntegration:
"""Test 22: DetectionTimer start/stop + Callback (P1b: gateway_state PRIMARY, pgrep SECONDARY)."""
"""Test 22: DetectionTimer start/stop + Callback (V8.7: pgrep PRIMARY)."""
def test_detection_timer_start_stop(self):
"""DetectionTimer startet und stoppt korrekt, pgrep-Fallback bei fehlendem gateway_state."""
"""DetectionTimer startet und stoppt korrekt, pgrep bei fehlendem gateway_state."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
callback_results = []
def on_detection_change(is_active):
callback_results.append(is_active)
# Mock pgrep: findet anderen Prozess → True (gateway_state fehlt → Fallback)
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n99999\n",
stderr="",
returncode=0,
)
# Mock _pgrep_hermes_subagents: findet 2 Prozesse → True
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=2):
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
@@ -1353,106 +1322,82 @@ class TestP0LiveVerdrahtung:
class TestDetectionTimerPgrepSelfExclude:
"""P1-2: DetectionTimer pgrep self-exclude — eigene PID wird ignoriert."""
"""V8.7: _pgrep_hermes_subagents() self-exclude — eigene PID wird ignoriert."""
def test_pgrep_self_exclude_own_pid(self):
"""pgrep findet nur den eigenen Prozess → check() returns False."""
"""_pgrep_hermes_subagents() findet nur eigenen Prozess → 0."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
# Mock subprocess.run: pgrep returns only our own PID
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run, \
# Mock _pgrep_hermes_subagents: 0 (eigene PID ausgeschlossen)
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=0), \
patch.object(DetectionTimer, '_check_gateway_pid_alive', return_value=False):
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n",
stderr="",
returncode=0,
)
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
plugin_state_path="/tmp/nonexistent_plugin_state_v8_7.json",
)
result = dt.check()
# Own PID should be excluded → no background agents → False
# Own PID excluded → no background agents → False
assert result is False
def test_pgrep_self_exclude_with_other_pids(self):
"""pgrep findet eigenen + fremden Prozess → check() returns True."""
"""_pgrep_hermes_subagents() findet 2 Prozesse → True."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
# Mock subprocess.run: pgrep returns own PID + another PID
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n99999\n",
stderr="",
returncode=0,
)
# Mock _pgrep_hermes_subagents: 2 (eigene PID + anderer)
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=2):
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
)
result = dt.check()
# After excluding own PID, one other PID remains → True
# 2 Prozesse gefunden → True
assert result is True
class TestDetectionTimerOnChangeCallback:
"""P1-2: DetectionTimer on_change callback feuert bei pgrep-Zustandswechsel."""
"""V8.7: DetectionTimer on_change callback feuert bei pgrep-Zustandswechsel."""
def test_on_change_callback_fires_on_state_change(self):
"""Mock pgrep: Prozesse erscheinen/verschwinden → on_change feuert."""
"""Mock _pgrep_hermes_subagents: Prozesse erscheinen/verschwinden → on_change feuert."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
callback_results = []
def on_change(is_active):
callback_results.append(is_active)
# Mock pgrep: zuerst nur eigener Prozess (inactive), dann anderer erscheint
mock_results = [
# Check 1: nur eigener Prozess → False
MagicMock(stdout=f"{own_pid}\n", stderr="", returncode=0),
# Check 2: anderer Prozess erscheint → True
MagicMock(stdout=f"{own_pid}\n99999\n", stderr="", returncode=0),
# Check 3: immer noch da → True (kein Callback)
MagicMock(stdout=f"{own_pid}\n99999\n", stderr="", returncode=0),
# Check 4: verschwindet → False
MagicMock(stdout=f"{own_pid}\n", stderr="", returncode=0),
]
# Mock _pgrep_hermes_subagents: 0 → 2 → 2 → 0
mock_counts = [0, 2, 2, 0]
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run, \
with patch.object(DetectionTimer, '_pgrep_hermes_subagents') as mock_pgrep, \
patch.object(DetectionTimer, '_check_gateway_pid_alive', return_value=False):
mock_run.side_effect = mock_results
mock_pgrep.side_effect = mock_counts
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
on_change=on_change,
plugin_state_path="/tmp/nonexistent_plugin_state_v8_7.json",
)
# Check 1: nur eigener Prozess → False (kein Callback, _last_result startet False)
# Check 1: 0 Prozesse → False (kein Callback, _last_result startet False)
result1 = dt.check()
assert result1 is False
assert len(callback_results) == 0
# Check 2: anderer Prozess erscheint → True (Callback: False→True)
# Check 2: 2 Prozesse → True (Callback: False→True)
result2 = dt.check()
assert result2 is True
assert len(callback_results) == 1
assert callback_results[0] is True
# Check 3: immer noch da → True (kein Callback, kein Wechsel)
# Check 3: immer noch 2 → True (kein Callback, kein Wechsel)
result3 = dt.check()
assert result3 is True
assert len(callback_results) == 1
# Check 4: verschwindet → False (Callback: True→False)
# Check 4: 0 → False (Callback: True→False)
result4 = dt.check()
assert result4 is False
assert len(callback_results) == 2
@@ -1616,6 +1561,9 @@ class TestHoneymoonBusyMinVisible:
engine = PresenceEngine(rest=mock_rest, enabled=True)
# V8.7: Mock _read_subagent_count → 0 (keine echten Prozesse)
engine._read_subagent_count = lambda: 0
# Start busy
engine.mark_busy_started(room_name="#Test")
assert engine._active_tasks == 1
@@ -1656,6 +1604,9 @@ class TestHoneymoonBusyMinVisible:
engine = PresenceEngine(rest=mock_rest, enabled=True)
# V8.7: Mock _read_subagent_count → 0
engine._read_subagent_count = lambda: 0
engine.mark_busy_started(room_name="#Test")
assert engine._active_tasks == 1
@@ -1682,6 +1633,9 @@ class TestHoneymoonBusyMinVisible:
engine = PresenceEngine(rest=mock_rest, enabled=True)
# V8.7: Mock _read_subagent_count → 0
engine._read_subagent_count = lambda: 0
# Erster busy
engine.mark_busy_started(room_name="#Test")
first_timer = engine._busy_honeymoon_timer
@@ -1744,6 +1698,9 @@ class TestHoneymoonBusyMinVisible:
engine = PresenceEngine(rest=mock_rest, enabled=True)
# V8.7: Mock _read_subagent_count → 0
engine._read_subagent_count = lambda: 0
try:
engine.mark_busy_started(room_name="#Test")
raise RuntimeError("simulated dispatch failure")
@@ -1775,6 +1732,9 @@ class TestHoneymoonBusyMinVisible:
engine = PresenceEngine(rest=mock_rest, enabled=True)
# V8.7: Mock _read_subagent_count → 0
engine._read_subagent_count = lambda: 0
engine.mark_busy_started(room_name="#Test")
time.sleep(0.05)
engine.mark_busy_done()
@@ -2089,41 +2049,29 @@ class TestDetectionTimerPluginState:
"schema_version": 1,
})
own_pid = os.getpid()
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run, \
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=0), \
patch.object(DetectionTimer, '_check_gateway_pid_alive', return_value=False):
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n",
stderr="",
returncode=0,
)
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
plugin_state_path=state_path,
)
result = dt.check()
# background_tasks=0, gateway_state fehlt, pgrep nur self → False
# background_tasks=0, gateway_state fehlt, pgrep=0 → False
assert result is False
def test_plugin_state_missing_file_no_error(self):
"""Plugin-State-Datei fehlt → kein Fehler, fällt zurück auf gateway_state+pgrep."""
"""Plugin-State-Datei fehlt → kein Fehler, fällt zurück auf pgrep."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n99999\n",
stderr="",
returncode=0,
)
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=2):
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
plugin_state_path="/tmp/nonexistent_plugin_state.json",
)
result = dt.check()
# pgrep findet anderen Prozess → True
# pgrep findet 2 Prozesse → True
assert result is True
def test_plugin_state_corrupt_json_no_crash(self, tmp_path):
@@ -2136,20 +2084,14 @@ class TestDetectionTimerPluginState:
with open(state_path, 'w') as f:
f.write("{invalid json {{{")
own_pid = os.getpid()
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n99999\n",
stderr="",
returncode=0,
)
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=2):
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
plugin_state_path=state_path,
)
result = dt.check()
# Sollte nicht crashen, pgrep findet anderen Prozess → True
# Sollte nicht crashen, pgrep findet 2 Prozesse → True
assert result is True
def test_plugin_state_priority_after_gateway(self, tmp_path):
@@ -2174,7 +2116,7 @@ class TestDetectionTimerPluginState:
"schema_version": 1,
})
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
with patch.object(DetectionTimer, '_pgrep_hermes_subagents') as mock_pgrep:
dt = DetectionTimer(
gateway_state_path=gw_path,
detection_interval=0.1,
@@ -2183,7 +2125,7 @@ class TestDetectionTimerPluginState:
result = dt.check()
# gateway_state=3 > 1 → PRIMARY → True (pgrep NICHT aufgerufen)
assert result is True
mock_run.assert_not_called()
mock_pgrep.assert_not_called()
finally:
os.unlink(gw_path)
@@ -2229,14 +2171,8 @@ class TestDetectionSourceTelemetry:
routing_telemetry.heartbeat_detection_source = "none"
own_pid = os.getpid()
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run, \
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=0), \
patch.object(DetectionTimer, '_check_gateway_pid_alive', return_value=False):
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n",
stderr="",
returncode=0,
)
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
@@ -2252,14 +2188,8 @@ class TestDetectionSourceTelemetry:
routing_telemetry.heartbeat_detection_source = "none"
own_pid = os.getpid()
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run, \
with patch.object(DetectionTimer, '_pgrep_hermes_subagents', return_value=2), \
patch.object(DetectionTimer, '_check_gateway_pid_alive', return_value=False):
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n99999\n",
stderr="",
returncode=0,
)
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
@@ -2282,7 +2212,7 @@ class TestDetectionSourceTelemetry:
gw_path = f.name
try:
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run, \
with patch.object(DetectionTimer, '_pgrep_hermes_subagents') as mock_pgrep, \
patch.object(DetectionTimer, '_check_gateway_pid_alive', return_value=False):
dt = DetectionTimer(
gateway_state_path=gw_path,
@@ -2290,7 +2220,7 @@ class TestDetectionSourceTelemetry:
)
dt.check()
assert routing_telemetry.heartbeat_detection_source == "gateway"
mock_run.assert_not_called()
mock_pgrep.assert_not_called()
finally:
os.unlink(gw_path)