# neural-maze/substack-brain-course architecture

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

- Interactive diagram: https://gitdiagram.com/neural-maze/substack-brain-course
- Repository: https://github.com/neural-maze/substack-brain-course

## Overview

The Substack Brain turns allowlisted newsletter feeds into a searchable, citation-bearing knowledge base. Its main workflow discovers articles, durably ingests and versions their passages, then retrieves and synthesizes answers; users can also search, manage backfills and syncs, and access the system through MCP. Postgres stores content, jobs, and answer snapshots. The README describes later graph extraction, evaluations, and agent/digest capabilities, but those implementations are not present in the supplied tree. Edges reflect shown imports, calls, or README statements; unsampled internals are not inferred.

Main entry points: [`kb/functions/ingest.py`](https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/ingest.py) (Article ingestion), [`kb/functions/ask.py`](https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/ask.py) (Answer and citations), [`kb/inngest_client.py`](https://github.com/neural-maze/substack-brain-course/blob/main/kb/inngest_client.py) (Durable orchestration), [`kb/db/queries.py`](https://github.com/neural-maze/substack-brain-course/blob/main/kb/db/queries.py) (Database queries)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and MCP"]
  node_http["HTTP API<br/>[main.py]"]
  node_mcpserver["MCP server<br/>[server.py]"]
  node_mcptools["MCP tools<br/>[tools.py]"]
  node_health["Health checks<br/>[health.py]"]
end

subgraph group_workflows["Durable workflows"]
  node_discovery["Feed discovery<br/>[discovery.py]"]
  node_backfill["Publication backfill<br/>[backfill.py]"]
  node_freshness["Freshness polling<br/>[freshness.py]"]
  node_ingest["Article ingestion<br/>[ingest.py]"]
  node_inngest["Durable orchestration<br/>[inngest_client.py]"]
  node_events["Event contracts<br/>[events.py]"]
end

subgraph group_content["Content ingestion"]
  node_allowlist["Publication allowlist<br/>[allowlist.py]"]
  node_rss["RSS handling<br/>[rss.py]"]
  node_articleprocessing["Fetch parse chunk"]
  node_embedder["Embeddings<br/>[embedder.py]"]
  node_ids["Canonical identifiers<br/>[ids.py]"]
end

subgraph group_knowledge["Retrieval and answers"]
  node_retrieval["Hybrid retrieval<br/>[hybrid.py]"]
  node_ask["Answer and citations<br/>[ask.py]"]
  node_snapshots["Retrieval snapshots<br/>[snapshot.py]"]
  node_latercapabilities["Later course capabilities"]
end

subgraph group_storage["Persistence"]
  node_dbqueries["Database queries<br/>[queries.py]"]
  node_models["Database models<br/>[models.py]"]
  node_session["Database sessions<br/>[session.py]"]
end

node_user(("Engineer / AI tool"))
node_postgres[("Postgres / pgvector")]
node_newsletters(("Newsletter feeds"))
node_openai["OpenAI embeddings"]
node_anthropic["Anthropic synthesis"]
node_inngestservice["Inngest service"]

node_user -->|"calls"| node_http
node_user -->|"connects"| node_mcpserver
node_mcpserver -->|"registers"| node_mcptools
node_mcptools -->|"calls"| node_ask
node_mcptools -->|"searches"| node_retrieval
node_mcptools -->|"checks"| node_health
node_http -->|"routes"| node_health
node_http -->|"routes"| node_ask
node_http -->|"dispatches"| node_discovery
node_newsletters -->|"provides feeds"| node_rss
node_discovery -->|"fetches and parses"| node_rss
node_backfill -->|"fetches and parses"| node_rss
node_freshness -->|"polls conditionally"| node_rss
node_discovery -->|"validates sources"| node_allowlist
node_backfill -->|"validates sources"| node_allowlist
node_freshness -->|"validates sources"| node_allowlist
node_discovery -->|"emits events"| node_events
node_backfill -->|"emits events"| node_events
node_freshness -->|"emits events"| node_events
node_discovery -->|"dispatches"| node_inngest
node_backfill -->|"dispatches"| node_inngest
node_freshness -->|"dispatches"| node_inngest
node_inngest -->|"runs steps"| node_ingest
node_ingest -->|"checks URLs"| node_allowlist
node_ingest -->|"fetches and parses"| node_articleprocessing
node_ingest -->|"embeds passages"| node_embedder
node_ingest -->|"persists articles"| node_dbqueries
node_ingest -->|"emits outcomes"| node_events
node_ingest -->|"uses dedupe keys"| node_ids
node_inngest -->|"communicates"| node_inngestservice
node_retrieval -->|"embeds query"| node_embedder
node_retrieval -->|"searches passages"| node_dbqueries
node_ask -->|"retrieves candidates"| node_retrieval
node_ask -->|"records answer"| node_snapshots
node_ask -->|"emits answer event"| node_events
node_ask -->|"synthesizes answer"| node_anthropic
node_embedder -->|"requests embeddings"| node_openai
node_dbqueries -->|"reads and writes"| node_postgres
node_models -->|"defines stored records"| node_postgres
node_session -->|"opens sessions"| node_postgres
node_dbqueries -->|"queries records"| node_models
node_snapshots -->|"stores snapshot"| node_dbqueries
node_inngest -->|"records step costs"| node_dbqueries
node_latercapabilities -.->|"offers planned features"| node_user

click node_http "https://github.com/neural-maze/substack-brain-course/blob/main/kb/main.py"
click node_mcpserver "https://github.com/neural-maze/substack-brain-course/blob/main/kb/mcp/server.py"
click node_mcptools "https://github.com/neural-maze/substack-brain-course/blob/main/kb/mcp/tools.py"
click node_health "https://github.com/neural-maze/substack-brain-course/blob/main/kb/health.py"
click node_discovery "https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/discovery.py"
click node_backfill "https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/backfill.py"
click node_freshness "https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/freshness.py"
click node_ingest "https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/ingest.py"
click node_inngest "https://github.com/neural-maze/substack-brain-course/blob/main/kb/inngest_client.py"
click node_events "https://github.com/neural-maze/substack-brain-course/blob/main/kb/schemas/events.py"
click node_allowlist "https://github.com/neural-maze/substack-brain-course/blob/main/kb/sources/allowlist.py"
click node_rss "https://github.com/neural-maze/substack-brain-course/blob/main/kb/sources/rss.py"
click node_articleprocessing "https://github.com/neural-maze/substack-brain-course/tree/main/kb/sources"
click node_embedder "https://github.com/neural-maze/substack-brain-course/blob/main/kb/sources/embedder.py"
click node_ids "https://github.com/neural-maze/substack-brain-course/blob/main/kb/schemas/ids.py"
click node_retrieval "https://github.com/neural-maze/substack-brain-course/blob/main/kb/retrieval/hybrid.py"
click node_ask "https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/ask.py"
click node_snapshots "https://github.com/neural-maze/substack-brain-course/blob/main/kb/retrieval/snapshot.py"
click node_dbqueries "https://github.com/neural-maze/substack-brain-course/blob/main/kb/db/queries.py"
click node_models "https://github.com/neural-maze/substack-brain-course/blob/main/kb/db/models.py"
click node_session "https://github.com/neural-maze/substack-brain-course/blob/main/kb/db/session.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_http,node_mcpserver,node_mcptools,node_health toneBlue
class node_discovery,node_backfill,node_freshness,node_ingest,node_inngest,node_events,node_postgres toneAmber
class node_allowlist,node_rss,node_articleprocessing,node_embedder,node_ids toneMint
class node_retrieval,node_ask,node_snapshots,node_latercapabilities toneRose
class node_dbqueries,node_models,node_session,node_user,node_newsletters,node_openai,node_anthropic,node_inngestservice toneIndigo
```

## Components

### API and MCP

- [HTTP API](https://github.com/neural-maze/substack-brain-course/blob/main/kb/main.py): `kb/main.py`
- [MCP server](https://github.com/neural-maze/substack-brain-course/blob/main/kb/mcp/server.py): `kb/mcp/server.py`
- [MCP tools](https://github.com/neural-maze/substack-brain-course/blob/main/kb/mcp/tools.py): `kb/mcp/tools.py`
- [Health checks](https://github.com/neural-maze/substack-brain-course/blob/main/kb/health.py): `kb/health.py`

### Durable workflows

- [Feed discovery](https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/discovery.py): `kb/functions/discovery.py`
- [Publication backfill](https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/backfill.py): `kb/functions/backfill.py`
- [Freshness polling](https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/freshness.py): `kb/functions/freshness.py`
- [Article ingestion](https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/ingest.py): `kb/functions/ingest.py`
- [Durable orchestration](https://github.com/neural-maze/substack-brain-course/blob/main/kb/inngest_client.py): `kb/inngest_client.py`
- [Event contracts](https://github.com/neural-maze/substack-brain-course/blob/main/kb/schemas/events.py): `kb/schemas/events.py`

### Content ingestion

- [Publication allowlist](https://github.com/neural-maze/substack-brain-course/blob/main/kb/sources/allowlist.py): `kb/sources/allowlist.py`
- [RSS handling](https://github.com/neural-maze/substack-brain-course/blob/main/kb/sources/rss.py): `kb/sources/rss.py`
- [Fetch parse chunk](https://github.com/neural-maze/substack-brain-course/tree/main/kb/sources): `kb/sources`
- [Embeddings](https://github.com/neural-maze/substack-brain-course/blob/main/kb/sources/embedder.py): `kb/sources/embedder.py`
- [Canonical identifiers](https://github.com/neural-maze/substack-brain-course/blob/main/kb/schemas/ids.py): `kb/schemas/ids.py`

### Retrieval and answers

- [Hybrid retrieval](https://github.com/neural-maze/substack-brain-course/blob/main/kb/retrieval/hybrid.py): `kb/retrieval/hybrid.py`
- [Answer and citations](https://github.com/neural-maze/substack-brain-course/blob/main/kb/functions/ask.py): `kb/functions/ask.py`
- [Retrieval snapshots](https://github.com/neural-maze/substack-brain-course/blob/main/kb/retrieval/snapshot.py): `kb/retrieval/snapshot.py`
- **Later course capabilities**

### Persistence

- [Database queries](https://github.com/neural-maze/substack-brain-course/blob/main/kb/db/queries.py): `kb/db/queries.py`
- [Database models](https://github.com/neural-maze/substack-brain-course/blob/main/kb/db/models.py): `kb/db/models.py`
- [Database sessions](https://github.com/neural-maze/substack-brain-course/blob/main/kb/db/session.py): `kb/db/session.py`

### Other components

- **Engineer / AI tool**
- **Postgres / pgvector**
- **Newsletter feeds**
- **OpenAI embeddings**
- **Anthropic synthesis**
- **Inngest service**

## Connections

- Engineer / AI tool → HTTP API: calls (evidence: `README.md`)
- Engineer / AI tool → MCP server: connects (evidence: `README.md`)
- MCP server → MCP tools: registers (evidence: `kb/mcp/server.py`)
- MCP tools → Answer and citations: calls (evidence: `kb/mcp/tools.py`)
- MCP tools → Hybrid retrieval: searches (evidence: `kb/mcp/tools.py`)
- MCP tools → Health checks: checks (evidence: `kb/mcp/tools.py`)
- HTTP API → Health checks: routes (evidence: `kb/main.py`)
- HTTP API → Answer and citations: routes (evidence: `kb/main.py`)
- HTTP API → Feed discovery: dispatches (evidence: `kb/main.py`)
- Newsletter feeds → RSS handling: provides feeds (evidence: `kb/sources/rss.py`)
- Feed discovery → RSS handling: fetches and parses (evidence: `kb/functions/discovery.py`)
- Publication backfill → RSS handling: fetches and parses (evidence: `kb/functions/backfill.py`)
- Freshness polling → RSS handling: polls conditionally (evidence: `kb/functions/freshness.py`)
- Feed discovery → Publication allowlist: validates sources (evidence: `kb/functions/discovery.py`)
- Publication backfill → Publication allowlist: validates sources (evidence: `kb/functions/backfill.py`)
- Freshness polling → Publication allowlist: validates sources (evidence: `kb/functions/freshness.py`)
- Feed discovery → Event contracts: emits events (evidence: `kb/schemas/events.py`)
- Publication backfill → Event contracts: emits events (evidence: `kb/schemas/events.py`)
- Freshness polling → Event contracts: emits events (evidence: `kb/schemas/events.py`)
- Feed discovery → Durable orchestration: dispatches (evidence: `kb/functions/discovery.py`)
- Publication backfill → Durable orchestration: dispatches (evidence: `kb/functions/backfill.py`)
- Freshness polling → Durable orchestration: dispatches (evidence: `kb/functions/freshness.py`)
- Durable orchestration → Article ingestion: runs steps (evidence: `kb/functions/ingest.py`)
- Article ingestion → Publication allowlist: checks URLs (evidence: `kb/functions/ingest.py`)
- Article ingestion → Fetch parse chunk: fetches and parses (evidence: `kb/functions/ingest.py`)
- Article ingestion → Embeddings: embeds passages (evidence: `kb/functions/ingest.py`)
- Article ingestion → Database queries: persists articles (evidence: `kb/functions/ingest.py`)
- Article ingestion → Event contracts: emits outcomes (evidence: `kb/functions/ingest.py`)
- Article ingestion → Canonical identifiers: uses dedupe keys (evidence: `kb/schemas/ids.py`)
- Durable orchestration → Inngest service: communicates (evidence: `README.md`)
- Hybrid retrieval → Embeddings: embeds query (evidence: `kb/retrieval/hybrid.py`)
- Hybrid retrieval → Database queries: searches passages (evidence: `kb/retrieval/hybrid.py`)
- Answer and citations → Hybrid retrieval: retrieves candidates (evidence: `kb/functions/ask.py`)
- Answer and citations → Retrieval snapshots: records answer (evidence: `kb/functions/ask.py`)
- Answer and citations → Event contracts: emits answer event (evidence: `kb/functions/ask.py`)
- Answer and citations → Anthropic synthesis: synthesizes answer (evidence: `kb/functions/ask.py`)
- Embeddings → OpenAI embeddings: requests embeddings (evidence: `kb/sources/embedder.py`)
- Database queries → Postgres / pgvector: reads and writes (evidence: `kb/db/queries.py`)
- Database models → Postgres / pgvector: defines stored records (evidence: `kb/db/models.py`)
- Database sessions → Postgres / pgvector: opens sessions (evidence: `kb/db/session.py`)
- Database queries → Database models: queries records (evidence: `kb/db/queries.py`)
- Retrieval snapshots → Database queries: stores snapshot (evidence: `kb/retrieval/snapshot.py`)
- Durable orchestration → Database queries: records step costs (evidence: `kb/inngest_client.py`)
- Later course capabilities → Engineer / AI tool: offers planned features (evidence: `README.md`)
