Skip to content

The agent that grows with you

fix(desktop): coalesce duplicate branch creates

Ahmett101c5e2a32a38f8
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 aaa6c312e34..b7ee1e52020 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
@@ -118,6 +118,7 @@ const RUNTIME_SESSION_ID = 'rt-new-001'
type HarnessHandle = Pick<
ReturnType<typeof useSessionActions>,
| 'archiveSession'
+ | 'branchStoredSession'
| 'createBackendSessionForSend'
| 'openNewSessionTile'
| 'removeSession'
@@ -189,6 +190,71 @@ function Harness({
return null
}
+describe('desktop branch creation idempotency', () => {
+ afterEach(() => {
+ cleanup()
+ setSessions([])
+ vi.clearAllMocks()
+ })
+
+ it('coalesces duplicate stored-session branch attempts onto one backend child', async () => {
+ const createReady = deferred<{ session_id: string; stored_session_id: string }>()
+
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ if (method === 'session.create') {
+ return createReady.promise as never
+ }
+
+ return {} as never
+ })
+
+ let actions: HarnessHandle | null = null
+
+ setSessions([storedSession({ id: 'parent', message_count: 2, title: 'Parent' })])
+ vi.mocked(getAllSessionMessages).mockResolvedValue({
+ messages: [
+ { content: 'question', role: 'user', timestamp: 1 },
+ { content: 'answer', role: 'assistant', timestamp: 2 }
+ ],
+ session_id: 'parent'
+ } as never)
+
+ render(<Harness onReady={value => (actions = value)} requestGateway={requestGateway} />)
+ await waitFor(() => expect(actions).not.toBeNull())
+
+ let first!: Promise<boolean>
+ let second!: Promise<boolean>
+
+ act(() => {
+ first = actions!.branchStoredSession('parent')
+ second = actions!.branchStoredSession('parent')
+ })
+
+ await waitFor(() =>
+ expect(requestGateway.mock.calls.filter(([method]) => method === 'session.create')).toHaveLength(1)
+ )
+
+ await act(async () => {
+ createReady.resolve({ session_id: 'runtime-branch', stored_session_id: 'stored-branch' })
+ await expect(Promise.all([first, second])).resolves.toEqual([true, true])
+ })
+
+ expect(requestGateway.mock.calls.filter(([method]) => method === 'session.create')).toHaveLength(1)
+ expect(requestGateway).toHaveBeenCalledWith(
+ 'session.create',
+ expect.objectContaining({
+ messages: [
+ { content: 'question', role: 'user' },
+ { content: 'answer', role: 'assistant' }
+ ],
+ parent_session_id: 'parent',
+ source: 'desktop'
+ })
+ )
+ expect($sessions.get().filter(session => session.id === 'stored-branch')).toHaveLength(1)
+ })
+})
+
describe('connection-qualified session deletion', () => {
afterEach(() => {
cleanup()
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index e73ddf147f4..297e0948073 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -204,6 +204,42 @@ interface SessionActionsOptions {
// (NOT in this set) still legitimately drops to a draft.
const createdThisRun = new Set<string>()
+const branchMessagesFingerprint = (messages: BranchMessage[]): string =>
+ JSON.stringify(messages.map(({ content, role }) => [role, content]))
+
+// Identity of one branch create, so a re-entered branch action (a retried
+// renderer transition, a double right-click) rides the create already in
+// flight instead of minting a second child. The OWNER is part of the identity:
+// the same parent id served by two connections is two different sessions.
+function branchCreateKey({
+ branchCount,
+ branchMessages,
+ cwd,
+ ownerRoute,
+ parentStoredId,
+ profile,
+ sourceSessionId
+}: {
+ branchCount?: number
+ branchMessages: BranchMessage[]
+ cwd?: string
+ ownerRoute?: SessionOwnerRoute
+ parentStoredId: null | string
+ profile?: null | string
+ sourceSessionId: null | string
+}): string {
+ return JSON.stringify({
+ branchCount: branchCount ?? null,
+ connectionId: ownerRoute?.connectionId || null,
+ cwd: cwd?.trim() || null,
+ messages: sourceSessionId ? null : branchMessagesFingerprint(branchMessages),
+ ownerProfile: ownerRoute?.profile || null,
+ parentStoredId,
+ profile: profile?.trim() || null,
+ sourceSessionId
+ })
+}
+
// Reflect a stored row's persisted token counts into the live usage atom
// (total is derived, so callers can't drift it out of sync with input/output).
function applyStoredUsage(stored: { input_tokens?: number | null; output_tokens?: number | null }) {
@@ -343,6 +379,7 @@ export function useSessionActions({
const { t } = useI18n()
const copy = t.desktop
const resumeRequestRef = useRef(0)
+ const branchCreateFlightsRef = useRef(new Map<string, Promise<SessionCreateResponse>>())
// Follow auto-compression's stored-id rotation only while the exact runtime,
// selection, and route intent still belong to the rotating conversation.
@@ -1991,20 +2028,47 @@ export function useSessionActions({
? requestGatewayForAgent<T>(ownerRoute.connectionId, ownerRoute.profile, method, params)
: requestGateway<T>(method, params)
+ // The owner is part of the identity: the same parent id on two
+ // connections is two different sessions, so a route-blind key would
+ // coalesce them onto one create.
+ const createKey = branchCreateKey({
+ branchCount,
+ branchMessages,
+ cwd,
+ ownerRoute,
+ parentStoredId,
+ profile,
+ sourceSessionId
+ })
+
+ let createFlight = branchCreateFlightsRef.current.get(createKey)
+
// No title: the backend auto-names the branch from its parent's lineage.
- const branched = sourceSessionId
- ? await requestBranchGateway<SessionCreateResponse>('session.branch', {
- session_id: sourceSessionId,
- ...(branchCount !== undefined ? { count: branchCount } : {})
- })
- : await requestBranchGateway<SessionCreateResponse>('session.create', {
- cols: 96,
- source: 'desktop',
- ...(cwd && { cwd }),
- ...(profile ? { profile } : {}),
- messages: branchMessages.map(({ content, role }) => ({ content, role })),
- ...(parentStoredId && { parent_session_id: parentStoredId })
- })
+ if (!createFlight) {
+ createFlight = (
+ sourceSessionId
+ ? requestBranchGateway<SessionCreateResponse>('session.branch', {
+ session_id: sourceSessionId,
+ ...(branchCount !== undefined ? { count: branchCount } : {})
+ })
+ : requestBranchGateway<SessionCreateResponse>('session.create', {
+ cols: 96,
+ source: 'desktop',
+ ...(cwd && { cwd }),
+ ...(profile ? { profile } : {}),
+ messages: branchMessages.map(({ content, role }) => ({ content, role })),
+ ...(parentStoredId && { parent_session_id: parentStoredId })
+ })
+ ).catch(err => {
+ // Drop the flight so a genuine retry re-issues the create; a
+ // resolved flight is cleared once the child is fully published.
+ branchCreateFlightsRef.current.delete(createKey)
+ throw err
+ })
+ branchCreateFlightsRef.current.set(createKey, createFlight)
+ }
+
+ const branched = await createFlight
const responseBranchMessages =
sourceSessionId && branched.messages?.length ? toBranchMessages(toChatMessages(branched.messages)) : []
@@ -2097,6 +2161,7 @@ export function useSessionActions({
revealTreePane(`session-tile:${routedSessionId}`)
}
+ branchCreateFlightsRef.current.delete(createKey)
broadcastSessionsChanged()
return true