Skip to content

The agent that grows with you

fix(desktop): identify local profile mutations by backend mode

brooklyn!3e379a7702fc
diff --git a/apps/desktop/electron/desktop-profile-mutation.test.ts b/apps/desktop/electron/desktop-profile-mutation.test.ts
new file mode 100644
index 00000000000..d9b23748fa8
--- /dev/null
+++ b/apps/desktop/electron/desktop-profile-mutation.test.ts
@@ -0,0 +1,87 @@
+import assert from 'node:assert/strict'
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+
+import { test } from 'vitest'
+
+import { apiRequestRegistryConnectionId } from './connection-config'
+import { createDesktopProfilePreferences } from './desktop-profile'
+
+const sources = [
+ { label: 'legacy local', connectionId: null, mode: 'local' },
+ { label: 'registry local', connectionId: 'local', mode: 'local' },
+ { label: 'legacy remote', connectionId: null, mode: 'remote' },
+ { label: 'legacy SSH', connectionId: null, mode: 'ssh' },
+ { label: 'registry remote', connectionId: 'remote-work', mode: 'remote' },
+ { label: 'registry SSH', connectionId: 'ssh-work', mode: 'ssh' },
+ { label: 'registry cloud', connectionId: 'cloud-work', mode: 'cloud' }
+]
+
+for (const method of ['PATCH', 'DELETE']) {
+ test.each(sources)(`${method} on $label preserves startup ownership across restart`, source => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'desktop-profile-owner-'))
+ const target = path.join(root, 'active-profile.json')
+ const otherTarget = path.join(root, 'other', 'active-profile.json')
+ const preferences = createDesktopProfilePreferences(target)
+ const other = createDesktopProfilePreferences(otherTarget)
+
+ const request = {
+ connectionId: source.connectionId,
+ method,
+ path: '/api/profiles/work',
+ body: method === 'PATCH' ? { new_name: 'renamed' } : undefined
+ }
+
+ const connectionId = apiRequestRegistryConnectionId(request)
+ const route = { connectionId, profile: 'work' }
+
+ try {
+ preferences.remember('work')
+ preferences.setDefault(route)
+ other.remember('work')
+ other.setDefault(route)
+ preferences.afterProfileRequest(connectionId, request, { ok: true }, source.mode)
+
+ const restarted = createDesktopProfilePreferences(target)
+ assert.equal(
+ restarted.readActive(),
+ source.mode === 'local' ? (method === 'PATCH' ? 'renamed' : 'default') : 'work'
+ )
+ assert.deepEqual(restarted.getDefault(), method === 'PATCH' ? { ...route, profile: 'renamed' } : null)
+ assert.equal(createDesktopProfilePreferences(otherTarget).readActive(), 'work')
+ assert.deepEqual(other.getDefault(), route)
+ assert.equal(preferences.readActive(), restarted.readActive())
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+ })
+
+ test.each([null, 'local'])(`${method} failures through %s preserve both preferences`, connectionId => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'desktop-profile-failure-'))
+ const target = path.join(root, 'active-profile.json')
+ const preferences = createDesktopProfilePreferences(target)
+ const route = { connectionId, profile: 'work' }
+ const request = { method, path: '/api/profiles/work', body: { new_name: 'renamed' } }
+
+ try {
+ preferences.remember('work')
+ preferences.setDefault(route)
+ const original = fs.readFileSync(target, 'utf8')
+
+ for (const failure of [{ ok: false }, { success: false }, { error: 'Permission denied' }]) {
+ preferences.afterProfileRequest(connectionId, request, failure, 'local')
+ assert.equal(fs.readFileSync(target, 'utf8'), original)
+ }
+
+ preferences.afterProfileRequest(connectionId, { ...request, path: '/api/profiles/other' }, { ok: true }, 'local')
+ assert.equal(fs.readFileSync(target, 'utf8'), original)
+
+ fs.mkdirSync(`${target}.tmp`)
+ assert.throws(() => preferences.afterProfileRequest(connectionId, request, { ok: true }, 'local'))
+ assert.equal(fs.readFileSync(target, 'utf8'), original)
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+ })
+}
diff --git a/apps/desktop/electron/desktop-profile.test.ts b/apps/desktop/electron/desktop-profile.test.ts
index fc701234ae3..af606fd6371 100644
--- a/apps/desktop/electron/desktop-profile.test.ts
+++ b/apps/desktop/electron/desktop-profile.test.ts
@@ -121,23 +121,25 @@ test('an explicit default survives last-used profile writes and app restarts, is
a.afterProfileRequest(
'remote-work',
{ method: 'PATCH', path: '/api/profiles/work', body: { new_name: 'ignored' } },
- { ok: false }
+ { ok: false },
+ 'remote'
)
assert.deepEqual(a.getDefault(), route)
a.afterProfileRequest(
'remote-work',
{ method: 'PATCH', path: '/api/profiles/work', body: { new_name: 'renamed' } },
- { ok: true }
+ { ok: true },
+ 'remote'
)
assert.deepEqual(a.getDefault(), { ...route, profile: 'renamed' })
- a.afterProfileRequest('remote-work', { method: 'DELETE', path: '/api/profiles/renamed' }, { ok: true })
+ a.afterProfileRequest('remote-work', { method: 'DELETE', path: '/api/profiles/renamed' }, { ok: true }, 'remote')
assert.equal(a.getDefault(), null)
a.setDefault(route)
- a.profileChanged('another-source', 'work', 'renamed')
+ a.profileChanged('another-source', 'work', 'renamed', 'remote')
assert.deepEqual(a.getDefault(), route)
- a.profileChanged(route.connectionId, route.profile, 'renamed')
+ a.profileChanged(route.connectionId, route.profile, 'renamed', 'remote')
assert.deepEqual(a.getDefault(), { ...route, profile: 'renamed' })
- a.profileChanged(route.connectionId, 'renamed', null)
+ a.profileChanged(route.connectionId, 'renamed', null, 'remote')
assert.equal(a.getDefault(), null)
a.setDefault(route)
a.connectionRemoved(route.connectionId)
@@ -147,29 +149,54 @@ test('an explicit default survives last-used profile writes and app restarts, is
}
})
-test('successful local profile changes retarget the saved startup profile', () => {
- const root = fs.mkdtempSync(path.join(os.tmpdir(), 'desktop-profile-change-'))
- const target = path.join(root, 'active-profile.json')
- const preferences = createDesktopProfilePreferences(target)
-
- try {
- preferences.remember('local-old')
- preferences.afterProfileRequest(null, { method: 'DELETE', path: '/api/profiles/local-old' }, { ok: false })
- assert.equal(preferences.readActive(), 'local-old')
-
- preferences.afterProfileRequest('remote-work', { method: 'DELETE', path: '/api/profiles/local-old' }, { ok: true })
- assert.equal(preferences.readActive(), 'local-old')
-
- preferences.afterProfileRequest(
- null,
- { method: 'PATCH', path: '/api/profiles/local-old', body: { new_name: 'local-new' } },
- { ok: true }
- )
- assert.equal(preferences.readActive(), 'local-new')
-
- preferences.afterProfileRequest(null, { method: 'DELETE', path: '/api/profiles/local-new' }, { ok: true })
- assert.equal(preferences.readActive(), 'default')
- } finally {
- fs.rmSync(root, { recursive: true, force: true })
+test.each([null, 'local'])(
+ 'successful local profile changes through %s retarget the saved startup profile',
+ connectionId => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'desktop-profile-change-'))
+ const target = path.join(root, 'active-profile.json')
+ const preferences = createDesktopProfilePreferences(target)
+
+ try {
+ const defaultRoute = { connectionId: 'remote-work', profile: 'local-old' }
+ preferences.setDefault(defaultRoute)
+ preferences.remember('local-old')
+ preferences.afterProfileRequest(
+ connectionId,
+ { method: 'DELETE', path: '/api/profiles/local-old' },
+ { ok: false },
+ 'local'
+ )
+ assert.equal(preferences.readActive(), 'local-old')
+
+ preferences.afterProfileRequest(
+ 'remote-work',
+ { method: 'DELETE', path: '/api/profiles/local-old' },
+ { ok: true },
+ 'remote'
+ )
+ assert.equal(preferences.readActive(), 'local-old')
+ preferences.setDefault(defaultRoute)
+
+ preferences.afterProfileRequest(
+ connectionId,
+ { method: 'PATCH', path: '/api/profiles/local-old', body: { new_name: 'local-new' } },
+ { ok: true },
+ 'local'
+ )
+ const restarted = createDesktopProfilePreferences(target)
+ assert.equal(restarted.readActive(), 'local-new')
+ assert.deepEqual(restarted.getDefault(), defaultRoute)
+
+ restarted.afterProfileRequest(
+ connectionId,
+ { method: 'DELETE', path: '/api/profiles/local-new' },
+ { ok: true },
+ 'local'
+ )
+ assert.equal(createDesktopProfilePreferences(target).readActive(), 'default')
+ assert.deepEqual(restarted.getDefault(), defaultRoute)
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
}
-})
+)
diff --git a/apps/desktop/electron/desktop-profile.ts b/apps/desktop/electron/desktop-profile.ts
index a799fc21bbe..08a97c0a9a8 100644
--- a/apps/desktop/electron/desktop-profile.ts
+++ b/apps/desktop/electron/desktop-profile.ts
@@ -139,8 +139,8 @@ export function createDesktopProfilePreferences(
options.onDefaultChanged?.(null)
}
- function profileChanged(connectionId: null | string, oldName: string, newName: null | string) {
- if (connectionId === null && readActive() === oldName) {
+ function profileChanged(connectionId: null | string, oldName: string, newName: null | string, backendMode: string) {
+ if (backendMode === 'local' && readActive() === oldName) {
remember(newName || 'default')
}
@@ -163,7 +163,12 @@ export function createDesktopProfilePreferences(
}
}
- function afterProfileRequest(connectionId: null | string, request: ProfileRenameRequest, response: unknown) {
+ function afterProfileRequest(
+ connectionId: null | string,
+ request: ProfileRenameRequest,
+ response: unknown,
+ backendMode: string
+ ) {
if (response && typeof response === 'object') {
const result = response as Record<string, unknown>
@@ -176,9 +181,9 @@ export function createDesktopProfilePreferences(
const deleted = profileNameFromDeleteRequest(request)
if (renamed) {
- profileChanged(connectionId, renamed.oldName, renamed.newName)
+ profileChanged(connectionId, renamed.oldName, renamed.newName, backendMode)
} else if (deleted) {
- profileChanged(connectionId, deleted, null)
+ profileChanged(connectionId, deleted, null, backendMode)
}
}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index d03b789221f..3718ac358d7 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -16862,6 +16862,8 @@ async function dispatchRegistryApiRequest(
timeoutMs: resolveTimeoutMs(request?.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS)
})
+ desktopProfilePreferences.afterProfileRequest(registryConnectionId, request, response, connection.mode)
+
return (request?.method || 'GET').toUpperCase() === 'GET'
? tagRegistrySessionResponse(requestPath, response, registryConnectionId)
: response
@@ -16898,10 +16900,7 @@ async function handleHermesApiRequest(request) {
const registryConnectionId = apiRequestRegistryConnectionId(request)
if (registryConnectionId) {
- const response = await dispatchRegistryApiRequest(request, registryConnectionId)
- desktopProfilePreferences.afterProfileRequest(registryConnectionId, request, response)
-
- return response
+ return dispatchRegistryApiRequest(request, registryConnectionId)
}
// Remote-profile session requests would otherwise hit the local primary off
@@ -16938,9 +16937,10 @@ async function handleHermesApiRequest(request) {
: resolveRouteProfile(tornDownProfile, apiRoute.backendProfile)
let response
+ let connection
try {
- const connection = await ensureBackend(routeProfile, { passive: request?.passive, spawnPriority })
+ connection = await ensureBackend(routeProfile, { passive: request?.passive, spawnPriority })
const timeoutMs = resolveTimeoutMs(request?.timeoutMs, DEFAULT_FETCH_TIMEOUT_MS)
response = await fetchJsonForBackend(connection, apiRoute.requestPath, {
@@ -16964,7 +16964,7 @@ async function handleHermesApiRequest(request) {
}
try {
- desktopProfilePreferences.afterProfileRequest(null, request, response)
+ desktopProfilePreferences.afterProfileRequest(null, request, response, connection.mode)
} finally {
await profileRename?.complete()
}
@@ -16981,7 +16981,7 @@ ipcMain.handle('hermes:api', async (_event, request) => {
const registryConnectionId = apiRequestRegistryConnectionId(request)
if (deletingProfile && registryConnectionId) {
- const response = await dispatchConnectionScopedProfileDelete(request, {
+ return dispatchConnectionScopedProfileDelete(request, {
acquire: profile => profileDeletionGate.acquire(profile),
connectionKind: connectionId => registryConnectionKind(connectionId),
dispatch: routeProfile =>
@@ -16991,9 +16991,6 @@ ipcMain.handle('hermes:api', async (_event, request) => {
prepareLocal: localRequest => prepareProfileDeleteRequest(localRequest).then(() => undefined),
teardownConnection: (connectionId, profile) => teardownConnectionScopedProfileBackend(connectionId, profile)
})
- desktopProfilePreferences.afterProfileRequest(registryConnectionId, request, response)
-
- return response
}
if (!mutatingProfile) {