Galaxy Shell / docs
Docs / Arquitetura e desenvolvimento

Capturing the real composition of the shared session

docs/capture-tty4.md · Editar no GitHub ↗

Esta página ainda não tem tradução em português. O conteúdo abaixo está em inglês.

Nesta página

Capturing the real composition of the shared session

Doc status: current contract. The Smithay capture (scripts/capture-desktop.py) is current; the River and grim parts are history. Checked 2026-11-10.

The name of this file keeps the historical reference to TTY4. Discover the current session and backend before you use the procedure; do not assume River, TTY4 or screencopy support. In the direct Smithay proof of 2026-10-27, grim reported that wlr-screencopy-unstable-v1 is absent. The private bridge now offers its own native capture, described below; the grim protocol is still absent. An old compositor that is already open gets this capability only at the next login.

Flow proven on 2026-10-26: grim 1.4.0, obtained from the Ubuntu repository without sudo, captured the real River desktop at 1366x768 through the Wayland socket of the TTY4 session. The image included the wallpaper, the Electron shell, the controls and the WM clock. This makes it possible to inspect the native composition, in addition to the browser connected to the core.

Smithay's own capture

After you discover the real session/socket and follow the coordination below, use the updated checkout:

python3 scripts/capture-desktop.py --socket /real/path/to/control.sock /tmp/desktop-real.png

The socket is AGENT_NATIVE_SMITHAY_SOCKET in the environment of the core. You can omit the argument when this variable is already available. Do not print the whole environment. The operation does not activate VTs, does not restart processes and does not open an additional compositor/host. If the session is inactive, there is no guarantee of a frame: the client ends with a timeout and tells you to check the existing target.

The typed capture command of the Unix bridge asks for a read of the next composed framebuffer. It includes windows, wallpaper, panels and cursor according to the current scene, with the same transforms used in presentation. The initial response confirms only the request. Check state.capture: generation identifies the request, pending indicates waiting, ready confirms the complete image and error reports a failure. Only ready provides path. The client waits for this state for up to ten seconds, refuses a replaced generation and converts the PPM to PNG without additional dependencies.

Since 2026-11-02 (user screenshots, see screenshots) the command takes two optional fields. cursor: false leaves the pointer out of that one frame. window_id renders the surfaces of one application window alone, offscreen, at its geometry and scale 1, and writes a PAM (P7, RGBA, straight alpha) under the same file name; state.capture.window names that window while it is pending. The compositor refuses a shell window, an unknown window, an unmapped window and any capture while locked. The command clipboard_image reads clipboard.png from the same private directory once, removes it and offers it as the image/png clipboard selection. The snapshot reports capabilities.screenshots: true.

The compositor reads pixels only on request, with a limit of 16 megapixels. Capture errors stay in the status and do not end the compositor. The destination is fixed in the private directory of the socket, with a 0600 file and publishing by rename; the compositor accepts no path from the client. The last image stays there until another capture or the normal shutdown of the compositor. The client creates the 0600 PNG and refuses to overwrite existing files. Pixels do not pass through the state JSON, logs or the renderer. The authority stays restricted to the private socket of the same user and to the authenticated maintenance tools, with no access from generated HTML.

Validation on 2026-10-27: a read of a real GLES framebuffer with red/blue stripes (that GLES test was deleted at step 8 of openspec/changes/archive/2026-11-10-gles-removal-plan/design.md; its mirror is native_wgpu_framebuffer_readback and native_wgpu_framebuffer_readback_on_gl in capture.rs) verified the channels and the orientation in the Normal (DRM) and Flipped180 (nested) transforms, without opening a Wayland session. Tests also cover the serialization of requests, invalid dimensions, PNG, permissions and the RPC client waiting for the frame. This verifies the GPU and file path, but it is not a capture of the shared DRM session. Visual acceptance of the desktop depends on the next login with the new compositor; the existing session was preserved.

Show to the agent (window capture from the protected host)

The protected host composer has a "Mostrar ao agente" button. One click attaches a capture of one real application window to the next message. The core reuses the capture command above; the compositor did not change. The core crops the composed output to the window and sends nothing to the model until the user sends the message.

