Fix: Make the MCP command sentinel robust and fail stalled downloads fast
Re-verified every finding of the 2026-10-25 MCP test report against the source and then end-to-end on the Debug instance, and fixed the ones that live on the server. P1/P3/P4 share one root cause: the sentinel was appended to the command line, so any command that swallows the rest of its line (rem, cmd /c, if, for) swallowed the sentinel too and the call always timed out. On ConPTY clients the command and the sentinel are now sent as two lines, the sentinel prints %errorlevel% (whose echoed text is a literal template, not digits) so folding echo can no longer be mistaken for the sentinel, and FindSentinel returns the real exit code (9009, 1, 0, ...) instead of 0/1. Old non-PTY clients keep the single-line form, since their echo skipping depends on the write length. ConPTY also returns a rendered screen stream rather than a byte stream, so stripped VT sequences leave duplicate text behind; output cleaning now cuts through the echoed command line, strips the echoed sentinel line and erases any leftover sentinel text by its per-command nonce. StripAnsi no longer leaves the backslash of an OSC terminated by ST. A terminal session destroyed by a timeout used to report the same "not found" text as a typo in the session id; a 600s tombstone now says what happened instead. download_file gets a stall watchdog: no data for 60s fails with a clear message and releases the host, where a missing remote path used to hold the whole host for the full 600s default timeout. Also corrects the get_activity_history annotation (it does reach the remote host), completes the -32007/-32008/-32009 error table, documents the sentinel design in Mcp_Design.md 12.1, and corrects the report's claim that the tools blocked during a transfer are local DB reads -- they are remote machine-manager fetches, and purely server-local tools are unaffected. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
1c5fab1645
commit
eaa10cc7d3
5 files changed
+486
-71
No files matched your search
@@ -665,8 +665,31 @@ token 只从 `-Token` / env 读取,不落仓库、不打印;无在线主机
|
||||
| `-32004` | Send failed | 命令写入主连接失败(连接刚断) | 重试一次;仍失败按 `-32002` 处理 |
|
||||
| `-32005` | Not supported on this host | 需要 Windows 的工具用在 LNX/MAC/APK 主机上 | 换 Windows 主机,或改用跨平台只读工具(`list_files` 等) |
|
||||
| `-32006` | Policy refused | 只读模式 / 对应 `Mcp*` 开关关闭 / 主机不支持该能力(如 V2 文件传输) | **不要重试**:这是配置决定,需管理员改服务端配置 |
|
||||
| `-32007` | Not in command whitelist | `exec_command` 的命令不在 `McpCmdWhitelist` 里,或词边界不匹配(`dir` 过、`dirx` 拒) | **不要重试**:换成白名单内的命令,或请管理员加白名单项 |
|
||||
| `-32008` | Rejected by validation | 同一码覆盖四类语义:`exec_command` 含 shell 元字符 `&\|<>^` 或控制字符(含 `\r\n`);路径/参数含非法字符;老客户端不支持 V2 文件传输格式;`remote_open` 遇到多显示器 | 按 `message` 修正参数;多显示器/老客户端属能力限制,换目标或换主机 |
|
||||
| `-32009` | Target not found | `kill_network_connection` 的四元组在当前连接表里查不到(`dry_run` 传假四元组最常见) | 先用 `host_list_network_state` 取真实四元组再 `dry_run` 校验 |
|
||||
|
||||
- **命令超时会销毁持久会话**(`terminal_exec`):等服务端收集到哨兵超时返回 `-32001`,会话随即被销毁并留 600 秒「墓碑」;对同一 `session_id` 再发命令返回 `-32002`,文案明确写 `Terminal session was destroyed after this command timed out (waited N ms); call terminal_open again`,与「id 写错」(`Terminal session not found: <id>`)区分开。
|
||||
- **`download_file` 有停滞看门狗**:60 秒没收到任何数据(远程路径不存在/不可访问最典型)即失败,返回 `-32001` 且文案带 `File transfer stalled: no data received for 60s ...`,主机立即释放,不必等 `timeout_ms`。真实的大文件传输只要还在持续收数据就不会触发。
|
||||
|
||||
- `-32006` 会额外在审计日志里记一条 `CALL_DENIED`(§10.8),其余错误只体现在 `CALL_FAIL` 行。
|
||||
- 批量调用(带 `ids[]`)**整体不报错**:每个 id 的结果单独带 `error`,顶层用 `succeeded` / `failed` / `skipped` 汇总;只有整批请求本身非法(超过 50 个 id、写操作传 `ids`)才返回顶层 `-32602`。
|
||||
- 冒烟脚本 `tools/mcp_smoke.ps1` 覆盖了上表的可复现项:`-32601`(未知 method)、`-32602`(未知工具/缺参数/51 个 id)、`-32002`(不存在的主机)、`-32006`(任一开关关闭时自动挑一个关着的功能验证,并断言审计里出现 `CALL_DENIED`;全部开启时跳过)。其余码由各批次的功能验证覆盖(`-32001`/`-32003` 等需要构造竞态或超时,不进冒烟)。
|
||||
- 本次验证(Debug 实例,`McpKeylog` 临时置 0):`get_keylog` 返回 `-32006`,`Bin\logs\mcp_audit.log` 末尾出现 `CALL_DENIED ... tool=get_keylog reason=Keyboard log is disabled (McpKeylog=0)`;此时冒烟 34/34 通过。恢复开关后 `-32006` 与审计两条转为跳过,32/32 通过;带 `-SearchPath` 时 37/37 通过。
|
||||
|
||||
### 12.1 命令哨兵与输出清洗(测试报告 P1/P3/P4 的修复)
|
||||
|
||||
`exec_command` 与 `terminal_exec` 都靠哨兵判断「命令已执行完、退出码是多少」。哨兵有两种形态:
|
||||
|
||||
| 客户端 | 下发内容 | 退出码 |
|
||||
|---|---|---|
|
||||
| ConPTY(`is_pty=true`,新版 Windows 客户端) | 第一行 `@echo off & <cmd>`;第二行单独 `echo __MCP_DONE_<nonce>__%errorlevel%` | 命令的真实退出码 |
|
||||
| 老 ShellManager 管道(`is_pty=false`) | 单行 `<cmd> 2>&1 && echo __MCP_DONE_<nonce>__0 || echo __MCP_DONE_<nonce>__1` | 仅 0 / 非 0 |
|
||||
|
||||
**为什么 PTY 要分两行下发**:`rem hello`、`cmd /c exit 0`、`if`、`for` 这类「吞行」命令会把同一行里的 `&& echo 哨兵` 一起吞掉(`rem`/`cmd /c` 把行内剩余文本当作自己的参数),哨兵永不出现,于是必然超时并销毁会话。哨兵独立成行后由 shell 自己执行,不再受命令行影响。老客户端只能维持单行形态:`client/ShellManager.cpp` 按「写入字节数」跳过回显,多写一行会吃掉上一行的输出。
|
||||
|
||||
**为什么用 `%errorlevel%`**:ConPTY 会把输入整行回显进输出流(80 列会折行)。回显文本里是字面 `%errorlevel%` 而不是数字,而 `FindSentinel` 只认「标记 + 数字」,折行回显再也不可能被误判成哨兵(评审 P3:`ver` 曾因回显里的 `__1` 被判成 `exit_code=1`),同时还能拿到真实退出码(如 `nosuchcmd` 的 9009)。
|
||||
|
||||
**输出清洗顺序**:剥 ANSI(OSC 序列以 `ESC\` 结尾时两字节一起吞,否则会残留 `\`)→ CRLF 归一 → 从缓冲区开头删到「我们下发的命令行回显」结束(顺带清掉 shell 启动画面与提示符)→ 删哨兵行回显 → **按 nonce 擦掉残留的哨兵文本** → 去首尾空行。最后一步是必需的:ConPTY 送回来的是**屏幕渲染流**(带光标移动/擦除序列),剥掉序列后同一行可能重复出现,哨兵会留下副本(实测不擦时 `rem hello` 只剩哨兵、`cmd /c echo abc` 成了 `abc__MCP_DONE_x__0`)。nonce 每条命令随机生成,命令的真实输出不可能包含它,所以按 nonce 擦除不会误伤业务输出。
|
||||
|
||||
**已知边界**:屏幕渲染流里「光标下移空行」会被剥成紧邻行(例如 `ping` 的统计块会贴在最后一条回复后面),不影响语义。
|
||||
Reference in new issue
Block a user