"""Diagnose-Dekorator für den Debug-Modus (COINPOKER_DEBUG=1). Hängt an jede MCP-Antwort den Rohzustand (Log-Zeilen, Fenster, Ports, letzte Entscheidung) an. """ import json import os # client/ ist die Naht (Adresse und Sockets): unabhaengig vom Aufrufer auffindbar import sys _wurzel = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) for _u in ("client", "hand"): _c = os.path.join(_wurzel, _u) if os.path.isdir(_c) and _c not in sys.path: sys.path.append(_c) import umgebung _w = os.path.dirname(os.path.abspath(__file__)) while _w != "/" and not os.path.isdir(os.path.join(_w, "lesen")): _w = os.path.dirname(_w) WURZEL = _w LOG_PATH = umgebung.log() SHADOW_PATH = os.path.join(WURZEL, "betrieb", "scratch", "shadow.jsonl") def mit_debug(text, fenster_liste=None, port_ist_offen=None, pids=None): """Hängt bei COINPOKER_DEBUG=1 Diagnose-Rohdaten an die Werkzeug-Ausgabe an.""" if os.environ.get("COINPOKER_DEBUG") != "1": return text from diagnose import fenster_alle_gecacht, port_offen, prozesse_laufen if pids is None: pids = prozesse_laufen() if port_ist_offen is None: port_ist_offen = port_offen() if fenster_liste is None: fenster_liste = fenster_alle_gecacht() letzte_zeile = "" if os.path.exists(LOG_PATH): try: with open(LOG_PATH, "r", errors="replace") as f: f.seek(max(0, os.path.getsize(LOG_PATH) - 1000)) zeilen = [z.strip() for z in f.readlines() if z.strip()] if zeilen: letzte_zeile = zeilen[-1][:120] except Exception: pass letzte_entscheidung = "" if os.path.exists(SHADOW_PATH): try: with open(SHADOW_PATH, "r", errors="replace") as f: zeilen = [z.strip() for z in f.readlines() if z.strip()] if zeilen: d = json.loads(zeilen[-1]) letzte_entscheidung = ( f"{d.get('zeit')} {d.get('kurz')} -> {d.get('antwort', {}).get('action')}" ) except Exception: pass debug_block = ( "\n\n--- DEBUG ---\n" f"Prozesse: {pids}\n" f"Debug-Port 9333: {'offen' if port_ist_offen else 'geschlossen'}\n" f"Fenster ({len(fenster_liste)}): {[(f['pid'], f['titel'][:20], f['tischfenster']) for f in fenster_liste]}\n" f"Letzte Logzeile: {letzte_zeile or 'keine'}\n" f"Letzte Entscheidung: {letzte_entscheidung or 'keine'}" ) return text + debug_block