Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(selection): read selections without accessibility, stop losing agent commands (#1593)

Gabriel Stein1bcf5fc54724
diff --git a/resources/macos-fast-paste.swift b/resources/macos-fast-paste.swift
index f8bed4c4f..faeba79d2 100644
--- a/resources/macos-fast-paste.swift
+++ b/resources/macos-fast-paste.swift
@@ -4,8 +4,20 @@ if !AXIsProcessTrusted() {
exit(2)
}
-guard let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: 0x09, keyDown: true),
- let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: 0x09, keyDown: false) else {
+// Selection capture sends ⌘C and reports which app received it, so the caller
+// can tell a copied selection from a target that changed underneath it. With no
+// arguments this stays what the paste path expects: ⌘V, no output.
+let copyMode = CommandLine.arguments.contains("--copy")
+let virtualKey: CGKeyCode = copyMode ? 0x08 : 0x09 // kVK_ANSI_C : kVK_ANSI_V
+
+// Resolved before the keystroke is posted: this is the app that will receive it.
+let target = copyMode ? NSWorkspace.shared.frontmostApplication : nil
+if copyMode && target == nil {
+ exit(1)
+}
+
+guard let keyDown = CGEvent(keyboardEventSource: nil, virtualKey: virtualKey, keyDown: true),
+ let keyUp = CGEvent(keyboardEventSource: nil, virtualKey: virtualKey, keyDown: false) else {
exit(1)
}
@@ -15,3 +27,7 @@ keyDown.post(tap: .cgSessionEventTap)
usleep(8000)
keyUp.post(tap: .cgSessionEventTap)
usleep(20000)
+
+if let target = target {
+ print("COPY_OK \(target.processIdentifier) \(target.localizedName ?? "")")
+}
diff --git a/resources/macos-text-monitor.swift b/resources/macos-text-monitor.swift
index 9dbef7f1e..ca7f1dd4a 100644
--- a/resources/macos-text-monitor.swift
+++ b/resources/macos-text-monitor.swift
@@ -52,20 +52,56 @@ func observerCallback(
// Usage: macos-text-monitor <pid>
// macos-text-monitor --selected-text <pid>
+// macos-text-monitor --window-bounds <pid>
let selectionReadMode = CommandLine.arguments.count >= 3 &&
CommandLine.arguments[1] == "--selected-text"
-let pidArgumentIndex = selectionReadMode ? 2 : 1
+let windowBoundsMode = CommandLine.arguments.count >= 3 &&
+ CommandLine.arguments[1] == "--window-bounds"
+let pidArgumentIndex = selectionReadMode || windowBoundsMode ? 2 : 1
guard CommandLine.arguments.count > pidArgumentIndex,
let targetPid = Int32(CommandLine.arguments[pidArgumentIndex]),
targetPid > 0 else {
- writeError("Usage: macos-text-monitor [--selected-text] <pid>")
+ writeError("Usage: macos-text-monitor [--selected-text|--window-bounds] <pid>")
writeOutput("NO_ELEMENT")
exit(1)
}
monitoredPid = targetPid
+// Reports the target's window rect so the caller can tell which display the user
+// is working on. Asks the window server rather than accessibility on purpose:
+// AXFocusedWindow is unavailable in exactly the apps whose accessibility tree
+// stays dormant (Chromium), and window bounds need no Screen Recording
+// permission — only window *titles* do.
+if windowBoundsMode {
+ let options: CGWindowListOption = [.optionOnScreenOnly, .excludeDesktopElements]
+ let windows = CGWindowListCopyWindowInfo(options, kCGNullWindowID) as? [[String: Any]] ?? []
+ // Largest window rather than frontmost: layer 0 still includes the full-width
+ // toolbar strips Arc-family browsers float above their content, and one of
+ // those spanning another display would name the wrong screen.
+ var best: (x: Int, y: Int, width: Int, height: Int, area: Double)?
+ for window in windows {
+ guard let ownerPid = window[kCGWindowOwnerPID as String] as? pid_t, ownerPid == targetPid,
+ let layer = window[kCGWindowLayer as String] as? Int, layer == 0,
+ let bounds = window[kCGWindowBounds as String] as? [String: Any],
+ let x = bounds["X"] as? Double, let y = bounds["Y"] as? Double,
+ let width = bounds["Width"] as? Double, let height = bounds["Height"] as? Double
+ else { continue }
+ let area = width * height
+ if area > (best?.area ?? 0) {
+ best = (Int(x), Int(y), Int(width), Int(height), area)
+ }
+ }
+
+ guard let window = best else {
+ writeOutput("NO_WINDOW")
+ exit(1)
+ }
+ writeOutput("BOUNDS:\(window.x),\(window.y),\(window.width),\(window.height)")
+ exit(0)
+}
+
// Read original text from stdin (monitoring mode only). Selection reads are
// spawned via execFile, which keeps stdin open without writing — blocking on
// readLine there would hang until the caller's timeout kills the process.
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index bcf47522d..9e8fe5f9b 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -90,7 +90,6 @@ import {
extractSelectionEditReplacement,
getSelectionCaptureDisposition,
} from "./selectionEditing";
-import { isAccessibilitySkipped } from "../utils/permissions";
const REASONING_CACHE_TTL = 30000; // 30 seconds
const RECORDING_TIMESLICE_MS = 250; // flush chunks periodically so short recordings still carry audio frames. See #871.
@@ -478,6 +477,7 @@ class AudioManager {
this.translationApplied = false;
this.pendingSelectionEdit = null;
this.screenContextPromise = null;
+ this.selectionCapturePromise = null;
this.context = "dictation";
this.sttConfig = null;
this.lastAudioBlob = null;
@@ -706,6 +706,9 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
// cancelled voice-agent recording, even after the setting was turned
// off). A live voice-agent start re-captures right after this call.
this.screenContextPromise = null;
+ // Same for a prefetched selection: bounded to one recording, so a read taken
+ // in an earlier app can never be edited in place by this command.
+ this.selectionCapturePromise = null;
}
setTranslationRequested(requested) {
@@ -728,6 +731,22 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
this.screenContextPromise = window.electronAPI?.captureScreenContext?.() ?? null;
}
+ // Kicked off at voice-agent recording start, alongside the screenshot, so the
+ // read resolves while the user is still speaking.
+ beginSelectionCapture() {
+ this.selectionCapturePromise = window.electronAPI?.captureSelectedText?.() ?? null;
+ // Marks the stored promise handled without consuming it: a failure nobody is
+ // awaiting yet must not surface as an unhandled rejection, and the awaiting
+ // caller must still see the original error.
+ this.selectionCapturePromise?.catch(() => {});
+ }
+
+ consumeSelectionCapture() {
+ const pending = this.selectionCapturePromise;
+ this.selectionCapturePromise = null;
+ return pending ?? window.electronAPI?.captureSelectedText?.();
+ }
+
async consumeScreenContext() {
const pending = this.screenContextPromise;
this.screenContextPromise = null;
@@ -2223,7 +2242,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
async processAgentCommand(text, model, agentName, config) {
let capture;
try {
- capture = await window.electronAPI?.captureSelectedText?.();
+ capture = await this.consumeSelectionCapture();
} catch (cause) {
const error = new Error(
`Selection edit could not safely read the selection: ${cause.message}`
@@ -2248,12 +2267,11 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
throw error;
}
- const captureDisposition = getSelectionCaptureDisposition(capture, isAccessibilitySkipped());
+ const captureDisposition = getSelectionCaptureDisposition(capture);
if (captureDisposition === "standalone") {
- // No selection, or selection capture is unavailable by design (for
- // example, the user explicitly skipped macOS Accessibility): preserve
- // the existing Voice Agent behavior and type at the cursor.
+ // Nothing selected, or a target that can never report one: type at the
+ // cursor (see STANDALONE_CAPTURE_CODES).
return this.processWithReasoningModel(text, model, agentName, config);
}
diff --git a/src/helpers/clipboard.js b/src/helpers/clipboard.js
index 36c5f7017..ce39a329e 100644
--- a/src/helpers/clipboard.js
+++ b/src/helpers/clipboard.js
@@ -91,6 +91,11 @@ const LINUX_TERMINAL_CLASSES = [
"waveterm",
];
+// macOS reports localized app names rather than window classes, and iTerm2 has
+// no entry above because it has no Linux window class. Matching it on Linux too
+// is harmless — no such window class exists there.
+const TERMINAL_SIGNATURES = [...LINUX_TERMINAL_CLASSES, "iterm"];
+
function writeClipboardInRenderer(webContents, text) {
if (!webContents || !webContents.executeJavaScript) {
return Promise.reject(new Error("Invalid webContents for clipboard write"));
@@ -256,10 +261,15 @@ class ClipboardManager {
return null;
}
+ // Accepts a Linux window class or a macOS app name.
+ isTerminalSignature(signature) {
+ if (!signature) return false;
+ const normalized = String(signature).toLowerCase();
+ return TERMINAL_SIGNATURES.some((term) => normalized.includes(term));
+ }
+
isLinuxTerminalWindowClass(windowClass) {
- if (!windowClass) return false;
- const normalized = String(windowClass).toLowerCase();
- return LINUX_TERMINAL_CLASSES.some((term) => normalized.includes(term));
+ return this.isTerminalSignature(windowClass);
}
// Selection capture (SelectionManager) seeds a sentinel and polls until a
diff --git a/src/helpers/dragManager.js b/src/helpers/dragManager.js
index 7b57eece8..ae860b013 100644
--- a/src/helpers/dragManager.js
+++ b/src/helpers/dragManager.js
@@ -1,4 +1,5 @@
const { screen } = require("electron");
+const { WindowPositionUtil } = require("./windowConfig");
class DragManager {
constructor() {
@@ -69,27 +70,20 @@ class DragManager {
updateWindowPosition() {
try {
const cursorPos = screen.getCursorScreenPoint();
- const newX = cursorPos.x - this.dragOffset.x;
- const newY = cursorPos.y - this.dragOffset.y;
-
- // Get screen bounds to keep window visible
- const display = screen.getDisplayNearestPoint(cursorPos);
- const bounds = display.workArea;
-
- // Get window size for boundary calculations
- const windowBounds = this.targetWindow.getBounds();
-
- // Constrain to screen bounds
- const constrainedX = Math.max(
- bounds.x,
- Math.min(newX, bounds.x + bounds.width - windowBounds.width)
- );
- const constrainedY = Math.max(
- bounds.y,
- Math.min(newY, bounds.y + bounds.height - windowBounds.height)
- );
-
- this.targetWindow.setPosition(constrainedX, constrainedY);
+ const { width, height } = this.targetWindow.getBounds();
+ const x = cursorPos.x - this.dragOffset.x;
+ const y = cursorPos.y - this.dragOffset.y;
+
+ // Constrain against the display the window lands on, not the one under the
+ // cursor: near a boundary between differently sized displays, the cursor's
+ // work area permits positions that leave the window in dead space.
+ const display = screen.getDisplayNearestPoint({
+ x: x + width / 2,
+ y: y + height / 2,
+ });
+ const clamped = WindowPositionUtil.clampToWorkArea({ x, y, width, height }, display);
+
+ this.targetWindow.setPosition(clamped.x, clamped.y);
} catch (error) {
console.error("Error updating window position:", error);
this.stopWindowDrag();
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 2d0f06e9f..0ea358b38 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -4557,7 +4557,17 @@ class IPCHandlers {
// return it. A signed-out user has no workspace and no policy; managed
// users are gated even if a stale renderer asks. null matches capture's
// contract — a screenshot must never break the dictation it accompanies.
- const capturePromise = screenContextCapture.captureCursorDisplay();
+ // The target's window rect picks the display it is on rather than the one
+ // under the cursor, and the panel reposition has normally cached it already.
+ // A failed rect read falls back to the cursor rather than rejecting: the
+ // policy branch below can abandon this promise, and capture's contract is
+ // to yield null, never to throw.
+ const targetPid = this.textEditMonitor?.lastTargetPid;
+ const capturePromise = Promise.resolve(
+ targetPid ? this.textEditMonitor.getTargetWindowBounds?.(targetPid) : null
+ )
+ .catch(() => null)
+ .then((targetBounds) => screenContextCapture.captureActiveDisplay(targetBounds));
const authHeaders = await getAuthHeader(event);
if (authHeaders.Authorization || authHeaders.Cookie) {
// Bound the verdict wait: a lapsed policy TTL on a degraded network
diff --git a/src/helpers/screenContextCapture.js b/src/helpers/screenContextCapture.js
index b36ac02af..dc38fcd7a 100644
--- a/src/helpers/screenContextCapture.js
+++ b/src/helpers/screenContextCapture.js
@@ -64,9 +64,12 @@ function encodeWithinBudget(image) {
return smallest.length <= MAX_ENCODED_BYTES ? smallest : null;
}
+// Photographs the display showing the app being dictated into, given that app's
+// window rect; without one it falls back to the display under the cursor, which
+// on a multi-monitor desk is often not the screen the user is working on.
// Returns null on any failure — a screenshot must never break the dictation
// it accompanies.
-async function captureCursorDisplay() {
+async function captureActiveDisplay(targetBounds = null) {
const accessStatus = getAccessStatus();
if (accessStatus !== "granted") {
debugLogger.warn("Screen context capture skipped", { accessStatus }, "screenContext");
@@ -74,8 +77,9 @@ async function captureCursorDisplay() {
}
try {
- const cursor = screen.getCursorScreenPoint();
- const display = screen.getDisplayNearestPoint(cursor);
+ const display = targetBounds
+ ? screen.getDisplayMatching(targetBounds)
+ : screen.getDisplayNearestPoint(screen.getCursorScreenPoint());
const scale = Math.min(1, MAX_EDGE_PX / Math.max(display.size.width, display.size.height));
const thumbnailSize = {
width: Math.max(1, Math.round(display.size.width * scale)),
@@ -115,4 +119,4 @@ async function requestAccess() {
return getAccessStatus();
}
-module.exports = { getAccessStatus, getAccessResult, captureCursorDisplay, requestAccess };
+module.exports = { getAccessStatus, getAccessResult, captureActiveDisplay, requestAccess };
diff --git a/src/helpers/selectionEditing.js b/src/helpers/selectionEditing.js
index aad547720..a36ed66cf 100644
--- a/src/helpers/selectionEditing.js
+++ b/src/helpers/selectionEditing.js
@@ -22,22 +22,25 @@ export function buildSelectionEditUserPrompt(spokenInstruction, selectedText) {
});
}
-export function getSelectionCaptureDisposition(capture, accessibilitySkipped = false) {
+// Codes meaning "this target can never report a selection", as opposed to "a
+// selection may exist and the read failed". They fall back to typing at the
+// cursor — the Voice Agent's behavior before selection editing existed — because
+// losing in-place editing is acceptable where losing the command is not.
+const STANDALONE_CAPTURE_CODES = new Set([
+ "target_unavailable",
+ "copy_helper_unavailable",
+ "selection_manager_unavailable",
+ "unsupported_platform",
+ // macOS: neither the accessibility tree nor a synthetic copy could inspect the
+ // app, so a selection is neither readable nor ruled out.
+ "accessibility_unavailable",
+]);
+
+export function getSelectionCaptureDisposition(capture) {
if (!capture || capture.status === "none") return "standalone";
if (capture.status === "selected") return "selection";
- if (capture.status === "unavailable") {
- const structuralUnavailable = new Set([
- "target_unavailable",
- "copy_helper_unavailable",
- "selection_manager_unavailable",
- "unsupported_platform",
- ]);
- if (
- structuralUnavailable.has(capture.code) ||
- (capture.code === "accessibility_unavailable" && accessibilitySkipped)
- ) {
- return "standalone";
- }
+ if (capture.status === "unavailable" && STANDALONE_CAPTURE_CODES.has(capture.code)) {
+ return "standalone";
}
return capture.status === "target_changed" ? "changed" : "unavailable";
}
diff --git a/src/helpers/selectionManager.js b/src/helpers/selectionManager.js
index 69906b19c..9d2f587da 100644
--- a/src/helpers/selectionManager.js
+++ b/src/helpers/selectionManager.js
@@ -15,10 +15,12 @@ const CLIPBOARD_POLL_MS = 20;
// back to standalone dictation, pasting over the selection.
const ATSPI_TARGET_TIMEOUT_MS = 2000;
-// Editors that copy the whole current line when Ctrl+C lands with an empty
-// selection (VS Code's editor.emptySelectionClipboard, Scintilla, JetBrains,
-// Visual Studio), making a bare caret look like a selection to the
-// synthetic-copy capture. Matched against the target's exe name and window class.
+// Editors that copy the whole current line when Ctrl+C (⌘C on macOS) lands with
+// an empty selection (VS Code's editor.emptySelectionClipboard, Scintilla,
+// JetBrains, Visual Studio), making a bare caret look like a selection to the
+// synthetic-copy capture. Matched against exe name and window class on
+// Windows/Linux and against the localized app name on macOS, so the JetBrains
+// IDEs need both spellings.
const LINE_COPY_EDITOR_SIGNATURES = [
"code", // VS Code and forks (VSCodium, code-oss)
"cursor",
@@ -27,10 +29,13 @@ const LINE_COPY_EDITOR_SIGNATURES = [
"sublime",
"jetbrains",
"idea64",
+ "intellij", // macOS app name for idea64
"pycharm",
"webstorm",
"phpstorm",
"rider64",
+ "rider", // macOS app name for rider64
+ "android studio", // macOS app name for studio64
"clion",
"goland",
"rubymine",
@@ -263,7 +268,50 @@ class SelectionManager {
if (result.state === "none") {
return { status: "none", target: { kind: "mac-pid", pid } };
}
- return { status: "unavailable", code: "accessibility_unavailable" };
+ // Chromium browsers (and any app whose accessibility tree stays dormant)
+ // never resolve a focused element, so the read above cannot tell a selection
+ // from an empty field. A synthetic copy still can — the same route Windows
+ // and Linux take by default.
+ return this._readMacSelectionViaClipboard(pid, expectedTarget);
+ }
+
+ async _readMacSelectionViaClipboard(pid, expectedTarget) {
+ const binary = this.clipboardManager.resolveFastPasteBinary?.();
+ if (!binary) return { status: "unavailable", code: "accessibility_unavailable" };
+
+ const capture = await this._captureViaClipboard(
+ async () => {
+ const result = await this._runCopyHelper(binary, ["--copy"]);
+ const match = result.stdout.match(/COPY_OK\s+(\d+)\s*(.*)/);
+ if (!result.success || !match) return { success: false };
+ return {
+ success: true,
+ target: { kind: "mac-pid", pid: Number(match[1]), appName: match[2].trim() || null },
+ };
+ },
+ expectedTarget || { kind: "mac-pid", pid }
+ );
+
+ // A copy that never landed leaves the selection as unknown as the
+ // accessibility read did, so it gets the same non-fatal treatment.
+ if (capture.status === "unavailable" && capture.code === "copy_failed") {
+ return { status: "unavailable", code: "accessibility_unavailable" };
+ }
+ // Replacement text typed into a shell executes on its embedded newlines, so
+ // terminal selections are declined exactly as on Linux. GPU-rendered
+ // terminals (Ghostty, Alacritty, kitty) have no accessibility tree, so this
+ // path is the only way they reach selection editing at all.
+ if (
+ capture.status === "selected" &&
+ this.clipboardManager.isTerminalSignature?.(capture.target?.appName)
+ ) {
+ return { status: "none", target: capture.target };
+ }
+ return capture;
+ }
+
+ _runCopyHelper(binary, args) {
+ return runSpawn(binary, args, { timeout: COPY_TIMEOUT_MS });
}
async _readWindowsSelection(expectedTarget) {
@@ -481,14 +529,24 @@ class SelectionManager {
// trailing terminator; treat that shape from a known line-copy editor as
// "no selection" so a bare caret never gets its line rewritten. Proper
// fix: a real selection read (UIA TextPattern), like --atspi-selection.
- if (/^[^\n]*\r?\n$/.test(copiedText) && this._isLineCopyEditor(expectedTarget)) {
+ if (
+ /^[^\n]*\r?\n$/.test(copiedText) &&
+ this._isLineCopyEditor(expectedTarget, copyResult.target)
+ ) {
return { status: "none", target: copyResult.target };
}
return { status: "selected", text: copiedText, target: copyResult.target };
}
- _isLineCopyEditor(target) {
- const signature = `${target?.exeName || ""} ${target?.windowClass || ""}`.toLowerCase();
+ // Windows and Linux name the app in the target captured before the copy;
+ // macOS learns it from the copy helper's own report, so both are checked.
+ _isLineCopyEditor(...targets) {
+ const signature = targets
+ .map((target) =>
+ `${target?.exeName || ""} ${target?.windowClass || ""} ${target?.appName || ""}`.trim()
+ )
+ .join(" ")
+ .toLowerCase();
if (!signature.trim()) return false;
return LINE_COPY_EDITOR_SIGNATURES.some((editor) => signature.includes(editor));
}
diff --git a/src/helpers/textEditMonitor.js b/src/helpers/textEditMonitor.js
index 83a6c24a1..ec1e15373 100644
--- a/src/helpers/textEditMonitor.js
+++ b/src/helpers/textEditMonitor.js
@@ -15,6 +15,10 @@ const ACTIVATE_CONFIRM_DELAY_MS = 25;
// across that burst. Kept short so back-to-back dictations in different apps
// still get a fresh capture.
const TARGET_CAPTURE_FRESHNESS_MS = 250;
+// Must outlast the binary's retry ladder (5 attempts, 300ms apart, ~1.25s): a
+// killed run's verdict is discarded, so a tighter timeout means the app below is
+// never learned and every read pays the full ladder.
+const SELECTED_TEXT_TIMEOUT_MS = 1600;
// AXError -25212 (kAXErrorNoValue) on the focused-element read is how
// Chromium/Electron apps respond while their AX tree is dormant, making the
// native binary's 5-attempt retry (~1.2s) dead time on every read. A single
@@ -119,6 +123,7 @@ class TextEditMonitor extends EventEmitter {
this.lastTargetPid = null;
this._captureTargetPromise = null;
this._lastCaptureAt = 0;
+ this._windowBounds = null;
// PIDs whose AX tree never yields a focused element (see
// isPersistentAxNoValueFailure). A recycled PID only costs a detour via
// AppleScript, which is still correct.
@@ -229,7 +234,7 @@ class TextEditMonitor extends EventEmitter {
return false;
}
- getSelectedText(pid, timeoutMs = 1200) {
+ getSelectedText(pid, timeoutMs = SELECTED_TEXT_TIMEOUT_MS) {
return new Promise((resolve) => {
if (process.platform !== "darwin" || !pid) {
resolve({ state: "unavailable" });
@@ -316,6 +321,50 @@ class TextEditMonitor extends EventEmitter {
);
}
+ /**
+ * macOS: the target app's largest on-screen window rect, used to decide which
+ * display the user is working on. Resolves to null when the app has no
+ * ordinary window (or off macOS), leaving the caller to fall back to the
+ * cursor. Cached over the same press-time burst as captureTargetPid, so the
+ * dictation panel and the screen-context capture share one spawn.
+ */
+ async getTargetWindowBounds(pid, timeoutMs = 700) {
+ if (process.platform !== "darwin" || !pid) return null;
+ if (
+ this._windowBounds?.pid === pid &&
+ Date.now() - this._windowBounds.at < TARGET_CAPTURE_FRESHNESS_MS
+ ) {
+ return this._windowBounds.bounds;
+ }
+
+ const resolved = this.resolveBinary();
+ if (!resolved) return null;
+
+ const bounds = await new Promise((resolve) => {
+ execFile(
+ resolved.command,
+ [...resolved.args, "--window-bounds", String(pid)],
+ { timeout: timeoutMs },
+ (error, stdout) => {
+ const match = stdout?.match(/^BOUNDS:(-?\d+),(-?\d+),(\d+),(\d+)$/m);
+ if (!match) {
+ resolve(null);
+ return;
+ }
+ resolve({
+ x: Number(match[1]),
+ y: Number(match[2]),
+ width: Number(match[3]),
+ height: Number(match[4]),
+ });
+ }
+ );
+ });
+
+ this._windowBounds = { pid, bounds, at: Date.now() };
+ return bounds;
+ }
+
/**
* macOS: read the char before the cursor in the focused text field, used by
* paste-time smart spacing. Resolves to { state: "ok", char } | { state:
diff --git a/src/helpers/windowConfig.js b/src/helpers/windowConfig.js
index 48861594d..192e06db8 100644
--- a/src/helpers/windowConfig.js
+++ b/src/helpers/windowConfig.js
@@ -160,26 +160,50 @@ class WindowPositionUtil {
let x, y;
if (position === "bottom-left") {
x = workArea.x + MARGIN;
- y = Math.max(0, workArea.y + workArea.height - height - MARGIN);
+ y = workArea.y + workArea.height - height - MARGIN;
} else if (position === "center") {
x = Math.round(workArea.x + (workArea.width - width) / 2);
- y = Math.max(0, workArea.y + workArea.height - height - MARGIN);
+ y = workArea.y + workArea.height - height - MARGIN;
} else {
// bottom-right (default)
- x = Math.max(0, workArea.x + workArea.width - width - MARGIN);
- y = Math.max(0, workArea.y + workArea.height - height - MARGIN);
+ x = workArea.x + workArea.width - width - MARGIN;
+ y = workArea.y + workArea.height - height - MARGIN;
}
- return { x, y, width, height };
+ // Clamped to the display's own work area, never to zero: a monitor placed
+ // above or left of the primary one has a negative origin, so flooring at zero
+ // lands the window on a coordinate that display doesn't cover.
+ return {
+ ...WindowPositionUtil.clampToWorkArea({ x, y, width, height }, display),
+ width,
+ height,
+ };
+ }
+
+ // Keeps a window's whole frame inside one display's work area. Displays of
+ // different sizes leave dead space beside the smaller one, and a window parked
+ // there is invisible even though the window server still reports it on screen.
+ static clampToWorkArea(bounds, display) {
+ const workArea = display.workArea || display.bounds;
+ return {
+ x: Math.max(workArea.x, Math.min(bounds.x, workArea.x + workArea.width - bounds.width)),
+ y: Math.max(workArea.y, Math.min(bounds.y, workArea.y + workArea.height - bounds.height)),
+ };
}
static getNotificationPosition(display) {
const { width, height } = NOTIFICATION_WINDOW_CONFIG;
const MARGIN = 16;
const workArea = display.workArea || display.bounds;
- const x = Math.max(0, workArea.x + workArea.width - width - MARGIN);
- const y = Math.max(0, workArea.y + MARGIN);
- return { x, y, width, height };
+ // Same negative-origin trap as getMainWindowPosition: clamp to the display,
+ // not to zero, or a monitor above the primary one puts the prompt nowhere.
+ const bounds = {
+ x: workArea.x + workArea.width - width - MARGIN,
+ y: workArea.y + MARGIN,
+ width,
+ height,
+ };
+ return { ...WindowPositionUtil.clampToWorkArea(bounds, display), width, height };
}
static getTranscriptionPreviewPosition(display, mainWindowBounds, size = {}) {
diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.js
index 3e555366f..1c397825f 100644
--- a/src/helpers/windowManager.js
+++ b/src/helpers/windowManager.js
@@ -171,7 +171,6 @@ class WindowManager {
x: currentBounds.x + currentBounds.width / 2,
y: currentBounds.y + currentBounds.height,
});
- const workArea = display.workArea || display.bounds;
let newX, newY;
@@ -191,18 +190,11 @@ class WindowManager {
newY = currentBounds.y + currentBounds.height - newSize.height;
}
- // Clamp to work area
- newX = Math.max(workArea.x, Math.min(newX, workArea.x + workArea.width - newSize.width));
- newY = Math.max(workArea.y, Math.min(newY, workArea.y + workArea.height - newSize.height));
+ const clamped = WindowPositionUtil.clampToWorkArea({ x: newX, y: newY, ...newSize }, display);
- this.mainWindow.setBounds({
- x: newX,
- y: newY,
- width: newSize.width,
- height: newSize.height,
- });
+ this.mainWindow.setBounds({ ...clamped, ...newSize });
- return { success: true, bounds: { x: newX, y: newY, ...newSize } };
+ return { success: true, bounds: { ...clamped, ...newSize } };
}
async loadWindowContent(window, isControlPanel = false, isAgent = false) {
@@ -1104,11 +1096,23 @@ class WindowManager {
this.agentWindow.setBounds(bounds);
}
- _repositionToCursorDisplay() {
+ // The display the user is working on is the one showing the app being dictated
+ // into, which on a multi-monitor desk is often not the one the mouse rests on.
+ // Falls back to the cursor when the target has no readable window (non-macOS,
+ // no target captured yet, or an app with no ordinary window).
+ async _resolveActiveDisplay() {
+ const pid = this.textEditMonitor?.lastTargetPid;
+ const bounds = pid ? await this.textEditMonitor.getTargetWindowBounds(pid) : null;
+ return bounds
+ ? screen.getDisplayMatching(bounds)
+ : screen.getDisplayNearestPoint(screen.getCursorScreenPoint());
+ }
+
+ async _repositionToActiveDisplay() {
if (!this.mainWindow || this.mainWindow.isDestroyed()) return;
- const cursorPos = screen.getCursorScreenPoint();
- const cursorDisplay = screen.getDisplayNearestPoint(cursorPos);
+ const activeDisplay = await this._resolveActiveDisplay();
+ if (!this.mainWindow || this.mainWindow.isDestroyed()) return;
const currentBounds = this.mainWindow.getBounds();
const currentDisplay = screen.getDisplayNearestPoint({
@@ -1116,20 +1120,38 @@ class WindowManager {
y: currentBounds.y + currentBounds.height / 2,
});
- if (currentDisplay.id === cursorDisplay.id) return;
+ if (currentDisplay.id === activeDisplay.id) {
+ // Nearest-display math can't tell "on this display" from "just past its
+ // edge", so a rearranged monitor or a drag that ended over another
+ // display can leave the panel stranded in dead space, looking like the
+ // overlay vanished. Pull it back before showing it.
+ const clamped = WindowPositionUtil.clampToWorkArea(currentBounds, currentDisplay);
+ if (clamped.x !== currentBounds.x || clamped.y !== currentBounds.y) {
+ this.mainWindow.setBounds({ ...currentBounds, ...clamped });
+ }
+ return;
+ }
const newPos = WindowPositionUtil.getMainWindowPosition(
- cursorDisplay,
+ activeDisplay,
{ width: currentBounds.width, height: currentBounds.height },
this._panelStartPosition
);
+ debugLogger.debug(
+ "[WindowManager] Moving dictation panel to the active display",
+ { from: currentBounds, to: newPos, displayId: activeDisplay.id },
+ "window"
+ );
this.mainWindow.setBounds(newPos);
}
showDictationPanel(options = {}) {
const { focus = false } = options;
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
- this._repositionToCursorDisplay();
+ // Reading the target's window costs a helper spawn, so show now and move
+ // when the answer lands: a visible hop only happens when the panel was on
+ // the wrong display, which is the case being corrected.
+ void this._repositionToActiveDisplay();
if (this.mainWindow.isMinimized()) {
this.mainWindow.restore();
diff --git a/src/hooks/useAudioRecording.js b/src/hooks/useAudioRecording.js
index 7e67e4634..6a02d8c96 100644
--- a/src/hooks/useAudioRecording.js
+++ b/src/hooks/useAudioRecording.js
@@ -88,6 +88,13 @@ export const useAudioRecording = (toast, options = {}) => {
audioManagerRef.current.beginScreenContextCapture();
}
+ // The selection to edit is whatever was highlighted at press time, so
+ // read it now: it resolves while the user speaks instead of adding a
+ // round trip after transcription.
+ if (voiceAgentRequested) {
+ audioManagerRef.current.beginSelectionCapture();
+ }
+
// Retry STT config fetch if it wasn't loaded on mount (e.g. auth wasn't ready)
if (!audioManagerRef.current.sttConfig) {
const config = await window.electronAPI.getSttConfig?.();
diff --git a/test/helpers/audioManagerSelectionPrefetch.test.js b/test/helpers/audioManagerSelectionPrefetch.test.js
new file mode 100644
index 000000000..8b3201294
--- /dev/null
+++ b/test/helpers/audioManagerSelectionPrefetch.test.js
@@ -0,0 +1,154 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const { createRendererServer, installBrowserGlobals } = require("../lib/rendererTestHarness");
+
+// The selection read used to be the last serial step before the model call. A
+// voice-agent recording now starts it at press time, alongside the screenshot,
+// so it resolves while the user is still speaking.
+async function loadAudioManager(t, { cachePrefix, settingsKey }) {
+ const { window } = installBrowserGlobals(t);
+ const vite = await createRendererServer(t, {
+ cachePrefix,
+ mockModules: {
+ "/utils/logger":
+ "export default { debug() {}, info() {}, warn() {}, error() {}, logReasoning() {} };",
+ "/stores/settingsStore": `
+ export const getSettings = () => globalThis.${settingsKey};
+ export const getEffectiveCleanupModel = () => null;
+ export const isCloudCleanupMode = () => false;
+ export const isCloudDictationAgentMode = () => false;
+ export const isCloudTranslationMode = () => false;
+ `,
+ "/services/ReasoningService": 'export default { processText: async () => "" };',
+ "/services/SyncService.js": "export const syncService = {};",
+ "/lib/auth": "export const withSessionRefresh = (fn) => fn();",
+ "/utils/permissions": "export const isAccessibilitySkipped = () => false;",
+ },
+ });
+ t.after(() => {
+ delete globalThis[settingsKey];
+ });
+ globalThis[settingsKey] = {};
+
+ const AudioManager = (await vite.ssrLoadModule("/helpers/audioManager.js")).default;
+ return {
+ window,
+ createManager: (overrides = {}) =>
+ Object.assign(Object.create(AudioManager.prototype), overrides),
+ };
+}
+
+function countingCapture(window) {
+ const calls = [];
+ window.electronAPI.captureSelectedText = async () => {
+ calls.push(true);
+ return { status: "selected", text: "selected body", sessionId: `s${calls.length}` };
+ };
+ return calls;
+}
+
+test("a prefetched selection is reused instead of read again", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "openwhispr-sel-prefetch-test-",
+ settingsKey: "__selPrefetchSettings",
+ });
+ const calls = countingCapture(window);
+ const manager = createManager();
+
+ manager.beginSelectionCapture();
+ assert.equal(calls.length, 1, "the read starts without being awaited");
+
+ const capture = await manager.consumeSelectionCapture();
+ assert.equal(capture.sessionId, "s1");
+ assert.equal(calls.length, 1, "consuming must reuse the prefetch");
+});
+
+test("consuming without a prefetch falls back to reading on demand", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "openwhispr-sel-ondemand-test-",
+ settingsKey: "__selOnDemandSettings",
+ });
+ const calls = countingCapture(window);
+ const manager = createManager();
+
+ const capture = await manager.consumeSelectionCapture();
+ assert.equal(capture.sessionId, "s1");
+ assert.equal(calls.length, 1);
+});
+
+test("a prefetch is single-use", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "openwhispr-sel-singleuse-test-",
+ settingsKey: "__selSingleUseSettings",
+ });
+ const calls = countingCapture(window);
+ const manager = createManager();
+
+ manager.beginSelectionCapture();
+ await manager.consumeSelectionCapture();
+ const second = await manager.consumeSelectionCapture();
+
+ assert.equal(second.sessionId, "s2", "a second consume must read fresh");
+ assert.equal(calls.length, 2);
+});
+
+// A capture must never outlive the recording that asked for it: a read from an
+// earlier app would otherwise be edited in place by the next command.
+test("starting a recording discards a capture left over from the previous one", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "openwhispr-sel-stale-test-",
+ settingsKey: "__selStaleSettings",
+ });
+ const calls = countingCapture(window);
+ const manager = createManager();
+
+ manager.beginSelectionCapture();
+ assert.equal(calls.length, 1);
+
+ manager.setVoiceAgentRequested(true);
+ assert.ok(!manager.selectionCapturePromise, "the stale prefetch must be dropped");
+
+ const capture = await manager.consumeSelectionCapture();
+ assert.equal(capture.sessionId, "s2");
+ assert.equal(calls.length, 2);
+});
+
+test("an agent command consumes the prefetch rather than re-reading the selection", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "openwhispr-sel-command-test-",
+ settingsKey: "__selCommandSettings",
+ });
+ const calls = countingCapture(window);
+ const manager = createManager({ onError: () => {} });
+
+ manager.beginSelectionCapture();
+ // Selection editing needs a model round trip; stub it out and assert only that
+ // the capture came from the prefetch.
+ manager.processWithReasoningModel = async () => {
+ throw new Error("model unavailable");
+ };
+
+ await assert.rejects(
+ () => manager.processAgentCommand("make it shorter", "gpt-5", "Agent", {}),
+ /Selection edit failed: model unavailable/
+ );
+ assert.equal(calls.length, 1, "the command must not read the selection a second time");
+});
+
+test("a rejected prefetch still surfaces its error to the awaiting command", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "openwhispr-sel-reject-test-",
+ settingsKey: "__selRejectSettings",
+ });
+ window.electronAPI.captureSelectedText = async () => {
+ throw new Error("ipc exploded");
+ };
+ const manager = createManager();
+
+ manager.beginSelectionCapture();
+
+ await assert.rejects(
+ () => manager.processAgentCommand("make it shorter", "gpt-5", "Agent", {}),
+ /Selection edit could not safely read the selection: ipc exploded/
+ );
+});
diff --git a/test/helpers/clipboardRestore.test.js b/test/helpers/clipboardRestore.test.js
index f151fe2ba..1982b8c6e 100644
--- a/test/helpers/clipboardRestore.test.js
+++ b/test/helpers/clipboardRestore.test.js
@@ -259,3 +259,25 @@ test("pasteMacOSWithOsascript fallback uses the short macOS restore delay", asyn
expectedText: "dictated text",
});
});
+
+// Terminal detection now serves two callers: the Linux paste path, which matches
+// window classes, and macOS selection capture, which matches localized app names.
+test("terminal detection matches window classes and macOS app names alike", () => {
+ const manager = new ClipboardManager();
+
+ for (const signature of ["konsole", "org.kde.konsole", "Ghostty", "kitty", "WezTerm"]) {
+ assert.equal(manager.isTerminalSignature(signature), true, signature);
+ }
+ // iTerm2 has no Linux window class, so it only appears in the macOS-facing list.
+ assert.equal(manager.isTerminalSignature("iTerm2"), true);
+ assert.equal(manager.isTerminalSignature("Terminal"), true);
+
+ for (const signature of ["Dia", "Google Chrome", "Mail", "", null, undefined]) {
+ assert.equal(manager.isTerminalSignature(signature), false, String(signature));
+ }
+
+ // The Linux window-class entry point keeps behaving exactly as before.
+ assert.equal(manager.isLinuxTerminalWindowClass("konsole"), true);
+ assert.equal(manager.isLinuxTerminalWindowClass("org.mozilla.firefox"), false);
+ assert.equal(manager.isLinuxTerminalWindowClass(null), false);
+});
diff --git a/test/helpers/screenContextCapture.test.js b/test/helpers/screenContextCapture.test.js
index 74059f284..0941e8ab8 100644
--- a/test/helpers/screenContextCapture.test.js
+++ b/test/helpers/screenContextCapture.test.js
@@ -33,6 +33,7 @@ function loadCapture(t, { image, platform = "darwin", accessStatus = "granted" }
let currentAccessStatus = accessStatus;
const calls = {
thumbnailSizes: [],
+ matchedRects: [],
warns: [],
setAccessStatus: (status) => {
currentAccessStatus = status;
@@ -55,6 +56,12 @@ function loadCapture(t, { image, platform = "darwin", accessStatus = "granted" }
screen: {
getCursorScreenPoint: () => ({ x: 0, y: 0 }),
getDisplayNearestPoint: () => ({ id: 1, size: { width: 2560, height: 1440 } }),
+ // A different size from the cursor's display, so tests can tell which
+ // screen a capture was taken from.
+ getDisplayMatching: (rect) => {
+ calls.matchedRects.push(rect);
+ return { id: 2, size: { width: 1512, height: 982 } };
+ },
},
desktopCapturer: {
getSources: async ({ thumbnailSize }) => {
@@ -85,7 +92,7 @@ function loadCapture(t, { image, platform = "darwin", accessStatus = "granted" }
test("a typical screenshot is sent at the highest quality", async (t) => {
const { capture } = loadCapture(t, { image: fakeImage(() => 300_000) });
- const result = await capture.captureCursorDisplay();
+ const result = await capture.captureActiveDisplay();
assert.equal(result.mediaType, "image/jpeg");
assert.equal(Buffer.from(result.data, "base64").toString(), "jpeg-82");
@@ -97,7 +104,7 @@ test("an oversized screenshot steps down the quality ladder until it fits", asyn
image: fakeImage((quality) => (quality === 55 ? 900_000 : 2_000_000)),
});
- const result = await capture.captureCursorDisplay();
+ const result = await capture.captureActiveDisplay();
assert.equal(Buffer.from(result.data, "base64").toString(), "jpeg-55");
});
@@ -109,7 +116,7 @@ test("a screenshot that no quality fits is resized before a final attempt", asyn
image: fakeImage((_quality, edge) => (edge < 1568 ? 800_000 : 4_000_000), { resizes }),
});
- const result = await capture.captureCursorDisplay();
+ const result = await capture.captureActiveDisplay();
assert.deepEqual(resizes, [1024]);
assert.equal(Buffer.from(result.data, "base64").toString(), "jpeg-55");
@@ -118,7 +125,7 @@ test("a screenshot that no quality fits is resized before a final attempt", asyn
test("a screenshot that still cannot fit is dropped rather than sent oversized", async (t) => {
const { capture } = loadCapture(t, { image: fakeImage(() => 9_000_000) });
- assert.equal(await capture.captureCursorDisplay(), null);
+ assert.equal(await capture.captureActiveDisplay(), null);
});
test("capture is skipped when screen recording access is not granted", async (t) => {
@@ -127,7 +134,7 @@ test("capture is skipped when screen recording access is not granted", async (t)
accessStatus: "denied",
});
- assert.equal(await capture.captureCursorDisplay(), null);
+ assert.equal(await capture.captureActiveDisplay(), null);
assert.equal(calls.thumbnailSizes.length, 0);
// The skip must be diagnosable from the debug log, with the TCC status.
assert.deepEqual(calls.warns, [
@@ -185,8 +192,32 @@ test("non-macOS platforms never need a relaunch", (t) => {
test("the captured thumbnail is bounded to the vision-model edge", async (t) => {
const { capture, calls } = loadCapture(t, { image: fakeImage(() => 300_000) });
- await capture.captureCursorDisplay();
+ await capture.captureActiveDisplay();
const [size] = calls.thumbnailSizes;
assert.equal(Math.max(size.width, size.height), 1568);
});
+
+// On a multi-monitor desk the cursor often rests on a different screen than the
+// app being dictated into, and photographing the cursor's screen sends the agent
+// a picture of the wrong monitor.
+test("a target window rect decides the screen, not the cursor", async (t) => {
+ const { capture, calls } = loadCapture(t, { image: fakeImage(() => 300_000) });
+ const targetBounds = { x: 0, y: 33, width: 1512, height: 949 };
+
+ await capture.captureActiveDisplay(targetBounds);
+
+ assert.deepEqual(calls.matchedRects, [targetBounds]);
+ // The matched display is 1512x982, already inside the edge budget; the
+ // cursor's 2560x1440 screen would have been scaled to 1568 wide instead.
+ assert.deepEqual(calls.thumbnailSizes, [{ width: 1512, height: 982 }]);
+});
+
+test("without a target rect the cursor's screen is still used", async (t) => {
+ const { capture, calls } = loadCapture(t, { image: fakeImage(() => 300_000) });
+
+ await capture.captureActiveDisplay();
+
+ assert.deepEqual(calls.matchedRects, []);
+ assert.equal(calls.thumbnailSizes[0].width, 1568);
+});
diff --git a/test/helpers/selectionEditing.test.js b/test/helpers/selectionEditing.test.js
index bad0994da..a683a6d31 100644
--- a/test/helpers/selectionEditing.test.js
+++ b/test/helpers/selectionEditing.test.js
@@ -41,15 +41,11 @@ test("builds a structured prompt that keeps instruction and selection separate",
getSelectionCaptureDisposition({ status: "unavailable", code: "copy_helper_unavailable" }),
"standalone"
);
+ // An app whose accessibility tree never yields a focused element can't report
+ // a selection at all, so the command runs as plain agent dictation instead of
+ // failing — otherwise the Voice Agent is unusable in Chromium browsers.
assert.equal(
getSelectionCaptureDisposition({ status: "unavailable", code: "accessibility_unavailable" }),
- "unavailable"
- );
- assert.equal(
- getSelectionCaptureDisposition(
- { status: "unavailable", code: "accessibility_unavailable" },
- true
- ),
"standalone"
);
assert.equal(getSelectionCaptureDisposition({ status: "target_changed" }), "changed");
diff --git a/test/helpers/selectionManager.test.js b/test/helpers/selectionManager.test.js
index 0fa7e5c05..c47eec771 100644
--- a/test/helpers/selectionManager.test.js
+++ b/test/helpers/selectionManager.test.js
@@ -318,6 +318,124 @@ test("a terminal target reads as no selection", async () => {
assert.deepEqual(result.target, terminalTarget);
});
+// macOS accessibility never resolves a focused element in Chromium browsers, so
+// a synthetic ⌘C is the only way to tell a real selection from an empty field.
+function makeMacClipboardHarness({ copyOutput = "COPY_OK 42 Dia", copied = null } = {}) {
+ const writes = [];
+ // The copied text only becomes visible after the helper runs; anything on the
+ // clipboard beforehand is baselined as stale.
+ let copySent = false;
+ const clipboardManager = {
+ runClipboardOperation: (operation) => operation(),
+ resolveFastPasteBinary: () => "/bin/macos-fast-paste",
+ isTerminalSignature: (signature) => /ghostty|iterm|terminal/i.test(signature || ""),
+ _saveClipboard: () => ({ type: "text", data: "user clipboard" }),
+ _restoreClipboard: () => {},
+ _writeClipboardTextAll: (text) => writes.push(text),
+ _readClipboardTextAll: () => {
+ if (writes.length === 0) return ["user clipboard"];
+ return copySent && copied !== null ? [copied, writes[0]] : [writes[0]];
+ },
+ };
+ const manager = new SelectionManager({
+ clipboardManager,
+ textEditMonitor: { lastTargetPid: 42, getSelectedText: async () => ({ state: "unavailable" }) },
+ platform: "darwin",
+ now: () => 1000,
+ });
+ manager._runCopyHelper = async () => {
+ copySent = true;
+ return { success: true, stdout: copyOutput, stderr: "" };
+ };
+ return { manager, writes };
+}
+
+test("an inaccessible macOS target falls back to a synthetic copy", async () => {
+ const { manager, writes } = makeMacClipboardHarness({ copied: "the selected paragraph" });
+ const result = await manager.captureSelectedText();
+
+ assert.equal(result.status, "selected");
+ assert.equal(result.text, "the selected paragraph");
+ assert.equal(writes.at(-1), "user clipboard");
+});
+
+test("nothing selected in an inaccessible macOS target reads as no selection", async () => {
+ const { manager } = makeMacClipboardHarness({ copied: null });
+ assert.equal((await manager.captureSelectedText()).status, "none");
+});
+
+test("a copy landing in a different app than expected reports the target changed", async () => {
+ const { manager } = makeMacClipboardHarness({
+ copyOutput: "COPY_OK 99 Some Other App",
+ copied: "text from the wrong app",
+ });
+ assert.equal((await manager.captureSelectedText()).status, "target_changed");
+});
+
+// Replacement text typed into a shell executes on its embedded newlines, and
+// terminals without an accessibility tree reach editing only via this path.
+test("a macOS terminal selection reads as no selection", async () => {
+ const { manager } = makeMacClipboardHarness({
+ copyOutput: "COPY_OK 42 Ghostty",
+ copied: "rm -rf important",
+ });
+ assert.equal((await manager.captureSelectedText()).status, "none");
+});
+
+// A bare caret in VS Code copies the whole line, which must never be mistaken
+// for a selection and silently rewritten.
+test("a macOS line copy in a line-copy editor reads as no selection", async () => {
+ const { manager } = makeMacClipboardHarness({
+ copyOutput: "COPY_OK 42 Code",
+ copied: "const x = 1;\n",
+ });
+ assert.equal((await manager.captureSelectedText()).status, "none");
+});
+
+test("a failed macOS copy stays non-fatal so the command still runs", async () => {
+ const { manager } = makeMacClipboardHarness({ copied: null });
+ manager._runCopyHelper = async () => ({ success: false, stdout: "", stderr: "" });
+
+ assert.deepEqual(await manager.captureSelectedText(), {
+ status: "unavailable",
+ code: "accessibility_unavailable",
+ });
+});
+
+test("a missing copy helper stays non-fatal so the command still runs", async () => {
+ const { manager } = makeMacClipboardHarness();
+ manager.clipboardManager.resolveFastPasteBinary = () => null;
+
+ assert.deepEqual(await manager.captureSelectedText(), {
+ status: "unavailable",
+ code: "accessibility_unavailable",
+ });
+});
+
+// The clipboard fallback is macOS-only; Windows and Linux keep their own capture
+// paths untouched.
+test("a win32 read never reaches the macOS clipboard fallback", async () => {
+ let macCalled = false;
+ const manager = new SelectionManager({
+ clipboardManager: {
+ runClipboardOperation: (operation) => operation(),
+ resolveWindowsFastPasteBinary: () => null,
+ },
+ textEditMonitor: { lastTargetPid: 42 },
+ platform: "win32",
+ now: () => 1000,
+ });
+ manager._readMacSelectionViaClipboard = async () => {
+ macCalled = true;
+ return { status: "selected", text: "wrong platform" };
+ };
+
+ const result = await manager._readCurrentSelection({ kind: "win-hwnd", id: "7" });
+
+ assert.equal(macCalled, false);
+ assert.deepEqual(result, { status: "unavailable", code: "copy_helper_unavailable" });
+});
+
test("empty replacement output is rejected without consuming a paste", async () => {
const { manager, pastes } = makeHarness({ selections: ["original"] });
const capture = await manager.captureSelectedText();
diff --git a/test/helpers/textEditMonitorWindowBounds.test.js b/test/helpers/textEditMonitorWindowBounds.test.js
new file mode 100644
index 000000000..cd1c3f977
--- /dev/null
+++ b/test/helpers/textEditMonitorWindowBounds.test.js
@@ -0,0 +1,82 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const fs = require("fs");
+const os = require("os");
+const path = require("path");
+
+const TextEditMonitor = require("../../src/helpers/textEditMonitor");
+
+// The panel reposition and the screen-context capture both need to know which
+// display the dictation target is on. A stub binary stands in for the real
+// window-server read so the parsing, the fallback, and the cache are covered.
+function withStubBinary(t, script) {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ow-window-bounds-"));
+ const binary = path.join(dir, "macos-text-monitor");
+ fs.writeFileSync(binary, `#!/bin/sh\n${script}\n`, { mode: 0o755 });
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+
+ const monitor = new TextEditMonitor();
+ monitor.resolveBinary = () => ({ command: binary, args: [] });
+ return monitor;
+}
+
+const darwinOnly = { skip: process.platform !== "darwin" };
+
+test("a reported window rect is parsed", darwinOnly, async (t) => {
+ const monitor = withStubBinary(t, 'echo "BOUNDS:-451,-1440,2560,1440"');
+
+ assert.deepEqual(await monitor.getTargetWindowBounds(42), {
+ x: -451,
+ y: -1440,
+ width: 2560,
+ height: 1440,
+ });
+});
+
+test("an app with no ordinary window resolves to null", darwinOnly, async (t) => {
+ const monitor = withStubBinary(t, 'echo "NO_WINDOW"; exit 1');
+
+ assert.equal(await monitor.getTargetWindowBounds(42), null);
+});
+
+test("a failing read resolves to null rather than throwing", darwinOnly, async (t) => {
+ const monitor = withStubBinary(t, "exit 3");
+
+ assert.equal(await monitor.getTargetWindowBounds(42), null);
+});
+
+// The panel and the screenshot both ask at press time; one spawn should serve
+// both. A later dictation must still get a fresh read.
+test("reads are cached per pid for one press, then re-read", darwinOnly, async (t) => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "ow-window-bounds-count-"));
+ const counter = path.join(dir, "count");
+ const binary = path.join(dir, "macos-text-monitor");
+ fs.writeFileSync(
+ binary,
+ `#!/bin/sh\nprintf x >> ${counter}\necho "BOUNDS:0,33,1512,949"\n`,
+ { mode: 0o755 }
+ );
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+
+ const monitor = new TextEditMonitor();
+ monitor.resolveBinary = () => ({ command: binary, args: [] });
+
+ await monitor.getTargetWindowBounds(42);
+ await monitor.getTargetWindowBounds(42);
+ assert.equal(fs.readFileSync(counter, "utf8").length, 1, "second read must hit the cache");
+
+ // A different target is never served from another app's cached rect.
+ await monitor.getTargetWindowBounds(43);
+ assert.equal(fs.readFileSync(counter, "utf8").length, 2);
+
+ monitor._windowBounds.at -= 1000;
+ await monitor.getTargetWindowBounds(43);
+ assert.equal(fs.readFileSync(counter, "utf8").length, 3, "a stale entry must be re-read");
+});
+
+test("no target pid means no spawn at all", darwinOnly, async (t) => {
+ const monitor = withStubBinary(t, 'echo "BOUNDS:0,0,100,100"');
+
+ assert.equal(await monitor.getTargetWindowBounds(null), null);
+ assert.equal(await monitor.getTargetWindowBounds(0), null);
+});
diff --git a/test/helpers/windowPositionClamp.test.js b/test/helpers/windowPositionClamp.test.js
new file mode 100644
index 000000000..7ec33d880
--- /dev/null
+++ b/test/helpers/windowPositionClamp.test.js
@@ -0,0 +1,69 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+const { WindowPositionUtil } = require("../../src/helpers/windowConfig");
+
+// A 1512px laptop screen with a wider monitor mounted above it: x beyond 1512
+// is dead space at the laptop's y range, even though the desktop spans further.
+const LAPTOP = { workArea: { x: 0, y: 0, width: 1512, height: 949 } };
+
+test("pulls a window parked beyond a display edge back into its work area", () => {
+ const stranded = { x: 1472, y: 33, width: 96, height: 96 };
+ assert.deepEqual(WindowPositionUtil.clampToWorkArea(stranded, LAPTOP), { x: 1416, y: 33 });
+});
+
+test("leaves a window already inside the work area untouched", () => {
+ const inside = { x: 1412, y: 849, width: 96, height: 96 };
+ assert.deepEqual(WindowPositionUtil.clampToWorkArea(inside, LAPTOP), { x: 1412, y: 849 });
+});
+
+// A monitor mounted above (or left of) the primary display has a negative origin
+// in Electron's coordinate space. Flooring a computed position at zero lands the
+// window on a row that display doesn't cover — beside the primary screen, where
+// nothing is drawn and the overlay looks like it vanished.
+const MONITOR_ABOVE = { workArea: { x: -451, y: -1440, width: 2560, height: 1440 } };
+
+test("the panel lands on a monitor mounted above the primary display", () => {
+ const position = WindowPositionUtil.getMainWindowPosition(MONITOR_ABOVE, null, "bottom-right");
+
+ assert.deepEqual(position, { x: 2009, y: -100, width: 96, height: 96 });
+ assert.ok(position.y < 0, "a display above the primary one needs a negative y");
+});
+
+test("every panel anchor stays inside a negative-origin display", () => {
+ for (const anchor of ["bottom-right", "bottom-left", "center"]) {
+ const { x, y, width, height } = WindowPositionUtil.getMainWindowPosition(
+ MONITOR_ABOVE,
+ null,
+ anchor
+ );
+ const { workArea } = MONITOR_ABOVE;
+ assert.ok(x >= workArea.x && x + width <= workArea.x + workArea.width, `${anchor} x`);
+ assert.ok(y >= workArea.y && y + height <= workArea.y + workArea.height, `${anchor} y`);
+ }
+});
+
+test("the meeting prompt also lands on a monitor above the primary display", () => {
+ const { x, y, width, height } = WindowPositionUtil.getNotificationPosition(MONITOR_ABOVE);
+ const { workArea } = MONITOR_ABOVE;
+
+ assert.ok(y < 0, "the prompt must not be floored onto the primary display's rows");
+ assert.ok(x >= workArea.x && x + width <= workArea.x + workArea.width);
+ assert.ok(y >= workArea.y && y + height <= workArea.y + workArea.height);
+});
+
+test("a window larger than its display is pinned to the work area origin", () => {
+ const tiny = { workArea: { x: 0, y: 25, width: 800, height: 600 } };
+ const position = WindowPositionUtil.getMainWindowPosition(tiny, { width: 900, height: 700 });
+
+ assert.equal(position.x, 0);
+ assert.equal(position.y, 25);
+});
+
+test("clamps against negative-origin displays and falls back to bounds", () => {
+ const external = { bounds: { x: -451, y: -1440, width: 2560, height: 1440 } };
+ assert.deepEqual(
+ WindowPositionUtil.clampToWorkArea({ x: -900, y: -2000, width: 400, height: 500 }, external),
+ { x: -451, y: -1440 }
+ );
+});