Skip to content

Show gh event listen hints only when event subscriptions are enabled - #15223

Merged
BagToad merged 15 commits into
trunkfrom
babakks/event-hint-feature-detection
Dec 3, 2026
Merged

BagToad merged 15 commits into
trunkfrom
babakks/event-hint-feature-detection

Conversation

@babakks

@babakks babakks commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Description

When gh detects that an agent is calling it, gh pr create, gh pr view, gh run view, gh run watch and gh api print a hint on stderr suggesting gh event listen instead of polling. The Event Streams API behind gh event is in preview and only enabled for some accounts, but the hint was shown to every agent on github.com. Agents on accounts without access followed the hint and got a 403.

This PR shows the hints only when feature detection says event subscriptions are enabled for the account:

  • EventsFeatures sends GET /event-subscriptions?per_page=1 with a gh-feature-detection: true header. A 200 means event subscriptions are enabled. A 403 (not enabled for the account) or a 404 (the host does not have the endpoint) means they are not. Any other failure, including the 3 second timeout, also hides the hint.
  • The result is cached for 24 hours, including 403 and 404 responses, which the HTTP cache does not store by default. NewCachedHTTPClientWithStatuses opts into those statuses through the X-GH-CACHE-STATUSES header added in feat(api): allow requests to opt into caching extra response statuses go-gh#335.
  • The check only runs when a hint would otherwise be printed, so human callers never trigger it.
  • Event subscriptions remain github.com only. On GitHub Enterprise Server and GHE.com hosts, EventsFeatures reports them unavailable without sending a request, and the hints and gh event commands keep refusing those hosts as before.
  • Since the hint is now only shown to enabled accounts, its "Event subscriptions are in preview and may not be enabled for your account" note is dropped.

docs/event-listen-preview.md describes how feature detection gates the hints.

How did you test this change?

I ran a build of this branch against github.com, with an account that has event subscriptions enabled, and against a GitHub Enterprise Server 3.23 host, which does not have the API. Unless stated otherwise, gh ran as an agent caller (COPILOT_CLI set), with an empty cache directory and GH_DEBUG=api to see the requests. The GHES host and repository are shown as <ghes-host> and OWNER/REPO.

I ran these scenarios before Restore github.com host restriction for event streaming, which does not change the github.com paths they exercise. I have not rerun the GitHub Enterprise Server scenarios against that commit, and I left out the scenarios for behavior it reverts.

Feature detection and caching

Given an agent caller and an empty cache
When I ran `gh api repos/cli/cli/pulls/15213 --jq .state` (an open pull request)
Then I saw `GET /event-subscriptions?per_page=1` with `Gh-Feature-Detection: true`, answered with 200
And I saw the `gh event listen --resource https://github.com/cli/cli/pull/15213 ...` hint on stderr

Given the same cache
When I ran the same command again
Then the detection response had the same `Date` as the first one, so it came from the cache
And I saw the hint again

Given a human caller (agent environment variables unset)
When I ran `gh api repos/cli/cli/pulls/15213 --jq .state`
Then I saw no request to `/event-subscriptions` and no hint

Hints

Given an agent caller
When I ran `gh pr view 15213 -R cli/cli --json state`
Then I saw `{"state":"OPEN"}` on stdout and the listen hint for https://github.com/cli/cli/pull/15213 on stderr

Given an agent caller
When I ran `gh api https://api.github.com/repos/cli/cli/pulls/15213 --jq .state`
Then I saw the listen hint for https://github.com/cli/cli/pull/15213

Given an agent caller
When I ran `gh api --hostname <ghes-host> https://api.github.com/repos/cli/cli/pulls/15213 --jq .state`
Then I saw the listen hint for https://github.com/cli/cli/pull/15213, taking the host from the URL rather than `--hostname`

Given an agent caller
When I ran `gh pr view https://<ghes-host>/OWNER/REPO/pull/2 --json state`
Then I saw `{"state":"OPEN"}` and no hint

Given an agent caller
When I ran `gh api --verbose repos/cli/cli/pulls/15213`
Then stdout did not include the detection request

