Files
floop/code/floop/stream.py
T
onibot 6733cb660e feat(stream): thinking + run log for CLI (GAP-13 CLI part)
RunStreams: run.log always (flushed, tail -f-able) with full technical detail; thinking stream to screen under --verbose (flushed, [T] lines, > depth prefixes). Machine hooks on INIT/MISSING/ROUTE/EXPERT/ANSWER + budget ticks; child runs share the parent's stream; engine emits its raw output. Verified live: 8-call train run inline, fresh-question run 69.6s (proxy caches identical temp-0 prompts; noted).
2026-10-13 17:50:57 +02:00

91 lines
3.3 KiB
Python

"""Progress streams for a floop run: the log (complete, technical) and thinking
(the screen channel for CLI; the future "thinking" output of floop-as-LLM).
Design (SCOPE.md §4 / GAP-13): every event lands in runs/<ts>-<slug>/run.log with
explicit flush (tail -f-able). The screen shows ONLY the thinking subset, and only
with --verbose. log ⊇ thinking; they will diverge over time.
"""
from __future__ import annotations
import re
import sys
from datetime import datetime, timezone
from pathlib import Path
def _slug(text: str, maxlen: int = 40) -> str:
s = re.sub(r"[^a-z0-9]+", "-", text.lower()).strip("-")
return s[:maxlen].rstrip("-") or "run"
def now_hms() -> str:
return datetime.now(timezone.utc).astimezone().strftime("%H:%M:%S")
class RunStreams:
"""Two sinks, one class: log always (flushed); thinking to screen iff verbose."""
def __init__(self, question: str, verbose: bool = False,
runs_dir: str | None = None) -> None:
base = Path(runs_dir) if runs_dir else Path(__file__).resolve().parent.parent / "runs"
base.mkdir(parents=True, exist_ok=True)
ts = datetime.now(timezone.utc).astimezone().strftime("%Y%m%d-%H%M%S")
self.run_dir = base / f"{ts}-{_slug(question)}"
self.run_dir.mkdir(parents=True, exist_ok=True)
self.log_path = self.run_dir / "run.log"
self.verbose = verbose
self._fh = open(self.log_path, "a", encoding="utf-8")
def close(self) -> None:
if self._fh and not self._fh.closed:
self._fh.close()
# ---- channels ----------------------------------------------------------
def log(self, text: str) -> None:
"""Technical detail: log only, never on screen."""
self._emit_log(f"LOG {text}")
def think(self, text: str | None) -> None:
"""Thinking: screen iff verbose (flushed); always to log as [T] lines."""
if text is None:
return
for line in text.rstrip(chr(10)).split(chr(10)):
self._emit_log(f"[T] {line}", screen=f"[T] {line}")
def think_line(self, text: str) -> None:
self.think(text)
# ---- structural events -------------------------------------------------
def call(self, stage: str, description: str, think: str | None = None) -> None:
"""An LLM call or engine step is starting."""
self._emit_log(f"CALL {stage}: {description}")
if think is not None:
self.think_line(f"[{stage}] {think}")
def output(self, stage: str, text: str, think: bool = True) -> None:
"""Raw LLM (or engine) output as it lands."""
self._emit_log(f"OUTPUT {stage}: {text}")
if think:
self.think(text)
def state(self, text: str, think: bool = False) -> None:
"""State change: evidence appended, question status, budget."""
self._emit_log(f"STATE {text}")
if think:
self.think_line(text)
def final(self, text: str) -> None:
self._emit_log(f"FINAL {text}")
self.think_line(f"[FINAL] {text}")
# ---- internals ---------------------------------------------------------
def _emit_log(self, log_payload: str, screen: str | None = None) -> None:
line = f"[{now_hms()}] {log_payload}"
self._fh.write(line + "\n")
self._fh.flush()
if screen is not None and self.verbose:
print(screen, flush=True)