Skip to main content

agentmux_mcp/
window_blackout.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The blackout of a `UI` window capture of an AgentMux window
5//! (docs/specs/SPEC_DEEP_LINKS_ONE_FRAMEWORK_2026_11_20.md §7, G5).
6//!
7//! Each pane or panel showing private or secret content (the Vault, the Audit, Memory, a browser pane in the
8//! person's own profile, and the panels listed in the app's `capture-mask.ts`) is blacked out by its rectangle, and
9//! the rest of the window is captured. The rectangles and the frame come from the same instant: srv asks every window
10//! for its rectangles just before the frame (`begin`) and just after (`end`); the capture is refused if the window
11//! did not answer, if its layout moved or changed in between, if it says it can't give them, or if the frame's size
12//! or scale differs from the one they were measured for. Nothing is ever blacked out from rectangles of another
13//! moment.
14
15use anyhow::Result;
16use serde::Deserialize;
17
18/// A rectangle in the window's viewport, in CSS pixels.
19#[derive(Debug, Clone, Copy, Deserialize, Default, PartialEq)]
20pub(crate) struct MaskRect {
21    pub(crate) x: f64,
22    pub(crate) y: f64,
23    pub(crate) w: f64,
24    pub(crate) h: f64,
25}
26
27/// One window's answer, as srv's `POST /api/v1/ui/capture_mask` gives it.
28#[derive(Debug, Clone, Deserialize, Default, PartialEq)]
29pub(crate) struct WindowMask {
30    pub(crate) window_id: String,
31    pub(crate) title: String,
32    pub(crate) width: f64,
33    pub(crate) height: f64,
34    pub(crate) scale: f64,
35    #[serde(default)]
36    pub(crate) rects: Vec<MaskRect>,
37    #[serde(default)]
38    pub(crate) moving: bool,
39    #[serde(default)]
40    pub(crate) changed: bool,
41    #[serde(default)]
42    pub(crate) refuse: Option<String>,
43}
44
45/// What to do instead, said with every refusal.
46const INSTEAD: &str = "try again in a moment, or use UI screenshot of your own pane";
47
48/// What the host adds to a window's title on Linux when it runs without the sandbox
49/// (`crates/cef/src/client/display.rs`); the page's own title, which the window reports, does not have it.
50pub(crate) const UNSANDBOXED_SUFFIX: &str = " — Sandbox Disabled";
51
52/// Whether the operating system's `title` for a window is the page title `page` the window reported.
53fn same_title(title: &str, page: &str) -> bool {
54    title == page || title.strip_suffix(UNSANDBOXED_SUFFIX) == Some(page)
55}
56
57/// Refuses when more than one of this AgentMux's own windows has the target's title (`same_title` counts the
58/// windows on screen, the target included), whichever of them answered: an answer is found by its title.
59pub(crate) fn unique_title(same_title: usize, title: &str) -> Result<()> {
60    if same_title > 1 {
61        anyhow::bail!(
62            "{same_title} AgentMux windows have the title {title:?}, so their private panes can't be told apart and neither is captured: rename one, or use UI screenshot of your own pane"
63        );
64    }
65    Ok(())
66}
67
68/// The answer for the window titled `title`, out of every window's answer to `begin`, if it may be captured now.
69pub(crate) fn before<'a>(masks: &'a [WindowMask], title: &str) -> Result<&'a WindowMask> {
70    let mine: Vec<&WindowMask> = masks.iter().filter(|m| same_title(title, &m.title)).collect();
71    let mask = match mine.as_slice() {
72        [] => anyhow::bail!("this AgentMux window did not give the rectangles of its private panes, so it is not captured: {INSTEAD}"),
73        [one] => *one,
74        _ => anyhow::bail!(
75            "{} AgentMux windows have the title {title:?}, so their private panes can't be told apart and neither is captured: rename one, or use UI screenshot of your own pane",
76            mine.len()
77        ),
78    };
79    if let Some(why) = &mask.refuse {
80        anyhow::bail!("this AgentMux window can't be captured now: {why}; {INSTEAD}");
81    }
82    if mask.moving {
83        anyhow::bail!("this AgentMux window's layout is moving, so it is not captured: {INSTEAD}");
84    }
85    Ok(mask)
86}
87
88/// Whether the frame (`width` × `height` device pixels) may be blacked out by `begin`'s rectangles, given every
89/// window's answer to `end`.
90pub(crate) fn after(begin: &WindowMask, ends: &[WindowMask], width: u32, height: u32) -> Result<()> {
91    let Some(end) = ends.iter().find(|m| m.window_id == begin.window_id) else {
92        anyhow::bail!("this AgentMux window did not answer after the frame was taken, so it is not captured: {INSTEAD}");
93    };
94    if end.changed || end.moving || end.refuse.is_some() || end.rects != begin.rects {
95        anyhow::bail!("this AgentMux window's layout changed while the frame was taken, so it is not captured: {INSTEAD}");
96    }
97    let same_geometry = end.width == begin.width && end.height == begin.height && end.scale == begin.scale;
98    let near = |frame: u32, css: f64| (frame as f64 - css * begin.scale).abs() <= 2.0;
99    if !same_geometry || !near(width, begin.width) || !near(height, begin.height) {
100        anyhow::bail!(
101            "the frame ({width}×{height}) is not the size or scale the rectangles were measured for ({:.0}×{:.0} at {}), so it is not captured: {INSTEAD}",
102            begin.width * begin.scale,
103            begin.height * begin.scale,
104            begin.scale
105        );
106    }
107    Ok(())
108}
109
110/// Black out each of `mask`'s rectangles in `image`, a pixel wider on every side so no edge of the content shows.
111pub(crate) fn black_out(image: &mut image::RgbaImage, mask: &WindowMask) {
112    let (w, h) = (image.width() as i64, image.height() as i64);
113    for r in &mask.rects {
114        let x0 = ((r.x * mask.scale).floor() as i64 - 1).clamp(0, w);
115        let y0 = ((r.y * mask.scale).floor() as i64 - 1).clamp(0, h);
116        let x1 = (((r.x + r.w) * mask.scale).ceil() as i64 + 1).clamp(0, w);
117        let y1 = (((r.y + r.h) * mask.scale).ceil() as i64 + 1).clamp(0, h);
118        for y in y0..y1 {
119            for x in x0..x1 {
120                image.put_pixel(x as u32, y as u32, image::Rgba([0, 0, 0, 255]));
121            }
122        }
123    }
124}
125
126/// The refusal for an AgentMux window of another AgentMux, which this one can't ask for its rectangles.
127pub(crate) const OTHER_INSTANCE: &str = "that window belongs to another AgentMux, which this one can't ask for the rectangles of its private panes, so it is not captured: capture it from an agent of that AgentMux, or use UI screenshot of your own pane";
128
129/// The refusal for a container agent, which has no access to the host's screen.
130pub(crate) const IN_CONTAINER: &str = "a container agent can't capture the host's windows: use UI screenshot of your own pane, or of a browser pane you opened";
131
132#[cfg(test)]
133#[path = "tests/window_blackout_tests.rs"]
134mod tests;