12-Stage Pipeline
Pipeline Overview: The PWIE pipeline processes user context through 12 deterministic stages, ensuring every user with the same profile receives identical meal recommendations.
1. Context
Gather user profile, goals, constraints
2. Goals
Compute nutrition targets (calories, macros)
3. Constraints
Apply hard constraints (allergens, diet)
4. Knowledge
Search LWKG for candidates
5. Template
Select meal structure
6. Candidates
Generate 10-20 meal plans
7. Scoring
Score by 4 dimensions
8. Ranking
Sort by composite score
9. Validation
Safety checks (allergens, health)
10. Selection
Pick top safe candidate
11. Assembly
LLM explanation with fallback
12. Logging
Audit event to database
Safety Validation System
3-Tier Safety Model: Every meal plan undergoes three levels of validation to ensure safety and appropriateness for the user.
🛑 BLOCK (Hard Constraints)
Plan is rejected if:
- Contains allergens
- Contains excluded foods
- Violates dietary type
- Unsafe calorie range
- Health condition conflict
⚠️ WARN (Soft Flags)
Plan flagged for review if:
- >30% nutrition deviation
- Extreme macro ratios
- High sodium content
- Rare allergen presence
- Unusual prep complexity
✅ PASS (Safe Plan)
Plan approved if:
- No hard constraints violated
- Within nutrition bounds
- Appropriate for health goals
- User ready for acceptance
- Fully documented
🔍 Validation Checks
Comprehensive checks include:
- Allergen cross-check
- Health condition matching
- Nutrition bounds verification
- Ingredient availability
- Recipe complexity match
Determinism Guarantee
Reproducibility: Every call with the same user profile, goals, and constraints produces identical recommendations. This is achieved through deterministic candidate selection using modulo-based rotation and sorted collections rather than random sampling.
Determinism Features:
- No Randomness - All randomness eliminated from pipeline; deterministic rotation used instead
- Reproducible Scoring - Scoring engine produces identical scores for identical inputs
- Consistent Ranking - Ranking algorithm always produces same order for same candidates
- Sorted Collections - All collections sorted by ID before processing to ensure order stability
- Verified in Tests - 16+ unit tests verify determinism with repeated executions
API Endpoint
POST /api/v1/recommendations/meal - Generate personalized meal plan
Request Body: User profile, health goals, dietary constraints, budget, and preferences
Response: Selected meal plan with 12-stage pipeline trace, composite scores, validation result, and LLM explanation