Given an agent caller
When I ran `gh pr view 15205 -R cli/cli --json state` (a merged pull request)
Then I saw `{"state":"MERGED"}`, no request to `/event-subscriptions`, and no hint

gh event on github.com

Given an account with event subscriptions enabled
When I ran `gh event listen --resource https://github.com/cli/cli/pull/15213 --event com.github.pull_request.state.changed.v1 --timeout 5s`
Then I saw "Listening for events." and then "Timed out waiting for events.", and it exited with status 8
And I deleted the subscription it created with `gh event subscription delete`, and `gh event subscription list --json id` printed `[]`

I did not exercise the gh run view and gh run watch hints end to end, because no pull request run was in progress at the time.

Key points

  • The check hits a real endpoint. Other detectors inspect the GraphQL schema, which cannot tell whether an account is enabled for a preview. The gh-feature-detection: true header marks the request as feature detection rather than data retrieval.
  • 403 and 404 are cached for 24 hours. Without that, every hint on an account without access would send a request. The cost is that an account enabled during the day may wait up to 24 hours to see the hints.
  • The github.com restriction stays. GitHub Enterprise Server and GHE.com do not serve the API yet, so detection skips them without a request rather than relying on their 404.

Notes for reviewers

The first four commits are in internal/featuredetection and api: a NewCachedDetector constructor, NewCachedHTTPClientWithStatuses, detection clients built from a cache TTL, and EventsFeatures. The next commit wires the hints to the detector. The commits after it that lifted the host restriction are reverted by Restore github.com host restriction for event streaming, so from there on the overall diff is easier to review than the individual commits.

cli/go-gh#335 adds the X-GH-CACHE-STATUSES header this PR relies on to cache 403 and 404 responses, released in go-gh v2.17.1.

Authorship and follow-up

Who wrote this:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @babakks will read and reply directly.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

babakks and others added 10 commits December 3, 2026 11:24
Callers that cached feature detection results each built a cached HTTP
client before creating the detector. NewCachedDetector takes the TTL
directly, so callers only decide how long results are cached and the
caching mechanism stays inside the detector.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
NewCachedHTTPClientWithStatuses returns a cached client that also caches
responses with the given status codes, such as 403, which are not cached
by default. It relies on the X-GH-CACHE-STATUSES header supported by the
go-gh cache, so go-gh is bumped accordingly. Without statuses, the header
is not sent and the client behaves like NewCachedHTTPClient.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
The detector now keeps the client it was given along with the cache TTL,
and builds the client for each detection request through a private
client method. This lets a detection method ask for extra cacheable
status codes without callers knowing about the caching mechanism.
Detectors created with NewDetector still do not cache.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
EventsFeatures probes the event subscriptions endpoint to find out
whether the authenticated account can use event subscriptions. A 403
means they are not enabled for the account, and a 404 means the host
does not have the endpoint, so both report the feature as unavailable
and are cached like a successful response when the detector caches.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
The `gh event listen` hints shown to agents by `gh api`, `gh pr create`, `gh pr view`, `gh run view` and `gh run watch` now check event subscriptions access through feature detection, so accounts without access no longer see them.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
Feature detection decides whether event subscriptions are enabled, so the hints no longer check the host. For an absolute API URL, `gh api` takes the pull request host from the URL.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
The API decides whether event subscriptions are available, so the client no longer refuses GitHub Enterprise Server and GHE.com hosts before sending a request.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
Also documents how feature detection decides whether the listen hints are shown.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
GitHub Enterprise Server answers the event stream request with a 415, since it rejects the text/event-stream Accept header before it finds that the route does not exist. Report it as ErrUnsupportedHost instead of a bare HTTP error.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
A host without the Event Subscriptions API answers 404, which cannot be told apart from a missing resource or subscription without another request. Name the host as a possible cause in the not found errors, and in the error of listing subscriptions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
@babakks
babakks requested a review from a team as a code owner December 3, 2026 14:59
@babakks
babakks requested review from BagToad and a balanced review from Copilot December 3, 2026 14:59
@babakks

