fix: prevent duplicate metadata storage by returning empty strings from finalize

Co-authored-by: aider (openai/andrew/openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-16 14:37:13 -03:00
parent 6b2cb49881
commit 31a653449c
2 changed files with 23 additions and 88 deletions

View File

@@ -52,14 +52,8 @@ impl MetaPlugin for BinaryMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
// If we already saved during IO, don't save again // Since we save during initialize() or update(), return empty to avoid duplicate saves
if self.is_saved { Ok("".to_string())
// 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() })
} }
fn update(&mut self, data: &[u8]) { fn update(&mut self, data: &[u8]) {
@@ -123,13 +117,8 @@ impl MetaPlugin for CwdMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
match env::current_dir() {
Ok(path) => Ok(path.to_string_lossy().to_string()),
Err(_) => Ok("unknown".to_string()),
}
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -176,10 +165,8 @@ impl MetaPlugin for UidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
Ok(get_current_uid().to_string())
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -223,13 +210,8 @@ impl MetaPlugin for UserMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
match get_current_username() {
Some(username) => Ok(username.to_string_lossy().to_string()),
None => Ok("unknown".to_string()),
}
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -276,10 +258,8 @@ impl MetaPlugin for GidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
Ok(get_current_gid().to_string())
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -323,13 +303,8 @@ impl MetaPlugin for GroupMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
match get_current_groupname() {
Some(groupname) => Ok(groupname.to_string_lossy().to_string()),
None => Ok("unknown".to_string()),
}
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -376,13 +351,8 @@ impl MetaPlugin for ShellMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
match env::var("SHELL") {
Ok(shell) => Ok(shell),
Err(_) => Ok("unknown".to_string()),
}
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -429,13 +399,8 @@ impl MetaPlugin for ShellPidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
match env::var("PPID") {
Ok(ppid) => Ok(ppid),
Err(_) => Ok(process::id().to_string()),
}
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -482,10 +447,8 @@ impl MetaPlugin for KeepPidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
Ok(process::id().to_string())
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -529,13 +492,8 @@ impl MetaPlugin for HostnameMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
match gethostname().into_string() {
Ok(hostname) => Ok(hostname),
Err(_) => Ok("unknown".to_string()),
}
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {
@@ -582,31 +540,8 @@ impl MetaPlugin for FullHostnameMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved { // Since we save during initialize(), return empty to avoid duplicate saves
return Ok("".to_string()); // Already saved, return empty to avoid duplicate Ok("".to_string())
}
// Try to get the FQDN through reverse DNS lookup
match local_ip() {
Ok(my_local_ip) => {
match lookup_addr(&my_local_ip) {
Ok(hostname) => Ok(hostname),
Err(_) => {
// Fall back to regular hostname if reverse DNS fails
match gethostname().into_string() {
Ok(hostname) => Ok(hostname),
Err(_) => Ok("unknown".to_string()),
}
}
}
}
Err(_) => {
// Fall back to regular hostname if we can't get local IP
match gethostname().into_string() {
Ok(hostname) => Ok(hostname),
Err(_) => Ok("unknown".to_string()),
}
}
}
} }
fn update(&mut self, _data: &[u8]) { fn update(&mut self, _data: &[u8]) {

View File

@@ -213,7 +213,7 @@ fn process_input_stream(
for meta_plugin in meta_plugins.iter_mut() { for meta_plugin in meta_plugins.iter_mut() {
match meta_plugin.finalize() { match meta_plugin.finalize() {
Ok(value) => { Ok(value) => {
// Only save non-empty values (empty means already saved) // Only save non-empty values (empty means already saved during initialize/update)
if !value.is_empty() { if !value.is_empty() {
if let Err(e) = meta_plugin.save_meta(conn, item_id, value) { if let Err(e) = meta_plugin.save_meta(conn, item_id, value) {
eprintln!("Warning: Failed to save meta value: {}", e); eprintln!("Warning: Failed to save meta value: {}", e);