# nahom-zewdu/kms architecture

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

- Interactive diagram: https://gitdiagram.com/nahom-zewdu/kms
- Repository: https://github.com/nahom-zewdu/kms

## Overview

1. **Overall shape**
   KMS is a two-process system: a **Go backend** handles all inbound webhooks and Slack interactions, while a **Python NLP worker** performs extraction, graph building, and natural-language answering. The main architectural boundary is **event ingestion/orchestration in Go** versus **semantic processing and database writes/reads in Python**.

2. **External entry points**
   The system receives events from **Slack** and **GitHub**. Slack is both an ingestion source and the user-facing query surface via the `@KMS` bot. GitHub events appear to cover webhook-driven knowledge capture from commits/PRs. These sources enter through the Go API under `api/handlers/`.

3. **Go backend responsibilities**
   The Go app is the edge/orchestration layer. Its `handlers/` package exposes webhook routes, while `services/` coordinates source-specific logic for Slack, GitHub, verification, bot behavior, and metrics. `domain/` holds shared contracts and structs, and `repository/` contains adapters for Redis Streams and Supabase.

4. **Queue-based handoff**
   After validation, the Go service pushes work into **Redis Streams**. The README names three logical streams: `slack_jobs`, `github_jobs`, and `query_jobs`. This decouples webhook latency from downstream processing and gives the system retry/consumer-group semantics for resilience and exactly-once-style processing.

5. **Python worker as the knowledge engine**
   The Python side consumes Redis stream jobs through `nlp/worker/consumer.py` and `processor.py`. This worker is the main computational layer: it transforms raw Slack/GitHub payloads into structured knowledge and also handles user questions. The worker is separate from the Go API, which keeps heavy NLP/LLM work out of the request path.

6. **Ingestion pipeline**
   The ingestion path in `nlp/worker/ingestion.py` uses the `engine/` package for deterministic extraction:
   - `ner.py` for entity recognition
   - `re.py` for relation extraction
   - `llm.py` and `prompt.py` for structured LLM calls
   - `schema.py` for strict output models  
   The design goal is grounded extraction: entities and edges are tied to real UUIDs before being stored.

7. **Knowledge graph persistence**
   Supabase/Postgres is the system of record. The schema files show tables/functions for:
   - `entities`, `edges`, `raw_data`, `events`
   - embedding support
   - graph traversal RPCs
   - vector search RPCs  
   This indicates the database is not just storage; it also exposes graph/query primitives used by the Python layer.

8. **Query and answer path**
   User questions enter through Slack, are routed by the Go backend into the query job stream, and are handled in Python by the query subsystem (`nlp/query_handler.py` plus `query_engine/`). The query engine combines retrieval, analysis, synthesis, and caching, then uses an LLM to produce a natural-language answer grounded in the stored graph and vector data.

9. **Slack response loop**
   The Slack bot is the user-facing feedback channel. Go owns verification, message intake, and bot command plumbing (`slack_verifier`, `slack_bot`, `slack` services), while Python produces the answer payload. The final answer is posted back into the original Slack thread, closing the asynchronous loop.

10. **Data integrity and operational concerns**
   The repo emphasizes reliability features: **exactly-once processing**, **safe upserts with fallback**, **referential integrity**, and an audit trail via raw events. The Go `metrics` service and Python logging/utilities suggest the system is designed for observable ingestion and query processing rather than a stateless request/response app.

11. **Supporting subsystems**
   `nlp/utils/` provides Redis, Supabase, DB helper, and logging wrappers; `nlp/migrations/` includes an embedding backfill path. The `query_engine/` package is more modular than the older `query_handler.py`, which suggests the query stack is evolving toward a cleaner retrieval/analysis/synthesis pipeline.

12. **What to draw in an architecture diagram**
   The key boxes are: **Slack/GitHub sources**, **Go API edge layer**, **Redis Streams**, **Python NLP worker**, **Supabase knowledge graph**, and **Slack bot response path**. The most important arrows are:
   - inbound webhooks → Go
   - Go → Redis Streams
   - Redis Streams → Python worker
   - Python worker → Supabase
   - query path → retrieval/vector/graph/LLM → Slack reply  
   This captures the repo’s main separation of concerns and the asynchronous knowledge-oracle workflow.

