Skip to content

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

TypeScript

fix(desktop): clear the pending-rename queue only on user-identity change (R1-H1)

Diego Zacariascf8a05930c6a
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx
index 21bd21665..b0ce37ce7 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx
@@ -11,7 +11,7 @@
* is never called and there is no rollback/pending behavior to assert).
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-import { act, waitFor } from '@testing-library/react'
+import { act, cleanup, waitFor } from '@testing-library/react'
import { parseSessionsListResult } from '../../../../../src/rpcProxyClient'
import type { ChatIndex } from '../../../../../src/types'
import { renderController } from './__fixtures__/controllerHarness'
@@ -26,6 +26,11 @@ beforeEach(() => {
})
afterEach(() => {
+ // Unmount every controller first: without it a prior test's mounted controller
+ // keeps its `window 'online'` listener alive, and a later test that dispatches
+ // that global event flushes the stale controller's queue too (a phantom PATCH
+ // on the shared clerum mock). This file otherwise has no auto-cleanup.
+ cleanup()
vi.restoreAllMocks()
uninstallMockClerum()
})
@@ -427,3 +432,82 @@ describe('rename pending queue — concurrency (spec 15 §2.5)', () => {
expect(spies.pushToast).toHaveBeenCalledWith(expect.any(String), 'error')
})
})
+
+describe('rename pending queue — user-identity teardown (spec 15 §2.5 / R1-H1)', () => {
+ // R1-H1 repro (fails at be421da6): sessions are per-USER server-side, so a
+ // pending rename queued by user-1 must NOT survive a switch to user-2 — a later
+ // reconnect/poll would PATCH user-1's rename under user-2's token. Assert the
+ // observable: after the identity change, no further renameSession fires.
+ it('drops the pending queue on a user-identity change — no PATCH under the new token', async () => {
+ wireStatefulIndex(clerum, [{ id: 'c1', title: 'old' }])
+ // Deterministic network failure → the rename queues offline (retried on reconnect).
+ clerum.rpc.renameSession.mockRejectedValue(new Error('fetch failed'))
+
+ const { result, rerender } = renderController({
+ selectedAgent: 'agent-x',
+ agentNames: ['agent-x'],
+ currentUserId: 'user-1',
+ })
+ await waitFor(() => expect(titleInList(result.current, 'c1')).toBe('old'))
+
+ await act(async () => {
+ await result.current.handleRenameChatForAgent('agent-x', 'c1', 'renamed')
+ })
+ // Optimistic title applied, one PATCH attempted, entry queued offline.
+ expect(titleInList(result.current, 'c1')).toBe('renamed')
+ expect(clerum.rpc.renameSession).toHaveBeenCalledTimes(1)
+
+ // The user identity changes (logout → login as a different user).
+ await act(async () => {
+ rerender({ currentUserId: 'user-2' })
+ await flushMicrotasks()
+ })
+
+ // A reconnect fires under the new identity. The obsolete entry was dropped, so
+ // NO PATCH is issued under user-2's token.
+ await act(async () => {
+ window.dispatchEvent(new Event('online'))
+ await flushMicrotasks()
+ })
+ expect(clerum.rpc.renameSession).toHaveBeenCalledTimes(1)
+ })
+
+ // Anti-regression guard (passes at be421da6, must keep passing after R1-H1):
+ // a TEAM-switch of the SAME user must PRESERVE the queue and keep syncing —
+ // clearing it here would let the server title revert the user's own rename
+ // (resolveSessionTitle case C).
+ it('preserves the pending queue across a team-switch of the same user and keeps syncing', async () => {
+ wireStatefulIndex(clerum, [{ id: 'c1', title: 'old' }])
+ // Server reports a DIFFERENT title — it must never win while a rename is pending.
+ clerum.rpc.listSessions.mockResolvedValue(reportedSessions([{ chatId: 'c1', title: 'server' }]))
+ // First attempt fails offline; the retry after the team-switch succeeds.
+ clerum.rpc.renameSession
+ .mockRejectedValueOnce(new Error('fetch failed'))
+ .mockResolvedValue({ title: 'renamed' })
+
+ const { result, rerender } = renderController({
+ selectedAgent: 'agent-x',
+ agentNames: ['agent-x'],
+ currentUserId: 'user-1',
+ currentTeamId: 'team-1',
+ })
+ await waitFor(() => expect(titleInList(result.current, 'c1')).toBe('server'))
+
+ await act(async () => {
+ await result.current.handleRenameChatForAgent('agent-x', 'c1', 'renamed')
+ })
+ expect(titleInList(result.current, 'c1')).toBe('renamed')
+ expect(clerum.rpc.renameSession).toHaveBeenCalledTimes(1)
+
+ // Same user, different team: the queue survives and the offline entry retries.
+ await act(async () => {
+ rerender({ currentTeamId: 'team-2' })
+ await flushMicrotasks()
+ })
+ await waitFor(() => expect(clerum.rpc.renameSession).toHaveBeenCalledTimes(2))
+
+ // The user's optimistic rename survived the team-switch — the server title did
+ // NOT overwrite it.
+ expect(titleInList(result.current, 'c1')).toBe('renamed')
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index cd92b70b0..0c1464618 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -386,6 +386,9 @@ export function useAgentChatController({
agentNames,
isAuthenticated,
scopeKey: authenticatedScope,
+ // User portion of the scope only (NOT the combined user:team scopeKey): the
+ // pending-rename queue is per-user and must survive a team-switch (R1-H1).
+ authUserKey: currentUserId ?? 'unknown-user',
loadMenuData,
chatStore,
fsm,
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index d17b76a41..8617a8bfb 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -75,6 +75,14 @@ interface UseChatListControllerParams {
agentNames: string[]
isAuthenticated: boolean
scopeKey: string
+ /**
+ * The USER portion of the scope (`currentUserId ?? 'unknown-user'`), passed
+ * explicitly rather than split from `scopeKey` (which is `${userId}:${teamId}`).
+ * The pending-rename queue is per-user (sessions are keyed by userId server-side),
+ * so it must be dropped when the user identity changes but PRESERVED across a
+ * team-switch of the same user — see the teardown effect below.
+ */
+ authUserKey: string
loadMenuData: boolean
chatStore: ReturnType<typeof useChatStore>
fsm: SessionFsmStore
@@ -136,6 +144,7 @@ export function useChatListController({
agentNames,
isAuthenticated,
scopeKey,
+ authUserKey,
loadMenuData,
chatStore,
fsm,
@@ -200,6 +209,23 @@ export function useChatListController({
requestGenerationRef.current += 1
}, [isAuthenticated, scopeKey])
+ // R1-H1 — the pending-rename queue is per-USER identity. mcp-host keys a chat
+ // session by userId (`${userSub}:rpc:${agent}:${chatId}`) and filters by
+ // `conversation.user_id === userId`, so a queued rename stays legitimate across
+ // a TEAM-switch of the same user and must keep syncing. Only a change of user
+ // identity (logout, or login as someone else) makes the queued renames belong to
+ // a foreign identity — drop them so a later flush/poll/online never PATCHes them
+ // under the new user's token. Keyed on the user portion ONLY, never the combined
+ // `scopeKey`: clearing on a team-switch would strip the pending marker and let
+ // the server title overwrite the user's own optimistic rename on the next poll
+ // (resolveSessionTitle case C). This hook is never unmounted on logout/team-switch
+ // (it lives at App's root), so this effect is the queue's only identity teardown.
+ // A PATCH still in flight when this runs no-ops on completion without re-inserting
+ // (attemptRenameRpc's `pendingRenamesRef.current.get(key) !== entry` guard).
+ useEffect(() => {
+ pendingRenamesRef.current.clear()
+ }, [authUserKey])
+
// Live `selectedAgent` for the stable callbacks below (they gate a chatList
// write on "is this the selected agent"). A ref keeps the callbacks stable
// while always reading the committed value at call time.