Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] 1bfa3c3ecd fix(app): keep turns separate when plugins replace prompts
A replacement prompt kept its source but lost its role as a turn trigger and summary host. Preserve that role so both replies stay visible when the chat folds completed work.
2026-11-29 12:32:21 +00:00
3 changed files with 118 additions and 18 deletions
@@ -34,6 +34,7 @@ const runtime = {
function installProbe(
itemType: "user_message" | "assistant_message" | "tool_call" | "reasoning",
condition = "true",
cards = 1,
): InstalledPlugin {
const clientBundle = `(function() {
return { default: function(client) {
@@ -43,7 +44,7 @@ function installProbe(
transform: function({ item, phase }) {
if (!(${condition})) return undefined;
return {
items: [{
items: Array.from({ length: ${cards} }, () => ({
type: "plugin",
kind: "probe",
version: 1,
@@ -51,7 +52,7 @@ function installProbe(
? { callId: item.callId, name: item.name, status: item.status,
detail: item.detail, ...(item.metadata ? { metadata: item.metadata } : {}), error: item.error, phase: phase }
: { text: item.text, phase: phase }
}]
}))
};
}
});
@@ -593,6 +594,88 @@ describe("timeline presentation", () => {
});
});
it.each([
{ tagged: false, trigger: "user_message", cards: 1 },
{ tagged: true, trigger: "user_message", cards: 1 },
{ tagged: false, trigger: "tool_call", cards: 1 },
{ tagged: true, trigger: "tool_call", cards: 1 },
{ tagged: false, trigger: "user_message", cards: 2 },
{ tagged: true, trigger: "user_message", cards: 2 },
] as const)(
"keeps replies visible and folds work with a replaced $trigger prompt (turn IDs: $tagged, cards: $cards)",
({ tagged, trigger, cards }) => {
const firstTurn = [
userMessage("first-prompt", 1),
toolCall("first-work", "Bash"),
assistantMessage("first-reply", 3),
];
const message = {
...toolCall("second-prompt", "agent_message"),
timestamp: createTimestamp(4),
};
if (message.payload.source !== "agent") throw new Error("Expected agent tool");
message.payload.data.agentMessage = {
event: "message",
sender: { id: "sender" },
text: "second-prompt",
};
const secondPrompt = trigger === "tool_call" ? message : userMessage("second-prompt", 4);
const secondTurn = [
secondPrompt,
toolCall("second-work", "Bash"),
assistantMessage("second-reply", 6),
];
const tail = tagged
? [
...firstTurn.map((item) => ({ ...item, turnId: "first" })),
...secondTurn.map((item) => ({ ...item, turnId: "second" })),
]
: [...firstTurn, ...secondTurn];
const present = createStreamPresentation();
const input = {
...presentationOptions,
tail,
head: [],
display: "collapseTurns" as const,
isTurnActive: false,
};
const original = present({ ...input, transform: undefined });
expect(original.tail.map((item) => item.id)).toEqual([
"first-prompt",
"turn-summary:first-prompt",
"first-reply",
secondPrompt.id,
`turn-summary:${secondPrompt.id}`,
"second-reply",
]);
const transformed = present({
...input,
transform: installedTransform(
installProbe(
trigger,
trigger === "tool_call"
? 'item.name === "agent_message"'
: 'item.text === "second-prompt"',
cards,
),
),
});
const promptCards = Array.from(
{ length: cards },
(_, index) => `probe-plugin/${secondPrompt.id}/${index}`,
);
expect(transformed.tail.map((item) => item.id)).toEqual([
"first-prompt",
"turn-summary:first-prompt",
"first-reply",
...promptCards,
`turn-summary:${promptCards.at(-1)}`,
"second-reply",
]);
},
);
it("folds work before a plugin-rendered reply and leaves the reply visible", () => {
const timestamp = new Date("2026-01-01T00:00:00.000Z");
const tail: StreamItem[] = [
@@ -1,4 +1,4 @@
import { isTurnBoundary, isTurnTrigger } from "../turn-membership";
import { continuesProjectedTrigger, isTurnBoundary, isTurnTrigger } from "../turn-membership";
import {
getStreamItemMessageId,
getAssistantReply,
@@ -104,6 +104,10 @@ function staysVisibleWhenCollapsed(item: StreamItem): boolean {
return isTurnTrigger(item) || item.kind === "compaction";
}
function canHostSummary(item: StreamItem): boolean {
return item.kind === "user_message" || item.kind === "tool_call" || isTurnTrigger(item);
}
function findLastReplyIndex(tail: StreamItem[], start: number, end: number): number {
for (let index = end - 1; index >= start; index -= 1) {
const item = tail[index]!;
@@ -139,12 +143,7 @@ export function collapseTurns(input: CollapseTurnsInput): TurnCollapse {
const host = tail[runStart - 1];
const isClosed = runEnd < tail.length || !input.isLatestTurnOpen;
const replyIndex = findLastReplyIndex(tail, runStart, runEnd);
if (
!host ||
(host.kind !== "user_message" && host.kind !== "tool_call") ||
!isClosed ||
replyIndex <= runStart
) {
if (!host || !canHostSummary(host) || !isClosed || replyIndex <= runStart) {
output.push(...tail.slice(runStart, runEnd));
return;
}
@@ -183,13 +182,16 @@ export function collapseTurns(input: CollapseTurnsInput): TurnCollapse {
};
for (let index = 0; index < tail.length; index += 1) {
if (
!(index === 0 && isTurnTrigger(tail[index]!)) &&
!isTurnBoundary(tail[index - 1] ?? null, tail[index] ?? null)
)
const item = tail[index]!;
const previous = tail[index - 1] ?? null;
if (index === runStart && previous && continuesProjectedTrigger(previous, item)) {
output.push(item);
runStart = index + 1;
continue;
}
if (!(index === 0 && isTurnTrigger(item)) && !isTurnBoundary(previous, item)) continue;
flushRun(index);
output.push(tail[index]!);
output.push(item);
runStart = index + 1;
}
flushRun(tail.length);
@@ -7,7 +7,11 @@ import type { StreamItem } from "@/types/stream";
*/
export function continuesTurn(previous: StreamItem | null, next: StreamItem | null): boolean {
if (!previous || !next) return false;
return continuesTimelineTurn(previous.turnId, next.turnId, isTurnTrigger(next));
return continuesTimelineTurn(
previous.turnId,
next.turnId,
isTurnTrigger(next) && !continuesProjectedTrigger(previous, next),
);
}
/**
@@ -37,9 +41,20 @@ export function belongsToTurn(item: StreamItem, turnId: string | null): boolean
/** Inputs that start a turn when older history has no canonical turn IDs. */
export function isTurnTrigger(item: StreamItem): boolean {
const source = item.kind === "plugin" ? (item.source ?? item) : item;
return isTimelineTurnTrigger(
item.kind === "tool_call" && item.payload.source === "agent"
? { type: item.kind, ...item.payload.data }
: { type: item.kind },
source.kind === "tool_call" && source.payload.source === "agent"
? { type: source.kind, name: source.payload.data.name }
: { type: source.kind },
);
}
/** Consecutive replacement rows for one prompt represent a single trigger. */
export function continuesProjectedTrigger(previous: StreamItem, next: StreamItem): boolean {
return (
previous.kind === "plugin" &&
next.kind === "plugin" &&
previous.source === next.source &&
isTurnTrigger(next)
);
}