Skip to content

Commit 6704da3

Browse files
authored
refactor(agents): consolidate session text component reuse (#134105)
* refactor(agents): consolidate session text component reuse * chore: refresh CI after Windows worker repair * chore: refresh CI after gateway test repair
1 parent 258c737 commit 6704da3

9 files changed

Lines changed: 66 additions & 55 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,14 +1991,11 @@ src/agents/sessions/session-manager-persistence.ts 2
19911991
src/agents/sessions/session-manager.ts 2
19921992
src/agents/sessions/settings-manager.ts 11
19931993
src/agents/sessions/steering-message-identity.ts 2
1994-
src/agents/sessions/tools/bash.ts 3
1994+
src/agents/sessions/tools/bash.ts 2
19951995
src/agents/sessions/tools/edit.ts 14
1996-
src/agents/sessions/tools/find.ts 2
1997-
src/agents/sessions/tools/grep.ts 4
1998-
src/agents/sessions/tools/ls.ts 2
1999-
src/agents/sessions/tools/read.ts 2
1996+
src/agents/sessions/tools/grep.ts 2
20001997
src/agents/sessions/tools/tool-definition-wrapper.ts 1
2001-
src/agents/sessions/tools/write.ts 4
1998+
src/agents/sessions/tools/write.ts 3
20021999
src/agents/shell-snapshot.ts 1
20032000
src/agents/spawn-plan.ts 1
20042001
src/agents/subagents/announce/subagent-announce-completion-delivery.ts 2

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";
2626
import type { BashOperations } from "./bash-operations.js";
2727
import { OutputAccumulator } from "./output-accumulator.js";
28-
import { getTextOutput, invalidArgText, str } from "./render-utils.js";
28+
import { getTextOutput, invalidArgText, reuseTextComponent, str } from "./render-utils.js";
2929
import { formatFullOutputFooter, type BashToolDetails } from "./tool-contracts.js";
3030
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
3131
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize } from "./truncate.js";
@@ -493,9 +493,7 @@ export function createBashToolDefinition(
493493
state.startedAt = Date.now();
494494
state.endedAt = undefined;
495495
}
496-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
497-
text.setText(formatBashCall(args));
498-
return text;
496+
return reuseTextComponent(context.lastComponent, formatBashCall(args));
499497
},
500498
renderResult(result, optionsLocal, themeLocal, context) {
501499
void themeLocal;

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { existsSync } from "node:fs";
22
import path from "node:path";
33
import { createInterface } from "node:readline";
4-
import { Text } from "@earendil-works/pi-tui";
54
import { Type } from "typebox";
65
import { releaseChildProcessOutputAfterExit } from "../../../process/child-process.js";
76
import { spawnCommand } from "../../../process/exec.js";
@@ -20,6 +19,7 @@ import {
2019
appendSessionToolTruncationWarning,
2120
formatSessionToolOutput,
2221
invalidArgText,
22+
reuseTextComponent,
2323
shortenPath,
2424
str,
2525
} from "./render-utils.js";
@@ -383,14 +383,11 @@ export function createFindToolDefinition(
383383
});
384384
},
385385
renderCall(args, theme, context) {
386-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
387-
text.setText(formatFindCall(args, theme));
388-
return text;
386+
return reuseTextComponent(context.lastComponent, formatFindCall(args, theme));
389387
},
390388
renderResult(result, optionsLocal, theme, context) {
391-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
392-
text.setText(formatFindResult(result, optionsLocal, theme, context.showImages));
393-
return text;
389+
const content = formatFindResult(result, optionsLocal, theme, context.showImages);
390+
return reuseTextComponent(context.lastComponent, content);
394391
},
395392
};
396393
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import { statSync } from "node:fs";
77
import path from "node:path";
88
import { createInterface } from "node:readline";
9-
import { Text } from "@earendil-works/pi-tui";
109
import { resolveNonNegativeIntegerOption } from "@openclaw/normalization-core/number-coercion";
1110
import { Type } from "typebox";
1211
import { releaseChildProcessOutputAfterExit } from "../../../process/child-process.js";
@@ -21,6 +20,7 @@ import {
2120
appendSessionToolTruncationWarning,
2221
formatSessionToolOutput,
2322
invalidArgText,
23+
reuseTextComponent,
2424
shortenPath,
2525
str,
2626
} from "./render-utils.js";
@@ -459,14 +459,11 @@ export function createGrepToolDefinition(
459459
});
460460
},
461461
renderCall(args, theme, context) {
462-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
463-
text.setText(formatGrepCall(args, theme));
464-
return text;
462+
return reuseTextComponent(context.lastComponent, formatGrepCall(args, theme));
465463
},
466464
renderResult(result, optionsLocal, theme, context) {
467-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
468-
text.setText(formatGrepResult(result, optionsLocal, theme, context.showImages));
469-
return text;
465+
const content = formatGrepResult(result, optionsLocal, theme, context.showImages);
466+
return reuseTextComponent(context.lastComponent, content);
470467
},
471468
};
472469
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
import { existsSync, readdirSync, statSync } from "node:fs";
77
import nodePath from "node:path";
8-
import { Text } from "@earendil-works/pi-tui";
98
import { Type } from "typebox";
109
import { toErrorObject } from "../../../infra/errors.js";
1110
import type { AgentTool } from "../../runtime/index.js";
@@ -16,6 +15,7 @@ import {
1615
appendSessionToolTruncationWarning,
1716
formatSessionToolOutput,
1817
invalidArgText,
18+
reuseTextComponent,
1919
shortenPath,
2020
str,
2121
} from "./render-utils.js";
@@ -225,14 +225,11 @@ export function createLsToolDefinition(
225225
}
226226
},
227227
renderCall(args, theme, context) {
228-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
229-
text.setText(formatLsCall(args, theme));
230-
return text;
228+
return reuseTextComponent(context.lastComponent, formatLsCall(args, theme));
231229
},
232230
renderResult(result, optionsLocal, theme, context) {
233-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
234-
text.setText(formatLsResult(result, optionsLocal, theme, context.showImages));
235-
return text;
231+
const content = formatLsResult(result, optionsLocal, theme, context.showImages);
232+
return reuseTextComponent(context.lastComponent, content);
236233
},
237234
};
238235
}

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { constants } from "node:fs";
22
import { access as fsAccess, readdir as fsReaddir, stat as fsStat } from "node:fs/promises";
33
import { basename, dirname, isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
4-
import { Text } from "@earendil-works/pi-tui";
54
import { hasErrnoCode, toErrorObject } from "../../../infra/errors.js";
65
import { readRegularFile } from "../../../infra/regular-file.js";
76
import { decodeWindowsTextFileBuffer } from "../../../infra/windows-encoding.js";
@@ -52,6 +51,7 @@ import {
5251
getTextOutput,
5352
invalidArgText,
5453
replaceTabs,
54+
reuseTextComponent,
5555
shortenPath,
5656
str,
5757
trimTrailingEmptyLines,
@@ -623,31 +623,25 @@ export function createReadToolDefinition(
623623
});
624624
},
625625
renderCall(args, theme, context) {
626-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
627626
const classification = !context.expanded
628627
? getCompactReadClassification(args, context.cwd)
629628
: undefined;
630-
text.setText(
631-
classification
632-
? formatCompactReadCall(classification, args, theme)
633-
: formatReadCall(args, theme),
634-
);
635-
return text;
629+
const content = classification
630+
? formatCompactReadCall(classification, args, theme)
631+
: formatReadCall(args, theme);
632+
return reuseTextComponent(context.lastComponent, content);
636633
},
637634
renderResult(result, optionsLocal, theme, context) {
638-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
639-
text.setText(
640-
formatReadResult(
641-
context.args,
642-
result,
643-
optionsLocal,
644-
theme,
645-
context.showImages,
646-
context.cwd,
647-
context.isError,
648-
),
635+
const content = formatReadResult(
636+
context.args,
637+
result,
638+
optionsLocal,
639+
theme,
640+
context.showImages,
641+
context.cwd,
642+
context.isError,
649643
);
650-
return text;
644+
return reuseTextComponent(context.lastComponent, content);
651645
},
652646
};
653647
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import fs from "node:fs";
22
import * as os from "node:os";
33
import path from "node:path";
4+
import { Text } from "@earendil-works/pi-tui";
45
import { describe, expect, it } from "vitest";
56
import {
67
appendSessionToolTruncationWarning,
8+
reuseTextComponent,
79
shortenPath,
810
trimTrailingEmptyLines,
911
} from "./render-utils.js";
@@ -62,6 +64,25 @@ describe("trimTrailingEmptyLines", () => {
6264
});
6365
});
6466

