From 3adec3563278cdd215a989da0a300cd5bd8a3fd8 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 9 Jan 2026 21:52:47 +0000 Subject: [PATCH] fix: make forced block chunking fence-safe --- src/agents/pi-embedded-block-chunker.ts | 9 ++++++--- src/auto-reply/reply.block-streaming.test.ts | 4 ++++ src/markdown/fences.ts | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/agents/pi-embedded-block-chunker.ts b/src/agents/pi-embedded-block-chunker.ts index 53e933273..4236eb53d 100644 --- a/src/agents/pi-embedded-block-chunker.ts +++ b/src/agents/pi-embedded-block-chunker.ts @@ -68,7 +68,7 @@ export class EmbeddedBlockChunker { const breakResult = force && this.#buffer.length <= maxChars ? this.#pickSoftBreakIndex(this.#buffer, 1) - : this.#pickBreakIndex(this.#buffer); + : this.#pickBreakIndex(this.#buffer, force ? 1 : undefined); if (breakResult.index <= 0) { if (force) { emit(this.#buffer); @@ -169,8 +169,11 @@ export class EmbeddedBlockChunker { return { index: -1 }; } - #pickBreakIndex(buffer: string): BreakResult { - const minChars = Math.max(1, Math.floor(this.#chunking.minChars)); + #pickBreakIndex(buffer: string, minCharsOverride?: number): BreakResult { + const minChars = Math.max( + 1, + Math.floor(minCharsOverride ?? this.#chunking.minChars), + ); const maxChars = Math.max(minChars, Math.floor(this.#chunking.maxChars)); if (buffer.length < minChars) return { index: -1 }; const window = buffer.slice(0, Math.min(maxChars, buffer.length)); diff --git a/src/auto-reply/reply.block-streaming.test.ts b/src/auto-reply/reply.block-streaming.test.ts index 1aa142470..a2d592f2c 100644 --- a/src/auto-reply/reply.block-streaming.test.ts +++ b/src/auto-reply/reply.block-streaming.test.ts @@ -74,6 +74,7 @@ describe("block streaming", () => { { onReplyStart, onBlockReply, + disableBlockStreaming: false, }, { agents: { @@ -131,6 +132,7 @@ describe("block streaming", () => { { onReplyStart, onBlockReply, + disableBlockStreaming: false, }, { agents: { @@ -178,6 +180,7 @@ describe("block streaming", () => { }, { onBlockReply, + disableBlockStreaming: false, }, { agents: { @@ -234,6 +237,7 @@ describe("block streaming", () => { { onBlockReply, blockReplyTimeoutMs: 10, + disableBlockStreaming: false, }, { agents: { diff --git a/src/markdown/fences.ts b/src/markdown/fences.ts index efd31da99..975e5405b 100644 --- a/src/markdown/fences.ts +++ b/src/markdown/fences.ts @@ -44,7 +44,7 @@ export function parseFenceSpans(buffer: string): FenceSpan[] { open.markerChar === markerChar && markerLen >= open.markerLen ) { - const end = nextNewline === -1 ? buffer.length : nextNewline + 1; + const end = lineEnd; spans.push({ start: open.start, end,