From 770dee519972f7d8f9c2d1ae4d5f3b1cb481c050 Mon Sep 17 00:00:00 2001 From: cflmy Date: Wed, 18 Nov 2026 09:54:06 +0800 Subject: [PATCH] fix(agent): prevent EffectLedger atomic temp collisions under parallel operations --- plugins/agent/src/effects.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/agent/src/effects.rs b/plugins/agent/src/effects.rs index 8c4ff03..0cc35ed 100644 --- a/plugins/agent/src/effects.rs +++ b/plugins/agent/src/effects.rs @@ -91,6 +91,8 @@ fn ensure_dir(root: &Path) -> Result<(), String> { fs::create_dir_all(root).map_err(|e| format!("effects mkdir: {e}")) } +static EFFECT_TEMP_SEQUENCE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + fn atomic_write_json(path: &Path, doc: &Value) -> Result<(), String> { let parent = path .parent() @@ -102,7 +104,8 @@ fn atomic_write_json(path: &Path, doc: &Value) -> Result<(), String> { .and_then(|s| s.to_str()) .unwrap_or("op.json"), std::process::id(), - now_rfc3339().replace(':', "") + format!("{}-{}", now_rfc3339().replace(':', ""), + EFFECT_TEMP_SEQUENCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) ); let tmp = parent.join(tmp_name); let text = serde_json::to_string_pretty(doc).map_err(|e| e.to_string())?;