Feature: Add window info RPC for foreground checks and window activation

Add a capability-gated main-connection RPC (COMMAND_MCP_WINDOW_INFO=179 /
TOKEN_MCP_WINDOW_INFO=180) that returns visible top-level windows with UTF-8
titles, screen rectangles, foreground flag and z-order, so MCP can verify
focus before injecting input. New tool get_foreground_window; host_list_windows
switches to the richer fields; host_control_window and operate_window gain
action=activate; remote_keyboard and remote_mouse gain expect_hwnd,
require_foreground and report_foreground. Old clients keep the previous
behavior: the RPC is refused up front via CLIENT_CAP_WINDOW_INFO.

The client caps a response at 96 windows (about 101 KB, under MAX_SEND_BUFFER).
Signal the cut-off in the mode byte (0x80) instead of silently dropping windows:
host_list_windows now reports truncated=true and says the list is incomplete,
and activate verification reports state=unknown with reason=list_truncated
rather than claiming the window is gone. expect_hwnd is checked only after the
session is validated, so an invalid session_id no longer burns a window RPC and
no longer reports a window error for a session that does not exist. Nullable
output fields (foreground, window) are declared as ["object","null"] so strict
clients do not reject responses.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-10 15:09:24 +01:00
1 parent c7e23f750d
commit b2c0d81396
18 files changed
+1298 -68

No files matched your search

+61
View File
@@ -1002,3 +1002,64 @@ schema 里写明这一点;同时根键分支补上 `path: ""`,让两个分
- 没有 `since`(按时间起点取)。记录里的时间是 `[YYYY-MM-DD HH:MM:SS]` 文本前缀,做时间
过滤得先解析每行;当前 `limit/offset` 已够用。
## 20. 增补(2026-11-10:窗口信息 RPC 与前台窗口校验)
### 20.1 问题
GUI 自动化的失败大多不是「事件没发出去」,而是「事件进了别的窗口」:后台窗口
`ShowWindow` 不会升到前台,`remote_keyboard` / `remote_mouse` 又只能按屏幕坐标注入,
调用方无法确认焦点是否在目标窗口上。旧的窗口列表(tab 1 / `COMMAND_WSLIST`)只有
标题和尺寸,没有屏幕坐标、前后台与 z 序,且标题是 ANSI/GBK。
### 20.2 协议
新增能力位 `CLIENT_CAP_WINDOW_INFO=0x0100`(仅 Windows 客户端在 `DefaultCaps()` 置位)。
声明该能力位的客户端支持一个**主连接** RPC:
```
请求 [COMMAND_MCP_WINDOW_INFO(179)][mode:1] // mode 0 = 可见顶层窗口
响应 [TOKEN_MCP_WINDOW_INFO(180)][mode:1][count:u16 LE][WINDOWINFO_MCP × count]
```
`WINDOWINFO_MCP` 为 1056 字节 packed:`hwnd/pid/left/top/right/bottom/flags/zorder/title[1024]`,
标题 UTF-8;`flags` 含 `VISIBLE/MINIMIZED/MAXIMIZED/FOREGROUND`。客户端在 worker 线程
枚举(`GetWindowTextW` 会给挂死的窗口发消息),单机同时只允许一次枚举在飞,最多返回
96 个窗口(约 101 KB,低于 `MAX_SEND_BUFFER`);可见窗口更多时响应把 `mode` 的最高位
(`0x80`)置 1,表示返回的 `count` 条只是前缀、列表不完整。服务端复用主连接的
`BeginPending` / `WaitPending`,由 `2015RemoteDlg::MessageHandle` 的
`TOKEN_MCP_WINDOW_INFO` 分支调用 `TakeMainResponse`,不建子连接、不弹框。
老客户端不认识 179,服务端按能力位提前拒绝(`-32005`),不会白等超时。
### 20.3 新工具与参数
- **`get_foreground_window`**(第 61 个工具,仅 Windows):返回
`{windowCount, window|null}`,`window` 含 hwnd/pid/title/rect/flags/z 序。
- **`host_list_windows`**:能力位存在时改走新 RPC,字段扩展为
`left/top/right/bottom/visible/minimized/maximized/isForeground/zOrder`;否则回落旧的
tab 1(字段不变)。过滤与分页完全不变。
- **`host_control_window` / `operate_window` 新增 `activate`**:下发
`CMD_WINDOW_ACTIVATE_FLAG(0xFFFFFFFF)` 哨兵。客户端用
`AttachThreadInput + BringWindowToTop + SetForegroundWindow` 把窗口真正升到前台;
动作后服务端再发一次窗口信息 RPC 复核 `GetForegroundWindow`,未成功返回
`ok=false, reason=not_foreground`,而不是「已下发」。
- **`remote_keyboard` / `remote_mouse` 新增可选参数**:
- `expect_hwnd`:注入前确认窗口仍在可见列表里;`require_foreground=true` 还要求它是
前台,否则**不发注入**并返回 `-32000`;
- `report_foreground`:注入后回报前台窗口(`structuredContent.foreground`);给了
`expect_hwnd` 时默认开启。回报失败只写 `foregroundError`,不把已发生的注入判失败。
### 20.4 兼容与代价
- 所有新能力都挂在 `CLIENT_CAP_WINDOW_INFO` 后面;老客户端行为逐字节不变。
- 每次窗口信息 RPC 是一次主连接往返;只在 `host_list_windows`、`get_foreground_window`、
`activate` 和显式给了 `expect_hwnd` / `report_foreground` 时发生。
- `activate` 仍可能被 Windows 前台锁定策略拒绝(用户正在操作、目标进程有模态框),
工具如实返回失败;`hide` 后窗口从可见列表消失、无法再被 `activate`,这一旧限制不变。
- 窗口列表只含**可见**顶层窗口,且按 z 序从上到下截取前 96 个:被 `hide`、最小化到托盘
等不可见窗口查不到;桌面窗口超过 96 个时,排在后面的窗口不会出现。客户端会在响应
里置截断位,服务端据此如实报告:`host_list_windows` 返回 `truncated=true` 且正文说明
列表不完整;`expect_hwnd` 查不到时 `-32009` 的文案区分「窗口可能还在、只是没返回」
与「隐藏/已关闭」;`activate` 复核时目标不在返回的这一批里则记 `state=unknown`、
`reason=list_truncated`,而不是 `missing`。任何情况下都不要据截断的列表断定窗口已销毁。