fix(protocol): handle non-UTF-8 files in read_file_snippet

When reading Obsidian vault notes or source files, non-UTF-8 bytes
caused a hard error. Now gracefully breaks on invalid UTF-8 lines
and returns whatever was read so far.

Closes #91

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
林 駿甫 (Shunsuke Hayashi) 2026-04-10 10:07:45 +09:00
parent 32f034e2b2
commit 6045f67647

View file

@ -1211,7 +1211,13 @@ fn read_file_snippet(path: &Path, max_lines: usize) -> Result<String, Error> {
let reader = BufReader::new(file); let reader = BufReader::new(file);
let mut lines = Vec::new(); let mut lines = Vec::new();
for line in reader.lines().take(max_lines) { for line in reader.lines().take(max_lines) {
lines.push(line?); match line {
Ok(l) => lines.push(l),
Err(_) => {
// Non-UTF-8 file (binary) — return what we have so far
break;
}
}
} }
Ok(lines.join("\n")) Ok(lines.join("\n"))
} }