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:
yuanyuanxianganddeepseek-flash committed 2026-10-25 17:09:39 +01:00
1 parent 1c5fab1645
commit eaa10cc7d3
5 files changed
+486 -71

No files matched your search

+27 -5
View File
@@ -186,10 +186,16 @@ public:
// 登记持久终端挂起(复用单设备单终端约束;sessionId 由调用方生成)。
bool BeginTermOpen(uint64_t device_id, const std::string& sessionId);
// 复位单条命令字段并置 busy;输出 subCtx/isPty。false = 不存在/不匹配/未就绪/忙碌/已关。
bool BeginTermCommand(uint64_t device_id, const std::string& sessionId,
const std::string& command, const std::string& nonce,
context*& subCtx, bool& isPty);
// 复位单条命令字段并置 busy;输出 subCtx/isPty。
// 返回:0=ok;1=会话不存在;2=sessionId 不匹配;3=未就绪;4=忙(另一条命令在飞);
// 5=shell 已退出。1/2 之外的原因由 DescribeTermDestroyed 提供可读文案。
int BeginTermCommand(uint64_t device_id, const std::string& sessionId,
const std::string& command, const std::string& nonce,
context*& subCtx, bool& isPty);
// 若该设备上的持久终端最近被销毁(超时/退出/关闭),返回可读原因(英文,含时间);
// 否则返回空串。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,
@@ -437,6 +443,10 @@ private:
// POST /mcp 处理器:token 校验 + JSON-RPC 分发。
void HandleMcp(const httplib::Request& req, httplib::Response& res);
// 记一条「持久终端已销毁」墓碑(调用方须持有 m_TermMutex)。
void MarkTermDestroyedLocked(uint64_t device_id, const std::string& sessionId,
const std::string& reason);
// ===== 鉴权加固 =====
// 同一 IP 在窗口期内连续鉴权失败达到阈值即封禁一段时间;封禁期内直接 429,不再比对 token。
bool IsAuthBlocked(const std::string& ip);
@@ -559,6 +569,15 @@ private:
std::condition_variable m_TermCv;
std::map<uint64_t, TermSession> m_TermSessions; // device_id → 会话
std::map<context*, uint64_t> m_TermContextToDevice; // subCtx → device_id(顶部路由)
// 最近一次持久终端被销毁的原因(device → 信息)。终端因超时/退出/关闭被清理后,后续
// terminal_exec 无法区分「会话被超时销毁」与「session_id 写错」,只能报同一句
// "not found"(评审 P1)。留一条短墓碑,让错误文案能说清是哪种情况。
struct TermDestroyedInfo {
std::string sessionId;
std::string reason; // 英文短句,直接拼进 -32002 文案
std::chrono::steady_clock::time_point expiry;
};
std::map<uint64_t, TermDestroyedInfo> m_TermDestroyed; // device_id → 墓碑(10 分钟内有效)
// ===== P5:远程控制会话(受 m_ScreenCtrlMutex 保护;单设备单会话)=====
struct ScreenCtrlSession {
@@ -588,11 +607,14 @@ private:
uint32_t directoryCount = 0; // 目录项计数(目录不发 COMPLETE 包)
uint32_t filesDone = 0; // 已完成的文件数(含跳过的)
bool done = false; // 全部完成或出错
int error = 0; // 0=ok;8=哈希不匹配;1001=路径逃逸;1002=超时;1003=流式子连接断线
int error = 0; // 0=ok;8=哈希不匹配;1001=路径逃逸;1002=超时;1003=流式子连接断线;1004=停滞(看门狗)
std::map<uint32_t, FileTransferEntry> fileEntries; // fileIndex → {path,size,sha256}
std::set<uint32_t> skipIndexes; // overwrite=false 跳过的 fileIndex
std::vector<FileTransferEntry> skipped; // 跳过的文件(供输出)
time_t startAt = 0;
// 停滞看门狗:最后一次收到数据的时间。远程路径不存在时客户端一个包都不发,
// 旧实现只能干等满 timeout_ms(默认 600s,整台主机被单飞互斥锁住,评审 P2)。
std::chrono::steady_clock::time_point lastDataAt;
};
std::mutex m_FileXferMutex;
std::condition_variable m_FileXferCv;