Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Smart Results files: trailing blank lines are not lines (typed-M2) (#252)

Oleksandr Ivanchenko17630a480bcd
diff --git a/docs/smart-results.md b/docs/smart-results.md
index e8d2cf9b..01fc2a38 100644
--- a/docs/smart-results.md
+++ b/docs/smart-results.md
@@ -216,7 +216,9 @@ is complete, so old fixtures stay valid:
Totals come with `truncated` and only when counted exactly: a file cut at the 1 MB cap has
`truncated: true` and no totals (the renderer shows "Showing first 20 rows" without a count), and
so does a table with a row over 1,000 fields or a cell over 100 KB. A total is never below what the
-preview shows. Per-cell and per-line cuts (300 characters) do not set `truncated`. Example:
+preview shows. Per-cell and per-line cuts (300 characters) do not set `truncated`. Lines are counted
+as a reader counts them: a final newline or trailing blank lines add none, so a 20-line file is
+complete and never reports 22 lines. Example:
`{"kind":"table","columns":[...],"rows":[...20 rows],"truncated":true,"total_rows":143,"total_cols":5}`
shows as "Showing 20 of 143 rows". Bytes
are read only through `attachments::blob_path` (a sha256 hex string under the attachments
diff --git a/src/server/smart_results/files.rs b/src/server/smart_results/files.rs
index 9bc4c957..b53cbf10 100644
--- a/src/server/smart_results/files.rs
+++ b/src/server/smart_results/files.rs
@@ -158,9 +158,13 @@ fn utf16(bytes: &[u8], read: fn([u8; 2]) -> u16) -> Option<String> {
}
/// The first 20 lines (control characters dropped, each at most 300 characters) joined by newlines.
+/// Trailing blank lines are not lines.
/// When the file has more, `truncated` is set and, if every line was counted, `total_lines` says
/// how many there are. A file cut at the read cap (`capped`) has `truncated` and no total.
fn excerpt(text: &str, capped: bool) -> FilePreview {
+ // Lines are counted the way a reader counts them: a final newline and trailing blank lines
+ // add none, so a 20-line file is never "cut" or reported as 22 lines.
+ let text = text.trim_end_matches(|c: char| c.is_whitespace());
let mut lines = text.lines();
let shown: Vec<String> = lines.by_ref().take(MAX_LINES).map(|l| safe(l, MAX_CELL)).collect();
let rest = lines.count();
@@ -256,3 +260,55 @@ impl Iterator for Records<'_> {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn text(p: FilePreview) -> (String, bool, Option<u64>) {
+ match p {
+ FilePreview::Text { excerpt, truncated, total_lines } => (excerpt, truncated, total_lines),
+ _ => panic!("not a text preview"),
+ }
+ }
+
+ fn lines(n: usize) -> String {
+ (1..=n).map(|i| format!("line {i}")).collect::<Vec<_>>().join("\n")
+ }
+
+ #[test]
+ fn trailing_blank_lines_are_not_lines() {
+ // 20 real lines followed by "\n\n" is a 20-line file that fits: complete, no totals
+ let (shown, truncated, total) = text(excerpt(&format!("{}\n\n", lines(20)), false));
+ assert_eq!(shown.lines().count(), 20);
+ assert!(!truncated, "a file that fits must not be marked truncated");
+ assert_eq!(total, None);
+ // whitespace-only trailing lines and CRLF endings count the same way
+ let (shown, truncated, total) = text(excerpt(&format!("{}\r\n \r\n\t\r\n", lines(20).replace('\n', "\r\n")), false));
+ assert_eq!((shown.lines().count(), truncated, total), (20, false, None));
+ }
+
+ #[test]
+ fn a_short_file_keeps_no_blank_tail() {
+ let (shown, truncated, total) = text(excerpt("a\nb\n\n\n\n\n", false));
+ assert_eq!((shown.as_str(), truncated, total), ("a\nb", false, None));
+ }
+
+ #[test]
+ fn a_cut_file_counts_only_real_lines() {
+ // 22 real lines plus a blank tail: 20 shown, total 22 (not 24)
+ let (shown, truncated, total) = text(excerpt(&format!("{}\n\n\n", lines(22)), false));
+ assert_eq!(shown.lines().count(), 20);
+ assert!(truncated);
+ assert_eq!(total, Some(22));
+ // a blank line inside the file is still a line
+ let (_, truncated, total) = text(excerpt("a\n\nb\n", false));
+ assert_eq!((truncated, total), (false, None));
+ }
+
+ #[test]
+ fn a_file_cut_at_the_read_cap_stays_truncated_without_a_total() {
+ let (_, truncated, total) = text(excerpt(&lines(5), true));
+ assert_eq!((truncated, total), (true, None));
+ }
+}