# ignaciobarroso/datasecurity-melichallenge architecture

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

- Interactive diagram: https://gitdiagram.com/ignaciobarroso/datasecurity-melichallenge
- Repository: https://github.com/ignaciobarroso/datasecurity-melichallenge

## Overview

1. This repository is a Python 3.11 system for autonomous threat detection and reporting. The architecture is split into four major boundaries: the FastAPI service under `api/`, the core analysis/RAG code under `src/`, the static frontend under `frontend/`, and the operational stack defined by Docker Compose plus supporting services.

2. The main runtime entrypoints are `main.py` at the repo root for CLI usage and `api/main.py` for the HTTP API. The API is the primary integration surface for the rest of the system; it exposes analysis, RAG, health, and UI-serving endpoints, while the CLI wraps the same backend capabilities for local execution.

3. The API layer is thin and service-oriented. `api/routers/analysis.py` handles the multi-agent analysis workflow, `api/routers/rag.py` handles direct question-answering and RAG debugging, and `api/app.py` wires the FastAPI application together. `api/services/crew_service.py` and `api/services/rag_service.py` adapt the core `src/` modules into request/response behavior, session handling, and mode selection.

4. The central analysis pipeline lives in `src/mcp_crews.py` and `src/agents.py`. The repository implements a 3-agent sequential flow: Analyzers classify the incident, a second stage maps findings to MITRE ATT&CK, and a final stage generates a prioritized report. `src/models.py` defines the structured outputs and validation used to keep the report in a stable JSON schema.

5. There are two execution modes for the analyzer, and this is an important architectural boundary. In `heavy` mode, the system uses a CrewAI-style multi-agent flow with more retrieval and more tracing. In `turbo` mode, it uses a simpler single-pass pipeline optimized for speed, but both modes must produce the same final report shape and both can be selected per request via query parameter.

6. RAG is a separate subsystem under `src/rag_system/` and `src/tools/dbir_rag_tool.py`. The knowledge base is the DBIR 2025 PDF in `data/input/`, ingested through `src/rag_system/ingest.py` using hierarchical chunking. Retrieval uses a `ParentDocumentRetriever` pattern backed by Chroma as the vector store and Redis as the persistent docstore when available.

7. The retrieval flow has two layers of relevance control. First, embeddings and the vector store retrieve candidate passages from the DBIR corpus; then reranking is applied either with Cohere if `COHERE_API_KEY` is configured or with a local MMR-style selection fallback. This affects both answer quality and the context that is passed to the LLM.

8. MITRE ATT&CK mapping is isolated behind tool adapters. `src/tools/mitre_tool.py` provides local MITRE access, while `src/tools/mcp_external.py` connects to an external MCP server. In the deployed architecture, `docker/` and `docker-compose.yml` run that MCP server in its own container, so the analyzer can prefer remote ATT&CK tools and fall back to local `attackcti`-based logic when needed.

9. LLM and model-provider concerns are centralized in `src/llm_provider.py`, with runtime configuration in `src/config.py`. The system can use OpenAI by default or Ollama as an alternative provider, and the chosen provider affects embeddings, retrieval augmentation, and final report generation. `src/cache.py` supports reuse in the faster execution path.

10. Tracing and observability are first-class parts of the design. `src/trace.py` and `src/logging_config.py` coordinate per-session logging, producing session-scoped logs and structured JSON traces under `logs/`. That trace boundary is important because the system is meant to audit both task inputs/outputs and tool calls across the multi-agent workflow.

11. The frontend is intentionally minimal and static. `frontend/index.html` plus its CSS/JS assets are served by the API at `/ui`, and it acts as a lightweight operator console rather than a separate application tier. The frontend mainly triggers the API endpoints and exposes the heavy/turbo mode switch.

12. The data and artifact directories reflect the system’s operational flow. `data/input/` contains the source DBIR PDF, `data/custom_inputs/` and `reports/` hold sample inputs and outputs, and `data/examples/` provides an example report artifact. This makes the repo suitable for offline reproducibility and for regression testing of report structure.

13. Docker is used to separate the app from its dependencies. The stack includes the API container, ChromaDB REST, Redis, and the MITRE MCP service, with an optional Ollama path. The health endpoint is designed to verify these boundaries individually, so architecture diagrams should show service-level dependencies rather than a single monolithic process.

14. Quality and validation tooling are part of the architecture, not just development support. `tests/` includes API, UI, logging, tool, and end-to-end checks; `evaluation/validate_rag.py` measures RAG faithfulness and context precision against the real pipeline. That means the repo’s core contracts are the API schema, the final report schema, and the retrieval behavior, all of which are testable and benchmarked.

