maproom 0.1.0

Semantic code search powered by embeddings and SQLite
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Intelligent code truncation for fitting content within token budgets.
//!
//! This module provides utilities for truncating code content while preserving
//! the most important parts (signatures, docstrings, type annotations) and
//! maintaining code readability. Truncation is used when a chunk's content
//! exceeds its allocated token budget.

use crate::context::token_counter::TokenCounter;
use anyhow::Result;

/// Truncation strategy for code content.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TruncationStrategy {
    /// Keep signature and docstring, truncate body
    PreserveSignature,
    /// Keep only the first N lines
    Head,
    /// Keep first and last N lines
    HeadAndTail,
}

/// Result of truncating content to fit a budget.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TruncationResult {
    /// The truncated content
    pub content: String,
    /// Actual token count after truncation
    pub tokens: usize,
    /// Whether content was truncated
    pub truncated: bool,
    /// Number of lines removed (if truncated)
    pub lines_removed: usize,
}

/// Intelligent code truncator that preserves important parts of code.
///
/// The truncator attempts to keep:
/// - Function/class signatures
/// - Docstrings and leading comments
/// - Type annotations
/// - A representative sample of the body
///
/// # Example
///
/// ```ignore
/// use maproom::context::truncation::{CodeTruncator, TruncationStrategy};
///
/// let truncator = CodeTruncator::new();
/// let code = "fn long_function() {\n".to_string() + &"    // line\n".repeat(100) + "}";
///
/// let result = truncator.truncate(&code, 100, TruncationStrategy::PreserveSignature).unwrap();
/// assert!(result.truncated);
/// assert!(result.tokens <= 100);
/// ```
pub struct CodeTruncator {
    counter: TokenCounter,
}

impl CodeTruncator {
    /// Create a new code truncator.
    pub fn new() -> Self {
        Self {
            counter: TokenCounter::new(),
        }
    }

    /// Truncate content to fit within the specified token budget.
    ///
    /// Returns the truncated content, actual token count, and whether
    /// truncation occurred.
    ///
    /// # Arguments
    ///
    /// * `content` - The code content to truncate
    /// * `budget` - Maximum tokens allowed
    /// * `strategy` - Truncation strategy to use
    ///
    /// # Example
    ///
    /// ```
    /// use maproom::context::truncation::{CodeTruncator, TruncationStrategy};
    ///
    /// let truncator = CodeTruncator::new();
    /// let code = "fn test() { println!(\"hello\"); }";
    ///
    /// let result = truncator.truncate(code, 1000, TruncationStrategy::PreserveSignature).unwrap();
    /// assert!(!result.truncated); // Short code, no truncation needed
    /// ```
    pub fn truncate(
        &self,
        content: &str,
        budget: usize,
        strategy: TruncationStrategy,
    ) -> Result<TruncationResult> {
        // Check if content already fits
        let current_tokens = self.counter.count(content)?;
        if current_tokens <= budget {
            return Ok(TruncationResult {
                content: content.to_string(),
                tokens: current_tokens,
                truncated: false,
                lines_removed: 0,
            });
        }

        // Apply truncation strategy
        match strategy {
            TruncationStrategy::PreserveSignature => {
                self.truncate_preserve_signature(content, budget)
            }
            TruncationStrategy::Head => self.truncate_head(content, budget),
            TruncationStrategy::HeadAndTail => self.truncate_head_and_tail(content, budget),
        }
    }

