fix(computer_use): address Copilot review feedback

- Validate direction before key dispatch (unknown values return False)
- Log exceptions instead of silently swallowing them
- Add 150ms delay before overlay restart to avoid DWM race
- Route switch_desktop through _maybe_follow_capture for consistency
- Add JSON Schema if/then to require direction for switch_desktop
This commit is contained in:
灵越羽毛 2026-06-15 17:00:34 +08:00 • committed by Teknium
parent bcb13396cd
commit b3428667d4
No known key found for this signature in database
3 changed files with 22 additions and 4 deletions

View file

@ -230,6 +230,17 @@ COMPUTER_USE_SCHEMA: Dict[str, Any] = {
},
},
"required": ["action"],
"allOf": [
{
"if": {
"properties": {"action": {"const": "switch_desktop"}},
"required": ["action"],
},
"then": {
"required": ["direction"],
},
},
],
},
}

View file

@ -431,7 +431,7 @@ def _dispatch(backend: ComputerUseBackend, action: str, args: Dict[str, Any]) ->
if not hasattr(backend, "switch_desktop"):
return json.dumps({"error": "switch_desktop not supported by current backend"})
res = backend.switch_desktop(str(direction))
return json.dumps({"ok": res.ok, "message": res.message})
return _maybe_follow_capture(backend, res, capture_after)
return json.dumps({"error": f"unknown action {action!r}"})

View file

@ -302,6 +302,9 @@ def _switch_desktop_via_keybd(direction: str, overlay_client) -> bool:
virtual-desktop transition, so we stop it before switching and restart
it on the new desktop.
"""
if direction not in ("left", "right"):
return False
VK_CONTROL = 0x11
VK_LWIN = 0x5B
VK_LEFT = 0x25
@ -322,15 +325,19 @@ def _switch_desktop_via_keybd(direction: str, overlay_client) -> bool:
try:
overlay_client.stop()
_send_inputs(seq)
# The virtual-desktop transition is asynchronous — a brief delay
# before restarting the overlay avoids racing the DWM compositor.
time.sleep(0.15)
overlay_client._dead = False
overlay_client.start()
return True
except Exception:
except Exception as e:
logger.warning("switch_desktop SendInput failed: %s", e)
try:
overlay_client._dead = False
overlay_client.start()
except Exception:
pass
except Exception as e2:
logger.warning("switch_desktop overlay restart also failed: %s", e2)
return False