Skip to content

The agent that grows with you

fix(desktop): scope every session mutation to its owning profile

Bergmann8918f429a89b56
diff --git a/apps/desktop/src/api/sessions.test.ts b/apps/desktop/src/api/sessions.test.ts
index 15e362daaba..0bbf3dadaf3 100644
--- a/apps/desktop/src/api/sessions.test.ts
+++ b/apps/desktop/src/api/sessions.test.ts
@@ -10,7 +10,9 @@ vi.mock('./client', () => ({
}))
const client = await import('./client')
-const { listSidebarSessions } = await import('./sessions')
+
+const { deleteSession, setSessionArchived, setSessionPinnedRemote, setSessionUnreadRemote, listSidebarSessions } =
+ await import('./sessions')
const hermesApi = vi.mocked(client.hermesApi)
@@ -19,6 +21,137 @@ beforeEach(() => {
vi.mocked(client.getApiRequestConnection).mockReturnValue('prometheus')
})
+describe('deleteSession profile scoping', () => {
+ it('scopes the DELETE to the owning profile in the URL (object owner)', async () => {
+ // Regression: the sidebar "All Profiles" delete sent the profile only via
+ // request.profile, not in the URL. On a remote gateway with no remoteProfile
+ // alias the main-process path rewrite left the URL unscoped, so the backend
+ // opened its own default state.db, missed the row, and returned
+ // {ok:true, already_absent:true} — the row vanished optimistically but was
+ // never deleted and came back on refresh. The URL must carry ?profile=.
+ hermesApi.mockResolvedValue({ ok: true } as never)
+ // Mirrors the real capabilityScoped for an object owner (remote-stamped row).
+ vi.mocked(client.capabilityScoped).mockReturnValue({ profile: 'tommy', connectionId: 'hermes-pi' })
+
+ await deleteSession('sess-1', { connectionId: 'hermes-pi', profile: 'tommy' })
+
+ expect(hermesApi.mock.calls[0][0]).toMatchObject({
+ method: 'DELETE',
+ path: '/api/sessions/sess-1?profile=tommy',
+ connectionId: 'hermes-pi',
+ profile: 'tommy'
+ })
+ })
+
+ it('scopes the DELETE to the owning profile in the URL (bare string owner)', async () => {
+ hermesApi.mockResolvedValue({ ok: true } as never)
+ // Bare-string owner: capabilityScoped resolves it to a profile scope.
+ vi.mocked(client.capabilityScoped).mockReturnValue({ profile: 'tommy' })
+
+ await deleteSession('sess-2', 'tommy')
+
+ expect(hermesApi.mock.calls[0][0]).toMatchObject({
+ method: 'DELETE',
+ path: '/api/sessions/sess-2?profile=tommy'
+ })
+ })
+
+ it('omits the profile query when no owner is known', async () => {
+ hermesApi.mockResolvedValue({ ok: true } as never)
+
+ await deleteSession('sess-3')
+
+ expect(hermesApi.mock.calls[0][0]).toMatchObject({
+ method: 'DELETE',
+ path: '/api/sessions/sess-3'
+ })
+ expect((hermesApi.mock.calls[0][0] as { path: string }).path).not.toContain('profile=')
+ })
+
+ it('keeps an explicit local pin routed to the local pool', async () => {
+ hermesApi.mockResolvedValue({ ok: true } as never)
+ // capabilityScoped drops a 'local' connection id by design; sessionScoped
+ // must re-add it so the request stays pinned to this device.
+ vi.mocked(client.capabilityScoped).mockReturnValue({ profile: 'tommy' })
+
+ await deleteSession('sess-4', { connectionId: 'local', profile: 'tommy' })
+
+ expect(hermesApi.mock.calls[0][0]).toMatchObject({
+ method: 'DELETE',
+ path: '/api/sessions/sess-4?profile=tommy',
+ connectionId: 'local',
+ profile: 'tommy'
+ })
+ })
+})
+
+describe('setSessionArchived profile scoping', () => {
+ it('carries the owning profile in the PATCH body', async () => {
+ // Same class as the unscoped DELETE: the PATCH handler reads its target DB
+ // from body.profile, so archiving a foreign-profile session must send it in
+ // the body, not only as request.profile (Electron routing), or on a remote
+ // gateway the archive lands on the wrong state.db and silently no-ops.
+ hermesApi.mockResolvedValue({ ok: true } as never)
+
+ await setSessionArchived('sess-a', true, 'tommy')
+
+ expect(hermesApi.mock.calls[0][0]).toMatchObject({
+ method: 'PATCH',
+ path: '/api/sessions/sess-a',
+ profile: 'tommy',
+ body: { archived: true, profile: 'tommy' }
+ })
+ })
+
+ it('omits the profile from the body when none is given', async () => {
+ hermesApi.mockResolvedValue({ ok: true } as never)
+
+ await setSessionArchived('sess-b', false)
+
+ const req = hermesApi.mock.calls[0][0] as { body: Record<string, unknown> }
+ expect(req).toMatchObject({ method: 'PATCH', body: { archived: false } })
+ expect(req.body).not.toHaveProperty('profile')
+ })
+})
+
+describe('setSessionPinnedRemote / setSessionUnreadRemote profile scoping', () => {
+ it('carries the owning profile in the pin PATCH body', async () => {
+ hermesApi.mockResolvedValue({ ok: true } as never)
+
+ await setSessionPinnedRemote('sess-p', true, 'tommy')
+
+ expect(hermesApi.mock.calls[0][0]).toMatchObject({
+ method: 'PATCH',
+ path: '/api/sessions/sess-p',
+ profile: 'tommy',
+ body: { pinned: true, profile: 'tommy' }
+ })
+ })
+
+ it('carries the owning profile in the unread PATCH body', async () => {
+ hermesApi.mockResolvedValue({ ok: true } as never)
+
+ await setSessionUnreadRemote('sess-u', true, 'tommy')
+
+ expect(hermesApi.mock.calls[0][0]).toMatchObject({
+ method: 'PATCH',
+ path: '/api/sessions/sess-u',
+ profile: 'tommy',
+ body: { unread: true, profile: 'tommy' }
+ })
+ })
+
+ it('omits the profile from the body when none is given', async () => {
+ hermesApi.mockResolvedValue({ ok: true } as never)
+
+ await setSessionPinnedRemote('sess-p2', false)
+
+ const req = hermesApi.mock.calls[0][0] as { body: Record<string, unknown> }
+ expect(req).toMatchObject({ method: 'PATCH', body: { pinned: false } })
+ expect(req.body).not.toHaveProperty('profile')
+ })
+})
+
describe('listSidebarSessions remote ownership', () => {
it('stamps active remote rows so a later resume stays on their gateway', async () => {
hermesApi.mockResolvedValue({
diff --git a/apps/desktop/src/api/sessions.ts b/apps/desktop/src/api/sessions.ts
index c5f559c07af..8c2f358a5d5 100644
--- a/apps/desktop/src/api/sessions.ts
+++ b/apps/desktop/src/api/sessions.ts
@@ -298,11 +298,17 @@ export async function listSidebarSessions(req: SidebarSessionsRequest): Promise<
// Mutations take the owning `profile` so Electron can route them to the correct
// remote backend or local profile scope. Omit for the current/default profile.
export function setSessionArchived(id: string, archived: boolean, profile?: string | null): Promise<{ ok: boolean }> {
+ // Carry the owning profile IN THE PATCH BODY, mirroring renameSession — the
+ // backend reads its target DB from body.profile (_open_session_db_for_profile).
+ // Passing it only as request.profile (Electron routing) is not enough on a
+ // remote gateway with no remoteProfile alias: the archive lands on the wrong
+ // (default) state.db, no-ops on a missing row, and the archived/unarchived
+ // state silently fails to stick — the same class as the unscoped DELETE.
return hermesApi<{ ok: boolean }>({
...(profile ? { profile } : {}),
path: `/api/sessions/${encodeURIComponent(id)}`,
method: 'PATCH',
- body: { archived }
+ body: { archived, ...(profile ? { profile } : {}) }
})
}
@@ -311,11 +317,14 @@ export function setSessionArchived(id: string, archived: boolean, profile?: stri
// pinned chat. Best-effort: the sidebar stays localStorage-driven for its own
// display; this only feeds the backend policy.
export function setSessionPinnedRemote(id: string, pinned: boolean, profile?: string | null): Promise<{ ok: boolean }> {
+ // Owning profile in the PATCH body (see setSessionArchived / renameSession):
+ // the handler reads its target DB from body.profile, so a remote/foreign
+ // profile's pin must travel in the body or it no-ops on the wrong state.db.
return hermesApi<{ ok: boolean }>({
...(profile ? { profile } : {}),
path: `/api/sessions/${encodeURIComponent(id)}`,
method: 'PATCH',
- body: { pinned }
+ body: { pinned, ...(profile ? { profile } : {}) }
})
}
@@ -324,11 +333,15 @@ export function setSessionPinnedRemote(id: string, pinned: boolean, profile?: st
// routing as the other session mutations: a remote session's row lives only
// on its remote host, so the owning profile must travel with the request.
export function setSessionUnreadRemote(id: string, unread: boolean, profile?: string | null): Promise<{ ok: boolean }> {
+ // Owning profile in the PATCH body (see setSessionArchived / renameSession):
+ // the handler reads its target DB from body.profile, so a remote/foreign
+ // profile's unread toggle must travel in the body or it no-ops on the wrong
+ // state.db.
return hermesApi<{ ok: boolean }>({
...(profile ? { profile } : {}),
path: `/api/sessions/${encodeURIComponent(id)}`,
method: 'PATCH',
- body: { unread }
+ body: { unread, ...(profile ? { profile } : {}) }
})
}
@@ -526,9 +539,23 @@ export async function getAllSessionMessages(
}
export function deleteSession(id: string, profile?: ProfileScope): Promise<{ ok: boolean }> {
+ // Scope the DELETE to the owning profile IN THE URL, mirroring getSession /
+ // getSessionMessages. Passing the profile only via request.profile (which the
+ // Electron main process consumes for backend routing) is NOT enough: on a
+ // remote gateway whose connection has no remoteProfile alias, the main-process
+ // path rewrite leaves the URL unscoped, so the backend opens its OWN (default)
+ // state.db, fails to find another profile's session, and returns
+ // {ok:true, already_absent:true}. The row then vanishes optimistically but was
+ // never deleted and reappears on the next sidebar refresh — the "All Profiles
+ // delete doesn't stick" report. The endpoint already honours ?profile=
+ // (get/rename/messages all send it); DELETE was the one mutation that dropped
+ // it after the api/ split. request.profile stays on for per-profile remote
+ // override + global-remote routing (both re-read/re-append the param).
+ const suffix = sessionScopeQuery(profile)
+
return hermesApi<{ ok: boolean }>({
...sessionScoped(profile),
- path: `/api/sessions/${encodeURIComponent(id)}`,
+ path: `/api/sessions/${encodeURIComponent(id)}${suffix}`,
method: 'DELETE'
})
}