fix(web): discard projection state across host generations

This commit is contained in:
_Kerman committed 2026-09-09 16:28:03 +08:00
1 parent e101c448e5
commit f3332af2a4
11 files changed
+178 -17

No files matched your search

@@ -7,7 +7,7 @@ import type { ContentBlock } from '@deepseek-ai/dsh-llm/types'
import type { SessionControlFrame } from '@deepseek-ai/dsh-api-session-controller/types'
import type { SessionId } from '@deepseek-ai/dsh-session/types'
import { SessionManager } from '../src/client/sessions/manager.ts'
import { FakeApiClient, fakeRemote } from './fake-api.client.ts'
import { deferred, FakeApiClient, fakeRemote, ok } from './fake-api.client.ts'
const SID = 'fk-q1' as SessionId
const text = (value: string): ContentBlock[] => [{ type: 'text', text: value }]
@@ -63,6 +63,50 @@ describe('Inbox projection intake', () => {
expect(manager.get(SID).projections.faceOf('inbox').getSnapshot()).toBeUndefined()
})
it.each(['control-first', 'list-first'] as const)(
'replaces cold Session Inbox values across Host generations (%s)',
async (order) => {
const api = new FakeApiClient()
const list = deferred<Awaited<ReturnType<FakeApiClient['onList']>>>()
api.onList = () => list.promise
const manager = new SessionManager(fakeRemote(api))
const hiddenSessionId = 'cold-hidden-inbox' as SessionId
const ghost = message('ghost', 'acceptance was not persisted')
const pending = message('pending', 'claim was not persisted')
const empty = { 'next-turn': [], 'next-step': [] }
const restored = { 'next-turn': [pending], 'next-step': [] }
manager.handleControlFrame({ ...inboxFrame({ ...empty, 'next-turn': [ghost] }), seq: 20 })
manager.handleControlFrame({ ...inboxFrame(empty), sessionId: hiddenSessionId, seq: 20 })
const face = manager.get(SID).projections.faceOf('inbox')
const baseline = { type: 'baseline', value: { jobs: {}, projections: {} } } as const
const result = ok({ items: [
{ sessionId: SID, updatedAt: 1, running: false, blank: false,
projections: { asOfSeq: 1, values: { inbox: empty } } },
{ sessionId: hiddenSessionId, updatedAt: 1, running: false, blank: false,
projections: { asOfSeq: 1, values: { inbox: restored } } },
] }) as Awaited<ReturnType<FakeApiClient['onList']>>
let refreshed: Promise<void> | undefined
try {
manager.handleConnected()
refreshed = manager.refreshList()
expect(face.getSnapshot()).toBeUndefined()
if (order === 'control-first') manager.handleControlFrame(baseline)
list.resolve(result)
await refreshed
if (order === 'list-first') manager.handleControlFrame(baseline)
expect(manager.get(SID).projections.faceOf('inbox')).toBe(face)
expect(face.getSnapshot()).toEqual(empty)
expect(manager.get(hiddenSessionId).projections.faceOf('inbox').getSnapshot()).toEqual(restored)
} finally {
list.resolve(result)
await refreshed
await manager.dispose()
}
},
)
it('retains only the highest-seq value received before Session materialization', () => {
const manager = new SessionManager(fakeRemote(new FakeApiClient()))
manager.handleControlFrame(inboxFrame({
@@ -751,6 +751,76 @@ describe('remaining branches', () => {
})
describe('connected generation', () => {
it.each(['old-first', 'new-first'] as const)(
'ignores a previous generation list response (%s)',
async (order) => {
const api = new FakeApiClient()
const oldList = deferred<Awaited<ReturnType<FakeApiClient['onList']>>>()
const newList = deferred<Awaited<ReturnType<FakeApiClient['onList']>>>()
let calls = 0
api.onList = () => calls++ === 0 ? oldList.promise : newList.promise
const manager = new SessionManager(fakeRemote(api))
const oldResult = ok({ items: [{ ...summary(S1), projections: {
asOfSeq: 20, values: { title: 'Unpersisted title' },
} }] as never[] })
const newResult = ok({ items: [{ ...summary(S1), projections: {
asOfSeq: 1, values: { title: 'Durable title' },
} }] as never[] })
const oldPull = manager.refreshList()
let newPull: Promise<void> | undefined
try {
manager.handleConnected()
newPull = manager.refreshList()
expect(api.callsOf('session.list')).toHaveLength(2)
if (order === 'old-first') {
oldList.resolve(oldResult)
await oldPull
expect(manager.getListSnapshot().state).toBe('loading')
expect(manager.refreshList()).toBe(newPull)
}
newList.resolve(newResult)
await newPull
oldList.resolve(oldResult)
await oldPull
expect(manager.getListSnapshot()).toMatchObject({ state: 'idle', error: null })
expect(manager.getListSnapshot().items[0]?.title).toBe('Durable title')
} finally {
oldList.resolve(oldResult)
newList.resolve(newResult)
await Promise.all([oldPull, newPull])
await manager.dispose()
}
},
)
it('ignores a previous generation request failure while the new list is loading', async () => {
const api = new FakeApiClient()
const oldList = deferred<Awaited<ReturnType<FakeApiClient['onList']>>>()
const newList = deferred<Awaited<ReturnType<FakeApiClient['onList']>>>()
let calls = 0
api.onList = () => calls++ === 0 ? oldList.promise : newList.promise
const manager = new SessionManager(fakeRemote(api))
const oldPull = manager.refreshList()
let newPull: Promise<void> | undefined
try {
manager.handleConnected()
newPull = manager.refreshList()
oldList.reject(new RemoteError('gateway/internal', 'old Host disconnected', {}))
await oldPull
expect(manager.getListSnapshot()).toMatchObject({ state: 'loading', error: null })
expect(manager.refreshList()).toBe(newPull)
newList.resolve(ok({ items: [summary(S1)] as never[] }))
await newPull
expect(manager.getListSnapshot()).toMatchObject({ state: 'idle', error: null })
} finally {
oldList.resolve(ok({ items: [] }))
newList.resolve(ok({ items: [] }))
await Promise.all([oldPull, newPull])
await manager.dispose()
}
})
it('refreshes query baselines without rebuilding independently resumed Session sources', async () => {
const api = new FakeApiClient()
api.onHistory = () => Promise.resolve(ok({
@@ -68,6 +68,32 @@ describe('Session projection value semantics', () => {
expect(store.get('other')).toBeUndefined()
})
it('clears all generation watermarks without replacing subscribed faces', async () => {
const store = new ProjectionValueStore()
const face = store.faceOf('test/marks')
const observed: unknown[] = []
const unsubscribe = face.subscribe(() => { observed.push(face.getSnapshot()) })
try {
store.apply('test/marks', { marks: ['lost-tail'] }, SessionSeq(20))
store.apply('empty-session', 'old generation', -1)
const previous = store.values()
await Promise.resolve()
store.clear()
await Promise.resolve()
expect(face.getSnapshot()).toBeUndefined()
expect(store.get('empty-session')).toBeUndefined()
expect(store.faceOf('test/marks')).toBe(face)
expect(store.values()).toEqual({})
expect(store.values()).not.toBe(previous)
store.seed({ asOfSeq: SessionSeq(1), values: { 'test/marks': { marks: ['durable'] } } })
await Promise.resolve()
expect(observed).toEqual([{ marks: ['lost-tail'] }, undefined, { marks: ['durable'] }])
} finally {
unsubscribe()
}
})
it('notifies the key face on change (batched) and not on dropped applications', async () => {
const store = new ProjectionValueStore()
let keyTicks = 0