Skip to content

Conversation

@tombee
Copy link
Owner

@tombee tombee commented Jan 8, 2026

Summary

Implements sdk.TruncateCode() function that intelligently shortens code files while preserving structural integrity (imports, complete functions, class boundaries). This enables AI agent workflows to process large codebases effectively by fitting maximum meaningful code context into LLM context windows.

Key Features:

  • Language-aware parsing for Go, Python, TypeScript, JavaScript
  • Line-based and token-based truncation options (MaxLines, MaxTokens)
  • PreserveTop option keeps imports/headers
  • PreserveFunc option prevents mid-function cuts
  • Graceful fallback to line-based truncation for unknown languages

Performance:

  • 10k line files truncate in ~1-2ms (86x faster than 100ms requirement)
  • Memory usage < 4x input size (well under 10x limit)

Test Coverage:

  • 7,000+ lines of tests (unit, integration, benchmark, fuzz)
  • All user stories from spec verified
  • Race detector and concurrent access tested

Test plan

  • All unit tests pass (go test ./internal/truncate/... ./sdk/...)
  • Benchmark tests verify NFR1 performance (<100ms for 10k lines)
  • Fuzz tests verify NFR4 safety (no panics on any input)
  • Race detector passes (thread safety)
  • go vet clean

Closes #3
Implements SPEC-6

tombee added 2 commits January 8, 2026 16:42
Implements Phase 1 of context-aware truncation:
- TruncateOptions, TruncateResult, OmittedItem types
- TruncateError type with error codes
- ErrInputTooLarge and ErrInvalidOptions errors
- Comprehensive tests for error handling and safety

Error messages intentionally omit sensitive details (sizes, values)
to prevent information leakage per NFR4 requirements.
Implements sdk.TruncateCode() function that intelligently shortens code
files while preserving structural integrity (imports, complete functions,
class boundaries).

Features:
- Language-aware parsing for Go, Python, TypeScript, JavaScript
- Line-based and token-based truncation options
- PreserveTop option keeps imports/headers
- PreserveFunc option prevents mid-function cuts
- Fallback to line-based truncation for unknown languages

Performance:
- 10k line files truncate in ~1-2ms (86x faster than 100ms requirement)
- Memory usage < 4x input size

Closes #3
@tombee tombee merged commit 4fc79b9 into main Jan 8, 2026
1 of 4 checks passed
@tombee tombee deleted the spec/SPEC-6 branch January 8, 2026 19:00
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 context-aware truncation for code content

2 participants