Architecture & Design
This document provides a comprehensive technical overview of the Pincer architecture, internal subsystems, concurrency models, and domain modules.
Architectural Philosophy
Pincer is architected strictly around the Single Responsibility Principle (SRP). Every source file is isolated to a focused domain responsibility (~100 lines per module) to ensure maintainability, auditability, and safety.
pincer-engine/
├── src/
│ ├── cli/ # Command-line interface, argument parsing, ANSI UI, daemonizer
│ ├── common/ # Common utilities, error types, URI expansion, sanitization
│ ├── converter/ # Media transcoding subsystem (sips, cupsfilter, ffmpeg, afconvert)
│ ├── engine/ # Core HTTP/FTP chunking, worker streams, disk allocation, throttling
│ ├── manager/ # Task registry, scheduling, persistence, lifecycle, query facade
│ ├── metalink/ # Metalink XML 3.0 / 4.0 file and hash parser
│ ├── models/ # Strongly-typed serde data transfer and domain models
│ ├── protocol/ # Protocol adapters (HTTP/S, FTP, SFTP)
│ ├── resolver/ # URL, Magnet, Torrent, and media scraping resolvers
│ ├── rpc/ # Axum WebSocket server, JSON-RPC 2.0 router, token auth, method handlers
│ └── torrent/ # BitTorrent integration engine (librqbit session, stats, file selector)
Core Subsystems
Protocol Adapter Layer (src/protocol/)
Provides uniform stream abstractions via the ProtocolAdapter trait:
HttpAdapter: Asynchronous byte-range streaming and metadata discovery viareqwest.FtpAdapter: FTP/FTPS segmented streaming usingsuppaftp.SftpAdapter: SSH/SFTP streaming and metadata discovery usingrusshandrussh-sftp.
Core Execution Engine (src/engine/)
DiskAllocator: Zero-fragmentation space pre-allocation (set_len).DownloadBundle: macOS.downloadstaging bundle structure,Info.plistUTI assignment, andcom.apple.quarantinehandling.RangeChunker: Parallel byte-range partitioner and resume offset arithmetic.DownloadWorker: Non-blocking zero-allocation disk writes using POSIXwrite_aton anArc<File>across concurrent threads without lock contention.RateThrottler: Proportional bandwidth allocation using theThreadGuardRAII active thread counter.
BitTorrent Integration Engine (src/torrent/)
TorrentSessionManager: Configureslibrqbit::Sessionwith port range6881..6891and automatic ephemeral port fallback (0..1).TorrentTaskSpawner: Inspects Magnet and Torrent byte sources and prepares isolated directory trees.TorrentStatsTracker: Computes instantaneous download/upload speeds, seeders, and peer snapshots.TorrentFileSelector: Cleans up unselected files and prunes empty directory branches.
Universal Media & Metadata Resolver (src/resolver/)
TorrentResolver: List-only metadata extraction for Magnet links and Base64-encoded.torrentfiles.DirectHttpResolver: Content-Type, Content-Length, Content-Disposition, and Accept-Ranges resolution.HtmlScraper&ScriptExtractor: OpenGraph/Twitter card extraction, Next.js embedded JSON scraping, and direct video stream detection.
Media Transcoder (src/converter/)
ImageSips: macOSsipsimage conversion (jpeg,png,webp,heic).CupsPdf: macOScupsfilterraster-to-PDF conversion.Ffmpeg: Universal media transcoding.Afconvert: macOSafconvertaudio encoding.- Includes rollback restoration that re-establishes original files if conversion fails.
Central Manager (src/manager/)
TaskSpawner&TaskRunner: Coordinates task lifecycle, execution, SHA256 integrity verification, and quarantine removal.TaskLifecycleManager: Safe pause/unpause state machine transitions.TaskRemovalManager: Atomic task deletion and non-blocking background Trash operations (trash::delete).SessionPersistence: Session serialization to~/.pincer/pincer.sessionand.download/state.json.
JSON-RPC 2.0 Layer (src/rpc/)
- Axum WebSocket server at
ws://127.0.0.1:6842/jsonrpc. - Native
pin.*andsystem.*namespace dispatch. - Per-connection authentication with
token:<secret>support. - Real-time broadcast notification channel (
pin.onDownloadStart,pin.onDownloadProgress,pin.onDownloadComplete, etc.).
Concurrency & Disk I/O Model
┌─────────────────────────┐
│ DownloadManager │
└────────────┬────────────┘
│
┌─────────────────────┼─────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ Worker 1 │ │ Worker 2 │ │ Worker N │
│ (Range 0..A) │ │ (Range A..B) │ │ (Range B..End)│
└───────┬───────┘ └───────┬───────┘ └───────┬───────┘
│ │ │
└─────────────────────┼─────────────────────┘
▼
┌───────────────────────┐
│ Shared Arc<File> │
│ (write_at POSIX I/O) │
└───────────────────────┘
│
▼
┌───────────────────────┐
│ Staging .download │
│ Bundle (with UTI) │
└───────────────────────┘