Skip to content

The agent that grows with you

fix(web): satisfy code-health ratchet and contributor attribution

Austin Pickett7ed284fee0e0
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..71e03b5ca43
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1,2 @@
+sinh2024
+# PR #133936
diff --git a/hermes_cli/pty_session.py b/hermes_cli/pty_session.py
index b3bd742be6e..a24e1a51958 100644
--- a/hermes_cli/pty_session.py
+++ b/hermes_cli/pty_session.py
@@ -186,7 +186,7 @@ class PtySession:
# bridge.close() joins the child — blocking; keep it off the event loop.
# See #53227.
await asyncio.to_thread(self.bridge.close)
- except Exception:
+ except Exception: # health: allow BLE001 S110 -- teardown of an already-dead PTY must not mask the caller's error path
pass
try:
if self.active_session_file is not None:
@@ -196,7 +196,7 @@ class PtySession:
if self.active_session_cleanup is not None:
try:
self.active_session_cleanup()
- except Exception:
+ except Exception: # health: allow BLE001 S110 -- cleanup callback must not mask the close path; the PTY is dead either way
pass
self.active_session_cleanup = None
diff --git a/hermes_cli/web_routers/chat_ws.py b/hermes_cli/web_routers/chat_ws.py
index c2b75574820..f463430ec31 100644
--- a/hermes_cli/web_routers/chat_ws.py
+++ b/hermes_cli/web_routers/chat_ws.py
@@ -71,7 +71,7 @@ def _discard_active_session_file(app: "FastAPI", channel: Optional[str], path: O
files = _get_pty_active_session_files(app)
if files.get(channel) == path:
files.pop(channel, None)
- except Exception:
+ except Exception: # health: allow BLE001 S110 -- marker cleanup must never mask the PTY teardown path it runs inside
pass
@@ -479,6 +479,62 @@ def _lease_holder_pid(session_id: Optional[str], *, registry_home: Optional[str]
return None
+async def _pump_keepalive_input(ws: "WebSocket", session: Any) -> None:
+ """Writer loop for a keep-alive /api/pty socket: forward client input to the
+ session's bridge, consuming resize escapes locally. On child EOF the drain
+ task closes the attached socket with 4410, which unparks ws.receive()."""
+ from hermes_cli.web_server_chat import _RESIZE_RE
+ while True:
+ try:
+ msg = await ws.receive()
+ except RuntimeError: # receive() after the drain task already closed us
+ break
+ if msg.get("type") == "websocket.disconnect":
+ break
+ raw = msg.get("bytes")
+ if raw is None:
+ text = msg.get("text")
+ raw = text.encode("utf-8") if isinstance(text, str) else b""
+ if not raw:
+ continue
+ # Resize escape is consumed locally, never written to the PTY.
+ match = _RESIZE_RE.match(raw)
+ if match and match.end() == len(raw):
+ session.bridge.resize(cols=int(match.group(1)), rows=int(match.group(2)))
+ continue
+ if not await session.write(ws, raw):
+ await _close_stalled_pty_input(ws, path="keepalive")
+ break
+
+
+async def _pty_channel_marker_state(
+ ws: WebSocket, resume: Optional[str], force_fresh: bool,
+) -> tuple[Optional[Path], bool, Optional[str]]:
+ """(active_session_file, marker_preexisting, resume) for the /api/pty handler.
+
+ A channel's marker may already belong to a live keep-alive PTY on that
+ channel — only a marker this handler allocates is its own to drop on
+ failure (#63553)."""
+ channel = _channel_or_close_code(ws)
+ if not channel:
+ return None, False, resume
+ from hermes_cli.web_server import _get_pty_active_session_files
+ marker_files = _get_pty_active_session_files(ws.app)
+ marker_preexisting = channel in marker_files
+
+ active_session_file = _active_session_file_for_channel(ws.app, channel)
+ if force_fresh:
+ resume = None
+ try:
+ active_session_file.unlink(missing_ok=True)
+ except OSError:
+ pass
+ marker_preexisting = False
+ elif not resume:
+ resume = _read_active_session_file(active_session_file)
+ return active_session_file, marker_preexisting, resume
+
+
@router.websocket("/api/pty")
async def pty_ws(ws: WebSocket) -> None:
from hermes_cli.web_server_chat import PTY_REGISTRY, PtyBridge, PtyUnavailableError, _PTY_BRIDGE_AVAILABLE, _RESIZE_RE
@@ -507,30 +563,14 @@ async def pty_ws(ws: WebSocket) -> None:
channel = _channel_or_close_code(ws)
sidecar_url = _build_sidecar_url(channel) if channel else None
force_fresh = (ws.query_params.get("fresh") or "").strip().lower() in {"1", "true", "yes", "on"}
- active_session_file: Optional[Path] = None
- marker_preexisting = False
+ active_session_file, marker_preexisting, resume = await _pty_channel_marker_state(ws, resume, force_fresh)
- if channel:
- from hermes_cli.web_server import _get_pty_active_session_files
- marker_files = _get_pty_active_session_files(ws.app)
- marker_preexisting = channel in marker_files
-
- active_session_file = _active_session_file_for_channel(ws.app, channel)
- if force_fresh:
- resume = None
- try:
- active_session_file.unlink(missing_ok=True)
- except OSError:
- pass
- marker_preexisting = False
- elif not resume:
- resume = _read_active_session_file(active_session_file)
- if resume:
- # The client only pins the viewport to the bottom when it asked
- # for `?resume=`; announce the implicit active-session replay so
- # it gets the same follow-scroll treatment.
- # See #93518.
- await ws.send_json({"type": "resume", "id": resume})
+ if channel and not force_fresh and resume and resume != raw_resume:
+ # The client only pins the viewport to the bottom when it asked
+ # for `?resume=`; announce the implicit active-session replay so
+ # it gets the same follow-scroll treatment.
+ # See #93518.
+ await ws.send_json({"type": "resume", "id": resume})
resolve_kwargs = {"resume": resume, "sidecar_url": sidecar_url, "profile": profile}
if active_session_file is not None:
@@ -639,27 +679,7 @@ async def pty_ws(ws: WebSocket) -> None:
# it while detached. On child EOF it closes the attached socket with 4410,
# which unparks ws.receive() — same half-open protection as the legacy pump.
try:
- while True:
- try:
- msg = await ws.receive()
- except RuntimeError: # receive() after the drain task already closed us
- break
- if msg.get("type") == "websocket.disconnect":
- break
- raw = msg.get("bytes")
- if raw is None:
- text = msg.get("text")
- raw = text.encode("utf-8") if isinstance(text, str) else b""
- if not raw:
- continue
- # Resize escape is consumed locally, never written to the PTY.
- match = _RESIZE_RE.match(raw)
- if match and match.end() == len(raw):
- session.bridge.resize(cols=int(match.group(1)), rows=int(match.group(2)))
- continue
- if not await session.write(ws, raw):
- await _close_stalled_pty_input(ws, path="keepalive")
- break
+ await _pump_keepalive_input(ws, session)
except WebSocketDisconnect:
pass
finally: