@fractaal/pi-agent-core 0.85.4 → 0.86.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +92 -38
  2. package/dist/agent-loop.d.ts +27 -2
  3. package/dist/agent-loop.d.ts.map +1 -1
  4. package/dist/agent-loop.js +183 -63
  5. package/dist/agent-loop.js.map +1 -1
  6. package/dist/agent.d.ts +23 -6
  7. package/dist/agent.d.ts.map +1 -1
  8. package/dist/agent.js +46 -19
  9. package/dist/agent.js.map +1 -1
  10. package/dist/index.d.ts +0 -18
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +0 -24
  13. package/dist/index.js.map +1 -1
  14. package/dist/proxy.d.ts +6 -3
  15. package/dist/proxy.d.ts.map +1 -1
  16. package/dist/proxy.js +42 -10
  17. package/dist/proxy.js.map +1 -1
  18. package/dist/stream-fn.d.ts.map +1 -1
  19. package/dist/stream-fn.js.map +1 -1
  20. package/dist/types.d.ts +122 -31
  21. package/dist/types.d.ts.map +1 -1
  22. package/dist/types.js.map +1 -1
  23. package/package.json +6 -17
  24. package/dist/harness/agent-harness.d.ts +0 -94
  25. package/dist/harness/agent-harness.d.ts.map +0 -1
  26. package/dist/harness/agent-harness.js +0 -965
  27. package/dist/harness/agent-harness.js.map +0 -1
  28. package/dist/harness/compaction/branch-summarization.d.ts +0 -55
  29. package/dist/harness/compaction/branch-summarization.d.ts.map +0 -1
  30. package/dist/harness/compaction/branch-summarization.js +0 -173
  31. package/dist/harness/compaction/branch-summarization.js.map +0 -1
  32. package/dist/harness/compaction/compaction.d.ts +0 -107
  33. package/dist/harness/compaction/compaction.d.ts.map +0 -1
  34. package/dist/harness/compaction/compaction.js +0 -583
  35. package/dist/harness/compaction/compaction.js.map +0 -1
  36. package/dist/harness/compaction/utils.d.ts +0 -25
  37. package/dist/harness/compaction/utils.d.ts.map +0 -1
  38. package/dist/harness/compaction/utils.js +0 -120
  39. package/dist/harness/compaction/utils.js.map +0 -1
  40. package/dist/harness/env/nodejs.d.ts +0 -45
  41. package/dist/harness/env/nodejs.d.ts.map +0 -1
  42. package/dist/harness/env/nodejs.js +0 -623
  43. package/dist/harness/env/nodejs.js.map +0 -1
  44. package/dist/harness/messages.d.ts +0 -51
  45. package/dist/harness/messages.d.ts.map +0 -1
  46. package/dist/harness/messages.js +0 -102
  47. package/dist/harness/messages.js.map +0 -1
  48. package/dist/harness/prompt-templates.d.ts +0 -48
  49. package/dist/harness/prompt-templates.d.ts.map +0 -1
  50. package/dist/harness/prompt-templates.js +0 -230
  51. package/dist/harness/prompt-templates.js.map +0 -1
  52. package/dist/harness/session/jsonl-repo.d.ts +0 -26
  53. package/dist/harness/session/jsonl-repo.d.ts.map +0 -1
  54. package/dist/harness/session/jsonl-repo.js +0 -103
  55. package/dist/harness/session/jsonl-repo.js.map +0 -1
  56. package/dist/harness/session/jsonl-storage.d.ts +0 -42
  57. package/dist/harness/session/jsonl-storage.d.ts.map +0 -1
  58. package/dist/harness/session/jsonl-storage.js +0 -293
  59. package/dist/harness/session/jsonl-storage.js.map +0 -1
  60. package/dist/harness/session/memory-repo.d.ts +0 -18
  61. package/dist/harness/session/memory-repo.d.ts.map +0 -1
  62. package/dist/harness/session/memory-repo.js +0 -42
  63. package/dist/harness/session/memory-repo.js.map +0 -1
  64. package/dist/harness/session/memory-storage.d.ts +0 -33
  65. package/dist/harness/session/memory-storage.d.ts.map +0 -1
  66. package/dist/harness/session/memory-storage.js +0 -168
  67. package/dist/harness/session/memory-storage.js.map +0 -1
  68. package/dist/harness/session/repo-utils.d.ts +0 -11
  69. package/dist/harness/session/repo-utils.d.ts.map +0 -1
  70. package/dist/harness/session/repo-utils.js +0 -39
  71. package/dist/harness/session/repo-utils.js.map +0 -1
  72. package/dist/harness/session/session.d.ts +0 -49
  73. package/dist/harness/session/session.d.ts.map +0 -1
  74. package/dist/harness/session/session.js +0 -263
  75. package/dist/harness/session/session.js.map +0 -1
  76. package/dist/harness/skills.d.ts +0 -44
  77. package/dist/harness/skills.d.ts.map +0 -1
  78. package/dist/harness/skills.js +0 -311
  79. package/dist/harness/skills.js.map +0 -1
  80. package/dist/harness/system-prompt.d.ts +0 -3
  81. package/dist/harness/system-prompt.d.ts.map +0 -1
  82. package/dist/harness/system-prompt.js +0 -30
  83. package/dist/harness/system-prompt.js.map +0 -1
  84. package/dist/harness/tools/bash.d.ts +0 -27
  85. package/dist/harness/tools/bash.d.ts.map +0 -1
  86. package/dist/harness/tools/bash.js +0 -132
  87. package/dist/harness/tools/bash.js.map +0 -1
  88. package/dist/harness/tools/edit-diff.d.ts +0 -89
  89. package/dist/harness/tools/edit-diff.d.ts.map +0 -1
  90. package/dist/harness/tools/edit-diff.js +0 -386
  91. package/dist/harness/tools/edit-diff.js.map +0 -1
  92. package/dist/harness/tools/edit.d.ts +0 -19
  93. package/dist/harness/tools/edit.d.ts.map +0 -1
  94. package/dist/harness/tools/edit.js +0 -95
  95. package/dist/harness/tools/edit.js.map +0 -1
  96. package/dist/harness/tools/file-mutation-queue.d.ts +0 -4
  97. package/dist/harness/tools/file-mutation-queue.d.ts.map +0 -1
  98. package/dist/harness/tools/file-mutation-queue.js +0 -46
  99. package/dist/harness/tools/file-mutation-queue.js.map +0 -1
  100. package/dist/harness/tools/image.d.ts +0 -3
  101. package/dist/harness/tools/image.d.ts.map +0 -1
  102. package/dist/harness/tools/image.js +0 -106
  103. package/dist/harness/tools/image.js.map +0 -1
  104. package/dist/harness/tools/index.d.ts +0 -6
  105. package/dist/harness/tools/index.d.ts.map +0 -1
  106. package/dist/harness/tools/index.js +0 -5
  107. package/dist/harness/tools/index.js.map +0 -1
  108. package/dist/harness/tools/path-utils.d.ts +0 -4
  109. package/dist/harness/tools/path-utils.d.ts.map +0 -1
  110. package/dist/harness/tools/path-utils.js +0 -26
  111. package/dist/harness/tools/path-utils.js.map +0 -1
  112. package/dist/harness/tools/read.d.ts +0 -34
  113. package/dist/harness/tools/read.d.ts.map +0 -1
  114. package/dist/harness/tools/read.js +0 -110
  115. package/dist/harness/tools/read.js.map +0 -1
  116. package/dist/harness/tools/tool-context.d.ts +0 -6
  117. package/dist/harness/tools/tool-context.d.ts.map +0 -1
  118. package/dist/harness/tools/tool-context.js +0 -2
  119. package/dist/harness/tools/tool-context.js.map +0 -1
  120. package/dist/harness/tools/write.d.ts +0 -11
  121. package/dist/harness/tools/write.d.ts.map +0 -1
  122. package/dist/harness/tools/write.js +0 -31
  123. package/dist/harness/tools/write.js.map +0 -1
  124. package/dist/harness/types.d.ts +0 -681
  125. package/dist/harness/types.d.ts.map +0 -1
  126. package/dist/harness/types.js +0 -94
  127. package/dist/harness/types.js.map +0 -1
  128. package/dist/harness/utils/shell-output.d.ts +0 -22
  129. package/dist/harness/utils/shell-output.d.ts.map +0 -1
  130. package/dist/harness/utils/shell-output.js +0 -181
  131. package/dist/harness/utils/shell-output.js.map +0 -1
  132. package/dist/harness/utils/truncate.d.ts +0 -70
  133. package/dist/harness/utils/truncate.d.ts.map +0 -1
  134. package/dist/harness/utils/truncate.js +0 -296
  135. package/dist/harness/utils/truncate.js.map +0 -1
  136. package/dist/node.d.ts +0 -3
  137. package/dist/node.d.ts.map +0 -1
  138. package/dist/node.js +0 -3
  139. package/dist/node.js.map +0 -1
