Implementation:416rehman DeepZero StateStore
| Knowledge Sources | |
|---|---|
| Domains | State_Management, Persistence |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Atomic file-based state persistence layer for pipeline runs, providing crash-safe per-sample state, run metadata, and manifest management with resume support.
Description
The StateStore class manages all persistent state for a DeepZero pipeline run. It writes JSON files atomically (via temp-file + os.replace) so a crash mid-write never corrupts state. Three layers of state are persisted:
- RunState: top-level run metadata (run_id, pipeline, model, status, timestamps, PID liveness token).
- SampleState: per-sample state (sample_id, sha256, verdict, per-stage history with StageOutput records).
- RunManifest: a summary manifest of all samples with their verdicts and current stages.
The SampleState lifecycle transitions are: pending → active → skipped | failed | completed, with per-stage marks for running, completed (with continue/filter verdict), failed, timed_out, cached, and skipped. The STATE_VERSION=2 gate rejects incompatible state from older runs on load.
Usage
- Persisting run state during pipeline execution (called by PipelineRunner after each stage barrier).
- Inspecting run status via deepzero status (loads RunState and manifest).
- Resuming runs: list_samples() returns all persisted sample states, which the runner uses to skip completed work.
- Clearing timed-out stages for retry: SampleState.clear_timed_out_stages() drops only TIMED_OUT stage records, preserving everything else.
Code Reference
Source Location
- Repository: DeepZero
- File: src/deepzero/engine/state.py
Signature
class StateStore:
def __init__(self, work_dir: Path): ...
def save_run(self, run: RunState) -> None: ...
def load_run(self) -> RunState | None: ...
def save_sample(self, state: SampleState) -> None: ...
def load_sample(self, sample_id: str) -> SampleState | None: ...
def list_samples(self) -> list[SampleState]: ...
def save_manifest(self, samples: list[SampleState]) -> None: ...
def load_manifest(self) -> list[dict]: ...
def sample_dir(self, sample_id: str) -> Path: ...
@dataclass
class SampleState:
sample_id: str
sha256: str = ""
source_path: str = ""
filename: str = ""
verdict: SampleStatus = SampleStatus.PENDING
current_stage: str = ""
history: dict[str, StageOutput] = field(default_factory=dict)
error: str | None = None
def mark_stage_running(self, stage_name: str) -> None: ...
def mark_stage_completed(self, stage_name, verdict, artifacts, data) -> None: ...
def mark_stage_failed(self, stage_name: str, error: str) -> None: ...
def mark_stage_timed_out(self, stage_name: str, budget: int) -> None: ...
def mark_stage_cached(self, stage_name: str, reason: str = "") -> None: ...
def mark_stage_skipped(self, stage_name: str, reason: str = "") -> None: ...
def clear_timed_out_stages(self) -> list[str]: ...
def is_stage_done(self, stage_name: str) -> bool: ...
def is_active(self) -> bool: ...
Import
from deepzero.engine.state import StateStore, SampleState, RunState, StageOutput
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| work_dir | Path | Yes | Root directory for run state files |
| run | RunState | Yes (save_run) | Run metadata to persist |
| state | SampleState | Yes (save_sample) | Per-sample state to persist |
Outputs
| Name | Type | Description |
|---|---|---|
| run.json | File | Run metadata (atomic write) |
| samples/<id>/state.json | File | Per-sample state with stage history |
| run_manifest.json | File | Summary manifest of all samples |
| load_run() returns | None | Deserialized run state or None if absent |
| load_sample() returns | None | Deserialized sample state or None |
| list_samples() returns | list[SampleState] | All persisted sample states |
Usage Examples
Basic State Management
from pathlib import Path
from deepzero.engine.state import StateStore, RunState, SampleState
store = StateStore(Path("./work/demo"))
# Save run state
run = RunState(run_id="run-001", pipeline="demo", target="./files", model="")
store.save_run(run)
# Save sample state with stage output
sample = SampleState(sample_id="abc123def456", sha256="abc123...")
sample.mark_stage_running("discover")
sample.mark_stage_completed("discover", data={"sha256": "abc123", "size_bytes": 1024})
store.save_sample(sample)
# Resume: load existing samples
existing = store.list_samples()
print(f"Found {len(existing)} existing samples to resume")
Clearing Timed-Out Stages for Retry
# Only TIMED_OUT stages are dropped; all other results are preserved
for sample in store.list_samples():
cleared = sample.clear_timed_out_stages()
if cleared:
print(f"Sample {sample.sample_id}: cleared {cleared}")
store.save_sample(sample)