Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(cli): preserve screenshot output on failure

cyh37efd53f97fb
diff --git a/tests/test_cli_screenshot_command.py b/tests/test_cli_screenshot_command.py
index 7f63a4d7c..63a31aba6 100644
--- a/tests/test_cli_screenshot_command.py
+++ b/tests/test_cli_screenshot_command.py
@@ -4,6 +4,7 @@ from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+import vibe.screenshot as screenshot
from vibe import cli
from vibe.screenshot import ScreenshotError, ScreenshotResult, capture_screenshot
@@ -77,3 +78,52 @@ def test_capture_screenshot_wraps_output_preparation_errors(tmp_path: Path) -> N
assert "failed to prepare screenshot output path" in str(exc)
else:
raise AssertionError("expected ScreenshotError")
+
+
+def test_capture_screenshot_preserves_existing_output_when_linux_backend_fails(
+ tmp_path: Path, monkeypatch
+) -> None:
+ output = tmp_path / "screen.png"
+ output.write_bytes(b"existing")
+
+ monkeypatch.setattr(screenshot.sys, "platform", "linux")
+ monkeypatch.setattr(screenshot, "which", lambda name: f"/usr/bin/{name}" if name == "grim" else None)
+
+ def fail_capture(command, label):
+ Path(command[-1]).write_bytes(b"partial")
+ raise ScreenshotError("backend failed")
+
+ monkeypatch.setattr(screenshot, "_run_capture_command", fail_capture)
+
+ try:
+ capture_screenshot(output)
+ except ScreenshotError:
+ pass
+ else:
+ raise AssertionError("expected ScreenshotError")
+
+ assert output.read_bytes() == b"existing"
+
+
+def test_capture_screenshot_replaces_existing_output_only_after_success(tmp_path: Path, monkeypatch) -> None:
+ output = tmp_path / "screen.png"
+ output.write_bytes(b"existing")
+ capture_paths: list[Path] = []
+
+ monkeypatch.setattr(screenshot.sys, "platform", "linux")
+ monkeypatch.setattr(screenshot, "which", lambda name: f"/usr/bin/{name}" if name == "grim" else None)
+
+ def successful_capture(command, label):
+ capture_path = Path(command[-1])
+ capture_paths.append(capture_path)
+ capture_path.write_bytes(b"new screenshot")
+
+ monkeypatch.setattr(screenshot, "_run_capture_command", successful_capture)
+
+ result = capture_screenshot(output)
+
+ assert result.path == output
+ assert output.read_bytes() == b"new screenshot"
+ assert capture_paths
+ assert output not in capture_paths
+ assert all(not path.exists() for path in capture_paths)
diff --git a/vibe/screenshot.py b/vibe/screenshot.py
index 4e244f142..9892b7a8d 100644
--- a/vibe/screenshot.py
+++ b/vibe/screenshot.py
@@ -4,6 +4,8 @@ from __future__ import annotations
import subprocess
import sys
+import tempfile
+import os
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
@@ -42,19 +44,30 @@ def capture_screenshot(output: str | Path | None = None) -> ScreenshotResult:
try:
output_path = _resolve_output_path(output)
output_path.parent.mkdir(parents=True, exist_ok=True)
+ capture_path = _temporary_output_path(output_path)
except OSError as exc:
raise ScreenshotError(f"failed to prepare screenshot output path: {exc}") from exc
- if sys.platform == "darwin":
- backend = _capture_macos(output_path)
- elif sys.platform.startswith("win"):
- backend = _capture_windows(output_path)
- elif sys.platform.startswith("linux"):
- backend = _capture_linux(output_path)
- else:
- raise ScreenshotError(f"screenshots are not supported on this platform: {sys.platform}")
-
- _verify_output(output_path)
+ try:
+ if sys.platform == "darwin":
+ backend = _capture_macos(capture_path)
+ elif sys.platform.startswith("win"):
+ backend = _capture_windows(capture_path)
+ elif sys.platform.startswith("linux"):
+ backend = _capture_linux(capture_path)
+ else:
+ raise ScreenshotError(f"screenshots are not supported on this platform: {sys.platform}")
+
+ _verify_output(capture_path)
+ try:
+ capture_path.replace(output_path)
+ except OSError as exc:
+ raise ScreenshotError(f"failed to finalize screenshot output: {exc}") from exc
+ finally:
+ try:
+ capture_path.unlink(missing_ok=True)
+ except OSError:
+ pass
return ScreenshotResult(path=output_path, backend=backend)
@@ -67,6 +80,20 @@ def _resolve_output_path(output: str | Path | None) -> Path:
return path.resolve()
+def _temporary_output_path(output: Path) -> Path:
+ fd, raw_path = tempfile.mkstemp(
+ prefix=f".{output.name}.",
+ suffix=".tmp.png",
+ dir=output.parent,
+ )
+ try:
+ os.close(fd)
+ except OSError:
+ pass
+ Path(raw_path).unlink()
+ return Path(raw_path)
+
+
def _capture_macos(output: Path) -> str:
if which("screencapture") is None:
raise ScreenshotError("macOS screencapture command was not found")
@@ -116,11 +143,7 @@ def _capture_linux(output: Path) -> str:
_run_capture_command(command, name)
return name
except ScreenshotError:
- try:
- if output.exists():
- output.unlink()
- except OSError:
- pass
+ pass
if attempted:
raise ScreenshotError(f"installed screenshot tools failed: {', '.join(attempted)}")