Skip to content

Computer use CLI for AI agents

fix: restore cursor overlay lifecycle for keyboard and clipboard actions

detail-app[bot]c827cf965606
diff --git a/src/dispatch/cursor_overlay_lifecycle_tests.rs b/src/dispatch/cursor_overlay_lifecycle_tests.rs
new file mode 100644
index 000000000..60356922e
--- /dev/null
+++ b/src/dispatch/cursor_overlay_lifecycle_tests.rs
@@ -0,0 +1,325 @@
+use super::dispatch;
+use crate::cli::Commands;
+use crate::cli_args::actions::PressArgs;
+use crate::cli_args::system::ClipboardSetArgs;
+use crate::dispatch::test_support::HomeGuard;
+
+use agent_desktop_core::session::{ArtifactsMode, SessionTraceMode, StartSessionOptions};
+use agent_desktop_core::{
+ ActionOps, ActionRequest, ActionResult, AdapterError, ClipboardContent, CursorOverlayControl,
+ Deadline, InputOps, InteractionLease, KeyCombo, NativeHandle, ObservationOps, PermissionReport,
+ SystemOps, context::CommandContext,
+};
+use std::sync::Mutex;
+
+struct RecordingOverlayAdapter {
+ controls: Mutex<Vec<CursorOverlayControl>>,
+}
+
+impl RecordingOverlayAdapter {
+ fn new() -> Self {
+ Self {
+ controls: Mutex::new(Vec::new()),
+ }
+ }
+}
+
+impl ObservationOps for RecordingOverlayAdapter {}
+
+impl ActionOps for RecordingOverlayAdapter {
+ fn execute_action(
+ &self,
+ _handle: &NativeHandle,
+ _request: ActionRequest,
+ _lease: &InteractionLease,
+ ) -> Result<ActionResult, AdapterError> {
+ Ok(ActionResult::delivered_unverified("PressKey"))
+ }
+}
+
+impl InputOps for RecordingOverlayAdapter {
+ fn set_clipboard_content(
+ &self,
+ _content: &ClipboardContent,
+ _lease: &InteractionLease,
+ ) -> Result<(), AdapterError> {
+ Ok(())
+ }
+
+ fn clear_clipboard(&self, _lease: &InteractionLease) -> Result<(), AdapterError> {
+ Ok(())
+ }
+}
+
+impl SystemOps for RecordingOverlayAdapter {
+ fn acquire_interaction_lease(
+ &self,
+ deadline: Deadline,
+ ) -> Result<InteractionLease, AdapterError> {
+ InteractionLease::guarded(deadline, ())
+ }
+
+ fn update_cursor_overlay(&self, control: &CursorOverlayControl) -> Result<(), AdapterError> {
+ self.controls.lock().unwrap().push(control.clone());
+ Ok(())
+ }
+}
+
+struct BlockingComboAdapter {
+ controls: Mutex<Vec<CursorOverlayControl>>,
+}
+
+impl BlockingComboAdapter {
+ fn new() -> Self {
+ Self {
+ controls: Mutex::new(Vec::new()),
+ }
+ }
+}
+
+impl ObservationOps for BlockingComboAdapter {}
+impl ActionOps for BlockingComboAdapter {}
+impl InputOps for BlockingComboAdapter {}
+
+impl SystemOps for BlockingComboAdapter {
+ fn acquire_interaction_lease(
+ &self,
+ deadline: Deadline,
+ ) -> Result<InteractionLease, AdapterError> {
+ InteractionLease::guarded(deadline, ())
+ }
+
+ fn is_blocked_combo(&self, _combo: &KeyCombo) -> bool {
+ true
+ }
+
+ fn update_cursor_overlay(&self, control: &CursorOverlayControl) -> Result<(), AdapterError> {
+ self.controls.lock().unwrap().push(control.clone());
+ Ok(())
+ }
+}
+
+struct FailingOverlayAdapter {
+ controls: Mutex<Vec<CursorOverlayControl>>,
+}
+
+impl FailingOverlayAdapter {
+ fn new() -> Self {
+ Self {
+ controls: Mutex::new(Vec::new()),
+ }
+ }
+}
+
+impl ObservationOps for FailingOverlayAdapter {}
+
+impl ActionOps for FailingOverlayAdapter {
+ fn execute_action(
+ &self,
+ _handle: &NativeHandle,
+ _request: ActionRequest,
+ _lease: &InteractionLease,
+ ) -> Result<ActionResult, AdapterError> {
+ Ok(ActionResult::delivered_unverified("PressKey"))
+ }
+}
+
+impl InputOps for FailingOverlayAdapter {
+ fn clear_clipboard(&self, _lease: &InteractionLease) -> Result<(), AdapterError> {
+ Ok(())
+ }
+}
+
+impl SystemOps for FailingOverlayAdapter {
+ fn acquire_interaction_lease(
+ &self,
+ deadline: Deadline,
+ ) -> Result<InteractionLease, AdapterError> {
+ InteractionLease::guarded(deadline, ())
+ }
+
+ fn update_cursor_overlay(&self, control: &CursorOverlayControl) -> Result<(), AdapterError> {
+ self.controls.lock().unwrap().push(control.clone());
+ Err(AdapterError::internal("renderer unavailable"))
+ }
+}
+
+fn started_session() -> agent_desktop_core::session::SessionManifest {
+ agent_desktop_core::session::start_session(StartSessionOptions {
+ trace: SessionTraceMode::Off,
+ artifacts: ArtifactsMode::Events,
+ name: None,
+ })
+ .unwrap()
+}
+
+fn started_overlay_session() -> agent_desktop_core::session::SessionManifest {
+ let manifest = started_session();
+ agent_desktop_core::session::set_cursor_overlay(
+ &manifest.id,
+ agent_desktop_core::CursorOverlayConfig::enabled(None, 6).unwrap(),
+ )
+ .unwrap();
+ manifest
+}
+
+fn headed_context(manifest: &agent_desktop_core::session::SessionManifest) -> CommandContext {
+ CommandContext::new(Some(manifest.id.clone()), None, false)
+ .unwrap()
+ .with_headed(true)
+}
+
+#[test]
+fn mutating_keyboard_and_clipboard_commands_re_show_cursor_overlay() {
+ let _home = HomeGuard::new();
+ let manifest = started_overlay_session();
+ let context = headed_context(&manifest);
+ let adapter = RecordingOverlayAdapter::new();
+
+ dispatch(
+ Commands::Press(PressArgs {
+ combo: "a".into(),
+ app: None,
+ force: false,
+ }),
+ &adapter,
+ &PermissionReport::default(),
+ &context,
+ )
+ .unwrap();
+ dispatch(
+ Commands::ClipboardSet(ClipboardSetArgs {
+ text: Some("agent".into()),
+ image: None,
+ file_url: vec![],
+ }),
+ &adapter,
+ &PermissionReport::default(),
+ &context,
+ )
+ .unwrap();
+ dispatch(
+ Commands::ClipboardClear,
+ &adapter,
+ &PermissionReport::default(),
+ &context,
+ )
+ .unwrap();
+
+ let recorded = adapter.controls.lock().unwrap();
+ assert_eq!(
+ recorded.len(),
+ 6,
+ "each mutating command must bracket with a Hide+Show pair"
+ );
+ for (index, control) in recorded.iter().enumerate() {
+ if index % 2 == 0 {
+ assert!(
+ matches!(control, CursorOverlayControl::Hide { .. }),
+ "control #{index} should be Hide, got {control:?}"
+ );
+ } else {
+ assert!(
+ matches!(control, CursorOverlayControl::Show { .. }),
+ "control #{index} should be Show, got {control:?}"
+ );
+ }
+ assert_eq!(
+ control.session_id(),
+ manifest.id.as_str(),
+ "control #{index} must target the active session"
+ );
+ }
+}
+
+#[test]
+fn non_mutating_commands_skip_cursor_overlay_lifecycle() {
+ let _home = HomeGuard::new();
+ let manifest = started_overlay_session();
+ let context = headed_context(&manifest);
+ let adapter = RecordingOverlayAdapter::new();
+
+ dispatch(
+ Commands::Version,
+ &adapter,
+ &PermissionReport::default(),
+ &context,
+ )
+ .unwrap();
+
+ assert!(
+ adapter.controls.lock().unwrap().is_empty(),
+ "non-mutating commands must not emit overlay controls"
+ );
+}
+
+#[test]
+fn cursor_overlay_show_fires_even_when_the_mutating_command_errors() {
+ let _home = HomeGuard::new();
+ let manifest = started_overlay_session();
+ let context = headed_context(&manifest);
+ let adapter = BlockingComboAdapter::new();
+
+ let result = dispatch(
+ Commands::Press(PressArgs {
+ combo: "cmd+q".into(),
+ app: None,
+ force: false,
+ }),
+ &adapter,
+ &PermissionReport::default(),
+ &context,
+ );
+
+ assert!(
+ result.is_err(),
+ "the blocked combo must still be rejected by the press command"
+ );
+
+ let recorded = adapter.controls.lock().unwrap();
+ assert_eq!(
+ recorded.len(),
+ 2,
+ "the Hide+Show bracket must complete even when the command errors"
+ );
+ assert!(
+ matches!(recorded[0], CursorOverlayControl::Hide { .. }),
+ "Hide must be emitted before the command runs"
+ );
+ assert!(
+ matches!(recorded[1], CursorOverlayControl::Show { .. }),
+ "Show must re-show the overlay even after a failed command"
+ );
+ for control in recorded.iter() {
+ assert_eq!(control.session_id(), manifest.id.as_str());
+ }
+}
+
+#[test]
+fn failing_overlay_control_does_not_abort_the_mutating_command() {
+ let _home = HomeGuard::new();
+ let manifest = started_overlay_session();
+ let context = headed_context(&manifest);
+ let adapter = FailingOverlayAdapter::new();
+
+ let result = dispatch(
+ Commands::ClipboardClear,
+ &adapter,
+ &PermissionReport::default(),
+ &context,
+ );
+
+ assert!(
+ result.is_ok(),
+ "the mutating command must succeed even when the overlay renderer errors"
+ );
+
+ let recorded = adapter.controls.lock().unwrap();
+ assert_eq!(
+ recorded.len(),
+ 2,
+ "the Hide+Show bracket must still be attempted when the renderer errors"
+ );
+ assert!(matches!(recorded[0], CursorOverlayControl::Hide { .. }));
+ assert!(matches!(recorded[1], CursorOverlayControl::Show { .. }));
+}
diff --git a/src/dispatch/mod.rs b/src/dispatch/mod.rs
index 39110623a..664747c6e 100644
--- a/src/dispatch/mod.rs
+++ b/src/dispatch/mod.rs
@@ -13,8 +13,12 @@ mod trace;
#[cfg(test)]
mod test_support;
+#[cfg(test)]
+mod cursor_overlay_lifecycle_tests;
+
use agent_desktop_core::{
- AppError, PermissionReport, PlatformAdapter, commands, context::CommandContext,
+ AppError, CursorOverlayControl, PermissionReport, PlatformAdapter, commands,
+ context::CommandContext,
};
use serde_json::Value;
@@ -27,11 +31,20 @@ pub(crate) fn dispatch(
context: &CommandContext,
) -> Result<Value, AppError> {
tracing::debug!("dispatch: {}", cmd.name());
+ let overlay_session =
+ if cmd.is_mutating() && context.is_headed() && context.cursor_overlay().is_enabled() {
+ context.session_id().map(str::to_owned)
+ } else {
+ None
+ };
let scope = if cmd.is_mutating() {
context.mutating_command_scope(cmd.name())?
} else {
context.command_scope(cmd.name())?
};
+ if let Some(session_id) = overlay_session.as_ref() {
+ let _ = adapter.update_cursor_overlay(&CursorOverlayControl::hide(session_id.clone()));
+ }
let result = match cmd {
Commands::Snapshot(args) => observation::snapshot(args, adapter, context),
Commands::Find(args) => observation::find(args, adapter, context),
@@ -97,6 +110,9 @@ pub(crate) fn dispatch(
Commands::CursorOverlay(args) => cursor_overlay::dispatch(args, adapter, context),
Commands::Trace(args) => trace::dispatch(args, context),
};
+ if let Some(session_id) = overlay_session {
+ let _ = adapter.update_cursor_overlay(&CursorOverlayControl::show(session_id));
+ }
scope.complete(&result)?;
result
}