diff --git a/src/markdown/render-aware-chunking.test.ts b/src/markdown/render-aware-chunking.test.ts index 92b66ca89bc..5daf9419ef5 100644 --- a/src/markdown/render-aware-chunking.test.ts +++ b/src/markdown/render-aware-chunking.test.ts @@ -70,4 +70,17 @@ describe("renderMarkdownIRChunksWithinLimit", () => { expect(chunks.map((chunk) => chunk.source.text)).toEqual(["README.md", "<"]); }); + + it("normalizes non-finite limits before chunking", () => { + const ir = markdownToIR("abc"); + const chunks = renderMarkdownIRChunksWithinLimit({ + ir, + limit: Number.NaN, + renderChunk: renderEscapedHtml, + measureRendered: (rendered) => rendered.length, + }); + + expect(chunks.map((chunk) => chunk.source.text)).toEqual(["a", "b", "c"]); + expect(chunks.every((chunk) => chunk.rendered.length <= 1)).toBe(true); + }); }); diff --git a/src/markdown/render-aware-chunking.ts b/src/markdown/render-aware-chunking.ts index 3f0441dc296..861dc286d5b 100644 --- a/src/markdown/render-aware-chunking.ts +++ b/src/markdown/render-aware-chunking.ts @@ -1,3 +1,4 @@ +import { resolveIntegerOption } from "../shared/number-coercion.js"; import { chunkMarkdownIR, sliceMarkdownIR, @@ -30,7 +31,7 @@ export function renderMarkdownIRChunksWithinLimit( return []; } - const normalizedLimit = Math.max(1, Math.floor(options.limit)); + const normalizedLimit = resolveIntegerOption(options.limit, 1, { min: 1 }); const pending = chunkMarkdownIR(options.ir, normalizedLimit); const finalized: MarkdownIR[] = []; @@ -196,7 +197,7 @@ function splitMarkdownIRPreserveWhitespace(ir: MarkdownIR, limit: number): Markd return []; } - const normalizedLimit = Math.max(1, Math.floor(limit)); + const normalizedLimit = resolveIntegerOption(limit, 1, { min: 1 }); if (normalizedLimit <= 0 || ir.text.length <= normalizedLimit) { return [ir]; }