Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(wikisync): report appended and removed lines in diff summaries

Jun5166229390e1955
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e3539787..60f8bcd0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,9 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
### Fixed
+- Reported appended and removed lines in wiki sync conflict summaries, with
+ an explicit end-of-file marker instead of line 0 and matching previews.
+ ([#1477](https://github.com/akitaonrails/ai-memory/issues/1477))
- Routed OpenCode's `claude-haiku-5-5` through the Anthropic Messages API for
both Go and Zen instead of the unsupported Chat Completions protocol.
([#1470](https://github.com/akitaonrails/ai-memory/issues/1470))
diff --git a/companions/ai-memory-wikisync/README.md b/companions/ai-memory-wikisync/README.md
index 242182b0..4a806097 100644
--- a/companions/ai-memory-wikisync/README.md
+++ b/companions/ai-memory-wikisync/README.md
@@ -112,7 +112,9 @@ This section describes `plan` and `export`; `sync` adds the rules under
are replaced atomically (tmp + rename + fsync).
- **Local edits win until forced.** Each page is compared three ways:
destination file, last exported state, server body. A file that
- diverged from both is reported with a diff summary and refused; the
+ diverged from both is reported with a diff summary and refused. The summary
+ identifies the first differing line, including appended or removed lines;
+ a missing line is shown as `<end of file>`. The
whole batch is then refused and nothing is written. `--force` overwrites the
divergent files with server content.
- **Never deletes.** `plan` and `export` never delete local files or server
diff --git a/companions/ai-memory-wikisync/src/sync.rs b/companions/ai-memory-wikisync/src/sync.rs
index c8ff3ace..3f654357 100644
--- a/companions/ai-memory-wikisync/src/sync.rs
+++ b/companions/ai-memory-wikisync/src/sync.rs
@@ -192,22 +192,28 @@ pub fn diff_summary(disk: Option<&[u8]>, server: Option<&[u8]>) -> String {
};
let local_lines = disk.iter().filter(|byte| **byte == b'\n').count();
let server_lines = server.iter().filter(|byte| **byte == b'\n').count();
+ // Compare the first missing line too, even when the shared prefix is identical.
let first_difference = disk
.split(|byte| *byte == b'\n')
- .zip(server.split(|byte| *byte == b'\n'))
+ .map(Some)
+ .chain(std::iter::once(None))
+ .zip(
+ server
+ .split(|byte| *byte == b'\n')
+ .map(Some)
+ .chain(std::iter::once(None)),
+ )
.position(|(local, remote)| local != remote)
.map_or(0, |index| index + 1);
let local_line = disk
.split(|byte| *byte == b'\n')
- .nth(first_difference.saturating_sub(1))
- .unwrap_or_default();
+ .nth(first_difference.saturating_sub(1));
let server_line = server
.split(|byte| *byte == b'\n')
- .nth(first_difference.saturating_sub(1))
- .unwrap_or_default();
+ .nth(first_difference.saturating_sub(1));
format!(
"local {} bytes/{} lines vs server {} bytes/{} lines; first difference at line \
- {}: local {:?} vs server {:?}",
+ {}: local {} vs server {}",
disk.len(),
local_lines,
server.len(),
@@ -218,8 +224,12 @@ pub fn diff_summary(disk: Option<&[u8]>, server: Option<&[u8]>) -> String {
)
}
-fn preview(line: &[u8]) -> String {
- line.iter()
+fn preview(line: Option<&[u8]>) -> String {
+ let Some(line) = line else {
+ return "<end of file>".to_string();
+ };
+ let text: String = line
+ .iter()
.take(60)
.map(|byte| {
if byte.is_ascii_graphic() || *byte == b' ' {
@@ -228,7 +238,8 @@ fn preview(line: &[u8]) -> String {
'?'
}
})
- .collect()
+ .collect();
+ format!("{text:?}")
}
/// Atomic file replacement inside the destination: write a hidden tmp file
@@ -674,6 +685,47 @@ mod tests {
assert!(revalidated.contains("revalidated by ETag"), "{revalidated}");
}
+ #[test]
+ fn diff_summary_reports_appended_and_removed_lines() {
+ type Case<'a> = (&'a [u8], &'a [u8], usize, &'a str, &'a str);
+ let cases: &[Case<'_>] = &[
+ (b"one", b"one\nextra", 2, "<end of file>", "\"extra\""),
+ (b"one\nextra", b"one", 2, "\"extra\"", "<end of file>"),
+ (b"one\n", b"one\nextra\n", 2, "\"\"", "\"extra\""),
+ (b"one\nextra\n", b"one\n", 2, "\"extra\"", "\"\""),
+ (b"", b"extra", 1, "\"\"", "\"extra\""),
+ (b"extra", b"", 1, "\"extra\"", "\"\""),
+ (b"one", b"one\n", 2, "<end of file>", "\"\""),
+ (b"one\n", b"one", 2, "\"\"", "<end of file>"),
+ (b"", b"\n", 2, "<end of file>", "\"\""),
+ (b"\n", b"", 2, "\"\"", "<end of file>"),
+ (
+ b"one",
+ b"one\n<end of file>",
+ 2,
+ "<end of file>",
+ "\"<end of file>\"",
+ ),
+ ];
+ for (disk, server, line, local_preview, server_preview) in cases {
+ let summary = diff_summary(Some(disk), Some(server));
+ let expected = format!(
+ "first difference at line {line}: local {local_preview} vs server {server_preview}"
+ );
+ assert!(summary.ends_with(&expected), "{summary}");
+ }
+ }
+
+ #[test]
+ fn diff_summary_keeps_previews_bounded_and_printable() {
+ let mut disk = vec![b'a'; 57];
+ disk.extend_from_slice("é\x1b[31mhidden".as_bytes());
+ let summary = diff_summary(Some(&disk), Some(b"changed"));
+ let expected = format!("local \"{}???\" vs server \"changed\"", "a".repeat(57));
+ assert!(summary.ends_with(&expected), "{summary}");
+ assert!(!summary.contains('\x1b'), "{summary}");
+ }
+
#[test]
fn atomic_write_replaces_and_never_deletes_siblings() {
let tmp = tempfile::tempdir().unwrap();