The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): stop extrapolating registry overlap from one fetched page
Teknium10152158f58c
diff --git a/apps/desktop/electron/profile-session-routing.test.ts b/apps/desktop/electron/profile-session-routing.test.tsindex 27357b160d7..fcb34bb0748 100644--- a/apps/desktop/electron/profile-session-routing.test.ts+++ b/apps/desktop/electron/profile-session-routing.test.ts@@ -18,6 +18,7 @@ import {isAllProfilesSessionListRequest,isRemoteSessionRequestPath,mergeProfileSessionWindow,+ mergeRegistrySessionRows,pathWithRemoteOwnerScope,remoteOwnerRoutePath,remoteProfileQueryScope,@@ -513,6 +514,39 @@ test('a registry source that overlaps the primary base is counted once, not twicassert.equal(profileTotals.default, 650)})+// Independent review (RT-42): overlap was extrapolated from the first fetched page, so a+// half-overlapping primary gave 325 or 975 for a 650-session union depending on which half+// it held. Drive both partial cases through mergeRegistrySessionRows, the real entry point.+for (const [label, primaryIds] of [+ ['none of', [] as number[]],+ ['all of', Array.from({ length: 650 }, (_, i) => i)],+ ['the newest half of', Array.from({ length: 325 }, (_, i) => i)],+ ['the oldest half of', Array.from({ length: 325 }, (_, i) => 325 + i)]+] as const) {+ test(`a registry scope whose primary holds ${label} its sessions totals the 650-session union`, async () => {+ const rows = Array.from({ length: 650 }, (_, i) => ({ id: `s${i}`, profile: 'default', started_at: 650 - i }))+ const merged: unknown[] = primaryIds.slice(0, 20).map(i => ({ id: `s${i}`, profile: 'default', connection_id: 'R' }))+ const profileTotals: Record<string, number> = primaryIds.length ? { default: primaryIds.length } : {}++ const contribution = await mergeRegistrySessionRows(+ [{ connectionId: 'R', kind: 'remote', backends: [{ descriptor: 'remote', profileLabel: null }] }],+ new URLSearchParams({ limit: '20', offset: '0' }),+ async (_descriptor, path) => {+ const params = new URL(path, 'http://x').searchParams+ const start = Number(params.get('offset'))+ const limit = Number(params.get('limit'))++ return { sessions: rows.slice(start, start + limit), total: 650, profile_totals: { default: 650 } }+ },+ merged,+ profileTotals+ )++ assert.equal(primaryIds.length + contribution, 650)+ assert.equal(profileTotals.default, 650)+ })+}+test('a failed later registry page does not contribute a stale first-page total', async () => {const reported = new Map<string, number>()diff --git a/apps/desktop/electron/profile-session-routing.ts b/apps/desktop/electron/profile-session-routing.tsindex 81ae099eecb..8a482696b12 100644--- a/apps/desktop/electron/profile-session-routing.ts+++ b/apps/desktop/electron/profile-session-routing.ts@@ -621,15 +621,29 @@ export function spliceRegistrySessionRows(return { added }}-/** Report full registry totals for scopes the primary/override path did not already count.+/** Report registry totals for scopes the primary/override path did not already count.* The fetched prefix can be shorter than the backend's total and can include pins outside- * the ordinary cursor, so counting only its rows undercounts deep pages or overcounts pins. */+ * the ordinary cursor, so counting only its rows undercounts deep pages or overcounts pins.+ *+ * Overlap with the primary is never extrapolated from the fetched prefix: duplicates in+ * one page say nothing about how much of the deeper scope the primary also holds. A scope+ * is disjoint (its full backend total is owed) only when no fetched row was a duplicate+ * AND the primary served no row from the same connection. Otherwise both sides counted+ * the same backend scope, and the only exact-or-safe figure is max(primary, registry):+ * exact when they are the same backend (their totals agree), a lower bound otherwise.+ * `primaryRows` are the merged rows BEFORE the registry splice. */export function applyRegistrySessionTotals(fetchedRows: unknown[],addedRows: unknown[],reportedTotals: Map<string, number>,- profileTotals: Record<string, number>+ profileTotals: Record<string, number>,+ primaryRows: unknown[] = []): number {+ const connectionOf = (row: unknown) =>+ row && typeof row === 'object' && typeof (row as { connection_id?: unknown }).connection_id === 'string'+ ? (row as { connection_id: string }).connection_id+ : ''+const counts = (rows: unknown[]) => {const perScope = new Map<string, number>()@@ -639,27 +653,42 @@ export function applyRegistrySessionTotals(}const session = row as Record<string, unknown>- const connection = typeof session.connection_id === 'string' ? session.connection_id : ''const profile = typeof session.profile === 'string' && session.profile ? session.profile : 'default'- const key = `${connection}\0${profile}`+ const key = `${connectionOf(row)}\0${profile}`perScope.set(key, (perScope.get(key) || 0) + 1)}return perScope}+ const primaryConnections = new Set(primaryRows.map(connectionOf).filter(Boolean))const fetched = counts(fetchedRows)const added = counts(addedRows)+ // Per-profile totals as the primary/override side left them, before this scan's splice.+ const primaryTotals: Record<string, number> = { ...profileTotals }++ for (const [key, n] of added) {+ const profile = key.slice(key.indexOf('\0') + 1)+ primaryTotals[profile] = (primaryTotals[profile] || 0) - n+ }+let contribution = 0for (const key of new Set([...fetched.keys(), ...reportedTotals.keys()])) {const addedCount = added.get(key) || 0const duplicates = (fetched.get(key) || 0) - addedCount- // A scope whose rows were already in `merged` overlaps the primary/override- // source, whose total already counted that whole scope. Only a scope that- // added every row it fetched is new, and only then is its full total owed.- const count = reportedTotals.has(key) && duplicates === 0 ? reportedTotals.get(key)! : addedCount+ const connection = key.slice(0, key.indexOf('\0'))const profile = key.slice(key.indexOf('\0') + 1)+ const primaryCount = Math.max(0, primaryTotals[profile] || 0)+ const overlaps = duplicates > 0 || primaryConnections.has(connection)+ let count = addedCount++ if (reportedTotals.has(key)) {+ const reported = reportedTotals.get(key)!+ // Never below the rows this scope visibly added.+ count = Math.max(addedCount, overlaps ? reported - primaryCount : reported)+ }+profileTotals[profile] = (profileTotals[profile] || 0) + count - addedCountcontribution += count}@@ -680,7 +709,13 @@ export async function mergeRegistrySessionRows(const before = merged.lengthspliceRegistrySessionRows(merged, registryRows, profileTotals)- return applyRegistrySessionTotals(registryRows, merged.slice(before), reportedTotals, profileTotals)+ return applyRegistrySessionTotals(+ registryRows,+ merged.slice(before),+ reportedTotals,+ profileTotals,+ merged.slice(0, before)+ )}/**