Skip to content

The agent that grows with you

fix(desktop): claim scoped reads by shown-conversation identity so HUD handoff stops stranding window.read

Sebastiao Almeida8e4ff54d890a
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts
index 1cba27430be..f0395b4efca 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.test.ts
@@ -2,8 +2,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createClientSessionState } from '@/lib/chat-runtime'
import { hasOpenServerRequest, resetServerRequestsForTests } from '@/store/server-requests'
-import { setActiveSessionId, setSessions } from '@/store/session'
-import { $sessionTiles } from '@/store/session-states'
+import { setActiveSessionId, setSelectedStoredSessionId, setSessions } from '@/store/session'
+import { $sessionStates, $sessionTiles } from '@/store/session-states'
import { $toursEnabled } from '@/store/tours'
import type { SessionInfo } from '@/types/hermes'
@@ -130,6 +130,21 @@ describe('preview action request routing', () => {
expect(replay.respond).not.toHaveBeenCalled()
})
+ it('declines window.read instead of stalling when no window claims the session (#121609)', async () => {
+ // window.read has no per-window pane: an unclaimed request used to wait
+ // out the tool's full 30s deadline because silence was the "not mine"
+ // signal. It now declines — the backend settles fast once every attached
+ // window declined (#119333) — while pane-owned reads keep waiting for
+ // their owner, whose real answer must not be beaten by an empty one (#113348).
+ const { decline, handled, respond } = deliver('window.read', { session_id: 'session-a' }, 'session-b')
+
+ await Promise.resolve()
+
+ expect(handled).toBe(true)
+ expect(decline).toHaveBeenCalledTimes(1)
+ expect(respond).not.toHaveBeenCalled()
+ })
+
it("answers pane reads for a session hosted in one of this window's tiles", async () => {
// The tile session is not the active one, but this window hosts it: its
// panes are here, so an 'ignore' would stall the tool until its deadline.
@@ -159,6 +174,76 @@ describe('preview action request routing', () => {
})
})
+describe('window.read claim tolerance (#121609)', () => {
+ beforeEach(() => {
+ setSessions([
+ { id: 'stored-a', title: 'HUD conversation', _lineage_root_id: 'root-a' } as SessionInfo
+ ])
+ })
+
+ afterEach(() => {
+ setSessions([])
+ setActiveSessionId(null)
+ setSelectedStoredSessionId(null)
+ $sessionStates.set({})
+ $sessionTiles.set([])
+ })
+
+ it('claims when the window shows the conversation under its stored id while the backend asks about the runtime id', () => {
+ // The HUD state: active is the pre-handoff runtime (or nothing), but this
+ // window has the conversation selected and its runtime id lineage-maps.
+ setSelectedStoredSessionId('stored-a')
+
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', replayed: false, sessionId: 'root-a' })).toBe('run')
+ expect(previewSessionRoute({ activeSessionId: null, replayed: false, sessionId: 'root-a' })).toBe('run')
+ // Plain stored-id ask (no rotation): selected matches directly.
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', replayed: false, sessionId: 'stored-a' })).toBe('run')
+ })
+
+ it('maps an unknown runtime id through the session-state cache to the shown conversation', () => {
+ // A resume rebound the runtime without this window re-deriving lineage:
+ // the state cache records which stored id the runtime id belongs to.
+ setSelectedStoredSessionId('stored-a')
+ $sessionStates.set({ 'runtime-rotated': createClientSessionState('stored-a') })
+
+ expect(previewSessionRoute({ activeSessionId: 'runtime-rotated', replayed: false, sessionId: 'runtime-rotated' })).toBe('run')
+ })
+
+ it('claims for a tile whose stored session lineage-matches the asked id', () => {
+ $sessionTiles.set([{ runtimeId: 'tile-runtime', storedSessionId: 'stored-a' } as never])
+
+ expect(previewSessionRoute({ activeSessionId: 'session-b', replayed: false, sessionId: 'root-a' })).toBe('run')
+ expect(previewSessionRoute({ activeSessionId: 'session-b', replayed: false, sessionId: 'stored-a' })).toBe('run')
+ })
+
+ it('never claims a conversation this window does not show', () => {
+ setSelectedStoredSessionId('stored-a')
+
+ expect(previewSessionRoute({ activeSessionId: 'session-b', replayed: false, sessionId: 'session-unrelated' })).toBe('ignore')
+ expect(previewSessionRoute({ activeSessionId: 'session-b', replayed: false, sessionId: 'root-other' })).toBe('ignore')
+ })
+
+ it('claims nothing without a shown conversation — a background session stays unclaimed', () => {
+ // No selection, no tiles: the tolerant branch must stay inert so a window
+ // midsession cannot answer for a background conversation it never showed.
+ expect(previewSessionRoute({ activeSessionId: 'session-b', replayed: false, sessionId: 'root-a' })).toBe('ignore')
+ })
+
+ it('lets a shown-conversation window answer a window.read end to end without a resume', async () => {
+ // The filed repro: HUD mode / post-handoff main window — no runtime claim,
+ // only the stored selection. The request now answers (empty here, because
+ // the test window exposes no readWindowBelow bridge) instead of stalling.
+ setSelectedStoredSessionId('stored-a')
+
+ const { handled, respond } = deliver('window.read', { session_id: 'root-a' }, 'runtime-x')
+
+ await Promise.resolve()
+
+ expect(handled).toBe(true)
+ expect(respond).toHaveBeenCalledTimes(1)
+ })
+})
+
describe('tour request routing', () => {
afterEach(() => {
$toursEnabled.set(true)
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
index f741bb054c7..3ae511bf93f 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
@@ -24,8 +24,8 @@ import {
setVaultUnlockRequest
} from '@/store/prompts'
import { rememberServerRequest } from '@/store/server-requests'
-import { $sessions, sessionMatchesStoredId } from '@/store/session'
-import { $sessionTiles } from '@/store/session-states'
+import { $selectedStoredSessionId, $sessions, lineageAliases, sessionMatchesStoredId } from '@/store/session'
+import { $sessionStates, $sessionTiles } from '@/store/session-states'
import { requestScrollToBottom } from '@/store/thread-scroll'
import { $toursEnabled } from '@/store/tours'
@@ -130,15 +130,49 @@ export function requestNamesActiveSession({
)
}
-/** This window hosts the session: it is the primary view or an open session tile. */
+/**
+ * This window hosts the session: the primary view, an open session tile, or
+ * (identity-tolerantly) a window showing the conversation under another id.
+ *
+ * The strict runtime-id checks alone strand real states (#121609): the HUD
+ * shows the conversation but never holds it active (main holds the id on its
+ * behalf — hud-shell.tsx), and after the HUD hands the session back the app
+ * window's active id can still name the pre-handoff runtime until a resume
+ * re-binds it. In both, every attached window ignored the request and the
+ * tool stalled its full 30s deadline; only an explicit session resume fixed
+ * it. So alongside the strict checks, the asked id may resolve to a
+ * conversation this window SHOWS: the selected stored session or a tile's
+ * stored session, matched through lineageAliases (compression rotates the
+ * runtime tip under the stored identity) and the session-state cache, which
+ * records which stored id each runtime id maps to. Without evidence of a
+ * shown conversation — no selection, no tile, no state mapping — nothing is
+ * claimed, exactly as before.
+ */
export function windowHostsSession(
sessionId: string,
activeSessionId: null | string,
storedIdForRuntimeId?: (runtimeId: string) => string | undefined
): boolean {
- return (
+ if (
requestNamesActiveSession({ activeSessionId, sessionId, storedIdForRuntimeId }) ||
$sessionTiles.get().some(tile => tile.runtimeId === sessionId)
+ ) {
+ return true
+ }
+
+ const sessions = $sessions.get()
+
+ const shown = [
+ $selectedStoredSessionId.get(),
+ ...$sessionTiles.get().map(tile => tile.storedSessionId)
+ ]
+
+ return shown.some(
+ stored =>
+ stored !== null &&
+ (stored === sessionId ||
+ lineageAliases(stored, sessions).includes(sessionId) ||
+ ($sessionStates.get()[sessionId]?.storedSessionId ?? null) === stored)
)
}
@@ -586,6 +620,10 @@ export function handleServerRequest(
const route = previewSessionRoute({ activeSessionId, replayed: request.replayed, sessionId, storedIdForRuntimeId })
if (route === 'ignore') {
+ // #121609's unclaimed window.read lands here too: with the claim widened
+ // to shown conversations, a window that still does not show the session
+ // declines, and the backend settles fast once every attached window
+ // declined (#119333) instead of the tool stalling its full 30s deadline.
declineNotShown(request)
return true