# ayush488-glitch/ai-pr-review-agent architecture

> Architecture diagram of the GitHub repository ayush488-glitch/ai-pr-review-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ayush488-glitch/ai-pr-review-agent
- Repository: https://github.com/ayush488-glitch/ai-pr-review-agent

## Overview

#### Purpose and entry points
AI PR Review Agent is a modular monolith that receives GitHub pull-request webhooks, runs parallel AI reviews, persists review/audit/economic data, posts results to GitHub, and exposes a dashboard plus human-approval workflow. Backend HTTP startup is `backend/main.py`; webhook ingress is `backend/webhook_receiver/router.py`; the asynchronous worker is `backend/job_queue/arq_worker.py`. The operator UI is a Next.js application rooted at `frontend/src/app/page.tsx`.

#### Ingress, validation, and asynchronous boundary
GitHub webhook payloads are parsed and validated before becoming review work. The ingress boundary includes webhook parsing/validation and API-facing queue dispatch: `backend/webhook_receiver/parser.py`, `backend/webhook_receiver/validator.py`, and `backend/api/queue.py`. Redis plus ARQ separates request acknowledgement from potentially long-running LLM orchestration; Docker Compose provisions API, worker, and Redis (`docker-compose.yml`, `backend/job_queue/arq_worker.py`).

#### Review workflow orchestration
A queued review is executed by a LangGraph-oriented orchestration layer that owns state, fan-out/fan-in, checkpoints, aggregation, and downstream routing/posting. Its central graph and node definitions are `backend/orchestrator/graph.py`, `backend/orchestrator/nodes.py`, and `backend/orchestrator/state.py`; an alternate/core workflow abstraction exists in `backend/core/workflow_engine.py`. This is the control-plane boundary between durable job execution and agent/model/tool calls.

#### Specialist agents and reasoning layer
Four domain agents—security, quality, tests, and documentation—share contracts/base behavior while producing structured findings for aggregation. Core paths are `backend/agents/base_agent.py`, `backend/agents/contracts.py`, and representative implementations such as `backend/agents/security_agent.py` and `backend/agents/quality_agent.py`. Prompt versions are externalized under `backend/prompts/templates/`, while LLM selection and invocation are mediated by `backend/tools/model_router.py` and `backend/tools/llm_client.py`; OpenAI GPT-4o is the stated model service.

#### Context, tools, and execution safety
Agents enrich PR diffs with semantic codebase context and may use registered, capability-scoped tools. Memory access is centered on Tiger/Timescale-compatible clients and retrieval: `backend/memory/tiger_client.py`, `backend/memory/context_retriever.py`, and `backend/memory/embedder.py`. The tree also retains `backend/memory/qdrant_client.py`, so migration/compatibility boundaries should be verified rather than assuming Qdrant is inactive. Tool execution is bounded by `backend/tools/tool_registry.py`, `backend/tools/capability_scope.py`, and Docker sandboxing in `backend/tools/sandbox.py`.

#### Persistence, observability, and economics
Tiger Cloud/TimescaleDB is the intended consolidated data layer: structured records, vector-backed context, and time-series agent events. Database access and schema models reside in `backend/database/postgres.py`, `backend/database/repository.py`, and `backend/database/models.py`; provisioning DDL is `scripts/migrations/2026-06-tiger-init.sql`. Observability records events, audits, and traces through `backend/observability/events.py`, `backend/observability/audit.py`, and `backend/observability/tracing.py`. Cost budgets and routing decisions consume this telemetry via `backend/economics/budget.py`, `backend/economics/cost_repository.py`, and `backend/economics/routing_advisor.py`.

#### Human governance and user-facing APIs
Low-confidence or disputed findings move to a human-in-the-loop queue for approval, escalation, and feedback, implemented by `backend/hitl/queue.py`, `backend/hitl/escalation.py`, and `backend/hitl/feedback.py`. REST surfaces are separated into review, HITL, and economics routers: `backend/api/reviews.py`, `backend/api/hitl_router.py`, and `backend/api/economics_router.py`. The frontend consumes these APIs for review detail, HITL actions, and economics views (`frontend/src/app/reviews/[id]/page.tsx`, `frontend/src/app/hitl/page.tsx`, `frontend/src/app/economics/page.tsx`).

#### Reliability, security, deployment
The service applies idempotency, retry, timeout, and circuit-breaker controls around external and asynchronous work (`backend/reliability/idempotency.py`, `backend/reliability/retry.py`, `backend/reliability/circuit_breaker.py`). Security boundaries include RBAC, injection protection, and data masking in `backend/security/rbac.py`, `backend/security/injection_guard.py`, and `backend/security/masking.py`. Deployment targets include containerized local/runtime deployment and Railway configuration (`Dockerfile`, `Procfile`, `railway.toml`), with GitHub, Redis, Tiger Cloud, OpenAI, and Docker as material external dependencies.

