Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/recent-features/v2.9274.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions src/content/releaseNotes/v2.9274.ts
Original file line number Diff line number Diff line change
@@ -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
20 changes: 11 additions & 9 deletions src/hooks/useBidAuditsPendingCount.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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.
}
Expand Down
34 changes: 33 additions & 1 deletion src/lib/bids/bidAudits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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([])
})
})
27 changes: 27 additions & 0 deletions src/lib/bids/bidAudits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, 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<string>, rows: ReadonlyArray<{ id: string; bids_count_rows?: ReadonlyArray<unknown> | null }>): Set<string> {
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).
Expand Down
Loading