Skip to content

The agent that grows with you

fix(desktop): confirming a deferred combined pick resends its reasoning and the owner token

Teknium76e7a828c6b6
diff --git a/apps/desktop/src/api/canonical-live-pick.test.ts b/apps/desktop/src/api/canonical-live-pick.test.ts
index 69ef100c32f..86b3ad077b3 100644
--- a/apps/desktop/src/api/canonical-live-pick.test.ts
+++ b/apps/desktop/src/api/canonical-live-pick.test.ts
@@ -92,3 +92,12 @@ test("a guarded next-turn pick's confirmed resend carries the owner's token from
expect(calls[0].params.payload).toEqual({ model: 'guarded', provider: 'custom', confirm: 'owner-token' })
})
+
+test("a deferred pick's confirmation resends its whole selection (reasoning included) with the owner's token", async () => {
+ const { calls, client } = await attached(params => ({ session_id: 's', operation: 'model', model: (params.payload as { model: string }).model, revision: 5 }))
+
+ await client.request('session.model', { session_id: 's', payload: { model: 'guarded', provider: 'custom', reasoning: 'high' }, confirm: 'owner-token' })
+
+ expect(calls).toEqual([{ method: 'session.mutate', params: expect.objectContaining({
+ operation: 'model', payload: { model: 'guarded', provider: 'custom', reasoning: 'high', confirm: 'owner-token' }, expected_generation: 2 }) }])
+})
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index e622f2f8b71..5167ab82108 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -191,7 +191,9 @@ const POLICY_EDIT_METHODS = new Set(['slash.exec', 'config.set', 'session.worksp
const BRANCH_METHODS = new Set(['session.branch', 'session.branch_stored', 'session.branch_whole'])
// Value-setting mutations: re-applying one is harmless, so a later acknowledged edit retires it.
const VALUE_OPERATIONS = new Set(['rename', 'archive', 'model', 'reasoning', 'workspace'])
-const MUTATION_METHODS = new Set(['session.title', 'session.archive', 'session.compress', 'session.rewind', ...BRANCH_METHODS])
+// `session.model`: a whole canonical model selection (model / provider / reasoning) plus the owner's
+// token, as the owner asked it (a deferred pick's confirmation): the same request the TUI resends.
+const MUTATION_METHODS = new Set(['session.title', 'session.archive', 'session.compress', 'session.rewind', 'session.model', ...BRANCH_METHODS])
export class CanonicalDesktopProtocol {
private creates = new Map<string, string>()
@@ -355,6 +357,12 @@ export class CanonicalDesktopProtocol {
if (fenced) { return this.retainedMutation(params.session_id, params.profile, fenced.operation, fenced.payload, true) }
+ if (method === 'session.model') {
+ const payload = { ...(params.payload as Record<string, unknown>), ...(typeof params.confirm === 'string' ? { confirm: params.confirm } : {}) }
+
+ return this.retainedMutation(params.session_id, params.profile, 'model', payload, true)
+ }
+
if (method === 'session.branch_stored' || method === 'session.branch_whole') {
// The stored-parent form names the parent as `parent_session_id`; the live form as `session_id`.
const parent = params.parent_session_id ?? params.session_id
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.test.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.test.ts
index 9128ab44589..0261781709c 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.test.ts
@@ -7,9 +7,11 @@ import type { GatewayEventContext } from './types'
const notifyError = vi.hoisted(() => vi.fn())
const surfaceModelSwitchConfirm = vi.hoisted(() => vi.fn(async () => true))
+const requestGatewayForAgent = vi.hoisted(() => vi.fn(async () => ({})))
vi.mock('@/store/notifications', async importOriginal => ({ ...(await importOriginal<object>()), notifyError }))
vi.mock('@/lib/guarded-model-switch', () => ({ surfaceModelSwitchConfirm }))
+vi.mock('@/store/gateway', async importOriginal => ({ ...(await importOriginal<object>()), requestGatewayForAgent }))
function context(payload: Record<string, unknown>, overrides: Record<string, unknown> = {}) {
const states: Record<string, Record<string, unknown>> = { s: { model: 'picked', provider: 'custom' } }
@@ -109,4 +111,20 @@ describe('handleDeferredPickEvent', () => {
expect(states.s).toMatchObject({ model: 'picked', provider: 'custom' })
expect((ctx.deps.queryClient.invalidateQueries as any).mock.calls[0][0].queryKey).toContain('other')
})
+
+ it('confirming a combined model + reasoning pick resends the whole selection with the owner token', async () => {
+ surfaceModelSwitchConfirm.mockImplementationOnce((async (options: { requestConfirmed: () => Promise<unknown> }) => {
+ await options.requestConfirmed()
+
+ return true
+ }) as never)
+
+ const { ctx } = context({ confirm: 'token', operation: 'model', payload: { model: 'guarded', provider: 'custom', reasoning: 'high' },
+ session_id: 's', status: 'confirmation_required', target_model: 'guarded' })
+
+ handleDeferredPickEvent(ctx)
+ await vi.waitFor(() => expect(requestGatewayForAgent).toHaveBeenCalled())
+ expect(requestGatewayForAgent).toHaveBeenCalledWith(null, 'default', 'session.model', {
+ confirm: 'token', payload: { model: 'guarded', provider: 'custom', reasoning: 'high' }, session_id: 's' })
+ })
})
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.ts
index bd05260234f..0f607957a66 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/deferred-pick.ts
@@ -61,7 +61,7 @@ export function handleDeferredPickEvent(ctx: GatewayEventContext): boolean {
}
if (update.status === 'confirmation_required' && typeof update.confirm === 'string') {
- const pick = (update.payload ?? {}) as { model?: string; provider?: string }
+ const pick = (update.payload ?? {}) as { model?: string; provider?: string; reasoning?: string }
const connectionId = event.connectionId ?? activeGatewayConnectionId()
const before = { model: $currentModel.get(), provider: $currentProvider.get() }
const target = String(update.target_model ?? pick.model ?? '')
@@ -73,16 +73,10 @@ export function handleDeferredPickEvent(ctx: GatewayEventContext): boolean {
// A newer pick replaced this one while the dialog was open: confirming it would undo that pick.
isStale: () => $currentModel.get() !== before.model || $currentProvider.get() !== before.provider,
model: target,
- // The protocol attaches the owner's token (kept from this event) to the same target's resend.
+ // The whole selection the owner asked about (its reasoning level included) with the owner's
+ // token: the same canonical model mutation the TUI resends.
requestConfirmed: () =>
- requestGatewayForAgent(
- connectionId,
- profile,
- pick.provider ? 'config.set' : 'slash.exec',
- pick.provider
- ? { session_id: sessionId, key: 'model', value: `${pick.model} --provider ${pick.provider} --session`, confirm_expensive_model: true }
- : { session_id: sessionId, command: `/model ${pick.model}`, confirm_expensive_model: true }
- ),
+ requestGatewayForAgent(connectionId, profile, 'session.model', { confirm: update.confirm, payload: pick, session_id: sessionId }),
finish: invalidate
}).then(applied => {
// Declined, or the confirmed resend failed: the committed model is still the session's.