Skip to content

fix: optimistic session creation for instant UI feedback#252

Merged
StephaneDelcroix merged 3 commits intomainfrom
fix/theres-often-a-delay-between-create-sess-20260302-0821
Mar 3, 2026
Merged

fix: optimistic session creation for instant UI feedback#252
StephaneDelcroix merged 3 commits intomainfrom
fix/theres-often-a-delay-between-create-sess-20260302-0821

Conversation

@StephaneDelcroix
Copy link
Copy Markdown
Collaborator

Problem

When creating a session in local (Persistent/Embedded) mode, the UI blocked during the slow SDK call (_client.CreateSessionAsync). The session only appeared in the sidebar after the SDK finished creating it, causing a noticeable delay where the user had no visual feedback.

Fix

Apply the same optimistic UI pattern already used for remote mode to local mode:

  1. Immediate placeholder: Create an AgentSessionInfo with IsCreating = true and add it to _sessions before the SDK call
  2. Instant UI update: Fire OnStateChanged and set _activeSessionName so the sidebar and chat view update immediately
  3. Background completion: The SDK session creation continues asynchronously
  4. Success: Update the state with the real CopilotSession, set IsCreating = false
  5. Failure cleanup: Remove the placeholder from _sessions and Organization, re-throw

Also adds:

  • IsCreating property on AgentSessionInfo to signal creating state to the UI
  • Guard in SendPromptAsync to reject prompts to sessions still being created
  • SessionState.Session changed from init to set to allow updating after optimistic add

Tests Added

  • IsCreating_DefaultsToFalse — model property test
  • IsCreating_CanBeSetAndCleared — model property test
  • CreateSessionAsync_DemoMode_IsCreatingIsFalse — demo path doesn't use IsCreating
  • CreateSessionAsync_DemoMode_SessionActiveImmediately — session is immediately active
  • CreateSession_BeforeInitialize_NoOptimisticAdd — no placeholder on init failure
  • CreateSessionAsync_EmptyName_Throws — validation before optimistic add

All 1666 tests pass (1 pre-existing unrelated failure in PlatformHelperTests).

@StephaneDelcroix StephaneDelcroix force-pushed the fix/theres-often-a-delay-between-create-sess-20260302-0821 branch from cc4a178 to eed392b Compare March 3, 2026 11:58
StephaneDelcroix and others added 3 commits March 3, 2026 14:34
In local (Persistent/Embedded) mode, CreateSessionAsync blocked the UI
during the slow SDK call. The session only appeared in the sidebar after
the SDK finished, causing a noticeable delay.

Apply the same optimistic UI pattern already used for remote mode:
- Add a placeholder AgentSessionInfo with IsCreating=true immediately
- Fire OnStateChanged so the sidebar and chat view update instantly
- Complete the SDK session creation in the background
- On success: update the state with the real CopilotSession
- On failure: clean up the placeholder and re-throw

Also adds a guard in SendPromptAsync to reject prompts to sessions
that are still being created.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Dispose existing session before overwriting in _sessions[name] to
  prevent leaking the old CopilotSession when a name collision occurs
- Save previousActiveSessionName before optimistic add and restore it
  in the catch block so _activeSessionName is not left dangling after
  a failed SDK session creation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
If a session is closed while awaiting SDK creation, the returned
CopilotSession would be assigned to an orphaned state object and
leak permanently (with a dangling event handler). Now checks
_sessions.ContainsKey after the await and disposes the SDK session
if it was removed during creation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@StephaneDelcroix StephaneDelcroix force-pushed the fix/theres-often-a-delay-between-create-sess-20260302-0821 branch from eed392b to 02f4f2f Compare March 3, 2026 13:36
Copy link
Copy Markdown
Collaborator Author

@StephaneDelcroix StephaneDelcroix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multi-model consensus review (5 models × 2 rounds)

Previous Findings -- All Fixed

Finding Status
🔴 CRITICAL -- Dangling _activeSessionName after SDK creation failure FIXED -- previousActiveSessionName saved/restored in catch block
🔴 CRITICAL -- Silent overwrite of _sessions[name] leaks CopilotSession FIXED -- Existing session disposed before overwrite
🟡 MODERATE -- Close-during-create race leaks SDK session FIXED -- !_sessions.ContainsKey(name) guard after await CreateSessionAsync, disposes orphaned session

Summary

Three fix commits address all consensus findings from two review rounds:

  1. fix: optimistic session creation for instant UI feedback -- original feature
  2. Fix session overwrite leak and dangling _activeSessionName -- fixes both CRITICALs
  3. Guard against close-during-create race -- fixes the MODERATE race condition

The IsCreating flag correctly guards SendPromptAsync (line 1825) to prevent prompts during creation. CloseSessionCoreAsync handles null! Session via is not null check. Tests pass (1666/1667, 1 pre-existing).

Action: ✅ Approve

@StephaneDelcroix StephaneDelcroix merged commit c69a608 into main Mar 3, 2026
@StephaneDelcroix StephaneDelcroix deleted the fix/theres-often-a-delay-between-create-sess-20260302-0821 branch March 3, 2026 13:52
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