# asiertxo09/obradoriq architecture

> Architecture diagram of the GitHub repository asiertxo09/obradoriq, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/asiertxo09/obradoriq
- Repository: https://github.com/asiertxo09/obradoriq

## Overview

1. **Repository shape**
   - This is a **single full-stack product**: a FastAPI backend plus a React/Vite frontend.
   - The backend is the system of record and business logic; the frontend is a thin dashboard that consumes `/api`.
   - Persistence is relational (local dev DB plus PostgreSQL in deployment).

2. **Primary architectural boundary**
   - The repo is organized around a strict split between:
     - a **deterministic recommender core** that computes all numeric recommendations,
     - and an **LLM layer** that only phrases, routes, or explains those grounded outputs.
   - This is the key design constraint in the product: the model cannot invent numbers.

3. **Data ingestion and enrichment**
   - `backend/app/data_hub/` handles ingestion of daily bakery data and external context.
   - Inputs include sales, waste, products, and sites from CSV/POS-style files, plus:
     - weather from **Open-Meteo**
     - holiday flags from a **Madrid calendar**
   - `generate_data.py` and `seed.py` support synthetic/demo dataset creation and initial population.

4. **Domain model and persistence**
   - `backend/app/models/` defines the persisted entities; `schemas/` defines API-facing shapes.
   - `service.py` appears to coordinate domain operations over the database layer.
   - Tests emphasize **multi-tenant / cross-tenant isolation**, so the DB boundary matters: data must stay scoped by bakery/site/account.

5. **Deterministic recommendation engine**
   - `backend/app/recommender/` is the core analytics engine.
   - It includes:
     - `forecast.py` for demand prediction,
     - `newsvendor.py` for production quantity optimization,
     - `production.py` for daily production planning,
     - `reallocation.py` for cross-site plan redistribution,
     - `intraday.py` for live-day adjustments,
     - `signals.py` for weather/holiday/confidence features,
     - `backtest.py` and `intraday_backtest.py` for evaluation.
   - This layer is explicitly designed to be **pure Python and deterministic**.

6. **Trust and grounding layer**
   - `backend/app/trust/layer.py` is the architectural guardrail.
   - It masks sensitive fields, checks that LLM output matches computed values, and rejects outputs that drift from grounded numbers.
   - This means the LLM is treated as a **phrase generator**, not a calculator.

7. **Agent and orchestration layer**
   - `backend/app/llm/` contains the conversational agent stack:
     - `orchestrator.py` for higher-level agent flow,
     - `router.py` for model/tool routing and fallback,
     - `agents.py` and `tools.py` for tool-backed actions.
   - The assistant can answer questions, draft production sheets, and import messy input by calling recommender functions as tools.
   - The LLM provider is **pluggable** and can be Anthropic, Groq, NVIDIA, or another OpenAI-compatible endpoint.

8. **API surface**
   - `backend/app/api/` exposes the backend to the UI and external callers.
   - `routes.py` defines endpoints; `deps.py` handles shared dependencies such as auth/context.
   - `core/security.py` suggests authenticated access, including an admin-gated data generation path mentioned in the README.

9. **Frontend role**
   - `frontend/` is a TypeScript React app built with Vite.
   - It provides the user-facing tabs for:
     - landing/onboarding,
     - live intraday planning,
     - simulation/reallocation,
     - conversational “Ask ObradorIQ”.
   - In production, the frontend is built statically and served by the FastAPI service.

10. **Operational data flows**
   - The main flow is:
     - ingest sales/waste/site context,
     - enrich with weather/holiday signals,
     - compute forecast and production recommendations,
     - optionally reallocate across sites,
     - phrase through the trust-checked LLM,
     - persist recommendations and user decisions.
   - A second flow supports the **intraday/live plan**, where minute-stamped sales events are used to revise the plan during the day.

11. **Evaluation and feedback loop**
   - Backtests are first-class artifacts in the repo, not an afterthought.
   - They measure historical and walk-forward performance for baseline, forecast, newsvendor, and intraday strategies.
   - User decisions, recommendation audits, and weekly summaries create a feedback loop for the owner and for system evaluation.

12. **Deployment and engineering support**
   - `Dockerfile`, `docker-compose.yml`, and `render.yaml` show two deployment modes: local containerized and Render-hosted.
   - GitHub Actions runs the full test suite in CI.
   - The `tests/` directory is broad and architecture-relevant: API/auth, agent trust, backtests, intraday behavior, CSV validation, and end-to-end smoke coverage.

