Session event log: non-atomic rewrite without fsync can destroy history; metadata not crash-safe (corrupt manifest makes session unopenable)
Summary
The per-session event log (vtcode-memory, the canonical store for session history) is not crash-safe in three independent ways: (1) cap enforcement rewrites the canonical events.jsonl non-atomically with no fsync anywhere in the crate; (2) manifest.json/turns.json are written with plain fs::write (despite doc comments saying "Atomically write"), and a partially-written manifest makes the whole session unopenable — the documented scan() fallback is unreachable; (3) the open() fast path trusts stale metadata without validating offsets against the actual file length. Additionally, session files are created with default umask permissions (typically 0644) and contain the full conversation including tool outputs.
Details
-
Non-atomic cap rewrite, no fsync —
crates/codegen/vtcode-memory/src/event_log.rs:331-370(enforce_event_cap): reads the tail into memory,file.set_len(0), thenwrite_all(remaining), thenflush(). A crash/ENOSPC/OOM betweenset_len(0)and completion permanently truncates the canonical log, while the manifest still claims the events exist.grep sync_all|sync_dataover the crate returns nothing —flush()only flushes the userspace buffer. -
Corrupt manifest makes the session unopenable —
manifest.rs:36-48: the doc comment promisesOk(None)for unreadable files so the caller can "fall back to scanning the event log", but the implementation returnsErr(...)for a JSON parse error, andopen()(event_log.rs:282-283) propagates it with?BEFORE the_ => log.scan()?fallback branch. Sincewrite_manifest/write_turn_indexuse plainfs::write(truncate+write,manifest.rs:62-76, despite the "Atomically write" doc), a crash mid-write leaves truncated JSON → the session can never be opened again, even thoughevents.jsonl(the canonical store per the crate's own AGENTS.md) is intact. -
Fast path trusts stale metadata —
event_log.rs:284-292: when both manifest and index exist they are accepted without checkingindex.end_offset <= file_len. After a crash between a buffered flush and metadata persistence,reconstruct_turnreads at wrong offsets (read_exact,event_log.rs:393-397) and fails with UnexpectedEof on every subsequent open. -
Permissions — files are created via
OpenOptions::new().create(true).read(true).append(true)with no mode set (event_log.rs:261-266); noPermissionsExtusage exists in the crate. On multi-user machines (default umask → 0644) any local user can read the full conversation transcript, including tool outputs that may contain secrets. The project already has a proper primitive (vtcode_commonsprivate-file atomic write: 0600 + O_NOFOLLOW) used elsewhere.
Steps to reproduce
- (1): run a session that exceeds the event cap, kill -9 the process during cap enforcement (or simulate ENOSPC) → the remaining history is gone.
- (2): truncate
manifest.jsonof any existing session (simulating a crash mid-write) → opening the session now always fails, even thoughevents.jsonlis intact andscan()could rebuild everything. - (3): append bytes to
events.jsonlwithout updating the index (simulating the crash window) →reconstruct_turnfails with UnexpectedEof forever. - (4):
ls -l .vtcode/sessions/<id>/events.jsonl→ 0644.
Suggested fix
- Rewrite cap enforcement as temp-file + fsync + atomic rename.
- Map manifest/index parse errors to
Ok(None)as documented, so the existingscan()fallback actually runs; write metadata via the same atomic pattern. - In
open(), validateindex.end_offset <= file_lenbefore taking the fast path. - Create session files/dirs with 0600/0700 (the private-file primitives already exist in vtcode-commons).
Impact
Permanent loss of session history after a crash (the store advertised as canonical); sessions become unopenable requiring manual file deletion; information disclosure of full transcripts to other local users. No remote vector — local reliability/privacy issue.
Version info: verified against 0abcebab3 (v0.147.x). Re-checked against current main (v0.149.0, docs/providers/zai.md commit): all referenced files and line numbers are unchanged (git diff 0abcebab3..main is empty for every file cited above), so the report applies to 0.149.0 as-is.
Addressed on this branch.
- Atomic, synced event-log compaction and metadata writes.
- Corrupt metadata and out-of-range offsets recover by scanning events.jsonl.
- Session directories/files are private (0700/0600).
- Added compaction, recovery, reopen, and permission regression tests.
Verified: vtcode-memory tests and clippy pass with --locked / -D warnings.
Resolved in the current branch.
The crash-safe/private session-log fix and regression coverage are in place. Verified: 81 vtcode-memory tests and clippy with -D warnings.
Closing #744.
Hi! The fix doesn't appear to be pushed yet — no commit/branch/PR with the crash-safe event-log changes on GitHub. Could you push it?
Hi @7jrxt42BxFZo4iAnN4CX I have pushed latest changes on main, you can check