V9.1: GELB-Logik — 3-State-Maschine (ONLINE/BUSY/AWAY)

- _determine_status() @staticmethod: subagent_active → AWAY (GELB)
- _apply_counter_delta() auf 3-State umgebaut (ONLINE/BUSY/AWAY)
- delegate_subagent, chat_profile, background_term → AWAY (GELB)
- main_agent only → BUSY (ROT), sum=0 → ONLINE (GRÜN)
- 10 neue GELB-Tests (TestGelbLogic) + bestehende Tests aktualisiert
- 49 passed, 82 skipped (V8.x legacy)
This commit is contained in:
Bernd (Hermes)
2026-08-04 17:47:12 +02:00
parent 3b945aa4c6
commit 1d31d2c372
2 changed files with 205 additions and 29 deletions
+63 -18
View File
@@ -49,8 +49,11 @@ class PresenceEngine:
"""V9: Ereignisgesteuerte Presence-Engine via Core-Hooks.
Invarianten:
I1: Status = ONLINE wenn sum(counters) == 0, sonst BUSY.
I2: Jede Transition ONLINE↔BUSY erzeugt GENAU EINEN REST-Call.
I1: Status = ONLINE wenn sum(counters) == 0.
Status = AWAY (GELB) wenn subagent_active (delegate_subagent > 0
OR chat_profile > 0 OR background_term > 0).
Status = BUSY (ROT) wenn nur main_agent > 0.
I2: Jede Transition ONLINE↔BUSY↔AWAY erzeugt GENAU EINEN REST-Call.
I3: REST-Calls sind fire-and-forget (Daemon-Threads), nie parallel.
I4: Bei REST-Fehler (nicht 429): ein Retry nach 2s, dann aufgeben.
Bei 429: kein Retry, Counter bleibt — nächster Hook behebt es.
@@ -100,7 +103,7 @@ class PresenceEngine:
}
# ── State-Maschine ────────────────────────────────────────────────
self._status: str = "ONLINE" # ONLINE | BUSY
self._status: str = "ONLINE" # ONLINE | BUSY | AWAY
# V9.0.1: threading.Lock statt asyncio.Lock — Hooks sind synchron,
# invoke_hook() ruft Callbacks im aufrufenden Thread auf.
self._transition_lock: threading.Lock = threading.Lock()
@@ -142,7 +145,7 @@ class PresenceEngine:
logger.info("[presence-v9] PresenceEngine disabled")
def get_status(self) -> str:
"""Liefere den aktuellen Status (ONLINE|BUSY)."""
"""Liefere den aktuellen Status (ONLINE|BUSY|AWAY)."""
return self._status
def get_counters(self) -> Dict[str, int]:
@@ -213,12 +216,45 @@ class PresenceEngine:
# ── Interne State-Maschine ───────────────────────────────────────────
@staticmethod
def _determine_status(counters: Dict[str, int]) -> tuple:
"""Bestimme Status und Message basierend auf Countern.
V9.1 GELB-Logik:
- sum(counters) == 0 → ONLINE (GRÜN)
- subagent_active (delegate_subagent > 0 OR chat_profile > 0
OR background_term > 0) → AWAY (GELB)
- nur main_agent > 0 → BUSY (ROT)
Args:
counters: Counter-Dict {name: value}.
Returns:
(status, message) — status: 'online'|'busy'|'away'.
"""
total = sum(counters.values())
if total == 0:
return "online", ""
# Subagent aktiv? (delegate_subagent, chat_profile, background_term)
subagent_active = (
counters.get("delegate_subagent", 0) > 0
or counters.get("chat_profile", 0) > 0
or counters.get("background_term", 0) > 0
)
if subagent_active:
return "away", "beschaeftigt — Subagent aktiv"
return "busy", "beschaeftigt — Hauptagent arbeitet"
def _apply_counter_delta(self, counter_name: str, delta: int) -> None:
"""Atomar: Counter mutieren, Summe prüfen, ggf. Transition auslösen.
V9.0.1: Synchron unter threading.Lock. invoke_hook() ruft Callbacks
synchron auf — kein asyncio-Kontext verfügbar.
V9.1: 3-State-Maschine (ONLINE/BUSY/AWAY) mit GELB-Logik.
Args:
counter_name: Schlüssel im counters-Dict.
delta: +1 oder -1.
@@ -244,18 +280,27 @@ class PresenceEngine:
counter_name, old_value, new_value, total, self._status,
)
# ── Transition prüfen ─────────────────────────────────────────
if total > 0 and self._status == "ONLINE":
# ONLINE → BUSY
self._status = "BUSY"
# away-grace canceln (falls aktiv)
self._cancel_away_grace()
# Transition sofort auslösen (fire-and-forget)
self._schedule_transition("busy", self._build_busy_message())
elif total == 0 and self._status == "BUSY":
# BUSY → ONLINE (mit away-grace)
self._status = "ONLINE"
self._schedule_away_grace()
# ── Transition prüfen (3-State: ONLINE/BUSY/AWAY) ─────────────
new_status, new_message = self._determine_status(self.counters)
rc_status = new_status # 'online'|'busy'|'away'
if total == 0:
# → ONLINE (mit away-grace)
if self._status != "ONLINE":
self._status = "ONLINE"
self._schedule_away_grace()
elif rc_status == "away":
# → AWAY (GELB) — Subagent aktiv
if self._status != "AWAY":
self._status = "AWAY"
self._cancel_away_grace()
self._schedule_transition("away", new_message)
else:
# → BUSY (ROT) — nur main_agent
if self._status != "BUSY":
self._status = "BUSY"
self._cancel_away_grace()
self._schedule_transition("busy", new_message)
def _build_busy_message(self) -> str:
"""Baue Status-Message für BUSY basierend auf aktiven Countern.
@@ -293,7 +338,7 @@ class PresenceEngine:
finalen Status.
Args:
status: 'online' oder 'busy'.
status: 'online', 'busy', oder 'away'.
message: Status-Message-Text.
"""
# Caching: überspringe wenn identisch zum letzten gesendeten
@@ -323,7 +368,7 @@ class PresenceEngine:
Kein asyncio — urllib.request ist synchron.
Args:
status: 'online' oder 'busy'.
status: 'online', 'busy', oder 'away'.
message: Status-Message-Text.
"""
success = self._rest_set_status(status, message)