Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): open chats at the latest message

Josué57a38da89a76
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
index 80cd2a135..387503f4c 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
@@ -133,6 +133,82 @@ describe('useAgentChatController — characterization (D.0)', () => {
expect(result.current.activeChatId).toBe('chat-2')
})
+ it('opens a remote chat at the bottom after its empty cache hydrates', async () => {
+ vi.useFakeTimers()
+ const hydration = deferred<{
+ agent: string
+ chatId: string
+ state: 'idle'
+ turns: Array<{
+ number: number
+ user_input: string
+ response: string
+ started_at: string
+ completed_at: string
+ }>
+ }>()
+ clerum.chat.loadMessages.mockResolvedValue([])
+ clerum.rpc.loadSessionMessages.mockReturnValue(hydration.promise)
+
+ const { result } = renderController()
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(0)
+ })
+
+ const scrollContainer = document.createElement('div')
+ const anchor = document.createElement('div')
+ let scrollTop = 0
+ Object.defineProperties(scrollContainer, {
+ clientHeight: { configurable: true, value: 100 },
+ scrollHeight: { configurable: true, value: 800 },
+ scrollTop: {
+ configurable: true,
+ get: () => scrollTop,
+ set: (value: number) => {
+ scrollTop = value
+ },
+ },
+ })
+ Object.defineProperty(anchor, 'scrollIntoView', { configurable: true, value: vi.fn() })
+ scrollContainer.append(anchor)
+ document.body.append(scrollContainer)
+ result.current.chatEndRef.current = anchor
+
+ let switching!: Promise<void>
+ await act(async () => {
+ switching = result.current.switchToChat('agent-x', 'chat-remote')
+ await vi.advanceTimersByTimeAsync(200)
+ })
+ expect(clerum.rpc.loadSessionMessages).toHaveBeenCalledTimes(1)
+
+ // The initial cache scroll ran while the cache was empty. Model a reader
+ // at the top when the remote transcript subsequently renders.
+ scrollTop = 0
+ await act(async () => {
+ hydration.resolve({
+ agent: 'agent-x',
+ chatId: 'chat-remote',
+ state: 'idle',
+ turns: [
+ {
+ number: 1,
+ user_input: 'remote question',
+ response: 'remote answer',
+ started_at: '2026-05-28T10:00:00Z',
+ completed_at: '2026-05-28T10:00:05Z',
+ },
+ ],
+ })
+ await switching
+ })
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(100)
+ })
+
+ expect(scrollTop).toBe(800)
+ scrollContainer.remove()
+ })
+
it('stops delta pagination when the active chat changes during the first page', async () => {
clerum.chat.loadMessages.mockImplementation(async (_agentRef: string, chatId: string) =>
chatId === 'chat-a'
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index e0d4993c8..a817dac4e 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -581,7 +581,9 @@ export function useAgentChatController({
const { chatEndRef, scrollChatToBottom } = useChatScroll({
selectedAgent,
+ activeChatId,
chatMessages,
+ chatMessagesLoading,
agentSending,
activeChatProgress: progressByMessageId,
})
diff --git a/desktop-app/ui/src/hooks/domain/useChatScroll.ts b/desktop-app/ui/src/hooks/domain/useChatScroll.ts
index cbe080b02..b6c7a6618 100644
--- a/desktop-app/ui/src/hooks/domain/useChatScroll.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatScroll.ts
@@ -9,7 +9,9 @@ import type { AgentChatMessage, TaskProgress } from '../../uiTypes'
*/
interface UseChatScrollParams {
selectedAgent: string | null
+ activeChatId: string | null
chatMessages: AgentChatMessage[]
+ chatMessagesLoading: boolean
agentSending: boolean
/**
* Progress slice for the ACTIVE view only (the selected agent's
@@ -34,13 +36,18 @@ interface UseChatScrollParams {
*/
export function useChatScroll({
selectedAgent,
+ activeChatId,
chatMessages,
+ chatMessagesLoading,
agentSending,
activeChatProgress,
}: UseChatScrollParams) {
const chatEndRef = useRef<HTMLDivElement | null>(null)
const scrollAnimationFrameIdsRef = useRef<number[]>([])
const scrollTimeoutIdsRef = useRef<number[]>([])
+ const pendingEntryScrollChatKeyRef = useRef<string | null>(null)
+ const activeChatKey =
+ selectedAgent && activeChatId ? `${selectedAgent}\u0000${activeChatId}` : null
const cancelScheduledScrolls = useCallback(() => {
if (typeof window !== 'undefined') {
@@ -144,6 +151,28 @@ export function useChatScroll({
scrollChatToBottom,
])
+ // A newly selected chat must open at its latest message even when its local
+ // cache was empty and the remote transcript arrives after the first scroll.
+ // Keep the entry scroll pending until at least one message is rendered; later
+ // updates still follow the near-bottom guard above.
+ useEffect(() => {
+ pendingEntryScrollChatKeyRef.current = activeChatKey
+ }, [activeChatKey])
+
+ useEffect(() => {
+ if (
+ !activeChatKey ||
+ chatMessagesLoading ||
+ chatMessages.length === 0 ||
+ pendingEntryScrollChatKeyRef.current !== activeChatKey
+ ) {
+ return
+ }
+
+ scrollChatToBottom()
+ pendingEntryScrollChatKeyRef.current = null
+ }, [activeChatKey, chatMessages, chatMessagesLoading, scrollChatToBottom])
+
useEffect(() => cancelScheduledScrolls, [cancelScheduledScrolls])
return { chatEndRef, scrollChatToBottom }