返回源码地图

packages/core/session/src/fork.ts

main snapshot · da00f7f5358f · 正文引用章节 05;完整原文可核对,不声称全文件人工逐行审计

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1/**
2 * Fork seed construction over an exact source-event prefix.
3 * @module @deepseek-ai/dsh-session/fork
4 */
5
6import { openTurnClosers } from './repair.ts'
7import { SessionSeq } from './types.ts'
8import type { SessionEvent, SessionSeq as SessionSeqType } from './types.ts'
9
10/**
11 * Copy an inclusive event prefix, mark its inherited cut, and close its open tail with forked results
12 * 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 synthetic
19 * closers outside the inherited prefix counted by `inheritedEventCount`.
20 */
21export 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}