Feature(Go): terminal_close, idle sweep and legacy-client guard

Add the terminal_close tool so a caller can release a shell session
explicitly instead of waiting for it to age out. The tool is idempotent
for the session it names, rejects a session_id that does not match the
live session with -32002, and runs without the per-device lock so it can
interrupt a terminal_exec call that is still polling on the same device.

Give hub leases a generation counter: the generation is bumped when a
pending lease is created and carried across a device re-login. MCP-side
closes pass the generation, so a stale session can no longer tear down a
shell a human operator opened after the device reconnected. The reaper
runs on a 30 s ticker (shortened when TermIdle is smaller), skips
sessions that are still opening, and reclaims idle sessions as well as
commands overdue past the one-hour busy cap.

The pre-bind terminal-close path in cmd no longer closes
unconditionally; it touches only a pending lease, because an unbound
sub-connection carries no lease identity and may be a stale orphan.

Covered by unit tests for close semantics, lock exemption, reaper
lifecycle and lease-generation ownership, plus a live run against a
Debug ghost: close before and during a command, reopen, idle reap and
mismatched-id rejection all behave as specified.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 21:53:54 +01:00
1 parent 599c891c00
commit 808747f9d2
10 files changed
+633 -52

No files matched your search

+52
View File
@@ -54,8 +54,60 @@ type terminalExecOutput struct {
ElapsedMS int64 `json:"elapsed_ms"`
}
// terminalCloseArgs is the published inputSchema as Go.
type terminalCloseArgs struct {
ID string `json:"id"`
SessionID string `json:"session_id"`
}
type terminalCloseOutput struct {
Closed bool `json:"closed"`
}
func (s *Server) registerTerminalTools() {
s.handlers["terminal_exec"] = s.callTerminalExec
s.handlers["terminal_close"] = s.callTerminalClose
}
// callTerminalClose releases the persistent session MCP opened for a device.
//
// It is idempotent for the session it is given: a session that is already gone
// still answers closed, so a retried close after a timeout does not fail. Only
// sessions in MCP's own table are touched — the web console's shell is left
// alone, and the hub-side close is lease-checked, so a session whose lease was
// taken over is never torn down.
//
// A session_id that names a different live session is a caller mistake
// (-32002), mirroring the Win32 server (McpServer.cpp BuildTerminalClose).
func (s *Server) callTerminalClose(_ context.Context, raw json.RawMessage) (any, *rpcError) {
if s.cfg.Hub == nil {
return nil, errf(CodeInternal, "server device registry is not configured")
}
var args terminalCloseArgs
if rerr := decodeArgs(raw, &args); rerr != nil {
return nil, rerr
}
if args.ID == "" {
return nil, errf(CodeInvalidParams, "Missing required parameter: id")
}
if !isDecimalID(args.ID) {
return nil, errf(CodeInvalidParams, "Invalid id: expected a decimal host id string")
}
if args.SessionID == "" {
return nil, errf(CodeInvalidParams, "Missing required parameter: session_id")
}
if sess := s.sessions.get(args.ID); sess != nil {
if sess.id != args.SessionID {
return nil, errf(CodeOffline, "Terminal session_id mismatch: %s", args.SessionID)
}
// Wakes any terminal_exec still waiting on the command; it delivers the
// bytes buffered so far (done=true, exit_code=-1) before it returns.
s.sessions.closeSessionIf(args.ID, sess)
}
return toolResult{
Content: []contentItem{{Type: "text", Text: "closed"}},
StructuredContent: terminalCloseOutput{Closed: true},
}, nil
}
// callTerminalExec runs one command or polls the output of the previous one.