Skip to content

The agent that grows with you

fix(desktop): read an off-screen owner queue only on a provably local route

Tekniumb7a9d0dc3b34
diff --git a/apps/desktop/src/store/owner-queue-reconcile.test.ts b/apps/desktop/src/store/owner-queue-reconcile.test.ts
index be96fa8baa9..646b508545e 100644
--- a/apps/desktop/src/store/owner-queue-reconcile.test.ts
+++ b/apps/desktop/src/store/owner-queue-reconcile.test.ts
@@ -5,12 +5,20 @@ import { createClientSessionState } from '@/lib/chat-runtime'
import type { SessionInfo } from '@/types/hermes'
import { $queuedPromptsBySession } from './composer-queue'
+import { requestGatewayForProfile } from './gateway'
import { watchOwnerQueues } from './owner-queue-reconcile'
import { reconcilePendingSubmissions, trackPendingSubmission } from './pending-submissions'
import { $connection, $gatewayState, $selectedStoredSessionId, $sessions, setSessions } from './session'
import { $sessionDotStateById } from './session-dot-state'
import { clearAllSessionStates, publishSessionState } from './session-states'
+// Pass-through: records which profile's own backend a read is routed to.
+vi.mock('@/store/gateway', async importOriginal => {
+ const actual = await importOriginal<{ requestGatewayForProfile: (...args: unknown[]) => unknown }>()
+
+ return { ...actual, requestGatewayForProfile: vi.fn(actual.requestGatewayForProfile) }
+})
+
const connectionAt = (wsUrl: string) => ({ mode: 'local', wsUrl }) as HermesConnection
const row = (id: string, extra: Partial<SessionInfo> = {}): SessionInfo =>
@@ -119,3 +127,20 @@ it('each gateway open re-reads, once the list loads, the owner queue of every ch
await settle()
expect(request).not.toHaveBeenCalled()
})
+
+it('a chat owned by a profile whose backend is not provably local is never read in the background', async () => {
+ // The foreground is a local canonical gateway, but the chat's owner is a bare profile name: the
+ // read would go to that profile's own backend (remote override, possibly legacy, unknown here).
+ $connection.set(connectionAt('ws://127.0.0.1:9/api/ws?native_dial=1'))
+ trackPendingSubmission('elsewhere', { id: 'queued-send', text: 'queued after hold', status: 'queued' })
+ const request = vi.fn(async () => ({}))
+ const stop = watchOwnerQueues(request as unknown as Parameters<typeof watchOwnerQueues>[0])
+
+ $gatewayState.set('open')
+ setSessions([row('elsewhere', { profile: 'work' })])
+ await new Promise(resolve => setTimeout(resolve, 0))
+ stop()
+
+ expect(request).not.toHaveBeenCalled()
+ expect(vi.mocked(requestGatewayForProfile)).not.toHaveBeenCalled()
+})
diff --git a/apps/desktop/src/store/owner-queue-reconcile.ts b/apps/desktop/src/store/owner-queue-reconcile.ts
index 37657372931..aac6187b103 100644
--- a/apps/desktop/src/store/owner-queue-reconcile.ts
+++ b/apps/desktop/src/store/owner-queue-reconcile.ts
@@ -2,13 +2,14 @@ import { computed } from 'nanostores'
import { singleFlightSessionResume } from '@/app/session/hooks/use-prompt-actions/single-flight-resume'
import { primaryResumeParams } from '@/app/session/hooks/use-session-actions/resume-owner'
+import { mapPool } from '@/lib/pool'
import { stableArray } from '@/lib/stable-array'
import type { SessionResumeResult } from '@/types/hermes'
import { $queuedPromptsBySession, serverOwnsComposerQueue } from './composer-queue'
import { journaledPendingSessions, pendingSnapshotFence, reconcilePendingSubmissions } from './pending-submissions'
-import { $gatewayState, $selectedStoredSessionId, $sessions, resolveComposerSessionKey } from './session'
-import { knownOwnerForSession, requestForOwnedSession } from './session-states'
+import { $connection, $gatewayState, $selectedStoredSessionId, $sessions, resolveComposerSessionKey } from './session'
+import { knownOwnerForSession, ownerConnectionMode, requestForOwnedSession } from './session-states'
type GatewayRequest = <T>(method: string, params?: Record<string, unknown>, timeoutMs?: number) => Promise<T>
@@ -60,6 +61,16 @@ async function reconcileOne(request: GatewayRequest, key: string): Promise<void>
}
}
+// The read goes to the chat's OWN backend (`requestForOwnedSession`), which for a bare profile name
+// is that profile's backend, not the foreground connection `serverOwnsComposerQueue` inspects: a
+// profile with a remote override is a remote, possibly legacy, backend. Only a route provably local
+// is read; an owner whose backend mode is unknown is skipped (its chat's own open path reads it).
+function ownerRouteIsLocal(key: string): boolean {
+ const owner = knownOwnerForSession(key)
+
+ return (owner ? ownerConnectionMode(owner) : $connection.get()?.mode) === 'local'
+}
+
/**
* Re-read the owner's pending list for every chat this machine queued owner work in, except the
* one on screen (its route resume reads it). The queue card of a send queued behind a running turn
@@ -68,9 +79,11 @@ async function reconcileOne(request: GatewayRequest, key: string): Promise<void>
* and never ran. Every open edge re-reads: a snapshot from an earlier connection says nothing about
* what the restarted owner holds.
*
- * The read is the open path's own `session.resume` (deferred history, no messages). Only chats
- * whose owner admits canonically are read: that owner answers with `pending_submissions` and never
- * auto-continues an interrupted turn on resume, where a legacy remote backend's cold resume would
+ * The read is the open path's own `session.resume`. It is a full attach, not a lightweight read: the
+ * canonical gateway ignores `omit_messages`, so the reply carries the whole stored transcript and
+ * the window stays subscribed to that chat (as after opening it). Only chats whose owner admits
+ * canonically on a provably local route are read: that owner answers with `pending_submissions` and
+ * never auto-continues an interrupted turn on resume, where a legacy backend's cold resume would
* start a billed model turn in a chat the user never opened.
*/
export async function reconcileOwnerQueues(request: GatewayRequest): Promise<void> {
@@ -79,16 +92,11 @@ export async function reconcileOwnerQueues(request: GatewayRequest): Promise<voi
const selected = resolveComposerSessionKey($selectedStoredSessionId.get(), sessions)
const queue = sessionsWithOwnerWork().filter(
- key => resolveComposerSessionKey(key, sessions) !== selected && serverOwnsComposerQueue(key)
+ key =>
+ resolveComposerSessionKey(key, sessions) !== selected && serverOwnsComposerQueue(key) && ownerRouteIsLocal(key)
)
- const worker = async () => {
- for (let key = queue.shift(); key !== undefined; key = queue.shift()) {
- await reconcileOne(request, key)
- }
- }
-
- await Promise.all(Array.from({ length: Math.min(RECONCILE_CONCURRENCY, queue.length) }, worker))
+ await mapPool(queue, RECONCILE_CONCURRENCY, key => reconcileOne(request, key))
}
// One read per edge into an open gateway (first boot, relaunch, owner restart), taken once the
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 89f749ffa8f..2937b77345b 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -1819,7 +1819,7 @@ syncPreviewScope()
/** The mode of the backend that serves `owner`: the route's own `mode`, else
* its registry connection's kind, else the socket already dialed for it (a
* bare profile rides the primary or its own pool secondary). Null = unknown. */
-function ownerConnectionMode(owner: SessionOwnerScope): 'local' | 'remote' | null {
+export function ownerConnectionMode(owner: SessionOwnerScope): 'local' | 'remote' | null {
if (!owner) {
return null
}