Skip to content

The agent that grows with you

fix(clipboard): give image probes a valid stdin

Mohamad Kansod264b2bbc9c8
diff --git a/hermes_cli/clipboard.py b/hermes_cli/clipboard.py
index b79ee7b06fb..131b4355898 100644
--- a/hermes_cli/clipboard.py
+++ b/hermes_cli/clipboard.py
@@ -17,7 +17,7 @@ from hermes_constants import is_wsl as _is_wsl
logger = logging.getLogger(__name__)
_PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
-_TEXT = dict(capture_output=True, text=True, encoding='utf-8', errors='replace')
+_TEXT = dict(capture_output=True, text=True, encoding='utf-8', errors='replace', stdin=subprocess.DEVNULL)
_PS_FLAGS = ("-NoProfile", "-NonInteractive")
_FILE_IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
@@ -42,7 +42,7 @@ def _probe(argv: list, timeout: int, ok, *, missing: str | None = None) -> bool:
def _pipe_to_file(argv: list, dest: Path) -> bool:
"""Run *argv* with stdout redirected into *dest*; True when a non-empty file resulted."""
with open(dest, "wb") as f:
- subprocess.run(argv, stdout=f, stderr=subprocess.DEVNULL, timeout=5, check=True)
+ subprocess.run(argv, stdin=subprocess.DEVNULL, stdout=f, stderr=subprocess.DEVNULL, timeout=5, check=True)
return _nonempty(dest)
@@ -163,7 +163,7 @@ def _macos_save_file_image(dest: Path) -> bool:
def _macos_pngpaste(dest: Path) -> bool:
"""pngpaste (brew install pngpaste) — fastest, cleanest."""
try:
- r = subprocess.run(["pngpaste", str(dest)], capture_output=True, timeout=3)
+ r = subprocess.run(["pngpaste", str(dest)], stdin=subprocess.DEVNULL, capture_output=True, timeout=3)
return r.returncode == 0 and _nonempty(dest)
except FileNotFoundError:
pass # pngpaste not installed
@@ -360,7 +360,7 @@ def _convert_to_png(path: Path) -> bool:
tmp = path.with_suffix(".bmp")
try:
path.rename(tmp)
- r = subprocess.run(["convert", str(tmp), "png:" + str(path)], capture_output=True,
+ r = subprocess.run(["convert", str(tmp), "png:" + str(path)], stdin=subprocess.DEVNULL, capture_output=True,
timeout=5)
if r.returncode == 0 and _nonempty(path):
tmp.unlink(missing_ok=True)
diff --git a/tests/tools/test_clipboard.py b/tests/tools/test_clipboard.py
index 0ea0c6aefac..0f503400377 100644
--- a/tests/tools/test_clipboard.py
+++ b/tests/tools/test_clipboard.py
@@ -34,6 +34,8 @@ from hermes_cli.clipboard import (
_windows_save,
_windows_has_image,
_convert_to_png,
+ _pipe_to_file,
+ _probe,
)
from cli import _should_auto_attach_clipboard_image_on_paste
@@ -56,6 +58,26 @@ class TestSaveClipboardImage:
assert dest.parent.exists()
+class TestClipboardChildStdin:
+ def test_probe_uses_devnull_stdin(self):
+ with patch("hermes_cli.clipboard.subprocess.run") as mock_run:
+ mock_run.return_value = MagicMock(returncode=0)
+ assert _probe(["clipboard-tool"], 3, lambda result: result.returncode == 0)
+ assert mock_run.call_args.kwargs["stdin"] == subprocess.DEVNULL
+
+ def test_pipe_to_file_uses_devnull_stdin(self, tmp_path):
+ dest = tmp_path / "out.png"
+
+ def fake_run(_argv, **kwargs):
+ assert kwargs["stdin"] == subprocess.DEVNULL
+ kwargs["stdout"].write(FAKE_PNG)
+ return MagicMock(returncode=0)
+
+ with patch("hermes_cli.clipboard.subprocess.run", side_effect=fake_run):
+ assert _pipe_to_file(["clipboard-tool"], dest) is True
+ assert dest.read_bytes() == FAKE_PNG
+
+
# ── macOS ────────────────────────────────────────────────────────────────
class TestMacosPngpaste:
@@ -242,7 +264,10 @@ class TestWaylandHasImage:
class TestWaylandSave:
def test_png_extraction(self, tmp_path):
dest = tmp_path / "out.png"
+ calls = []
+
def fake_run(cmd, **kw):
+ calls.append(kw)
if "--list-types" in cmd:
return MagicMock(stdout="image/png\ntext/plain\n", returncode=0)
# Extract call — write fake data to stdout file
@@ -252,6 +277,7 @@ class TestWaylandSave:
with patch("hermes_cli.clipboard.subprocess.run", side_effect=fake_run):
assert _wayland_save(dest) is True
assert dest.stat().st_size > 0
+ assert all(kwargs["stdin"] == subprocess.DEVNULL for kwargs in calls)
def test_prefers_png_over_bmp(self, tmp_path):