Skip to content

fix: command palette pin/unpin toggle reads stale closure#64

Open
samueljklee wants to merge 1 commit intomainfrom
fix/command-palette-pin-unpin
Open

fix: command palette pin/unpin toggle reads stale closure#64
samueljklee wants to merge 1 commit intomainfrom
fix/command-palette-pin-unpin

Conversation

@samueljklee
Copy link
Contributor

Summary

The command palette "Pin/Unpin Session" command only ever pinned sessions — it never unpinned them.

Root Cause

The pin-session handler is registered inside a useEffect and captures pinnedSessionIds from the closure scope. Since pinnedSessionIds is React state and the effect does not re-run when it changes, the handler always sees the initial empty Set(). This means isPinned is always false, so the command always pins and never unpins.

This is a classic stale closure bug in React hooks.

Fix

- const isPinned = pinnedSessionIds.has(sessionIdRef.current);
+ const isPinned = cmdCtxRef.current.pinnedSessionIds.has(sessionIdRef.current);

Read from cmdCtxRef.current.pinnedSessionIds instead of the stale closure variable. The cmdCtxRef ref is already updated every render with the current pinned session IDs, so it always has the fresh value. This matches the existing pattern used for sessionIdRef.current on the adjacent line.

@samueljklee samueljklee force-pushed the fix/command-palette-pin-unpin branch from 96888d5 to a5babe0 Compare March 24, 2026 23:09
The "Pin/Unpin Session" command palette handler was registered inside a
useEffect and captured `pinnedSessionIds` from the closure scope. Since
pinnedSessionIds is React state and the effect does not re-run when it
changes, the handler always saw the initial empty Set(), so isPinned was
always false and the command only ever pinned sessions.

Fix: read from `cmdCtxRef.current.pinnedSessionIds` instead, which is
already updated every render with the current pinned set. This matches
the existing pattern used for `sessionIdRef.current` on the adjacent line.

🤖 Generated with [Amplifier](https://github.com/microsoft/amplifier)

Co-Authored-By: Amplifier <240397093+microsoft-amplifier@users.noreply.github.com>
@samueljklee samueljklee force-pushed the fix/command-palette-pin-unpin branch from a5babe0 to 5a8e855 Compare March 25, 2026 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant