Commit c15abb7
feat(state): FileSystem trait, LiveFs, StateStore<F> with read/write/append (#7)
Chunk M1-W1-E of M1 plan. Lands the I/O surface for state.toml.
Public surface:
- pub trait FileSystem with three primitives: read_string,
write_temp_then_rename, fsync_dir. Splitting rename and dir-fsync
lets a future MockFs simulate "crashed between rename and fsync"
in chunk M1-W1-F.
- pub struct LiveFs — production impl backed by std::fs and
tempfile::NamedTempFile.
- pub struct StateStore<F: FileSystem = LiveFs> with:
fn new(path) (default LiveFs)
fn with_fs(fs, path) (test injection)
fn path() -> &Path
fn read() -> Result<State, StateError>
fn write_atomic(&State) -> Result<(), StateError>
fn append_history(HistoryEntry)
fn append_failure(FailureRef)
fn record_reconciliation(ReconciliationEntry)
Atomic write per PRD §7.4: temp file in same dir + sync_all + rename
+ fsync of parent directory entry. The two-step decomposition (write
+ fsync_dir) keeps the parent fsync separately mockable.
read() rejects future schema_version with
StateError::UnsupportedSchemaVersion. Migration framework lands in
chunk M1-W1-F.
append_*() are read-modify-write_atomic — file is rewritten in full
but [managed], [adopted], and [reserved] sections retain their values
on round-trip. Byte-level surgical preservation of unknown sections
outside [reserved] is deferred (would require toml_edit; the
[reserved] BTreeMap covers the documented forward-compat namespace).
Cargo.toml: tempfile is now a real (non-dev) dependency because
LiveFs uses NamedTempFile in production.
Tests (LiveFs against tempdir):
- full_state_round_trip
- unknown_fields_preserved_in_reserved
- read_missing_file_yields_not_found
- read_unsupported_schema_version
- write_fails_when_parent_missing
- history_append_does_not_rewrite_managed (asserts managed/adopted/
reserved unchanged in value, history grows)
- append_failure_grows_failures_array
- record_reconciliation_appends_entry
What's NOT in this chunk:
- Migration framework (chunk M1-W1-F).
- MockFs feature-gate + crash-during-write tests (chunk M1-W1-F).
- Property tests via proptest (chunk M1-W1-F).
- contract: state_host_mismatch_detected (engine-side concern; the
StateError variant is defined but not raised here — the engine
compares state.host to hostname() at preflight).
Verification:
- cargo test -p pearlite-state (8 passed; 0 failed)
- cargo clippy --workspace --all-targets -- -D warnings (zero warnings)
- cargo fmt --all --check
- scripts/ci/check-spdx.sh
- pearlite-audit check . (1 check, 0 violations)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent f104d88 commit c15abb7
4 files changed
Lines changed: 409 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| 28 | + | |
0 commit comments