From f034ee964bed1ddf5ce9ce11d55676b977bc150e Mon Sep 17 00:00:00 2001 From: "pi-agent-action[bot]" <339978130+pi-agent-action[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:01:46 +0000 Subject: [PATCH] feat: poll visible pull request lists and readers Co-authored-by: Valian <92361802+lw396@users.noreply.github.com> --- README.md | 2 +- mods/github/main.js | 75 +++++++++++++++++++++++++++++++++++--- src/github/io/pulls.ts | 8 +++- src/github/views/panel.ts | 18 ++++++--- src/github/views/pull.ts | 69 ++++++++++++++++++++++++++++++++--- src/github/views/reader.ts | 2 +- test/io/pull-poll.test.ts | 23 ++++++++++++ 7 files changed, 177 insertions(+), 20 deletions(-) create mode 100644 test/io/pull-poll.test.ts diff --git a/README.md b/README.md index 5507fbf..44bdf38 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The mod uses GitHub CLI (`gh`) on the machine hosting the repository, so local a - **Automatic list refresh** — while the GitHub sidebar is visible, check for changes every 60 seconds using ETags; refresh expanded groups only when changed. Showing it again refreshes immediately. While the PRs tab and local group are visible and expanded, local branches are re-read each minute; only a changed branch snapshot or an explicit refresh reissues their GraphQL batch. - **Native detail tabs** — open different issues and PRs in separate tabs; selecting an already-open item reuses its tab. Contents remain session-only. - **Discussion reader** — read Markdown descriptions, comments, and reaction counts in individual cards. -- **Automatic discussion refresh** — check the visible issue tab every 10 seconds using ETags; keep unsent comments through refreshes, stop polling hidden tabs, and refresh immediately when shown again. +- **Automatic discussion refresh** — check the visible issue or PR tab every 10 seconds using ETags; PRs also conditionally check discussion, review, inline-comment and commit-check resources rather than assuming the PR validator covers them; keep unsent comments through refreshes, stop polling hidden tabs, and refresh immediately when shown again. - **Issue and PR links** — links to the repository's issues and PRs open the appropriate reader in the same tab, with **←** to go back across both kinds; `#123` references open as issue links. Other links open in your browser. - **Comment, close and reopen** — post a new comment (⌘/Ctrl+Enter) or close and reopen an issue from its tab; the sidebar's groups update at once. - **Issue and PR metadata** — view colored labels, assignees, linked Projects, and milestones. PR descriptions also show reviewers with their review states and the source and target branches. diff --git a/mods/github/main.js b/mods/github/main.js index 717f8c9..9fc440a 100644 --- a/mods/github/main.js +++ b/mods/github/main.js @@ -10657,6 +10657,8 @@ function threadRow(rows, thread) { } // src/github/io/pulls.ts +var pullListChanges = (repo, etag, on) => changes(`repos/${repo}/pulls?state=all&sort=updated&per_page=1`, etag, on); +var pullChanges = (repo, number, etag, on) => changes(`repos/${repo}/pulls/${number}`, etag, on); async function pullPage(kind, remote2, login2, on, after) { return parsePullGroup(await graphql(pullGroupQuery(kind, remote2, login2, after), on)); } @@ -11681,7 +11683,11 @@ var Panel = class { const current = () => run4 === this.latest && this.poll.current(request); let change; try { - change = await listChanges(`${r.remote.owner}/${r.remote.repo}`, request.etag, r.on); + change = await (this.tab === "prs" ? pullListChanges : listChanges)( + `${r.remote.owner}/${r.remote.repo}`, + request.etag, + r.on + ); if (!current()) return; this.checkedAt = Date.now(); this.checkFailed = false; @@ -11689,8 +11695,8 @@ var Panel = class { if (current()) this.checkFailed = true; } finally { if (current()) { - if (change?.changed || refresh) this.issues.load(cx2, true); - if (refresh && this.tab === "prs") this.pulls.load(cx2, refresh === "asked"); + if (this.tab === "issues" && (change?.changed || refresh)) this.issues.load(cx2, true); + if ((change?.changed || refresh) && this.tab === "prs") this.pulls.load(cx2, true); cx2.notify(); } this.poll.finish(request, change); @@ -11832,6 +11838,8 @@ var Panel = class { TABS.map( ({ id, label }) => div9().id(`gh-tab-${id}`).px(8).py(3).rounded(6).text_size(12).font_medium().cursor_pointer().text_color(this.tab === id ? t.foreground : t.muted).when(this.tab === id, (el) => el.bg(t.hover)).when(this.tab !== id, (el) => el.hover((s) => s.bg(t.hover))).accessibility_label(label).on_mouse_down("left", stop).on_click((_e, cx) => { this.tab = id; + this.poll.invalidate(); + this.check(cx, "shown"); this.openFailure = null; if (id === "prs") this.pulls.load(cx); cx.notify(); @@ -13171,8 +13179,56 @@ var Pull = class { notices = new Notices(); missingFile = null; expandedActivity = /* @__PURE__ */ new Set(); - init() { + polling = new ConditionalPoll(); + validators = /* @__PURE__ */ new Map(); + init(cx) { this.diff.init(); + cx.timer.every(1e4, (cx2) => { + const shown = this.syncPolling(); + if (!this.writing && !this.entry?.loading) this.check(cx2, shown); + }); + } + syncPolling() { + const place = detailLocation(this.host.reader); + return this.polling.sync( + this.ref && place ? `${place.on} +${place.pane} +${detailKey(this.ref)}` : null, + Boolean(place?.visible) && this.host.active("pr") + ); + } + check(cx, force = false) { + this.syncPolling(); + const request = this.polling.begin(force); + const ref = this.ref; + const place = detailLocation(this.host.reader); + if (!request || !ref || !place) return; + cx.spawn(async (cx2) => { + let completed; + try { + const change = await pullChanges(ref.repo, ref.number, request.etag, place.on); + this.syncPolling(); + if (!this.polling.current(request)) return; + const paths = [`issues/${ref.number}`, `pulls/${ref.number}/reviews`, `pulls/${ref.number}/comments`]; + const sha = this.entry?.pull?.headSha; + if (sha) paths.push(`commits/${sha}/check-runs`, `commits/${sha}/statuses`); + const replies = await Promise.all( + paths.map(async (path) => ({ + path, + change: await changes(`repos/${ref.repo}/${path}`, this.validators.get(path) ?? null, place.on) + })) + ); + if (!detailLocation(this.host.reader)?.visible) this.syncPolling(); + if (!this.polling.current(request)) return; + const changed = change.changed || replies.some((r) => r.change.changed); + if (force || changed) this.load(cx2); + for (const reply of replies) if (reply.change.changed) this.validators.set(reply.path, reply.change.etag); + completed = change; + } catch { + } finally { + this.polling.finish(request, completed); + } + }); } host; metadata = new DetailMetadata(); @@ -13185,6 +13241,8 @@ var Pull = class { this.writeFailure = ""; this.metadata.expanded = false; this.ref = ref; + this.validators.clear(); + this.polling.invalidate(); this.entry = null; this.files = null; this.discussions = null; @@ -13222,7 +13280,11 @@ var Pull = class { entry.on = place.on; loadProjects(cx2, ref, place.on, entry, current); } catch (e) { - if (current()) entry.failure = readFailure(ref, e); + if (current()) { + entry.failure = readFailure(ref, e); + this.polling.invalidate(); + this.validators.clear(); + } } finally { if (current()) { entry.loading = false; @@ -13232,6 +13294,7 @@ var Pull = class { }); } render(cx, layout2) { + if (this.syncPolling()) this.check(cx, true); const on = detailLocation(this.host.reader)?.on; if (this.notices.take().some((n) => n.repo === this.ref?.repo && n.number === this.ref.number && n.threads?.on === on && n.threads)) this.load(cx, false); @@ -13457,7 +13520,7 @@ var Reader = class { } init(cx) { this.issue.init(cx); - this.pull.init(); + this.pull.init(cx); cx.spawn(async (cx2) => { for (; ; ) { const ref = detailCommand(await nextCommand(), this.reader); diff --git a/src/github/io/pulls.ts b/src/github/io/pulls.ts index afac9bb..915e82f 100644 --- a/src/github/io/pulls.ts +++ b/src/github/io/pulls.ts @@ -16,9 +16,15 @@ import { import type { RemoteRepo } from "../core/remote.ts"; import { parseThreads, type ReviewThread, type ThreadNode } from "../core/review-threads.ts"; import { written } from "./changes.ts"; -import { api, eachPage, GhError, graphql, mutate } from "./gh.ts"; +import { api, changes, eachPage, GhError, graphql, mutate } from "./gh.ts"; import type { LocalBranches } from "./git.ts"; +export const pullListChanges = (repo: string, etag: string | null, on: string | null) => + changes(`repos/${repo}/pulls?state=all&sort=updated&per_page=1`, etag, on); + +export const pullChanges = (repo: string, number: number, etag: string | null, on: string | null) => + changes(`repos/${repo}/pulls/${number}`, etag, on); + /** One page of a group, asked with the gh on `on`, whose account is `login` (for the searches). */ export async function pullPage( kind: PullGroupKind, diff --git a/src/github/views/panel.ts b/src/github/views/panel.ts index 337471c..23709b6 100644 --- a/src/github/views/panel.ts +++ b/src/github/views/panel.ts @@ -15,6 +15,7 @@ import { DetailTabs } from "../io/detail-tabs.ts"; import * as gh from "../io/gh.ts"; import { listChanges } from "../io/issues.ts"; import * as known from "../io/known.ts"; +import { pullListChanges } from "../io/pulls.ts"; import type { ListHost } from "./groups.ts"; import { issueList } from "./issues.ts"; import { pullList } from "./pulls.ts"; @@ -90,7 +91,11 @@ export class Panel implements Role { const current = () => run === this.latest && this.poll.current(request); let change: Change | undefined; try { - change = await listChanges(`${r.remote.owner}/${r.remote.repo}`, request.etag, r.on); + change = await (this.tab === "prs" ? pullListChanges : listChanges)( + `${r.remote.owner}/${r.remote.repo}`, + request.etag, + r.on, + ); if (!current()) return; this.checkedAt = Date.now(); this.checkFailed = false; @@ -98,11 +103,10 @@ export class Panel implements Role { if (current()) this.checkFailed = true; } finally { if (current()) { - if (change?.changed || refresh) this.issues.load(cx, true); - // PR lists have their own change check later (#34); a refresh asks the shown tab's groups now. - // Two of them are searches (30 a minute for the whole account, ADR 0006): kept pages under a minute - // old are reused, and only ↻ asks them all again. - if (refresh && this.tab === "prs") this.pulls.load(cx, refresh === "asked"); + if (this.tab === "issues" && (change?.changed || refresh)) this.issues.load(cx, true); + // Reload only the shown tab's expanded groups. + // Two groups use search: only probe changes or an explicit/show refresh re-ask them. + if ((change?.changed || refresh) && this.tab === "prs") this.pulls.load(cx, true); // Every finished check redraws: the footer shows a failed check and its recovery. cx.notify(); } @@ -355,6 +359,8 @@ export class Panel implements Role { .on_mouse_down("left", stop) .on_click((_e, cx) => { this.tab = id; + this.poll.invalidate(); + this.check(cx, "shown"); // A tab that failed to open was asked from the other list. this.openFailure = null; if (id === "prs") this.pulls.load(cx); diff --git a/src/github/views/pull.ts b/src/github/views/pull.ts index 11581d5..afe009b 100644 --- a/src/github/views/pull.ts +++ b/src/github/views/pull.ts @@ -1,15 +1,16 @@ // A PR reader switches between its published discussion and a changed file's read-only unified diff. import { h_flex, v_flex } from "gpui-base"; -import { type Context, div, type Element } from "gpui-kit"; -import { type DetailRef, detailUrl } from "../core/detail.ts"; +import { type AsyncContext, type Context, div, type Element } from "gpui-kit"; +import { type DetailRef, detailKey, detailUrl } from "../core/detail.ts"; import type { MergeMethod } from "../core/merge.ts"; +import { ConditionalPoll } from "../core/polling.ts"; import type { Projects } from "../core/projects.ts"; import type { PullDetail } from "../core/pull-detail.ts"; import type { ReviewState } from "../core/reviewers.ts"; import { Notices } from "../io/changes.ts"; import { detailLocation } from "../io/detail-tabs.ts"; import * as gh from "../io/gh.ts"; -import { deletePullBranch, mergePull, readPull } from "../io/pulls.ts"; +import { deletePullBranch, mergePull, pullChanges, readPull } from "../io/pulls.ts"; import { type DetailHost, type DetailLayout, @@ -63,8 +64,59 @@ export class Pull { private missingFile: Extract | null = null; private expandedActivity = new Set(); - init(): void { + private polling = new ConditionalPoll(); + private validators = new Map(); + + init(cx: AsyncContext): void { this.diff.init(); + cx.timer.every(10_000, (cx) => { + const shown = this.syncPolling(); + if (!this.writing && !this.entry?.loading) this.check(cx, shown); + }); + } + + private syncPolling(): boolean { + const place = detailLocation(this.host.reader); + return this.polling.sync( + this.ref && place ? `${place.on}\n${place.pane}\n${detailKey(this.ref)}` : null, + Boolean(place?.visible) && this.host.active("pr"), + ); + } + + private check(cx: Context, force = false): void { + this.syncPolling(); + const request = this.polling.begin(force); + const ref = this.ref; + const place = detailLocation(this.host.reader); + if (!request || !ref || !place) return; + cx.spawn(async (cx) => { + let completed: import("../core/gh.ts").Change | undefined; + try { + const change = await pullChanges(ref.repo, ref.number, request.etag, place.on); + this.syncPolling(); + if (!this.polling.current(request)) return; + // PR validators are not assumed to cover discussions or commit checks. + const paths = [`issues/${ref.number}`, `pulls/${ref.number}/reviews`, `pulls/${ref.number}/comments`]; + const sha = this.entry?.pull?.headSha; + if (sha) paths.push(`commits/${sha}/check-runs`, `commits/${sha}/statuses`); + const replies = await Promise.all( + paths.map(async (path) => ({ + path, + change: await gh.changes(`repos/${ref.repo}/${path}`, this.validators.get(path) ?? null, place.on), + })), + ); + if (!detailLocation(this.host.reader)?.visible) this.syncPolling(); + if (!this.polling.current(request)) return; + const changed = change.changed || replies.some((r) => r.change.changed); + if (force || changed) this.load(cx); + for (const reply of replies) if (reply.change.changed) this.validators.set(reply.path, reply.change.etag); + completed = change; + } catch { + // Keep the discussion and drafts on transient failures; retry next tick. + } finally { + this.polling.finish(request, completed); + } + }); } private readonly host: DetailHost; private metadata = new DetailMetadata(); @@ -79,6 +131,8 @@ export class Pull { this.writeFailure = ""; this.metadata.expanded = false; this.ref = ref; + this.validators.clear(); + this.polling.invalidate(); this.entry = null; this.files = null; this.discussions = null; @@ -119,7 +173,11 @@ export class Pull { entry.on = place.on; loadProjects(cx, ref, place.on, entry, current); } catch (e) { - if (current()) entry.failure = readFailure(ref, e); + if (current()) { + entry.failure = readFailure(ref, e); + this.polling.invalidate(); + this.validators.clear(); + } } finally { if (current()) { entry.loading = false; @@ -130,6 +188,7 @@ export class Pull { } render(cx: Context, layout: DetailLayout): Element[] { + if (this.syncPolling()) this.check(cx, true); const on = detailLocation(this.host.reader)?.on; if ( this.notices diff --git a/src/github/views/reader.ts b/src/github/views/reader.ts index 67876ec..25762bc 100644 --- a/src/github/views/reader.ts +++ b/src/github/views/reader.ts @@ -49,7 +49,7 @@ export class Reader implements Role { init(cx: AsyncContext): void { this.issue.init(cx); - this.pull.init(); + this.pull.init(cx); cx.spawn(async (cx) => { for (;;) { const ref = detailCommand(await nextCommand(), this.reader); diff --git a/test/io/pull-poll.test.ts b/test/io/pull-poll.test.ts new file mode 100644 index 0000000..fe798f4 --- /dev/null +++ b/test/io/pull-poll.test.ts @@ -0,0 +1,23 @@ +import { afterEach, expect, test } from "bun:test"; +import { pullChanges, pullListChanges } from "../../src/github/io/pulls.ts"; +import { machine, reset } from "../fake-herdr.ts"; + +afterEach(reset); +for (const [name, path, read] of [ + [ + "list", + "pulls?state=all&sort=updated&per_page=1", + (tag: string | null) => pullListChanges("owner/repo", tag, "remote"), + ], + ["detail", "pulls/12", (tag: string | null) => pullChanges("owner/repo", 12, tag, "remote")], +] as const) { + test(`${name}: changed and unchanged responses use the remote validator`, async () => { + const command = `~$ gh api repos/owner/repo/${path} --include`; + machine("remote", { [command]: { stdout: 'HTTP/2.0 200 OK\r\nETag: W/"one"\r\n\r\n{}' } }); + expect(await read(null)).toEqual({ changed: true, etag: 'W/"one"' }); + machine("remote", { + [`${command} -H If-None-Match: W/"one"`]: { stdout: "HTTP/2.0 304 Not Modified\r\n\r\n", code: 1 }, + }); + expect(await read('W/"one"')).toEqual({ changed: false }); + }); +}