Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(cua-driver): retain inert Hyprland pointer on desktop faults (#3702)
Francesco Bonacci648c251400eb
diff --git a/libs/cua-driver/hyprland-plugin/CMakeLists.txt b/libs/cua-driver/hyprland-plugin/CMakeLists.txtindex e507463bbc..8a80de28a2 100644--- a/libs/cua-driver/hyprland-plugin/CMakeLists.txt+++ b/libs/cua-driver/hyprland-plugin/CMakeLists.txt@@ -40,6 +40,13 @@ cua_hyprland_harden(cua_hyprland_protocol)set_target_properties(cua_hyprland_protocol PROPERTIES POSITION_INDEPENDENT_CODE ON)if(BUILD_TESTING)+ find_package(Python3 REQUIRED COMPONENTS Interpreter)+ add_test(NAME cua_hyprland_desktop_fault_policy_test+ COMMAND ${Python3_EXECUTABLE} -B+ ${CMAKE_CURRENT_SOURCE_DIR}/tests/desktop_fault_policy_test.py)+ set_tests_properties(cua_hyprland_desktop_fault_policy_test PROPERTIES+ ENVIRONMENT "CXX=${CMAKE_CXX_COMPILER}")+add_executable(cua_hyprland_foreground_route_test tests/foreground_route_test.cpp)target_compile_features(cua_hyprland_foreground_route_test PRIVATE cxx_std_26)target_include_directories(cua_hyprland_foreground_route_test PRIVATE src)diff --git a/libs/cua-driver/hyprland-plugin/packaging/release/bundle.py b/libs/cua-driver/hyprland-plugin/packaging/release/bundle.pyindex ef23657160..fd29e7e243 100644--- a/libs/cua-driver/hyprland-plugin/packaging/release/bundle.py+++ b/libs/cua-driver/hyprland-plugin/packaging/release/bundle.py@@ -42,6 +42,8 @@ src/session.cppsrc/status.cpptests/cmake-api/CMakeLists.txttests/cmake-api/include/src/plugins/PluginAPI.hpp+tests/desktop_fault_policy_fixture.cpp+tests/desktop_fault_policy_test.pytests/drag_geometry_test.cpptests/foreground_route_test.cpptests/input_grant_test.cppdiff --git a/libs/cua-driver/hyprland-plugin/packaging/release/test_release.py b/libs/cua-driver/hyprland-plugin/packaging/release/test_release.pyindex 02a0fea3d9..7042569d3e 100644--- a/libs/cua-driver/hyprland-plugin/packaging/release/test_release.py+++ b/libs/cua-driver/hyprland-plugin/packaging/release/test_release.py@@ -106,6 +106,8 @@ class ReleaseTest(unittest.TestCase):for command in (configure,["cmake", "--build", str(build), "--target", "cua_hyprland_foreground_route_test"],+ ["ctest", "--test-dir", str(build), "--output-on-failure", "--no-tests=error",+ "-R", "^cua_hyprland_desktop_fault_policy_test$"],):result = subprocess.run(command, capture_output=True, text=True, timeout=120)self.assertEqual(result.returncode, 0, result.stdout + result.stderr)diff --git a/libs/cua-driver/hyprland-plugin/src/input_experiment.cpp b/libs/cua-driver/hyprland-plugin/src/input_experiment.cppindex 3f08ee198f..fc7e740bb3 100644--- a/libs/cua-driver/hyprland-plugin/src/input_experiment.cpp+++ b/libs/cua-driver/hyprland-plugin/src/input_experiment.cpp@@ -353,7 +353,7 @@ struct InputExperiment::Impl {}void suspend(std::string_view reason = "plugin_disabled") {suspended = true;- revoke(reason);+ revoke(reason, reason != "plugin_shutdown");reservation = nullptr;if (listen_source) wl_event_source_remove(listen_source);listen_source = nullptr;@@ -610,7 +610,10 @@ struct InputExperiment::Impl {// unconditionally; an off/on pair between timer ticks must not revive// authority. Kill pending connections as well as the active lease so// a pre-transition signed grant cannot be approved after unlock.- revoke("desktop_changed");+ // Admission is gone immediately, but an unchanged live target may keep+ // inert hover just as after normal completion. A leave directly after+ // the release can interfere with the client's drag-release processing.+ revoke("desktop_changed", true);for (auto& c : clients) {// Observers and operator-control connections own no action target.// Keep them alive so Stop/status/evidence survives a transition.@@ -1026,8 +1029,8 @@ struct InputExperiment::Impl {const auto requested_cap = kProduction ? number(f[3]) : 0;if (kProduction && (!InputGrant::single_operation(requested_cap) ||(requested_cap == 16 && route != InputRoute::primary_foreground))) { invalidate(c); send(c, refusal("unsupported")); return; }- if (kProduction && !available()) { invalidate(c); send(c, refusal("session_unavailable")); return; }- if (!layout_qualified()) { invalidate(c); send(c, refusal("unsupported_layout")); return; }+ if (kProduction && !available()) { invalidate(c, false); send(c, refusal("session_unavailable")); return; }+ if (!layout_qualified()) { invalidate(c, false); send(c, refusal("unsupported_layout")); return; }const auto pid = number(f[1]); const auto address = number(f[2], 16);PHLWINDOW window;for (const auto& w : Desktop::windowState()->windows())@@ -1086,8 +1089,8 @@ struct InputExperiment::Impl {c.sequence = sequence;if (c.token.empty() || f[2] != c.token || !refresh(c)) { send(c, refusal("stale_target")); return; }if (number(f[3]) != c.revision) { if (kProduction) revoke("stale_geometry"); send(c, refusal("stale_geometry")); return; }- if (!available()) { revoke("session_unavailable"); send(c, refusal("session_unavailable")); return; }- if (!layout_qualified()) { revoke("unsupported_layout"); send(c, refusal("unsupported_layout")); return; }+ if (!available()) { revoke("session_unavailable", true); send(c, refusal("session_unavailable")); return; }+ if (!layout_qualified()) { revoke("unsupported_layout", true); send(c, refusal("unsupported_layout")); return; }if (lease && Clock::now() >= expires) revoke("lease_expired");if (drag) { send(c, refusal("lease_busy")); return; }if (lease != &c || !(capabilities & cap) || (kProduction && !grant.permits(cap, Clock::now()))) {@@ -1220,13 +1223,12 @@ struct InputExperiment::Impl {try { self.step(); } catch (...) { self.revoke("internal_error"); }wl_event_source_timer_update(self.timer, self.retired ? 500 : 16); return 0;}- void step() {- if (!retired) sync_keymap();- for (auto& c : clients) if (c->deadline.expired(c->hello, Clock::now())) c->dead = true;+ void guard_targets() {if (lease) {if (lease->dead) revoke("disconnected", true);else if (Clock::now() >= expires) revoke("lease_expired");- else if (!available() || !layout_qualified() || !refresh(*lease) || primary_conflict(*lease) || agent_conflict(*lease) ||+ else if (!available() || !layout_qualified()) revoke("cancelled", true);+ else if (!refresh(*lease) || primary_conflict(*lease) || agent_conflict(*lease) ||(drag && !drag->geometry.matches(lease->revision))) revoke("cancelled");}if (pointer_target.entered()) {@@ -1235,9 +1237,16 @@ struct InputExperiment::Impl {const bool bound = std::ranges::any_of(pointers, [&](const auto& p) {return !p->dead && p->wl->resource() && surface && p->focus == surface;});- if (!available() || !layout_qualified() || !geometry || *geometry != pointer_target.geometry() ||+ // Availability controls admission, not passive pointer presence.+ // Retention never excuses a changed target, binding, or conflict.+ if (!geometry || *geometry != pointer_target.geometry() ||!bound || primary_conflict(surface) || agent_conflict(surface)) revoke("cancelled");}+ }+ void step() {+ if (!retired) sync_keymap();+ for (auto& c : clients) if (c->deadline.expired(c->hello, Clock::now())) c->dead = true;+ guard_targets();if (drag) {const auto d = *drag;const auto elapsed = std::chrono::duration<double, std::milli>(Clock::now() - d.start).count();diff --git a/libs/cua-driver/hyprland-plugin/tests/desktop_fault_policy_fixture.cpp b/libs/cua-driver/hyprland-plugin/tests/desktop_fault_policy_fixture.cppnew file mode 100644index 0000000000..45c48efa69--- /dev/null+++ b/libs/cua-driver/hyprland-plugin/tests/desktop_fault_policy_fixture.cpp@@ -0,0 +1,168 @@+// Used by desktop_fault_policy_test.py, which inserts actual production bodies.+// Transport effects are counted here; native protocol/app behavior is separate.+#include "drag_geometry.hpp"+#include "input_grant.hpp"+#include "passive_pointer_target.hpp"++#include <algorithm>+#include <cstdlib>+#include <iostream>+#include <memory>+#include <optional>+#include <string>+#include <vector>++using namespace cua::hyprland;+using Clock = InputGrant::Clock;+struct Window {};+struct Surface { int client() const { return 1; } };+using Target = PassivePointerTarget<std::weak_ptr<Window>, std::weak_ptr<Surface>>;+struct Client {+ bool dead = false;+ void* source = nullptr;+ int fd = -1;+ std::string token = "old-target";+ std::weak_ptr<Window> window;+ std::weak_ptr<Surface> surface;+ uint64_t approved_deadline = 42, revision = 1;+};+struct Pointer {+ bool dead = false, resource_live = true;+ Pointer* wl = this;+ std::shared_ptr<Surface> focus;+ bool resource() const { return resource_live; }+};+struct Drag { Client* client; DragGeometry geometry{1}; };+struct Trace { void mark(const char*, unsigned) {} };+enum class ForegroundFailureReason { none };+struct ForegroundFailure { ForegroundFailureReason reason; };+void wl_event_source_remove(void*) {}+int close(int) { return 0; }+std::string refusal(std::string_view reason) { return std::string(reason); }+std::string refusal(std::string_view reason, ForegroundFailure) { return refusal(reason); }+void check(bool value, const char* message) {+ if (!value) { std::cerr << message << '\n'; std::exit(1); }+}++struct Lane {+ static constexpr bool kProduction = true;+ bool suspended = false, session = true, layout = true, target_live = true;+ bool primary_busy = false, peer_busy = false, refresh_ok = true;+ bool foreground_started = false, keyboard_focus = true;+ unsigned lane = 0, held_button = 272, releases = 0, leaves = 0;+ uint64_t capabilities = 8, desktop_generation = 1;+ Trace* trace = nullptr;+ void* listen_source = nullptr;+ int listener = -1;+ std::shared_ptr<Window> window = std::make_shared<Window>();+ std::shared_ptr<Surface> surface = std::make_shared<Surface>();+ Target::Geometry geometry{20, 30, 500, 400, 20, 30};+ Target pointer_target;+ std::vector<std::unique_ptr<Pointer>> pointers;+ std::vector<std::unique_ptr<Client>> clients;+ Client* lease = nullptr;+ Client* reservation = nullptr;+ std::optional<Drag> drag;+ InputGrant grant;+ Clock::time_point expires;+ std::vector<unsigned> held_keys{42};+ std::vector<std::string> responses;++ Lane() {+ clients.push_back(std::make_unique<Client>());+ lease = reservation = clients.front().get();+ lease->window = window; lease->surface = surface;+ pointer_target.capture(window, surface, geometry);+ pointers.push_back(std::make_unique<Pointer>());+ pointers.front()->focus = surface;+ grant.arm(8, Clock::now()); expires = grant.deadline();+ drag.emplace(lease);+ }+ bool available() const { return !suspended && session; }+ bool layout_qualified() const { return layout; }+ bool refresh(Client&) const { return refresh_ok; }+ template<typename T> bool primary_conflict(const T&) const { return primary_busy; }+ template<typename T> bool agent_conflict(const T&) const { return peer_busy; }+ std::optional<Target::Geometry> target_geometry(+ const std::shared_ptr<Window>& w, const std::shared_ptr<Surface>& s) const {+ if (!target_live || !w || !s) return {};+ return geometry;+ }+ void send(Client&, const std::string& response) { responses.push_back(response); }+ void finish_foreground() { foreground_started = false; }+ void release_pointer_button() { if (held_button) ++releases; held_button = 0; }+ void leave_pointer() {+ release_pointer_button(); ++leaves; pointer_target.reset();+ for (auto& pointer : pointers) pointer->focus.reset();+ }+ void leave_keyboard() { held_keys.clear(); keyboard_focus = false; }+ void cleanup_socket() {}+ // PRODUCTION_METHODS+ void check_inert(bool retained = true) const {+ check(!lease && !drag && !held_button && held_keys.empty() && !keyboard_focus &&+ !capabilities && grant.deadline() == Clock::time_point{} &&+ expires == Clock::time_point{}, "fault retained input or action authority");+ check(releases == 1, "held button was not released exactly once");+ check(pointer_target.entered() == retained && leaves == (retained ? 0u : 1u),+ "unexpected pointer leave/retention");+ }+};++int main() {+ { Lane l; l.suspend(); l.guard_targets(); l.check_inert();+ check(!l.reservation && l.clients.empty() && !l.available(), "disable retained admission"); }+ { Lane l; l.suspend("plugin_shutdown"); l.guard_targets(); l.check_inert(false); }+ { Lane l; l.desktop_transition(); l.session = false; l.layout = false;+ l.guard_targets(); l.check_inert();+ check(!l.reservation && l.clients.front()->dead && l.desktop_generation == 2,+ "transition retained connection or generation");+ l.session = true; l.layout = true; l.guard_targets(); l.check_inert(); }+ { Lane l; l.desktop_generation = UINT64_MAX; l.desktop_transition();+ l.guard_targets(); l.check_inert(); check(l.suspended, "generation overflow admitted input"); }+ for (bool session_fault : {true, false}) {+ for (int path = 0; path < 3; ++path) {+ Lane l;+ if (session_fault) l.session = false; else l.layout = false;+ if (path == 0) l.guard_targets();+ if (path == 1) l.target_refusal(*l.lease);+ if (path == 2) l.action_refusal(*l.lease);+ l.guard_targets(); l.check_inert();+ if (path == 1)+ check(l.clients.front()->token.empty() && !l.clients.front()->approved_deadline,+ "refused TARGET retained old binding authority");+ l.session = true; l.layout = true; l.guard_targets(); l.check_inert();+ }+ }+ // Every passive safety guard still applies during either desktop fault.+ for (bool session_fault : {true, false}) {+ for (int bad = 0; bad < 8; ++bad) {+ Lane l; l.desktop_transition();+ if (session_fault) l.session = false; else l.layout = false;+ switch (bad) {+ case 0: l.target_live = false; break;+ case 1: ++l.geometry[0]; break;+ case 2: l.pointers.front()->dead = true; break;+ case 3: l.pointers.front()->resource_live = false; break;+ case 4: l.pointers.front()->focus.reset(); break;+ case 5: l.primary_busy = true; break;+ case 6: l.peer_busy = true; break;+ case 7: l.window.reset(); break;+ }+ l.guard_targets(); l.check_inert(false);+ }+ }+ // Active safety failures and explicit human takeover continue to leave.+ for (int bad = 0; bad < 6; ++bad) {+ Lane l;+ switch (bad) {+ case 0: l.refresh_ok = false; break;+ case 1: l.primary_busy = true; break;+ case 2: l.peer_busy = true; break;+ case 3: ++l.lease->revision; break;+ case 4: l.expires = Clock::time_point{}; break;+ case 5: l.cancel_authority("primary_target_busy", false); break;+ }+ l.guard_targets(); l.check_inert(false);+ }+ std::cout << "production desktop fault policy tests passed\n";+}diff --git a/libs/cua-driver/hyprland-plugin/tests/desktop_fault_policy_test.py b/libs/cua-driver/hyprland-plugin/tests/desktop_fault_policy_test.pynew file mode 100644index 0000000000..68ca6c882b--- /dev/null+++ b/libs/cua-driver/hyprland-plugin/tests/desktop_fault_policy_test.py@@ -0,0 +1,67 @@+"""Execute production cleanup decisions with fake transport/compositor state.++This is policy coverage, not evidence of Wayland delivery or app recovery. The+method bodies come from input_experiment.cpp so changing a production call site+changes the behavior under test; the fixture does not restate those decisions.+"""+import os+from pathlib import Path+import re+import shlex+import shutil+import subprocess+import tempfile+import unittest+++ROOT = Path(__file__).resolve().parents[1]+++def method(source, name):+ match = re.search(r'^ void ' + name + r'\(.*?^ }', source, re.M | re.S)+ if not match:+ raise AssertionError(f'production method not found: {name}')+ return match.group()+++def fixture(source):+ methods = '\n'.join(method(source, name) for name in (+ 'invalidate', 'retire_grant', 'revoke', 'suspend', 'desktop_transition',+ 'guard_targets', 'cancel_authority'))+ # Exercise the real refusal branches from both TARGET and action dispatch.+ for label, start, end in (+ ('target_refusal', 'const auto requested_cap =', 'const auto pid ='),+ ('action_refusal', 'const auto sequence = number(f[1]);', 'if (lease && Clock::now() >= expires)'),+ ):+ block = source.split(start, 1)[1].split(end, 1)[0]+ refusals = [line.strip() for line in block.splitlines()+ if 'if (' in line and ('!available()' in line or '!layout_qualified()' in line)]+ if len(refusals) != 2:+ raise AssertionError(f'production refusal branches not found: {label}')+ methods += '\nvoid ' + label + '(Client& c) {\n' + '\n'.join(refusals) + '\n}'+ return (ROOT / 'tests/desktop_fault_policy_fixture.cpp').read_text().replace(+ '// PRODUCTION_METHODS', methods)+++class DesktopFaultPolicyTests(unittest.TestCase):+ def test_production_desktop_fault_cleanup(self):+ source = (ROOT / 'src/input_experiment.cpp').read_text()+ # Fail if the tested guard is disconnected from the production timer.+ self.assertIn('guard_targets();', method(source, 'step'))+ compiler = shlex.split(os.environ.get('CXX', '')) or [+ shutil.which('clang++-18') or shutil.which('clang++') or 'c++']+ with tempfile.TemporaryDirectory(prefix='cua-desktop-fault-policy-') as directory:+ cpp = Path(directory) / 'policy.cpp'+ binary = Path(directory) / 'policy'+ cpp.write_text(fixture(source))+ build = subprocess.run([*compiler, '-std=c++20', '-Wall', '-Wextra',+ '-Wpedantic', '-Werror', '-I', str(ROOT / 'src'),+ str(cpp), '-o', str(binary)],+ capture_output=True, text=True, timeout=60)+ self.assertEqual(build.returncode, 0, build.stdout + build.stderr)+ result = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)+ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)+++if __name__ == '__main__':+ unittest.main()