Skip to content

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.

fix(clipboard): use the X11 tool on X11 even when wl-clipboard is installed

rubenluetke10-beepe896a092c634
diff --git a/jarvis/platform/clipboard.py b/jarvis/platform/clipboard.py
index 7bcb7d4c3b..8d6c610620 100644
--- a/jarvis/platform/clipboard.py
+++ b/jarvis/platform/clipboard.py
@@ -15,6 +15,7 @@ from __future__ import annotations
import ctypes
import logging
+import os
import shutil
import subprocess
import tempfile
@@ -107,13 +108,27 @@ def _read_command(command: Sequence[str]) -> str | None:
return completed.stdout
+def _session_order(
+ candidates: tuple[tuple[str, list[str]], ...],
+) -> tuple[tuple[str, list[str]], ...]:
+ """The Wayland tool first only in a Wayland session; last otherwise.
+
+ ``wl-copy``/``wl-paste`` are often installed on X11 desktops too, and
+ there they fail ("Failed to connect to a Wayland server"), so trying them
+ first made every copy and paste on such a desktop fail.
+ """
+ if os.environ.get("WAYLAND_DISPLAY"):
+ return candidates
+ return candidates[1:] + candidates[:1]
+
+
def _read_linux() -> str | None:
- """Use the available Wayland/X11 clipboard command, if any."""
- candidates = (
+ """Use the clipboard command for this session (Wayland or X11), if any."""
+ candidates = _session_order((
("wl-paste", ["wl-paste", "--no-newline"]),
("xclip", ["xclip", "-selection", "clipboard", "-out"]),
("xsel", ["xsel", "--clipboard", "--output"]),
- )
+ ))
for executable, command in candidates:
resolved = shutil.which(executable)
if resolved:
@@ -229,12 +244,12 @@ def _run_command(command: Sequence[str], text: str) -> bool:
def _write_linux(text: str) -> bool:
- """Use the available Wayland/X11 clipboard command, if any."""
- candidates = (
+ """Use the clipboard command for this session (Wayland or X11), if any."""
+ candidates = _session_order((
("wl-copy", ["wl-copy", "--type", "text/plain;charset=utf-8"]),
("xclip", ["xclip", "-selection", "clipboard", "-in"]),
("xsel", ["xsel", "--clipboard", "--input"]),
- )
+ ))
for executable, command in candidates:
resolved = shutil.which(executable)
if resolved:
diff --git a/tests/unit/platform/test_clipboard.py b/tests/unit/platform/test_clipboard.py
index 7dcd07c29a..ab53e2bb61 100644
--- a/tests/unit/platform/test_clipboard.py
+++ b/tests/unit/platform/test_clipboard.py
@@ -200,3 +200,30 @@ def test_empty_clipboard_reads_as_empty_string_not_unavailable(
monkeypatch.setattr(clipboard.subprocess, "run", _run)
assert clipboard._read_command(["pbpaste"]) == ""
+
+
+ ("wayland", "expected_write", "expected_read"),
+ [
+ ("wayland-0", "wl-copy", "wl-paste"),
+ # An X11 desktop with wl-clipboard installed must not try Wayland first.
+ ("", "xclip", "xclip"),
+ ],
+)
+def test_linux_picks_the_tool_for_the_session(
+ monkeypatch: pytest.MonkeyPatch, wayland: str, expected_write: str, expected_read: str
+) -> None:
+ monkeypatch.setenv("WAYLAND_DISPLAY", wayland)
+ monkeypatch.setattr(clipboard.shutil, "which", lambda name: f"/usr/bin/{name}")
+ written: list[str] = []
+ read: list[str] = []
+ monkeypatch.setattr(
+ clipboard, "_run_command", lambda command, _text: written.append(command[0]) or True
+ )
+ monkeypatch.setattr(clipboard, "_read_command", lambda command: read.append(command[0]) or "")
+
+ clipboard._write_linux("hello")
+ clipboard._read_linux()
+
+ assert written == [f"/usr/bin/{expected_write}"]
+ assert read == [f"/usr/bin/{expected_read}"]