# brocodeadi/contextforge. architecture

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

- Interactive diagram: https://gitdiagram.com/brocodeadi/contextforge.
- Repository: https://github.com/brocodeadi/contextforge.

## Overview

#### Purpose and Entry Points
ContextForge is a local Python context-compilation engine for cybersecurity skill corpora. It converts a natural-language task (optionally ambient desktop state) into a budget-constrained JSON context package. Primary entry points are the Typer CLI in `context_compiler/main.py`, desktop UI in `context_compiler/app.py`, tray launcher `run_tray_app.py`, and MCP stdio server `context_compiler/mcp_server.py`.

#### Corpus Ingestion and Persistent Data
Markdown skills are parsed into structured records and stored locally in SQLite; indexing then creates lexical and dense-search artifacts. The repository includes a small sample corpus and persisted Chroma data under `sample_skills/`; production artifacts are expected to be generated externally from a larger corpus.
- `context_compiler/ingest.py`
- `context_compiler/lexical_index.py`
- `context_compiler/semantic_index.py`
- `sample_skills/`

#### Hybrid Retrieval
Query handling combines BM25 lexical search, dense embedding cosine similarity, domain metadata, MITRE/framework matches, and a history/recency signal. `hybrid_retrieval.py` is the orchestration boundary over SQLite metadata plus lexical and semantic indexes, returning ranked candidates with score breakdowns.
- `context_compiler/hybrid_retrieval.py`
- `context_compiler/lexical_index.py`
- `context_compiler/semantic_index.py`

#### Budgeting and Compression Pipeline
Ranked skills flow through a greedy three-tier allocator: retain full content, mark viable overflow for compression, or drop it. The compressor first attempts local Ollama model synthesis and falls back to section-aware truncation, preserving an offline path. The CLI’s compile command coordinates retrieval, allocation, compression, and JSON assembly.
- `context_compiler/token_budget.py`
- `context_compiler/compress.py`
- `context_compiler/main.py`

#### Ambient Context and Event-Driven Use
Windows desktop context can supplement a query through foreground-window and clipboard capture. A polling watcher detects clipboard changes; hotkey, popup, and tray layers invoke compilation and present results without requiring the main GUI. This boundary is platform-sensitive because collection uses Win32 capabilities.
- `context_compiler/collector.py`
- `context_compiler/watcher.py`
- `context_compiler/tray_app.py`
- `context_compiler/hotkey_handler.py`

#### Delivery Surfaces
The same compiler capability is exposed through CLI, a CustomTkinter desktop application, and MCP tools/resources for clients such as Claude Desktop or Cursor. MCP is a stdio JSON-RPC integration rather than an HTTP service; it supports search, detail lookup, compilation, ambient collection, catalog, and domains.
- `context_compiler/main.py`
- `context_compiler/app.py`
- `context_compiler/mcp_server.py`

#### Runtime, Infrastructure, and External Boundaries
The runtime is Python 3.10+ with local SQLite, serialized BM25 data, NumPy embedding matrices, and `tiktoken`-style token accounting. Dense embeddings use the local `all-MiniLM-L6-v2` model; optional Ollama (documented default `qwen3:8b`) is the only model-serving dependency. The system is designed for no external API calls; MCP clients and Ollama are local-process/service boundaries. Evaluation measures retrieval and token-budget compliance.
- `requirements.txt`
- `context_compiler/evaluate.py`
- `benchmark_results.json`

Main entry points: [`context_compiler/hybrid_retrieval.py`](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/hybrid_retrieval.py) (Hybrid retrieval), [`context_compiler/main.py`](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/main.py) (Typer CLI), [`context_compiler/token_budget.py`](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/token_budget.py) (Three-tier allocator), [`context_compiler/ingest.py`](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/ingest.py) (Markdown ingestion)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Delivery surfaces"]
  node_cli["Typer CLI<br/>CLI entry point<br/>[main.py]"]
  node_desktop_app["Desktop app<br/>CustomTkinter UI<br/>[app.py]"]
  node_mcp["MCP stdio server<br/>JSON-RPC integration<br/>[mcp_server.py]"]
