# neurotron/kernel_log_agent.py

from pathlib import Path
from neurotron.logbus import logbus

class KernelLogAgent:
    name = "kernel.log"

    def __init__(self, cortex, max_lines=256):
        self.ctx = cortex
        self.max_lines = max_lines
        self.buffer = []  # futuramente vai para dashboard pane

    def observe(self):
        # leitura "bulk" por enquanto
        try:
            with open("/dev/kmsg", "r", encoding="utf-8", errors="ignore") as f:
                for _ in range(self.max_lines):
                    line = f.readline()
                    if not line:
                        break
                    line = line.strip()
                    if not line:
                        continue

                    # guarda em memória (para TUI)
                    self.buffer.append(line)
                    if len(self.buffer) > self.max_lines:
                        self.buffer = self.buffer[-self.max_lines:]

                    # opcional: envia alguns para o Hippocampus
                    # self.ctx.memory.remember("kernel.msg", {"line": line})
        except Exception as e:
            logbus.debug(f"[kernel.log] erro a ler /dev/kmsg: {e}")

    def think(self):
        # futuro: detectar padrões, warnings, crashes…
        pass

    def act(self):
        # futuro: alertar o utilizador, reiniciar serviços…
        pass