File size: 743 Bytes
acf77ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from __future__ import annotations

import os
from pathlib import Path

from codeforge.ralph.models import RunResult


def save_checkpoint(run: RunResult, checkpoint_dir: Path) -> Path:
    """Atomically write a RunResult to ``checkpoint_dir/run_{run_id}.json``."""
    checkpoint_dir.mkdir(parents=True, exist_ok=True)
    final = checkpoint_dir / f"run_{run.run_id}.json"
    tmp = final.with_suffix(".json.tmp")
    tmp.write_text(run.model_dump_json(indent=2), encoding="utf-8")
    os.replace(tmp, final)
    return final


def load_checkpoint(checkpoint_path: Path) -> RunResult:
    """Load a RunResult from a checkpoint JSON file."""
    return RunResult.model_validate_json(
        checkpoint_path.read_text(encoding="utf-8"),
    )