fix: exclude test files from AI context window - #538
Conversation
Closes piyushdotcomm#528 Test files (.test.*, .spec.*) can be very large and are rarely useful in the general AI context window for code generation and Chat. This commit excludes them in collectFilePaths(), saving tokens and improving context relevance.
|
Warning Review limit reached
Next review available in: 10 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
👋 Thanks for opening a PR, @Somil450!Your PR has entered the 🚦 PR Review Pipeline.
What happens next
A pipeline status comment will appear below and update automatically as your PR progresses. While you wait
This comment is posted only once. |
PR Summary by QodoExclude .test/.spec files from AI fileTree context
AI Description
Diagram
High-Level Assessment
Files changed (1)
|
Code Review by Qodo
Context used✅ Compliance rules (platform):
22 rules 1. Incomplete test filtering
|
| const fileName = `${item.filename}${ext}`; | ||
| if (fileName.includes(".test.") || fileName.includes(".spec.")) continue; | ||
| paths.push(prefix ? `${prefix}/${fileName}` : fileName); |
There was a problem hiding this comment.
1. Incomplete test filtering 🐞 Bug ≡ Correctness
collectFilePaths() only skips files whose basename contains ".test." or ".spec.", so test-support files in common locations like "tests/setup.ts" or "__mocks__/*" will still be included in the AI fileTree and keep consuming context tokens. This can reduce the effectiveness of the PR’s goal for projects that organize tests by directory rather than filename suffix.
Agent Prompt
### Issue description
`collectFilePaths()` currently filters only by `fileName.includes(".test.")` / `fileName.includes(".spec.")`, which won’t exclude test-support files that don’t use those suffixes (e.g., `tests/setup.ts`, files under `__mocks__/`). Since `AIChatPanel` sends `collectFilePaths(...).join("\n")` as `fileTree` system context, these files will still be sent and waste tokens.
### Issue Context
The PR intends to exclude test files from the AI context window, but the current rule set is filename-only and does not consider common test directory conventions.
### Fix Focus Areas
- modules/playground/hooks/useAI.ts[145-157]
- modules/playground/components/ai-chat-panel.tsx[83-134]
### Implementation notes
- Compute `fullPath = prefix ? `${prefix}/${fileName}` : fileName` first, and filter using a single predicate against `fullPath.toLowerCase()`.
- Extend the predicate to exclude common test directory segments (e.g., `"/tests/"`, `"/__tests__/"`, `"/__mocks__/"`) and optionally handle suffixes like `.test` / `.spec` when there is no further extension.
- Keep the final returned format unchanged (folder entries still end with `/`).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| const ext = item.fileExtension ? `.${item.fileExtension}` : ""; | ||
| paths.push(prefix ? `${prefix}/${item.filename}${ext}` : `${item.filename}${ext}`); | ||
| const fileName = `${item.filename}${ext}`; | ||
| if (fileName.includes(".test.") || fileName.includes(".spec.")) continue; |
There was a problem hiding this comment.
2. Misleading helper contract 🐞 Bug ⚙ Maintainability
The JSDoc for collectFilePaths() states it collects “all file paths,” but the implementation now intentionally omits some files, which can cause future callers to silently miss paths. This is especially risky because the helper is exported and used as a general-purpose utility.
Agent Prompt
### Issue description
`collectFilePaths()` is documented as collecting all file paths, but it now filters out some entries. This makes the function contract misleading and can cause subtle bugs if the helper is reused elsewhere expecting a complete enumeration.
### Issue Context
Filtering was added for AI context optimization, but the function name/JSDoc still describes unfiltered behavior.
### Fix Focus Areas
- modules/playground/hooks/useAI.ts[137-157]
### Implementation notes
Choose one:
- Update the JSDoc to explicitly document the exclusions (e.g., excludes files matching `.test.`/`.spec.` and any other patterns you add).
- Or, split responsibilities: keep `collectFilePaths()` as a true “collect all paths” helper and apply filtering at the call site (e.g., in `ai-chat-panel.tsx`) or via a new wrapper like `collectFilePathsForAIContext()`.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
Summary
Closes #528
Modifies
collectFilePaths()inuseAI.tsto filter out files containing.test.or.spec..Problem
The AI chat panel receives a
fileTreestring (a list of all files in the current template) as system context. Previously, this included test files. Test files (and their associated mock data) are often quite large and rarely useful for the AI's general context window unless explicitly asked for, leading to unnecessary token waste.Change
// modules/playground/hooks/useAI.ts } else { const ext = item.fileExtension ? `.${item.fileExtension}` : ""; - paths.push(prefix ? `${prefix}/${item.filename}${ext}` : `${item.filename}${ext}`); - + const fileName = `${item.filename}${ext}`; - + if (fileName.includes(".test.") || fileName.includes(".spec.")) continue; - + paths.push(prefix ? `${prefix}/${fileName}` : fileName); - } - ``` ## Files Changed - `modules/playground/hooks/useAI.ts` - 3 lines added, 1 line removed