Repository navigation
Description
Summary
JsonlStorage guards against stale writes only with an in-memory CAS (expected_seq vs self.state.seq, storage.rs:881). Nothing stops a second process from appending to the same session file. docs/architecture.md:171 says "CAS on state.seq protects against double-drive (e.g. resume + host race) — only one wins" — that holds inside one process only.
Live repro (develop @ 438ad0d, i.e. post-v0.9.0 with an [Unreleased] section; workspace version 0.9.0; Linux aarch64; mock OpenAI-compatible server, no real provider)
tole run "first turn" --sessions-dir S-> session<id>, 5 records, last seq 5.- Process A:
tole chat --resume <id> --sessions-dir S, send "from A, message 1" -> last seq 10. - Process B (while A idles):
tole resume <id> "from B" --sessions-dir S-> rc 0, last seq 15. - Process A sends "from A, message 2" -> rc 0, no error; it appends with its stale in-memory seq, so seq 11-15 are written a second time.
tole status <id>andtole resume <id> ...-> rc 1:
tole: replaying session log: corrupt session file: line 11: seq 11 not strictly increasing (last 15)
Result: 20 records, duplicated seq 11-15, session can no longer be opened. open_with repair only truncates a torn EOF tail, so this is not self-healing.
Impact
Any two writers: a tole serve / ACP / MCP daemon holding a live session plus a CLI tole resume (the global session index, #439, lists sessions across projects), or two terminals. Only chat + resume was reproduced; the other combinations share the same commit path.
Inferred from source, not reproduced: compact() renames a fresh file over the live path, so a stale second handle would append to the unlinked inode (silent loss).
Proposal
- Advisory exclusive lock for the lifetime of every writable handle (
File::try_lock, stable since Rust 1.89; the workspace has norust-version, CI usesstable; check the Android/mobile profile).open_read_onlystays lock-free. - The lock must survive
compact(): it renamesjsonl.tmpover the live path, and an advisory lock on the old inode is gone after that. Reproduced with two real processes andFile::try_lock: after the rename a second process gets the lock; locking the temp file before the rename, or holding a sidecar<id>.jsonl.lockthat is never renamed, keeps it refused. - Take the lock before the torn-tail repair in
open_with(a writable open truncates, and another process may be mid-append). - Belt and braces: before each append compare the file length with the last known good length (one
fstat, negligible next to the ~5 msfdatasyncper commit) and fail with a stale-handle error instead of appending. - Error names the holder ("session is open for writing by pid N") instead of a generic failure.
- Separate follow-up (own issue):
tole session recover <id>that copies the longest valid prefix into a new session. fx shipsfx session recoverandfx doctorreports session damage.
Acceptance
- E2E with two REAL processes (the repro above): the second writer gets a clear error; no duplicate seq in the file;
statusandresumework afterwards. - Read-only commands (
status,sessions) succeed while a writer holds the lock. - Lock is released on crash/SIGKILL (OS lock); the existing E5 crash-resume tests still pass.
- serve / ACP / MCP faces: a CLI
tole resumeagainst a session held by the daemon fails with the holder message instead of corrupting. -
docs/architecture.mdsection 9 corrected. - A lock held across
compact(): a second process is still refused after the rename. - The torn-tail repair runs only while the lock is held.
- Both feature profiles (
--no-default-features) build and test.
Found by
Competitor research 2026-11-02 (fx durable-session TLA+ model Fencing.tla: "a process replaced while it ran is refused at its next write"), then a live repro against develop @ 438ad0d. The repro above is complete and needs only a mock OpenAI-compatible server.
Activity
[-]storage: no cross-process writer exclusion \u2014 a second writer corrupts the session file (live repro)[/-][+]storage: no cross-process writer exclusion — a second writer corrupts the session file (live repro)[/+]fix(storage): retry a writer lock that names our own pid (#552) (#557)