#!/usr/bin/env python3 """Evaluate pending Engrams and set correctness verdict automatically.""" import sys import json from pathlib import Path BRAIN_DIR = Path("/root/.openclaw/workspace/second-brain") sys.path.insert(0, str(BRAIN_DIR)) from src.store import EngramStore DB_PATH = BRAIN_DIR / "data" / "brain.sqlite" store = EngramStore(str(DB_PATH)) # Hole alle unbestätigten Engrams (verdict ist NULL oder nicht confirmed_true/false) cursor = store._conn.execute(""" SELECT id, metadata_json, correctness_json FROM engrams WHERE json_extract(correctness_json, '$.verdict') IS NULL """) rows = cursor.fetchall() print(f"Unbestätigte Engrams: {len(rows)}") evaluated = 0 true_count = 0 false_count = 0 for eid, meta_json, corr_json in rows: try: meta = json.loads(meta_json) if meta_json else {} corr = json.loads(corr_json) if corr_json else {} source = meta.get("source", "") tags = meta.get("tags", []) if isinstance(tags, str): tags = [tags] # Entscheidungsregeln verdict = None reason = None if source == "worker": verdict = "confirmed_true" reason = "source=worker" elif source == "memory": safe_tags = ["ops", "housekeeping", "sop", "meta", "system"] if any(t in safe_tags for t in tags): verdict = "confirmed_true" reason = f"memory with safe tags: {safe_tags}" elif source == "agent": verdict = "confirmed_true" reason = "source=agent" else: # Prüfe auf Fehler-Tags error_tags = ["error", "failure", "exception", "bug", "critical"] if any(t in error_tags for t in tags): verdict = "confirmed_false" reason = f"error tags: {error_tags}" if verdict: eg = store.get(eid) if eg is None: continue eg.correctness.verdict = verdict if verdict == "confirmed_true": eg.correctness.confirmed = True true_count += 1 else: eg.correctness.confirmed = False false_count += 1 store.save(eg) evaluated += 1 # Log pro 100 if evaluated % 100 == 0: print(f" ... {evaluated} evaluiert (true={true_count}, false={false_count})") except Exception as e: print(f"Fehler bei {eid}: {e}") print(f"Evaluierte Engrams: {evaluated}") print(f" -> confirmed_true: {true_count}") print(f" -> confirmed_false: {false_count}")