Repository navigation
feat: type and press keys without moving keyboard focus - #483
Conversation
type_text and press_key take delivery: "background". The target picks the route and a target no route serves is refused with nothing sent, never typed through focus: - an AT-SPI element: text goes in through EditableText at its caret and reads back; Enter or space runs the action the key means on a button, menu item, check box, link or single-line field - a browser tab over a DevTools port the user opened (COMPUTER_USE_LINUX_DEVTOOLS): Input.insertText and dispatchKeyEvent - a terminal by tty: kitty remote control, wezterm cli, tmux, GNU screen, TIOCSTI or the pty master where the kernel allows them On GNOME, refusals point to agent-workspace-linux for full isolation. doctor lists the routes in capabilities.background_input. press_key accepts KP_Enter. Refs #480
Independent reviewReviewed head 092d222. Findings, most serious first. 1. GNU screen route can type into the wrong session or window (src/background_input.rs:1004-1015)
2. screen paste can send stale buffer contents, and it leaves the text in screen's paste buffer (src/background_input.rs:1209-1247)
3. TIOCSTI is chosen whenever the sysctl allows it, but it can only work for root, and choosing it blocks the pidfd route (src/background_input.rs:858-861, 554-559)The kernel's 4. tty and pty-master matching ignores devpts instances (src/background_input.rs:638-685, 320-326)
5. The DevTools route trusts whatever listens on the configured loopback port (src/devtools.rs:232, server.rs:10463)
6. A background password insert can overwrite the whole field when AT-SPI Text is missing (src/atspi_tree.rs:1332-1342)
7. terminal_pid resolves through stdin, not the controlling tty (src/background_input.rs:320, server.rs:10236-10244)
Checked, no issue found
|
There was a problem hiding this comment.
This is an auto review done by revuto.
Read the full diff (src/background_input.rs, src/devtools.rs, the src/server.rs and src/atspi_tree.rs hunks, the fixture script) and traced the route picker against the existing focus and Hyprland paths. Nothing was built or run.
Three findings, all in the route selection rather than the element or DevTools paths:
- TIOCSTI is chosen from the sysctl alone. On a kernel with
dev.tty.legacy_tiocsti = 1it is picked for every tty, fails with EPERM on any tty that is not the server's own controlling terminal, and shadows the pty-master route behind it. The new terminal fixture's plain-pty branch would also fail on such a kernel. - The GNU screen channel trusts an inherited
STY/WINDOW. A tty whose processes merely inherited those variables gets its text pasted into a different screen window. tmux and kitty both verify the tty; screen does not. - Explicit
delivery: "background"with a window target is refused on Hyprland even withCOMPUTER_USE_LINUX_HYPRLAND_BACKGROUND=1, where the same call withoutdeliveryis delivered without focus.
Minor: uid_of at src/background_input.rs:417 has a bare unsafe { libc::getuid() } with no // SAFETY: comment, unlike the other two unsafe blocks in the file.
The AT-SPI element path (snapshot and owner checks, action chosen only from the element's own action list, no caret move in named mode) and the DevTools client (loopback only, id validation, bounded frames, call timeouts) read correctly. No version, dependency or required-parameter changes.
| } | ||
|
|
||
| match legacy_tiocsti_enabled() { | ||
| Some(true) => { |
There was a problem hiding this comment.
This is an auto review done by revuto.
TIOCSTI is selected on the sysctl alone, so it fails for almost every tty and blocks the pty-master route.
legacy_tiocsti = 1 only lifts the CAP_SYS_ADMIN requirement. The kernel still returns EPERM unless the tty is the caller's controlling terminal, as the doc comment on tiocsti_send already says. The probe does not check that, so on such a kernel:
- Every tty other than the server's own controlling terminal gets
TerminalChannel::Tiocsti, and the send then fails with EPERM. - The
pidfd_getfdpty-master route below is never tried, even whereptrace_scopewould allow it. - The caller gets a raw
TIOCSTI on /dev/pts/N: the kernel refused…error instead ofprobe.refusal()with the emulator name, the notes and the GNOME hint. available_routes(line 298) reportstiocstitodoctoras a usable route on the same condition.
The fixture hits this too: the plain-pty branch in scripts/background_input_test.py asserts 'nothing reaches the program' when the call is not ok, which this error does not contain. The test would fail on a kernel with the sysctl at 1.
Suggested fix: pick TIOCSTI only when the target is the server's controlling tty (compare stat_tty of /proc/self/stat with (major, minor)) or the process has CAP_SYS_ADMIN. Otherwise add a note and fall through to the pty master.
| /// or a screen client whose server it started. | ||
| fn screen_channel(processes: &[TtyProcess]) -> Option<TerminalChannel> { | ||
| for process in processes { | ||
| if let Some(session) = environ_value(&process.environ, "STY").filter(|v| !v.is_empty()) { |
There was a problem hiding this comment.
This is an auto review done by revuto.
The screen channel trusts STY/WINDOW from the environment without checking that the tty belongs to that session.
Those variables are inherited by anything started from inside a screen window: a terminal emulator launched from it (xterm &, kitty &), a vim :terminal, a script session. Processes on that new pty still carry the outer window's STY and WINDOW. For such a tty this returns Screen { session, window }, and screen_paste puts the text into the screen window that launched it, not the tty the call named. The result still reports success.
The other channels verify the tty: tmux matches #{pane_tty} / #{client_tty}, kitty matches the window's pids against the tty. Screen is also probed before the pty-master route, so the wrong channel wins even where a correct one exists.
probe_terminal already has what is needed to check this. For a real screen window the pty master is held by the SCREEN server, whose pid is the numeric prefix of STY (<pid>.<name>). Requiring holder to be that pid (or at least comm == "SCREEN") before taking the channel would close it.
| .map(BackgroundRoute::Terminal) | ||
| .map_err(|error| format!("Nothing was sent: {error}.")), | ||
| None => Err(format!( | ||
| "Nothing was sent and keyboard focus did not move: window_id {} is not a terminal with a known tty, and this desktop has no route that types into a whole window without focusing it. Name an element from get_app_state, a browser_tab, or a terminal running tmux.{}", |
There was a problem hiding this comment.
This is an auto review done by revuto.
On Hyprland with COMPUTER_USE_LINUX_HYPRLAND_BACKGROUND=1, explicit delivery: "background" on a window target is refused, although that desktop already has a window-level background route.
dispatch_background_keyboard never consults hyprland_background_input. A call with delivery: "background" and a window_id therefore ends here with "this desktop has no route that types into a whole window without focusing it". The same call without delivery goes through dispatch_press_key / dispatch_type_text and is delivered by send_key_state without focus. A terminal window on Hyprland is affected the same way: it only gets the tmux/kitty/screen probe.
So asking for background delivery explicitly is strictly worse than omitting it on the one desktop that supports it. The SKILL.md line added in this PR tells agents to pass delivery: "background", directly under the Hyprland line telling them to pass window_id on every call.
Suggested fix: when hyprland_background_applies(target) holds, hand window targets to hyprland_background_input in the route picker (after the terminal route, or before it), and list it in capabilities.background_input.
From the independent review on #483: - screen: use a session only when its SCREEN process holds the tty's pty master, since STY and WINDOW are inherited; paste through one eval that reads a private register, pastes and clears it, and delete the file only after a -Q round trip, so a failed read pastes nothing - TIOCSTI only with CAP_SYS_ADMIN, the only way it works on another terminal; otherwise go on to the pty master - match processes by the tty they hold (same devpts instance) and the pty master holder by mount namespace, so a container pts with the same number is never the target - terminal_pid resolves through the controlling tty, not stdin - DevTools: connect only when the loopback listener is the same user's - a named password field without AT-SPI Text is refused instead of having its contents replaced - the terminal fixture covers kitty remote control on a private display
There was a problem hiding this comment.
This is an auto review done by revuto.
Screen can still deliver input to the wrong window after renumbering, and the revised paste path destroys saved register contents. The earlier Hyprland finding remains open.
| if screen_session_pid(&session) == Some(*holder_pid) { | ||
| return Some(TerminalChannel::Screen { | ||
| session, | ||
| window: environ_value(&process.environ, "WINDOW"), |
There was a problem hiding this comment.
This is an auto review done by revuto.
[P1] Verify the Screen window, not just its session
The holder check establishes the session, but WINDOW is the window number at creation time, as Screen’s manual documents. Screen’s number command can exchange two windows’ numbers without updating their processes’ environments. After swapping windows 0 and 1, a call naming the original window 0’s tty passes this check but uses -p 0, delivering text or Enter to the other window and reporting success. Resolve the current window against the requested tty, or refuse when that binding cannot be verified.
| .map(BackgroundRoute::Terminal) | ||
| .map_err(|error| format!("Nothing was sent: {error}.")), | ||
| None => Err(format!( | ||
| "Nothing was sent and keyboard focus did not move: window_id {} is not a terminal with a known tty, and this desktop has no route that types into a whole window without focusing it. Name an element from get_app_state, a browser_tab, or a terminal running tmux.{}", |
There was a problem hiding this comment.
This is an auto review done by revuto.
[P2] Connect explicit background delivery to the existing Hyprland route
Still open from the previous review: on Hyprland with COMPUTER_USE_LINUX_HYPRLAND_BACKGROUND=1, a nonterminal window_id target with delivery: "background" reaches this refusal. Omitting delivery sends the same input without focus through dispatch_type_text/dispatch_press_key and hyprland_background_input. The explicit background dispatcher never consults that existing route. Wire it into background delivery so requesting the documented mode does not disable a working backend.
| // returns only once screen has, so the file can go. | ||
| let read = format!("readreg {SCREEN_REGISTER} {}", screen_quote(&path_text)); | ||
| let paste = format!("paste {SCREEN_REGISTER}"); | ||
| let clear = format!("register {SCREEN_REGISTER} ''"); |
There was a problem hiding this comment.
This is an auto review done by revuto.
[P2] Preserve the user’s Screen register contents
SCREEN_REGISTER is fixed to q, which is a global Screen register, not private storage. Every successful background text or key call overwrites it with readreg q and then clears it here. A user who saved text or a macro in register q permanently loses it after an unrelated input call. Preserve and restore its contents, or use a delivery mechanism that does not overwrite an existing user register.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit c1c4f46. Configure here.
| } | ||
| if (e.isContentEditable) return {kind: 'editable', tag}; | ||
| return {kind: 'other', tag, role: e.getAttribute('role') || ''}; | ||
| })()"#; |
There was a problem hiding this comment.
DevTools misses iframe and body editors
Medium Severity
The tab focus script treats document.body as no field, and any iframe whose inner activeElement is body as a cross-origin frame. Same-origin iframe composers (contenteditable body, TinyMCE/CKEditor) and designMode pages are refused with a misleading origin error, so background type_text never reaches those editors.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit c1c4f46. Configure here.
| let mut file = fs::File::from(fd.try_clone()?); | ||
| file.write_all(bytes) | ||
| .context("writing to the pty master failed") | ||
| } |
There was a problem hiding this comment.
Pty master write can hang input
High Severity
Writing to a copied pty master uses a blocking write_all with no timeout, unlike kitty/tmux/wezterm/screen which are capped at 5 seconds. background_terminal holds the global input lock around that write, so a slave that is not reading can stall every later click, key, and type call.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit c1c4f46. Configure here.
- GNU screen: only a client's tty is a screen route (pastes into the session's current window); a window's own tty is refused, since window numbers can be swapped without updating WINDOW. Paste through the ^_ register, not a register a user may keep text in. - a tty taken from a terminal window must have its pty master held by that window's terminal, so a container's pts with the same number is refused instead of reaching a host pane - TIOCSTI also on the server's own controlling tty - the pty master write is non-blocking with a 2 s limit, so a program that stops reading cannot hold the input lock - DevTools: a designMode document or contenteditable body, also in a same-origin iframe, is an editor - Hyprland background mode serves delivery "background" window targets
There was a problem hiding this comment.
This is an auto review done by revuto.
Re-review of c1c4f46a..cc54b54a.
Resolved from the earlier review
- TIOCSTI is now picked only with CAP_SYS_ADMIN or on the server's own controlling tty, and otherwise falls through to the pty master.
- The screen window route (
STY/WINDOW) is gone, so neither the inherited-environment case nor the renumbered-window case can deliver to the wrong window. - The screen paste no longer uses register
q. - A non-terminal Hyprland window with
delivery: "background"now reachessend_key_state.
Still open or new
- New:
write_mastersetsO_NONBLOCKon the terminal emulator's own pty master and never restores it. - Still open in part: a terminal window on Hyprland is still refused with explicit
delivery: "background", while the same call withoutdeliveryis delivered without focus. - Lower confidence: the screen client route does not tell screen which attached client it means. This rests on my recollection of screen's display selection, which I could not check in this sandbox.
Nothing was built or run; this is from reading the diff and the surrounding code.
| // SAFETY: fcntl and write on an fd this process owns, with a valid buffer. | ||
| unsafe { | ||
| let flags = libc::fcntl(raw, libc::F_GETFL); | ||
| if flags < 0 || libc::fcntl(raw, libc::F_SETFL, flags | libc::O_NONBLOCK) < 0 { |
There was a problem hiding this comment.
This is an auto review done by revuto.
[P1] O_NONBLOCK is set on the terminal emulator's own pty master and never restored
The fd comes from pidfd_getfd, which duplicates the descriptor onto the same open file description. O_NONBLOCK is a file status flag, so F_SETFL here changes it for the emulator's fd as well. The flag stays set after the call returns and after this copy is closed.
A holder that reads or writes its master in blocking mode then gets EAGAIN instead of blocking. Holders that read on a dedicated thread (pty4j-style or portable-pty-style readers) typically treat that as a read error and end the session. That is a persistent side effect on the user's terminal from a single background call, including one that fails or times out.
Suggested fix: keep the original flags and put them back on every return path when they did not already include O_NONBLOCK. Alternatively, leave the description blocking and bound the write another way, for example poll(POLLOUT) against the deadline with small writes, or a blocking write on a helper thread with a timeout.
Smaller point in the same function: the retry loop uses std::thread::sleep for up to 2 seconds inside an async call path, which stalls a runtime worker while the input lock is held.
| .map_err(|error| format!("Nothing was sent: {error:#}"))?; | ||
| match window.terminal.as_ref() { | ||
| Some(terminal) => crate::background_input::tty_path(&terminal.tty) | ||
| .map(|tty| BackgroundRoute::Terminal(tty, window.pid)) |
There was a problem hiding this comment.
This is an auto review done by revuto.
[P2] Still open in part: a terminal window on Hyprland is refused with explicit delivery: "background"
The new HyprlandWindow arm is only reached when window.terminal is None. A window recognised as a terminal always takes BackgroundRoute::Terminal, and background_terminal refuses when the probe finds no channel. That is the normal case for foot or Alacritty without tmux, where ptrace_scope is 1.
The same call without delivery goes through dispatch_type_text/dispatch_press_key to hyprland_background_input, which sends to any Hyprland window, terminal or not, without focus. The same holds for a tty or terminal_pid selector, which returns Terminal before the window is resolved.
So with COMPUTER_USE_LINUX_HYPRLAND_BACKGROUND=1, asking for background delivery on a terminal window is still strictly worse than omitting it. The README line added here says a window target with delivery: "background" goes through send_key_state.
Suggested fix: when the terminal probe yields no channel and hyprland_background_applies(target) holds for a Hyprland window, fall back to the HyprlandWindow route instead of refusing. available_routes also still does not list this route for doctor.
| let server = process_args(pid) | ||
| .first() | ||
| .is_some_and(|arg0| arg0 == "SCREEN"); | ||
| (server && clients.contains(&ppid)).then(|| TerminalChannel::Screen { |
There was a problem hiding this comment.
This is an auto review done by revuto.
[P2] The screen client route cannot tell screen which attached client it means
This is now the only screen route, and the README describes it as "the current window of a client on the tty". The command that is sent is screen -S <pid> -X eval … with no -p. Nothing in it identifies the client on the requested tty, so screen picks the display itself.
As far as I recall from screen's command handling, a remote command with no preselected window runs against the first display of that user in screen's display list, and the most recently attached display is first. I could not check screen's source or manual in this sandbox, so please confirm.
If that holds, the failure case is a session with a second client attached (screen -x) showing a different window. A call naming the creating client's tty then pastes into the other client's foreground window and reports success with "its current window".
The tmux route avoids this by resolving the active pane of the specific client via #{client_tty}. Suggested fix: refuse when the session has more than one display attached, or otherwise confirm that the display screen will use is the one on the requested tty.
- the pty master copy shares the terminal's open file description, so its blocking mode is left alone: each byte waits for room with poll, bounded at two seconds, on a blocking thread off the async workers - GNU screen: use a session only when no other client may be attached, since a remote command goes to a display screen picks - Hyprland background mode also serves a terminal window that no tty channel reaches; doctor lists it as hyprland
|
Review findings, where each one landed:
revuto stopped at its 3-round limit; the independent review above and this round are the review of record. CI is green on c38c742. |


The keyboard half of #480. The pen handles the pointer; this lets an agent type and press keys into a target while the user keeps keyboard focus.
What
type_textandpress_keytakedelivery: "background". The target picks the route. Naming an element or abrowser_tabselects it on its own. When no route serves the target, the call is refused and nothing is sent. It never falls back to focusing the window.element_index+snapshot_id,element_identifier, orrole/name/statesactivatebrowser_tab(id, or text in one tab's URL or title) withCOMPUTER_USE_LINUX_DEVTOOLSInput.insertTextat the tab's focused element, read back in inputs and textareasInput.dispatchKeyEventttyor the terminal selectorswezterm cli, tmux, GNU screen, TIOCSTI, pty master viapidfd_getfd, in that orderThe result names the channel. GNU screen is used through a client's tty, when no other client may share its session; a screen window's own tty is refused.
doctorlists the routes incapabilities.background_input.press_keyalso acceptsKP_Enteron every backend.Limits
set_value. Keys with modifiers, and any key not listed above, are refused.--remote-debugging-portand its own--user-data-dir. The server reads the port from the variable (a number or aDevToolsActivePortpath), connects only to 127.0.0.1 and only when that listener belongs to the same user, and never starts or restarts a browser. The tab needs a focused field for text. Browser shortcuts outside the page (Ctrl+T) do not run. A field in a cross-origin frame is refused.allow_remote_controlandlisten_on; the server reads the socket from theKITTY_LISTEN_ONkitty gives its shells. TIOCSTI only wheredev.tty.legacy_tiocstiis 1 and the server has CAP_SYS_ADMIN (the kernel refuses it on another terminal otherwise). The pty master only where ptrace rules let this process take it (ptrace_scope1 refuses). GNOME Terminal, Ptyxis and Ghostty have no remote input command, and Konsole's D-Bus route is not wired yet, so programs in them need tmux or screen. The refusal names the terminal found and the setting that would open a route. Text never goes in a process argument. The server never writes to/dev/pts/N, which would only print on the screen.Tests
tests/background_input.rsrunsscripts/background_input_test.pyin three fixtures. Each fails on main and passes here.DevToolsActivePortfile. Text reads back, Enter submits the form, a server without the variable refuses.cargo fmt,clippy -D warnings,cargo test --no-fail-fast(all pass), rustdoc,mcp_safety_check(now also checks the background parameters),check_plugins, env forwarding, Pi catalog--check.Review fixes from the independent review are in the second commit: screen sessions are checked against the pty master holder and pasted through one eval with a sync round trip, processes are matched on the same devpts instance,
terminal_piduses the controlling tty, and a password field without AT-SPI Text is refused.Live check
GNOME 50.1 Wayland. A GTK target window, a kitty with remote control on its own socket (
--config NONE), a kitty running a tmux client with no remote control, and a Chrome with its own profile and debugging port, plus a GTK focus-holder window activated last.focused_windowreturned the holder before and after every call. Results: the entry readhello world, the button clicked, the check box toggled, kitty'scatgothello kittythrough kitty remote control, the tmux pane gothello tmuxthrough the client's tty, the tab input readabcXYZand Enter submitted the form. The holder got no input. All windows were closed afterwards.