Skip to content

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

TypeScript

fix(desktop-ui): r12-l6 fence successful create admission by scope

Josué6b4481895824
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/createSuccessScopeWindow.r12.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/createSuccessScopeWindow.r12.test.tsx
new file mode 100644
index 000000000..52826cbfb
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/createSuccessScopeWindow.r12.test.tsx
@@ -0,0 +1,96 @@
+// @vitest-environment jsdom
+import { useLayoutEffect, useState } from 'react'
+import { afterEach, expect, it, vi } from 'vitest'
+import { AgentTaskTrackerProvider } from '@contexts/AgentTaskTrackerContext'
+import { act, cleanup, renderHook, waitFor } from '@testing-library/react'
+import type { ChatMetadata } from '../../../../../src/types'
+import { useAgentChatController } from '../useAgentChatController'
+import { deferred } from './__fixtures__/catalogFixtures'
+import { renderController } from './__fixtures__/controllerHarness'
+import { useHarnessHostAuthority } from './__fixtures__/hostAuthorityHarness'
+import { installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'
+
+afterEach(() => {
+ cleanup()
+ vi.restoreAllMocks()
+ uninstallMockClerum()
+})
+
+it('does not open or publish an old-team successful create delivered in the new render layout effect', async () => {
+ const clerum = installMockClerum()
+ await clerum.chat.create('agent-x', 'chat-a')
+ await clerum.persistLastActive('agent-x', 'chat-a')
+ clerum.chat.setLastActive.mockImplementation(clerum.persistLastActive)
+ expect((await clerum.readIndex('agent-x')).lastActiveChatId).toBe('chat-a')
+ const seed = renderController({ loadMenuData: false })
+ const { params, spies } = seed
+ seed.unmount()
+
+ const produced = deferred<ChatMetadata>()
+ const delivered = deferred<ChatMetadata>()
+ const layout = deferred<void>()
+ let metadata: ChatMetadata | null = null
+ let resolveInLayout = false
+ const app = renderHook(
+ () => {
+ const [team, setTeam] = useState('team-1')
+ const authority = useHarnessHostAuthority()
+ const controller = useAgentChatController({
+ ...params,
+ currentTeamId: team,
+ chatAuthorityTeamId: team,
+ isHostAccessBlocked: authority.isHostAccessBlocked,
+ getHostAuthorityEpoch: authority.getHostAuthorityEpoch,
+ hostAuthorityRevision: authority.revision,
+ onHostAccessRevoked: authority.onHostAccessRevoked,
+ onHostAuthorityUncertain: authority.onHostAuthorityUncertain,
+ } as Parameters<typeof useAgentChatController>[0])
+ useLayoutEffect(() => {
+ if (team === 'team-2' && resolveInLayout && metadata) {
+ delivered.resolve(metadata)
+ layout.resolve()
+ }
+ }, [team])
+ return { controller, team, setTeam }
+ },
+ { wrapper: ({ children }) => <AgentTaskTrackerProvider>{children}</AgentTaskTrackerProvider> }
+ )
+ await waitFor(() => expect(app.result.current.controller.chatListLoading).toBe(false))
+ const createProducer = clerum.chat.create.getMockImplementation()!
+ clerum.chat.create.mockImplementationOnce(async (agentRef, chatId) => {
+ const created = await createProducer(agentRef, chatId)
+ produced.resolve(created)
+ return delivered.promise
+ })
+ let request!: Promise<void>
+ act(() => {
+ request = app.result.current.controller.handleCreateChat()
+ })
+ metadata = await produced.promise
+ await clerum.chat.upsertMessages('agent-x', metadata.id, [
+ {
+ id: 'old-team-created-history',
+ role: 'user',
+ content: 'old team created history',
+ timestamp: 1,
+ },
+ ])
+ expect((await clerum.readIndex('agent-x')).chats.some(chat => chat.id === metadata?.id)).toBe(
+ true
+ )
+ resolveInLayout = true
+ // Normal-priority state commits: layout delivers real already-persisted create
+ // metadata before passive effects invalidate the old request generation.
+ app.result.current.setTeam('team-2')
+ await layout.promise
+ await expect(request).resolves.toBeUndefined()
+ await waitFor(() => expect(app.result.current.team).toBe('team-2'))
+
+ expect((await clerum.readIndex('agent-x')).lastActiveChatId).toBe('chat-a')
+ expect(clerum.chat.setLastActive).not.toHaveBeenCalled()
+ expect(app.result.current.controller.chatList.some(chat => chat.id === metadata?.id)).toBe(false)
+ expect(app.result.current.controller.activeChatId).toBeNull()
+ expect(app.result.current.controller.chatMessages).toEqual([])
+ expect(app.result.current.controller.chatMessagesLoading).toBe(false)
+ expect(spies.pushToast).not.toHaveBeenCalled()
+})
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index 44a3841c8..02fe00ee1 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -1089,15 +1089,15 @@ export function useChatListController({
}
throw error
}
- chatStore.clearCachedRemoteData()
- if (selectedAgentRef.current !== agentRef || requestGenerationRef.current !== requestGeneration)
- return
- appendNewEntry(agentRef, meta)
if (
- selectionIntentRevision !== undefined &&
- host.current?.getSelectionIntentRevision() !== selectionIntentRevision
+ selectedAgentRef.current !== agentRef ||
+ requestGenerationRef.current !== requestGeneration ||
+ currentAuthorityScopeRef.current !== authorityScopeAtCreate
)
return
+ chatStore.clearCachedRemoteData()
+ appendNewEntry(agentRef, meta)
+ if (!isCurrentCreate()) return
host.current?.markAutoSelectedChat(null)
await host.current?.switchToChat(agentRef, chatId, {
onCurrentError: error => {