ui/goose2/ui_improvements/state_management/goose2-zustand-state-management-improvement-plan.md
Goose2 Zustand State Management Improvement Plan
This plan translates the findings from goose2-zustand-state-management-review.md into a concrete implementation sequence.
The plan is optimized for:
This is a single master plan. It is intentionally sequenced so that higher-leverage, lower-risk changes happen before larger store refactors.
Goals
useShallow and Immer where they materially improve maintainability.Sequencing Principles
High-Level Phase Order
useShallow where appropriate.projectStore into clearer layers.Priority Snapshot
Highest priority
chatSessionStoreMedium priority
agentStore UI state and chatSessionStore UI stateprojectStoreLower priority but useful
Dependency Logic
Phase 1: Remove Whole-Store Subscriptions
Goal
Why first
Primary files
Detailed tasks
AppShell.tsx
useChatStore(), useChatSessionStore(), useAgentStore(), and useProjectStore() broad subscriptions with specific selectors.getState() usage in async helper callbacks where necessary.Sidebar.tsx
chatStore, agentStoreState, and projectStoreState subscriptions with targeted selectors.usePersonas.ts
const store = useAgentStore() with explicit state/action selectors.useChat.ts
Success criteria
useSomeStore() with no selector.Phase 2: Introduce a Selector-First Read Layer
Goal
Why second
useShallow only where needed.Primary files to add or refactor
src/features/chat/stores/chatSelectors.tssrc/features/chat/stores/chatSessionSelectors.tssrc/features/agents/stores/agentSelectors.tssrc/features/projects/stores/projectSelectors.tsDetailed tasks
useShallow where components need multiple values together.useShallow.lib/ helpers if it is not inherently store-specific.Where useShallow should be used
Where useShallow should not be used
Success criteria
useShallow appears only on object/array selectors where it adds value.Phase 3: Separate Backend Side Effects From Generic Store Actions
Goal
Why third
Primary file
Current problem
updateSession looks like a local patch action but can also trigger backend rename/project-update side effects.Detailed tasks
updateSession is the main entry point:
Possible target modules
src/features/chat/api/ for backend-facing operationssrc/features/chat/hooks/ for orchestration wrappersSuccess criteria
Phase 4: Split the Broadest Stores by Responsibility
Goal
Why fourth
AgentStore Boundary Work
Primary file
Current mix
Detailed tasks
personaEditorOpeneditingPersonapersonaEditorModePossible target files
agentCatalogStore.tsagentUiStore.tsChatSessionStore Boundary Work
Primary file
Current mix
Detailed tasks
contextPanelOpenBySessionactiveWorkspaceBySessionPossible target files
chatSessionStore.tschatSessionUiStore.tsChatStore Re-Evaluation
Primary file
Current mix
Detailed tasks
Success criteria for Phase 4
Phase 5: Decide Chat Session Workflow Failure Policy
Goal
Why this phase matters
chatSessionStore.Detailed tasks
Success criteria
Phase 6: Refactor projectStore Into Clearer Layers
Goal
Why this phase matters
projectStore currently combines state, cache hydration, persistence, CRUD orchestration, and optimistic mutation policy.Primary file
Current mix
Detailed tasks
Likely support files
src/features/projects/hooks/useProjectCommands.tssrc/features/projects/stores/projectSelectors.tsLikely consumer files to revisit
Success criteria
Phase 7: Standardize Persistence Boundaries
Goal
Why this phase matters
Primary files
Detailed tasks
persist where appropriate.partialize and versioning for persisted stores.Good persistence candidates
Avoid persisting by default
Success criteria
Phase 8: Standardize Test Reset Patterns and Close Coverage Gaps
Goal
Why this phase matters
Primary files
setState(...) setup patternsDetailed tasks
Coverage gaps to address
projectStore
providerInventoryStore
Success criteria
Phase 9: Optional Immer Adoption for Nested Update Ergonomics
Goal
Why this phase matters
Primary candidate
Secondary candidate
Detailed tasks
Success criteria
Suggested PR Breakdown
AppShell, Sidebar, usePersonas, useChatuseShallowchatSessionStore local patching from backend side effectsagentUiStore from agentStorechatSessionUiStore from chatSessionStoreprojectStore orchestration and mutation policyPlan Usage