Skip to content

fix(agent-task): resolve Copilot API URL dynamically#12956

Merged
BagToad merged 4 commits intotrunkfrom
kw/agent-task-tenant-header
Mar 18, 2026
Merged

fix(agent-task): resolve Copilot API URL dynamically#12956
BagToad merged 4 commits intotrunkfrom
kw/agent-task-tenant-header

Conversation

@BagToad
Copy link
Copy Markdown
Member

@BagToad BagToad commented Mar 18, 2026

Description

gh agent-task commands fail with 401 Unauthorized for users on ghe.com tenancy hosts. The Copilot API URL was hardcoded to api.githubcopilot.com, but ghe.com tenants use a different endpoint. The URL must be resolved dynamically per host.

Key changes

  • shared/capi.go: Added resolveCapiURL which queries viewer.copilotEndpoints.api via GraphQL to get the correct Copilot API URL for the user's host. Uses the cached HTTP client to avoid redundant lookups.
  • capi/client.go: Removed hardcoded baseCAPIURL and capiHost constants. NewCAPIClient now accepts a capiURL parameter. The transport extracts the host from the resolved URL for conditional header matching.
  • capi/sessions.go / capi/job.go: Replaced baseCAPIURL constant references with the instance-level capiURL field.

Notes for Reviewers

  • This may also resolve the Copilot API URL to a different host than api.githubcopilot.com for some github.com users, depending on their plan.
  • The IsEnterprise guard in agent_task.go already allows ghe.com tenancy hosts through (they are not considered "enterprise" by go-gh), so no changes were needed there.

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

This PR fixes gh agent-task failures on ghe.com tenancy hosts by dynamically resolving the correct Copilot API base URL per authenticated host via a GraphQL lookup, instead of assuming https://api.githubcopilot.com.

Changes:

  • Add Copilot API endpoint resolution (viewer.copilotEndpoints.api) and wire it into CapiClientFunc.
  • Refactor the CAPI client to accept a resolved base URL (removing hardcoded constants) and use instance-level URL construction.
  • Update session/job CAPI calls and tests to use the new URL plumbing.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/cmd/agent-task/shared/capi.go Resolves the Copilot API URL via GraphQL and passes it into the CAPI client constructor.
pkg/cmd/agent-task/shared/capi_test.go Adds tests covering endpoint resolution and that CapiClientFunc triggers the GraphQL lookup.
pkg/cmd/agent-task/capi/client.go Removes hardcoded Copilot base URL; adds capiURL to the client and derives host for conditional headers.
pkg/cmd/agent-task/capi/sessions.go Switches session endpoints to build URLs from the instance capiURL.
pkg/cmd/agent-task/capi/sessions_test.go Updates tests to construct the client with an explicit base URL parameter.
pkg/cmd/agent-task/capi/job.go Switches jobs endpoints to use the instance capiURL via a helper method.
pkg/cmd/agent-task/capi/job_test.go Updates tests to construct the client with an explicit base URL parameter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@BagToad BagToad force-pushed the kw/agent-task-tenant-header branch from d71b1be to 15def46 Compare March 18, 2026 00:50
Query viewer.copilotEndpoints.api to get the correct Copilot API URL
for the user's host instead of hardcoding api.githubcopilot.com. This
fixes 401 errors for ghe.com tenancy users whose Copilot API lives at
a different endpoint.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@BagToad BagToad force-pushed the kw/agent-task-tenant-header branch from 15def46 to 4da2abf Compare March 18, 2026 00:51
Copy link
Copy Markdown
Member

@babakks babakks left a comment

Choose a reason for hiding this comment

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

LGTM! 🚢

BagToad and others added 3 commits March 18, 2026 11:59
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
- Remove redundant assert on GraphQL request path in test
- Rename capiURL to capiBaseURL for clarity

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@BagToad BagToad enabled auto-merge (squash) March 18, 2026 18:07
@BagToad BagToad merged commit 78b958f into trunk Mar 18, 2026
11 checks passed
@BagToad BagToad deleted the kw/agent-task-tenant-header branch March 18, 2026 18:14
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