Repository navigation
feat(shopping): match each AI shopping card to the brand's catalog product it shows (Product Visibility, part 2) - #1637
Merged
gkhngyk merged 12 commits intoOct 28, 2026
Conversation
…popup and the Shelf and Products tabs (product visibility, part 1) The first step to the Products screens in Drive › E-commerce (ECOM-spec, ECOM-mockup): the page says what it's for, "Which of your products AI recommends, why, and what it sells.", with "Why Ansvisor Product Visibility?" beside the title. That opens the four steps from the AI shelf to the sale, and offers Connect Google Analytics while GA isn't connected (ECOM-010). Under the title, the data sources (ECOM-009), as on AI Traffic: - the product catalog (its products and last sync, Manage, or Connect); - Google Analytics and Search Console, each with what it adds here; - a Marketplaces & feeds row: Merchant Center connects as a catalog source, and the marketplaces show Soon until they have a connector. The tabs read Shelf, Prompts, Products and Competitors, the one open in the URL (?tab=products). The sidebar item is new. AI Traffic's Google source card is shared, with an optional line for while it isn't connected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…oduct it shows (product visibility, part 2) Product Visibility's Shelf, Products and Categories (ECOM-005…008) count a brand's products on the AI shelf, so each product card that is the brand's own now records which catalog product it shows (prompt_result_shopping_cards.catalog_product_id, and catalog_match for how it was found; 00360). lib/catalog/card-match.js matches in order: - by the marketplace listing a card links (ASIN, eBay item id), to a variant's marketplace_item_id; - by a GTIN the card carries, without its padding; - by its link, to a product's or variant's page, with or without its query; - else by its title: at least 0.6 of the words in common (Dice) and 0.1 clear of the next product, the brand's name and filler words aside. A key two products share matches neither. Cards are matched as they're written, against the brand's catalog read at most every 10 minutes. If the catalog can't be read, the cards go in without matches. If the API runs before 00360, they go in without the new columns rather than not at all. After the daily catalog sweep, each brand whose catalog synced in the last day has its own cards of the last 90 days matched again. Only the matches that changed are written, 500 a call, through set_shopping_card_matches (service role only). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ibility-shell Co-Authored-By: Claude Opus 5.5 <[email protected]>
…visibility-matching Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a product's delete (00360) `on delete set null` looks up a deleted catalog product's cards by the product alone, which the (brand_id, catalog_product_id) index can't serve: each delete would scan the cards. A partial index on catalog_product_id covers it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…loop, and read every GTIN key in the sweep A 50,000-product catalog took about 90 s to match 20,000 cards, in one synchronous run. Now about 1 s, the longest hold under 70 ms: - A word in more than 5% of the titles (and more than 500) finds no products to compare; it still counts in a title's score. A title of common words only is found by its rarest one. A product not compared can't come within the margin of the best: when one could (it would share only the card's common words), no title match is made. So every title match is one the full comparison makes too; a few it made are not. A catalog of 500 titles or fewer is matched exactly as before. - matchCards matches 500 cards at a time, with a turn of the event loop between (the bot-log parser's nextTurn). - The kept catalogs are capped at 200,000 products in all, not 50 brands; the brand read least recently goes first, and a read keeps a brand. - The daily sweep reads a card's GTIN under gtin, gtin13, gtin12, gtin14, ean and upc, as matching at write time does, not under gtin alone. Co-Authored-By: Claude Opus 5.5 <[email protected]>
7 of 12 tasks
# Conflicts: # server/src/server.js # supabase/schema.sql
# Conflicts: # web/src/lib/dashboard-navigation.test.ts
…y-matching # Conflicts: # supabase/schema.sql
Collaborator
Author
|
Merged main forward (8356ba3) through the stack; supabase/schema.sql rebuilt. Typecheck and lint pass on the chain tip. |
…ing cards to the catalog A card's link has its trailing slashes cut by a walk back rather than an end-anchored regex, and a title match reads a card title's first 40 words (MAX_CARD_TITLE_WORDS): each word more was compared with every product it found.
gkhngyk
approved these changes
Oct 28, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
Looks good with 51f8b98: matching a card to the catalog now bounds its work on a long card link or title. pathKey cuts trailing slashes with a walk back instead of an end-anchored regex, which took time quadratic in a long run of slashes. A title match reads the card title's first 40 words (MAX_CARD_TITLE_WORDS); before, each extra word was compared with every product it found. Each fix has a test that fails without it. Later parts carry the fix through merges.
What I checked:
- Own change. cf37045..a460878: the merge of part 1, then 69c4e31 (the product-only index for
on delete set null) and a460878 (matching in slices, GTIN keys read in the sweep). - Merge on today's main. It needs a merge of main.
server/src/server.jsconflicts: main now runspurgeRemovedCatalogProducts()after the catalog sweep. Chain the three as sweep, then purge, thensweepShoppingCardMatches(), each with its own catch.supabase/schema.sqlalso conflicts (rebuild it). Part 1'sdashboard-navigation.test.tsconflict carries up. - 00360.
- Two nullable columns with no default, so there's no table rewrite. The foreign key is
on delete set nulltocatalog_products, and the check limitscatalog_matchto listing, gtin, url or title. Both useif not exists. - Two partial indexes (
where catalog_product_id is not null): one per brand for the reads, and one on the product alone for deletes. Main's new 90-day purge of removed products relies on the second one. set_shopping_card_matchesissecurity definerwithsearch_path = public. Execute is revoked from public, anon and authenticated and granted to service_role only. It only updates cards withbrand_id = p_brand_id, and only those whose match changed.- Members still read the cards through the existing RLS. Nothing new is granted to anon or authenticated. The number clashes with nothing on main, and nothing outside this stack uses 00360.
- Two nullable columns with no default, so there's no table rewrite. The foreign key is
- Matching. The order is listing, GTIN, link, then title (Dice ≥ 0.6, and it must beat the runner-up by 0.1). A key two products share matches neither. Common words don't widen the compared set, and the
unseenbound stops a product that wasn't compared from tying the best one. When cards are written, the catalog index is cached per brand for 10 minutes, with a total product cap. If the catalog can't be read, the cards are still written. If the API ships before 00360, the upsert retries without the two columns, so cards are never lost. The daily sweep only revisits brands whose catalog synced in the last 26 hours, one brand at a time, and logs a failure without stopping the others. - Tests. The PR's own tests pass (card-match 15, cloro-result-handler 7), and so does the full server suite, apart from the known flaky
content-peopletiming test, which fails the same way on the PR head. With the fix: card-match 17, and the full suite again with only the same flaky test. Webtscis clean, and prettier and eslint are clean on the touched files. - Deploy. Apply 00360 before parts 3–5 merge, because their reads select
catalog_product_id.
Follow-ups (none block):
set_shopping_card_matchestrustsproduct_id. Addand exists (select 1 from catalog_products p where p.id = m.product_id and p.brand_id = p_brand_id)(or a null product), plusmatched_brand_role = 'own'as the header comment says. Only the service role calls it, but the database would then enforce what the comment promises.catalogIndexbuilds a big catalog's index in one go on the API's event loop: on the first own card per brand every 10 minutes, and in the sweep. Yield between slices of products, asmatchCardsdoes between cards.- The two
create indexstatements aren'tconcurrently, so they hold a write lock onprompt_result_shopping_cardswhile they scan it. Check the table's size before applying. If it's large, run them on their own, withconcurrently, before the rest of the migration.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Product Visibility, part 2 of 14: each AI shopping card linked to the catalog product it shows (ECOM-005). The Shelf, Products and Categories count a brand's products on the AI shelf by this link.
Stacked on #1636. This PR's own change is its commits after #1636's: 17bacc9, 69c4e31, a460878.
Where you see it: nothing on screen in this PR. From part 3 on, these matches are what you see:
Migration 00360:
prompt_result_shopping_cards.catalog_product_id(→catalog_products,on delete set null) andcatalog_match(listing,gtin,urlortitle).(brand_id, catalog_product_id)for the reads, and(catalog_product_id)for a product's delete.set_shopping_card_matches(p_brand_id, p_matches): writes matches in bulk, only the brand's own cards and only the changed ones. Security definer, service role only. Members read the cards as before (RLS).Matching (
server/src/lib/catalog/card-match.js), in this order:marketplace_item_id;When:
cloro-result-handler.js), against the brand's catalog read at most every 10 minutes.server.js): each brand whose catalog synced in the last day has its own cards of the last 90 days matched again. Only changed matches are written, 500 a call.Fixed in the pre-push review:
(catalog_product_id)index above. Without it, each product delete scanned the cards: a source removed in Settings, or chore(catalog): delete catalog products removed more than 90 days ago #1632's purge.gtin,gtin13,gtin12,gtin14,eanandupc, as matching at write time does. Before, a card matched by itseanlost its match the next day.Related issue
No issue: ECOM-005 in Drive › E-commerce › ECOM-spec.md. The screens' tasks were only in epic #1190, closed as not planned. It builds on the catalog model (#1263).
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
cd server && npx vitest run src/lib/catalog/card-match.test.js src/lib/cloro-result-handler.test.jscatalog_product_idandcatalog_match;Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)The web change here is the generated types only (
web/src/types/supabase.ts).Checks:
server/npm test(3,383 tests),web/vitest(4,451 tests) andyarn build.yarn typecheck, and the tests, eslint and prettier of the files it changes.bash supabase/build-schema.shleavesschema.sqlas committed.For you, @gkhngyk:
needs-migrationlabel (I can't set labels on this repo).supabase db pushmay need--include-all.catalog_product_id, and says it can't read until 00360 is there. The API is safe either way (see above).server.js. Whichever merges second keeps both.🤖 Generated with Claude Code