Main entry points: [`src/mcp_crews.py`](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/mcp_crews.py) (Crew pipeline), [`api/app.py`](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/app.py) (App wiring), [`src/tools/dbir_rag_tool.py`](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/tools/dbir_rag_tool.py) (DBIR tool), [`src/turbo_pipeline.py`](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/turbo_pipeline.py) (Turbo pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API service"]
  node_api_main(("API entry<br/>entrypoint<br/>[main.py]"))
  node_api_app["App wiring<br/>fastapi app<br/>[app.py]"]
  node_analysis_router["Analysis API<br/>router<br/>[analysis.py]"]
  node_rag_router["RAG API<br/>router<br/>[rag.py]"]
  node_crew_service["Crew service<br/>adapter<br/>[crew_service.py]"]
  node_rag_service["RAG service<br/>adapter<br/>[rag_service.py]"]
end

subgraph group_core["Analysis core"]
  node_main_py(("CLI entry<br/>entrypoint<br/>[main.py]"))
  node_mcp_crews["Crew pipeline<br/>analysis flow<br/>[mcp_crews.py]"]
  node_agents["Agents<br/>analysis stages<br/>[agents.py]"]
  node_models["Report schema<br/>[models.py]"]
  node_turbo_pipeline["Turbo pipeline<br/>analysis flow<br/>[turbo_pipeline.py]"]
  node_llm_provider["LLM provider<br/>runtime<br/>[llm_provider.py]"]
  node_config["Runtime config<br/>settings<br/>[config.py]"]
  node_cache["Cache<br/>runtime support<br/>[cache.py]"]
  node_trace["Tracing<br/>observability<br/>[trace.py]"]
  node_logging_config["Logging<br/>observability<br/>[logging_config.py]"]
end

subgraph group_rag["RAG subsystem"]
  node_ingest["DBIR ingest<br/>ingestion<br/>[ingest.py]"]
  node_retriever_factory["Retriever<br/>retrieval"]
  node_dbir_tool["DBIR tool<br/>rag tool<br/>[dbir_rag_tool.py]"]
end

subgraph group_tools["Tool adapters"]
  node_mitre_tool["MITRE tool<br/>tool adapter<br/>[mitre_tool.py]"]
  node_mcp_external["MCP client<br/>tool adapter<br/>[mcp_external.py]"]
end

subgraph group_frontend["Frontend"]
  node_frontend_index["UI shell<br/>static frontend<br/>[index.html]"]
end

subgraph group_ops["Operations"]
  node_docker_compose["Compose stack<br/>deployment<br/>[docker-compose.yml]"]
  node_mitre_dockerfile["MCP image<br/>container build"]
end

subgraph group_data["Data and artifacts"]
  node_dbir_pdf["DBIR corpus<br/>input data"]
  node_example_report["Example report<br/>artifact"]
end

subgraph group_evaluation["Evaluation"]
  node_validate_rag["RAG validation<br/>evaluation<br/>[validate_rag.py]"]
end

node_main_py -->|"run analysis"| node_mcp_crews
node_main_py -->|"run RAG"| node_dbir_tool
node_api_main -->|"start app"| node_api_app
node_api_app -->|"mount"| node_analysis_router
node_api_app -->|"mount"| node_rag_router
node_api_app -->|"serve UI"| node_frontend_index
node_analysis_router -->|"delegate"| node_crew_service
node_rag_router -->|"delegate"| node_rag_service
node_crew_service -->|"analyze"| node_mcp_crews
node_crew_service -->|"fast path"| node_turbo_pipeline
node_mcp_crews -->|"orchestrate"| node_agents
node_mcp_crews -->|"shape output"| node_models
node_turbo_pipeline -->|"shape output"| node_models
node_mcp_crews -->|"generate"| node_llm_provider
node_turbo_pipeline -->|"generate"| node_llm_provider
node_mcp_crews -->|"trace"| node_trace
node_mcp_crews -->|"log"| node_logging_config
node_turbo_pipeline -->|"reuse"| node_cache
node_rag_service -->|"query"| node_dbir_tool
node_dbir_tool -->|"build retrieval"| node_retriever_factory
node_retriever_factory -->|"index corpus"| node_ingest
node_ingest -->|"source"| node_dbir_pdf
node_dbir_tool -->|"answer"| node_llm_provider
node_mcp_crews -->|"local ATT&CK"| node_mitre_tool
node_mcp_crews -->|"remote ATT&CK"| node_mcp_external
node_mcp_external -->|"run in stack"| node_docker_compose
node_mitre_dockerfile -->|"build image"| node_mcp_external
node_docker_compose -->|"deploy"| node_api_app
node_docker_compose -->|"serve UI"| node_frontend_index
node_docker_compose -->|"backing services"| node_retriever_factory
node_validate_rag -->|"measure"| node_dbir_tool
node_validate_rag -->|"compare"| node_example_report

click node_main_py "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/main.py"
click node_api_main "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/main.py"
click node_api_app "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/app.py"
click node_analysis_router "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/routers/analysis.py"
click node_rag_router "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/routers/rag.py"
click node_crew_service "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/services/crew_service.py"
click node_rag_service "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/services/rag_service.py"
click node_mcp_crews "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/mcp_crews.py"
click node_agents "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/agents.py"
click node_models "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/models.py"
click node_turbo_pipeline "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/turbo_pipeline.py"
click node_llm_provider "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/llm_provider.py"
click node_config "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/config.py"
click node_cache "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/cache.py"
click node_trace "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/trace.py"
click node_logging_config "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/logging_config.py"
click node_ingest "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/rag_system/ingest.py"
click node_retriever_factory "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/rag_system/retriever_factory.py"
click node_dbir_tool "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/tools/dbir_rag_tool.py"
click node_mitre_tool "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/tools/mitre_tool.py"
click node_mcp_external "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/tools/mcp_external.py"
click node_frontend_index "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/frontend/index.html"
click node_docker_compose "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/docker-compose.yml"
click node_mitre_dockerfile "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/docker/Dockerfile.mitre-mcp"
click node_dbir_pdf "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/data/input/2025-dbir-data-breach-investigations-report.pdf"
click node_example_report "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/data/examples/security_report_output.json"
click node_validate_rag "https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/evaluation/validate_rag.py"

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_api_main,node_api_app,node_analysis_router,node_rag_router,node_crew_service,node_rag_service,node_dbir_pdf,node_example_report toneBlue
class node_main_py,node_mcp_crews,node_agents,node_models,node_turbo_pipeline,node_llm_provider,node_config,node_cache,node_trace,node_logging_config,node_validate_rag toneAmber
class node_ingest,node_retriever_factory,node_dbir_tool toneMint
class node_mitre_tool,node_mcp_external toneRose
class node_frontend_index toneIndigo
class node_docker_compose,node_mitre_dockerfile toneTeal
```

## Components

### API service

FastAPI application, routers, and request-facing adapters.

- [API entry](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/main.py): entrypoint · HTTP server entrypoint. · `api/main.py`
- [App wiring](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/app.py): fastapi app · Creates and configures the FastAPI application. · `api/app.py`
- [Analysis API](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/routers/analysis.py): router · Exposes multi-agent analysis and mode selection endpoints. · `api/routers/analysis.py`
- [RAG API](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/routers/rag.py): router · Exposes retrieval, QA, and RAG debugging endpoints. · `api/routers/rag.py`
- [Crew service](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/services/crew_service.py): adapter · Bridges API requests to the analysis pipeline. · `api/services/crew_service.py`
- [RAG service](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/api/services/rag_service.py): adapter · Bridges API requests to retrieval and answer generation. · `api/services/rag_service.py`

### Analysis core

Incident analysis pipeline, report schema, configuration, and runtime plumbing.

- [CLI entry](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/main.py): entrypoint · Command-line entrypoint for local execution. · `main.py`
- [Crew pipeline](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/mcp_crews.py): analysis flow · Sequential multi-agent analysis and report generation flow. · `src/mcp_crews.py`
- [Agents](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/agents.py): analysis stages · Defines analyzer, MITRE mapping, and reporting roles. · `src/agents.py`
- [Report schema](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/models.py): schema · Structured outputs and validation for the final report. · `src/models.py`
- [Turbo pipeline](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/turbo_pipeline.py): analysis flow · Faster single-pass analysis path with the same output shape. · `src/turbo_pipeline.py`
- [LLM provider](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/llm_provider.py): runtime · Centralizes model provider selection and generation/embedding access. · `src/llm_provider.py`
- [Runtime config](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/config.py): settings · Loads environment-driven settings for providers, retrieval, and modes. · `src/config.py`
- [Cache](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/cache.py): runtime support · Supports reuse in the faster execution path. · `src/cache.py`
- [Tracing](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/trace.py): observability · Session-scoped tracing and structured execution records. · `src/trace.py`
- [Logging](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/logging_config.py): observability · Configures structured logging for sessions and artifacts. · `src/logging_config.py`

### RAG subsystem

DBIR ingestion, retrieval, and question-answering support.

- [DBIR ingest](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/rag_system/ingest.py): ingestion · Ingests and chunks the DBIR corpus for retrieval. · `src/rag_system/ingest.py`
- [Retriever](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/rag_system/retriever_factory.py): retrieval · Builds the parent-document retrieval stack and reranking path. · `src/rag_system/retriever_factory.py`
- [DBIR tool](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/tools/dbir_rag_tool.py): rag tool · Tool wrapper for DBIR-backed retrieval and QA. · `src/tools/dbir_rag_tool.py`

### Tool adapters

MITRE and external MCP integrations used by the analyzers.

- [MITRE tool](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/tools/mitre_tool.py): tool adapter · Local MITRE ATT&CK lookup and normalization logic. · `src/tools/mitre_tool.py`
- [MCP client](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/src/tools/mcp_external.py): tool adapter · Connects to the external MITRE MCP service. · `src/tools/mcp_external.py`

### Frontend

Static operator UI served by the API.

- [UI shell](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/frontend/index.html): static frontend · Minimal operator console served by the API. · `frontend/index.html`

### Operations

Dockerized runtime and supporting service definitions.

- [Compose stack](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/docker-compose.yml): deployment · Defines the API, vector store, Redis, and MCP service stack. · `docker-compose.yml`
- [MCP image](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/docker/Dockerfile.mitre-mcp): container build · Builds the container for the external MITRE MCP service. · `docker/Dockerfile.mitre-mcp`

### Data and artifacts

Source corpus, sample inputs, and example outputs.

- [DBIR corpus](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/data/input/2025-dbir-data-breach-investigations-report.pdf): input data · Source PDF used as the knowledge base. · `data/input/2025-dbir-data-breach-investigations-report.pdf`
- [Example report](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/data/examples/security_report_output.json): artifact · Sample structured output for regression and reference. · `data/examples/security_report_output.json`

### Evaluation

Benchmarking and validation scripts for the pipeline.

- [RAG validation](https://github.com/ignaciobarroso/datasecurity-melichallenge/blob/master/evaluation/validate_rag.py): evaluation · Measures faithfulness and context precision against the pipeline. · `evaluation/validate_rag.py`

## Connections

- CLI entry → Crew pipeline: run analysis · CLI invokes the core analysis pipeline.
- CLI entry → DBIR tool: run RAG · CLI can exercise retrieval-backed workflows.
- API entry → App wiring: start app · HTTP entrypoint constructs the FastAPI app.
- App wiring → Analysis API: mount · Registers analysis routes.
- App wiring → RAG API: mount · Registers retrieval routes.
- App wiring → UI shell: serve UI · Serves the static operator interface.
- Analysis API → Crew service: delegate · Routes analysis requests to the service layer.
- RAG API → RAG service: delegate · Routes RAG requests to the service layer.
- Crew service → Crew pipeline: analyze · Service executes the multi-agent or turbo analysis path.
- Crew service → Turbo pipeline: fast path · Service selects the lighter pipeline for turbo mode.
- Crew pipeline → Agents: orchestrate · Pipeline coordinates the agent stages.
- Crew pipeline → Report schema: shape output · Final report is validated against the structured schema.
- Turbo pipeline → Report schema: shape output · Turbo mode emits the same report contract.
- Crew pipeline → LLM provider: generate · Uses provider abstraction for reasoning and synthesis.
- Turbo pipeline → LLM provider: generate · Uses provider abstraction for lightweight generation.
- Crew pipeline → Tracing: trace · Records workflow events and tool calls.
- Crew pipeline → Logging: log · Emits structured session logs.
- Turbo pipeline → Cache: reuse · Caches repeated work in the fast path.
- RAG service → DBIR tool: query · Service queries the DBIR retrieval tool.
- DBIR tool → Retriever: build retrieval · Tool uses the retriever stack for candidate context and reranking.
- Retriever → DBIR ingest: index corpus · Retrieval stack depends on ingested DBIR chunks.
- DBIR ingest → DBIR corpus: source · Ingestion starts from the DBIR PDF corpus.
- DBIR tool → LLM provider: answer · Retrieval results are fed to the model provider for responses.
- Crew pipeline → MITRE tool: local ATT&CK · Analyzer can use local MITRE mapping logic.
- Crew pipeline → MCP client: remote ATT&CK · Analyzer can prefer the external MCP-backed MITRE service.
- MCP client → Compose stack: run in stack · External MCP service is deployed as part of the compose stack.
- MCP image → MCP client: build image · Container build produces the MCP service image.
- Compose stack → App wiring: deploy · Compose stack includes the API service.
- Compose stack → UI shell: serve UI · API container serves the static frontend.
- Compose stack → Retriever: backing services · Retrieval depends on vector and document-store services.
- RAG validation → DBIR tool: measure · Validation script evaluates the live retrieval pipeline.
- RAG validation → Example report: compare · Validation can compare against reference artifacts.
