# winrisef/ctxrun architecture

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

- Interactive diagram: https://gitdiagram.com/winrisef/ctxrun
- Repository: https://github.com/winrisef/ctxrun

## Overview

1. **Overall shape**
   - CtxRun is a **Tauri 2 desktop application**: a React/TypeScript frontend runs inside multiple WebView windows, while a Rust backend owns system access, local services, persistence, and privileged operations.
   - The repository is split into a **frontend app** under `src/` and a **Rust/Tauri workspace** under `src-tauri/`, with feature-heavy domains implemented as separate Rust crates.

2. **Windowed desktop shell**
   - The frontend is organized around distinct windows and UI shells: `main`, `spotlight`, `peek`, and `guard`.
   - `src/windows/main` is the primary workspace shell; `spotlight` is the global command/chat surface; `peek` is a standalone preview window; `guard` is the idle-lock screen.
   - Shared layout pieces like title bar, workspace switcher, and view switcher live in `src/components/layout`, while app-wide state is managed with Zustand stores in `src/store`.

3. **Feature-oriented frontend**
   - Each major capability is isolated in `src/components/features/*` with its own view layer and supporting hooks.
   - The frontend feature set matches the product areas in the README: **Context Forge**, **Spotlight**, **Prompt Verse**, **Patch Weaver**, **Refinery**, **Automator**, **Model Miner**, **Transfer**, **Guard**, **Peek**, **monitoring**, and **settings**.
   - The frontend also includes reusable infrastructure for i18n, markdown rendering, previewing, code editing, AI/LLM interactions, and local storage abstractions in `src/lib`.

4. **Rust backend as domain services**
   - The backend is not a single monolith; it is split into focused crates under `src-tauri/crates/`.
   - Most crates expose **Tauri commands** and have their own permission manifests, showing a clear boundary between UI requests and privileged OS/file/network operations.
   - This structure makes it straightforward to draw the backend as multiple domain services behind the Tauri bridge.

5. **Local persistence layer**
   - The `db` crate owns **SQLite persistence** and schema migrations.
   - It stores application data such as app metadata, project config, prompts, secrets, shell history, and URL history.
   - Migrations are managed separately, so the database is an explicit subsystem rather than an incidental cache.

6. **Context assembly and secret scanning**
   - The `context` crate handles project tree scanning, ignore-file awareness, tokenization, content assembly, and sensitive-data detection.
   - It includes gitleaks-inspired rules and allowlists, so its data flow is: **project filesystem → scanner/tokenizer → filtered context output → UI preview/export**.
   - This is the core engine behind Context Forge and the security scan features.

7. **AI tool and command execution**
   - The `tool-runtime` crate provides a sandboxed AI tool environment for filesystem, web, and patch-related actions.
   - The `exec-runtime` crate is the command-execution counterpart: it manages request/approval, terminal interaction, resizing, termination, and safety policy enforcement.
   - On the frontend, `src/lib/agent` and the Spotlight UI coordinate these capabilities, so AI conversations can invoke tools or launch shell actions only through approved runtime boundaries.

8. **Git and patch workflow**
   - The `git` crate exposes commit/diff/export commands, while frontend patch components provide diff viewing, workspace comparison, and export flows.
   - Patch-related logic is therefore split between **Rust for Git data extraction** and **React for visualization and editing assistance**.
   - This is a distinct pipeline from plain text context assembly: source tree/diff data is transformed into reviewable patch views.

9. **Clipboard history and cleanup subsystem**
   - The `refinery` crate manages clipboard history, notes, pinning, cleanup, statistics, and Spotlight quick-paste integration.
   - It includes worker and cleanup worker components, which implies background processing rather than purely UI-driven behavior.
   - The frontend `RefineryView` and related sidebar/feed components are the presentation layer for this locally persisted activity stream.

10. **Web mining and preview pipeline**
    - The `miner` crate performs crawl/extraction work using Readability-style processing and Markdown conversion, with queue/scope/storage separation.
    - The frontend `MinerView` drives search, start/stop, and result presentation.
    - Related to this, the `hyperview` and `ocr` crates support rich file preview and OCR recognition, feeding preview panels and standalone preview windows.

