fix(agent-core): avoid a crash when empty output has a negative line limit (#103425)

* fix(agent-core): avoid empty truncation crash

* refactor(agent-core): simplify empty truncation handling

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
Co-authored-by: Peter Steinberger <peter@steipete.me>
This commit is contained in:
qingminlong
2026-07-11 08:41:00 +08:00
committed by GitHub
parent 0e05973657
commit 6e0d590bd8
2 changed files with 22 additions and 2 deletions
@@ -201,8 +201,8 @@ export function truncateHead(content: string, options: TruncationOptions = {}):
});
}
const firstLineBytes = utf8ByteLength(input.lines[0]);
if (firstLineBytes > input.maxBytes) {
const firstLine = input.lines[0];
if (firstLine !== undefined && utf8ByteLength(firstLine) > input.maxBytes) {
return buildTruncationResult(input, {
content: "",
truncated: true,
@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { truncateHead } from "./truncate.js";
describe("session tool truncation facade", () => {
it.each([
{ limit: "maxLines", options: { maxLines: -1 }, truncatedBy: "lines" as const },
{ limit: "maxBytes", options: { maxBytes: -1 }, truncatedBy: "bytes" as const },
])("handles empty content with a negative $limit", ({ options, truncatedBy }) => {
expect(truncateHead("", options)).toMatchObject({
content: "",
truncated: true,
truncatedBy,
totalLines: 0,
totalBytes: 0,
outputLines: 0,
outputBytes: 0,
firstLineExceedsLimit: false,
});
});
});