normalize.spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. type NormalizeContext,
  4. normalizeSessionLog,
  5. normalizeStdout,
  6. scrubRequestHeaders,
  7. scrubSystemPrompts,
  8. scrubToolSchemas,
  9. } from '../src/normalize.ts'
  10. /**
  11. * Unit tests for the pure snapshot normalizers. Live as a *.spec.ts (runs in
  12. * the default unit gate) and import the normalizers directly.
  13. */
  14. const ctx: NormalizeContext = {
  15. sessionIds: ['11111111-2222-3333-4444-555555555555'],
  16. cwd: '/tmp/acp-snap-cwd-abc123',
  17. }
  18. describe('normalizeStdout', () => {
  19. it('rewrites JSON-RPC ids to a stable first-seen sequence', () => {
  20. const raw = [
  21. JSON.stringify({ jsonrpc: '2.0', id: 42, method: 'initialize' }),
  22. JSON.stringify({ jsonrpc: '2.0', id: 42, result: {} }),
  23. JSON.stringify({ jsonrpc: '2.0', id: 99, method: 'session/new' }),
  24. ].join('\n')
  25. const out = normalizeStdout(raw, ctx)
  26. expect(out).toContain('"id":1')
  27. expect(out).toContain('"id":2')
  28. expect(out).not.toContain('42')
  29. expect(out).not.toContain('99')
  30. })
  31. it('scrubs the cwd and session id anywhere they appear', () => {
  32. const raw = JSON.stringify({
  33. jsonrpc: '2.0', method: 'session/update',
  34. params: { sessionId: ctx.sessionIds[0], cwd: ctx.cwd, note: `at ${ctx.cwd}/x` },
  35. })
  36. const out = normalizeStdout(raw, ctx)
  37. expect(out).toContain('{{sessionId}}')
  38. expect(out).toContain('{{cwd}}')
  39. expect(out).not.toContain(ctx.cwd)
  40. expect(out).not.toContain(ctx.sessionIds[0] as string)
  41. })
  42. it('scrubs every filesystem spelling of the cwd longest-first', () => {
  43. const longCwd = String.raw`C:\Users\runneradmin\AppData\Local\Temp\acp-snapshot`
  44. const aliasedCtx: NormalizeContext = {
  45. sessionIds: [],
  46. cwd: String.raw`C:\Users\RUNNER~1\AppData\Local\Temp\acp-snapshot`,
  47. cwdAliases: [
  48. longCwd,
  49. String.raw`C:\Users\runneradmin\AppData\Local\Temp\acp`,
  50. ],
  51. }
  52. const raw = JSON.stringify({
  53. cwd: longCwd,
  54. path: `${longCwd}\\nested\\proof.txt`,
  55. })
  56. const frame = JSON.parse(normalizeStdout(raw, aliasedCtx)) as { cwd: string; path: string }
  57. expect(frame).toEqual({ cwd: '{{cwd}}', path: '{{cwd}}/nested/proof.txt' })
  58. })
  59. it('canonicalizes only cwd-rooted path separators', () => {
  60. const windowsCtx: NormalizeContext = {
  61. sessionIds: [],
  62. cwd: String.raw`C:\Users\runner\AppData\Local\Temp\acp-snapshot`,
  63. }
  64. const raw = JSON.stringify({
  65. jsonrpc: '2.0',
  66. method: 'session/update',
  67. params: {
  68. path: `${windowsCtx.cwd}\\nested\\proof.txt`,
  69. regex: String.raw`\d+\w+`,
  70. command: String.raw`printf "\\n"`,
  71. },
  72. })
  73. const frame = JSON.parse(normalizeStdout(raw, windowsCtx)) as {
  74. params: { path: string; regex: string; command: string }
  75. }
  76. expect(frame.params).toEqual({
  77. path: '{{cwd}}/nested/proof.txt',
  78. regex: String.raw`\d+\w+`,
  79. command: String.raw`printf "\\n"`,
  80. })
  81. })
  82. it('canonicalizes generated relative path fields and text markers without rewriting other text', () => {
  83. const raw = JSON.stringify({
  84. path: String.raw`nested\AGENTS.md`,
  85. content: String.raw`<path>.\nested\task.txt</path>
  86. Additional instructions from: nested\AGENTS.md`,
  87. regex: String.raw`\d+\w+`,
  88. })
  89. const frame = JSON.parse(normalizeStdout(raw, { sessionIds: [], cwd: '/unused' })) as {
  90. path: string
  91. content: string
  92. regex: string
  93. }
  94. expect(frame).toEqual({
  95. path: 'nested/AGENTS.md',
  96. content: '<path>./nested/task.txt</path>\nAdditional instructions from: nested/AGENTS.md',
  97. regex: String.raw`\d+\w+`,
  98. })
  99. })
  100. it('can preserve native cwd-rooted separators for a platform golden', () => {
  101. const windowsCtx: NormalizeContext = { sessionIds: [], cwd: String.raw`C:\work\snapshot` }
  102. const raw = JSON.stringify({ path: `${windowsCtx.cwd}\\nested\\proof.txt` })
  103. const frame = JSON.parse(normalizeStdout(raw, windowsCtx, { cwdPathMode: 'native' })) as { path: string }
  104. expect(frame.path).toBe(String.raw`{{cwd}}\nested\proof.txt`)
  105. })
  106. it('scrubs a stray UUID not in the known list', () => {
  107. const raw = JSON.stringify({ jsonrpc: '2.0', method: 'x', params: { id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' } })
  108. expect(normalizeStdout(raw, ctx)).toContain('{{sessionId}}')
  109. })
  110. it('leaves notification frames without an id untouched in id-space', () => {
  111. const raw = JSON.stringify({ jsonrpc: '2.0', method: 'session/update', params: {} })
  112. const out = normalizeStdout(raw, ctx)
  113. expect(out).not.toContain('"id"')
  114. })
  115. it('stabilizes only the top-level event timestamp and spill byte count in event-read text', () => {
  116. const raw = JSON.stringify({
  117. jsonrpc: '2.0',
  118. method: 'session/update',
  119. params: {
  120. update: {
  121. sessionUpdate: 'tool_call_update',
  122. content: [{
  123. type: 'content',
  124. content: {
  125. type: 'text',
  126. 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.)',
  127. },
  128. }],
  129. },
  130. },
  131. })
  132. const out = normalizeStdout(raw, ctx)
  133. expect(out).toContain('\\"time\\": {{eventTime}}')
  134. expect(out).toContain('\\"time\\": 31337')
  135. expect(out).toContain('\\"time\\": 424242')
  136. expect(out).toContain('Omitted {{eventOmittedBytes}} bytes')
  137. expect(out).not.toContain('1784876275593')
  138. expect(out).not.toContain('39387')
  139. })
  140. it('preserves event-like timestamps in unrelated output text', () => {
  141. const raw = JSON.stringify({
  142. jsonrpc: '2.0',
  143. method: 'session/update',
  144. params: {
  145. update: {
  146. sessionUpdate: 'tool_call_update',
  147. content: [{
  148. type: 'content',
  149. content: {
  150. type: 'text',
  151. 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.)',
  152. },
  153. }],
  154. },
  155. },
  156. })
  157. const out = normalizeStdout(raw, ctx)
  158. expect(out).toContain('1784876275593')
  159. expect(out).toContain('39387')
  160. expect(out).not.toContain('{{eventTime}}')
  161. expect(out).not.toContain('{{eventOmittedBytes}}')
  162. })
  163. it('throws on a non-JSON stdout line (the purity check)', () => {
  164. const raw = `${JSON.stringify({ jsonrpc: '2.0', id: 1 })}\noops a log leaked\n`
  165. expect(() => normalizeStdout(raw, ctx)).toThrow()
  166. })
  167. it('ignores blank lines', () => {
  168. const raw = `\n${JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'm' })}\n\n`
  169. expect(() => normalizeStdout(raw, ctx)).not.toThrow()
  170. })
  171. })
  172. describe('normalizeSessionLog', () => {
  173. const header = (over: object) => JSON.stringify({ type: 'session', version: 0, id: 's', createdAt: 123, ...over })
  174. const event = (over: object) => JSON.stringify({ type: 'turn/start', seq: 1, time: 999, data: { turn: 1 }, ...over })
  175. it('zeroes the header createdAt', () => {
  176. const out = normalizeSessionLog(`${header({})}\n`, ctx)
  177. expect(out).toContain('"createdAt":0')
  178. expect(out).not.toContain('123')
  179. })
  180. it('zeroes each event time but keeps seq', () => {
  181. const out = normalizeSessionLog(`${header({})}\n${event({ seq: 7, time: 999 })}\n`, ctx)
  182. expect(out).toContain('"time":0')
  183. expect(out).toContain('"seq":7') // seq is deterministic — NOT scrubbed
  184. expect(out).not.toContain('999')
  185. })
  186. it('scrubs cwd and session id deep inside event data', () => {
  187. const ev = JSON.stringify({
  188. type: 'tool/result', seq: 2, time: 5,
  189. data: { content: [{ type: 'text', text: `wrote ${ctx.cwd}/proof.txt` }] },
  190. })
  191. const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
  192. expect(out).toContain('{{cwd}}')
  193. expect(out).not.toContain(ctx.cwd)
  194. })
  195. it('scrubs random local spill paths under the snapshot cwd', () => {
  196. const ev = JSON.stringify({
  197. type: 'tool/result', seq: 2, time: 5,
  198. data: {
  199. content: [{
  200. type: 'text',
  201. 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.`,
  202. }],
  203. },
  204. })
  205. const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
  206. expect(out).toContain('{{spillLocator:bash.txt}}')
  207. expect(out).not.toContain('session-c22bc3f1d2af')
  208. expect(out).not.toContain('8a7b6c5d4e3f')
  209. })
  210. it('scrubs macOS /private aliases for local spill paths', () => {
  211. const ev = JSON.stringify({
  212. type: 'tool/result', seq: 2, time: 5,
  213. data: {
  214. content: [{
  215. type: 'text',
  216. 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.`,
  217. }],
  218. },
  219. })
  220. const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
  221. expect(out).toContain('{{spillLocator:bash.txt}}')
  222. expect(out).not.toContain('/private{{spillLocator')
  223. })
  224. it('scrubs fixed snapshot spill paths', () => {
  225. const ev = JSON.stringify({
  226. type: 'tool/result', seq: 2, time: 5,
  227. data: {
  228. content: [{
  229. type: 'text',
  230. 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.',
  231. }],
  232. },
  233. })
  234. const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
  235. expect(out).toContain('{{spillLocator:bash.txt}}')
  236. expect(out).not.toContain('/tmp/dsh-acp-snapshot-spill')
  237. })
  238. it('scrubs scenario-owned snapshot spill paths', () => {
  239. const ev = JSON.stringify({
  240. type: 'tool/result', seq: 2, time: 5,
  241. data: {
  242. content: [{
  243. type: 'text',
  244. 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.',
  245. }],
  246. },
  247. })
  248. const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
  249. expect(out).toContain('{{spillLocator:bash.txt}}')
  250. expect(out).not.toContain('/tmp/dsh-acp-snap-012345678')
  251. })
  252. it('scrubs scenario-owned snapshot spill paths with Windows drive and separators', () => {
  253. const ev = JSON.stringify({
  254. type: 'tool/result', seq: 2, time: 5,
  255. data: {
  256. content: [{
  257. type: 'text',
  258. 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.`,
  259. }],
  260. },
  261. })
  262. const out = normalizeSessionLog(`${header({ cwd: ctx.cwd })}\n${ev}\n`, ctx)
  263. expect(out).toContain('{{spillLocator:bash.txt}}')
  264. expect(out).not.toContain('C:\\t\\dsh-acp-snap-012345678')
  265. })
  266. it('shares cwd-rooted path handling with stdout normalization', () => {
  267. const windowsCtx: NormalizeContext = { sessionIds: [], cwd: String.raw`C:\work\snapshot` }
  268. const ev = JSON.stringify({
  269. type: 'tool/result', seq: 2, time: 5,
  270. data: { path: `${windowsCtx.cwd}\\nested\\proof.txt` },
  271. })
  272. expect(normalizeSessionLog(`${header({ cwd: windowsCtx.cwd })}\n${ev}\n`, windowsCtx))
  273. .toContain('{{cwd}}/nested/proof.txt')
  274. expect(normalizeSessionLog(`${header({ cwd: windowsCtx.cwd })}\n${ev}\n`, windowsCtx, { cwdPathMode: 'native' }))
  275. .toContain(String.raw`{{cwd}}\\nested\\proof.txt`)
  276. })
  277. it('scrubs the session id in the header', () => {
  278. const out = normalizeSessionLog(`${header({ id: ctx.sessionIds[0] })}\n`, ctx)
  279. expect(out).toContain('{{sessionId}}')
  280. })
  281. it('zeroes a hook/result durationMs (run-to-run noise) but keeps its decision', () => {
  282. const ev = JSON.stringify({
  283. type: 'hook/result', seq: 2, time: 5,
  284. data: { turn: 1, point: 'UserPromptSubmit', handlerId: 'h', decision: 'block', exitCode: 2, durationMs: 37 },
  285. })
  286. const out = normalizeSessionLog(`${header({})}\n${ev}\n`, ctx)
  287. expect(out).toContain('"durationMs":0')
  288. expect(out).not.toContain('37')
  289. expect(out).toContain('"decision":"block"') // the decision is the behavior — kept
  290. })
  291. it('zeroes a packed chunk row\'s time0 and dt gaps but keeps seq0 and payload', () => {
  292. const row = JSON.stringify({
  293. type: 'text-chunks', seq0: 7, time0: 999,
  294. data: { turn: 1, step: 1, index: 0, dt: [212, 27, 0], texts: ['a', 'b', 'c', 'd'] },
  295. })
  296. const out = normalizeSessionLog(`${header({})}\n${row}\n`, ctx)
  297. expect(out).toContain('"time0":0')
  298. expect(out).toContain('"dt":[0,0,0]')
  299. expect(out).toContain('"seq0":7') // seq0 is deterministic, like seq — NOT scrubbed
  300. expect(out).toContain('"texts":["a","b","c","d"]')
  301. expect(out).not.toContain('999')
  302. expect(out).not.toContain('212')
  303. })
  304. it('zeroes time0 even when a malformed row carries no dt array', () => {
  305. const row = JSON.stringify({ type: 'text-chunks', seq0: 1, time0: 999, data: 'not-an-object' })
  306. const out = normalizeSessionLog(`${header({})}\n${row}\n`, ctx)
  307. expect(out).toContain('"time0":0')
  308. expect(out).not.toContain('999')
  309. })
  310. it('leaves a non-hook event durationMs untouched (only hook/result is scrubbed)', () => {
  311. const ev = JSON.stringify({ type: 'tool/result', seq: 2, time: 5, data: { durationMs: 88 } })
  312. const out = normalizeSessionLog(`${header({})}\n${ev}\n`, ctx)
  313. expect(out).toContain('"durationMs":88')
  314. })
  315. it('tolerates records missing the volatile fields it would zero', () => {
  316. const bareHeader = JSON.stringify({ type: 'session', id: 's' })
  317. const timeless = JSON.stringify({ type: 'note', seq: 1 })
  318. const bareHook = JSON.stringify({ type: 'hook/result', seq: 2, time: 5, data: { decision: 'allow' } })
  319. const nullDataHook = JSON.stringify({ type: 'hook/result', seq: 3, time: 6, data: null })
  320. const out = normalizeSessionLog(`${bareHeader}\n${timeless}\n${bareHook}\n${nullDataHook}\n`, ctx)
  321. expect(out).toContain('"type":"note","seq":1')
  322. expect(out).toContain('"decision":"allow"')
  323. expect(out).not.toContain('durationMs')
  324. })
  325. })
  326. describe('scrubRequestHeaders', () => {
  327. const headerLine = JSON.stringify({ type: 'session', version: 0, id: 's', createdAt: 1, cwd: '/w' })
  328. const headerEvent = (header: object) =>
  329. JSON.stringify({ type: 'request/header', seq: 3, time: 9, data: { header, reason: 'initial' } })
  330. it('replaces header system and tools with tokens, keeping config and reason', () => {
  331. const ev = headerEvent({
  332. config: { model: 'm' },
  333. system: 'You are an agent.\nBe brief.',
  334. tools: [{ name: 'read', description: 'Read a file.', parameters: { type: 'object' } }],
  335. })
  336. const out = scrubRequestHeaders(`${headerLine}\n${ev}\n`)
  337. expect(out).toContain('"system":"{{system}}"')
  338. expect(out).toContain('"tools":"{{tools}}"')
  339. expect(out).toContain('"config":{"model":"m"}')
  340. expect(out).toContain('"reason":"initial"')
  341. expect(out).not.toContain('You are an agent')
  342. expect(out).not.toContain('Read a file')
  343. })
  344. it('keeps an absent system/tools absent (presence is behavior)', () => {
  345. const out = scrubRequestHeaders(`${headerLine}\n${headerEvent({ config: { model: 'm' } })}\n`)
  346. expect(out).not.toContain('{{system}}')
  347. expect(out).not.toContain('{{tools}}')
  348. })
  349. it('scrubs a header carrying only one of system/tools, leaving the other absent', () => {
  350. const systemOnly = scrubRequestHeaders(`${headerLine}\n${headerEvent({ system: 'secret prompt' })}\n`)
  351. expect(systemOnly).toContain('"system":"{{system}}"')
  352. expect(systemOnly).not.toContain('{{tools}}')
  353. const toolsOnly = scrubRequestHeaders(`${headerLine}\n${headerEvent({ tools: [{ name: 't' }] })}\n`)
  354. expect(toolsOnly).toContain('"tools":"{{tools}}"')
  355. expect(toolsOnly).not.toContain('{{system}}')
  356. })
  357. it('scrubs the header session prefix to one token per message, keeping the count', () => {
  358. const ev = headerEvent({
  359. config: { model: 'm' },
  360. messagePrefix: [
  361. { role: 'user', content: [{ type: 'text', text: 'workspace AGENTS digest' }] },
  362. { role: 'user', content: [{ type: 'text', text: 'skills catalog' }] },
  363. ],
  364. })
  365. const out = scrubRequestHeaders(`${headerLine}\n${ev}\n`)
  366. expect(out).toContain('"messagePrefix":["{{messagePrefix}}","{{messagePrefix}}"]')
  367. expect(out).not.toContain('AGENTS digest')
  368. expect(out).not.toContain('skills catalog')
  369. // Absence stays absent — a prefix-less header gains no token…
  370. expect(scrubRequestHeaders(`${headerLine}\n${headerEvent({ system: 's' })}\n`)).not.toContain('{{messagePrefix}}')
  371. // …and a non-array shape passes through untouched.
  372. const odd = JSON.stringify({ type: 'request/header', seq: 4, time: 9, data: { header: { config: { model: 'm' }, messagePrefix: 'weird' }, reason: 'initial' } })
  373. expect(scrubRequestHeaders(`${headerLine}\n${odd}\n`)).toContain('"messagePrefix":"weird"')
  374. })
  375. it('leaves malformed headers with no scrubbable payload byte-identical', () => {
  376. const headerless = JSON.stringify({ type: 'request/header', seq: 10, time: 9, data: { reason: 'initial' } })
  377. const nullData = JSON.stringify({ type: 'request/header', seq: 11, time: 9, data: null })
  378. const raw = `${headerLine}\n${headerless}\n${nullData}\n`
  379. expect(scrubRequestHeaders(raw)).toBe(raw)
  380. })
  381. it('passes every other line through byte-for-byte and is idempotent', () => {
  382. const other = JSON.stringify({ type: 'assistant/chunk', seq: 4, time: 9, data: { turn: 1, step: 1, chunk: { type: 'text-delta', index: 0, text: 'hi' } } })
  383. const raw = `${headerLine}\n${headerEvent({ config: { model: 'm' }, system: 's', tools: [] })}\n${other}\n`
  384. const once = scrubRequestHeaders(raw)
  385. expect(once.split('\n')[0]).toBe(headerLine)
  386. expect(once.split('\n')[2]).toBe(other)
  387. expect(scrubRequestHeaders(once)).toBe(once)
  388. })
  389. })
  390. describe('scrubSystemPrompts', () => {
  391. it('scrubs only system prompt payloads while keeping tools and prefixes verbatim', () => {
  392. const header = JSON.stringify({
  393. type: 'request/header', seq: 1, time: 2,
  394. data: {
  395. header: {
  396. system: 'full prompt',
  397. tools: [{ name: 'read', description: 'full schema' }],
  398. messagePrefix: [{ role: 'user', content: [{ type: 'text', text: 'full prefix' }] }],
  399. },
  400. reason: 'initial',
  401. },
  402. })
  403. const changed = JSON.stringify({
  404. type: 'request/header', seq: 2, time: 3,
  405. data: {
  406. header: {
  407. system: 'new prompt',
  408. tools: [{ name: 'read', description: 'changed schema' }],
  409. messagePrefix: [{ role: 'user', content: [{ type: 'text', text: 'changed prefix' }] }],
  410. },
  411. reason: 'change',
  412. },
  413. })
  414. const toolsOnly = JSON.stringify({
  415. type: 'request/header', seq: 3, time: 4,
  416. data: { header: { tools: [{ name: 'read', description: 'schema only' }] }, reason: 'resume' },
  417. })
  418. const out = scrubSystemPrompts(`${header}\n${changed}\n${toolsOnly}\n`)
  419. expect(out).toContain('"system":"{{system}}"')
  420. expect(out).not.toContain('full prompt')
  421. expect(out).not.toContain('new prompt')
  422. expect(out).toContain('full schema')
  423. expect(out).toContain('full prefix')
  424. expect(out).toContain('changed schema')
  425. expect(out).toContain('changed prefix')
  426. expect(out.split('\n')[2]).toBe(toolsOnly)
  427. expect(scrubSystemPrompts(out)).toBe(out)
  428. })
  429. })
  430. describe('scrubToolSchemas', () => {
  431. it('scrubs only tool-schema payloads while keeping prompts and prefixes verbatim', () => {
  432. const header = JSON.stringify({
  433. type: 'request/header', seq: 1, time: 2,
  434. data: {
  435. header: {
  436. system: 'full prompt',
  437. tools: [{ name: 'read', description: 'full schema', parameters: { type: 'object' } }],
  438. messagePrefix: [{ role: 'user', content: [{ type: 'text', text: 'full prefix' }] }],
  439. },
  440. reason: 'initial',
  441. },
  442. })
  443. const changed = JSON.stringify({
  444. type: 'request/header', seq: 2, time: 3,
  445. data: {
  446. header: {
  447. system: 'new prompt',
  448. tools: [{ name: 'grep', description: 'new schema' }],
  449. messagePrefix: [{ role: 'user', content: [{ type: 'text', text: 'changed prefix' }] }],
  450. },
  451. reason: 'change',
  452. },
  453. })
  454. const systemOnly = JSON.stringify({
  455. type: 'request/header', seq: 3, time: 4,
  456. data: { header: { system: 'prompt only' }, reason: 'resume' },
  457. })
  458. const out = scrubToolSchemas(`${header}\n${changed}\n${systemOnly}\n`)
  459. expect(out.match(/"tools":"{{tools}}"/g)).toHaveLength(2)
  460. expect(out).not.toContain('full schema')
  461. expect(out).not.toContain('new schema')
  462. expect(out).toContain('full prompt')
  463. expect(out).toContain('new prompt')
  464. expect(out).toContain('full prefix')
  465. expect(out).toContain('changed prefix')
  466. expect(out.split('\n')[2]).toBe(systemOnly)
  467. expect(scrubToolSchemas(out)).toBe(out)
  468. })
  469. })