The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): settle each remote profile's sidebar read under its own budget
Austin Pickett049d2c222347
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 8d65d7e4104..e5ab3eae80b 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -485,18 +485,20 @@ import {import { migrateActiveProfileIfMissing as migrateActiveProfileIfMissingPure } from './profile-migration'import { prepareProfileRenameLifecycle, profileRenameFromRequest } from './profile-rename-routing'import {+ applyRemoteProfileSessionOutcomes,assembleSidebarSessionSlices,buildSidebarSessionSliceParams,+ composeUnifiedSessionResponse,fetchPrimaryProfileSessions,fetchRegistrySessionRows,fetchRemoteProfileSessions,findRemoteOwnerProfileForSession,hasPinnedRegistrySessionSource,isAllProfilesSessionListRequest,- mergeProfileSessionWindow,pathWithRemoteOwnerScope,type RegistrySessionSource,remoteProfileQueryScope,+ settleRemoteProfileSessions,shouldIncludeLocalRegistrySessionSource,spliceRegistrySessionRows,tagRegistrySessionResponse,@@ -17411,24 +17413,27 @@ async function mergeRemoteProfileSessions(searchParams, remoteProfiles, registryconst profileTotals = { ...(base.profile_totals || {}) }let total = (Number(base.total) || 0) - remoteProfiles.reduce((n, p) => n + (profileTotals[p] || 0), 0)- // Swap each remote profile's stale local rows/total for the remote's real ones.- await Promise.all(- remoteProfiles.map(async name => {- const list = await remoteSessionList(name, remoteParams).catch(() => null)-- if (!list) {- delete profileTotals[name] // dead remote → drop its stale local total too-- return- }+ // Swap each remote profile's stale local rows/total for the remote's real+ // ones. #75712: each remote fetch runs under its own bounded budget and+ // settles — one unavailable remote no longer holds the whole aggregate (and+ // the sidebar behind it) hostage for the 180s backend readiness wait. A+ // dead or still-pending remote contributes no rows, drops its stale local+ // total, and is NAMED in the response `errors` instead of silently+ // vanishing; the fetch itself keeps running so a remote that is merely slow+ // to boot lands on a later refresh.+ const remoteOutcomes = await settleRemoteProfileSessions(remoteProfiles, name =>+ remoteSessionList(name, remoteParams)+ )- const rows = rowsOf(list)- merged.push(...rows)- profileTotals[name] = Number(list.total) || rows.length- total += profileTotals[name]- })+ const { total: remoteTotal, errors: remoteErrors } = applyRemoteProfileSessionOutcomes(+ remoteOutcomes,+ merged,+ profileTotals,+ total)+ total = remoteTotal+// Registry gateways (v2 connections): splice every CONNECTED gateway's rows// into the unified list. Only already-pooled backends are read — a sidebar// refresh must never dial or spawn a backend (the Bot Mode roster-respawn@@ -17448,12 +17453,7 @@ async function mergeRemoteProfileSessions(searchParams, remoteProfiles, registryconst recency = s => s?.[order] ?? s?.started_at ?? 0merged.sort((a, b) => recency(b) - recency(a))- return {- ...(base as any),- sessions: mergeProfileSessionWindow(merged, offset, limit),- total,- profile_totals: profileTotals- }+ return composeUnifiedSessionResponse(base, merged, offset, limit, total, profileTotals, remoteErrors)}// Every CONNECTED registry gateway as a session source: resolved descriptorsdiff --git a/apps/desktop/electron/profile-session-routing.test.ts b/apps/desktop/electron/profile-session-routing.test.tsindex b5bad5da537..c6367dfdc55 100644--- a/apps/desktop/electron/profile-session-routing.test.ts+++ b/apps/desktop/electron/profile-session-routing.test.ts@@ -14,6 +14,7 @@ import {mergeProfileSessionWindow,pathWithRemoteOwnerScope,remoteProfileQueryScope,+ settleRemoteProfileSessions,shouldIncludeLocalRegistrySessionSource,spliceRegistrySessionRows,tagRegistrySessionResponse,@@ -172,6 +173,41 @@ test('reassembled sidebar slices keep each slice errors', () => {assert.deepEqual(result.cron.sessions, [{ id: 'cron-1' }])})+// #75712: one unavailable remote profile must not hold the whole sidebar+// aggregate hostage — a dead URL hangs until the backend readiness deadline+// (180s), so the per-profile fetch needs its own bounded budget and a settled+// outcome naming the failure, never a rejection.+test('settleRemoteProfileSessions returns healthy rows while a dead remote hits its budget', async () => {+ const healthy = { sessions: [{ id: 'remote-1', profile: 'taro' }], total: 1, profile_totals: {} }++ const result = await settleRemoteProfileSessions(+ ['taro', 'dead'],+ profile => (profile === 'taro' ? Promise.resolve(healthy) : new Promise(() => {})), // never settles — readiness wait+ { budgetMs: 10 }+ )++ assert.equal(result.length, 2)+ assert.deepEqual(result[0], { profile: 'taro', list: healthy, error: null })+ assert.equal(result[1].list, null)+ assert.match(result[1].error, /did not respond within 10ms/)+})++test('settleRemoteProfileSessions settles a rejecting remote as an error entry', async () => {+ const result = await settleRemoteProfileSessions(+ ['ok', 'refused'],+ profile =>+ profile === 'ok'+ ? Promise.resolve({ sessions: [], total: 0, profile_totals: {} })+ : Promise.reject(new Error('connect ECONNREFUSED 127.0.0.1:19119')),+ { budgetMs: 5_000 }+ )++ assert.equal(result[0].error, null)+ assert.deepEqual(result[0].list, { sessions: [], total: 0, profile_totals: {} })+ assert.equal(result[1].list, null)+ assert.equal(result[1].error, 'connect ECONNREFUSED 127.0.0.1:19119')+})+test('remote session reads split oversized sidebar windows into API-safe pages', async () => {const calls: Array<{ profile: string | null; path: string }> = []const rows = Array.from({ length: 250 }, (_, index) => ({ id: `session-${index}` }))diff --git a/apps/desktop/electron/profile-session-routing.ts b/apps/desktop/electron/profile-session-routing.tsindex b58412e7272..645ac7a33cb 100644--- a/apps/desktop/electron/profile-session-routing.ts+++ b/apps/desktop/electron/profile-session-routing.ts@@ -768,6 +768,126 @@ export async function fetchRemoteProfileSessions(}}+/** Per-profile budget for one remote's session read inside a sidebar aggregate.+ * A healthy remote answers in well under a second; a dead URL hangs until the+ * 180s backend readiness deadline. The aggregate must not inherit that deadline+ * (#75712) — each remote gets this budget, then contributes an error entry+ * instead of rows. */+export const REMOTE_PROFILE_SESSION_BUDGET_MS = 10_000++/** One remote profile's settled contribution to a sidebar aggregate: rows when+ * it answered inside its budget, otherwise a named error. Never a rejection —+ * one dead remote must not take the other profiles' rows down with it. */+export interface RemoteProfileSessionsOutcome {+ profile: string+ list: SessionListResponse | null+ error: string | null+}++/**+ * #75712: fetch every remote profile's session list, each under its own+ * bounded budget, and settle each outcome instead of letting one unavailable+ * remote block (or silently vanish from) the whole sidebar aggregate. The+ * underlying fetch keeps running past its budget — a remote that is merely+ * still booting can answer on a later refresh.+ */+export async function settleRemoteProfileSessions(+ remoteProfiles: readonly string[],+ fetchRemote: (profile: string) => Promise<unknown>,+ options: { budgetMs?: number } = {}+): Promise<RemoteProfileSessionsOutcome[]> {+ const budgetMs = options.budgetMs ?? REMOTE_PROFILE_SESSION_BUDGET_MS++ return Promise.all(+ remoteProfiles.map(async profile => {+ let timer: ReturnType<typeof setTimeout> | undefined++ try {+ const list = (await Promise.race([+ fetchRemote(profile),+ new Promise<never>((_, reject) => {+ timer = setTimeout(+ () => reject(new Error(`remote profile "${profile}" did not respond within ${budgetMs}ms`)),+ budgetMs+ )+ })+ ])) as SessionListResponse++ return { profile, list, error: null }+ } catch (error) {+ return {+ profile,+ list: null,+ error: error instanceof Error ? error.message : String(error)+ }+ } finally {+ if (timer) {+ clearTimeout(timer)+ }+ }+ })+ )+}++/** Fold each settled remote outcome (#75712) into the unified aggregate:+ * answering remotes contribute rows and their real per-profile total; a+ * dead or still-pending remote drops its stale local total and is NAMED in+ * the returned error entries instead of silently vanishing. Returns the+ * updated grand total plus the collected errors so they can ride the+ * response payload. */+export function applyRemoteProfileSessionOutcomes(+ outcomes: readonly RemoteProfileSessionsOutcome[],+ merged: unknown[],+ profileTotals: Record<string, number>,+ total: number+): { total: number; errors: Array<{ profile: string; error: string }> } {+ const errors: Array<{ profile: string; error: string }> = []++ for (const { profile, list, error } of outcomes) {+ if (!list) {+ delete profileTotals[profile] // dead remote → drop its stale local total too+ errors.push({ profile, error: error || 'unavailable' })++ continue+ }++ const rows = rowsOf(list)+ merged.push(...rows)+ profileTotals[profile] = Number(list.total) || rows.length+ total += profileTotals[profile]+ }++ return { total, errors }+}++/** Compose the unified aggregate's response (#75712): failures ride the+ * payload, not the void — base scan errors and each dead remote's named+ * error entry are merged into `errors` so a failed remote profile is+ * identifiable. */+export function composeUnifiedSessionResponse(+ base: unknown,+ merged: unknown[],+ offset: number,+ limit: number,+ total: number,+ profileTotals: Record<string, number>,+ extraErrors: Array<{ profile: string; error: string }>+) {+ const baseErrors = Array.isArray((base as { errors?: unknown })?.errors)+ ? (base as { errors: Array<{ profile: string; error: string }> }).errors+ : []++ const scanErrors = [...baseErrors, ...extraErrors]++ return {+ ...(base as Record<string, unknown>),+ sessions: mergeProfileSessionWindow(merged, offset, limit),+ total,+ profile_totals: profileTotals,+ ...(scanErrors.length ? { errors: scanErrors } : {})+ }+}+/*** #85834: which remote profile owns `sessionId`, when a /api/sessions/{id}* caller supplied no profile hint. Reads the same per-remote lists the list