Main entry points: [`backend/app/recommender/forecast.py`](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/forecast.py) (Recommender core), [`frontend/src/App.tsx`](https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/App.tsx) (Dashboard), [`backend/app/models/entities.py`](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/models/entities.py) (Entities), [`backend/app/data_hub/ingest.py`](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/data_hub/ingest.py) (Data hub)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_main["App entry<br/>fastapi startup<br/>[main.py]"]
  node_api["API layer<br/>http routes<br/>[routes.py]"]
  node_security["Security<br/>auth guard<br/>[security.py]"]
  node_service["Domain service<br/>application service<br/>[service.py]"]
  node_models[("Entities<br/>persistence models<br/>[entities.py]")]
  node_schemas["Schemas<br/>api contracts<br/>[schemas.py]"]
  node_datahub["Data hub<br/>ingestion pipeline<br/>[ingest.py]"]
  node_weather["Weather<br/>external signal<br/>[weather.py]"]
  node_calendar["Holiday calendar<br/>external signal<br/>[calendar.py]"]
  node_recommender["Recommender core<br/>deterministic analytics<br/>[forecast.py]"]
  node_forecast["Forecasting<br/>demand model<br/>[forecast.py]"]
  node_newsvendor["Production opt<br/>quantity optimizer<br/>[newsvendor.py]"]
  node_intraday["Intraday<br/>live-day planner<br/>[intraday.py]"]
  node_reallocation["Reallocation<br/>cross-site planner<br/>[reallocation.py]"]
  node_trust["Trust layer<br/>grounding guardrail<br/>[layer.py]"]
  node_llm["Agent layer<br/>llm orchestration<br/>[orchestrator.py]"]
  node_router["Model router<br/>provider routing<br/>[router.py]"]
  node_tools["Tools<br/>tool bridge<br/>[tools.py]"]
end

subgraph group_frontend["Frontend"]
  node_frontend["Dashboard<br/>react app<br/>[App.tsx]"]
  node_live["Live planning<br/>ui view<br/>[Live.tsx]"]
  node_simulate["Simulation<br/>ui view<br/>[Simulate.tsx]"]
  node_tour["Ask ObradorIQ<br/>chat ui<br/>[Tour.tsx]"]
  node_api_client["API client<br/>frontend adapter<br/>[api.ts]"]
end

subgraph group_ops["Ops & data"]
  node_seed["Seed data<br/>bootstrap script<br/>[seed.py]"]
  node_gen["Synthetic data<br/>demo generator<br/>[generate_data.py]"]
  node_fixtures["CSV fixtures<br/>sample data<br/>[sales.csv]"]
  node_ci["CI pipeline<br/>test automation<br/>[ci.yml]"]
end

node_main -->|"mounts"| node_api
node_api -->|"uses"| node_security
node_api -->|"calls"| node_service
node_service -->|"persists"| node_models
node_service -->|"shapes"| node_schemas
node_datahub -->|"ingests"| node_fixtures
node_datahub -->|"enriches with"| node_weather
node_datahub -->|"enriches with"| node_calendar
node_datahub -->|"loads"| node_models
node_recommender -->|"includes"| node_forecast
node_recommender -->|"includes"| node_newsvendor
node_recommender -->|"includes"| node_intraday
node_recommender -->|"includes"| node_reallocation
node_forecast -->|"uses"| node_weather
node_forecast -->|"uses"| node_calendar
node_newsvendor -->|"reads"| node_models
node_intraday -->|"reads"| node_models
node_llm -->|"routes through"| node_router
node_llm -->|"uses"| node_tools
node_tools -->|"calls"| node_recommender
node_llm -->|"checked by"| node_trust
node_trust -->|"verifies against"| node_recommender
node_frontend -->|"calls"| node_api_client
node_frontend -->|"contains"| node_live
node_frontend -->|"contains"| node_simulate
node_frontend -->|"contains"| node_tour
node_api_client -->|"requests"| node_api
node_seed -->|"populates"| node_models
node_gen -->|"creates"| node_fixtures
node_ci -->|"tests"| node_main
node_ci -->|"tests"| node_frontend