end

subgraph group_ambient["Windows ambient context"]
  node_tray_launcher["Tray launcher<br/>desktop entry point<br/>[run_tray_app.py]"]
  node_tray["Tray and popup<br/>desktop presentation<br/>[tray_app.py]"]
  node_hotkey["Hotkey handler<br/>desktop trigger<br/>[hotkey_handler.py]"]
  node_watcher["Clipboard watcher<br/>polling watcher<br/>[watcher.py]"]
  node_collector["Ambient collector<br/>Win32 context capture<br/>[collector.py]"]
end

subgraph group_pipeline["Compilation pipeline"]
  node_retrieval["Hybrid retrieval<br/>ranking orchestrator"]
  node_budget["Three-tier allocator<br/>token budget planner<br/>[token_budget.py]"]
  node_compressor["Compressor<br/>content compression<br/>[compress.py]"]
end

subgraph group_storage["Local corpus and indexes"]
  node_ingestion["Markdown ingestion<br/>corpus parser<br/>[ingest.py]"]
  node_sqlite[("SQLite metadata<br/>local database")]
  node_lexical[("BM25 index<br/>lexical search index<br/>[lexical_index.py]")]
  node_semantic[("Dense index<br/>embedding search index<br/>[semantic_index.py]")]
  node_sample_corpus["Skill corpus and artifacts<br/>Markdown corpus"]
end

subgraph group_external["Local dependencies"]
  node_ollama{{"Ollama<br/>local model service"}}
  node_mcp_clients{{"MCP clients<br/>local clients"}}
end

node_evaluation["Evaluation<br/>[evaluate.py]"]

node_mcp_clients -->|"stdio JSON-RPC"| node_mcp
node_cli -->|"compile query"| node_retrieval
node_cli -->|"coordinate compile"| node_budget
node_cli -->|"assemble JSON package"| node_compressor
node_desktop_app -.->|"invoke compiler"| node_cli
node_mcp -->|"search and lookup"| node_retrieval
node_mcp -->|"compile tool"| node_budget
node_tray_launcher -->|"start"| node_tray
node_watcher -->|"clipboard changes"| node_collector
node_hotkey -->|"trigger compilation"| node_cli
node_tray -->|"trigger and present"| node_cli
node_collector -->|"ambient query context"| node_cli
node_retrieval -->|"metadata and history"| node_sqlite
node_retrieval -->|"BM25 scores"| node_lexical
node_retrieval -->|"dense similarity"| node_semantic
node_retrieval -->|"ranked candidates"| node_budget
node_budget -->|"overflow skills"| node_compressor
node_compressor -->|"optional synthesis"| node_ollama
node_ingestion -->|"structured records"| node_sqlite
node_ingestion -->|"build index"| node_lexical
node_ingestion -->|"build embeddings"| node_semantic
node_sample_corpus -->|"source skills"| node_ingestion
node_evaluation -.->|"measure retrieval"| node_retrieval
node_evaluation -.->|"check token compliance"| node_budget

