Skip to content

Add context-aware truncation for code content #3

@tombee

Description

@tombee

Problem

When truncating large content to fit context windows, naive truncation (cut at character/line limit) breaks code understanding:

  • Functions get cut mid-body
  • Import statements separated from usage
  • Class definitions incomplete

Concrete example from foreman: When a 2000-line file is truncated to 500 lines, it often cuts in the middle of a function, making the code unusable for analysis.

Proposal

Add language-aware truncation that respects code structure:

// Truncation options
type TruncateOptions struct {
    MaxLines     int
    MaxTokens    int
    Language     string  // "typescript", "go", "python", etc.
    PreserveTop  bool    // Keep imports/headers
    PreserveFunc bool    // Don't cut mid-function
}

// Usage
content := sdk.TruncateCode(fileContent, TruncateOptions{
    MaxLines:     500,
    Language:     "typescript",
    PreserveTop:  true,
    PreserveFunc: true,
})

General Utility

  1. Code review agents - Summarizing large files intelligently
  2. Documentation generators - Extracting relevant code sections
  3. Refactoring tools - Understanding code structure before changes

Acceptance Criteria

  • TruncateCode function with language awareness
  • Support for at least: TypeScript, Go, Python, JavaScript
  • Preserves import/header sections when PreserveTop enabled
  • Doesn't cut mid-function/class when PreserveFunc enabled
  • Falls back to line-based truncation for unknown languages
  • Output includes truncation indicator

Priority

Medium - Improves agent effectiveness but not blocking.

Labels

enhancement, sdk, priority:medium

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions