| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179 |
- import { describe, expect, it } from 'vitest'
- import {
- type NormalizeContext,
- extractSnapshotSpillPaths,
- normalizeSessionLog,
- normalizeSessionFormatProvenance,
- normalizeSessionSnapshot,
- normalizeSessionSnapshots,
- normalizeStdout,
- scrubModelRequestBulk,
- scrubSessionSnapshot,
- scrubSystemPrompts,
- scrubToolSchemas,
- tokenizeSessionFixtureCwd,
- } from '../src/normalize.ts'
- /**
- * Unit tests for the pure snapshot normalizers. Live as a *.spec.ts (runs in
- * the default unit gate) and import the normalizers directly.
- */
- const ctx: NormalizeContext = {
- sessionIds: ['11111111-2222-3333-4444-555555555555'],
- cwd: '/tmp/acp-snap-cwd-abc123',
- }
- describe('normalizeStdout', () => {
- it('rewrites JSON-RPC ids to a stable first-seen sequence', () => {
- const raw = [
- JSON.stringify({ jsonrpc: '2.0', id: 42, method: 'initialize' }),
- JSON.stringify({ jsonrpc: '2.0', id: 42, result: {} }),
- JSON.stringify({ jsonrpc: '2.0', id: 99, method: 'session/new' }),
- ].join('\n')
- const out = normalizeStdout(raw, ctx)
- expect(out).toContain('"id":1')
- expect(out).toContain('"id":2')
- expect(out).not.toContain('42')
- expect(out).not.toContain('99')
- })
- it('scrubs the cwd and session id anywhere they appear', () => {
- const raw = JSON.stringify({
- jsonrpc: '2.0', method: 'session/update',
- params: { sessionId: ctx.sessionIds[0], cwd: ctx.cwd, note: `at ${ctx.cwd}/x` },
- })
- const out = normalizeStdout(raw, ctx)
- expect(out).toContain('{{sessionId}}')
- expect(out).toContain('{{cwd}}')
- expect(out).not.toContain(ctx.cwd)
- expect(out).not.toContain(ctx.sessionIds[0] as string)
- })
- it('keeps standard message identity distinct from session identity', () => {
- const raw = JSON.stringify({
- jsonrpc: '2.0',
- method: 'session/update',
- params: {
- sessionId: ctx.sessionIds[0],
- update: {
- sessionUpdate: 'agent_message_chunk',
- messageId: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
- content: { type: 'text', text: 'done' },
- },
- },
- })
- const out = normalizeStdout(raw, ctx)
- expect(out).toContain('"sessionId":"{{sessionId}}"')
- expect(out).toContain('"messageId":"{{messageId}}"')
- })
- it('stabilizes path-dependent context occupancy without hiding capacity', () => {
- const raw = JSON.stringify({
- jsonrpc: '2.0',
- method: 'session/update',
- params: {
- sessionId: ctx.sessionIds[0],
- update: { sessionUpdate: 'usage_update', used: 6_438, size: 1_000_000 },
- },
- })
- const frame = JSON.parse(normalizeStdout(raw, ctx)) as {
- params: { update: { used: string; size: number } }
- }
- expect(frame.params.update).toEqual({
- sessionUpdate: 'usage_update',
- used: '{{usedTokens}}',
- size: 1_000_000,
- })
- })
- it('scrubs cwd at file URI and chained-punctuation boundaries', () => {
- const raw = JSON.stringify({
- jsonrpc: '2.0',
- method: 'session/update',
- params: {
- uri: `file://${ctx.cwd}/proof.txt`,
- punctuated: `${ctx.cwd}.,`,
- dottedSegment: `${ctx.cwd}.backup`,
- dashedSegment: `${ctx.cwd}-backup`,
- },
- })
- const frame = JSON.parse(normalizeStdout(raw, ctx)) as {
- params: Record<string, string>
- }
- expect(frame.params).toEqual({
- uri: 'file://{{cwd}}/proof.txt',
- punctuated: '{{cwd}}.,',
- dottedSegment: `${ctx.cwd}.backup`,
- dashedSegment: `${ctx.cwd}-backup`,
- })
- })
- it('scrubs every filesystem spelling of the cwd longest-first', () => {
- const longCwd = String.raw`C:\Users\runneradmin\AppData\Local\Temp\acp-snapshot`
- const aliasedCtx: NormalizeContext = {
- sessionIds: [],
- cwd: String.raw`C:\Users\RUNNER~1\AppData\Local\Temp\acp-snapshot`,
- cwdAliases: [
- longCwd,
- String.raw`C:\Users\runneradmin\AppData\Local\Temp\acp`,
- ],
- }
- const raw = JSON.stringify({
- cwd: longCwd,
- path: `${longCwd}\\nested\\proof.txt`,
- })
- const frame = JSON.parse(normalizeStdout(raw, aliasedCtx)) as { cwd: string; path: string }
- expect(frame).toEqual({ cwd: '{{cwd}}', path: '{{cwd}}/nested/proof.txt' })
- })
- it('canonicalizes only cwd-rooted path separators', () => {
- const windowsCtx: NormalizeContext = {
- sessionIds: [],
- cwd: String.raw`C:\Users\runner\AppData\Local\Temp\acp-snapshot`,
- }
- const raw = JSON.stringify({
- jsonrpc: '2.0',
- method: 'session/update',
- params: {
- path: `${windowsCtx.cwd}\\nested\\proof.txt`,
- regex: String.raw`\d+\w+`,
- command: String.raw`printf "\\n"`,
- },
- })
- const frame = JSON.parse(normalizeStdout(raw, windowsCtx)) as {
- params: { path: string; regex: string; command: string }
- }
- expect(frame.params).toEqual({
- path: '{{cwd}}/nested/proof.txt',
- regex: String.raw`\d+\w+`,
- command: String.raw`printf "\\n"`,
- })
- })
- it('canonicalizes generated relative path fields and text markers without rewriting other text', () => {
- const raw = JSON.stringify({
- path: String.raw`nested\AGENTS.md`,
- content: String.raw`<path>.\nested\task.txt</path>
- Additional instructions from: nested\AGENTS.md`,
- regex: String.raw`\d+\w+`,
- })
- const frame = JSON.parse(normalizeStdout(raw, { sessionIds: [], cwd: '/unused' })) as {
- path: string
- content: string
- regex: string
- }
- expect(frame).toEqual({
- path: 'nested/AGENTS.md',
- content: '<path>./nested/task.txt</path>\nAdditional instructions from: nested/AGENTS.md',
- regex: String.raw`\d+\w+`,
- })
- })
- it('can preserve native cwd-rooted separators for a platform golden', () => {
- const windowsCtx: NormalizeContext = { sessionIds: [], cwd: String.raw`C:\work\snapshot` }
- const raw = JSON.stringify({ path: `${windowsCtx.cwd}\\nested\\proof.txt` })
- const frame = JSON.parse(normalizeStdout(raw, windowsCtx, { cwdPathMode: 'native' })) as { path: string }
- expect(frame.path).toBe(String.raw`{{cwd}}\nested\proof.txt`)
- })
- it('scrubs a stray UUID not in the known list', () => {
- const raw = JSON.stringify({ jsonrpc: '2.0', method: 'x', params: { id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' } })
- expect(normalizeStdout(raw, ctx)).toContain('{{sessionId}}')
- })
- it('leaves notification frames without an id untouched in id-space', () => {
- const raw = JSON.stringify({ jsonrpc: '2.0', method: 'session/update', params: {} })
- const out = normalizeStdout(raw, ctx)
- expect(out).not.toContain('"id"')
- })
- it('stabilizes only the top-level event timestamp and spill byte count in event-read text', () => {
- const raw = JSON.stringify({
- jsonrpc: '2.0',
- method: 'session/update',
- params: {
- update: {
- sessionUpdate: 'tool_call_update',
- content: [{
- type: 'content',
- content: {
- type: 'text',
- text: 'Session prior — title\nTarget event seq 4:\n```json\n{\n "seq": 4,\n "time": 1784876275593,\n "data": {\n "time": 31337,\n "note": "model-visible"\n }\n}\n```\n\nAfter:\n "time": 424242,\n neighbor semantic text\n\n(Omitted 39387 bytes. Full formatted result stored at: /tmp/result.txt.)',
- },
- }],
- },
- },
- })
- const out = normalizeStdout(raw, ctx)
- expect(out).toContain('\\"time\\": {{eventTime}}')
- expect(out).toContain('\\"time\\": 31337')
- expect(out).toContain('\\"time\\": 424242')
- expect(out).toContain('Omitted {{eventOmittedBytes}} bytes')
- expect(out).not.toContain('1784876275593')
- expect(out).not.toContain('39387')
- })
- it('preserves event-like timestamps in unrelated output text', () => {
- const raw = JSON.stringify({
- jsonrpc: '2.0',
- method: 'session/update',
- params: {
- update: {
- sessionUpdate: 'tool_call_update',
- content: [{
- type: 'content',
- content: {
- type: 'text',
- text: 'bash output:\n```json\n{\n "time": 1784876275593,\n "data": {}\n}\n```\n\n(Omitted 39387 bytes. Full formatted result stored at: /tmp/result.txt.)',
- },
- }],
- },
- },
- })
- const out = normalizeStdout(raw, ctx)
- expect(out).toContain('1784876275593')
- expect(out).toContain('39387')
- expect(out).not.toContain('{{eventTime}}')
- expect(out).not.toContain('{{eventOmittedBytes}}')
- })
- it('throws on a non-JSON stdout line (the purity check)', () => {
- const raw = `${JSON.stringify({ jsonrpc: '2.0', id: 1 })}\noops a log leaked\n`
- expect(() => normalizeStdout(raw, ctx)).toThrow()
- })
- it('ignores blank lines', () => {
- const raw = `\n${JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'm' })}\n\n`
- expect(() => normalizeStdout(raw, ctx)).not.toThrow()
- })
- })
- describe('normalizeSessionLog', () => {
- it('normalizes only message-feedback item clocks', () => {
- const item = { messageId: 'answer', version: 'version', createdAt: 123, updatedAt: 456, note: 'keep 123' }
- const input = ['feedback/message-put', 'tool/result'].map(type => JSON.stringify({ type, data: { item } })).join('\n')
- const output = normalizeSessionLog(input, ctx)
- expect(output).toContain('"createdAt":0,"updatedAt":0,"note":"keep 123"')
- expect(output).toContain('"createdAt":123,"updatedAt":456,"note":"keep 123"')
- })
- const header = (over: object) => JSON.stringify({ type: 'session', version: 0, id: 's', createdAt: 123, ...over })
- const event = (over: object) => JSON.stringify({ type: 'turn/start', seq: 1, time: 999, data: { turn: 1 }, ...over })
- it('keeps unexpected request-header fields observable in comparisons', () => {
- const request = (system: boolean) => event({
- type: 'request/header',
- data: { header: { config: { model: 'mock' }, ...(system ? { system: 'unexpected prompt' } : {}) } },
- })
- for (const normalize of [normalizeSessionLog, normalizeSessionSnapshot]) {
- const actual = normalize(`${header({})}\n${request(true)}\n`, ctx)
- expect(actual).toContain('"system":"unexpected prompt"')
- expect(actual).not.toEqual(normalize(`${header({})}\n${request(false)}\n`, ctx))
- }
- })
- it('zeroes the header createdAt', () => {
- const out = normalizeSessionLog(`${header({})}\n`, ctx)
- expect(out).toContain('"createdAt":0')
- expect(out).not.toContain('123')
- })
- it('preserves event sequence and zeroes event time', () => {
- const out = normalizeSessionLog(`${header({})}\n${event({ seq: 7, time: 999 })}\n`, ctx)
- expect(out).toContain('"time":0')
- expect(out).toContain('"seq":7')
- expect(out).not.toContain('999')
- })
- it('normalizes a projected event without adding a persistence envelope', () => {
- const projected = JSON.stringify({ type: 'turn/start', data: { turn: 1 } })
- const out = normalizeSessionLog(`${header({})}\n${projected}\n`, ctx)
- expect(JSON.parse(out.trimEnd().split('\n')[1] ?? '{}')).toStrictEqual({
- type: 'turn/start',
- data: { turn: 1 },
- })
- })
- it('scrubs cwd and session id deep inside event data', () => {
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: { content: [{ type: 'text', text: `wrote ${ctx.cwd}/proof.txt` }] },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('{{cwd}}')
- expect(out).not.toContain(ctx.cwd)
- })
- it('scrubs cwd at file URI and chained-punctuation boundaries in event data', () => {
- const ev = JSON.stringify({
- type: 'tool/result',
- seq: 2,
- time: 5,
- data: {
- uri: `file://${ctx.cwd}/proof.txt`,
- punctuated: `${ctx.cwd}.,`,
- },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('file://{{cwd}}/proof.txt')
- expect(out).toContain('{{cwd}}.,')
- expect(out).not.toContain(`file://${ctx.cwd}`)
- })
- it('scrubs random local spill paths under the snapshot cwd', () => {
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: {
- content: [{
- type: 'text',
- text: `Full formatted result stored at: ${ctx.cwd}/.spill/session-c22bc3f1d2af/8a7b6c5d4e3f-bash.txt. Use read with offset/limit, or grep this path to search within it.`,
- }],
- },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('{{spillLocator:bash.txt}}')
- expect(out).not.toContain('session-c22bc3f1d2af')
- expect(out).not.toContain('8a7b6c5d4e3f')
- })
- it('scrubs macOS /private aliases for local spill paths', () => {
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: {
- content: [{
- type: 'text',
- text: `Full formatted result stored at: /private${ctx.cwd}/.spill/session-c22bc3f1d2af/8a7b6c5d4e3f-bash.txt. Use read with offset/limit, or grep this path to search within it.`,
- }],
- },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('{{spillLocator:bash.txt}}')
- expect(out).not.toContain('/private{{spillLocator')
- })
- it('scrubs macOS /private prefix on cwd-rooted fs tool result paths', () => {
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: {
- content: [{
- type: 'text',
- text: `The file /private${ctx.cwd}/config.txt has been updated successfully.`,
- }],
- },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('{{cwd}}/config.txt')
- expect(out).not.toContain('/private{{cwd}}')
- })
- it('scrubs fixed snapshot spill paths', () => {
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: {
- content: [{
- type: 'text',
- text: 'Full formatted result stored at: /tmp/dsh-acp-snapshot-spill/session-c22bc3f1d2af/8a7b6c5d4e3f-bash.txt. Use read with offset/limit, or grep this path to search within it.',
- }],
- },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('{{spillLocator:bash.txt}}')
- expect(out).not.toContain('/tmp/dsh-acp-snapshot-spill')
- })
- it('scrubs scenario-owned snapshot spill paths', () => {
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: {
- content: [{
- type: 'text',
- text: 'Full formatted result stored at: /tmp/dsh-acp-snap-012345678/session-c22bc3f1d2af/8a7b6c5d4e3f-bash.txt. Use read with offset/limit, or grep this path to search within it.',
- }],
- },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('{{spillLocator:bash.txt}}')
- expect(out).not.toContain('/tmp/dsh-acp-snap-012345678')
- })
- it('scrubs scenario-owned snapshot spill paths with Windows drive and separators', () => {
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: {
- content: [{
- type: 'text',
- text: String.raw`Full formatted result stored at: C:\t\dsh-acp-snap-012345678\session-c22bc3f1d2af\8a7b6c5d4e3f-bash.txt. Use read with offset/limit, or grep this path to search within it.`,
- }],
- },
- })
- const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
- expect(out).toContain('{{spillLocator:bash.txt}}')
- expect(out).not.toContain('C:\\t\\dsh-acp-snap-012345678')
- })
- it('shares cwd-rooted path handling with stdout normalization', () => {
- const windowsCtx: NormalizeContext = { sessionIds: [], cwd: String.raw`C:\work\snapshot` }
- const ev = JSON.stringify({
- type: 'tool/result', seq: 2, time: 5,
- data: { path: `${windowsCtx.cwd}\\nested\\proof.txt` },
- })
- expect(normalizeSessionLog(`${header({ cwd: windowsCtx.cwd })}\n${ev}\n`, windowsCtx))
- .toContain('{{cwd}}/nested/proof.txt')
- expect(normalizeSessionLog(`${header({ cwd: windowsCtx.cwd })}\n${ev}\n`, windowsCtx, { cwdPathMode: 'native' }))
- .toContain(String.raw`{{cwd}}\\nested\\proof.txt`)
- })
- it('scrubs the session id in the header', () => {
- const out = normalizeSessionLog(`${header({ id: ctx.sessionIds[0] })}\n`, ctx)
- expect(out).toContain('{{sessionId}}')
- })
- it('zeroes a hook/result durationMs (run-to-run noise) but keeps its decision', () => {
- const ev = JSON.stringify({
- type: 'hook/result', seq: 2, time: 5,
- data: { turn: 1, point: 'UserPromptSubmit', handlerId: 'h', decision: 'block', exitCode: 2, durationMs: 37 },
- })
- const out = normalizeSessionLog(`${header({})}\n${ev}\n`, ctx)
- expect(out).toContain('"durationMs":0')
- expect(out).not.toContain('37')
- expect(out).toContain('"decision":"block"') // the decision is the behavior — kept
- })
- it('preserves a packed chunk row\'s sequence, zeroes time, and zeroes volatile dt gaps', () => {
- const row = JSON.stringify({
- type: 'text-chunks', seq0: 7, time0: 999,
- data: { turn: 1, step: 1, index: 0, dt: [212, 27, 0], texts: ['a', 'b', 'c', 'd'] },
- })
- const out = normalizeSessionLog(`${header({})}\n${row}\n`, ctx)
- expect(out).toContain('"time0":0')
- expect(out).toContain('"dt":[0,0,0]')
- expect(out).toContain('"seq0":7')
- expect(out).toContain('"texts":["a","b","c","d"]')
- expect(out).not.toContain('999')
- expect(out).not.toContain('212')
- })
- it('normalizes timing inside an embedded Assistant stream and ignores opaque members', () => {
- const event = JSON.stringify({
- type: 'assistant/attempt',
- seq: 2,
- time: 9,
- data: {
- turn: 1,
- step: 1,
- stream: [
- null,
- 'opaque',
- { type: 'chunk', time: 8, chunk: { type: 'finish', reason: { kind: 'stop' } } },
- { type: 'usage', time: 7, time0: 6, dt: [5, 4], usage: { inputTokens: 1, outputTokens: 2 } },
- { type: 'chunk', time: 8, chunk: { type: 'finish', reason: { kind: 'stop' } } },
- ],
- },
- })
- const [, normalized] = normalizeSessionLog(`${header({})}\n${event}\n`, ctx)
- .trimEnd()
- .split('\n')
- .map(line => JSON.parse(line) as Record<string, unknown>)
- expect(normalized).toMatchObject({
- time: 0,
- data: {
- stream: [
- null,
- 'opaque',
- { time: 0 },
- { time: 0, time0: 0, dt: [0, 0] },
- { type: 'chunk', time: 0, chunk: { type: 'finish', reason: { kind: 'stop' } } },
- ],
- },
- })
- })
- it('normalizes a headerless packed-like stream record without decoding it', () => {
- const row = JSON.stringify({ type: 'text-chunks', seq0: 1, time0: 999, data: 'not-an-object' })
- const out = normalizeSessionLog(`${row}\n`, ctx)
- expect(out).toContain('"seq0":1')
- expect(out).toContain('"time0":0')
- })
- it('leaves a non-hook event durationMs untouched (only hook/result is scrubbed)', () => {
- const ev = JSON.stringify({ type: 'tool/result', seq: 2, time: 5, data: { durationMs: 88 } })
- const out = normalizeSessionLog(`${header({})}\n${ev}\n`, ctx)
- expect(out).toContain('"durationMs":88')
- })
- it('normalizes goal lifecycle clocks without scrubbing unrelated payload timestamps', () => {
- const goal = JSON.stringify({
- type: 'goal/change',
- seq: 2,
- time: 5,
- data: { operation: 'create', createdAt: 123, updatedAt: 124 },
- })
- const tool = JSON.stringify({ type: 'tool/result', seq: 3, time: 6, data: { createdAt: 125 } })
- const goalWithoutClocks = JSON.stringify({ type: 'goal/change', seq: 4, time: 7, data: { operation: 'resume' } })
- const out = normalizeSessionLog(`${header({})}\n${goal}\n${tool}\n${goalWithoutClocks}\n`, ctx)
- expect(out).toContain('"operation":"create","createdAt":0,"updatedAt":0')
- expect(out).toContain('"createdAt":125')
- expect(out).toContain('"operation":"resume"')
- })
- it('normalizes subagent catalog child creation clocks', () => {
- const catalog = JSON.stringify({
- type: 'subagent/catalog',
- seq: 2,
- time: 5,
- data: {
- version: 0,
- childId: 'child',
- childCreatedAt: 123,
- mode: 'one-shot',
- },
- })
- const out = normalizeSessionLog(`${header({})}\n${catalog}\n`, ctx)
- expect(out).toContain('"childCreatedAt":0')
- })
- it('handles complete envelopes when optional normalized fields are absent', () => {
- const bareHeader = JSON.stringify({ type: 'session', id: 's' })
- const bareHook = JSON.stringify({ type: 'hook/result', seq: 2, time: 5, data: { decision: 'allow' } })
- const nullDataHook = JSON.stringify({ type: 'hook/result', seq: 3, time: 6, data: null })
- const bareCatalog = JSON.stringify({
- type: 'subagent/catalog',
- seq: 4,
- time: 7,
- data: { version: 0 },
- })
- const out = normalizeSessionLog(`${bareHeader}\n${bareHook}\n${nullDataHook}\n${bareCatalog}\n`, ctx)
- expect(out).toContain('"decision":"allow"')
- expect(out).toContain('"version":0')
- expect(out).not.toContain('durationMs')
- })
- })
- describe('normalizeSessionSnapshot', () => {
- it('normalizes, scrubs, and projects each parsed body record', () => {
- const raw = [
- JSON.stringify({ type: 'session', version: 0, createdAt: 123, cwd: ctx.cwd }),
- JSON.stringify({
- type: 'system/message',
- seq: 6,
- time: 998,
- data: { turn: 1, step: 1, message: { role: 'system', content: [{ type: 'text', text: `work in ${ctx.cwd}` }] } },
- }),
- JSON.stringify({
- type: 'request/header',
- seq: 7,
- time: 999,
- data: { header: { tools: [{ name: 'tool' }] } },
- }),
- ].join('\n') + '\n'
- expect(normalizeSessionSnapshot(raw, ctx)).toBe([
- JSON.stringify({ type: 'session', version: 0, createdAt: 0, cwd: '{{cwd}}' }),
- JSON.stringify({
- type: 'system/message',
- data: { turn: 1, step: 1, message: { role: 'system', content: [{ type: 'text', text: '{{system}}' }] } },
- }),
- JSON.stringify({ type: 'request/header', data: { header: { tools: '{{tools}}' } } }),
- ].join('\n') + '\n')
- })
- it('normalizes an already-projected packed row', () => {
- const raw = [
- JSON.stringify({ type: 'session', version: 0 }),
- JSON.stringify({
- type: 'text-chunks',
- data: { turn: 1, step: 1, index: 0, dt: [9, 8], texts: ['a', 'b', 'c'] },
- }),
- ].join('\n') + '\n'
- expect(normalizeSessionSnapshot(raw, ctx)).toContain('"dt":[0,0]')
- })
- it('retains historical packed-row boundaries while normalizing their timing', () => {
- const raw = [
- JSON.stringify({ type: 'session', version: 0 }),
- JSON.stringify({
- type: 'text-chunks',
- data: { turn: 1, step: 1, index: 0, dt: [4, 5], texts: ['a', 'b', 'c'] },
- }),
- JSON.stringify({
- type: 'text-chunks',
- data: { turn: 1, step: 1, index: 0, dt: [6, 7], texts: ['d', 'e', 'f'] },
- }),
- ].join('\n') + '\n'
- expect(normalizeSessionSnapshot(raw, ctx)).toBe([
- JSON.stringify({ type: 'session', version: 0 }),
- JSON.stringify({
- type: 'text-chunks',
- data: { turn: 1, step: 1, index: 0, dt: [0, 0], texts: ['a', 'b', 'c'] },
- }),
- JSON.stringify({
- type: 'text-chunks',
- data: { turn: 1, step: 1, index: 0, dt: [0, 0], texts: ['d', 'e', 'f'] },
- }),
- '',
- ].join('\n'))
- })
- it('preserves adjacent catalog facts in parent event order', () => {
- const raw = [
- JSON.stringify({ type: 'session', version: 0 }),
- JSON.stringify({ type: 'tool/call', data: { callId: 'parallel' } }),
- JSON.stringify({
- type: 'subagent/catalog',
- data: { version: 0, childId: '{{session:3}}', childCreatedAt: 123, mode: 'one-shot' },
- }),
- JSON.stringify({
- type: 'subagent/catalog',
- data: { version: 0, childId: '{{session:2}}', childCreatedAt: 124, mode: 'one-shot' },
- }),
- JSON.stringify({ type: 'tool/result', data: { callId: 'parallel' } }),
- ].join('\n') + '\n'
- const normalized = normalizeSessionSnapshot(raw, ctx)
- expect(normalized.indexOf('{{session:3}}')).toBeLessThan(normalized.indexOf('{{session:2}}'))
- expect(normalized).toContain('"childCreatedAt":0')
- })
- it('preserves malformed catalog payloads', () => {
- const raw = [
- JSON.stringify({ type: 'session', version: 0 }),
- JSON.stringify({
- type: 'subagent/catalog',
- data: { version: 0, childId: '{{session:2}}', childCreatedAt: 1, mode: 'one-shot' },
- }),
- JSON.stringify({
- type: 'subagent/catalog',
- data: { version: 0, childId: 3, childCreatedAt: 3, mode: 'one-shot' },
- }),
- ].join('\n') + '\n'
- const normalized = normalizeSessionSnapshot(raw, ctx)
- expect(normalized).toContain('{{session:2}}')
- expect(normalized).toContain('"childId":3')
- })
- it.each([
- { sources: [0, 1] },
- { sources: [0, 2] },
- ])('preserves source references and catalog order: $sources', ({ sources }) => {
- const records = [
- { type: 'session', version: 2 },
- { type: 'tool/call', data: { callId: 'parallel' } },
- { type: 'subagent/catalog', data: { childId: 'child-z', childCreatedAt: 1, version: 0, mode: 'one-shot' } },
- { type: 'subagent/catalog', data: { childId: 'child-a', childCreatedAt: 2, version: 0, mode: 'one-shot' } },
- { type: 'tool/result', data: { callId: 'parallel' }, sourceEventSeqs: sources, surfaceOp: 'append' },
- ]
- const normalized = normalizeSessionSnapshot(records.map(record => JSON.stringify(record)).join('\n'), ctx)
- expect(normalized).toBe([
- records[0],
- records[1],
- { ...records[2], data: { ...records[2]?.data, childCreatedAt: 0 } },
- { ...records[3], data: { ...records[3]?.data, childCreatedAt: 0 } },
- records[4],
- ].map(record => JSON.stringify(record)).join('\n') + '\n')
- expect(normalizeSessionSnapshot(normalized, ctx)).toBe(normalized)
- })
- it('migrates and re-packs multi-session fixtures after relationship-preserving id redaction', () => {
- const raw = [
- JSON.stringify({ type: 'session', version: 0, id: '{{session:1}}', createdAt: 0, delegationDepth: 0 }),
- JSON.stringify({ type: 'turn/start', data: { turn: 1 } }),
- JSON.stringify({ type: 'step/start', data: { turn: 1, step: 1 } }),
- JSON.stringify({
- type: 'reasoning-chunks',
- data: { turn: 1, step: 1, index: 0, dt: [1, 2], texts: ['a', 'b', 'c'] },
- }),
- JSON.stringify({
- type: 'reasoning-chunks',
- data: { turn: 1, step: 1, index: 0, dt: [3, 4], texts: ['d', 'e', 'f'] },
- }),
- ].join('\n') + '\n'
- expect(normalizeSessionSnapshots([raw], ctx)).toEqual([[
- JSON.stringify({
- type: 'session', id: '{{session:1}}', createdAt: 0, isSeeded: false, delegationDepth: 0,
- }),
- JSON.stringify({ type: 'turn/start', data: { turn: 1 } }),
- JSON.stringify({ type: 'step/start', data: { turn: 1, step: 1 } }),
- JSON.stringify({
- type: 'system/message',
- data: {
- turn: 1,
- step: 1,
- message: {
- id: 'v2-to-v3-system-590b72aa4994fd6d3c6e61bb4bf5bf2f80bae0bc7564d388378ba4f51b816fd6',
- role: 'system',
- source: { kind: 'plugin', plugin: '@deepseek-ai/dsh-system-prompt' },
- content: [],
- },
- },
- surfaceOp: 'append',
- }),
- JSON.stringify({
- type: 'assistant/attempt',
- data: {
- turn: 1,
- step: 1,
- stream: [{
- type: 'reasoning-chunks',
- time0: 0,
- index: 0,
- dt: [0, 0, 0, 0, 0],
- texts: ['a', 'b', 'c', 'd', 'e', 'f'],
- }],
- },
- }),
- '',
- ].join('\n')])
- })
- it('normalizes an already-projected snapshot without a released-format field', () => {
- const raw = `${JSON.stringify({
- type: 'session',
- id: '11111111-2222-3333-4444-555555555555',
- createdAt: 9,
- })}\n`
- expect(normalizeSessionSnapshots([raw], { sessionIds: [], cwd: '/unused' })).toEqual([
- `${JSON.stringify({ type: 'session', id: '{{session:1}}', createdAt: 0 })}\n`,
- ])
- })
- it('rejects an empty snapshot before classifying its released format', () => {
- expect(() => normalizeSessionSnapshots(['\n'], { sessionIds: [], cwd: '/unused' }))
- .toThrow('session snapshot must start with a session header')
- })
- it('rejects a nonempty snapshot whose first record is not a session header', () => {
- const raw = `${JSON.stringify({ type: 'turn/start', data: { turn: 1 } })}\n`
- expect(() => normalizeSessionSnapshots([raw], { sessionIds: [], cwd: '/unused' }))
- .toThrow('session snapshot must start with a session header')
- })
- it('preserves delivery and captured-source generations after artifact migration', () => {
- const event = (version: number): string => JSON.stringify({
- type: 'session-log-deepseek/delivery-accepted',
- data: { sessionId: 's', throughSeq: 4, sessionFormatVersion: version },
- })
- expect(normalizeSessionFormatProvenance(event(0))).toBe(event(0))
- expect(normalizeSessionFormatProvenance(event(3))).not.toBe(normalizeSessionFormatProvenance(event(0)))
- })
- it('preserves opaque generation qualifiers and their lookalikes', () => {
- const raw = [
- JSON.stringify({
- type: 'session',
- id: '11111111-2222-3333-4444-555555555555',
- createdAt: 0,
- }),
- JSON.stringify({
- type: 'session-log-deepseek/delivery-accepted',
- data: { sessionFormatVersion: 1, throughSeq: 21, otherVersion: 8 },
- }),
- JSON.stringify({
- type: 'user/message',
- data: {
- role: 'user',
- content: [],
- source: {
- kind: 'session-reference',
- form: 'recall',
- version: 1,
- references: [
- null,
- 'opaque',
- [{ capturedFormatVersion: 6 }],
- { capturedFormatVersion: 1, otherVersion: 9 },
- ],
- },
- },
- }),
- JSON.stringify({
- type: 'assistant/message',
- data: {
- message: {
- role: 'assistant',
- content: [],
- source: [{ capturedFormatVersion: 7 }],
- },
- },
- }),
- JSON.stringify({
- type: 'custom/event',
- data: { capturedFormatVersion: 5, sessionFormatVersion: 4 },
- ignorable: true,
- }),
- '',
- ].join('\n')
- const [normalized] = normalizeSessionSnapshots([raw], { sessionIds: [], cwd: '/unused' })
- const [, delivery, captured, sourceLookalike, opaqueEvent] = normalized
- ?.trimEnd()
- .split('\n')
- .map(line => JSON.parse(line) as Record<string, unknown>) ?? []
- expect(delivery?.data).toEqual({ sessionFormatVersion: 1, throughSeq: 21, otherVersion: 8 })
- expect(captured?.data).toMatchObject({
- source: {
- references: [
- null,
- 'opaque',
- [{ capturedFormatVersion: 6 }],
- { capturedFormatVersion: 1, otherVersion: 9 },
- ],
- },
- })
- expect(sourceLookalike?.data).toEqual({
- message: {
- role: 'assistant',
- content: [],
- source: [{ capturedFormatVersion: 7 }],
- },
- })
- expect(opaqueEvent?.data).toEqual({ capturedFormatVersion: 5, sessionFormatVersion: 4 })
- })
- it('preserves an unexpected session-reference payload instead of omitting its fields', () => {
- const raw = JSON.stringify({
- type: 'user/message',
- data: { source: { kind: 'session-reference', form: 'recall', version: 1, references: {} } },
- })
- expect(normalizeSessionFormatProvenance(raw)).toBe(raw)
- })
- it('keeps session-reference lookalikes outside Message source positions unchanged', () => {
- const lookalike = [
- JSON.stringify({ type: 'session', version: 1, id: 's', createdAt: 0, delegationDepth: 0 }),
- JSON.stringify({
- type: 'custom/event',
- data: {
- meta: {
- kind: 'session-reference',
- form: 'recall',
- version: 1,
- references: [{ capturedFormatVersion: 7 }],
- },
- },
- ignorable: true,
- }),
- '',
- ].join('\n')
- const normalized = normalizeSessionFormatProvenance(lookalike).split('\n')
- expect(JSON.parse(normalized[0] as string)).not.toHaveProperty('version')
- expect(normalized[1]).toBe(lookalike.split('\n')[1])
- })
- it('projects persisted provenance ranges back to logical seq arrays', () => {
- const raw = [
- JSON.stringify({ type: 'session', version: 0 }),
- JSON.stringify({
- type: 'assistant/message',
- sourceEventSeqs: [[1, 3], 5],
- surfaceOp: 'append',
- data: { turn: 1, step: 1 },
- }),
- ].join('\n') + '\n'
- expect(normalizeSessionSnapshot(raw, ctx)).toContain('"sourceEventSeqs":[1,2,3,5]')
- })
- it('rejects headerless input', () => {
- expect(() => normalizeSessionSnapshot('{"type":"turn/start"}\n', ctx))
- .toThrow('session snapshot must start with a session header')
- })
- })
- describe('tokenizeSessionFixtureCwd', () => {
- it.each([
- {
- name: 'macOS',
- context: {
- sessionIds: [],
- cwd: '/var/folders/2g/snapshot/T/acp-snap-cwd-abc123',
- cwdAliases: ['/private/var/folders/2g/snapshot/T/acp-snap-cwd-abc123'],
- },
- reportedCwd: '/private/var/folders/2g/snapshot/T/acp-snap-cwd-abc123',
- },
- {
- name: 'Linux',
- context: {
- sessionIds: [],
- cwd: '/tmp/acp-snap-cwd-abc123',
- },
- reportedCwd: '/tmp/acp-snap-cwd-abc123',
- },
- {
- name: 'Windows',
- context: {
- sessionIds: [],
- cwd: String.raw`C:\Users\runner\AppData\Local\Temp\acp-snap-cwd-abc123`,
- },
- reportedCwd: String.raw`C:\Users\runner\AppData\Local\Temp\acp-snap-cwd-abc123`,
- },
- ])('stores $name temporary workspaces with one portable root token', ({ context, reportedCwd }) => {
- const raw = [
- JSON.stringify({ type: 'session', id: 's', createdAt: 1, cwd: context.cwd }),
- JSON.stringify({
- type: 'tool/result',
- seq: 1,
- time: 2,
- data: {
- content: [{
- type: 'text',
- text: `wrote ${reportedCwd}/proof.txt. alias /different/root/acp-snap-cwd-abc123/alias.txt. cwd ${context.cwd}. Next; kept ${context.cwd}-backup, ${context.cwd}.backup, and /tmp/authored.txt`,
- }],
- },
- }),
- '',
- ].join('\n')
- const out = tokenizeSessionFixtureCwd(raw)
- const result = JSON.parse(out.split('\n')[1] as string) as {
- data: { content: { text: string }[] }
- }
- const resultText = (result.data.content[0] as { text: string }).text
- expect(out).toContain('"cwd":"{{cwd}}"')
- expect(resultText).toContain('wrote {{cwd}}/proof.txt')
- expect(resultText).toContain('alias {{cwd}}/alias.txt')
- expect(resultText).toContain('cwd {{cwd}}. Next')
- expect(resultText).toContain(`${context.cwd}-backup`)
- expect(resultText).toContain(`${context.cwd}.backup`)
- expect(resultText).toContain('/tmp/authored.txt')
- expect(resultText).not.toContain(`${reportedCwd}/proof.txt`)
- expect(tokenizeSessionFixtureCwd(out)).toBe(out)
- })
- it('collapses a residual macOS realpath prefix around an existing cwd token', () => {
- const raw = [
- JSON.stringify({ type: 'session', id: 's', createdAt: 1, cwd: '{{cwd}}' }),
- JSON.stringify({
- type: 'tool/result',
- seq: 1,
- time: 2,
- data: { content: [{ type: 'text', text: 'wrote /private{{cwd}}/proof.txt' }] },
- }),
- '',
- ].join('\n')
- const out = tokenizeSessionFixtureCwd(raw)
- expect(out).toContain('wrote {{cwd}}/proof.txt')
- expect(out).not.toContain('/private{{cwd}}')
- expect(tokenizeSessionFixtureCwd(out)).toBe(out)
- })
- it('rejects a log without a session cwd', () => {
- expect(() => tokenizeSessionFixtureCwd('')).toThrow(
- 'acp-snapshot: cannot tokenize a cwd without a basename',
- )
- })
- })
- describe('extractSnapshotSpillPaths', () => {
- it.each([
- ['/tmp', '/'],
- ['/tmp', String.fromCharCode(92)],
- ['C:/t', String.fromCharCode(92)],
- ])('recognizes %s locators with %s separators in nested JSON omissions without scrubbing byte counts', (root, separator) => {
- const locator = `${root}/dsh-acp-snap-123456789/session-123456abcdef/abcdef123456-session-reference-1.txt`.replaceAll('/', separator)
- const notice = { sessionId: 'source', omittedBytes: 42, fullSnapshot: { status: 'saved', locator, bytes: 1234 } }
- const log = JSON.stringify({ type: 'user/message', data: { content: [{ type: 'text', text: JSON.stringify([notice]) }] } })
- const encodedLocator = JSON.stringify(JSON.stringify(locator).slice(1, -1)).slice(1, -1)
- expect(extractSnapshotSpillPaths(log)).toEqual(new Map([['session-reference-1.txt', encodedLocator]]))
- const normalized = normalizeSessionLog(log, ctx)
- const unrelated = '/tmp/unrelated/session-123456abcdef/abcdef123456-session-reference-1.txt'
- expect(normalizeSessionLog(log.replaceAll(encodedLocator, unrelated), ctx)).toContain(unrelated)
- const expectedNotice = { ...notice, fullSnapshot: { ...notice.fullSnapshot, locator: '{{spillLocator:session-reference-1.txt}}' } }
- expect(normalized).toBe(JSON.stringify({ type: 'user/message', data: { content: [{ type: 'text', text: JSON.stringify([expectedNotice]) }] } }) + '\n')
- expect(normalized).toContain('{{spillLocator:session-reference-1.txt}}')
- expect(normalized).toContain('omittedBytes\\":42')
- expect(normalized).toContain('bytes\\":1234')
- })
- it.each(['canonical', 'native'] as const)('normalizes nested Windows local spill locators with %s paths', (cwdPathMode) => {
- const locator = String.raw`{{cwd}}\.spill\session-123456abcdef\abcdef123456-session-reference-1.txt`
- const notice = { locator, unrelated: String.raw`C:\work\literal\file.txt`, regex: String.raw`\d+\w` }
- const log = JSON.stringify({ type: 'user/message', data: { text: JSON.stringify(notice) } })
- const expected = { ...notice, locator: '{{spillLocator:session-reference-1.txt}}' }
- expect(normalizeSessionLog(log, ctx, { cwdPathMode })).toBe(
- JSON.stringify({ type: 'user/message', data: { text: JSON.stringify(expected) } }) + '\n',
- )
- })
- it('maps each spill filename to its full matched path, last match wins per name', () => {
- const log = [
- 'Full formatted result stored at: /tmp/dsh-acp-snapshot-spill/session-c22bc3f1d2af/8a7b6c5d4e3f-bash.txt. Use read with offset/limit, or grep this path to search within it.',
- 'stale copy at /tmp/dsh-acp-snap-012345678/session-aaaaaaaaaaaa/bbbbbbbbbbbb-grep.txt then',
- 'fresh copy at /tmp/dsh-acp-snap-012345678/session-cccccccccccc/dddddddddddd-grep.txt then',
- ].join('\n')
- expect(extractSnapshotSpillPaths(log)).toEqual(new Map([
- ['bash.txt', '/tmp/dsh-acp-snapshot-spill/session-c22bc3f1d2af/8a7b6c5d4e3f-bash.txt'],
- ['grep.txt', '/tmp/dsh-acp-snap-012345678/session-cccccccccccc/dddddddddddd-grep.txt'],
- ]))
- })
- it('returns an empty map when the log carries no snapshot spill paths', () => {
- expect(extractSnapshotSpillPaths('no spill paths here, only /tmp/other.txt\n')).toEqual(new Map())
- })
- })
- /** One `system/message` record whose single text block carries the rendered prompt. */
- function systemMessageEvent(text: string, seq = 2): string {
- return JSON.stringify({
- type: 'system/message',
- seq,
- time: 9,
- data: {
- turn: 1,
- step: 1,
- message: {
- id: '11111111-1111-4111-8111-111111111111',
- role: 'system',
- content: text.length === 0 ? [] : [{ type: 'text', text }],
- source: { kind: 'plugin', plugin: '@deepseek-ai/dsh-system-prompt' },
- },
- },
- })
- }
- describe('scrubModelRequestBulk', () => {
- const headerLine = JSON.stringify({ type: 'session', version: 0, id: 's', createdAt: 1, cwd: '/w' })
- const headerEvent = (header: object) =>
- JSON.stringify({ type: 'request/header', seq: 3, time: 9, data: { header, reason: 'initial' } })
- it('replaces system/message text and header tools with tokens, keeping config and reason', () => {
- const ev = headerEvent({
- config: { model: 'm' },
- tools: [{ name: 'read', description: 'Read a file.', parameters: { type: 'object' } }],
- })
- const out = scrubModelRequestBulk(`${headerLine}\n${systemMessageEvent('You are an agent.\nBe brief.')}\n${ev}\n`)
- expect(out).toContain('"content":[{"type":"text","text":"{{system}}"}]')
- expect(out).toContain('"tools":"{{tools}}"')
- expect(out).toContain('"config":{"model":"m"}')
- expect(out).toContain('"reason":"initial"')
- expect(out).not.toContain('You are an agent')
- expect(out).not.toContain('Read a file')
- })
- it('keeps an absent tools field absent and an empty system prompt empty (presence is behavior)', () => {
- const out = scrubModelRequestBulk(`${headerLine}\n${systemMessageEvent('')}\n${headerEvent({ config: { model: 'm' } })}\n`)
- expect(out).not.toContain('{{system}}')
- expect(out).toContain('"content":[]')
- expect(out).not.toContain('{{tools}}')
- })
- it('leaves malformed records with no scrubbable payload byte-identical', () => {
- const headerless = JSON.stringify({ type: 'request/header', seq: 10, time: 9, data: { reason: 'initial' } })
- const nullData = JSON.stringify({ type: 'request/header', seq: 11, time: 9, data: null })
- const messageless = JSON.stringify({ type: 'system/message', seq: 12, time: 9, data: { turn: 1, step: 1 } })
- const textless = JSON.stringify({
- type: 'system/message', seq: 13, time: 9, data: { message: { content: [{ type: 'image', data: 'x' }] } },
- })
- const raw = `${headerLine}\n${headerless}\n${nullData}\n${messageless}\n${textless}\n`
- expect(scrubModelRequestBulk(raw)).toBe(raw)
- })
- it('passes every other line through byte-for-byte and is idempotent', () => {
- const other = JSON.stringify({ type: 'assistant/chunk', seq: 4, time: 9, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'hi' } } })
- const raw = `${headerLine}\n${systemMessageEvent('s')}\n${headerEvent({ config: { model: 'm' }, tools: [] })}\n${other}\n`
- const once = scrubModelRequestBulk(raw)
- expect(once.split('\n')[0]).toBe(headerLine)
- expect(once.split('\n')[3]).toBe(other)
- expect(scrubModelRequestBulk(once)).toBe(once)
- })
- })
- describe('scrubSessionSnapshot', () => {
- it('writes stable feedback clocks while retaining notes and version identity', () => {
- const input = [
- { type: 'session', id: 's' },
- { type: 'feedback/message-put', data: { item: { version: 'opaque-version', createdAt: 12, updatedAt: 34, note: 'keep 12' } } },
- { type: 'feedback/message-put', data: null },
- { type: 'feedback/message-put', data: { item: null } },
- { type: 'feedback/message-put', data: { item: {} } },
- ].map(record => JSON.stringify(record)).join('\n')
- const output = scrubSessionSnapshot(input)
- expect(output).toContain('"version":"opaque-version","createdAt":0,"updatedAt":0,"note":"keep 12"')
- expect(scrubSessionSnapshot(output)).toBe(output)
- })
- it('preserves the header while projecting and scrubbing each body record', () => {
- const header = ' {"type":"session","version":0,"id":"s","createdAt":7} '
- const system = systemMessageEvent('secret', 0)
- const request = JSON.stringify({
- type: 'request/header', seq: 1, time: 9,
- data: { header: { tools: [{ name: 'read' }] }, reason: 'initial' },
- })
- const event = JSON.stringify({
- type: 'turn/start', seq: 2, time: 10,
- data: { turn: 1, seq: 41, time: 42 },
- })
- expect(scrubSessionSnapshot(`${header}\n${system}\n${request}\n${event}\n`)).toBe([
- header,
- '{"type":"system/message","data":{"turn":1,"step":1,"message":{"id":"11111111-1111-4111-8111-111111111111","role":"system","content":[{"type":"text","text":"{{system}}"}],"source":{"kind":"plugin","plugin":"@deepseek-ai/dsh-system-prompt"}}}}',
- '{"type":"request/header","data":{"header":{"tools":"{{tools}}"},"reason":"initial"}}',
- '{"type":"turn/start","data":{"turn":1,"seq":41,"time":42}}',
- '',
- ].join('\n'))
- })
- it('rejects headerless input', () => {
- expect(() => scrubSessionSnapshot('{"type":"turn/start"}\n'))
- .toThrow('session snapshot must start with a session header')
- })
- })
- describe('scrubSystemPrompts', () => {
- it('scrubs only system/message text while keeping header tools verbatim', () => {
- const header = JSON.stringify({
- type: 'request/header', seq: 1, time: 2,
- data: { header: { tools: [{ name: 'read', description: 'full schema' }] }, reason: 'initial' },
- })
- const replaced = JSON.stringify({
- type: 'system/message', seq: 3, time: 4,
- surfaceOp: { op: 'replace', start: 0, end: 0 },
- sourceEventSeqs: [0],
- data: { turn: 1, step: 2, message: { role: 'system', content: [{ type: 'text', text: 'new prompt' }] } },
- })
- const out = scrubSystemPrompts(`${systemMessageEvent('full prompt', 0)}\n${header}\n${replaced}\n`)
- expect(out.match(/"text":"{{system}}"/g)).toHaveLength(2)
- expect(out).not.toContain('full prompt')
- expect(out).not.toContain('new prompt')
- expect(out).toContain('"surfaceOp":{"op":"replace","start":0,"end":0}')
- expect(out.split('\n')[1]).toBe(header)
- expect(scrubSystemPrompts(out)).toBe(out)
- })
- })
- describe('scrubToolSchemas', () => {
- it('scrubs only tool-schema payloads while keeping prompts verbatim', () => {
- const header = JSON.stringify({
- type: 'request/header', seq: 1, time: 2,
- data: {
- header: { tools: [{ name: 'read', description: 'full schema', parameters: { type: 'object' } }] },
- reason: 'initial',
- },
- })
- const changed = JSON.stringify({
- type: 'request/header', seq: 2, time: 3,
- data: { header: { tools: [{ name: 'grep', description: 'new schema' }] }, reason: 'change' },
- })
- const toolless = JSON.stringify({
- type: 'request/header', seq: 3, time: 4,
- data: { header: { config: { model: 'm' } }, reason: 'resume' },
- })
- const out = scrubToolSchemas(`${systemMessageEvent('full prompt', 0)}\n${header}\n${changed}\n${toolless}\n`)
- expect(out.match(/"tools":"{{tools}}"/g)).toHaveLength(2)
- expect(out).not.toContain('full schema')
- expect(out).not.toContain('new schema')
- expect(out).toContain('full prompt')
- expect(out.split('\n')[3]).toBe(toolless)
- expect(scrubToolSchemas(out)).toBe(out)
- })
- })
|