feat(p1-2): Telemetrie — heartbeat_detection_source Feld (gateway|plugin_state|pgrep|none)

This commit is contained in:
Bernd (Hermes)
2026-07-26 05:59:46 +02:00
parent 998ed2cedd
commit 0f6821fa9c
2 changed files with 110 additions and 1 deletions
+7 -1
View File
@@ -1270,7 +1270,13 @@ class DetectionTimer:
source: One of 'gateway', 'plugin_state', 'pgrep', 'none'. source: One of 'gateway', 'plugin_state', 'pgrep', 'none'.
""" """
try: try:
from archivar.routing_telemetry import routing_telemetry from plugin.archivar.routing_telemetry import routing_telemetry
except ImportError:
try:
from archivar.routing_telemetry import routing_telemetry
except ImportError:
return
try:
routing_telemetry.heartbeat_detection_source = source routing_telemetry.heartbeat_detection_source = source
except Exception: except Exception:
pass pass
@@ -2049,3 +2049,106 @@ class TestDetectionTimerPluginState:
mock_run.assert_not_called() mock_run.assert_not_called()
finally: finally:
os.unlink(gw_path) os.unlink(gw_path)
# ═══════════════════════════════════════════════════════════════════════════
# P1-2: Telemetrie — heartbeat_detection_source Feld
# ═══════════════════════════════════════════════════════════════════════════
class TestDetectionSourceTelemetry:
"""P1-2: heartbeat_detection_source Telemetrie-Feld."""
def test_detection_source_set_on_check_plugin_state(self, tmp_path):
"""Nach check() mit plugin_state → source='plugin_state'."""
from plugin.archivar.presence_engine import DetectionTimer, _write_plugin_state
from plugin.archivar.routing_telemetry import routing_telemetry
# Reset
routing_telemetry.heartbeat_detection_source = "none"
state_path = str(tmp_path / "hermes_rocket_plugin.json")
os.makedirs(os.path.dirname(state_path), exist_ok=True)
_write_plugin_state(state_path, {
"background_tasks": 3,
"last_update_iso": "2026-01-01T00:00:00",
"source": "plugin_internal",
"schema_version": 1,
})
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
plugin_state_path=state_path,
)
dt.check()
assert routing_telemetry.heartbeat_detection_source == "plugin_state"
def test_detection_source_none_when_inactive(self):
"""Wenn nichts gefunden → source='none'."""
from plugin.archivar.presence_engine import DetectionTimer
from plugin.archivar.routing_telemetry import routing_telemetry
routing_telemetry.heartbeat_detection_source = "none"
own_pid = os.getpid()
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="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
plugin_state_path="/tmp/nonexistent_plugin_state.json",
)
dt.check()
assert routing_telemetry.heartbeat_detection_source == "none"
def test_detection_source_pgrep(self):
"""pgrep findet Agenten → source='pgrep'."""
from plugin.archivar.presence_engine import DetectionTimer
from plugin.archivar.routing_telemetry import routing_telemetry
routing_telemetry.heartbeat_detection_source = "none"
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,
)
dt = DetectionTimer(
gateway_state_path="/tmp/nonexistent_gateway_state.json",
detection_interval=0.1,
plugin_state_path="/tmp/nonexistent_plugin_state.json",
)
dt.check()
assert routing_telemetry.heartbeat_detection_source == "pgrep"
def test_detection_source_gateway(self, tmp_path):
"""gateway_state entscheidet → source='gateway'."""
from plugin.archivar.presence_engine import DetectionTimer
from plugin.archivar.routing_telemetry import routing_telemetry
import tempfile, json
routing_telemetry.heartbeat_detection_source = "none"
state = {"active_agents": 3, "profiles": ["default", "miguel", "anna"]}
with tempfile.NamedTemporaryFile(suffix=".json", delete=False, mode="w") as f:
json.dump(state, f)
gw_path = f.name
try:
with patch('plugin.archivar.presence_engine.subprocess.run') as mock_run:
dt = DetectionTimer(
gateway_state_path=gw_path,
detection_interval=0.1,
)
dt.check()
assert routing_telemetry.heartbeat_detection_source == "gateway"
mock_run.assert_not_called()
finally:
os.unlink(gw_path)