Main entry points: [`api/services/slack.go`](https://github.com/nahom-zewdu/kms/blob/dev/api/services/slack.go) (Slack service), [`api/handlers/routes.go`](https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/routes.go) (Routes), [`api/handlers/slack.go`](https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/slack.go) (Slack handler), [`api/services/github.go`](https://github.com/nahom-zewdu/kms/blob/dev/api/services/github.go) (GitHub service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_sources["External sources"]
  node_slack(("Slack<br/>external source"))
  node_github(("GitHub<br/>external source"))
end

subgraph group_group_go["Go edge"]
  node_go_main["API entry<br/>go service<br/>[main.go]"]
  node_routes["Routes<br/>go handlers<br/>[routes.go]"]
  node_slack_handler["Slack handler<br/>go handler<br/>[slack.go]"]
  node_github_handler["GitHub handler<br/>go handler<br/>[github.go]"]
  node_core_services["Core service<br/>go orchestration<br/>[core.go]"]
  node_slack_service["Slack service<br/>go service<br/>[slack.go]"]
  node_github_service["GitHub service<br/>go service<br/>[github.go]"]
  node_slack_bot["Slack bot<br/>go service<br/>[slack_bot.go]"]
  node_slack_verifier["Verifier<br/>go service<br/>[slack_verifier.go]"]
end

subgraph group_group_python["Python NLP"]
  node_worker_consumer["Job consumer<br/>python worker<br/>[consumer.py]"]
  node_ingestion["Ingestion<br/>python pipeline<br/>[ingestion.py]"]
  node_query_query["Query worker<br/>python pipeline<br/>[query.py]"]
  node_engine["Extraction engine<br/>python NLP"]
  node_query_engine["Query engine<br/>python retrieval"]
end

subgraph group_group_data["Persistence"]
  node_redis_stream[("Redis Streams<br/>job queue<br/>[redis_stream.go]")]
  node_supabase[("Supabase<br/>postgres graph store<br/>[supabase.go]")]
end

node_slack -->|"events"| node_go_main
node_github -->|"webhooks"| node_go_main
node_go_main -->|"boot"| node_routes
node_routes -->|"route"| node_slack_handler
node_routes -->|"route"| node_github_handler
node_slack_handler -->|"verify"| node_slack_verifier
node_slack_handler -->|"delegate"| node_slack_service
node_github_handler -->|"delegate"| node_github_service
node_slack_service -->|"coordinate"| node_core_services
node_github_service -->|"coordinate"| node_core_services
node_slack_service -->|"enqueue"| node_redis_stream
node_github_service -->|"enqueue"| node_redis_stream
node_slack_service -->|"reply"| node_slack_bot
node_redis_stream -->|"consume"| node_worker_consumer
node_worker_consumer -->|"ingest"| node_ingestion
node_worker_consumer -->|"query"| node_query_query
node_ingestion -->|"extract"| node_engine
node_engine -->|"write"| node_supabase
node_query_query -->|"analyze"| node_query_engine
node_query_engine -->|"read"| node_supabase
node_query_engine -->|"answer"| node_slack_bot

click node_go_main "https://github.com/nahom-zewdu/kms/blob/dev/api/main.go"
click node_routes "https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/routes.go"
click node_slack_handler "https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/slack.go"
click node_github_handler "https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/github.go"
click node_core_services "https://github.com/nahom-zewdu/kms/blob/dev/api/services/core.go"
click node_slack_service "https://github.com/nahom-zewdu/kms/blob/dev/api/services/slack.go"
click node_github_service "https://github.com/nahom-zewdu/kms/blob/dev/api/services/github.go"
click node_slack_bot "https://github.com/nahom-zewdu/kms/blob/dev/api/services/slack_bot.go"
click node_slack_verifier "https://github.com/nahom-zewdu/kms/blob/dev/api/services/slack_verifier.go"
click node_redis_stream "https://github.com/nahom-zewdu/kms/blob/dev/api/repository/redis_stream.go"
click node_supabase "https://github.com/nahom-zewdu/kms/blob/dev/api/repository/supabase.go"
click node_worker_consumer "https://github.com/nahom-zewdu/kms/blob/dev/nlp/worker/consumer.py"
click node_ingestion "https://github.com/nahom-zewdu/kms/blob/dev/nlp/worker/ingestion.py"
click node_query_query "https://github.com/nahom-zewdu/kms/blob/dev/nlp/worker/query.py"
click node_engine "https://github.com/nahom-zewdu/kms/tree/dev/nlp/engine"
click node_query_engine "https://github.com/nahom-zewdu/kms/tree/dev/nlp/query_engine"

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_slack,node_github toneBlue
class node_go_main,node_routes,node_slack_handler,node_github_handler,node_core_services,node_slack_service,node_github_service,node_slack_bot,node_slack_verifier toneAmber
class node_worker_consumer,node_ingestion,node_query_query,node_engine,node_query_engine toneMint
class node_redis_stream,node_supabase toneRose
```

## Components

### External sources

Inbound event producers and user-facing Slack surface.

- **Slack**: external source · Webhook source and bot conversation surface.
- **GitHub**: external source · Event source for repository knowledge capture.

### Go edge

Webhook intake, validation, and job orchestration.

- [API entry](https://github.com/nahom-zewdu/kms/blob/dev/api/main.go): go service · Process entrypoint and HTTP server startup. · `api/main.go`
- [Routes](https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/routes.go): go handlers · HTTP route wiring for incoming webhooks. · `api/handlers/routes.go`
- [Slack handler](https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/slack.go): go handler · Slack webhook ingestion and event handling. · `api/handlers/slack.go`
- [GitHub handler](https://github.com/nahom-zewdu/kms/blob/dev/api/handlers/github.go): go handler · GitHub webhook ingestion and event handling. · `api/handlers/github.go`
- [Core service](https://github.com/nahom-zewdu/kms/blob/dev/api/services/core.go): go orchestration · Shared business coordination across sources and jobs. · `api/services/core.go`
- [Slack service](https://github.com/nahom-zewdu/kms/blob/dev/api/services/slack.go): go service · Slack-specific processing and command coordination. · `api/services/slack.go`
- [GitHub service](https://github.com/nahom-zewdu/kms/blob/dev/api/services/github.go): go service · GitHub-specific processing and event normalization. · `api/services/github.go`
- [Slack bot](https://github.com/nahom-zewdu/kms/blob/dev/api/services/slack_bot.go): go service · Bot plumbing for replies and thread updates. · `api/services/slack_bot.go`
- [Verifier](https://github.com/nahom-zewdu/kms/blob/dev/api/services/slack_verifier.go): go service · Slack request verification and trust boundary. · `api/services/slack_verifier.go`

### Python NLP

Async ingestion, query, and answer generation.

- [Job consumer](https://github.com/nahom-zewdu/kms/blob/dev/nlp/worker/consumer.py): python worker · Consumes Redis stream jobs and dispatches processing. · `nlp/worker/consumer.py`
- [Ingestion](https://github.com/nahom-zewdu/kms/blob/dev/nlp/worker/ingestion.py): python pipeline · Transforms source events into structured knowledge. · `nlp/worker/ingestion.py`
- [Query worker](https://github.com/nahom-zewdu/kms/blob/dev/nlp/worker/query.py): python pipeline · Handles user questions and response jobs. · `nlp/worker/query.py`
- [Extraction engine](https://github.com/nahom-zewdu/kms/tree/dev/nlp/engine): python NLP · NER, relation extraction, prompts, schemas, and LLM calls. · `nlp/engine`
- [Query engine](https://github.com/nahom-zewdu/kms/tree/dev/nlp/query_engine): python retrieval · Retrieval, analysis, caching, synthesis, and vector recall. · `nlp/query_engine`

### Persistence

Redis Streams and Supabase/Postgres knowledge store.

- [Redis Streams](https://github.com/nahom-zewdu/kms/blob/dev/api/repository/redis_stream.go): job queue · Async handoff for ingestion and query jobs. · `api/repository/redis_stream.go`
- [Supabase](https://github.com/nahom-zewdu/kms/blob/dev/api/repository/supabase.go): postgres graph store · System of record for entities, edges, events, and vector search. · `api/repository/supabase.go`

## Connections

- Slack → API entry: events · Slack webhooks enter the Go API.
- GitHub → API entry: webhooks · GitHub events enter the Go API.
- API entry → Routes: boot · Main wires HTTP routes and server startup.
- Routes → Slack handler: route · Slack routes are bound to the Slack handler.
- Routes → GitHub handler: route · GitHub routes are bound to the GitHub handler.
- Slack handler → Verifier: verify · Slack requests are authenticated before processing.
- Slack handler → Slack service: delegate · Verified Slack events are normalized and handled.
- GitHub handler → GitHub service: delegate · GitHub webhook payloads are normalized and handled.
- Slack service → Core service: coordinate · Slack logic shares orchestration with core services.
- GitHub service → Core service: coordinate · GitHub logic shares orchestration with core services.
- Slack service → Redis Streams: enqueue · Slack jobs are pushed to Redis Streams.
- GitHub service → Redis Streams: enqueue · GitHub jobs are pushed to Redis Streams.
- Slack service → Slack bot: reply · Slack bot plumbing posts responses back to threads.
- Redis Streams → Job consumer: consume · Python worker consumes queued jobs.
- Job consumer → Ingestion: ingest · Source jobs flow into the ingestion pipeline.
- Job consumer → Query worker: query · Query jobs flow into the question-answer path.
- Ingestion → Extraction engine: extract · Extraction engine produces structured entities and edges.
- Extraction engine → Supabase: write · Structured knowledge is persisted to Supabase.
- Query worker → Query engine: analyze · Query engine performs retrieval and synthesis.
- Query engine → Supabase: read · Query engine reads graph and vector data from Supabase.
- Query engine → Slack bot: answer · Generated answers are sent back through Slack.
