Skip to content

The agent that grows with you

fix(desktop): scope the shown-conversation claim to window.read; drop the fast-empty answer

Sebastiao Almeida47310227c2b8
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 f0395b4efca..28377c2040b 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
@@ -108,6 +108,11 @@ describe('preview action request routing', () => {
})
it('declines scoped pane reads in a window showing another session', async () => {
+ // A decline is not an answer: resolve_response keeps the FIRST response,
+ // so a fast empty answer from a non-claiming window could beat the
+ // claimant's real answer in the fanout race (review of #121715). The
+ // backend counts the decline as that client's vote and keeps the
+ // request open for the owner (#119333).
const reads = ['preview.read', 'terminal.read', 'window.read'].map(method =>
deliver(method, { session_id: 'session-a' }, 'session-b')
)
@@ -194,10 +199,10 @@ describe('window.read claim tolerance (#121609)', () => {
// 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')
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', method: 'window.read', replayed: false, sessionId: 'root-a' })).toBe('run')
+ expect(previewSessionRoute({ activeSessionId: null, method: 'window.read', 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')
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', method: 'window.read', replayed: false, sessionId: 'stored-a' })).toBe('run')
})
it('maps an unknown runtime id through the session-state cache to the shown conversation', () => {
@@ -206,27 +211,40 @@ describe('window.read claim tolerance (#121609)', () => {
setSelectedStoredSessionId('stored-a')
$sessionStates.set({ 'runtime-rotated': createClientSessionState('stored-a') })
- expect(previewSessionRoute({ activeSessionId: 'runtime-rotated', replayed: false, sessionId: 'runtime-rotated' })).toBe('run')
+ expect(previewSessionRoute({ activeSessionId: 'runtime-rotated', method: 'window.read', 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')
+ expect(previewSessionRoute({ activeSessionId: 'session-b', method: 'window.read', replayed: false, sessionId: 'root-a' })).toBe('run')
+ expect(previewSessionRoute({ activeSessionId: 'session-b', method: 'window.read', replayed: false, sessionId: 'stored-a' })).toBe('run')
+ })
+
+ it('keeps every other window-owned method on the strict host check even when the identity is tolerated', () => {
+ // preview.act and tour refuse on raw isActiveSession, so widening their
+ // claim would turn another window's silence into a false refusal that
+ // wins the race — and a tour refusal latches session["tour_bridge"],
+ // converting later tour actions into 45s waits (review of #121715).
+ setSelectedStoredSessionId('stored-a')
+
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', method: 'preview.read', replayed: false, sessionId: 'root-a' })).toBe('ignore')
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', method: 'terminal.read', replayed: false, sessionId: 'root-a' })).toBe('ignore')
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', method: 'preview.act', replayed: false, sessionId: 'root-a' })).toBe('ignore')
+ expect(previewSessionRoute({ activeSessionId: 'runtime-x', method: 'tour', replayed: false, sessionId: 'root-a' })).toBe('ignore')
})
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')
+ expect(previewSessionRoute({ activeSessionId: 'session-b', method: 'window.read', replayed: false, sessionId: 'session-unrelated' })).toBe('ignore')
+ expect(previewSessionRoute({ activeSessionId: 'session-b', method: 'window.read', 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')
+ expect(previewSessionRoute({ activeSessionId: 'session-b', method: 'window.read', replayed: false, sessionId: 'root-a' })).toBe('ignore')
})
it('lets a shown-conversation window answer a window.read end to end without a resume', async () => {
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 3ae511bf93f..e09157a6f78 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
@@ -130,33 +130,44 @@ export function requestNamesActiveSession({
)
}
-/**
- * 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.
- */
+/** This window hosts the session: it is the primary view or an open session tile. */
export function windowHostsSession(
sessionId: string,
activeSessionId: null | string,
storedIdForRuntimeId?: (runtimeId: string) => string | undefined
): boolean {
- if (
+ return (
requestNamesActiveSession({ activeSessionId, sessionId, storedIdForRuntimeId }) ||
$sessionTiles.get().some(tile => tile.runtimeId === sessionId)
- ) {
+ )
+}
+
+/**
+ * The window.read claim. window.read has no per-window pane, but its answer is
+ * keyed to the ANSWERING window's bounds — "below" is measured from them — so
+ * the only window that may answer is one showing the conversation. The strict
+ * host check strands real states (#121609): the HUD shows the conversation
+ * without holding it active (main holds the id on its behalf — hud-shell.tsx),
+ * and after the HUD hands the conversation back the app window's active id
+ * still names the pre-handoff runtime until a resume re-binds it (verified
+ * live: only an explicit session resume restored the answer). So beyond 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 — an entry's stored id is what this window observed
+ * for that runtime, so a stale entry still maps within its own conversation.
+ * No shown conversation — nothing claimed, as before.
+ *
+ * Scoped to window.read only. preview.act and tour refuse unless
+ * isActiveSession (raw id equality), so a tolerated claim there would turn
+ * another window's silence into a false refusal that wins the multi-window
+ * race — and a tour refusal latches session["tour_bridge"] = "answered",
+ * converting every later tour action in the session into a full 45s wait.
+ * Pane-owned reads keep #113348's owner-waiting semantics.
+ */
+export function windowReadClaimsSession(sessionId: string, activeSessionId: null | string): boolean {
+ if (windowHostsSession(sessionId, activeSessionId)) {
return true
}
@@ -186,16 +197,28 @@ export function windowHostsSession(
*/
export function previewSessionRoute({
activeSessionId,
+ method,
replayed,
sessionId,
storedIdForRuntimeId
}: {
activeSessionId: null | string
+ method?: string
replayed: boolean | undefined
sessionId: string
storedIdForRuntimeId?: (runtimeId: string) => string | undefined
}): PreviewSessionRoute {
- if (!sessionId || windowHostsSession(sessionId, activeSessionId, storedIdForRuntimeId)) {
+ if (!sessionId) {
+ return 'run'
+ }
+
+ // window.read routes through the tolerant claim (see windowReadClaimsSession);
+ // every other window-owned request keeps the strict host check.
+ if (
+ method === 'window.read'
+ ? windowReadClaimsSession(sessionId, activeSessionId)
+ : windowHostsSession(sessionId, activeSessionId, storedIdForRuntimeId)
+ ) {
return 'run'
}
@@ -617,13 +640,26 @@ export function handleServerRequest(
deps.sessionStateByRuntimeIdRef.current.get(runtimeId)?.storedSessionId ?? undefined
if (WINDOW_OWNED_REQUESTS.has(request.method)) {
- const route = previewSessionRoute({ activeSessionId, replayed: request.replayed, sessionId, storedIdForRuntimeId })
+ // Route window.read through the tolerant claim (see windowReadClaimsSession);
+ // every other window-owned request keeps the strict host check.
+ const route = previewSessionRoute({
+ activeSessionId,
+ method: request.method,
+ 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.
+ // Silence alone lets the owner win the fanout race (#113348:
+ // resolve_response keeps the FIRST response, so a fast empty or
+ // wrong-geometry answer from a non-claiming window could beat the
+ // claimant's real answer), but a decline is not an answer: the backend
+ // counts it as that client's vote and keeps the request open for the
+ // owner, settling only once every attached window declined (#119333).
+ // So an unclaimed window.read — even in a tolerated state where no
+ // window claims it (#121609) — fails fast instead of stalling the tool
+ // for its whole deadline.
declineNotShown(request)
return true