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

+43 -4
View File
@@ -117,7 +117,7 @@ func TestScanSentinel(t *testing.T) {
{"empty", "", 0, false},
}
for _, tc := range cases {
code, end, ok := scanSentinel([]byte(tc.in), sentinel)
code, start, end, ok := scanSentinel([]byte(tc.in), sentinel)
if ok != tc.ok {
t.Errorf("%s: ok = %v, want %v", tc.name, ok, tc.ok)
continue
@@ -125,11 +125,11 @@ func TestScanSentinel(t *testing.T) {
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 && (start < 0 || start >= end || end > len(tc.in)) {
t.Errorf("%s: start/end = %d/%d, want 0<=start<end<=%d", tc.name, start, end, len(tc.in))
}
}
if _, _, ok := scanSentinel([]byte("123"), ""); ok {
if _, _, _, ok := scanSentinel([]byte("123"), ""); ok {
t.Fatal("empty sentinel must never match")
}
}
@@ -345,6 +345,45 @@ func TestSweepSkipsReplacedSession(t *testing.T) {
}
}
// A draining session's close must not tear down a hub lease that a reconnected
// viewer has since opened: the shell the session referred to is already gone.
func TestCloseOfEndedSessionLeavesNewHubLease(t *testing.T) {
s, h := terminalTestServer(t)
sess, err := s.sessions.open("1001")
if err != nil {
t.Fatalf("open: %v", err)
}
s.OnTerminalReady("1001", true)
if _, rerr := sess.beginCommand("ab12cd34", "cmd", ""); rerr != nil {
t.Fatalf("begin: %v", rerr)
}
s.OnTerminalData("1001", termPacket("out\r\n"))
// The shell ends with output the client has not polled yet: the session
// stays in the table so the tail can still be collected. The hub event is
// delivered to subscribers by cmd/main.go's wiring.
h.CloseTerminalSession("1001")
s.OnTerminalClosed("1001", "closed")
if s.sessions.get("1001") != sess {
t.Fatal("draining session was dropped")
}
// Another viewer (or the same device after reconnecting) gets a fresh lease.
if err := h.OpenTerminalSession("1001"); err != nil {
t.Fatalf("new lease: %v", err)
}
if !h.HasTerminalSession("1001") {
t.Fatal("new lease not established")
}
if !s.sessions.closeSessionIf("1001", sess) {
t.Fatal("close did not remove the draining session")
}
if !h.HasTerminalSession("1001") {
t.Fatal("closing the ended session tore down the new viewer's shell")
}
}
func TestSentinelSplitAcrossChunks(t *testing.T) {
s, _ := terminalTestServer(t)
sess, err := s.sessions.open("1001")