The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a local notice keeps its place across a short chat's refresh
Teknium977e2530cb01
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 ea916a0a3ef..a2f76a9f351 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@@ -55,7 +55,10 @@ const live = (id: string, role: ChatMessage['role'], text: string, extra: Partia})const count = (messages: ChatMessage[], needle: string) =>- messages.map(message => chatMessageText(message)).join('\n').split(needle).length - 1+ 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[]) =>@@ -85,7 +88,9 @@ describe('local notices inside a steered turn', () => {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')]]+ ['a page that starts inside an older turn', [...olderTurn(1, 'old-a').slice(3), ...olderTurn(7, 'old-b')]],+ // Short chat: the page is the whole conversation (taken as is).+ ['a page that holds the whole chat', [...olderTurn(1, 'old-a'), ...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')]),diff --git a/apps/desktop/src/lib/chat-messages/reconciliation.ts b/apps/desktop/src/lib/chat-messages/reconciliation.tsindex f3afb6c7c38..dc54e5f11d2 100644--- a/apps/desktop/src/lib/chat-messages/reconciliation.ts+++ b/apps/desktop/src/lib/chat-messages/reconciliation.ts@@ -603,39 +603,60 @@ export function isLocalSystemNotice(message: ChatMessage): boolean {return message.role === 'system' && message.rowId === undefined}+/** Index in `messages` of the row `held` is, by id or stored row. */+function heldRowIndex(messages: ChatMessage[], held: ChatMessage): number {+ return messages.findIndex(+ message => message.id === held.id || (held.rowId !== undefined && message.rowId === held.rowId)+ )+}+/**- * Re-graft trailing client-local `system` notices (the fallback-switch notice- * from status.update): refreshes rebuild from stored rows, which never carry- * them, so the notice vanished on the next refresh (#126422). Stored rows own- * a `rowId` and are left to the page. Idempotent by id and text.+ * Re-graft client-local `system` notices (see `isLocalSystemNotice`):+ * refreshes rebuild from stored rows, which never carry them, so the notice+ * vanished on the next refresh (#126422). A notice keeps its place in front of+ * the next row the refreshed transcript still holds, so a /btw answer that+ * landed mid-turn survives the steer that puts later rows below it. Trailing+ * notices go at the end; one whose later rows the refresh no longer holds at+ * all (compaction, rewind) goes with them. Idempotent by id and text.*/export function preserveLocalSystemNotices(nextMessages: ChatMessage[], currentMessages: ChatMessage[]): ChatMessage[] {- const trailing: ChatMessage[] = []+ const nextIds = new Set(nextMessages.map(message => message.id))+ const nextTexts = new Set(nextMessages.map(message => chatMessageText(message).trim()))- for (let index = currentMessages.length - 1; index >= 0; index -= 1) {- const message = currentMessages[index]+ const missing = (message: ChatMessage) =>+ isLocalSystemNotice(message) && !nextIds.has(message.id) && !nextTexts.has(chatMessageText(message).trim())- if (message.role !== 'system') {- break- }+ if (!currentMessages.some(missing)) {+ return nextMessages+ }++ const merged = [...nextMessages]+ let pending: ChatMessage[] = []+ // Leading part of `pending` already followed by a row the refresh dropped.+ let orphaned = 0- if (isLocalSystemNotice(message)) {- trailing.unshift(message)+ for (const message of currentMessages) {+ if (missing(message)) {+ pending.push(message)++ continue}- }- if (!trailing.length) {- return nextMessages- }+ const at = pending.length ? heldRowIndex(merged, message) : -1- const nextIds = new Set(nextMessages.map(message => message.id))- const nextTexts = new Set(nextMessages.map(message => chatMessageText(message).trim()))+ if (at >= 0) {+ merged.splice(at, 0, ...pending)+ pending = []+ orphaned = 0+ } else if (message.role !== 'system') {+ orphaned = pending.length+ }+ }- const unstored = trailing.filter(- message => !nextIds.has(message.id) && !nextTexts.has(chatMessageText(message).trim())- )+ const trailing = pending.slice(orphaned)+ const preserved = trailing.length ? [...merged, ...trailing] : merged- return unstored.length ? [...nextMessages, ...unstored] : nextMessages+ return preserved.length === nextMessages.length ? nextMessages : preserved}export function branchGroupForUser(userMessage: ChatMessage): string {