VTCode icon indicating copy to clipboard operation
VTCode copied to clipboard

Session event log: non-atomic rewrite without fsync can destroy history; metadata not crash-safe (corrupt manifest makes session unopenable)

Open 7jrxt42BxFZo4iAnN4CX opened this issue 3 months ago • 5 comments

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

  1. 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), then write_all(remaining), then flush(). A crash/ENOSPC/OOM between set_len(0) and completion permanently truncates the canonical log, while the manifest still claims the events exist. grep sync_all|sync_data over the crate returns nothing — flush() only flushes the userspace buffer.

  2. Corrupt manifest makes the session unopenable — manifest.rs:36-48: the doc comment promises Ok(None) for unreadable files so the caller can "fall back to scanning the event log", but the implementation returns Err(...) for a JSON parse error, and open() (event_log.rs:282-283) propagates it with ? BEFORE the _ => log.scan()? fallback branch. Since write_manifest/write_turn_index use plain fs::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 though events.jsonl (the canonical store per the crate's own AGENTS.md) is intact.

  3. Fast path trusts stale metadata — event_log.rs:284-292: when both manifest and index exist they are accepted without checking index.end_offset <= file_len. After a crash between a buffered flush and metadata persistence, reconstruct_turn reads at wrong offsets (read_exact, event_log.rs:393-397) and fails with UnexpectedEof on every subsequent open.

  4. Permissions — files are created via OpenOptions::new().create(true).read(true).append(true) with no mode set (event_log.rs:261-266); no PermissionsExt usage 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_commons private-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.json of any existing session (simulating a crash mid-write) → opening the session now always fails, even though events.jsonl is intact and scan() could rebuild everything.
  • (3): append bytes to events.jsonl without updating the index (simulating the crash window) → reconstruct_turn fails 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 existing scan() fallback actually runs; write metadata via the same atomic pattern.
  • In open(), validate index.end_offset <= file_len before 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.

7jrxt42BxFZo4iAnN4CX avatar Aug 28 '26 07:08 7jrxt42BxFZo4iAnN4CX

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.

7jrxt42BxFZo4iAnN4CX avatar Aug 28 '26 07:08 7jrxt42BxFZo4iAnN4CX

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.

vinhnx avatar Aug 29 '26 13:08 vinhnx

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.

vinhnx avatar Aug 29 '26 13:08 vinhnx

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?

7jrxt42BxFZo4iAnN4CX avatar Aug 29 '26 14:08 7jrxt42BxFZo4iAnN4CX

Hi @7jrxt42BxFZo4iAnN4CX I have pushed latest changes on main, you can check

vinhnx avatar Aug 30 '26 02:08 vinhnx