Skip to content

Event streaming (2/11): Add gh event listen - #15072

Merged
BagToad merged 2 commits into
babakks/event-clientfrom
babakks/event-listen
Nov 17, 2026
Merged

BagToad merged 2 commits into
babakks/event-clientfrom
babakks/event-listen

Conversation

@babakks

@babakks babakks commented Nov 17, 2026

Copy link
Copy Markdown
Member

Layer 2 of 11 in the stack tracked by #15070.

Description

Scripts and coding agents that need to wait for a pull request to change, for example to be reviewed or merged, poll today: they loop over gh pr view or gh api with a sleep. That is slow to react, wastes requests, and for agents burns context on every read. The Event Subscriptions API (in preview, github.com only) can push these changes instead.

This PR adds a gh event command group, marked as preview, with one command:

gh event listen --resource https://github.com/OWNER/REPO/pull/123 --event com.github.pull_request.review.submitted.v1

It creates a subscription for the pull request and event types (or reuses the matching one, since creation is idempotent), attaches to its event stream, and prints each event to stdout as one compact CloudEvent JSON object per line. Status messages go to stderr, so stdout carries only events. --subscription ID attaches to an existing subscription instead.

  • Lifecycle. It exits after the first event by default, --count N exits after N events, and --follow listens until stopped. --timeout (a duration or a number of seconds) exits with status 8 when it is reached first.
  • Readiness. The API is live only, so earlier changes are never replayed. Listening for events. is printed to stderr each time the stream attaches, followed by a reminder to read the current state now.
  • Formatting. --jq and --template format each event, like gh api, and are validated before subscribing.
  • Resilience. Network errors, 5xx responses (honoring Retry-After), streams that end without an end frame, and reconnect end frames lead to a reconnect with jittered exponential backoff up to 30 seconds. A stream with no data, not even a heartbeat, for 45 seconds is treated as dead. An expired subscription is created again when listening with --resource.
  • Errors. Missing preview access, a missing or inaccessible pull request, an expired subscription, revoked access, and the subscription limit each get a specific message. At the limit, the message suggests gh event subscription list and delete to free a slot.
  • Discovery. --event completion suggests the four known pull request event types but accepts any value, since the server may support types this build does not know about. The help lists the types with a description of each.

The shared package holds what the subscription commands in the next PRs reuse: the client factory, event type help and completion, and the resource host and error helpers.

How did you test this change?

Recording of gh event listen on my fork's pull request babakks#17, with a check after each case. Background jobs make the changes. It waits for a review and exits after it, times out with exit status 8 when no push arrives within 15 seconds, exits after two state changes with --count 2 and --jq, and with --follow keeps printing state changes and a review until Ctrl-C.

02-listen.mp4

With a gh built from the complete stack, I ran the live smoke check from the evals PR against a sandbox pull request on github.com. gh event listen --follow printed Listening for events. on stderr, and as a second account submitted two change requests and an approval, I saw each review event arrive on stdout with its data.api_url pointing at that review.

Key points

  • Exit on the first event by default. Agent harnesses resume an agent when a background command exits, not when it prints, so a listener that kept running left agents waiting. --follow is opt-in.
  • Full event type names, no aliases or client-side validation. The server rejects unsupported types with a clear 422, and new types work without a gh release.
  • Subscriptions are not deleted on exit. They are shared per user, resource, and exact event set, so another listener may be using the same one, and they expire after 24 hours. Cleanup on exit is left for a follow-up.
  • Status 8 for a timeout. It reuses cmdutil.PendingError, the status gh pr checks returns for pending checks, so a script can tell "nothing happened yet" from a failure.
  • Forward reference. The limit error names gh event subscription list and delete, which land in the next PRs of this stack.

Notes for reviewers

Stack, bottom to top (this is 2 of 11, based on babakks/event-client): babakks/event-client, babakks/event-listen, babakks/event-sub-list, babakks/event-sub-create, babakks/event-sub-view, babakks/event-sub-delete, babakks/event-hints, babakks/event-evals, babakks/event-listen-flush, babakks/event-listen-deleted-sub, babakks/event-check-suite.

Start with the help text and flag validation in NewCmdListen, then listener.listen in pkg/cmd/event/listen/listen.go, which holds the attach, reconnect, and resubscribe loop. readEvents handles the stream's frames, and idleReader the dead-connection timeout.

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. Name the account.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

babakks and others added 2 commits November 17, 2026 16:36
Co-authored-by: Copilot <[email protected]>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
Co-authored-by: Copilot <[email protected]>
Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
@babakks
babakks requested a review from a team as a code owner November 17, 2026 17:11
@babakks
babakks requested review from a team and niik and a balanced review from Copilot and removed request for a team November 17, 2026 17:11
@babakks
babakks added this pull request to stack #15082 November 17, 2026 17:14
@babakks
babakks requested a review from BagToad November 17, 2026 17:15

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

Frame-terminator buffering can delay event output and cause false timeout exits, while ignoring HTTP-date Retry-After values allows retries before the server’s requested time.

2 open issues
What changed in this PR

Adds the preview gh event listen command so scripts and agents can wait for pull request changes instead of polling. This is the second layer of the event-subscriptions stack tracked in #15070.

Changes:

  • Registers gh event listen with resource or subscription selection, event counts, continuous listening, and timeout controls.
  • Adds JSON event output, per-event formatting, readiness messages, and reconnect and resubscribe handling.
  • Introduces reusable subscription helpers and tests for command behavior and event-type completion.
File Description
pkg/​cmd/​root/​root.go Registers the event command group.
pkg/​cmd/​event/​shared/​subscription.go Adds resource-host, event-normalization, and subscription-error helpers.
pkg/​cmd/​event/​shared/​help.go Defines known event types and their help descriptions.
pkg/​cmd/​event/​shared/​completion.go Completes comma-separated event types.
pkg/​cmd/​event/​shared/​completion_test.go Tests completion filtering and duplicate exclusion.
pkg/​cmd/​event/​shared/​client.go Creates event clients through the command factory.
pkg/​cmd/​event/​listen/​listen.go Implements flags, streaming, formatting, lifecycle controls, and retries.
pkg/​cmd/​event/​listen/​listen_test.go Tests validation, output, retries, timeouts, and subscription lifecycle behavior.
pkg/​cmd/​event/​event.go Defines the preview command group and registers listen.

Review effort: Balanced

Comment on lines +423 to +434
// Event data is JSON, which the API client sanitizes only for JSON responses. Sanitizing it here, rather than
// printing through ContentOut, keeps the escape sequences that --template helpers such as color generate.
reader := eventstream.NewFrameReader(transform.NewReader(body, &asciisanitizer.Sanitizer{JSON: true}))
reader.OnComment = func(string) { l.delivered = true }
for {
frame, err := reader.Next()
if errors.Is(err, io.EOF) {
return nil, nil
}
if err != nil {
return nil, err
}
Comment thread pkg/cmd/event/listen/listen.go Resolved
@BagToad
BagToad merged commit 42d52b4 into trunk Nov 17, 2026
19 checks passed
@BagToad
BagToad deleted the babakks/event-listen branch November 17, 2026 19:18
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