2026-07-12 11:28:39 +07:00
|
|
|
//! Append-only JSONL edit log recording every file mutation made by tools,
|
|
|
|
|
//! for audit and undo/history purposes.
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// A single recorded file edit: which tool made it, to which path, why,
|
|
|
|
|
/// and a content hash/size delta for verification.
|
2026-07-11 13:16:10 +07:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
|
pub struct EditLogEntry {
|
|
|
|
|
pub ts: i64,
|
|
|
|
|
pub tool: String,
|
|
|
|
|
pub path: String,
|
|
|
|
|
pub reason: String,
|
|
|
|
|
pub content_sha256: String,
|
|
|
|
|
pub bytes_delta: i64,
|
|
|
|
|
pub origin: String,
|
|
|
|
|
pub session_id: String,
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// In-memory view of a session's edit log, backed by `edits.jsonl` on disk.
|
2026-07-11 13:16:10 +07:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct EditLog {
|
|
|
|
|
pub entries: Vec<EditLogEntry>,
|
|
|
|
|
pub path: std::path::PathBuf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EditLog {
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Open (or start tracking) the edit log for a session directory,
|
|
|
|
|
/// replaying any existing `edits.jsonl` into memory.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn new(session_dir: &std::path::Path) -> Self {
|
2026-07-12 03:14:52 +07:00
|
|
|
let path = session_dir.join("edits.jsonl");
|
|
|
|
|
let entries = Self::load_from_disk(&path);
|
|
|
|
|
EditLog { entries, path }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Reads every line of edits.jsonl back into memory so callers who create a
|
|
|
|
|
/// *new* EditLog after a previous session can inspect the full history.
|
|
|
|
|
fn load_from_disk(path: &std::path::Path) -> Vec<EditLogEntry> {
|
|
|
|
|
let file = match std::fs::File::open(path) {
|
|
|
|
|
Ok(f) => f,
|
|
|
|
|
Err(_) => return Vec::new(),
|
|
|
|
|
};
|
|
|
|
|
use std::io::{BufRead, BufReader};
|
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
|
reader
|
|
|
|
|
.lines()
|
|
|
|
|
.filter_map(|line| line.ok().and_then(|l| serde_json::from_str(&l).ok()))
|
|
|
|
|
.collect()
|
2026-07-11 13:16:10 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:49:33 +07:00
|
|
|
/// Append one entry to `edits.jsonl` on disk and to the in-memory log,
|
|
|
|
|
/// with fsync for crash safety.
|
2026-07-12 11:28:39 +07:00
|
|
|
///
|
|
|
|
|
/// Flow: serialize `entry` to a JSON line → ensure parent dir exists →
|
2026-07-12 11:49:33 +07:00
|
|
|
/// open the file in append mode → write the line → fsync → push into
|
2026-07-12 11:28:39 +07:00
|
|
|
/// `self.entries`.
|
|
|
|
|
///
|
|
|
|
|
/// Why: appending (not rewriting) keeps the log durable and cheap even
|
2026-07-12 11:49:33 +07:00
|
|
|
/// as it grows across a long session; fsync ensures the entry survives
|
|
|
|
|
/// a crash rather than lingering in the page cache.
|
2026-07-12 11:28:39 +07:00
|
|
|
///
|
|
|
|
|
/// Return: `Ok(())` on success; an `io::Error` if serialization or
|
|
|
|
|
/// any filesystem operation fails.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn append(&mut self, entry: EditLogEntry) -> std::io::Result<()> {
|
|
|
|
|
let line = serde_json::to_string(&entry)? + "\n";
|
|
|
|
|
let parent = self.path.parent().unwrap();
|
|
|
|
|
std::fs::create_dir_all(parent)?;
|
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
|
|
|
|
.create(true)
|
|
|
|
|
.append(true)
|
|
|
|
|
.open(&self.path)?;
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
file.write_all(line.as_bytes())?;
|
2026-07-12 11:49:33 +07:00
|
|
|
file.sync_all()?;
|
2026-07-11 13:16:10 +07:00
|
|
|
self.entries.push(entry);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Number of edit entries recorded so far in this log.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
|
self.entries.len()
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-11 22:10:17 +07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_editlog_new_empty() {
|
|
|
|
|
let dir = std::env::temp_dir().join("editlog_test");
|
|
|
|
|
let _ = std::fs::create_dir_all(&dir);
|
|
|
|
|
let log = EditLog::new(&dir);
|
|
|
|
|
assert_eq!(log.len(), 0);
|
|
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_editlog_append_and_reload() {
|
|
|
|
|
let dir = std::env::temp_dir().join("editlog_append_test");
|
|
|
|
|
let _ = std::fs::create_dir_all(&dir);
|
|
|
|
|
let mut log = EditLog::new(&dir);
|
|
|
|
|
let entry = EditLogEntry {
|
|
|
|
|
ts: 1,
|
|
|
|
|
tool: "write".to_string(),
|
|
|
|
|
path: "test.txt".to_string(),
|
|
|
|
|
reason: "test reason".to_string(),
|
|
|
|
|
content_sha256: "abc123".to_string(),
|
|
|
|
|
bytes_delta: 42,
|
|
|
|
|
origin: "main".to_string(),
|
|
|
|
|
session_id: "sess-1".to_string(),
|
|
|
|
|
};
|
|
|
|
|
log.append(entry.clone()).unwrap();
|
|
|
|
|
assert_eq!(log.len(), 1);
|
2026-07-11 23:45:13 +07:00
|
|
|
assert_eq!(log.entries[0].reason, "test reason");
|
|
|
|
|
assert_eq!(log.entries[0].tool, "write");
|
|
|
|
|
assert_eq!(log.entries[0].path, "test.txt");
|
|
|
|
|
assert_eq!(log.entries[0].bytes_delta, 42);
|
2026-07-11 22:10:17 +07:00
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_editlog_multiple_entries() {
|
|
|
|
|
let dir = std::env::temp_dir().join("editlog_multiple_test");
|
|
|
|
|
let _ = std::fs::create_dir_all(&dir);
|
|
|
|
|
let mut log = EditLog::new(&dir);
|
|
|
|
|
for i in 0..5 {
|
|
|
|
|
log.append(EditLogEntry {
|
|
|
|
|
ts: i,
|
|
|
|
|
tool: "edit".to_string(),
|
|
|
|
|
path: format!("file{}.txt", i),
|
|
|
|
|
reason: format!("reason {}", i),
|
|
|
|
|
content_sha256: "hash".to_string(),
|
|
|
|
|
bytes_delta: 10 + i,
|
|
|
|
|
origin: "main".to_string(),
|
|
|
|
|
session_id: "sess-1".to_string(),
|
|
|
|
|
}).unwrap();
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(log.len(), 5);
|
2026-07-11 23:45:13 +07:00
|
|
|
assert_eq!(log.entries[0].reason, "reason 0");
|
|
|
|
|
assert_eq!(log.entries[4].reason, "reason 4");
|
2026-07-11 22:10:17 +07:00
|
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
|
|
|
}
|
|
|
|
|
}
|