Skip to content

Commit f212c91

Browse files
Alix-007steipete
andauthored
fix(agents): report discarded search stderr (#132627)
Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: Alix-007 <[email protected]>
1 parent a7f9461 commit f212c91

5 files changed

Lines changed: 97 additions & 6 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,44 @@ it("keeps multibyte stderr intact when pipe chunks split a character", async ()
120120
await expect(result).rejects.toThrow("fd 失败:权限被拒绝");
121121
});
122122

123+
it.each([
124+
{
125+
chunks: ["x".repeat(65536), "y".repeat(65536), "终"],
126+
dropped: 65539,
127+
tail: "y".repeat(65533) + "终",
128+
},
129+
{
130+
chunks: ["aaaa😀" + "c".repeat(65527), "dddddd"],
131+
dropped: 8,
132+
tail: "c".repeat(65527) + "dddddd",
133+
},
134+
{ chunks: [" ".repeat(65540)], dropped: 4, tail: "fd exited with code 2" },
135+
])(
136+
"discloses $dropped discarded stderr bytes before the fd diagnostic",
137+
async ({ chunks, dropped, tail }) => {
138+
const child = createChild();
139+
vi.mocked(spawnCommand).mockReturnValue(child as never);
140+
vi.mocked(ensureTool).mockResolvedValue("fd");
141+
const result = createFindToolDefinition("/workspace").execute(
142+
"stderr",
143+
{ pattern: "*" },
144+
undefined,
145+
undefined,
146+
{} as never,
147+
);
148+
await vi.waitFor(() => expect(spawnCommand).toHaveBeenCalledOnce());
149+
for (const chunk of chunks) {
150+
child.stderr.write(chunk);
151+
}
152+
child.stdout.end();
153+
child.stderr.end();
154+
child.emit("close", 2, null);
155+
await expect(result).rejects.toThrow(
156+
`[${dropped} UTF-8 bytes of earlier stderr discarded at the 65536-byte retention cap]\n${tail}`,
157+
);
158+
},
159+
);
160+
123161
it.each(["stdout", "stderr"] as const)(
124162
"rejects and stops fd when %s emits an error",
125163
async (stream) => {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { normalizeNativePathSeparators } from "../../../shared/ignore-rules.js";
1313
import type { AgentTool } from "../../runtime/index.js";
1414
import { ensureTool } from "../../utils/tools-manager.js";
1515
import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";
16-
import { appendBoundedTextTail, normalizePositiveLimit } from "./limits.js";
16+
import { appendBoundedTextTail, formatStderrTail, normalizePositiveLimit } from "./limits.js";
1717
import { resolveLocalPathToCwd, resolveToCwd } from "./path-utils.js";
1818
import {
1919
appendSessionToolTruncationWarning,
@@ -297,6 +297,7 @@ export function createFindToolDefinition(
297297
releaseChildProcessOutputAfterExit(child.nodeChildProcess);
298298
const rl = createInterface({ input: child.stdout });
299299
let stderr = "";
300+
let stderrDroppedBytes = 0;
300301
const lines: string[] = [];
301302

302303
stopChild = () => {
@@ -321,7 +322,9 @@ export function createFindToolDefinition(
321322
// cannot split multibyte characters into U+FFFD replacement noise.
322323
child.stderr?.setEncoding("utf8");
323324
child.stderr?.on("data", (chunk: string) => {
324-
stderr = appendBoundedTextTail(stderr, chunk).tail;
325+
const appended = appendBoundedTextTail(stderr, chunk);
326+
stderr = appended.tail;
327+
stderrDroppedBytes += appended.droppedBytes;
325328
});
326329
// Readline re-emits input failures, while the stream listener also catches
327330
// implementations that do not. settle() keeps the shared failure path one-shot.
@@ -346,7 +349,8 @@ export function createFindToolDefinition(
346349
}
347350
const output = lines.join("\n");
348351
if (code !== 0) {
349-
const errorMsg = stderr.trim() || `fd exited with code ${code}`;
352+
const fallback = `fd exited with code ${code}`;
353+
const errorMsg = formatStderrTail(stderr, stderrDroppedBytes, fallback);
350354
settle(() => reject(new Error(errorMsg)));
351355
return;
352356
}

‎src/agents/sessions/tools/grep.stream-errors.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,43 @@ function textContent(
7171
}
7272

7373
describe("grep tool streaming", () => {
74+
it.each([
75+
{
76+
chunks: ["x".repeat(65536), "y".repeat(65536), "终"],
77+
dropped: 65539,
78+
tail: "y".repeat(65533) + "终",
79+
},
80+
{
81+
chunks: ["aaaa😀" + "c".repeat(65527), "dddddd"],
82+
dropped: 8,
83+
tail: "c".repeat(65527) + "dddddd",
84+
},
85+
{ chunks: [" ".repeat(65540)], dropped: 4, tail: "ripgrep exited with code 2" },
86+
])(
87+
"discloses $dropped discarded stderr bytes before the ripgrep diagnostic",
88+
async ({ chunks, dropped, tail }) => {
89+
const child = createChild();
90+
vi.mocked(spawnCommand).mockReturnValue(child as never);
91+
vi.mocked(ensureTool).mockResolvedValue("rg");
92+
const result = createGrepToolDefinition(process.cwd()).execute(
93+
"stderr",
94+
{ pattern: "needle" },
95+
undefined,
96+
undefined,
97+
{} as never,
98+
);
99+
await vi.waitFor(() => expect(spawnCommand).toHaveBeenCalledOnce());
100+
for (const chunk of chunks) {
101+
child.stderr.write(chunk);
102+
}
103+
child.stdout.end();
104+
child.stderr.end();
105+
child.emit("close", 2);
106+
await expect(result).rejects.toThrow(
107+
`[${dropped} UTF-8 bytes of earlier stderr discarded at the 65536-byte retention cap]\n${tail}`,
108+
);
109+
},
110+
);
74111
it.each([1, 3])("keeps colliding byte-path context separate at match limit %s", async (limit) => {
75112
const cwd = tempDirs.make("openclaw-grep-byte-path-");
76113
const child = createChild();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { normalizeNativePathSeparators } from "../../../shared/ignore-rules.js";
1414
import type { AgentTool } from "../../runtime/index.js";
1515
import { ensureTool } from "../../utils/tools-manager.js";
1616
import type { ToolDefinition, ToolRenderResultOptions } from "../extensions/types.js";
17-
import { appendBoundedTextTail, normalizePositiveLimit } from "./limits.js";
17+
import { appendBoundedTextTail, formatStderrTail, normalizePositiveLimit } from "./limits.js";
1818
import { resolveLocalPathToCwd, resolveToCwd } from "./path-utils.js";
1919
import {
2020
appendSessionToolTruncationWarning,
@@ -268,6 +268,7 @@ export function createGrepToolDefinition(
268268
child = spawnedChild;
269269
rl = createInterface({ input: spawnedChild.stdout });
270270
let stderr = "";
271+
let stderrDroppedBytes = 0;
271272
let matchCount = 0;
272273
let matchLimitReached = false;
273274
let linesTruncated = false;
@@ -277,7 +278,9 @@ export function createGrepToolDefinition(
277278
// cannot split multibyte characters into U+FFFD replacement noise.
278279
spawnedChild.stderr?.setEncoding("utf8");
279280
spawnedChild.stderr?.on("data", (chunk: string) => {
280-
stderr = appendBoundedTextTail(stderr, chunk).tail;
281+
const appended = appendBoundedTextTail(stderr, chunk);
282+
stderr = appended.tail;
283+
stderrDroppedBytes += appended.droppedBytes;
281284
});
282285
const onStreamError = (stream: "stdout" | "stderr", error: Error) => {
283286
if (settled) {
@@ -372,7 +375,8 @@ export function createGrepToolDefinition(
372375
return;
373376
}
374377
if (!killedDueToLimit && code !== 0 && code !== 1) {
375-
const errorMsg = stderr.trim() || `ripgrep exited with code ${code}`;
378+
const fallback = `ripgrep exited with code ${code}`;
379+
const errorMsg = formatStderrTail(stderr, stderrDroppedBytes, fallback);
376380
settle(() => reject(new Error(errorMsg)));
377381
return;
378382
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ export function appendBoundedTextTail(
2727
const tail = truncateUtf8Suffix(combined, effectiveMaxBytes);
2828
return { tail, droppedBytes: Buffer.byteLength(combined) - Buffer.byteLength(tail) };
2929
}
30+
31+
/** Label lost stderr before the retained diagnostic so it cannot look complete. */
32+
export function formatStderrTail(tail: string, droppedBytes: number, fallback: string): string {
33+
const diagnostic = tail.trim() || fallback;
34+
return droppedBytes > 0
35+
? `[${droppedBytes} UTF-8 bytes of earlier stderr discarded at the ${SESSION_TOOL_STDERR_TAIL_BYTES}-byte retention cap]\n${diagnostic}`
36+
: diagnostic;
37+
}

0 commit comments

Comments
 (0)