babylon.engine.history.io
File I/O operations for the History Stack system.
This module provides save/load functionality for: - WorldState snapshots (simple state persistence) - Full Checkpoint bundles (state + config + metadata)
All writes use atomic patterns (temp file + rename) to prevent corruption.
Sprint C: File I/O for Phase 3 persistence layer.
Functions
|
List all valid checkpoints in a directory. |
|
Load a full Checkpoint from a JSON file. |
|
Load a WorldState from a JSON file. |
|
Save a full Checkpoint to a JSON file. |
|
Save a WorldState to a JSON file. |
|
Check if a file is a valid checkpoint. |
Exceptions
|
Raised when a checkpoint file contains invalid JSON. |
|
Base exception for checkpoint I/O errors. |
|
Raised when a checkpoint file is not found. |
|
Raised when a checkpoint file has invalid schema. |
- exception babylon.engine.history.io.CheckpointIOError(message)[source]
Bases:
StorageErrorBase exception for checkpoint I/O errors.
Now inherits from StorageError to be part of unified BabylonError hierarchy.
- Parameters:
message (str)
- Return type:
None
- exception babylon.engine.history.io.CheckpointNotFoundError(message)[source]
Bases:
CheckpointIOErrorRaised when a checkpoint file is not found.
- Parameters:
message (str)
- Return type:
None
- exception babylon.engine.history.io.CheckpointCorruptedError(message)[source]
Bases:
CheckpointIOErrorRaised when a checkpoint file contains invalid JSON.
- Parameters:
message (str)
- Return type:
None
- exception babylon.engine.history.io.CheckpointSchemaError(message)[source]
Bases:
CheckpointIOErrorRaised when a checkpoint file has invalid schema.
- Parameters:
message (str)
- Return type:
None
- babylon.engine.history.io.save_state(state, path)[source]
Save a WorldState to a JSON file.
Uses atomic write pattern (temp file + rename) for safety. Creates parent directories if they don’t exist.
- Parameters:
state (
WorldState) – The WorldState to save.path (
Path) – The file path to save to.
- Return type:
- Returns:
The path where the file was saved.
- babylon.engine.history.io.load_state(path)[source]
Load a WorldState from a JSON file.
- Parameters:
path (
Path) – The file path to load from.- Return type:
- Returns:
The loaded WorldState.
- Raises:
CheckpointNotFoundError – If the file doesn’t exist.
CheckpointCorruptedError – If the file contains invalid JSON.
CheckpointSchemaError – If the JSON doesn’t match WorldState schema.
- babylon.engine.history.io.save_checkpoint(state, config, path, description='')[source]
Save a full Checkpoint to a JSON file.
Creates a Checkpoint with current metadata and saves it atomically. Creates parent directories if they don’t exist.
- Parameters:
state (
WorldState) – The WorldState to save.config (
SimulationConfig) – The SimulationConfig to save.path (
Path) – The file path to save to.description (
str) – Optional description for the checkpoint.
- Return type:
- Returns:
The path where the file was saved.
- babylon.engine.history.io.load_checkpoint(path)[source]
Load a full Checkpoint from a JSON file.
- Parameters:
path (
Path) – The file path to load from.- Return type:
- Returns:
The loaded Checkpoint.
- Raises:
CheckpointNotFoundError – If the file doesn’t exist.
CheckpointCorruptedError – If the file contains invalid JSON.
CheckpointSchemaError – If the JSON doesn’t match Checkpoint schema.
- babylon.engine.history.io.list_checkpoints(directory)[source]
List all valid checkpoints in a directory.
Scans the directory for JSON files that are valid checkpoints. Returns a list of (path, metadata) tuples for valid checkpoints.
- Parameters:
directory (
Path) – The directory to scan.- Return type:
- Returns:
List of (Path, CheckpointMetadata) tuples for valid checkpoints.