Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(wikisync): count the final unterminated line in diff summaries
Rudy Mizrahi Celekli5ead357f0807
diff --git a/companions/ai-memory-wikisync/src/sync.rs b/companions/ai-memory-wikisync/src/sync.rsindex 3f654357..8b8de240 100644--- a/companions/ai-memory-wikisync/src/sync.rs+++ b/companions/ai-memory-wikisync/src/sync.rs@@ -190,8 +190,8 @@ pub fn diff_summary(disk: Option<&[u8]>, server: Option<&[u8]>) -> String {let Some(server) = server else {return "server content unchanged since the last export (revalidated by ETag)".to_string();};- let local_lines = disk.iter().filter(|byte| **byte == b'\n').count();- let server_lines = server.iter().filter(|byte| **byte == b'\n').count();+ let local_lines = line_count(disk);+ let server_lines = line_count(server);// Compare the first missing line too, even when the shared prefix is identical.let first_difference = disk.split(|byte| *byte == b'\n')@@ -224,6 +224,14 @@ pub fn diff_summary(disk: Option<&[u8]>, server: Option<&[u8]>) -> String {)}+/// Lines in a file's bytes. Content not ending in a newline still has a+/// final line, which the first-difference position below also counts, so+/// the two numbers in one summary cannot contradict each other.+fn line_count(bytes: &[u8]) -> usize {+ let newlines = bytes.iter().filter(|byte| **byte == b'\n').count();+ newlines + usize::from(!bytes.is_empty() && !bytes.ends_with(b"\n"))+}+fn preview(line: Option<&[u8]>) -> String {let Some(line) = line else {return "<end of file>".to_string();@@ -716,6 +724,41 @@ mod tests {}}+ #[test]+ fn diff_summary_counts_a_final_unterminated_line() {+ type Case<'a> = (&'a [u8], &'a [u8], &'a str);+ let cases: &[Case<'_>] = &[+ (+ b"one",+ b"one\nextra",+ "local 3 bytes/1 lines vs server 9 bytes/2 lines",+ ),+ (+ b"one\n",+ b"one\nextra\n",+ "local 4 bytes/1 lines vs server 10 bytes/2 lines",+ ),+ // Control: an empty file has no final line to count.+ (+ b"",+ b"extra",+ "local 0 bytes/0 lines vs server 5 bytes/1 lines",+ ),+ ];+ for (disk, server, expected) in cases {+ let summary = diff_summary(Some(disk), Some(server));+ assert!(summary.starts_with(expected), "{summary}");+ }++ // A difference inside both files must not sit past a reported count.+ let summary = diff_summary(Some(b"---\n---\nbody"), Some(b"---\n---\nbodyX"));+ assert!(+ summary.starts_with("local 12 bytes/3 lines vs server 13 bytes/3 lines"),+ "{summary}"+ );+ assert!(summary.contains("first difference at line 3"), "{summary}");+ }+#[test]fn diff_summary_keeps_previews_bounded_and_printable() {let mut disk = vec![b'a'; 57];