Skip to content

fix(frontend): inject orchestrator session into cache before navigating#371

Open
i-trytoohard wants to merge 2 commits into
mainfrom
fix/create-project-session-cache-race
Open

fix(frontend): inject orchestrator session into cache before navigating#371
i-trytoohard wants to merge 2 commits into
mainfrom
fix/create-project-session-cache-race

Conversation

@i-trytoohard

Copy link
Copy Markdown
Collaborator

Fixes #370

Summary

New project creation navigated to /projects/$projectId/sessions/$sessionId before useWorkspaceQuery's cache contained the spawned orchestrator session. SessionView found no session and showed "Session not found" until the 15s refetchInterval eventually caught up — confusing UX on every new project.

Root cause (3 compounding factors):

  1. createProject's optimistic setQueryData (adding workspace with sessions: []) reset dataUpdatedAt to now.
  2. React Query v5.101.0's invalidateQueriesrefetchQueries swallows fetch errors (promise.catch(noop) in queryClient.js:174), so await invalidateQueries(...) always resolves — even when the refetch fails (daemon busy during spawn workload: git worktree + provisioning + Zellij runtime). Cache retains stale sessions: [].
  3. Global staleTime: 10_000 suppresses the mount-triggered refetch in SessionView's observer, because dataUpdatedAt was just reset by the optimistic update.

Fix: After spawnOrchestrator returns the sessionId, optimistically inject the orchestrator WorkspaceSession into the workspace's sessions array via updateWorkspaces, then use refetchQueries (forces fresh data ASAP). SessionView can now find the session immediately regardless of refetch timing.

The backend is not the issue — Manager.Spawn persists the session synchronously (manager.go:191); the race is purely in the React Query cache layer.

Test

cd frontend && npx tsc --noEmit && npx vitest run --exclude e2e
  • TypeScript: ✅ no errors
  • Full unit suite: ✅ 178/178 pass (including SessionView, Sidebar, useWorkspaceQuery tests)

AO Bot and others added 2 commits June 21, 2026 14:41
Fixes #370 — new project creation navigated to the session route before
useWorkspaceQuery's cache contained the spawned orchestrator session,
leaving SessionView stuck on "Session not found" until the 15s
refetchInterval caught up.

Root cause: createProject's optimistic setQueryData (adding the workspace
with sessions: []) reset dataUpdatedAt, and React Query v5's
invalidateQueries swallows refetch errors (refetchQueries does
promise.catch(noop)), so a failed refetch left stale sessions: [] in the
cache. The global staleTime: 10_000 then suppressed a mount-triggered
refetch in SessionView.

Fix: after spawnOrchestrator returns the sessionId, optimistically inject
the orchestrator session into the workspace's sessions array via
updateWorkspaces, then use refetchQueries (forces fresh data ASAP).
SessionView can now find the session immediately regardless of refetch
timing.
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.

Race condition: project creation navigates to session route before workspace cache contains the new session

1 participant