Principle:416rehman DeepZero State Persistence
| Knowledge Sources | |
|---|---|
| Domains | State_Management, Persistence |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
A crash-safe state persistence pattern using atomic file writes, per-sample state versioning, and lifecycle transitions that distinguish timed-out work from failures.
Description
Atomic state persistence ensures a pipeline run can survive crashes and resume without repeating work. The core technique is atomic write replacement: state is written to a temporary file, then atomically renamed to its final path. A crash during the write leaves either the old (complete) state or the new (complete) state, never a corrupt hybrid.
Per-sample state tracks the lifecycle of each sample through stages: pending → active → completed/filtered/failed/timed_out. Each stage records a StageOutput with its status, verdict, artifacts, data, and error. This granular per-stage ledger allows:
- Resume granularity: re-running skips only stages that haven't completed for each sample.
- Timeout preservation: TIMED_OUT is recorded with the budget that was exceeded, so a retry knows what to give it next time.
- State versioning: a version gate rejects incompatible state from older runs, preventing silent corruption from schema changes.
Usage
Use this pattern when:
- A pipeline runs for hours or days and crash recovery is essential
- Per-stage resume granularity is needed (not all-or-nothing)
- Timeout-as-budget semantics require distinguishing slow work from broken work
- Multiple runs against the same corpus must not collide
Theoretical Basis
The atomic write pattern:
# Abstract algorithm (NOT real implementation)
def atomic_write(path, content):
tmp = path.with_suffix(".tmp")
tmp.write_text(content)
os.replace(tmp, path) # atomic on POSIX and Windows
The os.replace call is atomic at the filesystem level: the destination either has the old content or the new content, never a partial write. On Windows, a retry loop handles brief locks from antivirus scanning.
The state lifecycle is a finite state machine per sample:
# State transitions
PENDING -> ACTIVE -> COMPLETED # normal completion
-> FILTERED # intentionally excluded
-> FAILED # processing error
-> TIMED_OUT # budget exhausted (not an error)