diff --git a/ui/src/components/TaskChatThread.test.tsx b/ui/src/components/TaskChatThread.test.tsx index 1c6b3da2d1d..101d04d0152 100644 --- a/ui/src/components/TaskChatThread.test.tsx +++ b/ui/src/components/TaskChatThread.test.tsx @@ -6418,3 +6418,111 @@ it("explains a missing provider runtime with an installation repair", () => { expect(container.textContent).toContain("Reinstall or rebuild Paperclip"); expect(container.textContent).toContain("test the agent's runtime"); }); + +it("keeps each consecutive native runner turn's progress commentary on its own reply instead of stacking onto the latest message", () => { + nativeTranscriptState.transcriptByRun.set("codie-run-1", [ + assistantEntry("2026-12-04T17:18:17.000Z", "I'll check the setup task's final notes.", "progress"), + assistantEntry("2026-12-04T17:18:39.000Z", "PAP-7268 is complete (first update).", "final"), + ]); + nativeTranscriptState.transcriptByRun.set("codie-run-2", [ + assistantEntry("2026-12-04T17:52:43.000Z", "I'll check the latest saved result for PAP-7268.", "progress"), + assistantEntry("2026-12-04T17:53:12.000Z", "PAP-7268 is Done (second update).", "final"), + ]); + + const codie = { id: "agent-codie", name: "Codie", urlKey: "codie", icon: null } as unknown as Agent; + renderThread({ + issueId: "chat-codie", + conversationMode: true, + agentMap: new Map([[codie.id, codie]]), + comments: [ + { + id: "reply-1", + companyId: "company-1", + issueId: "chat-codie", + authorType: "agent", + authorAgentId: codie.id, + authorUserId: null, + body: "PAP-7268 is complete (first update).", + presentation: null, + metadata: null, + runId: "codie-run-1", + createdByRunId: "codie-run-1", + createdAt: new Date("2026-12-04T17:18:41.000Z"), + updatedAt: new Date("2026-12-04T17:18:41.000Z"), + }, + { + id: "cross-task-msg", + companyId: "company-1", + issueId: "chat-codie", + authorType: "agent", + authorAgentId: codie.id, + authorUserId: null, + body: "- Native recovery PR #23696 merged.", + presentation: null, + metadata: { + version: 1, + sections: [], + chatMessage: { + sourceTaskId: "task-7354", + sourceTaskIdentifier: "PAP-7354", + sourceRunId: "other-task-run", + contentHash: "hash-1", + }, + }, + runId: "other-task-run", + createdByRunId: "other-task-run", + createdAt: new Date("2026-12-04T17:35:00.000Z"), + updatedAt: new Date("2026-12-04T17:35:00.000Z"), + }, + { + id: "reply-2", + companyId: "company-1", + issueId: "chat-codie", + authorType: "agent", + authorAgentId: codie.id, + authorUserId: null, + body: "PAP-7268 is Done (second update).", + presentation: null, + metadata: null, + runId: "codie-run-2", + createdByRunId: "codie-run-2", + createdAt: new Date("2026-12-04T17:53:15.000Z"), + updatedAt: new Date("2026-12-04T17:53:15.000Z"), + }, + ], + onAdd: async () => {}, + linkedRuns: [ + { + runId: "codie-run-1", + runtimeMode: "native", + status: "succeeded", + agentId: codie.id, + agentName: "Codie", + adapterType: "paperclip_runner", + createdAt: "2026-12-04T17:17:24.000Z", + startedAt: "2026-12-04T17:17:24.000Z", + finishedAt: "2026-12-04T17:18:39.000Z", + execution: { phase: "completed", label: "Completed", canStop: false, cause: null, lastConfirmedActivityAt: null, retryAt: null, attempt: 1, maxAttempts: 3, recoveryOwner: null, nextAction: null, permittedActions: [], predecessorRunId: null, successorRunId: null }, + }, + { + runId: "codie-run-2", + runtimeMode: "native", + status: "succeeded", + agentId: codie.id, + agentName: "Codie", + adapterType: "paperclip_runner", + createdAt: "2026-12-04T17:52:00.000Z", + startedAt: "2026-12-04T17:52:00.000Z", + finishedAt: "2026-12-04T17:53:12.000Z", + execution: { phase: "completed", label: "Completed", canStop: false, cause: null, lastConfirmedActivityAt: null, retryAt: null, attempt: 1, maxAttempts: 3, recoveryOwner: null, nextAction: null, permittedActions: [], predecessorRunId: null, successorRunId: null }, + }, + ], + }); + + const firstCommentEl = container.querySelector("#comment-reply-1"); + const secondCommentEl = container.querySelector("#comment-reply-2"); + expect(firstCommentEl?.textContent).toContain("I'll check the setup task's final notes."); + expect(firstCommentEl?.textContent).not.toContain("I'll check the latest saved result for PAP-7268."); + expect(secondCommentEl?.textContent).toContain("I'll check the latest saved result for PAP-7268."); + expect(secondCommentEl?.textContent).not.toContain("I'll check the setup task's final notes."); +}); diff --git a/ui/src/components/TaskChatThread.tsx b/ui/src/components/TaskChatThread.tsx index 6f040a42e1e..e05880eedb6 100644 --- a/ui/src/components/TaskChatThread.tsx +++ b/ui/src/components/TaskChatThread.tsx @@ -2038,6 +2038,7 @@ export function TaskChatThread(props: TaskChatThreadProps) { turnMergeMetaById.set(turnId, { agentKey: meta?.agentId ?? "", agentName: meta?.agentName, + anchorCommentId: null, parts: [{ entries, durationMs, failed }], }); settledTurns.push({ @@ -2251,9 +2252,17 @@ export function TaskChatThread(props: TaskChatThreadProps) { Number.isFinite(segment.startMs) && Number.isFinite(segmentFinishedMs) ? Math.max(0, segmentFinishedMs - segment.startMs) : durationMs; + const anchorCommentId = segmented + ? null + : sourceIsPaperclipRunner + ? sourceHasPresentationComment + ? sourcePresentationCommentId + : null + : (lastCommentIdByRun.get(source.id) ?? null); turnMergeMetaById.set(turnId, { agentKey: meta?.agentId ?? "", agentName: meta?.agentName, + anchorCommentId, parts: [ { entries: segment.entries, durationMs: segmentDurationMs, failed }, ], @@ -2283,13 +2292,7 @@ export function TaskChatThread(props: TaskChatThreadProps) { failed, }), }, - anchorCommentId: segmented - ? null - : sourceIsPaperclipRunner - ? sourceHasPresentationComment - ? sourcePresentationCommentId - : null - : (lastCommentIdByRun.get(source.id) ?? null), + anchorCommentId, // A post-steering segment ties the acknowledgement-backed human // bubble and is therefore inserted immediately after it. The first // segment retains the normal run-start slot. @@ -2346,6 +2349,7 @@ export function TaskChatThread(props: TaskChatThreadProps) { turnMergeMetaById.set(turnId, { agentKey: liveRun.agentId ?? "", agentName: liveRun.agentName, + anchorCommentId: null, parts: [ { entries: segment.entries, diff --git a/ui/src/components/design-sandbox/settled-preview-reply.ts b/ui/src/components/design-sandbox/settled-preview-reply.ts index 1c5b06149a4..02c294cc802 100644 --- a/ui/src/components/design-sandbox/settled-preview-reply.ts +++ b/ui/src/components/design-sandbox/settled-preview-reply.ts @@ -9,5 +9,5 @@ export function settledPreviewReply(response: TaskChatMessageItem, entries: Para id: `${runId}:turn`, kind: "turn", settled: true, historical: true, items: settledRunChildren(parsed), summary: buildTurnSummary(entries, { durationMs, failed }), }; - return attachSettledTurns([response, turn], new Map([[turn.id, { agentKey: "synthetic-agent", agentName: response.authorName, parts: [{ entries, durationMs, failed }] }]])); + return attachSettledTurns([response, turn], new Map([[turn.id, { agentKey: "synthetic-agent", agentName: response.authorName, anchorCommentId: null, parts: [{ entries, durationMs, failed }] }]])); } diff --git a/ui/src/components/task-chat/transcript-adapter.test.ts b/ui/src/components/task-chat/transcript-adapter.test.ts index 0518129503f..a3cd3c582b0 100644 --- a/ui/src/components/task-chat/transcript-adapter.test.ts +++ b/ui/src/components/task-chat/transcript-adapter.test.ts @@ -2558,6 +2558,7 @@ describe("coalesceSettledTurns (PAP-362)", () => { { agentKey: "agent-1", agentName: "CEO", + anchorCommentId: null, parts: [{ entries: runA, durationMs: 30_000 }], }, ], @@ -2566,6 +2567,7 @@ describe("coalesceSettledTurns (PAP-362)", () => { { agentKey: "agent-1", agentName: "CEO", + anchorCommentId: null, parts: [{ entries: runB, durationMs: 15_000 }], }, ], @@ -2628,6 +2630,7 @@ describe("coalesceSettledTurns (PAP-362)", () => { otherAgent.set("run-b:turn", { agentKey: "agent-2", agentName: "QA", + anchorCommentId: null, parts: [{ entries: runB }], }); const sameShape: TaskChatItem[] = [ @@ -2643,8 +2646,8 @@ describe("coalesceSettledTurns (PAP-362)", () => { it("lets another agent's chat message end the merge when runs carry no display name", () => { const unnamed = new Map([ - ["run-a:turn", { agentKey: "agent-1", parts: [{ entries: runA, durationMs: 30_000 }] }], - ["run-b:turn", { agentKey: "agent-1", parts: [{ entries: runB, durationMs: 15_000 }] }], + ["run-a:turn", { agentKey: "agent-1", anchorCommentId: null, parts: [{ entries: runA, durationMs: 30_000 }] }], + ["run-b:turn", { agentKey: "agent-1", anchorCommentId: null, parts: [{ entries: runB, durationMs: 15_000 }] }], ]); const fromAgent = (id: string, agentId: string, authorName: string): TaskChatItem => ({ id, kind: "message", author: "agent", authorName, agent: { id: agentId }, text: "Note.", @@ -2693,6 +2696,89 @@ describe("coalesceSettledTurns (PAP-362)", () => { (merged[0] as Extract).animateFold, ).toBe(true); }); + + it("never coalesces standaloneHeader (native runner) turns across consecutive replies", () => { + const nativeTurn = (id: string, commentary: string): TaskChatItem => ({ + id, + kind: "turn", + settled: true, + historical: true, + standaloneHeader: true, + items: [ + { + id: `${id}:phase:0`, + kind: "activity_phase", + active: false, + summary: "", + interstitial: { + id: `${id}:interstitial:0`, + kind: "message", + author: "agent", + channel: "progress", + interstitial: true, + text: commentary, + }, + items: [], + }, + ], + summary: buildTurnSummary([], { durationMs: 10_000 }), + }); + const nativeMeta = new Map([ + ["run-1:turn", { agentKey: "codie", agentName: "Codie", anchorCommentId: "msg-1", parts: [{ entries: [], durationMs: 10_000 }] }], + ["run-2:turn", { agentKey: "codie", agentName: "Codie", anchorCommentId: "msg-2", parts: [{ entries: [], durationMs: 12_000 }] }], + ]); + const items: TaskChatItem[] = [ + { id: "msg-1", kind: "message", author: "agent", authorName: "Codie", agent: { id: "codie" }, text: "First update.", sourceRunId: "run-1" }, + nativeTurn("run-1:turn", "I'll check the setup task's final notes."), + { id: "msg-2", kind: "message", author: "agent", authorName: "Codie", agent: { id: "codie" }, text: "Second update.", sourceRunId: "run-2" }, + nativeTurn("run-2:turn", "I'll check the latest saved result for PAP-7268."), + ]; + const out = coalesceSettledTurns(items, nativeMeta); + expect(out.map((i) => i.id)).toEqual(["msg-1", "run-1:turn", "msg-2", "run-2:turn"]); + const firstTurn = out[1] as Extract; + const secondTurn = out[3] as Extract; + expect(firstTurn.items).toHaveLength(1); + expect(secondTurn.items).toHaveLength(1); + }); + + it("does not coalesce legacy turns across fast_response or cross-task chatMessage bubbles", () => { + const withFastResponse: TaskChatItem[] = [ + agentBubble("msg-a"), + turn("run-a:turn", runA, 30_000), + { id: "msg-fr", kind: "message", author: "agent", authorName: "CEO", origin: "fast_response", text: "On it." }, + agentBubble("msg-b"), + turn("run-b:turn", runB, 15_000), + ]; + expect(coalesceSettledTurns(withFastResponse, metaById).map((i) => i.id)).toEqual([ + "msg-a", + "run-a:turn", + "msg-fr", + "msg-b", + "run-b:turn", + ]); + + const withChatMessage: TaskChatItem[] = [ + agentBubble("msg-a"), + turn("run-a:turn", runA, 30_000), + { + id: "msg-cross", + kind: "message", + author: "agent", + authorName: "CEO", + text: "Merged PR #123.", + chatMessage: { sourceTaskId: "task-1", sourceTaskIdentifier: "PAP-7354", sourceRunId: "other-run", contentHash: "abc" }, + }, + agentBubble("msg-b"), + turn("run-b:turn", runB, 15_000), + ]; + expect(coalesceSettledTurns(withChatMessage, metaById).map((i) => i.id)).toEqual([ + "msg-a", + "run-a:turn", + "msg-cross", + "msg-b", + "run-b:turn", + ]); + }); }); describe("attachSettledTurns (round 9)", () => { @@ -2725,6 +2811,7 @@ describe("attachSettledTurns (round 9)", () => { { agentKey: "agent-1", agentName: "CEO", + anchorCommentId: null, parts: [{ entries: runA, durationMs: 30_000 }], }, ], @@ -2771,7 +2858,7 @@ describe("attachSettledTurns (round 9)", () => { it("matches the bubble's agent ID when the run carries no display name", () => { const unnamed = new Map([ - ["run-a:turn", { agentKey: "agent-1", parts: [{ entries: runA, durationMs: 30_000 }] }], + ["run-a:turn", { agentKey: "agent-1", anchorCommentId: null, parts: [{ entries: runA, durationMs: 30_000 }] }], ]); const fromAgent = (agentId: string, authorName: string): TaskChatItem => ({ id: `msg-${agentId}`, kind: "message", author: "agent", authorName, agent: { id: agentId }, text: "Done.", @@ -2808,6 +2895,59 @@ describe("attachSettledTurns (round 9)", () => { (twice[0] as Extract).attachedTurn?.id, ).toBe("run-a:turn"); }); + + it("never attaches an unanchored or mismatched standaloneHeader turn, finalResponse turn, or cross-task chatMessage bubble", () => { + const standaloneTurn = (id: string, extra?: Partial): TaskChatTurnItem => ({ + id, + kind: "turn", + settled: true, + standaloneHeader: true, + items: [], + summary: buildTurnSummary([], { durationMs: 10_000 }), + ...extra, + }); + const meta = new Map([ + ["run-unanchored:turn", { agentKey: "codie", agentName: "Codie", anchorCommentId: null, parts: [] }], + ["run-anchored:turn", { agentKey: "codie", agentName: "Codie", anchorCommentId: "msg-1", parts: [] }], + ["run-other:turn", { agentKey: "codie", agentName: "Codie", anchorCommentId: "msg-2", parts: [] }], + ]); + const bubble1: TaskChatItem = { + id: "msg-1", kind: "message", author: "agent", authorName: "Codie", agent: { id: "codie" }, text: "First reply.", sourceRunId: "run-1", + }; + + // Unanchored native turn after an earlier reply stays standalone (must not invert above msg-1). + const unanchoredOut = attachSettledTurns([bubble1, standaloneTurn("run-unanchored:turn")], meta); + expect(unanchoredOut.map((i) => i.id)).toEqual(["msg-1", "run-unanchored:turn"]); + + // Mismatched anchorCommentId native turn stays standalone. + const mismatchedOut = attachSettledTurns([bubble1, standaloneTurn("run-other:turn")], meta); + expect(mismatchedOut.map((i) => i.id)).toEqual(["msg-1", "run-other:turn"]); + + // Turn carrying its own finalResponse never attaches to a preceding bubble. + const withFinal = standaloneTurn("run-anchored:turn", { + finalResponse: { id: "final-1", kind: "message", author: "agent", text: "Own final." }, + }); + const finalOut = attachSettledTurns([bubble1, withFinal], meta); + expect(finalOut.map((i) => i.id)).toEqual(["msg-1", "run-anchored:turn"]); + + // Cross-task send_chat_message bubble never accepts an attached turn. + const crossTaskBubble: TaskChatItem = { + id: "msg-1", + kind: "message", + author: "agent", + authorName: "Codie", + agent: { id: "codie" }, + text: "Merged PR #123.", + chatMessage: { sourceTaskId: "task-1", sourceTaskIdentifier: "PAP-7354", sourceRunId: "other-run", contentHash: "abc" }, + }; + const crossOut = attachSettledTurns([crossTaskBubble, standaloneTurn("run-anchored:turn")], meta); + expect(crossOut.map((i) => i.id)).toEqual(["msg-1", "run-anchored:turn"]); + + // Matching anchored native turn attaches cleanly to its own reply bubble. + const matchedOut = attachSettledTurns([bubble1, standaloneTurn("run-anchored:turn")], meta); + expect(matchedOut).toHaveLength(1); + expect((matchedOut[0] as Extract).attachedTurn?.id).toBe("run-anchored:turn"); + }); }); describe("assembleThreadItems (PAP-367)", () => { @@ -2899,6 +3039,7 @@ describe("assembleThreadItems (PAP-367)", () => { { agentKey: "agent-1", agentName: "CEO", + anchorCommentId: null, parts: [{ entries: [toolCall("Read")], durationMs: 34_000 }], }, ], diff --git a/ui/src/components/task-chat/transcript-adapter.ts b/ui/src/components/task-chat/transcript-adapter.ts index 78e07f84359..ed7abee71b1 100644 --- a/ui/src/components/task-chat/transcript-adapter.ts +++ b/ui/src/components/task-chat/transcript-adapter.ts @@ -2498,6 +2498,12 @@ export interface SettledTurnMergeMeta { agentKey: string; /** Display name, used to keep another agent's bubble from bridging a merge. */ agentName?: string; + /** + * Explicit reply comment id this turn anchored to (`null` = unanchored). + * Keeps unanchored or mismatched standaloneHeader turns from attaching above + * an earlier agent bubble and inverting chronology. + */ + anchorCommentId: string | null; parts: TurnSummaryPart[]; } @@ -2542,6 +2548,15 @@ export function coalesceSettledTurns( let heldIdx = -1; // index in `out` of the last mergeable settled turn for (const item of items) { if (item.kind === "turn" && item.settled) { + // Paperclip Runner turns own an open chronological timeline rendered + // above their reply bubble (`beforeTurn`), including progress commentary. + // Never coalesce them across runs or replies: doing so steals earlier + // runs' timelines and stacks every prior commentary onto the newest reply. + if (item.standaloneHeader) { + out.push(item); + heldIdx = -1; + continue; + } const meta = metaFor(item.id); const held = heldIdx >= 0 ? (out[heldIdx] as TaskChatTurnItem) : null; const heldMeta = held ? metaFor(held.id) : undefined; @@ -2572,7 +2587,12 @@ export function coalesceSettledTurns( continue; } out.push(item); - if (item.kind === "message" && item.author === "agent") { + if ( + item.kind === "message" && + item.author === "agent" && + item.origin !== "fast_response" && + !item.chatMessage + ) { // The same agent's reply bubble sits between its runs — keep merging // across it. A different agent's bubble ends the run of turns. const heldMeta = @@ -2608,15 +2628,18 @@ export function attachSettledTurns( if ( item.kind === "turn" && item.settled && + !item.finalResponse && prev?.kind === "message" && prev.author === "agent" && prev.origin !== "fast_response" && + !prev.chatMessage && !prev.interstitial && !prev.streaming && prev.attachedTurn == null ) { const meta = metaById.get(item.id); - if (meta != null && sameTurnAgent(meta, prev)) { + const standaloneOwnershipMatches = !item.standaloneHeader || meta?.anchorCommentId === prev.id; + if (meta != null && sameTurnAgent(meta, prev) && standaloneOwnershipMatches) { out[out.length - 1] = { ...prev, attachedTurn: item }; continue; }