Skip to content

refactor(engine): write thread files through one durable atomic writer - #4417

Merged
LHagfoss merged 2 commits into
mainfrom
refactor/engine-durable-atomic-writer
Nov 25, 2026
Merged

LHagfoss merged 2 commits into
mainfrom
refactor/engine-durable-atomic-writer

Conversation

@LHagfoss

Copy link
Copy Markdown
Owner

What

Thread files now go through one durable writer, rustcode_session::atomic_file::write_atomic. It writes a temporary file whose name is unique to the process and the call, fsyncs it, renames it into place (keeping the Windows replace fallback), and fsyncs the directory on Unix.

Files that now use it:

  • Thread folder: history.json, title.txt, metadata.json, workspace.json, image_cache.json, segment.json, harness.json, status.json and the session settings log.
  • Elsewhere: settings.json, workflow run records, mirror files and the project-relocate edits.

Removed duplicates:

  • Four separate helpers: worktrees::write_atomic, workflow::store::write_atomically, acp::mirror::write_atomic and acp::project_relocate::atomic_write.
  • Three more inline temp-and-rename writers, in harness/store.rs, acp/settings.rs and text_generation.rs.

Several of these used a fixed <name>.tmp, so two writers of the same file could rename each other's half-written temporary.

The history writer used to drop every error silently. A failed write is now reported once per file until a write of it succeeds again, in two places: standard error, and a history.write_failed operational event (error_kind) that the CLI and the harness register at startup.

Changelog

A full disk or a crash can no longer leave a thread's history, status or settings file half-written, and a history that stops saving is now logged.

Testing

  • New tests for write_atomic:
    • it creates parent folders and leaves no temporary file behind
    • eight concurrent writers of one file never leave a partial file
    • a failed write keeps the old file
  • New tests for the history writer:
    • a failure is reported once, again after a success, and the failing state clears
    • no temporary file is left beside history.json
  • Locally: cargo fmt --all -- --check, python3 scripts/check-comments.py, python3 scripts/check-rust-size.py. Compilation and tests run in CI; this machine does not compile Rust.
  • Not measured: how much the added fsync slows a large history write. Writes stay debounced at 250 ms on a background thread, so a turn does not wait on it.

Closes #4406

@LHagfoss LHagfoss added refactor Code improvement and restructuring rust Pull requests that update rust code labels Nov 25, 2026
@LHagfoss
LHagfoss merged commit cfbbde8 into main Nov 25, 2026
6 checks passed
@LHagfoss
LHagfoss deleted the refactor/engine-durable-atomic-writer branch November 25, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Code improvement and restructuring rust Pull requests that update rust code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(engine): write thread files through one durable atomic writer

2 participants