Skip to content

The agent that grows with you

refactor: keep handle_ws and en.ts inside their code-health caps

Tekniuma67e2272ccaf
diff --git a/apps/desktop/src/i18n/en.ts b/apps/desktop/src/i18n/en.ts
index 11c289801e9..9e3d66b9d1a 100644
--- a/apps/desktop/src/i18n/en.ts
+++ b/apps/desktop/src/i18n/en.ts
@@ -3535,8 +3535,7 @@ export const en: Translations = {
restoreImageDraft: 'Restore image draft',
queueCancelFailed: "Couldn't remove the queued turn",
queueLostDiscard: 'Discard',
- queueLostDiscardTip:
- 'The gateway restarted mid-turn and cannot finish this one. Discard it so the queued turns behind it run.',
+ queueLostDiscardTip: 'The gateway restarted mid-turn and cannot finish this one. Discard it so the queued turns behind it run.',
queueResume: 'Resume',
queueResumeTip: 'Paused by Stop — resume sending the queued turns',
queueStuckTitle: 'Queued message not sent',
@@ -4694,14 +4693,7 @@ export const en: Translations = {
title: 'Hermes got stuck in a loop',
body: 'The reply kept repeating the same steps, so Hermes stopped it. Retry, or start a new chat if it happens again.'
},
- SESSION_OWNER_BUSY: {
- title: 'Hermes is busy in this chat',
- body: 'Another turn is running here and your message could not join its queue. Nothing was sent. Retry once that turn ends.'
- },
- SESSION_OWNER_UNCONFIRMED: {
- title: 'Your message may already be queued',
- body: 'Another turn is running in this chat and the gateway did not confirm your message in time. It may still run after that turn. Check the chat before sending it again.'
- },
+ SESSION_OWNER_BUSY: { title: 'Hermes is busy in this chat', body: 'Another turn is running here and your message could not join its queue. Nothing was sent. Retry once that turn ends.' }, SESSION_OWNER_UNCONFIRMED: { title: 'Your message may already be queued', body: 'The gateway did not confirm your message in time. It may still run after the current turn. Check the chat before sending it again.' },
SESSION_NOT_OWNED: {
title: 'This chat is open somewhere else',
body: 'This chat is currently open in another Hermes window or terminal. Close it there and send your message again, or start a new chat here.'
diff --git a/tui_gateway/ws.py b/tui_gateway/ws.py
index 266ff612755..a8f4275b0b5 100644
--- a/tui_gateway/ws.py
+++ b/tui_gateway/ws.py
@@ -484,6 +484,23 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
continue
await _dispatch_one(req)
+ async def _queue_request(req: Any) -> None:
+ if isinstance(req, dict) and req.get("method") in _WS_CONCURRENT_READ_METHODS:
+ # A read past the waiting cap parks the reader, as the serial lane did; released
+ # when its task ends (a read cancelled before it ran releases it too).
+ await _unless_dispatch_failed(read_admission.acquire())
+ await _unless_dispatch_failed(pending.put(req))
+
+ async def _end_reads() -> None:
+ """Reads that never started are dropped (their client is gone); started ones finish before the
+ authority closes, unless the connection itself was cancelled (server shutdown)."""
+ task = asyncio.current_task()
+ shutting_down = task is not None and task.cancelling()
+ for read in concurrent_reads:
+ if shutting_down or read not in started_reads:
+ read.cancel()
+ await asyncio.gather(*concurrent_reads, return_exceptions=True)
+
async def _unless_dispatch_failed(aw: Any) -> Any:
"""Await *aw*, or raise _SendFailed once a reply could not be sent (the dispatcher ended on one,
or the read lane failed one)."""
@@ -568,11 +585,7 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
await _reply({"jsonrpc": "2.0", "result": {"ok": True}, "id": req_id}, "send_failed_after_heartbeat",
"ws heartbeat reply send failed peer=%s id=%s", peer, req_id)
continue
- if isinstance(req, dict) and req.get("method") in _WS_CONCURRENT_READ_METHODS:
- # A read past the waiting cap parks the reader, as the serial lane did; released
- # when its task ends (a read cancelled before it ran releases it too).
- await _unless_dispatch_failed(read_admission.acquire())
- await _unless_dispatch_failed(pending.put(req))
+ await _queue_request(req)
except _SendFailed:
pass
finally:
@@ -590,14 +603,7 @@ async def handle_ws(ws: Any, *, auth_identity: dict | None = None, subprotocol:
failure = None if dispatcher.cancelled() else dispatcher.exception()
if failure is not None and not isinstance(failure, _SendFailed):
_log.error("ws dispatcher failed peer=%s", peer, exc_info=failure)
- # Reads that never started are dropped (their client is gone); started ones finish before the
- # authority closes, unless the connection itself was cancelled (server shutdown).
- task = asyncio.current_task()
- shutting_down = task is not None and task.cancelling()
- for read in concurrent_reads:
- if shutting_down or read not in started_reads:
- read.cancel()
- await asyncio.gather(*concurrent_reads, return_exceptions=True)
+ await _end_reads()
if authority_connection is not None:
# After the dispatcher drained: an in-flight authority call must not see its connection closed.
await authority_connection.close()