Main entry points: [`backend/orchestrator/graph.py`](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/orchestrator/graph.py) (LangGraph orchestration), [`backend/agents/base_agent.py`](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/agents/base_agent.py) (Specialist review agents), [`backend/api/reviews.py`](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/api/reviews.py) (Review, HITL & economics APIs), [`backend/webhook_receiver/router.py`](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/webhook_receiver/router.py) (Webhook validation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingress["Ingress &amp; API"]
  node_main["Backend HTTP service<br/>FastAPI startup<br/>[main.py]"]
  node_webhook["Webhook validation<br/>webhook ingress<br/>[router.py]"]
  node_validation["Payload parser &amp; validator<br/>validation layer<br/>[parser.py]"]
  node_api["Review, HITL &amp; economics APIs<br/>REST routers<br/>[reviews.py]"]
end

subgraph group_workflow["Review Execution"]
  node_redis["Redis + ARQ<br/>job queue<br/>[arq_worker.py]"]
  node_worker["Review worker<br/>ARQ worker<br/>[arq_worker.py]"]
  node_orchestrator{{"LangGraph orchestration<br/>workflow control plane<br/>[graph.py]"}}
  node_workflow_nodes["Workflow nodes &amp; state<br/>orchestration internals<br/>[nodes.py]"]
end

subgraph group_intelligence["Agents &amp; Context"]
  node_agents["Specialist review agents<br/>agent layer<br/>[base_agent.py]"]
  node_prompts["Versioned prompts<br/>prompt templates"]
  node_llm["LLM router &amp; client<br/>model gateway<br/>[model_router.py]"]
  node_context["Semantic context retrieval<br/>memory retrieval"]
  node_tools["Scoped tool sandbox<br/>safe tool execution<br/>[tool_registry.py]"]
end

subgraph group_governance["Data &amp; Governance"]
  node_database[("Tiger Cloud / TimescaleDB<br/>consolidated data store<br/>[postgres.py]")]
  node_observability["Events, audit &amp; tracing<br/>telemetry layer<br/>[events.py]"]
  node_economics["Budget &amp; routing advisor<br/>economic controls<br/>[budget.py]"]
  node_hitl["Human approval workflow<br/>HITL queue<br/>[queue.py]"]
end

subgraph group_ui["Operator UI"]
  node_frontend["Next.js dashboard<br/>operator UI<br/>[page.tsx]"]
end

node_github{{"GitHub<br/>external service"}}
node_openai{{"OpenAI GPT-4o<br/>external model service"}}

node_github -->|"PR webhook"| node_webhook
node_main -->|"mounts"| node_webhook
node_webhook -->|"parses and validates"| node_validation
node_validation -->|"dispatches review job"| node_redis
node_redis -->|"executes asynchronously"| node_worker
node_worker -->|"runs review graph"| node_orchestrator
node_orchestrator -->|"state and fan-out"| node_workflow_nodes
node_orchestrator -->|"parallel specialist reviews"| node_agents
node_agents -->|"uses"| node_prompts
node_agents -->|"requests inference"| node_llm
node_llm -->|"invokes"| node_openai
node_agents -->|"retrieves code context"| node_context
node_context -->|"vector and record access"| node_database
node_agents -->|"capability-scoped calls"| node_tools
node_orchestrator -->|"persists review results"| node_database
node_orchestrator -->|"emits events and traces"| node_observability
node_observability -->|"cost telemetry"| node_economics
node_economics -.->|"routing and budget decisions"| node_llm
node_orchestrator -->|"routes uncertain findings"| node_hitl
node_orchestrator -->|"posts review results"| node_github
node_frontend -->|"dashboard requests"| node_api
node_api -->|"reads review data"| node_database
node_api -->|"approval actions"| node_hitl
node_api -->|"economics views"| node_economics

click node_main "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/main.py"
click node_webhook "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/webhook_receiver/router.py"
click node_validation "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/webhook_receiver/parser.py"
click node_api "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/api/reviews.py"
click node_redis "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/job_queue/arq_worker.py"
click node_worker "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/job_queue/arq_worker.py"
click node_orchestrator "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/orchestrator/graph.py"
click node_workflow_nodes "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/orchestrator/nodes.py"
click node_agents "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/agents/base_agent.py"
click node_llm "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/tools/model_router.py"
click node_context "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/memory/context_retriever.py"
click node_tools "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/tools/tool_registry.py"
click node_database "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/database/postgres.py"
click node_observability "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/observability/events.py"
click node_economics "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/economics/budget.py"
click node_hitl "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/hitl/queue.py"
click node_frontend "https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/frontend/src/app/page.tsx"

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_webhook,node_validation,node_api toneBlue
class node_redis,node_worker,node_orchestrator,node_workflow_nodes toneAmber
class node_agents,node_prompts,node_llm,node_context,node_tools toneMint
class node_database,node_observability,node_economics,node_hitl toneRose
class node_frontend toneIndigo
class node_github,node_openai toneNeutral
```

## Components

### Ingress & API

- [Backend HTTP service](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/main.py): FastAPI startup · `backend/main.py`
- [Webhook validation](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/webhook_receiver/router.py): webhook ingress · `backend/webhook_receiver/router.py`
- [Payload parser & validator](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/webhook_receiver/parser.py): validation layer · `backend/webhook_receiver/parser.py`
- [Review, HITL & economics APIs](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/api/reviews.py): REST routers · `backend/api/reviews.py`

### Review Execution

- [Redis + ARQ](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/job_queue/arq_worker.py): job queue · Acknowledges webhooks before durable asynchronous work. · `backend/job_queue/arq_worker.py`
- [Review worker](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/job_queue/arq_worker.py): ARQ worker · `backend/job_queue/arq_worker.py`
- [LangGraph orchestration](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/orchestrator/graph.py): workflow control plane · Owns review state, fan-out/fan-in, checkpoints, and result routing. · `backend/orchestrator/graph.py`
- [Workflow nodes & state](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/orchestrator/nodes.py): orchestration internals · `backend/orchestrator/nodes.py`

### Agents & Context

- [Specialist review agents](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/agents/base_agent.py): agent layer · Security, quality, tests, and documentation findings share common contracts. · `backend/agents/base_agent.py`
- **Versioned prompts**: prompt templates
- [LLM router & client](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/tools/model_router.py): model gateway · Selects and invokes OpenAI GPT-4o. · `backend/tools/model_router.py`
- [Semantic context retrieval](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/memory/context_retriever.py): memory retrieval · `backend/memory/context_retriever.py`
- [Scoped tool sandbox](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/tools/tool_registry.py): safe tool execution · `backend/tools/tool_registry.py`

### Data & Governance

- [Tiger Cloud / TimescaleDB](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/database/postgres.py): consolidated data store · Stores records, vectors, and time-series agent events. · `backend/database/postgres.py`
- [Events, audit & tracing](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/observability/events.py): telemetry layer · `backend/observability/events.py`
- [Budget & routing advisor](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/economics/budget.py): economic controls · `backend/economics/budget.py`
- [Human approval workflow](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/backend/hitl/queue.py): HITL queue · Approves, escalates, and feeds back on uncertain findings. · `backend/hitl/queue.py`

### Operator UI

- [Next.js dashboard](https://github.com/ayush488-glitch/ai-pr-review-agent/blob/main/frontend/src/app/page.tsx): operator UI · `frontend/src/app/page.tsx`

### Other components

- **GitHub**: external service · Sends PR webhooks and receives review results.
- **OpenAI GPT-4o**: external model service

## Connections

- GitHub → Webhook validation: PR webhook
- Backend HTTP service → Webhook validation: mounts
- Webhook validation → Payload parser & validator: parses and validates
- Payload parser & validator → Redis + ARQ: dispatches review job
- Redis + ARQ → Review worker: executes asynchronously
- Review worker → LangGraph orchestration: runs review graph
- LangGraph orchestration → Workflow nodes & state: state and fan-out
- LangGraph orchestration → Specialist review agents: parallel specialist reviews
- Specialist review agents → Versioned prompts: uses
- Specialist review agents → LLM router & client: requests inference
- LLM router & client → OpenAI GPT-4o: invokes
- Specialist review agents → Semantic context retrieval: retrieves code context
- Semantic context retrieval → Tiger Cloud / TimescaleDB: vector and record access
- Specialist review agents → Scoped tool sandbox: capability-scoped calls
- LangGraph orchestration → Tiger Cloud / TimescaleDB: persists review results
- LangGraph orchestration → Events, audit & tracing: emits events and traces
- Events, audit & tracing → Budget & routing advisor: cost telemetry
- Budget & routing advisor → LLM router & client: routing and budget decisions
- LangGraph orchestration → Human approval workflow: routes uncertain findings
- LangGraph orchestration → GitHub: posts review results
- Next.js dashboard → Review, HITL & economics APIs: dashboard requests
- Review, HITL & economics APIs → Tiger Cloud / TimescaleDB: reads review data
- Review, HITL & economics APIs → Human approval workflow: approval actions
- Review, HITL & economics APIs → Budget & routing advisor: economics views
