Skip to content

The agent that grows with you

fix(desktop): local new chats with a workspace, effort or speed reach the owner's create contract

Tekniumf2b59754569e
diff --git a/apps/desktop/src/api/canonical-create-options.test.ts b/apps/desktop/src/api/canonical-create-options.test.ts
new file mode 100644
index 00000000000..da1164833c7
--- /dev/null
+++ b/apps/desktop/src/api/canonical-create-options.test.ts
@@ -0,0 +1,66 @@
+// @vitest-environment node
+import { readFileSync } from 'node:fs'
+import { join } from 'node:path'
+
+import { expect, test } from 'vitest'
+
+import { HermesGateway } from './client'
+
+// B09 / C-hx-1: a local Desktop new chat sends `desktopSessionCreateParams` (use-session-actions)
+// verbatim. The fixture owner refuses exactly what the generated canonical contract refuses, so a
+// frame that reaches it as `session.create` is one the real owner admits.
+const doc = JSON.parse(readFileSync(join(process.cwd(), '..', 'shared', 'src', 'gateway-contract.openrpc.json'), 'utf8'))
+const createRef = doc['x-canonical-methods'].find((m: { name: string }) => m.name === 'session.create').params[0].schema.$ref
+const CREATE_KEYS = new Set(Object.keys(doc.components.schemas[createRef.split('/').at(-1)].properties))
+
+const BASE = { cols: 96, source: 'desktop', profile: 'default' }
+
+// [label, desktopSessionCreateParams output, expected owner frame subset | named refusal]
+const CASES: Array<[string, Record<string, unknown>, Record<string, unknown> | RegExp]> = [
+ ['inherited workspace', { ...BASE, cwd: '/w', cwd_explicit: false, fast: false }, { cwd: '/w' }],
+ ['picked workspace', { ...BASE, cwd: '/w', cwd_explicit: true, fast: false }, { cwd: '/w' }],
+ ['composer effort', { ...BASE, cwd: '/w', cwd_explicit: false, reasoning_effort: 'high', fast: false }, { cwd: '/w', reasoning: 'high' }],
+ ['onboarding minimal effort', { ...BASE, reasoning_effort: 'minimal', fast: false, title: 'Welcome' }, { reasoning: 'minimal', title: 'Welcome' }],
+ ['Fast speed', { ...BASE, cwd: '/w', cwd_explicit: false, fast: true }, /fast speed is not available on the shared gateway yet/],
+ ['Ultrafast speed', { ...BASE, fast: true, service_tier: 'ultrafast' }, /ultrafast speed is not available on the shared gateway yet/]
+]
+
+test.each(CASES)('%s: the Desktop create frame is admitted by the owner contract or refused by name', async (_label, params, expected) => {
+ const wsPackage = 'ws'
+ const { WebSocketServer } = await import(wsPackage)
+ const server = new WebSocketServer({ host: '127.0.0.1', port: 0 })
+ await new Promise<void>(resolve => server.once('listening', resolve))
+ const sent: any[] = []
+ server.on('connection', (socket: any) => {
+ socket.on('message', (bytes: Buffer) => {
+ const frame = JSON.parse(bytes.toString())
+ sent.push(frame)
+ const fields = Object.keys(frame.params).filter(key => !CREATE_KEYS.has(key))
+
+ socket.send(JSON.stringify(fields.length
+ ? { jsonrpc: '2.0', id: frame.id, error: { code: 4001, message: 'invalid_params', data: { reason: 'invalid_params', fields } } }
+ : { jsonrpc: '2.0', id: frame.id, result: { session_id: 's', stored_session_id: 's', revision: 1, execution_generation: 0, messages: [], prompts: [] } }))
+ })
+ })
+ const client = new HermesGateway()
+
+ try {
+ const address = server.address() as { port: number }
+ await client.connect(`ws://127.0.0.1:${address.port}/api/ws?native_dial=fixture&ticket=one-use`)
+ const call = client.request('session.create', { ...params, request_id: 'intent' })
+
+ if (expected instanceof RegExp) {
+ await expect(call).rejects.toThrow(expected)
+ expect(sent).toEqual([])
+ } else {
+ await expect(call).resolves.toMatchObject({ session_id: 's' })
+ expect(sent.map(frame => frame.method)).toEqual(['session.create'])
+ expect(sent[0].params).toMatchObject({ ...expected, source: 'gui', request_id: 'intent' })
+ }
+ } finally {
+ client.close()
+
+ for (const socket of server.clients) { socket.terminate() }
+ await new Promise<void>(resolve => server.close(() => resolve()))
+ }
+})
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 71fe5a9bdec..ec3bb771c95 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -357,14 +357,25 @@ export class CanonicalDesktopProtocol {
if (this.modelConfirmations.get(key)?.token === payload.confirm) { this.modelConfirmations.delete(key) }
}
- private prepareCreate(params: Record<string, unknown>): Record<string, unknown> {
- const allowed = new Set(['request_id', 'source', 'cwd', 'model', 'toolsets', 'profile', 'cols', 'title', 'hidden', 'follow_profile_config'])
- const unsupported = Object.keys(params).filter(key => !allowed.has(key) && !(key === 'fast' && params[key] === false))
+ // The composer's per-session create options in the owner's create contract: the effort is the
+ // frozen launch `reasoning`; `cwd_explicit` is provenance the owner has no field for (it freezes
+ // `cwd` as given). No owner field carries a speed tier, so a non-normal pick is refused by name
+ // rather than silently running the chat at the profile's tier.
+ private prepareCreate(input: Record<string, unknown>): Record<string, unknown> {
+ const { reasoning_effort: effort, cwd_explicit: _provenance, fast, service_tier: tier, ...params } = input
+
+ if (fast === true || (typeof tier === 'string' && tier && tier !== 'normal')) {
+ throw new Error(`Starting a chat at ${tier === 'ultrafast' ? 'ultrafast' : 'fast'} speed is not available on the shared gateway yet; switch speed to Normal`)
+ }
+
+ if (typeof effort === 'string' && effort) { params.reasoning = effort }
+ const allowed = new Set(['request_id', 'source', 'cwd', 'model', 'toolsets', 'reasoning', 'profile', 'cols', 'title', 'hidden', 'follow_profile_config'])
+ const unsupported = Object.keys(params).filter(key => !allowed.has(key))
if (unsupported.length) { throw new Error(`Canonical gateway does not support explicit session options: ${unsupported.join(', ')}`) }
const result = Object.fromEntries(Object.entries(params).filter(([key, value]) =>
- ['request_id', 'cwd', 'model', 'toolsets', 'title', 'hidden'].includes(key) || (key === 'profile' && siblingRoute(value))))
+ ['request_id', 'cwd', 'model', 'toolsets', 'reasoning', 'title', 'hidden'].includes(key) || (key === 'profile' && siblingRoute(value))))
const key = JSON.stringify(result)
const requestId = params.request_id ?? this.creates.get(key) ?? crypto.randomUUID()