Skip to content

The agent that grows with you

fix(desktop): sync pins across all session slices

fangliquanflqd417aee3877b
diff --git a/apps/desktop/src/store/session-pin-sync.test.ts b/apps/desktop/src/store/session-pin-sync.test.ts
index f9c39e76b3b..0e470a9ac40 100644
--- a/apps/desktop/src/store/session-pin-sync.test.ts
+++ b/apps/desktop/src/store/session-pin-sync.test.ts
@@ -15,7 +15,7 @@ vi.mock('@/hermes', () => ({
import { $pinnedSessionIds } from '@/store/layout'
import { $activeGatewayProfile } from '@/store/profile'
-import { $sessions } from '@/store/session'
+import { $cronSessions, $messagingSessions, $sessions } from '@/store/session'
import { $unconfirmedPinWrites, resetSessionPinMirror, watchSessionPins } from './session-pin-sync'
@@ -33,6 +33,8 @@ beforeAll(() => {
beforeEach(() => {
$sessions.set([])
+ $cronSessions.set([])
+ $messagingSessions.set([])
$pinnedSessionIds.set([])
// The mirror/pending/unconfirmed maps are module-global, so one test's
// bookkeeping would otherwise suppress the next test's PATCH (or fence out
@@ -43,6 +45,8 @@ beforeEach(() => {
afterEach(() => {
$sessions.set([])
+ $cronSessions.set([])
+ $messagingSessions.set([])
$pinnedSessionIds.set([])
})
@@ -103,6 +107,32 @@ describe('watchSessionPins', () => {
})
describe('watchSessionPins remote pull', () => {
+ it('adopts and durably unpins a backend-only messaging pin', async () => {
+ $messagingSessions.set([row('photon-pin', { pinned: true, profile: 'messages', source: 'photon' })])
+ await flush()
+
+ expect($pinnedSessionIds.get()).toEqual(['photon-pin'])
+ patch.mockClear()
+
+ $pinnedSessionIds.set([])
+ await flush()
+
+ expect(patch).toHaveBeenCalledWith('photon-pin', false, 'messages')
+ })
+
+ it('adopts and durably unpins a backend-only cron pin', async () => {
+ $cronSessions.set([row('cron-pin', { pinned: true, profile: 'jobs', source: 'cron' })])
+ await flush()
+
+ expect($pinnedSessionIds.get()).toEqual(['cron-pin'])
+ patch.mockClear()
+
+ $pinnedSessionIds.set([])
+ await flush()
+
+ expect(patch).toHaveBeenCalledWith('cron-pin', false, 'jobs')
+ })
+
it('adopts a pin another app made', async () => {
$sessions.set([row('remote', { pinned: true })])
await flush()
diff --git a/apps/desktop/src/store/session-pin-sync.ts b/apps/desktop/src/store/session-pin-sync.ts
index 8d162a7e831..5c9586efcfa 100644
--- a/apps/desktop/src/store/session-pin-sync.ts
+++ b/apps/desktop/src/store/session-pin-sync.ts
@@ -27,7 +27,13 @@ import { setSessionPinnedRemote } from '@/hermes'
import { onConnectionScopeChange } from '@/lib/connection-scoped'
import { $pinnedSessionIds, pinSession, unpinSession } from '@/store/layout'
import { $activeGatewayProfile, normalizeProfileKey } from '@/store/profile'
-import { $sessions, sessionMatchesStoredId, sessionPinId } from '@/store/session'
+import {
+ $cronSessions,
+ $messagingSessions,
+ $sessions,
+ sessionMatchesStoredId,
+ sessionPinId
+} from '@/store/session'
import type { SessionInfo } from '@/types/hermes'
// pin ids we've successfully PATCHed pinned=true this session.
@@ -71,7 +77,11 @@ function publishUnconfirmed(): void {
}
function profileFor(pinId: string): null | string | undefined {
- return $sessions.get().find(row => sessionMatchesStoredId(row, pinId))?.profile
+ return loadedSessionRows().find(row => sessionMatchesStoredId(row, pinId))?.profile
+}
+
+function loadedSessionRows(): SessionInfo[] {
+ return [...$sessions.get(), ...$cronSessions.get(), ...$messagingSessions.get()]
}
/**
@@ -140,7 +150,7 @@ function writePin(id: string, pinned: boolean, profile?: null | string): Promise
function pullRemotePins(): void {
const local = new Set($pinnedSessionIds.get())
- for (const row of rowsByPinId($sessions.get()).values()) {
+ for (const row of rowsByPinId(loadedSessionRows()).values()) {
// A backend without the flag has no opinion; never act on `undefined`.
if (typeof row.pinned !== 'boolean') {
continue
@@ -190,8 +200,8 @@ function pullRemotePins(): void {
}
}
-// Re-entrancy guard: reconcile() is subscribed to BOTH $sessions and
-// $pinnedSessionIds, and pullRemotePins() mutates $pinnedSessionIds (via
+// Re-entrancy guard: reconcile() is subscribed to every loaded-session slice
+// and $pinnedSessionIds, and pullRemotePins() mutates $pinnedSessionIds (via
// pinSession/unpinSession), which fires reconcile() again synchronously.
// Without this guard, a session whose pin state oscillates — two rows with the
// same durable id but conflicting `pinned` flags, possible when profile
@@ -247,9 +257,9 @@ function reconcileInner(): void {
}
// Flush whatever we can resolve now; unresolved ids (row not loaded yet)
- // retry on the next $sessions change.
+ // retry on the next loaded-session slice change.
for (const id of [...pending]) {
- const row = $sessions.get().find(entry => sessionMatchesStoredId(entry, id))
+ const row = loadedSessionRows().find(entry => sessionMatchesStoredId(entry, id))
if (!row) {
continue
@@ -276,6 +286,8 @@ export function watchSessionPins(): void {
reconcile()
$pinnedSessionIds.listen(reconcile)
$sessions.listen(reconcile)
+ $cronSessions.listen(reconcile)
+ $messagingSessions.listen(reconcile)
}
/**