Skip to content

Computer use CLI for AI agents

fix: preserve wait_timeout envelope when inherited deadline expires

detail-app[bot]07913a7cdb85
diff --git a/crates/core/src/commands/wait_selector.rs b/crates/core/src/commands/wait_selector.rs
index 68e715679..21c97d239 100644
--- a/crates/core/src/commands/wait_selector.rs
+++ b/crates/core/src/commands/wait_selector.rs
@@ -131,7 +131,7 @@ fn timeout_response(
) -> Result<Value, AppError> {
if last_built.is_none() {
let diagnostic_deadline =
- crate::Deadline::after(DIAGNOSTIC_SNAPSHOT_BUDGET.as_millis() as u64)?;
+ crate::Deadline::detached_after(DIAGNOSTIC_SNAPSHOT_BUDGET.as_millis() as u64)?;
match snapshot::build(
adapter,
&input.opts,
@@ -162,7 +162,7 @@ fn persist_last_built(
return Ok(None);
};
let store = RefStore::for_session(context.session_id())?;
- let snapshot_id = store.save_new_snapshot(&result.refmap)?;
+ let snapshot_id = store.save_new_snapshot_detached(&result.refmap)?;
trace_artifacts::copy_refmap_if_full(context, &store, &snapshot_id, &result.refmap)?;
Ok(Some(snapshot_id))
}
diff --git a/crates/core/src/commands/wait_selector_inherited_deadline_tests.rs b/crates/core/src/commands/wait_selector_inherited_deadline_tests.rs
new file mode 100644
index 000000000..a59596e2b
--- /dev/null
+++ b/crates/core/src/commands/wait_selector_inherited_deadline_tests.rs
@@ -0,0 +1,87 @@
+use super::*;
+
+/// Regression guard for the post-action-wait `lock_timeout` envelope-replacement
+/// bug. Under a batch, the wait's deadline is intersected with the inherited
+/// batch deadline; once that inherited deadline has expired, the recovery write
+/// in `persist_last_built` used to take the inheritance-aware ref-store lock
+/// (`Deadline::after(LOCK_TIMEOUT_MS)`), whose budget collapsed to 0, so the
+/// uncontested lock returned `lock_timeout` and the `?` in `timeout_response`
+/// replaced the intended `wait_timeout` envelope. The fix routes the recovery
+/// write through [`RefStore::save_new_snapshot_detached`] (a detached lock
+/// deadline) so the `wait_timeout` envelope — with its `snapshot_id` — is
+/// always built.
+#[test]
+fn flicker_path_emits_wait_timeout_under_an_expired_inherited_deadline() {
+ let _guard = HomeGuard::new();
+ let adapter = FlippingTreeAdapter {
+ calls: AtomicUsize::new(0),
+ before: window_node(vec![button_node("flicker")]),
+ after: window_node(vec![]),
+ };
+ let inherited = crate::Deadline::after(300).unwrap();
+ let _scope = crate::deadline::enter_scope(Some(inherited));
+
+ let err = execute(
+ WaitSelectorInput {
+ timeout_ms: 30_000,
+ ..base_input("button:flicker", false)
+ },
+ &adapter,
+ &CommandContext::default(),
+ )
+ .unwrap_err();
+ drop(_scope);
+
+ assert_eq!(err.code(), "TIMEOUT");
+ let details = match err {
+ AppError::Adapter(adapter_err) => adapter_err.details.expect("timeout details"),
+ other => panic!("expected adapter timeout, got {other:?}"),
+ };
+ assert_eq!(details["kind"], "wait_timeout");
+ assert_eq!(details["predicate"], "selector");
+ let snapshot_id = details["snapshot_id"]
+ .as_str()
+ .expect("diagnostic snapshot must be persisted via the detached lock");
+ assert!(RefStore::new().unwrap().load(Some(snapshot_id)).is_ok());
+ assert!(
+ adapter.calls.load(Ordering::SeqCst) >= 2,
+ "flicker requires two observe_tree calls (observe + build)"
+ );
+}
+
+/// Symmetric condition — the bug does NOT fire when `last_built` is `None`,
+/// because `persist_last_built(None)` short-circuits before touching the lock.
+/// The correct `wait_timeout` envelope (no `snapshot_id`, `last_error`
+/// carried) must still be produced under an expired inherited deadline,
+/// guarding against any future change that routes the `None` path through the
+/// lock.
+#[test]
+fn expired_inherited_deadline_without_a_snapshot_still_emits_wait_timeout() {
+ let _guard = HomeGuard::new();
+ let inherited = crate::Deadline::after(300).unwrap();
+ let _scope = crate::deadline::enter_scope(Some(inherited));
+
+ let err = execute(
+ WaitSelectorInput {
+ timeout_ms: 30_000,
+ ..base_input("button:absent", false)
+ },
+ &ErrorThenTreeAdapter,
+ &CommandContext::default(),
+ )
+ .unwrap_err();
+ drop(_scope);
+
+ assert_eq!(err.code(), "TIMEOUT");
+ let details = match err {
+ AppError::Adapter(adapter_err) => adapter_err.details.expect("timeout details"),
+ other => panic!("expected adapter timeout, got {other:?}"),
+ };
+ assert_eq!(details["kind"], "wait_timeout");
+ assert_eq!(details["predicate"], "selector");
+ assert!(
+ details.get("snapshot_id").is_none(),
+ "no diagnostic snapshot when the adapter never resolves a window, got {details}"
+ );
+ assert_eq!(details["last_error"]["code"], "APP_NOT_FOUND");
+}
diff --git a/crates/core/src/commands/wait_selector_tests.rs b/crates/core/src/commands/wait_selector_tests.rs
index 57fc08cb3..90b4333eb 100644
--- a/crates/core/src/commands/wait_selector_tests.rs
+++ b/crates/core/src/commands/wait_selector_tests.rs
@@ -316,6 +316,9 @@ fn timeout_persists_the_last_diagnostic_snapshot() {
);
}
+#[path = "wait_selector_inherited_deadline_tests.rs"]
+mod inherited_deadline_tests;
+
#[test]
fn retryable_app_not_found_swallowed_until_timeout() {
let _guard = HomeGuard::new();
diff --git a/crates/core/src/refs_lock.rs b/crates/core/src/refs_lock.rs
index d68bc56d4..2bb994125 100644
--- a/crates/core/src/refs_lock.rs
+++ b/crates/core/src/refs_lock.rs
@@ -14,6 +14,16 @@ impl RefStoreLock {
Self::acquire_with_deadline(path, deadline)
}
+ /// Recovery-path lock: the budget is detached from the (possibly already
+ /// expired) inherited command deadline. Use ONLY for post-deadline
+ /// diagnostic work — e.g. persisting the last built snapshot after a wait
+ /// timeout — which must run after the operation's own deadline has expired
+ /// and must not collapse to an immediate `lock_timeout`.
+ pub(crate) fn acquire_detached(path: &Path) -> Result<Self, AppError> {
+ let deadline = Deadline::detached_after(LOCK_TIMEOUT_MS)?;
+ Self::acquire_with_deadline(path, deadline)
+ }
+
pub(crate) fn acquire_with_deadline(path: &Path, deadline: Deadline) -> Result<Self, AppError> {
Ok(Self {
_lock: FileLock::acquire(path, deadline, "ref store lock")?,
@@ -65,4 +75,32 @@ mod tests {
assert_eq!(error.code(), "TIMEOUT");
assert!(deadline.elapsed() < Duration::from_secs(1));
}
+
+ #[test]
+ fn acquire_collapses_to_lock_timeout_under_an_expired_inherited_deadline() {
+ let path = lock_path("expired-inherited");
+ let inherited = Deadline::after(0).unwrap();
+ let _scope = crate::deadline::enter_scope(Some(inherited));
+ let error = match RefStoreLock::acquire(&path) {
+ Ok(_) => panic!("expired inherited deadline must not acquire the lock"),
+ Err(error) => error,
+ };
+ assert_eq!(error.code(), "TIMEOUT");
+ let AppError::Adapter(adapter_err) = error else {
+ panic!("expected adapter error");
+ };
+ let details = adapter_err.details.expect("lock timeout details");
+ assert_eq!(details["kind"], "lock_timeout");
+ assert_eq!(details["purpose"], "ref store lock");
+ assert!(!lock_holder_is_live(&path));
+ }
+
+ #[test]
+ fn acquire_detached_survives_an_expired_inherited_deadline() {
+ let path = lock_path("detached");
+ let inherited = Deadline::after(0).unwrap();
+ let _scope = crate::deadline::enter_scope(Some(inherited));
+ let _lock = RefStoreLock::acquire_detached(&path).unwrap();
+ assert!(lock_holder_is_live(&path));
+ }
}
diff --git a/crates/core/src/refs_store.rs b/crates/core/src/refs_store.rs
index ab7bb06eb..0054ffe8a 100644
--- a/crates/core/src/refs_store.rs
+++ b/crates/core/src/refs_store.rs
@@ -49,13 +49,25 @@ impl RefStore {
}
pub fn save_new_snapshot(&self, refmap: &RefMap) -> Result<String, AppError> {
- self.with_write_lock(|| {
- let snapshot_id = new_snapshot_id();
- self.save_snapshot_unlocked(&snapshot_id, refmap)?;
- self.set_latest_unlocked(&snapshot_id)?;
- self.prune_old_snapshots_unlocked(&snapshot_id)?;
- Ok(snapshot_id)
- })
+ self.with_write_lock(|| self.save_new_snapshot_unlocked_with_id(refmap))
+ }
+
+ /// Recovery-path variant of [`save_new_snapshot`]: the ref-store write
+ /// lock is acquired with a deadline detached from any inherited (and
+ /// possibly already-expired) command deadline. Used by post-timeout
+ /// diagnostic persistence, which runs after the operation's own deadline
+ /// has expired and must not collapse to an immediate `lock_timeout` that
+ /// would replace the intended `wait_timeout` envelope.
+ pub(crate) fn save_new_snapshot_detached(&self, refmap: &RefMap) -> Result<String, AppError> {
+ self.with_write_lock_detached(|| self.save_new_snapshot_unlocked_with_id(refmap))
+ }
+
+ fn save_new_snapshot_unlocked_with_id(&self, refmap: &RefMap) -> Result<String, AppError> {
+ let snapshot_id = new_snapshot_id();
+ self.save_snapshot_unlocked(&snapshot_id, refmap)?;
+ self.set_latest_unlocked(&snapshot_id)?;
+ self.prune_old_snapshots_unlocked(&snapshot_id)?;
+ Ok(snapshot_id)
}
pub fn save_snapshot(&self, snapshot_id: &str, refmap: &RefMap) -> Result<(), AppError> {
@@ -205,6 +217,17 @@ impl RefStore {
f()
}
+ /// Recovery-path write lock: uses [`RefStoreLock::acquire_detached`] so the
+ /// lock budget survives an inherited command deadline that has already
+ /// expired by the time post-deadline diagnostic work runs.
+ fn with_write_lock_detached<T>(
+ &self,
+ f: impl FnOnce() -> Result<T, AppError>,
+ ) -> Result<T, AppError> {
+ let _lock = RefStoreLock::acquire_detached(&self.lock_path())?;
+ f()
+ }
+
fn migrate_legacy_latest(&self) -> Result<Option<RefMap>, AppError> {
if !self.allow_legacy_migration {
return Ok(None);