Skip to main content

Monolex Daemons

Monolex uses standalone Rust daemons for performance-critical background operations. These daemons run as separate processes, providing crash resilience and optimal performance.

Architecture

┌─────────────────────────────────────────────────────────────────────┐
│                    DAEMON ARCHITECTURE                              │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│   Tauri App Process                                                 │
│   ┌─────────────────────────────────────────────────────────────┐   │
│   │  Rust Backend (lib.rs)                                      │   │
│   │     ├── PtyClient    ────────→ pty-daemon-rust              │   │
│   │     ├── WatcherClient ───────→ niia-watcher-daemon          │   │
│   │     └── McpClient    ────────→ niia-mcp                     │   │
│   └─────────────────────────────────────────────────────────────┘   │
│                                                                     │
│   Sidecar Processes (survive app crashes)                           │
│   ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐       │
│   │ pty-daemon-rust │ │ niia-watcher    │ │ niia-mcp        │       │
│   │ 953KB           │ │ 2.75MB          │ │ 2.36MB          │       │
│   │ PTY sessions    │ │ File watching   │ │ MCP server      │       │
│   └─────────────────┘ └─────────────────┘ └─────────────────┘       │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

Available Daemons

PTY Daemon

Manages terminal sessions via Unix sockets. Survives app crashes for session recovery.

NIIA Watcher

File system monitoring with intelligent filtering and event batching.

PTY Log

Terminal session logging and replay capabilities.

Why Separate Daemons?

Crash Resilience

Scenario: App crashes during terminal session

Without daemons:
├── App crashes
├── PTY process dies (child of app)
├── Terminal session lost
└── User loses work ❌

With daemons:
├── App crashes
├── Daemon continues (separate process)
├── PTY session alive
├── App restarts
├── Reconnects to daemon
└── Session restored ✅

Performance Isolation

Each daemon runs in its own process:
  • No GIL contention
  • Independent memory spaces
  • Parallel execution
  • Dedicated CPU scheduling

Native Performance

Pure Rust implementations:
  • Zero JavaScript overhead
  • No Node.js runtime required
  • Minimal memory footprint
  • Fast operations

Communication

Unix Sockets

All daemons communicate via Unix sockets:
/tmp/monolex-pty/
├── daemon-control-{PID}.sock    # PTY control
├── session-{ID}.sock            # Per-session data
└── ...

/tmp/monolex-watcher/
├── watcher-control.sock         # Watcher control
└── ...

Protocol

Length-prefixed binary protocol:
┌─────────────────┬─────────────────────────────────────────────────┐
│ Length (4 bytes)│ Payload (variable)                              │
└─────────────────┴─────────────────────────────────────────────────┘

Binary Management

Build & Deploy

# Build all daemons
npm run build:rust

# Deploy to Tauri binaries folder
npm run deploy:rust

# Sign binaries (required for macOS)
npm run sign:rust

# All in one
npm run rust

Binary Locations

EnvironmentPath
Developmentsrc-tauri/binaries/{name}-{triple}
ProductionMacOS/{name}

Target Triple Suffix

PlatformSuffix
macOS ARMaarch64-apple-darwin
macOS Intelx86_64-apple-darwin
Linuxx86_64-unknown-linux-gnu
Windowsx86_64-pc-windows-msvc.exe

Tauri Configuration

{
  "bundle": {
    "externalBin": [
      "binaries/pty-daemon-rust",
      "binaries/niia-watcher-daemon",
      "binaries/niia-mcp"
    ]
  }
}

Logging

All daemons log to /tmp/:
# PTY daemon
tail -f /tmp/pty-daemon.log

# Watcher daemon
tail -f /tmp/niia-watcher.log

# MCP server
tail -f /tmp/niia-mcp.log

Troubleshooting

Check Running Daemons

ps aux | grep -E "pty-daemon|niia-watcher|niia-mcp"

Kill All Daemons

pkill -f "pty-daemon-rust"
pkill -f "niia-watcher-daemon"
pkill -f "niia-mcp"

Clean Socket Files

rm -rf /tmp/monolex-pty/
rm -rf /tmp/monolex-watcher/

Verify Code Signing (macOS)

codesign -dv src-tauri/binaries/pty-daemon-rust-aarch64-apple-darwin
Important: Unsigned binaries will be killed by macOS (SIGKILL).

Characteristics

DaemonBinary SizeMemoryStartup
pty-daemon-rust953KBLow per sessionFast
niia-watcher2.75MBLowFast
niia-mcp2.36MBLowFast