Code Anomaly Engine is a polyglot, event-driven microservice architecture designed to intercept GitHub push events in real-time, extract code diffs, and classify them for security vulnerabilities using a fine-tuned CodeBERT transformer model.
It is engineered to handle high-throughput development environments, providing sub-millisecond inference latency and real-time WebSocket updates to a centralized monitoring dashboard.
The system leverages an event-driven design decoupled via Apache Kafka, allowing components to scale independently.
graph TD
GH[GitHub Webhook] -->|JSON Push Event| IS[Ingestion Service <br/><i>Go</i>]
IS -->|Parse & Chunk| IS2[Code Snippets]
IS2 -->|Produce| K[Apache Kafka <br/><i>KRaft Mode</i>]
K -->|Consume| GW[Gateway Service <br/><i>Go</i>]
GW -->|gRPC Stream| IE[Inference Engine <br/><i>Rust / ONNX</i>]
IE -->|Predict Vulnerability| GW
GW -->|WebSocket Broadcast| UI[Real-time Dashboard <br/><i>React / TS</i>]
- Ingestion Service (Go): Receives GitHub webhook payloads, parses git diffs to extract added/modified code chunks, and publishes structured messages to Kafka.
- Kafka Event Bus: Decouples ingestion from inference, absorbing sudden traffic spikes during CI/CD surges.
- Gateway Service (Go): Consumes Kafka messages, acts as a gRPC client to request predictions from the ML backend, and broadcasts results to connected UI clients via WebSockets.
- Inference Engine (Rust): A high-performance gRPC server utilizing
ort(ONNX Runtime) for ultra-fast, sub-50ms ML inference on a CodeBERT sequence classification model. - Dashboard (React/TypeScript): Connects to the Gateway via WebSockets to provide a live, real-time feed of code anomalies with syntax-highlighted diffs and latency metrics.
- ML Pipeline (Python): Downloads the Devign vulnerability dataset, fine-tunes Microsoft's
CodeBERT, and exports the computational graph to an ONNX format with dynamic sequence lengths.
The entire system is containerized. To spin up the Kafka broker, microservices, and dashboard:
docker-compose up --build -d| Service | Port | Description |
|---|---|---|
| Dashboard | http://localhost:5173 |
Real-time React frontend |
| Ingestion | http://localhost:8080/webhook |
GitHub webhook receiver |
| Gateway | http://localhost:8081/ws |
WebSocket endpoint for UI |
| Inference | grpc://localhost:50051 |
Rust ONNX gRPC Backend |
| Kafka | localhost:9092 |
Internal event broker |
The anomaly detection model is based on Microsoft's CodeBERT fine-tuned on the Devign Dataset (27k vulnerable and clean C/C++ functions).
To recreate the model locally:
make train # Downloads dataset, tokenizes, and fine-tunes CodeBERT
make export # Exports the PyTorch model to ONNX with dynamic axes- Go (1.22): Used for IO-bound microservices (Ingestion, Gateway). Leverages goroutines for concurrent webhook processing and Kafka consumption.
- Rust (1.77): Used for CPU-bound ML inference.
tonicfor async gRPC andortfor highly optimized C++ backend ONNX inference. - Kafka (KRaft): Used without Zookeeper for simplified infrastructure. Provides fault tolerance and buffering.
- gRPC / Protobuf: Enables strict-schema, low-overhead binary communication between the Go Gateway and Rust Inference Engine.
- React + Recharts: Provides dynamic, real-time metrics (p99 latency) and live UI updates without polling.