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.

Comment thread pkg/cmd/agent-task/shared/capi.go
Comment thread pkg/cmd/agent-task/shared/capi_test.go
Comment thread pkg/cmd/agent-task/capi/sessions.go
@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! 🚢

Comment thread pkg/cmd/agent-task/shared/capi_test.go Outdated
Comment thread pkg/cmd/agent-task/capi/client.go Outdated
Comment thread pkg/cmd/agent-task/capi/sessions.go
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
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Apr 2, 2026
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://github.com/cli/cli) | minor | `v2.88.1` → `v2.89.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.89.0`](https://github.com/cli/cli/releases/tag/v2.89.0): GitHub CLI 2.89.0

[Compare Source](cli/cli@v2.88.1...v2.89.0)

#### :copilot: `gh agent-task` now works on ghe.com tenancies

`gh agent-task` commands previously failed with `401 Unauthorized` for users on ghe.com tenancy hosts because the Copilot API URL was hardcoded. The URL is now resolved dynamically per host, so `gh agent-task` works correctly regardless of your GitHub hosting environment.

#### Experimental new prompter

A new TUI-based prompter powered by [charmbracelet/huh](https://github.com/charmbracelet/huh) is available behind the `GH_EXPERIMENTAL_PROMPTER` environment variable. This is an early preview — try it out and share feedback!

```
export GH_EXPERIMENTAL_PROMPTER=1
```

#### `gh issue create` and `gh issue transfer` no longer require extra token scopes

`gh issue create` and `gh issue transfer` previously fetched repository fields they didn't need, which could require additional token scopes. These commands now fetch only the minimal fields necessary for issue operations.

#### What's Changed

##### ✨ Features

- `gh pr create`, `gh issue create`, `gh issue edit`: search-based assignee selection and login-based mutation on github.com by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;13009](cli/cli#13009)
- Add experimental huh-only prompter gated by `GH_EXPERIMENTAL_PROMPTER` by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;12859](cli/cli#12859)

##### 🐛 Fixes

- fix(agent-task): resolve Copilot API URL dynamically for ghe.com tenancies by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;12956](cli/cli#12956)
- fix(issue): avoid fetching unnecessary fields in `issue create` and `issue transfer` by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12884](cli/cli#12884)
- fix: resolve data race in codespaces port forwarder by [@&#8203;Lslightly](https://github.com/Lslightly) in [#&#8203;13033](cli/cli#13033)

##### 📚 Docs & Chores

- Record agentic invocations in User-Agent header by [@&#8203;williammartin](https://github.com/williammartin) in [#&#8203;13023](cli/cli#13023)
- docs: clarify that `gh pr edit --add-reviewer` can re-request reviews by [@&#8203;joshjohanning](https://github.com/joshjohanning) in [#&#8203;13021](cli/cli#13021)
- Add AGENTS.md by [@&#8203;williammartin](https://github.com/williammartin) in [#&#8203;13024](cli/cli#13024)
- Fix typo: remove extra space in README.md link by [@&#8203;realMelTuc](https://github.com/realMelTuc) in [#&#8203;12725](cli/cli#12725)
- Align triage.md with current triage process by [@&#8203;tidy-dev](https://github.com/tidy-dev) in [#&#8203;13030](cli/cli#13030)
- Remove auto-labels from issue templates by [@&#8203;tidy-dev](https://github.com/tidy-dev) in [#&#8203;12972](cli/cli#12972)
- Consolidate actor-mode signals into `ApiActorsSupported` by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;13025](cli/cli#13025)
- Fix acceptance test failures: git identity, headRepository JSON, obsolete traversal test by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;13037](cli/cli#13037)

##### :dependabot: Dependencies

- chore(deps): bump google.golang.org/grpc from 1.79.2 to 1.79.3 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;12963](cli/cli#12963)
- chore(deps): bump github.com/google/go-containerregistry from 0.20.7 to 0.21.3 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;12962](cli/cli#12962)
- chore(deps): bump github.com/zalando/go-keyring from 0.2.6 to 0.2.8 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13031](cli/cli#13031)
- chore(deps): bump microsoft/setup-msbuild from 2.0.0 to 3.0.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13005](cli/cli#13005)
- chore(deps): bump mislav/bump-homebrew-formula-action from 3.6 to 4.1 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13004](cli/cli#13004)
- chore(deps): bump azure/login from 2.3.0 to 3.0.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;12951](cli/cli#12951)

#### New Contributors

- [@&#8203;joshjohanning](https://github.com/joshjohanning) made their first contribution in [#&#8203;13021](cli/cli#13021)
- [@&#8203;realMelTuc](https://github.com/realMelTuc) made their first contribution in [#&#8203;12725](cli/cli#12725)
- [@&#8203;Lslightly](https://github.com/Lslightly) made their first contribution in [#&#8203;13033](cli/cli#13033)

**Full Changelog**: [v2.88.1...v2.89.0](cli/cli@v2.88.1...v2.89.0)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My45MS40IiwidXBkYXRlZEluVmVyIjoiNDMuOTEuNCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6Om1pbm9yIl19-->
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