Skip to content

Conversation

@tombee
Copy link
Owner

@tombee tombee commented Jan 7, 2026

Summary

Implements configurable line-based read limits for the SDK file tool (SPEC-4). This enables LLMs to request bounded file reads that prevent context window exhaustion when only a portion of a file is needed.

Changes

  • New Parameters: Added optional max_lines and offset parameters to file read operations
  • Memory Efficiency: Implemented streaming line-based reading using bufio.Scanner to avoid loading entire files into memory
  • Truncation Metadata: Added structured JSON metadata to read responses indicating truncation status, total lines, and line ranges
  • Backward Compatibility: Maintains existing behavior when no parameters are specified (unlimited reads)
  • Comprehensive Testing: Added 6 new test functions covering all scenarios including edge cases

Technical Details

Helper Functions

  • getIntParam(): Validates and extracts integer parameters with type checking
  • readWithLimits(): Memory-efficient bounded line reading using buffered scanner
  • countFileLines(): Efficiently counts total lines for truncation metadata
  • buildTruncationMetadata(): Constructs structured truncation information

Metadata Structure

{
  "truncated": true,
  "lines_shown": 500,
  "total_lines": 10000,
  "start_line": 0,
  "end_line": 499,
  "more_content": true
}

Test Coverage

  • Line-limited reads with various max_lines values
  • Offset-based reads skipping initial lines
  • Combined max_lines and offset parameters
  • Parameter validation (negative values, type errors)
  • Backward compatibility (unlimited reads)
  • Empty file handling with limits

All existing tests continue to pass, confirming backward compatibility.

Closes

- Add optional max_lines parameter to limit number of lines read
- Add optional offset parameter to skip lines before reading
- Implement memory-efficient line-based reading using bufio.Scanner
- Add structured truncation metadata to read responses
- Maintain full backward compatibility (unlimited reads by default)
- Add comprehensive test coverage for all scenarios

Closes #1
@tombee tombee merged commit 012b705 into main Jan 7, 2026
2 of 5 checks passed
@tombee tombee deleted the spec/SPEC-4 branch January 7, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add configurable file read limits to SDK tools

2 participants