Skip to content

Commit 403fcf3

Browse files
fix(clownfish): address review for ghcrawl-157042-autonomous-smoke (1)
1 parent 3057e51 commit 403fcf3

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

‎src/agents/pi-embedded-block-chunker.test.ts‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe("EmbeddedBlockChunker", () => {
143143
expect(chunks.join("") + chunker.bufferedText).toBe(text);
144144
});
145145

146-
it("keeps markdown tables intact when a max-char break falls inside one", () => {
146+
it("caps markdown table chunks when a max-char break falls inside one", () => {
147147
const chunker = new EmbeddedBlockChunker({
148148
minChars: 1,
149149
maxChars: 20,
@@ -164,13 +164,34 @@ describe("EmbeddedBlockChunker", () => {
164164

165165
const chunks = drainChunks(chunker);
166166

167-
expect(chunks).toEqual([
168-
"Intro\n\n",
169-
"| Name | Value |\n| --- | --- |\n| Alpha | One |\n| Beta | Two |\n\n",
170-
]);
167+
expect(chunks[0]).toBe("Intro\n\n");
168+
expect(chunks.every((chunk) => chunk.length <= 20)).toBe(true);
171169
expect(chunks.join("") + chunker.bufferedText).toBe(text);
172170
});
173171

172+
it("does not emit an oversized chunk for a table-only payload longer than maxChars", () => {
173+
const maxChars = 25;
174+
const chunker = new EmbeddedBlockChunker({
175+
minChars: 1,
176+
maxChars,
177+
breakPreference: "paragraph",
178+
});
179+
const text = [
180+
"| Name | Description |",
181+
"| --- | --- |",
182+
`| Alpha | ${"a".repeat(70)} |`,
183+
`| Beta | ${"b".repeat(70)} |`,
184+
].join("\n");
185+
186+
chunker.append(text);
187+
188+
const chunks = drainChunks(chunker, true);
189+
190+
expect(chunks.length).toBeGreaterThan(1);
191+
expect(chunks.every((chunk) => chunk.length <= maxChars)).toBe(true);
192+
expect(chunks.join("")).toBe(text);
193+
});
194+
174195
it("parses fence spans once per drain call for long fenced buffers", () => {
175196
const parseSpy = vi.spyOn(fences, "parseFenceSpans");
176197
const chunker = new EmbeddedBlockChunker({

‎src/agents/pi-embedded-block-chunker.ts‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type FenceSplit = {
2020
type BreakResult = {
2121
index: number;
2222
fenceSplit?: FenceSplit;
23+
preserveNextWhitespace?: boolean;
2324
};
2425

2526
type ParagraphBreak = {
@@ -124,6 +125,28 @@ function extendBreakThroughParagraphSeparator(buffer: string, index: number): nu
124125
return paragraphSeparator ? index + paragraphSeparator[0].length : index;
125126
}
126127

128+
function findTableRowBreakBeforeLimit(params: {
129+
buffer: string;
130+
table: TableSpan;
131+
offset: number;
132+
minChars: number;
133+
maxChars: number;
134+
}): number {
135+
const { buffer, table, offset, minChars, maxChars } = params;
136+
const tableStart = Math.max(0, table.start - offset);
137+
const tableEnd = Math.min(buffer.length, table.end - offset);
138+
const limit = Math.min(maxChars, tableEnd);
139+
let newlineIdx = buffer.lastIndexOf("\n", limit - 1);
140+
while (newlineIdx >= tableStart) {
141+
const candidate = newlineIdx + 1;
142+
if (candidate > tableStart && candidate >= minChars && candidate <= maxChars) {
143+
return candidate;
144+
}
145+
newlineIdx = buffer.lastIndexOf("\n", newlineIdx - 1);
146+
}
147+
return -1;
148+
}
149+
127150
export class EmbeddedBlockChunker {
128151
#buffer = "";
129152
readonly #chunking: BlockReplyChunking;
@@ -285,10 +308,17 @@ export class EmbeddedBlockChunker {
285308
}
286309

287310
const nextStart =
288-
absoluteBreakIdx < source.length && /\s/.test(source[absoluteBreakIdx])
311+
!breakResult.preserveNextWhitespace &&
312+
absoluteBreakIdx < source.length &&
313+
/\s/.test(source[absoluteBreakIdx])
289314
? absoluteBreakIdx + 1
290315
: absoluteBreakIdx;
291-
return { start: skipLeadingNewlines(source, nextStart), reopenFence: undefined };
316+
return {
317+
start: breakResult.preserveNextWhitespace
318+
? nextStart
319+
: skipLeadingNewlines(source, nextStart),
320+
reopenFence: undefined,
321+
};
292322
}
293323

294324
#pickSoftBreakIndex(
@@ -448,8 +478,22 @@ export class EmbeddedBlockChunker {
448478
}
449479
const tableEnd = table.end - offset;
450480
if (tableEnd > 0 && tableEnd <= buffer.length) {
451-
return { index: extendBreakThroughParagraphSeparator(buffer, tableEnd) };
481+
const extendedTableEnd = extendBreakThroughParagraphSeparator(buffer, tableEnd);
482+
if (extendedTableEnd <= maxChars) {
483+
return { index: extendedTableEnd };
484+
}
485+
}
486+
const rowBreak = findTableRowBreakBeforeLimit({
487+
buffer,
488+
table,
489+
offset,
490+
minChars,
491+
maxChars,
492+
});
493+
if (rowBreak !== -1) {
494+
return { index: rowBreak, preserveNextWhitespace: true };
452495
}
496+
return { index: maxChars, preserveNextWhitespace: true };
453497
}
454498
return { index: maxChars };
455499
}

0 commit comments

Comments
 (0)