Feature(mcp): keep terminal sessions on timeout and allow output polling

terminal_exec used to destroy the persistent session on timeout, so long
commands (robocopy, upgrade scripts) could never be observed: the collected
output was thrown away and the shell was killed. Waiting is now decoupled from
the command:

- WaitTermCommand delivers [delivered, ...) and no longer erases on timeout.
  It returns done:false plus the output produced so far; the session stays
  busy and polling continues until done:true with exit_code.
- Calling terminal_exec with only session_id polls (no command); command is
  optional and the result gains session_id / done / truncated / dropped_bytes
  / elapsed_ms.
- Request cancellation still destroys the session (-32800 path); shell exit
  and terminal_close deliver the remaining output and then clean up.
- Chunks are capped at max_output_bytes (default 64KB, newline-aligned); the
  per-command buffer is capped at 8MB and reports dropped_bytes; busy sessions
  are force-reclaimed after an hour so a host cannot stay busy forever.
- A finished command with undrained output returns -32003 with a drain hint
  instead of silently losing it when the next command clears the buffer.

Verified on an isolated Debug instance against a Windows host: a 3s timeout on
an 8s ping returns done:false + partial output and two polls finish it; a
277KB dir listing needs 5 polls and then accepts a new command; 200 echo lines
delivered in 12 chunks arrive exactly once in order; cancel returns -32800 and
destroys the session. tools.json regenerated (63 tools, PASS).

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 15:53:33 +01:00
1 parent b63c97c5eb
commit 3fe3a11494
3 files changed
+339 -134

No files matched your search

+34 -10
View File
@@ -2235,7 +2235,7 @@
},
{
"name": "terminal_exec",
"description": "在已打开的持久会话中执行一条命令并返回 stdout 与退出码。命令不受白名单约束。PTY 会话(新 Windows 客户端 ConPTY / Linux 与新版 Android 的 POSIX PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联,适合「ls -1 | wc -l」这类先过滤再回传的用法;不要用后台执行符 &(POSIX)或换行。老非 PTY Windows 客户端仍不支持 & 与 |,请拆成多条调用。",
"description": "在已打开的持久会话中执行一条命令,或轮询取回正在运行的命令输出。带 command 时执行并在 timeout_ms 内等待;等待超时返回 done:false 与已产生的部分输出,会话不销毁(命令仍在远端运行)。不带 command、只传 session_id 时进入轮询模式:继续等待并取回增量输出,直到 done:true(此时有 exit_code)。命令不受白名单约束。PTY 会话(新 Windows 客户端 ConPTY / Linux 与新版 Android 的 POSIX PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联,适合「ls -1 | wc -l」这类先过滤再回传的用法;不要用后台执行符 &(POSIX)或换行。老非 PTY Windows 客户端仍不支持 & 与 |,请拆成多条调用。",
"gate": "terminal",
"write": true,
"platforms": [
@@ -2247,43 +2247,67 @@
"inputSchema": {
"properties": {
"command": {
"description": "要执行的命令(完整 shell,不受白名单约束)。PTY 会话(Windows ConPTY / Linux PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联;不要用后台执行符 &(POSIX)或换行。老非 PTY 客户端仍不支持 & 与 |,请拆成多条调用。Linux 下通过 POSIX shell 执行,可用 ; 串联且退出码取最后一条命令",
"description": "要执行的命令(完整 shell,不受白名单约束)。省略时进入轮询模式:不执行新命令,只等待并取回当前会话的增量输出(上一条命令超时后用这个方式继续取)。PTY 会话(Windows ConPTY / Linux PTY)下可用管道 | 与 && / || 串联,退出码取整条管道/串联;不要用后台执行符 &(POSIX)或换行。老非 PTY 客户端仍不支持 & 与 |,请拆成多条调用。Linux 下通过 POSIX shell 执行,可用 ; 串联且退出码取最后一条命令",
"type": "string"
},
"id": {
"description": "主机 id,取 list_online_hosts / search_hosts 返回的 id 字段",
"type": "string"
},
"max_output_bytes": {
"description": "单次返回的原始输出上限(可选,默认 65536,最小 1024,最大 1048576);超出按换行边界截断并置 truncated:true,再轮询一次可取回剩余",
"type": "integer"
},
"session_id": {
"description": "terminal_open 返回的 session_id",
"type": "string"
},
"timeout_ms": {
"description": "等待输出完成的超时毫秒数(可选,默认 20000,上限 600000)",
"description": "本次调用最多等待的毫秒数(可选,默认 20000,上限 600000)。等待超时不算失败:返回 done:false 与已产生的输出,命令仍在远端运行,继续轮询即可",
"type": "integer"
}
},
"required": [
"id",
"session_id",
"command"
"session_id"
],
"type": "object"
},
"outputSchema": {
"properties": {
"exit_code": {
"description": "退出码:PTY 客户端(Windows ConPTY / Linux PTY)为命令的真实退出码;老客户端(非 PTY)仅区分 0/非 0;-1=未知(进程异常退出)",
"done": {
"description": "false=命令仍在运行,用同一工具只传 session_id 继续取增量;true=命令已结束,exit_code 有效",
"type": "boolean"
},
"dropped_bytes": {
"description": "因单条命令输出超过 8MB 上限被丢弃的最旧字节数(该命令累计,0 或省略=无丢失)",
"type": "integer"
},
"stdout": {
"description": "命令输出(已剥哨兵与 ANSI 转义)",
"elapsed_ms": {
"description": "本次调用实际等待的毫秒数",
"type": "integer"
},
"exit_code": {
"description": "退出码(done=true 时提供):PTY 客户端(Windows ConPTY / Linux PTY)为命令的真实退出码;老客户端(非 PTY)仅区分 0/非 0;-1=未知(shell 进程退出)",
"type": "integer"
},
"session_id": {
"description": "会话 token,原样回传,便于续接轮询",
"type": "string"
},
"stdout": {
"description": "本次新增的输出(已剥哨兵与 ANSI 转义)",
"type": "string"
},
"truncated": {
"description": "true=本次受 max_output_bytes 限制仍有剩余输出,立即再轮询即可取回",
"type": "boolean"
}
},
"required": [
"stdout",
"exit_code"
"done",
"session_id"
],
"type": "object"
}