Skip to content

Commit f6d7fe4

Browse files
authored
refactor(agents): share trailing empty-line trimming (#133837)
1 parent 8b6cc1c commit f6d7fe4

4 files changed

Lines changed: 50 additions & 18 deletions

File tree

‎src/agents/sessions/tools/read.ts‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ import {
4343
readToolInputSchema,
4444
readToolOutputSchema,
4545
} from "./read-tool-contract.js";
46-
import { getTextOutput, invalidArgText, replaceTabs, shortenPath, str } from "./render-utils.js";
46+
import {
47+
getTextOutput,
48+
invalidArgText,
49+
replaceTabs,
50+
shortenPath,
51+
str,
52+
trimTrailingEmptyLines,
53+
} from "./render-utils.js";
4754
import type { ReadToolDetails } from "./tool-contracts.js";
4855
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
4956
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize } from "./truncate.js";
@@ -182,14 +189,6 @@ function formatReadCall(args: ReadRenderArgs | undefined, theme: Theme): string
182189
return `${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}${formatReadLineRange(args, theme)}`;
183190
}
184191

185-
function trimTrailingEmptyLines(lines: string[]): string[] {
186-
let end = lines.length;
187-
while (end > 0 && lines[end - 1] === "") {
188-
end--;
189-
}
190-
return lines.slice(0, end);
191-
}
192-
193192
function getOpenClawDocsClassification(
194193
absolutePath: string,
195194
): CompactReadClassification | undefined {

‎src/agents/sessions/tools/render-utils.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import fs from "node:fs";
22
import * as os from "node:os";
33
import path from "node:path";
44
import { describe, expect, it } from "vitest";
5-
import { appendSessionToolTruncationWarning, shortenPath } from "./render-utils.js";
5+
import {
6+
appendSessionToolTruncationWarning,
7+
shortenPath,
8+
trimTrailingEmptyLines,
9+
} from "./render-utils.js";
610

711
const theme = {
812
fg: (color: string, text: string) => `<${color}>${text}</${color}>`,
@@ -26,6 +30,38 @@ describe("appendSessionToolTruncationWarning", () => {
2630
});
2731
});
2832

33+
describe("trimTrailingEmptyLines", () => {
34+
it.each([
35+
{
36+
name: "removes multiple trailing empty lines",
37+
lines: ["first", "second", "", ""],
38+
expected: ["first", "second"],
39+
},
40+
{ name: "removes all-empty input", lines: ["", ""], expected: [] },
41+
{
42+
name: "keeps leading and interior empty lines",
43+
lines: ["", "first", "", "second", ""],
44+
expected: ["", "first", "", "second"],
45+
},
46+
{
47+
name: "keeps whitespace-only trailing lines",
48+
lines: ["first", " ", "\t"],
49+
expected: ["first", " ", "\t"],
50+
},
51+
])("$name", ({ lines, expected }) => {
52+
expect(trimTrailingEmptyLines(lines)).toEqual(expected);
53+
});
54+
55+
it("does not mutate the caller-owned lines", () => {
56+
const lines = ["first", "", ""];
57+
const original = [...lines];
58+
59+
trimTrailingEmptyLines(lines);
60+
61+
expect(lines).toEqual(original);
62+
});
63+
});
64+
2965
describe("shortenPath", () => {
3066
const home = os.homedir();
3167

‎src/agents/sessions/tools/render-utils.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export function normalizeDisplayText(text: string): string {
4141
return text.replace(/\r/g, "");
4242
}
4343

44+
export function trimTrailingEmptyLines(lines: readonly string[]): string[] {
45+
return lines.slice(0, lines.findLastIndex((line) => line !== "") + 1);
46+
}
47+
4448
/** Extracts text output and image placeholders from a tool result. */
4549
export function getTextOutput(
4650
result:

‎src/agents/sessions/tools/write.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
replaceTabs,
3333
shortenPath,
3434
str,
35+
trimTrailingEmptyLines,
3536
} from "./render-utils.js";
3637
import type { WriteToolDetails } from "./tool-contracts.js";
3738
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
@@ -248,14 +249,6 @@ function updateWriteHighlightCacheIncremental(
248249
return cache;
249250
}
250251

251-
function trimTrailingEmptyLines(lines: string[]): string[] {
252-
let end = lines.length;
253-
while (end > 0 && lines[end - 1] === "") {
254-
end--;
255-
}
256-
return lines.slice(0, end);
257-
}
258-
259252
function formatWriteCall(
260253
args: { path?: string; file_path?: string; content?: string } | undefined,
261254
options: ToolRenderResultOptions,

0 commit comments

Comments
 (0)