Skip to content

Commit 9ef7fff

Browse files
hugenshenNIOvincentkoc
authored
fix(agents): bound tool_search_code stderr accumulation (#101007)
* fix(agents): bound tool_search_code stderr accumulation * fix(agents): report latest bounded stderr --------- Co-authored-by: NIO <[email protected]> Co-authored-by: Vincent Koc <[email protected]>
1 parent 60ce506 commit 9ef7fff

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎src/agents/tool-search.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isToolWrappedWithBeforeToolCallHook,
88
wrapToolWithBeforeToolCallHook,
99
} from "./agent-tools.before-tool-call.js";
10+
import { SESSION_TOOL_STDERR_TAIL_BYTES } from "./sessions/tools/limits.js";
1011
import {
1112
testing,
1213
addClientToolsToToolSearchCatalog,
@@ -1726,4 +1727,18 @@ describe("Tool Search", () => {
17261727
});
17271728
expect(second.catalogReused).toBe(false);
17281729
});
1730+
1731+
it("bounds tool_search_code stderr accumulation to the session tool tail limit", () => {
1732+
let stderrTail = "";
1733+
stderrTail = testing.appendToolSearchCodeStderrTail(
1734+
stderrTail,
1735+
`HEAD_OVERFLOW_${"x".repeat(SESSION_TOOL_STDERR_TAIL_BYTES + 10_000)}TAIL`,
1736+
);
1737+
1738+
expect(stderrTail).not.toContain("HEAD_OVERFLOW_");
1739+
expect(stderrTail.endsWith("TAIL")).toBe(true);
1740+
expect(Buffer.byteLength(stderrTail, "utf8")).toBeLessThanOrEqual(
1741+
SESSION_TOOL_STDERR_TAIL_BYTES,
1742+
);
1743+
});
17291744
});

‎src/agents/tool-search.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from "./agent-tools.before-tool-call.js";
2222
import type { AgentMessage, AgentToolResult, AgentToolUpdateCallback } from "./runtime/index.js";
2323
import type { ToolDefinition } from "./sessions/index.js";
24+
import { appendBoundedTextTail, SESSION_TOOL_STDERR_TAIL_BYTES } from "./sessions/tools/limits.js";
2425
import { asToolParamsRecord, jsonResult, ToolInputError } from "./tools/common.js";
2526
import type { AnyAgentTool } from "./tools/common.js";
2627

@@ -2093,6 +2094,10 @@ async function runCodeModeBridgeRequest(
20932094
throw new ToolInputError("Unsupported tool_search_code bridge method.");
20942095
}
20952096

2097+
function appendToolSearchCodeStderrTail(current: string, chunk: string): string {
2098+
return appendBoundedTextTail(current, chunk, SESSION_TOOL_STDERR_TAIL_BYTES);
2099+
}
2100+
20962101
function runCodeModeChild(params: {
20972102
code: string;
20982103
config: ToolSearchConfig;
@@ -2108,7 +2113,7 @@ function runCodeModeChild(params: {
21082113
env: {},
21092114
stdio: ["ignore", "pipe", "pipe", "ipc"],
21102115
});
2111-
const stderr: string[] = [];
2116+
let stderrTail = "";
21122117
let settled = false;
21132118
let timedOut = false;
21142119
let exitRejectionTimer: ReturnType<typeof setTimeout> | undefined;
@@ -2147,7 +2152,7 @@ function runCodeModeChild(params: {
21472152

21482153
child.stderr?.setEncoding("utf8");
21492154
child.stderr?.on("data", (chunk: string) => {
2150-
stderr.push(chunk);
2155+
stderrTail = appendToolSearchCodeStderrTail(stderrTail, chunk);
21512156
});
21522157

21532158
child.on("error", (error) => {
@@ -2158,8 +2163,8 @@ function runCodeModeChild(params: {
21582163
return;
21592164
}
21602165
const rejectOnExit = () => {
2161-
const suffix = stderr.join("").trim();
2162-
const detail = suffix ? `: ${suffix.slice(0, 500)}` : "";
2166+
const suffix = stderrTail.trim();
2167+
const detail = suffix ? `: ${suffix.slice(-500)}` : "";
21632168
settle(() =>
21642169
reject(
21652170
new Error(
@@ -2351,5 +2356,6 @@ export const testing = {
23512356
},
23522357
applyToolSearchCatalog,
23532358
addClientToolsToToolSearchCatalog,
2359+
appendToolSearchCodeStderrTail,
23542360
};
23552361
export { testing as __testing };

0 commit comments

Comments
 (0)