Skip to content

The agent that grows with you

fix: fence canonical desktop RPC metadata edits with revision receipts

Tekniumce3037dc1d40
diff --git a/apps/desktop/src/api/canonical-protocol.test.ts b/apps/desktop/src/api/canonical-protocol.test.ts
index 816d9c1d985..251d16f6abe 100644
--- a/apps/desktop/src/api/canonical-protocol.test.ts
+++ b/apps/desktop/src/api/canonical-protocol.test.ts
@@ -2,6 +2,21 @@ import { expect, test } from 'vitest'
import { CanonicalDesktopProtocol } from './canonical-protocol'
+test('metadata writes retain the original CAS revision and request identity until acknowledgement', () => {
+ const protocol = new CanonicalDesktopProtocol()
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 7 })
+ const first = protocol.prepare('session.title', { session_id: 's', title: 'new title' })
+ expect(first).toEqual({ session_id: 's', request_id: expect.any(String), expected_revision: 7, operation: 'rename', payload: { title: 'new title' } })
+ protocol.result('session.resume', { session_id: 's' }, { session_id: 's', revision: 8 })
+ expect(protocol.prepare('session.title', { session_id: 's', title: 'new title' })).toEqual(first)
+ protocol.failure(first, { data: { reason: 'revision_conflict' } })
+ const retry = protocol.prepare('session.title', { session_id: 's', title: 'new title' })
+ expect(retry.expected_revision).toBe(8)
+ expect(retry.request_id).not.toBe(first.request_id)
+ protocol.result('session.title', first, { session_id: 's', revision: 8, title: 'new title' })
+ expect(protocol.prepare('session.archive', { session_id: 's', archived: true })).toMatchObject({ expected_revision: 8, operation: 'archive', payload: { archived: true } })
+})
+
test('canonical create retries retain identity and reject unsupported explicit intent', () => {
const protocol = new CanonicalDesktopProtocol()
const params = { source: 'desktop', profile: 'default', cols: 96, fast: false, cwd: '/tmp' }
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index ffe5a9a6251..851cda560b5 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -2,10 +2,39 @@
// existing protocol; unsupported explicit semantics fail before network admission.
export class CanonicalDesktopProtocol {
private creates = new Map<string, string>()
+ private revisions = new Map<string, number>()
+ private mutations = new Map<string, Record<string, unknown>>()
private generations = new Map<string, number>()
private prompts = new Map<string, Record<string, unknown>>()
+ failure(params: Record<string, unknown>, error: unknown) {
+ if ((error as { data?: { reason?: string } })?.data?.reason !== 'revision_conflict') { return }
+
+ // A confirmed CAS refusal did not mutate. Ambiguous transport failures keep
+ // the original revision/id so a retry cannot overwrite another user's edit.
+ 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]
+
+ if (field) {
+ const key = JSON.stringify([params.session_id, field, params[field]])
+ const retained = this.mutations.get(key)
+
+ if (retained) { return retained }
+ const revision = this.revisions.get(String(params.session_id))
+
+ if (revision === undefined) { throw new Error('Session revision unavailable; reopen the session before editing metadata') }
+
+ const mutation = { session_id: params.session_id, request_id: crypto.randomUUID(), expected_revision: revision,
+ operation: field === 'title' ? 'rename' : 'archive', payload: { [field]: params[field] } }
+
+ this.mutations.set(key, mutation)
+
+ return mutation
+ }
+
if (method === 'session.create') {
const allowed = new Set(['request_id', 'source', 'cwd', 'model', 'toolsets', 'profile', 'cols'])
const unsupported = Object.keys(params).filter(key => !allowed.has(key) && !(key === 'fast' && params[key] === false))
@@ -72,6 +101,18 @@ export class CanonicalDesktopProtocol {
result(method: string, params: Record<string, unknown>, value: any): any {
if (!value || typeof value !== 'object') { return value }
+ if (typeof value.session_id === 'string' && typeof value.revision === 'number') {
+ this.revisions.set(value.session_id, value.revision)
+ }
+
+ if (method === 'session.title' || method === 'session.archive') {
+ 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) } }
+
+ return { ...value, ok: true }
+ }
+
if (method === 'session.create') {
for (const [key, id] of this.creates) { if (id === params.request_id) { this.creates.delete(key) } }
}
diff --git a/apps/desktop/src/api/client.ts b/apps/desktop/src/api/client.ts
index 99447dabc75..b76b62e8906 100644
--- a/apps/desktop/src/api/client.ts
+++ b/apps/desktop/src/api/client.ts
@@ -40,9 +40,16 @@ 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 result = await super.request<T>(method, prepared, timeoutMs, signal)
+ const wireMethod = method === 'session.title' || method === 'session.archive' ? 'session.mutate' : method
- return this.protocol.result(method, prepared, result) as T
+ try {
+ const result = await super.request<T>(wireMethod, prepared, timeoutMs, signal)
+
+ return this.protocol.result(method, prepared, result) as T
+ } catch (error) {
+ this.protocol.failure(prepared, error)
+ throw error
+ }
}
constructor() {