Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Replace per-page local recordings with one 720p display recording (SKY-13472) (#8367)
LawyZheng3f262811705a
diff --git a/skyvern/config.py b/skyvern/config.pyindex ce401c779..302f16de9 100644--- a/skyvern/config.py+++ b/skyvern/config.py@@ -92,6 +92,8 @@ class Settings(BaseSettings):ADDITIONAL_MODULES: list[str] = []BROWSER_TYPE: str = "chromium-headful"+ # Whole-display recording is opt-in and intended for packaged Linux workers only.+ EXCLUSIVE_DISPLAY_RECORDING: bool = FalseBROWSER_REMOTE_DEBUGGING_URL: str = "http://127.0.0.1:9222"BROWSER_REMOTE_DEBUGGING_HOST_HEADER: str | None = NoneBROWSER_REMOTE_DEBUGGING_CONNECT_HEADERS: str | None = Nonediff --git a/skyvern/webeye/browser_artifacts.py b/skyvern/webeye/browser_artifacts.pyindex 4914f2f4e..be8c65639 100644--- a/skyvern/webeye/browser_artifacts.py+++ b/skyvern/webeye/browser_artifacts.py@@ -76,6 +76,11 @@ class BrowserArtifacts(BaseModel):# None when none was requested or the chosen creator could not load one (remote/vendor browsers,# storage miss, corruption fallback) — consumers must treat None as "profile not applied".applied_browser_profile_id: str | None = None+ local_display_recording_eligible: bool = False+ # ((input_w, input_h), (output_w, output_h)) for the whole-display recorder, resolved from the final+ # launch --window-size at the configure seam and carried to the acquire seam. None → fixed full screen.+ _display_capture_sizes: tuple[tuple[int, int], tuple[int, int]] | None = PrivateAttr(default=None)+ _display_recorder: object | None = PrivateAttr(default=None)_browser_console_log_lock: asyncio.Lock = PrivateAttr(default_factory=asyncio.Lock)# Tombstoned synchronously before any await, so set_popup_video_listener can't# re-register a page's video after RealBrowserState decides to discard it.diff --git a/skyvern/webeye/browser_factory.py b/skyvern/webeye/browser_factory.pyindex 5c351db98..0cd107467 100644--- a/skyvern/webeye/browser_factory.py+++ b/skyvern/webeye/browser_factory.py@@ -70,6 +70,13 @@ from skyvern.webeye.cdp_connection import ()from skyvern.webeye.cdp_download_interceptor import CDPDownloadInterceptor, bind_download_interceptor_to_contextfrom skyvern.webeye.dialog_handler import set_dialog_handler+from skyvern.webeye.display_recorder import (+ acquire_display_recorder,+ configure_local_display_recording,+ release_display_recorder,+ resolve_display_capture_sizes,+ resolve_owner_id,+)from skyvern.webeye.playwright_input import register_playwright_input_contextfrom skyvern.webeye.session_cookies import restore_banked_cookies, restore_session_cookies@@ -753,6 +760,7 @@ class BrowserContextFactory:browser_type = settings.BROWSER_TYPEbrowser_context: BrowserContext | None = Nonecleanup_func: BrowserCleanupFunc = None+ started_display_recorder = Nonebrowser_internal_headers, scoped_headers = _partition_browser_headers(kwargs.get("extra_http_headers"))creator_kwargs = {**kwargs, "extra_http_headers": browser_internal_headers}try:@@ -789,7 +797,7 @@ class BrowserContextFactory:# producer of video_artifacts it took the main page's recording with it, not just popups.# The thing that genuinely cannot record is the attach-only worker, which is what# is_enforcing() names.- if not attach_only_enforcing():+ if not attach_only_enforcing() and not browser_artifacts.local_display_recording_eligible:set_popup_video_listener(browser_context=browser_context, browser_artifacts=browser_artifacts)set_download_file_listener(browser_context=browser_context, **kwargs)set_dialog_handler(browser_context=browser_context)@@ -820,8 +828,31 @@ class BrowserContextFactory:context = ensure_context()context.tz_info = get_tzinfo_from_proxy(proxy_location)+ if browser_artifacts.local_display_recording_eligible:+ owner_id_override = cast(str | None, kwargs.get("display_recording_owner_id"))+ owner_id = resolve_owner_id(+ owner_id_override=owner_id_override,+ workflow_run_id=cast(str | None, kwargs.get("workflow_run_id")),+ task_id=cast(str | None, kwargs.get("task_id")),+ script_id=cast(str | None, kwargs.get("script_id")),+ )+ display = os.environ.get("DISPLAY")+ if owner_id and display and settings.VIDEO_PATH:+ video_dir = Path(settings.VIDEO_PATH) / datetime.utcnow().strftime("%Y-%m-%d")+ acquisition = await acquire_display_recorder(+ display, owner_id, video_dir, owner_id_override, browser_artifacts._display_capture_sizes+ )+ if acquisition.recorder is not None and acquisition.video_artifact is not None:+ browser_artifacts._display_recorder = acquisition.recorder+ browser_artifacts.video_artifacts = [acquisition.video_artifact]+ if acquisition.started:+ started_display_recorder = acquisition.recorder+return browser_context, browser_artifacts, cleanup_funcexcept BaseException as e:+ if started_display_recorder is not None:+ with suppress(Exception):+ await release_display_recorder(started_display_recorder)if browser_context is not None:# FIXME: sometimes it can't close the browser context?LOG.error("unexpected error happens after created browser context, going to close the context")@@ -1000,7 +1031,6 @@ async def _create_headless_chromium("downloads_path": download_dir,})-browser_artifacts = BrowserContextFactory.build_browser_artifacts(har_path=browser_args["record_har_path"],browser_session_dir=user_data_dir,@@ -1098,12 +1128,24 @@ async def _create_headful_chromium("headless": False,})+ local_display_recording_eligible = configure_local_display_recording(+ browser_args,+ owner_id_override=cast(str | None, kwargs.get("display_recording_owner_id")),+ workflow_run_id=cast(str | None, kwargs.get("workflow_run_id")),+ task_id=cast(str | None, kwargs.get("task_id")),+ script_id=cast(str | None, kwargs.get("script_id")),+ )+ # Derive the capture rectangle from the final launch args (post viewport override); reused unchanged by+ # the corruption-fallback rebuild below since that changes only the user-data-dir, not --window-size.+ capture_sizes = resolve_display_capture_sizes(browser_args) if local_display_recording_eligible else Nonebrowser_artifacts = BrowserContextFactory.build_browser_artifacts(har_path=browser_args["record_har_path"],browser_session_dir=user_data_dir,)if loaded_from_saved_profile:browser_artifacts.applied_browser_profile_id = browser_profile_id+ browser_artifacts.local_display_recording_eligible = local_display_recording_eligible+ browser_artifacts._display_capture_sizes = capture_sizestry:browser_context = await playwright.chromium.launch_persistent_context(**browser_args)except Exception as launch_error:@@ -1125,6 +1167,8 @@ async def _create_headful_chromium(browser_session_dir=fallback_dir,)browser_artifacts.mark_seed_load_failed()+ browser_artifacts.local_display_recording_eligible = local_display_recording_eligible+ browser_artifacts._display_capture_sizes = capture_sizesbrowser_context = await playwright.chromium.launch_persistent_context(**browser_args)else:raisediff --git a/skyvern/webeye/display_recorder.py b/skyvern/webeye/display_recorder.pynew file mode 100644index 000000000..c7aaf8b66--- /dev/null+++ b/skyvern/webeye/display_recorder.py@@ -0,0 +1,521 @@+from __future__ import annotations++import asyncio+import hashlib+import os+import platform+import re+import signal+import sys+from contextlib import suppress+from dataclasses import dataclass+from pathlib import Path++import structlog++from skyvern.config import settings+from skyvern.webeye import attach_only+from skyvern.webeye.browser_artifacts import VideoArtifact++try:+ import fcntl+except ImportError:+ # fcntl is POSIX-only. This module is part of the OSS `skyvern/` import surface, which must import+ # cleanly on non-POSIX platforms; whole-display recording only ever runs on the Linux worker family,+ # so acquisition fails closed when fcntl is unavailable.+ fcntl = None # type: ignore[assignment]++LOG = structlog.get_logger()++SIGINT_TIMEOUT = 5.0+SIGTERM_TIMEOUT = 2.0+STARTUP_READINESS_TIMEOUT = 0.5++# x11grab captures the browser window rectangle at the display origin (--window-position=0,0). The Xvfb+# screen and the base --window-size are both 1920x1080, so an absent/unparseable/larger-than-screen window+# falls back to capturing the full screen. The output is bounded within 1280x720, aspect-preserved.+CAPTURE_DISPLAY_SIZE = (1920, 1080)+CAPTURE_OUTPUT_BOUND = (1280, 720)+CaptureSizes = tuple[tuple[int, int], tuple[int, int]]++_REGISTRY: dict[tuple[str, str], DisplayRecorder] = {}++# Parent-death via an exec'd python trampoline, NOT a fork-time preexec_fn (prctl/ctypes in the multithreaded+# worker's fork child can inherit locks it never owns and hang before exec). The parent passes its pid (argv[1]);+# the trampoline exits unless the live parent still matches it before AND after prctl (so a parent that dies+# pre-exec never binds PDEATHSIG to the reaper), then execvp's ffmpeg (pid kept; PDEATHSIG survives non-setuid exec).+_PDEATHSIG_TRAMPOLINE = (+ "import ctypes,os,signal,sys\n"+ "e=int(sys.argv[1])\n"+ "os.getppid()==e or os._exit(0)\n"+ "try:ctypes.CDLL(None,use_errno=True).prctl(1,signal.SIGTERM)\n" # 1 == PR_SET_PDEATHSIG+ "except Exception:pass\n"+ "os.getppid()==e or os._exit(0)\n"+ "os.execvp(sys.argv[2],sys.argv[2:])\n"+)+++def normalize_display(display: str) -> str:+ # Strip only the ".<screen>" that trails the ":<display>" segment; never split on a dot inside a+ # hostname (e.g. "host.example.com:0.0" -> "host.example.com:0", ":99.0" -> ":99").+ display = display.strip()+ head, sep, tail = display.rpartition(":")+ if not sep:+ return display+ return f"{head}:{tail.split('.', 1)[0]}"+++def resolve_owner_id(+ *,+ owner_id_override: str | None = None,+ workflow_run_id: str | None = None,+ task_id: str | None = None,+ script_id: str | None = None,+ browser_session_id: str | None = None,+) -> str | None:+ # owner_id_override is FALLBACK-ONLY (used only when no canonical owner is present, e.g. a standalone+ # reconnect), so a stray/hostile override can never displace an explicit canonical owner and weaken isolation.+ return workflow_run_id or task_id or script_id or browser_session_id or owner_id_override+++def configure_local_display_recording(+ browser_args: dict[str, object],+ *,+ owner_id_override: str | None = None,+ workflow_run_id: str | None = None,+ task_id: str | None = None,+ script_id: str | None = None,+ browser_session_id: str | None = None,+) -> bool:+ eligible = bool(+ platform.system() == "Linux"+ and settings.EXCLUSIVE_DISPLAY_RECORDING+ and os.environ.get("DISPLAY")+ and not attach_only.is_enforcing()+ and resolve_owner_id(+ owner_id_override=owner_id_override,+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ script_id=script_id,+ browser_session_id=browser_session_id,+ )+ )+ if eligible:+ browser_args.pop("record_video_dir", None)+ browser_args.pop("record_video_size", None)+ return eligible+++def unlink_recordings_for_owner(owner_id: str) -> int:+ """Best-effort, synchronous, never-raising removal of this owner's whole-display recordings.++ Recordings are written to ``VIDEO_PATH/<YYYY-MM-DD>/<_safe_owner_id(owner_id)>.webm``; the owner+ digest makes the filename derivable from the run's own identity, so a glob across date dirs is+ owner-exact — a sibling owner's digest and Playwright random-name videos never match — and survives+ a midnight date-dir rollover. Returns the number of files removed.+ """+ root = settings.VIDEO_PATH+ if not root:+ return 0+ removed = 0+ try:+ for path in Path(root).glob(f"*/{_safe_owner_id(owner_id)}.webm"):+ try:+ path.unlink(missing_ok=True)+ removed += 1+ except OSError:+ LOG.warning("Failed to unlink whole-display recording", path=str(path), exc_info=True)+ except OSError:+ LOG.warning("Failed to scan for whole-display recordings to unlink", owner_id=owner_id, exc_info=True)+ return removed+++def _safe_owner_id(owner_id: str) -> str:+ sanitized = re.sub(r"[^A-Za-z0-9._-]", "_", owner_id).strip("._")+ prefix = (sanitized or "run")[:48]+ digest = hashlib.sha256(owner_id.encode("utf-8")).hexdigest()+ return f"{prefix}-{digest}"+++def _parse_window_size(browser_args: dict[str, object]) -> tuple[int, int] | None:+ args = browser_args.get("args")+ if not isinstance(args, list):+ return None+ resolved: str | None = None+ for arg in args:+ text = str(arg)+ if text.startswith("--window-size="):+ resolved = text # the launch keeps one entry; take the last so a rewrite always wins+ if resolved is None:+ return None+ try:+ width_str, height_str = resolved.split("=", 1)[1].split(",", 1)+ return int(width_str), int(height_str)+ except (ValueError, IndexError):+ return None+++def _even_floor(value: float) -> int:+ floored = int(value)+ return floored - (floored % 2)+++def _bounded_even_output(width: int, height: int) -> tuple[int, int]:+ # Fit within CAPTURE_OUTPUT_BOUND, aspect-preserved and never upscaled. The binding dimension is chosen+ # by integer cross-multiplication (so it lands exactly on the bound, no float drift), the other derived+ # and floored to even — libvpx needs even dimensions and flooring never pads, crops, or distorts.+ max_w, max_h = CAPTURE_OUTPUT_BOUND+ if width * max_h >= height * max_w:+ out_w: float = min(width, max_w)+ out_h: float = out_w * height / width+ else:+ out_h = min(height, max_h)+ out_w = out_h * width / height+ return _even_floor(out_w), _even_floor(out_h)+++def resolve_display_capture_sizes(browser_args: dict[str, object]) -> CaptureSizes:+ """Derive the immutable ((input_w, input_h), (output_w, output_h)) for x11grab from the FINAL, validated+ ``--window-size`` in ``browser_args`` (set at the launch seam, after any dynamic viewport override).++ The input is the browser window captured at the display origin; the output is that rectangle scaled to+ fit within 1280x720 with even dimensions and no padding, crop, or distortion. A missing, unparseable, or+ larger-than-display window falls back to the fixed 1920x1080 capture + 1280x720 output of the base launch.+ """+ window = _parse_window_size(browser_args)+ if window is None:+ return CAPTURE_DISPLAY_SIZE, CAPTURE_OUTPUT_BOUND+ width, height = window+ if not (0 < width <= CAPTURE_DISPLAY_SIZE[0] and 0 < height <= CAPTURE_DISPLAY_SIZE[1]):+ return CAPTURE_DISPLAY_SIZE, CAPTURE_OUTPUT_BOUND+ return (width, height), _bounded_even_output(width, height)+++def build_ffmpeg_command(+ display: str, owner_id: str, video_dir: Path, capture_sizes: CaptureSizes | None = None+) -> tuple[list[str], Path]:+ output_path = video_dir / f"{_safe_owner_id(owner_id)}.webm"+ (input_w, input_h), (output_w, output_h) = capture_sizes or (CAPTURE_DISPLAY_SIZE, CAPTURE_OUTPUT_BOUND)+ command = [+ "ffmpeg",+ "-y",+ "-f",+ "x11grab",+ "-video_size",+ f"{input_w}x{input_h}",+ "-framerate",+ "15",+ "-i",+ normalize_display(display),+ "-vf",+ f"scale={output_w}:{output_h}",+ "-c:v",+ "libvpx",+ "-b:v",+ "1M",+ "-qmin",+ "0",+ "-qmax",+ "50",+ "-an",+ "-deadline",+ "realtime",+ str(output_path),+ ]+ return command, output_path+++async def _reap_surviving_cancellation(process: asyncio.subprocess.Process) -> bool:+ """Await the child's reaping to completion even under repeated cancellation.++ ``asyncio.shield`` protects the wait from a single cancel, but a second cancel delivered while the+ shielded await is pending would still abort it and skip lock release. We loop on the SAME wait future,+ absorbing every cancel, and report whether any arrived so the caller re-raises exactly once — after it+ has released its resources.+ """+ waiter = asyncio.ensure_future(process.wait())+ cancelled = False+ while True:+ try:+ await asyncio.shield(waiter)+ return cancelled+ except asyncio.CancelledError:+ cancelled = True+++@dataclass(frozen=True)+class DisplayRecorderAcquisition:+ recorder: DisplayRecorder | None+ video_artifact: VideoArtifact | None+ started: bool+++class DisplayRecorder:+ def __init__(+ self,+ *,+ display: str,+ owner_id: str,+ process: asyncio.subprocess.Process,+ lock_fd: int,+ video_artifact: VideoArtifact,+ ) -> None:+ self.display = normalize_display(display)+ self.owner_id = owner_id+ self.process = process+ self.lock_fd = lock_fd+ self.video_artifact = video_artifact+ self._stop_lock = asyncio.Lock()+ self._stop_result: bool | None = None+ # Set when the ffmpeg is stopped for upload but the display must stay fenced to this owner until a+ # later, separate teardown (a deferred live-view browser close). Keeps a stopped recorder from being+ # pruned/reaped as a plain dead entry, so a different owner cannot acquire the display in between.+ self._reserved = False++ @property+ def is_stopped(self) -> bool:+ """True once ``stop()`` has run to completion, so the WebM on disk is finalized and uploadable."""+ return self._stop_result is not None++ async def finalize_keeping_reservation(self) -> bool:+ """Stop the ffmpeg so the WebM is complete and uploadable, but keep the display lock and registry+ reservation. Used when a live-view close is deferred: the run's browser is still mapped on the+ display, so releasing the reservation now would let a different-owner run acquire it and capture the+ prior tenant's window. The reservation is released only at real browser teardown+ (``release_display_recorder``) or process death."""+ self._reserved = True+ return await self.stop(release_lock=False)++ @staticmethod+ def lock_path_for_display(display: str) -> str:+ safe_display = re.sub(r"[^A-Za-z0-9_-]", "_", normalize_display(display))+ return f"/tmp/skyvern-display-recording-{safe_display}.lock"++ def _release_lock(self) -> None:+ if self.lock_fd is not None and self.lock_fd >= 0:+ with suppress(OSError):+ os.close(self.lock_fd)+ self.lock_fd = -1++ async def stop(self, release_lock: bool = True) -> bool:+ async with self._stop_lock:+ if self._stop_result is not None:+ return self._stop_result+ graceful = self.process.returncode == 0+ pending_cancel = False+ try:+ graceful = await self._escalate_shutdown(graceful)+ except asyncio.CancelledError:+ pending_cancel = True+ finally:+ # Reap the child and (unless the display stays reserved) release the lock even under repeated+ # cancellation, so a cancelled stop never leaks ffmpeg or blocks the next run. The reap loop+ # absorbs further cancels; we re-raise exactly once, after lock release. A reserved recorder+ # keeps its lock — release_display_recorder frees it at the real browser teardown.+ if self.process.returncode is None:+ with suppress(ProcessLookupError):+ self.process.kill()+ if await _reap_surviving_cancellation(self.process):+ pending_cancel = True+ graceful = False+ if release_lock:+ self._release_lock()+ self._stop_result = graceful+ if pending_cancel:+ raise asyncio.CancelledError()+ return graceful++ async def _escalate_shutdown(self, graceful: bool) -> bool:+ if self.process.returncode is not None:+ return graceful+ self.process.send_signal(signal.SIGINT)+ try:+ await asyncio.wait_for(self.process.wait(), timeout=SIGINT_TIMEOUT)+ return True+ except TimeoutError:+ pass+ self.process.terminate()+ try:+ await asyncio.wait_for(self.process.wait(), timeout=SIGTERM_TIMEOUT)+ except TimeoutError:+ return False+ return graceful+++def _prune_dead_entries(normalized_display: str, owner_id: str) -> None:+ # Drop exited recorders of a DIFFERENT owner on this display so a previous run's dead recorder never+ # fail-closes the next one. Same-owner is handled by reuse above; a live owner is left entirely alone.+ dead_keys = [+ key+ for key, rec in _REGISTRY.items()+ if key[0] == normalized_display+ and key[1] != owner_id+ and rec.process.returncode is not None+ and not rec._reserved+ ]+ for key in dead_keys:+ dead = _REGISTRY.pop(key)+ dead._release_lock()+++async def acquire_display_recorder(+ display: str,+ owner_id: str,+ video_dir: Path,+ owner_id_override: str | None = None,+ capture_sizes: CaptureSizes | None = None,+) -> DisplayRecorderAcquisition:+ if fcntl is None:+ LOG.warning("Whole-display recording unavailable: fcntl not present on this platform", display=display)+ return DisplayRecorderAcquisition(None, None, False)++ normalized_display = normalize_display(display)+ key = (normalized_display, owner_id)++ existing = _REGISTRY.get(key)+ if existing is not None:+ # Same (display, owner): reuse the exact recorder + VideoArtifact (never restart a dead one — a fresh+ # ffmpeg -y would overwrite the partial WebM and mint a second RECORDING row); the partial uploads as-is.+ return DisplayRecorderAcquisition(existing, existing.video_artifact, False)++ _prune_dead_entries(normalized_display, owner_id)++ conflicting = next((rec for reg_key, rec in _REGISTRY.items() if reg_key[0] == normalized_display), None)+ if conflicting is not None:+ if owner_id_override is not None and conflicting.owner_id == owner_id_override:+ # A shared-state reconnect rebuilds under the aliased child id but carries its own live recorder's+ # owner as the override; re-adopt that exact recorder + artifact (same object, no restart).+ return DisplayRecorderAcquisition(conflicting, conflicting.video_artifact, False)+ LOG.error(+ "Whole-display recording refused because the display has a live owner",+ display=normalized_display,+ owner_id=owner_id,+ holder_owner_id=conflicting.owner_id,+ )+ return DisplayRecorderAcquisition(None, None, False)++ # Any failure below must fail closed WITHOUT leaking the lock fd and WITHOUT propagating into the+ # browser-creation path — a run whose recorder cannot start simply continues unrecorded.+ lock_fd: int | None = None+ process: asyncio.subprocess.Process | None = None+ try:+ lock_fd = os.open(DisplayRecorder.lock_path_for_display(normalized_display), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ except BlockingIOError:+ os.close(lock_fd)+ LOG.error("Whole-display recording refused because the display lock is held", display=normalized_display)+ return DisplayRecorderAcquisition(None, None, False)++ video_dir.mkdir(parents=True, exist_ok=True)+ command, output_path = build_ffmpeg_command(normalized_display, owner_id, video_dir, capture_sizes)+ # Capture intentionally stays WebM so interrupted recordings remain repairable/finalizable;+ # the existing upload preparation path handles MP4 conversion.+ prefix = [sys.executable, "-c", _PDEATHSIG_TRAMPOLINE, str(os.getpid())] if os.name == "posix" else []+ process = await asyncio.create_subprocess_exec(+ *prefix,+ *command,+ stdin=asyncio.subprocess.DEVNULL,+ stdout=asyncio.subprocess.DEVNULL,+ stderr=asyncio.subprocess.DEVNULL,+ )+ # Give ffmpeg a bounded window to fail its x11grab init before we seed a VideoArtifact and suppress+ # Playwright recording; wait_for returns the instant the child exits and times out (healthy) otherwise.+ with suppress(asyncio.TimeoutError, TimeoutError):+ await asyncio.wait_for(process.wait(), timeout=STARTUP_READINESS_TIMEOUT)+ if process.returncode is not None:+ os.close(lock_fd)+ LOG.warning(+ "Whole-display recorder exited during startup",+ display=normalized_display,+ returncode=process.returncode,+ )+ return DisplayRecorderAcquisition(None, None, False)+ except BaseException as exc:+ cancelled_during_reap = False+ if process is not None and process.returncode is None:+ with suppress(ProcessLookupError):+ process.kill()+ cancelled_during_reap = await _reap_surviving_cancellation(process)+ if lock_fd is not None:+ with suppress(OSError):+ os.close(lock_fd)+ if not isinstance(exc, Exception):+ # A BaseException entry (the unwind was itself cancelled): cleanup is done, propagate it.+ raise+ if cancelled_during_reap:+ # A plain-exception unwind was cancelled while reaping; cleanup is complete, so surface the+ # cancellation instead of returning a fail-closed acquisition that would swallow it.+ raise asyncio.CancelledError()+ LOG.warning("Failed to start whole-display recorder", display=normalized_display, exc_info=True)+ return DisplayRecorderAcquisition(None, None, False)++ video_artifact = VideoArtifact(video_path=str(output_path))+ recorder = DisplayRecorder(+ display=normalized_display,+ owner_id=owner_id,+ process=process,+ lock_fd=lock_fd,+ video_artifact=video_artifact,+ )+ _REGISTRY[key] = recorder+ return DisplayRecorderAcquisition(recorder, video_artifact, True)+++async def release_display_recorder(recorder: DisplayRecorder | None) -> bool:+ if recorder is None:+ return True+ # One stop task per release, awaited through repeated caller cancellation: the reservation/lock/registry+ # entry must never be freed until the serialized stop is terminal. A caller cancelled while parked behind+ # another release's _stop_lock would otherwise release mid-shutdown and expose the display to a new owner.+ stop_task = asyncio.ensure_future(recorder.stop())+ cancelled = False+ try:+ while True:+ try:+ return await asyncio.shield(stop_task)+ except asyncio.CancelledError:+ cancelled = True+ if stop_task.done():+ # The stop is terminal; the finally releases and then re-raises the cancellation, so this+ # returned value is discarded — it only proves to the type checker that no path falls through.+ return stop_task.result()+ finally:+ # This is the real browser-teardown release, so drop the reservation and free the display lock — but+ # only once the stop is terminal, even when ``stop()`` short-circuited on an already-finalized recorder.+ if recorder.is_stopped:+ recorder._reserved = False+ recorder._release_lock()+ key = (recorder.display, recorder.owner_id)+ # Only drop OUR entry: a delayed release of a superseded recorder must never evict a replacement+ # that a later same-owner acquisition registered under the same key.+ if _REGISTRY.get(key) is recorder:+ del _REGISTRY[key]+ if cancelled:+ raise asyncio.CancelledError()+++async def stop_display_recorders_for_owner(owner_id: str) -> int:+ """Stop and de-register every live whole-display recorder this owner still holds.++ A run whose teardown never reached ``release_display_recorder`` (a Temporal ``CancelledError`` bypassing+ the ``except Exception`` cleanup, or a mid-run crash) leaves its ffmpeg, ``_REGISTRY`` entry, and lock+ live. Call this BEFORE unlinking so they are reaped inside ``stop()`` instead of orphaned into the next+ activity; a cancellation re-raised by ``stop()`` is absorbed here (resources already freed).+ """+ released = 0+ for key, recorder in list(_REGISTRY.items()):+ if key[1] != owner_id:+ continue+ if recorder._reserved:+ # Reserved (deferred live-view close): reaping here would free a display still mapped to this+ # owner's browser. Only browser teardown or process death releases the reservation.+ continue+ try:+ await release_display_recorder(recorder)+ except asyncio.CancelledError:+ pass+ except Exception:+ LOG.warning("Failed to stop whole-display recorder during owner cleanup", owner_id=owner_id, exc_info=True)+ released += 1+ return releaseddiff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex 2d972ea4d..4387fb3e4 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -46,6 +46,7 @@ from skyvern.webeye.cdp_frame_publisher import (stream_key_for_task,stream_key_for_workflow_run,)+from skyvern.webeye.display_recorder import DisplayRecorderfrom skyvern.webeye.persistent_sessions_manager import PBS_TASK_RUNNABLE_TYPEfrom skyvern.webeye.real_browser_state import RealBrowserStatefrom skyvern.webeye.session_cookies import persist_session_cookies@@ -1128,12 +1129,10 @@ class RealBrowserManager(BrowserManager):return Nonedef set_video_artifact_for_task(self, task: Task, artifacts: list[VideoArtifact]) -> None:- if task.workflow_run_id and task.workflow_run_id in self.pages:- self.pages[task.workflow_run_id].browser_artifacts.video_artifacts = artifacts- return- if task.task_id in self.pages:- self.pages[task.task_id].browser_artifacts.video_artifacts = artifacts- return+ for run_key in (task.workflow_run_id, task.task_id):+ if run_key and run_key in self.pages:+ self.pages[run_key].browser_artifacts.video_artifacts = artifacts+ returnraise MissingBrowserState(task_id=task.task_id,@@ -1163,24 +1162,31 @@ class RealBrowserManager(BrowserManager):)return []- for i, video_artifact in enumerate(browser_state.browser_artifacts.video_artifacts):+ display_recorder = browser_state.browser_artifacts._display_recorder+ display_video_artifact = (+ display_recorder.video_artifact if isinstance(display_recorder, DisplayRecorder) else None+ )+ # The whole-display recording is the legacy index-0 recording: per-step syncs read its current+ # (growing) bytes and terminal finalize remuxes the SAME artifact/id, exactly like a Playwright+ # per-page recording. It additionally finalizes once its recorder has stopped even when a deferred+ # stream left finalize=False, so the deferred-close upload is finalized rather than a partial prefix.+ display_recorder_stopped = isinstance(display_recorder, DisplayRecorder) and display_recorder.is_stopped++ for video_artifact in browser_state.browser_artifacts.video_artifacts:+ finalize_this = finalize or (video_artifact is display_video_artifact and display_recorder_stopped)path = video_artifact.video_pathif path and os.path.exists(path=path):is_webm = path.lower().endswith(".webm")- if finalize and is_webm:+ if finalize_this and is_webm:async with prepare_recording_for_upload(path) as prepared:with open(prepared.path, "rb") as f:- browser_state.browser_artifacts.video_artifacts[i].video_data = f.read()- browser_state.browser_artifacts.video_artifacts[- i- ].video_file_extension = prepared.file_extension+ video_artifact.video_data = f.read()+ video_artifact.video_file_extension = prepared.file_extensionelse:# Non-WebM sources are already container-valid; per-step WebM snapshots are still incomplete.with open(path, "rb") as f:- browser_state.browser_artifacts.video_artifacts[i].video_data = f.read()- browser_state.browser_artifacts.video_artifacts[i].video_file_extension = (- os.path.splitext(path)[1].lstrip(".").lower() or "webm"- )+ video_artifact.video_data = f.read()+ video_artifact.video_file_extension = os.path.splitext(path)[1].lstrip(".").lower() or "webm"else:LOG.debug("Video path not found",@@ -1439,6 +1445,11 @@ class RealBrowserManager(BrowserManager):# eventual ``close(True)`` fires the on-close callback that# stops it; ``close(False)`` is covered by the publisher's# own disconnect-driven self-termination.+ # The whole-display recording is per-run and decoupled from the deferred browser close, so+ # finalize it now: the run is terminal and the activity finally will unlink the file, so+ # without this the terminal video persist would run finalize=False and the recording would+ # be unlinked before it was ever uploaded.+ await self._finalize_deferred_display_recording(browser_state_to_close, workflow_run_id, task_ids)else:# Detach the publisher's CDP session before the Playwright context# closes; otherwise the stale session can race the teardown.@@ -1512,6 +1523,33 @@ class RealBrowserManager(BrowserManager):recording_finalized=recording_finalized,)+ async def _finalize_deferred_display_recording(+ self, browser_state: BrowserState, workflow_run_id: str, task_ids: list[str]+ ) -> None:+ """Finalize this run's whole-display recorder when its browser close is deferred for an active stream.++ Stops the ffmpeg so the WebM is complete/uploadable, but KEEPS the display reserved to this owner:+ the browser is still mapped on the shared display, so freeing it now would let a later different-owner+ run acquire the display and capture this tenant's window. The reservation is released only when the+ deferred browser is actually torn down (real_browser_state.close -> release_display_recorder) or at+ process death. Gated on the workflow-tree-owned task set (workflow_run_id or one of its task_ids — a+ task-created browser owns its recorder under the task id) so an inherited/shared browser never has a+ sibling run's recording stopped out from under it. Best-effort and never raising (cancellation still+ propagates, preserving cancellation ownership)."""+ recorder = browser_state.browser_artifacts._display_recorder+ if not isinstance(recorder, DisplayRecorder) or recorder.owner_id not in {workflow_run_id, *task_ids}:+ return+ try:+ await recorder.finalize_keeping_reservation()+ except asyncio.CancelledError:+ raise+ except Exception:+ LOG.warning(+ "Failed to finalize whole-display recording on deferred close",+ workflow_run_id=workflow_run_id,+ exc_info=True,+ )+async def get_or_create_for_script(self,script_id: str | None = None,diff --git a/skyvern/webeye/real_browser_state.py b/skyvern/webeye/real_browser_state.pyindex 55596a2c3..b23dac0e7 100644--- a/skyvern/webeye/real_browser_state.py+++ b/skyvern/webeye/real_browser_state.py@@ -36,6 +36,7 @@ from skyvern.webeye.browser_factory import BrowserCleanupFunc, BrowserContextFacfrom skyvern.webeye.browser_health import BrowserOperationfrom skyvern.webeye.browser_state import BLANK_PAGE_URLS, BrowserStatefrom skyvern.webeye.cdp_download_interceptor import disable_download_interceptor_for_context+from skyvern.webeye.display_recorder import DisplayRecorder, release_display_recorderfrom skyvern.webeye.driver_connection import close_driver_connection_on_transport_lossfrom skyvern.webeye.navigation import is_permanent_navigation_error, navigate_with_retryfrom skyvern.webeye.scraper import scraper@@ -253,6 +254,7 @@ class RealBrowserState(BrowserState):browser_address: str | None = None,browser_profile_id: str | None = None,download_binding: DownloadBinding | None = None,+ display_recording_owner_id: str | None = None,) -> None:if self.browser_context is None:LOG.info("creating browser context")@@ -284,6 +286,7 @@ class RealBrowserState(BrowserState):browser_profile_id=browser_profile_id,engine_selection=self.engine_selection,download_binding=effective_download_binding,+ display_recording_owner_id=display_recording_owner_id,)self.browser_context = browser_contextself.browser_artifacts = browser_artifacts@@ -655,6 +658,11 @@ class RealBrowserState(BrowserState):prior_download_binding = (self.browser_artifacts.download_binding if self.browser_artifacts else DownloadBinding.RUN_DIR)+ # Preserve the active whole-display recorder's owner across the rebuild, derived from THIS state's own+ # recorder (never a caller id), so a standalone task/script reconnect re-acquires the SAME recorder+ # instead of a fresh Playwright recording. A different run cannot inherit it (id is this run's own).+ recorder = self.browser_artifacts._display_recorder if self.browser_artifacts else None+ display_recording_owner_id = recorder.owner_id if isinstance(recorder, DisplayRecorder) else Noneself.browser_context = Noneawait self.set_working_page(None)# Reconnect on the SAME engine this state was pinned to at creation; never silently switch@@ -675,6 +683,7 @@ class RealBrowserState(BrowserState):browser_address=browser_address,browser_profile_id=browser_profile_id,download_binding=prior_download_binding,+ display_recording_owner_id=display_recording_owner_id,)except Exception:# The caller abandons this state on failure, so stop the just-started driver too or it leaks.@@ -847,11 +856,28 @@ class RealBrowserState(BrowserState):BROWSER_CLOSE_TIMEOUT,"browser context teardown",)+ # The display recorder is stopped/finalized on its own, decoupled from context teardown:+ # ``recording_finalized`` reports only that the browser context closed, which is what gates+ # profile persistence. A recorder that exited non-zero mid-run (or needed a forced kill)+ # must not clear that flag and suppress an otherwise-clean run's profile write-back, and a+ # context-teardown failure must not stop us from finalizing the WebM for upload. The stop+ # runs even when teardown failed above, so the recording is always finalized best-effort.+ recorder = self.browser_artifacts._display_recorder if self.browser_artifacts else None+ if isinstance(recorder, DisplayRecorder):+ await self._run_bounded_detachable(+ self._stop_display_recorder(recorder),+ BROWSER_CLOSE_TIMEOUT,+ "whole-display recorder stop",+ )await self._run_browser_cleanup_bounded()await self._stop_driver_bounded(release_driver)return recording_finalized+ async def _stop_display_recorder(self, recorder: DisplayRecorder) -> None:+ if not await release_display_recorder(recorder):+ raise RuntimeError("Whole-display recorder required forced termination")+async def detach_remote_driver(self) -> None:"""Release this process's adopted-remote resources without closing the remote browser.diff --git a/tests/unit/test_browser_context_factory_marker.py b/tests/unit/test_browser_context_factory_marker.pyindex fb873221f..9a546d8af 100644--- a/tests/unit/test_browser_context_factory_marker.py+++ b/tests/unit/test_browser_context_factory_marker.py@@ -9,6 +9,7 @@ auto-stamp.from __future__ import annotations+import asynciofrom typing import Anyfrom unittest.mock import AsyncMock, MagicMock@@ -17,6 +18,7 @@ from playwright.async_api import BrowserContext, Locator, Pagefrom skyvern.forge.sdk.workflow.models.code_block_recorder import RecordingPagefrom skyvern.webeye import browser_factory as factory_module+from skyvern.webeye import display_recorder as recorder_modulefrom skyvern.webeye.browser_artifacts import BrowserArtifactsfrom skyvern.webeye.browser_factory import BrowserContextFactoryfrom skyvern.webeye.playwright_input import playwright_input_defaults_for_page@@ -130,6 +132,164 @@ def _factory_harness(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(factory_module, "app", _FakeApp())+def _register_eligible_local_creator(monkeypatch: pytest.MonkeyPatch, name: str) -> None:+ from skyvern.webeye.browser_artifacts import BrowserArtifacts+ from skyvern.webeye.browser_factory import BrowserContextFactory++ async def _eligible_creator(playwright: Any, **kwargs: Any) -> tuple[Any, BrowserArtifacts, None]:+ artifacts = BrowserArtifacts()+ # Mirrors what the real local-launch creators do after configure_local_display_recording().+ artifacts.local_display_recording_eligible = True+ return object(), artifacts, None++ BrowserContextFactory.register_type(name, _eligible_creator)+ monkeypatch.setattr(factory_module.settings, "BROWSER_TYPE", name)+++async def test_eligible_local_launch_seeds_single_artifact_and_skips_playwright_listener(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Any+) -> None:+ from skyvern.webeye.browser_artifacts import VideoArtifact+ from skyvern.webeye.browser_factory import BrowserContextFactory+ from skyvern.webeye.display_recorder import DisplayRecorderAcquisition++ _factory_harness(monkeypatch)+ popup = MagicMock()+ monkeypatch.setattr(factory_module, "set_popup_video_listener", popup)+ seeded = VideoArtifact(video_path=str(tmp_path / "wr_seed.webm"))+ recorder = MagicMock()+ acquire = AsyncMock(return_value=DisplayRecorderAcquisition(recorder, seeded, True))+ monkeypatch.setattr(factory_module, "acquire_display_recorder", acquire)+ monkeypatch.setenv("DISPLAY", ":99")+ monkeypatch.setattr(factory_module.settings, "VIDEO_PATH", str(tmp_path))+ _register_eligible_local_creator(monkeypatch, "test-eligible-seed")++ _, artifacts, _ = await BrowserContextFactory.create_browser_context(playwright=object(), workflow_run_id="wr_seed")++ assert acquire.await_count == 1+ # Exactly one run-scoped VideoArtifact, seeded synchronously in the creator tail.+ assert artifacts.video_artifacts == [seeded]+ assert artifacts._display_recorder is recorder+ # The Playwright per-page listener must NOT run on the eligible whole-display path.+ popup.assert_not_called()+++async def test_remote_cdp_path_never_spawns_recorder_and_keeps_playwright_listener(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Any+) -> None:+ from skyvern.webeye.browser_artifacts import BrowserArtifacts+ from skyvern.webeye.browser_factory import BrowserContextFactory++ _factory_harness(monkeypatch)+ popup = MagicMock()+ monkeypatch.setattr(factory_module, "set_popup_video_listener", popup)+ acquire = AsyncMock()+ monkeypatch.setattr(factory_module, "acquire_display_recorder", acquire)+ monkeypatch.setenv("DISPLAY", ":99")+ monkeypatch.setattr(factory_module.settings, "VIDEO_PATH", str(tmp_path))++ async def _remote_creator(playwright: Any, **kwargs: Any) -> tuple[Any, BrowserArtifacts, None]:+ # Remote/CDP/vendor creators never set the eligibility marker.+ return object(), BrowserArtifacts(), None++ BrowserContextFactory.register_type("test-remote", _remote_creator)+ monkeypatch.setattr(factory_module.settings, "BROWSER_TYPE", "test-remote")++ _, artifacts, _ = await BrowserContextFactory.create_browser_context(playwright=object(), workflow_run_id="wr_x")++ assert acquire.await_count == 0+ assert artifacts.video_artifacts == []+ assert artifacts._display_recorder is None+ popup.assert_called_once()+++async def test_same_owner_recreation_reuses_exact_video_artifact_object(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Any+) -> None:+ """Recreation (check_and_fix_state re-enters the factory) must reuse the exact VideoArtifact object so+ its video_artifact_id survives, which is what keeps agent.initialize_execution_state at one RECORDING row."""+ from skyvern.webeye.browser_factory import BrowserContextFactory++ _factory_harness(monkeypatch)+ recorder_module._REGISTRY.clear()+ process = MagicMock(returncode=None)+ process.wait = AsyncMock()+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))+ monkeypatch.setenv("DISPLAY", ":99")+ monkeypatch.setattr(factory_module.settings, "VIDEO_PATH", str(tmp_path))+ _register_eligible_local_creator(monkeypatch, "test-eligible-reuse")++ try:+ _, first, _ = await BrowserContextFactory.create_browser_context(+ playwright=object(), workflow_run_id="wr_reuse"+ )+ _, second, _ = await BrowserContextFactory.create_browser_context(+ playwright=object(), workflow_run_id="wr_reuse"+ )++ assert first.video_artifacts[0] is second.video_artifacts[0]+ # Step-1 stamps the id on the shared object; recreation therefore never yields a second row.+ first.video_artifacts[0].video_artifact_id = "va_1"+ assert second.video_artifacts[0].video_artifact_id == "va_1"+ finally:+ process.returncode = 0+ await recorder_module.release_display_recorder(first._display_recorder)+ recorder_module._REGISTRY.clear()+++ ("producer_kwargs", "reconnect_kwargs"),+ [+ pytest.param({"task_id": "tsk_solo"}, {}, id="standalone"),+ pytest.param({"workflow_run_id": "wr_parent"}, {"workflow_run_id": "wr_child"}, id="aliased-child"),+ ],+)+async def test_reconnect_end_to_end_preserves_exact_display_recorder(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Any, producer_kwargs: dict[str, str], reconnect_kwargs: dict[str, str]+) -> None:+ """End-to-end (real factory + reconnect()): the rebuild must re-acquire the EXACT same recorder + index-0+ VideoArtifact, popup suppressed. Covers standalone reconnect AND the use_parent_browser_session aliased+ child (parent-owned recorder, child reconnect id) — RED without the acquire-level override reuse."""+ from skyvern.webeye.browser_factory import BrowserContextFactory+ from skyvern.webeye.real_browser_state import RealBrowserState++ _factory_harness(monkeypatch)+ recorder_module._REGISTRY.clear()+ popup = MagicMock()+ monkeypatch.setattr(factory_module, "set_popup_video_listener", popup)+ process = MagicMock(returncode=None, wait=AsyncMock())+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))+ monkeypatch.setenv("DISPLAY", ":99")+ monkeypatch.setattr(factory_module.settings, "VIDEO_PATH", str(tmp_path))+ _register_eligible_local_creator(monkeypatch, "test-eligible-e2e")++ _, first, _ = await BrowserContextFactory.create_browser_context(playwright=object(), **producer_kwargs)+ recorder0, artifact0 = first._display_recorder, first.video_artifacts[0]+ assert recorder0 is not None++ state = RealBrowserState.__new__(RealBrowserState)+ state.pw = AsyncMock()+ state.browser_context = MagicMock()+ state.engine_selection = MagicMock(start_driver=AsyncMock(return_value=AsyncMock()))+ state.set_working_page = AsyncMock()+ state.get_working_page = AsyncMock(return_value=MagicMock())+ state.browser_artifacts = first++ try:+ await state.reconnect(**reconnect_kwargs)+ assert state.browser_artifacts._display_recorder is recorder0, "exact same recorder survives reconnect"+ assert state.browser_artifacts.video_artifacts == [artifact0], "one index-0 VideoArtifact object survives"+ popup.assert_not_called()+ finally:+ process.returncode = 0+ await recorder_module.release_display_recorder(recorder0)+ recorder_module._REGISTRY.clear()++@pytest.mark.asyncioasync def test_factory_registers_authoritative_playwright_input_defaults(monkeypatch: pytest.MonkeyPatch) -> None:context = MagicMock()diff --git a/tests/unit/test_display_recorder.py b/tests/unit/test_display_recorder.pynew file mode 100644index 000000000..b0a10d71d--- /dev/null+++ b/tests/unit/test_display_recorder.py@@ -0,0 +1,811 @@+from __future__ import annotations++import asyncio+import fcntl+import hashlib+import os+import signal+import sys+from collections.abc import Iterator+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock++import pytest++from skyvern.webeye import attach_only, display_recorder+from skyvern.webeye.display_recorder import (+ CAPTURE_DISPLAY_SIZE,+ CAPTURE_OUTPUT_BOUND,+ DisplayRecorder,+ acquire_display_recorder,+ build_ffmpeg_command,+ configure_local_display_recording,+ normalize_display,+ release_display_recorder,+ resolve_display_capture_sizes,+ resolve_owner_id,+ stop_display_recorders_for_owner,+)++[email protected](autouse=True)+def _clear_recorder_registry() -> Iterator[None]:+ display_recorder._REGISTRY.clear()+ yield+ # Close lock fds still held by leaked entries so the per-display lock does not fail-close a later test.+ for rec in list(display_recorder._REGISTRY.values()):+ rec._release_lock()+ display_recorder._REGISTRY.clear()+++ "raw, expected",+ [+ (":99", ":99"),+ (":99.0", ":99"),+ (" :99.0 ", ":99"),+ ("host.example.com:0", "host.example.com:0"),+ ("host.example.com:0.0", "host.example.com:0"),+ ],+)+def test_normalize_display_strips_screen_but_preserves_hostname_dots(raw: str, expected: str) -> None:+ assert normalize_display(raw) == expected+++def test_eligibility_hard_gates_attach_only_worker(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(display_recorder.settings, "EXCLUSIVE_DISPLAY_RECORDING", True)+ monkeypatch.setattr(display_recorder.platform, "system", lambda: "Linux")+ monkeypatch.setenv("DISPLAY", ":99")+ monkeypatch.setattr(attach_only, "is_enforcing", lambda: True)+ args: dict[str, object] = {"record_video_dir": "/video", "record_video_size": {"width": 800}}++ assert configure_local_display_recording(args, task_id="task") is False+ assert args == {"record_video_dir": "/video", "record_video_size": {"width": 800}}+++def test_ffmpeg_command_uses_safe_run_path_and_production_shape(tmp_path: Path) -> None:+ command, output_path = build_ffmpeg_command(display=":99", owner_id="../../run / unsafe", video_dir=tmp_path)++ assert output_path.parent.resolve().is_relative_to(tmp_path.resolve())+ assert output_path.name == f"run___unsafe-{hashlib.sha256(b'../../run / unsafe').hexdigest()}.webm"+ assert command == [+ "ffmpeg",+ "-y",+ "-f",+ "x11grab",+ "-video_size",+ "1920x1080",+ "-framerate",+ "15",+ "-i",+ ":99",+ "-vf",+ "scale=1280:720",+ "-c:v",+ "libvpx",+ "-b:v",+ "1M",+ "-qmin",+ "0",+ "-qmax",+ "50",+ "-an",+ "-deadline",+ "realtime",+ str(output_path),+ ]+++ "window, expected",+ [+ ("1600,900", ((1600, 900), (1280, 720))),+ ("1512,982", ((1512, 982), (1108, 720))), # height binds; width floored to an even 1108+ ("1920,1080", ((1920, 1080), (1280, 720))), # base window → the fixed full-screen capture+ ],+)+def test_resolve_display_capture_sizes_scales_final_window_size(+ window: str, expected: tuple[tuple[int, int], tuple[int, int]]+) -> None:+ # The seam derives the capture rectangle from the FINAL --window-size (last entry wins, as the viewport+ # override leaves exactly one) and bounds the output within 1280x720, aspect-preserved with even dims.+ args = {"args": ["--window-position=0,0", "--window-size=1920,1080", f"--window-size={window}"]}+ assert resolve_display_capture_sizes(args) == expected+++ "args",+ [+ {"args": ["--window-position=0,0"]}, # no --window-size at all+ {"args": ["--window-size=not,a,size"]}, # unparseable+ {"args": ["--window-size=2000,1200"]}, # larger than the 1920x1080 display+ {"args": "not-a-list"}, # malformed args+ ],+)+def test_resolve_display_capture_sizes_falls_back_to_full_screen(args: dict[str, object]) -> None:+ assert resolve_display_capture_sizes(args) == (CAPTURE_DISPLAY_SIZE, CAPTURE_OUTPUT_BOUND)+++ "capture_sizes, video_size, scale",+ [+ (((1600, 900), (1280, 720)), "1600x900", "scale=1280:720"),+ (((1512, 982), (1108, 720)), "1512x982", "scale=1108:720"),+ (None, "1920x1080", "scale=1280:720"), # control: no resolved sizes → fixed full-screen capture+ ],+)+def test_ffmpeg_command_captures_window_and_scales_to_resolved_output(+ tmp_path: Path,+ capture_sizes: tuple[tuple[int, int], tuple[int, int]] | None,+ video_size: str,+ scale: str,+) -> None:+ command, _ = build_ffmpeg_command(":99", "owner", tmp_path, capture_sizes=capture_sizes)+ assert command[command.index("-video_size") + 1] == video_size+ assert command[command.index("-vf") + 1] == scale+++def test_local_eligibility_strips_playwright_video_only_with_all_structural_inputs(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(display_recorder.settings, "EXCLUSIVE_DISPLAY_RECORDING", True)+ monkeypatch.setattr(display_recorder.platform, "system", lambda: "Linux")+ monkeypatch.setenv("DISPLAY", ":99.0")+ args: dict[str, object] = {"record_video_dir": "/video", "record_video_size": {"width": 800}}++ assert configure_local_display_recording(args, workflow_run_id="workflow", task_id="task") is True+ assert "record_video_dir" not in args+ assert "record_video_size" not in args+ assert resolve_owner_id(workflow_run_id="workflow", task_id="task", script_id="script") == "workflow"++[email protected]("missing", ["marker", "display", "owner"])+def test_ineligible_path_preserves_playwright_video_args(missing: str, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(display_recorder.settings, "EXCLUSIVE_DISPLAY_RECORDING", True)+ monkeypatch.setattr(display_recorder.platform, "system", lambda: "Linux")+ monkeypatch.setenv("DISPLAY", ":99")+ owner = "owner"+ if missing == "marker":+ monkeypatch.setattr(display_recorder.settings, "EXCLUSIVE_DISPLAY_RECORDING", False)+ elif missing == "display":+ monkeypatch.delenv("DISPLAY")+ else:+ owner = None+ args: dict[str, object] = {"record_video_dir": "/video", "record_video_size": {"width": 800}}++ assert configure_local_display_recording(args, task_id=owner) is False+ assert args == {"record_video_dir": "/video", "record_video_size": {"width": 800}}+++def test_non_linux_remains_ineligible_when_enabled(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(display_recorder.settings, "EXCLUSIVE_DISPLAY_RECORDING", True)+ monkeypatch.setattr(display_recorder.platform, "system", lambda: "Darwin")+ monkeypatch.setenv("DISPLAY", ":99")+ args = {"record_video_dir": "/video", "record_video_size": {"width": 800}}+ assert configure_local_display_recording(args, task_id="task") is False+ assert args == {"record_video_dir": "/video", "record_video_size": {"width": 800}}+++async def test_same_owner_reuses_recorder_and_exact_artifact(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ process = MagicMock(returncode=None)+ process.wait = AsyncMock()+ spawn = AsyncMock(return_value=process)+ monkeypatch.setattr(asyncio, "create_subprocess_exec", spawn)++ first = await acquire_display_recorder(":99", "owner", tmp_path)+ second = await acquire_display_recorder(":99", "owner", tmp_path)++ assert first.started is True+ assert second.started is False+ assert second.recorder is first.recorder+ assert second.video_artifact is first.video_artifact+ first.video_artifact.video_artifact_id = "artifact-id"+ assert second.video_artifact.video_artifact_id == "artifact-id"+ assert spawn.await_count == 1++ process.returncode = 0+ assert await release_display_recorder(first.recorder) is True++[email protected]("override", [None, "owner-c"], ids=["no-override", "foreign-override"])+async def test_different_owner_cannot_touch_holder(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, override: str | None+) -> None:+ # The override re-adopts a live display ONLY when it names the current holder; a missing or foreign+ # override is refused, so a different owner never adopts another run's recorder.+ process = MagicMock(returncode=None)+ process.wait = AsyncMock()+ spawn = AsyncMock(return_value=process)+ monkeypatch.setattr(asyncio, "create_subprocess_exec", spawn)++ first = await acquire_display_recorder(":99", "owner-a", tmp_path)+ refused = await acquire_display_recorder(":99", "owner-b", tmp_path, owner_id_override=override)++ assert refused.recorder is None+ assert refused.video_artifact is None+ assert process.send_signal.call_count == 0+ assert process.terminate.call_count == 0+ assert process.kill.call_count == 0+ assert spawn.await_count == 1++ process.returncode = 0+ assert await release_display_recorder(first.recorder) is True+++async def test_finalized_recorder_keeps_display_reserved_until_released(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Security: finalizing a recorder while its owner's browser stays mapped (deferred live-view close) keeps+ the display reserved to that owner until real browser teardown — a different owner must not capture it."""+ proc_a = MagicMock(returncode=None)+ proc_a.wait = AsyncMock()+ proc_b = MagicMock(returncode=None)+ proc_b.wait = AsyncMock()+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(side_effect=[proc_a, proc_b]))++ a = await acquire_display_recorder(":99", "owner-a", tmp_path)+ proc_a.returncode = 0 # ffmpeg exits gracefully on the finalize SIGINT+ await a.recorder.finalize_keeping_reservation()+ assert a.recorder.is_stopped++ # The owner's activity-finally cleanup must NOT reap the reserved recorder (dropping the reserved-skip+ # guard in stop_display_recorders_for_owner reaps it here and REDs both assertions below).+ assert await stop_display_recorders_for_owner("owner-a") == 0, (+ "the activity finally must not reap a recorder still reserved to its deferred browser"+ )++ refused = await acquire_display_recorder(":99", "owner-b", tmp_path)+ assert refused.recorder is None, "a different owner must not acquire a display reserved to the prior owner"++ # Only the real browser teardown releases the reservation.+ await release_display_recorder(a.recorder)+ b = await acquire_display_recorder(":99", "owner-b", tmp_path)+ assert b.recorder is not None+ proc_b.returncode = 0+ await release_display_recorder(b.recorder)+++async def test_recorder_launch_is_fork_safe_without_preexec(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ """P1: a fork-time preexec_fn (ctypes prctl) can hang the multithreaded worker's fork child before+ exec; the launch must pass NO preexec_fn and set PDEATHSIG via an exec'd python trampoline instead."""+ process = MagicMock(returncode=None)+ process.wait = AsyncMock()+ spawn = AsyncMock(return_value=process)+ monkeypatch.setattr(asyncio, "create_subprocess_exec", spawn)++ rec = await acquire_display_recorder(":99", "owner", tmp_path)+ assert rec.recorder is not None++ assert spawn.call_args.kwargs.get("preexec_fn") is None, "recorder launch must not run a fork-time preexec_fn"+ argv = list(spawn.call_args.args)+ assert argv[:2] == [sys.executable, "-c"], "ffmpeg must be launched through the exec'd python trampoline"+ assert "prctl" in argv[2] and "os.execvp(sys.argv[2]" in argv[2], "trampoline sets PDEATHSIG then execs ffmpeg"+ # Parent passes its pid to catch a pre-exec reparent; trampoline-time getppid capture drops it (RED below).+ assert argv[3] == str(os.getpid()), "the parent must pass its own pid as the expected-parent argv"+ assert argv[4] == "ffmpeg", "ffmpeg must follow the trampoline and the expected-parent pid, passed as argv"+++async def test_sequential_owners_get_fresh_recorders_artifacts_and_paths(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ process_a = MagicMock(returncode=None)+ process_a.wait = AsyncMock()+ process_b = MagicMock(returncode=None)+ process_b.wait = AsyncMock()+ spawn = AsyncMock(side_effect=[process_a, process_b])+ monkeypatch.setattr(asyncio, "create_subprocess_exec", spawn)++ owner_a = await acquire_display_recorder(":99", "owner/a", tmp_path)+ assert owner_a.recorder is not None and owner_a.video_artifact is not None+ process_a.returncode = 0+ assert await release_display_recorder(owner_a.recorder) is True+ assert (":99", "owner/a") not in display_recorder._REGISTRY+ assert process_a.send_signal.call_count == 0+ assert process_a.terminate.call_count == 0+ assert process_a.kill.call_count == 0++ owner_b = await acquire_display_recorder(":99", "owner_a", tmp_path)+ assert owner_b.recorder is not None and owner_b.video_artifact is not None+ assert owner_b.recorder is not owner_a.recorder+ assert owner_b.video_artifact is not owner_a.video_artifact+ assert owner_a.video_artifact.video_path != owner_b.video_artifact.video_path+ assert Path(owner_a.video_artifact.video_path).name == (f"owner_a-{hashlib.sha256(b'owner/a').hexdigest()}.webm")+ assert Path(owner_b.video_artifact.video_path).name == (f"owner_a-{hashlib.sha256(b'owner_a').hexdigest()}.webm")+ assert spawn.await_count == 2++ process_b.returncode = 0+ assert await release_display_recorder(owner_b.recorder) is True+ assert display_recorder._REGISTRY == {}+++async def test_stop_escalates_reaps_and_releases_lock(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ process = MagicMock(returncode=None)++ async def wait() -> None:+ if process.kill.called:+ process.returncode = -signal.SIGKILL+ return+ await asyncio.Future()++ process.wait = wait+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))+ monkeypatch.setattr("skyvern.webeye.display_recorder.SIGINT_TIMEOUT", 0.001)+ monkeypatch.setattr("skyvern.webeye.display_recorder.SIGTERM_TIMEOUT", 0.001)+ monkeypatch.setattr("skyvern.webeye.display_recorder.STARTUP_READINESS_TIMEOUT", 0.001)++ acquired = await acquire_display_recorder(":99", "owner", tmp_path)+ recorder = acquired.recorder+ assert recorder is not None+ lock_path = recorder.lock_path_for_display(":99")++ assert await recorder.stop() is False+ assert await recorder.stop() is False+ process.send_signal.assert_called_once_with(signal.SIGINT)+ process.terminate.assert_called_once()+ process.kill.assert_called_once()+ lock_fd = os.open(lock_path, os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(lock_fd)+++async def test_start_failure_releases_display_lock(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(side_effect=OSError("missing ffmpeg")))++ failed = await acquire_display_recorder(":99", "owner-a", tmp_path)+ assert failed.recorder is None++ lock_path = DisplayRecorder.lock_path_for_display(":99")+ fd = os.open(lock_path, os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(fd)+++async def test_dead_different_owner_entry_does_not_block_next_run(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ process_a = MagicMock(returncode=None)+ process_a.wait = AsyncMock()+ process_b = MagicMock(returncode=None)+ process_b.wait = AsyncMock()+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(side_effect=[process_a, process_b]))++ first = await acquire_display_recorder(":99", "owner-a", tmp_path)+ assert first.recorder is not None+ # owner-a's recorder exits without a clean release (e.g. pod-local leak).+ process_a.returncode = 0++ second = await acquire_display_recorder(":99", "owner-b", tmp_path)+ assert second.recorder is not None+ assert second.started is True+ # The dead holder must not have been signalled while it was being pruned.+ assert process_a.kill.call_count == 0+ assert process_a.terminate.call_count == 0++ process_b.returncode = 0+ assert await release_display_recorder(second.recorder) is True+++async def test_stop_reaps_and_releases_lock_on_cancellation(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ process = MagicMock(returncode=None)++ async def wait() -> None:+ if process.kill.called:+ process.returncode = -signal.SIGKILL+ return+ await asyncio.Future()++ process.wait = wait+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))+ monkeypatch.setattr("skyvern.webeye.display_recorder.STARTUP_READINESS_TIMEOUT", 0.001)++ acquired = await acquire_display_recorder(":99", "owner", tmp_path)+ recorder = acquired.recorder+ assert recorder is not None++ task = asyncio.ensure_future(recorder.stop())+ await asyncio.sleep(0.01) # let stop() reach the SIGINT wait+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task++ # Cancellation must have reaped the child and released the lock before propagating.+ assert process.kill.called+ lock_fd = os.open(recorder.lock_path_for_display(":99"), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(lock_fd)+++async def test_stop_survives_double_cancel_and_still_releases_lock(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """A second cancel arriving while stop() is under the post-kill reap must not skip lock release."""+ killed = asyncio.Event()+ allow_reap = asyncio.Event()+ process = MagicMock(returncode=None)++ def _kill() -> None:+ killed.set()++ async def wait() -> int:+ await allow_reap.wait()+ process.returncode = -signal.SIGKILL+ return process.returncode++ process.kill = _kill+ process.wait = wait+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))+ monkeypatch.setattr("skyvern.webeye.display_recorder.STARTUP_READINESS_TIMEOUT", 0.001)+ monkeypatch.setattr("skyvern.webeye.display_recorder.SIGINT_TIMEOUT", 0.001)+ monkeypatch.setattr("skyvern.webeye.display_recorder.SIGTERM_TIMEOUT", 0.001)++ acquired = await acquire_display_recorder(":99", "owner", tmp_path)+ recorder = acquired.recorder+ assert recorder is not None++ task = asyncio.ensure_future(recorder.stop())+ await asyncio.sleep(0)+ task.cancel() # first cancel: escalation is abandoned, finally kills and reaps+ await killed.wait()+ task.cancel() # second cancel: lands while the post-kill reap is pending+ await asyncio.sleep(0)+ allow_reap.set() # let the reap complete+ with pytest.raises(asyncio.CancelledError):+ await task++ # Despite two cancels, the child was reaped and the lock is free for the next run.+ lock_fd = os.open(recorder.lock_path_for_display(":99"), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(lock_fd)+++async def test_release_holds_resources_until_serialized_stop_is_terminal_under_cancel(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """P2-A: a release caller cancelled while parked behind another release's serialized stop must not free the+ reservation/lock/registry until that stop is terminal — otherwise a different owner could acquire the display+ mid-shutdown. The cancelled caller re-raises exactly once, only after the stop completes and resources drop."""+ entered = asyncio.Event()+ allow = asyncio.Event()+ process = MagicMock(returncode=None)+ lock_fd = os.open(DisplayRecorder.lock_path_for_display(":99"), os.O_CREAT | os.O_RDWR, 0o600)+ recorder = DisplayRecorder(+ display=":99", owner_id="owner", process=process, lock_fd=lock_fd, video_artifact=MagicMock()+ )+ key = (recorder.display, recorder.owner_id)+ display_recorder._REGISTRY[key] = recorder++ async def _escalate(_graceful: bool) -> bool:+ entered.set()+ await allow.wait()+ process.returncode = 0+ return True++ monkeypatch.setattr(recorder, "_escalate_shutdown", _escalate)++ first = asyncio.ensure_future(release_display_recorder(recorder))+ await entered.wait() # first release's serialized stop holds _stop_lock, parked mid-shutdown+ second = asyncio.ensure_future(release_display_recorder(recorder))+ await asyncio.sleep(0) # park second behind _stop_lock+ second.cancel()+ await asyncio.sleep(0) # deliver the cancel while second is parked++ assert not recorder.is_stopped, "the serialized stop must still be in flight"+ assert recorder.lock_fd == lock_fd, "a cancelled parked release must not release the flock early"+ assert display_recorder._REGISTRY.get(key) is recorder, "the registry reservation must not drop early"++ allow.set()+ assert await first is True+ with pytest.raises(asyncio.CancelledError):+ await second++ assert recorder.is_stopped+ assert recorder.lock_fd == -1+ assert key not in display_recorder._REGISTRY+++async def test_dead_same_owner_fails_closed_and_preserves_exact_artifact(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """An exited SAME-owner recorder must not be restarted (-y overwrites the partial WebM and mints a second+ RECORDING row); reuse the exact prior artifact object instead."""+ process = MagicMock(returncode=None)+ process.wait = AsyncMock()+ spawn = AsyncMock(return_value=process)+ monkeypatch.setattr(asyncio, "create_subprocess_exec", spawn)++ first = await acquire_display_recorder(":99", "owner", tmp_path)+ assert first.recorder is not None+ first.video_artifact.video_artifact_id = "va_1"+ process.returncode = 1 # the recorder died mid-run++ second = await acquire_display_recorder(":99", "owner", tmp_path)+ assert second.recorder is first.recorder+ assert second.video_artifact is first.video_artifact+ assert second.video_artifact.video_artifact_id == "va_1"+ assert second.started is False+ assert spawn.await_count == 1 # never restarted+++async def test_startup_readiness_detects_early_ffmpeg_exit(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ """ffmpeg that spawns then dies (bad x11grab init) must not seed an artifact or leak the lock."""+ process = MagicMock(returncode=None)++ async def wait() -> int:+ process.returncode = 1 # exits shortly after spawn+ return 1++ process.wait = wait+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))++ acquisition = await acquire_display_recorder(":99", "owner", tmp_path)+ assert acquisition.recorder is None+ assert acquisition.video_artifact is None++ lock_fd = os.open(DisplayRecorder.lock_path_for_display(":99"), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(lock_fd)+++async def test_stale_release_does_not_evict_replacement(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:+ """A delayed release of a superseded recorder must not remove the replacement under the same key."""+ process_a = MagicMock(returncode=None)+ process_a.wait = AsyncMock()+ process_b = MagicMock(returncode=None)+ process_b.wait = AsyncMock()+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(side_effect=[process_a, process_b]))++ a = await acquire_display_recorder(":99", "owner", tmp_path)+ process_a.returncode = 0+ await release_display_recorder(a.recorder) # A leaves the registry++ b = await acquire_display_recorder(":99", "owner", tmp_path) # B registers under the same key+ assert b.recorder is not a.recorder # B is a distinct recorder+ assert b.started is True++ process_a.returncode = 0+ await release_display_recorder(a.recorder) # delayed duplicate release of A — must not evict B++ reuse = await acquire_display_recorder(":99", "owner", tmp_path)+ assert reuse.recorder is b.recorder+ assert reuse.video_artifact is b.video_artifact++ process_b.returncode = 0+ await release_display_recorder(b.recorder)+++async def test_acquire_unwind_propagates_cancel_during_plain_exception_reap(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Startup fails with an ordinary Exception while a (repeated) cancel lands mid-reap: the unwind must still+ reap, release the fd, and PROPAGATE the cancellation rather than swallow it into a fail-closed acquisition."""+ allow_reap = asyncio.Event()+ killed = asyncio.Event()+ calls = {"n": 0}+ process = MagicMock(returncode=None)++ def _kill() -> None:+ killed.set()++ async def wait() -> int:+ calls["n"] += 1+ if calls["n"] == 1:+ raise RuntimeError("readiness probe boom") # ordinary-Exception unwind, process still alive+ await allow_reap.wait()+ process.returncode = -signal.SIGKILL+ return process.returncode++ process.kill = _kill+ process.wait = wait+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))++ task = asyncio.ensure_future(acquire_display_recorder(":99", "owner", tmp_path))+ await killed.wait() # unwind entered, kill sent, reap now blocked+ task.cancel()+ await asyncio.sleep(0)+ task.cancel() # repeated cancel during the blocked reap+ await asyncio.sleep(0)+ allow_reap.set()+ with pytest.raises(asyncio.CancelledError):+ await task++ lock_fd = os.open(DisplayRecorder.lock_path_for_display(":99"), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(lock_fd)+++async def test_healthy_recorder_survives_readiness_wait_timeout(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """A healthy ffmpeg does not exit inside the readiness window, so wait_for times out and cancels that+ initial Process.wait. The recorder must still be seeded AND remain fully stoppable/finalizable after."""+ exited = asyncio.Event()+ process = MagicMock(returncode=None)++ def _sigint(_sig: signal.Signals) -> None:+ process.returncode = 0+ exited.set()++ async def wait() -> int:+ await exited.wait()+ return process.returncode or 0++ process.send_signal = MagicMock(side_effect=_sigint)+ process.wait = wait+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))+ monkeypatch.setattr("skyvern.webeye.display_recorder.STARTUP_READINESS_TIMEOUT", 0.02)++ acquisition = await acquire_display_recorder(":99", "owner", tmp_path)+ assert acquisition.recorder is not None+ assert acquisition.started is True # readiness timed out (healthy) → seeded++ # The timed-out/cancelled initial wait() must not have broken later shutdown: stop finalizes cleanly.+ assert await release_display_recorder(acquisition.recorder) is True+ process.send_signal.assert_called_once_with(signal.SIGINT)++ lock_fd = os.open(acquisition.recorder.lock_path_for_display(":99"), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(lock_fd)+++def _seed(video_root: Path, owner_id: str, date: str) -> Path:+ date_dir = video_root / date+ date_dir.mkdir(parents=True, exist_ok=True)+ path = date_dir / f"{display_recorder._safe_owner_id(owner_id)}.webm"+ path.write_bytes(b"webm")+ return path+++def test_unlink_recordings_removes_only_owner_digest_across_date_dirs(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ monkeypatch.setattr(display_recorder.settings, "VIDEO_PATH", str(tmp_path))+ own_day1 = _seed(tmp_path, "owner", "2026-08-26")+ own_day2 = _seed(tmp_path, "owner", "2026-08-27")+ sibling = _seed(tmp_path, "other-owner", "2026-08-27")+ playwright_style = tmp_path / "2026-08-27" / "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0.webm"+ playwright_style.write_bytes(b"pw")++ removed = display_recorder.unlink_recordings_for_owner("owner")++ assert removed == 2+ assert not own_day1.exists()+ assert not own_day2.exists()+ assert sibling.exists(), "a sibling owner's digest must survive"+ assert playwright_style.exists(), "a Playwright random-name recording must survive"++[email protected]("root", [None, "/nonexistent/video/root"])+def test_unlink_recordings_missing_or_unset_root_is_log_only(monkeypatch: pytest.MonkeyPatch, root: str | None) -> None:+ monkeypatch.setattr(display_recorder.settings, "VIDEO_PATH", root)+ # No raise, no file system requirement — a missing/unset root simply removes nothing.+ assert display_recorder.unlink_recordings_for_owner("owner") == 0+++def test_resolve_owner_id_accepts_browser_session_id_as_lowest_priority() -> None:+ assert resolve_owner_id(browser_session_id="bs_1") == "bs_1"+ assert resolve_owner_id(task_id="tsk_1", browser_session_id="bs_1") == "tsk_1"+ assert (+ resolve_owner_id(workflow_run_id="wr_1", task_id="tsk_1", script_id="scr_1", browser_session_id="bs_1")+ == "wr_1"+ )+++def test_owner_id_override_is_fallback_only() -> None:+ """The reconnect override preserves the standalone owner ONLY when no canonical id is present; any explicit+ canonical owner wins so a stray/hostile override can't weaken isolation (RED if the override resolves first)."""+ assert resolve_owner_id(owner_id_override="ovr") == "ovr"+ for field in ("workflow_run_id", "task_id", "script_id", "browser_session_id"):+ assert resolve_owner_id(owner_id_override="ovr", **{field: "canon"}) == "canon"+++async def _acquire_live(display: str, owner: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> DisplayRecorder:+ process = MagicMock(returncode=None)++ async def wait() -> None:+ if process.kill.called:+ process.returncode = -signal.SIGKILL+ return+ await asyncio.Future()++ process.wait = wait+ monkeypatch.setattr(asyncio, "create_subprocess_exec", AsyncMock(return_value=process))+ monkeypatch.setattr("skyvern.webeye.display_recorder.SIGINT_TIMEOUT", 0.001)+ monkeypatch.setattr("skyvern.webeye.display_recorder.SIGTERM_TIMEOUT", 0.001)+ monkeypatch.setattr("skyvern.webeye.display_recorder.STARTUP_READINESS_TIMEOUT", 0.001)+ acquired = await acquire_display_recorder(display, owner, tmp_path)+ assert acquired.recorder is not None+ return acquired.recorder+++async def test_stop_display_recorders_for_owner_reaps_live_recorder_and_clears_registry(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ # B2: a cancelled activity leaves a live ffmpeg + registry entry + display lock; the owner-scoped stop+ # must reap it BEFORE the unlink so it is not orphaned into the next activity on this worker.+ recorder = await _acquire_live(":99", "wr_cancelled", tmp_path, monkeypatch)+ assert (":99", "wr_cancelled") in display_recorder._REGISTRY++ released = await display_recorder.stop_display_recorders_for_owner("wr_cancelled")++ assert released == 1+ assert recorder.process.kill.called, "the surviving ffmpeg must be forcibly reaped"+ assert (":99", "wr_cancelled") not in display_recorder._REGISTRY, "the registry entry must be dropped"+ # The display lock must be free for the next run.+ lock_fd = os.open(recorder.lock_path_for_display(":99"), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ finally:+ os.close(lock_fd)+++async def test_stop_display_recorders_for_owner_leaves_other_owners_alone(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ mine = await _acquire_live(":99", "wr_mine", tmp_path, monkeypatch)+ theirs = await _acquire_live(":98", "wr_theirs", tmp_path, monkeypatch)++ released = await display_recorder.stop_display_recorders_for_owner("wr_mine")++ assert released == 1+ assert mine.process.kill.called+ assert (":98", "wr_theirs") in display_recorder._REGISTRY, "a different owner's live recorder must survive"+ assert not theirs.process.kill.called+++async def test_stop_display_recorders_for_owner_absent_is_noop() -> None:+ assert await display_recorder.stop_display_recorders_for_owner("wr_absent") == 0+++async def test_stop_display_recorders_for_owner_absorbs_cancellation_and_clears_registry(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ # stop() re-raises a single cancellation AFTER it has reaped and released; the owner-scoped helper+ # must absorb it (so the caller's unlink still runs) yet still drop the registry entry.+ recorder = await _acquire_live(":99", "wr_cancel_in_stop", tmp_path, monkeypatch)++ async def _cancel_stop() -> bool:+ recorder._release_lock()+ key = (recorder.display, recorder.owner_id)+ display_recorder._REGISTRY.pop(key, None)+ raise asyncio.CancelledError()++ monkeypatch.setattr(recorder, "stop", _cancel_stop)++ released = await display_recorder.stop_display_recorders_for_owner("wr_cancel_in_stop")++ assert released == 1+ assert (":99", "wr_cancel_in_stop") not in display_recorder._REGISTRYdiff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex 3ca2c13b6..16f029f83 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -28,6 +28,7 @@ from skyvern.webeye.browser_engine import (BrowserEngineSelection,)from skyvern.webeye.browser_factory import set_popup_video_listener+from skyvern.webeye.display_recorder import DisplayRecorderfrom skyvern.webeye.real_browser_manager import RealBrowserManager, _PersistentSessionLeasefrom skyvern.webeye.real_browser_state import RealBrowserState@@ -828,6 +829,161 @@ async def test_get_video_artifacts_empty_finalize_path_warns() -> None:mock_log.warning.assert_called_once()+def _make_browser_state_with_display_recorder(video_path: str) -> tuple[MagicMock, VideoArtifact]:+ artifact = VideoArtifact(video_path=video_path)+ recorder = DisplayRecorder(+ display=":99",+ owner_id="owner",+ process=MagicMock(),+ lock_fd=-1,+ video_artifact=artifact,+ )+ browser_state = MagicMock()+ browser_state.browser_artifacts.video_artifacts = [artifact]+ browser_state.browser_artifacts._display_recorder = recorder+ return browser_state, artifact+++async def test_display_recording_index0_real_seams_first_step_two_updates_terminal_same_id(+ tmp_path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """V2 parity through the real consumer wiring: the whole-display recording is the single index-0+ ArtifactType.RECORDING, created ONCE on the first step, then two real per-step syncs and the terminal+ update that SAME id (never a duplicate/last-step row). Only the DB/storage artifact boundary is faked."""+ from skyvern.forge import app as forge_app+ from skyvern.forge.agent import ForgeAgent+ from skyvern.forge.sdk.artifact.models import ArtifactType++ src = tmp_path / "whole-display.webm"+ src.write_bytes(b"step1")+ browser_state, display_artifact = _make_browser_state_with_display_recorder(str(src))+ manager = RealBrowserManager()+ manager.pages["tsk_int"] = browser_state+ task = MagicMock(task_id="tsk_int", organization_id="org", workflow_run_id=None)++ creates: list[dict] = []+ updates: list[dict] = []++ async def _create(*, step, artifact_type, data=None, path=None, file_extension=None):+ creates.append({"step": step, "type": artifact_type})+ return "rec_0"++ async def _update(*, artifact_id, organization_id, data, file_extension=None):+ updates.append({"id": artifact_id, "data": data, "ext": file_extension})+ return "upload_key"++ artifact_mgr = MagicMock(+ create_artifact=AsyncMock(side_effect=_create), update_artifact_data=AsyncMock(side_effect=_update)+ )+ monkeypatch.setattr(forge_app, "ARTIFACT_MANAGER", artifact_mgr)+ monkeypatch.setattr(forge_app, "BROWSER_MANAGER", manager)+ (tmp_path / "f.mp4").write_bytes(b"final-mp4")+ monkeypatch.setattr(+ real_browser_manager,+ "prepare_recording_for_upload",+ lambda p: fake_prepared_recording(p, str(tmp_path / "f.mp4")),+ )+ first_step, last_step = MagicMock(step_id="s0"), MagicMock(step_id="s9")++ # First-step init seam: real get_video_artifacts -> create the RECORDING row on the FIRST step.+ for a in await manager.get_video_artifacts(task_id="tsk_int", browser_state=browser_state, finalize=False):+ if a.video_artifact_id is None:+ a.video_artifact_id = await artifact_mgr.create_artifact(+ step=first_step, artifact_type=ArtifactType.RECORDING, data=a.video_data+ )+ manager.set_video_artifact_for_task(task, browser_state.browser_artifacts.video_artifacts)++ agent = ForgeAgent()+ src.write_bytes(b"step1-step2")+ await agent._sync_video_artifact_after_step(task, browser_state) # real per-step update #1+ src.write_bytes(b"step1-step2-step3")+ await agent._sync_video_artifact_after_step(task, browser_state) # real per-step update #2++ # Terminal seam (mirrors cleanup_browser_and_create_artifacts' inline video loop; the full method also does+ # HAR/logs/webhooks, out of scope): real finalize read updates the SAME id, never creates a last-step row.+ for a in await manager.get_video_artifacts(task_id="tsk_int", browser_state=browser_state, finalize=True):+ if a.video_artifact_id:+ await artifact_mgr.update_artifact_data(+ artifact_id=a.video_artifact_id,+ organization_id=task.organization_id,+ data=a.video_data,+ file_extension=a.video_file_extension,+ )+ else:+ a.video_artifact_id = await artifact_mgr.create_artifact(+ step=last_step, artifact_type=ArtifactType.RECORDING, data=a.video_data+ )++ recording_creates = [c for c in creates if c["type"] == ArtifactType.RECORDING]+ assert len(recording_creates) == 1, "exactly one index-0 RECORDING row, created once"+ assert recording_creates[0]["step"] is first_step, "the recording is attached to the FIRST step, never last_step"+ assert display_artifact.video_artifact_id == "rec_0"+ assert len(updates) == 3, "two real per-step syncs + the terminal, all real update_artifact_data calls"+ assert all(u["id"] == "rec_0" for u in updates), "every update targets the same first-step artifact id"+ assert [updates[0]["data"], updates[1]["data"]] == [b"step1-step2", b"step1-step2-step3"], "growing per-step bytes"+ assert updates[2]["data"] == b"final-mp4" and updates[2]["ext"] == "mp4", "terminal finalized bytes/extension"+ assert browser_state.browser_artifacts.video_artifacts == [display_artifact], "cardinality one; no duplicate row"+++async def _stopped_display_recorder(owner_id: str, video_path: str) -> tuple[DisplayRecorder, VideoArtifact]:+ """A run-owned recorder wrapping a fake ffmpeg that a graceful stop() reaps to returncode 0."""+ artifact = VideoArtifact(video_path=video_path)+ process = MagicMock(returncode=None)++ async def _wait() -> int:+ process.returncode = 0+ return 0++ process.wait = AsyncMock(side_effect=_wait)+ recorder = DisplayRecorder(display=":99", owner_id=owner_id, process=process, lock_fd=-1, video_artifact=artifact)+ return recorder, artifact+++ "recorder_owner, task_ids, expect_stopped",+ [("wfr_stream", [], True), ("tsk_child", ["tsk_child"], True), ("wfr_parent", [], False)],+)+async def test_deferred_stream_close_finalizes_display_recording_before_persist(+ tmp_path, monkeypatch: pytest.MonkeyPatch, recorder_owner: str, task_ids: list[str], expect_stopped: bool+) -> None:+ """Deferred close finalizes a workflow- or task-owned recorder, never a foreign parent."""+ src = tmp_path / "whole-display.webm"+ src.write_bytes(b"raw-webm")+ recorder, artifact = await _stopped_display_recorder(recorder_owner, str(src))++ manager = RealBrowserManager()+ browser_state = MagicMock()+ browser_state.browser_artifacts.traces_dir = None+ browser_state.browser_artifacts.browser_session_dir = "/tmp/fake_profile"+ browser_state.browser_artifacts.video_artifacts = [artifact]+ browser_state.browser_artifacts._display_recorder = recorder+ browser_state.close = AsyncMock()+ manager.pages["wfr_stream"] = browser_state++ monkeypatch.setattr(real_browser_manager, "persist_session_cookies", AsyncMock())+ monkeypatch.setattr(real_browser_manager, "stream_ref_active", lambda wrid: True)+ monkeypatch.setattr(real_browser_manager, "set_deferred_close_params", lambda *a, **k: True)+ prepared = tmp_path / "whole-display.mp4"+ prepared.write_bytes(b"final-mp4")+ monkeypatch.setattr(+ real_browser_manager, "prepare_recording_for_upload", lambda path: fake_prepared_recording(path, str(prepared))+ )++ result = await manager.cleanup_for_workflow_run("wfr_stream", task_ids=task_ids, close_browser_on_completion=True)++ # Defer semantics preserved (browser NOT closed, recording_finalized False). Only a run-owned recorder+ # is stopped, so its WebM is remuxed+uploaded; a foreign owner is left running and uploads its raw prefix.+ browser_state.close.assert_not_awaited()+ assert result.recording_finalized is False+ assert recorder.is_stopped is expect_stopped+ artifacts = await manager.get_video_artifacts(browser_state=browser_state, finalize=result.recording_finalized)+ assert artifact in artifacts+ assert artifact.video_data == (b"final-mp4" if expect_stopped else b"raw-webm")+ assert artifact.video_file_extension == ("mp4" if expect_stopped else "webm")++def _make_page_mock(video_path: str | None) -> MagicMock:page = MagicMock()if video_path is None:diff --git a/tests/unit/test_real_browser_state_on_close_hook.py b/tests/unit/test_real_browser_state_on_close_hook.pyindex 63aed8e96..20a7708ee 100644--- a/tests/unit/test_real_browser_state_on_close_hook.py+++ b/tests/unit/test_real_browser_state_on_close_hook.py@@ -7,10 +7,12 @@ state so any caller of ``close()`` stops the publisher implicitly.from __future__ import annotationsfrom typing import Any-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, MagicMock, patchimport pytest+from skyvern.webeye.browser_artifacts import BrowserArtifacts, VideoArtifact+from skyvern.webeye.display_recorder import DisplayRecorderfrom skyvern.webeye.real_browser_state import RealBrowserState@@ -23,6 +25,16 @@ def _bare_state() -> RealBrowserState:return RealBrowserState(pw=pw_stub, browser_context=None)+def _recorder(owner_id: str = "owner") -> DisplayRecorder:+ return DisplayRecorder(+ display=":99",+ owner_id=owner_id,+ process=MagicMock(),+ lock_fd=-1,+ video_artifact=VideoArtifact(video_path="recording.webm"),+ )++@pytest.mark.asyncioasync def test_close_runs_registered_callbacks_in_order() -> None:state = _bare_state()@@ -77,3 +89,77 @@ async def test_close_swallows_callback_errors() -> None:# Must not raise — close() is the universal teardown path.await state.close()assert later_ran is True+++def _state_with_recorder(events: list[str]) -> RealBrowserState:+ artifacts = BrowserArtifacts()+ artifacts._display_recorder = _recorder()+ state = RealBrowserState(pw=MagicMock(), browser_context=MagicMock(), browser_artifacts=artifacts)++ async def cleanup() -> None:+ events.append("provider")++ state._run_browser_cleanup_bounded = cleanup # type: ignore[method-assign]+ state._stop_driver_bounded = AsyncMock() # type: ignore[method-assign]+ return state++[email protected]("stop_result", [True, False])+async def test_display_recorder_outcome_does_not_change_close_result(stop_result: bool) -> None:+ """B4: ``close()``'s return reports only the browser-context teardown (which gates profile+ persistence). A recorder that exits non-zero mid-run — ``release_display_recorder`` returning False+ — must NOT flip that result and suppress an otherwise-clean run's profile write-back."""+ events: list[str] = []+ state = _state_with_recorder(events)++ async def teardown() -> None:+ events.append("context")++ state._teardown_context = teardown # type: ignore[method-assign]++ async def stop(_: DisplayRecorder) -> bool:+ events.append("recorder")+ return stop_result++ with patch("skyvern.webeye.real_browser_state.release_display_recorder", side_effect=stop):+ assert await state.close() is True++ assert events == ["context", "recorder", "provider"]+++async def test_context_teardown_failure_still_finalizes_display_recorder() -> None:+ """B4: a Playwright context-teardown failure must still stop/finalize the WebM (so upload prep /+ MP4 conversion can run) even though ``close()`` reports the teardown failure for profile gating."""+ events: list[str] = []+ state = _state_with_recorder(events)++ async def teardown() -> None:+ events.append("context")+ raise RuntimeError("context close blew up")++ state._teardown_context = teardown # type: ignore[method-assign]++ async def stop(_: DisplayRecorder) -> bool:+ events.append("recorder")+ return True++ with patch("skyvern.webeye.real_browser_state.release_display_recorder", side_effect=stop):+ assert await state.close() is False++ assert "recorder" in events, "the recorder must still be finalized after a context-teardown failure"+ assert events == ["context", "recorder", "provider"]+++async def test_close_false_leaves_display_recorder_running() -> None:+ artifacts = BrowserArtifacts()+ artifacts._display_recorder = _recorder()+ state = RealBrowserState(pw=MagicMock(), browser_context=MagicMock(), browser_artifacts=artifacts)+ state._stop_driver_bounded = AsyncMock() # type: ignore[method-assign]++ with patch("skyvern.webeye.real_browser_state.release_display_recorder", new_callable=AsyncMock) as stop:+ assert await state.close(close_browser_on_completion=False, release_driver=False) is False++ stop.assert_not_awaited()