feat: server-side nearHex color-distance sort for polish list, web and mobile color search #407
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #407"
general
weight: 0.35
mattmck
Follow-up from #399 (mobile By color): catalog color search fetched one page of a color family and ranked it on the device, so in a big family the best match could be missing. It turned out the web was worse.
/polishes/searchranked the newest 50 color polishes. Polish detail "related shades" and Looks gap fills also ranked a newest-N page.API:
GET /api/polishes?nearHex=RRGGBB[,…]colorDistance. It equals the sharedcolorDistance(resolveDisplayHex(p), target), so clients can grade it with the dupe thresholds.#, comma-separated or repeated, up to 24. Anything else is a 400. Several targets rank by the nearest one, which is how web color search ranks a harmony palette in one query.POLISH_VISIBLE_SQL), all other filters, paging andCOUNT() OVER()are untouched. It is part of the per-user list cache key, andsortBy/sortOrderare ignored when it is set.polishes.tsdiff: limited to query parsing, the select column and ORDER BY. The logic lives in the newlib/near-hex.ts.GET /api/catalog/search: no change. Neither web nor mobile By color uses it; both useGET /api/polishes.Why stored generated columns, not a query-time SQL function
hex_to_oklab()already exists (migration20261017024958000). I measured both options with EXPLAIN ANALYZE on a copy of the dev DB with 60k synthetic shades:| | ranking 60k rows (distance only) | full list query, pink family (5.2k rows) | full list query, whole catalog |
|---|---|---|---|
| default sort (
createdAt) | n/a | 36.1 ms | 495 ms ||
hex_to_oklab()per row at query time | 73 ms | 36.2 ms | 634 ms || stored
shade.oklab_l/a/b(this PR) | 9.5 ms | 37.6 ms | 526 ms |nearHexcosts ~1.5 ms over the default sort on a family page (top-N heapsort).COUNT() OVER()over every row, and it is the same with or withoutnearHex. That is a possible follow-up, not part of this PR.20261023001520034_shade-display-oklabadds the three STORED GENERATED columns. They use the same trimmed display-hex expression ascolor_family, so every write path keeps them current. The Down section drops them. Up/Down/Up ran cleanly on 60k rows in ~1.1 s.SQL ↔ TS parity test
tests/near-hex.unit.test.cjsfollows the same pattern as the color-family test:hex_to_oklab()indocs/schema.sqland in its migration, and evaluates them in JS against the sharedhexToOklabon 24 fixed hexes (tolerance 1e-12). Changing one coefficient by 1e-10 makes it fail; I checked.NEAR_HEX_SQL_TEST_DATABASE_URL: it also compares the live function and the list query's distance expression. I ran this locally against the bench DB and it passed.Clients
/polishes/search: loads the whole owned color collection (listAllPolishes). Catalog rows come from one debouncednearHexquery for the active color, the focused target, palette anchors and the raw (unsnapped) harmony targets (120 rows). Brand and finish filters go to the server. Client-side ranking stays only over that loaded set. The "collection" scope is now complete; before, it only held owned rows that happened to be among the newest 50.nearHex=<display hex>&pageSize=6, minus the polish itself.nearHex, so it gets the nearest shades instead of the newest 60.nearHex=<picked>, narrowed only by the selected family chips. This replaces the 250-row "picked color's family" sample.catalogFamiliesForis replaced bycatalogColorQuery, and the tests are updated.Verification
npm run typecheck,npm run lint(0 errors; the 11 warnings are all in files this PR doesn't touch), and unit tests for functions, shared, web and mobile.-
nearHex=E0457Breturned total 60,267 with the nearest at 0.0043, and a bad value got a 400.- In the browser,
/polishes/search?color=E0457Brendered 20 owned + 120 server-ranked rows, with the top row matching the API's nearest.access-control-reviewer: no findings. Visibility, theui.user_id = $1join, parameter binding, the fallback count and the per-user cache key are unchanged, and there is no injection path because hexes are validated and bound as numbers. Following its suggestion, the test now also asserts the caller join and$1.docs-sync-checker: docs in sync.Not verified:
/polishes/detailand/polishes/looksin the browser. Both stuck on their Suspense fallback in my local dev setup, and the detail page does the same with this PR's change reverted, so this predates the PR. Their logic is covered by unit tests (Looks) and typecheck only.Rollout
ALTER TABLE shade ADD COLUMN … GENERATED … STOREDrewritesshadeunder an ACCESS EXCLUSIVE lock. On 60k rows the migration took ~1.1 s, and the prod catalog is smaller, but run it at a quiet time. Deploy runsnpm run migratebefore the functions deploy. A web build that ships before the new functions is harmless, because old functions ignorenearHexand return the default sort.The
/create-migrationskill can only be invoked by the user, so I created the migration with the repo'smigrate:createscript and hand-wrote the Down section anddocs/schema.sql.🤖 Generated with Claude Code
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
Summary by CodeRabbit
New Features
Color searches now find nearby catalog shades using the active color targets, with optional family, brand, and finish filters.
Owned shades remain available alongside catalog matches, and inventory changes update search results.
Related shades and missing colors in recommended looks are matched by color similarity.
The polish API supports searches by up to 24 target colors and returns each result’s distance from its nearest target.
Bug Fixes
* Search results and related shades now reflect server-ranked color matches rather than ranking a limited set of results locally.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->