Skip to content

Refresh Agent Host sessions after connector authorization - #338934

Merged
Paul (pwang347) merged 4 commits into
mainfrom
fix/workiq-connector-auth-refresh
Sep 30, 2026
Merged

Paul (pwang347) merged 4 commits into
mainfrom
fix/workiq-connector-auth-refresh

Conversation

@pwang347

Copy link
Copy Markdown
Contributor

Related to microsoft/vscode-internalbacklog#9799

Refreshes live Agent Host Copilot sessions when the active GitHub account's connector authorization changes. This covers OAuth scope upgrades that retain the same access-token string: the connector catalog is invalidated as before, and the existing Agent Host refresh path now recreates the SDK session on the next turn so its tool snapshot includes newly available connector tools.

Adds regression coverage for upgrading an active account from ordinary GitHub scopes to write:plugin_gateway_connections without changing its token.

Testing

  • npm run transpile-client — passed
  • ./scripts/test.sh --run src/vs/workbench/contrib/chat/test/browser/aiCustomization/copilotConnectorsService.test.ts --run src/vs/platform/agentHost/test/node/copilotAgent.test.ts --grep "[Cc]onnector" — passed, 59 tests
  • npm run typecheck-client — passed

How to Test

  1. Start an Agent Host session before connector authorization is complete.
  2. Authorize the active GitHub account for Copilot connectors.
  3. Retry in the same session and verify that the session refreshes and connector tools become available.

Copilot AI balanced review requested due to automatic review settings September 30, 2026 16:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Routine token renewals currently trigger unnecessary SDK session recreation.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Refreshes live Agent Host sessions after GitHub connector authorization changes.

Changes:

  • Triggers Agent Host connector-session refreshes on authentication session events.
  • Adds regression coverage for scope upgrades retaining the same token.
File Description
copilotConnectorsService.ts Triggers live-session refresh after relevant authentication events.
copilotConnectorsService.test.ts Tests connector authorization scope upgrades.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Screenshot Changes

Base: 5e418d6d Current: e5fd4d8a

Changed (2)

chat/aiCustomizations/aiCustomizationManagementEditor/DiscoverInfiniteScroll/Light
Before After
before after
chat/aiCustomizations/aiCustomizationManagementEditor/DiscoverPluginsLoadingMore/Light
Before After
before after

2 insignificant change(s) omitted (≤20 px, Δ≤2). See CI logs for details.

