Skip to content

Commit 2dd09e5

Browse files
Copilotbhauman
andauthored
Fix truncation message showing file size in bytes instead of line count (#107)
* Initial plan * Initial plan for fixing truncation message Co-authored-by: bhauman <2624+bhauman@users.noreply.github.com> * Fix truncation message to show line count instead of file size in bytes Co-authored-by: bhauman <2624+bhauman@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: bhauman <2624+bhauman@users.noreply.github.com>
1 parent f81db88 commit 2dd09e5

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ NOTES.md
6565

6666
# Log directories
6767
/logs/
68+
linux-install.sh

src/clojure_mcp/tools/unified_read_file/core.clj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
- :truncated-by - The reason for truncation (e.g., 'max-lines')
117117
- :size - The file size in bytes
118118
- :line-count - The number of lines returned
119+
- :total-line-count - The total number of lines in the file (only present when truncated)
119120
- :offset - The line offset used
120121
- :max-line-length - The max line length used
121122
- :line-lengths-truncated? - Whether any lines were truncated in length
@@ -143,6 +144,10 @@
143144
(str (subs line 0 max-line-length) "...")
144145
line))))))
145146
truncated-by-lines? (and limit (> (count lines) limit))
147+
;; If truncated, count total lines in file for accurate message
148+
total-line-count (when truncated-by-lines?
149+
(with-open [rdr (io/reader file)]
150+
(count (line-seq rdr))))
146151
content-lines (if truncated-by-lines? (take limit lines) lines)
147152
content (str/join "\n" content-lines)]
148153
{:content content
@@ -151,6 +156,7 @@
151156
:truncated-by (when truncated-by-lines? "max-lines")
152157
:size size
153158
:line-count (count content-lines)
159+
:total-line-count total-line-count
154160
:offset offset
155161
:max-line-length max-line-length
156162
:line-lengths-truncated? (and max-line-length

src/clojure_mcp/tools/unified_read_file/tool.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,12 @@ By default, reads up to " max-lines " lines, truncating lines longer than " max-
253253
(defn format-raw-file
254254
"Formats raw file content with markdown."
255255
[result max-lines]
256-
(let [{:keys [content path size line-count offset truncated? line-lengths-truncated?]} result
256+
(let [{:keys [content path size line-count offset truncated? line-lengths-truncated? total-line-count]} result
257257
file-type (last (str/split path #"\."))
258258
lang-hint (when file-type (str file-type))
259259
preamble (str "### " path "\n"
260260
(when truncated?
261-
(str "File truncated (showing " line-count " of " size " lines)\n\n")))]
261+
(str "File truncated (showing " line-count " of " total-line-count " lines)\n\n")))]
262262
[(str preamble "```" lang-hint "\n" content "\n```")]))
263263

264264
(defn format-text-collapsed-view

test/clojure_mcp/tools/unified_read_file/core_test.clj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,26 @@
6363
(is (= 2 (count (str/split (:content result) #"\n"))))
6464
(is (str/starts-with? (:content result) "Line 1"))
6565
(is (:truncated? result))
66-
(is (= "max-lines" (:truncated-by result)))))
66+
(is (= "max-lines" (:truncated-by result)))
67+
(is (= 2 (:line-count result)))
68+
(is (= 5 (:total-line-count result)))))
6769

6870
(testing "Reading with offset and limit"
6971
(let [result (read-file-core/read-file (.getPath *test-file*) 1 2)]
7072
(is (map? result))
7173
(is (= 2 (count (str/split (:content result) #"\n"))))
7274
(is (str/starts-with? (:content result) "Line 2"))
73-
(is (:truncated? result))))
75+
(is (:truncated? result))
76+
;; total-line-count should be total lines in file, not remaining from offset
77+
(is (= 5 (:total-line-count result)))))
7478

7579
(testing "Reading with line length limit"
7680
(let [result (read-file-core/read-file (.getPath *large-test-file*) 0 5 :max-line-length 10)]
7781
(is (map? result))
7882
(is (= 5 (count (str/split (:content result) #"\n"))))
7983
(is (every? #(str/includes? % "...") (str/split (:content result) #"\n")))
80-
(is (:line-lengths-truncated? result)))))
84+
(is (:line-lengths-truncated? result))
85+
(is (= 100 (:total-line-count result))))))
8186

8287
(deftest read-file-error-test
8388
(testing "Reading non-existent file"

test/clojure_mcp/tools/unified_read_file/tool_test.clj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,27 @@
6565
(is (not (unified-read-file-tool/collapsible-clojure-file? "test.txt")))
6666
(is (not (unified-read-file-tool/collapsible-clojure-file? "test.md")))
6767
(is (not (unified-read-file-tool/collapsible-clojure-file? "test.js")))))
68+
69+
(deftest format-raw-file-truncation-message-test
70+
(testing "Truncation message shows total line count, not file size"
71+
(let [result {:content "line1\nline2\nline3"
72+
:path "/test/file.txt"
73+
:size 118628 ; File size in bytes (should NOT be shown)
74+
:line-count 3 ; Lines shown
75+
:total-line-count 2000 ; Total lines in file (should be shown)
76+
:truncated? true}
77+
formatted (unified-read-file-tool/format-raw-file result 2000)
78+
formatted-str (first formatted)]
79+
(is (re-find #"showing 3 of 2000 lines" formatted-str)
80+
"Should show total line count (2000), not file size (118628)")))
81+
82+
(testing "Non-truncated file doesn't show truncation message"
83+
(let [result {:content "line1\nline2"
84+
:path "/test/file.txt"
85+
:size 1000
86+
:line-count 2
87+
:truncated? false}
88+
formatted (unified-read-file-tool/format-raw-file result 2000)
89+
formatted-str (first formatted)]
90+
(is (not (re-find #"truncated" formatted-str))
91+
"Should not show truncation message when not truncated"))))

0 commit comments

Comments
 (0)