From 66a56cc33776b4d47c5dc0f2e949461f09e39533 Mon Sep 17 00:00:00 2001 From: kyssta-exe <218078013+kyssta-exe@users.noreply.github.com> Date: Sat, 5 Sep 2026 03:50:42 +0000 Subject: [PATCH] fix(desktop): prevent background tile reconcile from starving pool slots (#103375) Passive tile reconciles (reconcileTileTranscripts on every sessions.changed tick) were cold-starting pooled backends for every open bot tile, including hidden ones, and holding each spawned slot for its lifetime via the 10s touch loop. With 15-20+ profiles this permanently saturated the Warm Bot Backends pool so interactive opens timed out. Add a passive read intent: getLatestSessionMessages now accepts a passive flag forwarded as HermesApiRequest.passive. The Electron main process honors it by serving only already-warm pool entries and failing fast otherwise, without spawning or bumping lastActiveAt. reconcileTileTranscripts uses passive reads so background refresh never consumes a pool slot; interactive opens keep the normal spawn path. --- apps/desktop/electron/main.ts | 47 ++++++++++++++++++- apps/desktop/src/api/sessions.ts | 23 +++++---- .../app/contrib/hooks/use-background-sync.ts | 2 +- apps/desktop/src/global.d.ts | 5 ++ 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index 64eb1e76be..362ddca389 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -16590,6 +16590,32 @@ async function dispatchRegistryApiRequest( routeProfile = request?.profile, requestProfile = request?.profile ) { + // Passive tile reconcile must never cold-start a pooled backend (#103375): + // fail fast when no warm entry exists instead of spawning a child and + // starving the pool. Interactive opens (passive:false) keep the normal spawn. + if (request?.passive) { + const poolKey = backendScopeKey(registryConnectionId, routeProfile) + const entry = backendPool.get(poolKey) + + if (!entry) { + throw new Error(`Passive read: no warm backend for "${poolKey}"`) + } + + const connection = await entry.connectionPromise + const requestPath = pathForRegistryBackendRequest(request.path, requestProfile, connection) + + const response = await fetchJsonForBackend(connection, requestPath, { + method: request?.method, + body: request?.body, + upload: request?.upload, + timeoutMs: resolveTimeoutMs(request?.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS) + }) + + return (request?.method || 'GET').toUpperCase() === 'GET' + ? tagRegistrySessionResponse(requestPath, response, registryConnectionId) + : response + } + // Claim-guarded (#90812): every registry-scoped REST call funnels through // here, so it can race a renderer's own WS reconnect dial for the same // (connectionId, profile) scope; coalescing avoids bootstrapping a second @@ -16681,7 +16707,26 @@ async function handleHermesApiRequest(request) { let response try { - const connection = await ensureBackend(routeProfile) + // Passive reads (tile reconciles) must not spawn pool backends (#103375). + // If the target is pooled and has no warm entry, fail fast without + // consuming a slot; interactive reads keep the normal ensureBackend path. + // Primary route (backendProfile === null) always has a backend (startHermes). + const isPassivePooled = Boolean(request?.passive && routeProfile && apiRoute.backendProfile) + + let connection + + if (isPassivePooled) { + const key = String(routeProfile).trim() + const entry = backendPool.get(key) || backendPool.get(backendScopeKey(null, key)) + + if (!entry) { + throw new Error(`Passive read: no warm backend for profile "${key}"`) + } + + connection = await entry.connectionPromise + } else { + connection = await ensureBackend(routeProfile) + } const timeoutMs = resolveTimeoutMs(request?.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS) const url = `${connection.baseUrl}${apiRoute.requestPath}` diff --git a/apps/desktop/src/api/sessions.ts b/apps/desktop/src/api/sessions.ts index 61368921bc..314c5c5ba8 100644 --- a/apps/desktop/src/api/sessions.ts +++ b/apps/desktop/src/api/sessions.ts @@ -396,7 +396,8 @@ export function getSession(id: string, profile?: ProfileScope): Promise { const query = new URLSearchParams() @@ -426,7 +427,8 @@ export function getSessionMessages( return hermesApi({ ...sessionScope, - path: `/api/sessions/${encodeURIComponent(id)}/messages${suffix}` + path: `/api/sessions/${encodeURIComponent(id)}/messages${suffix}`, + ...(options.passive ? { passive: true } : {}) }) } @@ -439,15 +441,20 @@ export function getSessionMessages( */ export const LATEST_SESSION_MESSAGES_LIMIT = 120 -export function getLatestSessionMessages(id: string, profile?: ProfileScope): Promise { +export function getLatestSessionMessages(id: string, profile?: ProfileScope, passive = false): Promise { // includeCompacted: durable display history must include rows preserved by // in-place compaction (active=0, compacted=1); without them the transcript // silently ends at the compaction boundary and earlier turns are unreachable. - return getSessionMessages(id, profile, { - limit: LATEST_SESSION_MESSAGES_LIMIT, - order: 'latest', - includeCompacted: true - }).then(page => { + return getSessionMessages( + id, + profile, + { + limit: LATEST_SESSION_MESSAGES_LIMIT, + order: 'latest', + includeCompacted: true + }, + passive ? { passive: true } : {} + ).then(page => { // Record whether the tail was truncated (page came back full) and where // the next older page starts, so "Show earlier" can backfill over REST // (app/chat/transcript-backfill). Keyed under both the requested id and diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts index 5363eeb149..530de287f7 100644 --- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts +++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts @@ -160,7 +160,7 @@ export async function reconcileTileTranscripts({ const signatureKey = tileTranscriptSignatureKey(tile) try { - const latest = await getLatestSessionMessages(storedSessionId, profileScope) + const latest = await getLatestSessionMessages(storedSessionId, profileScope, true) if ( requestId !== requestSequenceRef.current || diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts index 181ae83f9c..0e021599e0 100644 --- a/apps/desktop/src/global.d.ts +++ b/apps/desktop/src/global.d.ts @@ -1238,6 +1238,11 @@ export interface HermesApiRequest { // through the owning connection, not the local profile pool. Omit / '' to // keep the legacy profile-routed path; explicit 'local' forces this device. connectionId?: string | null + // Passive background read that must never cold-start a pooled backend (#103375). + // When true and the target profile has no warm pool entry, the main process + // fails fast without spawning a child or consuming a pool slot, so background + // tile reconciles cannot starve interactive opens. + passive?: boolean } export interface HermesPreviewTarget {