omi icon indicating copy to clipboard operation
omi copied to clipboard

fix(plugins/openfoodfacts): barcode hygiene, graceful 404s, and multilingual allergen matching

Open shuoYun114 opened this issue 2 months ago • 0 comments

Problem Description

The omi-openfoodfacts-app plugin provides nutrition, Nutri-Score, allergen detection, and barcode lookup tools for Omi users. However, in real-world usage and adversarial test scenarios, several edge cases lead to service errors, false negative allergen reports, or unexpected crashes:

  1. Barcode & URL Sanitization Limitations:

    • When users query with formatted barcodes (e.g. barcode: 3017666510800, #012345678905), packaged product URLs (https://world.openfoodfacts.org/product/3017666510800-nutella), or strings with wrapping quotes/spaces, the plugin fails or sends corrupted queries. Conversely, unstructured natural language text containing numbers (e.g. coca cola 1000ml) was previously extracting raw digits into bogus barcode queries.
  2. Unhandled 404 Status as Service Failure:

    • In _openfoodfacts_get, querying an uncataloged barcode returns HTTP 404 from upstream. Calling response.raise_for_status() treated expected misses as internal crashes ("Open Food Facts request failed"), obscuring the friendly "product not found" message and masking true upstream network errors.
  3. Dirty Data & Non-Finite Numbers in Community Catalog:

    • Open Food Facts catalog items occasionally contain dirty types or booleans in nutriscore_grade, ecoscore_grade, or nutriments. Calling .upper() or membership testing directly on these fields triggers AttributeError or TypeError. Similarly, non-finite values like "inf" or extreme floats passed to _safe_int triggered unhandled OverflowError.
  4. Allergen Detection Edge Cases (CJK Negation, Accents, and Singular/Plural Variants):

    • Non-spaced CJK ingredients (Chinese, Japanese, Korean) had a blunt whole-text negation check that caused false negatives if a disclaimer mentioned "no peanut oil" while ingredients contained peanuts.
    • Latin-script ingredients previously missed word stems (e.g., apples / grapes truncated to appl / grap).
    • Matching against official allergen tags (known_allergens, traces) previously required exact string equality, failing when a user specified singular terms (e.g. avoid: ["peanut"]) against standard plural tags (peanuts).

Proposed Solution

  1. Robust Barcode & URL Extraction:
    • Implement sanitize_barcode with prefix stripping, URL slug isolation, GTIN length/digit validation, and alphanumeric noise rejection.
  2. Graceful 404 Handling:
    • Return structured {"not_found": True, "status_code": 404} without raising exception noise or masking upstream errors.
  3. Resilient Data Sanitization:
    • Guard _safe_int against non-finite floats and OverflowError. Safely cast string grades and validate dict structures for nutriments.
  4. Context-Aware Multilingual Allergen Matching:
    • Traverse individual token matches in CJK with localized negation prefix checks (无, 不含, 零, 免).
    • Support Latin plural/singular stemming (_term_variants) and English negation markers (free from, free of, no , without ).
    • Match both singular and plural forms against official Open Food Facts tags and traces.
  5. Hermetic Test Suite & CI Check:
    • Add 29 hermetic standard-library tests in test_main.py (0.02s execution).
    • Register omi-openfoodfacts-tests in .github/checks-manifest.yaml.

Bounty Claim

  • Bounty: $50
  • USDT (TRC20): TGKmfSUmt2dggErAo8BicaCoXhkMwLetxV
  • Bitcoin (BTC): 1NkUnv23H65ZYJn64pseAcWRA6sJitxh2f
  • Contact Email: [email protected]

shuoYun114 avatar Sep 17 '26 22:09 shuoYun114