Skip to content

Conversation

@ammar-agent
Copy link
Collaborator

@ammar-agent ammar-agent commented Dec 6, 2025

Problem

On page reload in Electron, the app fails to restore the previously selected workspace and instead shows the home page with this error:

Workspace 6164ccb357 no longer exists, clearing selection

Root Cause

Two race conditions were causing this:

1. API connection timing

WorkspaceContext's loadWorkspaceMetadata() returned early when api was null (during initial 'connecting' state), but setLoading(false) was still called. This caused App to render with empty metadata while selectedWorkspace was restored from localStorage, triggering the validation effect to clear the selection.

2. Missing metadata guard

Even after fixing the API timing, if selectedWorkspace from localStorage referred to a deleted workspace, the AIView would try to render before the validation effect could clear the stale selection.

Solution

  1. Wait for API: loadWorkspaceMetadata now returns a boolean indicating success. The effect only calls setLoading(false) after actual load. When api becomes available, the effect re-runs.

  2. Guard AIView render: If selectedWorkspace exists but currentMetadata is undefined, return null instead of rendering AIView. The validation effect will clear the stale selection on the next tick.

  3. Remove redundant effect: Removed the simple validation effect (lines 126-130) since the comprehensive one (lines 165-189) handles all cases including missing fields update.


Generated with mux

@ammar-agent ammar-agent force-pushed the fix-workspace-reload-race-condition branch from d251d30 to a13fa3c Compare December 6, 2025 19:51
On page reload, selectedWorkspace is restored from localStorage synchronously
during render, but workspace metadata is loaded asynchronously. If the
selected workspace was deleted or the metadata hasn't loaded yet, rendering
AIView with a non-existent workspace ID causes WorkspaceStore.assertGet()
to throw.

Fix: Guard against missing metadata before rendering AIView. If selectedWorkspace
exists but currentMetadata is undefined, return null instead of rendering.
The existing validation effect will clear the stale selection on the next tick.

This also simplifies the code by removing unnecessary optional chaining since
we now guard at the top of the conditional.

_Generated with mux_
@ammar-agent ammar-agent force-pushed the fix-workspace-reload-race-condition branch from a13fa3c to 19145ce Compare December 6, 2025 21:01
Root cause: WorkspaceContext's loadWorkspaceMetadata() returned early
when api was null (during initial 'connecting' state), but setLoading(false)
was still called. This caused App to render with empty metadata while
selectedWorkspace was restored from localStorage, triggering the validation
effect to clear the selection.

Fix:
- loadWorkspaceMetadata now returns boolean indicating if it actually loaded
- Effect only calls setLoading(false) after successful load
- When api becomes available, effect re-runs due to dependency change

Also:
- Removed redundant validation effect (lines 126-130) - the comprehensive
  one at lines 165-189 handles all cases including missing fields update
@ammario ammario merged commit 5bc5ae3 into main Dec 6, 2025
18 of 19 checks passed
@ammario ammario deleted the fix-workspace-reload-race-condition branch December 6, 2025 21:10
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