@@ -53,7 +53,10 @@ impl ContextFetcher {
5353 let end_idx = end. min ( file_lines. len ( ) ) ;
5454
5555 if start_idx < file_lines. len ( ) {
56- let chunk_content = file_lines[ start_idx..end_idx] . join ( "\n " ) ;
56+ let chunk_content = truncate_with_notice (
57+ file_lines[ start_idx..end_idx] . join ( "\n " ) ,
58+ MAX_CONTEXT_CHARS ,
59+ ) ;
5760 chunks. push ( LLMContextChunk {
5861 file_path : file_path. clone ( ) ,
5962 content : chunk_content,
@@ -106,6 +109,7 @@ impl ContextFetcher {
106109 . take ( max_lines)
107110 . collect :: < Vec < _ > > ( )
108111 . join ( "\n " ) ;
112+ let snippet = truncate_with_notice ( snippet, MAX_CONTEXT_CHARS ) ;
109113 if snippet. trim ( ) . is_empty ( ) {
110114 continue ;
111115 }
@@ -153,7 +157,10 @@ impl ContextFetcher {
153157 // Extract a few lines around the definition for context
154158 let start_line = line_num. saturating_sub ( 2 ) ;
155159 let end_line = ( line_num + 5 ) . min ( lines. len ( ) ) ;
156- let definition_content = lines[ start_line..end_line] . join ( "\n " ) ;
160+ let definition_content = truncate_with_notice (
161+ lines[ start_line..end_line] . join ( "\n " ) ,
162+ MAX_CONTEXT_CHARS ,
163+ ) ;
157164
158165 chunks. push ( LLMContextChunk {
159166 file_path : file_path. clone ( ) ,
@@ -194,6 +201,17 @@ fn merge_ranges(lines: &[(usize, usize)]) -> Vec<(usize, usize)> {
194201 merged
195202}
196203
204+ const MAX_CONTEXT_CHARS : usize = 8000 ;
205+
206+ fn truncate_with_notice ( mut content : String , max_chars : usize ) -> String {
207+ if max_chars == 0 || content. len ( ) <= max_chars {
208+ return content;
209+ }
210+ content. truncate ( max_chars. saturating_sub ( 20 ) ) ;
211+ content. push_str ( "\n [Truncated]\n " ) ;
212+ content
213+ }
214+
197215async fn read_file_lossy ( path : & Path ) -> Result < String > {
198216 match tokio:: fs:: read_to_string ( path) . await {
199217 Ok ( content) => Ok ( content) ,
0 commit comments