Skip to content

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 into
ansvisor:mainfrom
cihan-gyk:feature/product-visibility-matching
Oct 28, 2026
Merged

gkhngyk merged 12 commits into
ansvisor:mainfrom
cihan-gyk:feature/product-visibility-matching

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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:

  • the Shelf's "Products on the shelf · 41 of 320" and the "You" marks on shelf cards (part 3);
  • the Products table's shelves, win rate and position (part 4);
  • the Categories' "On shelf" and shelf share (part 5).

Migration 00360:

  • prompt_result_shopping_cards.catalog_product_id (→ catalog_products, on delete set null) and catalog_match (listing, gtin, url or title).
  • Two partial indexes: (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:

  • the marketplace listing the card links (ASIN, eBay item id), to a variant's marketplace_item_id;
  • a GTIN the card carries, without its padding;
  • its link, to a product's or a variant's page, with or without its query;
  • else 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.

When:

  • As cards are written (cloro-result-handler.js), 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.
  • Daily, after the catalog sweep (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:

  • B3 · scale (a460878). Bench with 50,000 products and 20,000 cards: ~44 s in one block → ~1–2 s, the longest block under 82 ms.
    • A word in more than 5% of the titles (and more than 500) no longer finds products to compare. It still counts in a title's score.
    • Cards are matched 500 at a time, with a turn of the event loop between.
    • The kept catalogs are capped at 200,000 products in all, not 50 brands. The brand read least recently goes first.
    • What changes: in a big catalog, some title matches are no longer made. No wrong new match appears: every match made is one the full comparison makes too. In the bench's random data at 20,000 products, about 5% of the old title matches were left out.
    • A catalog of 500 titles or fewer is matched exactly as before.
  • B4 · index (69c4e31): the (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.
  • B10 · GTIN keys (a460878): the daily sweep reads gtin, gtin13, gtin12, gtin14, ean and upc, as matching at write time does. Before, a card matched by its ean lost 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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. cd server && npx vitest run src/lib/catalog/card-match.test.js src/lib/cloro-result-handler.test.js
  2. With 00360 applied and a brand with a catalog:
    • run tracking on a shopping question whose answer shows one of the brand's products;
    • its card row gets catalog_product_id and catalog_match;
    • from part 3, the Shelf counts it.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

The web change here is the generated types only (web/src/types/supabase.ts).

Checks:

  • At the top of the chain (part 14): server/ npm test (3,383 tests), web/ vitest (4,451 tests) and yarn build.
  • At this branch's tip: yarn typecheck, and the tests, eslint and prettier of the files it changes.
  • bash supabase/build-schema.sh leaves schema.sql as committed.

For you, @gkhngyk:

🤖 Generated with Claude Code

cihan-gyk and others added 6 commits October 27, 2026 18:33
…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]>
…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]>
@cihan-gyk

Copy link
Copy Markdown
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 gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.js conflicts: main now runs purgeRemovedCatalogProducts() after the catalog sweep. Chain the three as sweep, then purge, then sweepShoppingCardMatches(), each with its own catch. supabase/schema.sql also conflicts (rebuild it). Part 1's dashboard-navigation.test.ts conflict carries up.
  • 00360.
    • Two nullable columns with no default, so there's no table rewrite. The foreign key is on delete set null to catalog_products, and the check limits catalog_match to listing, gtin, url or title. Both use if 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_matches is security definer with search_path = public. Execute is revoked from public, anon and authenticated and granted to service_role only. It only updates cards with brand_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.
  • 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 unseen bound 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-people timing 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. Web tsc is 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):

  1. set_shopping_card_matches trusts product_id. Add and exists (select 1 from catalog_products p where p.id = m.product_id and p.brand_id = p_brand_id) (or a null product), plus matched_brand_role = 'own' as the header comment says. Only the service role calls it, but the database would then enforce what the comment promises.
  2. catalogIndex builds 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, as matchCards does between cards.
  3. The two create index statements aren't concurrently, so they hold a write lock on prompt_result_shopping_cards while they scan it. Check the table's size before applying. If it's large, run them on their own, with concurrently, before the rest of the migration.

@gkhngyk
gkhngyk merged commit 474f4ae into ansvisor:main Oct 28, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants