From 24f7b477172cdfdeecc861c49f05ee78dd1e8eca Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 24 Nov 2026 06:55:46 +0000 Subject: [PATCH 1/2] fix(pm-aggregator): census needs-drew labels via the GitHub list endpoint Closes #14103: the search endpoint refuses a query with neither is:issue nor is:pull-request (HTTP 422), so every census query failed and 'What needs you' showed nothing. The list endpoint answers the same open issues+PRs per label on the core allowance. A 4xx that is not a rate limit is now named as a refused query, not a transient failure. --- pm-stack/aggregator/src/needs-drew.ts | 65 +++++++++----- pm-stack/aggregator/test/needs-drew.test.ts | 98 ++++++++++++--------- 2 files changed, 95 insertions(+), 68 deletions(-) diff --git a/pm-stack/aggregator/src/needs-drew.ts b/pm-stack/aggregator/src/needs-drew.ts index 1198467d6c..e4294c06a2 100644 --- a/pm-stack/aggregator/src/needs-drew.ts +++ b/pm-stack/aggregator/src/needs-drew.ts @@ -2,7 +2,7 @@ // system of record for "waits on Drew", and this module reconciles them // into the aggregator's attention view as a read-only source. It censuses // OPEN issues and PRs carrying user/review or user/escalated across the -// four repos via the GitHub search API (paginated), deduplicates items +// four repos via the GitHub list endpoint (paginated, #14103), deduplicates items // carrying both labels, and reports per-source failures and freshness // honestly — a failed repo/label query is named in `sources`, never // silently rendered as "nothing waiting". @@ -76,10 +76,11 @@ export interface NeedsDrewSourceStatus { /** True when the page cap stopped the census early — the count is a * floor, and the census says so instead of looking complete. */ truncated?: boolean; - /** True when any page of this query came back with GitHub's - * `incomplete_results: true` (search timed out / partially indexed). - * The count is then a floor even when it equals `total_count`, so the - * census must never present it as authoritative (Codex P2 on #3970). */ + /** Retained for the render contract (Codex P2 on #3970): the old + * search endpoint could answer 200 with `incomplete_results: true` + * (search timed out / partially indexed), and the count was then a + * floor even when it equalled `total_count`. The list endpoint + * (#14103) has no such flag, so nothing sets this today. */ incomplete?: boolean; } @@ -98,12 +99,16 @@ export type NeedsDrewFetchPage = (url: string) => Promise<{ ok: boolean; json: u const PER_PAGE = 100; const MAX_PAGES = 10; -export function needsDrewSearchUrl(repo: string, label: NeedsDrewLabel, page: number): string { - const q = `repo:${repo} state:open label:"${label}"`; - return `https://api.github.com/search/issues?q=${encodeURIComponent(q)}&per_page=${PER_PAGE}&page=${page}`; +/** One page of a repo's open issues and PRs carrying a label, as + * GitHub's REST list endpoint has it (#14103): the search endpoint + * refuses a query with neither `is:issue` nor `is:pull-request` + * (HTTP 422), and the list endpoint is on the core allowance, not + * the search allowance, and answers 304 to an unchanged list. */ +export function needsDrewListUrl(repo: string, label: NeedsDrewLabel, page: number): string { + return `https://api.github.com/repos/${repo}/issues?state=open&labels=${encodeURIComponent(label)}&per_page=${PER_PAGE}&page=${page}`; } -interface RawSearchItem { +interface RawListItem { number?: unknown; title?: unknown; html_url?: unknown; @@ -194,7 +199,7 @@ export interface NeedsDrewSweeperOptions { * * Only the attention page render consumes the census; the overview, * Today, /api/attention.json and /api/coverage.json discard it. The - * sweep is up to eight sequential GitHub searches, so paying it for + * sweep is up to eight sequential GitHub list reads, so paying it for * callers that ignore the result made every view depend on an external * API it never needed. Default is "do not fetch" — opting in is * explicit, so a new caller cannot inherit the dependency by accident. @@ -208,7 +213,7 @@ export function shouldFetchNeedsDrewCensus(opts?: { census?: boolean }): boolean * * A cold or expired cache used to let every concurrent caller start its * own sweep — the three attention fragments rendering together produced - * 24 GitHub search requests instead of 8, burning the search rate limit + * 24 GitHub list requests instead of 8, burning the rate limit * and then caching a rate-limited partial result for five minutes. * Callers that arrive mid-sweep now share the in-flight promise. Only a * resolved sweep is cached, so a failure is never served as real data. @@ -251,21 +256,33 @@ export async function buildNeedsDrewCensus( const status: NeedsDrewSourceStatus = { repo, label, ok: true, count: 0, fetchedAt }; try { let page = 1; - let total = Number.POSITIVE_INFINITY; while (page <= MAX_PAGES) { - const res = await fetchPage(needsDrewSearchUrl(repo, label, page)); + const res = await fetchPage(needsDrewListUrl(repo, label, page)); if (!res.ok) { const j = res.json as { status?: unknown; error?: unknown } | null; - throw new Error(typeof j?.status === "number" ? `HTTP ${j.status}` : String(j?.error ?? "fetch failed")); + const statusCode = typeof j?.status === "number" ? j.status : null; + // A 4xx that is not a rate limit (403/429) is a fault of + // the query itself (#14103): waiting cannot fix it, so the + // error says so instead of reading as a transient failure. + if ( + statusCode !== null && + statusCode >= 400 && + statusCode < 500 && + statusCode !== 403 && + statusCode !== 429 + ) { + throw new Error( + `query refused by GitHub (HTTP ${statusCode}): the request is invalid, retrying will not help`, + ); + } + throw new Error( + statusCode !== null ? `HTTP ${statusCode}` : String(j?.error ?? "fetch failed"), + ); } - const payload = res.json as { total_count?: unknown; items?: unknown; incomplete_results?: unknown }; - if (typeof payload?.total_count === "number") total = payload.total_count; - // GitHub returns 200 + incomplete_results for a search that - // timed out or is only partially indexed. Ignoring the flag - // would let a truncated sweep look complete — worst case an - // authoritative empty queue (Codex P2 on #3970). - if (payload?.incomplete_results === true) status.incomplete = true; - const rawItems = Array.isArray(payload?.items) ? (payload.items as RawSearchItem[]) : []; + // The list endpoint answers a bare array: no total_count, no + // incomplete_results flag. A short page ends the list; a full + // page at the cap means the census stopped early (#14103). + const rawItems = Array.isArray(res.json) ? (res.json as RawListItem[]) : []; for (const raw of rawItems) { // Defensive: the query asks for state:open, but a stale or // hand-built payload must never leak a closed item in. @@ -295,10 +312,10 @@ export async function buildNeedsDrewCensus( hands: extractNeedsDrewHands(raw.body), }); } - if (rawItems.length < PER_PAGE || status.count >= total) break; + if (rawItems.length < PER_PAGE) break; + if (page === MAX_PAGES) status.truncated = true; page++; } - if (status.count < total) status.truncated = true; } catch (err) { status.ok = false; status.error = err instanceof Error ? err.message : String(err); diff --git a/pm-stack/aggregator/test/needs-drew.test.ts b/pm-stack/aggregator/test/needs-drew.test.ts index 70f80a5fe1..e8233dde01 100644 --- a/pm-stack/aggregator/test/needs-drew.test.ts +++ b/pm-stack/aggregator/test/needs-drew.test.ts @@ -8,7 +8,7 @@ import { needsDrewWait, needsDrewWaitCounts, needsDrewIncidentKey, - needsDrewSearchUrl, + needsDrewListUrl, type NeedsDrewFetchPage, shouldFetchNeedsDrewCensus, } from "../src/needs-drew.ts"; @@ -30,38 +30,40 @@ function item(number: number, labels: string[], extra: Record = }; } -/** Fake search API keyed by (repo, label, page) from the query URL. */ +/** Fake list endpoint keyed by (repo, label, page) from the request URL. */ function fakeFetch( - handler: (repo: string, label: string, page: number) => { total: number; items: unknown[] } | Error, + handler: (repo: string, label: string, page: number) => { items: unknown[] } | Error, ): NeedsDrewFetchPage { return async (url: string) => { const u = new URL(url); - const q = u.searchParams.get("q") ?? ""; - const repo = /repo:(\S+)/.exec(q)?.[1] ?? ""; - const label = /label:"([^"]+)"/.exec(q)?.[1] ?? ""; + const repo = /^\/repos\/([^/]+\/[^/]+)\/issues$/.exec(u.pathname)?.[1] ?? ""; + const label = u.searchParams.get("labels") ?? ""; const page = Number(u.searchParams.get("page") ?? "1"); const r = handler(repo, label, page); if (r instanceof Error) return { ok: false, json: { error: r.message } }; - return { ok: true, json: { total_count: r.total, items: r.items } }; + return { ok: true, json: r.items }; }; } describe("needs-drew census (#1226)", () => { - test("search URL is open-only, per-label, paginated", () => { - const url = needsDrewSearchUrl(REPO, "user/review", 2); - expect(url).toContain("page=2"); - const q = new URL(url).searchParams.get("q") ?? ""; - expect(q).toContain("state:open"); - expect(q).toContain('label:"user/review"'); - expect(q).toContain(`repo:${REPO}`); + // #14103: the census reads the list endpoint, not the search API — + // a search query with neither is:issue nor is:pull-request is a 422. + test("list URL is open-only, per-label, paginated", () => { + const url = needsDrewListUrl(REPO, "user/review", 2); + const u = new URL(url); + expect(u.pathname).toBe(`/repos/${REPO}/issues`); + expect(u.searchParams.get("state")).toBe("open"); + expect(u.searchParams.get("labels")).toBe("user/review"); + expect(u.searchParams.get("per_page")).toBe("100"); + expect(u.searchParams.get("page")).toBe("2"); }); test("both labels censused; an item carrying both appears once with both", async () => { const both = item(7, ["user/review", "user/escalated"]); const fetchPage = fakeFetch((repo, label) => { - if (repo !== REPO) return { total: 0, items: [] }; - if (label === "user/review") return { total: 2, items: [both, item(8, ["user/review"])] }; - return { total: 1, items: [both] }; + if (repo !== REPO) return { items: [] }; + if (label === "user/review") return { items: [both, item(8, ["user/review"])] }; + return { items: [both] }; }); const census = await buildNeedsDrewCensus(fetchPage, { now: NOW }); expect(census.complete).toBe(true); @@ -75,8 +77,8 @@ describe("needs-drew census (#1226)", () => { test("PRs are included and distinguished from issues", async () => { const fetchPage = fakeFetch((repo, label) => repo === REPO && label === "user/escalated" - ? { total: 1, items: [item(9, ["user/escalated"], { pull_request: { url: "x" } })] } - : { total: 0, items: [] }, + ? { items: [item(9, ["user/escalated"], { pull_request: { url: "x" } })] } + : { items: [] }, ); const census = await buildNeedsDrewCensus(fetchPage, { now: NOW }); expect(census.items).toHaveLength(1); @@ -86,18 +88,18 @@ describe("needs-drew census (#1226)", () => { test("closed items in a payload are excluded even though the query is open-only", async () => { const fetchPage = fakeFetch((repo, label) => repo === REPO && label === "user/review" - ? { total: 2, items: [item(10, ["user/review"]), item(11, ["user/review"], { state: "closed" })] } - : { total: 0, items: [] }, + ? { items: [item(10, ["user/review"]), item(11, ["user/review"], { state: "closed" })] } + : { items: [] }, ); const census = await buildNeedsDrewCensus(fetchPage, { now: NOW }); expect(census.items.map((i) => i.number)).toEqual([10]); }); - test("pagination walks pages until total_count is covered", async () => { + test("pagination walks pages until a short page ends the list", async () => { const page1 = Array.from({ length: 100 }, (_, i) => item(i + 1, ["user/review"])); const fetchPage = fakeFetch((repo, label, page) => { - if (repo !== REPO || label !== "user/review") return { total: 0, items: [] }; - return page === 1 ? { total: 101, items: page1 } : { total: 101, items: [item(101, ["user/review"])] }; + if (repo !== REPO || label !== "user/review") return { items: [] }; + return page === 1 ? { items: page1 } : { items: [item(101, ["user/review"])] }; }); const census = await buildNeedsDrewCensus(fetchPage, { now: NOW }); expect(census.items.filter((i) => i.repo === REPO)).toHaveLength(101); @@ -107,11 +109,24 @@ describe("needs-drew census (#1226)", () => { expect(census.complete).toBe(true); }); + test("a full page at the cap is truncated, not complete", async () => { + const full = Array.from({ length: 100 }, (_, i) => item(i + 1, ["user/review"])); + const fetchPage = fakeFetch((repo, label) => { + if (repo !== REPO || label !== "user/review") return { items: [] }; + return { items: full }; + }); + const census = await buildNeedsDrewCensus(fetchPage, { now: NOW, repos: [REPO], labels: ["user/review"] }); + expect(census.items).toHaveLength(1000); + expect(census.complete).toBe(false); + expect(census.sources[0].count).toBe(1000); + expect(census.sources[0].truncated).toBe(true); + }); + test("a failed source is named, the census is incomplete, other sources still count", async () => { const fetchPage = fakeFetch((repo, label) => { if (repo === "dezren39/darwin-nix") return new Error("HTTP 403"); - if (repo === REPO && label === "user/review") return { total: 1, items: [item(12, ["user/review"])] }; - return { total: 0, items: [] }; + if (repo === REPO && label === "user/review") return { items: [item(12, ["user/review"])] }; + return { items: [] }; }); const census = await buildNeedsDrewCensus(fetchPage, { now: NOW }); expect(census.complete).toBe(false); @@ -121,28 +136,24 @@ describe("needs-drew census (#1226)", () => { expect(nix.every((x) => !x.ok && x.error?.includes("403"))).toBe(true); }); - // Codex P2 on PR #3970: a search that answers 200 with - // incomplete_results:true is a partial sweep, not an empty queue. - test("GitHub's incomplete_results flag keeps an authoritative-looking payload incomplete", async () => { - const fetchPage: NeedsDrewFetchPage = async () => ({ - ok: true, - json: { total_count: 0, items: [], incomplete_results: true }, - }); + // #14103: a query GitHub refuses outright (a 4xx that is not a rate + // limit) is a fault of the query, not a transient failure to wait out. + test("a refused query is named as a fault of the query", async () => { + const fetchPage: NeedsDrewFetchPage = async () => ({ ok: false, json: { status: 422 } }); const census = await buildNeedsDrewCensus(fetchPage, { now: NOW, repos: [REPO], labels: ["user/review"] }); expect(census.items).toHaveLength(0); expect(census.complete).toBe(false); - expect(census.sources[0].incomplete).toBe(true); - expect(census.sources[0].truncated).toBeUndefined(); - // The page must warn, not present an authoritative empty queue. + expect(census.sources[0].ok).toBe(false); + expect(census.sources[0].error).toContain("query refused by GitHub (HTTP 422)"); + // The page names the refusal, not just an incomplete census. const html = needsDrewCensusHtml(census); expect(html).toContain("Label census incomplete"); - expect(html).toContain("incomplete results"); - expect(html).toContain("in the sources that answered"); + expect(html).toContain("query refused by GitHub (HTTP 422)"); }); test("only a flagged page marks the source incomplete; a clean sweep stays complete", async () => { const clean = await buildNeedsDrewCensus( - fakeFetch(() => ({ total: 0, items: [] })), + fakeFetch(() => ({ items: [] })), { now: NOW, repos: [REPO], @@ -173,7 +184,7 @@ describe("needs-drew census (#1226)", () => { const fetchPage: NeedsDrewFetchPage = async () => { calls++; await gate; - return { ok: true, json: { total_count: 0, items: [] } }; + return { ok: true, json: [] }; }; const sweep = createNeedsDrewSweeper(fetchPage, { repos: [REPO], labels: ["user/review"] }); const a = sweep(); @@ -197,7 +208,7 @@ describe("needs-drew census (#1226)", () => { let calls = 0; const fetchPage: NeedsDrewFetchPage = async () => { calls++; - return { ok: true, json: { total_count: 0, items: [] } }; + return { ok: true, json: [] }; }; const sweep = createNeedsDrewSweeper( fetchPage, @@ -277,10 +288,9 @@ describe("how an item waits", () => { test("the census reads the marker from the item body and counts each kind of wait", async () => { const fetchPage = fakeFetch((repo, label) => { - if (repo !== REPO) return { total: 0, items: [] }; - if (label === "user/review") return { total: 1, items: [item(1, ["user/review"])] }; + if (repo !== REPO) return { items: [] }; + if (label === "user/review") return { items: [item(1, ["user/review"])] }; return { - total: 3, items: [ item(2, ["user/escalated"]), item(3, ["user/escalated"], { body: '' }), From 78e20c980d312a1166a96b5b5dbff4077a36df47 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 24 Nov 2026 07:06:26 +0000 Subject: [PATCH 2/2] test(pm-aggregator): cap test pages carry distinct items The census dedupes by ref, so re-answering the same 100 items on every page collapsed the 10-page sweep to 100 items, not 1000. --- pm-stack/aggregator/test/needs-drew.test.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pm-stack/aggregator/test/needs-drew.test.ts b/pm-stack/aggregator/test/needs-drew.test.ts index e8233dde01..95d30c0574 100644 --- a/pm-stack/aggregator/test/needs-drew.test.ts +++ b/pm-stack/aggregator/test/needs-drew.test.ts @@ -110,10 +110,11 @@ describe("needs-drew census (#1226)", () => { }); test("a full page at the cap is truncated, not complete", async () => { - const full = Array.from({ length: 100 }, (_, i) => item(i + 1, ["user/review"])); - const fetchPage = fakeFetch((repo, label) => { + // Each page carries distinct numbers: the census dedupes by ref, so + // re-answering the same page would collapse to one page of items. + const fetchPage = fakeFetch((repo, label, page) => { if (repo !== REPO || label !== "user/review") return { items: [] }; - return { items: full }; + return { items: Array.from({ length: 100 }, (_, i) => item((page - 1) * 100 + i + 1, ["user/review"])) }; }); const census = await buildNeedsDrewCensus(fetchPage, { now: NOW, repos: [REPO], labels: ["user/review"] }); expect(census.items).toHaveLength(1000);