Feature(Go): MCP terminal sessions with a 256 KiB ring buffer

Add the server-side half of the terminal tools: a per-device session
table that claims the hub's single-viewer shell lease for MCP, and an
output ring that survives a timed-out request so a later poll can still
collect the command's result (terminal_exec itself arrives in M4b).

The ring keeps the newest 256 KiB of shell output, tracks absolute
offsets, and counts dropped bytes so callers can report truncation. A
sentinel scanner looks for the per-command done marker, skipping the
echoed command line and accepting Windows' negative error codes; its
scan position is incremental with a small overlap so a marker split
across TCP reads is still found.

Server implements hub.EventHandler and cmd subscribes it when the
endpoint is enabled; only the three terminal callbacks do work, and
OnTerminalData stays cheap on the TCP read loop (lookup, append, scan,
broadcast). Opening inserts the session before calling the hub so an
early OnTerminalReady cannot be lost, stale close events that race a
fresh open are ignored while the hub still holds the lease, and idle
sweeps verify session identity. The reaper and terminal_close land in
M4c.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 21:14:53 +01:00
1 parent 1076e7614a
commit 12b1bd4786
6 files changed
+876 -5

No files matched your search

+10
View File
@@ -798,6 +798,16 @@ func (h *Hub) IsTerminalPending(deviceID string) bool {
return ok && d.terminalPending
}
// HasTerminalSession reports whether the device currently has a pending or
// active shell lease. MCP uses it to tell a genuine terminal close from a
// stale one that raced a fresh open (see mcp.sessionTable.closed).
func (h *Hub) HasTerminalSession(deviceID string) bool {
h.mu.RLock()
defer h.mu.RUnlock()
d, ok := h.devices[deviceID]
return ok && (d.terminalConn != nil || d.terminalPending)
}
// BindTerminalConn promotes the pending session to an active one by
// associating the device's freshly-arrived shell sub-conn. Returns false
// if no pending session exists — callers should drop the orphan ctx.