fix: add is_saved tracking to prevent duplicate metadata saves

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:32:40 -03:00
parent 4f05dbd61f
commit 6b2cb49881
2 changed files with 65 additions and 11 deletions

View File

@@ -15,6 +15,7 @@ use crate::meta_plugin::MetaPlugin;
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct CwdMetaPlugin { pub struct CwdMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@@ -107,6 +108,7 @@ impl CwdMetaPlugin {
pub fn new() -> CwdMetaPlugin { pub fn new() -> CwdMetaPlugin {
CwdMetaPlugin { CwdMetaPlugin {
meta_name: "cwd".to_string(), meta_name: "cwd".to_string(),
is_saved: false,
} }
} }
} }
@@ -121,6 +123,9 @@ impl MetaPlugin for CwdMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
match env::current_dir() { match env::current_dir() {
Ok(path) => Ok(path.to_string_lossy().to_string()), Ok(path) => Ok(path.to_string_lossy().to_string()),
Err(_) => Ok("unknown".to_string()), Err(_) => Ok("unknown".to_string()),
@@ -141,7 +146,7 @@ impl MetaPlugin for CwdMetaPlugin {
Err(_) => "unknown".to_string(), Err(_) => "unknown".to_string(),
}; };
self.save_meta(conn, item_id, cwd)?; self.save_meta(conn, item_id, cwd)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -149,12 +154,14 @@ impl MetaPlugin for CwdMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct UidMetaPlugin { pub struct UidMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl UidMetaPlugin { impl UidMetaPlugin {
pub fn new() -> UidMetaPlugin { pub fn new() -> UidMetaPlugin {
UidMetaPlugin { UidMetaPlugin {
meta_name: "uid".to_string(), meta_name: "uid".to_string(),
is_saved: false,
} }
} }
} }
@@ -169,6 +176,9 @@ impl MetaPlugin for UidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
Ok(get_current_uid().to_string()) Ok(get_current_uid().to_string())
} }
@@ -183,6 +193,7 @@ impl MetaPlugin for UidMetaPlugin {
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> { fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
let uid = get_current_uid().to_string(); let uid = get_current_uid().to_string();
self.save_meta(conn, item_id, uid)?; self.save_meta(conn, item_id, uid)?;
self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -190,12 +201,14 @@ impl MetaPlugin for UidMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct UserMetaPlugin { pub struct UserMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl UserMetaPlugin { impl UserMetaPlugin {
pub fn new() -> UserMetaPlugin { pub fn new() -> UserMetaPlugin {
UserMetaPlugin { UserMetaPlugin {
meta_name: "user".to_string(), meta_name: "user".to_string(),
is_saved: false,
} }
} }
} }
@@ -210,6 +223,9 @@ impl MetaPlugin for UserMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
match get_current_username() { match get_current_username() {
Some(username) => Ok(username.to_string_lossy().to_string()), Some(username) => Ok(username.to_string_lossy().to_string()),
None => Ok("unknown".to_string()), None => Ok("unknown".to_string()),
@@ -230,7 +246,7 @@ impl MetaPlugin for UserMetaPlugin {
None => "unknown".to_string(), None => "unknown".to_string(),
}; };
self.save_meta(conn, item_id, user)?; self.save_meta(conn, item_id, user)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -238,12 +254,14 @@ impl MetaPlugin for UserMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct GidMetaPlugin { pub struct GidMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl GidMetaPlugin { impl GidMetaPlugin {
pub fn new() -> GidMetaPlugin { pub fn new() -> GidMetaPlugin {
GidMetaPlugin { GidMetaPlugin {
meta_name: "gid".to_string(), meta_name: "gid".to_string(),
is_saved: false,
} }
} }
} }
@@ -258,6 +276,9 @@ impl MetaPlugin for GidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
Ok(get_current_gid().to_string()) Ok(get_current_gid().to_string())
} }
@@ -272,7 +293,7 @@ impl MetaPlugin for GidMetaPlugin {
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> { fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
let gid = get_current_gid().to_string(); let gid = get_current_gid().to_string();
self.save_meta(conn, item_id, gid)?; self.save_meta(conn, item_id, gid)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -280,12 +301,14 @@ impl MetaPlugin for GidMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct GroupMetaPlugin { pub struct GroupMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl GroupMetaPlugin { impl GroupMetaPlugin {
pub fn new() -> GroupMetaPlugin { pub fn new() -> GroupMetaPlugin {
GroupMetaPlugin { GroupMetaPlugin {
meta_name: "group".to_string(), meta_name: "group".to_string(),
is_saved: false,
} }
} }
} }
@@ -300,6 +323,9 @@ impl MetaPlugin for GroupMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
match get_current_groupname() { match get_current_groupname() {
Some(groupname) => Ok(groupname.to_string_lossy().to_string()), Some(groupname) => Ok(groupname.to_string_lossy().to_string()),
None => Ok("unknown".to_string()), None => Ok("unknown".to_string()),
@@ -320,7 +346,7 @@ impl MetaPlugin for GroupMetaPlugin {
None => "unknown".to_string(), None => "unknown".to_string(),
}; };
self.save_meta(conn, item_id, group)?; self.save_meta(conn, item_id, group)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -328,12 +354,14 @@ impl MetaPlugin for GroupMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct ShellMetaPlugin { pub struct ShellMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl ShellMetaPlugin { impl ShellMetaPlugin {
pub fn new() -> ShellMetaPlugin { pub fn new() -> ShellMetaPlugin {
ShellMetaPlugin { ShellMetaPlugin {
meta_name: "shell".to_string(), meta_name: "shell".to_string(),
is_saved: false,
} }
} }
} }
@@ -348,6 +376,9 @@ impl MetaPlugin for ShellMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
match env::var("SHELL") { match env::var("SHELL") {
Ok(shell) => Ok(shell), Ok(shell) => Ok(shell),
Err(_) => Ok("unknown".to_string()), Err(_) => Ok("unknown".to_string()),
@@ -368,7 +399,7 @@ impl MetaPlugin for ShellMetaPlugin {
Err(_) => "unknown".to_string(), Err(_) => "unknown".to_string(),
}; };
self.save_meta(conn, item_id, shell)?; self.save_meta(conn, item_id, shell)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -376,12 +407,14 @@ impl MetaPlugin for ShellMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct ShellPidMetaPlugin { pub struct ShellPidMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl ShellPidMetaPlugin { impl ShellPidMetaPlugin {
pub fn new() -> ShellPidMetaPlugin { pub fn new() -> ShellPidMetaPlugin {
ShellPidMetaPlugin { ShellPidMetaPlugin {
meta_name: "shell_pid".to_string(), meta_name: "shell_pid".to_string(),
is_saved: false,
} }
} }
} }
@@ -396,6 +429,9 @@ impl MetaPlugin for ShellPidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
match env::var("PPID") { match env::var("PPID") {
Ok(ppid) => Ok(ppid), Ok(ppid) => Ok(ppid),
Err(_) => Ok(process::id().to_string()), Err(_) => Ok(process::id().to_string()),
@@ -416,7 +452,7 @@ impl MetaPlugin for ShellPidMetaPlugin {
Err(_) => process::id().to_string(), Err(_) => process::id().to_string(),
}; };
self.save_meta(conn, item_id, pid)?; self.save_meta(conn, item_id, pid)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -424,12 +460,14 @@ impl MetaPlugin for ShellPidMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct KeepPidMetaPlugin { pub struct KeepPidMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl KeepPidMetaPlugin { impl KeepPidMetaPlugin {
pub fn new() -> KeepPidMetaPlugin { pub fn new() -> KeepPidMetaPlugin {
KeepPidMetaPlugin { KeepPidMetaPlugin {
meta_name: "keep_pid".to_string(), meta_name: "keep_pid".to_string(),
is_saved: false,
} }
} }
} }
@@ -444,6 +482,9 @@ impl MetaPlugin for KeepPidMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
Ok(process::id().to_string()) Ok(process::id().to_string())
} }
@@ -458,7 +499,7 @@ impl MetaPlugin for KeepPidMetaPlugin {
fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> { fn initialize(&mut self, conn: &Connection, item_id: i64) -> Result<()> {
let pid = process::id().to_string(); let pid = process::id().to_string();
self.save_meta(conn, item_id, pid)?; self.save_meta(conn, item_id, pid)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -466,12 +507,14 @@ impl MetaPlugin for KeepPidMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct HostnameMetaPlugin { pub struct HostnameMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl HostnameMetaPlugin { impl HostnameMetaPlugin {
pub fn new() -> HostnameMetaPlugin { pub fn new() -> HostnameMetaPlugin {
HostnameMetaPlugin { HostnameMetaPlugin {
meta_name: "hostname".to_string(), meta_name: "hostname".to_string(),
is_saved: false,
} }
} }
} }
@@ -486,6 +529,9 @@ impl MetaPlugin for HostnameMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
match gethostname().into_string() { match gethostname().into_string() {
Ok(hostname) => Ok(hostname), Ok(hostname) => Ok(hostname),
Err(_) => Ok("unknown".to_string()), Err(_) => Ok("unknown".to_string()),
@@ -506,7 +552,7 @@ impl MetaPlugin for HostnameMetaPlugin {
Err(_) => "unknown".to_string(), Err(_) => "unknown".to_string(),
}; };
self.save_meta(conn, item_id, hostname)?; self.save_meta(conn, item_id, hostname)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }
@@ -514,12 +560,14 @@ impl MetaPlugin for HostnameMetaPlugin {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct FullHostnameMetaPlugin { pub struct FullHostnameMetaPlugin {
meta_name: String, meta_name: String,
is_saved: bool,
} }
impl FullHostnameMetaPlugin { impl FullHostnameMetaPlugin {
pub fn new() -> FullHostnameMetaPlugin { pub fn new() -> FullHostnameMetaPlugin {
FullHostnameMetaPlugin { FullHostnameMetaPlugin {
meta_name: "full_hostname".to_string(), meta_name: "full_hostname".to_string(),
is_saved: false,
} }
} }
} }
@@ -534,6 +582,9 @@ impl MetaPlugin for FullHostnameMetaPlugin {
} }
fn finalize(&mut self) -> io::Result<String> { fn finalize(&mut self) -> io::Result<String> {
if self.is_saved {
return Ok("".to_string()); // Already saved, return empty to avoid duplicate
}
// Try to get the FQDN through reverse DNS lookup // Try to get the FQDN through reverse DNS lookup
match local_ip() { match local_ip() {
Ok(my_local_ip) => { Ok(my_local_ip) => {
@@ -590,7 +641,7 @@ impl MetaPlugin for FullHostnameMetaPlugin {
} }
}; };
self.save_meta(conn, item_id, hostname)?; self.save_meta(conn, item_id, hostname)?;
// Mark as saved to prevent duplicate saves self.is_saved = true;
Ok(()) Ok(())
} }
} }

View File

@@ -213,8 +213,11 @@ 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) => {
if let Err(e) = meta_plugin.save_meta(conn, item_id, value) { // Only save non-empty values (empty means already saved)
eprintln!("Warning: Failed to save meta value: {}", e); if !value.is_empty() {
if let Err(e) = meta_plugin.save_meta(conn, item_id, value) {
eprintln!("Warning: Failed to save meta value: {}", e);
}
} }
} }
Err(e) => { Err(e) => {