    /// Truncate while preserving function signature and docstring.
    ///
    /// This strategy:
    /// 1. Identifies the signature (first line or opening brace)
    /// 2. Identifies the docstring (leading comment block)
    /// 3. Keeps a sample of the body if budget allows
    /// 4. Adds truncation markers
    fn truncate_preserve_signature(
        &self,
        content: &str,
        budget: usize,
    ) -> Result<TruncationResult> {
        let lines: Vec<&str> = content.lines().collect();
        if lines.is_empty() {
            return Ok(TruncationResult {
                content: String::new(),
                tokens: 0,
                truncated: false,
                lines_removed: 0,
            });
        }

        // Find signature (first non-empty line or lines until opening brace)
        let signature_end = self.find_signature_end(&lines);

        // Find docstring end (comments after signature)
        let docstring_end = self.find_docstring_end(&lines, signature_end);

        // Start with signature and docstring
        let mut kept_lines = Vec::new();
        for line in lines
            .iter()
            .take(docstring_end.min(lines.len().saturating_sub(1)) + 1)
        {
            kept_lines.push(*line);
        }

        let marker = "\n    // ... [truncated] ...\n";
        let marker_tokens = self.counter.count(marker)?;

        // Try to add body lines until we approach budget
        let mut current_content = kept_lines.join("\n");
        let mut current_tokens = self.counter.count(&current_content)?;

        // Reserve space for truncation marker
        let available = budget.saturating_sub(marker_tokens);

        let mut body_start = docstring_end + 1;
        let mut lines_added = 0;

        // Add body lines one at a time while under budget
        while body_start < lines.len() && current_tokens < available {
            let next_line = lines[body_start];
            let test_content = format!("{}\n{}", current_content, next_line);
            let test_tokens = self.counter.count(&test_content)?;

            if test_tokens <= available {
                current_content = test_content;
                current_tokens = test_tokens;
                body_start += 1;
                lines_added += 1;
            } else {
                break;
            }
        }

        // Add truncation marker if we removed lines
        let lines_removed = lines.len().saturating_sub(docstring_end + 1 + lines_added);
        if lines_removed > 0 {
            current_content.push_str(marker);
            current_tokens = self.counter.count(&current_content)?;
        }

        Ok(TruncationResult {
            content: current_content,
            tokens: current_tokens,
            truncated: lines_removed > 0,
            lines_removed,
        })
    }

    /// Truncate by keeping only the first N lines.
    fn truncate_head(&self, content: &str, budget: usize) -> Result<TruncationResult> {
        let lines: Vec<&str> = content.lines().collect();
        if lines.is_empty() {
            return Ok(TruncationResult {
                content: String::new(),
                tokens: 0,
                truncated: false,
                lines_removed: 0,
            });
        }

        let marker = "\n// ... [truncated] ...";
        let marker_tokens = self.counter.count(marker)?;
        let available = budget.saturating_sub(marker_tokens);

        let mut kept_lines = Vec::new();
        let mut current_content = String::new();

        for (i, line) in lines.iter().enumerate() {
            let test_content = if i == 0 {
                line.to_string()
            } else {
                format!("{}\n{}", current_content, line)
            };

            let test_tokens = self.counter.count(&test_content)?;
            if test_tokens <= available {
                current_content = test_content;
                kept_lines.push(*line);
            } else {
                break;
            }
        }

        let lines_removed = lines.len().saturating_sub(kept_lines.len());
        if lines_removed > 0 {
            current_content.push_str(marker);
        }

        let tokens = self.counter.count(&current_content)?;

        Ok(TruncationResult {
            content: current_content,
            tokens,
            truncated: lines_removed > 0,
            lines_removed,
        })
    }

    /// Truncate by keeping first and last N lines.
    fn truncate_head_and_tail(&self, content: &str, budget: usize) -> Result<TruncationResult> {
        let lines: Vec<&str> = content.lines().collect();
        if lines.is_empty() {
            return Ok(TruncationResult {
                content: String::new(),
                tokens: 0,
                truncated: false,
                lines_removed: 0,
            });
        }

        // Allocate budget: 60% head, 40% tail
        let marker = "\n// ... [truncated] ...\n";
        let marker_tokens = self.counter.count(marker)?;
        let available = budget.saturating_sub(marker_tokens);
        let head_budget = (available * 60) / 100;
        let tail_budget = available - head_budget;

        // Collect head lines
        let mut head_lines = Vec::new();
        let mut head_content = String::new();

        for (i, line) in lines.iter().enumerate() {
            let test_content = if i == 0 {
                line.to_string()
            } else {
                format!("{}\n{}", head_content, line)
            };

            let test_tokens = self.counter.count(&test_content)?;
            if test_tokens <= head_budget {
                head_content = test_content;
                head_lines.push(*line);
            } else {
                break;
            }
        }

        // Collect tail lines (from end backwards)
        let mut tail_lines = Vec::new();
        let mut tail_content = String::new();

        for line in lines.iter().rev() {
            let test_content = if tail_content.is_empty() {
                line.to_string()
            } else {
                format!("{}\n{}", line, tail_content)
            };

            let test_tokens = self.counter.count(&test_content)?;
            if test_tokens <= tail_budget {
                tail_content = test_content;
                tail_lines.insert(0, *line);
            } else {
                break;
            }
        }

        // Check if head and tail overlap
        if head_lines.len() + tail_lines.len() >= lines.len() {
            // No truncation needed (or very little removed)
            return Ok(TruncationResult {
                content: content.to_string(),
                tokens: self.counter.count(content)?,
                truncated: false,
                lines_removed: 0,
            });
        }

        // Combine head + marker + tail
        let final_content = format!("{}{}{}", head_content, marker, tail_content);
        let tokens = self.counter.count(&final_content)?;
        let lines_removed = lines.len() - head_lines.len() - tail_lines.len();

        Ok(TruncationResult {
            content: final_content,
            tokens,
            truncated: lines_removed > 0,
            lines_removed,
        })
    }

