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

+11 -5
View File
@@ -68,8 +68,9 @@ type Server struct {
// locks serialises device operations; sem bounds the global in-flight
// tools/call count. Both are non-blocking: contention is -32003, not a
// queue, so a slow call cannot pile up requests behind it (design §9).
locks *deviceLocks
sem chan struct{}
locks *deviceLocks
sem chan struct{}
sessions *sessionTable
}
// New builds the endpoint. Zero-valued knobs fall back to the documented
@@ -102,6 +103,11 @@ func New(cfg Config) *Server {
locks: newDeviceLocks(),
sem: make(chan struct{}, cfg.MaxConcurrency),
}
if cfg.Hub != nil {
s.sessions = newSessionTable(cfg.Hub)
} else {
s.sessions = newSessionTable(nil)
}
s.registerLocalTools()
return s
}
@@ -111,9 +117,9 @@ func New(cfg Config) *Server {
// unknown path.
func (s *Server) Enabled() bool { return s != nil && s.cfg.Token != "" }
// Close releases resources held by long-lived tool state. M1 has none; the
// terminal session table plugs in here in M4.
func (s *Server) Close() {}
// Close releases resources held by long-lived tool state: every terminal
// session MCP owns is torn down and the shell told to exit.
func (s *Server) Close() { s.sessions.closeAll() }
// Handle serves POST /mcp. Transport-level failures (405/401/413) are answered
// here; everything after a successfully parsed JSON-RPC request returns HTTP