Feature(mcp): let terminal_exec open or reuse a session implicitly

terminal_exec no longer requires session_id. Given a command and no
session_id, it reuses the host's persistent terminal session or opens one
through the same path as terminal_open, and reports session_id, is_pty and
reused in the result so the caller can keep using it. Polling still
requires the session token.

terminal_open now returns the existing session with reused:true instead of
-32003 when one is already active. An implicitly created session is
invisible to the caller, so rejecting would silently lock the host's single
terminal slot and leave exec_command failing with a busy error. A session
that is still starting, or one owned by a one-shot exec_command, still
answers -32003.

The server instruction line that told models to always pair terminal_open
with terminal_close is updated accordingly.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 16:01:13 +01:00
1 parent 3fe3a11494
commit 0378e96470
3 files changed
+132 -19

No files matched your search

+9
View File
@@ -258,6 +258,15 @@ public:
// 登记持久终端挂起(复用单设备单终端约束;sessionId 由调用方生成)。
bool BeginTermOpen(uint64_t device_id, const std::string& sessionId);
// terminal_exec 不带 session_id 时:复用该主机已有的持久会话;没有就登记一个挂起会话。
// 返回:0=ok(reused=true 复用已有会话;false=本次新建,调用方需发 COMMAND_SHELL 并
// WaitTerminalReady);2=已有会话但尚未就绪(并发 open,调用方报 -32003)。
int OpenOrReuseTermSession(uint64_t device_id, std::string& sessionId, bool& reused);
// terminal_open 撞上已有会话时复用它(不再报 -32003):0=ok(输出 token 与 isPty);
// 2=已有会话但尚未就绪(有 open 在并发建立中)。
int ReuseTermSession(uint64_t device_id, std::string& sessionId, bool& isPty);
// 复位单条命令字段并置 busy;输出 subCtx/isPty。
// 返回:0=ok;1=会话不存在;2=sessionId 不匹配;3=未就绪;4=忙(另一条命令在飞);
// 5=shell 已退出。1/2 之外的原因由 DescribeTermDestroyed 提供可读文案。