Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(cu): never strand a pressed button or modifier when SendInput is refused
rubenluetke10-beep0361e6edf6c5
diff --git a/jarvis/cu/actuate/windows.py b/jarvis/cu/actuate/windows.pyindex 0191e4ba5c..e28c2890af 100644--- a/jarvis/cu/actuate/windows.py+++ b/jarvis/cu/actuate/windows.py@@ -126,6 +126,22 @@ _EXTENDED_VKS = frozenset({})+class SendInputRefused(OSError):+ """``SendInput`` accepted only part of an input batch (or none of it).++ An ``OSError`` subclass so existing ``except OSError`` / ``except+ Exception`` handlers around actuation keep working unchanged. ``injected``+ is the extra fact they could not get before: how many events the OS took+ before it stopped. Pointer callers need exactly that number to tell whether+ a button-DOWN is still held and a recovery release is owed.+ """++ def __init__(self, message: str, *, injected: int, total: int) -> None:+ super().__init__(message)+ self.injected = injected+ self.total = total++def resolve_vk(key: str) -> int | None:"""Virtual-key code for a key name, or ``None`` for an unknown key."""k = key.strip().lower()@@ -259,10 +275,110 @@ class WindowsActuator(Actuator):def _send(self, events: list) -> None:t = self._tarr = (t.INPUT * len(events))(*events)- sent = t.send_input(len(events), arr, t.ctypes.sizeof(t.INPUT))- if sent != len(events):- err = t.ctypes.get_last_error()- raise t.ctypes.WinError(err if err else None)+ sent = int(t.send_input(len(events), arr, t.ctypes.sizeof(t.INPUT)))+ if sent == len(events):+ return+ err = t.ctypes.get_last_error()+ # Read the code ctypes SAVED for us, and say so plainly when there is+ # none. ``WinError(None)`` reads the THREAD's last-error instead, which+ # the ``use_last_error=True`` wrapper has already restored to whatever+ # it was before the call — so a refused injection used to surface as+ # "OSError: [Errno 0] The operation completed successfully", the least+ # diagnosable message a failed actuation could carry.+ reason = t.ctypes.FormatError(err).strip() if err else "no Win32 error reported"+ raise SendInputRefused(+ f"SendInput injected {sent} of {len(events)} events "+ f"(last_error={err}: {reason})",+ injected=sent,+ total=len(events),+ )++ def _send_pointer_batch(+ self, events: list, button_states: list[int], *, button: str,+ ) -> None:+ """Send a batch that presses ``button``, never leaving it held down.++ ``button_states[i]`` is ``+1`` when ``events[i]`` presses the button,+ ``-1`` when it releases, ``0`` otherwise.++ SendInput fills a batch element by element and stops at the first+ rejection — UIPI blocks the rest the moment a higher-integrity window+ takes the foreground mid-batch. A partly accepted ``[move, down, up]``+ therefore lands the DOWN and drops its UP: the physical button stays+ pressed and the whole desktop is unusable until the user clicks to+ clear it. :meth:`drag_from_cursor` already pays for that lesson in a+ try/finally; the click paths raised straight through it.++ The recovery release is sent ONLY when the injected prefix actually+ leaves the button down. A batch refused outright must not inject a+ stray WM_*BUTTONUP that no press ever preceded.+ """+ try:+ self._send(events)+ except SendInputRefused as exc:+ if sum(button_states[: exc.injected]) <= 0:+ raise+ try:+ self._send([self._mouse_input(0, 0, 0, _MOUSE_FLAGS_UP[button])])+ logger.warning(+ "SendInput refused a %s-click batch after the button went "+ "down; released it again to keep the desktop usable.",+ button,+ )+ except Exception: # noqa: BLE001 — recovery is best-effort by nature+ logger.error(+ "Could not release the %s mouse button after a refused click "+ "batch — it may still be held down.", button, exc_info=True,+ )+ raise++ def _send_press_release(self, downs: list, ups: list) -> None:+ """Press ``downs`` in order, release in reverse — even when a press fails.++ ``ups[i]`` MUST be the release event for ``downs[i]`` (same order, same+ length); the reverse release order is applied here, so callers describe+ key pairs instead of hand-rolling two mirrored lists.++ ``SendInput`` reports how many events it actually inserted, and a+ partial insert is a real outcome: UIPI truncates injection the moment+ the foreground window belongs to a higher-integrity process (Task+ Manager, an elevated app, the secure desktop appearing mid-combo), and+ so does any other Win32 failure inside the batch. Sending the downs and+ the ups as ONE array meant that failure raised with the modifiers+ already in the input stream and their key-ups never emitted — so Ctrl /+ Alt / Shift / Win stayed logically HELD for the rest of the session:+ every later click became a modified click, typing turned into+ shortcuts, and nothing but tapping the physical key could clear it.++ The mouse path in this same backend already guards exactly this case+ ("a stuck pressed button makes the whole desktop unusable",+ :meth:`drag_from_cursor` and :meth:`_send_pointer_batch`); the keyboard+ path did not.++ Like the pointer recovery, this releases only the keys the OS actually+ took (``downs[:injected]``): a batch refused outright must not inject a+ stray WM_KEYUP for a key that was never pressed, which applications+ watching raw key transitions do react to.+ """+ try:+ self._send(downs)+ except SendInputRefused as exc:+ held = list(reversed(ups[: exc.injected]))+ if not held:+ raise+ try:+ self._send(held)+ logger.warning(+ "SendInput refused a key batch after %d key(s) went down; "+ "released them again to keep the keyboard usable.", len(held),+ )+ except Exception: # noqa: BLE001 — recovery is best-effort by nature+ logger.error(+ "Could not release %d key(s) after a refused batch — a "+ "modifier may still be held down.", len(held), exc_info=True,+ )+ raise+ self._send(list(reversed(ups)))def _abs_move_event(self, x: int, y: int):vx, vy, vw, vh = self._virtual_bounds()@@ -293,10 +409,13 @@ class WindowsActuator(Actuator):)with input_space():events = [self._abs_move_event(x, y)]+ states = [0] # the move neither presses nor releasesfor _ in range(2 if double else 1):events.append(self._mouse_input(0, 0, 0, _MOUSE_FLAGS_DOWN[b]))+ states.append(1)events.append(self._mouse_input(0, 0, 0, _MOUSE_FLAGS_UP[b]))- self._send(events)+ states.append(-1)+ self._send_pointer_batch(events, states, button=b)def click_at_cursor(self,@@ -328,10 +447,13 @@ class WindowsActuator(Actuator):)with input_space():events = []+ states: list[int] = []for _ in range(2 if double else 1):events.append(self._mouse_input(0, 0, 0, _MOUSE_FLAGS_DOWN[b]))+ states.append(1)events.append(self._mouse_input(0, 0, 0, _MOUSE_FLAGS_UP[b]))- self._send(events)+ states.append(-1)+ self._send_pointer_batch(events, states, button=b)def drag(self, x1: int, y1: int, x2: int, y2: int, *, duration_s: float = 0.4,@@ -405,14 +527,13 @@ class WindowsActuator(Actuator):f |= _KEYEVENTF_EXTENDEDKEYreturn f- events = [- self._key_input(vk, 0, flags(vk, keyup=False)) for vk in vk_codes- ] + [- self._key_input(vk, 0, flags(vk, keyup=True))- for vk in reversed(vk_codes)- ]+ # Presses and releases go out as two batches rather than one array, so+ # a refused press can still be undone — see _send_press_release. The+ # emitted order is unchanged: modifiers first, released last.+ downs = [self._key_input(vk, 0, flags(vk, keyup=False)) for vk in vk_codes]+ ups = [self._key_input(vk, 0, flags(vk, keyup=True)) for vk in vk_codes]with input_space():- self._send(events)+ self._send_press_release(downs, ups)def type_text(self, text: str, *, delay_s: float = 0.02) -> None:with input_space():diff --git a/tests/unit/cu/test_actuate_pointer_recovery.py b/tests/unit/cu/test_actuate_pointer_recovery.pynew file mode 100644index 0000000000..9101818825--- /dev/null+++ b/tests/unit/cu/test_actuate_pointer_recovery.py@@ -0,0 +1,142 @@+"""The Windows click paths must never strand a mouse button in the down state.++``SendInput`` fills a batch element by element and stops at the first rejection+(UIPI truncates injection the moment a higher-integrity window takes the+foreground). A partly accepted ``[move, down, up]`` therefore lands the DOWN and+drops its UP: the physical button stays pressed and the whole desktop is+unusable until the user clicks to clear it.++``drag_from_cursor`` has guarded this with a try/finally since the drag bug;+``click`` / ``click_at_cursor`` raised straight through it. These tests pin both+halves of the contract — release what the OS actually took, and never inject a+release for a press that never happened.++Pure unit tests: ``_send`` is replaced, so no real input is ever injected and+the file runs on every OS.+"""++from __future__ import annotations++import pytest++_LEFT_DOWN = 0x0002+_LEFT_UP = 0x0004+++def _pointer_actuator(refuse_after: int | None = None):+ """Recording ``WindowsActuator`` whose first ``_send`` optionally reports a+ partial injection, the way SendInput does when UIPI truncates a batch."""+ from jarvis.cu.actuate.windows import SendInputRefused, WindowsActuator++ actuator = WindowsActuator.__new__(WindowsActuator)+ actuator._t = None+ actuator._abs_move_event = lambda x, y: ("move", x, y)+ actuator._mouse_input = lambda dx, dy, data, flags: ("mouse", flags)+ actuator.cursor_pos = lambda: (10, 20)+ sent: list[list[tuple]] = []++ def _send(events):+ batch = list(events)+ sent.append(batch)+ if refuse_after is not None and len(sent) == 1:+ raise SendInputRefused(+ "refused", injected=refuse_after, total=len(batch),+ )++ actuator._send = _send+ return actuator, sent+++def test_click_sends_move_then_press_and_release() -> None:+ """Baseline: the happy path is one batch, positioned before pressing."""+ actuator, sent = _pointer_actuator()+ actuator.click(100, 200)++ assert len(sent) == 1+ assert sent[0] == [+ ("move", 100, 200), ("mouse", _LEFT_DOWN), ("mouse", _LEFT_UP),+ ]+++def test_click_releases_the_button_a_refused_batch_left_down() -> None:+ """Move + DOWN accepted, UP rejected — the button must come back up."""+ from jarvis.cu.actuate.windows import SendInputRefused++ actuator, sent = _pointer_actuator(refuse_after=2)++ with pytest.raises(SendInputRefused):+ actuator.click(100, 200)++ assert len(sent) == 2, "expected a recovery release after the refused batch"+ assert sent[1] == [("mouse", _LEFT_UP)]+++def test_click_sends_no_stray_release_when_only_the_move_landed() -> None:+ """Refused before the press: a WM_LBUTTONUP with no preceding DOWN is real+ input the caller never asked for, and applications that watch raw button+ transitions do react to it."""+ from jarvis.cu.actuate.windows import SendInputRefused++ actuator, sent = _pointer_actuator(refuse_after=1)++ with pytest.raises(SendInputRefused):+ actuator.click(100, 200)++ assert len(sent) == 1+++def test_click_sends_no_stray_release_when_nothing_landed() -> None:+ from jarvis.cu.actuate.windows import SendInputRefused++ actuator, sent = _pointer_actuator(refuse_after=0)++ with pytest.raises(SendInputRefused):+ actuator.click(100, 200)++ assert len(sent) == 1+++def test_double_click_recovery_tracks_the_actual_button_state() -> None:+ """The batch is [move, down, up, down, up]. Truncated after the FIRST+ press/release pair the button is already up — releasing again would be the+ stray event the previous test guards. Truncated one event later it is held,+ and the release is owed. A naive "did we press at all" check gets the first+ case wrong."""+ from jarvis.cu.actuate.windows import SendInputRefused++ actuator, sent = _pointer_actuator(refuse_after=3)+ with pytest.raises(SendInputRefused):+ actuator.click(1, 2, double=True)+ assert len(sent) == 1, "button was already released — no recovery owed"++ actuator, sent = _pointer_actuator(refuse_after=4)+ with pytest.raises(SendInputRefused):+ actuator.click(1, 2, double=True)+ assert len(sent) == 2+ assert sent[1] == [("mouse", _LEFT_UP)]+++def test_click_at_cursor_also_recovers_a_held_button() -> None:+ """The no-positioning path (the visible glide animation already moved the+ cursor) shares the same hazard and the same guard."""+ from jarvis.cu.actuate.windows import SendInputRefused++ actuator, sent = _pointer_actuator(refuse_after=1)++ with pytest.raises(SendInputRefused):+ actuator.click_at_cursor(expected=(10, 20))++ # No move event here, so injected=1 IS the press.+ assert len(sent) == 2+ assert sent[1] == [("mouse", _LEFT_UP)]+++def test_send_input_refused_carries_the_injected_count() -> None:+ """The recovery decision is only possible because the exception carries how+ far the batch got — a plain WinError threw that away."""+ from jarvis.cu.actuate.windows import SendInputRefused++ exc = SendInputRefused("nope", injected=2, total=5)+ assert exc.injected == 2+ assert exc.total == 5+ assert isinstance(exc, OSError), "existing except-OSError handlers must catch it"