Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): keep Antigravity step output around invalid UTF-8 bytes
Rudy Celekli17461ee52363
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fee7fa0b..adb60bfa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,10 @@ version in [`docs/changelog/`](docs/changelog/):## [Unreleased]### Fixed+- Kept Antigravity step output that contains bytes which are not valid UTF-8,+ such as cp1252 text from a Windows command or binary data. Everything after+ the first invalid byte was dropped, and output starting with one was recorded+ as "(no output captured)"; invalid bytes now become U+FFFD.- Fixed `memory_lint` reporting findings for TTL-expired pages. The lint passnow omits expired pages before evaluating episodic staleness, rule suggestions,duplicate titles, and vector or LLM contradiction candidates, preventingdiff --git a/crates/ai-memory-hooks/src/antigravity.rs b/crates/ai-memory-hooks/src/antigravity.rsindex 9e320ffd..4173ab35 100644--- a/crates/ai-memory-hooks/src/antigravity.rs+++ b/crates/ai-memory-hooks/src/antigravity.rs@@ -100,13 +100,7 @@ pub fn read_step_output(output_path: &Path, max_bytes: usize) -> Option<String>return None;}- let text = match std::str::from_utf8(&buffer) {- Ok(valid) => valid,- Err(err) => {- let valid = &buffer[..err.valid_up_to()];- std::str::from_utf8(valid).unwrap_or_default()- }- };+ let text = decode_step_output(&buffer);let trimmed = text.trim_end();if trimmed.trim().is_empty() {None@@ -115,6 +109,32 @@ pub fn read_step_output(output_path: &Path, max_bytes: usize) -> Option<String>}}+/// Decode step output read under a byte cap. A code point the cap cut in+/// half at the end is dropped; any other invalid byte (a legacy code page+/// such as cp1252, or binary output) becomes U+FFFD, so the text after it is+/// kept instead of being discarded with it.+fn decode_step_output(buffer: &[u8]) -> String {+ let mut out = String::with_capacity(buffer.len());+ let mut rest = buffer;+ loop {+ match std::str::from_utf8(rest) {+ Ok(valid) => {+ out.push_str(valid);+ return out;+ }+ Err(err) => {+ let (valid, after) = rest.split_at(err.valid_up_to());+ out.push_str(std::str::from_utf8(valid).unwrap_or_default());+ let Some(invalid_len) = err.error_len() else {+ return out;+ };+ out.push(char::REPLACEMENT_CHARACTER);+ rest = &after[invalid_len..];+ }+ }+ }+}+fn extract_step_idx(raw: &serde_json::Value) -> Option<u64> {if let Some(idx) = raw.get("stepIdx").and_then(serde_json::Value::as_u64) {return Some(idx);@@ -301,6 +321,36 @@ mod tests {assert_eq!(read, Some("abc".to_string()));}+ /// Output that is not valid UTF-8 (cp1252 text from a Windows command,+ /// or binary data) must not lose everything after the first bad byte,+ /// nor all of it when the bad byte comes first.+ #[test]+ fn test_read_step_output_keeps_text_around_invalid_utf8() {+ let dir = tempdir().unwrap();+ let file_path = dir.path().join("output.txt");++ fs::write(&file_path, b"ok\n\xff tail").unwrap();+ assert_eq!(+ read_step_output(&file_path, 1024),+ Some("ok\n\u{FFFD} tail".to_string())+ );++ fs::write(&file_path, b"\xe9chec: build failed").unwrap();+ assert_eq!(+ read_step_output(&file_path, 1024),+ Some("\u{FFFD}chec: build failed".to_string())+ );++ // An invalid byte and a code point cut by the cap in one read.+ let mut bytes = b"a\xffb".to_vec();+ bytes.extend_from_slice("🦀".as_bytes());+ fs::write(&file_path, &bytes).unwrap();+ assert_eq!(+ read_step_output(&file_path, 5),+ Some("a\u{FFFD}b".to_string())+ );+ }+#[test]fn test_read_step_output_missing_or_empty() {// Arrange