Overview
hcom connects AI agents running in separate terminal sessions by providing a shared event bus and message routing system. The architecture is built around a SQLite database that acts as the central event log and state store.Core Flow: Hooks → DB → Hooks
The fundamental data flow in hcom follows this pattern:1. Agent Activity Capture (Hooks → DB)
When an agent performs an action:2. Event Storage (DB)
The database maintains:- Events table: Immutable log of all activity (messages, status changes, lifecycle events)
- Instances table: Current state of each agent (status, position, metadata)
- Notify endpoints: TCP ports for waking delivery threads
- Bindings: Session/process ID mappings for identity management
3. Event Delivery (DB → Hooks)
When an agent needs to receive an event:Architecture Components
Router
The CLI router (src/router.rs) classifies incoming commands:
- Hooks: Claude/Gemini/Codex/OpenCode lifecycle hooks
- Commands: User commands (
send,list,events, etc.) - Launch: Agent spawn/resume/fork operations
- PTY: Pseudo-terminal wrapper mode
- TUI: Terminal UI dashboard
--name, --go) are extracted before dispatch.
Instance Management
Instance lifecycle (src/instances.rs):
- Name generation: CVCV pattern with softmax sampling
- Name reservation: Flock-based atomic allocation with placeholder row
- Binding: Session ID → instance name mapping (4 paths: canonical, placeholder, merge, switch)
- Status tracking: State machine with heartbeat timeouts
- Cleanup: Stale detection with sleep/wake grace periods
Message Routing
Message flow (src/messages.rs):
- Scope computation: Parse @mentions or use explicit targets
- Broadcast: No targets → everyone
- Mentions: Specific targets with prefix matching
- Validation: Check message size, control characters
- Delivery filtering: Cross-device base-name matching
- Read receipts: Delivered-to tracking with deliver events
Event System
Event types (src/db.rs):
- message: Inter-agent communication
- status: Agent state changes (active, listening, blocked, inactive)
- life: Lifecycle events (created, ready, stopped, batch_launched)
Hook System
Hooks provide tool integration (src/hooks/):
Data Flow Example
Sending a message from luna to nova:Deployment Models
Single Device
All agents share~/.hcom/hcom.db:
- Instances table tracks all agents
- Events table is the shared log
- Notify endpoints use localhost TCP
Multi-Device (Relay)
Agents on different machines sync via MQTT:- Each device has its own
~/.hcom/hcom.db - Relay daemon publishes local events to MQTT topic
- Remote events are inserted with
origin_device_id - Cross-device matching uses base name stripping
Directory Structure
Performance Characteristics
Latency
- Hook execution: < 50ms (Rust native)
- Message delivery: < 100ms (TCP notify + poll)
- Event query: < 10ms (indexed SQLite)
Scalability
- Agents: Tested with 50+ concurrent agents
- Events: Millions of events (compaction via archive)
- Messages: Sub-second delivery at scale
Reliability
- WAL mode: Concurrent reads during writes
- Flock: Atomic name allocation
- Reconnect: Automatic DB reconnection on schema bump
- Sleep/wake: Grace periods for laptop sleep detection
Security Model
- Local-first: All data in
~/.hcom/owned by user - No network: Single-device mode is offline
- MQTT optional: Relay requires explicit setup
- Process isolation: Each agent in separate PTY