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:
1 parent
b63c97c5eb
commit
3fe3a11494
3 files changed
+339
-134
No files matched your search
@@ -273,9 +273,21 @@ public:
|
||||
// 否则返回空串。sessionId 不匹配时也返回空串(那是「id 写错」而不是「已销毁」)。
|
||||
std::string DescribeTermDestroyed(uint64_t device_id, const std::string& sessionId);
|
||||
|
||||
// 等待本条命令完成(done || closed || 超时)。done=保持会话;closed/超时=清理会话+路由。
|
||||
bool WaitTermCommand(uint64_t device_id, std::vector<BYTE>& out, int& exitCode,
|
||||
bool& closed, int timeoutMs);
|
||||
// 等待本条命令结束或超时,并交付 [delivered, ...) 的增量输出。
|
||||
// pollMode=true 表示本次调用没有携带新命令(只轮询),此时会话空闲 = 调用方用错,返回 7。
|
||||
// 返回:0=命令结束(done=true;closed=true 表示 shell 退出,会话已清理)
|
||||
// 1=等待超时,命令仍在运行(done=false;会话保持 busy,可再次轮询)
|
||||
// 2=请求被取消,会话已销毁(与旧语义一致:取消方不会再轮询)
|
||||
// 3=会话不存在/未就绪/等待期间被销毁(细节看 DescribeTermDestroyed)
|
||||
// 4=sessionId 不匹配
|
||||
// 7=轮询时没有在跑的命令(会话空闲)
|
||||
// 每次最多交付 maxOutputBytes 原始字节(按换行边界回退);truncated=本次是否仍有剩余。
|
||||
// droppedBytes=该命令因 data 硬上限被丢弃的字节数(累计,如实上报)。
|
||||
int WaitTermCommand(uint64_t device_id, const std::string& sessionId,
|
||||
std::vector<BYTE>& out, bool& done, int& exitCode, bool& closed,
|
||||
bool& truncated, size_t& droppedBytes,
|
||||
bool& isPty, std::string& nonce,
|
||||
int maxOutputBytes, int timeoutMs, bool pollMode);
|
||||
|
||||
// 关闭持久终端(校验 sessionId;CancelIO + 清理路由/会话)。
|
||||
// 返回:0=已关闭;1=会话不存在(幂等);2=sessionId 不匹配(调用方报错)。
|
||||
@@ -828,6 +840,10 @@ private:
|
||||
bool busy = false; // 一条命令在飞(terminal_exec 复位→WaitTermCommand 返回)
|
||||
time_t lastActiveAt = 0; // idle 回收用(秒)
|
||||
std::chrono::steady_clock::time_point startedAt = std::chrono::steady_clock::now(); // P11
|
||||
// ===== 超时轮询(P0-2)=====
|
||||
size_t delivered = 0; // 已交付给调用方的 data 前缀长度(超时/轮询增量回传)
|
||||
size_t droppedBytes = 0; // 因 data 硬上限被丢弃的最旧字节数(如实上报)
|
||||
std::chrono::steady_clock::time_point busySince{}; // busy 起点(长时间在飞的强制回收兜底)
|
||||
};
|
||||
std::mutex m_TermMutex;
|
||||
std::condition_variable m_TermCv;
|
||||
|
||||
Reference in new issue
Block a user