Skip to content

The agent that grows with you

fix: preserve desktop busy correction intent on rejection

Tekniumb3cca0f3caf7
diff --git a/apps/desktop/src/api/canonical-protocol.test.ts b/apps/desktop/src/api/canonical-protocol.test.ts
index fa97404ae2c..77fe8a0804d 100644
--- a/apps/desktop/src/api/canonical-protocol.test.ts
+++ b/apps/desktop/src/api/canonical-protocol.test.ts
@@ -2,6 +2,24 @@ import { expect, test } from 'vitest'
import { CanonicalDesktopProtocol } from './canonical-protocol'
+test('corrections preserve intent and fence the observed session generation', () => {
+ const protocol = new CanonicalDesktopProtocol()
+
+ for (const method of ['session.redirect', 'session.steer']) {
+ expect(() => protocol.prepare(method, { session_id: 's', text: 'correction' })).toThrow('execution identity')
+ }
+
+ protocol.event({ type: 'message.start', session_id: 's', payload: { execution_generation: 4 } })
+ protocol.event({ type: 'message.start', session_id: 'other', payload: { execution_generation: 9 } })
+
+ for (const method of ['session.redirect', 'session.steer']) {
+ const params = protocol.prepare(method, { session_id: 's', text: 'correction' })
+ expect(params).toEqual({ session_id: 's', text: 'correction', execution_generation: 4 })
+ expect(protocol.wire(method, params)).toBe(method)
+ expect(protocol.prepare(method, { ...params, execution_generation: 2 }).execution_generation).toBe(2)
+ }
+})
+
test('metadata writes retain the original CAS revision and request identity until acknowledgement', () => {
const protocol = new CanonicalDesktopProtocol()
protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 7 })
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 916a8a969bf..719a6d06b86 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -107,12 +107,12 @@ export class CanonicalDesktopProtocol {
return { ...result, request_id: requestId, source: 'gui' }
}
- if (method === 'session.interrupt') {
+ if (method === 'session.interrupt' || method === 'session.redirect' || method === 'session.steer') {
const generation = params.execution_generation ?? this.generations.get(String(params.session_id))
- if (typeof generation !== 'number') { throw new Error('Session execution identity unavailable; reconnect before Stop') }
+ if (typeof generation !== 'number') { throw new Error('Session execution identity unavailable; reconnect before controlling this turn') }
- return { session_id: params.session_id, execution_generation: generation }
+ return { ...params, session_id: params.session_id, execution_generation: generation }
}
if (method === 'prompt.resolve_unknown') {
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
index 5bbcb34b128..9153d3008eb 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.test.tsx
@@ -56,6 +56,8 @@ function renderSubmitHook({
editor.dataset.slot = 'composer-rich-input'
editor.textContent = text
const editorRef = { current: editor }
+ const loadIntoComposer = vi.fn()
+ const stashAt = vi.fn()
const onCancel = vi.fn()
const onSteer = vi.fn(async () => true)
const onSubmit = vi.fn(async () => true)
@@ -111,7 +113,7 @@ function renderSubmitHook({
exitQueuedEdit: vi.fn(() => false),
focusInput: vi.fn(),
inputDisabled,
- loadIntoComposer: vi.fn(),
+ loadIntoComposer,
onCancel,
onSteer,
onSubmit,
@@ -120,12 +122,14 @@ function renderSubmitHook({
queuedPrompts: [],
sessionId: 'runtime-session',
setComposerText: vi.fn(),
- stashAt: vi.fn()
+ stashAt
}),
{ wrapper: Wrapper }
)
return {
+ loadIntoComposer,
+ stashAt,
clearDraft,
hook,
onCancel,
@@ -149,6 +153,18 @@ describe('useComposerSubmit external request routing', () => {
vi.restoreAllMocks()
})
+ it.each(['interrupt', 'steer'] as const)('restores a rejected %s draft without queue admission', async mode => {
+ const h = renderSubmitHook({ busy: true, busyInputMode: mode, text: 'keep guidance' })
+ h.onSteer.mockRejectedValue(new Error('correction unsupported'))
+ act(() => h.hook.result.current.submitDraft())
+ await waitFor(() => expect(h.loadIntoComposer).toHaveBeenCalledWith('keep guidance', []))
+ expect(h.stashAt).toHaveBeenCalledWith('stored-session', 'keep guidance', [])
+ expect(h.onSteer).toHaveBeenCalledWith('keep guidance', mode)
+ expect(h.onSubmit).not.toHaveBeenCalled()
+ expect(h.queueCurrentDraft).not.toHaveBeenCalled()
+ expect(getQueuedPrompts('stored-session')).toEqual([])
+ })
+
it('routes a refused native steer through canonical queue admission', async () => {
$connection.set({ mode: 'local', wsUrl: 'ws://localhost/api/ws?native_dial=unminted' } as never)
$queuedPromptsBySession.set({})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
index ec10f87f50a..0fc592b883a 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
@@ -251,8 +251,15 @@ export function useComposerSubmit({
triggerHaptic('submit')
clearDraft()
+ const submittedScope = activeQueueSessionKeyRef.current
+
+ const restore = () => {
+ loadIntoComposer(text, [])
+ stashAt(submittedScope, text, [])
+ }
+
const canonical = serverOwnsComposerQueue(sessionId ?? activeQueueSessionKey)
- void Promise.resolve(onSteer(text, mode)).then(accepted => {
+ void Promise.resolve().then(() => onSteer(text, mode)).then(accepted => {
if (!accepted && activeQueueSessionKey) {
if (canonical) {
dispatchSubmit(text, [], undefined, { fromQueue: true, sessionId: sessionId ?? null, storedSessionId: activeQueueSessionKey })
@@ -260,7 +267,7 @@ export function useComposerSubmit({
enqueueQueuedPrompt(activeQueueSessionKey, { text, attachments: [] })
}
}
- })
+ }).catch(restore)
}
const queueDraft = () => {
diff --git a/apps/desktop/src/app/chat/session-tile-actions.test.ts b/apps/desktop/src/app/chat/session-tile-actions.test.ts
index cd221ad8e54..a7b3a7a1a61 100644
--- a/apps/desktop/src/app/chat/session-tile-actions.test.ts
+++ b/apps/desktop/src/app/chat/session-tile-actions.test.ts
@@ -140,6 +140,15 @@ describe('useSessionTileActions sleep/wake session recovery', () => {
expect($sessionTiles.get()[0]?.runtimeId).toBe(RECOVERED_SESSION_ID)
})
+ it.each(['interrupt', 'steer'] as const)('rejects tile %s RPC failure without converting it to a queue signal', async mode => {
+ requestGatewayMock.mockRejectedValue(new Error('correction unsupported'))
+ const { result } = renderTileActions()
+ await act(async () => {
+ await expect(result.current.steerPrompt('keep correction', mode)).rejects.toThrow('correction unsupported')
+ })
+ expect(requestGatewayMock).toHaveBeenCalledExactlyOnceWith(mode === 'steer' ? 'session.steer' : 'session.redirect', { session_id: RUNTIME_SESSION_ID, text: 'keep correction' })
+ })
+
it('resumes the stored session and retries once when session.redirect (steer) reports "session not found"', async () => {
const calls: { method: string; params?: Record<string, unknown> }[] = []
let redirectAttempts = 0
diff --git a/apps/desktop/src/app/chat/session-tile-actions.ts b/apps/desktop/src/app/chat/session-tile-actions.ts
index 1c8dcfd246b..b71b0614997 100644
--- a/apps/desktop/src/app/chat/session-tile-actions.ts
+++ b/apps/desktop/src/app/chat/session-tile-actions.ts
@@ -437,18 +437,17 @@ export function useSessionTileActions({ requestGateway, runtimeId, scope, stored
return true
}
- } catch {
+ } catch (err) {
discardOptimisticMessage()
- // Swallow — the caller queues the text so nothing is lost.
-
- return false
+ notifyError(err, copy.promptFailed)
+ throw err
}
discardOptimisticMessage()
return false
},
- [bindRecoveredRuntime, requestSessionGateway]
+ [bindRecoveredRuntime, copy.promptFailed, requestSessionGateway]
)
// Rewind primitive (interrupt-first for live turns, busy-retry) — shared with
diff --git a/apps/desktop/src/app/session/hooks/use-busy-input-mode.test.tsx b/apps/desktop/src/app/session/hooks/use-busy-input-mode.test.tsx
index 472804b9194..acfaca0611c 100644
--- a/apps/desktop/src/app/session/hooks/use-busy-input-mode.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-busy-input-mode.test.tsx
@@ -23,6 +23,24 @@ afterEach(() => {
$gatewayState.set('closed')
})
+it('canonical busy policy is session scoped even with a cached global mode', async () => {
+ $connection.set({ mode: 'local', wsUrl: 'ws://localhost/api/ws?native_dial=fixture' } as never)
+ $gatewayState.set('open')
+ $busyInputConfig.set({ owner: JSON.stringify(['', 'default']), connection: $connection.get(), mode: 'queue' })
+ let resolveNext!: (result: { value: string }) => void
+ const request = vi.fn().mockResolvedValueOnce({ value: 'steer' }).mockImplementationOnce(() => new Promise(resolve => { resolveNext = resolve }))
+ const h = renderHook(({ sessionId }) => useBusyInputMode({ sessionId, storedSessionId: null, requestGateway: request }), { initialProps: { sessionId: 'first' } })
+
+ try {
+ await waitFor(() => expect(h.result.current).toBe('steer'))
+ h.rerender({ sessionId: 'second' })
+ expect(h.result.current).toBeNull()
+ await act(async () => resolveNext({ value: 'interrupt' }))
+ expect(h.result.current).toBe('interrupt')
+ expect(request).toHaveBeenLastCalledWith('config.get', { key: 'busy', session_id: 'second' })
+ } finally { h.unmount(); $connection.set(null) }
+})
+
it('uses only the current owner config and ignores a late response from the previous profile', async () => {
$gatewayState.set('open')
let resolveOld!: (value: { value: string }) => void
diff --git a/apps/desktop/src/app/session/hooks/use-busy-input-mode.ts b/apps/desktop/src/app/session/hooks/use-busy-input-mode.ts
index 40457a9ee0b..1ba0a2ec61c 100644
--- a/apps/desktop/src/app/session/hooks/use-busy-input-mode.ts
+++ b/apps/desktop/src/app/session/hooks/use-busy-input-mode.ts
@@ -8,6 +8,7 @@ import {
busyInputOwnerKey,
normalizeBusyInputMode
} from '@/store/busy-input-mode'
+import { serverOwnsComposerQueue } from '@/store/composer-queue'
import { $activeGatewayProfile } from '@/store/profile'
import { $connection, $gatewayState, knownSessionOwner, ownerLookupSessionRows } from '@/store/session'
import { requestForSessionProfile } from '@/store/session-request-router'
@@ -36,11 +37,12 @@ export function useBusyInputMode({
const targetProfile = route && typeof route === 'object' ? route.targetProfile : undefined
const owner = busyInputOwnerKey(connectionId, targetProfile || ownerProfile)
- const [loaded, setLoaded] = useState<{ owner: string; connection: typeof connection; mode: BusyInputMode } | null>(
+ const [loaded, setLoaded] = useState<{ owner: string; connection: typeof connection; sessionId: string; mode: BusyInputMode } | null>(
null
)
- const configured = config?.owner === owner && config.connection === connection ? config.mode : null
+ const canonical = serverOwnsComposerQueue(sessionId ?? storedSessionId)
+ const configured = !canonical && config?.owner === owner && config.connection === connection ? config.mode : null
useEffect(() => {
if (configured !== null || !sessionId || gatewayState !== 'open') {
@@ -55,7 +57,7 @@ export function useBusyInputMode({
})
.then(result => {
if (!cancelled && result && Object.hasOwn(result, 'value')) {
- setLoaded({ owner, connection, mode: normalizeBusyInputMode(result.value) })
+ setLoaded({ owner, connection, sessionId, mode: normalizeBusyInputMode(result.value) })
}
})
.catch(() => undefined)
@@ -79,5 +81,5 @@ export function useBusyInputMode({
return null
}
- return configured ?? (loaded?.owner === owner && loaded.connection === connection ? loaded.mode : null)
+ return configured ?? (loaded?.owner === owner && loaded.connection === connection && loaded.sessionId === sessionId ? loaded.mode : null)
}
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
index 2fe5f2334b3..1d6799ac60f 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx
@@ -2807,7 +2807,7 @@ describe('usePromptActions redirectPrompt', () => {
expect(await handle!.redirectPrompt('too late')).toBe(false)
})
- it('reports rejection without throwing when the redirect RPC errors', async () => {
+ it('surfaces redirect RPC errors instead of converting rejection to queue admission', async () => {
const requestGateway = vi.fn(async () => {
throw new Error('agent does not support redirect')
})
@@ -2817,7 +2817,8 @@ describe('usePromptActions redirectPrompt', () => {
<Harness onReady={h => (handle = h)} refreshSessions={async () => undefined} requestGateway={requestGateway} />
)
- expect(await handle!.redirectPrompt('boom')).toBe(false)
+ await expect(handle!.redirectPrompt('boom')).rejects.toThrow('agent does not support redirect')
+ expect($notifications.get().some(item => item.message?.includes('agent does not support redirect'))).toBe(true)
})
it('skips the RPC entirely for empty text', async () => {
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index 24c3aa1b705..821895026a3 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -843,13 +843,12 @@ export function usePromptActions({
})
return result
- } catch {
- // Swallow — caller queues the text so nothing is lost.
+ } catch (err) {
+ notifyError(err, copy.promptFailed)
+ throw err
}
-
- return false
},
- [activeSessionIdRef, appendSessionTextMessage, requestGateway, selectedStoredSessionIdRef, updateSessionState]
+ [activeSessionIdRef, appendSessionTextMessage, copy.promptFailed, requestGateway, selectedStoredSessionIdRef, updateSessionState]
)
// After a durable rewind the surviving bubbles' cached rowIds are stale (the