67+
describe("reuseTextComponent", () => {
68+
it("creates a zero-padding Text component when no prior component exists", () => {
69+
const component = reuseTextComponent(undefined, "hello");
70+
71+
expect(component).toBeInstanceOf(Text);
72+
expect(component.render(5)).toEqual(["hello"]);
73+
});
74+
75+
it("reuses the prior Text component and invalidates its rendered content", () => {
76+
const component = new Text("before", 0, 0);
77+
expect(component.render(6)).toEqual(["before"]);
78+
79+
const reused = reuseTextComponent(component, "after!");
80+
81+
expect(reused).toBe(component);
82+
expect(reused.render(6)).toEqual(["after!"]);
83+
});
84+
});
85+
6586
describe("shortenPath", () => {
6687
const home = os.homedir();
6788

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
* Normalizes paths/text/image fallbacks before tool results are styled or truncated.
55
*/
66
import * as os from "node:os";
7-
import { getCapabilities, getImageDimensions, imageFallback } from "@earendil-works/pi-tui";
7+
import {
8+
type Component,
9+
getCapabilities,
10+
getImageDimensions,
11+
imageFallback,
12+
Text,
13+
} from "@earendil-works/pi-tui";
814
import { shortenPathWithHome } from "../../../infra/home-display.js";
915
import { keyHint } from "../../modes/interactive/components/keybinding-hints.js";
1016
import type { Theme } from "../../modes/interactive/theme/theme.js";
@@ -45,6 +51,11 @@ export function trimTrailingEmptyLines(lines: readonly string[]): string[] {
4551
return lines.slice(0, lines.findLastIndex((line) => line !== "") + 1);
4652
}
4753

54+
export function reuseTextComponent(lastComponent: Component | undefined, content: string): Text {
55+
const text = (lastComponent as Text | undefined) ?? new Text("", 0, 0); // SAFETY: Render slots only reuse the component returned by this renderer.
56+
text.setText(content);
57+
return text;
58+
}
4859
/** Extracts text output and image placeholders from a tool result. */
4960
export function getTextOutput(
5061
result:

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
invalidArgText,
3131
normalizeDisplayText,
3232
replaceTabs,
33+
reuseTextComponent,
3334
shortenPath,
3435
str,
3536
trimTrailingEmptyLines,
@@ -620,9 +621,7 @@ export function createWriteToolDefinition(
620621
component.clear();
621622
return component;
622623
}
623-
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
624-
text.setText(output);
625-
return text;
624+
return reuseTextComponent(context.lastComponent, output);
626625
},
627626
};
628627
}

0 commit comments

Comments
 (0)