From 9a085dc4941faa0849c1ef643987acaaa0111f09 Mon Sep 17 00:00:00 2001 From: "Bernd (Hermes)" Date: Tue, 4 Aug 2026 20:54:53 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20V9.2.2=20=E2=80=94=20False-Positive=20Pu?= =?UTF-8?q?ls-Bug:=20Puls-Timer=20nur=20bei=20delegate=5Fsubagent=20>=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root Cause: _apply_counter_delta prüfte subagent_active über alle drei Counter (delegate_subagent, chat_profile, background_term). on_session_start feuert bei JEDEM Turn und inkrementiert chat_profile → Puls startete ohne echten Subagenten. Fix: - Puls-Trigger (pulse_counters) prüft NUR delegate_subagent - chat_profile und background_term zeigen weiterhin AWAY (statisch, ohne Puls) - _on_pulse_tick und _on_circuit_breaker_expired ebenfalls auf delegate_subagent-only umgestellt Tests (5 neue): - test_no_pulse_on_session_start: on_session_start → kein Puls - test_no_pulse_on_background_term: terminal(bg=true) → kein Puls - test_pulse_only_on_delegate_subagent: nur delegate_subagent triggert Puls - test_pulse_stops_when_delegate_done_others_stay: delegate→0 stoppt Puls trotz chat_profile>0 - test_chat_profile_away_no_pulse: chat_profile → AWAY statisch, kein Puls Alle 77 V9-Tests PASSED, keine Regression. --- plugin/archivar/presence_engine.py | 43 +++++----- plugin/archivar/tests/test_presence_engine.py | 85 +++++++++++++++++++ 2 files changed, 107 insertions(+), 21 deletions(-) diff --git a/plugin/archivar/presence_engine.py b/plugin/archivar/presence_engine.py index 5efcf94..48bb7a4 100644 --- a/plugin/archivar/presence_engine.py +++ b/plugin/archivar/presence_engine.py @@ -289,10 +289,13 @@ class PresenceEngine: return with self._transition_lock: - # ── V9.2: Subagent-Aktivität VOR Mutation prüfen ───────────── - subagent_counters = ["delegate_subagent", "chat_profile", "background_term"] - subagent_active_before = any( - self.counters[c] > 0 for c in subagent_counters + # ── V9.2.2: Puls-Trigger VOR Mutation prüfen ───────────────── + # Der Puls-Timer startet NUR bei delegate_subagent > 0. + # chat_profile und background_term zeigen AWAY (statisch, ohne Puls), + # weil sie keine echten Subagenten sind (False-Positive Bug V9.2.2). + pulse_counters = ["delegate_subagent"] + pulse_active_before = any( + self.counters[c] > 0 for c in pulse_counters ) # ── Counter mutieren ───────────────────────────────────────── @@ -312,21 +315,21 @@ class PresenceEngine: counter_name, old_value, new_value, total, self._status, ) - # ── V9.2: Subagent-Aktivität NACH Mutation prüfen ──────────── - subagent_active_now = any( - self.counters[c] > 0 for c in subagent_counters + # ── V9.2.2: Puls-Trigger NACH Mutation prüfen ───────────────── + pulse_active_now = any( + self.counters[c] > 0 for c in pulse_counters ) - if subagent_active_now and not subagent_active_before: - # Subagent wurde gerade aktiv → Puls starten + if pulse_active_now and not pulse_active_before: + # delegate_subagent wurde gerade aktiv → Puls starten self._start_pulse() # Initialen AWAY-Status setzen (der Puls-Timer übernimmt dann) self._status = "AWAY" self._cancel_away_grace() self._schedule_transition("away", "beschaeftigt — Subagent aktiv") return - elif not subagent_active_now and subagent_active_before: - # Subagent wurde gerade inaktiv → Puls stoppen + elif not pulse_active_now and pulse_active_before: + # delegate_subagent wurde gerade inaktiv → Puls stoppen self._cancel_pulse() # ── Transition prüfen (3-State: ONLINE/BUSY/AWAY) ───────────── @@ -542,13 +545,12 @@ class PresenceEngine: return with self._transition_lock: - # Prüfe ob Subagent immer noch aktiv ist - subagent_active = ( + # V9.2.2: Puls stoppt nur wenn delegate_subagent == 0. + # chat_profile und background_term triggern keinen Puls (False-Positive Fix). + pulse_active = ( self.counters.get("delegate_subagent", 0) > 0 - or self.counters.get("chat_profile", 0) > 0 - or self.counters.get("background_term", 0) > 0 ) - if not subagent_active: + if not pulse_active: # Subagent ist fertig — Puls beenden self._pulse_active = False self._pulse_timer = None @@ -662,13 +664,12 @@ class PresenceEngine: self._circuit_breaker_paused = False self._circuit_breaker_timer = None - # Puls resumen wenn Subagent immer noch aktiv - subagent_active = ( + # V9.2.2: Puls resumen nur wenn delegate_subagent > 0. + # chat_profile und background_term triggern keinen Puls (False-Positive Fix). + pulse_active = ( self.counters.get("delegate_subagent", 0) > 0 - or self.counters.get("chat_profile", 0) > 0 - or self.counters.get("background_term", 0) > 0 ) - if subagent_active and self._pulse_active: + if pulse_active and self._pulse_active: self._schedule_pulse_tick() def _cancel_circuit_breaker(self) -> None: diff --git a/plugin/archivar/tests/test_presence_engine.py b/plugin/archivar/tests/test_presence_engine.py index 2290671..eae8db2 100644 --- a/plugin/archivar/tests/test_presence_engine.py +++ b/plugin/archivar/tests/test_presence_engine.py @@ -756,6 +756,91 @@ class TestPulseRotGelb: assert not engine.is_pulse_active() assert engine._pulse_timer is None + # ── V9.2.2: False-Positive Puls-Bug Tests ────────────────────────── + + def test_no_pulse_on_session_start(self): + """V9.2.2: on_session_start feuert → KEIN Puls (chat_profile ist kein Subagent).""" + engine = _make_engine() + assert not engine.is_pulse_active() + + engine.on_session_start() + counters = engine.get_counters() + assert counters["chat_profile"] == 1 + assert engine.get_status() == "AWAY" # statisches AWAY, kein Puls + assert not engine.is_pulse_active() # ← DER FIX: kein Puls! + + def test_no_pulse_on_background_term(self): + """V9.2.2: terminal(background=true) → KEIN Puls (background_term ist kein Subagent).""" + engine = _make_engine() + assert not engine.is_pulse_active() + + engine.on_pre_tool_call( + tool_name="terminal", + args={"background": True, "command": "sleep 10"}, + ) + counters = engine.get_counters() + assert counters["background_term"] == 1 + assert engine.get_status() == "AWAY" # statisches AWAY, kein Puls + assert not engine.is_pulse_active() # ← DER FIX: kein Puls! + + def test_pulse_only_on_delegate_subagent(self): + """V9.2.2: Nur delegate_subagent > 0 startet den Puls.""" + engine = _make_engine() + assert not engine.is_pulse_active() + + # chat_profile allein → kein Puls + engine.on_session_start() + assert not engine.is_pulse_active() + + # background_term allein → kein Puls + engine.on_pre_tool_call( + tool_name="terminal", + args={"background": True, "command": "sleep 10"}, + ) + assert not engine.is_pulse_active() + + # delegate_subagent → Puls startet + engine.on_subagent_start() + assert engine.is_pulse_active() + + def test_pulse_stops_when_delegate_done_others_stay(self): + """V9.2.2: delegate_subagent → 0 stoppt Puls, auch wenn chat_profile/background_term > 0.""" + engine = _make_engine() + engine.on_subagent_start() + assert engine.is_pulse_active() + + # chat_profile und background_term werden aktiv — Puls läuft weiter + engine.on_session_start() + engine.on_pre_tool_call( + tool_name="terminal", + args={"background": True, "command": "sleep 10"}, + ) + assert engine.is_pulse_active() + + # delegate_subagent stoppt → Puls muss stoppen, obwohl andere Counter > 0 + engine.on_subagent_stop() + assert not engine.is_pulse_active() # ← DER FIX: Puls stoppt! + + # chat_profile und background_term sind noch aktiv → AWAY (statisch) + assert engine.get_status() == "AWAY" + + def test_chat_profile_away_no_pulse(self): + """V9.2.2: chat_profile > 0 → AWAY (statisch), kein Puls.""" + engine = _make_engine() + engine.on_session_start() + assert engine.get_status() == "AWAY" + assert not engine.is_pulse_active() + + # main_agent wird aktiv → bleibt AWAY (chat_profile hat Vorrang), kein Puls + engine.on_pre_gateway_dispatch() + assert engine.get_status() == "AWAY" # chat_profile > 0 → AWAY + assert not engine.is_pulse_active() + + # main_agent fertig → immer noch AWAY (chat_profile noch aktiv) + engine.on_post_llm_call() + assert engine.get_status() == "AWAY" + assert not engine.is_pulse_active() + # ═══════════════════════════════════════════════════════════════════════════ # V9.2.1 Tests — 429-Circuit-Breaker + 5s-Intervall + REST-Throttling