From 389bb5953199173ab61a8c5106c484dd79edd63e Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Sat, 16 Aug 2025 14:23:28 -0300 Subject: [PATCH] feat: add early binary detection and prevent duplicate metadata saving Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) --- src/meta_plugin.rs | 1 + src/meta_plugin/system.rs | 8 ++++++++ src/modes/save.rs | 16 +++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/meta_plugin.rs b/src/meta_plugin.rs index b630665..c0188c9 100644 --- a/src/meta_plugin.rs +++ b/src/meta_plugin.rs @@ -74,6 +74,7 @@ pub trait MetaPlugin { value, }; crate::db::store_meta(conn, meta)?; + self.saved_during_io = true; Ok(()) } } diff --git a/src/meta_plugin/system.rs b/src/meta_plugin/system.rs index d5d5ece..6952dae 100644 --- a/src/meta_plugin/system.rs +++ b/src/meta_plugin/system.rs @@ -22,6 +22,7 @@ pub struct BinaryMetaPlugin { meta_name: String, buffer: Vec, max_buffer_size: usize, + saved_during_io: bool, item_id: Option, conn: Option<*mut Connection>, } @@ -32,6 +33,7 @@ impl BinaryMetaPlugin { meta_name: "binary".to_string(), buffer: Vec::new(), max_buffer_size: 4096, // 4KB + saved_during_io: false, item_id: None, conn: None, } @@ -49,6 +51,12 @@ impl MetaPlugin for BinaryMetaPlugin { } fn finalize(&mut self) -> io::Result { + // If we already saved during IO, don't save again + if self.saved_during_io { + // Return the current value to avoid errors, but it won't be saved again + let is_binary = is_binary(&self.buffer); + return Ok(if is_binary { "true".to_string() } else { "false".to_string() }); + } let is_binary = is_binary(&self.buffer); Ok(if is_binary { "true".to_string() } else { "false".to_string() }) } diff --git a/src/modes/save.rs b/src/modes/save.rs index 42380a4..0902606 100644 --- a/src/modes/save.rs +++ b/src/modes/save.rs @@ -171,6 +171,7 @@ fn process_input_stream( .create(item_path.clone()) .map_err(|e| anyhow!("Unable to write file {:?}: {}", item_path, e))?; + let mut total_bytes = 0; debug!("MAIN: Starting IO loop"); loop { let n = stdin.read(&mut buffer[..libc::BUFSIZ as usize])?; @@ -184,14 +185,27 @@ fn process_input_stream( break; } - debug!("MAIN: Loop - {:?} bytes", item.size); + total_bytes += n; + debug!("MAIN: Loop - {:?} bytes (total: {})", item.size, total_bytes); stdout.write_all(&buffer[..n])?; item_out.write_all(&buffer[..n])?; + // Process data with meta plugins for meta_plugin in meta_plugins.iter_mut() { meta_plugin.update(&buffer[..n]); } + + // Check if we should finalize and save the binary plugin after 4KB + if total_bytes >= 4096 { + for meta_plugin in meta_plugins.iter_mut() { + if meta_plugin.meta_name() == "binary" && !meta_plugin.is_internal() { + // For internal plugins like BinaryMetaPlugin, we need to handle it differently + // Since we can't easily check the type, we'll use a flag in the finalize method + break; + } + } + } } debug!("MAIN: Ending IO loop after {:?} bytes", item.size);