@@ -1,70 +0,0 @@
1
- /**
2
- * Shared truncation utilities for tool outputs.
3
- *
4
- * Truncation is based on two independent limits - whichever is hit first wins:
5
- * - Line limit (default: 2000 lines)
6
- * - Byte limit (default: 50KB)
7
- *
8
- * Never returns partial lines (except bash tail truncation edge case).
9
- */
10
- export declare const DEFAULT_MAX_LINES = 2000;
11
- export declare const DEFAULT_MAX_BYTES: number;
12
- export declare const GREP_MAX_LINE_LENGTH = 500;
13
- export interface TruncationResult {
14
- /** The truncated content */
15
- content: string;
16
- /** Whether truncation occurred */
17
- truncated: boolean;
18
- /** Which limit was hit: "lines", "bytes", or null if not truncated */
19
- truncatedBy: "lines" | "bytes" | null;
20
- /** Total number of lines in the original content */
21
- totalLines: number;
22
- /** Total number of bytes in the original content */
23
- totalBytes: number;
24
- /** Number of complete lines in the truncated output */
25
- outputLines: number;
26
- /** Number of bytes in the truncated output */
27
- outputBytes: number;
28
- /** Whether the last line was partially truncated (only for tail truncation edge case) */
29
- lastLinePartial: boolean;
30
- /** Whether the first line exceeded the byte limit (for head truncation) */
31
- firstLineExceedsLimit: boolean;
32
- /** The max lines limit that was applied */
33
- maxLines: number;
34
- /** The max bytes limit that was applied */
35
- maxBytes: number;
36
- }
37
- export interface TruncationOptions {
38
- /** Maximum number of lines (default: 2000) */
39
- maxLines?: number;
40
- /** Maximum number of bytes (default: 50KB) */
41
- maxBytes?: number;
42
- }
43
- /**
44
- * Format bytes as human-readable size.
45
- */
46
- export declare function formatSize(bytes: number): string;
47
- /**
48
- * Truncate content from the head (keep first N lines/bytes).
49
- * Suitable for file reads where you want to see the beginning.
50
- *
51
- * Never returns partial lines. If first line exceeds byte limit,
52
- * returns empty content with firstLineExceedsLimit=true.
53
- */
54
- export declare function truncateHead(content: string, options?: TruncationOptions): TruncationResult;
55
- /**
56
- * Truncate content from the tail (keep last N lines/bytes).
57
- * Suitable for bash output where you want to see the end (errors, final results).
58
- *
59
- * May return partial first line if the last line of original content exceeds byte limit.
60
- */
61
- export declare function truncateTail(content: string, options?: TruncationOptions): TruncationResult;
62
- /**
63
- * Truncate a single line to max characters, adding [truncated] suffix.
64
- * Used for grep match lines.
65
- */
66
- export declare function truncateLine(line: string, maxChars?: number): {
67
- text: string;
68
- wasTruncated: boolean;
69
- };
70
- //# sourceMappingURL=truncate.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"truncate.d.ts","sourceRoot":"","sources":["../../../src/harness/utils/truncate.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,eAAO,MAAM,iBAAiB,OAAO,CAAC;AACtC,eAAO,MAAM,iBAAiB,QAAY,CAAC;AAC3C,eAAO,MAAM,oBAAoB,MAAM,CAAC;AAExC,MAAM,WAAW,gBAAgB;IAChC,4BAA4B;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,kCAAkC;IAClC,SAAS,EAAE,OAAO,CAAC;IACnB,sEAAsE;IACtE,WAAW,EAAE,OAAO,GAAG,OAAO,GAAG,IAAI,CAAC;IACtC,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,oDAAoD;IACpD,UAAU,EAAE,MAAM,CAAC;IACnB,uDAAuD;IACvD,WAAW,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;IACpB,yFAAyF;IACzF,eAAe,EAAE,OAAO,CAAC;IACzB,2EAA2E;IAC3E,qBAAqB,EAAE,OAAO,CAAC;IAC/B,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;IACjB,2CAA2C;IAC3C,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IACjC,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAmED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAQhD;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,gBAAgB,CAkF/F;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,gBAAgB,CAyE/F;AA2CD;;;GAGG;AACH,wBAAgB,YAAY,CAC3B,IAAI,EAAE,MAAM,EACZ,QAAQ,GAAE,MAA6B,GACrC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,OAAO,CAAA;CAAE,CAKzC","sourcesContent":["/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines (except bash tail truncation edge case).\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\nexport const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\ninterface RuntimeBuffer {\n\tbyteLength(content: string, encoding: \"utf8\"): number;\n}\n\nconst runtimeBuffer = (globalThis as { Buffer?: RuntimeBuffer }).Buffer;\nconst nonAsciiPattern = /[^\\x00-\\x7f]/;\n\nfunction utf8ByteLength(content: string): number {\n\tif (runtimeBuffer) return runtimeBuffer.byteLength(content, \"utf8\");\n\n\tconst firstNonAscii = content.search(nonAsciiPattern);\n\tif (firstNonAscii === -1) return content.length;\n\n\tlet bytes = firstNonAscii;\n\tfor (let i = firstNonAscii; i < content.length; i++) {\n\t\tconst code = content.charCodeAt(i);\n\t\tif (code <= 0x7f) {\n\t\t\tbytes += 1;\n\t\t} else if (code <= 0x7ff) {\n\t\t\tbytes += 2;\n\t\t} else if (code >= 0xd800 && code <= 0xdbff && i + 1 < content.length) {\n\t\t\tconst next = content.charCodeAt(i + 1);\n\t\t\tif (next >= 0xdc00 && next <= 0xdfff) {\n\t\t\t\tbytes += 4;\n\t\t\t\ti++;\n\t\t\t} else {\n\t\t\t\tbytes += 3;\n\t\t\t}\n\t\t} else {\n\t\t\tbytes += 3;\n\t\t}\n\t}\n\treturn bytes;\n}\n\nfunction splitLinesForCounting(content: string): string[] {\n\tif (content.length === 0) return [];\n\tconst lines = content.split(\"\\n\");\n\tif (content.endsWith(\"\\n\")) lines.pop();\n\treturn lines;\n}\n\nfunction replaceUnpairedSurrogates(content: string): string {\n\tlet output = \"\";\n\tfor (let i = 0; i < content.length; i++) {\n\t\tconst code = content.charCodeAt(i);\n\t\tif (code >= 0xd800 && code <= 0xdbff) {\n\t\t\tif (i + 1 < content.length) {\n\t\t\t\tconst next = content.charCodeAt(i + 1);\n\t\t\t\tif (next >= 0xdc00 && next <= 0xdfff) {\n\t\t\t\t\toutput += content[i] + content[i + 1];\n\t\t\t\t\ti++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\toutput += \"�\";\n\t\t} else if (code >= 0xdc00 && code <= 0xdfff) {\n\t\t\toutput += \"�\";\n\t\t} else {\n\t\t\toutput += content[i];\n\t\t}\n\t}\n\treturn output;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = utf8ByteLength(content);\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = utf8ByteLength(lines[0]);\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = utf8ByteLength(line) + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = utf8ByteLength(outputContent);\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate content from the tail (keep last N lines/bytes).\n * Suitable for bash output where you want to see the end (errors, final results).\n *\n * May return partial first line if the last line of original content exceeds byte limit.\n */\nexport function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = utf8ByteLength(content);\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Work backwards from the end\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\tlet lastLinePartial = false;\n\n\tfor (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = utf8ByteLength(line) + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\t// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,\n\t\t\t// take the end of the line (partial)\n\t\t\tif (outputLinesArr.length === 0) {\n\t\t\t\tconst truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);\n\t\t\t\toutputLinesArr.unshift(truncatedLine);\n\t\t\t\toutputBytesCount = utf8ByteLength(truncatedLine);\n\t\t\t\tlastLinePartial = true;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.unshift(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = utf8ByteLength(outputContent);\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate a string to fit within a byte limit (from the end).\n * Handles multi-byte UTF-8 characters correctly.\n */\nfunction truncateStringToBytesFromEnd(str: string, maxBytes: number): string {\n\tif (maxBytes <= 0) return \"\";\n\n\tlet outputBytes = 0;\n\tlet start = str.length;\n\tlet needsReplacement = false;\n\tfor (let i = str.length; i > 0; ) {\n\t\tlet characterStart = i - 1;\n\t\tconst code = str.charCodeAt(characterStart);\n\t\tlet characterBytes: number;\n\t\tlet unpairedSurrogate = false;\n\t\tif (code >= 0xdc00 && code <= 0xdfff && characterStart > 0) {\n\t\t\tconst previous = str.charCodeAt(characterStart - 1);\n\t\t\tif (previous >= 0xd800 && previous <= 0xdbff) {\n\t\t\t\tcharacterStart--;\n\t\t\t\tcharacterBytes = 4;\n\t\t\t} else {\n\t\t\t\tcharacterBytes = 3;\n\t\t\t\tunpairedSurrogate = true;\n\t\t\t}\n\t\t} else if (code >= 0xd800 && code <= 0xdfff) {\n\t\t\tcharacterBytes = 3;\n\t\t\tunpairedSurrogate = true;\n\t\t} else {\n\t\t\tcharacterBytes = code <= 0x7f ? 1 : code <= 0x7ff ? 2 : 3;\n\t\t}\n\t\tif (outputBytes + characterBytes > maxBytes) break;\n\t\toutputBytes += characterBytes;\n\t\tstart = characterStart;\n\t\tneedsReplacement ||= unpairedSurrogate;\n\t\ti = characterStart;\n\t}\n\n\tconst output = str.slice(start);\n\treturn needsReplacement ? replaceUnpairedSurrogates(output) : output;\n}\n\n/**\n * Truncate a single line to max characters, adding [truncated] suffix.\n * Used for grep match lines.\n */\nexport function truncateLine(\n\tline: string,\n\tmaxChars: number = GREP_MAX_LINE_LENGTH,\n): { text: string; wasTruncated: boolean } {\n\tif (line.length <= maxChars) {\n\t\treturn { text: line, wasTruncated: false };\n\t}\n\treturn { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };\n}\n"]}
@@ -1,296 +0,0 @@
1
- /**
2
- * Shared truncation utilities for tool outputs.
3
- *
4
- * Truncation is based on two independent limits - whichever is hit first wins:
5
- * - Line limit (default: 2000 lines)
6
- * - Byte limit (default: 50KB)
7
- *
8
- * Never returns partial lines (except bash tail truncation edge case).
9
- */
10
- export const DEFAULT_MAX_LINES = 2000;
11
- export const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB
12
- export const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line
13
- const runtimeBuffer = globalThis.Buffer;
14
- const nonAsciiPattern = /[^\x00-\x7f]/;
15
- function utf8ByteLength(content) {
16
- if (runtimeBuffer)
17
- return runtimeBuffer.byteLength(content, "utf8");
18
- const firstNonAscii = content.search(nonAsciiPattern);
19
- if (firstNonAscii === -1)
20
- return content.length;
21
- let bytes = firstNonAscii;
22
- for (let i = firstNonAscii; i < content.length; i++) {
23
- const code = content.charCodeAt(i);
24
- if (code <= 0x7f) {
25
- bytes += 1;
26
- }
27
- else if (code <= 0x7ff) {
28
- bytes += 2;
29
- }
30
- else if (code >= 0xd800 && code <= 0xdbff && i + 1 < content.length) {
31
- const next = content.charCodeAt(i + 1);
32
- if (next >= 0xdc00 && next <= 0xdfff) {
33
- bytes += 4;
34
- i++;
35
- }
36
- else {
37
- bytes += 3;
38
- }
39
- }
40
- else {
41
- bytes += 3;
42
- }
43
- }
44
- return bytes;
45
- }
46
- function splitLinesForCounting(content) {
47
- if (content.length === 0)
48
- return [];
49
- const lines = content.split("\n");
50
- if (content.endsWith("\n"))
51
- lines.pop();
52
- return lines;
53
- }
54
- function replaceUnpairedSurrogates(content) {
55
- let output = "";
56
- for (let i = 0; i < content.length; i++) {
57
- const code = content.charCodeAt(i);
58
- if (code >= 0xd800 && code <= 0xdbff) {
59
- if (i + 1 < content.length) {
60
- const next = content.charCodeAt(i + 1);
61
- if (next >= 0xdc00 && next <= 0xdfff) {
62
- output += content[i] + content[i + 1];
63
- i++;
64
- continue;
65
- }
66
- }
67
- output += "�";
68
- }
69
- else if (code >= 0xdc00 && code <= 0xdfff) {
70
- output += "�";
71
- }
72
- else {
73
- output += content[i];
74
- }
75
- }
76
- return output;
77
- }
78
- /**
79
- * Format bytes as human-readable size.
80
- */
81
- export function formatSize(bytes) {
82
- if (bytes < 1024) {
83
- return `${bytes}B`;
84
- }
85
- else if (bytes < 1024 * 1024) {
86
- return `${(bytes / 1024).toFixed(1)}KB`;
87
- }
88
- else {
89
- return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
90
- }
91
- }
92
- /**
93
- * Truncate content from the head (keep first N lines/bytes).
94
- * Suitable for file reads where you want to see the beginning.
95
- *
96
- * Never returns partial lines. If first line exceeds byte limit,
97
- * returns empty content with firstLineExceedsLimit=true.
98
- */
99
- export function truncateHead(content, options = {}) {
100
- const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
101
- const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
102
- const totalBytes = utf8ByteLength(content);
103
- const lines = splitLinesForCounting(content);
104
- const totalLines = lines.length;
105
- // Check if no truncation needed
106
- if (totalLines <= maxLines && totalBytes <= maxBytes) {
107
- return {
108
- content,
109
- truncated: false,
110
- truncatedBy: null,
111
- totalLines,
112
- totalBytes,
113
- outputLines: totalLines,
114
- outputBytes: totalBytes,
115
- lastLinePartial: false,
116
- firstLineExceedsLimit: false,
117
- maxLines,
118
- maxBytes,
119
- };
120
- }
121
- // Check if first line alone exceeds byte limit
122
- const firstLineBytes = utf8ByteLength(lines[0]);
123
- if (firstLineBytes > maxBytes) {
124
- return {
125
- content: "",
126
- truncated: true,
127
- truncatedBy: "bytes",
128
- totalLines,
129
- totalBytes,
130
- outputLines: 0,
131
- outputBytes: 0,
132
- lastLinePartial: false,
133
- firstLineExceedsLimit: true,
134
- maxLines,
135
- maxBytes,
136
- };
137
- }
138
- // Collect complete lines that fit
139
- const outputLinesArr = [];
140
- let outputBytesCount = 0;
141
- let truncatedBy = "lines";
142
- for (let i = 0; i < lines.length && i < maxLines; i++) {
143
- const line = lines[i];
144
- const lineBytes = utf8ByteLength(line) + (i > 0 ? 1 : 0); // +1 for newline
145
- if (outputBytesCount + lineBytes > maxBytes) {
146
- truncatedBy = "bytes";
147
- break;
148
- }
149
- outputLinesArr.push(line);
150
- outputBytesCount += lineBytes;
151
- }
152
- // If we exited due to line limit
153
- if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
154
- truncatedBy = "lines";
155
- }
156
- const outputContent = outputLinesArr.join("\n");
157
- const finalOutputBytes = utf8ByteLength(outputContent);
158
- return {
159
- content: outputContent,
160
- truncated: true,
161
- truncatedBy,
162
- totalLines,
163
- totalBytes,
164
- outputLines: outputLinesArr.length,
165
- outputBytes: finalOutputBytes,
166
- lastLinePartial: false,
167
- firstLineExceedsLimit: false,
168
- maxLines,
169
- maxBytes,
170
- };
171
- }
172
- /**
173
- * Truncate content from the tail (keep last N lines/bytes).
174
- * Suitable for bash output where you want to see the end (errors, final results).
175
- *
176
- * May return partial first line if the last line of original content exceeds byte limit.
177
- */
178
- export function truncateTail(content, options = {}) {
179
- const maxLines = options.maxLines ?? DEFAULT_MAX_LINES;
180
- const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
181
- const totalBytes = utf8ByteLength(content);
182
- const lines = splitLinesForCounting(content);
183
- const totalLines = lines.length;
184
- // Check if no truncation needed
185
- if (totalLines <= maxLines && totalBytes <= maxBytes) {
186
- return {
187
- content,
188
- truncated: false,
189
- truncatedBy: null,
190
- totalLines,
191
- totalBytes,
192
- outputLines: totalLines,
193
- outputBytes: totalBytes,
194
- lastLinePartial: false,
195
- firstLineExceedsLimit: false,
196
- maxLines,
197
- maxBytes,
198
- };
199
- }
200
- // Work backwards from the end
201
- const outputLinesArr = [];
202
- let outputBytesCount = 0;
203
- let truncatedBy = "lines";
204
- let lastLinePartial = false;
205
- for (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {
206
- const line = lines[i];
207
- const lineBytes = utf8ByteLength(line) + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline
208
- if (outputBytesCount + lineBytes > maxBytes) {
209
- truncatedBy = "bytes";
210
- // Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,
211
- // take the end of the line (partial)
212
- if (outputLinesArr.length === 0) {
213
- const truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);
214
- outputLinesArr.unshift(truncatedLine);
215
- outputBytesCount = utf8ByteLength(truncatedLine);
216
- lastLinePartial = true;
217
- }
218
- break;
219
- }
220
- outputLinesArr.unshift(line);
221
- outputBytesCount += lineBytes;
222
- }
223
- // If we exited due to line limit
224
- if (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {
225
- truncatedBy = "lines";
226
- }
227
- const outputContent = outputLinesArr.join("\n");
228
- const finalOutputBytes = utf8ByteLength(outputContent);
229
- return {
230
- content: outputContent,
231
- truncated: true,
232
- truncatedBy,
233
- totalLines,
234
- totalBytes,
235
- outputLines: outputLinesArr.length,
236
- outputBytes: finalOutputBytes,
237
- lastLinePartial,
238
- firstLineExceedsLimit: false,
239
- maxLines,
240
- maxBytes,
241
- };
242
- }
243
- /**
244
- * Truncate a string to fit within a byte limit (from the end).
245
- * Handles multi-byte UTF-8 characters correctly.
246
- */
247
- function truncateStringToBytesFromEnd(str, maxBytes) {
248
- if (maxBytes <= 0)
249
- return "";
250
- let outputBytes = 0;
251
- let start = str.length;
252
- let needsReplacement = false;
253
- for (let i = str.length; i > 0;) {
254
- let characterStart = i - 1;
255
- const code = str.charCodeAt(characterStart);
256
- let characterBytes;
257
- let unpairedSurrogate = false;
258
- if (code >= 0xdc00 && code <= 0xdfff && characterStart > 0) {
259
- const previous = str.charCodeAt(characterStart - 1);
260
- if (previous >= 0xd800 && previous <= 0xdbff) {
261
- characterStart--;
262
- characterBytes = 4;
263
- }
264
- else {
265
- characterBytes = 3;
266
- unpairedSurrogate = true;
267
- }
268
- }
269
- else if (code >= 0xd800 && code <= 0xdfff) {
270
- characterBytes = 3;
271
- unpairedSurrogate = true;
272
- }
273
- else {
274
- characterBytes = code <= 0x7f ? 1 : code <= 0x7ff ? 2 : 3;
275
- }
276
- if (outputBytes + characterBytes > maxBytes)
277
- break;
278
- outputBytes += characterBytes;
279
- start = characterStart;
280
- needsReplacement ||= unpairedSurrogate;
281
- i = characterStart;
282
- }
283
- const output = str.slice(start);
284
- return needsReplacement ? replaceUnpairedSurrogates(output) : output;
285
- }
286
- /**
287
- * Truncate a single line to max characters, adding [truncated] suffix.
288
- * Used for grep match lines.
289
- */
290
- export function truncateLine(line, maxChars = GREP_MAX_LINE_LENGTH) {
291
- if (line.length <= maxChars) {
292
- return { text: line, wasTruncated: false };
293
- }
294
- return { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };
295
- }
296
- //# sourceMappingURL=truncate.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"truncate.js","sourceRoot":"","sources":["../../../src/harness/utils/truncate.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CAAC,CAAC;YACnB,iBAAiB,GAAG,IAAI,CAAC;QAC1B,CAAC;aAAM,CAAC;YACP,cAAc,GAAG,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3D,CAAC;QACD,IAAI,WAAW,GAAG,cAAc,GAAG,QAAQ;YAAE,MAAM;QACnD,WAAW,IAAI,cAAc,CAAC;QAC9B,KAAK,GAAG,cAAc,CAAC;QACvB,gBAAgB,KAAK,iBAAiB,CAAC;QACvC,CAAC,GAAG,cAAc,CAAC;IACpB,CAAC;IAED,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAChC,OAAO,gBAAgB,CAAC,CAAC,CAAC,yBAAyB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;AAAA,CACrE;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY,CAC3B,IAAY,EACZ,QAAQ,GAAW,oBAAoB,EACG;IAC1C,IAAI,IAAI,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC7B,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,CAAC;IAC5C,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,iBAAiB,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;AAAA,CACjF","sourcesContent":["/**\n * Shared truncation utilities for tool outputs.\n *\n * Truncation is based on two independent limits - whichever is hit first wins:\n * - Line limit (default: 2000 lines)\n * - Byte limit (default: 50KB)\n *\n * Never returns partial lines (except bash tail truncation edge case).\n */\n\nexport const DEFAULT_MAX_LINES = 2000;\nexport const DEFAULT_MAX_BYTES = 50 * 1024; // 50KB\nexport const GREP_MAX_LINE_LENGTH = 500; // Max chars per grep match line\n\nexport interface TruncationResult {\n\t/** The truncated content */\n\tcontent: string;\n\t/** Whether truncation occurred */\n\ttruncated: boolean;\n\t/** Which limit was hit: \"lines\", \"bytes\", or null if not truncated */\n\ttruncatedBy: \"lines\" | \"bytes\" | null;\n\t/** Total number of lines in the original content */\n\ttotalLines: number;\n\t/** Total number of bytes in the original content */\n\ttotalBytes: number;\n\t/** Number of complete lines in the truncated output */\n\toutputLines: number;\n\t/** Number of bytes in the truncated output */\n\toutputBytes: number;\n\t/** Whether the last line was partially truncated (only for tail truncation edge case) */\n\tlastLinePartial: boolean;\n\t/** Whether the first line exceeded the byte limit (for head truncation) */\n\tfirstLineExceedsLimit: boolean;\n\t/** The max lines limit that was applied */\n\tmaxLines: number;\n\t/** The max bytes limit that was applied */\n\tmaxBytes: number;\n}\n\nexport interface TruncationOptions {\n\t/** Maximum number of lines (default: 2000) */\n\tmaxLines?: number;\n\t/** Maximum number of bytes (default: 50KB) */\n\tmaxBytes?: number;\n}\n\ninterface RuntimeBuffer {\n\tbyteLength(content: string, encoding: \"utf8\"): number;\n}\n\nconst runtimeBuffer = (globalThis as { Buffer?: RuntimeBuffer }).Buffer;\nconst nonAsciiPattern = /[^\\x00-\\x7f]/;\n\nfunction utf8ByteLength(content: string): number {\n\tif (runtimeBuffer) return runtimeBuffer.byteLength(content, \"utf8\");\n\n\tconst firstNonAscii = content.search(nonAsciiPattern);\n\tif (firstNonAscii === -1) return content.length;\n\n\tlet bytes = firstNonAscii;\n\tfor (let i = firstNonAscii; i < content.length; i++) {\n\t\tconst code = content.charCodeAt(i);\n\t\tif (code <= 0x7f) {\n\t\t\tbytes += 1;\n\t\t} else if (code <= 0x7ff) {\n\t\t\tbytes += 2;\n\t\t} else if (code >= 0xd800 && code <= 0xdbff && i + 1 < content.length) {\n\t\t\tconst next = content.charCodeAt(i + 1);\n\t\t\tif (next >= 0xdc00 && next <= 0xdfff) {\n\t\t\t\tbytes += 4;\n\t\t\t\ti++;\n\t\t\t} else {\n\t\t\t\tbytes += 3;\n\t\t\t}\n\t\t} else {\n\t\t\tbytes += 3;\n\t\t}\n\t}\n\treturn bytes;\n}\n\nfunction splitLinesForCounting(content: string): string[] {\n\tif (content.length === 0) return [];\n\tconst lines = content.split(\"\\n\");\n\tif (content.endsWith(\"\\n\")) lines.pop();\n\treturn lines;\n}\n\nfunction replaceUnpairedSurrogates(content: string): string {\n\tlet output = \"\";\n\tfor (let i = 0; i < content.length; i++) {\n\t\tconst code = content.charCodeAt(i);\n\t\tif (code >= 0xd800 && code <= 0xdbff) {\n\t\t\tif (i + 1 < content.length) {\n\t\t\t\tconst next = content.charCodeAt(i + 1);\n\t\t\t\tif (next >= 0xdc00 && next <= 0xdfff) {\n\t\t\t\t\toutput += content[i] + content[i + 1];\n\t\t\t\t\ti++;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\t\t\toutput += \"�\";\n\t\t} else if (code >= 0xdc00 && code <= 0xdfff) {\n\t\t\toutput += \"�\";\n\t\t} else {\n\t\t\toutput += content[i];\n\t\t}\n\t}\n\treturn output;\n}\n\n/**\n * Format bytes as human-readable size.\n */\nexport function formatSize(bytes: number): string {\n\tif (bytes < 1024) {\n\t\treturn `${bytes}B`;\n\t} else if (bytes < 1024 * 1024) {\n\t\treturn `${(bytes / 1024).toFixed(1)}KB`;\n\t} else {\n\t\treturn `${(bytes / (1024 * 1024)).toFixed(1)}MB`;\n\t}\n}\n\n/**\n * Truncate content from the head (keep first N lines/bytes).\n * Suitable for file reads where you want to see the beginning.\n *\n * Never returns partial lines. If first line exceeds byte limit,\n * returns empty content with firstLineExceedsLimit=true.\n */\nexport function truncateHead(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = utf8ByteLength(content);\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Check if first line alone exceeds byte limit\n\tconst firstLineBytes = utf8ByteLength(lines[0]);\n\tif (firstLineBytes > maxBytes) {\n\t\treturn {\n\t\t\tcontent: \"\",\n\t\t\ttruncated: true,\n\t\t\ttruncatedBy: \"bytes\",\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: 0,\n\t\t\toutputBytes: 0,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: true,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Collect complete lines that fit\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\n\tfor (let i = 0; i < lines.length && i < maxLines; i++) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = utf8ByteLength(line) + (i > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.push(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = utf8ByteLength(outputContent);\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial: false,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate content from the tail (keep last N lines/bytes).\n * Suitable for bash output where you want to see the end (errors, final results).\n *\n * May return partial first line if the last line of original content exceeds byte limit.\n */\nexport function truncateTail(content: string, options: TruncationOptions = {}): TruncationResult {\n\tconst maxLines = options.maxLines ?? DEFAULT_MAX_LINES;\n\tconst maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;\n\n\tconst totalBytes = utf8ByteLength(content);\n\tconst lines = splitLinesForCounting(content);\n\tconst totalLines = lines.length;\n\n\t// Check if no truncation needed\n\tif (totalLines <= maxLines && totalBytes <= maxBytes) {\n\t\treturn {\n\t\t\tcontent,\n\t\t\ttruncated: false,\n\t\t\ttruncatedBy: null,\n\t\t\ttotalLines,\n\t\t\ttotalBytes,\n\t\t\toutputLines: totalLines,\n\t\t\toutputBytes: totalBytes,\n\t\t\tlastLinePartial: false,\n\t\t\tfirstLineExceedsLimit: false,\n\t\t\tmaxLines,\n\t\t\tmaxBytes,\n\t\t};\n\t}\n\n\t// Work backwards from the end\n\tconst outputLinesArr: string[] = [];\n\tlet outputBytesCount = 0;\n\tlet truncatedBy: \"lines\" | \"bytes\" = \"lines\";\n\tlet lastLinePartial = false;\n\n\tfor (let i = lines.length - 1; i >= 0 && outputLinesArr.length < maxLines; i--) {\n\t\tconst line = lines[i];\n\t\tconst lineBytes = utf8ByteLength(line) + (outputLinesArr.length > 0 ? 1 : 0); // +1 for newline\n\n\t\tif (outputBytesCount + lineBytes > maxBytes) {\n\t\t\ttruncatedBy = \"bytes\";\n\t\t\t// Edge case: if we haven't added ANY lines yet and this line exceeds maxBytes,\n\t\t\t// take the end of the line (partial)\n\t\t\tif (outputLinesArr.length === 0) {\n\t\t\t\tconst truncatedLine = truncateStringToBytesFromEnd(line, maxBytes);\n\t\t\t\toutputLinesArr.unshift(truncatedLine);\n\t\t\t\toutputBytesCount = utf8ByteLength(truncatedLine);\n\t\t\t\tlastLinePartial = true;\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\toutputLinesArr.unshift(line);\n\t\toutputBytesCount += lineBytes;\n\t}\n\n\t// If we exited due to line limit\n\tif (outputLinesArr.length >= maxLines && outputBytesCount <= maxBytes) {\n\t\ttruncatedBy = \"lines\";\n\t}\n\n\tconst outputContent = outputLinesArr.join(\"\\n\");\n\tconst finalOutputBytes = utf8ByteLength(outputContent);\n\n\treturn {\n\t\tcontent: outputContent,\n\t\ttruncated: true,\n\t\ttruncatedBy,\n\t\ttotalLines,\n\t\ttotalBytes,\n\t\toutputLines: outputLinesArr.length,\n\t\toutputBytes: finalOutputBytes,\n\t\tlastLinePartial,\n\t\tfirstLineExceedsLimit: false,\n\t\tmaxLines,\n\t\tmaxBytes,\n\t};\n}\n\n/**\n * Truncate a string to fit within a byte limit (from the end).\n * Handles multi-byte UTF-8 characters correctly.\n */\nfunction truncateStringToBytesFromEnd(str: string, maxBytes: number): string {\n\tif (maxBytes <= 0) return \"\";\n\n\tlet outputBytes = 0;\n\tlet start = str.length;\n\tlet needsReplacement = false;\n\tfor (let i = str.length; i > 0; ) {\n\t\tlet characterStart = i - 1;\n\t\tconst code = str.charCodeAt(characterStart);\n\t\tlet characterBytes: number;\n\t\tlet unpairedSurrogate = false;\n\t\tif (code >= 0xdc00 && code <= 0xdfff && characterStart > 0) {\n\t\t\tconst previous = str.charCodeAt(characterStart - 1);\n\t\t\tif (previous >= 0xd800 && previous <= 0xdbff) {\n\t\t\t\tcharacterStart--;\n\t\t\t\tcharacterBytes = 4;\n\t\t\t} else {\n\t\t\t\tcharacterBytes = 3;\n\t\t\t\tunpairedSurrogate = true;\n\t\t\t}\n\t\t} else if (code >= 0xd800 && code <= 0xdfff) {\n\t\t\tcharacterBytes = 3;\n\t\t\tunpairedSurrogate = true;\n\t\t} else {\n\t\t\tcharacterBytes = code <= 0x7f ? 1 : code <= 0x7ff ? 2 : 3;\n\t\t}\n\t\tif (outputBytes + characterBytes > maxBytes) break;\n\t\toutputBytes += characterBytes;\n\t\tstart = characterStart;\n\t\tneedsReplacement ||= unpairedSurrogate;\n\t\ti = characterStart;\n\t}\n\n\tconst output = str.slice(start);\n\treturn needsReplacement ? replaceUnpairedSurrogates(output) : output;\n}\n\n/**\n * Truncate a single line to max characters, adding [truncated] suffix.\n * Used for grep match lines.\n */\nexport function truncateLine(\n\tline: string,\n\tmaxChars: number = GREP_MAX_LINE_LENGTH,\n): { text: string; wasTruncated: boolean } {\n\tif (line.length <= maxChars) {\n\t\treturn { text: line, wasTruncated: false };\n\t}\n\treturn { text: `${line.slice(0, maxChars)}... [truncated]`, wasTruncated: true };\n}\n"]}
package/dist/node.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export { NodeExecutionEnv } from "./harness/env/nodejs.ts";
2
- export * from "./index.ts";
3
- //# sourceMappingURL=node.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"node.d.ts","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,cAAc,YAAY,CAAC","sourcesContent":["export { NodeExecutionEnv } from \"./harness/env/nodejs.ts\";\nexport * from \"./index.ts\";\n"]}
package/dist/node.js DELETED
@@ -1,3 +0,0 @@
1
- export { NodeExecutionEnv } from "./harness/env/nodejs.js";
2
- export * from "./index.js";
3
- //# sourceMappingURL=node.js.map
package/dist/node.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,cAAc,YAAY,CAAC","sourcesContent":["export { NodeExecutionEnv } from \"./harness/env/nodejs.ts\";\nexport * from \"./index.ts\";\n"]}