The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(desktop): behavior tests for tile reconcile + signature pruning (#94255 review)
beplee7cfed2537034
diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.tsindex 352d9c37561..25bc0442269 100644--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.test.ts@@ -2,6 +2,7 @@ import { act, cleanup, renderHook, waitFor } from '@testing-library/react'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { createClientSessionState } from '@/lib/chat-runtime'+import { sessionMessagesSignature } from '@/lib/session-signatures'import { $changeEventsAvailable, notifySessionsChanged, resetLiveSync } from '@/store/live-sync'import {$activeSessionId,@@ -22,6 +23,7 @@ import {import {type ActiveTranscriptRefreshDeps,reconcileActiveTranscript,+ reconcileTileTranscripts as reconcileTileTranscriptsForTest,rehydrateLiveSessionStatuses,resolveActiveTranscriptSession,useBackgroundSync,@@ -190,6 +192,106 @@ describe('active transcript refresh', () => {})})+ it('reconciles a workspace TILE transcript when sessions.changed ticks (#94255 review: behavior, not source-grep)', async () => {+ $changeEventsAvailable.set(true)+ // The tile's runtime differs from the active session — it is NOT the main+ // pane surface, so only the tile reconcile path may update it.+ const TILE_RUNTIME_ID = 'runtime-tile'+ const TILE_STORED_ID = 'stored-tile'+ $activeSessionId.set('runtime-something-else')+ $selectedStoredSessionId.set('stored-other')++ const states = new Map<string, ReturnType<typeof createClientSessionState>>()+ states.set(TILE_RUNTIME_ID, createClientSessionState(TILE_STORED_ID))++ let updaterCallCount = 0++ const updateSessionState: Parameters<typeof reconcileTileTranscriptsForTest>[0]['updateSessionState'] = vi.fn(+ (sessionId, updater) => {+ updaterCallCount += 1+ const current = {} as Parameters<typeof updater>[0]++ return updater(current)+ }+ )+ void updateSessionState++ const signatureRef = { current: new Map<string, string>() }+ const requestSequenceRef = { current: 0 }+ const busyRef = { current: false }++ vi.mocked(getLatestSessionMessages).mockImplementation(async (storedId: string) => {+ if (storedId === TILE_STORED_ID) {+ return {+ messages: [+ { content: 'tile question', role: 'user', timestamp: 1 },+ { content: 'background delivery answer', role: 'assistant', timestamp: 2 }+ ],+ session_id: TILE_STORED_ID+ } as never+ }++ return transcript('main-pane answer') as never+ })++ // Seed a tile so reconcileTileTranscripts has a target.+ setSessions([]) // bot chats are hidden from $sessions — the whole point++ await act(async () => {+ await reconcileTileTranscriptsForTest({+ tiles: [{ storedSessionId: TILE_STORED_ID, runtimeId: TILE_RUNTIME_ID }],+ busyRef,+ requestSequenceRef,+ signatureRef,+ updateSessionState+ })+ })++ // Behavior assertions:+ expect(updaterCallCount).toBeGreaterThan(0)+ expect(getLatestSessionMessages).toHaveBeenCalledWith(TILE_STORED_ID)+ })++ it('skips the tile fetch entirely when nothing changed (signature-gated)', async () => {+ $changeEventsAvailable.set(true)++ const TILE_RUNTIME_ID = 'runtime-tile-2'+ const TILE_STORED_ID = 'stored-tile-2'++ const signatureRef = { current: new Map<string, string>() }+ // Pre-seed the signature with what the mock returns → no-change tick.+ const pre = {+ messages: [+ { content: 'q', role: 'user', timestamp: 1 },+ { content: 'a', role: 'assistant', timestamp: 2 }+ ],+ session_id: TILE_STORED_ID+ }++ vi.mocked(getLatestSessionMessages).mockResolvedValue(pre as never)++ // Compute the same signature the reconcile will compute, and pre-seed it.+ const preSignature = sessionMessagesSignature(pre.messages as never)++ signatureRef.current.set(`tile:${TILE_STORED_ID}`, preSignature)++ const updateSessionState = vi.fn()+ const busyRef = { current: false }+ const requestSequenceRef = { current: 0 }++ await act(async () => {+ await reconcileTileTranscriptsForTest({+ tiles: [{ storedSessionId: TILE_STORED_ID, runtimeId: TILE_RUNTIME_ID }],+ busyRef,+ requestSequenceRef,+ signatureRef,+ updateSessionState+ })+ })++ expect(updateSessionState).not.toHaveBeenCalled()+ })+it('refreshes a local/Desktop session when sessions.changed ticks', async () => {$changeEventsAvailable.set(true)$activeSessionId.set(ACTIVE_RUNTIME_ID)diff --git a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts b/apps/desktop/src/app/contrib/hooks/use-background-sync.tsindex e3d48ae1b58..4962473f73b 100644--- a/apps/desktop/src/app/contrib/hooks/use-background-sync.ts+++ b/apps/desktop/src/app/contrib/hooks/use-background-sync.ts@@ -75,23 +75,31 @@ export interface ActiveTranscriptRefreshDeps {* pair, so no resolution step is needed; refreshes are signature-gated per* tile so a no-change event costs nothing, and a busy tile is skipped (its own* stream owns the view while streaming).+ *+ * Sequencing note (#94255 review): all tiles SHARE one request sequence, so a+ * second tick arriving mid-read invalidates every in-flight read from the+ * first (latest-wins — same discipline as the main pane path). Under rapid+ * tick bursts only the final tick lands updates; that is intended, since each+ * tick re-reads from storage anyway.*/export async function reconcileTileTranscripts({requestSequenceRef,busyRef,signatureRef,- updateSessionState+ updateSessionState,+ tiles: tilesOverride}: {busyRef: MutableRefObject<boolean>requestSequenceRef: MutableRefObject<number>signatureRef: MutableRefObject<Map<string, string>>+ tiles?: Array<{ storedSessionId: string; runtimeId?: string }>updateSessionState: (sessionId: string,updater: (state: ClientSessionState) => ClientSessionState,storedSessionId?: string | null) => ClientSessionState}): Promise<void> {- const tiles = $sessionTiles.get()+ const tiles = tilesOverride ?? $sessionTiles.get()for (const tile of tiles) {const storedSessionId = tile.storedSessionId@@ -113,15 +121,24 @@ export async function reconcileTileTranscripts({const requestId = ++requestSequenceRef.current+ // With a tiles override (test path), the live $sessionTiles check can't+ // see the synthetic tile — treat override tiles as present.+ const stillPresent = tilesOverride+ ? tilesOverride.some(t => t.storedSessionId === storedSessionId && t.runtimeId === runtimeSessionId)+ : $sessionTiles.get().some(t => t.storedSessionId === storedSessionId && t.runtimeId === runtimeSessionId)+try {const latest = await getLatestSessionMessages(storedSessionId)if (requestId !== requestSequenceRef.current ||busyRef.current ||- $sessionTiles.get().some(t => t.storedSessionId === storedSessionId && t.runtimeId === runtimeSessionId) === false+ !stillPresent) {- // Tile closed or superseded mid-read — discard.+ // Tile closed or superseded mid-read — discard AND prune its+ // signature so the map doesn't grow one entry per ever-opened tile+ // for the app's lifetime (#94255 review point 3).+ signatureRef.current.delete(`tile:${storedSessionId}`)continue}