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:
1 parent
12b1bd4786
commit
599c891c00
9 files changed
+1728
-20
No files matched your search
@@ -25,6 +25,7 @@ type terminalHub interface {
|
||||
OpenTerminalSession(deviceID string) error
|
||||
CloseTerminalSession(deviceID string)
|
||||
HasTerminalSession(deviceID string) bool
|
||||
SendToTerminal(deviceID string, data []byte) error
|
||||
}
|
||||
|
||||
// sessionTable owns the MCP-created terminal sessions, keyed by device. A
|
||||
@@ -119,6 +120,12 @@ func (t *sessionTable) ready(deviceID string, isPTY bool) {
|
||||
// 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.
|
||||
//
|
||||
// A session with a command still in flight stays in the table even after the
|
||||
// shell is gone: the bytes it produced but nobody has polled yet must remain
|
||||
// reachable (MFC's closed branch keeps it for the same reason). The entry is
|
||||
// dropped by forget once the tail is delivered, or by the M4c idle reaper if
|
||||
// the model never comes back.
|
||||
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
|
||||
@@ -128,25 +135,50 @@ func (t *sessionTable) closed(deviceID, reason string) {
|
||||
return
|
||||
}
|
||||
t.mu.Lock()
|
||||
defer t.mu.Unlock()
|
||||
sess, ok := t.byDevice[deviceID]
|
||||
if !ok {
|
||||
t.mu.Unlock()
|
||||
return
|
||||
}
|
||||
sess.mu.Lock()
|
||||
defer sess.mu.Unlock()
|
||||
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()
|
||||
if !sess.hasUndrainedLocked() {
|
||||
delete(t.byDevice, deviceID)
|
||||
}
|
||||
sess.closed = true
|
||||
sess.closeReason = reason
|
||||
sess.cond.Broadcast()
|
||||
sess.mu.Unlock()
|
||||
}
|
||||
|
||||
// hasUndrainedLocked reports whether the in-flight command still has output the
|
||||
// client has not received. An ended shell delivers everything up to total, so a
|
||||
// running command's undelivered bytes all count.
|
||||
func (s *termSession) hasUndrainedLocked() bool {
|
||||
p := s.pending
|
||||
if p == nil {
|
||||
return false
|
||||
}
|
||||
end := s.total
|
||||
if p.done {
|
||||
end = min(p.outputEnd, s.total)
|
||||
}
|
||||
return s.delivered < end
|
||||
}
|
||||
|
||||
// forget removes a session whose shell is already gone. It must not call the
|
||||
// hub: the device may have reconnected and a new viewer may hold a fresh shell
|
||||
// by now.
|
||||
func (t *sessionTable) forget(deviceID string, want *termSession) {
|
||||
t.mu.Lock()
|
||||
if t.byDevice[deviceID] == want {
|
||||
delete(t.byDevice, deviceID)
|
||||
}
|
||||
t.mu.Unlock()
|
||||
}
|
||||
|
||||
// closeSession tears down the MCP-owned session for deviceID, if any. It is
|
||||
@@ -169,11 +201,15 @@ func (t *sessionTable) closeSessionIf(deviceID string, want *termSession) bool {
|
||||
delete(t.byDevice, deviceID)
|
||||
t.mu.Unlock()
|
||||
sess.mu.Lock()
|
||||
wasClosed := sess.closed
|
||||
sess.closed = true
|
||||
sess.closeReason = "closed"
|
||||
sess.cond.Broadcast()
|
||||
sess.mu.Unlock()
|
||||
if t.hub != nil {
|
||||
// A session that was already closed lost its hub lease when the close event
|
||||
// arrived; the hub may since have handed a fresh shell to another viewer (a
|
||||
// reconnected device). Only an explicit close of a live session may touch it.
|
||||
if t.hub != nil && !wasClosed {
|
||||
// Fires OnTerminalClosed, which is a no-op now that the entry is gone.
|
||||
t.hub.CloseTerminalSession(deviceID)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user