"""Incremental, crash-safe JSON output. Each agent's output is (over)written atomically after every research round and again at finalize, so a hang or power loss mid-run still leaves the best-so-far result on disk under state/output//.json. """ from __future__ import annotations import os from pathlib import Path from agents.schemas import AgentOutput, RunManifest def _atomic_write(path: Path, text: str) -> None: tmp = path.with_suffix(path.suffix + ".tmp") tmp.write_text(text) os.replace(tmp, path) # atomic on the same filesystem class Checkpointer: def __init__(self, output_dir: Path, trade_date: str): self.dir = Path(output_dir) / trade_date self.dir.mkdir(parents=True, exist_ok=True) def agent_path(self, persona_id: str) -> Path: return self.dir / f"{persona_id}.json" def write(self, output: AgentOutput) -> None: _atomic_write(self.agent_path(output.persona_id), output.model_dump_json(indent=2)) def write_manifest(self, manifest: RunManifest) -> None: _atomic_write(self.dir / "_manifest.json", manifest.model_dump_json(indent=2))