Skip to content

The agent that grows with you

Revert the render-time turn reply fold (maintainer ruling)

Teknium0c06e8e6a61e
diff --git a/apps/desktop/src/app/chat/runtime-repository.ts b/apps/desktop/src/app/chat/runtime-repository.ts
index f2f66838acb..f15329afa1e 100644
--- a/apps/desktop/src/app/chat/runtime-repository.ts
+++ b/apps/desktop/src/app/chat/runtime-repository.ts
@@ -3,11 +3,7 @@ import type { ExportedMessageRepository, ThreadMessage } from '@assistant-ui/rea
import { useMemo, useRef } from 'react'
import type { ChatMessage } from '@/lib/chat-messages'
-import {
- createTurnReplyFoldCache,
- foldTurnReplyDuplicates,
- withUniqueToolCallIdsWithinMessage
-} from '@/lib/chat-messages'
+import { withUniqueToolCallIdsWithinMessage } from '@/lib/chat-messages'
import { coalesceToolOnlyAssistants, createToolMergeCache, toRuntimeMessage } from '@/lib/chat-runtime'
// The exact fallback status ExportedMessageRepository.fromBranchableArray uses.
@@ -31,7 +27,6 @@ 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 }[] = []
@@ -40,11 +35,7 @@ export function useRuntimeMessageRepository(messages: ChatMessage[]): ExportedMe
let visibleParentId: string | null = null
let headId: string | null = null
- // 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)) {
+ for (const message of coalesceToolOnlyAssistants(messages, 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
deleted file mode 100644
index c850e029dd6..00000000000
--- a/apps/desktop/src/app/chat/turn-reply-fold.test.ts
+++ /dev/null
@@ -1,115 +0,0 @@
-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('a stored prompt delivered twice is one turn, and its reply copies fold', () => {
- const { result } = renderHook(() =>
- useRuntimeMessageRepository([
- { id: 'user-temp-1', role: 'user', parts: [text('check the config')], rowId: 41 },
- { id: 'assistant-stream-1', role: 'assistant', parts: [text(ANSWER)] },
- { id: 'row-41', role: 'user', parts: [text('check the config')], rowId: 41 },
- { id: 'row-42', role: 'assistant', parts: [text(ANSWER)], rowId: 42 }
- ])
- )
-
- expect(result.current.messages.map(item => item.message.id)).toEqual(['user-temp-1', 'row-42'])
- })
-
- 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 776a3c9cc8e..efde87bcd2a 100644
--- a/apps/desktop/src/lib/chat-messages/index.ts
+++ b/apps/desktop/src/lib/chat-messages/index.ts
@@ -38,6 +38,4 @@ 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
deleted file mode 100644
index 72b749e77af..00000000000
--- a/apps/desktop/src/lib/chat-messages/turn-reply-fold.ts
+++ /dev/null
@@ -1,328 +0,0 @@
-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).
- *
- * A user row whose stored row id an earlier user row already carries is the
- * same prompt twice: it is dropped and its turn joins the earlier one, so the
- * reply copies under it fold too.
- *
- * 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 = []
- }
-
- const userRows = new Set<number>()
-
- for (const message of messages) {
- if (message.role === 'user' && !message.hidden) {
- // The same stored prompt delivered twice (a history read beside the
- // bound live row) is one turn, not two.
- if (message.rowId !== undefined && userRows.has(message.rowId)) {
- fold.dropped.add(message)
-
- continue
- }
-
- if (message.rowId !== undefined) {
- userRows.add(message.rowId)
- }
-
- 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
-}