Skip to content

The agent that grows with you

feat(desktop): step reasoning effort through a session-scoped ladder

Brooklyn Nicholsonad799e37fba8
diff --git a/apps/desktop/src/lib/reasoning-step.test.ts b/apps/desktop/src/lib/reasoning-step.test.ts
new file mode 100644
index 00000000000..39a83dd75ce
--- /dev/null
+++ b/apps/desktop/src/lib/reasoning-step.test.ts
@@ -0,0 +1,57 @@
+import { describe, expect, it, vi } from 'vitest'
+
+import { stepReasoningEffort, writeSessionReasoningEffort } from '@/lib/reasoning-step'
+
+describe('stepReasoningEffort', () => {
+ it('steps up and down through off and the normal levels, clamping at both ends', () => {
+ expect(stepReasoningEffort('none', 1)).toBe('minimal')
+ expect(stepReasoningEffort('minimal', 1)).toBe('low')
+ expect(stepReasoningEffort('low', 1)).toBe('medium')
+ expect(stepReasoningEffort('medium', 1)).toBe('high')
+ expect(stepReasoningEffort('high', 1)).toBe('xhigh')
+ expect(stepReasoningEffort('xhigh', 1)).toBe('xhigh')
+
+ expect(stepReasoningEffort('xhigh', -1)).toBe('high')
+ expect(stepReasoningEffort('high', -1)).toBe('medium')
+ expect(stepReasoningEffort('medium', -1)).toBe('low')
+ expect(stepReasoningEffort('low', -1)).toBe('minimal')
+ expect(stepReasoningEffort('minimal', -1)).toBe('none')
+ expect(stepReasoningEffort('none', -1)).toBe('none')
+ })
+
+ it('never steps into the expensive tiers (max/ultra stay behind the menu)', () => {
+ // From an expensive tier, stepping down rejoins the ladder at xhigh; up is
+ // a no-op (callers compare and skip the RPC).
+ expect(stepReasoningEffort('max', -1)).toBe('xhigh')
+ expect(stepReasoningEffort('ultra', -1)).toBe('xhigh')
+ expect(stepReasoningEffort('max', 1)).toBe('max')
+ // The ladder's top clamps — stepping up from xhigh never crosses into max.
+ expect(stepReasoningEffort('xhigh', 1)).toBe('xhigh')
+ })
+
+ it('resolves an unset or stale value through the fallback before stepping', () => {
+ expect(stepReasoningEffort('', 1, 'low')).toBe('medium')
+ expect(stepReasoningEffort('', -1, 'low')).toBe('minimal')
+ // An unrecognized value falls back to Hermes' own default (medium).
+ expect(stepReasoningEffort('banana', 1, 'high')).toBe('high')
+ expect(stepReasoningEffort('', 1)).toBe('high')
+ })
+})
+
+describe('writeSessionReasoningEffort', () => {
+ it('writes the session-scoped reasoning RPC and reports the gateway value', async () => {
+ const request = vi.fn().mockResolvedValue({ value: 'xhigh' })
+
+ await expect(writeSessionReasoningEffort(request, 'runtime-1', 'xhigh')).resolves.toBe('xhigh')
+
+ expect(request).toHaveBeenCalledWith('config.set', { key: 'reasoning', session_id: 'runtime-1', value: 'xhigh' })
+ })
+
+ it('normalizes a gateway reply that is not a step level', async () => {
+ const request = vi.fn().mockResolvedValue({ value: 'banana' })
+
+ // The gateway echoed something unrecognized: settle on the default level
+ // rather than passing the stale string back into the store.
+ await expect(writeSessionReasoningEffort(request, 'runtime-1', 'high')).resolves.toBe('medium')
+ })
+})
diff --git a/apps/desktop/src/lib/reasoning-step.ts b/apps/desktop/src/lib/reasoning-step.ts
new file mode 100644
index 00000000000..39d02beb305
--- /dev/null
+++ b/apps/desktop/src/lib/reasoning-step.ts
@@ -0,0 +1,66 @@
+import { DEFAULT_REASONING_EFFORT, isReasoningEffort } from '@hermes/shared'
+
+import { normalize } from '@/lib/text'
+
+/** The level ladder a shortcut steps through: thinking off, then the normal
+ * levels in ascending order. `max`/`ultra` are deliberately absent — they are
+ * expensive tiers kept behind an explicit menu pick, so stepping must not
+ * cross into them (#71627). */
+export const REASONING_STEP_LEVELS = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh'] as const
+
+export type ReasoningStepLevel = (typeof REASONING_STEP_LEVELS)[number]
+
+export type ReasoningStepDirection = -1 | 1
+
+/** Resolve what a step starts from: `none` (thinking off), a real level, or
+ * the fallback for an unset/unknown value. */
+const DEFAULT_STEP_LEVEL = DEFAULT_REASONING_EFFORT as ReasoningStepLevel
+
+export function normalizeReasoningStepLevel(effort: string, fallback: string = DEFAULT_REASONING_EFFORT): ReasoningStepLevel {
+ const value = normalize(effort || fallback)
+
+ if (value === 'none') {
+ return 'none'
+ }
+
+ return isReasoningEffort(value) && REASONING_STEP_LEVELS.includes(value as ReasoningStepLevel)
+ ? (value as ReasoningStepLevel)
+ : DEFAULT_STEP_LEVEL
+}
+
+/** One notch up/down the ladder, clamped at both ends — never wraps, never
+ * reaches past `xhigh` into `max`/`ultra`. The expensive tiers sit ABOVE the
+ * ladder: stepping down rejoins it at `xhigh`; stepping up has nowhere to go,
+ * so the current value is returned unchanged (callers treat that as a no-op). */
+export function stepReasoningEffort(
+ effort: string,
+ direction: ReasoningStepDirection,
+ fallback: string = DEFAULT_REASONING_EFFORT
+): ReasoningStepLevel | 'max' | 'ultra' {
+ const value = normalize(effort || fallback)
+
+ if (value === 'max' || value === 'ultra') {
+ return direction === -1 ? 'xhigh' : value
+ }
+
+ const current = normalizeReasoningStepLevel(value)
+ const index = REASONING_STEP_LEVELS.indexOf(current)
+ const next = Math.min(Math.max(index + direction, 0), REASONING_STEP_LEVELS.length - 1)
+
+ return REASONING_STEP_LEVELS[next]
+}
+
+type RequestGateway = <T>(method: string, params?: Record<string, unknown>) => Promise<T>
+
+/** Push a stepped level onto the session (session-scoped `config.set`, same
+ * RPC the model menu uses). Returns the gateway-reported value so the caller
+ * can settle its optimistic write on the truth, not the guess. */
+export async function writeSessionReasoningEffort(
+ request: RequestGateway,
+ sessionId: string,
+ effort: ReasoningStepLevel | 'max' | 'ultra'
+): Promise<ReasoningStepLevel> {
+ const result = await request<{ value?: unknown }>('config.set', { key: 'reasoning', session_id: sessionId, value: effort })
+
+ return normalizeReasoningStepLevel(typeof result?.value === 'string' ? result.value : effort)
+}