| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /** Canonical packed-row and envelope projection helpers for repository session fixtures. */
- import { deepStrictEqual } from 'node:assert'
- import { execFileSync } from 'node:child_process'
- import { existsSync, readFileSync } from 'node:fs'
- import { resolve } from 'node:path'
- import { packChunkRuns, type SessionEvent } from '@deepseek-ai/dsh-session'
- import { parseSessionLog } from '@deepseek-ai/dsh-llm-replay'
- /** Physical persistence artifacts validated by the WebWorker runtime fixture spec. */
- const PHYSICAL_SESSION_FIXTURE_ROOT =
- 'packages/experimental/webworker-runtime/tests/fixtures/vfs-example/home/sessions/'
- /** One repository session fixture and its canonical projected representation. */
- export interface SessionFixtureLayout {
- /** Repository-relative path with `/` separators. */
- path: string
- /** Current fixture bytes decoded as UTF-8. */
- source: string
- /** Canonical projected fixture bytes. */
- canonical: string
- }
- /**
- * Whether a repository JSONL is a production-layout persistence artifact rather
- * than an envelope-free replay snapshot owned by this script.
- * @param path - Repository-relative path with `/` separators.
- * @returns True only for Session logs under the WebWorker VFS example root.
- */
- export function isPhysicalSessionFixture(path: string): boolean {
- return path.startsWith(PHYSICAL_SESSION_FIXTURE_ROOT) && path.endsWith('/session.jsonl')
- }
- function isSessionHeader(value: unknown): boolean {
- return value !== null && typeof value === 'object' && (value as { type?: unknown }).type === 'session'
- }
- function renderFixture(headerLine: string, events: readonly SessionEvent[]): string {
- return [
- headerLine,
- ...packChunkRuns(events).map((stored) => {
- const record = stored as unknown as Record<string, unknown>
- delete record.seq
- delete record.time
- delete record.seq0
- delete record.time0
- return JSON.stringify(record)
- }),
- '',
- ].join('\n')
- }
- function withoutEnvelope(events: readonly SessionEvent[]): Array<Omit<SessionEvent, 'seq' | 'time'>> {
- return events.map((event) => {
- const { seq: _seq, time: _time, ...projected } = event
- return projected
- })
- }
- /**
- * Canonicalize one JSONL document when its first record is a session header.
- * The header line remains byte-identical; body records decode to logical events,
- * re-encode with {@link packChunkRuns}, and omit storage sequence/time envelopes.
- * Non-session JSONL returns undefined.
- *
- * @param content - JSONL source text.
- * @param label - path-like diagnostic label.
- * @returns Canonical text for a session fixture, otherwise undefined.
- */
- export function canonicalSessionFixture(content: string, label = '<session-fixture>'): string | undefined {
- const headerLine = content.split(/\r?\n/).find(line => line.trim().length > 0)
- if (headerLine === undefined) return undefined
- let headerValue: unknown
- try {
- headerValue = JSON.parse(headerLine) as unknown
- } catch {
- return undefined
- }
- if (!isSessionHeader(headerValue)) return undefined
- let events
- try {
- events = parseSessionLog(content)
- } catch (error) {
- const detail = error instanceof Error ? error.message : String(error)
- throw new Error(`${label}: ${detail}`, { cause: error })
- }
- const canonical = renderFixture(headerLine, events)
- const decoded = parseSessionLog(canonical)
- try {
- deepStrictEqual(withoutEnvelope(decoded), withoutEnvelope(events))
- } catch (error) {
- throw new Error(`${label}: packed snapshot rewrite changed the event payload stream`, { cause: error })
- }
- if (renderFixture(headerLine, decoded) !== canonical) {
- throw new Error(`${label}: packed rewrite is not idempotent`)
- }
- return canonical
- }
- /**
- * Discover tracked and unignored untracked JSONL files through Git.
- *
- * @param root - repository root.
- * @returns Stable repository-relative paths.
- */
- function discoverJsonlFiles(root: string): string[] {
- return execFileSync(
- 'git',
- ['ls-files', '-z', '--cached', '--others', '--exclude-standard', '--', '*.jsonl'],
- { cwd: root, encoding: 'utf8' },
- ).split('\0')
- .filter(path => path.length > 0 && existsSync(resolve(root, path)))
- .sort()
- }
- /**
- * Inspect every repository JSONL whose first record is a session header.
- *
- * @param root - repository root.
- * @returns Session fixtures with current and canonical text.
- */
- export function inspectSessionFixtureLayouts(root: string): SessionFixtureLayout[] {
- return discoverJsonlFiles(root).flatMap((path) => {
- if (isPhysicalSessionFixture(path)) return []
- const source = readFileSync(resolve(root, path), 'utf8')
- const canonical = canonicalSessionFixture(source, path)
- return canonical === undefined ? [] : [{ path, source, canonical }]
- })
- }
|