11. **Network and device transfer**
    - The `transfer` crate exposes a local LAN transfer service: device discovery, QR pairing, file/message exchange, WebSocket chat, and server lifecycle control.
    - The frontend `TransferView` becomes a control panel over a backend-local HTTP/WebSocket service.
    - This subsystem is explicitly networked, unlike most of the app, but still remains local-first and peer-to-peer over the LAN.

12. **System monitoring and environment probing**
    - `env-probe` collects machine, browser, SDK, IDE, binary, and network metadata.
    - `monitor`-related frontend views surface this information as system dashboards, port/process tools, network diagnostics, and environment fingerprints.
    - This subsystem is read-heavy and diagnostic, feeding observability-style views rather than core editing workflows.

13. **Guard and input interception**
    - The `guard` window and backend support idle detection, low-level input monitoring, long-press unlock, and sleep-prevention behavior.
    - This is a separate security/usability boundary because it needs global input and OS-level hooks, unlike normal in-app UI state.

14. **Build, assets, and generated data**
    - Build-time content is significant: `build/` generates prompt packs, and `models/` contains OCR model metadata.
    - GitHub Actions workflows cover CI, releases, prompt updates, and OCR model sync, so some runtime assets are produced from external sources and then bundled locally.
    - The repo also contains generated Tauri schemas and permissions manifests, reinforcing that capabilities are intentionally constrained and surfaced per crate.

15. **Key architectural boundary for a diagram**
    - The cleanest mental model is: **React/Tauri UI windows on the left**, **Rust domain crates and local services on the right**, with **SQLite and local OS/network resources underneath**.
    - Most user actions start in a feature view, cross the Tauri command boundary, hit a dedicated Rust crate, and then return structured results to the UI for rendering or storage.

