diff --git a/console/src-tauri/src/computer_use_runtime.rs b/console/src-tauri/src/computer_use_runtime.rs index 80626c095a2..8fb05235f5c 100644 --- a/console/src-tauri/src/computer_use_runtime.rs +++ b/console/src-tauri/src/computer_use_runtime.rs @@ -1130,6 +1130,25 @@ fn serve_control_connection( ControlResponse::error("runtime_unavailable") } }, + // TCC decisions are cached per process, so a helper started before + // the user granted Screen Recording or Accessibility never sees + // the grant. Replace it so the fresh helper picks the + // authorization up. + "restart" => { + if let Err(err) = stop_helper(app) { + log::warn!("[computer-use] control restart stop failed: {err}"); + } + match ensure(app).and_then(|_| { + runtime_capability(app) + .ok_or_else(|| "Computer Use helper did not expose a capability".to_string()) + }) { + Ok(capability) => ControlResponse::capability(capability), + Err(err) => { + log::warn!("[computer-use] control restart failed: {err}"); + ControlResponse::error("runtime_unavailable") + } + } + }, #[cfg(target_os = "macos")] "begin_focus" => match (request.helper_pid, request.lease_id.as_deref()) { (Some(helper_pid), Some(lease_id)) if !lease_id.is_empty() => { diff --git a/src/qwenpaw/app/computer_use/runtime.py b/src/qwenpaw/app/computer_use/runtime.py index 8c2c66bbb42..d6bf038adb2 100644 --- a/src/qwenpaw/app/computer_use/runtime.py +++ b/src/qwenpaw/app/computer_use/runtime.py @@ -113,6 +113,31 @@ def acquire_capability(cls) -> RuntimeCapability | None: cls._capability = capability return capability + @classmethod + def restart_helper(cls) -> bool: + """Replace the live helper with a freshly started one. + + macOS caches TCC decisions (Screen Recording, Accessibility) per + process. A helper that started before the user granted those + permissions keeps being denied even after the grant, so it must be + replaced before the authorization becomes visible to Computer Use. + """ + with cls._lock: + control = _control_endpoint() + if control is None: + return False + response = _request_control(control, {"action": "restart"}) + # Whatever the host managed to do, the old helper is gone now; + # neither the cached capability nor the environment bootstrap + # (which names the old helper's endpoint) may answer again. + cls._capability = None + cls._environment_spent = True + capability = _capability_from_response(response) + if capability is None: + return False + cls._capability = capability + return True + @classmethod def invalidate_capability(cls, capability: RuntimeCapability) -> None: """Forget a capability whose endpoint has gone away. @@ -210,8 +235,9 @@ def _control_endpoint() -> _ControlEndpoint | None: return _ControlEndpoint(host, port, token) -def _request_capability(control: _ControlEndpoint) -> RuntimeCapability | None: - response = _request_control(control, {"action": "acquire"}) +def _capability_from_response( + response: dict[str, object] | None, +) -> RuntimeCapability | None: if response is None or response.get("ok") is not True: return None pipe_name = response.get("pipe_name") @@ -223,6 +249,12 @@ def _request_capability(control: _ControlEndpoint) -> RuntimeCapability | None: return RuntimeCapability(pipe_name, secret, COMPUTER_USE_PROTOCOL_VERSION) +def _request_capability(control: _ControlEndpoint) -> RuntimeCapability | None: + return _capability_from_response( + _request_control(control, {"action": "acquire"}), + ) + + def _request_control( control: _ControlEndpoint, fields: dict[str, object], diff --git a/tests/unit/plugins/computer_use/test_capability_recovery.py b/tests/unit/plugins/computer_use/test_capability_recovery.py index 40b82d22998..ce4cea238d0 100644 --- a/tests/unit/plugins/computer_use/test_capability_recovery.py +++ b/tests/unit/plugins/computer_use/test_capability_recovery.py @@ -148,3 +148,69 @@ def test_no_host_and_no_environment_means_no_capability( ) -> None: monkeypatch.setattr(runtime_module, "_control_endpoint", lambda: None) assert HostRuntimeProvider.acquire_capability() is None + + +def test_restart_helper_replaces_the_cached_capability( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A helper replaced after a TCC grant hands out a new endpoint. + + The old helper is dead, so the restarted one must answer with a fresh + capability and the cached one must never be returned again. + """ + monkeypatch.setattr(runtime_module, "_control_endpoint", object) + responses = iter( + [ + {"ok": True, "pipe_name": "pipe-old", "capability": "secret"}, + {"ok": True, "pipe_name": "pipe-new", "capability": "secret"}, + ], + ) + monkeypatch.setattr( + runtime_module, + "_request_control", + lambda _control, _fields: next(responses), + ) + + first = HostRuntimeProvider.acquire_capability() + assert first is not None + assert first.names_same_endpoint(_capability("pipe-old")) + + assert HostRuntimeProvider.restart_helper() is True + + second = HostRuntimeProvider.get_capability() + assert second is not None + assert second.names_same_endpoint(_capability("pipe-new")) + + +def test_restart_helper_failure_stops_answering_with_the_old_capability( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A failed restart still retires the dead helper's capability. + + The host may fail to bring a replacement up, but the old helper is gone + either way, so its endpoint must not be handed out again. + """ + monkeypatch.setattr(runtime_module, "_control_endpoint", object) + responses = iter( + [ + {"ok": True, "pipe_name": "pipe-old", "capability": "secret"}, + ], + ) + monkeypatch.setattr( + runtime_module, + "_request_control", + lambda _control, _fields: next(responses, None), + ) + + first = HostRuntimeProvider.acquire_capability() + assert first is not None + + assert HostRuntimeProvider.restart_helper() is False + assert HostRuntimeProvider.get_capability() is None + + +def test_restart_helper_without_a_control_endpoint_fails( + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(runtime_module, "_control_endpoint", lambda: None) + assert HostRuntimeProvider.restart_helper() is False