Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(file-browser): only truncate beyond the file cap

Zhao Dengfeng4a3330a94ded
diff --git a/packages/file-browser-core/src/__tests__/listAllFilesWalk.test.ts b/packages/file-browser-core/src/__tests__/listAllFilesWalk.test.ts
index 5615097c3b..36b17f4261 100644
--- a/packages/file-browser-core/src/__tests__/listAllFilesWalk.test.ts
+++ b/packages/file-browser-core/src/__tests__/listAllFilesWalk.test.ts
@@ -53,6 +53,21 @@ describe('listAllFilesWalk', () => {
expect(res.files.length).toBe(2);
});
+ it('only truncates after finding a file beyond the cap', async () => {
+ const exact = path.join(workdir, 'exact');
+ await mkdir(exact);
+ await writeFile(path.join(exact, 'a.ts'), 'a\n', 'utf8');
+ await writeFile(path.join(exact, 'b.ts'), 'b\n', 'utf8');
+
+ const atCap = await listAllFilesWalk({ workdir: exact, cap: 2 });
+ expect(atCap.files).toHaveLength(2);
+ expect(atCap.truncated).toBe(false);
+
+ const overCap = await listAllFilesWalk({ workdir: exact, cap: 1 });
+ expect(overCap.files).toHaveLength(1);
+ expect(overCap.truncated).toBe(true);
+ });
+
it('skips symlinks (no follow, no loops)', async () => {
// 指回 workdir 自身的目录链接:follow 会造环,必须跳过。
// 'junction':Windows 无特权也能建目录链接(POSIX 下该参数被忽略,行为不变)。
diff --git a/packages/file-browser-core/src/__tests__/ripgrep-exit-handling.test.ts b/packages/file-browser-core/src/__tests__/ripgrep-exit-handling.test.ts
index 295f6aa61d..27f2d29ab1 100644
--- a/packages/file-browser-core/src/__tests__/ripgrep-exit-handling.test.ts
+++ b/packages/file-browser-core/src/__tests__/ripgrep-exit-handling.test.ts
@@ -128,6 +128,17 @@ describe('listAllFiles error handling', () => {
expect(result.files).toEqual(['file1.ts', 'file2.js']);
});
+ it('does not kill rg or truncate when the file count exactly reaches cap', async () => {
+ const child = createFakeChild(0, ['file1.ts', 'file2.ts']);
+ spawnHandler = () => child;
+
+ const { listAllFiles } = await import('../listAllFiles.js');
+ const result = await listAllFiles({ rgPath: '/fake/rg', workdir: '/tmp/work', cap: 2 });
+ expect(result.files).toEqual(['file1.ts', 'file2.ts']);
+ expect(result.truncated).toBe(false);
+ expect(child.kill).not.toHaveBeenCalled();
+ });
+
it('resolves when truncated (rg killed by us, exit code non-zero is OK)', async () => {
// When we kill rg after cap, it exits non-zero — should still resolve
spawnHandler = () => createFakeChild(1, ['a.ts', 'b.ts']);
@@ -146,6 +157,8 @@ describe('listAllFiles error handling', () => {
const { listAllFiles } = await import('../listAllFiles.js');
const resultPromise = listAllFiles({ rgPath: '/fake/rg', workdir: '/tmp/work', cap: 1 });
(child.stdout as PassThrough).write('a.ts\n');
+ expect(child.kill).not.toHaveBeenCalled();
+ (child.stdout as PassThrough).write('b.ts\n');
expect(child.kill).toHaveBeenCalledWith('SIGTERM');
await vi.advanceTimersByTimeAsync(200);
diff --git a/packages/file-browser-core/src/listAllFiles.ts b/packages/file-browser-core/src/listAllFiles.ts
index 5f4a71ff7a..5d51e7223b 100644
--- a/packages/file-browser-core/src/listAllFiles.ts
+++ b/packages/file-browser-core/src/listAllFiles.ts
@@ -113,11 +113,13 @@ export function listAllFiles(args: ListAllFilesArgs): Promise<ListAllFilesResult
// rg 偶尔有 `./` 前缀(取决于 cwd 形式),剥掉。
const clean = normalized.startsWith('./') ? normalized.slice(2) : normalized;
if (!clean) return;
- files.push(clean);
+ // 只有第 cap + 1 个文件才证明清单被截断;恰好 cap 个自然结束不算超限。
if (files.length >= cap) {
truncated = true;
- killRg('cap reached');
+ killRg('cap exceeded');
+ return;
}
+ files.push(clean);
});
child.on('error', (err) => {
@@ -208,8 +210,11 @@ export async function listAllFilesWalk(args: ListAllFilesWalkArgs): Promise<List
if (isDir) {
await walk(childRel);
} else if (ent.isFile()) {
+ if (files.length >= cap) {
+ truncated = true;
+ return;
+ }
files.push(childRel);
- if (files.length >= cap) truncated = true;
}
}
};