Skip to content

The agent that grows with you

fix: route Desktop /model, /branch and branch action through canonical session.mutate

Teknium22a5028eaceb
diff --git a/apps/desktop/src/api/canonical-protocol.test.ts b/apps/desktop/src/api/canonical-protocol.test.ts
index 6f68eb19460..46b8eb48201 100644
--- a/apps/desktop/src/api/canonical-protocol.test.ts
+++ b/apps/desktop/src/api/canonical-protocol.test.ts
@@ -55,3 +55,21 @@ test('pending resume and live updates share the existing queue projection', () =
protocol.event(cleared)
expect(cleared.payload).toHaveProperty('pending_submissions', [])
})
+
+test('composer branch and model switch become canonical prepared mutations with identity retained', () => {
+ const protocol = new CanonicalDesktopProtocol()
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 4, execution_generation: 9 })
+ const branch = protocol.prepare('session.branch', { session_id: 's' })
+ expect(protocol.wire('session.branch')).toBe('session.mutate')
+ expect(branch).toEqual({ session_id: 's', request_id: expect.any(String), expected_revision: 4, expected_generation: 9, operation: 'branch', payload: {} })
+ expect(protocol.prepare('session.branch', { session_id: 's' })).toEqual(branch)
+ const branched = protocol.result('session.branch', branch, { session_id: 's', revision: 5, operation: 'branch', branched_session_id: 'child', copied_messages: 6 })
+ expect(branched).toMatchObject({ session_id: 'child', stored_session_id: 'child', parent_session_id: 's' })
+ expect(protocol.prepare('session.branch', { session_id: 's' }).request_id).not.toBe(branch.request_id)
+
+ const model = protocol.prepare('slash.exec', { session_id: 's', command: 'model switched' })
+ expect(protocol.wire('slash.exec', model)).toBe('session.mutate')
+ expect(model).toMatchObject({ session_id: 's', expected_revision: 5, operation: 'model', payload: { model: 'switched' } })
+ expect(protocol.result('slash.exec', model, { session_id: 's', revision: 6, operation: 'model', model: 'switched', provider: 'custom' })).toMatchObject({ type: 'exec', output: expect.stringContaining('switched') })
+ expect(protocol.wire('slash.exec', protocol.prepare('slash.exec', { session_id: 's', command: 'help' }))).toBe('slash.exec')
+})
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 4e52ae9ec14..5ea66d79182 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -1,5 +1,35 @@
// Canonical local authority wire adapter. Remote legacy transports keep their
// existing protocol; unsupported explicit semantics fail before network admission.
+
+// Mirrors hermes_cli/gateway_mutations.slash_mutation: the typed directives
+// that are canonical mutations, not gateway-executed slash commands. Model
+// flags (--global/--once/--refresh) have no canonical mutation and stay on the
+// exec path so the authority refuses them explicitly.
+export function slashMutation(command: string): { operation: string; payload: Record<string, unknown> } | null {
+ const [name, ...rest] = command.trim().replace(/^\/+/, '').split(/\s+/)
+ const arg = rest.join(' ').trim()
+
+ if (name === 'model') {
+ if (!arg || arg.startsWith('-') || /(^|\s)--/.test(arg)) { return null }
+ const [model, ...flags] = arg.split(/\s+/)
+
+ return flags.length ? null : { operation: 'model', payload: { model } }
+ }
+ const field = ({ branch: 'title', compress: 'focus' } as Record<string, string>)[name]
+
+ if (!field) { return null }
+
+ return { operation: name, payload: arg ? { [field]: arg } : {} }
+}
+
+function mutationSummary(operation: string, value: Record<string, unknown>): string {
+ if (operation === 'model') { return `model: ${value.model}${value.provider ? ` (${value.provider})` : ''}` }
+ if (operation === 'branch') { return `branch: ${value.branched_session_id}` }
+ if (operation === 'compress') { return `compress: ${value.target_session_id ?? value.session_id}` }
+
+ return `${operation}: ok`
+}
+
export class CanonicalDesktopProtocol {
private creates = new Map<string, string>()
private revisions = new Map<string, number>()
@@ -15,24 +45,46 @@ export class CanonicalDesktopProtocol {
for (const [key, mutation] of this.mutations) { if (mutation.request_id === params.request_id) { this.mutations.delete(key) } }
}
- prepare(method: string, params: Record<string, unknown>): Record<string, unknown> {
- const field = ({ 'session.title': 'title', 'session.archive': 'archived' } as Record<string, string>)[method]
+ // Wire method for a prepared request: composer metadata, branch and the
+ // typed `/model <name>` / `/branch [title]` / `/compress [focus]` directives
+ // all travel as canonical `session.mutate`; everything else keeps its name.
+ wire(method: string, prepared: Record<string, unknown> = {}): string {
+ if (method === 'session.title' || method === 'session.archive' || method === 'session.branch') { return 'session.mutate' }
- if (field) {
- const key = JSON.stringify([params.session_id, field, params[field]])
- const retained = this.mutations.get(key)
+ return method === 'slash.exec' && typeof prepared.operation === 'string' ? 'session.mutate' : method
+ }
- if (retained) { return retained }
- const revision = this.revisions.get(String(params.session_id))
+ private retainedMutation(sessionId: unknown, operation: string, payload: Record<string, unknown>, withGeneration: boolean): Record<string, unknown> {
+ const key = JSON.stringify([sessionId, operation, payload])
+ const retained = this.mutations.get(key)
- if (revision === undefined) { throw new Error('Session revision unavailable; reopen the session before editing metadata') }
+ if (retained) { return retained }
+ const revision = this.revisions.get(String(sessionId))
- const mutation = { session_id: params.session_id, request_id: crypto.randomUUID(), expected_revision: revision,
- operation: field === 'title' ? 'rename' : 'archive', payload: { [field]: params[field] } }
+ if (revision === undefined) { throw new Error('Session revision unavailable; reopen the session before editing metadata') }
+ const generation = this.generations.get(String(sessionId))
- this.mutations.set(key, mutation)
+ if (withGeneration && generation === undefined) { throw new Error('Session execution identity unavailable; reconnect before this command') }
- return mutation
+ const mutation: Record<string, unknown> = { session_id: sessionId, request_id: crypto.randomUUID(), expected_revision: revision,
+ ...(withGeneration ? { expected_generation: generation } : {}), operation, payload }
+
+ this.mutations.set(key, mutation)
+
+ return mutation
+ }
+
+ prepare(method: string, params: Record<string, unknown>): Record<string, unknown> {
+ const field = ({ 'session.title': 'title', 'session.archive': 'archived' } as Record<string, string>)[method]
+
+ if (field) { return this.retainedMutation(params.session_id, field === 'title' ? 'rename' : 'archive', { [field]: params[field] }, false) }
+
+ if (method === 'session.branch') { return this.retainedMutation(params.session_id, 'branch', {}, true) }
+
+ if (method === 'slash.exec') {
+ const directive = slashMutation(String(params.command ?? ''))
+
+ if (directive) { return this.retainedMutation(params.session_id, directive.operation, directive.payload, true) }
}
if (method === 'session.create') {
@@ -109,11 +161,17 @@ export class CanonicalDesktopProtocol {
this.revisions.set(value.session_id, value.revision)
}
- if (method === 'session.title' || method === 'session.archive') {
+ if (method === 'session.title' || method === 'session.archive' || method === 'session.branch' || (method === 'slash.exec' && typeof params.operation === 'string')) {
if (value.session_id !== params.session_id) { throw new Error('Metadata receipt destination mismatch') }
for (const [key, mutation] of this.mutations) { if (mutation.request_id === params.request_id) { this.mutations.delete(key) } }
+ if (method === 'session.branch') {
+ return { ...value, session_id: value.branched_session_id, stored_session_id: value.branched_session_id, parent_session_id: params.session_id, message_count: value.copied_messages }
+ }
+
+ if (method === 'slash.exec') { return { type: 'exec', output: mutationSummary(params.operation as string, value) } }
+
return { ...value, ok: true }
}
diff --git a/apps/desktop/src/api/client.ts b/apps/desktop/src/api/client.ts
index d00a0fe46ae..09d1ca89ffc 100644
--- a/apps/desktop/src/api/client.ts
+++ b/apps/desktop/src/api/client.ts
@@ -49,7 +49,7 @@ export class HermesGateway extends JsonRpcGatewayClient {
override async request<T>(method: string, params: Record<string, unknown> = {}, timeoutMs?: number, signal?: AbortSignal): Promise<T> {
if (!this.canonical) { return super.request<T>(method, params, timeoutMs, signal) }
const prepared = this.protocol.prepare(method, params)
- const wireMethod = method === 'session.title' || method === 'session.archive' ? 'session.mutate' : method
+ const wireMethod = this.protocol.wire(method, prepared)
try {
const result = await super.request<T>(wireMethod, prepared, timeoutMs, signal)