Skip to content

Commit a987b9a

Browse files
authored
Merge pull request #3445 from jeanduplessis/fix/issue-1458-file-filter-limit
fix: apply file limit after .kilocodeignore filtering instead of before
2 parents 1de9e0e + 8065f7a commit a987b9a

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

.changeset/great-frogs-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"kilo-code": patch
3+
---
4+
5+
fix: apply file limit after .kilocodeignore filtering instead of before

src/core/environment/__tests__/getEnvironmentDetails.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ describe("getEnvironmentDetails", () => {
166166
expect(result).toContain("# Current Workspace Directory")
167167
expect(result).toContain("Files")
168168

169-
expect(listFiles).toHaveBeenCalledWith(mockCwd, true, 50)
169+
// kilocode_change start
170+
// Expecting 3x multiplier: 50 * 3 = 150
171+
expect(listFiles).toHaveBeenCalledWith(mockCwd, true, 150)
172+
// kilocode_change end
170173

171174
expect(formatResponse.formatFilesList).toHaveBeenCalledWith(
172175
mockCwd,
@@ -177,6 +180,31 @@ describe("getEnvironmentDetails", () => {
177180
)
178181
})
179182

183+
// kilocode_change start
184+
it("should NOT apply multiplier when showRooIgnoredFiles is true", async () => {
185+
mockProvider.getState.mockResolvedValue({
186+
...mockState,
187+
showRooIgnoredFiles: true, // No filtering happens - ignored files are just marked
188+
maxWorkspaceFiles: 50,
189+
})
190+
191+
const result = await getEnvironmentDetails(mockCline as Task, true)
192+
expect(result).toContain("# Current Workspace Directory")
193+
expect(result).toContain("Files")
194+
195+
// Should use normal limit without multiplier since no files will be filtered out
196+
expect(listFiles).toHaveBeenCalledWith(mockCwd, true, 50)
197+
198+
expect(formatResponse.formatFilesList).toHaveBeenCalledWith(
199+
mockCwd,
200+
["file1.ts", "file2.ts"],
201+
false,
202+
mockCline.rooIgnoreController,
203+
true, // showRooIgnoredFiles = true
204+
)
205+
})
206+
// kilocode_change end
207+
180208
it("should not include file details when includeFileDetails is false", async () => {
181209
await getEnvironmentDetails(mockCline as Task, false)
182210
expect(listFiles).not.toHaveBeenCalled()

src/core/environment/getEnvironmentDetails.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ import { OpenRouterHandler } from "../../api/providers/openrouter"
2626
import { TelemetryService } from "@roo-code/telemetry"
2727
import { t } from "../../i18n"
2828
import { NativeOllamaHandler } from "../../api/providers/native-ollama"
29+
30+
// Multiplier for fetching extra files when filtering is enabled to ensure enough non-ignored files; only applied when showRooIgnoredFiles is false.
31+
const FILE_LIST_OVER_FETCH_MULTIPLIER = 3
32+
33+
function trimFileList(fileListStr: string, maxFiles: number) {
34+
let lines = fileListStr.split("\n")
35+
if (lines.length <= maxFiles) {
36+
return fileListStr
37+
}
38+
39+
const lastLine = lines[lines.length - 1]
40+
if (lastLine.startsWith("(File list truncated.")) {
41+
// Remove last 3 items from lines (two empty lines and truncation message)
42+
lines = lines.slice(0, -3)
43+
}
44+
45+
// Truncate lines to maxFiles
46+
lines = lines.slice(0, maxFiles)
47+
48+
const truncationMsg =
49+
"(File list truncated. Use list_files on specific subdirectories if you need to explore further.)"
50+
51+
return lines.join("\n") + "\n\n" + truncationMsg
52+
}
2953
// kilocode_change end
3054

3155
export async function getEnvironmentDetails(cline: Task, includeFileDetails: boolean = false) {
@@ -282,9 +306,15 @@ export async function getEnvironmentDetails(cline: Task, includeFileDetails: boo
282306
if (maxFiles === 0) {
283307
details += "(Workspace files context disabled. Use list_files to explore if needed.)"
284308
} else {
285-
const [files, didHitLimit] = await listFiles(cline.cwd, true, maxFiles)
286309
const { showRooIgnoredFiles = false } = state ?? {}
287310

311+
// kilocode_change start
312+
// Only apply multiplier when filtering will remove files (showRooIgnoredFiles = false)
313+
// When showRooIgnoredFiles = true, ignored files are just marked with lock symbol, not removed
314+
const fetchLimit = showRooIgnoredFiles ? maxFiles : maxFiles * FILE_LIST_OVER_FETCH_MULTIPLIER
315+
const [files, didHitLimit] = await listFiles(cline.cwd, true, fetchLimit)
316+
// kilocode_change end
317+
288318
const result = formatResponse.formatFilesList(
289319
cline.cwd,
290320
files,
@@ -293,7 +323,14 @@ export async function getEnvironmentDetails(cline: Task, includeFileDetails: boo
293323
showRooIgnoredFiles,
294324
)
295325

296-
details += result
326+
// kilocode_change start
327+
if (!showRooIgnoredFiles) {
328+
// Trim because we over-fetched
329+
details += trimFileList(result, maxFiles)
330+
} else {
331+
details += result
332+
}
333+
// kilocode_change end
297334
}
298335
}
299336
}

0 commit comments

Comments
 (0)