Skip to content

Commit 3a4e2ef

Browse files
maweibinclaudesteipete
authored
fix(auto-reply): treat U+2028/U+2029 as paragraph boundaries when chunking (#103518)
* fix(auto-reply): treat U+2028/U+2029 as paragraph boundaries when chunking chunkByParagraph normalized only CR/CRLF before blank-line paragraph detection, so model output using Unicode LINE/PARAGRAPH SEPARATOR (U+2028/U+2029) instead of a blank line was not split at those boundaries and fell back to length-based splitting. Normalize U+2028/U+2029 to \n alongside CR/CRLF, matching how the Control UI markdown renderer handles them. Co-Authored-By: Claude Opus 4.8 <[email protected]> * fix(auto-reply): normalize U+2028 as line break, U+2029 as blank-line paragraph boundary Distinguish U+2028 (LINE SEPARATOR) from U+2029 (PARAGRAPH SEPARATOR): U+2029 becomes \n\n (blank line — paragraph boundary) while U+2028 becomes \n (single newline — intra-paragraph line break). The original fix mapped both to \n, so standalone U+2029 still produced single-line text without a blank-line gap — paragraph detection failed. The combined U+2028
 input accidentally produced the right blank-line sequence, which masked the bug. Adds individual tests for lone U+2029 (splits at paragraph boundary), lone U+2028 (stays within paragraph), and consecutive U+2028
 (combined blank line — matches \n\n behavior). * test(auto-reply): simplify Unicode separator cases --------- Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 96add8e commit 3a4e2ef

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/auto-reply/chunk.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as fences from "../../packages/markdown-core/src/fences.js";
66
import { hasBalancedFences } from "../test-utils/chunk-test-helpers.js";
77
import {
88
chunkByNewline,
9+
chunkByParagraph,
910
chunkMarkdownText,
1011
chunkMarkdownTextWithMode,
1112
chunkText,
@@ -230,6 +231,37 @@ describe("chunkText", () => {
230231
]);
231232
});
232233

234+
describe("chunkByParagraph Unicode line/paragraph separators", () => {
235+
it.each([
236+
{
237+
name: "treats lone U+2029 as a standalone paragraph boundary",
238+
text: "paragraph one\u2029paragraph two starts here",
239+
normalized: "paragraph one\n\nparagraph two starts here",
240+
limit: 39,
241+
expected: ["paragraph one", "paragraph two starts here"],
242+
},
243+
{
244+
name: "treats lone U+2028 as a line break within one paragraph",
245+
text: "paragraph one line\u2028still same paragraph",
246+
normalized: "paragraph one line\nstill same paragraph",
247+
limit: 50,
248+
expected: ["paragraph one line\nstill same paragraph"],
249+
},
250+
{
251+
name: "treats consecutive U+2028 and U+2029 as a paragraph boundary",
252+
text: "paragraph one line\u2028\u2029paragraph two starts here",
253+
normalized: "paragraph one line\n\nparagraph two starts here",
254+
limit: 40,
255+
expected: ["paragraph one line", "paragraph two starts here"],
256+
},
257+
] as const)("$name", ({ text, normalized, limit, expected }) => {
258+
const chunks = chunkByParagraph(text, limit);
259+
260+
expect(chunks).toEqual(expected);
261+
expect(chunks).toEqual(chunkByParagraph(normalized, limit));
262+
});
263+
});
264+
233265
describe("resolveTextChunkLimit", () => {
234266
it.each([
235267
...(["whatsapp", "telegram", "slack", "signal", "imessage", "discord"] as const).map(

‎src/auto-reply/chunk.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ export function chunkByParagraph(
207207
}
208208
const splitLongParagraphs = opts?.splitLongParagraphs !== false;
209209

210-
// Normalize to \n so blank line detection is consistent.
211-
const normalized = text.replace(/\r\n?/g, "\n");
210+
// U+2029 PARAGRAPH SEPARATOR maps to a blank-line boundary; U+2028 LINE
211+
// SEPARATOR and CR/CRLF map to a single newline.
212+
// Normalize in two steps so consecutive U+2028\u2029 also produces a blank line.
213+
const normalized = text.replace(/\u2029/g, "\n\n").replace(/\r\n?|\u2028/g, "\n");
212214

213215
// Fast-path: if there are no blank-line paragraph separators, do not split.
214216
// (We *do not* early-return based on `limit` — newline mode is about paragraph

0 commit comments

Comments
 (0)