fix(mcp): collect the console stream in websocat binary mode (-b)

websocat's text mode (-t, and also the default) appends a newline after
every received WebSocket message and turns CR/LF inside a message into
spaces. The compute ships console output as one BINARY frame per
arbitrary read(1024) burst, so text mode diced the stream at every frame
boundary — prompts split into pieces, short "narrow-column" lines, lost
carriage-return redraws (field report: Password: broken in three).

Measured against a local WS server sending known frames:
  -t:  A\nB\nC\na b\na  b\n  ("a\rb" -> "a b")
  -b:  ABCa\rba\r\nb        (byte-exact, both directions)

Binary command frames stay console data on the compute side (only a line
that is itself a JSON {cols,rows} object would be taken as a resize
frame). The regression test fails on -t and passes on -b.
This commit is contained in:
YueGuobin committed 2026-11-03 00:02:58 +08:00
1 parent 7bf18ac2e8
commit 78199d14a4
3 files changed
+58 -9

No files matched your search

+14 -4
View File
@@ -22,10 +22,10 @@ node_console_exec: one-shot node console executor.
Server-side equivalent of the client-side chain this workflow used to need:
node_console (mint URL+ticket) -> bash: { sleep ..; printf 'cmd\r\n'; .. }
| timeout N websocat -t --no-close '<url>'
| timeout N websocat -b --no-close '<url>'
One tool call is one complete console session: the server spawns
``timeout N websocat -t --no-close <url>``, feeds the command lines through
``timeout N websocat -b --no-close <url>``, feeds the command lines through
stdin, keeps reading stdout until the timeout kills websocat, and returns the
collected output as plain text. No session state survives the call; node-side
state (config mode, running pings) persists across calls by nature of the
@@ -161,7 +161,17 @@ async def _resolve_console_node(
async def _spawn(url: str, seconds: int) -> asyncio.subprocess.Process:
"""Start ``timeout <seconds> websocat -t --no-close <url>``.
"""Start ``timeout <seconds> websocat -b --no-close <url>``.
Binary mode is mandatory, not a preference: in text mode (-t, and also
the default) websocat appends a ``\\n`` after every received WebSocket
message and turns CR/LF inside a message into spaces. The compute ships
console output as one BINARY frame per ``read(1024)`` burst, so text mode
injects a newline at every arbitrary frame boundary — output arrives
diced into short lines mid-word (field symptom: prompts split into
pieces, "narrow columns"). ``-b`` is byte-exact in both directions;
binary command frames are still handled as data by the compute console
handler.
The subprocess gets its own process group: websocat is a child of
``timeout``, and a cancelled call must take down the whole tree (an
@@ -172,7 +182,7 @@ async def _spawn(url: str, seconds: int) -> asyncio.subprocess.Process:
"timeout",
str(seconds),
"websocat",
"-t",
"-b",
"--no-close",
url,
stdin=asyncio.subprocess.PIPE,