The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a steered turn's /btw and /bg notices stay in their own turn in a long chat
Teknium85f39e6bb871
diff --git a/apps/desktop/src/app/chat/transcript-backfill.ts b/apps/desktop/src/app/chat/transcript-backfill.tsindex 15b20326e73..6e43ae91e28 100644--- a/apps/desktop/src/app/chat/transcript-backfill.ts+++ b/apps/desktop/src/app/chat/transcript-backfill.ts@@ -322,6 +322,24 @@ function mergeOverlappingTail(previous: ChatMessage[], refreshedTail: ChatMessag}const byRowId = new Map<number, StoredRowSlot>()+ // A turn the page holds whole is the page's to draw, whatever stored ids+ // its live rows managed to bind (see transcript-receipt-turns).+ const receiptDrop = dropReceiptTurnsThePageHolds(previous, refreshedTail)+ // Local notices of a dropped turn, keyed by the page row they precede.+ const anchored = new Map<number, ChatMessage[]>()++ const anchorNotice = (message: ChatMessage): boolean => {+ const anchor = receiptDrop.noticeAnchors.get(message.id)+ const slot = anchor === undefined ? undefined : refreshedBySource.get(anchor)?.rowId++ if (slot === undefined) {+ return false+ }++ anchored.set(slot, [...(anchored.get(slot) ?? []), message])++ return true+ }const place = (messages: ChatMessage[], fresh: boolean): ChatMessage[] => {let pending: ChatMessage[] = []@@ -373,17 +391,22 @@ function mergeOverlappingTail(previous: ChatMessage[], refreshedTail: ChatMessagreturn pending}- // A turn the page holds whole is the page's to draw, whatever stored ids- // its live rows managed to bind (see transcript-receipt-turns).- const previousTrailing = place(dropReceiptTurnsThePageHolds(previous, refreshedTail), false)+ const previousTrailing = place(+ receiptDrop.window.filter(message => !anchorNotice(message)),+ false+ )+const freshRows = dropPageRowsOfStoppedTurns(previous, dropPageFoldsCutByTheBoundary(previous, refreshedTail))const refreshedTrailing = place(freshRows, true)const stored = [...byRowId.entries()].sort((left, right) => left[0] - right[0])- .flatMap(([, { leading, message }]) => [...leading, message])+ .flatMap(([rowId, { leading, message }]) => [...leading, ...(anchored.get(rowId) ?? []), message])++ // A notice whose anchor row the page dropped after all still stays on screen.+ const unplaced = [...anchored.entries()].filter(([rowId]) => !byRowId.has(rowId)).flatMap(([, notices]) => notices)- return [...stored, ...refreshedTrailing, ...previousTrailing]+ return [...stored, ...refreshedTrailing, ...previousTrailing, ...unplaced]}export function graftRefreshedTailOntoBackfill(refreshedTail: ChatMessage[], previous: ChatMessage[]): ChatMessage[] {diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.local-rows.test.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.local-rows.test.tsindex a2f76a9f351..305b6daa0d2 100644--- a/apps/desktop/src/app/chat/transcript-receipt-turns.local-rows.test.ts+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.local-rows.test.ts@@ -38,6 +38,15 @@ const tool = (id: number, call: string): SessionMessage =>const answer = (id: number, text: string): SessionMessage =>({ id, role: 'assistant', content: text, timestamp: at(id) }) as SessionMessage+const processComplete = (id: number): SessionMessage =>+ ({+ id,+ role: 'user',+ content: 'Background Process Finished',+ timestamp: at(id),+ display_kind: 'process_complete'+ }) as SessionMessage+const olderTurn = (first: number, tag: string): SessionMessage[] => [user(first, `${tag} prompt`),narration(first + 1, `${tag} look one`, `${tag}-1`),@@ -116,4 +125,42 @@ describe('local notices inside a steered turn', () => {const side = merged.findIndex(message => message.id === 'btw-complete-btw_1')expect(side).toBeGreaterThan(merged.findIndex(message => chatMessageText(message) === 'steered prompt'))})++ it('stay inside their own turn when a later turn whose prompt only the page holds follows it', () => {+ // The background process the turn started finished after it and ran its+ // own turn; the window drew that turn's reply but never its stored prompt.+ const auto: PersistedTurn = { row_ids: [18, 19], complete: true, user_row_ids: [18], submission_id: 'auto' }++ const window = [+ ...toChatMessages([...olderTurn(1, 'old-a'), ...olderTurn(7, 'old-b')]),+ live('user-sub-steer', 'user', 'steered prompt', { rowId: 13 }),+ live('assistant-stream-1', 'assistant', 'live look', { interim: false }),+ ...notices,+ live('user-other', 'user', 'change course', { rowId: 16 }),+ live('assistant-stream-2', 'assistant', 'steered answer', { rowId: 17, persistedTurn: receipt }),+ live('assistant-auto', 'assistant', 'auto reply', { rowId: 19, persistedTurn: auto })+ ]++ const page = toChatMessages([+ ...olderTurn(1, 'old-a').slice(3),+ ...olderTurn(7, 'old-b'),+ ...turnRows,+ user(16, 'change course'),+ answer(17, 'steered answer'),+ processComplete(18),+ answer(19, 'auto reply')+ ])++ const merged = refresh(page, window)+ const indexOf = (text: string) => merged.findIndex(message => chatMessageText(message).includes(text))++ for (const text of ['SIDE ANSWER', 'answering from a conversation snapshot', 'auto reply', 'change course']) {+ expect(count(merged, text), text).toBe(1)+ }++ // In front of the steer, as drawn live: never after the next turn's prompt.+ expect(indexOf('SIDE ANSWER')).toBeGreaterThan(indexOf('steered prompt'))+ expect(indexOf('SIDE ANSWER')).toBeLessThan(indexOf('change course'))+ expect(indexOf('Switched to fallback model')).toBeLessThan(indexOf('change course'))+ })})diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.tsindex 4fbeefcb2ca..a3d6d8ee12d 100644--- a/apps/desktop/src/app/chat/transcript-receipt-turns.ts+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.ts@@ -106,23 +106,63 @@ function receiptTurnIndices(window: ChatMessage[], end: number, pageRows: Set<nureturn windowTurnIndices(window, end, bounds.first, bounds.last, bounds.receipt)}+/** The window without the turns the page holds whole, and where their notices go. */+export interface ReceiptTurnDrop {+ window: ChatMessage[]+ /**+ * A local notice drawn inside a dropped turn, by message id, mapped to the+ * stored id of the next dropped row of that turn (a steer prompt, the final+ * answer). The page carries that row, so the notice goes in front of the+ * page's copy and stays inside its own turn. Anchored to the next row the+ * window still holds instead, it slid below a later turn whose prompt only+ * the page carries (a background-process completion turn).+ */+ noticeAnchors: Map<string, number>+}++/** Stored id of the first dropped row after `at` that has one. */+function nextDroppedRowId(window: ChatMessage[], indices: number[], at: number): number | undefined {+ for (const index of indices) {+ const ids = index > at ? transcriptRowIds(window[index]) : []++ if (ids.length) {+ return ids[0]+ }+ }++ return undefined+}+/** Drop the window rows whose turn the page holds whole, by the turn's receipt. */-export function dropReceiptTurnsThePageHolds(window: ChatMessage[], page: ChatMessage[]): ChatMessage[] {+export function dropReceiptTurnsThePageHolds(window: ChatMessage[], page: ChatMessage[]): ReceiptTurnDrop {const pageRows = new Set(page.flatMap(transcriptRowIds))+ const noticeAnchors = new Map<string, number>()if (!pageRows.size) {- return window+ return { window, noticeAnchors }}const dropped = new Set<number>()window.forEach((message, index) => {- if (message.persistedTurn && !message.pending) {- receiptTurnIndices(window, index, pageRows).forEach(at => dropped.add(at))+ if (!message.persistedTurn || message.pending) {+ return+ }++ const indices = receiptTurnIndices(window, index, pageRows)++ indices.forEach(at => dropped.add(at))++ for (let at = indices[0] ?? index; at < index; at += 1) {+ const anchor = isLocalSystemNotice(window[at]) ? nextDroppedRowId(window, indices, at) : undefined++ if (anchor !== undefined) {+ noticeAnchors.set(window[at].id, anchor)+ }}})- return dropped.size ? window.filter((_, index) => !dropped.has(index)) : window+ return { window: dropped.size ? window.filter((_, index) => !dropped.has(index)) : window, noticeAnchors }}/**