diff --git a/pm-stack/aggregator/src/projects.ts b/pm-stack/aggregator/src/projects.ts index 33d16f991c..50b1950212 100644 --- a/pm-stack/aggregator/src/projects.ts +++ b/pm-stack/aggregator/src/projects.ts @@ -334,6 +334,7 @@ export async function fetchProjectSnapshot( ): Promise { const parsedItems: ProjectPoolItem[] = []; const auditItems: GqlItem[] = []; + const seenItemIds = new Set(); let firstPage: unknown; let cursor: string | null = null; let lastPageInfo: ReturnType | null = null; @@ -353,13 +354,30 @@ export async function fetchProjectSnapshot( if ((json as GqlProjectPage).errors) throw new Error("github GraphQL returned errors"); if (!firstPage) firstPage = json; const nodes = (json as GqlProjectPage).data?.node?.items?.nodes ?? []; - auditItems.push(...nodes.filter((node): node is GqlItem => node !== null)); - parsedItems.push(...parseProjectItems(json)); + // #8599: overlapping pages (the board mutating mid-export) can + // deliver the same node twice. Deduplicate by item id at + // collection, first delivery wins: the audit distributions and + // the completeness check below are judged on the deduplicated + // export, so an over-delivered export still sums to totalItems + // and survives the feed-side isProjectAudit gate instead of + // being discarded as an absent snapshot. Fetch-side tolerance + // and feed-side strictness agree on the distinct-item count. + const freshNodes = nodes.filter((node): node is GqlItem => { + if (node === null) return false; + if (!node.id) return true; + if (seenItemIds.has(node.id)) return false; + seenItemIds.add(node.id); + return true; + }); + auditItems.push(...freshNodes); + const freshIds = new Set(freshNodes.map((node) => node.id)); + parsedItems.push(...parseProjectItems(json).filter((item) => freshIds.has(item.id))); const info = projectPageInfo(json); if (!info.hasNextPage) { if (auditItems.length < info.totalCount) { - // Shortfall only: overlapping pages can over-deliver nodes, but - // fewer nodes than the board total means the export lost items. + // Shortfall only, judged on the deduplicated count: fewer + // distinct items than the board total means the export lost + // items. Over-delivery is absorbed by the dedupe above. throw new Error(`github GraphQL project export incomplete (${auditItems.length}/${info.totalCount} items)`); } return { diff --git a/pm-stack/aggregator/test/projects.test.ts b/pm-stack/aggregator/test/projects.test.ts index 554a72de74..b5f1b389fe 100644 --- a/pm-stack/aggregator/test/projects.test.ts +++ b/pm-stack/aggregator/test/projects.test.ts @@ -216,6 +216,32 @@ describe("collector projects parsing (GraphQL -> feed section)", () => { expect(projectDisagreement(pool[0], section)).toBeNull(); }); + test("overlapping pages: a node delivered twice is counted once and the audit survives the feed gate (#8599)", async () => { + // Page 1 delivers PVTI_1 + PVTI_2, page 2 repeats PVTI_2 and adds + // PVTI_3 (the board mutated mid-export). totalCount is 3 distinct + // items; without dedupe every distribution would sum to 4 and + // isProjectAudit would discard the fetched snapshot as absent. + const page1 = structuredClone(GQL); + page1.data.node.items.totalCount = 3; + page1.data.node.items.pageInfo = { hasNextPage: true, endCursor: "cursor-1" }; + page1.data.node.items.nodes = [GQL.data.node.items.nodes[0], GQL.data.node.items.nodes[1]]; + const page2 = structuredClone(GQL); + page2.data.node.items.totalCount = 3; + page2.data.node.items.pageInfo = { hasNextPage: false, endCursor: null }; + page2.data.node.items.nodes = [GQL.data.node.items.nodes[1], GQL.data.node.items.nodes[2]]; + const pages = [page1, page2]; + let call = 0; + const mockFetch = (async () => + new Response(JSON.stringify(pages[call++]), { status: 200 })) as unknown as typeof fetch; + const snapshot = await fetchProjectSnapshot("test-token", mockFetch, "2026-11-01T21:00:00.000Z"); + expect(call).toBe(2); + expect(snapshot.audit.totalItems).toBe(3); + expect(snapshot.audit.counts.status).toEqual({ "(unset)": 1, Done: 1, "In Progress": 1 }); + expect(isProjectAudit(snapshot.audit)).toBe(true); + // The pool is deduplicated by item id as well. + expect(snapshot.items.map((item) => item.id)).toEqual(["PVTI_1", "PVTI_2"]); + }); + test("scheduled fetch keeps the full item total and publishes one-page audit metadata", async () => { const mockFetch = (async () => new Response(JSON.stringify(GQL), { status: 200 })) as unknown as typeof fetch; const snapshot = await fetchProjectSnapshot( @@ -229,7 +255,7 @@ describe("collector projects parsing (GraphQL -> feed section)", () => { expect(snapshot.audit.views).toEqual([{ id: "PVTV_board", name: "Board", layout: "BOARD_LAYOUT", filter: "Status:open" }]); expect(snapshot.audit.counts.status).toEqual({ "(unset)": 1, Done: 1, "In Progress": 1 }); expect(snapshot.audit.counts.area).toEqual({ "(unset)": 2, "lab-cluster": 1 }); - expect(snapshot.audit.counts.repo).toEqual({ "(unlinked)": 1, "developing-today/code": 1, "dezren39/nix": 1 }); + expect(snapshot.audit.counts.repo).toEqual({ "(unlinked)": 1, "developing-today/code": 1, "dezren39/darwin-nix": 1 }); }); });