From 12b1bd478639012ffd9d1345245c8db2929e3ef4 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Thu, 12 Nov 2026 21:14:53 +0100 Subject: [PATCH] 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 --- server/go/cmd/main.go | 10 + server/go/hub/hub.go | 10 + server/go/mcp/server.go | 16 +- server/go/mcp/session.go | 279 +++++++++++++++++++++++++ server/go/mcp/session_test.go | 371 ++++++++++++++++++++++++++++++++++ server/go/mcp/term.go | 195 ++++++++++++++++++ 6 files changed, 876 insertions(+), 5 deletions(-) create mode 100644 server/go/mcp/session.go create mode 100644 server/go/mcp/session_test.go create mode 100644 server/go/mcp/term.go diff --git a/server/go/cmd/main.go b/server/go/cmd/main.go index 79b5461..1f218dc 100644 --- a/server/go/cmd/main.go +++ b/server/go/cmd/main.go @@ -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() } diff --git a/server/go/hub/hub.go b/server/go/hub/hub.go index fc097e8..c035112 100644 --- a/server/go/hub/hub.go +++ b/server/go/hub/hub.go @@ -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. diff --git a/server/go/mcp/server.go b/server/go/mcp/server.go index 7387574..4db241e 100644 --- a/server/go/mcp/server.go +++ b/server/go/mcp/server.go @@ -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 diff --git a/server/go/mcp/session.go b/server/go/mcp/session.go new file mode 100644 index 0000000..a14f219 --- /dev/null +++ b/server/go/mcp/session.go @@ -0,0 +1,279 @@ +package mcp + +import ( + "bytes" + "crypto/rand" + "encoding/hex" + "errors" + "fmt" + "sync" + "sync/atomic" + "time" + + "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/protocol" +) + +// errNoDeviceHub means the endpoint was built without a device registry, so +// no shell can be opened. Mirrors the local tools' nil-Hub behaviour (-32000). +var errNoDeviceHub = errors.New("no device hub configured") + +// terminalHub is the slice of *hub.Hub the session table needs. An interface +// keeps the table testable and documents that MCP never touches a terminal +// sub-connection directly — it goes through the hub's single-viewer lease. +type terminalHub interface { + OpenTerminalSession(deviceID string) error + CloseTerminalSession(deviceID string) + HasTerminalSession(deviceID string) bool +} + +// sessionTable owns the MCP-created terminal sessions, keyed by device. A +// session is created before hub.OpenTerminalSession so OnTerminalReady (which +// can fire on another goroutine as soon as the client answers) always finds +// the object; a failed open rolls the entry back. +type sessionTable struct { + hub terminalHub + + mu sync.Mutex + byDevice map[string]*termSession +} + +func newSessionTable(h terminalHub) *sessionTable { + return &sessionTable{hub: h, byDevice: make(map[string]*termSession)} +} + +func (t *sessionTable) get(deviceID string) *termSession { + t.mu.Lock() + defer t.mu.Unlock() + return t.byDevice[deviceID] +} + +// errSessionClosed means the session disappeared while its open was in +// flight (shutdown or an explicit close raced it). +var errSessionClosed = errors.New("terminal session closed during open") + +// open claims a shell for the device. ErrTerminalBusy comes back when a web +// console (or another MCP call) already holds it; the hub enforces the +// single-viewer lease, so v1 never pre-empts a human. +func (t *sessionTable) open(deviceID string) (*termSession, error) { + if t.hub == nil { + return nil, errNoDeviceHub + } + t.mu.Lock() + if _, ok := t.byDevice[deviceID]; ok { + t.mu.Unlock() + return nil, hub.ErrTerminalBusy + } + sess := newTermSession(deviceID) + // Until the hub call returns, any OnTerminalClosed for this device belongs + // to the viewer that just released the lease, not to this session. + sess.opening = true + t.byDevice[deviceID] = sess + t.mu.Unlock() + + if err := t.hub.OpenTerminalSession(deviceID); err != nil { + t.mu.Lock() + delete(t.byDevice, deviceID) + t.mu.Unlock() + sess.mu.Lock() + sess.opening = false + sess.closed = true + sess.closeReason = "open failed" + sess.cond.Broadcast() + sess.mu.Unlock() + return nil, err + } + + sess.mu.Lock() + sess.opening = false + sess.mu.Unlock() + + // An explicit close (or shutdown) may have removed the entry while the hub + // call was in flight. The hub lease is ours by now, so release it instead + // of returning a session nobody can reach. + t.mu.Lock() + cur, ok := t.byDevice[deviceID] + t.mu.Unlock() + if !ok || cur != sess { + t.hub.CloseTerminalSession(deviceID) + return nil, errSessionClosed + } + return sess, nil +} + +// ready records that the shell sub-conn is bound. Called from the hub's TCP +// read loop, so it must stay short (a mutex and a broadcast). +func (t *sessionTable) ready(deviceID string, isPTY bool) { + sess := t.get(deviceID) + if sess == nil { + return + } + sess.mu.Lock() + sess.isPTY = isPTY + sess.ready = true + sess.lastUsed = time.Now() + sess.cond.Broadcast() + sess.mu.Unlock() +} + +// closed runs when the shell ends for any reason — device-side exit, sub-conn +// drop, or an explicit teardown. Waiters wake up and deliver whatever output +// is already buffered. +func (t *sessionTable) closed(deviceID, reason string) { + // A web console's teardown can race a fresh open: by the time this event is + // delivered, the hub lease may already belong to a new MCP session. The hub + // is the authority on whether a session still exists; if it does, the event + // described the previous viewer and must not touch ours. + if t.hub != nil && t.hub.HasTerminalSession(deviceID) { + return + } + t.mu.Lock() + sess, ok := t.byDevice[deviceID] + if !ok { + t.mu.Unlock() + return + } + sess.mu.Lock() + if sess.opening { + // Our own hub call has not returned yet, so any close for this device + // still refers to whoever held the lease before us. + sess.mu.Unlock() + t.mu.Unlock() + return + } + delete(t.byDevice, deviceID) + t.mu.Unlock() + sess.closed = true + sess.closeReason = reason + sess.cond.Broadcast() + sess.mu.Unlock() +} + +// closeSession tears down the MCP-owned session for deviceID, if any. It is +// idempotent: a call for a device MCP does not own returns false and leaves +// the web console's session alone. +func (t *sessionTable) closeSession(deviceID string) bool { + return t.closeSessionIf(deviceID, nil) +} + +// closeSessionIf closes the device's session only if the table still holds +// exactly want. The identity check matters for sweepIdle: between its scan +// and the close, the stale session can be replaced by a fresh one. +func (t *sessionTable) closeSessionIf(deviceID string, want *termSession) bool { + t.mu.Lock() + sess, ok := t.byDevice[deviceID] + if !ok || (want != nil && sess != want) { + t.mu.Unlock() + return false + } + delete(t.byDevice, deviceID) + t.mu.Unlock() + sess.mu.Lock() + sess.closed = true + sess.closeReason = "closed" + sess.cond.Broadcast() + sess.mu.Unlock() + if t.hub != nil { + // Fires OnTerminalClosed, which is a no-op now that the entry is gone. + t.hub.CloseTerminalSession(deviceID) + } + return true +} + +// sweepIdle closes sessions whose last use is older than idle. Intended for +// the periodic reaper: a model that forgets terminal_close must not lock a +// human out of the device's shell. +// +// The M4c reaper must skip sessions with an in-flight command (pending != +// nil): a quiet command (sleep, a slow download) still owns the lease and +// must not be reaped just because it printed nothing for a while. +func (t *sessionTable) sweepIdle(now time.Time, idle time.Duration) int { + type staleEntry struct { + id string + sess *termSession + } + var stale []staleEntry + t.mu.Lock() + for id, sess := range t.byDevice { + sess.mu.Lock() + last := sess.lastUsed + sess.mu.Unlock() + if now.Sub(last) >= idle { + stale = append(stale, staleEntry{id, sess}) + } + } + t.mu.Unlock() + + n := 0 + for _, e := range stale { + if t.closeSessionIf(e.id, e.sess) { + n++ + } + } + return n +} + +// closeAll drops every session; used at shutdown. The hub teardown is still +// best-effort because the TCP servers may already be stopping. +func (t *sessionTable) closeAll() { + t.mu.Lock() + ids := make([]string, 0, len(t.byDevice)) + for id := range t.byDevice { + ids = append(ids, id) + } + t.mu.Unlock() + for _, id := range ids { + t.closeSession(id) + } +} + +// sessionIDFallback numbers ids when crypto/rand is unavailable. It is only a +// last resort: ids are opaque to clients, and uniqueness matters, not secrecy. +var sessionIDFallback atomic.Uint32 + +func newSessionID() string { + var b [3]byte + if _, err := rand.Read(b[:]); err == nil { + return "s-" + hex.EncodeToString(b[:]) + } + return fmt.Sprintf("s-%06x", sessionIDFallback.Add(1)&0xffffff) +} + +// Server implements hub.EventHandler so the endpoint can observe shell +// traffic. Only the terminal events do work; the screen and lifecycle events +// are ignored (the web console owns those views). +var _ hub.EventHandler = (*Server)(nil) + +func (s *Server) OnDeviceOnline(hub.DeviceInfo) {} +func (s *Server) OnDeviceOffline(string) {} +func (s *Server) OnDeviceUpdate(string, int, string) {} +func (s *Server) OnScreenFrame(string, []byte, bool) {} +func (s *Server) OnResolutionChange(string, int, int) {} +func (s *Server) OnCursorChange(string, byte, int, int) {} +func (s *Server) OnClipboardText(string, string) {} +func (s *Server) OnTerminalReady(deviceID string, isPTY bool) { s.sessions.ready(deviceID, isPTY) } +func (s *Server) OnTerminalClosed(deviceID, reason string) { s.sessions.closed(deviceID, reason) } + +// OnTerminalData runs on the device's TCP read loop. It must never block: +// look up the session, strip the browser demux prefix, append to the ring, +// check the in-flight sentinel and wake any waiter. Sessions MCP did not +// create (the web console's) are dropped at the map lookup with no work. +func (s *Server) OnTerminalData(deviceID string, packet []byte) { + sess := s.sessions.get(deviceID) + if sess == nil { + return + } + if len(packet) >= len(protocol.TerminalBinaryMagic) && + bytes.Equal(packet[:len(protocol.TerminalBinaryMagic)], protocol.TerminalBinaryMagic[:]) { + packet = packet[len(protocol.TerminalBinaryMagic):] + } + sess.mu.Lock() + if sess.closed { + sess.mu.Unlock() + return + } + sess.appendLocked(packet) + sess.scanPendingLocked() + sess.cond.Broadcast() + sess.mu.Unlock() +} diff --git a/server/go/mcp/session_test.go b/server/go/mcp/session_test.go new file mode 100644 index 0000000..15a26bb --- /dev/null +++ b/server/go/mcp/session_test.go @@ -0,0 +1,371 @@ +package mcp + +import ( + "bytes" + "errors" + "strings" + "testing" + "time" + + "github.com/yuanyuanxiang/SimpleRemoter/server/go/connection" + "github.com/yuanyuanxiang/SimpleRemoter/server/go/hub" +) + +// terminalTestServer builds a server backed by a real hub with one online +// device (1001) whose shell commands are accepted by a no-op sender. +func terminalTestServer(t *testing.T) (*Server, *hub.Hub) { + t.Helper() + h := hub.New() + h.SetSender(func(*connection.Context, []byte) error { return nil }) + h.Register(&hub.Device{ID: "1001", PeerIP: "10.0.0.1"}, + &connection.Context{RemoteAddr: "10.0.0.1:54321"}) + s := New(Config{Token: testToken, Hub: h}) + t.Cleanup(s.Close) + return s, h +} + +// termPacket wraps shell output the way cmd/main.go publishes it. +func termPacket(s string) []byte { + p := make([]byte, 4+len(s)) + copy(p, "TRM1") + copy(p[4:], s) + return p +} + +func TestTermRingTrimsOldest(t *testing.T) { + sess := newTermSession("1001") + payload := make([]byte, 1<<20) + for i := range payload { + payload[i] = byte(i*7 + 1) + } + + sess.mu.Lock() + sess.appendLocked(payload) + data, next, lost := sess.readLocked(0, 1<<20) + size, total, dropped := sess.size, sess.total, sess.droppedBytes + sess.mu.Unlock() + + if size != termRingCap { + t.Fatalf("retained = %d, want cap %d", size, termRingCap) + } + if total != 1<<20 { + t.Fatalf("total = %d, want %d", total, 1<<20) + } + if dropped != uint64((1<<20)-termRingCap) { + t.Fatalf("dropped = %d, want %d", dropped, (1<<20)-termRingCap) + } + if !lost { + t.Fatal("reading from 0 after overflow should report lost data") + } + if len(data) != termRingCap || next != 1<<20 { + t.Fatalf("read len/next = %d/%d, want %d/%d", len(data), next, termRingCap, 1<<20) + } + if want := payload[(1<<20)-termRingCap:]; !bytes.Equal(data, want) { + t.Fatal("retained data is not the newest window of the stream") + } +} + +func TestTermRingWraps(t *testing.T) { + sess := newTermSession("1001") + var want []byte + for i := range 10 { + chunk := bytes.Repeat([]byte{byte('a' + i)}, 40<<10) + want = append(want, chunk...) + sess.mu.Lock() + sess.appendLocked(chunk) + sess.mu.Unlock() + } + + sess.mu.Lock() + base, total := sess.baseLocked(), sess.total + data, next, lost := sess.readLocked(0, termRingCap) + tail, _, _ := sess.readLocked(total-100, 100) + sess.mu.Unlock() + + if !lost { + t.Fatal("reading from 0 after wraparound should report lost data") + } + if base != uint64(len(want)-termRingCap) { + t.Fatalf("base = %d, want %d", base, len(want)-termRingCap) + } + if next != total { + t.Fatalf("next = %d, want total %d", next, total) + } + if wantWin := want[len(want)-termRingCap:]; !bytes.Equal(data, wantWin) { + t.Fatal("wrapped ring does not return the newest window in order") + } + if wantTail := want[len(want)-100:]; !bytes.Equal(tail, wantTail) { + t.Fatal("tail read is not the newest bytes") + } +} + +func TestScanSentinel(t *testing.T) { + const sentinel = "__MCP_DONE_abc123__" + cases := []struct { + name string + in string + code int + ok bool + }{ + {"plain", "x\r\n" + sentinel + "0\r\n", 0, true}, + {"nonzero", sentinel + "42", 42, true}, + {"negative", sentinel + "-1073741510", -1073741510, true}, + {"echo then real", sentinel + "%errorlevel%\r\n" + sentinel + "1\r\n", 1, true}, + {"posix echo then real", sentinel + "$?\n" + sentinel + "0\n", 0, true}, + {"command echo only", sentinel + "%errorlevel%", 0, false}, + {"absent", "hello world", 0, false}, + {"empty", "", 0, false}, + } + for _, tc := range cases { + code, end, ok := scanSentinel([]byte(tc.in), sentinel) + if ok != tc.ok { + t.Errorf("%s: ok = %v, want %v", tc.name, ok, tc.ok) + continue + } + if ok && code != tc.code { + t.Errorf("%s: code = %d, want %d", tc.name, code, tc.code) + } + if ok && (end <= 0 || end > len(tc.in)) { + t.Errorf("%s: end = %d, want 1..%d", tc.name, end, len(tc.in)) + } + } + if _, _, ok := scanSentinel([]byte("123"), ""); ok { + t.Fatal("empty sentinel must never match") + } +} + +func TestOpenSessionWaitsForReadyAndBuffers(t *testing.T) { + s, h := terminalTestServer(t) + + sess, err := s.sessions.open("1001") + if err != nil { + t.Fatalf("open: %v", err) + } + if !strings.HasPrefix(sess.id, "s-") || len(sess.id) != 8 { + t.Fatalf("session id = %q, want s-XXXXXX", sess.id) + } + sess.mu.Lock() + ready := sess.ready + sess.mu.Unlock() + if ready { + t.Fatal("session must not be ready before OnTerminalReady") + } + + s.OnTerminalReady("1001", true) + sess.mu.Lock() + ready, isPTY := sess.ready, sess.isPTY + sess.mu.Unlock() + if !ready || !isPTY { + t.Fatalf("after ready: ready=%v isPTY=%v", ready, isPTY) + } + + // The browser demux prefix is stripped; a raw chunk is accepted as-is. + s.OnTerminalData("1001", termPacket("hello ")) + s.OnTerminalData("1001", []byte("world")) + sess.mu.Lock() + data, next, lost := sess.readLocked(0, 1<<10) + sess.mu.Unlock() + if string(data) != "hello world" || next != 11 || lost { + t.Fatalf("buffered = %q next=%d lost=%v", data, next, lost) + } + + // Simulate a genuine teardown: the hub releases the lease first, then its + // event reaches the MCP subscriber. + h.CloseTerminalSession("1001") + s.OnTerminalClosed("1001", "closed") + if s.sessions.get("1001") != nil { + t.Fatal("closed session still in the table") + } + sess.mu.Lock() + closed, reason := sess.closed, sess.closeReason + sess.mu.Unlock() + if !closed || reason != "closed" { + t.Fatalf("session state: closed=%v reason=%q", closed, reason) + } +} + +func TestOpenSessionBusyWithBrowser(t *testing.T) { + s, h := terminalTestServer(t) + _ = s + + // The web console opens the shell first (same call it makes today). + if err := h.OpenTerminalSession("1001"); err != nil { + t.Fatalf("browser open: %v", err) + } + tbl := newSessionTable(h) + if _, err := tbl.open("1001"); !errors.Is(err, hub.ErrTerminalBusy) { + t.Fatalf("open while browser holds it = %v, want ErrTerminalBusy", err) + } + if tbl.get("1001") != nil { + t.Fatal("failed open must not leave a session marker behind") + } + + // After the browser closes, MCP can claim the shell. + h.CloseTerminalSession("1001") + if _, err := tbl.open("1001"); err != nil { + t.Fatalf("open after browser closed: %v", err) + } +} + +func TestTerminalSentinelWakesPending(t *testing.T) { + s, _ := terminalTestServer(t) + sess, err := s.sessions.open("1001") + if err != nil { + t.Fatalf("open: %v", err) + } + s.OnTerminalReady("1001", true) + sess.mu.Lock() + sess.pending = &pendingCmd{sentinel: "__MCP_DONE_abc123__"} + sess.mu.Unlock() + + s.OnTerminalData("1001", termPacket("dir\r\nfile.txt\r\n__MCP_DONE_abc123__1\r\n")) + + sess.mu.Lock() + p := sess.pending + done, code, end := p.done, p.exitCode, p.outputEnd + sess.mu.Unlock() + if !done || code != 1 { + t.Fatalf("pending: done=%v code=%d", done, code) + } + if end == 0 { + t.Fatal("sentinel end offset not recorded") + } +} + +func TestSessionIdleSweep(t *testing.T) { + s, _ := terminalTestServer(t) + sess, err := s.sessions.open("1001") + if err != nil { + t.Fatalf("open: %v", err) + } + sess.mu.Lock() + sess.lastUsed = time.Now().Add(-time.Hour) + sess.mu.Unlock() + + if n := s.sessions.sweepIdle(time.Now(), time.Minute); n != 1 { + t.Fatalf("swept = %d, want 1", n) + } + if s.sessions.get("1001") != nil { + t.Fatal("idle session still in the table") + } + sess.mu.Lock() + closed := sess.closed + sess.mu.Unlock() + if !closed { + t.Fatal("swept session not marked closed") + } +} + +func TestSessionTableCloseAll(t *testing.T) { + s, h := terminalTestServer(t) + h.Register(&hub.Device{ID: "2002", PeerIP: "10.0.0.2"}, + &connection.Context{RemoteAddr: "10.0.0.2:54321"}) + for _, id := range []string{"1001", "2002"} { + if _, err := s.sessions.open(id); err != nil { + t.Fatalf("open %s: %v", id, err) + } + } + + s.Close() + for _, id := range []string{"1001", "2002"} { + if s.sessions.get(id) != nil { + t.Fatalf("session %s survived Close", id) + } + } +} + +// A close event emitted by the previous viewer's teardown must not delete a +// session whose open already owns the hub lease. Regression for the race where +// the web console closes as MCP opens. +func TestStaleTerminalCloseKeepsSession(t *testing.T) { + s, h := terminalTestServer(t) + sess, err := s.sessions.open("1001") + if err != nil { + t.Fatalf("open: %v", err) + } + + // The hub still holds our (new) pending lease, so this event describes the + // viewer that just went away. + s.OnTerminalClosed("1001", "closed") + if got := s.sessions.get("1001"); got != sess { + t.Fatal("stale close event removed the fresh session") + } + sess.mu.Lock() + closed := sess.closed + sess.mu.Unlock() + if closed { + t.Fatal("stale close event marked the fresh session closed") + } + + // Once the hub really has no session, the event is genuine. + h.CloseTerminalSession("1001") + s.OnTerminalClosed("1001", "closed") + if s.sessions.get("1001") != nil { + t.Fatal("genuine close event did not remove the session") + } +} + +func TestClosedIgnoredWhileOpening(t *testing.T) { + tbl := newSessionTable(nil) + sess := newTermSession("1001") + sess.opening = true + tbl.byDevice["1001"] = sess + + tbl.closed("1001", "closed") + if tbl.get("1001") != sess { + t.Fatal("close event during open removed the session") + } + + sess.mu.Lock() + sess.opening = false + sess.mu.Unlock() + tbl.closed("1001", "closed") + if tbl.get("1001") != nil { + t.Fatal("close after open finished did not remove the session") + } +} + +// sweepIdle snapshots sessions before closing them; if the entry was replaced +// in between, the close must not take out the new session. +func TestSweepSkipsReplacedSession(t *testing.T) { + tbl := newSessionTable(nil) + old := newTermSession("1001") + old.lastUsed = old.lastUsed.Add(-time.Hour) + fresh := newTermSession("1001") + tbl.byDevice["1001"] = fresh + + if tbl.closeSessionIf("1001", old) { + t.Fatal("closeSessionIf closed a session it did not own") + } + if tbl.get("1001") != fresh { + t.Fatal("replacement session was removed") + } + if n := tbl.sweepIdle(time.Now(), time.Minute); n != 0 { + t.Fatalf("swept = %d, want 0", n) + } +} + +func TestSentinelSplitAcrossChunks(t *testing.T) { + s, _ := terminalTestServer(t) + sess, err := s.sessions.open("1001") + if err != nil { + t.Fatalf("open: %v", err) + } + s.OnTerminalReady("1001", true) + sess.mu.Lock() + sess.pending = &pendingCmd{sentinel: "__MCP_DONE_abc123__"} + sess.mu.Unlock() + + // The sentinel prefix ends more than 64 bytes before the chunk boundary, + // so the retained overlap is what lets the second chunk complete it. + s.OnTerminalData("1001", termPacket(strings.Repeat("x", 100)+"__MCP_DONE_ab")) + s.OnTerminalData("1001", termPacket("c123__3\r\n")) + + sess.mu.Lock() + p := sess.pending + done, code := p.done, p.exitCode + sess.mu.Unlock() + if !done || code != 3 { + t.Fatalf("split sentinel: done=%v code=%d", done, code) + } +} diff --git a/server/go/mcp/term.go b/server/go/mcp/term.go new file mode 100644 index 0000000..5dae261 --- /dev/null +++ b/server/go/mcp/term.go @@ -0,0 +1,195 @@ +package mcp + +import ( + "bytes" + "strconv" + "sync" + "time" +) + +// termRingCap bounds one session's retained output. A shell that floods the +// session (a log tail, a build) must not grow server memory without bound; +// the oldest bytes are dropped and the loss is surfaced as truncated output +// (design §6.3). +const termRingCap = 256 << 10 + +// pendingCmd is one command whose output is still being collected. It lives +// on the session so a timed-out terminal_exec can hand the same command back +// to a later polling call: the shell keeps running and its output keeps +// accumulating whether or not an HTTP request is attached. +type pendingCmd struct { + sentinel string // "__MCP_DONE_<6 hex>__" + start uint64 // absolute offset where this command's output begins + scanPos uint64 // absolute offset scanned so far (sentinel may span chunks) + deadline time.Time // when the waiter gives up (the session survives) + done bool + exitCode int + outputEnd uint64 // absolute offset just past the sentinel's exit code +} + +// termSession is the server-side view of one shell child. All mutable state +// is guarded by mu; cond wakes a waiter when data arrives, the command ends, +// or the session closes. +type termSession struct { + id string + deviceID string + createdAt time.Time + + mu sync.Mutex + cond *sync.Cond + lastUsed time.Time + buf []byte // ring of termRingCap bytes + head int // index the next write goes to + size int // valid bytes in buf + total uint64 // bytes the shell produced, including dropped ones + delivered uint64 // absolute offset already handed to the client + droppedBytes uint64 + + isPTY bool + ready bool // OnTerminalReady seen + opening bool // hub.OpenTerminalSession still in flight + closed bool + closeReason string + + pending *pendingCmd // nil = idle +} + +func newTermSession(deviceID string) *termSession { + now := time.Now() + s := &termSession{ + id: newSessionID(), + deviceID: deviceID, + createdAt: now, + lastUsed: now, + buf: make([]byte, termRingCap), + } + s.cond = sync.NewCond(&s.mu) + return s +} + +// baseLocked is the absolute offset of the oldest retained byte. +func (s *termSession) baseLocked() uint64 { return s.total - uint64(s.size) } + +// appendLocked writes p into the ring, dropping the oldest bytes on overflow. +// total counts every byte the shell produced, including dropped ones, so the +// absolute offsets stay monotonic and a client can tell that it lost data. +func (s *termSession) appendLocked(p []byte) { + s.lastUsed = time.Now() + if len(p) == 0 { + return + } + if len(p) > len(s.buf) { + drop := len(p) - len(s.buf) + s.droppedBytes += uint64(drop) + s.total += uint64(drop) + p = p[drop:] + } + if overflow := s.size + len(p) - len(s.buf); overflow > 0 { + // The oldest overflow bytes are simply forgotten: head still marks + // the next write, and size now describes a shorter prefix. + s.droppedBytes += uint64(overflow) + s.size -= overflow + } + if n := copy(s.buf[s.head:], p); n < len(p) { + copy(s.buf, p[n:]) + } + s.head = (s.head + len(p)) % len(s.buf) + s.size += len(p) + s.total += uint64(len(p)) +} + +// readLocked returns up to max bytes starting at absolute offset. lost is +// true when the requested offset predates the ring's oldest byte; the data +// then starts at the oldest retained byte instead. +func (s *termSession) readLocked(offset uint64, max int) (data []byte, next uint64, lost bool) { + if max <= 0 { + return nil, offset, false + } + base := s.baseLocked() + if offset < base { + offset = base + lost = true + } + if offset >= s.total { + return nil, s.total, lost + } + n := int(min(s.total-offset, uint64(max))) + idx := (s.head - s.size + int(offset-base)) % len(s.buf) + if idx < 0 { + idx += len(s.buf) + } + data = make([]byte, n) + first := copy(data, s.buf[idx:min(idx+n, len(s.buf))]) + copy(data[first:], s.buf[:n-first]) + return data, offset + uint64(n), lost +} + +// scanPendingLocked looks for the in-flight command's sentinel in the bytes +// appended since the last look. A tail overlap is retained so a sentinel split +// across two TCP reads is still found. +func (s *termSession) scanPendingLocked() { + p := s.pending + if p == nil || p.done { + return + } + from := max(p.scanPos, s.baseLocked()) + if from >= s.total { + return + } + data, next, _ := s.readLocked(from, int(s.total-from)) + if code, end, ok := scanSentinel(data, p.sentinel); ok { + p.done = true + p.exitCode = code + p.outputEnd = from + uint64(end) + s.cond.Broadcast() + return + } + // Keep the last few bytes: the next chunk may complete a sentinel whose + // prefix sits just before the boundary. + const overlap = 64 + if next > overlap { + next -= overlap + } + if next > p.scanPos { + p.scanPos = next + } +} + +// scanSentinel finds the first occurrence of sentinel followed by a decimal +// exit code. The Windows %errorlevel% is signed (0xC000013A prints as +// -1073741510), so a leading '-' is accepted. end is one past the last digit. +// +// Echoed command lines contain the sentinel too but not the exit code +// ("__MCP_DONE_x__$?" / "__MCP_DONE_x__%errorlevel%"), so occurrences without +// digits are skipped rather than reported. +func scanSentinel(data []byte, sentinel string) (code int, end int, ok bool) { + if sentinel == "" { + // Defensive: an empty needle matches everywhere and would spin the + // device read loop. Callers always mint a non-empty sentinel. + return 0, 0, false + } + needle := []byte(sentinel) + from := 0 + for { + idx := bytes.Index(data[from:], needle) + if idx < 0 { + return 0, 0, false + } + idx += from + rest := data[idx+len(needle):] + i := 0 + if i < len(rest) && rest[i] == '-' { + i++ + } + digits := i + for i < len(rest) && rest[i] >= '0' && rest[i] <= '9' { + i++ + } + if i > digits { + if n, err := strconv.Atoi(string(rest[:i])); err == nil { + return n, idx + len(needle) + i, true + } + } + from = idx + len(needle) + } +}