babakks commented Dec 3, 2026 •

Copy link
Copy Markdown
Member Author

@iancanderson could you have a look at this one? 🙏 In particular, the help text and documentation changes are all in 466ce58 (docs: remove host restriction from event help and docs), so you can review the wording on its own. It drops the github.com only wording from the gh event, gh pr and gh run help, the gh skill, and docs/event-listen-preview.md, and adds a paragraph to the doc on how feature detection decides whether the listen hints are shown.

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.

🟡 Changes recommended

Absolute pull request URLs on configured API gateways are not mapped back to the logical GitHub host, causing incorrect feature detection and suppressed or misnamed hints.

1 open issue
What changed in this PR

This PR gates agent-facing gh event listen hints on cached account capability detection rather than a fixed host restriction.

Changes:

  • Adds event-subscription detection with a three-second timeout and 24-hour caching, including 403 and 404 responses.
  • Updates hints, host handling, and unsupported-host errors across API, pull request, run, and event commands.
  • Updates tests, documentation, and the go-gh dependency for status-aware caching.
File Description
skills/​gh/​SKILL.md Removes github.com-only wording.
pkg/​cmd/​workflow/​run/​run.go Uses cached detector constructor.
pkg/​cmd/​run/​watch/​watch.go Gates hints on capability detection.
pkg/​cmd/​run/​watch/​watch_test.go Updates hint fixtures.
pkg/​cmd/​run/​view/​view.go Wires detection into hints.
pkg/​cmd/​run/​view/​view_test.go Updates hint expectations and mocks.
pkg/​cmd/​run/​view/​hint.go Creates cached hint detector.
pkg/​cmd/​repo/​list/​list.go Uses cached detector constructor.
pkg/​cmd/​repo/​edit/​edit.go Uses cached detector constructor.
pkg/​cmd/​release/​list/​list.go Uses cached detector constructor.
pkg/​cmd/​project/​item-list/​item_list.go Uses cached detector constructor.
pkg/​cmd/​pr/​view/​view.go Supplies hint client and updates help.
pkg/​cmd/​pr/​view/​view_test.go Updates hint expectations and mocks.
pkg/​cmd/​pr/​view/​hint.go Gates hints on capability detection.
pkg/​cmd/​pr/​status/​status.go Uses cached detector constructor.
pkg/​cmd/​pr/​shared/​templates.go Uses cached detector constructor.
pkg/​cmd/​pr/​shared/​finder.go Uses cached detector constructor.
pkg/​cmd/​pr/​pr.go Removes host restriction from help.
pkg/​cmd/​pr/​list/​list.go Uses cached detector constructor.
pkg/​cmd/​pr/​edit/​edit.go Uses cached detector constructor.
pkg/​cmd/​pr/​create/​create.go Passes detector to hints.
pkg/​cmd/​pr/​create/​create_test.go Updates hint expectations.
pkg/​cmd/​pr/​checks/​checks.go Uses cached detector constructor.
pkg/​cmd/​issue/​view/​view.go Uses cached detector constructor.
pkg/​cmd/​issue/​list/​list.go Uses cached detector constructor.
pkg/​cmd/​issue/​edit/​edit.go Uses cached detector constructor.
pkg/​cmd/​issue/​create/​create.go Uses cached detector constructor.
pkg/​cmd/​event/​subscription/​view/​view_test.go Updates missing-subscription expectations.
pkg/​cmd/​event/​subscription/​list/​list.go Explains possible unsupported-host 404s.
pkg/​cmd/​event/​subscription/​list/​list_test.go Covers unsupported-host messaging.
pkg/​cmd/​event/​subscription/​delete/​delete_test.go Updates missing-subscription expectations.
pkg/​cmd/​event/​subscription/​create/​create.go Expands resource-not-found explanation.
pkg/​cmd/​event/​subscription/​create/​create_test.go Updates error expectations.
pkg/​cmd/​event/​shared/​subscription.go Adds host-support context to errors.
pkg/​cmd/​event/​shared/​hint.go Replaces host checks with capability detection.
pkg/​cmd/​event/​shared/​hint_test.go Covers enabled and disabled hints.
pkg/​cmd/​event/​listen/​listen.go Updates help and host-related errors.
pkg/​cmd/​event/​listen/​listen_test.go Updates error expectations.
pkg/​cmd/​event/​event.go Describes account-based preview access.
pkg/​cmd/​event/​client/​client.go Removes host restriction and handles 415 responses.
pkg/​cmd/​event/​client/​client_test.go Updates host-support coverage.
pkg/​cmd/​api/​hint.go Resolves absolute URL hosts and gates hints.
pkg/​cmd/​api/​api.go Supplies hint detector dependencies.
pkg/​cmd/​api/​api_test.go Covers additional API URL hosts.
internal/​featuredetection/​feature_detection.go Adds cached construction and event detection.
internal/​featuredetection/​feature_detection_test.go Tests cache headers and event detection.
internal/​featuredetection/​detector_mock.go Adds event capability mocks.
go.sum Updates go-gh checksums.
go.mod Pins go-gh status-aware caching support.
docs/​event-listen-preview.md Documents capability gating and caching.
api/​http_client.go Adds configurable cached response statuses.
api/​http_client_test.go Tests cache-status headers.
api/​client.go Defines cache-status header constant.

