Skip to content

The agent that grows with you

fix(desktop): the receipt drop keeps local notices drawn inside the turn

Teknium0a5032d660ab
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.ts
new file mode 100644
index 00000000000..ea916a0a3ef
--- /dev/null
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.local-rows.test.ts
@@ -0,0 +1,114 @@
+import type { PersistedTurn } from '@hermes/shared'
+import { describe, expect, it } from 'vitest'
+
+import { graftRefreshedTailOntoBackfill } from '@/app/chat/transcript-backfill'
+import { preserveLocalPendingTurnMessages } from '@/app/session/hooks/use-session-actions/utils'
+import {
+ type ChatMessage,
+ chatMessageText,
+ preserveLocalAssistantErrors,
+ preserveLocalSystemNotices,
+ toChatMessages
+} from '@/lib/chat-messages'
+import type { SessionMessage } from '@/types/hermes'
+
+/**
+ * Rows the renderer draws and never stores (a /btw echo and its answer, a
+ * fallback-switch notice) must survive the refresh that settles the turn they
+ * landed in. A steer puts later rows of the same turn below them.
+ */
+
+const at = (id: number) => 1_000 + id
+
+const user = (id: number, text: string): SessionMessage =>
+ ({ id, role: 'user', content: text, timestamp: at(id) }) as SessionMessage
+
+const narration = (id: number, text: string, call: string): SessionMessage =>
+ ({
+ id,
+ role: 'assistant',
+ content: text,
+ timestamp: at(id),
+ tool_calls: [{ id: call, type: 'function', function: { name: 'terminal', arguments: '{"command":"echo"}' } }]
+ }) as SessionMessage
+
+const tool = (id: number, call: string): SessionMessage =>
+ ({ id, role: 'tool', content: 'ok', tool_call_id: call, tool_name: 'terminal', timestamp: at(id) }) as SessionMessage
+
+const answer = (id: number, text: string): SessionMessage =>
+ ({ id, role: 'assistant', content: text, timestamp: at(id) }) as SessionMessage
+
+const olderTurn = (first: number, tag: string): SessionMessage[] => [
+ user(first, `${tag} prompt`),
+ narration(first + 1, `${tag} look one`, `${tag}-1`),
+ tool(first + 2, `${tag}-1`),
+ narration(first + 3, `${tag} look two`, `${tag}-2`),
+ tool(first + 4, `${tag}-2`),
+ answer(first + 5, `${tag} answer`)
+]
+
+const live = (id: string, role: ChatMessage['role'], text: string, extra: Partial<ChatMessage> = {}): ChatMessage => ({
+ id,
+ role,
+ parts: [{ type: 'text', text }],
+ ...extra
+})
+
+const count = (messages: ChatMessage[], needle: string) =>
+ messages.map(message => chatMessageText(message)).join('\n').split(needle).length - 1
+
+/** The refresh composition the post-turn and sessions.changed reads apply. */
+const refresh = (page: ChatMessage[], window: ChatMessage[]) =>
+ preserveLocalSystemNotices(
+ preserveLocalAssistantErrors(
+ preserveLocalPendingTurnMessages(graftRefreshedTailOntoBackfill(page, window), window),
+ window
+ ),
+ window
+ )
+
+describe('local notices inside a steered turn', () => {
+ const receipt: PersistedTurn = {
+ row_ids: [13, 14, 15, 16, 17],
+ complete: false,
+ user_row_ids: [13, 16],
+ submission_id: 'sub-steer'
+ }
+
+ const notices = [
+ live('system-1-btw', 'system', 'btw btw_1 — answering from a conversation snapshot'),
+ live('btw-complete-btw_1', 'system', '[btw "side?" (btw_1)]\nSIDE ANSWER'),
+ live('fallback-switch-1', 'system', 'Switched to fallback model')
+ ]
+
+ const turnRows = [user(13, 'steered prompt'), narration(14, 'live look', 'live-1'), tool(15, 'live-1')]
+
+ it.each([
+ // Long chat: the newest page starts inside an older turn (stored-id merge).
+ ['a page that starts inside an older turn', [...olderTurn(1, 'old-a').slice(3), ...olderTurn(7, 'old-b')]]
+ ])('keep their place and the turn draws once after %s', (_label, pageHead) => {
+ 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 })
+ ]
+
+ const page = toChatMessages([...pageHead, ...turnRows, user(16, 'change course'), answer(17, 'steered answer')])
+ const merged = refresh(page, window)
+
+ for (const text of ['SIDE ANSWER', 'answering from a conversation snapshot', 'Switched to fallback model']) {
+ expect(count(merged, text), text).toBe(1)
+ }
+
+ for (const text of ['steered prompt', 'live look', 'change course', 'steered answer', 'old-b look two']) {
+ expect(count(merged, text), text).toBe(1)
+ }
+
+ // Arrival order: after the turn's prompt, before anything newer than the turn.
+ const side = merged.findIndex(message => message.id === 'btw-complete-btw_1')
+ expect(side).toBeGreaterThan(merged.findIndex(message => chatMessageText(message) === 'steered prompt'))
+ })
+})
diff --git a/apps/desktop/src/app/chat/transcript-receipt-turns.ts b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
index f431f26f8f6..ad73dbcc72a 100644
--- a/apps/desktop/src/app/chat/transcript-receipt-turns.ts
+++ b/apps/desktop/src/app/chat/transcript-receipt-turns.ts
@@ -16,13 +16,15 @@
* The page is one contiguous run of the newest stored rows, so a page that
* holds the turn's first row and reaches its last row holds the whole turn.
* The window's rows from that prompt to the receipt bubble are then the live
- * copy of rows the page carries, and only the page's copy is kept.
+ * copy of rows the page carries, and only the page's copy is kept. A local
+ * notice drawn between them (see `isLocalSystemNotice`) is no row of the turn
+ * and is never dropped.
*/
import type { PersistedTurn } from '@hermes/shared'
import { transcriptRowIds } from '@/app/session/hooks/use-session-actions/pending-turn-identity'
-import type { ChatMessage } from '@/lib/chat-messages'
+import { type ChatMessage, isLocalSystemNotice } from '@/lib/chat-messages'
const isRowId = (value: unknown): value is number =>
typeof value === 'number' && Number.isSafeInteger(value) && value > 0
@@ -80,6 +82,12 @@ function receiptTurnIndices(window: ChatMessage[], end: number, pageRows: Set<nu
const indices: number[] = []
for (let index = start; index <= end; index += 1) {
+ // A notice this window drew mid-turn (a /btw answer, a fallback switch) is
+ // not a row of the turn: the page cannot carry it, so it stays.
+ if (isLocalSystemNotice(window[index])) {
+ continue
+ }
+
const ids = transcriptRowIds(window[index])
// A row that carries a stored id outside the turn is not this turn's copy:
diff --git a/apps/desktop/src/lib/chat-messages/index.ts b/apps/desktop/src/lib/chat-messages/index.ts
index efde87bcd2a..d0b90e69989 100644
--- a/apps/desktop/src/lib/chat-messages/index.ts
+++ b/apps/desktop/src/lib/chat-messages/index.ts
@@ -21,6 +21,7 @@ export {
export type { UnspokenTurnSpeech } from './parts'
export {
branchGroupForUser,
+ isLocalSystemNotice,
preserveLocalAssistantErrors,
preserveLocalSystemNotices,
spliceOlderPreservedRows
diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.ts b/apps/desktop/src/lib/chat-messages/reconciliation.ts
index 852db9b148d..f3afb6c7c38 100644
--- a/apps/desktop/src/lib/chat-messages/reconciliation.ts
+++ b/apps/desktop/src/lib/chat-messages/reconciliation.ts
@@ -592,6 +592,17 @@ export function preserveLocalAssistantErrors(
return insertPreservedErrorRuns(merged, currentMessages, preserveIds)
}
+/**
+ * A transcript line only this window drew, never stored: every renderer-made
+ * `system` row (slash output and the /btw and /bg echoes, the btw.complete and
+ * background.complete answers, fallback-switch, deferred-compress,
+ * approval-timeout and self-improvement review notices). A stored row owns a
+ * `rowId`, so no page read can bring one of these back.
+ */
+export function isLocalSystemNotice(message: ChatMessage): boolean {
+ return message.role === 'system' && message.rowId === undefined
+}
+
/**
* Re-graft trailing client-local `system` notices (the fallback-switch notice
* from status.update): refreshes rebuild from stored rows, which never carry
@@ -608,7 +619,7 @@ export function preserveLocalSystemNotices(nextMessages: ChatMessage[], currentM
break
}
- if (message.rowId === undefined) {
+ if (isLocalSystemNotice(message)) {
trailing.unshift(message)
}
}