click node_main "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/main.py"
click node_api "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/api/routes.py"
click node_security "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/core/security.py"
click node_service "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/service.py"
click node_models "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/models/entities.py"
click node_schemas "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/schemas/schemas.py"
click node_datahub "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/data_hub/ingest.py"
click node_weather "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/data_hub/weather.py"
click node_calendar "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/data_hub/calendar.py"
click node_recommender "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/forecast.py"
click node_forecast "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/forecast.py"
click node_newsvendor "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/newsvendor.py"
click node_intraday "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/intraday.py"
click node_reallocation "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/reallocation.py"
click node_trust "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/trust/layer.py"
click node_llm "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/llm/orchestrator.py"
click node_router "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/llm/router.py"
click node_tools "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/llm/tools.py"
click node_frontend "https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/App.tsx"
click node_live "https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/Live.tsx"
click node_simulate "https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/Simulate.tsx"
click node_tour "https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/Tour.tsx"
click node_api_client "https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/api.ts"
click node_seed "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/seed.py"
click node_gen "https://github.com/asiertxo09/obradoriq/blob/main/backend/app/generate_data.py"
click node_fixtures "https://github.com/asiertxo09/obradoriq/blob/main/data/sales.csv"
click node_ci "https://github.com/asiertxo09/obradoriq/blob/main/.github/workflows/ci.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_api,node_security,node_service,node_models,node_schemas,node_datahub,node_weather,node_calendar,node_recommender,node_forecast,node_newsvendor,node_intraday,node_reallocation,node_trust,node_llm,node_router,node_tools toneBlue
class node_frontend,node_live,node_simulate,node_tour,node_api_client toneAmber
class node_seed,node_gen,node_fixtures,node_ci toneMint
```

## Components

### Backend

FastAPI service, domain logic, and persistence.

- [App entry](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/main.py): fastapi startup · Bootstraps the backend service and wires the API application. · `backend/app/main.py`
- [API layer](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/api/routes.py): http routes · Exposes backend endpoints and shared request dependencies. · `backend/app/api/routes.py`
- [Security](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/core/security.py): auth guard · Authentication and access-control helpers for protected operations. · `backend/app/core/security.py`
- [Domain service](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/service.py): application service · Coordinates business operations over the database and models. · `backend/app/service.py`
- [Entities](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/models/entities.py): persistence models · Persisted bakery, site, sales, waste, and recommendation entities. · `backend/app/models/entities.py`
- [Schemas](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/schemas/schemas.py): api contracts · Request and response shapes exposed by the API. · `backend/app/schemas/schemas.py`
- [Data hub](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/data_hub/ingest.py): ingestion pipeline · Imports bakery data and enriches it with external context. · `backend/app/data_hub/ingest.py`
- [Weather](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/data_hub/weather.py): external signal · Fetches Open-Meteo weather features used for enrichment. · `backend/app/data_hub/weather.py`
- [Holiday calendar](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/data_hub/calendar.py): external signal · Adds Madrid holiday flags and calendar context. · `backend/app/data_hub/calendar.py`
- [Recommender core](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/forecast.py): deterministic analytics · Pure Python forecasting and optimization engine for grounded numeric recommendations. · `backend/app/recommender/forecast.py`
- [Forecasting](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/forecast.py): demand model · Predicts demand from historical sales and signals. · `backend/app/recommender/forecast.py`
- [Production opt](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/newsvendor.py): quantity optimizer · Computes production quantities from forecasts and uncertainty. · `backend/app/recommender/newsvendor.py`
- [Intraday](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/intraday.py): live-day planner · Revises plans using minute-stamped sales and live signals. · `backend/app/recommender/intraday.py`
- [Reallocation](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/recommender/reallocation.py): cross-site planner · Redistributes production across sites when inventory or demand shifts. · `backend/app/recommender/reallocation.py`
- [Trust layer](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/trust/layer.py): grounding guardrail · Masks sensitive fields and verifies LLM output matches computed values. · `backend/app/trust/layer.py`
- [Agent layer](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/llm/orchestrator.py): llm orchestration · Routes prompts, tools, and fallback behavior for conversational actions. · `backend/app/llm/orchestrator.py`
- [Model router](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/llm/router.py): provider routing · Selects or falls back between pluggable LLM providers. · `backend/app/llm/router.py`
- [Tools](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/llm/tools.py): tool bridge · Wraps recommender and data operations for agent use. · `backend/app/llm/tools.py`

### Frontend

React/Vite dashboard for operators.

- [Dashboard](https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/App.tsx): react app · Thin operator UI for live planning, simulation, and chat. · `frontend/src/App.tsx`
- [Live planning](https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/Live.tsx): ui view · Intraday planning surface for revising the day’s production. · `frontend/src/Live.tsx`
- [Simulation](https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/Simulate.tsx): ui view · Reallocation and what-if planning interface. · `frontend/src/Simulate.tsx`
- [Ask ObradorIQ](https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/Tour.tsx): chat ui · Conversational interface for grounded assistant interactions. · `frontend/src/Tour.tsx`
- [API client](https://github.com/asiertxo09/obradoriq/blob/main/frontend/src/api.ts): frontend adapter · Frontend wrapper for calling backend endpoints. · `frontend/src/api.ts`

### Ops & data

Data fixtures, deployment, and CI support.

- [Seed data](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/seed.py): bootstrap script · Creates initial or demo data for local environments. · `backend/app/seed.py`
- [Synthetic data](https://github.com/asiertxo09/obradoriq/blob/main/backend/app/generate_data.py): demo generator · Generates synthetic bakery datasets for demos and tests. · `backend/app/generate_data.py`
- [CSV fixtures](https://github.com/asiertxo09/obradoriq/blob/main/data/sales.csv): sample data · Representative sales, waste, products, and sites inputs. · `data/sales.csv`
- [CI pipeline](https://github.com/asiertxo09/obradoriq/blob/main/.github/workflows/ci.yml): test automation · Runs the test suite in GitHub Actions and containerized checks. · `.github/workflows/ci.yml`

## Connections

- App entry → API layer: mounts · The app entrypoint exposes the API layer through FastAPI.
- API layer → Security: uses · Endpoints rely on shared auth and context dependencies.
- API layer → Domain service: calls · Route handlers delegate business operations to the service layer.
- Domain service → Entities: persists · Domain operations read and write persisted entities.
- Domain service → Schemas: shapes · Service-facing payloads are exposed through API schemas.
- Data hub → CSV fixtures: ingests · Structured bakery inputs originate from CSV-style source files.
- Data hub → Weather: enriches with · Daily context is augmented with weather features.
- Data hub → Holiday calendar: enriches with · Holiday signals are added to the daily dataset.
- Data hub → Entities: loads · Ingested data is stored in the domain model boundary.
- Recommender core → Forecasting: includes · Demand forecasting is a core substage of the engine.
- Recommender core → Production opt: includes · Production optimization is a core substage of the engine.
- Recommender core → Intraday: includes · Live-day planning adjusts recommendations during operations.
- Recommender core → Reallocation: includes · Cross-site redistribution is part of the optimization flow.
- Forecasting → Weather: uses · Forecasting consumes external signals as features.
- Forecasting → Holiday calendar: uses · Holiday context informs demand prediction.
- Production opt → Entities: reads · Optimization uses stored history and current state.
- Intraday → Entities: reads · Intraday adjustments use live operational data.
- Agent layer → Model router: routes through · The agent stack delegates provider selection and fallback.
- Agent layer → Tools: uses · Agents invoke grounded tools for data and recommendation actions.
- Tools → Recommender core: calls · Tools wrap deterministic recommendation functions.
- Agent layer → Trust layer: checked by · LLM outputs are validated against grounded values.
- Trust layer → Recommender core: verifies against · The guardrail compares phrasing to computed recommendations.
- Dashboard → API client: calls · UI pages use a shared backend API adapter.
- Dashboard → Live planning: contains · The dashboard includes intraday planning views.
- Dashboard → Simulation: contains · The dashboard includes simulation and reallocation views.
- Dashboard → Ask ObradorIQ: contains · The dashboard includes the conversational assistant view.
- API client → API layer: requests · Frontend interactions are served through backend endpoints.
- Seed data → Entities: populates · Seed scripts initialize the database with baseline entities.
- Synthetic data → CSV fixtures: creates · Synthetic generation produces demo-ready source data.
- CI pipeline → App entry: tests · Continuous integration exercises the backend service entrypoint.
- CI pipeline → Dashboard: tests · CI also covers the UI build and frontend behavior.
