Skip to content

Throw AuthRequired from CopilotAgent.listSessions before auth#311293

Merged
roblourens merged 1 commit intomainfrom
roblou/agents/fix-agent-host-session-display
Apr 20, 2026
Merged

Throw AuthRequired from CopilotAgent.listSessions before auth#311293
roblourens merged 1 commit intomainfrom
roblou/agents/fix-agent-host-session-display

Conversation

@roblourens
Copy link
Copy Markdown
Member

Problem

Agent-host sessions returned by listSessions() did not appear in the Agents sidebar on fresh launch. They only showed up after the user sent a message — at which point a notify/sessionAdded would force the renderer to call getSessions() again, which would then return the real list.

Root cause

CopilotAgent.listSessions() (and _listModels()) silently returned [] whenever _githubToken was unset. On a fresh launch the renderer's eager _refreshSessions() ran before the auth token was resolved, so it cached an empty list. BaseAgentHostSessionsProvider._ensureSessionCache() is one-shot, so it never retried. On reload it appeared to work because the token was already cached and auth completed synchronously fast.

This also violates the AHP contract — per docs/specification/authentication.md, an agent declaring protectedResources with required: true MUST throw AuthRequired (-32007) when used unauthenticated, not lie by returning empty data.

Fix

Server side (copilotAgent.ts):

  • listSessions() and _listModels() no longer early-return []. They go through _ensureClient(), which already throws ProtocolError(AHP_AUTH_REQUIRED, ...) when no token is present.
  • _refreshModels() already guards on !_githubToken and catches errors, so it remains correct.

Renderer side (localAgentHostSessionsProvider.ts):

  • The existing autorun(authenticationPending) triggers _refreshSessions() exactly once when auth settles. This is the natural retry mechanism for the silent listSessions case.
  • The existing bare catch in _refreshSessions swallows the auth error — appropriate here, since unlike createSession we should NOT prompt the user to sign in just to render the sidebar.
  • This is consistent with the existing _isAuthRequiredError pattern in agentHostSessionHandler.ts; it differs only in retry strategy: createSession prompts interactively, listSessions waits for the in-flight auth attempt to settle.

Tests

  • Updated copilotAgent.test.ts "returns empty models and throws AuthRequired for sessions before authentication" to expect the throw.
  • New localAgentHostSessionsProvider.test.ts tests:
    • eagerly populates and fires onDidChangeSessions after construction without a getSessions() call
    • defers eager session list fetch until authentication settles
  • All 13 copilotAgent + 41 localAgentHostSessionsProvider tests pass; full typecheck clean.

(Written by Copilot)

Previously CopilotAgent.listSessions() and _listModels() silently
returned [] when no token was available. This violated the AHP
contract (protectedResources required: true mandates the server
return AuthRequired -32007 when used unauthenticated) and caused
agent-host sessions to not appear in the Agents sidebar on fresh
 the renderer cached the empty list and never retried untillaunch
the user sent a message and the resulting sessionAdded notification
forced a refresh.

Both methods now go through _ensureClient(), which already throws
the right ProtocolError. On the renderer side,
LocalAgentHostSessionsProvider already had an autorun on
authenticationPending that triggers _refreshSessions() once auth
settles; that's the natural retry mechanism for the silent
listSessions case (we don't want to interactively prompt the user
just to render the  that's only appropriate forsidebar
createSession).

(Written by Copilot)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 20, 2026 02:21
@github-actions
Copy link
Copy Markdown
Contributor

Screenshot Changes

Base: df49d483 Current: a5ab6e16

Changed (10)

chat/aiCustomizations/aiCustomizationManagementEditor/McpBrowseMode/Light
Before After
before after
editor/inlineChatAffordance/InlineChatOverlay/Light
Before After
before after
agentSessionsViewer/CompletedRead/Dark
Before After
before after
agentSessionsViewer/CompletedRead/Light
Before After
before after
agentSessionsViewer/WithFileChangesList/Dark
Before After
before after
agentSessionsViewer/WithFileChangesList/Light
Before After
before after
agentSessionsViewer/CloudProvider/Dark
Before After
before after
agentSessionsViewer/CloudProvider/Light
Before After
before after
agentSessionsViewer/BackgroundProvider/Dark
Before After
before after
agentSessionsViewer/BackgroundProvider/Light
Before After
before after

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an Agents sidebar startup issue where listSessions() could return an empty list before authentication completed, causing the renderer to cache “no sessions” and never retry. The change aligns Copilot’s agent-host implementation with the AHP authentication contract by throwing AuthRequired when unauthenticated, and adds a renderer-side eager refresh that waits for the initial auth pass to settle.

Changes:

  • Server: CopilotAgent.listSessions() / _listModels() now go through _ensureClient() and throw AuthRequired when unauthenticated (instead of returning []).
  • Renderer: LocalAgentHostSessionsProvider eagerly refreshes sessions once authenticationPending settles.
  • Tests updated/added to validate the new auth-required behavior and the eager-refresh flow.
Show a summary per file
File Description
src/vs/platform/agentHost/node/copilot/copilotAgent.ts Removes unauthenticated “return []” behavior so listSessions/_listModels correctly throw AuthRequired.
src/vs/platform/agentHost/test/node/copilotAgent.test.ts Updates expectations to assert listSessions() rejects with AuthRequired before authentication.
src/vs/sessions/contrib/agentHost/browser/localAgentHostSessionsProvider.ts Adds an autorun to refresh/populate the session cache after the initial auth pass settles.
src/vs/sessions/contrib/agentHost/test/browser/localAgentHostSessionsProvider.test.ts Adds coverage for eager session population and for deferring the eager refresh until auth settles.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 0

@roblourens roblourens marked this pull request as ready for review April 20, 2026 02:27
@roblourens roblourens enabled auto-merge (squash) April 20, 2026 02:27
@roblourens roblourens merged commit 332f8ff into main Apr 20, 2026
30 checks passed
@roblourens roblourens deleted the roblou/agents/fix-agent-host-session-display branch April 20, 2026 02:42
@vs-code-engineering vs-code-engineering bot added this to the 1.117.0 milestone Apr 20, 2026
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.

3 participants