exlab_wizard.cache.ingest_writer#
Orchestrator-only writer for ingest.json. Backend Spec §13.4, §4.4.5.
The orchestrator writes one ingest.json per staged run, capturing the
five-state lifecycle (§13.3): staging -> complete -> sync_queued
-> sync_verified -> cleared. State transitions are append-only – a
new entry is added to the history array on every transition, and the
top-level current_state mirrors the latest entry.
Disk-side guarantees per §4.4.5:
msgspec.jsonfor typed encode/decode (schema validation in one pass).filelock.FileLockadvisory exclusive lock around the read-mutate-write cycle so concurrent appends never lose entries.Atomic write via tempfile +
fsync+os.replace.
Reads enforce the §11.9.2 reader policy: a file at a different schema major
than the writer raises SchemaMajorMismatchError (no silent partial-parse
across major boundaries).
Functions
|
Return |
Classes
Writer for |
- class exlab_wizard.cache.ingest_writer.IngestWriter[source]#
Bases:
objectWriter for
ingest.json. Orchestrator-mode only.Backend Spec §13.4. State history is append-only (§13.3) to preserve full audit history – the writer never edits or deletes prior entries.
All public methods are
asyncto match the §4.4.5CacheWritercontract; the blocking lock + I/O work is dispatched throughasyncio.to_threadso the FastAPI event loop is never blocked.- async append_state_transition(path, new_state, *, host, files_received=None, bytes_received=None, nas_path=None, checksum_file=None)[source]#
Append a state-transition entry and update
current_state.File-locked for the entire read-mutate-write cycle so concurrent callers never lose entries. The new history entry has the shape:
{"state": "<new_state>", "at": "<UTC ISO 8601>", "host": "<host>"}
Per §13.4 the entry carries optional extras when transitioning to specific states:
complete–files_receivedandbytes_received.sync_verified–nas_pathandchecksum_file.
Other state transitions ignore those extras (they are silently dropped because the spec does not define their meaning at those states).
Raises
ValueErrorifnew_stateis not a permitted forward transition from the file’s current state. Going backward (e.g.cleared->staging) is rejected. The full state machine is documented in_FORWARD_TRANSITIONSabove and §13.3.
- async read_ingest(path)[source]#
Read and decode
pathinto anIngestJson.Raises
SchemaMajorMismatchError(§11.9.2) when the on-disk file carries a different schema major thanINGEST_JSON_VERSION.- Parameters:
path (
Path)- Return type:
- async write_ingest(path, payload)[source]#
Write
payloadtopathatomically under an exclusive lock.Reserved for the initial-creation path (no file exists yet); the per-file lock is taken defensively so a concurrent initial-write attempt serializes rather than races.
- Parameters:
path (
Path)payload (
IngestJson)
- Return type: