Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions harness/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
// preserves the message + location in debug.log even when the process
// dies without a crash report.
rustcode::logger::install_panic_hook();
rustcode::sessions::report_history_write_failures();
// Provider keys are often exported in `~/.zshrc` (interactive-only) and
// invisible to GUI/systemd/IDE launches. Hydrate missing keys from the
// login shell once at startup so profiles, MCP servers, and tool shells
Expand Down
1 change: 1 addition & 0 deletions rustcode/cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,7 @@ pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
}
rustcode::logger::rotate_if_oversized();
rustcode::logger::install_panic_hook();
rustcode::sessions::report_history_write_failures();
hydrate_shell_provider_keys();

if cli_args.init || matches!(cli_args.command.as_ref(), Some(crate::cli::Commands::Init)) {
Expand Down
204 changes: 204 additions & 0 deletions rustcode/core/rustcode-session/src/atomic_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//! Durable whole-file replacement: the one way RustCode writes a file that a
//! crash must never leave empty or half-written.

use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

/// Write `bytes` to `path` so a reader sees either the old file or the whole
/// new one, never a truncated mix, even after a crash or power loss: a
/// temporary file beside it that no other writer shares, flushed to disk,
/// renamed over `path`, and on Unix the directory entry flushed too. The
/// folder must exist: a write never brings back a folder that was removed.
pub fn write_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> {
let temporary = temporary_path(path);
let written = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&temporary)
.and_then(|mut file| {
file.write_all(bytes)?;
file.sync_all()
})
.and_then(|()| replace_file(&temporary, path));
if let Err(error) = written {
let _ = fs::remove_file(&temporary);
return Err(error);
}
sync_parent(path);
Ok(())
}

/// Replace `destination` with `source`, preserving the old destination while
/// the replacement is being installed on platforms that cannot rename over an
/// existing file.
pub fn replace_file(source: &Path, destination: &Path) -> io::Result<()> {
match fs::rename(source, destination) {
Ok(()) => Ok(()),
Err(_error) if cfg!(windows) => {
let backup = backup_path(destination);
let destination_exists = destination.exists();
if destination_exists {
fs::rename(destination, &backup)?;
}

match fs::rename(source, destination) {
Ok(()) => {
if destination_exists {
let _ = fs::remove_file(backup);
}
Ok(())
}
Err(replace_error) => {
if destination_exists {
let _ = fs::rename(&backup, destination);
}
Err(replace_error)
}
}
}
Err(error) => Err(error),
}
}

/// A temporary name beside `path` that is unique to this process and call,
/// so two writers of the same file never write into each other's temporary.
fn temporary_path(path: &Path) -> PathBuf {
static NEXT: AtomicU64 = AtomicU64::new(0);
let name = path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("file");
let sequence = NEXT.fetch_add(1, Ordering::Relaxed);
path.with_file_name(format!(".{name}.{}.{sequence}.tmp", std::process::id()))
}

fn backup_path(destination: &Path) -> PathBuf {
let name = destination
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("file");
destination.with_file_name(format!(".{name}.rustcode-backup-{}", std::process::id()))
}