Review effort: Balanced

Comment thread pkg/cmd/api/hint.go Outdated
return
}
host, requestPath = "github.com", u.Path
host, requestPath = ghauth.NormalizeHostname(u.Hostname()), strings.TrimPrefix(u.Path, "/api/v3")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, but I don't think this applies. api_host is meant to be a transparent gateway: the configured host stays the identity for login, git remotes, web URLs and so on, and only API traffic is rerouted behind the scenes (see docs/api-host.md). Users aren't expected to address the gateway directly, so passing a gateway URL like https://api-gateway.example.net/repos/OWNER/REPO/pulls/1 to gh api isn't an intended input.

In practice, the absolute URLs an agent passes here come from API responses (e.g. an event's api_url). The gateway just forwards requests and doesn't rewrite payloads, so those URLs point at the real API host (api.github.com or api.example.ghe.com), which already resolves to the right host via NormalizeHostname. Feature detection for that host then goes through the gateway as usual.

@iancanderson iancanderson 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.

LGTM!

Comment thread skills/gh/SKILL.md

Do not re-read the PR without a new event; the listener tells you when to look.

The command creates a subscription for the PR and event types, or reuses an existing one for the same set, and reconnects on its own if the stream drops. Events during a reconnect are lost, so each time the listener reports that it is listening again, read the state once. Subscriptions expire after 24 hours, and each account can hold a limited number at a time, so reuse the same set of event types rather than creating variations. To attach another listener to an existing subscription, pass its ID with `--subscription`. `--jq` and `--template` format each event, for example `--jq '.data.api_url'`. Events are only available on github.com, and this preview needs a user account enabled for the Event Subscriptions API.

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.

Events are only available on github.com, and this

Did you remove this phrase so that the docs here are more evergreen/durable and don't rely on the state of the rollout? Because this still stands today - GHES and GHE.com won't be able to use the API.

That being said, this is still kind of covered with "This preview needs a user account enabled for the Event Subscriptions API.", in the sense that no user accounts on GHE.com are currently enabled for this, for example. So I think this is all fine 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, that's the point.

@BagToad BagToad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also discussed with @babakks with regard to dropping the github.com assertion. We're going to add that back since we don't need the unnecessary API calls on hosts that definitely do not support event streaming.

Comment thread docs/event-listen-preview.md Resolved
Comment thread pkg/cmd/event/shared/hint.go Resolved
Revert the host-restriction removal across the event client, commands, discovery hints, help text, and documentation, and short-circuit EventsFeatures for GitHub Enterprise Server and GHE.com hosts without making an HTTP request.
@BagToad
BagToad merged commit 9171e63 into trunk Dec 3, 2026
11 checks passed
@BagToad
BagToad deleted the babakks/event-hint-feature-detection branch December 3, 2026 23:56
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.

4 participants