feat(tools): add maxChars to readFile with default hard cap#142
Merged
Conversation
Add `maxChars` to `ReadFileOptions` as a caller-controlled char limit, plus a module-level default cap so unbounded full-file reads are always protected, with a truncation notice appended to the content. Design - `const READ_FILE_MAX_CHARS = 50_000` — unexported local const in `files.ts` - `maxChars` defaults to `READ_FILE_MAX_CHARS` when not supplied - Applied **after** any line-range selection (works for both full and partial reads) - Truncation appends: `\n[file truncated: showing 50000 of 123456 chars]` - `maxChars` does **not** trigger `isPartialRead` Files to change 1. `src/utils/tools/filesystem/files.ts` - Add `const READ_FILE_MAX_CHARS = 50_000` - Add `maxChars?: number` to `ReadFileOptions` - Both the full-read and ranged-read paths produce a final `output` string; after that string is formed, apply `const cap = options.maxChars ?? READ_FILE_MAX_CHARS` and truncate with notice if `output.length > cap` before returning 2. `src/utils/tools/definitions.ts` - Add `maxChars` param to `TOOL.READ_FILE` schema with description noting the default cap 3. `src/utils/tools/dispatcher.ts` - Add `'maxChars'` to `numericArgs` validation - Pass `maxChars` to `readFile(...)` in the switch case 4. `src/utils/tools/filesystem/files.test.ts` - `maxChars` truncates full reads with notice - `maxChars` truncates line-range reads with notice - `maxChars` >= content length is a no-op (no notice) - Default cap truncates when no `maxChars` is provided and content exceeds `READ_FILE_MAX_CHARS` 5. `src/utils/tools/dispatcher.test.ts` - Validation rejects non-integer `maxChars` - Validation rejects `maxChars < 1` with a message that names `maxChars` (separate from the existing `startLine, endLine, and maxLines must be >= 1` message) - Execution: `read_file` passes `maxChars` through dispatcher and returns truncated content with notice
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is the motivation for this pull request?
Large files returned by
read_filewith no line range can silently consume a large portion of the model's context window. This adds a default hard cap so unbounded reads are always protected, while also exposingmaxCharsas a caller-controlled option for intentional bounded reads.plan.md
What is the current behavior?
readFilereturns the full file content with no upper bound when no line range is specified. The model must remember to usestartLine/maxLinesto avoid large reads.What is the new behavior?
READ_FILE_MAX_CHARS = 50_000constant caps all reads by default.maxCharsparameter is added toReadFileOptionsand exposed in theread_filetool schema.maxCharsis applied after any line-range selection, so it works for both full and partial reads.[file truncated: showing N of M chars].< 1values with a dedicated error message.Checklist: