Skip to content

Commit 7c47e4a

Browse files
authored
fix(channels): collapse cumulative commentary snapshots onto one progress-draft line
Providers stream commentary as cumulative snapshots, and the draft line id was derived from the commentary text, so each snapshot minted a new id and stacked another line instead of replacing the open one. Id-less commentary now remembers its open line and reuses that id when the next snapshot continues it; a snapshot sanitized to nothing no longer clears the line, since only an explicit item id retracts. Also fixes activation: the commentary gate guessed "partial" when streaming.mode was unset, which made progress.commentary a silent no-op on channels whose own default is progress (Telegram, Discord). It now takes the caller's resolved mode, matching resolveChannelStreamingPreviewToolProgress. Proven live on Telegram: three overlapping commentary lines collapse to one.
1 parent 4bd56b8 commit 7c47e4a

5 files changed

Lines changed: 212 additions & 7 deletions

File tree

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ d645d24bcb7a5f68cc46c692ad0d1fbd19be0a99996f31e9479ce9cffce301c1 module/channel
3434
4a7ada095f0f483525dcbd848fbccab26473749eba87e6a6c6e5074fd04ee1d1 module/channel-ingress-runtime
3535
c97dd36cdf8f83c2893c33e9430a93cd131a03d855725783ca5b545de0cf84f8 module/channel-lifecycle
3636
159d034b431d113f3a6dc41ec0bcadba2d6664051f158330b0e3dd3da8b5d42f module/channel-logging
37-
34868a4532173efb2ae17426bdedb618c56914d0919ba439d5b7c5a7d085fc9d module/channel-message
38-
d80682f483c876dbfdcec023f9a78381de340bef0aae36e0360389d8552e0db5 module/channel-outbound
37+
83297cb5672b5923ad7b288a27c29e265e71584e9a4828a6fa3641d847ce8179 module/channel-message
38+
cb95167f43ad2ebc272ee675df416c2870fd3ff1876a48c5dbeadb5a186422d1 module/channel-outbound
3939
930beff13ed42a138f65164013c82f4f4c96422292c5fc634a5edee1dce71367 module/channel-pairing
4040
ee4292b069d4d48cce4fc2dc26df5b5c87eb1fa4769f1f6be9a10c3e1221e1a9 module/channel-plugin-common
4141
94ef57c8f6087fcaa56e59e493c391a04377ed03f23c681edd8d8f6e2d64e0da module/channel-policy
@@ -46,7 +46,7 @@ bba5540be7cf9613a163663decdb2affe2af9bbd3ad7914989ab186f9c2abec1 module/channel
4646
7c90157a95bc0523fc66b1f78ce140f7ec7dbf809dfa3a480244efc01f972754 module/channel-send-result
4747
fb123c1b557ed2527e335f13c3d6de41ab0c3305151001cf2b1075d1da8034c5 module/channel-setup
4848
8de651a14a46014dc86fb8ccaff819e0854cf6bb2e3b59e642f1e55b37ef43c2 module/channel-status
49-
8269386e8f25e090c7837677b447582cfe301eb7debfb6ac8a4fc83b599ff13f module/channel-streaming
49+
d904e33114f056022ef9fec184edcfe37c30b539b268e28b0ff485f0a02b9c59 module/channel-streaming
5050
14f0103adb14627b662fbe9fdd5ed08596ed702a250853e8beb8bb8dd1361588 module/cli-argv
5151
c89ec1b194b76f67a6f4dd108dccf460da6065646cba31374c8aa748f23a39e4 module/collection-runtime
5252
391e6f0c77da2e17a058fc5aea87fd193830b2a14e12fae77c7f63b0226f0bb5 module/command-auth

‎src/channels/progress-draft-compositor.test.ts‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,144 @@ describe("createChannelProgressDraftCompositor", () => {
331331
expect(rendered).toContain("Shelling\n\n💬 _Checking the workspace_");
332332
});
333333

334+
it("collapses cumulative id-less commentary snapshots onto one line", async () => {
335+
const update = vi.fn();
336+
const progress = createChannelProgressDraftCompositor({
337+
entry: { streaming: { mode: "progress", progress: { label: "Shelling", commentary: true } } },
338+
mode: "progress",
339+
active: true,
340+
seed: "test",
341+
commentaryLinePrefix: "💬 ",
342+
update,
343+
});
344+
345+
expect(await progress.pushCommentaryProgress("Checking")).toBe(true);
346+
expect(await progress.pushCommentaryProgress("Checking the workspace")).toBe(true);
347+
expect(await progress.pushCommentaryProgress("Checking the workspace before answering.")).toBe(
348+
true,
349+
);
350+
351+
expect(update).toHaveBeenLastCalledWith(
352+
"Shelling\n\n💬 _Checking the workspace before answering._",
353+
{
354+
lines: [
355+
expect.objectContaining({
356+
text: "💬 _Checking the workspace before answering._",
357+
kind: "item",
358+
label: "Commentary",
359+
}),
360+
],
361+
},
362+
);
363+
expect(progress.getSnapshot().lines).toHaveLength(1);
364+
});
365+
366+
it("appends genuinely distinct id-less commentary as separate lines", async () => {
367+
const update = vi.fn();
368+
const progress = createChannelProgressDraftCompositor({
369+
entry: { streaming: { mode: "progress", progress: { label: "Shelling", commentary: true } } },
370+
mode: "progress",
371+
active: true,
372+
seed: "test",
373+
commentaryLinePrefix: "💬 ",
374+
update,
375+
});
376+
377+
expect(await progress.pushCommentaryProgress("Checking the workspace")).toBe(true);
378+
expect(await progress.pushCommentaryProgress("Writing the patch next")).toBe(true);
379+
380+
expect(update).toHaveBeenLastCalledWith(
381+
"Shelling\n\n💬 _Checking the workspace_\n💬 _Writing the patch next_",
382+
{
383+
lines: [
384+
expect.objectContaining({ text: "💬 _Checking the workspace_" }),
385+
expect.objectContaining({ text: "💬 _Writing the patch next_" }),
386+
],
387+
},
388+
);
389+
});
390+
391+
it("updates an id-less commentary line in place after a later tool line", async () => {
392+
const update = vi.fn();
393+
const progress = createChannelProgressDraftCompositor({
394+
entry: { streaming: { mode: "progress", progress: { label: "Shelling", commentary: true } } },
395+
mode: "progress",
396+
active: true,
397+
seed: "test",
398+
commentaryLinePrefix: "💬 ",
399+
update,
400+
});
401+
402+
await progress.pushCommentaryProgress("Checking");
403+
await progress.pushToolProgress("🛠️ Exec", { startImmediately: true });
404+
await progress.pushCommentaryProgress("Checking the workspace");
405+
406+
expect(update).toHaveBeenLastCalledWith("Shelling\n\n💬 _Checking the workspace_\n🛠️ Exec", {
407+
lines: [expect.objectContaining({ text: "💬 _Checking the workspace_" }), "🛠️ Exec"],
408+
});
409+
});
410+
411+
it("keeps id-less commentary when a later snapshot sanitizes to empty", async () => {
412+
const update = vi.fn();
413+
const progress = createChannelProgressDraftCompositor({
414+
entry: { streaming: { mode: "progress", progress: { label: "Shelling", commentary: true } } },
415+
mode: "progress",
416+
active: true,
417+
seed: "test",
418+
commentaryLinePrefix: "💬 ",
419+
update,
420+
});
421+
422+
expect(await progress.pushCommentaryProgress("Checking the workspace")).toBe(true);
423+
const callsAfterValid = update.mock.calls.length;
424+
expect(
425+
await progress.pushCommentaryProgress("[[reply_to_current]] _NO_REPLY_ [[audio_as_voice]]"),
426+
).toBe(false);
427+
428+
expect(update).toHaveBeenCalledTimes(callsAfterValid);
429+
expect(update).toHaveBeenLastCalledWith(
430+
"Shelling\n\n💬 _Checking the workspace_",
431+
expect.objectContaining({
432+
lines: [expect.objectContaining({ text: "💬 _Checking the workspace_" })],
433+
}),
434+
);
435+
expect(progress.getSnapshot().lines).toHaveLength(1);
436+
});
437+
438+
it("replaces and retracts commentary by itemId", async () => {
439+
const update = vi.fn();
440+
const progress = createChannelProgressDraftCompositor({
441+
entry: { streaming: { mode: "progress", progress: { label: "Shelling", commentary: true } } },
442+
mode: "progress",
443+
active: true,
444+
seed: "test",
445+
commentaryLinePrefix: "💬 ",
446+
update,
447+
});
448+
449+
expect(await progress.pushCommentaryProgress("First note", { itemId: "c1" })).toBe(true);
450+
expect(await progress.pushCommentaryProgress("Updated note", { itemId: "c1" })).toBe(true);
451+
expect(await progress.pushCommentaryProgress("Other note", { itemId: "c2" })).toBe(true);
452+
expect(progress.getSnapshot().lines).toHaveLength(2);
453+
expect(update).toHaveBeenLastCalledWith(
454+
"Shelling\n\n💬 _Updated note_\n💬 _Other note_",
455+
expect.objectContaining({
456+
lines: [
457+
expect.objectContaining({ id: "commentary:c1", text: "💬 _Updated note_" }),
458+
expect.objectContaining({ id: "commentary:c2", text: "💬 _Other note_" }),
459+
],
460+
}),
461+
);
462+
463+
expect(await progress.pushCommentaryProgress("", { itemId: "c1" })).toBe(false);
464+
expect(update).toHaveBeenLastCalledWith(
465+
"Shelling\n\n💬 _Other note_",
466+
expect.objectContaining({
467+
lines: [expect.objectContaining({ id: "commentary:c2", text: "💬 _Other note_" })],
468+
}),
469+
);
470+
});
471+
334472
it("interleaves reasoning bursts with tool calls in arrival order", async () => {
335473
const update = vi.fn();
336474
const progress = createChannelProgressDraftCompositor({

‎src/channels/progress-draft-compositor.ts‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export function createChannelProgressDraftCompositor(params: {
162162
const previewToolProgressEnabled =
163163
params.active && resolveChannelStreamingPreviewToolProgress(params.entry, true, params.mode);
164164
const commentaryProgressEnabled =
165-
params.active && resolveChannelStreamingProgressCommentary(params.entry);
165+
params.active && resolveChannelStreamingProgressCommentary(params.entry, false, params.mode);
166166
const thinkingProgressEnabled =
167167
params.active && (params.reasoningGate ?? previewToolProgressEnabled);
168168
const suppressDefaultToolProgressMessages =
@@ -177,6 +177,11 @@ export function createChannelProgressDraftCompositor(params: {
177177
let lastRenderedLines = lines;
178178
let reasoningRawText = "";
179179
let lastReasoningLine: string | undefined;
180+
// Id-less commentary streams as cumulative snapshots ("Checking" → "Checking
181+
// the workspace"). Remember the open line so successive snapshots replace in
182+
// place instead of appending one line per growing prefix.
183+
let lastIdLessCommentaryId: string | undefined;
184+
let lastIdLessCommentaryBare = "";
180185
// Model preambles and narration share the status slot while tool lines keep
181186
// accumulating underneath for turns where neither source is available.
182187
let preambleText = "";
@@ -249,6 +254,8 @@ export function createChannelProgressDraftCompositor(params: {
249254
lastRenderedLines = lines;
250255
reasoningRawText = "";
251256
lastReasoningLine = undefined;
257+
lastIdLessCommentaryId = undefined;
258+
lastIdLessCommentaryBare = "";
252259
preambleText = "";
253260
preambleItemId = undefined;
254261
preambleAt = undefined;
@@ -305,6 +312,35 @@ export function createChannelProgressDraftCompositor(params: {
305312
clearTimeoutFn,
306313
});
307314

315+
/**
316+
* Commentary line identity. An explicit item id owns its line. Without one,
317+
* providers stream cumulative snapshots ("Checking" → "Checking the
318+
* workspace"), so a snapshot that continues the open line reuses its id and
319+
* updates in place; anything else starts a new line.
320+
*/
321+
const resolveCommentaryLineId = (commentary: {
322+
itemId?: string;
323+
normalized: string;
324+
bareNormalized: string;
325+
}): string => {
326+
if (commentary.itemId) {
327+
return `commentary:${commentary.itemId}`;
328+
}
329+
if (!commentary.normalized) {
330+
// Sanitized to nothing (directive-only / NO_REPLY): no line to address, so
331+
// it cannot retract the open one. Only an explicit itemId clears a line.
332+
return "";
333+
}
334+
const continuesOpenLine =
335+
Boolean(lastIdLessCommentaryBare) &&
336+
(commentary.bareNormalized.startsWith(lastIdLessCommentaryBare) ||
337+
lastIdLessCommentaryBare.startsWith(commentary.bareNormalized));
338+
if (continuesOpenLine && lastIdLessCommentaryId) {
339+
return lastIdLessCommentaryId;
340+
}
341+
return `commentary:${commentary.normalized}`;
342+
};
343+
308344
const clearLine = async (lineId: string) => {
309345
const nextLines = removeChannelProgressDraftLine(lines, lineId);
310346
if (nextLines === lines) {
@@ -657,7 +693,10 @@ export function createChannelProgressDraftCompositor(params: {
657693
return false;
658694
}
659695
const normalized = normalizeCommentaryProgressText(text ?? "");
660-
const lineId = itemId ? `commentary:${itemId}` : normalized ? `commentary:${normalized}` : "";
696+
// Compare bare (de-italicized) text so cumulative snapshots still match
697+
// after normalizeCommentaryProgressText wraps each line in _…_.
698+
const bareNormalized = stripLaneItalics(normalized);
699+
const lineId = resolveCommentaryLineId({ itemId, normalized, bareNormalized });
661700
if (!normalized) {
662701
// Empty commentary with an item id means the producer retracted that
663702
// item; remove its draft line if it was already rendered.
@@ -670,14 +709,18 @@ export function createChannelProgressDraftCompositor(params: {
670709
id: lineId,
671710
// The lane marker (💬, matching 🧠 thinking / 🛠️ tools) is a per-channel
672711
// presentation choice supplied via commentaryLinePrefix; default none.
673-
text: `${commentaryLinePrefix}${commentaryItalics ? normalized : stripLaneItalics(normalized)}`,
712+
text: `${commentaryLinePrefix}${commentaryItalics ? normalized : bareNormalized}`,
674713
kind: "item",
675714
label: "Commentary",
676715
prefix: false,
677716
};
678717
lines = mergeChannelProgressDraftLine(lines, line, {
679718
maxLines: resolveChannelProgressDraftMaxLines(params.entry),
680719
});
720+
if (!itemId) {
721+
lastIdLessCommentaryId = lineId;
722+
lastIdLessCommentaryBare = bareNormalized;
723+
}
681724
const alreadyStarted = gate.hasStarted;
682725
await gate.startNow();
683726
if (!gate.hasStarted) {

‎src/channels/streaming.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
resolveChannelStreamingChunkMode,
1313
resolveChannelStreamingNativeTransport,
1414
resolveChannelStreamingPreviewChunk,
15+
resolveChannelStreamingProgressCommentary,
1516
resolveChannelStreamingProgressNarration,
1617
} from "./streaming.js";
1718

@@ -292,6 +293,20 @@ describe("progress narration", () => {
292293
expect(text).not.toContain("\n");
293294
});
294295

296+
it("honors the caller's mode when resolving commentary", () => {
297+
// The progress-draft channels default to "progress" when streaming.mode is
298+
// unset, so guessing "partial" here made progress.commentary a silent no-op.
299+
const entry = { streaming: { progress: { commentary: true } } };
300+
expect(resolveChannelStreamingProgressCommentary(entry, false, "progress")).toBe(true);
301+
expect(resolveChannelStreamingProgressCommentary(entry, false, "partial")).toBe(false);
302+
expect(
303+
resolveChannelStreamingProgressCommentary(
304+
{ streaming: { mode: "progress", progress: { commentary: true } } },
305+
false,
306+
),
307+
).toBe(true);
308+
});
309+
295310
it("resolves the narration toggle with default on", () => {
296311
// Mode gating is the caller's job; unset config keeps narration available.
297312
expect(resolveChannelStreamingProgressNarration(undefined)).toBe(true);

‎src/channels/streaming.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,18 @@ export function resolveChannelStreamingPreviewToolProgress(
826826
export function resolveChannelStreamingProgressCommentary(
827827
entry: StreamingCompatEntry | null | undefined,
828828
defaultValue = false,
829+
/**
830+
* The channel's resolved stream mode, for the same reason
831+
* resolveChannelStreamingPreviewToolProgress takes one: only the caller knows
832+
* which default applies when `streaming.mode` is unset. Guessing "partial"
833+
* here made `progress.commentary: true` a silent no-op on the progress-draft
834+
* channels, whose own default is "progress".
835+
*/
836+
mode?: StreamingMode,
829837
): boolean {
830838
const config = getChannelStreamingConfigObject(entry);
831-
if (resolveChannelPreviewStreamMode(entry, "partial") !== "progress") {
839+
const effectiveMode = mode ?? resolveChannelPreviewStreamMode(entry, "partial");
840+
if (effectiveMode !== "progress") {
832841
return false;
833842
}
834843
const progress = asObjectRecord(config?.progress);

0 commit comments

Comments
 (0)