Skip to content

feat: type and press keys without moving keyboard focus - #483

Merged
avifenesh merged 5 commits into
mainfrom
feat/focus-free-keys
Nov 19, 2026
Merged

avifenesh merged 5 commits into
mainfrom
feat/focus-free-keys

Conversation

@avifenesh

@avifenesh avifenesh commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

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_text and press_key take delivery: "background". The target picks the route. Naming an element or a browser_tab selects 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.

Route Target Text Keys
AT-SPI element element_index + snapshot_id, element_identifier, or role/name/states EditableText insert at the caret (or end), read back the element's action: Enter/space on buttons and menu items, space on check boxes, radios and toggles, Enter on links and single-line fields with activate
Browser tab browser_tab (id, or text in one tab's URL or title) with COMPUTER_USE_LINUX_DEVTOOLS Input.insertText at the tab's focused element, read back in inputs and textareas Input.dispatchKeyEvent
Terminal tty or the terminal selectors kitty remote control, wezterm cli, tmux, GNU screen, TIOCSTI, pty master via pidfd_getfd, in that order same channels

The 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. doctor lists the routes in capabilities.background_input. press_key also accepts KP_Enter on every backend.

Limits

  • Element: a selection is left alone and the caret is not moved. Line feed and tab go in only on multi-line elements; other control characters are refused. A password field takes the text but cannot be read back, as with set_value. Keys with modifiers, and any key not listed above, are refused.
  • Browser tab: the user starts the browser with --remote-debugging-port and its own --user-data-dir. The server reads the port from the variable (a number or a DevToolsActivePort path), 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.
  • Terminal: kitty needs allow_remote_control and listen_on; the server reads the socket from the KITTY_LISTEN_ON kitty gives its shells. TIOCSTI only where dev.tty.legacy_tiocsti is 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_scope 1 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.
  • GNOME has no second keyboard focus. Refusals on GNOME point to agent-workspace-linux for a fully isolated session with its own keyboard and pointer. The route picker is one function, so a seat route for other desktops can plug in after the terminal route.

Tests

  • tests/background_input.rs runs scripts/background_input_test.py in three fixtures. Each fails on main and passes here.
    • atspi: a GTK 3 app on a private Xvfb and D-Bus session. Text into a named entry reads back, a password entry takes text, Enter clicks a button, space toggles a check box, Enter activates an entry. Enter on a check box, a letter on a button, Ctrl+Enter and a newline in a single-line entry are refused. No widget of the target window gets a focus-in event.
    • terminal: a private tmux server reached by pane tty and through a client attached on another pty, a GNU screen window, kitty remote control on a private Xvfb display (when kitty is installed), and a plain pty with no multiplexer (refused, or delivered through the pty master where the kernel allows it).
    • devtools: headless Chrome on a loopback port named by its DevToolsActivePort file. Text reads back, Enter submits the form, a server without the variable refuses.
  • Unit tests for the pure mapping: element key to action, tmux, kitty and tty key names, DevTools key events, tab selection, port setting, stat and fdinfo parsing.
  • 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.
  • CI's cargo test job installs xvfb, at-spi2-core, gir1.2-gtk-3.0, tmux and screen; Chrome is on the runner.

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_pid uses 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_window returned the holder before and after every call. Results: the entry read hello world, the button clicked, the check box toggled, kitty's cat got hello kitty through kitty remote control, the tmux pane got hello tmux through the client's tty, the tab input read abcXYZ and Enter submitted the form. The holder got no input. All windows were closed afterwards.

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
@avifenesh

Copy link
Copy Markdown
Collaborator Author

Independent review

Reviewed head 092d222. Findings, most serious first.

1. GNU screen route can type into the wrong session or window (src/background_input.rs:1004-1015)

screen_channel takes STY/WINDOW from the environment of any process on the tty and uses them with no check that the screen window really is on that tty. Both variables are inherited. For example, a terminal emulator started from inside a screen window passes STY=<session> and WINDOW=<n> to the shell in its new tty. Background input to that tty then goes to window n of the outer screen session, which is another terminal. The tmux, kitty and wezterm routes all check the tty against a listing; screen does not.
Fix: before choosing the channel, check that the process that carries STY is a descendant of that session's SCREEN server pid (the pid prefix of STY). Or check that WINDOW maps to this tty (for example with screen -S <sty> -p <w> -Q tty on screen 4.x/5). If the check fails, refuse.

2. screen paste can send stale buffer contents, and it leaves the text in screen's paste buffer (src/background_input.rs:1209-1247)

readbuf and paste . are sent as two -X commands, which the server runs asynchronously. The file is then removed after a fixed 300 ms sleep. If the server reads the file late, readbuf fails, paste . pastes whatever the buffer held before (possibly an earlier secret), and the call still reports success. When it does work, the text (passwords included) stays in screen's paste buffer, where C-a ] can paste it again.
Fix: run readbuf and paste in one -X eval and clear the register afterwards (register . "" / removebuf). Do not report success unless the readbuf step is known to have succeeded.

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 tiocsti() returns EPERM when current->signal->tty != tty and the caller lacks CAP_SYS_ADMIN. The server opens the tty with O_NOCTTY and is never on the target's controlling tty. So on every non-root setup with legacy_tiocsti=1, the probe picks Tiocsti, the send fails with EPERM, and the PtyMaster channel that might work is never tried. available_routes also lists tiocsti in doctor when it cannot work.
Fix: offer Tiocsti only when the process has CAP_SYS_ADMIN (check CapEff in /proc/self/status). Otherwise fall through to the pty-master probe. Apply the same condition in available_routes.

4. tty and pty-master matching ignores devpts instances (src/background_input.rs:638-685, 320-326)

processes_on_tty matches by (major, minor) and pty_master_holder by tty-index only. Each devpts instance (a container with its own /dev/pts, for example rootless Docker or podman under the same uid) numbers its pts from 0 with the same major. pty_master_holder returns the first pid in /proc order, which can be a process inside a container that holds pts/N of its own instance. The PtyMaster route would then pidfd_getfd that master and write the input into a different terminal. processes_on_tty can likewise collect another instance's processes and their TMUX/STY/KITTY_LISTEN_ON, which makes finding 1 worse. process_tty (server.rs:10241) returns a path string that is resolved in our mount namespace, not the target's.
Fix: skip pids whose /proc/<pid>/ns/mnt (or the st_dev of the devpts behind their fds) differs from the server's own. For the master, compare stat(/proc/<pid>/fd/<n>) st_dev with the st_dev of the target pts's devpts mount.

5. The DevTools route trusts whatever listens on the configured loopback port (src/devtools.rs:232, server.rs:10463)

COMPUTER_USE_LINUX_DEVTOOLS=9222 is a bare port. Once the browser exits, any local user can bind 127.0.0.1:9222 and serve a fake /json/list and WebSocket. Typed text, including password fields that the tool treats as targets, then goes to that process. On a single-user machine the risk is low, but the tool claims the input reaches "a browser the user opened".
Fix: before connecting, check in /proc/net/tcp (the uid column of the LISTEN entry) that the listening socket belongs to the server's uid. Or require the DevToolsActivePort file form and check that the file is owned by the user.

6. A background password insert can overwrite the whole field when AT-SPI Text is missing (src/atspi_tree.rs:1332-1342)

insert_password_text treats a missing Text interface as (count, caret) = (0, 0) and then calls SetTextContents. That replaces whatever the field held, yet the result says the text was "Inserted ... at the caret". It is unverifiable by design, so the user cannot see the overwrite.
Fix: when Text is unavailable, either refuse, or say that the whole field was replaced (use the same wording as set_value).

7. terminal_pid resolves through stdin, not the controlling tty (src/background_input.rs:320, server.rs:10236-10244)

terminal_pid is documented as the shell or foreground process on a terminal's tty, but process_tty reads /proc/<pid>/fd/0. A process whose stdin is a different pts (redirected, or inherited from wherever it was launched) routes the input to that other terminal, and the window-based resolution is never consulted. The module already parses the controlling tty (stat_tty).
Fix: resolve by stat_tty of /proc/<pid>/stat, and require stdin to agree with it, or ignore stdin.

Checked, no issue found

  • No fallback to focused input: every background_route error and probe refusal returns before any input is sent, and TargetRaised::No is set.
  • Text never goes into process arguments: tmux load-buffer, kitty and wezterm use stdin, and screen uses a 0600 file.
  • The tmux pane is confirmed through pane_tty or client_tty, kitty through ls pids, and wezterm through tty_name.
  • Input lock: the element key path takes the lock inside perform_element_action, which also checks the snapshot again; the other paths take it before sending.
  • WebSocket framing (masking, 126/127 lengths, ping/pong, continuation frames) and the HTTP Content-Length and chunked handling look correct for Chrome's endpoint. The handshake does not check Sec-WebSocket-Accept, which is minor on loopback.
  • The "nothing was sent" wording matches the code paths: the TIOCSTI byte index, a partial DevTools chord, and AT-SPI may_have_changed.

@revuto-review revuto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. TIOCSTI is chosen from the sysctl alone. On a kernel with dev.tty.legacy_tiocsti = 1 it 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.
  2. 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.
  3. Explicit delivery: "background" with a window target is refused on Hyprland even with COMPUTER_USE_LINUX_HYPRLAND_BACKGROUND=1, where the same call without delivery is 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.

Comment thread src/background_input.rs Outdated
}

