fix(P-032): V10.0.2 — Hook-Spec-Mismatch Core↔Plugin (Rebase auf V10.0.1 main)

P-032 Fix manuell auf V10.0.1 (main, 46325ce) angewendet:

presence_engine.py (V10 Mediator-API):
- on_session_end → NO-OP (feuert PRO TURN, nicht pro Session)
- on_session_finalize → chat_profile -= 1 (echtes Session-Ende)
- on_session_reset → alle Counter auf 0 via Mediator.on_phase_transition()
- on_post_llm_call: Docstring mit P-032 Clamp-Erklaerung
- _apply_counter_delta: Clamp-Warnung auf debug-Level (P-032 erwartet)

__init__.py:
- 2 neue Hooks registriert: on_session_finalize, on_session_reset
- Hook-Mapping-Doku aktualisiert (8→10 Hooks)
- Log-Tag [presence-v9]→[presence-v10]

Tests (test_presence_engine.py):
- test_session_end_is_noop (ersetzt test_session_start_end_balanced)
- test_session_finalize_decrements_chat_profile
- test_session_reset_zeros_all_counters
- test_session_reset_disabled_engine_noop

84 passed, 0 skipped, 0 failed
This commit is contained in:
Bernd (Hermes)
2026-08-05 16:45:15 +02:00
parent 46325ce805
commit bd6db80017
3 changed files with 108 additions and 13 deletions
+19 -6
View File
@@ -164,13 +164,15 @@ def register(ctx):
# Der Adapter ist vollständig initialisiert, REST-Login erfolgt, # Der Adapter ist vollständig initialisiert, REST-Login erfolgt,
# PresenceEngine ist gebunden, und der asyncio-Loop läuft. # PresenceEngine ist gebunden, und der asyncio-Loop läuft.
# #
# Hook-Mapping (siehe docs/architecture/2026-08-03-presence-engine-schachuhr.md): # Hook-Mapping (V10.0.2 — P-032 Fix, siehe docs/architecture/2026-08-03-presence-engine-schachuhr.md):
# pre_gateway_dispatch → main_agent += 1 (ROT Start) # pre_gateway_dispatch → main_agent += 1 (ROT Start)
# post_llm_call → main_agent -= 1 (ROT Ende) # post_llm_call → main_agent -= 1 (ROT Ende, Clamp bei 0)
# subagent_start → delegate_subagent += 1 (GELB Start) # subagent_start → delegate_subagent += 1 (GELB Start)
# subagent_stop → delegate_subagent -= 1 (GELB Ende) # subagent_stop → delegate_subagent -= 1 (GELB Ende)
# on_session_start → chat_profile += 1 (GELB Start) # on_session_start → chat_profile += 1 (GELB Start, echter Session-Start)
# on_session_end → chat_profile -= 1 (GELB Ende) # on_session_end → NO-OP (feuert PRO TURN, nicht pro Session)
# on_session_finalize → chat_profile -= 1 (GELB Ende, echtes Session-Ende)
# on_session_reset → alle Counter auf 0 (/reset)
# pre_tool_call → background_term += 1 (wenn terminal bg=true) # pre_tool_call → background_term += 1 (wenn terminal bg=true)
# post_tool_call → background_term -= 1 (wenn terminal bg=true) # post_tool_call → background_term -= 1 (wenn terminal bg=true)
# #
@@ -241,8 +243,16 @@ def _register_presence_hooks() -> None:
_get_engine_and_call("on_session_start", **kwargs) _get_engine_and_call("on_session_start", **kwargs)
def _on_session_end(**kwargs): def _on_session_end(**kwargs):
# P-032: on_session_end feuert PRO TURN — No-Op in der Engine.
# chat_profile-Dekrement erfolgt via on_session_finalize/on_session_reset.
_get_engine_and_call("on_session_end", **kwargs) _get_engine_and_call("on_session_end", **kwargs)
def _on_session_finalize(**kwargs):
_get_engine_and_call("on_session_finalize", **kwargs)
def _on_session_reset(**kwargs):
_get_engine_and_call("on_session_reset", **kwargs)
# ── 2 Tool-Filter-Hooks (O-1: Background-Terminal-Workaround) ───── # ── 2 Tool-Filter-Hooks (O-1: Background-Terminal-Workaround) ─────
def _on_pre_tool_call(**kwargs): def _on_pre_tool_call(**kwargs):
_get_engine_and_call("on_pre_tool_call", **kwargs) _get_engine_and_call("on_pre_tool_call", **kwargs)
@@ -257,14 +267,17 @@ def _register_presence_hooks() -> None:
ctx.register_hook("subagent_stop", _on_subagent_stop) ctx.register_hook("subagent_stop", _on_subagent_stop)
ctx.register_hook("on_session_start", _on_session_start) ctx.register_hook("on_session_start", _on_session_start)
ctx.register_hook("on_session_end", _on_session_end) ctx.register_hook("on_session_end", _on_session_end)
ctx.register_hook("on_session_finalize", _on_session_finalize)
ctx.register_hook("on_session_reset", _on_session_reset)
ctx.register_hook("pre_tool_call", _on_pre_tool_call) ctx.register_hook("pre_tool_call", _on_pre_tool_call)
ctx.register_hook("post_tool_call", _on_post_tool_call) ctx.register_hook("post_tool_call", _on_post_tool_call)
_hooks_registered = True _hooks_registered = True
logger.info( logger.info(
"[presence-v9] 8 Hooks registriert: " "[presence-v10] 10 Hooks registriert: "
"pre_gateway_dispatch, post_llm_call, subagent_start, subagent_stop, " "pre_gateway_dispatch, post_llm_call, subagent_start, subagent_stop, "
"on_session_start, on_session_end, pre_tool_call, post_tool_call" "on_session_start, on_session_end, on_session_finalize, on_session_reset, "
"pre_tool_call, post_tool_call"
) )
except Exception as e: except Exception as e:
logger.warning( logger.warning(
+47 -4
View File
@@ -662,7 +662,13 @@ class PresenceEngine:
self._apply_counter_delta("main_agent", +1) self._apply_counter_delta("main_agent", +1)
def on_post_llm_call(self, **kwargs) -> None: def on_post_llm_call(self, **kwargs) -> None:
"""Hook: post_llm_call → main_agent -= 1 (ROT Ende).""" """Hook: post_llm_call → main_agent -= 1 (ROT Ende).
P-032 Fix: _apply_counter_delta klemmt main_agent bei 0.
Clarify-Button-Antworten und interne Events umgehen pre_gateway_dispatch,
sodass post_llm_call ohne matching pre_gateway_dispatch feuert.
Der Clamp in _apply_counter_delta verhindert negatives main_agent.
"""
self._apply_counter_delta("main_agent", -1) self._apply_counter_delta("main_agent", -1)
def on_subagent_start(self, **kwargs) -> None: def on_subagent_start(self, **kwargs) -> None:
@@ -678,9 +684,43 @@ class PresenceEngine:
self._apply_counter_delta("chat_profile", +1) self._apply_counter_delta("chat_profile", +1)
def on_session_end(self, **kwargs) -> None: def on_session_end(self, **kwargs) -> None:
"""Hook: on_session_end → chat_profile -= 1 (GELB Ende).""" """Hook: on_session_end → NO-OP (P-032 Fix).
Der Core feuert on_session_end PRO TURN, nicht pro Session.
chat_profile-Dekrement erfolgt stattdessen in on_session_finalize
(echtes Session-Ende) und on_session_reset (/reset).
"""
pass # P-032: on_session_end feuert pro Turn — nicht für chat_profile nutzen
def on_session_finalize(self, **kwargs) -> None:
"""Hook: on_session_finalize → chat_profile -= 1 (echtes Session-Ende).
P-032 Fix: on_session_finalize feuert NUR beim echten Session-Ende,
nicht pro Turn wie on_session_end.
"""
self._apply_counter_delta("chat_profile", -1) self._apply_counter_delta("chat_profile", -1)
def on_session_reset(self, **kwargs) -> None:
"""Hook: on_session_reset → alle Counter auf 0 (P-032 Fix).
/reset oder interner Reset: alle Counter zurücksetzen,
da die Session komplett neu startet.
V10: Nutzt Mediator.on_phase_transition() statt direkter Lock-Methoden.
"""
if not self._enabled:
return
with self._transition_lock:
for key in self.counters:
self.counters[key] = 0
self._status = "ONLINE"
old_phase = self._mediator.current_phase
new_phase = Phase.GREEN_STATIC
if old_phase != new_phase:
self._mediator.on_phase_transition(old_phase, new_phase)
logger.info(
"[presence-v10] on_session_reset: alle Counter auf 0, Status=ONLINE"
)
def on_pre_tool_call(self, **kwargs) -> None: def on_pre_tool_call(self, **kwargs) -> None:
"""Hook: pre_tool_call → background_term += 1 wenn terminal(background=true).""" """Hook: pre_tool_call → background_term += 1 wenn terminal(background=true)."""
tool_name = kwargs.get("tool_name", "") tool_name = kwargs.get("tool_name", "")
@@ -715,8 +755,11 @@ class PresenceEngine:
old_value = self.counters[counter_name] old_value = self.counters[counter_name]
new_value = old_value + delta new_value = old_value + delta
if new_value < 0: if new_value < 0:
logger.warning( # P-032: Clamp bei 0 ist erwartet — Clarify-Loops und interne
"[presence-v10] Counter %s würde negativ (%d→%d) — auf 0 geklemmt", # Events umgehen pre_gateway_dispatch, sodass post_llm_call
# ohne matching pre_gateway_dispatch feuert.
logger.debug(
"[presence-v10] Counter %s würde negativ (%d→%d) — auf 0 geklemmt (P-032)",
counter_name, old_value, new_value, counter_name, old_value, new_value,
) )
new_value = 0 new_value = 0
+42 -3
View File
@@ -466,7 +466,7 @@ class TestHookSubagentStartStop:
class TestHookSessionStartEnd: class TestHookSessionStartEnd:
"""on_session_start/end → chat_profile += 1 / -= 1 (GELB).""" """on_session_start/end/finalize/reset → chat_profile (P-032 Fix)."""
def test_session_start_increments_chat_profile(self): def test_session_start_increments_chat_profile(self):
engine = _make_engine() engine = _make_engine()
@@ -475,15 +475,54 @@ class TestHookSessionStartEnd:
assert counters["chat_profile"] == 1 assert counters["chat_profile"] == 1
assert engine.get_current_phase().name == "GREEN_PULSE" assert engine.get_current_phase().name == "GREEN_PULSE"
def test_session_start_end_balanced(self): def test_session_end_is_noop(self):
"""P-032: on_session_end feuert PRO TURN — No-Op, chat_profile unverändert."""
engine = _make_engine() engine = _make_engine()
engine.on_session_start() engine.on_session_start()
engine.on_session_end() engine.on_session_end() # No-Op
counters = engine.get_counters()
assert counters["chat_profile"] == 1 # unverändert
assert engine.get_current_phase().name == "GREEN_PULSE"
def test_session_finalize_decrements_chat_profile(self):
"""P-032: on_session_finalize (echtes Session-Ende) → chat_profile -= 1."""
engine = _make_engine()
engine.on_session_start()
engine.on_session_finalize()
time.sleep(engine._away_grace_s + 0.05) time.sleep(engine._away_grace_s + 0.05)
counters = engine.get_counters() counters = engine.get_counters()
assert counters["chat_profile"] == 0 assert counters["chat_profile"] == 0
assert engine.get_status() == "ONLINE" assert engine.get_status() == "ONLINE"
def test_session_reset_zeros_all_counters(self):
"""P-032: on_session_reset → alle Counter auf 0, Status ONLINE."""
engine = _make_engine()
engine.on_session_start()
engine.on_pre_gateway_dispatch()
engine.on_subagent_start()
# Alle Counter > 0
assert engine.get_counters()["chat_profile"] == 1
assert engine.get_counters()["main_agent"] == 1
assert engine.get_counters()["delegate_subagent"] == 1
assert engine.get_status() == "BUSY"
# Reset
engine.on_session_reset()
counters = engine.get_counters()
assert counters["chat_profile"] == 0
assert counters["main_agent"] == 0
assert counters["delegate_subagent"] == 0
assert counters["background_term"] == 0
assert engine.get_status() == "ONLINE"
def test_session_reset_disabled_engine_noop(self):
"""P-032: on_session_reset bei deaktivierter Engine → No-Op."""
engine = _make_engine(enabled=False)
engine.on_session_start()
engine.on_session_reset()
counters = engine.get_counters()
assert counters["chat_profile"] == 0 # disabled → kein Inkrement
assert engine.get_status() == "ONLINE"
class TestHookBackgroundTerminal: class TestHookBackgroundTerminal:
"""pre/post_tool_call → background_term += 1 / -= 1 (O-1 Workaround).""" """pre/post_tool_call → background_term += 1 / -= 1 (O-1 Workaround)."""