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

View File

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