From 7b951e46ab1a5ecf0da0a542b9d204573cc8ecae Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 04:10:39 -0700 Subject: [PATCH] fix(desktop): route a shared-id unpin to the row the pull adopted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With every slice feeding the pin sync, two profiles can legitimately hold the same session id. The pull already tie-breaks toward the active gateway's row (rowsByPinId), but the push resolved the profile by first match — so an unpin PATCHed the other profile and the next page re-adopted the pin. Resolve the write's row with the same active-gateway preference. Case and test from #92609. Co-authored-by: Jake Vincent <45184202+jakewvincent@users.noreply.github.com> --- .../src/store/session-pin-sync.test.ts | 19 +++++++++++++++++++ apps/desktop/src/store/session-pin-sync.ts | 17 +++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/store/session-pin-sync.test.ts b/apps/desktop/src/store/session-pin-sync.test.ts index 0e470a9ac4..9abfad3c91 100644 --- a/apps/desktop/src/store/session-pin-sync.test.ts +++ b/apps/desktop/src/store/session-pin-sync.test.ts @@ -133,6 +133,25 @@ describe('watchSessionPins remote pull', () => { expect(patch).toHaveBeenCalledWith('cron-pin', false, 'jobs') }) + it('routes a cross-slice unpin to the active profile', async () => { + $activeGatewayProfile.set('work') + + try { + $sessions.set([row('shared', { pinned: true, profile: 'default' })]) + $messagingSessions.set([row('shared', { pinned: true, profile: 'work', source: 'photon' })]) + await flush() + expect($pinnedSessionIds.get()).toEqual(['shared']) + patch.mockClear() + + $pinnedSessionIds.set([]) + await flush() + + expect(patch).toHaveBeenCalledWith('shared', false, 'work') + } finally { + $activeGatewayProfile.set('default') + } + }) + 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 5c9586efcf..95e71acca3 100644 --- a/apps/desktop/src/store/session-pin-sync.ts +++ b/apps/desktop/src/store/session-pin-sync.ts @@ -77,13 +77,26 @@ function publishUnconfirmed(): void { } function profileFor(pinId: string): null | string | undefined { - return loadedSessionRows().find(row => sessionMatchesStoredId(row, pinId))?.profile + return loadedRowFor(pinId)?.profile } function loadedSessionRows(): SessionInfo[] { return [...$sessions.get(), ...$cronSessions.get(), ...$messagingSessions.get()] } +/** + * The row a stored pin id resolves to, across every slice. Same tie-break as + * `rowsByPinId`: when two profiles share the id, the write must target the + * row the pull adopted — the active gateway's — or an unpin PATCHes the other + * profile and the next page re-adopts the pin. + */ +function loadedRowFor(pinId: string): SessionInfo | undefined { + const rows = loadedSessionRows().filter(row => sessionMatchesStoredId(row, pinId)) + const gateway = normalizeProfileKey($activeGatewayProfile.get()) + + return rows.find(row => normalizeProfileKey(row.profile) === gateway) ?? rows[0] +} + /** * One authoritative row per durable pin id. Session ids are only unique inside * a profile, so the cross-profile list can legitimately hold two rows with the @@ -259,7 +272,7 @@ function reconcileInner(): void { // Flush whatever we can resolve now; unresolved ids (row not loaded yet) // retry on the next loaded-session slice change. for (const id of [...pending]) { - const row = loadedSessionRows().find(entry => sessionMatchesStoredId(entry, id)) + const row = loadedRowFor(id) if (!row) { continue