name: pass-7b-implementation description: Implement the NutriAI Pass 7B meal planning engine redesign. Phase 1 is deterministic portioning where USDA resolves all macros and the LLM never sees calorie numbers. Phase 2 adds an AI overlay for ingredient naming only. This is the top engineering priority and the primary App Store release blocker.
The non-negotiable architectural constraint for this entire skill: the LLM never computes or receives final macro values. USDA FoodData Central resolves everything server-side.
Pre-flight reads — mandatory before any code change
Read each file fully before proposing any change:
- api/_lib/food-intelligence.js — understand the USDA resolution pipeline
- The meal plan generation endpoint — trace from api/food.js via ?action= routing to find the handler file
- supabase/migrations/ — understand meal_plans and meal_plan_items schema
- src/context/AppContext.tsx — understand plan state management
- Every file that currently assigns macro values to plan items
Before writing: list every location where macro values are currently assigned. Flag any location where an LLM response currently sets calories, protein, carbs, or fat.
Phase 1 — Deterministic portioning
Goal: every macro value comes from USDA. Zero exceptions.
Deterministic portioning module: Input: ingredient concept (name + quantity descriptor) Output: resolved macros from USDA FoodData Central Constraint: no calorie or macro numbers in any LLM prompt or response
Slot-level isolation using Promise.all: Each meal slot resolves independently. A failed slot retries in isolation — other slots unaffected. After retry failure: mark that slot with an error state. Coverage gate: surface a user-visible warning when fewer than 50% of slots have resolved. Do not treat it as a hard failure.
Double-normalization guard: After USDA resolution assert: abs(raw_calories - usda_resolved_calories) / raw_calories < 0.05 If guard fires: log with ingredient name, raw value, and resolved value. Alert. Never silently use the wrong value. Critical test case: banana input 312 cal → output must be 312 cal. It must never resolve to 975 cal/100g.
Feature flag and versioning: USE_AI_ORCHESTRATOR=false → deterministic path USE_AI_ORCHESTRATOR=true → AI-orchestrated path engine_version: 'behavioral-v1' vs 'ai-orchestrated-v1'
Phase 1 verification — required before starting Phase 2
□ pnpm build: PASS □ pnpm test: all meal planning tests pass □ Banana test: 312 cal in → 312 cal out □ Slot isolation: force one slot to fail → others complete □ Coverage gate: 4 of 8 slots resolved → warning displayed, no crash □ Grep LLM prompts for calorie patterns → 0 matches □ Grep LLM responses for numeric calorie patterns → 0 matches
Phase 2 — AI overlay (only after Phase 1 verified)
Goal: variety and natural ingredient naming via LLM, no macro computation.
LLM prompt contains:
- User dietary preferences and restrictions
- Meal slot context (breakfast / lunch / dinner / snack)
- Target food categories only — no calorie targets, no macro targets Expected output: ingredient names + approximate natural-language quantities e.g. "one medium banana", "150g grilled chicken breast" No numbers representing calories or macros.
After every LLM response — grep for: \d+\s*(cal|kcal|calories|g protein|g carb|g fat) If any match: reject the response, log the violation, fall back to the deterministic path for that slot.
USDA resolves all macros from the ingredient concept list — same pipeline as Phase 1.