Files
apes-sidebar/tests/sidechat-compaction-seed.spec.ts
T
NolanHo b025331b99 docs(sidechat): compaction-tail seed plan; test: discriminate remap and interleave paths
- plan doc: measured problem, the surface-equivalence argument, the
  construction (surface-order head emission, tail rebase with honest
  degradation, foldSurface self-verification, full-copy fallback), the
  explicit non-goals, and the deployment e2e gate.
- spec: assert the trim fires in the remap case (a replace fully inside
  the tail must stay a REMAPPED replace, not degrade into the fallback
  copy) and pin the interleave fallback (a tail replace covering a
  surviving pre-cut node).
2026-10-16 16:14:41 +08:00

303 lines
14 KiB
TypeScript

/**
* Compaction-tail seed spec: {@link buildSidechatInheritance} must reuse the
* parent's last summarizing compaction instead of copying its whole log.
*
* Why this is sound: the compaction checkpoint `user/message` REPLACES the
* shadowed span on the model-visible surface, so a child seeded with
* [pre-cut visible nodes + checkpoint + post-compaction tail] folds to the
* SAME surface as the parent — identical model-visible conversation, same
* provider context-cache prefix — while dropping the shadowed bulk from the
* durable copy. The construction is verified with the REAL fold
* (`foldSurface` from `@deepseek-ai/dsh-session/surface`): when the tail
* cannot be proven equivalent (dangling back-references, reordering
* replaces), the builder falls back to today's verbatim full copy.
*/
import { describe, expect, it } from 'vitest'
import { foldSurface } from '@deepseek-ai/dsh-session/surface'
import { Session, SessionLogOffset, SESSION_FORMAT_VERSION } from '@deepseek-ai/dsh-session'
import type { SessionEvent, SessionId } from '@deepseek-ai/dsh-session'
import { buildSidechatInheritance } from '../src/sidechat-core.ts'
import type { SidebarSessionEvent } from '../src/context-types.ts'
/** Log-only event (no surface marker; folds to nothing visible). */
function lo(type: string, seq: number, data: Record<string, unknown> = {}): SidebarSessionEvent {
return { type, seq, time: seq * 1000, data } as SidebarSessionEvent
}
/** Surface-eligible event appended the way the live pipeline appends it. */
function sf(type: string, seq: number, data: Record<string, unknown>): SidebarSessionEvent {
return { type, seq, time: seq * 1000, data, surfaceOp: 'append' } as SidebarSessionEvent
}
function userMessage(id: string, text: string): Record<string, unknown> {
return { id, role: 'user', content: [{ type: 'text', text }], source: { kind: 'user' } }
}
function assistantMessage(text: string): Record<string, unknown> {
return {
id: `m-${text}`,
role: 'assistant',
content: [{ type: 'text', text }],
source: { kind: 'model', provider: 'test', model: 'model-x' },
}
}
function toolResultMessage(callId: string, text: string): Record<string, unknown> {
return {
id: `m-${callId}`,
role: 'tool',
source: { kind: 'tool', callId },
toolCallId: callId,
content: [{ type: 'text', text }],
isError: false,
}
}
/** One completed turn with an optional tool pair; returns its events plus the
* seqs of its surface-visible nodes (user, result, assistant). */
function turn(
seq: number,
turnNumber: number,
over: { text?: string; callId?: string; result?: string } = {},
): { events: SidebarSessionEvent[]; nodes: number[] } {
const text = over.text ?? `q${turnNumber}`
const events: SidebarSessionEvent[] = [
lo('turn/start', seq, { turn: turnNumber }),
lo('step/start', seq + 1, { turn: turnNumber, step: 1 }),
]
const nodes: number[] = [seq + 2]
events.push(sf('user/message', seq + 2, userMessage(`m-q${turnNumber}`, text)))
let next = seq + 3
if (over.callId !== undefined) {
events.push(lo('tool/call', next, { turn: turnNumber, step: 1, callId: over.callId, name: 'bash', arguments: '{}' }))
events.push(sf('tool/result', next + 1, { turn: turnNumber, step: 1, message: toolResultMessage(over.callId, over.result ?? 'ok') }))
nodes.push(next + 1)
next += 2
}
events.push(sf('assistant/message', next, { turn: turnNumber, step: 1, message: assistantMessage(`a${turnNumber}`), stream: [] }))
nodes.push(next)
events.push(lo('step/end', next + 1, { turn: turnNumber, step: 1 }))
events.push(lo('turn/end', next + 2, { turn: turnNumber, reason: { kind: 'completed' } }))
return { events, nodes }
}
interface CompactedLog {
readonly events: SidebarSessionEvent[]
/** Seqs the checkpoint shadows (the old span's surface nodes). */
readonly shadowed: number[]
/** The checkpoint replacement event's seq. */
readonly checkpoint: number
}
/**
* The canonical compacted parent: protected system head, one shadowed old
* turn WITH a tool pair, one keep-window turn WITH its own tool pair, the
* compaction group, then one post-compaction tail turn.
*/
function compactedLog(): CompactedLog {
const events: SidebarSessionEvent[] = [
sf('system/message', 0, { message: { id: 'm-sys', role: 'system', content: [{ type: 'text', text: 'system prompt' }], source: { kind: 'system-prompt' } } }),
]
const old = turn(1, 1, { callId: 'c-old', result: 'old result' })
events.push(...old.events)
const keep = turn(1 + old.events.length, 2, { callId: 'c-keep', result: 'keep result' })
events.push(...keep.events)
const seq = events.length
const shadowed = old.nodes
events.push(lo('compaction/start', seq, { compactionId: 'cp1', turn: null }))
events.push(lo('compaction/summary', seq + 1, {
compactionId: 'cp1',
summary: [{ type: 'text', text: 'turn 1 summarized' }],
shadowedRange: { start: shadowed[0] ?? 0, end: shadowed[shadowed.length - 1] ?? 0 },
shadowedSeqs: shadowed,
shadowedTokenCount: 100,
provider: 'deepseek',
model: 'test-model',
rawOutput: [],
llmStreamCall: true,
}))
const checkpoint = seq + 2
events.push({
type: 'user/message',
seq: checkpoint,
time: checkpoint * 1000,
data: {
id: 'm-cp1',
role: 'user',
content: [{ type: 'text', text: 'turn 1 summarized' }],
source: { kind: 'compact-checkpoint', compactionId: 'cp1' },
},
surfaceOp: { op: 'replace', startSeq: shadowed[0] ?? 0, endSeq: shadowed[shadowed.length - 1] ?? 0 },
sourceEventSeqs: shadowed,
} as SidebarSessionEvent)
events.push(lo('compaction/end', seq + 3, { compactionId: 'cp1', turn: null }))
events.push(...turn(seq + 4, 3, { callId: 'c-tail', result: 'tail result' }).events)
return { events, shadowed, checkpoint }
}
/** The model-visible sequence a fold produces: one `type:data` token per node. */
function visibleSequence(events: readonly SidebarSessionEvent[]): string[] {
const fold = foldSurface(events as unknown as readonly SessionEvent[])
return fold.nodes.map(seq => {
const event = events[seq]
return event === undefined ? `missing:${seq}` : `${event.type}:${JSON.stringify(event.data)}`
})
}
function expectContiguous(seed: readonly { seq: number }[]): void {
seed.forEach((event, index) => expect(event.seq).toBe(index))
}
describe('buildSidechatInheritance compaction tail', () => {
it('reuses the last summarizing compaction instead of copying the shadowed span', () => {
const { events, checkpoint } = compactedLog()
const { seed, snapshot } = buildSidechatInheritance(events)
expect(snapshot).toBeNull()
// The shadowed old turn (its visible nodes AND its log-only scaffolding
// and tool/call) is dropped entirely.
expect(seed.length).toBeLessThan(events.length)
expect(seed.some(event => JSON.stringify(event).includes('old result'))).toBe(false)
expect(seed.some(event => JSON.stringify(event).includes('c-old'))).toBe(false)
// The keep window survives — including its paired tool call before its result.
const keepCall = seed.findIndex(event => JSON.stringify(event).includes('c-keep'))
const keepResult = seed.findIndex(event => JSON.stringify(event).includes('keep result'))
expect(keepCall).toBeGreaterThanOrEqual(0)
expect(keepResult).toBeGreaterThan(keepCall)
// The checkpoint survives as a plain append (its replace targets are gone).
const cp = seed.find(event => event.seq !== undefined && JSON.stringify(event).includes('turn 1 summarized'))
expect(cp).toBeDefined()
expect((cp as { surfaceOp?: unknown }).surfaceOp).toBe('append')
expect((cp as { sourceEventSeqs?: unknown }).sourceEventSeqs).toBeUndefined()
expectContiguous(seed)
void checkpoint
})
it('folds to the same model-visible sequence as the parent (surface equivalence)', () => {
const { events } = compactedLog()
const parent = visibleSequence(events)
expect(parent.length).toBeGreaterThan(0)
const { seed } = buildSidechatInheritance(events)
const child = visibleSequence(seed)
expect(child).toEqual(parent)
})
it('falls back to the verbatim full copy when the log has no compaction', () => {
const { events } = turn(0, 1, { callId: 'c1' })
const { seed } = buildSidechatInheritance(events)
expectContiguous(seed)
expect(seed.map(event => event.type)).toEqual(events.map(event => event.type))
})
it('falls back to the verbatim full copy when a tail event references the dropped head', () => {
const { events } = compactedLog()
// A developer/message citing a pre-cut request/header: the reference
// cannot be renumbered, so the tail is not provably equivalent.
const dangling = sf('developer/message', events.length, {
id: 'm-dev',
role: 'developer',
content: [{ type: 'text', text: 'dev' }],
source: { kind: 'developer' },
headerSeq: 1,
})
const withDangling = [...events, dangling]
const { seed } = buildSidechatInheritance(withDangling)
const full = buildSidechatInheritance(events)
// Full copy of the SAME log minus the dangling event appended... the
// fallback copies everything verbatim, so the seed carries the old span.
expect(seed.some(event => JSON.stringify(event).includes('old result'))).toBe(true)
void full
})
it('remaps replace operations that stay inside the tail', () => {
const { events } = compactedLog()
// One more closed tail turn, then a replacement user/message whose
// replace range covers ONLY that turn's tail nodes: the range rebases
// intact, the fold still shows the same transitions, the trim fires.
const tailTurn = turn(events.length, 4)
const replacedSeqs = tailTurn.nodes
events.push(...tailTurn.events)
const replacementSeq = events.length
events.push({
type: 'user/message',
seq: replacementSeq,
time: replacementSeq * 1000,
data: userMessage('m-edited', 'edited tail question'),
surfaceOp: { op: 'replace', startSeq: replacedSeqs[0] ?? 0, endSeq: replacedSeqs[replacedSeqs.length - 1] ?? 0 },
sourceEventSeqs: replacedSeqs,
} as SidebarSessionEvent)
const parent = visibleSequence(events)
const { seed } = buildSidechatInheritance(events)
// The trim must still fire here (the replace reorders only tail nodes —
// no fallback), which is what makes the remap assertions below meaningful.
expect(seed.length).toBeLessThan(events.length)
expect(visibleSequence(seed)).toEqual(parent)
const rebased = seed.find(event => {
const op = (event as { surfaceOp?: unknown }).surfaceOp
return typeof op === 'object' && op !== null && (op as { op?: unknown }).op === 'replace'
}) as { surfaceOp?: { startSeq: number; endSeq: number }; seq?: number } | undefined
// Not degraded: the range survived as a REMAPPED replace bound to the
// rebased positions (not to the parent's dropped-head seq space).
expect(rebased).toBeDefined()
expect(rebased?.surfaceOp?.startSeq).toBeLessThan(rebased?.seq ?? 0)
expect(rebased?.surfaceOp?.endSeq).toBeLessThan(rebased?.seq ?? 0)
})
it('falls back to the full copy when a tail replace reorders surviving nodes', () => {
const { events } = compactedLog()
// A replacement whose range covers a SURVIVING pre-cut node: the parent
// fold removes that node and repositions the replacement at its slot,
// which appending cannot reproduce — the verified equivalence must
// reject the trim and fall back to today's verbatim full copy.
const keepUserSeq = events.findIndex(event => JSON.stringify(event).includes('m-q2'))
const replacementSeq = events.length
events.push({
type: 'user/message',
seq: replacementSeq,
time: replacementSeq * 1000,
data: userMessage('m-resurfaced', 'resurfaced edit'),
surfaceOp: { op: 'replace', startSeq: keepUserSeq, endSeq: keepUserSeq },
sourceEventSeqs: [keepUserSeq],
} as SidebarSessionEvent)
const { seed } = buildSidechatInheritance(events)
expect(seed.some(event => JSON.stringify(event).includes('old result'))).toBe(true)
expect(seed.length).toBe(events.length + 0)
})
it('is accepted by the real dsh-session seed validator', () => {
const { events } = compactedLog()
const { seed } = buildSidechatInheritance(events)
// The REAL validator, exactly as the routes call it (isSeeded + the
// inherited cut) — this is the layer that once rejected a seed whose
// surfaceOp markers were stripped, so it gates every envelope decision
// the rebasing makes.
const id = 'session-compaction-seed-acceptance'
const session = Session.create(
id as SessionId,
seed as never,
{ version: SESSION_FORMAT_VERSION, id: id as SessionId, createdAt: Date.now(), isSeeded: true },
SessionLogOffset(seed.length),
)
// The constructor appends the inherited-cut marker right after the seed,
// and that marker is the child's first own event (log position = the
// inherited count) — same shape as today's full-copy threads.
expect(session.snapshotEvents()).toHaveLength(seed.length + 1)
expect(session.ownEvents()).toHaveLength(1)
expect(session.ownEvents()[0]?.type).toBe('session/end-seed')
})
it('keeps the open-turn closers and snapshot semantics on the trimmed tail', () => {
const { events, checkpoint } = compactedLog()
// Append an OPEN turn whose tool call has no result yet → dangling →
// the builder cuts before it and delivers a structured snapshot.
const seq = events.length
events.push(lo('turn/start', seq, { turn: 4 }))
events.push(lo('step/start', seq + 1, { turn: 4, step: 1 }))
events.push(lo('tool/call', seq + 2, { turn: 4, step: 1, callId: 'c-open', name: 'bash', arguments: '{}' }))
const { seed, snapshot } = buildSidechatInheritance(events)
expectContiguous(seed)
expect(seed.at(-1)?.type).not.toBe('tool/call')
expect(snapshot).not.toBeNull()
void checkpoint
})
})