Skip to the content.

TorrAi architecture (EN)

🌐 Language / Язык: English · Русский · Short overview · Detailed guide · Rules R1–R33

This document describes the major components, data flows and trust boundaries. The diagrams use Mermaid and render on GitHub without extra plugins.

1. System components

flowchart TB
    subgraph Clients["Clients"]
        USER["User"]
        EXT["External OpenAI client"]
    end

    subgraph UI["UI and API"]
        QT["PySide6 UI<br/>Torrents · Chat · Statistics · Settings"]
        API["Local API<br/>/v1/models · /v1/chat/completions · /v1/status"]
    end

    subgraph APP["ApplicationService — orchestrator"]
        ORCH["Catalog · settings · SQLite"]
        GW["ChatGateway<br/>sessions · streaming · queue"]
        PLAN["Layer planner<br/>R7 · R8"]
        RT["RuntimeManager<br/>demo · openai · llama-server · distributed"]
    end

    subgraph TRUST["Trust"]
        SIGN["Ed25519 signatures<br/>SHA-256 · quarantine"]
        REP["Reputation · credits<br/>R12 · R20–R33"]
    end

    subgraph STORAGE["Storage"]
        ST["StorageManager<br/>sparse · quotas · hardlink"]
        GGUF["GGUF v3<br/>parser and kernel"]
    end

    subgraph SWARM["Swarm"]
        LT["libtorrent / simulation"]
        WS["Web-seeds · BEP19"]
        DHT["DHT BEP44 · LAN multicast"]
    end

    subgraph NODES["Layer nodes"]
        LNS["LayerNodeServer<br/>TLS + HMAC + SHA-256"]
        RLC["RemoteLayerClient"]
    end

    subgraph EXTNET["External systems"]
        PEERS["Peers and trackers"]
        DHTNET["Public DHT"]
        HTTP["HTTP(S) web-seeds"]
        LLM["llama-server / Ollama / OpenAI"]
    end

    USER --> QT
    EXT --> API
    QT --> ORCH
    API --> GW
    ORCH --> GW
    ORCH --> ST
    ORCH --> SIGN
    ORCH --> LT
    ORCH --> DHT
    GW --> PLAN
    GW --> RT
    GW --> REP
    PLAN --> RLC
    RT --> LLM
    RLC --> LNS
    LT --> PEERS
    WS --> HTTP
    DHT --> DHTNET
    LT --> ST
    WS --> ST
    ST --> GGUF
    LNS --> GGUF
    SIGN --> ST

Key responsibilities

