1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! Windows [`Backend`] for the Computer tool: UI Automation for observe/act,
//! `SendInput` for pointer and keyboard synthesis, and Windows.Graphics.Capture
//! with GDI fallbacks for stills.
//!
//! # One dedicated apartment thread
//!
//! Every accessibility and capture call runs as a job on ONE thread of its own, created
//! lazily by the first call that needs it and initialized into a multithreaded apartment —
//! `RoInitialize(RO_INIT_MULTITHREADED)` plus the `CUIAutomation8` client with bounded
//! provider timeouts. Nothing here touches the agent's thread or the interface thread: the
//! [`Backend`] methods only build a plain-data [`thread::Call`] and await its reply, so no
//! COM interface crosses a thread boundary and the GUI cannot be blocked by a slow provider.
//!
//! # No call may hang
//!
//! Three bounds stack: each provider call is bounded client-side by the UIA timeouts this
//! thread sets; a walk runs under [`uia::WALK_DEADLINE`], a budget checked before every
//! provider read, so a stalled target costs one transaction; and the whole job is awaited
//! under a per-call deadline sized for what that shape pays. A provider that stalls on
//! several calls inside one job can still reach the job deadline: the thread is marked dead,
//! the next call starts a fresh MTA thread, and the wedged one is left to the OS (Windows
//! offers no safe way to interrupt it). A read then fails as a `degraded` timeout; an
//! actuation answers applied-but-unconfirmed. One thread is leaked per hang and none is
//! reused: the running count is reported in that answer and logged with it.
//!
//! # Structured exceptions
//!
//! Foreign code can fault rather than fail, and Rust can neither catch a structured exception
//! nor unwind past it. Every job, the apartment's setup and its teardown therefore run inside
//! the one C frame this backend owns ([`seh::guard`]). A caught exception fails the call with
//! `framework-failed`, naming the platform's exception code in hex, and retires the thread:
//! the state it owned is unknown, so the context is abandoned rather than released. [`seh`]
//! owns what a per-call frame cannot cover — a fault on a driver's own thread, and the Rust
//! panics, control-flow codes and stack overflows the frame must let pass.
//!
//! # Coordinates and DPI
//!
//! Everything here is PHYSICAL pixels: UIA rectangles, window frames, the virtual desktop rect
//! and pointer positions all agree once the process is per-monitor DPI aware. iced's winit
//! event loop makes it so at startup; the thread additionally asks for per-monitor-v2
//! awareness once, eagerly — a `cargo`-launched CLI run has no event loop, and a lazily-set
//! awareness would mix physical UIA bounds with virtualized window metrics.
//!
//! # Window identity
//!
//! A window target carries the window's own OS handle plus the pid that owned it
//! ([`window::resolve`] builds and parses that key), so it either lands on that window or
//! fails as `surface-gone`: a moved, resized or renamed window keeps resolving, a closed or
//! replaced one never silently becomes another window of the same application. An app target
//! names no window — it is the application's own focused window, re-resolved per call.
//!
//! # Actuation limits, stated rather than worked around
//!
//! [`window::ensure_actuation_allowed`] owns two refusals and names which one fired:
//!
//! * A window owned by an elevated process cannot be driven by this non-elevated one: Windows
//! blocks both synthetic input (UIPI) and accessibility actions into it. A CAPTURE of it may
//! still work, because the compositor serves the frame. Starting elevated is out of scope.
//! * The UAC secure desktop and the lock screen live on another desktop: nothing can be read
//! or actuated there, and no workaround is attempted.
//!
//! [`window::ensure_not_own_window`] adds the one refusal that is ours alone: an ELEMENT
//! ACTION on a window of mahbot itself. UI Automation answers our own process in-process, and
//! this backend has no path onto the interface thread the dashboard owns, so a press or a
//! value write could freeze the dashboard or kill the service. It is refused as
//! `self-target`; our windows stay listed, readable and capturable, and raw input still
//! reaches them.
//!
//! `cmd` in a key chord is the Windows key here (Super on Linux, Command on macOS); `win` is
//! accepted as a key name for a plain Windows-key tap.
//!
//! # Capture
//!
//! [`capture`] owns the route order: Windows.Graphics.Capture first, then a desktop `BitBlt`.
//! [`capture::take_route`] is the one statement of the frame rule every route obeys.
//! Consequences accepted rather than engineered around:
//!
//! * The platform's capture facility draws its own visible indicator around what it captures;
//! the GDI fallback has none. Removing it needs a packaged, specially entitled application,
//! which this product deliberately is not.
//! * The facility needs Windows 10 1903; older builds fall through to GDI, so observation
//! degrades instead of failing.
//! * Some applications hand it black frames (protected content, a surface that refuses
//! readback); that is reported honestly.
//! * A frame is expressed against the window's surface whatever extent the compositor reports
//! ([`capture::align_to_surface`]); one matching neither convention means the window changed
//! size during the grab and is refused.
//! * Availability is per target and per route, never cached per window:
//! [`Backend::capture_available`] answers for the desk itself. The tool layer caches that
//! answer for a minute, so a failure while it is positive keeps its per-route diagnosis.
//!
//! # Manual check (run once on a real Windows machine)
//!
//! Build with `cargo build --release`, run the bare executable from a normal Command Prompt
//! with `set HOME=C:\Users\<you>\mahbot-check`, sign in as admin and talk to the Assistant.
//! Check, in order: the dashboard starts; `observe` with no target returns a
//! `[Surface: app=… window=…]` tree (not "unknown tool"); a Notepad tree carries the typed
//! sentence as a `value`; a Calculator button `click { ref }` and `press { keys }` behave;
//! `screenshot`/`zoom` return the window and a minimized one is refused as `degraded`; an
//! elevated Notepad is refused `permission-denied` for input and still captures, and a locked
//! workstation answers `desktop-unavailable` rather than hanging; and
//! `cargo test --lib tools::computer::windows` passes with the harness alive — the three
//! `seh` tests cover a null-read fault, a foreign C++ throw and a Rust panic.
// The Win32 surface this backend speaks (C-style integer geometry, i32/u32 pixel dimensions,
// RAW handles) makes these pedantic cast lints inherent.
use Call;
use SECURE_DESKTOP_REASON;
use ;
use async_trait;
use Duration;
// One bound per call shape, sized for what that shape actually pays: for the accessibility
// shapes, the common path plus ONE provider transaction. The UIA timeouts end a single
// unresponsive provider call, and [`uia::WALK_DEADLINE`] — a [`super::core::Budget`] checked
// before every provider read, whose error names the window — ends a walk before the job
// deadline fires. A provider that stalls on more than one call inside a job can still reach
// the job deadline, and that error names the same surface. An actuating call answers
// applied-but-unconfirmed there, a read stays an error, and a deadline that expires before
// the job started is the plain `degraded` failure.
/// App/window enumeration and geometry: plain Win32, no provider involved.
const LIST_TIMEOUT: Duration = from_secs;
/// A full accessibility read: the walk plus one transaction.
const OBSERVE_TIMEOUT: Duration = from_secs;
/// An element action — the walk, the pattern it obtains, the action's own call — or a raw
/// input batch, which pays none of those.
const ACT_TIMEOUT: Duration = from_secs;
/// One still, every route included: the screen stitch is cut at `SCREEN_BUDGET`, then the
/// GDI route runs.
const CAPTURE_TIMEOUT: Duration = from_secs;
/// The desktop probe: user32 calls that need no apartment and no provider, bounded on a
/// blocking thread so a wedged desktop switch costs this and not the caller's budget.
const PROBE_TIMEOUT: Duration = from_secs;
/// Whether an interactive desktop is attached, or `None` when the probe could not answer.
async
pub static WINDOWS_BACKEND: WindowsBackend = WindowsBackend;
pub ;