Skip to content

The agent that grows with you

test(desktop): two connections serving one parent id are not one branch

Brooklyn Nicholson7f27ca27015f
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
index b7ee1e52020..506e6e718d8 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
@@ -253,6 +253,69 @@ describe('desktop branch creation idempotency', () => {
)
expect($sessions.get().filter(session => session.id === 'stored-branch')).toHaveLength(1)
})
+
+ it('does not coalesce two same-id parents that live on different connections', async () => {
+ // Two backends each expose a session called `parent`. They are different
+ // conversations, so a route-blind flight key would collapse both branch
+ // actions onto ONE create and hand the second caller the other backend's
+ // child. Both creates are held open so the second call sees the first's
+ // flight still in the map — that is the only state the key guards.
+ const routedCreate = vi.mocked(requestGatewayForAgent)
+ const pandoraCreate = deferred<{ session_id: string; stored_session_id: string }>()
+ const otherCreate = deferred<{ session_id: string; stored_session_id: string }>()
+
+ routedCreate.mockImplementation((async (connectionId: string, _profile: string, method: string) => {
+ if (method !== 'session.create') {
+ return {} as never
+ }
+
+ return connectionId === 'pandora' ? pandoraCreate.promise : otherCreate.promise
+ }) as never)
+
+ let actions: HarnessHandle | null = null
+
+ vi.mocked(getAllSessionMessages).mockResolvedValue({
+ messages: [{ content: 'question', role: 'user', timestamp: 1 }],
+ session_id: 'parent'
+ } as never)
+
+ render(<Harness onReady={value => (actions = value)} requestGateway={vi.fn(async () => ({}) as never)} />)
+ await waitFor(() => expect(actions).not.toBeNull())
+
+ // Same stored id, one owner at a time in the row cache — the branch resolves
+ // its owner from the row, so this is how the two owners reach forkBranch.
+ setSessions([storedSession({ connection_id: 'pandora', id: 'parent', message_count: 2, profile: 'default' })])
+
+ let first!: Promise<boolean>
+ let second!: Promise<boolean>
+
+ await act(async () => {
+ first = actions!.branchStoredSession('parent')
+ await waitFor(() => expect(routedCreate).toHaveBeenCalled())
+ })
+
+ setSessions([storedSession({ connection_id: 'other-box', id: 'parent', message_count: 2, profile: 'default' })])
+
+ await act(async () => {
+ second = actions!.branchStoredSession('parent')
+ await waitFor(() =>
+ expect(routedCreate.mock.calls.filter(([, , method]) => method === 'session.create')).toHaveLength(2)
+ )
+ })
+
+ await act(async () => {
+ pandoraCreate.resolve({ session_id: 'rt-pandora', stored_session_id: 'stored-pandora' })
+ otherCreate.resolve({ session_id: 'rt-other', stored_session_id: 'stored-other-box' })
+ await expect(Promise.all([first, second])).resolves.toEqual([true, true])
+ })
+
+ const creates = routedCreate.mock.calls.filter(([, , method]) => method === 'session.create')
+
+ expect(creates.map(([connectionId]) => connectionId)).toEqual(['pandora', 'other-box'])
+ // Two distinct children, not one child claimed twice.
+ expect($sessions.get().filter(session => session.id === 'stored-pandora')).toHaveLength(1)
+ expect($sessions.get().filter(session => session.id === 'stored-other-box')).toHaveLength(1)
+ })
})
describe('connection-qualified session deletion', () => {