logviewer: copy text now respects its own 200,000-byte budget
- Commit
501a58513df97bb4488b53f8cae499b6703adc69- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 23:27 UTC (5 days ago)
- Parents
17df7f7
clipText accounted only header and body bytes against the 200,000-byte clipboard budget, then appended bytes it never counted: both code fences (8), the multi-line-header separator (1) and the truncation marker (14) — up to 23 unbudgeted bytes past a limit its own comment says exists to avoid TransactionTooLargeException. Two boundary cases emitted 200,004 and 200,002 UTF-8 bytes against the budget. The emitted overhead is now accounted up front, and once truncation is needed the longest whole-line tail is kept that fits alongside the marker within budget. Tail-keeping, whole-line dropping, header retention and copyLine behaviour are otherwise unchanged; the two regression tests failed pre-fix. Parsing, filtering, ordering and the other export paths were audited and cleared. Verified: :logviewer:testDevUnitTest 161 tests pass; the one lint error is pre-existing on an untouched file.
Diff
logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt
diff --git a/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt b/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.ktindex f0dc894380..a3906a98c4 100644--- a/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt+++ b/logviewer/src/main/java/com/vayunmathur/logviewer/domain/LogDocument.kt@@ -55,22 +55,33 @@ class LogDocument( * over it is a `TransactionTooLargeException`, not a truncated paste. */ fun clipText(): ClipText {- var sumSize = headerLines.sumOf { utf8Length(it) + 1 }- var sumChars = 0- var bodyStartIndex = 0+ val fence = "```\n"+ val truncatedMarker = "[[TRUNCATED]]\n"+ // The budget covers the whole emitted text, not just header + body: the fences, the+ // blank separator after a multi-line header and the truncation marker are bytes the+ // clipboard transaction has to carry too, so they are accounted for up front.+ val headerBytes = headerLines.sumOf { utf8Length(it) + 1 }+ val separatorBytes = if (headerLines.size > 1) 1 else 0+ val fixedBytes = headerBytes + separatorBytes + fence.length * 2 - for (i in bodyLines.indices.reversed()) {- val line = bodyLines[i]- sumChars += line.length + 1- sumSize += utf8Length(line) + 1- if (sumSize > MAX_CLIP_BYTES) {- bodyStartIndex = i + 1- break+ var bodyStartIndex = 0+ var wholeBodyBytes = 0+ for (line in bodyLines) wholeBodyBytes += utf8Length(line) + 1+ if (fixedBytes + wholeBodyBytes > MAX_CLIP_BYTES) {+ // Truncated: keep the longest whole-line tail that fits alongside the marker.+ var sumSize = fixedBytes + truncatedMarker.length+ bodyStartIndex = bodyLines.size+ for (i in bodyLines.indices.reversed()) {+ val lineBytes = utf8Length(bodyLines[i]) + 1+ if (sumSize + lineBytes > MAX_CLIP_BYTES) break+ sumSize += lineBytes+ bodyStartIndex = i } } + val sumChars = (bodyStartIndex until bodyLines.size).sumOf { bodyLines[it].length + 1 } val sb = StringBuilder(sumChars + 1000)- sb.append("```\n")+ sb.append(fence) for (line in headerLines) { sb.append(line)@@ -78,14 +89,14 @@ class LogDocument( } if (headerLines.size > 1) sb.append('\n') - if (bodyStartIndex != 0) sb.append("[[TRUNCATED]]\n")+ if (bodyStartIndex != 0) sb.append(truncatedMarker) for (i in bodyStartIndex until bodyLines.size) { sb.append(kind.copyLine(bodyLines[i])) sb.append('\n') } - sb.append("```\n")+ sb.append(fence) return ClipText(sb.toString(), truncated = bodyStartIndex != 0) }
logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt
diff --git a/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt b/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.ktindex 223ce05807..8c0ca4169c 100644--- a/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt+++ b/logviewer/src/test/java/com/vayunmathur/logviewer/domain/LogDocumentExtraTest.kt@@ -142,6 +142,33 @@ class LogDocumentExtraTest { assertTrue(clip.text.contains("tail")) } + @Test+ fun `clip text stays within byte budget at the non-truncated boundary`() {+ // Counted body size is 199,996 bytes (line + newline), under the 200,000 cap on its+ // own - but the emitted text also carries the two fences, so it must not be emitted+ // whole if the total would pass the cap.+ val d = doc(bodyLines = listOf("x".repeat(199_995)))+ val clip = d.clipText()+ assertTrue(clip.truncated)+ assertTrue(clip.text.toByteArray(Charsets.UTF_8).size <= 200_000)+ }++ @Test+ fun `clip text stays within byte budget when truncated`() {+ // Header (4) + tail (5) + the long line (199,971) count to 199,980, under the cap;+ // adding the fences and the [[TRUNCATED]] marker takes the emitted text over it, so+ // the long line cannot be kept whole alongside the tail.+ val d = doc(+ headerLines = listOf("hdr"),+ bodyLines = listOf("early filler", "b".repeat(199_970), "tail"),+ )+ val clip = d.clipText()+ assertTrue(clip.truncated)+ assertTrue(clip.text.toByteArray(Charsets.UTF_8).size <= 200_000)+ assertTrue(clip.text.contains("tail"))+ assertFalse(clip.text.contains("b".repeat(199_970)))+ }+ @Test fun `clip keeps header even when body is truncated`() { val body = (1..20_000).map { "line $it with enough padding to fill" }