Skip to content

The agent that grows with you

fix(desktop): answer server→client requests when a handler crashes instead of stalling the backend

Pondae43ded1bcc0
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 3ff6186c4cb..01debc2d316 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -1,6 +1,7 @@
import {
type ConnectionState,
type GatewayEvent,
+ JSON_RPC_METHOD_NOT_FOUND,
reconnectBackoffDelayMs,
registryBackendScopeKey,
resolveGatewayWsUrl,
@@ -291,9 +292,18 @@ export interface ScopedServerRequest extends ServerRequest {
/** Fan a primary-socket server request into the registry handler with the active source tags. */
export function dispatchPrimaryServerRequest(request: ServerRequest, profile: string): void {
- const connectionId = g.config?.activeConnectionId?.() ?? null
+ // Fail fast, never swallow: the backend blocks on this answer (clarify waits
+ // its full 3600s deadline). Without a registry there is nobody to answer —
+ // -32601 tells the tool immediately instead of stalling the turn.
+ if (!g.config?.onServerRequest) {
+ request.fail(JSON_RPC_METHOD_NOT_FOUND, 'Hermes Desktop has no server-request registry yet')
- g.config?.onServerRequest?.({ ...request, ...(connectionId ? { connectionId } : {}), profile })
+ return
+ }
+
+ const connectionId = g.config.activeConnectionId?.() ?? null
+
+ g.config.onServerRequest({ ...request, ...(connectionId ? { connectionId } : {}), profile })
}
export function setPrimaryGateway(gateway: HermesGateway | null, profile = 'default'): void {
@@ -863,7 +873,18 @@ function createSecondary(profile: string, connectionId: null | string = null): S
})
entry.offRequest =
gateway.onRequest?.(request => {
- g.config?.onServerRequest?.({ ...request, ...(connectionId ? { connectionId } : {}), profile })
+ // Same contract as dispatchPrimaryServerRequest: no registry = fail fast
+ // with -32601 (the backend blocks on the answer; silent drops stall the
+ // tool for its whole deadline).
+ if (!g.config?.onServerRequest) {
+ request.fail(JSON_RPC_METHOD_NOT_FOUND, 'Hermes Desktop has no server-request registry yet')
+
+ return
+ }
+
+ const scopedConnectionId = connectionId ?? (g.config.activeConnectionId?.() ?? null)
+
+ g.config.onServerRequest({ ...request, ...(scopedConnectionId ? { connectionId: scopedConnectionId } : {}), profile })
}) ?? (() => {})
entry.offState = gateway.onState(state => {
reportGatewayState(scope, state)
diff --git a/apps/shared/src/json-rpc-channel.test.ts b/apps/shared/src/json-rpc-channel.test.ts
index 80685ba8df1..a6d71bab81e 100644
--- a/apps/shared/src/json-rpc-channel.test.ts
+++ b/apps/shared/src/json-rpc-channel.test.ts
@@ -216,4 +216,44 @@ describe('JsonRpcRequestChannel', () => {
await expect(resume).resolves.toMatchObject({ session_id: 's1' })
expect(delivered).toEqual([{ id: 'srq-9', replayed: true }])
})
+
+ // Regression (2026-09-15 clarify spinner): a throwing handler used to escape
+ // deliverRequest inside the socket listener — no response frame at all, so
+ // the backend (clarify_tool: 3600s deadline) waited out the whole block.
+ it('answers -32603 when a handler throws, keeps later frames working, and still answers -32601 otherwise', () => {
+ const channel = new JsonRpcRequestChannel()
+ const { sent, transport } = spyTransport()
+
+ channel.attach(transport)
+ channel.onRequest(req => {
+ if (req.method === 'boom') {
+ throw new Error('handler exploded')
+ }
+
+ if (req.method === 'clarify') {
+ req.respond({ answer: 'yes' })
+
+ return true
+ }
+
+ return false
+ })
+
+ channel.handleFrame(JSON.stringify({ id: 'srq-1', jsonrpc: '2.0', method: 'boom', params: { session_id: 's1' } }))
+ channel.handleFrame(JSON.stringify({ id: 'srq-2', jsonrpc: '2.0', method: 'nobody', params: { session_id: 's1' } }))
+
+ const frames = sent.map(f => JSON.parse(f) as { id: string; error?: { code: number; message?: string } })
+
+ expect(frames[0].id).toBe('srq-1')
+ expect(frames[0].error?.code).toBe(-32603)
+ expect(frames[0].error?.message).toContain('boom')
+ expect(frames[1].id).toBe('srq-2')
+ expect(frames[1].error?.code).toBe(-32601)
+
+ // The channel survives: a normal request after the crash still routes.
+ channel.handleFrame(JSON.stringify({ id: 'srq-3', jsonrpc: '2.0', method: 'clarify', params: { session_id: 's1' } }))
+ const third = sent.at(-1)!
+ expect((JSON.parse(third) as { id: string }).id).toBe('srq-3')
+ expect((JSON.parse(third) as { result?: { answer?: string } }).result?.answer).toBe('yes')
+ })
})
diff --git a/apps/shared/src/json-rpc-channel.ts b/apps/shared/src/json-rpc-channel.ts
index 46b1f1b0be2..f6a6a1926c8 100644
--- a/apps/shared/src/json-rpc-channel.ts
+++ b/apps/shared/src/json-rpc-channel.ts
@@ -69,6 +69,14 @@ export class JsonRpcGatewayError extends Error {
/** JSON-RPC "method not found" (tui_gateway/server.py::dispatch `_err(rid, -32601, …)`). */
export const JSON_RPC_METHOD_NOT_FOUND = -32601
+/** JSON-RPC "internal error" — used when a server→client request handler throws. */
+export const JSON_RPC_INTERNAL_ERROR = -32603
+
+/** Console sink so handler crashes are visible in the renderer devtools/console log. */
+const reportChannelError = (message: string, error: unknown): void => {
+ console.error(`[json-rpc-channel] ${message}`, error)
+}
+
/** Map a raw `error` member of a response frame to the typed error every surface inspects. */
export function jsonRpcErrorFromFrame(raw: unknown, fallbackMessage = 'Hermes RPC failed'): JsonRpcGatewayError {
const err = (raw && typeof raw === 'object' ? raw : {}) as JsonRpcErrorPayload
@@ -351,10 +359,38 @@ export class JsonRpcRequestChannel {
fail: (code, message) => send({ error: { code, message } })
}
+ let handled = false
+
for (const handler of this.requestHandlers) {
- if (handler(request) !== false) {
- return true
+ let accepted: boolean | void
+
+ try {
+ accepted = handler(request)
+ } catch (error) {
+ // A crashing handler must not leave the backend waiting out its full
+ // deadline (clarify blocks 3600s): answer -32603 and stop. The `send`
+ // guard makes this a no-op if the handler already responded.
+ request.fail(JSON_RPC_INTERNAL_ERROR, `server request handler crashed: ${method}`)
+ this.options.onUnhandledRequest?.({ id, method, params })
+
+ if (error instanceof Error) {
+ reportChannelError(`server request handler threw for ${method} (${id})`, error)
+ } else {
+ reportChannelError(`server request handler threw for ${method} (${id})`, new Error(String(error)))
+ }
+
+ return false
}
+
+ if (accepted !== false) {
+ handled = true
+
+ break
+ }
+ }
+
+ if (handled) {
+ return true
}
request.fail(JSON_RPC_METHOD_NOT_FOUND, `no handler for server request: ${method}`)
diff --git a/apps/shared/src/json-rpc-gateway.ts b/apps/shared/src/json-rpc-gateway.ts
index 338272b762c..612b5b17135 100644
--- a/apps/shared/src/json-rpc-gateway.ts
+++ b/apps/shared/src/json-rpc-gateway.ts
@@ -192,8 +192,20 @@ export class JsonRpcGatewayClient {
const text = wireFrameText(message.data)
- if (text !== null) {
+ if (text === null) {
+ return
+ }
+
+ try {
this.channel.handleFrame(text)
+ } catch (error) {
+ // handleFrame dispatches server→client requests into feature handlers;
+ // a throw here must never escape into the socket listener as an
+ // uncaught error (the backend would wait out its full request
+ // deadline — e.g. clarify's 3600s — with no answer frame at all).
+ // deliverRequest already fails crashed handlers with -32603; this
+ // catches everything else (event handlers, response resolution).
+ console.error('[json-rpc-gateway] frame handling crashed', error)
}
})