The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): stamp optimistic pin rows so value copies can't confirm the guard
Brooklyn Nicholson2953ead65863
diff --git a/apps/desktop/src/store/session-pin-sync.test.ts b/apps/desktop/src/store/session-pin-sync.test.tsindex f3c3aabef5c..1ec3b3f2b61 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 { $cronSessions, $messagingSessions, $sessions } from '@/store/session'+import { $cronSessions, $messagingSessions, $sessions, applySessionTitle, touchSessionActivity } from '@/store/session'import { $unconfirmedPinWrites, forgetPinSyncState, resetSessionPinMirror, watchSessionPins } from './session-pin-sync'@@ -418,6 +418,73 @@ describe('watchSessionPins remote pull', () => {}})+ it('does not let a value-copied painted row confirm the guard while the PATCH is in flight', async () => {+ // applyRowPinned paints the local intent onto a row object, but SessionInfo+ // rows are re-spread by value on ordinary user paths (touchSessionActivity on+ // send, applySessionTitle on rename). A copy carries the painted `pinned`+ // but not the optimistic marker, so it used to read as "a page agreeing+ // with what we wrote" and released the write guard while the PATCH was+ // still in flight — the exact revert this fix exists to close.+ let settle: (v: { ok: boolean }) => void = () => {}++ patch.mockImplementationOnce(() => new Promise(resolve => (settle = resolve)))++ $sessions.set([row('in-use', { pinned: true })])+ await flush()+ expect($pinnedSessionIds.get()).toEqual(['in-use'])++ // User unpins; the PATCH is held in flight so the guard stays armed.+ $pinnedSessionIds.set([])+ await flush()+ expect(patch).toHaveBeenCalledWith('in-use', false, undefined)++ // The very next message re-spreads the painted row by VALUE.+ touchSessionActivity('in-use', { at: 12345, preview: 'next message' })+ await flush()++ // The copied row agrees with what we wrote, but it is still OUR intent,+ // not a server confirmation. The guard must stay armed...+ expect($unconfirmedPinWrites.get().has('in-use')).toBe(true)+ // ...and a stale pre-PATCH page landing now must not resurrect the pin.+ $sessions.set([row('in-use', { pinned: true }), row('other')])+ await flush()++ expect($pinnedSessionIds.get()).not.toContain('in-use')++ settle({ ok: true })+ await flush()+ await flush()+ })++ it('does not let a title-applied copy of a painted row confirm the guard', async () => {+ // Same class, rename path: applySessionTitle re-spreads every matching row.+ let settle: (v: { ok: boolean }) => void = () => {}++ patch.mockImplementationOnce(() => new Promise(resolve => (settle = resolve)))++ $sessions.set([row('renamed', { pinned: true })])+ await flush()+ expect($pinnedSessionIds.get()).toEqual(['renamed'])++ $pinnedSessionIds.set([])+ await flush()+ expect(patch).toHaveBeenCalledWith('renamed', false, undefined)++ applySessionTitle('renamed', 'New Title')+ await flush()++ expect($unconfirmedPinWrites.get().has('renamed')).toBe(true)++ $sessions.set([row('renamed', { pinned: true, title: 'New Title' })])+ await flush()++ expect($pinnedSessionIds.get()).not.toContain('renamed')++ settle({ ok: true })+ await flush()+ await flush()+ })+it('keeps the pin and retries when the write itself fails', async () => {patch.mockImplementationOnce(() => Promise.reject(new Error('offline')))diff --git a/apps/desktop/src/store/session-pin-sync.ts b/apps/desktop/src/store/session-pin-sync.tsindex 6d674d55519..339bf3652ab 100644--- a/apps/desktop/src/store/session-pin-sync.ts+++ b/apps/desktop/src/store/session-pin-sync.ts@@ -49,10 +49,21 @@ const pending = new Set<string>()// write. Hold the guard until a page actually CONFIRMS the written value,// with a cooldown so a row that never comes back can't fence itself forever.const unconfirmed = new Map<string, { at: number; value: boolean }>()-// Rows this module rewrote to match a local toggle. They carry OUR intent, not-// the server's, so they may never confirm (release) a write guard — only a row-// a real list page delivered can do that.-const optimisticRows = new WeakSet<SessionInfo>()+// Marker stamped on rows this module repainted to match a local toggle. They+// carry OUR intent, not the server's, so they may never confirm (release) a+// write guard — only a row a real list page delivered can do that. It is an+// enumerable symbol so a value copy (`{ ...row }`) carries it too: rows are+// re-spread on ordinary user paths (touchSessionActivity on send,+// applySessionTitle on rename), and identity-keyed tracking would drop+// exactly those copies, letting our own painted value read as a remote+// confirmation and release the guard mid-flight.+const optimisticPin = Symbol('optimisticPin')++type OptimisticSessionRow = SessionInfo & { [optimisticPin]?: true }++function isOptimisticRow(row: SessionInfo): boolean {+ return (row as OptimisticSessionRow)[optimisticPin] === true+}/*** The ids `unconfirmed` currently fences, for readers outside this module.@@ -167,8 +178,7 @@ function applyRowPinned(pinId: string, pinned: boolean, profile?: null | string)}changed = true- const updated = { ...row, pinned }- optimisticRows.add(updated)+ const updated: OptimisticSessionRow = { ...row, pinned, [optimisticPin]: true }return updated})@@ -196,9 +206,10 @@ function writePin(id: string, pinned: boolean, profile?: null | string): Promise(err: unknown) => {// A failed write leaves the server on the old value, so the guard would// be fencing out the truth. Drop it and let the page win.- // The painted rows are left as-is: a failed pin is retried by the caller- // (repainting here would fire a reconcile before that retry is booked- // and drop the pin), and the next real page replaces them anyway.+ // The painted rows are left as-is (pin or unpin alike): a failed pin is+ // retried by the caller (repainting here would fire a reconcile before+ // that retry is booked and drop the pin), a failed unpin keeps the local+ // value until the next real page, and the next page replaces them anyway.unconfirmed.delete(id)publishUnconfirmed()throw err@@ -238,10 +249,11 @@ function pullRemotePins(): void {const guard = guardKey ? unconfirmed.get(guardKey) : undefinedif (guard && guardKey) {- if (optimisticRows.has(row)) {- // Our own painted row agrees by construction, so it is not a- // confirmation. Let the cooldown retire the guard as usual; the row- // then matches the local set and the checks below are a no-op.+ if (isOptimisticRow(row)) {+ // Our own painted row agrees by construction (as does any value copy+ // of it — the marker is enumerable and spreads with the row), so it is+ // not a confirmation. Let the cooldown retire the guard as usual; the+ // row then matches the local set and the checks below are a no-op.if (Date.now() - guard.at >= WRITE_GUARD_MS) {unconfirmed.delete(guardKey)}