Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 90 additions & 48 deletions website/src/store/chat/slotRefresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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 }
},
Expand Down Expand Up @@ -383,6 +426,8 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder<ChatState>)
.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.
Expand Down Expand Up @@ -456,18 +501,11 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder<ChatState>)
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
Expand Down Expand Up @@ -508,20 +546,22 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder<ChatState>)
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)]
Expand Down Expand Up @@ -557,15 +597,17 @@ export function addSlotRefreshCases(builder: ActionReducerMapBuilder<ChatState>)
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,
Expand Down
6 changes: 3 additions & 3 deletions website/src/store/chat/windowWalk.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
6 changes: 4 additions & 2 deletions website/src/store/chatSlice.boundedRefetchShrink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
Loading
Loading