Architecture Overview

ChatrixCD is built with a modular, asynchronous architecture for reliable Matrix bot operation. For detailed technical documentation, implementation details, and design patterns, see the comprehensive ARCHITECTURE.md in the repository root.

System Architecture

┌──────────────────────────────────────────────────────────────┐
│                    ChatrixCD Bot                             │
│                                                              │
│  ┌──────────┐  ┌───────────┐  ┌────────────┐  ┌─────────┐  │
│  │   TUI    │  │   Main    │  │   Config   │  │  Auth   │  │
│  │ (tui.py) │◄─│ (main.py) │─►│(config.py) │─►│(auth.py)│  │
│  └────┬─────┘  └─────┬─────┘  └────────────┘  └────┬────┘  │
│       │              │                              │       │
│       │         ┌────▼─────┐                        │       │
│       │         │   Bot    │◄───────────────────────┘       │
│       └────────►│ (bot.py) │                                │
│                 └────┬─────┘                                │
│                      │                                       │
│              ┌───────┴────────┐                             │
│              │                │                             │
│        ┌─────▼──────┐  ┌──────▼────────┐                   │
│        │  Commands  │  │ Verification  │                   │
│        │(commands.py)│  │(verification.py)                  │
│        └─────┬──────┘  └───────────────┘                   │
│              │                                               │
│        ┌─────▼──────┐  ┌────────────┐                      │
│        │ Semaphore  │  │  Aliases   │                      │
│        │(semaphore.py)  │(aliases.py)│                      │
│        └────────────┘  └────────────┘                      │
└──────────────┬──────────────────────────────────────────────┘
               │
         ┌─────┴──────┐
         │            │
    ┌────▼────┐  ┌────▼──────────┐
    │ Matrix  │  │  Semaphore UI │
    │ Server  │  │   (REST API)  │
    └─────────┘  └───────────────┘

Core Components

Main Entry Point (main.py)

  • Application lifecycle management
  • Signal handling (SIGINT, SIGTERM)
  • Graceful shutdown
  • Error recovery

Configuration (config.py)

  • JSON configuration file parsing
  • Environment variable handling
  • Configuration validation
  • Default value management

Authentication (auth.py)

  • Matrix authentication handling
  • Support for password authentication
  • OIDC/SSO authentication via Matrix native flow
  • Authentication configuration validation

Bot Core (bot.py)

  • Matrix client initialization
  • Room management and sync
  • Event handling and dispatch
  • Message sending
  • E2E encryption support
  • Callback registration

Command Handler (commands.py)

  • Command parsing and routing
  • Project listing
  • Template management
  • Task execution
  • Status monitoring
  • Error handling and user feedback

Semaphore Client (semaphore.py)

  • REST API client for Semaphore UI
  • Async HTTP requests
  • Project and template queries
  • Task lifecycle management
  • Log retrieval

Text User Interface (tui.py)

  • Interactive terminal interface for bot management
  • Built with Textual framework
  • Real-time status monitoring and metrics
  • Device verification workflows (emoji, QR code, fingerprint)
  • Interactive configuration editing
  • Session and room management
  • Log viewing and message sending

Data Flow

Command Execution Flow

  1. User sends message to Matrix room
  2. Bot receives message event
  3. Command parser extracts command and arguments
  4. Command handler validates and executes
  5. Semaphore API call made if needed
  6. Response sent back to Matrix room

Task Monitoring Flow

  1. Task started via !cd run command
  2. Task ID stored for monitoring
  3. Periodic status checks to Semaphore API
  4. Status updates sent to Matrix room
  5. Monitoring stops on task completion/failure

Technology Stack

Core Libraries

  • matrix-nio: Matrix protocol client with E2E encryption and native authentication
  • aiohttp: Async HTTP client for API requests
  • Textual: Terminal UI framework for interactive interface
  • hjson: Human-friendly JSON parser for configuration files

Python Version Support

  • Python 3.12+ (3.12, 3.13, 3.14 supported)
  • Async/await throughout
  • Type hints for better IDE support

Security Architecture

Credential Management

  • No credentials in logs
  • Environment variable support
  • Secure token storage
  • In-memory only sensitive data

Encryption

  • E2E encryption via matrix-nio
  • Encryption keys stored in store/ directory
  • Device verification support
  • Key backup and recovery

Access Control

  • Room-based access control
  • User permission checking
  • Command authorization
  • Rate limiting (future)

Async Architecture

Event Loop

All I/O operations are asynchronous:

  • Matrix client sync
  • HTTP API requests
  • Message handling
  • Task monitoring

Concurrency

  • Single event loop
  • Concurrent task monitoring
  • Non-blocking I/O operations
  • Proper resource cleanup

Storage

Encryption Store

  • Location: store/ directory (configurable)
  • Contents: Encryption keys, device IDs
  • Format: SQLite database (managed by matrix-nio)
  • Security: Restricted file permissions

No Persistent State

  • No database required
  • Stateless command handling
  • Task IDs managed by Semaphore
  • Configuration from files/environment

Error Handling

Graceful Degradation

  • Connection retry logic
  • Timeout handling
  • Partial failure recovery
  • User-friendly error messages

Logging

  • Structured logging
  • Multiple log levels
  • Security-conscious (no secrets)
  • Configurable output

Extensibility

Plugin Architecture (Future)

  • Command plugins
  • Custom task handlers
  • Event processors
  • Integration modules

Configuration Extension

  • Custom command prefixes
  • Multiple Semaphore instances (future)
  • Room-specific settings (future)

Performance Considerations

Resource Usage

  • Low memory footprint
  • Efficient async I/O
  • Minimal CPU usage
  • Scalable to many rooms

Optimization

  • Connection pooling
  • Request batching
  • Efficient sync
  • Minimal polling

Deployment Architecture

Containerized Deployment

  • Docker support (Debian and Alpine)
  • Volume mounts for config and store
  • Health checks
  • Auto-restart policies

Native Deployment

  • systemd service (Linux)
  • OpenRC service (Alpine)
  • Process supervision
  • Log management

Copyright © 2024 ChatrixCD Contributors. Distributed under the GNU GPL v3.0 License.