Skip to content

fix(data): backfill ingredient concern tiers#1169

Merged
ericsocrat merged 4 commits into
mainfrom
fix/ingredient-concern-tier-backfill
Jun 5, 2026
Merged

fix(data): backfill ingredient concern tiers#1169
ericsocrat merged 4 commits into
mainfrom
fix/ingredient-concern-tier-backfill

Conversation

@ericsocrat

Copy link
Copy Markdown
Owner

Summary

  • Backfills ingredient_ref.concern_tier using strict additive/E-code classification.
  • Recomputes products.ingredient_concern_score using tier weights:
    • Tier 1 = 15
    • Tier 2 = 40
    • Tier 3 = 100
    • capped at 100
  • Adds CI post-enrichment logic so future ingredient enrichment runs populate concern tiers before scoring.
  • Adds QA guards for concern-tier population and score formula consistency.

Validation

  • Migration applied locally:
    • UPDATE 24
    • UPDATE 204
  • Migration idempotency confirmed:
    • UPDATE 0
    • UPDATE 0
  • Concern-tier verification:
    • 24 concern-tier ingredients
    • 204 products with non-zero ingredient concern score
    • min score 15 / max score 80 / avg non-zero 29.14
  • QA checks added:
    • concern_tier populated when product ingredients exist = 0 violations
    • concern-tier linked products have concern score = 0 violations
    • ingredient_concern_score formula test = 0 rows

Notes

  • Existing unrelated scoring regression/high-additive-load QA rows were already present and are not caused by this patch.

Copilot AI review requested due to automatic review settings June 5, 2026 09:43
@vercel

vercel Bot commented Jun 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
tryvit Ready Ready Preview, Comment Jun 5, 2026 11:17am

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 358d776c2f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

WHERE p.is_deprecated IS NOT TRUE
)
UPDATE products p
SET ingredient_concern_score = cs.score

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Recompute product scores after changing concern input

When this migration changes products.ingredient_concern_score, the stored unhealthiness_score is left at the value computed from the old concern score. That concern value is an input to compute_unhealthiness_v32 in score_category, and the CI post-enrichment script explicitly re-scores after the same update, but the production migration commits immediately after line 133. Any product whose tier is backfilled here will expose stale overall scores until some later full re-score happens.

Useful? React with 👍 / 👎.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR backfills ingredient_ref.concern_tier using an additive/E-code classifier, recomputes products.ingredient_concern_score from tier weights (15/40/100, capped at 100), and adds CI post-enrichment + QA guards to keep concern-tier population and scoring consistent going forward.

Changes:

  • Adds a Supabase migration to classify concern tiers (without downgrading) and recompute ingredient_concern_score for all non-deprecated products.
  • Extends db/ci_post_enrichment.sql to populate concern tiers before recomputing ingredient_concern_score (and sets score to 0 when no tiered ingredients exist).
  • Adds QA checks for concern-tier population and a formula-consistency test for ingredient_concern_score.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.

File Description
supabase/migrations/20260605223000_backfill_ingredient_concern_tiers.sql New backfill migration for concern tiers + product concern-score recomputation.
db/ci_post_enrichment.sql Adds post-enrichment concern-tier classification and refactors concern-score recompute to cover all products.
db/qa/QA__scoring_formula_tests.sql Adds a QA test asserting ingredient_concern_score matches the tier-weight formula.
db/qa/QA__ingredient_quality.sql Adds QA guards to ensure concern tiers and concern scores are populated consistently.

OR n LIKE '%potassium nitrate%'
THEN 3

WHEN e_code IN ('e133','e150d','e211','e220','e223','e319','e385','e407','e407a','e621','e950','e951','e955')
WHEN e_code IN (
'e150','e150a','e150b','e150c','e172','e200','e202',
'e281','e282','e338','e339','e340','e341',
'e420','e421','e422','e440','e450','e451','e452',
OR n LIKE '%potassium nitrate%'
THEN 'Strict additive/E-code classifier: nitrite/nitrate preservative concern'

WHEN e_code IN ('e133','e150d','e211','e220','e223','e319','e385','e407','e407a','e621','e950','e951','e955')
WHEN e_code IN (
'e150','e150a','e150b','e150c','e172','e200','e202',
'e281','e282','e338','e339','e340','e341',
'e420','e421','e422','e440','e450','e451','e452',
Comment thread db/ci_post_enrichment.sql
OR n LIKE '%potassium nitrate%'
THEN 3

WHEN e_code IN ('e133','e150d','e211','e220','e223','e319','e385','e407','e407a','e621','e950','e951','e955')
Comment thread db/ci_post_enrichment.sql
WHEN e_code IN (
'e150','e150a','e150b','e150c','e172','e200','e202',
'e281','e282','e338','e339','e340','e341',
'e420','e421','e422','e440','e450','e451','e452',
Comment thread db/ci_post_enrichment.sql
OR n LIKE '%potassium nitrate%'
THEN 'Strict additive/E-code classifier: nitrite/nitrate preservative concern'

WHEN e_code IN ('e133','e150d','e211','e220','e223','e319','e385','e407','e407a','e621','e950','e951','e955')
Comment thread db/ci_post_enrichment.sql
WHEN e_code IN (
'e150','e150a','e150b','e150c','e172','e200','e202',
'e281','e282','e338','e339','e340','e341',
'e420','e421','e422','e440','e450','e451','e452',
Comment thread db/qa/QA__scoring_formula_tests.sql Outdated


-- ═══════════════════════════════════════════════════════════════════════════
-- Test 6: ingredient_concern_score matches concern-tier formula

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment on lines +202 to +215
-- ═══════════════════════════════════════════════════════════════════════════
-- 18. concern_tier should not remain all-zero once product_ingredient exists
-- ═══════════════════════════════════════════════════════════════════════════
SELECT '18. concern_tier populated when product ingredients exist' AS check_name,
CASE
WHEN NOT EXISTS (SELECT 1 FROM product_ingredient) THEN 0
WHEN EXISTS (
SELECT 1
FROM product_ingredient pi
JOIN ingredient_ref ir ON ir.ingredient_id = pi.ingredient_id
WHERE COALESCE(ir.concern_tier, 0) > 0
) THEN 0
ELSE 1
END AS violations;
Comment thread db/ci_post_enrichment.sql
Comment on lines +330 to +337
computed_scores AS (
SELECT
p.product_id,
COALESCE(ps.score, 0)::int AS score
FROM products p
LEFT JOIN product_scores ps ON ps.product_id = p.product_id
WHERE p.is_deprecated IS NOT TRUE
)
@ericsocrat ericsocrat merged commit c8e325b into main Jun 5, 2026
16 checks passed
@ericsocrat ericsocrat deleted the fix/ingredient-concern-tier-backfill branch June 5, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants