Skip to content

Commit 25c2b3a

Browse files
authored
Fix read_files normalized path results (#699)
1 parent 5c2d27b commit 25c2b3a

3 files changed

Lines changed: 64 additions & 7 deletions

File tree

common/src/tools/params/tool/read-files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const inputSchema = z
2828
.string()
2929
.min(1, 'Paths cannot be empty')
3030
.describe(
31-
`File path to read relative to the **project root**. Absolute file paths will not work.`,
31+
`File path to read. Prefer paths relative to the **project root**; absolute paths inside the project are accepted, but paths outside the project will not work.`,
3232
),
3333
),
3434
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, expect, test } from 'bun:test'
2+
3+
import { getFileReadingUpdates } from '../get-file-reading-updates'
4+
5+
describe('getFileReadingUpdates', () => {
6+
test('returns files keyed by the requested paths', async () => {
7+
const files = await getFileReadingUpdates({
8+
requestedFiles: ['src/index.ts'],
9+
requestFiles: async () => ({
10+
'src/index.ts': 'console.log("hello")',
11+
}),
12+
})
13+
14+
expect(files).toEqual([
15+
{
16+
path: 'src/index.ts',
17+
content: 'console.log("hello")',
18+
},
19+
])
20+
})
21+
22+
test('keeps files returned under normalized paths', async () => {
23+
const files = await getFileReadingUpdates({
24+
requestedFiles: ['/project/src/index.ts', './src/util.ts'],
25+
requestFiles: async () => ({
26+
'src/index.ts': 'console.log("hello")',
27+
'src/util.ts': 'export const util = true',
28+
}),
29+
})
30+
31+
expect(files).toEqual([
32+
{
33+
path: 'src/index.ts',
34+
content: 'console.log("hello")',
35+
},
36+
{
37+
path: 'src/util.ts',
38+
content: 'export const util = true',
39+
},
40+
])
41+
})
42+
43+
test('omits null file results', async () => {
44+
const files = await getFileReadingUpdates({
45+
requestedFiles: ['missing.ts', 'src/index.ts'],
46+
requestFiles: async () => ({
47+
'missing.ts': null,
48+
'src/index.ts': 'content',
49+
}),
50+
})
51+
52+
expect(files).toEqual([
53+
{
54+
path: 'src/index.ts',
55+
content: 'content',
56+
},
57+
])
58+
})
59+
})

packages/agent-runtime/src/get-file-reading-updates.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ export async function getFileReadingUpdates(params: {
1616
const allFilePaths = uniq(requestedFiles)
1717
const loadedFiles = await requestFiles({ filePaths: allFilePaths })
1818

19-
const addedFiles = allFilePaths
20-
.filter(
21-
(path) => loadedFiles[path] != null && loadedFiles[path] !== undefined,
22-
)
23-
.map((path) => ({
19+
const addedFiles = Object.entries(loadedFiles)
20+
.filter((entry): entry is [string, string] => typeof entry[1] === 'string')
21+
.map(([path, content]) => ({
2422
path,
25-
content: loadedFiles[path]!,
23+
content,
2624
}))
2725

2826
return addedFiles

0 commit comments

Comments
 (0)