The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(desktop): review follow-ups for the stale-RPC salvage
kshitijk4poor1df78c2da8ef
diff --git a/apps/desktop/src/store/composer-status.ts b/apps/desktop/src/store/composer-status.tsindex f63bbe33ca0..f6970880b6b 100644--- a/apps/desktop/src/store/composer-status.ts+++ b/apps/desktop/src/store/composer-status.ts@@ -10,6 +10,7 @@ import { dispatchNativeNotification } from './native-notifications'import { notifyError } from './notifications'import { isSessionGone, isSessionGoneForBackgroundPolling, markSessionGone, noteRuntimeAlive } from './runtime-gone'import { $sessions, lineageAliases } from './session'+import { ambientRequestFor } from './session-gone-latch'import { $sessionStates, requestForOwnedSession } from './session-states'import { $subagentsBySession, type SubagentProgress } from './subagents'import { $todosBySession } from './todos'@@ -401,12 +402,9 @@ export async function refreshBackgroundProcesses(sid: string): Promise<void> {}try {- const ambientRequest = <T>(method: string, params?: Record<string, unknown>) =>- gateway.request<T>(method, params ?? {})-const result = await requestForOwnedSession<{ processes?: GatewayProcessEntry[] }>(sid,- ambientRequest,+ ambientRequestFor(gateway),'process.list',{ session_id: sid })@@ -467,10 +465,7 @@ export async function stopBackgroundProcess(sid: string, id: string): Promise<vo}try {- const ambientRequest = <T>(method: string, params?: Record<string, unknown>) =>- gateway.request<T>(method, params ?? {})-- await requestForOwnedSession(sid, ambientRequest, 'process.kill', { process_id: id, session_id: sid })+ await requestForOwnedSession(sid, ambientRequestFor(gateway), 'process.kill', { process_id: id, session_id: sid })dismissBackgroundProcess(sid, id)} catch (err) {if (isSessionGoneForBackgroundPolling(err)) {@@ -507,10 +502,7 @@ export function resetSessionBackground(sid: string) {if (item.state === 'running') {if (gateway && !isSessionGone(sid)) {- const ambientRequest = <T>(method: string, params?: Record<string, unknown>) =>- gateway.request<T>(method, params ?? {})-- void requestForOwnedSession(sid, ambientRequest, 'process.kill', {+ void requestForOwnedSession(sid, ambientRequestFor(gateway), 'process.kill', {process_id: item.id,session_id: sid}).catch(error => {diff --git a/apps/desktop/src/store/goals.ts b/apps/desktop/src/store/goals.tsindex 9d846fbcafb..8bd1ba4a0b5 100644--- a/apps/desktop/src/store/goals.ts+++ b/apps/desktop/src/store/goals.ts@@ -4,6 +4,7 @@ import { keyedTimeouts } from '@/lib/keyed-timeouts'import { $gateway } from './gateway'import { isSessionGone, isSessionGoneForBackgroundPolling, markSessionGone } from './runtime-gone'+import { ambientRequestFor } from './session-gone-latch'import { requestForOwnedSession } from './session-states'export type GoalStatus = 'active' | 'done' | 'paused' | 'waiting'@@ -170,10 +171,7 @@ export async function refreshSessionGoal(sid: string): Promise<void> {}try {- const ambientRequest = <T>(method: string, params?: Record<string, unknown>) =>- gateway.request<T>(method, params ?? {})-- const result = await requestForOwnedSession<{ output?: string }>(sid, ambientRequest, 'slash.exec', {+ const result = await requestForOwnedSession<{ output?: string }>(sid, ambientRequestFor(gateway), 'slash.exec', {command: 'goal status',session_id: sid})diff --git a/apps/desktop/src/store/prompts.ts b/apps/desktop/src/store/prompts.tsindex 51b6f0df7c2..13637fc797a 100644--- a/apps/desktop/src/store/prompts.ts+++ b/apps/desktop/src/store/prompts.ts@@ -3,6 +3,7 @@ import { atom, computed, type ReadableAtom } from 'nanostores'import { $clarifyRequest, $clarifyRequests } from './clarify'import { isSessionGone, isSessionGoneForBackgroundPolling, markSessionGone } from './runtime-gone'import { $activeSessionId } from './session'+import { ambientRequestFor } from './session-gone-latch'import { requestForOwnedSession } from './session-states'// Blocking interactive prompts the gateway raises mid-turn. Each maps to a@@ -122,10 +123,7 @@ export async function receiveApprovalRequest(gateway: ApprovalGateway | null, reif (gateway && request.requestId && request.sessionId) {try {- const ambientRequest = <T>(method: string, params?: Record<string, unknown>) =>- gateway.request(method, params ?? {}) as Promise<T>-- await requestForOwnedSession(request.sessionId, ambientRequest, 'approval.received', {+ await requestForOwnedSession(request.sessionId, ambientRequestFor(gateway), 'approval.received', {request_id: request.requestId,session_id: request.sessionId})@@ -149,10 +147,9 @@ export async function replayPendingApproval(gateway: ApprovalGateway | null, seslet rawResult: unknowntry {- const ambientRequest = <T>(method: string, params?: Record<string, unknown>) =>- gateway.request(method, params ?? {}) as Promise<T>-- rawResult = await requestForOwnedSession(sessionId, ambientRequest, 'approval.pending', { session_id: sessionId })+ rawResult = await requestForOwnedSession(sessionId, ambientRequestFor(gateway), 'approval.pending', {+ session_id: sessionId+ })} catch (error) {if (isSessionGoneForBackgroundPolling(error)) {markSessionGone(sessionId)diff --git a/apps/desktop/src/store/runtime-gone.test.ts b/apps/desktop/src/store/runtime-gone.test.tsindex 6aedcdd3e9d..48170769c3e 100644--- a/apps/desktop/src/store/runtime-gone.test.ts+++ b/apps/desktop/src/store/runtime-gone.test.ts@@ -183,6 +183,20 @@ describe('gone-latch classifier and rebind seam', () => {expect(isSessionGone('rt-other')).toBe(false)})+ it('a respawned backend (global clear) also resets every heal budget', () => {+ for (const rt of ['rt-1', 'rt-2', 'rt-3']) {+ $sessionStates.set({ [rt]: cachedState(STORED) })+ $sessionTiles.set([tile(STORED, rt)])+ expect(markRuntimeGone(rt)).toBe(true)+ }++ resetBackgroundPollingGuard()++ $sessionStates.set({ 'rt-4': cachedState(STORED) })+ $sessionTiles.set([tile(STORED, 'rt-4')])+ expect(markRuntimeGone('rt-4')).toBe(true)+ })+it('refunds the stored session heal budget on a successful rebind', () => {// Three reaps exhaust MAX_CONSECUTIVE_HEALS for STORED...for (const rt of ['rt-1', 'rt-2', 'rt-3']) {diff --git a/apps/desktop/src/store/session-gone-latch.ts b/apps/desktop/src/store/session-gone-latch.tsindex d6cc153c2cf..5e22ebf5502 100644--- a/apps/desktop/src/store/session-gone-latch.ts+++ b/apps/desktop/src/store/session-gone-latch.ts@@ -8,13 +8,20 @@ import { JsonRpcGatewayError } from '@hermes/shared'* status) and by the owner-routed RPC seam that clears it. This module is a* dependency-free leaf on purpose: `session-request-router` (which every* store imports) must be able to clear the latch after a successful rebind- * without pulling the session/tile stores into its import graph. The public- * surface for callers is `runtime-gone.ts`, which re-exports everything here. */+ * without pulling the session/tile stores into its import graph. Stores that+ * also need the heal levers import through `runtime-gone.ts` (which re-exports+ * this module); cycle-sensitive callers (the router, the gateway event loop)+ * import the leaf directly. */const goneSessions = new Set<string>()/** Gateway JSON-RPC code for "session not found" (tui_gateway `_sess_nowait`). */const GATEWAY_SESSION_NOT_FOUND_CODE = 4001+/** Consecutive heals per stored session id (see `runtime-gone.ts`+ * `markRuntimeGone`). Lives here so the rebind seam below can refund it+ * without importing the heal module. */+export const healsByStoredId = new Map<string, number>()+/** A gone session is unrecoverable for THIS runtime id; a timeout or transport* blip is not. Only the former may stop a poll — misclassifying a transient* failure would silently freeze a healthy session.@@ -67,6 +74,9 @@ export function resetBackgroundPollingGuard(sid?: string): void {}goneSessions.clear()+ // Same lifetime as the latch: a respawned backend re-mints every runtime+ // id, so every stored session's heal budget starts over too.+ healsByStoredId.clear()}/** Ids a successful `session.resume` / `session.activate` just rebound — the@@ -90,11 +100,6 @@ function reboundSessionIds(method: string, params: Record<string, unknown>, resureturn ids}-/** Consecutive heals per stored session id (see `runtime-gone.ts`- * `markRuntimeGone`). Lives here so the rebind seam below can refund it- * without importing the heal module. */-export const healsByStoredId = new Map<string, number>()-/** Un-latch the ids a successful `session.resume` / `session.activate` just* rebound and refund the stored session's heal budget: a rebind is proof of* life, so the NEXT reap can still be healed. Without the refund a backend@@ -113,3 +118,13 @@ export function resetBackgroundPollingGuardAfterRebind(healsByStoredId.delete(id)}}++/** Adapt a store-level gateway handle (`$gateway.get()` or the narrower+ * `ApprovalGateway` shape) to the ambient-request callback+ * `requestForOwnedSession` expects. The pollers never pass a deadline, so the+ * 2-arg call shape is kept exactly (gateway.request callers assert on it). */+export function ambientRequestFor(gateway: {+ request: (method: string, params: Record<string, unknown>) => Promise<unknown>+}): <R>(method: string, params?: Record<string, unknown>) => Promise<R> {+ return <R>(method: string, params?: Record<string, unknown>) => gateway.request(method, params ?? {}) as Promise<R>+}