Component Responsibility Main files
UI onboarding, window, release table, details (including RAM + HDD), chat, settings, statistics, rules torr_ai/ui/*
ApplicationService wires subsystems, catalog, settings, SQLite, background jobs torr_ai/app/service.py
SwarmService download/seeding, download queue, priorities, seeding torr_ai/swarm/service.py
ChatGateway sessions, streaming, cancellation, generation queue, metrics torr_ai/inference/gateway.py
Planner critical layers, rarest-first, local/remote route, ping/queue/load/memory cost torr_ai/inference/planner.py
Orchestrator Clone telemetry, replication target, seed selection, R16 reroute, observation snapshot torr_ai/inference/orchestrator.py
Trust signatures, hashes, verdicts, reputation, credits, R12/R20–R33 torr_ai/trust/*
Storage quotas, sparse files, hardlink seeding, GGUF torr_ai/storage/*
Swarm libtorrent/simulation, DHT, web-seeds, LAN discovery torr_ai/swarm/*
Layer nodes node publish/lookup, layer RPC, KV, TLS+HMAC torr_ai/inference/node.py, torr_ai/swarm/dht.py
Local API OpenAI-compatible endpoints torr_ai/inference/api.py

2. Release lifecycle

sequenceDiagram
    autonumber
    actor U as User
    participant APP as ApplicationService
    participant TR as Trust
    participant SW as Swarm
    participant ST as Storage

    U->>APP: Add .torrent / magnet / bundle
    APP->>TR: Verify manifest signature
    TR-->>APP: Verdict R1/R2
    APP->>ST: Allocate sparse file, check quota R5
    APP->>SW: Enqueue download R6/R7
    SW->>ST: Write pieces and verify SHA-256/SHA-1
    U->>APP: "Recheck"
    APP->>TR: Compare available pieces with signed hashes
    TR-->>APP: Ready R1/R6
    APP->>ST: Create hardlink seeding view
    APP-->>U: Ready to run R13 and publish

Order matters: the network is never the source of truth. Pieces are accepted only after checking against the signed manifest, and seeding/publishing happens only after a full check.

2.1. Logical torrent-layer orchestrator

LayerOrchestrator observes clone catalog for every layer: which nodes keep a layer on disk, which have it warm in RAM/VRAM, clone ping/queue/load, and how many live copies exist in the swarm. It:

Key settings: max_hop_latency_ms, route_ping_budget_ms, route_transition_ms, queue_wait_ms, load_penalty_ms, cold_layer_penalty_ms, incomplete_model_penalty_ms, bandwidth_bonus_ms, reference_bandwidth_mbps, seed_usage_penalty_ms, hot_layer_demand_threshold, hot_layer_load_threshold, prefer_local_ping_ms, layer_replication_target, layer_replication_max_target, auto_replicate_layers, replication_interval_s, orchestrator_interval_s, ping_probe_timeout_ms, hybrid_network.

2.2. Onboarding, RAM + HDD and token counters

Before service.start() the UI shows WelcomeDialog when onboarding_accepted is absent. The dialog blocks continuation without consent; accepting it stores onboarding_accepted, hybrid_network, remote_nodes_allowed, discovery_enabled, layer_node_dht and webseeds_enabled, then the engine starts. There is no manual LAN/Internet choice: network_mode in ApplicationService always reports the hybrid mode, while sources and the R16 route are selected automatically.

The “RAM + HDD” tab uses model_completeness() and local_ready_layers(): the first bar covers verified layers on HDD, the second covers layers loaded into RAM/VRAM, and “Ready OK” requires a full check, runtime_loaded and matching warm/local counts. token_counters() combines RemoteLayerClient.requested_tokens (forward RPC to remote layers) and LayerNodeServer.telemetry().served_tokens (forward RPC served by your node); the values appear in the status bar, orchestrator summary, chat, statistics and heal window. Node telemetry reports bandwidth_mbps in Mbit/s and the UI converts it to bytes/s.

3. Inference path

sequenceDiagram
    autonumber
    participant C as Chat / API
    participant G as ChatGateway
    participant P as Planner
    participant L as Local layers
    participant R as Remote node
    participant K as Owner KV cache

    C->>G: prompt + priority
    G->>P: build_plan()
    P-->>G: local/remote + route
    G->>L: forward local layers
    L->>K: read and update KV
    G->>R: POST /v1/forward (TLS + HMAC + voucher)
    R->>K: KV for the layer range
    R-->>G: activations + KV + signature
    G->>G: verify SHA-256 R1/R9
    G-->>C: SSE chunks · Stop · metrics

4. Module map

flowchart LR
    subgraph core["core"]
        MODELS["models.py<br/>Manifest · LayerInfo · Settings"]
        RULES["rules.py<br/>R1–R19"]
        EVENTS["events.py<br/>EventBus"]
        AUTO["autoconfig.py"]
        MULTI["multimodal.py"]
        PATHS["paths.py · pieces.py"]
    end

    subgraph app["app"]
        SERVICE["service.py"]
        STATE["state.py"]
        CATALOG["catalog.py"]
        SETTINGS["settings_store.py"]
    end

    subgraph swarm["swarm"]
        BACKEND["backend.py"]
        SERVICE2["service.py"]
        BEP19["bep19.py"]
        WEBSEED["webseed.py"]
        DISCOVERY["discovery.py"]
        DHT["dht.py"]
    end

    subgraph inference["inference"]
        ADAPTERS["adapters.py"]
        GATEWAY["gateway.py"]
        PLANNER["planner.py"]
        NODE["node.py"]
        GGUF["gguf.py"]
        API["api.py"]
        SCHED["scheduler.py"]
        TLS["tls.py"]
    end

    subgraph trust["trust"]
        SIGNING["signing.py"]
        REPUTATION["reputation.py"]
        CREDITS["credits.py"]
        OBSERVER["observer_key.py"]
    end

    subgraph storage["storage"]
        CACHE["cache.py"]
    end

    subgraph pack["pack"]
        BUNDLE["bundle.py"]
        FETCH["fetch.py"]
    end

    SERVICE --> STATE
    SERVICE --> CATALOG
    SERVICE --> SETTINGS
    SERVICE --> SERVICE2
    SERVICE --> DISCOVERY
    SERVICE --> DHT
    SERVICE --> GATEWAY
    SERVICE --> NODE
    SERVICE --> CACHE
    SERVICE --> SIGNING
    SERVICE --> BUNDLE
    GATEWAY --> ADAPTERS
    GATEWAY --> PLANNER
    GATEWAY --> SCHED
    GATEWAY --> STATE
    PLANNER --> RULES
    NODE --> SIGNING
    NODE --> REPUTATION
    NODE --> CREDITS
    NODE --> GGUF
    SERVICE2 --> BACKEND
    SERVICE2 --> BEP19
    BACKEND --> WEBSEED
    DHT --> DISCOVERY
    BUNDLE --> FETCH
    SIGNING --> MODELS
    RULES --> MODELS

5. Threads and storage

6. Trust boundaries

flowchart LR
    MANIFEST["Signed manifest<br/>Ed25519 + SHA-256"] --> VERIFY["Verify R1/R2/R4"]
    VERIFY --> QUARANTINE["Quarantine<br/>before verification"]
    VERIFY --> CACHE["Working cache"]
    CACHE --> SEED["Seeding R6"]
    CACHE --> RUN["Run R8/R13"]
    RUN --> RPC["Remote RPC<br/>TLS + HMAC R9"]
    RPC --> HASH["Layer SHA-256 check R1"]
    HASH --> GEN["Generation R14–R16"]
    REP["Reputation and credits<br/>R12/R20–R33"] --> RPC
    REP --> SEED

No network response reaches the working cache without a hash and signature check. Reputation and credits influence node selection but never replace cryptographic verification.