Skip to main content

pixie_pi/tools/
truncate.rs

1//! Shared truncation utilities (`core/tools/truncate.ts`).
2//!
3//! Tools return bounded output: `read`/`write`/`ls` use head-truncation,
4//! `bash` uses tail-truncation (keep the most recent output), and `grep`
5//! truncates long individual lines.
6
7use serde::{Deserialize, Serialize};
8
9/// Default line cap for tool output (matches Claude Code / pi).
10pub const DEFAULT_MAX_LINES: usize = 2000;
11/// Default byte cap for tool output (50 KiB). Matches pi's `core/tools/truncate.ts`.
12pub const DEFAULT_MAX_BYTES: usize = 50 * 1024;
13/// Maximum length of a single grep result line. Matches pi's `GREP_MAX_LINE_LENGTH`.
14pub const GREP_MAX_LINE_LENGTH: usize = 500;
15
16#[derive(Debug, Clone, Default, Serialize, Deserialize)]
17pub struct TruncationResult {
18    pub content: String,
19    pub truncated: bool,
20    pub truncated_by: Option<String>, // "lines" | "bytes"
21    pub output_lines: usize,
22    pub total_lines: usize,
23    pub max_lines: Option<usize>,
24    pub max_bytes: Option<usize>,
25    pub first_line_exceeds_limit: bool,
26    pub output_bytes: usize,
27    pub last_line_partial: bool,
28}
29
30/// Truncate from the **head** (keep the first lines), used by read/write/ls.
31/// Stops at whichever limit is hit first: `max_lines` or `max_bytes`.
32pub fn truncate_head(content: &str, max_lines: Option<usize>, max_bytes: Option<usize>) -> TruncationResult {
33    let max_lines = max_lines.unwrap_or(DEFAULT_MAX_LINES);
34    let max_bytes = max_bytes.unwrap_or(DEFAULT_MAX_BYTES);
35    let mut lines: Vec<&str> = content.split('\n').collect();
36    // A trailing '\n' splits into a phantom empty final element (nearly every
37    // command output and source file ends in a newline). Drop it so a 3-line
38    // input counts as 3 lines, not 4 — otherwise `total_lines` is inflated and
39    // the line cap reports a spurious truncation or drops a real line. Mirrors
40    // the same accounting already applied in `read.rs` and `generate_diff`.
41    if content.ends_with('\n') {
42        lines.pop();
43    }
44    let total_lines = lines.len();
45
46    if !lines.is_empty() {
47        let first_line_bytes = lines[0].len();
48        if first_line_bytes > max_bytes {
49            return TruncationResult {
50                first_line_exceeds_limit: true,
51                total_lines,
52                max_bytes: Some(max_bytes),
53                max_lines: Some(max_lines),
54                ..Default::default()
55            };
56        }
57    }
58
59    let mut out: Vec<&str> = Vec::new();
60    let mut bytes = 0usize;
61    let mut truncated_by: Option<String> = None;
62
63    for line in &lines {
64        let line_bytes = line.len() + 1; // +1 for the '\n' separator
65        if out.len() + 1 > max_lines {
66            truncated_by = Some("lines".into());
67            break;
68        }
69        if bytes + line_bytes > max_bytes {
70            truncated_by = Some("bytes".into());
71            break;
72        }
73        bytes += line_bytes;
74        out.push(line);
75    }
76
77    let truncated = truncated_by.is_some();
78    let content = out.join("\n");
79    TruncationResult {
80        output_bytes: content.len(),
81        content,
82        truncated,
83        truncated_by,
84        output_lines: out.len(),
85        total_lines,
86        max_lines: Some(max_lines),
87        max_bytes: Some(max_bytes),
88        first_line_exceeds_limit: false,
89        last_line_partial: false,
90    }
91}
92
93/// Truncate from the **tail** (keep the most recent output), used by bash.
94pub fn truncate_tail(content: &str, max_lines: Option<usize>, max_bytes: Option<usize>) -> TruncationResult {
95    let max_lines = max_lines.unwrap_or(DEFAULT_MAX_LINES);
96    let max_bytes = max_bytes.unwrap_or(DEFAULT_MAX_BYTES);
97    let mut lines: Vec<&str> = content.split('\n').collect();
98    // Drop the phantom empty element produced by a trailing newline so the
99    // reported line count is exact. Bash passes raw command output (which almost
100    // always ends in '\n') here and reads `total_lines` / `output_lines` for its
101    // "Showing lines X-Y of N" truncation notice — without this drop that notice
102    // is off by one. See `truncate_head` for the full rationale.
103    if content.ends_with('\n') {
104        lines.pop();
105    }
106    let total_lines = lines.len();
107
108    // Keep the last `max_lines` lines.
109    let start = total_lines.saturating_sub(max_lines);
110    let mut kept: Vec<&str> = lines[start..].to_vec();
111    let mut truncated_by = if start > 0 { Some("lines".into()) } else { None };
112
113    // Enforce the byte cap from the tail.
114    let total_bytes: usize = kept.iter().map(|l| l.len() + 1).sum();
115    if total_bytes > max_bytes {
116        let mut bytes = 0usize;
117        let mut cut_from = 0;
118        for (i, line) in kept.iter().enumerate().rev() {
119            bytes += line.len() + 1;
120            if bytes > max_bytes {
121                cut_from = i + 1;
122                truncated_by = Some("bytes".into());
123                break;
124            }
125        }
126        if cut_from >= kept.len() {
127            cut_from = kept.len().saturating_sub(1);
128        }
129        kept = kept[cut_from..].to_vec();
130    }
131
132    let truncated = truncated_by.is_some();
133    let content = kept.join("\n");
134    let output_lines = kept.len();
135    TruncationResult {
136        output_bytes: content.len(),
137        content,
138        truncated,
139        truncated_by,
140        output_lines,
141        total_lines,
142        max_lines: Some(max_lines),
143        max_bytes: Some(max_bytes),
144        first_line_exceeds_limit: false,
145        last_line_partial: false,
146    }
147}
148
149/// Truncate a single (possibly very long) line for grep output.
150pub fn truncate_line(line: &str, max: Option<usize>) -> (String, bool) {
151    let max = max.unwrap_or(GREP_MAX_LINE_LENGTH);
152    // Operate on chars, not bytes, to avoid splitting multi-byte sequences.
153    let chars: Vec<char> = line.chars().collect();
154    if chars.len() <= max {
155        return (line.to_string(), false);
156    }
157    let truncated: String = chars[..max].iter().collect();
158    (format!("{truncated}…"), true)
159}
160
161/// Human-readable byte size, e.g. "12.3KB".
162pub fn format_size(bytes: usize) -> String {
163    const KB: f64 = 1024.0;
164    const MB: f64 = KB * 1024.0;
165    let b = bytes as f64;
166    if b >= MB {
167        format!("{:.1}MB", b / MB)
168    } else if b >= KB {
169        format!("{:.1}KB", b / KB)
170    } else {
171        format!("{}B", bytes)
172    }
173}
174
175#[cfg(test)]
176mod tests {
177    use super::*;
178
179    #[test]
180    fn head_truncates_by_lines() {
181        let content = "a\nb\nc\nd\ne";
182        let r = truncate_head(content, Some(2), Some(1024));
183        assert_eq!(r.content, "a\nb");
184        assert!(r.truncated);
185        assert_eq!(r.truncated_by.as_deref(), Some("lines"));
186    }
187
188    #[test]
189    fn tail_keeps_recent_lines() {
190        let content = "a\nb\nc\nd\ne";
191        let r = truncate_tail(content, Some(2), Some(1024));
192        assert_eq!(r.content, "d\ne");
193        assert!(r.truncated);
194    }
195
196    #[test]
197    fn line_truncation_is_char_safe() {
198        let s = "日本語".repeat(1000);
199        let (t, truncated) = truncate_line(&s, Some(10));
200        assert!(truncated);
201        assert!(t.chars().count() <= 11);
202    }
203
204    #[test]
205    fn head_does_not_inflate_line_count_for_trailing_newline() {
206        // "a\nb\nc\n" is 3 lines; the trailing newline must not count as a 4th
207        // (phantom) line that trips the line cap or inflates total_lines.
208        let r = truncate_head("a\nb\nc\n", Some(2000), Some(1024));
209        assert_eq!(r.total_lines, 3);
210        assert_eq!(r.output_lines, 3);
211        assert!(!r.truncated);
212    }
213
214    #[test]
215    fn tail_does_not_inflate_line_count_for_trailing_newline() {
216        // Command output almost always ends in '\n'. The phantom element must
217        // not inflate total_lines, which bash reads for its "Showing lines X-Y
218        // of N" truncation notice.
219        let r = truncate_tail("a\nb\nc\n", Some(2000), Some(1024));
220        assert_eq!(r.total_lines, 3);
221        assert_eq!(r.output_lines, 3);
222        assert!(!r.truncated);
223    }
224
225    #[test]
226    fn tail_truncation_reports_an_exact_line_range() {
227        // 10 lines + trailing newline, keep the last 3. Before the phantom fix
228        // `total_lines` was 11 and the derived range was off by one. It must be
229        // exactly lines 8-10 of 10 (bash computes start = total - output + 1).
230        let r = truncate_tail("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n", Some(3), Some(1024));
231        assert_eq!(r.total_lines, 10);
232        assert_eq!(r.output_lines, 3);
233        assert_eq!(r.content, "8\n9\n10");
234        assert!(r.truncated);
235    }
236}