From bc47ebaa11a7e3f63fa008f4812350c7a3f42a7f Mon Sep 17 00:00:00 2001 From: Yusheng Xu Date: Thu, 1 Oct 2026 20:26:36 +0000 Subject: [PATCH] perf(chat): bound the background warm with the same window walk warmSlotCache read a background pane's whole transcript whenever the pane held more than one page (the count-matched limit declines past the handler's 500-row clamp) and whenever the bounded window missed a cached row. It runs on every off-screen chat_done and on every reconnect, so a long session re-downloaded its full history on each background turn end. A cache past the clamp now asks for one clamp-sized page, and a coverage miss is closed with walkWindowBackTo, as refreshSlot and switchSlot do. A miss the walk cannot close (a cached row newer than the window's start: a rewind, a rewrite, or a stale response) is settled with one fresh read of the same bounded page. A walk that spends its page cap without anchoring replaces the cache instead of being spliced onto it, so no hole is published. Every request on the path is bounded, so the retained total is always the collapsed count. The identity guards (no identified row, an unplaceable row) keep the unbounded shape. Co-authored-by: Billy Gerhard --- website/src/store/chat/slotRefresh.ts | 138 +++++++---- website/src/store/chat/windowWalk.ts | 6 +- .../chatSlice.boundedRefetchShrink.test.ts | 6 +- .../src/store/chatSlice.windowWalk.test.ts | 229 +++++++++++++++++- .../test/chatSlice.warmSlotCacheBound.test.ts | 62 +++-- 5 files changed, 352 insertions(+), 89 deletions(-) diff --git a/website/src/store/chat/slotRefresh.ts b/website/src/store/chat/slotRefresh.ts index 22b963ddc65c..0155f960d1c5 100644 --- a/website/src/store/chat/slotRefresh.ts +++ b/website/src/store/chat/slotRefresh.ts @@ -2,7 +2,7 @@ * the open pane (reconnect, turn end, variant switch) and `warmSlotCache` for * a background pane, each with the reducer that merges its count-matched page * onto what the tab already holds. */ -import { createAsyncThunk, type ActionReducerMapBuilder } from '@reduxjs/toolkit' +import { createAsyncThunk, isDraft, original, type ActionReducerMapBuilder } from '@reduxjs/toolkit' import type { ChatMessage } from '../../types' import { mergePreservedPastes } from '../../utils/pasteTokens' import type { ChatState } from './state' @@ -214,16 +214,27 @@ export const warmSlotCache = createAsyncThunk( * page it got against the view and retries, so an unplaceable row costs it a round * trip rather than a row. */ const unplaceable = cache.some(m => isDurableRow(m) && transcriptTsMs(m.ts) === null) + /* The count-matched rule with no ceiling, so a cache held PAST the handler's + * clamp is told apart from one the rule declines for want of identity. The + * capped rule answers `undefined` for both; only the second needs the + * unbounded shape. A cache past the clamp asks for exactly one clamp-sized + * page and reaches the rest with `walkWindowBackTo` below, the way + * `refreshSlot` and `switchSlot` do -- before this, every background turn end + * and every reconnect re-read the whole transcript of any pane holding more + * than one page. */ + const uncapped = cache.length === 0 || unplaceable + ? undefined + : countMatchedFetchLimit({ + rows: cache, + floor: PANE_HYDRATE_LIMIT, + ceiling: Number.POSITIVE_INFINITY, + span: 'placeable', + }) const matchedLimit = cache.length === 0 ? PANE_HYDRATE_LIMIT - : unplaceable - ? undefined - : countMatchedFetchLimit({ - rows: cache, - floor: PANE_HYDRATE_LIMIT, - ceiling: SLOT_DETAIL_MAX_LIMIT, - span: 'placeable', - }) + : uncapped !== undefined && uncapped > SLOT_DETAIL_MAX_LIMIT + ? SLOT_DETAIL_MAX_LIMIT + : uncapped const limit = running && cache.length > 0 && matchedLimit !== undefined ? Math.min(SLOT_DETAIL_MAX_LIMIT, matchedLimit + 1) : matchedLimit @@ -233,22 +244,54 @@ export const warmSlotCache = createAsyncThunk( * extends BACKWARD from the newest row, so a pane parked on a head it paged into * holds rows a newest-N window never reaches however exactly that window is sized * to the cache's count, and this reducer replaces rather than merges when nothing - * anchors. A bare count cannot distinguish a true truncation from a bounded - * snapshot that predates a concurrent sibling, and bounded and unbounded totals - * do not even count the same corpus while streaming. Always close an observed - * coverage hole, then let the reducer's ordered comparable-total check decide - * whether rows were actually removed. */ + * anchors. A cache past the clamp always reports a shortfall here: its rows + * above the newest page are outside it by construction. + * + * An observed hole is closed by extending the window OLDER until its oldest row + * anchors a cached row (`walkWindowBackTo`); the reducer then keeps the cache + * above that anchor (`olderHeadAbovePage`). Every request on this path is + * bounded, so the totals all count the same collapsed corpus and the walk's + * own `total` is the comparable one. */ if (limit !== undefined && slotCoverageShortfall({ cached: cache, window: first.messages }) > 0) { - const wide = await fetchSlotDetail(key) - /* Carry the bounded read's total unconditionally, exactly as `switchSlot` does - * after its own coverage retry. The unbounded handler counts the RAW window -- - * every per-turn `done` row included, rows the bounded path collapses away -- - * whether or not the slot is running, so `wide.total` is never in the same - * units as the bounded counts the reducer compares it against. Storing it as - * the baseline makes the next bounded warm's smaller collapsed count read as a - * server shrink, which discards the mid-turn streaming row the page cannot - * vouch for and restarts the in-flight reply mid-sentence. */ - return { ...wide, comparableTotal: first.total, warmSeq, runTickAtDispatch } + const walked = await walkWindowBackTo(key, first, cache) + /* A hole the walk cannot close: the window anchors the cache, yet the cache + * holds a row at or after that anchor which the window lacks. Walking older + * cannot reach a row that is NEWER than the window's start -- either a + * rewind or rewrite removed it, or this response predates a sibling's. The + * unbounded retry this replaces settled that with a fresh read; one fresh + * read of the same bounded page settles it the same way, and the reducer's + * ordered comparable-total check then decides whether rows were removed. + * Only when the walk took no page (its rows are the first page's own array): + * a walk that did ends on its own re-read of the newest page. */ + const paged = walked.messages !== first.messages + if (!paged) { + const prior = cache.filter(m => m.role !== 'thinking') + const { cutIdx } = olderHeadAbovePage(prior, first.messages) + if (cutIdx >= 0 && slotCoverageShortfall({ cached: prior.slice(cutIdx), window: first.messages }) > 0) { + const fresh = await fetchSlotDetail(key, limit) + return { ...fresh, warmSeq, runTickAtDispatch } + } + } + /* A walk that paged older yet still does not anchor the cache spent its page + * cap. Its window is disjoint from the cache, and the + * reducer's disjoint branches would splice the two -- a transcript with the + * rows between them silently missing. The reducer is told to replace + * instead: the rows above the window leave this pane, one page-back away, + * the cost `walkWindowBackTo` documents for the other two readers. */ + const priorRows = cache.filter(m => m.role !== 'thinking') + /* The walk also stops on a SUPERSET -- a window holding the cache's oldest + * row -- whose own oldest row is older than the cache, so `cutIdx` misses it + * too. That window covers the cache, so it is not disjoint, and the reducer's + * rescue of rows newer than the page (a just-sent row, a client streaming + * copy) must still run. Same anchor set and test as the walk itself. */ + const anchorRows = priorRows.filter(m => m.role !== 'permission') + const coversCache = !walked.hasMore || idAnchorsOneRow( + anchorRows[0]?.meta?.mid, anchorRows, walked.messages, + midOccurrences(anchorRows), midOccurrences(walked.messages)) + const walkUnanchored = paged && !coversCache + && olderHeadAbovePage(priorRows, walked.messages).cutIdx < 0 + return { ...walked, warmSeq, runTickAtDispatch, + ...(walkUnanchored ? { walkUnanchored: true, cacheAtDispatch: cache } : {}) } } return { ...first, warmSeq, runTickAtDispatch } }, @@ -383,6 +426,8 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder) .addCase(warmSlotCache.fulfilled, (state, action) => { if (!action.payload) return const { key, messages, queue, hasMore, total, running, warmSeq } = action.payload + // Set when the thunk's window walk paged older without anchoring the cache. + const walkUnanchored = (action.payload as { walkUnanchored?: boolean }).walkUnanchored === true if (isUnsafeKey(key)) return // Slot became active between dispatch and fulfilment — switchSlot now // owns its messages, so leave the cache for it to manage. @@ -456,18 +501,11 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder) const priorSeq = state.slotServerTotalSeq?.[safeKey(key)] const staleTotal = typeof warmSeq === 'number' && typeof priorSeq === 'number' && warmSeq < priorSeq - // The payload's own `total` counts the RAW window: a coverage retry answers - // with the unbounded read, whose count includes every per-turn `done` row - // and every unfolded chunk run, running or idle. The retained baseline is - // the settled collapsed count, so a comparison against it must use the - // collapsed count the retry carries (`comparableTotal`). ONE value, read - // at every comparison site below and at the retain call: a raw count at - // any one of them reads a rewind as growth and a same-count rewrite as a - // newer row, restoring discarded rows and rendering a reply twice. - const comparable = (action.payload as { comparableTotal?: number }).comparableTotal - const cmpTotal = comparable ?? total - const serverShrank = typeof priorTotal === 'number' && typeof cmpTotal === 'number' - && cmpTotal < priorTotal && !staleTotal + // Bounded pages and their coverage walks report the same collapsed corpus + // count. Compare and retain the payload's total directly; boundedRead still + // governs whether a running snapshot can establish a retained baseline. + const serverShrank = typeof priorTotal === 'number' && typeof total === 'number' + && total < priorTotal && !staleTotal const anchorIds = anchorIdx >= 0 ? rowIdentities(prior[anchorIdx]) : [] const warmAnchorIdx = warmed.findIndex(m => rowIdentities(m).some(id => anchorIds.includes(id))) // A `streaming` row is minted client-side by the first chunk and carries @@ -508,20 +546,22 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder) const tail = prior.slice(anchorIdx + 1) const nextTurnAt = tail.findIndex(m => m.role === 'user' || m.role === 'inject') const beforeNextTurn = new Set(tail.slice(0, nextTurnAt >= 0 ? nextTurnAt : tail.length)) - const rescuable = anchorIdx >= 0 && !serverShrank + const rescuable = anchorIdx >= 0 && !serverShrank && !walkUnanchored ? tailNotInPage(tail, warmed).filter(m => !(beforeNextTurn.has(m) && supersededByPage(m))) : [] // A rewrite REPLACES a reply, so the count holds while the post-anchor rows // differ. Equal tail LENGTH is what separates that from a real newer row. const sameCountRewrite = rescuable.length > 0 && warmAnchorIdx >= 0 && !staleTotal - && typeof priorTotal === 'number' && typeof cmpTotal === 'number' && cmpTotal === priorTotal + && typeof priorTotal === 'number' && typeof total === 'number' && total === priorTotal && prior.length - anchorIdx === warmed.length - warmAnchorIdx const newerTail = sameCountRewrite ? [] : rescuable // A confirmed shrink means those rows were REMOVED, so the disjoint branches // below would restore them. It sits after the head: `cutIdx > 0` vs `< 0`. + // An unanchored walk replaces too: either disjoint branch would publish the + // gap between cache and walked window as contiguous history. const base = olderHead.length ? [...olderHead, ...warmed] - : serverShrank + : serverShrank || walkUnanchored ? warmed : priorEndsBeforePage ? [...prior, ...tailNotInPage(warmed, prior)] @@ -557,15 +597,17 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder) const pageRows = warmed.filter(m => m.role !== 'queued') const boundaryIdx = pageRows.length ? revived.indexOf(pageRows[pageRows.length - 1]) : -1 const boundedLen = boundaryIdx >= 0 ? boundaryIdx + 1 : pageRows.length - writeSlotPage(state, key, revived, warmIsPrefix ? hasMore : undefined, - warmIsPrefix && hasMore ? boundedLen : undefined) - // The baseline retained for the next warm is the same collapsed count the - // comparisons above read (`cmpTotal`), never the raw wide count: the raw - // one is not comparable with the collapsed counts later pages report. - retainServerTotal( - state, key, cmpTotal, running, warmSeq, - comparable !== undefined || action.payload.boundedRead, - ) + // An unanchored walk may replace only the cache it read. Immer changes the + // array identity for appends, nested row edits, and replacement pages alike. + // A changed cache keeps its rows AND paging markers until the next warm. + const cacheAtFulfil = state.slotMessages[safeKey(key)] + const cacheUnchanged = (isDraft(cacheAtFulfil) ? original(cacheAtFulfil) : cacheAtFulfil) + === action.payload.cacheAtDispatch + if (!walkUnanchored || cacheUnchanged) { + writeSlotPage(state, key, revived, warmIsPrefix ? hasMore : undefined, + warmIsPrefix && hasMore ? boundedLen : undefined) + } + retainServerTotal(state, key, total, running, warmSeq, action.payload.boundedRead) // The run-state write is ORDERED against the live frame writers by the // entry's receipt tick (`ChatState.slotRun`). The warm is a // point-in-time snapshot, and the frame writers (chunk -> streaming, diff --git a/website/src/store/chat/windowWalk.ts b/website/src/store/chat/windowWalk.ts index e745c44025f9..2559b9a3f4c7 100644 --- a/website/src/store/chat/windowWalk.ts +++ b/website/src/store/chat/windowWalk.ts @@ -1,7 +1,7 @@ /** Extending a most-recent slot-detail window OLDER, one bounded page at a time, - * until it reaches the rows a tab already holds. `refreshSlot` and `switchSlot` - * take this instead of re-reading the whole chained transcript when their - * window misses what the tab holds. */ + * until it reaches the rows a tab already holds. `refreshSlot`, `switchSlot` and + * `warmSlotCache` take this instead of re-reading the whole chained transcript + * when their window misses what the tab holds. */ import { api } from '../../api/client' import type { ChatMessage } from '../../types' import { devLog, inspectorOn } from '../../dev/scrollInspector' diff --git a/website/src/store/chatSlice.boundedRefetchShrink.test.ts b/website/src/store/chatSlice.boundedRefetchShrink.test.ts index b871099795e8..f91d61b57f57 100644 --- a/website/src/store/chatSlice.boundedRefetchShrink.test.ts +++ b/website/src/store/chatSlice.boundedRefetchShrink.test.ts @@ -154,8 +154,10 @@ describe('a bounded refetch must not shrink what is already loaded', () => { expect(store.getState().chat.slotMessages.bg.length).toBe(111) await store.dispatch(warmSlotCache('bg')) - // The newest 111 rows miss this oldest-111 cache, so coverage widens once. - expect(limitsFor('bg')).toEqual([111, undefined]) + // The newest 111 rows miss this oldest-111 cache, so the window walks older + // one clamp-sized page (which reaches the start here), then re-reads its + // newest edge -- every request bounded. + expect(limitsFor('bg')).toEqual([111, 500, 500]) expect(store.getState().chat.slotMessages.bg.length).toBeGreaterThanOrEqual(111) }) diff --git a/website/src/store/chatSlice.windowWalk.test.ts b/website/src/store/chatSlice.windowWalk.test.ts index 70a4515a4bdf..215b53eff69a 100644 --- a/website/src/store/chatSlice.windowWalk.test.ts +++ b/website/src/store/chatSlice.windowWalk.test.ts @@ -68,10 +68,13 @@ vi.mock('../api/client', () => ({ import chatReducer, { PANE_HYDRATE_LIMIT, WINDOW_WALK_MAX_PAGES, + appendSlotMessage, hydrateSlotMessages, refreshSlot, setActiveSlot, + sseChatMessage, switchSlot, + warmSlotCache, } from './chatSlice' import { api } from '../api/client' @@ -82,7 +85,7 @@ function makeStore(extra: Record = {}) { return configureStore({ reducer: { chat: chatReducer }, preloadedState: { chat: { ...base, activeSlot: SLOT, ...extra } }, - middleware: (getDefault) => getDefault({ serializableCheck: false, immutableCheck: false }), + // Match the production store, including the default development checks. }) } @@ -311,4 +314,228 @@ describe('walkWindowBackTo', () => { expect(after.slotHasMore).toBe(false) }) }) + + describe('warmSlotCache', () => { + /** A background pane holding rows `[from, from + n)`, as a switch away caches it. */ + function backgroundPane(n: number, from: number, total: number) { + const store = makeStore({ activeSlot: 'other' }) + store.dispatch(setActiveSlot('other')) + store.dispatch(hydrateSlotMessages({ + slot: SLOT, messages: rows(n, from), hasMore: from > 0, + bounded: true, total, running: false, + })) + return store + } + const cached = (store: ReturnType) => + (store.getState().chat.slotMessages?.[SLOT] ?? []).map(m => m.content) + + it('warms a pane past the clamp with one bounded page, not the whole transcript', async () => { + // The reported case: a background pane holds the newest 8,000 rows of an + // 8,000-row session and its turn ends with two new rows. One clamp-sized + // page overlaps the cache, so one request does it -- the cache is too wide + // for a count-matched limit, which used to mean an unbounded read. + HISTORY = rows(8000) + const store = backgroundPane(8000, 0, 8000) + HISTORY = rows(8002) + + await store.dispatch(warmSlotCache(SLOT) as never) + + expect(requests()).toEqual([[SERVER_CLAMP, undefined]]) + const contents = cached(store) + // The held head above the page survived, and the new rows landed. + expect(contents).toHaveLength(8002) + expect(contents[0]).toBe('m0') + expect(contents.at(-1)).toBe('m8001') + expect(new Set(contents).size).toBe(contents.length) + }) + + it('walks a coverage gap older, bounded, and keeps the held head where it anchors', async () => { + // The pane holds rows 0..599 and the server gained 1,400 while it was off + // screen. The newest page (1500..1999) misses the cache; the walk reads + // 1000..1499 and 500..999, which anchors cache row 500. Stop there. + HISTORY = rows(600) + const store = backgroundPane(600, 0, 600) + HISTORY = rows(2000) + + await store.dispatch(warmSlotCache(SLOT) as never) + + expect(requests()).toEqual([ + [SERVER_CLAMP, undefined], + [SERVER_CLAMP, 1500], + [SERVER_CLAMP, 1000], + [SERVER_CLAMP, undefined], + ]) + const contents = cached(store) + expect(contents).toHaveLength(2000) + // Rows 0..499 sat above the walked window: the reducer kept them. + expect(contents[0]).toBe('m0') + expect(contents).toContain('m599') + expect(contents.at(-1)).toBe('m1999') + expect(new Set(contents).size).toBe(contents.length) + }) + + it('keeps a just-sent row when the walk stops on a window that contains the whole cache', async () => { + // The pane holds rows 1100..1149 plus a send the server has not persisted yet, + // and the server is now 2,000 rows deep. The walk's second older page + // (949..1448) contains the cache's oldest row, so it stops on a SUPERSET whose + // own oldest row is older than the cache. That window covers the cache: the + // just-sent row must survive as newer than the page, not be dropped as if the + // walk had run out of pages. + HISTORY = rows(1150) + const store = backgroundPane(50, 1100, 1150) + store.dispatch(appendSlotMessage({ + slot: SLOT, + message: { role: 'user', content: 'just sent', cls: 'msg msg-u', ts: new Date(Date.UTC(2026, 0, 2)).toISOString(), meta: { sendId: 's-1' } } as never, + })) + HISTORY = rows(2000) + + await store.dispatch(warmSlotCache(SLOT) as never) + + expect(limits()).not.toContain(undefined) + const contents = cached(store) + expect(contents).toContain('m1100') + expect(contents).toContain('m1999') + expect(contents.at(-1)).toBe('just sent') + }) + + it.each([50, 5000])('keeps the cache when a user row is appended during an unanchored warm of %i rows', async (held) => { + HISTORY = rows(held) + const store = backgroundPane(held, 0, held) + const before = store.getState().chat + let live = before + HISTORY = rows(20_000) + ON_OLDER = () => { + store.dispatch(appendSlotMessage({ + slot: SLOT, + message: { role: 'user', content: 'sent during walk', cls: 'msg msg-u', meta: { sendId: 'during-walk' } }, + })) + live = store.getState().chat + } + + const payload = await store.dispatch(warmSlotCache(SLOT)).unwrap() + + expect(payload?.walkUnanchored).toBe(true) + expect(payload?.cacheAtDispatch).toBe(before.slotMessages[SLOT]) + expect(limits()).toHaveLength(1 + WINDOW_WALK_MAX_PAGES + 1) + expect(limits()).not.toContain(undefined) + // An optimistic send changes the cache without changing the run tick. + expect(live.slotRun[SLOT]?.tick).toBe(before.slotRun[SLOT]?.tick) + expect(live.slotMessages[SLOT]).not.toBe(before.slotMessages[SLOT]) + const after = store.getState().chat + expect(after.slotMessages[SLOT]).toBe(live.slotMessages[SLOT]) + expect(cached(store)).toEqual([...rows(held).map(m => m.content), 'sent during walk']) + expect(after.slotPaneHasMore?.[SLOT]).toBe(live.slotPaneHasMore?.[SLOT]) + expect(after.slotPaneBounded?.[SLOT]).toBe(live.slotPaneBounded?.[SLOT]) + expect(after.slotServerTotal?.[SLOT]).toBe(20_000) + }) + + it('keeps an in-place streaming chunk received during an unanchored warm', async () => { + HISTORY = rows(50) + const store = backgroundPane(50, 0, 50) + store.dispatch(sseChatMessage({ slot: SLOT, role: 'chunk', content: 'first', seq: 1 })) + const before = store.getState().chat + let live = before + HISTORY = rows(20_000) + ON_OLDER = () => { + store.dispatch(sseChatMessage({ slot: SLOT, role: 'chunk', content: ' second', seq: 2 })) + live = store.getState().chat + } + + const payload = await store.dispatch(warmSlotCache(SLOT)).unwrap() + + expect(payload?.walkUnanchored).toBe(true) + expect(payload?.cacheAtDispatch).toBe(before.slotMessages[SLOT]) + expect(limits()).toHaveLength(1 + WINDOW_WALK_MAX_PAGES + 1) + expect(limits()).not.toContain(undefined) + // The real chunk reducer edits the row, not the array length; Immer must + // still give that slot a new array identity without mutating the snapshot. + expect(live.slotMessages[SLOT]).toHaveLength(before.slotMessages[SLOT].length) + expect(live.slotMessages[SLOT]).not.toBe(before.slotMessages[SLOT]) + expect(before.slotMessages[SLOT].at(-1)?.content).toBe('first') + const after = store.getState().chat + expect(after.slotMessages[SLOT]).toBe(live.slotMessages[SLOT]) + expect(cached(store)).toEqual([...rows(50).map(m => m.content), 'first second']) + expect(after.slotMessages[SLOT].at(-1)).toMatchObject({ + role: 'streaming', content: 'first second', rawText: 'first second', + }) + expect(after.slotPaneHasMore?.[SLOT]).toBe(live.slotPaneHasMore?.[SLOT]) + expect(after.slotPaneBounded?.[SLOT]).toBe(live.slotPaneBounded?.[SLOT]) + expect(after.slotRun[SLOT]).toEqual(live.slotRun[SLOT]) + expect(after.slotServerTotal?.[SLOT]).toBe(20_000) + }) + + it('keeps the newer cache and paging markers when an older unanchored warm settles last', async () => { + HISTORY = rows(50) + const store = backgroundPane(50, 0, 50) + const detail = vi.mocked(api.chatSlotDetail) + const readDetail = detail.getMockImplementation()! + let releaseOlder!: () => void + detail.mockImplementationOnce((...args) => { + const response = readDetail(...args) + return new Promise(resolve => { releaseOlder = () => resolve(response) }) + }) + // Hold the old snapshot while a newer warm observes rewritten history. + const cacheAtDispatch = store.getState().chat.slotMessages[SLOT] + const staleHistory = rows(20_000) + HISTORY = staleHistory + const older = store.dispatch(warmSlotCache(SLOT)) + HISTORY = rows(20) + await store.dispatch(warmSlotCache(SLOT)).unwrap() + const newer = store.getState().chat + const requestsBeforeRelease = limits().length + expect(cached(store)).toEqual(HISTORY.map(m => m.content)) + expect(newer.slotPaneHasMore?.[SLOT]).toBe(false) + expect(newer.slotPaneBounded?.[SLOT]).toBeUndefined() + + // The older request finishes its bounded walk against its old corpus. + HISTORY = staleHistory + releaseOlder() + const payload = await older.unwrap() + expect(payload?.walkUnanchored).toBe(true) + expect(payload?.cacheAtDispatch).toBe(cacheAtDispatch) + expect(newer.slotMessages[SLOT]).not.toBe(cacheAtDispatch) + expect(limits()).toHaveLength(requestsBeforeRelease + WINDOW_WALK_MAX_PAGES + 1) + expect(limits()).not.toContain(undefined) + const after = store.getState().chat + expect(after.slotMessages[SLOT]).toBe(newer.slotMessages[SLOT]) + expect(after.slotPaneHasMore?.[SLOT]).toBe(newer.slotPaneHasMore?.[SLOT]) + expect(after.slotPaneBounded?.[SLOT]).toBe(newer.slotPaneBounded?.[SLOT]) + expect(after.slotServerTotal?.[SLOT]).toBe(20) + expect(after.slotServerTotalSeq?.[SLOT]).toBe(newer.slotServerTotalSeq?.[SLOT]) + expect(after.slotRun[SLOT]).toEqual(newer.slotRun[SLOT]) + }) + + it('never reads unbounded, spending at most 1 + cap + 1 requests, and replaces rather than splices when the cap runs out', async () => { + // The gap is wider than the cap covers: the pane holds rows 0..4999 -- more + // than the walk's whole window -- and the server is 20,000 rows deep. The walk + // takes exactly the cap and stops, where the unbounded read it replaces would + // have moved all 20,000. The walked window is disjoint from the cache, so + // merging would publish a hole between row 4999 and the window; the pane takes + // the window instead, with a paging cursor that reaches the rest. + HISTORY = rows(5000) + const store = backgroundPane(5000, 0, 5000) + const cacheAtDispatch = store.getState().chat.slotMessages[SLOT] + HISTORY = rows(20_000) + + const payload = await store.dispatch(warmSlotCache(SLOT)).unwrap() + + expect(payload?.walkUnanchored).toBe(true) + expect(payload?.cacheAtDispatch).toBe(cacheAtDispatch) + expect(Object.isFrozen(payload?.cacheAtDispatch)).toBe(true) + const sent = limits() + expect(sent).not.toContain(undefined) + expect(sent).toHaveLength(1 + WINDOW_WALK_MAX_PAGES + 1) + expect(Math.max(...(sent as number[]))).toBeLessThanOrEqual(SERVER_CLAMP) + const contents = cached(store) + const walked = (1 + WINDOW_WALK_MAX_PAGES) * SERVER_CLAMP + expect(contents).toHaveLength(walked) + expect(contents[0]).toBe(`m${20_000 - walked}`) + expect(contents.at(-1)).toBe('m19999') + expect(contents).not.toContain('m4999') + expect(contents).toEqual(HISTORY.slice(-walked).map(m => m.content)) + expect(store.getState().chat.slotMessages[SLOT]).not.toBe(cacheAtDispatch) + expect(store.getState().chat.slotPaneHasMore?.[SLOT]).toBe(true) + expect(store.getState().chat.slotPaneBounded?.[SLOT]).toBe(walked) + }) + }) }) diff --git a/website/src/test/chatSlice.warmSlotCacheBound.test.ts b/website/src/test/chatSlice.warmSlotCacheBound.test.ts index 8b2d6ce80428..93fb50241d1a 100644 --- a/website/src/test/chatSlice.warmSlotCacheBound.test.ts +++ b/website/src/test/chatSlice.warmSlotCacheBound.test.ts @@ -206,12 +206,11 @@ describe('warmSlotCache hydrate bound', () => { expect(store.getState().chat.slotMessages['bg-slot']).toHaveLength(PANE_HYDRATE_LIMIT + 1) }) - it('carries a bounded total through a running coverage retry', async () => { + it('carries a bounded total through a running coverage miss', async () => { const held = [msg('held', '2026-08-13T08:00:00Z', 'm-1')] const distant = msg('distant', '2026-08-13T12:00:00Z', 'm-9') ;(api.chatSlotDetail as ReturnType) .mockResolvedValueOnce({ ...detail, messages: [distant], total: 3, running: true }) - .mockResolvedValueOnce({ ...detail, messages: [held[0], distant], total: 9, running: true }) const store = makeStore('active-slot', { slotRun: { 'bg-slot': { state: 'streaming' } }, slotMessages: { 'bg-slot': held }, @@ -227,17 +226,15 @@ describe('warmSlotCache hydrate bound', () => { expect(api.chatSlotDetail).toHaveBeenCalledWith('bg-slot', PANE_HYDRATE_LIMIT) }) - /* The bounded total is carried whether the wide read finds the slot running or - * idle, the same invariant `switchSlot` states after its own coverage retry. The - * wide read's total counts the raw uncollapsed window (per-turn `done` rows - * included) in either state, so it is never a comparable baseline: retaining it - * makes the next bounded warm's smaller collapsed count read as a server shrink. */ - it('carries the bounded total when the coverage retry finds the slot idle', async () => { + /* The coverage path never reads unbounded, running or idle, so the total it + * retains is the collapsed count. A raw uncollapsed count (per-turn `done` rows + * included) is never a comparable baseline: retaining it makes the next bounded + * warm's smaller collapsed count read as a server shrink. */ + it('carries the bounded total when a coverage miss finds the slot idle', async () => { const held = [msg('held', '2026-08-13T08:00:00Z', 'm-1')] const distant = msg('distant', '2026-08-13T12:00:00Z', 'm-9') ;(api.chatSlotDetail as ReturnType) - .mockResolvedValueOnce({ ...detail, messages: [distant], total: 3, running: true }) - .mockResolvedValueOnce({ ...detail, messages: [held[0], distant], total: 4, running: false }) + .mockResolvedValueOnce({ ...detail, messages: [distant], total: 3, running: false }) const store = makeStore('active-slot', { slotRun: { 'bg-slot': { state: 'streaming' } }, slotMessages: { 'bg-slot': held }, @@ -246,14 +243,13 @@ describe('warmSlotCache hydrate bound', () => { expect(store.getState().chat.slotServerTotal['bg-slot']).toBe(3) }) - it('does not read a later collapsed warm as a shrink after an idle coverage retry', async () => { + it('does not read a later collapsed warm as a shrink after an idle coverage miss', async () => { const held = [msg('held', '2026-08-13T08:00:00Z', 'm-1')] const distant = msg('distant', '2026-08-13T12:00:00Z', 'm-9') + // Every request on the coverage path is bounded, so the baseline it leaves is + // the collapsed count -- never the raw count an unbounded read reports. ;(api.chatSlotDetail as ReturnType) .mockResolvedValueOnce({ ...detail, messages: [distant], total: 3, running: false }) - // The wide read reports the raw window: the same two rows plus per-turn - // rows the bounded path collapses away, so its count is inflated. - .mockResolvedValueOnce({ ...detail, messages: [held[0], distant], total: 9, running: false }) const store = makeStore('active-slot', { slotRun: { 'bg-slot': { state: 'idle' } }, slotMessages: { 'bg-slot': held }, @@ -270,31 +266,29 @@ describe('warmSlotCache hydrate bound', () => { expect(store.getState().chat.slotMessages['bg-slot'].map(m => m.content)).toContain('in-flight reply') }) - /* Every comparison in the reducer reads the carried collapsed count, not the - * payload's raw one. A rewind that removes a row a background pane still holds - * ALWAYS goes through the coverage retry (the window cannot contain the removed - * row), so the reducer's shrink test only ever sees the wide read's raw total on - * a rewind. The retained baseline is collapsed; the raw count is inflated by the - * per-turn rows the bounded path folds away. Comparing raw against collapsed - * reads a rewind as growth, and the rescue puts the discarded rows back. */ - it('drops a rewind-discarded tail when the coverage retry answers with a raw total', async () => { + /* A rewind that removes a row a background pane still holds ALWAYS reports a + * coverage miss (the window cannot contain the removed row), and walking older + * cannot reach a row newer than the window's start. The warm confirms with one + * fresh read of the same bounded page -- never an unbounded one, whose raw count + * would read the rewind as growth -- and the reducer's shrink test drops the + * discarded tail. */ + it('drops a rewind-discarded tail after one fresh bounded read', async () => { const anchor = msg('anchor', '2026-08-13T08:00:00Z', 'm-1') const kept = msg('kept reply', '2026-08-13T09:00:00Z', 'm-2') const discarded = msg('discarded reply', '2026-08-13T10:00:00Z', 'm-3') ;(api.chatSlotDetail as ReturnType) // Bounded read after the rewind: the collapsed count fell from 3 to 2. .mockResolvedValueOnce({ ...detail, messages: [anchor, kept], has_more: false, total: 2 }) - // The wide read the coverage retry issues counts the raw window: the same - // two rows plus the per-turn rows the bounded path collapses away. - .mockResolvedValueOnce({ ...detail, messages: [anchor, kept], has_more: false, total: 5 }) + // The fresh read confirms it, in the same units. + .mockResolvedValueOnce({ ...detail, messages: [anchor, kept], has_more: false, total: 2 }) const store = makeStore('active-slot', { slotMessages: { 'bg-slot': [anchor, kept, discarded] }, slotServerTotal: { 'bg-slot': 3 }, }) await store.dispatch(warmSlotCache('bg-slot') as never) - // The retry ran: the pane held a row the bounded window could not reach. + // The confirming read ran, bounded: the pane held a row the window lacked. expect((api.chatSlotDetail as ReturnType).mock.calls.map(c => c[1])) - .toEqual([PANE_HYDRATE_LIMIT, undefined]) + .toEqual([PANE_HYDRATE_LIMIT, PANE_HYDRATE_LIMIT]) const held = store.getState().chat.slotMessages['bg-slot'].map(m => m.content) expect(held).toEqual(['anchor', 'kept reply']) expect(new Set(held).size).toBe(held.length) @@ -303,23 +297,23 @@ describe('warmSlotCache hydrate bound', () => { /* Same units, opposite outcome: a REWRITE replaces a reply and the collapsed * count holds. That is also a coverage miss (the superseded row is not in the - * window), so the reducer again sees the raw wide total. Read raw, the count - * differs from the baseline, the rewrite is not recognised, and the superseded - * reply is appended beside its replacement and renders twice. */ - it('recognises a same-count rewrite when the coverage retry answers with a raw total', async () => { + * window). The confirming read is bounded, so its count matches the baseline, + * the rewrite is recognised, and the superseded reply is not appended beside + * its replacement. */ + it('recognises a same-count rewrite after one fresh bounded read', async () => { const question = { role: 'user', content: 'question', cls: '', ts: '2026-08-13T08:00:00Z', meta: { mid: 'm-1' } } const superseded = msg('first reply', '2026-08-13T09:00:00Z', 'm-2') const replacement = msg('rewritten reply', '2026-08-13T09:00:30Z', 'm-3') ;(api.chatSlotDetail as ReturnType) .mockResolvedValueOnce({ ...detail, messages: [question, replacement], has_more: false, total: 2 }) - .mockResolvedValueOnce({ ...detail, messages: [question, replacement], has_more: false, total: 4 }) + .mockResolvedValueOnce({ ...detail, messages: [question, replacement], has_more: false, total: 2 }) const store = makeStore('active-slot', { slotMessages: { 'bg-slot': [question, superseded] }, slotServerTotal: { 'bg-slot': 2 }, }) await store.dispatch(warmSlotCache('bg-slot') as never) expect((api.chatSlotDetail as ReturnType).mock.calls.map(c => c[1])) - .toEqual([PANE_HYDRATE_LIMIT, undefined]) + .toEqual([PANE_HYDRATE_LIMIT, PANE_HYDRATE_LIMIT]) const held = store.getState().chat.slotMessages['bg-slot'].map(m => m.content) expect(held).toEqual(['question', 'rewritten reply']) expect(held.filter(c => c.endsWith('reply'))).toHaveLength(1) @@ -1305,7 +1299,6 @@ describe('switching away from a pane whose own fetch has not landed', () => { ;(api.chatSlotDetail as ReturnType) .mockResolvedValueOnce({ ...detail, messages: [history, turn], total: 6, has_more: true }) .mockResolvedValueOnce({ ...detail, messages: [confirm], total: 1, has_more: false }) - .mockResolvedValueOnce({ ...detail, messages: [confirm], total: 1, has_more: false }) const store = makeStore('active-slot', { slotMessages: { 'bg-slot': [history] }, slotHydrated: { 'bg-slot': true }, @@ -1329,7 +1322,6 @@ describe('switching away from a pane whose own fetch has not landed', () => { .mockImplementationOnce(() => new Promise(r => { resolveA = r })) .mockImplementationOnce(() => new Promise(r => { resolveB = r })) .mockImplementationOnce(() => new Promise(() => {})) - .mockResolvedValueOnce({ ...detail, messages: [confirm], total: 4, has_more: false }) const store = makeStore('active-slot', { slotMessages: { 'bg-slot': [history, keep] }, slotHydrated: { 'bg-slot': true },