All MicroEvals
from __future__ import annotations import json import os im...
Create MicroEval
Header image for from __future__ import annotations

import json
import os
im...

from __future__ import annotations import json import os im...

Prompt

from __future__ import annotations import json import os import tempfile import threading from dataclasses import asdict, dataclass, field from pathlib import Path from typing import Any def _atomic_write(path: Path, data: str) -> None: path.parent.mkdir(parents=True, exist_ok=True) fd, tmp_name = tempfile.mkstemp(prefix=path.name + ".", dir=str(path.parent)) try: with os.fdopen(fd, "w", encoding="utf-8") as fh: fh.write(data) os.replace(tmp_name, path) except Exception: try: os.unlink(tmp_name) except OSError: pass raise def _read_json(path: Path, default: Any) -> Any: if not path.exists(): return default try: return json.loads(path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError): return default @dataclass class CompactionEventRecord: sequence: int started_at: float completed_at: float tokens_before: int tokens_after: int tokens_reclaimed: int messages_evicted: int messages_preserved: int boundary: dict[str, Any] checkpoint_id: str summary_artifact_id: str instructions: str | None retries: int machine_repaired: bool repair_actions: list[str] = field(default_factory=list) session_id: str = "" def to_dict(self) -> dict[str, Any]: return asdict(self) @dataclass class ProbeResult: model_id: str largest_success_tokens: int smallest_failure_tokens: int | None failure_error: str stop_reason: str timestamp: float attempts: list[dict[str, Any]] = field(default_factory=list) def to_dict(self) -> dict[str, Any]: return asdict(self) @dataclass class RemediationRecord: timestamp: float trigger: str knob: str before: int after: int justification: str observations: list[str] = field(default_factory=list) experiment_id: str | None = None def to_dict(self) -> dict[str, Any]: return asdict(self) @dataclass class SessionMetrics: session_id: str total_turns: int = 0 total_tokens_processed: int = 0 compaction_count: int = 0 reclaimed_per_compaction: list[int] = field(default_factory=list) turn_latencies_ms: list[float] = field(default_factory=list) length_limited_stops: int = 0 amnesia_incidents: int = 0 incomplete_implementation_incidents: int = 0 partial_success_report_incidents: int = 0 boundary_casualties: int = 0 characters_dropped_to_truncation: int = 0 def mean_reclaimed(self) -> float: if not self.reclaimed_per_compaction: return 0.0 return sum(self.reclaimed_per_compaction) / len(self.reclaimed_per_compaction) def max_reclaimed(self) -> int: return max(self.reclaimed_per_compaction) if self.reclaimed_per_compaction else 0 def mean_latency_ms(self) -> float: if not self.turn_latencies_ms: return 0.0 return sum(self.turn_latencies_ms) / len(self.turn_latencies_ms) def to_dict(self) -> dict[str, Any]: return { "session_id": self.session_id, "total_turns": self.total_turns, "total_tokens_processed": self.total_tokens_processed, "compaction_count": self.compaction_count, "mean_reclaimed_per_compaction": self.mean_reclaimed(), "max_reclaimed_per_compaction": self.max_reclaimed(), "mean_turn_latency_ms": self.mean_latency_ms(), "length_limited_stops": self.length_limited_stops, "amnesia_incidents": self.amnesia_incidents, "incomplete_implementation_incidents": self.incomplete_implementation_incidents, "partial_success_report_incidents": self.partial_success_report_incidents, "boundary_casualties": self.boundary_casualties, "characters_dropped_to_truncation": self.characters_dropped_to_truncation, } @dataclass class ExperimentRecord: experiment_id: str started_at: float knob: str before_value: int after_value: int cohort_session_ids: list[str] = field(default_factory=list) before_metrics: dict[str, float] = field(default_factory=dict) after_metrics: dict[str, float] = field(default_factory=dict) verdict: str = "inconclusive" delta: dict[str, float] = field(default_factory=dict) forced: bool = False completed_at: float | None = None def to_dict(self) -> dict[str, Any]: return asdict(self) class Persistence: def __init__(self, root: str | Path) -> None: self.root = Path(root) self.root.mkdir(parents=True, exist_ok=True) self._lock = threading.RLock() self.sessions_path = self.root / "sessions.json" self.compaction_path = self.root / "compaction_events.json" self.boundary_path = self.root / "boundary_records.json" self.summaries_path = self.root / "summaries.json" self.probes_path = self.root / "probes.json" self.remediations_path = self.root / "remediations.json" self.experiments_path = self.root / "experiments.json" self.metrics_path = self.root / "metrics.json" self.stop_log_path = self.root / "stop_reasons.jsonl" self.state_path = self.root / "state.json" def _load_list(self, path: Path) -> list[Any]: data = _read_json(path, []) return data if isinstance(data, list) else [] def _save_list(self, path: Path, items: list[Any]) -> None: _atomic_write(path, json.dumps(items, indent=2, sort_keys=True, default=str)) def append_compaction(self, event: CompactionEventRecord) -> None: with self._lock: items = self._load_list(self.compaction_path) items.append(event.to_dict()) self._save_list(self.compaction_path, items) def append_boundary(self, record: dict[str, Any]) -> None: with self._lock: items = self._load_list(self.boundary_path) items.append(record) self._save_list(self.boundary_path, items) def append_summary(self, summary: dict[str, Any]) -> None: with self._lock: items = self._load_list(self.summaries_path) items.append(summary) self._save_list(self.summaries_path, items) def append_probe(self, probe: ProbeResult) -> None: with self._lock: items = self._load_list(self.probes_path) items.append(probe.to_dict()) self._save_list(self.probes_path, items) def append_remediation(self, record: RemediationRecord) -> None: with self._lock: items = self._load_list(self.remediations_path) items.append(record.to_dict()) self._save_list(self.remediations_path, items) def append_experiment(self, record: ExperimentRecord) -> None: with self._lock: items = self._load_list(self.experiments_path) items = [e for e in items if e.get("experiment_id") != record.experiment_id] items.append(record.to_dict()) self._save_list(self.experiments_path, items) def list_experiments(self) -> list[dict[str, Any]]: with self._lock: return self._load_list(self.experiments_path) def list_compactions(self) -> list[dict[str, Any]]: with self._lock: return self._load_list(self.compaction_path) def list_boundaries(self) -> list[dict[str, Any]]: with self._lock: return self._load_list(self.boundary_path) def list_summaries(self) -> list[dict[str, Any]]: with self._lock: return self._load_list(self.summaries_path) def latest_summary(self) -> dict[str, Any] | None: items = self.list_summaries() return items[-1] if items else None def list_probes(self) -> list[dict[str, Any]]: with self._lock: return self._load_list(self.probes_path) def list_remediations(self) -> list[dict[str, Any]]: with self._lock: return self._load_list(self.remediations_path) def load_metrics(self) -> dict[str, SessionMetrics]: raw = _read_json(self.metrics_path, {}) out: dict[str, SessionMetrics] = {} for sid, data in raw.items(): out[sid] = SessionMetrics( session_id=sid, total_turns=int(data.get("total_turns", 0)), total_tokens_processed=int(data.get("total_tokens_processed", 0)), compaction_count=int(data.get("compaction_count", 0)), reclaimed_per_compaction=list(data.get("reclaimed_per_compaction", [])), turn_latencies_ms=list(data.get("turn_latencies_ms", [])), length_limited_stops=int(data.get("length_limited_stops", 0)), amnesia_incidents=int(data.get("amnesia_incidents", 0)), incomplete_implementation_incidents=int( data.get("incomplete_implementation_incidents", 0) ), partial_success_report_incidents=int( data.get("partial_success_report_incidents", 0) ), boundary_casualties=int(data.get("boundary_casualties", 0)), characters_dropped_to_truncation=int( data.get("characters_dropped_to_truncation", 0) ), ) return out def save_metrics(self, metrics: dict[str, SessionMetrics]) -> None: with self._lock: payload = {sid: m.to_dict() for sid, m in metrics.items()} _atomic_write(self.metrics_path, json.dumps(payload, indent=2, sort_keys=True)) def append_stop_reason(self, entry: dict[str, Any]) -> None: with self._lock, self.stop_log_path.open("a", encoding="utf-8") as fh: fh.write(json.dumps(entry, sort_keys=True, default=str) + "\n") def read_stop_reasons(self, tail: int = 100) -> list[dict[str, Any]]: if not self.stop_log_path.exists(): return [] try: lines = self.stop_log_path.read_text(encoding="utf-8").splitlines() except OSError: return [] out: list[dict[str, Any]] = [] for line in lines[-tail:]: try: out.append(json.loads(line)) except json.JSONDecodeError: continue return out def save_state(self, state: dict[str, Any]) -> None: with self._lock: _atomic_write( self.state_path, json.dumps(state, indent=2, sort_keys=True, default=str), ) def load_state(self) -> dict[str, Any]: return _read_json(self.state_path, {}) def save_history(self, session_id: str, payload: dict[str, Any]) -> None: with self._lock: path = self.root / "histories" / f"{session_id}.json" _atomic_write( path, json.dumps(payload, indent=2, sort_keys=True, default=str), ) def load_history(self, session_id: str) -> dict[str, Any] | None: path = self.root / "histories" / f"{session_id}.json" if not path.exists(): return None try: return json.loads(path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError): return None def export_all(self) -> dict[str, Any]: with self._lock: return { "sessions": _read_json(self.sessions_path, {}), "compaction_events": self._load_list(self.compaction_path), "boundary_records": self._load_list(self.boundary_path), "summaries": self._load_list(self.summaries_path), "probes": self._load_list(self.probes_path), "remediations": self._load_list(self.remediations_path), "experiments": self._load_list(self.experiments_path), "metrics": _read_json(self.metrics_path, {}), "stop_reasons": self.read_stop_reasons(tail=10_000), "state": _read_json(self.state_path, {}), } Send back the complete code with all the fixes. Fix each of the listed errors one by one, making sure to actually correct them so that there are 0 errors remaining. Keep the original imports, since the files exist. Write out every single character; do not abbreviate anything. Fix every error. There must be exactly one file. Do not write anything else; just output the complete code, and it must not contain any comments. Never, under any circumstances, use simplified, substitute, dummy, simulated, or fake code. Write the entire file as complete, unabridged, production-ready code in a single code block. It must be 100% error-free, a complete, error-free file, and must be submitted as a downloadable file. These requirements are mandatory and must be strictly adhered to. If no list of errors is provided, you must find all the errors and fix them. If there were comments in the original code, delete them. And most importantly: YOU MUST NEVER SIMPLIFY!