click node_cli "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/main.py"
click node_desktop_app "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/app.py"
click node_mcp "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/mcp_server.py"
click node_tray_launcher "https://github.com/brocodeadi/contextforge./blob/main/run_tray_app.py"
click node_tray "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/tray_app.py"
click node_hotkey "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/hotkey_handler.py"
click node_watcher "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/watcher.py"
click node_collector "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/collector.py"
click node_retrieval "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/hybrid_retrieval.py"
click node_budget "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/token_budget.py"
click node_compressor "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/compress.py"
click node_ingestion "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/ingest.py"
click node_lexical "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/lexical_index.py"
click node_semantic "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/semantic_index.py"
click node_evaluation "https://github.com/brocodeadi/contextforge./blob/main/context_compiler/evaluate.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_cli,node_desktop_app,node_mcp toneBlue
class node_tray_launcher,node_tray,node_hotkey,node_watcher,node_collector toneAmber
class node_retrieval,node_budget,node_compressor toneMint
class node_ingestion,node_sqlite,node_lexical,node_semantic,node_sample_corpus toneRose
class node_ollama,node_mcp_clients toneIndigo
class node_evaluation toneNeutral
```

## Components

### Delivery surfaces

- [Typer CLI](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/main.py): CLI entry point · Compile command coordinates the context package flow. · `context_compiler/main.py`
- [Desktop app](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/app.py): CustomTkinter UI · `context_compiler/app.py`
- [MCP stdio server](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/mcp_server.py): JSON-RPC integration · Exposes search, lookup, compilation, ambient, catalog, and domain tools. · `context_compiler/mcp_server.py`

### Windows ambient context

Platform-sensitive desktop collection and triggers.

- [Tray launcher](https://github.com/brocodeadi/contextforge./blob/main/run_tray_app.py): desktop entry point · `run_tray_app.py`
- [Tray and popup](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/tray_app.py): desktop presentation · `context_compiler/tray_app.py`
- [Hotkey handler](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/hotkey_handler.py): desktop trigger · `context_compiler/hotkey_handler.py`
- [Clipboard watcher](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/watcher.py): polling watcher · `context_compiler/watcher.py`
- [Ambient collector](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/collector.py): Win32 context capture · Captures foreground-window and clipboard state. · `context_compiler/collector.py`

### Compilation pipeline

- [Hybrid retrieval](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/hybrid_retrieval.py): ranking orchestrator · Combines lexical, dense, metadata, framework, and recency signals. · `context_compiler/hybrid_retrieval.py`
- [Three-tier allocator](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/token_budget.py): token budget planner · Retains, compresses, or drops ranked skills. · `context_compiler/token_budget.py`
- [Compressor](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/compress.py): content compression · Uses Ollama synthesis with section-aware truncation fallback. · `context_compiler/compress.py`

### Local corpus and indexes

- [Markdown ingestion](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/ingest.py): corpus parser · Parses skills into structured local records. · `context_compiler/ingest.py`
- **SQLite metadata**: local database · Stores structured skill records and metadata.
- [BM25 index](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/lexical_index.py): lexical search index · Serialized local lexical-search artifact. · `context_compiler/lexical_index.py`
- [Dense index](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/semantic_index.py): embedding search index · Local embedding matrix and dense-search artifacts. · `context_compiler/semantic_index.py`
- **Skill corpus and artifacts**: Markdown corpus · Sample skills and persisted Chroma data.

### Local dependencies

- **Ollama**: local model service · Optional local synthesis model service.
- **MCP clients**: local clients · Clients such as Claude Desktop or Cursor.

### Other components

- [Evaluation](https://github.com/brocodeadi/contextforge./blob/main/context_compiler/evaluate.py): retrieval and budget evaluation · `context_compiler/evaluate.py`

## Connections

- MCP clients → MCP stdio server: stdio JSON-RPC
- Typer CLI → Hybrid retrieval: compile query
- Typer CLI → Three-tier allocator: coordinate compile
- Typer CLI → Compressor: assemble JSON package
- Desktop app → Typer CLI: invoke compiler
- MCP stdio server → Hybrid retrieval: search and lookup
- MCP stdio server → Three-tier allocator: compile tool
- Tray launcher → Tray and popup: start
- Clipboard watcher → Ambient collector: clipboard changes
- Hotkey handler → Typer CLI: trigger compilation
- Tray and popup → Typer CLI: trigger and present
- Ambient collector → Typer CLI: ambient query context
- Hybrid retrieval → SQLite metadata: metadata and history
- Hybrid retrieval → BM25 index: BM25 scores
- Hybrid retrieval → Dense index: dense similarity
- Hybrid retrieval → Three-tier allocator: ranked candidates
- Three-tier allocator → Compressor: overflow skills
- Compressor → Ollama: optional synthesis
- Markdown ingestion → SQLite metadata: structured records
- Markdown ingestion → BM25 index: build index
- Markdown ingestion → Dense index: build embeddings
- Skill corpus and artifacts → Markdown ingestion: source skills
- Evaluation → Hybrid retrieval: measure retrieval
- Evaluation → Three-tier allocator: check token compliance