    /// Find the end of the function/class signature.
    ///
    /// Returns the line index where the signature ends.
    fn find_signature_end(&self, lines: &[&str]) -> usize {
        // Look for opening brace, arrow, or first non-comment line
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();
            if trimmed.contains('{') || trimmed.contains("=>") || trimmed.ends_with(':') {
                return i;
            }
            // If we hit a non-comment, non-empty line without signature markers,
            // assume signature is just the first line
            if !trimmed.is_empty()
                && !trimmed.starts_with("//")
                && !trimmed.starts_with("/*")
                && !trimmed.starts_with('*')
                && !trimmed.starts_with('#')
            {
                return i;
            }
        }
        0
    }

    /// Find the end of the docstring/comments after signature.
    ///
    /// Returns the line index where docstring ends.
    fn find_docstring_end(&self, lines: &[&str], start: usize) -> usize {
        let mut end = start;

        // Skip ahead past signature
        let mut i = start + 1;
        while i < lines.len() {
            let trimmed = lines[i].trim();

            // Check if this is a comment line
            if trimmed.starts_with("//")
                || trimmed.starts_with("/*")
                || trimmed.starts_with('*')
                || trimmed.starts_with('#')
                || trimmed.starts_with("///")
            {
                end = i;
                i += 1;
            } else if trimmed.is_empty() {
                // Empty line, keep going if we've seen comments
                if end > start {
                    end = i;
                }
                i += 1;
            } else {
                // Non-comment, non-empty line - docstring is done
                break;
            }
        }

        end
    }
}

impl Default for CodeTruncator {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_no_truncation_needed() {
        let truncator = CodeTruncator::new();
        let code = "fn test() { println!(\"hello\"); }";

        let result = truncator
            .truncate(code, 1000, TruncationStrategy::PreserveSignature)
            .unwrap();

        assert!(!result.truncated);
        assert_eq!(result.content, code);
        assert_eq!(result.lines_removed, 0);
    }

    #[test]
    fn test_truncate_long_function() {
        let truncator = CodeTruncator::new();

        let mut code = String::from("fn long_function() {\n");
        for i in 0..100 {
            code.push_str(&format!("    let x{} = {};\n", i, i));
        }
        code.push_str("}");

        let result = truncator
            .truncate(&code, 100, TruncationStrategy::PreserveSignature)
            .unwrap();

        assert!(result.truncated);
        assert!(result.tokens <= 100);
        assert!(result.content.contains("fn long_function()"));
        assert!(result.content.contains("[truncated]"));
        assert!(result.lines_removed > 0);
    }

    #[test]
    fn test_preserve_signature_with_docstring() {
        let truncator = CodeTruncator::new();

        let code = r#"fn documented() {
    // This is a docstring
    // explaining the function
    let x = 1;
    let y = 2;
    let z = 3;
    // ... many more lines
}"#;

        let result = truncator
            .truncate(code, 50, TruncationStrategy::PreserveSignature)
            .unwrap();

