fix(desktop): honor optimistic session tombstones on list & tree refresh

A refresh racing an in-flight delete/archive resurrected the just-removed
row: refreshSessions repopulated $sessions straight from the backend page
ignoring tombstones, and the projects.tree prune dropped a tombstone the
moment its id left scoped_session_ids — so the grouped lane un-filtered it
too. The row only vanished on a later refresh once the RPC finally landed.

refreshSessions now filters tombstoned rows (and their lineage tip) before
merging. The prune keeps a tombstone while its mutation is still in flight
locally via $sessionMutationsInFlight, then hands it back to the normal
scoped-based prune once settled.
This commit is contained in:
Brooklyn Nicholson 2026-07-22 22:54:25 -05:00
parent 66747f154c
commit 4db2f78ec7
4 changed files with 126 additions and 3 deletions

View file

@ -62,9 +62,18 @@ vi.mock('@/hermes', async importOriginal => ({
listSidebarSessions: (...args: unknown[]) => listSidebarSessions(...args)
}))
// The refresh only reads the optimistic tombstone set; stub it so we don't pull
// the whole projects store (gateway / fs / git) into this hook's test.
const removed = vi.hoisted(() => ({ ids: new Set<string>() }))
vi.mock('@/store/projects', () => ({
$removedSessionIds: { get: () => removed.ids }
}))
beforeEach(() => {
listSidebarSessions.mockReset()
listAllProfileSessions.mockReset()
removed.ids = new Set()
setSessions([])
setCronSessions([])
setMessagingSessions([])
@ -146,6 +155,28 @@ describe('refreshSessions identity + loading hygiene', () => {
expect(loadingStates).toEqual([false])
})
it('drops rows the user just deleted, even when the backend page still lists them', async () => {
// A delete RPC is in flight: the row is tombstoned optimistically but the
// batched refresh still carries it (and a lineage-tip variant). Both must be
// filtered so the optimistic removal never flashes back.
removed.ids = new Set(['b', 'root-c'])
listSidebarSessions.mockResolvedValue(
sidebar({
sessions: [row('a'), row('b'), row('c', { _lineage_root_id: 'root-c' } as Partial<SessionInfo>)],
total: 3,
profile_totals: {}
})
)
const { result } = renderHook(() => useSessionListActions({ profileScope: 'default' }))
await act(async () => {
await result.current.refreshSessions()
})
expect($sessions.get().map(s => s.id)).toEqual(['a'])
})
it('still shows loading for the initial (empty-list) fetch', async () => {
listSidebarSessions.mockResolvedValue(sidebar({ sessions: [row('a')], total: 1, profile_totals: {} }))
const { result } = renderHook(() => useSessionListActions({ profileScope: 'default' }))

View file

@ -11,6 +11,7 @@ import {
import { setCronJobs } from '@/store/cron'
import { $pinnedSessionIds, $sessionsLimit, bumpSessionsLimit, SIDEBAR_SESSIONS_PAGE_SIZE } from '@/store/layout'
import { ALL_PROFILES, normalizeProfileKey } from '@/store/profile'
import { $removedSessionIds } from '@/store/projects'
import {
$messagingSessions,
$selectedStoredSessionId,
@ -180,12 +181,22 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg
if (refreshSessionsRequestRef.current === requestId) {
const recents = result.recents
// Drop rows the user just deleted/archived: a refresh can race an
// in-flight mutation and the backend page still carries the doomed row.
// Honoring the optimistic tombstone keeps the removal from flashing back
// (the tombstone self-clears once projects.tree confirms the delete).
const tombstones = $removedSessionIds.get()
const incoming = tombstones.size
? recents.sessions.filter(s => !tombstones.has(s.id) && !(s._lineage_root_id && tombstones.has(s._lineage_root_id)))
: recents.sessions
// Signature-gate the swap (same pattern as cron/messaging): a refresh
// that returns content-identical rows must keep the previous array
// identity, or every sidebar memo keyed on $sessions recomputes and the
// whole list re-renders once per turn/broadcast for nothing.
setSessions(prev => {
const next = mergeSessionPage(prev, recents.sessions, sessionsToKeep())
const next = mergeSessionPage(prev, incoming, sessionsToKeep())
return sameCronSignature(prev, next) ? prev : next
})