Skip to content

Use shallow equality in session selector hooks#1358

Merged
jonathanlab merged 1 commit intomainfrom
twixes/stable-empty-selector-refs
Mar 30, 2026
Merged

Use shallow equality in session selector hooks#1358
jonathanlab merged 1 commit intomainfrom
twixes/stable-empty-selector-refs

Conversation

@Twixes
Copy link
Copy Markdown
Member

@Twixes Twixes commented Mar 30, 2026

Problem

During agent streaming, the session store fires on every event. Selector hooks like useAvailableCommandsForTask derive values (e.g. scanning events backwards for available commands) that return the same contents but a new reference each time. Zustand's default === check sees a different reference and re-renders all subscribers, even though nothing meaningful changed.

Changes

Pass shallow from zustand/shallow as the equality function on the four hooks that return arrays or Maps. This makes the comparison value-aware — shallow compares array elements and Map entries by reference instead of comparing the container by reference.

Zustand selectors use === by default, so derived values like
extractAvailableCommandsFromEvents() or fallback empty arrays/Maps
cause re-renders even when the contents haven't changed. Adding
shallow as the equality function makes these hooks value-aware.
@Twixes Twixes force-pushed the twixes/stable-empty-selector-refs branch from 72ad60b to 4e53b7b Compare March 30, 2026 07:33
@Twixes Twixes changed the title Use stable empty refs in session selector hooks Use shallow equality in session selector hooks Mar 30, 2026
@jonathanlab jonathanlab merged commit dd244a0 into main Mar 30, 2026
14 checks passed
@jonathanlab jonathanlab deleted the twixes/stable-empty-selector-refs branch March 30, 2026 09:22
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.

2 participants