Skip to content

The agent that grows with you

fix(desktop): sidebar edits work again against older standalone runtimes

Tekniumc59f8c47a584
diff --git a/apps/desktop/src/api/sessions-mutations.test.ts b/apps/desktop/src/api/sessions-mutations.test.ts
index c071f587a29..be6afbaa2ac 100644
--- a/apps/desktop/src/api/sessions-mutations.test.ts
+++ b/apps/desktop/src/api/sessions-mutations.test.ts
@@ -60,3 +60,40 @@ it('all sidebar mutations use real counters and surface conflicts without a blin
await expect(renameSession('unknown-counter', 'name')).rejects.toThrow(/snapshot/)
expect(hermesApi).toHaveBeenCalledTimes(1)
})
+
+// R12: an older standalone runtime has no fencing route (its SPA catch-all answers the exact
+// marker below); sidebar edits there keep the base write. Any other refusal of the snapshot read
+// (a profile 404, permission, timeout, an unavailable authority) is never a downgrade.
+it('sidebar edits fall back to the base write only when the backend lacks the fencing route', async () => {
+ const routeMissing = '404: {"detail":"No such API endpoint: /api/sessions/old/mutation-snapshot"}'
+ const writes: Array<{ path: string; method?: string; body?: unknown }> = []
+ vi.mocked(hermesApi).mockImplementation(async request => {
+ if (!request.method || request.method === 'GET') { throw new Error(routeMissing) }
+ writes.push(request as never)
+
+ return { ok: true } as never
+ })
+
+ await renameSession('old', 'name', 'work')
+ await setSessionArchived('old', true, 'work')
+ await setSessionPinnedRemote('old', true, 'work')
+ await setSessionUnreadRemote('old', false, 'work')
+ await deleteSession('old', { connectionId: 'server', profile: 'work' })
+ expect(writes.map(write => [write.method, write.body])).toEqual([
+ ['PATCH', { title: 'name', profile: 'work' }], ['PATCH', { archived: true, profile: 'work' }],
+ ['PATCH', { pinned: true, profile: 'work' }], ['PATCH', { unread: false, profile: 'work' }], ['DELETE', undefined]])
+ expect(new URL(writes[4].path, 'http://localhost').search).toBe('?profile=work')
+
+ for (const refusal of ['404: {"detail":"Profile \'work\' does not exist."}', '403: permission_denied',
+ '503: {"detail":"session_authority_unavailable"}', 'Request timed out']) {
+ writes.length = 0
+ vi.mocked(hermesApi).mockReset().mockImplementation(async request => {
+ if (!request.method || request.method === 'GET') { throw new Error(refusal) }
+ writes.push(request as never)
+
+ return { ok: true } as never
+ })
+ await expect(renameSession('old', 'name', 'work')).rejects.toThrow(refusal)
+ expect(writes).toEqual([])
+ }
+})
diff --git a/apps/desktop/src/api/sessions.ts b/apps/desktop/src/api/sessions.ts
index 69c93cb70d8..a9a6c409eeb 100644
--- a/apps/desktop/src/api/sessions.ts
+++ b/apps/desktop/src/api/sessions.ts
@@ -1,4 +1,4 @@
-import { isMissingRestEndpoint } from '@/lib/gateway-rpc'
+import { isMissingRestEndpoint, isUnroutedRestPath } from '@/lib/gateway-rpc'
import { maybeBackfillLegacySessionOwners } from '@/lib/legacy-session-owner-backfill'
import { stampRowsWithOwningConnection } from '@/lib/session-owner-stamp'
import { pageHonorsLatestOrder, recordTranscriptTail } from '@/store/transcript-tail'
@@ -10,7 +10,7 @@ import type {
SessionSearchResponse
} from '@/types/hermes'
-import { createSessionMutationClient, type SessionMutationSnapshot } from '../../../shared/src/session-http-mutations'
+import { createSessionMutationClient, type SessionMutationIdentity, type SessionMutationSnapshot } from '../../../shared/src/session-http-mutations'
import {
ambientOwnerConnectionId,
@@ -374,6 +374,25 @@ export async function listSidebarSessions(req: SidebarSessionsRequest): Promise<
const runSessionMutation = createSessionMutationClient()
+/** Run one fenced session edit. The fencing protocol is a backend capability: an older
+ * standalone runtime has no snapshot route and keeps its base write. Only that route-level
+ * verdict on the read, before any write was attempted, selects it; a session/profile 404,
+ * refusal, conflict, timeout or failed write keeps the canonical CAS path and its retained
+ * identity. */
+export function mutateSessionFenced<T>(key: string, read: () => Promise<SessionMutationSnapshot>,
+ send: (identity: Partial<SessionMutationIdentity>) => Promise<T>): Promise<T> {
+ let unfenced = false
+
+ return runSessionMutation(key, () => read().catch(error => {
+ unfenced = isUnroutedRestPath(error)
+ throw error
+ }), send).catch(error => {
+ if (!unfenced) { throw error }
+
+ return send({})
+ })
+}
+
function mutateSessionHttp<T>(id: string, method: 'PATCH' | 'DELETE', payload: Record<string, unknown>, profile?: ProfileScope): Promise<T> {
// Null is the window's v1 route (its primary, local or remote); defaulting it to
// the registry's 'local' source would pin a remote primary's edits to this machine.
@@ -383,20 +402,20 @@ function mutateSessionHttp<T>(id: string, method: 'PATCH' | 'DELETE', payload: R
const suffix = query.size ? `?${query}` : ''
const key = JSON.stringify([scope, id, method, payload])
- return runSessionMutation(key,
- () => hermesApi<SessionMutationSnapshot>({ ...scope, path: `${path}/mutation-snapshot${suffix}` }),
- identity => {
- if (method === 'DELETE') {
- const params = new URLSearchParams(query)
+ const send = (identity: Partial<SessionMutationIdentity>) => {
+ if (method === 'DELETE') {
+ const params = new URLSearchParams(query)
- for (const [name, value] of Object.entries(identity)) { params.set(name, String(value)) }
+ for (const [name, value] of Object.entries(identity)) { params.set(name, String(value)) }
- return hermesApi<T>({ ...scope, path: `${path}?${params}`, method })
- }
+ return hermesApi<T>({ ...scope, path: params.size ? `${path}?${params}` : path, method })
+ }
- return hermesApi<T>({ ...scope, path, method,
- body: { ...payload, ...identity, ...(scope.profile ? { profile: scope.profile } : {}) } })
- })
+ return hermesApi<T>({ ...scope, path, method,
+ body: { ...payload, ...identity, ...(scope.profile ? { profile: scope.profile } : {}) } })
+ }
+
+ return mutateSessionFenced(key, () => hermesApi<SessionMutationSnapshot>({ ...scope, path: `${path}/mutation-snapshot${suffix}` }), send)
}
export function setSessionArchived(id: string, archived: boolean, profile?: string | null): Promise<{ ok: boolean }> {
diff --git a/apps/desktop/src/lib/gateway-rpc.ts b/apps/desktop/src/lib/gateway-rpc.ts
index 4f4d1321e51..c30f5a0458b 100644
--- a/apps/desktop/src/lib/gateway-rpc.ts
+++ b/apps/desktop/src/lib/gateway-rpc.ts
@@ -38,6 +38,15 @@ export function isMissingRestEndpoint(error: unknown): boolean {
)
}
+/** Narrow twin of isMissingRestEndpoint for routes with path params: only the
+ * backend's catch-all verdict (or the Electron HTML guard) proves the ROUTE is
+ * absent; a handler's own 404 (unknown session/profile) never matches. */
+export function isUnroutedRestPath(error: unknown): boolean {
+ const message = error instanceof Error ? error.message : String(error)
+
+ return /no such api endpoint|endpoint is likely missing/i.test(message)
+}
+
/** True when the backend refused a request because it owns the profile and the
* call is offline-only maintenance (`web_server_sessions.py::_with_session_maintenance`
* → HTTP 409 "Exclusive maintenance refused"). The refusal is the steady state
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index 007580ad916..36b8c041197 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -22,10 +22,7 @@ import { atom, computed, type ReadableAtom } from 'nanostores'
import type { ReactNode } from 'react'
import { capabilityScoped, getApiRequestConnection } from '@/api/client'
-
-import { createSessionMutationClient, type SessionMutationSnapshot } from '../../../shared/src/session-http-mutations'
-
-const mutatePersistedVisibility = createSessionMutationClient()
+import { mutateSessionFenced } from '@/api/sessions'
import { PRIMARY_SESSION_VIEW } from '@/app/chat/session-view'
import { openSession, type OpenSessionIntent } from '@/app/open-session'
import { syncWorkspaceRoute } from '@/app/routes'
@@ -109,6 +106,8 @@ import {
import { runGatewayRestart } from '@/store/system-actions'
import type { PaginatedSessions, UsageStats } from '@/types/hermes'
+import type { SessionMutationSnapshot } from '../../../shared/src/session-http-mutations'
+
import { pluginDecisions, profiles, skills, toolsets } from './bridge'
import { composerHost } from './composer'
import { i18nHost } from './i18n'
@@ -1520,7 +1519,7 @@ export const host = {
const path = `/api/sessions/${encodeURIComponent(options.sessionId)}`
const payload = { hidden: options.hidden, profile }
- return mutatePersistedVisibility(
+ return mutateSessionFenced(
JSON.stringify([scope, options.sessionId, payload]),
() =>
hermesApi<SessionMutationSnapshot>({