Architecture
Internal architecture overview for Octomind contributors.
Source Layout
src/
main.rs # CLI entry point (clap)
lib.rs # Library root, spinner macros
directories.rs # Cross-platform directory paths
commands/
mod.rs # Command definitions (run, server, acp, tap, etc.)
common.rs # Shared: config resolution, tap fetching
run.rs # Interactive/non-interactive session
server.rs # WebSocket server
acp.rs # ACP agent mode
config.rs # Config generation/validation
tap.rs / untap.rs # Tap management
vars.rs # Variable display
complete.rs # Shell completion candidates
config/
mod.rs # Config struct, LogLevel, log macros
loading.rs # TOML parsing, multi-file merge, dedup
validation.rs # Config validation (thresholds, hooks, etc.)
roles.rs # Role configuration
mcp.rs # MCP server config (Builtin/Http/Stdio)
oauth_config.rs # OAuth 2.1 + PKCE validation
hooks.rs # Webhook hook config
workflows.rs # Workflow + step definitions
layers.rs # Layer configuration
agents.rs # Agent (ACP) configuration
env_source.rs # .env tracking
registry.rs # Capability resolution
session/
mod.rs # Message types, session entry points
inbox.rs # Unified inbox (schedule, agent, skill, inject, webhook)
inject_listener.rs # Unix socket for message injection
history/ # Session persistence
chat/
mod.rs # Chat orchestration
session/
core.rs # Session state management
commands/ # /help, /info, /report, /model, etc.
response.rs # Response processing, compression checks
conversation_compression.rs # Compression engine
semantic_chunking.rs # Discourse-aware chunking
token_counter.rs # Unified token counting
context_truncation.rs # Context trimming
file_context.rs # Active file tracking
tool_error_tracker.rs # Tool error tracking
cache.rs # Cache marker management
layers/ # Layer execution engine
workflows/ # Workflow orchestrator
output.rs # OutputMode, OutputSink trait
mcp/
mod.rs # MCP coordinator
server.rs # HTTP/stdio server communication
process.rs # Process lifecycle, health, registries
health_monitor.rs # Background health checking
workdir.rs # Thread-local working directory
core/
mod.rs # Core server: plan, mcp, agent, schedule, skill
plan/ # Plan tool + compression
skill.rs # Skill management
agent/
mod.rs # Agent server: agent_* tools
oauth/
discovery.rs # OAuth provider discovery
flow.rs # Authorization flow
callback_server.rs # Local callback HTTP server
device_flow.rs # Device code flow (headless)
token_store.rs # Keyring + file fallback
agent/
taps.rs # Tap management (clone, symlink, list)
registry.rs # Agent discovery across taps
deps.rs # Dependency resolution
inputs.rs # Input/env variable resolution
acp/
mod.rs # ACP agent implementation
agent.rs # ACP session handler
commands.rs # ACP command routing
websocket/
mod.rs # WebSocket server
protocol.rs # Message types (Client/Server)
providers.rs # Provider abstraction (delegates to octolib)
logging/ # Log backends (CLI, file, ACP error sink)
Key Patterns
Global Registries (src/mcp/process.rs)
MCP server processes are managed via global statics:
SERVER_PROCESSES-- active process handlesSERVER_RESTART_INFO-- health, restart count, timestampsSERVER_REF_COUNTS-- reference counting for shared serversSERVER_STDERR-- recent stderr per serverSERVER_CAPABILITIES-- parsed server capabilities
Session Context
CLI_SESSION_CONTEXT-- global (role, project) pairCLI_NOTIFICATION_SENDER-- channel for MCP notifications- Session ID derived from SHA256 of git remote or CWD
Output Abstraction (src/session/output.rs)
Zero-sized types for zero-cost output routing:
SilentSink-- discards (CLI mode)JsonlSink-- JSON Lines to stdoutWebSocketSink-- sends through channel
Error Handling
- Primary:
anyhow::Result<T>everywhere - Validation:
anyhow!()with descriptive messages - MCP tools:
McpToolResult::error()(neverErr()) - ACP: errors logged to JSONL file (stdout reserved for protocol)
Config Loading (src/config/loading.rs)
- Load
config.toml(TOML ->toml::Value) - Load other
*.tomlfiles alphabetically - Deep-merge tables, concatenate arrays of tables
- Deduplicate by
namefield (last wins) - Deserialize to
Configstruct - Validate all fields
Compression Pipeline
- Token monitor checks pressure levels
- Exponential cooldown prevents loops
- Cache-aware economics calculates net benefit
- Decision model (cheap AI) decides whether to compress
- Semantic chunking preserves conversation structure
- Summary replaces compressed content
- Knowledge entries retained across compressions
Dependencies
Key external crates:
octolib-- provider abstraction, LLM integrationrmcp-- MCP protocol implementationagent-client-protocol-- ACP supporttokio-- async runtimeclap-- CLI argument parsingserde/toml-- serializationoauth2-- OAuth 2.1 + PKCEkeyring-- secure token storagehyper-- HTTP serverreedline-- interactive readlinesyntect-- syntax highlighting