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
@@ -1305,6 +1305,7 @@ func main() {
// auto-generated token, because a secret that only appears in a log is not
// a secret. Version/commit come from the link-time buildinfo injection.
mcpReadonly := envBool("YAMA_MCP_READONLY")
var mcpUnsubscribe func()
bindAddr := ""
if *httpPort != 0 {
bindAddr = "0.0.0.0" // web server listens on :port (all interfaces)
@@ -1325,6 +1326,9 @@ func main() {
Bind: bindAddr,
})
if mcpSrv.Enabled() {
// Terminal tools need the hub's shell events (ready/data/closed).
// Unsubscribed again at shutdown, before the TCP servers stop.
mcpUnsubscribe = deviceHub.Subscribe(mcpSrv)
log.Info("MCP endpoint enabled on /mcp (readonly=%v, commit=%s)",
mcpReadonly, buildinfo.Commit)
}
@@ -1404,6 +1408,12 @@ func main() {
log.Warn("signer Close: %v", err)
}
httpSrv.Stop()
// Detach from hub events first, then tear down the shells MCP opened while
// the TCP sender is still alive so the client gets a clean BYE.
if mcpUnsubscribe != nil {
mcpUnsubscribe()
}
mcpSrv.Close()
for _, srv := range servers {
srv.Stop()
}