RAG

RCA-RAG Code Intelligence

Hệ thống SaaS nội bộ, air‑gapped, phân tích diff/PR của Java (chủ đạo) + Python + PHP; tự động tag owner, đề xuất fix, RCA, ước lượng tác động & thời gian fix; lưu 90 ngày raw diff; RAG cấp chức năng “Hỏi dự án”.

Cấu trúc thư mục

rca-rag/
├─ apps/
│  ├─ gateway/              # FastAPI app (webhooks, REST API)
│  │  ├─ main.py           # FastAPI app, routers
│  │  ├─ webhook/
│  │  │  └─ github.py      # GitHub webhook handler
│  │  └─ api/              # REST endpoints (health, metrics, admin)
│  ├─ ingestion/           # Ingestion worker
│  │  ├─ worker.py         # Main worker loop
│  │  ├─ processors/       # Event processors
│  │  ├─ storage/          # Storage operations
│  │  └─ retention/       # Retention job worker
│  ├─ notifier/            # Notification service
│  │  ├─ client.py         # Google Chat client
│  │  └─ formatters.py     # Message formatting
│  ├─ shared/              # Shared code
│  │  ├─ config.py         # Pydantic Settings
│  │  ├─ db/               # Database setup, models
│  │  ├─ storage/          # S3/MinIO abstraction
│  │  ├─ mq/               # Message queue abstraction
│  │  └─ utils/            # Utilities
├─ infra/
│  ├─ migrations/          # Alembic migrations
│  └─ sql/                 # Raw SQL scripts
├─ deployments/
│  ├─ docker-compose.dev.yml
│  └─ docker/
│     ├─ Dockerfile.api
│     └─ Dockerfile.worker
├─ configs/
│  ├─ app.example.env      # Environment template
│  ├─ logging.yaml
│  ├─ ruff.toml
│  └─ mypy.ini
├─ tests/                  # Test suite
│  ├─ unit/
│  ├─ integration/
│  └─ fixtures/
├─ pyproject.toml
├─ requirements.txt
└─ README.md

API Endpoints

Health & Metrics

Webhooks

Admin

RAG Query

Setup Guide

New to the project? See GETTING_STARTED.md for a comprehensive step-by-step guide with troubleshooting tips.

Quick Start

  1. Clone repository and install dependencies:
git clone <repo-url>
cd rca-rag
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
pip install -e .
  1. Configure environment:
cp configs/app.example.env configs/.env
# Edit configs/.env with your settings
  1. Start infrastructure:
docker-compose -f deployments/docker-compose.dev.yml up -d
  1. Run database migrations:
alembic -c infra/migrations/alembic.ini upgrade head
  1. Start services:
# Start API gateway
uvicorn apps.gateway.main:app --host 0.0.0.0 --port 8080

# Start ingestion worker (in separate terminal)
python -m apps.ingestion.worker

# Start analysis worker (in separate terminal)
python -m apps.analysis

# Start indexer worker in message queue mode (in separate terminal, optional)
python -m apps.indexer --mq

# Or manually index a repository
python -m apps.indexer <repo_id>

For detailed setup instructions, troubleshooting, and configuration options, see GETTING_STARTED.md.

Docker Compose Services

Configuration

Environment Variables

Key configuration options in configs/.env:

See configs/app.example.env for all available options.

Development

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=apps --cov-report=html

# Run specific test file
pytest tests/unit/test_event_normalization.py

Linting & Type Checking

# Lint
ruff check .

# Type check
mypy apps

# Format code
ruff format .

Database Migrations

# Create new migration
alembic -c infra/migrations/alembic.ini revision --autogenerate -m "description"

# Apply migrations
alembic -c infra/migrations/alembic.ini upgrade head

# Rollback
alembic -c infra/migrations/alembic.ini downgrade -1

Services & Modules

Gateway

FastAPI application handling:

Ingestion Worker

Background worker processing:

Analysis Worker

Background worker processing:

Indexer Worker

Background worker for RAG indexing:

Run indexer worker:

# Message queue mode (automatic)
python -m apps.indexer --mq

# Manual mode (index specific repository)
python -m apps.indexer <repo_id>

Notifier

Google Chat integration:

Retention Job

Scheduled job for:

Message Queue Support

The system supports multiple message queue backends:

Configure via MQ_TYPE environment variable.

Storage Support

Configure via STORAGE_TYPE environment variable.

RAG Query API

The RAG (Retrieval-Augmented Generation) system allows querying the codebase using natural language.

Example Query

curl -X POST http://localhost:8080/rag/query \
  -H "Content-Type: application/json" \
  -d '{
    "question": "How does user authentication work?",
    "repo": "org/repo",
    "top_k": 5,
    "files": ["app/auth/**"]
  }'

Response Format

{
  "answer": "Based on the codebase: ...",
  "citations": [
    {
      "path": "app/auth/AuthService.java",
      "commit": "abc123...",
      "lines": [10, 20],
      "score": 0.95
    }
  ],
  "chunks": [...]
}

The system uses hybrid retrieval (BM25 + vector search) to find relevant code snippets and provides citations for traceability.

Observability

Metrics

Prometheus metrics available at /metrics:

Logging

Structured logging with Loguru:

Configuration in configs/logging.yaml.

Retention Policy

Run retention job manually:

# Dry run
python -m apps.ingestion.retention.worker --dry-run

# Actual cleanup
python -m apps.ingestion.retention.worker

Security

Air-gapped Environment

GitHub Webhook Security

Roadmap

Phase 1 (Current) - Webhooks → Storage ✅

Phase 2 (Current) - Analysis & RAG ✅

Phase 3 (Next) - RCA & TTF

License

Internal use only.