diff --git a/crates/opensymphony-acp/src/durable.rs b/crates/opensymphony-acp/src/durable.rs index 08c21e904..4b59ddfe2 100644 --- a/crates/opensymphony-acp/src/durable.rs +++ b/crates/opensymphony-acp/src/durable.rs @@ -65,14 +65,19 @@ pub(super) struct Durability { workspace: WorkspaceHandle, manifest: ConversationManifest, // Never unlink this file: another process may already be waiting on its inode. - _owner_lock: File, + _owner_lock: OwnerLock, } -impl Drop for Durability { +/// A held owner lock that is released explicitly on every drop path. +/// +/// A concurrent fork can temporarily inherit an open descriptor before exec +/// closes it, so relying on close alone can keep the flock alive after a +/// failed claim returns, making the next claim observe a spurious `Owned`. +struct OwnerLock(File); + +impl Drop for OwnerLock { fn drop(&mut self) { - // Release explicitly: a concurrent fork can temporarily inherit an open - // descriptor before exec closes it, delaying close-only flock release. - let _ = self._owner_lock.unlock(); + let _ = self.0.unlock(); } } @@ -169,11 +174,12 @@ impl Durability { .create(true) .truncate(false) .open(&lock_path)?; - match owner_lock.try_lock() { - Ok(()) => {} + let owner_lock = match owner_lock.try_lock() { + // Wrap immediately so binding and metadata rejections below unlock too. + Ok(()) => OwnerLock(owner_lock), Err(std::fs::TryLockError::WouldBlock) => return Err(DurabilityError::Owned), Err(std::fs::TryLockError::Error(error)) => return Err(error.into()), - } + }; match tokio::fs::symlink_metadata(workspace.conversation_manifest_path()).await { Ok(metadata) if metadata.len() > MAX_MANIFEST_BYTES as u64 => { return Err(DurabilityError::InvalidMetadata); diff --git a/crates/opensymphony-cli/tests/run.rs b/crates/opensymphony-cli/tests/run.rs index e2a804486..10f168b28 100644 --- a/crates/opensymphony-cli/tests/run.rs +++ b/crates/opensymphony-cli/tests/run.rs @@ -20,6 +20,25 @@ use tokio::{ time::{Instant, sleep}, }; +/// The mock tracker keeps the issue active, so a successful run is followed +/// within milliseconds by a continuation retry that rewrites run.json as +/// `preparation_failed` with `pending_retry`. Polling only for `succeeded` +/// races that rewrite; a pending continuation retry (scheduled only after a +/// normal worker exit) on a manifest that recorded `after_run` is equally +/// stable evidence that the run completed normally. +fn acp_run_completed_normally(run: &Value) -> bool { + let after_run_succeeded = run["hooks"].as_array().is_some_and(|hooks| { + hooks + .iter() + .any(|hook| hook["kind"] == "after_run" && hook["status"] == "succeeded") + }); + let normal_exit = run["status"] == "succeeded" + || (run["pending_retry"] == true + && run["retry_reason"] == "continuation" + && run["retry_error"].is_null()); + after_run_succeeded && normal_exit +} + #[tokio::test] async fn run_dispatches_acp_with_exact_cwd_hooks_and_no_openhands() { let linear = MockLinearGraphqlServer::start_with_active_issue().await; @@ -46,7 +65,7 @@ async fn run_dispatches_acp_with_exact_cwd_hooks_and_no_openhands() { if std::fs::read_to_string(workspace.join(".opensymphony/run.json")) .ok() .and_then(|raw| serde_json::from_str::(&raw).ok()) - .is_some_and(|run| run["status"] == "succeeded") + .is_some_and(|run| acp_run_completed_normally(&run)) && std::fs::read_to_string(workspace.join("hooks")) .is_ok_and(|hooks| hooks.contains("after_run\n")) {