Skip to content

The agent that grows with you

fix(desktop): count an overlapping registry source's sessions once

Teknium60248fb5e784
diff --git a/apps/desktop/electron/profile-session-routing.test.ts b/apps/desktop/electron/profile-session-routing.test.ts
index 244a9586ea3..b2fe55241e5 100644
--- a/apps/desktop/electron/profile-session-routing.test.ts
+++ b/apps/desktop/electron/profile-session-routing.test.ts
@@ -457,6 +457,35 @@ test('registry paging uses the backend total instead of its fetched prefix or pi
assert.equal(profileTotals.work, 650)
})
+test('a registry source that overlaps the primary base is counted once, not twice', async () => {
+ // Remote registry primary R is both `base` and a pooled registry source:
+ // base already counted all 650, and every fetched registry row is a duplicate.
+ const rows = Array.from({ length: 650 }, (_, index) => ({ id: `s${index}`, profile: 'default', started_at: 650 - index }))
+ const reported = new Map<string, number>()
+
+ const fetched = await fetchRegistrySessionRows(
+ [{ 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 } }
+ },
+ reported
+ )
+
+ const merged: unknown[] = fetched.map(row => ({ ...(row as object) }))
+ const profileTotals: Record<string, number> = { default: 650 }
+ const before = merged.length
+ const { added } = spliceRegistrySessionRows(merged, fetched, profileTotals)
+
+ assert.equal(added, 0)
+ assert.equal(650 + applyRegistrySessionTotals(fetched, merged.slice(before), reported, profileTotals), 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.ts
index 5368492bde4..bc14dcb3a6b 100644
--- a/apps/desktop/electron/profile-session-routing.ts
+++ b/apps/desktop/electron/profile-session-routing.ts
@@ -642,7 +642,7 @@ export function spliceRegistrySessionRows(
return { added }
}
-/** Report full registry totals, minus rows already supplied by the primary/override path.
+/** Report full 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. */
export function applyRegistrySessionTotals(
@@ -676,7 +676,10 @@ export function applyRegistrySessionTotals(
for (const key of new Set([...fetched.keys(), ...reportedTotals.keys()])) {
const addedCount = added.get(key) || 0
const duplicates = (fetched.get(key) || 0) - addedCount
- const count = reportedTotals.has(key) ? Math.max(0, reportedTotals.get(key)! - duplicates) : 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 profile = key.slice(key.indexOf('\0') + 1)
profileTotals[profile] = (profileTotals[profile] || 0) + count - addedCount
contribution += count