@pwang347
Paul (pwang347) marked this pull request as ready for review September 30, 2026 18:06
@pwang347
Paul (pwang347) merged commit 515f346 into main Sep 30, 2026
55 of 56 checks passed
@pwang347
Paul (pwang347) deleted the fix/workiq-connector-auth-refresh branch September 30, 2026 21:53
@vs-code-engineering vs-code-engineering Bot added this to the 1.141.0 milestone Sep 30, 2026
Vritant Bhardwaj (vritant24) added a commit that referenced this pull request Oct 19, 2026
…ation (#342479)

<!-- Thank you for submitting a Pull Request. Please:
* Read our Pull Request guidelines:
  https://github.com/microsoft/vscode/wiki/How-to-Contribute#pull-requests
* Associate an issue with the Pull Request.
* Ensure that the code is up-to-date with the `main` branch.
* Include a description of the proposed changes and how to test them.
-->

Fixes #341123

## Problem

The runtime sends Copilot Connector MCP requests with **only** the SDK session's GitHub credential (the token set through `gitHubAuth.setCredentials` / `gitHubToken`). When the gateway returned `403 insufficient_scope`, the runtime fell back to a generic `mcp.oauth_required` (`reason=upscope`). VS Code's **Sign In**, and silent auto-auth, then created a GitHub session with only `write:plugin_gateway_connections` and forwarded it as an MCP token. The runtime ignores that token for connector servers, so the server restarted with the old credential, got another 403, and looped. Meanwhile the client logged "MCP authentication succeeded".

A second issue made the working path unreachable. `CopilotConnectorsService.authorize()` returned early if **any** same-account session had the connector scope. That includes the narrow session created by MCP Sign In, which never qualifies as the Copilot credential. So Authorize did nothing in exactly the state the issue logs show.

## Changes

**Sign In for connector servers goes through Connector authorization**
- `authenticateMcpServer` detects connector-owned servers. Detection uses new host-published `vscode.mcpServerConnectorName` metadata, or a `write:plugin_gateway_connections` challenge as a fallback.
- For those servers it calls `ICopilotConnectorsService.authorize()`, forwards the Copilot credential (which prefers the full-scope session), then force-reconciles that connector through a new narrow `refreshAgentHostSessions(connectorName)` API.
- `resolveMcpServerAuthentication` refuses connector-scope challenges. So neither the silent nor the interactive path creates a narrow session or forwards an MCP token for a connector.
- `authorize()` now skips consent only when a session has the connector scope **and** every scope of the default session.

**The agent host does not answer connector upscope requests with tokens**
- `CopilotAgentSession.resolveMcpAuthentication` skips connector-owned pending requests.
- Before a forced reconcile, affected servers are shown as Starting. After the reconcile succeeds, pending requests from before it are cancelled; requests raised by the restarted server are kept. If a turn is active, refresh is deferred, so stale requests are released straight away instead.
- #340547's rejected-token suppression is unchanged.

**Truthful reporting**
- The forwarding log now says the token was forwarded, not that authentication succeeded.
- `authenticateMcpServer` reports success only once the server leaves AuthRequired. This is a bounded wait (60s) that is cancellation-safe and cleans up its listeners. Connector servers must reach Ready.
- If a connector returns the same challenge after restarting, Sign In returns `false` and shows a localized message. It asks the user to authorize Copilot connectors with the same GitHub account they use for Copilot.
- **Behaviour change for non-connector servers:** if a forwarded token leaves the challenge unchanged, Sign In now waits up to 60s and returns `false`. Previously it returned `true` right after forwarding.

**`ConnectorStatus.authorizationRequirement`**
- Reconcile now reports connector ownership for each runtime server, and `authorizationRequirement`.
- A connector server in `needs-auth` with no `mcp.oauth_required` is shown as AuthRequired (insufficient scope). This covers the planned runtime fix that will stop emitting `mcp.oauth_required` for connector 403s.

## Notes for reviewers

- **Same token string after Authorize (#338934):** `_applyGitHubToken` skips `setCredentials`. That should be fine, because the runtime re-reads the session credential on every request and the forced reconcile always restarts the server. I have not confirmed against the live gateway that GitHub widens scope on the same token string.
- **Forced reconcile:** the runtime's `replace_managed_servers` already cancels its pending OAuth before restarting the server, so cancelling VS Code's stale entry afterwards is safe.
- **Mission Control / scoped credentials:** `updateGitHubCredentials` is only called for static-token sessions, so nothing throws. The challenge comes back and Sign In returns `false` with the message.
- **Known gaps:**
  - Connector refresh only exists for the local agent host, so on a remote host Sign In authorizes but usually times out.
  - Servers identified only by the scope fallback can't get a targeted reconcile.
  - The end-to-end integration test (coexisting sessions, Sign In through tool discovery) was not added. It needs a real runtime and gateway.

## How to test

1. Use a default GitHub session that lacks `write:plugin_gateway_connections`. Optionally add a narrow session that has only that scope.
2. Open an Agents session with the WorkIQ / Kusto connectors and click **Sign In** on a connector row.
3. Expect a consent prompt for the default scopes plus `write:plugin_gateway_connections` on the same account. Then the connector restarts, reaches Ready, and lists its tools. The gateway's `x-oauth-scopes` should include the connector scope.

Validation run locally:

| Command | Result |
|---|---|
| `./scripts/test.sh --run` agentHostAuth, copilotConnectorsService, agentHostCustomizationService, copilotAgentSession, copilotSessionLauncher, agentMetaReaders | 1621 passing |
| `./scripts/test.sh --run` mcpCustomizationController, sessions agentHostCustomizationService, agentHostChatContribution, workbenchAgentHostCustomizationService, chatMcpAuthenticationContentPart, chatToolAuthenticationSubPart | 781 passing |
| `./scripts/test.sh --run src/vs/workbench/contrib/chat/test/browser/agentSessions/agentHostClientTools.test.ts` | 73 passing |
| `npm run eslint -- <changed files>` | passed |
| `npm run typecheck-client` | passed |
| `npm run valid-layers-check` | passed |

Not run: integration tests (`copilotConnectors.integrationTest.ts` was only updated for the new reconcile result shape) and a manual end-to-end run against the live gateway.
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Nov 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants