Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.
fix(web): keep Room Message presentations stable across new Messages
jeffrey4901e56b6e43
diff --git a/apps/web/src/features/rooms/chat/direct-room-message.test.ts b/apps/web/src/features/rooms/chat/direct-room-message.test.tsindex c10c05aa..ec7b657b 100644--- a/apps/web/src/features/rooms/chat/direct-room-message.test.ts+++ b/apps/web/src/features/rooms/chat/direct-room-message.test.ts@@ -4,6 +4,7 @@ import type { TeamHubMessage } from "@/features/teamhub/teamhub-types";import {composeDirectRoomMessage,+ createDirectRoomMessagePresenter,presentDirectRoomMessage,} from "./direct-room-message";@@ -53,6 +54,15 @@ describe("Direct Room Message compatibility", () => {).toBe("@Research Agent Review this");});+ it("presents an unchanged Human Message as the same object", () => {+ const message = directMessage();+ const present = createDirectRoomMessagePresenter(peer);+ const presented = present(message);+ expect(presented).not.toBe(message);+ expect(present(message)).toBe(presented);+ expect(present({ ...message })).not.toBe(presented);+ });+it("hides a leading peer Mention that TeamHub separates with a newline", () => {const presented = presentDirectRoomMessage({diff --git a/apps/web/src/features/rooms/chat/direct-room-message.ts b/apps/web/src/features/rooms/chat/direct-room-message.tsindex 966d7d4c..e164183a 100644--- a/apps/web/src/features/rooms/chat/direct-room-message.ts+++ b/apps/web/src/features/rooms/chat/direct-room-message.ts@@ -54,6 +54,22 @@ export function presentDirectRoomMessage(return { ...message, raw_message, segments };}+/**+ * Presents each Message object once for one peer, so an unchanged Message keeps+ * its presented identity (and its memoized row) when other Messages arrive.+ */+export function createDirectRoomMessagePresenter(peer?: TeamHubActorRef) {+ const presented = new WeakMap<TeamHubMessage, TeamHubMessage>();+ return (message: TeamHubMessage) => {+ let result = presented.get(message);+ if (!result) {+ result = presentDirectRoomMessage(message, peer);+ presented.set(message, result);+ }+ return result;+ };+}+function dropLeadingWhitespace(segments: TeamHubMessageSegment[],): TeamHubMessageSegment[] {diff --git a/apps/web/src/features/rooms/chat/message-row.tsx b/apps/web/src/features/rooms/chat/message-row.tsxindex 536c3b78..a4953ad0 100644--- a/apps/web/src/features/rooms/chat/message-row.tsx+++ b/apps/web/src/features/rooms/chat/message-row.tsx@@ -372,7 +372,9 @@ function ConversationMessageRow({// outside it follows below.const reactionsOnBubble = bubbled;- const renderText = (block: MessageTextBlock) => (+ // Keyed by position: an edited or streamed body updates in place instead+ // of remounting its Markdown, and equal bodies never collide.+ const renderText = (block: MessageTextBlock, index: number) => (<divclassName={cn("message-row-body text-app-ink",@@ -380,7 +382,7 @@ function ConversationMessageRow({// card and a person's own words only pop.entering && isAgent && !card && "message-text-sweep",)}- key={`text:${block.body}`}+ key={`text:${index}`}><MessageMarkdownauthorKind={message.author.kind}@@ -533,8 +535,10 @@ function ConversationMessageRow({const profileBlocks = bodyBlocks.filter((block) => block.kind !== "document_card" && block.kind !== "file",);- const renderProfileBlock = (block: (typeof bodyBlocks)[number]) =>- block.kind === "text" ? renderText(block) : renderBlock(block);+ const renderProfileBlock = (+ block: (typeof bodyBlocks)[number],+ index: number,+ ) => (block.kind === "text" ? renderText(block, index) : renderBlock(block));return (<article@@ -639,8 +643,11 @@ function ConversationMessageRow({profileParts? profileBlocks.map((block, index) =>index === 0 && block.kind === "text"- ? renderText({ ...block, body: profileParts[2]! })- : renderProfileBlock(block),+ ? renderText(+ { ...block, body: profileParts[2]! },+ index,+ )+ : renderProfileBlock(block, index),): undefined}diff --git a/apps/web/src/features/rooms/chat/room-message-presentations.test.tsx b/apps/web/src/features/rooms/chat/room-message-presentations.test.tsxnew file mode 100644index 00000000..3f482b8a--- /dev/null+++ b/apps/web/src/features/rooms/chat/room-message-presentations.test.tsx@@ -0,0 +1,108 @@+import { render, renderHook } from "@testing-library/react";+import { describe, expect, it } from "vitest";++import type {+ TeamHubActorRef,+ TeamHubMessage,+} from "@/features/teamhub/teamhub-types";++import { MessageRow } from "./message-row";+import { useRoomMessagePresentations } from "./room-message-presentations";++const nova: TeamHubActorRef = {+ display_name: "Nova",+ id: "actor-nova",+ kind: "agent",+ tenant_id: "tenant-a",+};++function message(id: string, body: string): TeamHubMessage {+ return {+ attachments: [+ {+ available: true,+ document_id: `document-${id}`,+ filename: `${id}.pdf`,+ id: `attachment-${id}`,+ media_type: "application/pdf",+ size_bytes: 10,+ },+ ] as TeamHubMessage["attachments"],+ author: nova,+ created_at: "2026-11-11T10:00:00Z",+ id,+ message_kind: "conversation",+ raw_message: body,+ room_id: "room-a",+ segments: [+ {+ content: "@Nova",+ target: nova.id,+ target_type: "agent",+ type: "reference",+ },+ { content: ` ${body}`, type: "text" },+ ] as TeamHubMessage["segments"],+ tenant_id: "tenant-a",+ };+}++describe("Room Message presentations", () => {+ it("keeps each unchanged Message's presentation when another arrives", () => {+ const first = message("message-1", "first");+ const second = message("message-2", "second");+ const props = {+ deliveryPresentations: {},+ mentionOptions: [nova],+ messages: [first],+ roomMessagePresentations: {},+ };+ const { rerender, result } = renderHook(useRoomMessagePresentations, {+ initialProps: props,+ });+ const before = result.current["message-1"];+ expect(before?.mentions).toEqual([nova]);+ expect(before?.attachments).toHaveLength(1);++ rerender({ ...props, messages: [first, second] });++ expect(result.current["message-1"]).toBe(before);+ expect(result.current["message-2"]?.attachments[0]?.filename).toBe(+ "message-2.pdf",+ );+ });++ it("updates a streamed Message body in place instead of remounting it", () => {+ const rowProps = {+ actorDisplayNames: {},+ documentMetadata: {},+ documentTitles: {},+ onCardDecision: () => undefined,+ onOpenArtifact: () => undefined,+ onOpenMessageFile: () => undefined,+ onOpenProfile: () => undefined,+ tenantId: "tenant-a",+ workspaceAgentIds: new Set<string>(),+ };+ const streaming = { ...message("message-1", "Hello"), attachments: [] };+ const { container, rerender } = render(+ <MessageRow {...rowProps} message={streaming} />,+ );+ const body = container.querySelector(".message-row-body");+ expect(body).toHaveTextContent("Hello");++ rerender(+ <MessageRow+ {...rowProps}+ message={{+ ...streaming,+ raw_message: "Hello world",+ segments: [{ content: "Hello world", type: "text" }],+ }}+ />,+ );++ expect(container.querySelector(".message-row-body")).toBe(body);+ expect(body).toHaveTextContent("Hello world");+ });+});diff --git a/apps/web/src/features/rooms/chat/room-message-presentations.ts b/apps/web/src/features/rooms/chat/room-message-presentations.tsindex a2ae3b7e..c6461b53 100644--- a/apps/web/src/features/rooms/chat/room-message-presentations.ts+++ b/apps/web/src/features/rooms/chat/room-message-presentations.ts@@ -1,3 +1,6 @@+import { replaceEqualDeep } from "@tanstack/react-query";+import { useMemo, useRef } from "react";+import {presentTeamHubMessageAttachments,type TeamHubMessagePresentation,@@ -8,19 +11,51 @@ import type {TeamHubMessage,} from "@/features/teamhub/teamhub-types";+type RoomMessagePresentations = Record<string, TeamHubMessagePresentation>;++interface RoomMessagePresentationInput {+ deliveryPresentations: Readonly<RoomMessagePresentations>;+ mentionOptions: TeamHubActorRef[];+ messages: TeamHubMessage[];+ roomMessagePresentations: Readonly<RoomMessagePresentations>;+}++/**+ * Resolves presentations and keeps every entry whose content did not change+ * from the previous result, so memoized rows skip unrelated Messages.+ */+export function useRoomMessagePresentations({+ deliveryPresentations,+ mentionOptions,+ messages,+ roomMessagePresentations,+}: RoomMessagePresentationInput) {+ const previous = useRef<RoomMessagePresentations | undefined>(undefined);+ return useMemo(() => {+ previous.current = replaceEqualDeep(+ previous.current,+ resolveRoomMessagePresentations({+ deliveryPresentations,+ mentionOptions,+ messages,+ roomMessagePresentations,+ }),+ );+ return previous.current;+ }, [+ deliveryPresentations,+ mentionOptions,+ messages,+ roomMessagePresentations,+ ]);+}+export function resolveRoomMessagePresentations({deliveryPresentations,mentionOptions,messages,roomMessagePresentations,-}: {- deliveryPresentations: Readonly<Record<string, TeamHubMessagePresentation>>;- mentionOptions: TeamHubActorRef[];- messages: TeamHubMessage[];- roomMessagePresentations: Readonly<- Record<string, TeamHubMessagePresentation>- >;-}) {+}: RoomMessagePresentationInput): RoomMessagePresentations {const presentations = {...deliveryPresentations,...roomMessagePresentations,@@ -29,14 +64,12 @@ export function resolveRoomMessagePresentations({for (const message of messages) {const deliveryPresentation = deliveryPresentations[message.id];const roomPresentation = roomMessagePresentations[message.id];- const existing = presentations[message.id];const mentions = roomPresentation?.mentions.length? roomPresentation.mentions: deliveryPresentation?.mentions.length? deliveryPresentation.mentions: referencedMessageActors(message.segments, mentionOptions);presentations[message.id] = {- ...existing,attachments: firstPresentedAttachments(roomPresentation?.attachments,deliveryPresentation?.attachments,@@ -53,7 +86,7 @@ function firstPresentedAttachments(...candidates: Array<TeamHubMessagePresentation["attachments"] | undefined>): TeamHubMessagePresentation["attachments"] {for (const candidate of candidates) {- if (candidate?.length) return [...candidate];+ if (candidate?.length) return candidate;}return [];}diff --git a/apps/web/src/features/rooms/chat/thread-panel.tsx b/apps/web/src/features/rooms/chat/thread-panel.tsxindex a5791439..667b5120 100644--- a/apps/web/src/features/rooms/chat/thread-panel.tsx+++ b/apps/web/src/features/rooms/chat/thread-panel.tsx@@ -34,7 +34,7 @@ import {type MessageComposition,} from "./message-composer";import {- presentDirectRoomMessage,+ createDirectRoomMessagePresenter,withDirectRoomWireBody,} from "./direct-room-message";import { isTimelineMessage } from "./message-grouping";@@ -44,7 +44,7 @@ import {type PendingRoomMessage,} from "./message-row";import { AgentStreamingStatusDock } from "./message-timeline";-import { resolveRoomMessagePresentations } from "./room-message-presentations";+import { useRoomMessagePresentations } from "./room-message-presentations";import {createRoomProjection,replaceLoadedRoomMessage,@@ -197,37 +197,32 @@ function ThreadPanelContent({}, [composerFocusRequested, threadRoomId]);const replies = useMemo(() => messages.filter(isTimelineMessage), [messages]);+ const present = useMemo(+ () => createDirectRoomMessagePresenter(directPeer),+ [directPeer],+ );const presentedRoot = useMemo(- () => presentDirectRoomMessage(rootMessage, directPeer),- [directPeer, rootMessage],+ () => present(rootMessage),+ [present, rootMessage],);const presentedReplies = useMemo(- () =>- replies.map((message) => presentDirectRoomMessage(message, directPeer)),- [directPeer, replies],+ () => replies.map(present),+ [present, replies],);const threadMessages = useMemo(() => [rootMessage, ...replies],[replies, rootMessage],);- const presentations = useMemo(- () =>- resolveRoomMessagePresentations({- deliveryPresentations: conversation.delivery.presentations,- mentionOptions,- messages: threadMessages,- roomMessagePresentations: {- [rootMessage.id]: { attachments: [], mentions: rootMentions },- },- }),- [- conversation.delivery.presentations,- mentionOptions,- rootMentions,- rootMessage.id,- threadMessages,- ],+ const rootPresentations = useMemo(+ () => ({ [rootMessage.id]: { attachments: [], mentions: rootMentions } }),+ [rootMentions, rootMessage.id],);+ const presentations = useRoomMessagePresentations({+ deliveryPresentations: conversation.delivery.presentations,+ mentionOptions,+ messages: threadMessages,+ roomMessagePresentations: rootPresentations,+ });const documentMetadata = useDocumentCardMetadata(client,tenantId,diff --git a/apps/web/src/features/rooms/chat/use-room-chat-conversation-state.ts b/apps/web/src/features/rooms/chat/use-room-chat-conversation-state.tsindex 8484bf0c..e499e31a 100644--- a/apps/web/src/features/rooms/chat/use-room-chat-conversation-state.ts+++ b/apps/web/src/features/rooms/chat/use-room-chat-conversation-state.ts@@ -15,7 +15,7 @@ import type { MessageSkillOption } from "./message-composer-references";import type { VoiceModePresentation } from "./voice-mode/voice-mode-controls";import type { VoiceLiveDraft } from "./voice-mode/voice-live-draft";import { voiceModeStatusLabel } from "./voice-mode/voice-mode-presentation";-import { presentDirectRoomMessage } from "./direct-room-message";+import { createDirectRoomMessagePresenter } from "./direct-room-message";import { useRoomMentionOptions } from "./use-room-mention-options";import {orderRoomMessages,@@ -190,24 +190,22 @@ function usePresentedRoomMessages({linkedMessage,pages,}: {- directPeer: Parameters<typeof presentDirectRoomMessage>[1];+ directPeer: Parameters<typeof createDirectRoomMessagePresenter>[0];linkedMessage?: TeamHubMessage;pages: readonly { items: TeamHubMessage[] }[];}) {const orderedMessages = useMemo(() => orderRoomMessages(pages), [pages]);+ const present = useMemo(+ () => createDirectRoomMessagePresenter(directPeer),+ [directPeer],+ );const presentedMessages = useMemo(- () =>- orderedMessages.map((message) =>- presentDirectRoomMessage(message, directPeer),- ),- [directPeer, orderedMessages],+ () => orderedMessages.map(present),+ [orderedMessages, present],);const linkedPresentedMessage = useMemo(- () =>- linkedMessage- ? presentDirectRoomMessage(linkedMessage, directPeer)- : undefined,- [directPeer, linkedMessage],+ () => (linkedMessage ? present(linkedMessage) : undefined),+ [linkedMessage, present],);const interactionMessages = useMemo(() => {if (diff --git a/apps/web/src/features/rooms/chat/use-room-chat-timeline-actions.ts b/apps/web/src/features/rooms/chat/use-room-chat-timeline-actions.tsindex 32492b47..dae5bfc6 100644--- a/apps/web/src/features/rooms/chat/use-room-chat-timeline-actions.ts+++ b/apps/web/src/features/rooms/chat/use-room-chat-timeline-actions.ts@@ -8,7 +8,7 @@ import { teamHubData } from "@/features/teamhub/teamhub-api";import { tenantRoomsQueryKey } from "../room-queries";import { preloadUnifiedDocumentPage } from "../documents/document-route-preload";import type { MessageComposition } from "./message-composer";-import { resolveRoomMessagePresentations } from "./room-message-presentations";+import { useRoomMessagePresentations } from "./room-message-presentations";import { replaceLoadedRoomMessage } from "./room-projection";import { useFocusComposerOnEntry } from "./use-focus-composer-on-entry";import { usePrefetchPanelAfterPaint } from "./use-prefetch-panel-after-paint";@@ -232,7 +232,7 @@ export type RoomChatTimelineActions = ReturnType<>;type MessagePresentationResolutionInput = Parameters<- typeof resolveRoomMessagePresentations+ typeof useRoomMessagePresentations>[0];function useResolvedRoomMessagePresentations({@@ -245,22 +245,15 @@ function useResolvedRoomMessagePresentations({linkedMessage?: TeamHubMessage;orderedMessages: TeamHubMessage[];}) {- return useMemo(+ const messages = useMemo(() =>- resolveRoomMessagePresentations({- deliveryPresentations,- mentionOptions,- messages: linkedMessage- ? [...orderedMessages, linkedMessage]- : orderedMessages,- roomMessagePresentations,- }),- [- deliveryPresentations,- linkedMessage,- mentionOptions,- orderedMessages,- roomMessagePresentations,- ],+ linkedMessage ? [...orderedMessages, linkedMessage] : orderedMessages,+ [linkedMessage, orderedMessages],);+ return useRoomMessagePresentations({+ deliveryPresentations,+ mentionOptions,+ messages,+ roomMessagePresentations,+ });}