From 44457686ce9371f514517e5adeea8d89692e3d80 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 3 Dec 2026 00:34:39 -0600 Subject: [PATCH] =?UTF-8?q?v2.9274=20=E2=80=94=20Bids=20and=20the=20Dashbo?= =?UTF-8?q?ard=20open=20with=20far=20fewer=20requests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pending-audits gate asked the server once for each bid with a pending robot audit: 74 HEAD counts at once on every open of Bids and of Needs you, 8.1% of all database time. One read of the pending bids, each with at most one of its count rows, now answers for all of them. Client only. Co-Authored-By: Claude Fable 5.2 --- docs/recent-features/v2.9274.md | 25 ++++++++++++++++++++ src/content/releaseNotes/v2.9274.ts | 15 ++++++++++++ src/hooks/useBidAuditsPendingCount.ts | 20 +++++++++------- src/lib/bids/bidAudits.test.ts | 34 ++++++++++++++++++++++++++- src/lib/bids/bidAudits.ts | 27 +++++++++++++++++++++ 5 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 docs/recent-features/v2.9274.md create mode 100644 src/content/releaseNotes/v2.9274.ts diff --git a/docs/recent-features/v2.9274.md b/docs/recent-features/v2.9274.md new file mode 100644 index 0000000000..f4afcce51a --- /dev/null +++ b/docs/recent-features/v2.9274.md @@ -0,0 +1,25 @@ +# v2.9274 — Bids and the Dashboard open with far fewer requests (2026-12-03) + +A teammate reported the live app as nearly too slow to use, worst on the Bid Board. Read on prod on 2026-12-03, reads only. Client only. No migration, no function change, nothing owed on prod. + +## What was wrong + +`useBidAuditsPendingCount` decides which pending robot audits have nothing to judge yet (the robot opened the audit before pasting its counts). Since v2.2796 it asked with one `HEAD` count on `bids_count_rows` for each pending bid, all at once. It runs on every open of the Bids page and of every screen that reads Needs you, for the roles that work robot audits. + +- 74 bids had a pending audit, so each open sent 74 requests at once. +- In `pg_stat_statements` that one statement had 170,191 calls at 80 ms mean over nine and a half days: 8.1% of all database time and the largest of any statement the app sends. One burst was about 6 s of database time, landing at the moment the Bid Board sends its own reads. +- No merge of the week started it. The oldest pending audit is from 2026-08-31 and the pile had been 74 for more than a week. + +## What changed + +- **One read for all the pending bids** ([`useBidAuditsPendingCount.ts`](../../src/hooks/useBidAuditsPendingCount.ts)): `bids` by id, each with at most one of its count rows (`PENDING_PRICED_SELECT`, the limit set on the embedded table). A bid that comes back with no count row is unpriced (`unpricedPendingBidIds` in [`bidAudits.ts`](../../src/lib/bids/bidAudits.ts)). Past 100 pending bids the read is cut in chunks of 100 (`pendingPricedChunks`), so 74 requests became one. +- **The relationship is named** (`bids_count_rows!bids_count_rows_bid_id_fkey`): `bids` reaches `bids_count_rows` two ways, and PostgREST answers an unnamed embed with PGRST201. +- **The count is the same.** A count row is read through its bid (the row policy of `bids_count_rows`), so a bid the person cannot read counted 0 before and reads as unpriced now. A read that fails leaves every pending audit counted, as a failed count did. + +## Not in this change + +Every read of `jobs_ledger` costs 0.2 to 0.6 s on a table under 1,000 rows, in its row policies. That is a migration and a plan read of its own. + +## Tests + +`src/lib/bids/bidAudits.test.ts`: the unpriced set from the one read, the pending count it feeds, the named relationship, the chunks. diff --git a/src/content/releaseNotes/v2.9274.ts b/src/content/releaseNotes/v2.9274.ts new file mode 100644 index 0000000000..60bc5d7aac --- /dev/null +++ b/src/content/releaseNotes/v2.9274.ts @@ -0,0 +1,15 @@ +import type { ReleaseNote } from '../../lib/releaseNotes' + +const note: ReleaseNote = { + version: 'v2.9274', + date: '2026-12-03', + title: 'Bids and the Dashboard open with far fewer requests', + kind: 'fix', + highlights: [ + 'Opening Bids or the Dashboard no longer asks the server once for every robot bid waiting on an audit. One request now answers for all of them.', + 'That burst was 74 requests at once on each open, the largest load the app put on its database. The pages it slowed, the Bid Board first, have that time back.', + 'Nothing looks different. The Audits tab and the Needs you card count the same audits as before.', + ], +} + +export default note diff --git a/src/hooks/useBidAuditsPendingCount.ts b/src/hooks/useBidAuditsPendingCount.ts index 116323278a..8b5d0d00df 100644 --- a/src/hooks/useBidAuditsPendingCount.ts +++ b/src/hooks/useBidAuditsPendingCount.ts @@ -1,7 +1,7 @@ import { useEffect, useState } from 'react' import type { SupabaseClient } from '@supabase/supabase-js' import { supabase } from '../lib/supabase' -import { countWorkablePendingAudits, pairTwinReferences } from '../lib/bids/bidAudits' +import { countWorkablePendingAudits, pairTwinReferences, PENDING_PRICED_SELECT, PENDING_PRICED_TABLE, pendingPricedChunks, unpricedPendingBidIds } from '../lib/bids/bidAudits' import { openEstimatorQuestions, type OpenQuestionShape } from '../lib/bids/standingRulings' import { effectiveTwinQuestionKind } from '../../supabase/functions/_shared/twinQuestionKind' import type { RobotBlocked } from '../lib/bids/robotSilence' @@ -68,14 +68,16 @@ export function useBidAuditsPendingCount(enabled: boolean): { pending: number; a // Seal check is best-effort — fall back to the raw pending count. } try { - // One HEAD count per pending bid (cheap, indexed) instead of pulling every row. - const counts = await Promise.all( - pendingIds.map(async (bidId) => { - const { count } = await auditDb.from('bids_count_rows').select('id', { count: 'exact', head: true }).eq('bid_id', bidId) - return [bidId, count ?? 0] as const - }), - ) - unpricedBidIds = new Set(counts.filter(([, n]) => n === 0).map(([id]) => id)) + // One read for all the pending bids, each with at most one of its count rows (v2.9274). It was one HEAD + // count for each bid: 74 requests at once on every open of Bids and of Needs you, the largest load the + // app put on the database. + const priced: Array<{ id: string; bids_count_rows: Array<{ id: string }> | null }> = [] + for (const chunk of pendingPricedChunks(pendingIds)) { + const res = await auditDb.from('bids').select(PENDING_PRICED_SELECT).in('id', chunk).limit(1, { referencedTable: PENDING_PRICED_TABLE }) + if (res.error) throw new Error(res.error.message) + priced.push(...((res.data ?? []) as unknown as Array<{ id: string; bids_count_rows: Array<{ id: string }> | null }>)) + } + unpricedBidIds = unpricedPendingBidIds(pendingIds, priced) } catch { // Best-effort too — an unpriced audit then counts as pending, as before. } diff --git a/src/lib/bids/bidAudits.test.ts b/src/lib/bids/bidAudits.test.ts index 3c7aa533cb..2ce6a8dc4b 100644 --- a/src/lib/bids/bidAudits.test.ts +++ b/src/lib/bids/bidAudits.test.ts @@ -194,7 +194,7 @@ describe('questionContextLine', () => { }) // v2.2796: unpriced audits + the shadow-seal pairing. -import { isUnpricedAudit, pairTwinReferences, countWorkablePendingAudits } from './bidAudits' +import { isUnpricedAudit, pairTwinReferences, countWorkablePendingAudits, PENDING_PRICED_SELECT, pendingPricedChunks, unpricedPendingBidIds } from './bidAudits' describe('isUnpricedAudit', () => { it('true only when the draft loaded with zero active rows', () => { @@ -245,3 +245,35 @@ describe('countWorkablePendingAudits', () => { expect(countWorkablePendingAudits(audits, new Set(), new Set())).toBe(3) }) }) + +// v2.9274: one read says which pending bids are priced, where a count for each bid did. +describe('unpricedPendingBidIds', () => { + it('calls a bid unpriced when it came back with no count row, or did not come back', () => { + const rows = [ + { id: 'priced', bids_count_rows: [{ id: 'r1' }] }, + { id: 'empty', bids_count_rows: [] }, + { id: 'null', bids_count_rows: null }, + ] + expect([...unpricedPendingBidIds(['priced', 'empty', 'null', 'hidden'], rows)].sort()).toEqual(['empty', 'hidden', 'null']) + }) + + it('feeds the pending count as the old counts did', () => { + const audits = [ + { status: 'pending' as const, bid_id: 'priced' }, + { status: 'pending' as const, bid_id: 'empty' }, + { status: 'done' as const, bid_id: 'priced' }, + ] + const unpriced = unpricedPendingBidIds(['priced', 'empty'], [{ id: 'priced', bids_count_rows: [{ id: 'r1' }] }, { id: 'empty', bids_count_rows: [] }]) + expect(countWorkablePendingAudits(audits, new Set(), unpriced)).toBe(1) + }) + + it('names the relationship, since bids reaches its count rows two ways', () => { + expect(PENDING_PRICED_SELECT).toBe('id, bids_count_rows!bids_count_rows_bid_id_fkey(id)') + }) + + it('cuts the pending bids into reads of at most a hundred', () => { + const ids = Array.from({ length: 205 }, (_, i) => `b${i}`) + expect(pendingPricedChunks(ids).map((c) => c.length)).toEqual([100, 100, 5]) + expect(pendingPricedChunks([])).toEqual([]) + }) +}) diff --git a/src/lib/bids/bidAudits.ts b/src/lib/bids/bidAudits.ts index e4cd41d945..8a38842ea8 100644 --- a/src/lib/bids/bidAudits.ts +++ b/src/lib/bids/bidAudits.ts @@ -278,6 +278,33 @@ export function pairTwinReferences(twins: TwinPairingSource[], shadowRuns: Shado return out } +/** + * The one read that says which pending bids are priced (v2.9274): each bid with at most one of its count rows. The + * relationship is named, because `bids` reaches `bids_count_rows` two ways and PostgREST refuses to choose + * (PGRST201). The read sets the limit on the embedded table: `.limit(1, { referencedTable: PENDING_PRICED_TABLE })`. + */ +export const PENDING_PRICED_SELECT = 'id, bids_count_rows!bids_count_rows_bid_id_fkey(id)' +export const PENDING_PRICED_TABLE = 'bids_count_rows' +/** The most bids one such read names, so its address stays short. */ +export const PENDING_PRICED_CHUNK = 100 + +/** `ids` cut into reads of at most `size`. */ +export function pendingPricedChunks(ids: ReadonlyArray, size: number = PENDING_PRICED_CHUNK): string[][] { + const out: string[][] = [] + for (let i = 0; i < ids.length; i += size) out.push(ids.slice(i, i + size)) + return out +} + +/** + * The pending bids with nothing to judge (v2.9274): `rows` is the read above, and a bid that came back with no + * count row is unpriced. So is a bid that did not come back: a count row is read through its bid (the row policy of + * `bids_count_rows`), so a bid this person cannot read had a count of 0 under the old count for each bid too. + */ +export function unpricedPendingBidIds(pendingIds: ReadonlyArray, rows: ReadonlyArray<{ id: string; bids_count_rows?: ReadonlyArray | null }>): Set { + const priced = new Set(rows.filter((r) => (r.bids_count_rows?.length ?? 0) > 0).map((r) => r.id)) + return new Set(pendingIds.filter((id) => !priced.has(id))) +} + /** * The pending count every badge shows (Audits tab label, Needs-you card): pending * audits that are neither sealed (reference unsent) nor unpriced (no PT rows).