Skip to content

The agent that grows with you

fix(desktop): keep live pins out of the Archived cursor on the merge path

Tekniumc22fc8f84629
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index afb2ef865e3..8ae84272fe7 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -16267,7 +16267,9 @@ async function mergeRemoteProfileSessions(searchParams, remoteProfiles, registry
const recency = s => s?.[order] ?? s?.started_at ?? 0
merged.sort((a, b) => recency(b) - recency(a))
- return composeUnifiedSessionResponse(base, merged, offset, limit, total, profileTotals, remoteErrors)
+ return composeUnifiedSessionResponse(base, merged, offset, limit, total, profileTotals, remoteErrors, {
+ archivedOnly: searchParams.get('archived') === 'only'
+ })
}
// Every CONNECTED registry gateway as a session source: resolved descriptors
diff --git a/apps/desktop/electron/profile-session-routing.test.ts b/apps/desktop/electron/profile-session-routing.test.ts
index 874b569903f..27357b160d7 100644
--- a/apps/desktop/electron/profile-session-routing.test.ts
+++ b/apps/desktop/electron/profile-session-routing.test.ts
@@ -196,6 +196,7 @@ test('a primary aggregate supplies the full prefix before Desktop applies a late
const rows = Array.from({ length: 650 }, (_, index) => ({
id: `s${index}`,
profile: 'default',
+ archived: true,
last_active: 1000 - index
}))
@@ -586,7 +587,9 @@ test('archived merged pages reach every archived row from older and current back
)
const merged = [...base.sessions].sort((a: any, b: any) => b.last_active - a.last_active)
- const response = composeUnifiedSessionResponse(base, merged, offset, 200, base.total, base.profile_totals, [])
+ const response = composeUnifiedSessionResponse(base, merged, offset, 200, base.total, base.profile_totals, [], {
+ archivedOnly: true
+ })
for (const row of response.sessions as typeof archived) {
if (row.archived) {
@@ -599,6 +602,78 @@ test('archived merged pages reach every archived row from older and current back
}
})
+test('a full page of newer live pins does not hide an older archived row from the Archived loader', async () => {
+ // Greptile/@dokterdok on #140972: run the renderer loader's own stop rule
+ // (sidebar-archive.ts listEveryArchivedSession) over the merge path, with
+ // enough live pins to fill a page, on both backend orderings.
+ const archived = [{ id: 'old', profile: 'default', archived: true, pinned: false, last_active: 1 }]
+
+ const pins = Array.from({ length: 200 }, (_, index) => ({
+ id: `p${index}`,
+ profile: 'default',
+ archived: false,
+ pinned: true,
+ last_active: 1000 - index
+ }))
+
+ const backends = {
+ older: (offset: number, limit: number) => {
+ const rows = [...pins, ...archived]
+
+ return [...rows.slice(offset, offset + limit), ...rows.slice(offset + limit).filter(row => row.pinned)]
+ },
+ current: (offset: number, limit: number) => [...archived.slice(offset, offset + limit), ...pins]
+ }
+
+ for (const [name, page] of Object.entries(backends)) {
+ const seen = new Set<string>()
+ let archivedCount = 0
+
+ for (let offset = 0; offset < 2000; offset += 200) {
+ const params = new URLSearchParams({ profile: 'all', limit: '200', offset: String(offset), archived: 'only' })
+
+ const base = await fetchPrimaryProfileSessions(
+ params,
+ async (_profile, path) => {
+ const query = new URL(path, 'http://x').searchParams
+
+ return {
+ sessions: page(Number(query.get('offset')), Number(query.get('limit'))),
+ total: archived.length,
+ profile_totals: { default: archived.length }
+ }
+ },
+ { prefixForMerge: true }
+ )
+
+ const merged = [...base.sessions].sort((a: any, b: any) => b.last_active - a.last_active)
+
+ const response = composeUnifiedSessionResponse(base, merged, offset, 200, base.total, base.profile_totals, [], {
+ archivedOnly: true
+ })
+
+ const before = archivedCount
+
+ for (const row of response.sessions as typeof archived) {
+ if (!seen.has(row.id)) {
+ seen.add(row.id)
+ archivedCount += row.archived ? 1 : 0
+ }
+ }
+
+ if (
+ response.sessions.length === 0 ||
+ archivedCount >= response.total ||
+ (offset + 200 >= response.total && archivedCount === before)
+ ) {
+ break
+ }
+ }
+
+ assert.ok(seen.has('old'), name)
+ }
+})
+
test('remote session reads keep small requests on one call', async () => {
const calls: Array<{ profile: string | null; path: string }> = []
const expected = { sessions: [{ id: 'session-1' }], total: 1, limit: 20, offset: 0 }
diff --git a/apps/desktop/electron/profile-session-routing.ts b/apps/desktop/electron/profile-session-routing.ts
index 93a556fca23..5e0b94a0fb6 100644
--- a/apps/desktop/electron/profile-session-routing.ts
+++ b/apps/desktop/electron/profile-session-routing.ts
@@ -128,6 +128,10 @@ function isPinned(row: unknown): boolean {
return Boolean(row && typeof row === 'object' && 'pinned' in row && row.pinned)
}
+function isArchived(row: unknown): boolean {
+ return Boolean(row && typeof row === 'object' && 'archived' in row && row.archived)
+}
+
function profileSessionId(row: unknown): string | null {
const id = sessionId(row)
@@ -1016,13 +1020,33 @@ export function applyRemoteProfileSessionOutcomes(
return { total, errors }
}
+function archivedOnlyWindow(merged: unknown[], offset: number, limit: number): unknown[] {
+ const window = mergeProfileSessionWindow(merged.filter(isArchived), offset, limit)
+ const ids = new Set(window.map(profileSessionId).filter((id): id is string => id !== null))
+
+ for (const row of merged) {
+ const id = profileSessionId(row)
+
+ if (!isArchived(row) && isPinned(row) && !(id && ids.has(id))) {
+ if (id) {
+ ids.add(id)
+ }
+
+ window.push(row)
+ }
+ }
+
+ return window
+}
+
/** 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. The window counts every merged row, live pins included, even
- * on an archived-only page: an older backend's page lets a live pin take an
- * archived position, so a window over archived rows alone would skip the
- * archived rows that pin pushed past this source's prefix. */
+ * identifiable. On an archived-only page the cursor counts archived rows
+ * only, as `total` does, and live pins ride after the window: a window over
+ * every merged row lets a page of newer pins stop the Archived loader before
+ * an older archived row. The primary prefix reads enough archived rows for
+ * that even from an older backend whose pins took cursor positions. */
export function composeUnifiedSessionResponse(
base: unknown,
merged: unknown[],
@@ -1030,7 +1054,8 @@ export function composeUnifiedSessionResponse(
limit: number,
total: number,
profileTotals: Record<string, number>,
- extraErrors: Array<{ profile: string; error: string }>
+ extraErrors: Array<{ profile: string; error: string }>,
+ options: { archivedOnly?: boolean } = {}
) {
const baseErrors = Array.isArray((base as { errors?: unknown })?.errors)
? (base as { errors: Array<{ profile: string; error: string }> }).errors
@@ -1040,7 +1065,7 @@ export function composeUnifiedSessionResponse(
return {
...(base as Record<string, unknown>),
- sessions: mergeProfileSessionWindow(merged, offset, limit),
+ sessions: options.archivedOnly ? archivedOnlyWindow(merged, offset, limit) : mergeProfileSessionWindow(merged, offset, limit),
total,
profile_totals: profileTotals,
limit,
@@ -1135,10 +1160,16 @@ async function readProfileSessionRows(url: URL, profile: string, want: number, f
const seen = new Set<string>()
const errors: SessionScanError[] = []
const storage: Record<string, unknown> = {}
+ // An archived-only prefix needs `want` ARCHIVED rows. An older backend lets
+ // live pins take cursor positions, so read on until enough archived rows
+ // arrived, the backend runs out, or a page brings no new row.
+ const archivedOnly = url.searchParams.get('archived') === 'only'
+ let archivedRows = 0
for (let offset = 0; ; ) {
const params = new URLSearchParams(url.searchParams)
- const limit = Math.min(PROFILE_SESSION_PAGE_CAP, Math.max(0, want - offset))
+ const remaining = archivedOnly ? want - archivedRows : want - offset
+ const limit = Math.min(PROFILE_SESSION_PAGE_CAP, Math.max(0, remaining))
params.set('limit', String(limit))
params.set('offset', String(offset))
params.set('profile', profile)
@@ -1157,6 +1188,8 @@ async function readProfileSessionRows(url: URL, profile: string, want: number, f
Object.assign(storage, page.storage)
}
+ let newRows = 0
+
for (const row of pageRows) {
const id = profileSessionId(row)
@@ -1165,6 +1198,8 @@ async function readProfileSessionRows(url: URL, profile: string, want: number, f
}
rows.push(row)
+ newRows += 1
+ archivedRows += isArchived(row) ? 1 : 0
if (id) {
seen.add(id)
@@ -1173,7 +1208,13 @@ async function readProfileSessionRows(url: URL, profile: string, want: number, f
offset += limit
- if (offset >= want || pageRows.length < limit) {
+ const total = nonNegativeNumber(page?.total)
+
+ const done = archivedOnly
+ ? newRows === 0 || archivedRows >= Math.min(want, total ?? want)
+ : offset >= want || pageRows.length < limit
+
+ if (done) {
return { ...first, sessions: rows, errors, ...(first?.storage || Object.keys(storage).length ? { storage } : {}) }
}
}