Main entry points: [`src/components/features`](https://github.com/winrisef/ctxrun/tree/main/src/components/features) (Feature views), [`src-tauri/src/main.rs`](https://github.com/winrisef/ctxrun/blob/main/src-tauri/src/main.rs) (Tauri bridge), [`src-tauri/crates/db/src`](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/db/src) (SQLite DB), [`src/components/layout`](https://github.com/winrisef/ctxrun/tree/main/src/components/layout) (App shell)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React/Tauri UI"]
  node_main_window["Main window<br/>desktop shell<br/>[MainWindowApp.tsx]"]
  node_spotlight_window["Spotlight<br/>command surface"]
  node_peek_window["Peek<br/>preview window<br/>[PeekWindowApp.tsx]"]
  node_guard_window["Guard<br/>lock screen<br/>[GuardWindowApp.tsx]"]
  node_layout_shell["App shell<br/>shared layout"]
  node_feature_views["Feature views<br/>ui modules"]
  node_app_state["Stores<br/>state layer"]
  node_agent_ui["Agent client<br/>llm orchestration"]
  node_preview_stack["Preview stack<br/>render pipeline"]
  node_context_ui["Context Forge<br/>context UI"]
  node_spotlight_ui["Spotlight UI<br/>chat/search UI"]
  node_patch_ui["Patch tools<br/>diff UI"]
end

subgraph group_backend["Rust services"]
  node_backend_bridge["Tauri bridge<br/>command boundary<br/>[main.rs]"]
  node_context_service["Context service<br/>scan pipeline"]
  node_agent_runtime["Tool runtime<br/>sandboxed tools"]
  node_exec_runtime["Exec runtime<br/>command execution"]
  node_git_service["Git service<br/>git data"]
  node_refinery_service["Refinery<br/>clipboard service"]
  node_miner_service["Miner<br/>web mining"]
  node_transfer_service["Transfer<br/>lan service"]
  node_monitor_service["Env probe<br/>diagnostics"]
  node_guard_service["Guard service<br/>idle security<br/>[guard.rs]"]
end

subgraph group_data["Local data"]
  node_db_service[("SQLite DB<br/>persistence")]
  node_assets_build["Build assets<br/>generated content"]
end

node_main_window -->|"uses"| node_layout_shell
node_spotlight_window -->|"uses"| node_layout_shell
node_peek_window -->|"uses"| node_layout_shell
node_guard_window -->|"uses"| node_layout_shell
node_main_window -->|"hosts"| node_feature_views
node_spotlight_window -->|"hosts"| node_spotlight_ui
node_peek_window -->|"hosts"| node_preview_stack
node_guard_window -->|"drives"| node_guard_service
node_feature_views -->|"includes"| node_context_ui
node_feature_views -->|"includes"| node_patch_ui
node_feature_views -->|"includes"| node_preview_stack
node_feature_views -->|"includes"| node_agent_ui
node_feature_views -->|"includes"| node_refinery_service
node_feature_views -->|"includes"| node_miner_service
node_feature_views -->|"includes"| node_transfer_service
node_feature_views -->|"includes"| node_monitor_service
node_feature_views -.->|"uses"| node_assets_build
node_spotlight_ui -->|"calls"| node_agent_ui
node_agent_ui -->|"routes through"| node_backend_bridge
node_backend_bridge -->|"exposes"| node_context_service
node_backend_bridge -->|"exposes"| node_db_service
node_backend_bridge -->|"exposes"| node_agent_runtime
node_backend_bridge -->|"exposes"| node_exec_runtime
node_backend_bridge -->|"exposes"| node_git_service
node_backend_bridge -->|"exposes"| node_refinery_service
node_backend_bridge -->|"exposes"| node_miner_service
node_backend_bridge -->|"exposes"| node_transfer_service
node_backend_bridge -->|"exposes"| node_monitor_service
node_context_service -->|"reads/writes"| node_db_service
node_git_service -->|"reads/writes"| node_db_service
node_refinery_service -->|"reads/writes"| node_db_service
node_miner_service -->|"reads/writes"| node_db_service
node_transfer_service -->|"reads/writes"| node_db_service
node_monitor_service -.->|"uses"| node_assets_build

click node_main_window "https://github.com/winrisef/ctxrun/blob/main/src/windows/main/MainWindowApp.tsx"
click node_spotlight_window "https://github.com/winrisef/ctxrun/blob/main/src/windows/spotlight/SpotlightWindowApp.tsx"
click node_peek_window "https://github.com/winrisef/ctxrun/blob/main/src/windows/peek/PeekWindowApp.tsx"
click node_guard_window "https://github.com/winrisef/ctxrun/blob/main/src/windows/guard/GuardWindowApp.tsx"
click node_layout_shell "https://github.com/winrisef/ctxrun/tree/main/src/components/layout"
click node_feature_views "https://github.com/winrisef/ctxrun/tree/main/src/components/features"
click node_app_state "https://github.com/winrisef/ctxrun/tree/main/src/store"
click node_agent_ui "https://github.com/winrisef/ctxrun/tree/main/src/lib/agent"
click node_preview_stack "https://github.com/winrisef/ctxrun/tree/main/src/components/features/hyperview"
click node_context_ui "https://github.com/winrisef/ctxrun/tree/main/src/components/features/context"
click node_spotlight_ui "https://github.com/winrisef/ctxrun/tree/main/src/components/features/spotlight"
click node_patch_ui "https://github.com/winrisef/ctxrun/tree/main/src/components/features/patch"
click node_backend_bridge "https://github.com/winrisef/ctxrun/blob/main/src-tauri/src/main.rs"
click node_context_service "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/context/src"
click node_db_service "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/db/src"
click node_agent_runtime "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/tool-runtime/src"
click node_exec_runtime "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/exec-runtime/src"
click node_git_service "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/git/src"
click node_refinery_service "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/refinery/src"
click node_miner_service "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/miner/src/core"
click node_transfer_service "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/transfer/src"
click node_monitor_service "https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/env-probe/src/env_probe"
click node_guard_service "https://github.com/winrisef/ctxrun/blob/main/src-tauri/src/guard.rs"
click node_assets_build "https://github.com/winrisef/ctxrun/tree/main/build/dist/packs"

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_window,node_spotlight_window,node_peek_window,node_guard_window,node_layout_shell,node_feature_views,node_app_state,node_agent_ui,node_preview_stack,node_context_ui,node_spotlight_ui,node_patch_ui toneBlue
class node_backend_bridge,node_context_service,node_agent_runtime,node_exec_runtime,node_git_service,node_refinery_service,node_miner_service,node_transfer_service,node_monitor_service,node_guard_service toneAmber
class node_db_service,node_assets_build toneMint
```

## Components

### React/Tauri UI

WebView windows, views, shared components, and client-side orchestration.

- [Main window](https://github.com/winrisef/ctxrun/blob/main/src/windows/main/MainWindowApp.tsx): desktop shell · Primary workspace window for the app's main flows. · `src/windows/main/MainWindowApp.tsx`
- [Spotlight](https://github.com/winrisef/ctxrun/blob/main/src/windows/spotlight/SpotlightWindowApp.tsx): command surface · Global search, chat, and approval-driven action surface. · `src/windows/spotlight/SpotlightWindowApp.tsx`
- [Peek](https://github.com/winrisef/ctxrun/blob/main/src/windows/peek/PeekWindowApp.tsx): preview window · Standalone preview window for rich file and media inspection. · `src/windows/peek/PeekWindowApp.tsx`
- [Guard](https://github.com/winrisef/ctxrun/blob/main/src/windows/guard/GuardWindowApp.tsx): lock screen · Idle-lock and unlock window for guarded sessions. · `src/windows/guard/GuardWindowApp.tsx`
- [App shell](https://github.com/winrisef/ctxrun/tree/main/src/components/layout): shared layout · Title bar, workspace switching, and view switching across windows. · `src/components/layout`
- [Feature views](https://github.com/winrisef/ctxrun/tree/main/src/components/features): ui modules · Primary product areas exposed as React feature panels and dialogs. · `src/components/features`
- [Stores](https://github.com/winrisef/ctxrun/tree/main/src/store): state layer · Zustand stores and cross-window state synchronization. · `src/store`
- [Agent client](https://github.com/winrisef/ctxrun/tree/main/src/lib/agent): llm orchestration · Frontend agent runtime, tool registry, and approval-aware execution wiring. · `src/lib/agent`
- [Preview stack](https://github.com/winrisef/ctxrun/tree/main/src/components/features/hyperview): render pipeline · Hyperview preview, OCR, and renderer components for rich content display. · `src/components/features/hyperview`
- [Context Forge](https://github.com/winrisef/ctxrun/tree/main/src/components/features/context): context UI · Project scanning, token dashboard, filtering, and secret-scan presentation. · `src/components/features/context`
- [Spotlight UI](https://github.com/winrisef/ctxrun/tree/main/src/components/features/spotlight): chat/search UI · Chat, search, trace, and approval UI for the Spotlight window. · `src/components/features/spotlight`
- [Patch tools](https://github.com/winrisef/ctxrun/tree/main/src/components/features/patch): diff UI · Commit selection, diff viewing, workspace comparison, and export dialogs. · `src/components/features/patch`

### Rust services

Tauri backend workspace with domain crates and privileged commands.

- [Tauri bridge](https://github.com/winrisef/ctxrun/blob/main/src-tauri/src/main.rs): command boundary · Main Tauri application entry and command surface exposed to the frontend. · `src-tauri/src/main.rs`
- [Context service](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/context/src): scan pipeline · Filesystem scanning, tokenization, ignore handling, and secret detection. · `src-tauri/crates/context/src`
- [Tool runtime](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/tool-runtime/src): sandboxed tools · Sandboxed AI tools for filesystem, patch, web, and workspace operations. · `src-tauri/crates/tool-runtime/src`
- [Exec runtime](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/exec-runtime/src): command execution · Approved shell execution, terminal I/O, resizing, and safety policy enforcement. · `src-tauri/crates/exec-runtime/src`
- [Git service](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/git/src): git data · Commit, diff, and export operations used by patch workflows. · `src-tauri/crates/git/src`
- [Refinery](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/refinery/src): clipboard service · Clipboard history, notes, pinning, cleanup, statistics, and worker processing. · `src-tauri/crates/refinery/src`
- [Miner](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/miner/src/core): web mining · Web crawl, extraction, and search pipeline with queue and storage stages. · `src-tauri/crates/miner/src/core`
- [Transfer](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/transfer/src): lan service · Local device discovery, pairing, messaging, file exchange, and WebSocket server lifecycle. · `src-tauri/crates/transfer/src`
- [Env probe](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/env-probe/src/env_probe): diagnostics · System, browser, SDK, binary, and network metadata collection for monitoring views. · `src-tauri/crates/env-probe/src/env_probe`
- [Guard service](https://github.com/winrisef/ctxrun/blob/main/src-tauri/src/guard.rs): idle security · Idle detection, input monitoring, sleep prevention, and unlock support. · `src-tauri/src/guard.rs`

### Local data

Persistence and generated assets used across the app.

- [SQLite DB](https://github.com/winrisef/ctxrun/tree/main/src-tauri/crates/db/src): persistence · Application database and migrations for prompts, history, secrets, and configs. · `src-tauri/crates/db/src`
- [Build assets](https://github.com/winrisef/ctxrun/tree/main/build/dist/packs): generated content · Prompt packs, OCR model metadata, and bundled generated data used at runtime. · `build/dist/packs`

## Connections

- Main window → App shell: uses · Main shell composes shared navigation and chrome.
- Spotlight → App shell: uses · Spotlight window reuses shared title and switching controls.
- Peek → App shell: uses · Peek relies on the same app shell patterns.
- Guard → App shell: uses · Guard window is styled through shared window chrome.
- Main window → Feature views: hosts · Primary workspace window embeds major feature panels.
- Spotlight → Spotlight UI: hosts · Spotlight window renders chat, search, and trace flows.
- Peek → Preview stack: hosts · Peek window is driven by the preview pipeline.
- Guard → Guard service: drives · Guard UI depends on idle and unlock behavior from the backend.
- Feature views → Context Forge: includes · Context Forge is one of the major feature areas.
- Feature views → Patch tools: includes · Patch workflow views are part of the feature set.
- Feature views → Preview stack: includes · Preview-oriented features share the hyperview stack.
- Feature views → Agent client: includes · Features coordinate with the local agent/tool layer.
- Feature views → Refinery: includes · Clipboard history and cleanup are surfaced in the UI.
- Feature views → Miner: includes · Web mining has its own feature view.
- Feature views → Transfer: includes · LAN transfer is presented as a dedicated feature.
- Feature views → Env probe: includes · System monitoring views present probe results.
- Feature views → Build assets: uses · UI consumes generated prompt packs and model metadata.
- Spotlight UI → Agent client: calls · Chat and trace flows invoke tools through the agent runtime.
- Agent client → Tauri bridge: routes through · Frontend tool calls cross the Tauri bridge.
- Tauri bridge → Context service: exposes · Tauri commands publish context scanning operations.
- Tauri bridge → SQLite DB: exposes · Commands persist and read app data via SQLite.
- Tauri bridge → Tool runtime: exposes · Tool execution is mediated by the backend runtime.
- Tauri bridge → Exec runtime: exposes · Shell execution commands are surfaced through Tauri.
- Tauri bridge → Git service: exposes · Git inspection and export commands are routed here.
- Tauri bridge → Refinery: exposes · Clipboard history and cleanup commands are routed here.
- Tauri bridge → Miner: exposes · Mining search and extraction commands are routed here.
- Tauri bridge → Transfer: exposes · Transfer service lifecycle and messaging are bridged here.
- Tauri bridge → Env probe: exposes · Environment probe data is requested through Tauri commands.
- Context service → SQLite DB: reads/writes · Context and secret data can be stored or referenced in SQLite.
- Git service → SQLite DB: reads/writes · Patch workflows can store related metadata and history.
- Refinery → SQLite DB: reads/writes · Clipboard items, notes, and cleanup state are persisted.
- Miner → SQLite DB: reads/writes · Mining results and queue state are stored locally.
- Transfer → SQLite DB: reads/writes · Device and chat state are persisted locally.
- Env probe → Build assets: uses · Monitoring and OCR-related features depend on bundled assets.