        assert!(result.tokens <= 50);
        // Should keep signature and docstring
        assert!(result.content.contains("fn documented()"));
        assert!(result.content.contains("docstring"));
    }

    #[test]
    fn test_truncate_head_strategy() {
        let truncator = CodeTruncator::new();

        let code = "line 1\nline 2\nline 3\nline 4\nline 5";

        let result = truncator
            .truncate(code, 20, TruncationStrategy::Head)
            .unwrap();

        assert!(result.tokens <= 20);
        if result.truncated {
            assert!(result.content.contains("[truncated]"));
        }
    }

    #[test]
    fn test_truncate_head_and_tail_strategy() {
        let truncator = CodeTruncator::new();

        let mut code = String::new();
        for i in 0..50 {
            code.push_str(&format!("line {}\n", i));
        }

        let result = truncator
            .truncate(&code, 50, TruncationStrategy::HeadAndTail)
            .unwrap();

        assert!(result.tokens <= 50);
        if result.truncated {
            assert!(result.content.contains("[truncated]"));
            // Should have both beginning and end
            assert!(result.content.contains("line 0"));
        }
    }

    #[test]
    fn test_empty_content() {
        let truncator = CodeTruncator::new();

        let result = truncator
            .truncate("", 100, TruncationStrategy::PreserveSignature)
            .unwrap();

        assert!(!result.truncated);
        assert_eq!(result.content, "");
        assert_eq!(result.tokens, 0);
    }

    #[test]
    fn test_single_line() {
        let truncator = CodeTruncator::new();
        let code = "fn test();";

        let result = truncator
            .truncate(code, 100, TruncationStrategy::PreserveSignature)
            .unwrap();

        assert!(!result.truncated);
        assert_eq!(result.content, code);
    }

    #[test]
    fn test_very_small_budget() {
        let truncator = CodeTruncator::new();
        let code = "fn test() {\n    println!(\"hello\");\n}";

        let result = truncator
            .truncate(code, 10, TruncationStrategy::PreserveSignature)
            .unwrap();

        // Should at least try to keep signature
        assert!(result.tokens <= 10);
        assert!(result.content.contains("fn"));
    }

    #[test]
    fn test_find_signature_end() {
        let truncator = CodeTruncator::new();

        let lines1 = vec!["fn test() {", "    body", "}"];
        assert_eq!(truncator.find_signature_end(&lines1), 0);

        // When signature and brace are split, should find the brace line
        let lines2 = vec!["fn test()", "{", "    body"];
        // Current implementation returns 0 because "fn test()" is on first line
        // This is acceptable - signature is complete on line 0
        assert_eq!(truncator.find_signature_end(&lines2), 0);

        let lines3 = vec!["const func = () => {", "    body"];
        assert_eq!(truncator.find_signature_end(&lines3), 0);

        let lines4 = vec!["def func():", "    body"];
        assert_eq!(truncator.find_signature_end(&lines4), 0);
    }

    #[test]
    fn test_find_docstring_end() {
        let truncator = CodeTruncator::new();

        let lines = vec![
            "fn test() {",
            "    // Comment 1",
            "    // Comment 2",
            "    ",
            "    // Comment 3",
            "    let x = 1;",
        ];

        let end = truncator.find_docstring_end(&lines, 0);
        assert!(end >= 2); // Should include at least first comments
    }

    #[test]
    fn test_preserve_signature_rust_function() {
        let truncator = CodeTruncator::new();

        let code = r#"pub fn calculate(x: i32, y: i32) -> i32 {
    // Calculate the sum
    let sum = x + y;
    let doubled = sum * 2;
    let tripled = doubled * 3;
    let result = tripled * 4;
    result
}"#;

        let result = truncator
            .truncate(code, 50, TruncationStrategy::PreserveSignature)
            .unwrap();

        assert!(result.tokens <= 50);
        assert!(result.content.contains("pub fn calculate"));
    }

    #[test]
    fn test_preserve_signature_typescript_function() {
        let truncator = CodeTruncator::new();

        let code = r#"function processData(data: string[]): Result {
  // Process each item
  const results = data.map(item => transform(item));
  const filtered = results.filter(r => r.valid);
  const sorted = filtered.sort((a, b) => a.score - b.score);
  return { data: sorted };
}"#;

        let result = truncator
            .truncate(code, 60, TruncationStrategy::PreserveSignature)
            .unwrap();

        assert!(result.tokens <= 60);
        assert!(result.content.contains("function processData"));
    }

    #[test]
    fn test_truncation_strategies_comparison() {
        let truncator = CodeTruncator::new();

        let mut code = String::new();
        for i in 0..30 {
            code.push_str(&format!("line {}\n", i));
        }

        let budget = 40;

        let preserve = truncator
            .truncate(&code, budget, TruncationStrategy::PreserveSignature)
            .unwrap();

        let head = truncator
            .truncate(&code, budget, TruncationStrategy::Head)
            .unwrap();

        let head_tail = truncator
            .truncate(&code, budget, TruncationStrategy::HeadAndTail)
            .unwrap();

        // All should respect budget
        assert!(preserve.tokens <= budget);
        assert!(head.tokens <= budget);
        assert!(head_tail.tokens <= budget);

        // Head and tail might keep more context
        if head_tail.truncated && head.truncated {
            assert!(head_tail.content.len() >= head.content.len() / 2);
        }
    }
}