Skip to content

The agent that grows with you

fix(gateway): keep a deferred level across a direct model pick; show it in Desktop

unsupportedpastelsaa41a33223b3
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-reasoning.test.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-reasoning.test.ts
new file mode 100644
index 00000000000..b0929c42f7f
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash-reasoning.test.ts
@@ -0,0 +1,48 @@
+import { act, cleanup, renderHook } from '@testing-library/react'
+import { afterEach, expect, it, vi } from 'vitest'
+
+import { en } from '@/i18n/en'
+import { createClientSessionState } from '@/lib/chat-runtime'
+
+import { useSlashCommand } from './slash'
+import type { GatewayRequest } from './utils'
+
+afterEach(cleanup)
+
+it('says a /reasoning level typed while a reply streams applies from the next message', async () => {
+ const appendSessionTextMessage = vi.fn()
+
+ const { result } = renderHook(() =>
+ useSlashCommand({
+ activeSessionIdRef: { current: 'runtime-a' },
+ selectedStoredSessionIdRef: { current: 'stored-a' },
+ busyRef: { current: true },
+ copy: en.desktop,
+ createBackendSessionForSend: async () => null,
+ getRoutedStoredSessionId: () => 'stored-a',
+ getRuntimeIdForStoredSession: () => 'runtime-a',
+ requestGateway: vi.fn(async () => ({ status: 'deferred', value: 'high' })) as GatewayRequest,
+ resumeStoredSession: vi.fn(),
+ updateSessionState: vi.fn((_sid, updater) => updater(createClientSessionState())),
+ submitPromptText: vi.fn(async () => true),
+ appendSessionTextMessage,
+ branchCurrentSession: async () => true,
+ handleSkinCommand: () => '',
+ handoffSession: async () => ({ ok: true }),
+ openMemoryGraph: vi.fn(),
+ refreshSessions: async () => undefined,
+ startFreshSessionDraft: vi.fn()
+ })
+ )
+
+ await act(async () => {
+ await result.current('/reasoning high')
+ })
+
+ expect(appendSessionTextMessage).toHaveBeenCalledWith(
+ 'runtime-a',
+ 'system',
+ expect.stringContaining('high applies from your next message'),
+ 'stored-a'
+ )
+})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
index dad675337e2..ddd5c8f29c2 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
@@ -7,6 +7,7 @@ import { prepareDefaultNewSession } from '@/app/session/new-session-route'
import { invalidateContextBreakdown } from '@/app/shell/hooks/use-context-breakdown'
import { getProfiles } from '@/hermes'
import type { Translations } from '@/i18n'
+import { translateNow } from '@/i18n/runtime'
import { type ChatMessage, toChatMessages } from '@/lib/chat-messages'
import { sessionTitle } from '@/lib/chat-runtime'
import {
@@ -1022,10 +1023,16 @@ export function useSlashCommand(deps: SlashCommandDeps) {
return
}
- const result = await requestGateway<{ value?: string }>('config.set', params)
+ const result = await requestGateway<{ status?: string; value?: string }>('config.set', params)
+ const value = result.value || params.value
applyReasoningSlashResult(result.value)
- renderSlashOutput(`reasoning: ${result.value || params.value}`)
+ // Typed while a reply streams: the owner applies the level from the next turn.
+ renderSlashOutput(
+ result.status === 'deferred'
+ ? translateNow('shell.modelMenu.pickDeferred', value)
+ : `reasoning: ${value}`
+ )
} catch (err) {
if (isMissingRpcMethod(err)) {
await runExec(ctx)
diff --git a/apps/desktop/src/lib/reasoning-step.test.ts b/apps/desktop/src/lib/reasoning-step.test.ts
index 39a83dd75ce..a334925698f 100644
--- a/apps/desktop/src/lib/reasoning-step.test.ts
+++ b/apps/desktop/src/lib/reasoning-step.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import { stepReasoningEffort, writeSessionReasoningEffort } from '@/lib/reasoning-step'
+import { $notifications } from '@/store/notifications'
describe('stepReasoningEffort', () => {
it('steps up and down through off and the normal levels, clamping at both ends', () => {
@@ -54,4 +55,15 @@ describe('writeSessionReasoningEffort', () => {
// rather than passing the stale string back into the store.
await expect(writeSessionReasoningEffort(request, 'runtime-1', 'high')).resolves.toBe('medium')
})
+
+ it('says a level stepped while a reply streams applies from the next message', async () => {
+ $notifications.set([])
+ const request = vi.fn().mockResolvedValue({ status: 'deferred', value: 'high' })
+
+ await expect(writeSessionReasoningEffort(request, 'runtime-1', 'high')).resolves.toBe('high')
+
+ expect($notifications.get()).toContainEqual(
+ expect.objectContaining({ kind: 'info', message: 'high applies from your next message' })
+ )
+ })
})
diff --git a/apps/desktop/src/lib/reasoning-step.ts b/apps/desktop/src/lib/reasoning-step.ts
index 677da2f2f43..bf8e7fd8123 100644
--- a/apps/desktop/src/lib/reasoning-step.ts
+++ b/apps/desktop/src/lib/reasoning-step.ts
@@ -1,6 +1,8 @@
import { DEFAULT_REASONING_EFFORT, isReasoningEffort } from '@hermes/shared'
+import { translateNow } from '@/i18n/runtime'
import { normalize } from '@/lib/text'
+import { notify } from '@/store/notifications'
/** The level ladder a shortcut steps through: thinking off, then the normal
* levels in ascending order. `max`/`ultra` are deliberately absent — they are
@@ -63,11 +65,20 @@ export async function writeSessionReasoningEffort(
sessionId: string,
effort: ReasoningStepLevel | 'max' | 'ultra'
): Promise<ReasoningStepLevel> {
- const result = await request<{ value?: unknown }>('config.set', {
+ const result = await request<{ status?: unknown; value?: unknown }>('config.set', {
key: 'reasoning',
session_id: sessionId,
value: effort
})
+ // A level picked while a reply streams is the owner's for the next turn, like a model menu pick.
+ if (result?.status === 'deferred') {
+ notify({
+ id: `model-pick-deferred-${sessionId}`,
+ kind: 'info',
+ message: translateNow('shell.modelMenu.pickDeferred', effort)
+ })
+ }
+
return normalizeReasoningStepLevel(typeof result?.value === 'string' ? result.value : effort)
}
diff --git a/hermes_state_mutation_prepared.py b/hermes_state_mutation_prepared.py
index 089c2c64bba..5638ed213f7 100644
--- a/hermes_state_mutation_prepared.py
+++ b/hermes_state_mutation_prepared.py
@@ -53,8 +53,15 @@ def model_in_transaction(db, conn, session_id, payload, prepared):
model_config = json.loads(row[0] or '{}')
model_config['provider'] = config['provider']
conn.execute('UPDATE sessions SET model=?,model_config=? WHERE id=?', (model, _json(model_config), target))
- # This selection is newer than any pick still waiting for a turn boundary: that pick is superseded.
- conn.execute('DELETE FROM state_meta WHERE key=?', (DEFERRED_PICK_PREFIX + session_id,))
+ # This selection is newer than any model pick still waiting for a turn boundary: that pick is superseded.
+ # A level deferred beside it stays unless this selection carries its own, as defer_pick keeps it.
+ key = DEFERRED_PICK_PREFIX + session_id
+ row = conn.execute('SELECT value FROM state_meta WHERE key=?', (key,)).fetchone()
+ level = json.loads(row[0]).get('reasoning') if row is not None and 'reasoning' not in payload else None
+ if level is None:
+ conn.execute('DELETE FROM state_meta WHERE key=?', (key,))
+ else:
+ conn.execute('UPDATE state_meta SET value=? WHERE key=?', (_json({'reasoning': level}), key))
return {session_id, target}, {'model': model, 'provider': config['provider'], 'execution_generation': generation,
**({'effort': payload['reasoning']} if 'reasoning' in payload else {})}
diff --git a/tests/gateway/test_session_deferred_reasoning.py b/tests/gateway/test_session_deferred_reasoning.py
index b4c235d5fb1..dae576b40db 100644
--- a/tests/gateway/test_session_deferred_reasoning.py
+++ b/tests/gateway/test_session_deferred_reasoning.py
@@ -52,7 +52,12 @@ async def test_levels_picked_during_a_running_turn_apply_to_the_next_turn_in_pic
@pytest.mark.asyncio
-async def test_a_level_committed_after_the_turn_settles_supersedes_the_deferred_one(tmp_path, monkeypatch):
[email protected]('operation, payload, effort', [
+ ('reasoning', {'effort': 'high'}, 'high'),
+ ('model', {'model': 'picked'}, 'low'), # a model picked without a level keeps the deferred one
+], ids=['level', 'model'])
+async def test_a_pick_committed_after_the_turn_settles_supersedes_only_what_it_picks(
+ tmp_path, monkeypatch, operation, payload, effort):
from gateway import session_deferred_pick
from gateway.session_contract import Submission
from gateway.session_policy import policy_for_source
@@ -62,19 +67,18 @@ async def test_a_level_committed_after_the_turn_settles_supersedes_the_deferred_
async def handle(event):
live = state['authority'].sessions[state['ref'].session_id]
- seen.append((event.text, (policy_for_source(state['authority'].runner, live.source).reasoning_config
- or {}).get('effort')))
+ effort = (policy_for_source(state['authority'].runner, live.source).reasoning_config or {}).get('effort')
+ seen.append((event.text, _running_model(state['authority'], state['ref']), effort))
if event.text == 'RUNNING':
started.set()
await release.wait()
return 'ok'
- async def pick(n, effort):
+ async def pick(n, operation, payload):
snap = db.get_session(ref.session_id)
reply = await owner.dispatch({'id': n, 'method': 'session.mutate', 'params': {
'session_id': ref.session_id, 'request_id': f'pick-{n}', 'expected_revision': snap['runtime_revision'],
- 'expected_generation': snap['runtime_generation'], 'operation': 'reasoning',
- 'payload': {'effort': effort}}})
+ 'expected_generation': snap['runtime_generation'], 'operation': operation, 'payload': payload}})
return reply['result'].get('status')
store, db, authority, owner, ref = _owner(tmp_path, monkeypatch, handle)
state.update(authority=authority, ref=ref)
@@ -83,17 +87,17 @@ async def test_a_level_committed_after_the_turn_settles_supersedes_the_deferred_
try:
first = await authority.submit(owner.actor, Submission('running', ref, {'text': 'RUNNING'}, 'queue'))
await async_wait_until(started.is_set, timeout=10, what='the first turn is running')
- assert await pick(0, 'low') == 'deferred'
+ assert await pick(0, 'reasoning', {'effort': 'low'}) == 'deferred'
# The newer pick takes the mutation lock before the drain reaches the boundary.
monkeypatch.setattr(session_deferred_pick, 'apply_deferred_pick', lambda *a: asyncio.sleep(0))
release.set()
await async_wait_until(
lambda: rt.get_session_admission(db, admission_id=first.admission_id)['status'] == 'terminal',
timeout=10, what='the running turn settled')
- assert await pick(1, 'high') is None # committed now, not deferred
+ assert await pick(1, operation, payload) is None # committed now, not deferred
monkeypatch.setattr(session_deferred_pick, 'apply_deferred_pick', apply)
await _run_turn(authority, owner, db, ref, 'NEXT')
- assert seen[-1] == ('NEXT', 'high'), seen
+ assert seen[-1] == ('NEXT', payload.get('model', seen[0][1]), effort), seen
finally:
release.set()
await owner.close()