Skip to content

feat(core): state machine legality + effect sandwich (E2) - #16

Merged
ajianaz merged 2 commits into
developfrom
feat/e2-state-machine
Aug 26, 2026
Merged

ajianaz merged 2 commits into
developfrom
feat/e2-state-machine

Conversation

@ajianaz

@ajianaz ajianaz commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements E2 — state machine (issue #2).

  • Transition legality table enforced at commit AND at replay; illegal moves return StorageError::IllegalTransition, never panic, and never write a byte
  • compact() now writes a snapshot state record so compacted files stay replayable
  • New machine module: the three-layer effect sandwich (begin → effect → settle_ok/settle_err) with ReplaySafety contracts
  • Deterministic crash-resume: resume() reads only the durable pending cell → Clean / ReExecute / Fail
  • Sandwich invariant guards (round 2): begin() refuses while a pending cell is set; settle_* verify the handle matches the pending cell (no double-settle, no wrong-handle settle)

Why

The JSONL log from E1 is only crash-safe if program-counter steps are constrained to the legal transition table — otherwise a torn or hand-edited file could silently move the machine anywhere. E2 makes the table an enforced invariant at both write time and replay time, and gives tool effects a durable intent/settlement sandwich so a crash mid-effect resumes deterministically instead of guessing.

Testing

  • 24/24 tests green (13 E1 + 11 E2)
  • Determinism proof: crash simulated at each of 3 sandwich points; resumed state equals the no-crash reference run
  • Tampered log (illegal pc walk) rejected at open()
  • Regression tests: double-begin refused, wrong-handle settle refused, double-settle refused
  • Clippy clean, rustfmt clean

Closes #2

- state.rs: explicit legal transition table (can_transition) per
  architecture §5; illegal moves are errors, never panics
- storage.rs: commit() validates CAS + legality before writing a single
  byte; replay enforces the same table (tampered logs rejected at open)
- Record::State gains a 'snapshot' flag: compact() writes a materialized
  state record, not an illegal transition; backward compatible (serde
  default)
- machine.rs (new): effect sandwich driver — begin (intent + pending
  cell + pc→Executing, one atomic commit) / settle_ok / settle_err /
  finish, with ReplaySafety contracts (idempotent/guarded/never)
  persisted in the intent payload
- Deterministic resume: reads only the durable pending cell, resolves to
  Clean / ReExecute / Fail
- 8 new tests incl. crash-at-each-sandwich-point determinism proof:
  resumed durable state == no-crash reference run
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

Addresses cora review round 2 (both findings valid):
- begin() refuses when the pending cell is already set (one sandwich at
  a time; the cell is the crash sentinel and must never be overwritten)
- settle_ok/settle_err verify the handle matches the pending cell's
  intentId before settling (catches wrong-handle and double-settle)
- new StorageError::Effect variant for sandwich invariant violations
- 3 regression tests: double-begin, wrong-handle settle, double-settle
@ajianaz
ajianaz merged commit 5c66b8c into develop Aug 26, 2026
13 of 14 checks passed
@ajianaz
ajianaz deleted the feat/e2-state-machine branch August 26, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

E2: State machine — program counter, seq CAS, effect sandwich

1 participant