|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { buildToolCallRecord, readPathFor } from './record-tool-call.js'; |
| 3 | + |
| 4 | +// Provenance: what a change was derived from, so a wrong generated file leads |
| 5 | +// back to the input that produced it. |
| 6 | +describe('derivedFrom', () => { |
| 7 | + const base = { cwd: '/work/repo', intent: 'regenerate the client' }; |
| 8 | + |
| 9 | + it('records the reads that preceded the write', () => { |
| 10 | + const record = buildToolCallRecord({ |
| 11 | + ...base, |
| 12 | + tool: 'Write', |
| 13 | + input: { file_path: '/work/repo/src/client.ts', content: '…' }, |
| 14 | + readPaths: ['/work/repo/schema.json', '/work/repo/config/gen.yaml'], |
| 15 | + }); |
| 16 | + expect(record?.derivedFrom).toEqual(['config/gen.yaml', 'schema.json']); |
| 17 | + }); |
| 18 | + |
| 19 | + it('is absent when the turn read nothing', () => { |
| 20 | + // Not an empty array: a field that is always present is a field that stops |
| 21 | + // being read. |
| 22 | + const record = buildToolCallRecord({ |
| 23 | + ...base, |
| 24 | + tool: 'Write', |
| 25 | + input: { file_path: 'a.ts', content: 'x' }, |
| 26 | + }); |
| 27 | + expect(record?.derivedFrom).toBeUndefined(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('excludes the file being written', () => { |
| 31 | + // Edit reads its own target by construction; listing it would make every |
| 32 | + // edit look self-derived. |
| 33 | + const record = buildToolCallRecord({ |
| 34 | + ...base, |
| 35 | + tool: 'Edit', |
| 36 | + input: { file_path: '/work/repo/src/a.ts', old_string: 'x', new_string: 'y' }, |
| 37 | + readPaths: ['/work/repo/src/a.ts', '/work/repo/schema.json'], |
| 38 | + }); |
| 39 | + expect(record?.derivedFrom).toEqual(['schema.json']); |
| 40 | + }); |
| 41 | + |
| 42 | + it('normalizes to workspace-relative and dedupes', () => { |
| 43 | + const record = buildToolCallRecord({ |
| 44 | + ...base, |
| 45 | + tool: 'Write', |
| 46 | + input: { file_path: 'out.ts', content: 'x' }, |
| 47 | + readPaths: ['/work/repo/schema.json', 'schema.json', '/work/repo/./schema.json'], |
| 48 | + }); |
| 49 | + expect(record?.derivedFrom).toEqual(['schema.json']); |
| 50 | + }); |
| 51 | + |
| 52 | + it('attaches to Bash too — a command has inputs even without a declarable output', () => { |
| 53 | + const record = buildToolCallRecord({ |
| 54 | + ...base, |
| 55 | + tool: 'Bash', |
| 56 | + input: { command: 'make generate' }, |
| 57 | + readPaths: ['/work/repo/Makefile'], |
| 58 | + }); |
| 59 | + expect(record?.derivedFrom).toEqual(['Makefile']); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('readPathFor', () => { |
| 64 | + it('reports a Read', () => { |
| 65 | + expect(readPathFor('Read', { file_path: '/a/b.ts' })).toBe('/a/b.ts'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('ignores Grep and Glob', () => { |
| 69 | + // They take a search *root* and return many paths. Calling the root an |
| 70 | + // input claims a derivation the turn did not make; enumerating every hit |
| 71 | + // drowns the real inputs in whatever the search swept up. |
| 72 | + expect(readPathFor('Grep', { path: '/a' })).toBeUndefined(); |
| 73 | + expect(readPathFor('Glob', { path: '/a' })).toBeUndefined(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('ignores writes and anything without a path', () => { |
| 77 | + expect(readPathFor('Write', { file_path: '/a' })).toBeUndefined(); |
| 78 | + expect(readPathFor('Read', {})).toBeUndefined(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments