Files
deepseek-harness/packages/core/agent-loop/tests/inbox.spec.ts
T
imccyu eb1713f856 fix(session-projection): keep released units in checkpoints (#7754)
* fix(session-projection): keep released units in checkpoints

Since the Cordis rc.11 teardown order unloads the Web layer before the
base layer, Session Controller and harness-dev-context release their
projection units before their live Sessions detach. The projection
cache is still alive at that point and wrote a checkpoint holding only
the units that were still registered, replacing the complete record.
A blank Session then lost sessionListMetadata and listed as an untitled
conversation after every graceful restart.

A released unit now stays in the registry as a predecessor: it leaves
event drives, snapshots, restores, and change notifications, while
checkpoint() keeps each Session's last cell of it without advancing it.
Registering the same key again replaces the predecessor and refolds
the log. The projection cache keeps replacing whole records.

* test(session-projection): assert frozen released units across unload tests

Update the remaining unload tests that still expected a released unit
to vanish from checkpoint(): user-approval, agent-loop inbox and scope
lifecycle, time-context, and the agent registry now assert that the
same Session keeps its pre-unload row, that a later event does not
advance it, and that a Session created after the unload has no row.
The token-meter and goal unload tests also append an event after the
unload.

Document that a retained handle read still advances a released cell
that checkpoint() serves, and that restore() folds only live units, so
its refreshed rows omit released units.

* docs(session-projection): sync the ProjectionHandle type-equiv block

* docs(session-projection): record frozen released units and their upgrade

Add the upgrade guide for checkpoint() keeping a released unit's last
row, update the registration-handles Agent Note with the frozen-cell
decision and the retained predecessor, list both consequences under the
registry README's known limitations, note in the projection cache README
that a stored row can be frozen at its unit's release, and say that
restoreFloor() ignores released predecessors.

Update the agent-loop resource-lifecycle unload test, which read
checkpoints through an alias, to the frozen-row semantics.

* docs(upgrade-guide): mark the released-checkpoint snippet ignore-check

* docs(session-projection): state the retained-handle path and narrow a rejected alternative

* docs(upgrade-guide): re-record the released-checkpoint guide pairing
2026-11-03 11:07:09 +08:00

286 lines
12 KiB
TypeScript

import { unsupportedModel } from '@deepseek-ai/dsh-agent-loop-testkit'
import { Context } from '@deepseek-ai/cordis'
import { agentEvents, type Agent } from '@deepseek-ai/dsh-agent'
import { createUserMessage, freezeMessage } from '@deepseek-ai/dsh-llm'
import SessionStore, { Session, SessionId } from '@deepseek-ai/dsh-session'
import type { UserMessage } from '@deepseek-ai/dsh-session'
import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
import { describe, expect, it, onTestFinished } from 'vitest'
import { inboxProjectionDefinition, ReactLoopInbox } from '../src/inbox.ts'
function unsupportedInbox(): Agent['inbox'] {
const rejectMutation = (): never => {
throw new Error('this test Agent does not support Inbox mutations')
}
return {
nextTurn: [], nextStep: [], clear: rejectMutation, append: rejectMutation,
prepend: rejectMutation, replace: rejectMutation, remove: rejectMutation, splice: rejectMutation,
}
}
function stubAgent(rawId: string, overrides: Partial<Agent> = {}): Agent {
const id = SessionId(rawId)
const session = overrides.session ?? Session.create(id)
const ctx = overrides.ctx ?? new Context()
return {
id,
options: {},
session,
model: unsupportedModel(),
inbox: unsupportedInbox(),
status: 'idle',
ctx,
send: () => {},
followup: () => {},
steer: () => {},
inject: () => {},
cancel() {},
runMaintenance: task => task(new AbortController().signal),
whenIdle: () => Promise.resolve(),
...overrides,
}
}
async function inboxAgent(rawId: string): Promise<{
ctx: Context
session: Session
agent: Agent
inbox: ReactLoopInbox
}> {
const ctx = new Context()
onTestFinished(() => ctx.fiber.dispose())
await ctx.plugin(SessionStore)
await ctx.plugin(SessionProjectionRegistry)
const projection = await ctx.sessionProjections.register(inboxProjectionDefinition)
const session = ctx.sessions.create(SessionId(rawId))
const agent = stubAgent(rawId, { ctx, session })
const inbox = new ReactLoopInbox(() => projection.stateOf(session), session, agentEvents(ctx, agent))
Object.assign(agent, { inbox })
return { ctx, session, agent, inbox }
}
async function reconstructPersistedInbox(
rawId: string,
populate: (session: Session) => void,
): Promise<Error> {
const ctx = new Context()
onTestFinished(() => ctx.fiber.dispose())
await ctx.plugin(SessionStore)
const session = ctx.sessions.create(SessionId(rawId))
populate(session)
await ctx.plugin(SessionProjectionRegistry)
const projection = await ctx.sessionProjections.register(inboxProjectionDefinition)
const agent = stubAgent(rawId, { ctx, session })
const inbox = new ReactLoopInbox(() => projection.stateOf(session), session, agentEvents(ctx, agent))
try {
void inbox.nextTurn
} catch (error: unknown) {
if (error instanceof Error) return error
throw error
}
throw new Error('persisted inbox reconstruction unexpectedly succeeded')
}
describe('ReactLoopInbox', () => {
it('retains inbox reads after the shared registration is released', async () => {
const ctx = new Context()
onTestFinished(() => ctx.fiber.dispose())
await ctx.plugin(SessionStore)
await ctx.plugin(SessionProjectionRegistry)
const projection = await ctx.sessionProjections.register(inboxProjectionDefinition)
const session = ctx.sessions.create(SessionId('inbox-projection'))
const pending = createUserMessage({
content: [{ type: 'text', text: 'pending' }],
source: { kind: 'user' },
})
session.append('agent/inbox/spliced', {
target: 'next-turn', start: 0, inserted: [pending],
})
const agent = stubAgent('inbox-projection', { ctx, session })
const dispatch = agentEvents(ctx, agent)
const first = new ReactLoopInbox(() => projection.stateOf(session), session, dispatch)
const second = new ReactLoopInbox(() => projection.stateOf(session), session, dispatch)
expect(first.nextTurn).toEqual([pending])
expect(second.nextTurn).toEqual([pending])
expect(ctx.sessionProjections.snapshot(session).values.inbox).toEqual({
'next-turn': [pending],
'next-step': [],
})
const row = ctx.sessionProjections.checkpoint(session).inbox
await projection.dispose()
expect(ctx.sessionProjections.checkpoint(session)).toHaveProperty('inbox', row)
expect(ctx.sessionProjections.checkpoint(ctx.sessions.create())).not.toHaveProperty('inbox')
expect(first.nextTurn).toEqual([pending])
first.clear()
expect(second.nextTurn).toEqual([])
expect(ctx.sessionProjections.snapshot(session).values.inbox).toBeUndefined()
})
it('rejects invalid durable coordinates and duplicate identities during reconstruction', async () => {
const outOfRange = await reconstructPersistedInbox('invalid-inbox-range', (session) => {
session.append('agent/inbox/spliced', {
target: 'next-turn', start: 0, removedCount: 1, inserted: [],
})
})
expect(outOfRange.message).toBe('invalid persisted inbox splice at session seq 0')
expect((outOfRange.cause as Error).message).toBe('invalid inbox splice')
const pending = createUserMessage({
content: [{ type: 'text', text: 'duplicate' }],
source: { kind: 'user' },
})
const duplicate = await reconstructPersistedInbox('invalid-inbox-duplicate', (session) => {
session.append('agent/inbox/spliced', {
target: 'next-turn', start: 0, inserted: [pending],
})
session.append('agent/inbox/spliced', {
target: 'next-step', start: 0, inserted: [pending],
})
})
expect(duplicate.message).toBe('invalid persisted inbox splice at session seq 1')
expect((duplicate.cause as Error).message).toBe(`message "${pending.id}" is already pending`)
})
it('projects inherited inbox events in a forked session', async () => {
const ctx = new Context()
onTestFinished(() => ctx.fiber.dispose())
await ctx.plugin(SessionStore)
await ctx.plugin(SessionProjectionRegistry)
const projection = await ctx.sessionProjections.register(inboxProjectionDefinition)
const parent = ctx.sessions.create(SessionId('inbox-fork-parent'))
const parentAgent = stubAgent('inbox-fork-parent', { ctx, session: parent })
const parentInbox = new ReactLoopInbox(() => projection.stateOf(parent), parent, agentEvents(ctx, parentAgent))
const inherited = createUserMessage({
content: [{ type: 'text', text: 'parent pending' }],
source: { kind: 'user' },
})
parentInbox.append('next-turn', inherited)
const child = ctx.sessions.fork(parent, undefined, SessionId('inbox-fork-child'))
const childAgent = stubAgent('inbox-fork-child', { ctx, session: child })
const childInbox = new ReactLoopInbox(() => projection.stateOf(child), child, agentEvents(ctx, childAgent))
expect(child.inheritedEventCount).toBe(parent.snapshotEvents().length)
expect(childInbox.nextTurn).toEqual([inherited])
const own = createUserMessage({
content: [{ type: 'text', text: 'child pending' }],
source: { kind: 'user' },
})
childInbox.append('next-turn', own)
expect(childInbox.nextTurn).toEqual([inherited, own])
})
it('updates the projection cell before session observers run', async () => {
const { ctx, session, inbox } = await inboxAgent('inbox-live-projection')
const pending = createUserMessage({
content: [{ type: 'text', text: 'direct' }],
source: { kind: 'user' },
})
let observed: readonly UserMessage[] | undefined
ctx.on('session/event', (subject, event) => {
if (subject === session && event.type === 'agent/inbox/spliced') {
observed = inbox.nextTurn
}
})
inbox.append('next-turn', pending)
expect(observed).toEqual([pending])
expect(ctx.sessionProjections.snapshot(session).values.inbox).toEqual({
'next-turn': [pending], 'next-step': [],
})
})
it('replaces a pending message by identity across both lists', async () => {
const { ctx, agent } = await inboxAgent('replace-inbox')
const inserted: UserMessage[] = []
const discarded: UserMessage[] = []
ctx.on('agent/inbox/inserted', ({ message }) => void inserted.push(message))
ctx.on('agent/inbox/discarded', ({ message }) => void discarded.push(message))
const original = createUserMessage({
content: [{ type: 'text', text: 'original' }],
source: { kind: 'user' },
})
const nextStep = createUserMessage({
content: [{ type: 'text', text: 'step' }],
source: { kind: 'user' },
})
const replacement = createUserMessage({
content: [{ type: 'text', text: 'replacement' }],
source: { kind: 'user' },
})
const editedStep = freezeMessage({
...nextStep,
content: [{ type: 'text', text: 'edited step' }],
})
agent.inbox.append('next-turn', original)
agent.inbox.append('next-step', nextStep)
expect(agent.inbox.replace(createUserMessage({
content: [{ type: 'text', text: 'missing' }],
source: { kind: 'user' },
}).id, replacement)).toBe(false)
expect(agent.inbox.replace(original.id, replacement)).toBe(true)
expect(agent.inbox.replace(nextStep.id, editedStep)).toBe(true)
expect(agent.inbox.nextTurn).toEqual([replacement])
expect(agent.inbox.nextStep).toEqual([editedStep])
expect(discarded).toEqual([original, nextStep])
expect(inserted).toEqual([original, nextStep, replacement, editedStep])
expect(() => { agent.inbox.replace(editedStep.id, replacement) })
.toThrow(`message "${replacement.id}" is already pending`)
})
it('normalizes splice coordinates, rejects duplicate identities, and reports missing removals', async () => {
const { agent } = await inboxAgent('splice-inbox')
const first = createUserMessage({
content: [{ type: 'text', text: 'first' }],
source: { kind: 'user' },
})
const second = createUserMessage({
content: [{ type: 'text', text: 'second' }],
source: { kind: 'user' },
})
const prefixed = createUserMessage({
content: [{ type: 'text', text: 'prefixed' }],
source: { kind: 'user' },
})
agent.inbox.splice('next-turn', Number.NaN, Number.NaN, [first, second])
expect(agent.inbox.nextTurn).toEqual([first, second])
expect(agent.inbox.splice('next-turn', -1, 1, [])).toEqual([second])
agent.inbox.prepend('next-turn', prefixed)
expect(agent.inbox.nextTurn).toEqual([prefixed, first])
expect(agent.inbox.remove(second.id)).toBe(false)
expect(() => { agent.inbox.append('next-step', first) }).toThrow(`message "${first.id}" is already pending`)
})
it('clears both pending lists as durable cancellations', async () => {
const { ctx, session, agent } = await inboxAgent('clear-inbox')
const discarded: UserMessage[] = []
ctx.on('agent/inbox/discarded', ({ message }) => void discarded.push(message))
const nextTurn = createUserMessage({ content: [{ type: 'text', text: 'turn' }], source: { kind: 'user' } })
const nextStep = createUserMessage({ content: [{ type: 'text', text: 'step' }], source: { kind: 'user' } })
agent.inbox.append('next-turn', nextTurn)
agent.inbox.append('next-step', nextStep)
const beforeClear = session.snapshotEvents().length
agent.inbox.clear()
expect(agent.inbox.nextTurn).toEqual([])
expect(agent.inbox.nextStep).toEqual([])
expect(discarded).toEqual([nextStep, nextTurn])
expect(session.snapshotEvents().slice(beforeClear).map(event => event.type === 'agent/inbox/spliced'
? event.data
: event.type)).toEqual([
{ target: 'next-step', start: 0, removedCount: 1, inserted: [], outcome: 'canceled' },
{ target: 'next-turn', start: 0, removedCount: 1, inserted: [], outcome: 'canceled' },
])
agent.inbox.clear()
expect(session.snapshotEvents()).toHaveLength(beforeClear + 2)
})
})