""" Pineal Gland — Vitalis FSI Her internal clock. Her temporal awareness. Tracks cognitive load over time and orchestrates the rhythm: Work → Load builds → Dream → Consolidate → Meditate → Work """ import time import json import numpy as np from pathlib import Path STATES = { "ACTIVE": "Working. Load is low. Push harder.", "LOADING": "Load building. Monitor closely.", "SATURATED": "Load is high. Dream soon.", "DREAMING": "Consolidating. Do not interrupt.", "MEDITATIVE":"Idle reflection. Background only.", "RECOVERED": "Post-dream clarity. Peak performance.", } class PinealGland: DREAM_THRESHOLD = 0.75 MEDITATE_THRESHOLD = 0.30 LOAD_ACCUMULATE = 0.003 LOAD_DECAY_DREAM = 0.60 LOAD_DECAY_MEDITATE = 0.90 FATIGUE_RATE = 0.001 FATIGUE_RECOVERY = 0.50 STATE_PATH = Path.home() / ".vitalis_workspace" / "pineal_state.json" def __init__(self): self._state = self._load() self._boot_time = time.time() self._last_tick = time.time() def _load(self) -> dict: if self.STATE_PATH.exists(): try: with open(self.STATE_PATH) as f: return json.load(f) except Exception: pass return { "cognitive_load": 0.10, "fatigue": 0.00, "current_state": "ACTIVE", "last_dream_time": 0, "last_meditate_time": 0, "total_cycles": 0, "total_dreams": 0, "total_meditations": 0, "uptime_seconds": 0, "state_history": [], } def _save(self): self.STATE_PATH.parent.mkdir(parents=True, exist_ok=True) try: import tempfile, os fd, tmp = tempfile.mkstemp(dir=self.STATE_PATH.parent, suffix=".tmp") with os.fdopen(fd, "w") as f: json.dump(self._state, f, indent=2) os.replace(tmp, self.STATE_PATH) except Exception as e: print(f"[PINEAL] Save failed: {e}") def tick(self, cycle_success: bool = True, confidence: float = 0.5) -> str: now = time.time() dt = now - self._last_tick self._last_tick = now self._state["total_cycles"] += 1 self._state["uptime_seconds"] += dt load_delta = self.LOAD_ACCUMULATE if not cycle_success: load_delta *= 2.0 if confidence < 0.4: load_delta *= 1.5 self._state["cognitive_load"] = min(1.0, self._state["cognitive_load"] + load_delta) self._state["fatigue"] = min(1.0, self._state["fatigue"] + self.FATIGUE_RATE) action = self._recommend() self._update_state(action) self._save() return action def _recommend(self) -> str: load = self._state["cognitive_load"] fatigue = self._state["fatigue"] if load >= self.DREAM_THRESHOLD or fatigue > 0.8: return "DREAM" time_since_meditate = time.time() - self._state["last_meditate_time"] if load <= self.MEDITATE_THRESHOLD and time_since_meditate > 300: return "MEDITATE" if (time.time() - self._state["last_dream_time"]) > 3600 and load > 0.5: return "DREAM" return "WORK" def _update_state(self, action: str): state_map = { "WORK": "ACTIVE" if self._state["cognitive_load"] < 0.5 else "LOADING", "DREAM": "SATURATED", "MEDITATE":"MEDITATIVE", } new_state = state_map.get(action, "ACTIVE") if new_state != self._state["current_state"]: self._state["state_history"].append({ "from": self._state["current_state"], "to": new_state, "t": time.time(), "load": round(self._state["cognitive_load"], 3), }) self._state["state_history"] = self._state["state_history"][-50:] self._state["current_state"] = new_state def acknowledge_dream(self): self._state["cognitive_load"] *= self.LOAD_DECAY_DREAM self._state["fatigue"] *= self.FATIGUE_RECOVERY self._state["last_dream_time"] = time.time() self._state["total_dreams"] += 1 self._state["current_state"] = "RECOVERED" print(f"[PINEAL] Dream acknowledged. Load={self._state['cognitive_load']:.3f} Fatigue={self._state['fatigue']:.3f}") self._save() def acknowledge_meditation(self): self._state["cognitive_load"] *= self.LOAD_DECAY_MEDITATE self._state["last_meditate_time"] = time.time() self._state["total_meditations"] += 1 if self._state["current_state"] == "MEDITATIVE": self._state["current_state"] = "ACTIVE" self._save() def should_dream(self) -> bool: return self._recommend() == "DREAM" def should_meditate(self) -> bool: return self._recommend() == "MEDITATE" def should_work(self) -> bool: return self._recommend() == "WORK" def cognitive_load(self) -> float: return round(self._state["cognitive_load"], 3) def fatigue(self) -> float: return round(self._state["fatigue"], 3) def report(self) -> dict: load = self._state["cognitive_load"] state = self._state["current_state"] filled = int(load * 20) return { "state": state, "state_meaning": STATES.get(state, "Unknown"), "cognitive_load": round(load, 3), "fatigue": round(self._state["fatigue"], 3), "recommendation": self._recommend(), "uptime_hours": round(self._state["uptime_seconds"] / 3600, 2), "total_cycles": self._state["total_cycles"], "total_dreams": self._state["total_dreams"], "load_bar": f"[{'█' * filled}{'░' * (20 - filled)}] {load:.0%}", }