Skip to content

The agent that grows with you

fix(desktop): the owner-queue re-read skips the open chat after compression, and its watcher is tested end to end

Teknium2f0664edffde
diff --git a/apps/desktop/src/store/owner-queue-reconcile.test.ts b/apps/desktop/src/store/owner-queue-reconcile.test.ts
index fab8b50dfd4..be96fa8baa9 100644
--- a/apps/desktop/src/store/owner-queue-reconcile.test.ts
+++ b/apps/desktop/src/store/owner-queue-reconcile.test.ts
@@ -37,73 +37,85 @@ afterEach(() => {
$selectedStoredSessionId.set(null)
})
-it('a chat whose queue is paused behind a turn lost in a restart needs the user, it is not finished-unread', () => {
+it('a chat whose queue is paused behind a turn lost in a restart needs the user, unless its turn runs again', () => {
setSessions([row('beta', { unread: true }), row('alpha')])
reconcilePendingSubmissions('beta', pausedSnapshot)
expect($sessionDotStateById.get().beta).toBe('needs-input')
expect($sessionDotStateById.get().alpha).toBeUndefined()
+ // A turn running in the chat again reads working, not needs-input.
+ publishSessionState('runtime-beta', { ...createClientSessionState('beta'), busy: true })
+ expect($sessionDotStateById.get().beta).toBe('working')
+ publishSessionState('runtime-beta', createClientSessionState('beta'))
+
// Discarded: the owner lists only the send, now running. The chat stops asking for the user.
reconcilePendingSubmissions('beta', [{ admission_id: 'queued-send', status: 'started', user: 'queued after hold' }])
expect($sessionDotStateById.get().beta).toBe('unread')
})
-it('a paused chat whose turn is running again reads working, not needs-input', () => {
- setSessions([row('beta')])
- reconcilePendingSubmissions('beta', pausedSnapshot)
- publishSessionState('runtime-beta', { ...createClientSessionState('beta'), busy: true })
-
- expect($sessionDotStateById.get().beta).toBe('working')
-})
-
-it('every gateway open re-reads the owner queue of each chat off screen with the open path params, and never a legacy backend', async () => {
- setSessions([row('beta'), row('alpha'), row('routed')])
+it('each gateway open re-reads, once the list loads, the owner queue of every chat off screen, and never a legacy backend', async () => {
$connection.set(connectionAt('ws://127.0.0.1:9/api/ws?native_dial=1'))
trackPendingSubmission('beta', { id: 'queued-send', text: 'queued after hold', status: 'queued' })
+ // The chat on screen, keyed by its lineage root while the selection holds the compressed tip:
+ // its own route resume reads it.
trackPendingSubmission('alpha', { id: 'on-screen', text: 'y', status: 'queued' })
trackPendingSubmission('routed', { id: 'other', text: 'x', status: 'queued' })
// A live snapshot from an earlier connection says nothing about what a restarted owner holds.
publishSessionState('runtime-routed', createClientSessionState('routed'))
- // The chat on screen is read by its own route resume.
- $selectedStoredSessionId.set('alpha')
+ $selectedStoredSessionId.set('alpha-tip')
const request = vi.fn(async (_method: string, params?: Record<string, unknown>) => ({
session_id: params?.session_id,
info: { stored_session_id: params?.session_id, pending_submissions: pausedSnapshot }
}))
- const reads = () => request.mock.calls.map(([method, params]) => [method, params?.session_id, params?.defer_history])
- const stop = watchOwnerQueues(request as unknown as Parameters<typeof watchOwnerQueues>[0])
+ const reads = () =>
+ request.mock.calls.map(([method, params]) => [method, params?.session_id, params?.defer_history]).sort()
- const openEdge = async () => {
- $gatewayState.set('closed')
- $gatewayState.set('open')
- await vi.waitFor(() => expect(request).toHaveBeenCalledTimes(2))
- await new Promise(resolve => setTimeout(resolve, 0))
- }
+ const settle = () => new Promise(resolve => setTimeout(resolve, 0))
- await openEdge()
- expect(reads().sort()).toEqual([
+ const offScreenReads = [
['session.resume', 'beta', true],
['session.resume', 'routed', true]
- ])
+ ]
+
+ const stop = watchOwnerQueues(request as unknown as Parameters<typeof watchOwnerQueues>[0])
+
+ // The gateway opens before the session list can route a read: nothing goes out until it loads.
+ $gatewayState.set('open')
+ await settle()
+ expect(request).not.toHaveBeenCalled()
+
+ setSessions([row('beta'), row('alpha-tip', { _lineage_root_id: 'alpha' }), row('routed')])
+ await vi.waitFor(() => expect(request).toHaveBeenCalledTimes(2))
+ // A later list refresh on the same connection reads nothing more.
+ setSessions([row('beta'), row('alpha-tip', { _lineage_root_id: 'alpha' }), row('routed'), row('gamma')])
+ await settle()
+ expect(reads()).toEqual(offScreenReads)
expect($sessionDotStateById.get().beta).toBe('needs-input')
// The owner restarts while the window stays up: the same chats are read again.
request.mockClear()
- await openEdge()
- expect(reads().sort()).toEqual([
- ['session.resume', 'beta', true],
- ['session.resume', 'routed', true]
- ])
+ $gatewayState.set('closed')
+ $gatewayState.set('open')
+ await vi.waitFor(() => expect(request).toHaveBeenCalledTimes(2))
+ await settle()
+ expect(reads()).toEqual(offScreenReads)
// A legacy (non-canonical) backend's cold resume can auto-continue a crashed turn: never read there.
request.mockClear()
$connection.set(connectionAt('wss://remote.example/api/ws?token=t'))
$gatewayState.set('closed')
$gatewayState.set('open')
- await new Promise(resolve => setTimeout(resolve, 0))
+ await settle()
expect(request).not.toHaveBeenCalled()
+
+ // Stopped: later opens read nothing.
+ $connection.set(connectionAt('ws://127.0.0.1:9/api/ws?native_dial=1'))
stop()
+ $gatewayState.set('closed')
+ $gatewayState.set('open')
+ await settle()
+ expect(request).not.toHaveBeenCalled()
})
diff --git a/apps/desktop/src/store/owner-queue-reconcile.ts b/apps/desktop/src/store/owner-queue-reconcile.ts
index 4cd33e6f603..37657372931 100644
--- a/apps/desktop/src/store/owner-queue-reconcile.ts
+++ b/apps/desktop/src/store/owner-queue-reconcile.ts
@@ -7,7 +7,7 @@ import type { SessionResumeResult } from '@/types/hermes'
import { $queuedPromptsBySession, serverOwnsComposerQueue } from './composer-queue'
import { journaledPendingSessions, pendingSnapshotFence, reconcilePendingSubmissions } from './pending-submissions'
-import { $gatewayState, $selectedStoredSessionId, $sessions } from './session'
+import { $gatewayState, $selectedStoredSessionId, $sessions, resolveComposerSessionKey } from './session'
import { knownOwnerForSession, requestForOwnedSession } from './session-states'
type GatewayRequest = <T>(method: string, params?: Record<string, unknown>, timeoutMs?: number) => Promise<T>
@@ -74,8 +74,13 @@ async function reconcileOne(request: GatewayRequest, key: string): Promise<void>
* start a billed model turn in a chat the user never opened.
*/
export async function reconcileOwnerQueues(request: GatewayRequest): Promise<void> {
- const selected = $selectedStoredSessionId.get()
- const queue = sessionsWithOwnerWork().filter(key => key !== selected && serverOwnsComposerQueue(key))
+ // Queue keys are lineage roots; the selection is the stored tip, which differs after compression.
+ const sessions = $sessions.get()
+ const selected = resolveComposerSessionKey($selectedStoredSessionId.get(), sessions)
+
+ const queue = sessionsWithOwnerWork().filter(
+ key => resolveComposerSessionKey(key, sessions) !== selected && serverOwnsComposerQueue(key)
+ )
const worker = async () => {
for (let key = queue.shift(); key !== undefined; key = queue.shift()) {