From 27e700e796644c25bfc51d5f470ec58067f83b20 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Tue, 25 Aug 2026 04:58:30 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20write=20ready=20sentinel=20to=20real=20s?= =?UTF-8?q?tdout=20=E2=80=94=20server.py=20redirects=20sys.stdout=20to=20s?= =?UTF-8?q?tderr=20at=20module=20level?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tui_gateway/entry_ws.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tui_gateway/entry_ws.py b/tui_gateway/entry_ws.py index 759818ebc7..1ab2c2835e 100644 --- a/tui_gateway/entry_ws.py +++ b/tui_gateway/entry_ws.py @@ -182,6 +182,12 @@ def run(host: str = "127.0.0.1", port: int = 0) -> None: """ import signal + # Capture the real stdout BEFORE importing tui_gateway.server — + # server.py redirects sys.stdout → sys.stderr at module level + # (line 392: `sys.stdout = sys.stderr`) to keep JSON-RPC over stdio + # clean. Our ready sentinel must go to the pipe the desktop reads. + _real_stdout = sys.stdout + # tui_gateway.server does NOT import fastapi/starlette/uvicorn — it's # pure stdlib + agent modules. Importing it here is what boots the # dispatch surface (session store, thread pool, skin resolver, etc). @@ -209,11 +215,13 @@ def run(host: str = "127.0.0.1", port: int = 0) -> None: sockets = ws_server.sockets actual_port = sockets[0].getsockname()[1] if sockets else port - # Ready sentinel — the desktop's main.cjs parses this line. - print( - f"HERMES_BACKEND_READY port={actual_port} token={_SESSION_TOKEN}", - flush=True, + # Ready sentinel — the desktop's main.cjs parses this from stdout. + # Use the captured _real_stdout because tui_gateway.server redirects + # sys.stdout → sys.stderr at module level. + _real_stdout.write( + f"HERMES_BACKEND_READY port={actual_port} token={_SESSION_TOKEN}\n" ) + _real_stdout.flush() _log.info("ws-only gateway listening on %s:%d", host, actual_port) # Block forever.