"""Latent scratchpad: internal reasoning buffers for hypotheses and discarded paths.""" from __future__ import annotations from collections import deque from dataclasses import dataclass, field from typing import Any from fusionagi._logger import logger @dataclass class ThoughtState: """Internal reasoning state: hypotheses, partial conclusions, discarded paths.""" hypotheses: list[str] = field(default_factory=list) partial_conclusions: list[str] = field(default_factory=list) discarded_paths: list[str] = field(default_factory=list) metadata: dict[str, Any] = field(default_factory=dict) class LatentScratchpad: """ Internal buffer for intermediate reasoning; not exposed to user. Stores hypotheses, discarded paths, partial conclusions for meta-tracking. """ def __init__(self, max_hypotheses: int = 100, max_discarded: int = 50) -> None: self._hypotheses: deque[str] = deque(maxlen=max_hypotheses) self._partial: deque[str] = deque(maxlen=50) self._discarded: deque[str] = deque(maxlen=max_discarded) self._metadata: dict[str, Any] = {} def append_hypothesis(self, hypothesis: str) -> None: """Append a reasoning hypothesis.""" self._hypotheses.append(hypothesis) logger.debug("Scratchpad: hypothesis appended", extra={"len": len(self._hypotheses)}) def append_discarded(self, path: str) -> None: """Append a discarded reasoning path.""" self._discarded.append(path) def append_partial(self, conclusion: str) -> None: """Append a partial conclusion.""" self._partial.append(conclusion) def get_intermediate(self) -> ThoughtState: """Get current intermediate state.""" return ThoughtState( hypotheses=list(self._hypotheses), partial_conclusions=list(self._partial), discarded_paths=list(self._discarded), metadata=dict(self._metadata), ) def clear(self) -> None: """Clear scratchpad.""" self._hypotheses.clear() self._partial.clear() self._discarded.clear() self._metadata.clear() def set_metadata(self, key: str, value: Any) -> None: """Set metadata entry.""" self._metadata[key] = value def get_metadata(self, key: str, default: Any = None) -> Any: """Get metadata entry.""" return self._metadata.get(key, default)