Skip to content

Commit d84a8b1

Browse files
ly-wang19claude
andauthored
fix(discord): reserve closing-fence space on fence-closing lines (#95661)
`chunkDiscordText` reserved closing-fence space from the post-line fence state (`nextOpenFence`), but a flush during a line's segment loop appends the closing fence based on the still-open `openFence`, which is only advanced after the line. On a line that closes a fence yet carries trailing text, `reserveChars` was 0 while `flush()` still appended a `` ``` ``, producing a chunk of `maxChars + 4` (e.g. 2004 > 2000) that Discord rejects with HTTP 400. Reserve against `nextOpenFence ?? openFence` so whichever fence a flush can close is accounted for, keeping a fence-closing line's chunk within `maxChars`. Co-authored-by: ly-wang19 <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 1658fb6 commit d84a8b1

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎extensions/discord/src/chunk.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ describe("chunkDiscordText", () => {
7878
}
7979
});
8080

81+
it("keeps chunks within maxChars when a closing fence line carries trailing text", () => {
82+
// A line that both closes the fence and carries a long tail must still reserve closing-fence
83+
// space; otherwise a mid-line flush appended "```" and overflowed maxChars (e.g. 2004 > 2000).
84+
for (let pad = 1990; pad <= 2000; pad++) {
85+
const text = "hi\n```lang\n```" + "z".repeat(pad);
86+
for (const chunk of chunkDiscordText(text, { maxChars: 2000, maxLines: 100 })) {
87+
expect(chunk.length).toBeLessThanOrEqual(2000);
88+
}
89+
}
90+
});
91+
8192
it("preserves whitespace when splitting long lines", () => {
8293
const text = Array.from({ length: 40 }, () => "word").join(" ");
8394
const chunks = chunkDiscordText(text, { maxChars: 20, maxLines: 50 });

‎extensions/discord/src/chunk.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,12 @@ export function chunkDiscordText(text: string, opts: ChunkDiscordTextOpts = {}):
207207
}
208208
}
209209

210-
const reserveChars = nextOpenFence ? closeFenceLine(nextOpenFence).length + 1 : 0;
211-
const reserveLines = nextOpenFence ? 1 : 0;
210+
// A flush can fire mid-line, before `openFence` advances to `nextOpenFence` below, so it closes
211+
// against the still-open `openFence`. A fence-closing line that also carries trailing text would
212+
// otherwise reserve 0 yet still get a closing fence appended on flush, overflowing maxChars.
213+
const fenceToReserve = nextOpenFence ?? openFence;
214+
const reserveChars = fenceToReserve ? closeFenceLine(fenceToReserve).length + 1 : 0;
215+
const reserveLines = fenceToReserve ? 1 : 0;
212216
const effectiveMaxChars = maxChars - reserveChars;
213217
const effectiveMaxLines = maxLines - reserveLines;
214218
const charLimit = effectiveMaxChars > 0 ? effectiveMaxChars : maxChars;

0 commit comments

Comments
 (0)