fix(P1b): DetectionTimer pgrep-Pattern erweitert + gateway_state als Primärsignal

This commit is contained in:
Bernd (Hermes)
2026-07-24 09:06:40 +02:00
parent f61b0ba508
commit f028a30d6c
3 changed files with 94 additions and 59 deletions
+23 -25
View File
@@ -880,17 +880,17 @@ class TestPulseControllerLastSentPhase:
class TestDetectionTimerPgrepPrimary:
"""Test 20: DetectionTimer — pgrep als Primärsignal, gateway_state nur diagnostisch."""
"""Test 20: DetectionTimer — gateway_state PRIMARY, pgrep SECONDARY (P1b)."""
def test_pgrep_finds_other_agents(self):
"""pgrep findet andere hermes-Prozesse → result=True (gateway_state irrelevant)."""
"""gateway_state=1 (nicht >1) → pgrep-Fallback 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 (würde früher False liefern)
# 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:
json.dump(state, f)
@@ -910,50 +910,44 @@ class TestDetectionTimerPgrepPrimary:
detection_interval=0.1,
)
result = dt.check()
# pgrep findet anderen Prozess → True (gateway_state=1 wird ignoriert)
# gateway_state=1 → pgrep-Fallback findet anderen Prozess → True
assert result is True
finally:
os.unlink(state_path)
def test_pgrep_finds_no_other_agents(self):
"""pgrep findet nur eigenen Prozess → result=False (gateway_state irrelevant)."""
def test_gateway_state_primary_active(self):
"""P1b: gateway_state active_agents=3 (>1) → PRIMARY entscheidet → result=True, pgrep NICHT aufgerufen."""
from plugin.archivar.presence_engine import DetectionTimer
import tempfile
import json
own_pid = os.getpid()
# gateway_state mit active_agents=3 (würde früher True liefern)
# gateway_state mit active_agents=3 (>1 → PRIMARY entscheidet)
state = {"active_agents": 3, "profiles": ["default", "miguel", "anna"]}
with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="w") as f:
json.dump(state, f)
state_path = f.name
try:
# Mock pgrep: findet nur eigenen Prozess
# Mock pgrep: sollte NICHT aufgerufen werden
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
mock_run.return_value = MagicMock(
stdout=f"{own_pid}\n",
stderr="",
returncode=0,
)
dt = DetectionTimer(
gateway_state_path=state_path,
detection_interval=0.1,
)
result = dt.check()
# pgrep findet nur eigenen Prozess → False (gateway_state=3 wird ignoriert)
assert result is False
# gateway_state=3 > 1 → PRIMARY entscheidet → True
assert result is True
# pgrep wurde NICHT aufgerufen (gateway_state hat entschieden)
mock_run.assert_not_called()
finally:
os.unlink(state_path)
class TestDetectionTimerPgrepNoGatewayState:
"""Test 21: DetectionTimer pgrep funktioniert auch ohne gateway_state.json."""
"""Test 21: DetectionTimer pgrep-Fallback funktioniert auch ohne gateway_state.json (P1b)."""
def test_pgrep_works_without_gateway_state(self):
"""Wenn gateway_state.json nicht existiert, pgrep ist trotzdem Primärsignal."""
"""Wenn gateway_state.json nicht existiert, pgrep-Fallback greift."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
@@ -997,10 +991,10 @@ class TestDetectionTimerPgrepNoGatewayState:
class TestDetectionTimerIntegration:
"""Test 22: DetectionTimer start/stop + Callback (pgrep als Primärsignal)."""
"""Test 22: DetectionTimer start/stop + Callback (P1b: gateway_state PRIMARY, pgrep SECONDARY)."""
def test_detection_timer_start_stop(self):
"""DetectionTimer startet und stoppt korrekt, pgrep als Primärsignal."""
"""DetectionTimer startet und stoppt korrekt, pgrep-Fallback bei fehlendem gateway_state."""
from plugin.archivar.presence_engine import DetectionTimer
own_pid = os.getpid()
@@ -1009,7 +1003,7 @@ class TestDetectionTimerIntegration:
def on_detection_change(is_active):
callback_results.append(is_active)
# Mock pgrep: findet anderen Prozess → True
# 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",
@@ -1264,7 +1258,7 @@ class TestP0LiveVerdrahtung:
assert not engine._pulse_controller.is_running()
def test_detection_timer_starts_when_enabled(self):
"""P0-5: DetectionTimer startet automatisch wenn enabled=True."""
"""P1b: DetectionTimer startet NICHT automatisch — muss via start_detection() gestartet werden."""
from plugin.archivar.presence_engine import PresenceEngine
mock_rest = MagicMock()
@@ -1273,7 +1267,11 @@ class TestP0LiveVerdrahtung:
engine = PresenceEngine(rest=mock_rest, enabled=True)
# DetectionTimer läuft (wurde in __init__ gestartet)
# P1b: DetectionTimer läuft NICHT nach __init__ (startet erst via start_detection())
assert not engine._detection_timer._running.is_set()
# start_detection() startet ihn
engine.start_detection()
assert engine._detection_timer._running.is_set()
def test_detection_timer_not_started_when_disabled(self):