/// Make the rename itself durable. Best effort: the file is already in place.
fn sync_parent(path: &Path) {
if cfg!(unix)
&& let Some(parent) = path
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
{
let _ = fs::File::open(parent).and_then(|directory| directory.sync_all());
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Arc, Barrier};

#[test]
fn replaces_existing_file() {
let directory = tempfile::TempDir::new().unwrap();
let source = directory.path().join("source");
let destination = directory.path().join("destination");
fs::write(&source, b"new").unwrap();
fs::write(&destination, b"old").unwrap();

replace_file(&source, &destination).unwrap();

assert_eq!(fs::read(&destination).unwrap(), b"new");
assert!(!source.exists());
}

#[test]
fn replacement_failure_keeps_existing_file() {
let directory = tempfile::TempDir::new().unwrap();
let source = directory.path().join("missing-source");
let destination = directory.path().join("destination");
fs::write(&destination, b"old").unwrap();

assert!(replace_file(&source, &destination).is_err());

assert_eq!(fs::read(&destination).unwrap(), b"old");
}

#[test]
fn writes_and_leaves_no_temporary() {
let directory = tempfile::TempDir::new().unwrap();
let path = directory.path().join("file.json");

write_atomic(&path, b"first").unwrap();
write_atomic(&path, b"second").unwrap();

assert_eq!(fs::read(&path).unwrap(), b"second");
let names: Vec<_> = fs::read_dir(path.parent().unwrap())
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect();
assert_eq!(names, ["file.json"]);
}

#[test]
fn a_missing_folder_is_not_created() {
let directory = tempfile::TempDir::new().unwrap();
let folder = directory.path().join("removed");

assert!(write_atomic(&folder.join("file.json"), b"x").is_err());

assert!(!folder.exists());
}

#[test]
fn concurrent_writers_never_leave_a_partial_file() {
let directory = tempfile::TempDir::new().unwrap();
let path = Arc::new(directory.path().join("shared.json"));
let writers = 8;
let barrier = Arc::new(Barrier::new(writers));
let handles: Vec<_> = (0..writers)
.map(|writer| {
let path = Arc::clone(&path);
let barrier = Arc::clone(&barrier);
std::thread::spawn(move || {
let body = vec![b'a' + writer as u8; 256 * 1024];
barrier.wait();
for _ in 0..8 {
write_atomic(&path, &body).unwrap();
}
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}

let written = fs::read(&*path).unwrap();
assert_eq!(written.len(), 256 * 1024);
assert!(written.iter().all(|byte| *byte == written[0]));
}

#[test]
fn a_failed_write_keeps_the_old_file_and_no_temporary() {
let directory = tempfile::TempDir::new().unwrap();
let path = directory.path().join("file.json");
fs::write(&path, b"old").unwrap();
let under_a_file = path.join("child.json");

assert!(write_atomic(&under_a_file, b"new").is_err());

assert_eq!(fs::read(&path).unwrap(), b"old");
let temporaries = fs::read_dir(directory.path())
.unwrap()
.filter(|entry| {
entry
.as_ref()
.unwrap()
.file_name()
.to_string_lossy()
.ends_with(".tmp")
})
.count();
assert_eq!(temporaries, 0);
}
}
58 changes: 58 additions & 0 deletions rustcode/core/rustcode-session/src/history_write_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use super::*;

fn reported() -> &'static Mutex<Vec<PathBuf>> {
static REPORTED: OnceLock<Mutex<Vec<PathBuf>>> = OnceLock::new();
REPORTED.get_or_init(Default::default)
}

fn record_failure(path: &Path, _error: &std::io::Error) {
lock(reported()).push(path.to_path_buf());
}

fn reports_of(path: &Path) -> usize {
lock(reported()).iter().filter(|seen| *seen == path).count()
}

#[test]
fn a_failing_history_write_is_reported_once_until_it_succeeds() {
on_history_write_failure(record_failure);
let root = tempfile::TempDir::new().unwrap();
let blocker = root.path().join("thread");
std::fs::write(&blocker, b"a file where the folder should be").unwrap();
let path = blocker.join(HISTORY_FILE);
let history = vec![ChatMessage::new("user", "hello")];

write_history_file_unguarded(&path, &history);
write_history_file_unguarded(&path, &history);

assert!(history_write_failing(&path));
assert_eq!(reports_of(&path), 1);

std::fs::remove_file(&blocker).unwrap();
write_history_file_unguarded(&path, &history);

assert!(!history_write_failing(&path));
assert!(path.is_file());

std::fs::remove_dir_all(&blocker).unwrap();
std::fs::write(&blocker, b"blocked again").unwrap();
write_history_file_unguarded(&path, &history);

assert_eq!(reports_of(&path), 2);
}

#[test]
fn history_writes_leave_no_temporary_beside_the_file() {
let root = tempfile::TempDir::new().unwrap();
let path = root.path().join(HISTORY_FILE);
let history = vec![ChatMessage::new("user", "hello")];

write_history_file_unguarded(&path, &history);
write_history_file_unguarded(&path, &history);

let names: Vec<_> = std::fs::read_dir(root.path())
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
.collect();
assert_eq!(names, [HISTORY_FILE]);
}
Loading
Loading