Skip to content

The agent that grows with you

fix(desktop): one reply renders once per turn, whichever path delivered it

Teknium43c66b85bb47
diff --git a/apps/desktop/e2e/core/reattach-running-turn.spec.ts b/apps/desktop/e2e/core/reattach-running-turn.spec.ts
new file mode 100644
index 00000000000..fffc5e15018
--- /dev/null
+++ b/apps/desktop/e2e/core/reattach-running-turn.spec.ts
@@ -0,0 +1,174 @@
+/**
+ * C2 core: a window that re-attaches to a turn that is still running.
+ *
+ * The turn persists its narrated tool round before the tool runs, so the
+ * history a re-attaching window fetches already holds that round while the
+ * live stream keeps drawing the same segment (#127288, #127665). Each step
+ * leaves the chat mid-turn by a different road (switch away and back, reload,
+ * a second switch during the running tool) and holds the settled turn to the
+ * transcript oracle, whose in-page sampler also fails a transient double.
+ */
+
+import fs from 'node:fs'
+import path from 'node:path'
+
+import { type ElectronApplication, expect, type Page, test } from '@playwright/test'
+
+import {
+ coreAppEnv,
+ type CoreSandbox,
+ createCoreSandbox,
+ currentSessionId,
+ launchCoreApp,
+ recordWebSockets,
+ send,
+ waitForInteractive,
+ writeProviderHome,
+ type WsRecorder
+} from './harness'
+import { assertTranscriptOracle, installDuplicateSampler, type OracleTarget } from './oracle'
+import { shellQuote } from './process-census'
+import { gate, type ScriptedProvider, startScriptedProvider } from './provider'
+
+const nonce = Math.random()
+ .toString(36)
+ .slice(2, 8)
+ .replace(/[^a-z0-9]/g, 'x')
+ .padEnd(4, 'q')
+
+const U = (n: number) => `U${n}-${nonce}`
+const A = (n: number) => `A${n}-${nonce}`
+const AI = (n: number) => `A${n}i-${nonce}`
+
+const words = (marker: string, ...rest: string[]) => [
+ `${marker} `,
+ ...rest.map((w, i) => (i === rest.length - 1 ? w : `${w} `))
+]
+
+const echo = (tag: string) => ({ name: 'terminal', args: { command: `echo core-${tag}` } })
+
+function viewport(page: Page) {
+ return page.locator('[data-slot="aui_thread-viewport"]').filter({ visible: true }).first()
+}
+
+function stopButton(page: Page) {
+ return page.locator('[data-slot="composer-root"] button[aria-label="Stop"]')
+}
+
+async function settled(page: Page, ws: WsRecorder, marker: string) {
+ await expect
+ .poll(() => ws.events.some(e => e.type === 'message.complete' && String(e.payload?.text ?? '').includes(marker)), {
+ timeout: 120_000,
+ message: `message.complete for ${marker}`
+ })
+ .toBe(true)
+ await expect(stopButton(page)).toHaveCount(0, { timeout: 30_000 })
+}
+
+async function openSession(page: Page, sessionId: string, waitFor: string) {
+ await page.evaluate(id => {
+ window.location.hash = id ? `#/${id}` : '#/'
+ }, sessionId)
+ await expect.poll(() => currentSessionId(page)).toBe(sessionId)
+
+ if (waitFor) {
+ await expect(viewport(page)).toContainText(waitFor, { timeout: 30_000 })
+ }
+}
+
+async function withApp(
+ label: string,
+ body: (ctx: {
+ app: ElectronApplication
+ page: Page
+ provider: ScriptedProvider
+ sandbox: CoreSandbox
+ ws: WsRecorder
+ }) => Promise<void>
+) {
+ const provider = await startScriptedProvider()
+ const sandbox = createCoreSandbox(label)
+ writeProviderHome(sandbox.hermesHome, provider.url)
+ const { app, page } = await launchCoreApp(coreAppEnv(sandbox))
+ const ws = recordWebSockets(page)
+
+ try {
+ await waitForInteractive(app, page)
+ await installDuplicateSampler(page)
+ await body({ app, page, provider, sandbox, ws })
+ } finally {
+ await app.close().catch(() => undefined)
+ await provider.close()
+ sandbox.cleanup()
+ }
+}
+
+test('re-attaching to a running tool turn renders each segment once', async () => {
+ await withApp('reattach', async ({ page, provider, sandbox, ws }) => {
+ const session: OracleTarget = { sessionId: '', expectUserMarkers: [] }
+
+ await test.step('switch away and back while the answer after a tool streams', async () => {
+ const hold = gate()
+ provider.script(U(1), [
+ { text: words(AI(1), 'narrating', 'before', 'the', 'tool'), toolCalls: [echo('reattach-a')] },
+ { text: words(A(1), 'answer', 'after', 'the', 'tool'), holdAfterFirstChunk: hold }
+ ])
+ await send(page, `${U(1)} narrate then answer`, 'Enter', ws)
+ await provider.streamStarted(U(1), 1)
+ await expect(viewport(page)).toContainText(A(1))
+ session.sessionId = await currentSessionId(page)
+ session.expectUserMarkers.push(U(1))
+ await openSession(page, '', '')
+ await openSession(page, session.sessionId, AI(1))
+ hold.open()
+ await settled(page, ws, A(1))
+ await assertTranscriptOracle(page, ws, provider, session, 're-attach during the answer')
+ })
+
+ await test.step('switch away and back while the tool runs', async () => {
+ const ready = path.join(sandbox.root, `reattach-running-${nonce}`)
+ const release = path.join(sandbox.root, `reattach-release-${nonce}`)
+ provider.script(U(2), [
+ {
+ text: words(AI(2), 'running', 'a', 'slow', 'tool'),
+ toolCalls: [
+ {
+ name: 'terminal',
+ args: {
+ command: `touch ${shellQuote(ready)}; while [ ! -e ${shellQuote(release)} ]; do sleep 0.05; done; echo core-ran`
+ }
+ }
+ ]
+ },
+ { text: words(AI(3), 'second', 'round'), toolCalls: [echo('reattach-b')] },
+ { text: words(A(2), 'answer', 'after', 'two', 'rounds') }
+ ])
+ await send(page, `${U(2)} slow tool`, 'Enter', ws)
+ await expect.poll(() => fs.existsSync(ready), { timeout: 60_000, message: 'tool started' }).toBe(true)
+ session.expectUserMarkers.push(U(2))
+ await openSession(page, '', '')
+ await openSession(page, session.sessionId, AI(2))
+ fs.writeFileSync(release, '')
+ await settled(page, ws, A(2))
+ await assertTranscriptOracle(page, ws, provider, session, 're-attach during the tool')
+ })
+
+ await test.step('reload while the answer after a tool streams', async () => {
+ const hold = gate()
+ provider.script(U(4), [
+ { text: words(AI(4), 'narrating', 'again'), toolCalls: [echo('reattach-c')] },
+ { text: words(A(4), 'answer', 'across', 'a', 'reload'), holdAfterFirstChunk: hold }
+ ])
+ await send(page, `${U(4)} reload mid answer`, 'Enter', ws)
+ await provider.streamStarted(U(4), 1)
+ await expect(viewport(page)).toContainText(A(4))
+ session.expectUserMarkers.push(U(4))
+ await page.reload()
+ await expect(viewport(page)).toContainText(AI(4), { timeout: 60_000 })
+ await installDuplicateSampler(page)
+ hold.open()
+ await settled(page, ws, A(4))
+ await assertTranscriptOracle(page, ws, provider, session, 're-attach after a reload')
+ })
+ })
+})
diff --git a/apps/desktop/src/app/chat/runtime-repository.ts b/apps/desktop/src/app/chat/runtime-repository.ts
index f15329afa1e..f2f66838acb 100644
--- a/apps/desktop/src/app/chat/runtime-repository.ts
+++ b/apps/desktop/src/app/chat/runtime-repository.ts
@@ -3,7 +3,11 @@ import type { ExportedMessageRepository, ThreadMessage } from '@assistant-ui/rea
import { useMemo, useRef } from 'react'
import type { ChatMessage } from '@/lib/chat-messages'
-import { withUniqueToolCallIdsWithinMessage } from '@/lib/chat-messages'
+import {
+ createTurnReplyFoldCache,
+ foldTurnReplyDuplicates,
+ withUniqueToolCallIdsWithinMessage
+} from '@/lib/chat-messages'
import { coalesceToolOnlyAssistants, createToolMergeCache, toRuntimeMessage } from '@/lib/chat-runtime'
// The exact fallback status ExportedMessageRepository.fromBranchableArray uses.
@@ -27,6 +31,7 @@ const FALLBACK_STATUS = getAutoStatus(false, false, false, false, undefined)
export function useRuntimeMessageRepository(messages: ChatMessage[]): ExportedMessageRepository {
const cacheRef = useRef(new WeakMap<ChatMessage, ThreadMessage>())
const toolMergeCacheRef = useRef(createToolMergeCache())
+ const turnFoldCacheRef = useRef(createTurnReplyFoldCache())
return useMemo(() => {
const items: { message: ThreadMessage; parentId: string | null }[] = []
@@ -35,7 +40,11 @@ export function useRuntimeMessageRepository(messages: ChatMessage[]): ExportedMe
let visibleParentId: string | null = null
let headId: string | null = null
- for (const message of coalesceToolOnlyAssistants(messages, toolMergeCacheRef.current)) {
+ // One reply renders once per turn, whichever merge path let a second copy
+ // into the store (live overlay, committed row, re-attach, replay).
+ const folded = foldTurnReplyDuplicates(messages, turnFoldCacheRef.current)
+
+ for (const message of coalesceToolOnlyAssistants(folded, toolMergeCacheRef.current)) {
// A repeated id is a transcript bug upstream, but it must not reach the
// repository: MessageRepository throws on the second link ("A message
// with the same id already exists in the parent tree") and takes the
diff --git a/apps/desktop/src/app/chat/turn-reply-fold.test.ts b/apps/desktop/src/app/chat/turn-reply-fold.test.ts
new file mode 100644
index 00000000000..534ac5137a7
--- /dev/null
+++ b/apps/desktop/src/app/chat/turn-reply-fold.test.ts
@@ -0,0 +1,102 @@
+import { act, cleanup, renderHook } from '@testing-library/react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { renderMessageStream } from '@/app/session/hooks/use-message-stream/test-harness'
+import { STREAM_DELTA_FLUSH_MS } from '@/app/session/hooks/use-message-stream/utils'
+import type { ChatMessage, ChatMessagePart } from '@/lib/chat-messages'
+
+import { useRuntimeMessageRepository } from './runtime-repository'
+
+// One stored reply reaches the store through more than one path (live stream,
+// the committed row persisted before a tool round, a history read on
+// re-attach). Whatever path lets a second copy in, the transcript the runtime
+// renders shows the reply once.
+
+const ANSWER = 'The config loads from the profile home, so the override wins over the default.'
+const NEXT = 'Both checks pass; the second run confirms it.'
+
+const text = (body: string): ChatMessagePart => ({ type: 'text', text: body })
+
+const tool = (id: string, command: string): ChatMessagePart =>
+ ({ type: 'tool-call', toolCallId: id, toolName: 'terminal', args: { command }, result: 'ok' }) as ChatMessagePart
+
+const renderedTexts = (messages: ChatMessage[]) => {
+ const { result } = renderHook(() => useRuntimeMessageRepository(messages))
+
+ return result.current.messages
+ .filter(item => item.message.role === 'assistant')
+ .map(item =>
+ item.message.content.flatMap(part =>
+ part.type === 'text' ? [part.text] : part.type === 'tool-call' ? [part.toolCallId] : []
+ )
+ )
+}
+
+afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+})
+
+describe('a turn reply renders once', () => {
+ it('re-attach: the committed row and its live copies render as one reply plus the new tail (#127288)', () => {
+ const transcript: ChatMessage[] = [
+ { id: 'u1', role: 'user', parts: [text('check the config')], rowId: 6916 },
+ {
+ id: 'row-6917',
+ role: 'assistant',
+ rowId: 6917,
+ parts: [{ type: 'reasoning', text: 'look first' }, text(ANSWER), tool('call_a', 'cat a'), tool('call_b', 'cat b')]
+ },
+ { id: 'assistant-stream-1', role: 'assistant', interim: true, parts: [text(ANSWER)] },
+ {
+ id: 'assistant-stream-2',
+ role: 'assistant',
+ pending: true,
+ parts: [tool('call_a', 'cat a'), tool('call_b', 'cat b'), text(NEXT)]
+ }
+ ]
+
+ expect(renderedTexts(transcript)).toEqual([[ANSWER, 'call_a', 'call_b'], [NEXT]])
+ })
+
+ it('a message.start between the interim seal and the re-stream paints the commentary once (#144609)', async () => {
+ const SID = 'turn-reply-fold-144609'
+ const COMMENTARY = 'For fidelity the decisive knob is lm_head. Let me verify what each checkpoint does with it.'
+ const FINAL = 'Checkpoint B keeps lm_head in full precision; A quantizes it.'
+ vi.useFakeTimers()
+ const stream = renderMessageStream(SID)
+
+ const send = (type: string, payload: Record<string, unknown>) =>
+ act(() => stream.handleEvent({ type: type as never, payload, session_id: SID }))
+
+ const flush = () => act(() => vi.advanceTimersByTime(STREAM_DELTA_FLUSH_MS * 2))
+
+ await send('message.start', {})
+ await send('message.interim', { text: COMMENTARY, already_streamed: true })
+ await send('message.start', {})
+ await send('message.delta', { text: COMMENTARY })
+ await flush()
+ await send('message.interim', { text: COMMENTARY, already_streamed: true })
+ await send('tool.start', { name: 'terminal', tool_id: 'call_v', args: { command: 'ls' } })
+ await send('tool.complete', { name: 'terminal', tool_id: 'call_v', result: 'ok' })
+ await send('message.delta', { text: FINAL })
+ await flush()
+ await send('message.complete', { text: FINAL })
+
+ const texts = renderedTexts(stream.state(SID).messages).flat()
+
+ expect(texts.filter(value => value === COMMENTARY)).toHaveLength(1)
+ expect(texts.filter(value => value === FINAL)).toHaveLength(1)
+ })
+
+ it('keeps commentary the model authored twice in two tool rounds', () => {
+ const transcript: ChatMessage[] = [
+ { id: 'u1', role: 'user', parts: [text('run both')] },
+ { id: 's1', role: 'assistant', interim: true, parts: [text('Checking.'), tool('call_1', 'ls a')] },
+ { id: 's2', role: 'assistant', interim: true, parts: [text('Checking.'), tool('call_2', 'ls b')] },
+ { id: 's3', role: 'assistant', parts: [text(NEXT)] }
+ ]
+
+ expect(renderedTexts(transcript)).toEqual([['Checking.', 'call_1'], ['Checking.', 'call_2'], [NEXT]])
+ })
+})
diff --git a/apps/desktop/src/lib/chat-messages/index.ts b/apps/desktop/src/lib/chat-messages/index.ts
index efde87bcd2a..776a3c9cc8e 100644
--- a/apps/desktop/src/lib/chat-messages/index.ts
+++ b/apps/desktop/src/lib/chat-messages/index.ts
@@ -38,4 +38,6 @@ export {
withUniqueToolCallIdsWithinMessage
} from './tool-parts'
export type { PendingClarifyProjection, SettledClarifyProjection } from './tool-parts'
+export { createTurnReplyFoldCache, foldTurnReplyDuplicates } from './turn-reply-fold'
+export type { TurnReplyFoldCache } from './turn-reply-fold'
export type { ChatMessage, ChatMessagePart, GatewayEventPayload, TimelinePartMetadata } from './types'
diff --git a/apps/desktop/src/lib/chat-messages/turn-reply-fold.ts b/apps/desktop/src/lib/chat-messages/turn-reply-fold.ts
new file mode 100644
index 00000000000..c9a38e56d76
--- /dev/null
+++ b/apps/desktop/src/lib/chat-messages/turn-reply-fold.ts
@@ -0,0 +1,310 @@
+import { normalizeWs } from './parts'
+import type { ChatMessage, ChatMessagePart } from './types'
+
+/**
+ * One reply renders once per turn, whichever paths delivered it.
+ *
+ * A turn's reply reaches the renderer more than once: as live stream events,
+ * as the committed row the backend persists before each tool round, and again
+ * through every history read (re-attach to a running turn, reload, switch-back,
+ * reconnect replay, the in-flight journal). Each merge path used to decide on
+ * its own whether two bubbles were the same reply, and every path that decided
+ * wrong painted the reply twice while `state.db` held one row. This fold is the
+ * one place that decides, at the render boundary every surface shares, so a
+ * missed merge can no longer reach the screen.
+ *
+ * Identity is the content a bubble shows, compared in order: its text, its tool
+ * calls (name + id + arguments) and its other parts. Reasoning is ignored. A
+ * turn spans the assistant and system rows between two visible user rows.
+ *
+ * - A bubble whose content another bubble of the turn already shows in order is
+ * the same reply delivered twice. The committed copy (rowId) wins over a
+ * live copy, then the copy carrying the turn's footer (not interim), then the
+ * earlier one.
+ * - A bubble that starts with content an earlier bubble shows, including at
+ * least one tool call, is a live segment re-drawn after a re-attach: that
+ * leading run is dropped and the new remainder stays (#127288).
+ *
+ * Text alone never trims a bubble that adds anything new: equal commentary in
+ * two tool rounds is authored twice and stays, matching hydration
+ * (`dedupeRepeatedTextInParts`). Render-only; the store keeps every row.
+ */
+
+type Atom =
+ | { kind: 'other'; key: string }
+ | { kind: 'text'; key: string }
+ | { args: string; id: string; kind: 'tool'; name: string }
+
+interface Bubble {
+ atoms: Atom[]
+ /** Index into `parts` of the part each atom came from. */
+ partIndex: number[]
+}
+
+const PART_METADATA = new Set([
+ 'completedAt',
+ 'interrupted',
+ 'mediaSource',
+ 'sourceRowId',
+ 'storedResultToolName',
+ 'timestamp',
+ 'toolResultMetadata',
+ 'unpairedStoredToolResult'
+])
+
+const DUP_SUFFIX_RE = /-dup-\d+$/
+
+function stableArgs(part: Extract<ChatMessagePart, { type: 'tool-call' }>): string {
+ if (part.args && typeof part.args === 'object' && Object.keys(part.args).length) {
+ return JSON.stringify(part.args, Object.keys(part.args).sort())
+ }
+
+ const text = typeof part.argsText === 'string' ? part.argsText.trim() : ''
+
+ if (!text) {
+ return ''
+ }
+
+ try {
+ const parsed = JSON.parse(text)
+
+ return parsed && typeof parsed === 'object' ? JSON.stringify(parsed, Object.keys(parsed).sort()) : text
+ } catch {
+ return text
+ }
+}
+
+function atomOf(part: ChatMessagePart): Atom | null {
+ if (part.type === 'reasoning') {
+ return null
+ }
+
+ if (part.type === 'text') {
+ const key = normalizeWs(part.mediaSource ?? part.text)
+
+ return key ? { kind: 'text', key } : null
+ }
+
+ if (part.type === 'tool-call') {
+ return {
+ args: stableArgs(part),
+ id: (part.toolCallId ?? '').replace(DUP_SUFFIX_RE, ''),
+ kind: 'tool',
+ name: part.toolName ?? ''
+ }
+ }
+
+ const body = Object.fromEntries(Object.entries(part).filter(([key]) => !PART_METADATA.has(key)))
+
+ return { kind: 'other', key: JSON.stringify(body) }
+}
+
+function sameAtom(a: Atom, b: Atom): boolean {
+ if (a.kind !== b.kind) {
+ return false
+ }
+
+ if (a.kind === 'tool' && b.kind === 'tool') {
+ // A live tool part can carry no arguments yet; ids are per turn.
+ return a.name === b.name && a.id === b.id && (!a.args || !b.args || a.args === b.args)
+ }
+
+ return (a as { key: string }).key === (b as { key: string }).key
+}
+
+const bubbleCache = new WeakMap<ChatMessagePart[], Bubble>()
+
+function bubbleOf(message: ChatMessage): Bubble {
+ const cached = bubbleCache.get(message.parts)
+
+ if (cached) {
+ return cached
+ }
+
+ const atoms: Atom[] = []
+ const partIndex: number[] = []
+
+ message.parts.forEach((part, index) => {
+ const atom = atomOf(part)
+
+ if (atom) {
+ atoms.push(atom)
+ partIndex.push(index)
+ }
+ })
+
+ const bubble = { atoms, partIndex }
+ bubbleCache.set(message.parts, bubble)
+
+ return bubble
+}
+
+/** `inner` appears in `outer` in order (a subsequence). */
+function covers(outer: Atom[], inner: Atom[]): boolean {
+ let at = 0
+
+ for (const atom of outer) {
+ if (at < inner.length && sameAtom(atom, inner[at])) {
+ at += 1
+ }
+ }
+
+ return at === inner.length
+}
+
+/** Length of the longest prefix of `atoms` found as a contiguous run in `earlier`. */
+function coveredPrefix(earlier: Atom[], atoms: Atom[]): number {
+ let best = 0
+
+ for (let start = 0; start < earlier.length; start += 1) {
+ let length = 0
+
+ while (
+ length < atoms.length &&
+ start + length < earlier.length &&
+ sameAtom(earlier[start + length], atoms[length])
+ ) {
+ length += 1
+ }
+
+ best = Math.max(best, length)
+ }
+
+ return best
+}
+
+// Branch alternatives (regenerate) share content by design and render one at a time.
+function isReplyBubble(message: ChatMessage): boolean {
+ return message.role === 'assistant' && !message.hidden && !message.error && !message.branchGroupId
+}
+
+/** Prefer the committed copy, then the copy that carries the turn's footer. */
+function rank(message: ChatMessage): number {
+ return (message.rowId === undefined ? 0 : 2) + (message.interim ? 0 : 1)
+}
+
+interface TurnFold {
+ dropped: Set<ChatMessage>
+ trimmed: Map<ChatMessage, number>
+}
+
+function foldTurn(turn: ChatMessage[], fold: TurnFold): void {
+ const bubbles = turn.map(bubbleOf)
+
+ for (let i = 0; i < turn.length; i += 1) {
+ if (!bubbles[i].atoms.length || fold.dropped.has(turn[i])) {
+ continue
+ }
+
+ for (let j = 0; j < turn.length; j += 1) {
+ if (j === i || fold.dropped.has(turn[j]) || !covers(bubbles[j].atoms, bubbles[i].atoms)) {
+ continue
+ }
+
+ const otherIsEarlier = j < i
+ const laterWins = otherIsEarlier ? rank(turn[i]) > rank(turn[j]) : rank(turn[j]) > rank(turn[i])
+
+ // Between equal copies the better-ranked one stays (ties keep the
+ // first). An earlier superset always wins; a later superset retires an
+ // earlier subset only when it outranks it (the committed row over its
+ // live copy), so a growing live bubble never hides committed history.
+ const drop = covers(bubbles[i].atoms, bubbles[j].atoms)
+ ? otherIsEarlier !== laterWins
+ : otherIsEarlier || laterWins
+
+ if (drop) {
+ fold.dropped.add(turn[i])
+
+ break
+ }
+ }
+ }
+
+ for (let i = 1; i < turn.length; i += 1) {
+ const { atoms, partIndex } = bubbles[i]
+
+ if (fold.dropped.has(turn[i]) || !atoms.length) {
+ continue
+ }
+
+ let prefix = 0
+
+ for (let j = 0; j < i; j += 1) {
+ if (!fold.dropped.has(turn[j])) {
+ prefix = Math.max(prefix, coveredPrefix(bubbles[j].atoms, atoms))
+ }
+ }
+
+ if (prefix > 0 && prefix < atoms.length && atoms.slice(0, prefix).some(atom => atom.kind === 'tool')) {
+ fold.trimmed.set(turn[i], partIndex[prefix - 1] + 1)
+ }
+ }
+}
+
+export type TurnReplyFoldCache = WeakMap<ChatMessage, { cut: number; message: ChatMessage }>
+
+export function createTurnReplyFoldCache(): TurnReplyFoldCache {
+ return new WeakMap()
+}
+
+/**
+ * Drop the bubbles (and leading segments) of a turn that repeat what another
+ * bubble of the same turn already shows. Returns `messages` itself when there
+ * is nothing to fold, and stable trimmed objects across calls via `cache`.
+ */
+export function foldTurnReplyDuplicates(messages: ChatMessage[], cache?: TurnReplyFoldCache): ChatMessage[] {
+ const fold: TurnFold = { dropped: new Set(), trimmed: new Map() }
+ let turn: ChatMessage[] = []
+
+ const flush = () => {
+ if (turn.length > 1) {
+ foldTurn(turn, fold)
+ }
+
+ turn = []
+ }
+
+ for (const message of messages) {
+ if (message.role === 'user' && !message.hidden) {
+ flush()
+ } else if (isReplyBubble(message)) {
+ turn.push(message)
+ }
+ }
+
+ flush()
+
+ if (!fold.dropped.size && !fold.trimmed.size) {
+ return messages
+ }
+
+ const out: ChatMessage[] = []
+
+ for (const message of messages) {
+ if (fold.dropped.has(message)) {
+ continue
+ }
+
+ const cut = fold.trimmed.get(message)
+
+ if (cut === undefined) {
+ out.push(message)
+
+ continue
+ }
+
+ const cached = cache?.get(message)
+
+ if (cached && cached.cut === cut) {
+ out.push(cached.message)
+
+ continue
+ }
+
+ const trimmed = { ...message, parts: message.parts.slice(cut) }
+ cache?.set(message, { cut, message: trimmed })
+ out.push(trimmed)
+ }
+
+ return out
+}