Unify session surface validation

This commit is contained in:
Hypatia May
2026-07-14 13:49:36 +08:00
parent d9714fb30b
commit dbe65e1d13
17 changed files with 327 additions and 405 deletions

View File

@@ -1,7 +1,7 @@
/** One-shot session-lineage and event-relationship tracing helpers. */
import { foldSurface, validateSurfaceMetadata } from '@deepseek-ai/dsh-session'
import type { SessionEvent, SessionId } from '@deepseek-ai/dsh-session'
import { foldSurface } from '@deepseek-ai/dsh-session'
import type { SessionEvent, SessionId, SurfaceEventType } from '@deepseek-ai/dsh-session'
import { SessionQueryError } from './config.ts'
import type {
SessionEventRecord,
@@ -51,21 +51,6 @@ export function traceEventLog(
}
const analysis = analyzeEventLog(sessionId, events)
const knownSeqs = new Set<number>()
for (const event of events) {
const violation = validateSurfaceMetadata(
event,
knownSeqs,
analysis.replacedEventSeqs.get(event.seq),
)
if (violation !== undefined) {
throw new SessionQueryError(
`invalid session provenance: ${violation.message}`,
'SESSION_QUERY_INVALID_PROVENANCE',
)
}
knownSeqs.add(event.seq)
}
const replacementChain: number[] = []
let replacement = analysis.replacedBy.get(seq)
@@ -143,7 +128,9 @@ export function traceLineage(
children.push(record)
childrenByParent.set(parent, children)
}
for (const children of childrenByParent.values()) children.sort(compareSessionsAscending)
for (const children of childrenByParent.values()) {
children.sort((a, b) => a.header.createdAt - b.header.createdAt || a.header.id.localeCompare(b.header.id))
}
const descendants = buildDescendants(childrenByParent, sessionId)
const common = {
@@ -203,13 +190,8 @@ function analyzeEventLog(
}
}
function rawEventSources(event: SessionEvent): unknown {
return (event as SessionEvent & { sourceEventSeqs?: unknown }).sourceEventSeqs
}
function eventSources(event: SessionEvent): number[] {
const sources = rawEventSources(event)
return Array.isArray(sources) ? sources as number[] : []
return (event as SessionEvent<SurfaceEventType>).sourceEventSeqs ?? []
}
function buildDescendants(
@@ -238,10 +220,6 @@ function buildDescendants(
return descendants
}
function compareSessionsAscending(a: SessionRecord, b: SessionRecord): number {
return a.header.createdAt - b.header.createdAt || a.header.id.localeCompare(b.header.id)
}
function cloneRecord(record: SessionRecord): SessionRecord {
return { ...record, header: structuredClone(record.header) }
}