The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): rename a branched-draft session before its first turn (#70317)
SHL0MS8c30ef318d1e
diff --git a/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.ts b/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.tsindex 259cae81cc0..ae7ad776d7b 100644--- a/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.ts+++ b/apps/desktop/src/app/chat/sidebar/session-actions-menu.test.ts@@ -2,6 +2,7 @@ import { atom } from 'nanostores'import { afterEach, describe, expect, it, vi } from 'vitest'import { $activeSessionId, $selectedStoredSessionId } from '@/store/session'+import { $sessionStates, $sessionTiles } from '@/store/session-states'import { renameSessionPreferringRpc } from './session-actions-menu'@@ -53,6 +54,8 @@ afterEach(() => {activeGateway.mockReturnValue({ request })$activeSessionId.set(null)$selectedStoredSessionId.set(null)+ $sessionTiles.set([])+ $sessionStates.set({})})describe('renameSessionPreferringRpc', () => {@@ -89,6 +92,44 @@ describe('renameSessionPreferringRpc', () => {expect(renameSession).toHaveBeenCalledWith(STORED_ID, 'My branch', 'work')})+ it('renames a branched-draft TILE via RPC even when it is not the selected row (#70317)', async () => {+ // Branch opens as its own tab: NOT the selected primary row, and no DB row+ // yet — but the tile carries the bound runtime id. REST would 404 here.+ $selectedStoredSessionId.set('some-other-active-session')+ $activeSessionId.set('rt-other')+ $sessionTiles.set([{ storedSessionId: STORED_ID, runtimeId: RUNTIME_ID }])++ const result = await renameSessionPreferringRpc(STORED_ID, 'My branch')++ expect(request).toHaveBeenCalledWith('session.title', { session_id: RUNTIME_ID, title: 'My branch' })+ expect(renameSession).not.toHaveBeenCalled()+ expect(result.title).toBe('rpc-title')+ })++ it('resolves the runtime id from $sessionStates when no tile holds it (#70317)', async () => {+ $selectedStoredSessionId.set('some-other-active-session')+ $activeSessionId.set('rt-other')+ $sessionStates.set({+ [RUNTIME_ID]: { storedSessionId: STORED_ID } as never+ })++ await renameSessionPreferringRpc(STORED_ID, 'My branch')++ expect(request).toHaveBeenCalledWith('session.title', { session_id: RUNTIME_ID, title: 'My branch' })+ expect(renameSession).not.toHaveBeenCalled()+ })++ it('still uses REST when no surface holds a runtime id (persisted, not open)', async () => {+ // Nothing selected, no tile, no live state → genuinely a persisted-only+ // row; REST is correct (and resolves, because it has a DB row).+ $selectedStoredSessionId.set('some-other-active-session')++ await renameSessionPreferringRpc(STORED_ID, 'My branch', 'work')++ expect(request).not.toHaveBeenCalled()+ expect(renameSession).toHaveBeenCalledWith(STORED_ID, 'My branch', 'work')+ })+it('uses REST when clearing the title (RPC rejects empty titles)', async () => {$selectedStoredSessionId.set(STORED_ID)$activeSessionId.set(RUNTIME_ID)diff --git a/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx b/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsxindex ee4a221e586..1d77acb7eb3 100644--- a/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx+++ b/apps/desktop/src/app/chat/sidebar/session-actions-menu.tsx@@ -52,7 +52,7 @@ import {sessionPinId} from '@/store/session'import { $sessionColorOverrides, setSessionColorOverride } from '@/store/session-color'-import { $sessionTiles, closeAllOpenSessionTiles } from '@/store/session-states'+import { $sessionStates, $sessionTiles, closeAllOpenSessionTiles } from '@/store/session-states'import { ackStoredSessionId } from '@/store/session-unread'import { canOpenSessionInTerminal, canOpenSessionWindow, openSessionInTerminal } from '@/store/windows'@@ -74,13 +74,49 @@ import type { SessionTitleResponse } from '../../types'// background profile) keeps the REST path, which handles profile scoping and a// non-empty title is required by the RPC (it rejects clears), so clears stay on// REST too.+/** Resolve a live runtime id for a stored session id, from any surface that+ * currently holds one — not just the selected-primary row.+ *+ * A branched session opens as its own TAB and deliberately does NOT become the+ * selected primary row, so `$selectedStoredSessionId`/`$activeSessionId` do not+ * see it. Until its first turn it also has no persisted DB row, so a REST+ * rename 404s "session not found". But the branch flow binds a runtime and+ * stores it on the tile (`patchSessionTile(..., { runtimeId })`), and+ * `$sessionStates` is keyed by runtime id with `storedSessionId` on each state.+ * Consulting those lets the working `session.title` RPC path fire for a+ * just-branched draft instead of falling through to a 404. (#70317) */+function resolveRuntimeIdForStored(storedSessionId: string): null | string {+ if (storedSessionId === $selectedStoredSessionId.get()) {+ const active = $activeSessionId.get()++ if (active) {+ return active+ }+ }++ const tileRuntimeId = $sessionTiles+ .get()+ .find(tile => tile.storedSessionId === storedSessionId)?.runtimeId++ if (tileRuntimeId) {+ return tileRuntimeId+ }++ for (const [runtimeId, state] of Object.entries($sessionStates.get())) {+ if (state.storedSessionId === storedSessionId) {+ return runtimeId+ }+ }++ return null+}+export async function renameSessionPreferringRpc(storedSessionId: string,title: string,profile?: string): Promise<{ title?: string }> {- const isActiveRow = storedSessionId === $selectedStoredSessionId.get()- const runtimeId = isActiveRow ? $activeSessionId.get() : null+ const runtimeId = resolveRuntimeIdForStored(storedSessionId)const gateway = activeGateway()if (title && runtimeId && gateway) {