match legacy_tiocsti_enabled() {
Some(true) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_getfd pty-master route below is never tried, even where ptrace_scope would allow it.
  • The caller gets a raw TIOCSTI on /dev/pts/N: the kernel refused… error instead of probe.refusal() with the emulator name, the notes and the GNOME hint.
  • available_routes (line 298) reports tiocsti to doctor as 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.

Comment thread src/background_input.rs Outdated
/// 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()) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/server.rs
.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.{}",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@revuto-review revuto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/background_input.rs Outdated
if screen_session_pid(&session) == Some(*holder_pid) {
return Some(TerminalChannel::Screen {
session,
window: environ_value(&process.environ, "WINDOW"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/server.rs
.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.{}",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/background_input.rs
// 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} ''");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

❌ 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.

Comment thread src/devtools.rs
}
if (e.isContentEditable) return {kind: 'editable', tag};
return {kind: 'other', tag, role: e.getAttribute('role') || ''};
})()"#;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c1c4f46. Configure here.

Comment thread src/background_input.rs
let mut file = fs::File::from(fd.try_clone()?);
file.write_all(bytes)
.context("writing to the pty master failed")
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

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

@revuto-review revuto-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reaches send_key_state.

Still open or new

  • New: write_master sets O_NONBLOCK on 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 without delivery is 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.

Comment thread src/background_input.rs Outdated
// 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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/server.rs Outdated
.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))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/background_input.rs Outdated
let server = process_args(pid)
.first()
.is_some_and(|arg0| arg0 == "SCREEN");
(server && clients.contains(&ppid)).then(|| TerminalChannel::Screen {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@avifenesh

Copy link
Copy Markdown
Collaborator Author

Review findings, where each one landed:

  • TIOCSTI picked on the sysctl alone: needs CAP_SYS_ADMIN or the server's own controlling tty, else falls through (cc54b54, c1c4f46).
  • screen trusting inherited STY/WINDOW, renumbered windows: a screen window's own tty is refused; only a client's tty is used (cc54b54).
  • screen register: pastes through the ^_ register, read and cleared in one eval with a -Q round trip before the file goes (cc54b54).
  • screen display ambiguity: the session is used only when no other client may be attached to it (c38c742).
  • O_NONBLOCK on the shared pty master description: removed; each byte waits on poll(POLLOUT), bounded at 2 s, on a blocking thread (c38c742). A unit test on an openpty pair checks delivery, that the description stays blocking, and the deadline.
  • Hyprland with COMPUTER_USE_LINUX_HYPRLAND_BACKGROUND=1: non-terminal windows and terminal windows no tty channel reaches go through send_key_state; doctor lists hyprland (cc54b54, c38c742).
  • Bugbot: pty master write bounded (c38c742); designMode documents and contenteditable bodies, also in same-origin iframes, are editors, covered in the DevTools fixture (cc54b54).
  • Container devpts: processes are matched on the same devpts instance, and a tty from a window must have its pty master held by that window's terminal in this mount namespace (c1c4f46, cc54b54).

revuto stopped at its 3-round limit; the independent review above and this round are the review of record. CI is green on c38c742.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant