Skip to content

feat(core): JSONL session storage layer (E1) - #14

Merged
ajianaz merged 6 commits into
developfrom
feat/e1-jsonl-storage
Aug 26, 2026
Merged

ajianaz merged 6 commits into
developfrom
feat/e1-jsonl-storage

Conversation

@ajianaz

@ajianaz ajianaz commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

What

Implements Epic 1 (issue #1): durable JSONL session storage foundation per docs/architecture.md.

Model modules:

  • entry.rs — write-once conversation tree entries + EntryType taxonomy
  • register.rs — namespaced mutable cells (lane.leaf, op.state, pending, fact)
  • state.rs — operation state machine (Pc program counter + seq CAS token)

JSONL backend (storage.rs):

  • One file per session; replay-on-open recipe format (header + transaction lines)
  • Strictly increasing global seq across all record kinds
  • Atomic commit: validate-all-then-append, single physical line per transaction
  • Torn final line (crash mid-append) discarded whole on open
  • Newer storageVersion = hard error; older replays cleanly
  • Compaction rewrites header + live records; fsync + atomic rename + dir fsync
  • Stale transition (CAS mismatch) rejected, never overwritten
  • Invalid commits rejected before any byte is written

Why

The storage layer is the durability foundation every later epic (provider loop, tools, approvals) commits through. It must guarantee: crash safety (torn tail tolerated, committed lines durable via fsync), monotonic seq, and CAS-protected state transitions — before any higher layer is built on it.

Testing

12/12 integration tests green (cargo test -p cora-agent-core):

  • full-session roundtrip through dyn Storage
  • compact + reopen preserves logical state (incl. usage rows — regression test for the CodeCora finding)
  • torn final line discarded whole; corruption mid-file is a hard error
  • stale CAS transition rejected, machine untouched
  • invalid commits (unknown parent/namespace, dup id, set-without-value) rejected with zero bytes written
  • intra-commit parent chaining; empty-string parent rejected
  • seq monotonicity across mixed record kinds, survives reopen

Closes #1

Implement the durable storage foundation per docs/architecture.md:

- entry.rs: write-once tree entries with EntryType taxonomy
- register.rs: namespaced mutable cells (lane/op/pending/fact)
- state.rs: operation state machine (pc + seq CAS token)
- storage.rs: Storage trait + JsonlStorage backend
  * one file per session, replay-on-open recipe format
  * strictly increasing global seq across all record kinds
  * atomic commit: validate-all-then-append single line
  * torn final line discarded whole (crash mid-append safe)
  * newer storageVersion = hard error, older replays cleanly
  * compaction rewrites header + live records, seq preserved
  * stale transition (CAS mismatch) rejected, never overwritten

11 integration tests cover all E1 acceptance criteria incl.
roundtrip, compact, torn-tail, stale CAS, invalid commits,
intra-commit parent chaining, and seq monotonicity.
Address cora review MAJOR: compact() renamed temp file over the
live path without fsync, risking data loss on crash. Now:
- append_line fsyncs after every commit line (committed records
  never lost; torn tail still discarded whole on open)
- compact: write temp -> fsync -> rename -> fsync parent dir
  (POSIX; no-op on non-Unix targets)
@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

CodeCora finding: commit() serialized usage records into the commit
line but never pushed them to the in-memory list. Consequence:
compact() rewrites the file from that list, silently dropping every
usage row committed since open. Regression test now asserts usage
rows survive compact and reopen.

Also add Storage::usages() accessor (was missing from the trait).
CodeCora finding (valid): open() replayed around a torn final line
but left the partial bytes on disk. The append handle then wrote the
next commit line directly onto the fragment, permanently corrupting
the file (the merged line is no longer the final line, so the next
open fails with a hard Corrupt error).

open() now tracks the end of the last complete line and truncates +
fsyncs the torn bytes away before the writer is ever used. The
torn-tail test now commits after reopening a torn file and reopens
again to prove the file stays valid.
serde_json::from_str::<Record>(&line).map(|r| vec![r])
};
let records = match parsed {
Ok(r) => r,
CodeCora finding (valid): the replay loop's 'continue' for blank
lines skipped the good_bytes increment, so a stray blank line made
open() believe the file had a torn tail and truncate away the last
valid commits — silent data loss.

good_bytes now advances immediately after each complete physical
line is read, before any skip logic. Regression test opens a file
with an injected blank line and asserts both entries survive a
double reopen.
@ajianaz
ajianaz merged commit 1aaea81 into develop Aug 26, 2026
11 checks passed
@ajianaz
ajianaz deleted the feat/e1-jsonl-storage branch August 26, 2026 06:47
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.

E1: Storage layer — JSONL session file + Storage trait

2 participants