-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
51 lines (38 loc) · 1.55 KB
/
memory.py
File metadata and controls
51 lines (38 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations
import json
import time
from dataclasses import dataclass
from pathlib import Path
from typing import Any
@dataclass(frozen=True)
class SessionPaths:
session_dir: Path
jsonl_path: Path
metrics_path: Path
def ensure_session(session_id: int, *, reset_existing: bool = True) -> SessionPaths:
session_dir = Path("sessions") / f"session-{session_id:03d}"
session_dir.mkdir(parents=True, exist_ok=True)
jsonl_path = session_dir / "events.jsonl"
metrics_path = session_dir / "metrics.json"
if reset_existing:
if jsonl_path.exists():
jsonl_path.unlink()
if metrics_path.exists():
metrics_path.unlink()
for shot in session_dir.glob("step-*.png"):
try:
shot.unlink()
except OSError:
# Keep run startup resilient if a screenshot is temporarily locked.
pass
return SessionPaths(session_dir=session_dir, jsonl_path=jsonl_path, metrics_path=metrics_path)
def write_event(jsonl_path: Path, event: dict[str, Any]) -> None:
event = dict(event)
event.setdefault("ts", time.time())
with jsonl_path.open("a", encoding="utf-8") as f:
f.write(json.dumps(event, ensure_ascii=True) + "\n")
def write_metrics(metrics_path: Path, metrics: dict[str, Any]) -> None:
metrics_path.parent.mkdir(parents=True, exist_ok=True)
metrics_path.write_text(json.dumps(metrics, indent=2, sort_keys=True), encoding="utf-8")
def read_text(path: Path) -> str:
return path.read_text(encoding="utf-8")