FORGE-85: latestPerGroup() replaces DISTINCT ON at 11 sites #701

Merged
gmackie merged 1 commit from FORGE-85-latest-per-group into main 2026-10-12 05:50:30 +00:00
Owner

Part 2 of FORGE-85 (D1 Phase 2, parent FORGE-69). Stacked on #696. No behaviour change.

  • latestPerGroup() in @forgegraph/db (packages/db/src/latest-per-group.ts): the caller's select carries a row_number() over (partition by … order by …) column; the helper wraps it as a subquery, keeps rank 1, orders by the partition keys (as DISTINCT ON did) and strips the rank. Portable to SQLite/D1.
  • Converted: effective-evidence.ts (3, now named latestHeadBuilds/TestRuns/Reviews), attestation-engine.ts + routers/review.ts (shared latestVerdictsByAuthor), fleet.ts deployments (latestDeploymentPerApp), load-workspace-public-feed.ts deployments, delivery-alert-sweep.ts (3), monitoring/evaluate straggler builds (moved to lib/latest-changeset-builds.ts, since route modules can't export helpers).
  • Not touched: external/deep health and node_resources readers (FORGE-84), registry proposal-repair raw SQL.
  • Two subqueries alias a colliding column name (apps.id/changesets.id, builds.status/changesets.status). The approved-changeset ordering keeps PG's asc nulls last explicitly (SQLite sorts NULLs first by default).

Verification

  • Golden tests packages/api/src/__tests__/latest-per-group.database.test.ts (7) and apps/web/src/lib/__tests__/latest-per-group.database.test.ts (5) assert each new query equals the verbatim old DISTINCT ON query on fixtures with multiple rows per group, filtered rows, id tiebreakers and a NULL left join. Green on PGlite and on Postgres 16.
  • Existing tests: effective-evidence, attestation, review idempotency/concurrency (PG16), public feed, monitoring evaluate, app-list health rollup. Three mock-based tests updated for the new query shape.
  • tsc clean (db, api, web); oxlint no new findings.

🤖 Generated with Claude Code

Part 2 of FORGE-85 (D1 Phase 2, parent FORGE-69). Stacked on #696. No behaviour change. - `latestPerGroup()` in `@forgegraph/db` (packages/db/src/latest-per-group.ts): the caller's select carries a `row_number() over (partition by … order by …)` column; the helper wraps it as a subquery, keeps rank 1, orders by the partition keys (as DISTINCT ON did) and strips the rank. Portable to SQLite/D1. - Converted: effective-evidence.ts (3, now named `latestHeadBuilds/TestRuns/Reviews`), attestation-engine.ts + routers/review.ts (shared `latestVerdictsByAuthor`), fleet.ts deployments (`latestDeploymentPerApp`), load-workspace-public-feed.ts deployments, delivery-alert-sweep.ts (3), monitoring/evaluate straggler builds (moved to `lib/latest-changeset-builds.ts`, since route modules can't export helpers). - Not touched: external/deep health and node_resources readers (FORGE-84), registry proposal-repair raw SQL. - Two subqueries alias a colliding column name (`apps.id`/`changesets.id`, `builds.status`/`changesets.status`). The approved-changeset ordering keeps PG's `asc nulls last` explicitly (SQLite sorts NULLs first by default). Verification - Golden tests `packages/api/src/__tests__/latest-per-group.database.test.ts` (7) and `apps/web/src/lib/__tests__/latest-per-group.database.test.ts` (5) assert each new query equals the verbatim old DISTINCT ON query on fixtures with multiple rows per group, filtered rows, id tiebreakers and a NULL left join. Green on PGlite and on Postgres 16. - Existing tests: effective-evidence, attestation, review idempotency/concurrency (PG16), public feed, monitoring evaluate, app-list health rollup. Three mock-based tests updated for the new query shape. - tsc clean (db, api, web); oxlint no new findings. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
FORGE-85: latestPerGroup() replaces DISTINCT ON at 11 sites
All checks were successful
CI / gitleaks (pull_request) Successful in 10s
CI / storybook (pull_request) Successful in 1m27s
CI / web-build (pull_request) Successful in 1m58s
CI / unit-api (pull_request) Successful in 1m17s
CI / go (pull_request) Successful in 57s
CI / database (pull_request) Successful in 2m1s
CI / checks (pull_request) Successful in 4m10s
CI / unit-web (pull_request) Successful in 4m32s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 36s
c9b90ecca4
Add latestPerGroup() to @forgegraph/db: the caller's select gets a
row_number() OVER (PARTITION BY ... ORDER BY ...) column, the helper wraps
it in a subquery, keeps rank 1, orders by the partition keys and strips the
rank. It is the form SQLite/D1 also runs.

Converted: effective-evidence.ts (3), attestation-engine.ts and
routers/review.ts (shared latestVerdictsByAuthor), fleet.ts deployments,
load-workspace-public-feed.ts deployments, delivery-alert-sweep.ts (3) and
the monitoring evaluate straggler lookup (moved to
lib/latest-changeset-builds.ts). The health and node telemetry readers stay
on DISTINCT ON for FORGE-84.

Golden tests in both DB lanes compare every new query with the verbatim
DISTINCT ON query it replaced (multiple rows per group, filtered rows,
id tiebreakers, NULL app left join, column-name collisions), on PGlite and
Postgres 16.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
gmackie changed target branch from FORGE-85-enum-arrays to main 2026-10-12 04:51:00 +00:00
gmackie force-pushed FORGE-85-latest-per-group from c9b90ecca4
All checks were successful
CI / gitleaks (pull_request) Successful in 10s
CI / storybook (pull_request) Successful in 1m27s
CI / web-build (pull_request) Successful in 1m58s
CI / unit-api (pull_request) Successful in 1m17s
CI / go (pull_request) Successful in 57s
CI / database (pull_request) Successful in 2m1s
CI / checks (pull_request) Successful in 4m10s
CI / unit-web (pull_request) Successful in 4m32s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 36s
to a7c14b7c82
All checks were successful
CI / gitleaks (pull_request) Successful in 18s
CF executor smoke / smoke (pull_request) Successful in 23s
CI / unit-api (pull_request) Successful in 3m49s
CI / unit-web (pull_request) Successful in 6m33s
CI / go (pull_request) Successful in 1m10s
CI / storybook (pull_request) Successful in 1m53s
CI / web-build (pull_request) Successful in 3m32s
CI / checks (pull_request) Successful in 3m16s
CI / database (pull_request) Successful in 4m12s
forgegraph/ci CI passed
CI / ci (pull_request) Successful in 36s
2026-10-12 05:26:20 +00:00
Compare
Sign in to join this conversation.
No description provided.