Skip to content

The agent that grows with you

fix(desktop): a send honors the live route, not the render-time copy

teknium15b61e9a6b559
diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsx
index ba05caff1c9..8350c46e1cd 100644
--- a/apps/desktop/src/app/contrib/wiring.tsx
+++ b/apps/desktop/src/app/contrib/wiring.tsx
@@ -87,6 +87,7 @@ import {
setBusy,
setMessages
} from '@/store/session'
+import { liveRoutedSessionId } from '@/store/session-states'
import { $archivedSessions } from '@/store/sidebar-archive'
import { $titlebarAppActionsSide, titlebarAppActionsClusterCounts } from '@/store/titlebar-app-actions'
import { armWakeWord, stopClientCapture } from '@/store/wake-word'
@@ -301,7 +302,7 @@ export function ContribWiring({ children }: { children: ReactNode }) {
routeTokenRef.current = routeToken
const getRouteToken = useCallback(() => routeTokenRef.current, [])
- const getRoutedStoredSessionId = useCallback(() => routedSessionIdRef.current, [])
+ const getRoutedStoredSessionId = useCallback(() => liveRoutedSessionId(routedSessionIdRef.current), [])
const clearRoutedSessionIntent = useCallback(() => {
routedSessionIdRef.current = null
diff --git a/apps/desktop/src/store/session-states-live-route.test.ts b/apps/desktop/src/store/session-states-live-route.test.ts
new file mode 100644
index 00000000000..74c9d7ef979
--- /dev/null
+++ b/apps/desktop/src/store/session-states-live-route.test.ts
@@ -0,0 +1,31 @@
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { liveRoutedSessionId } from './session-states'
+
+// Branch → immediate Enter: navigate() had already rewritten location.hash to
+// the branch, but the render-time route copy still named the parent, so the
+// send's route/selection guard saw nothing to reconcile and U5 landed in the
+// parent (lineage-sidebar.spec.ts "U5 landed in a NEW session", ~1 in 28 runs
+// on a 2-core box). The send must honor the live hash over the stale copy.
+describe('liveRoutedSessionId', () => {
+ afterEach(() => {
+ window.history.replaceState(null, '', window.location.pathname)
+ })
+
+ it('prefers the live hash route over a stale render-time copy', () => {
+ window.location.hash = '#/local-branch'
+
+ expect(liveRoutedSessionId('local-parent')).toBe('local-branch')
+ })
+
+ it('treats a new-chat hash as authoritative even when the copy still names a session', () => {
+ window.location.hash = '#/'
+
+ expect(liveRoutedSessionId('local-parent')).toBeNull()
+ })
+
+ it('falls back to the render-time copy when there is no hash at all', () => {
+ expect(window.location.hash).toBe('')
+ expect(liveRoutedSessionId('local-parent')).toBe('local-parent')
+ })
+})
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 0bd969c6fd2..a1c35d93645 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -539,6 +539,17 @@ function windowRouteSessionId(): string | null {
return routeSessionId(window.location.hash.replace(/^#/, ''))
}
+/** The routed stored session a send should honor RIGHT NOW. navigate()
+ * rewrites location.hash synchronously, but a render-time copy of the route
+ * only catches up on the next render; a send inside that gap (branch, then
+ * an immediate Enter on a slow machine) would trust the stale copy and
+ * deliver into the previous chat. Any hash is authoritative (including a
+ * new-chat hash, which names no session); with no hash at all (unit tests,
+ * first paint) the render-time copy is all there is. */
+export function liveRoutedSessionId(renderTimeRoute: string | null): string | null {
+ return typeof window !== 'undefined' && window.location.hash ? windowRouteSessionId() : renderTimeRoute
+}
+
/** Whether the user is still focused on a session that belongs to the same
* durable lineage as the given stored id. Used to decide whether a
* backgrounded session's delayed id-rotation may follow the route/selection