Contract:

  • Route: POST /api/window-capture with an empty object, and POST /api/window-capture/discard with {id}. Both use the same authentication as the other /api/ host routes (X-Shell-Token, same origin). There is no agent tool and no MCP entry for capture. Generated HTML runs in opaque iframes without the token and never receives the button or the route.
  • Target: the focused application window (not a shell window). Clicking a host control moves keyboard focus to the shell, so the core also remembers the last application window that held focus (lastApplicationFocus in core/smithay.js). It uses that window only while it is still visible, not minimized, on the active workspace and in the same compositor instance.
  • Refusals: the core refuses, and the host shows the reason on the disabled button, when the compositor is not connected, does not offer capture yet, shows the workspace overview or a thumbnail animation, has no focused application window, has more than one output and the focused window is not on the primary output (or no output has the primary role), or has another visible window (not the wallpaper, not a dialog of the same application) over the target rectangle. It also refuses when the window geometry changes between the request and the frame, when another capture replaces the generation, and when the frame size differs from the output size.
  • Geometry: the crop uses the presentation rectangle (what the user sees), clipped to the output. In the chat presentation an application is a scaled preview, so the capture has the preview resolution. A per-window compositor capture at the native size is not implemented.
  • Storage: the cropped PNG is written to <data dir>/captures/<random id>.png with mode 0600 in a 0700 directory. At most one capture exists. A new capture, a discard, a dispatch, an expiry of ten minutes, a core shutdown and the next core start remove it. The core deletes the compositor's full-output capture.ppm right after it reads the frame, because that frame shows every window. Pixels never enter the state JSON or the logs; the host gets only a downscaled thumbnail (at most 320x200) in the response of the capture request.
  • Sending: POST /api/send accepts an optional attachment id. The core reads the PNG and sends it to OpenCode as one inline files entry (mime: image/png) in the same session.prompt call as the text. The selected model must list image input in its OpenCode capabilities (images: true in /api/state models). Otherwise the send fails with a clear message, the text is not sent and the thumbnail stays in the host. There is no text fallback. A capture belongs to the conversation that was active when it was taken. The core deletes its file after OpenCode accepts the prompt; OpenCode keeps its own copy in the session history inside the profile.

Evidence: unit tests (test/window-capture.test.js), a synthetic core with a fake compositor socket and a synthetic OpenCode (test/window-capture-http.test.js), and a browser check of the host button against that synthetic core. Not proven: the real compositor frame with the new route, Electron focus behavior when the button is clicked, an image-capable provider model, and the visual result of the chat presentation scaling.

Identify the target

Read AGENTS.md and the shared coordination.log. Check loginctl list-sessions, the compositor/Electron/core processes and their supervisors. Check systemctl status agent-native-tty4-test.service only as a historical hint. The unit may not exist: confirm the process that is actually running and its release, without starting another desktop.

Get WAYLAND_DISPLAY and XDG_RUNTIME_DIR from the environment of the core of the identified session, reading only these variables. Do not print the full environment, which can contain credentials. Confirm the existing socket in /run/user/UID. In the proof, the logind session was 56 and the socket was wayland-1; these values change after the session is recreated and must not be treated as constants.

Provide the client

Use grim if it is already installed. Otherwise, this procedure uses the official Ubuntu package as a temporary client, without a global install:

umask 077
mkdir -p /tmp/agent-native-screen-capture
cd /tmp/agent-native-screen-capture
apt-get download grim
for package in grim_*.deb; do
  dpkg-deb -x "$package" runtime
done
./runtime/usr/bin/grim -h

The proven path was /tmp/agent-native-screen-capture/runtime/usr/bin/grim. Do not add the packages, executables or images to Git. The tool ends after it captures; it is not another compositor, host or conversation profile.

Capture

If the identified session is inactive, grim can answer no wl_output: no active output is offered for capture at that moment. This does not mean that capturing TTY4 is impossible. Confirm the socket and activate the existing session for a capture the user requested.

Before you activate the VT, acquire an exclusive flock -n on coordination.lock and keep the descriptor open until you finish the checks and the recording of the result. After you acquire it, read coordination.log again and check the real processes. If the lock is busy, do not activate or change the runtime. Do not remove the lock file.

The example below records the historical values of the proof. Replace the session, UID and socket with the values you discovered, with the lock held; do not run the historical identifiers literally:

loginctl activate 56
XDG_RUNTIME_DIR=/run/user/1000 WAYLAND_DISPLAY=wayland-1 \
  timeout 8s /tmp/agent-native-screen-capture/runtime/usr/bin/grim \
  /tmp/agent-native-tty4.png
chmod 600 /tmp/agent-native-tty4.png

The activation can finish before the output becomes available. In the proof, it was enough to retry only when the error was no wl_output, up to five attempts, with a 0.3 s interval. For other errors, stop and diagnose. Do not restart River, Electron or the core to get an image. If the client reports that the capture protocol is unavailable, record this limitation instead of presenting a browser capture as if it came from TTY4.

Record the start and the result by appending to coordination.log, protecting each write with flock on the log itself. Include the time with time zone, the chat/worktree identification, the session, the socket, the result and the continuity of the processes. Do not record conversation content, tokens or credentials. Close the coordination.lock descriptor when you finish, also when an error occurs. Report which VT stayed active; do not do additional unnecessary switches.

Inspect and present

Open the local PNG with the image viewing tool and check the composition visually. Present it to the user through Markdown with an absolute path, for example:

![Real capture of TTY4](/tmp/agent-native-tty4.png)

The image is private and temporary, outside the repository. A real capture can contain user data; do not copy its content into logs, tests or documentation. Use synthetic data in versioned evidence.

Tell these apart explicitly:

  • Browser: DOM, controls and interaction connected to the core.
  • River/API: processes, focus, geometry, visibility and published buffers.
  • Wayland capture: pixels of the native composition, including wallpaper and overlay.

The capture does not prove the microphone, physical shortcuts, continuous smoothness or the absence of rendering failures. DRM problems when switching VTs and the dimension assert observed earlier still require their own diagnosis; getting a screenshot does not solve them.