Skip to content

The agent that grows with you

fix(desktop): sort survivors by last_active in mergeSessionPage to prevent stale sidebar order

liuhao1024d0295754073a
diff --git a/apps/desktop/src/store/session.test.ts b/apps/desktop/src/store/session.test.ts
index fc2e8967f5f..be1ea4adbcc 100644
--- a/apps/desktop/src/store/session.test.ts
+++ b/apps/desktop/src/store/session.test.ts
@@ -267,6 +267,42 @@ describe('mergeSessionPage', () => {
expect(merged.map(s => s.id)).toEqual(['tip-5'])
expect(merged[0]?.last_active).toBe(9_000)
})
+
+ it('sorts survivors by last_active so they interleave with incoming instead of forming a stale block', () => {
+ // Repro of #47203: two survivors (B and C) have different last_active
+ // timestamps. B settled more recently than C. Without sorting, survivors
+ // are prepended in their old order from `previous`, which may be stale.
+ // With sorting, B (more recent) should appear before C.
+ const previous = [
+ session({ id: 'c', last_active: 100 }),
+ session({ id: 'b', last_active: 200 }),
+ session({ id: 'a', last_active: 300 }),
+ ]
+ // Server returns A (fresh page, order=recent), omits B and C (min_messages=1)
+ const incoming = [session({ id: 'a', last_active: 300, message_count: 2 })]
+
+ const merged = mergeSessionPage(previous, incoming, ['b', 'c'])
+
+ // B (last_active 200) should come before C (last_active 100)
+ expect(merged.map(s => s.id)).toEqual(['a', 'b', 'c'])
+ })
+
+ it('places a very recent survivor in correct position among incoming sessions', () => {
+ // A survivor with last_active between two incoming sessions should be
+ // interleaved, not prepended as a block.
+ const previous = [
+ session({ id: 'survivor', last_active: 150 }),
+ session({ id: 'old', last_active: 50 }),
+ ]
+ const incoming = [
+ session({ id: 'newest', last_active: 200 }),
+ session({ id: 'older', last_active: 100 }),
+ ]
+
+ const merged = mergeSessionPage(previous, incoming, ['survivor'])
+
+ // survivor (150) should be between newest (200) and older (100)
+ expect(merged.map(s => s.id)).toEqual(['newest', 'survivor', 'older'])
})
describe('touchSessionActivity', () => {
diff --git a/apps/desktop/src/store/session.ts b/apps/desktop/src/store/session.ts
index c64a194ef59..36c872cc44e 100644
--- a/apps/desktop/src/store/session.ts
+++ b/apps/desktop/src/store/session.ts
@@ -437,7 +437,43 @@ export function mergeSessionPage(
(keep.has(session.id) || (session._lineage_root_id != null && keep.has(session._lineage_root_id)))
)
- return survivors.length ? [...survivors, ...merged] : merged
+ if (!survivors.length) {
+ return merged
+ }
+
+ // Survivors carry their old relative positions from `previous`, which can be
+ // stale — the server page is the fresh `order=recent` truth. Sort survivors
+ // by the same effective-recency key the backend sorts by (last_active, then
+ // started_at, then id) and interleave them into the title-preserving merged
+ // rows so a retained session lands where recency puts it instead of the
+ // whole set forming a stale block at the top of the sidebar (fixes #47203).
+ const recency = (session: SessionInfo): number => session.last_active || session.started_at || 0
+
+ const bySessionRecency = (a: SessionInfo, b: SessionInfo): number =>
+ recency(b) - recency(a) || (b.started_at ?? 0) - (a.started_at ?? 0) || (a.id < b.id ? 1 : a.id > b.id ? -1 : 0)
+
+ const sortedSurvivors = [...survivors].sort(bySessionRecency)
+ const interleaved: SessionInfo[] = []
+ let survivorIndex = 0
+ let mergedIndex = 0
+
+ while (survivorIndex < sortedSurvivors.length && mergedIndex < merged.length) {
+ if (bySessionRecency(sortedSurvivors[survivorIndex], merged[mergedIndex]) <= 0) {
+ interleaved.push(sortedSurvivors[survivorIndex++])
+ } else {
+ interleaved.push(merged[mergedIndex++])
+ }
+ }
+
+ while (survivorIndex < sortedSurvivors.length) {
+ interleaved.push(sortedSurvivors[survivorIndex++])
+ }
+
+ while (mergedIndex < merged.length) {
+ interleaved.push(merged[mergedIndex++])
+ }
+
+ return interleaved
}
/** Raise a session in recents on user send (before stream / turn resolve). */