Feature(Go): terminal_exec with sentinel cleaning and polling

Implement M4b of the Go MCP minimal plan: terminal_exec runs one command in a
device's persistent shell and polls the rest of a timed-out command.

Wire format follows the Win32 server: ConPTY sends "@echo off & <cmd>" plus a
sentinel line carrying %errorlevel%; POSIX PTY clears PS1 and uses $?. Output
cleaning is a byte-faithful port of the MFC stages (ANSI, CRLF, echoed input,
sentinel remnants) and is shared by commands and polls.

A timed-out call leaves the session and the command running; the next call with
only session_id delivers from the cursor it left. Per-window output is bounded
by max_output_bytes, truncated on a line boundary, and ring evictions are
reported as dropped_bytes. When the shell dies mid-command the session stays
reachable until its buffered tail has been polled, then is released without
touching the hub, which may already belong to a new viewer.

Adds ClientCapTerminal (0x8000) for the Android gate; older APKs, macOS and
legacy non-PTY shells are refused with -32005.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 21:38:00 +01:00
1 parent 12b1bd4786
commit 599c891c00
9 files changed
+1728 -20

No files matched your search

+23 -9
View File
@@ -19,12 +19,21 @@ const termRingCap = 256 << 10
// accumulating whether or not an HTTP request is attached.
type pendingCmd struct {
sentinel string // "__MCP_DONE_<6 hex>__"
nonce string // the 6-hex id inside sentinel, for output cleaning
cmdLine string // wire command line, removed from the PTY's echo
sentLine string // wire sentinel line, removed from the PTY's echo
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)
deadline time.Time // when the current waiter gives up (the session survives)
done bool
exitCode int
outputEnd uint64 // absolute offset just past the sentinel's exit code
outputEnd uint64 // absolute offset where the delivered output ends (sentinel start)
lost uint64 // this command's bytes evicted by the ring, cumulative
// firstChunk is true until the command's first delivery window is cleaned:
// the PTY echoes the input exactly once, at the start.
firstChunk bool
busySince time.Time // when the command started, for the M4c busy reaper
}
// termSession is the server-side view of one shell child. All mutable state
@@ -47,6 +56,7 @@ type termSession struct {
isPTY bool
ready bool // OnTerminalReady seen
sawOutput bool // shell printed its first screen (prompt); bootstrap done
opening bool // hub.OpenTerminalSession still in flight
closed bool
closeReason string
@@ -78,6 +88,7 @@ func (s *termSession) appendLocked(p []byte) {
if len(p) == 0 {
return
}
s.sawOutput = true
if len(p) > len(s.buf) {
drop := len(p) - len(s.buf)
s.droppedBytes += uint64(drop)
@@ -137,10 +148,12 @@ func (s *termSession) scanPendingLocked() {
return
}
data, next, _ := s.readLocked(from, int(s.total-from))
if code, end, ok := scanSentinel(data, p.sentinel); ok {
if code, start, _, ok := scanSentinel(data, p.sentinel); ok {
p.done = true
p.exitCode = code
p.outputEnd = from + uint64(end)
// Deliver only up to the sentinel itself: the echoed sentinel line and
// the prompt printed after it are not the command's output.
p.outputEnd = from + uint64(start)
s.cond.Broadcast()
return
}
@@ -157,23 +170,24 @@ func (s *termSession) scanPendingLocked() {
// 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.
// -1073741510), so a leading '-' is accepted. start is where the sentinel
// begins (delivery stops there); 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) {
func scanSentinel(data []byte, sentinel string) (code, start, 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
return 0, 0, 0, false
}
needle := []byte(sentinel)
from := 0
for {
idx := bytes.Index(data[from:], needle)
if idx < 0 {
return 0, 0, false
return 0, 0, 0, false
}
idx += from
rest := data[idx+len(needle):]
@@ -187,7 +201,7 @@ func scanSentinel(data []byte, sentinel string) (code int, end int, ok bool) {
}
if i > digits {
if n, err := strconv.Atoi(string(rest[:i])); err == nil {
return n, idx + len(needle) + i, true
return n, idx, idx + len(needle) + i, true
}
}
from = idx + len(needle)