File size: 894 Bytes
b85de73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
from pathlib import Path
from typing import Any


RUNS_DIR = Path(__file__).resolve().parents[2] / "runs"


def run_dir(run_id: str) -> Path:
    path = RUNS_DIR / run_id
    path.mkdir(parents=True, exist_ok=True)
    return path


def append_stage_trace(run_id: str, trace: dict[str, Any]) -> None:
    path = run_dir(run_id) / "stage_traces.jsonl"
    with path.open("a", encoding="utf-8") as handle:
        handle.write(json.dumps(trace, ensure_ascii=False) + "\n")


def write_run_export(run_id: str, export: dict[str, Any]) -> None:
    path = run_dir(run_id) / "run_export.json"
    path.write_text(json.dumps(export, indent=2, ensure_ascii=False), encoding="utf-8")


def read_stage_traces_jsonl(run_id: str) -> str:
    path = run_dir(run_id) / "stage_traces.jsonl"
    return path.read_text(encoding="utf-8") if path.exists() else ""