1
/**2
* Fork seed construction over an exact source-event prefix.3
* @module @deepseek-ai/dsh-session/fork4
*/6
import { openTurnClosers } from './repair.ts'7
import { SessionSeq } from './types.ts'8
import type { SessionEvent, SessionSeq as SessionSeqType } from './types.ts'10
/**11
* Copy an inclusive event prefix, mark its inherited cut, and close its open tail with forked results12
* and step/turn endings. Closed steps and turns are preserved unchanged.13
* The caller validates that the boundary is an existing contiguous event seq;14
* Session construction snapshots the borrowed events before publication.15
*16
* @param events - source log with contiguous seqs from zero.17
* @param boundary - inclusive source event seq the child inherits through.18
* @returns a new array retaining the source event objects, followed by synthetic19
* closers outside the inherited prefix counted by `inheritedEventCount`.20
*/21
export function buildForkSeed(events: readonly SessionEvent[], boundary: SessionSeqType): SessionEvent[] {22
const prefix = events.slice(0, boundary + 1)23
prefix.push({24
type: 'session/end-seed', seq: SessionSeq(boundary + 1),25
// oxlint-disable-next-line typescript/no-non-null-assertion -- the caller validated this exact source event.26
time: events[boundary]!.time,27
data: { inherited: true },28
})29
return prefix.concat(openTurnClosers(prefix, { kind: 'forked' }))30
}