# mule-ai/ranch architecture

> Architecture diagram of the GitHub repository mule-ai/ranch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mule-ai/ranch
- Repository: https://github.com/mule-ai/ranch
- Video tour (about a minute): https://gitdiagram.com/mule-ai/ranch/video

## Overview

Ranch is a persistent, daemon-owned terminal multiplexer with local CLI/TUI, browser, and phone clients. The main workflow is to select or create a machine session, attach to its live panes, and reconnect later; panes may host shells, agents, or workflow runs. The daemon also coordinates file operations, triggers, webhook events, and cloud relay. The README documents several subsystems whose source was not sampled, so their nodes use supported enclosing paths and only documented wiring is shown.

Main entry points: [`crates/ranch/src/daemon.rs`](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs) (Session daemon), [`crates/ranch/src/relay.rs`](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/relay.rs) (Cloud relay), [`crates/ranch/src/client.rs`](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/client.rs) (CLI and TUI), [`web/src/pages/WebApp.tsx`](https://github.com/mule-ai/ranch/blob/main/web/src/pages/WebApp.tsx) (Web client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["User clients"]
  node_cli["CLI and TUI<br/>[client.rs]"]
  node_web["Web client<br/>[WebApp.tsx]"]
  node_mobile["Mobile client<br/>[App.tsx]"]
end

subgraph group_daemon["Daemon runtime"]
  node_daemon_main["Daemon entry<br/>[main.rs]"]
  node_daemon_runtime["Session daemon<br/>[daemon.rs]"]
  node_state[("Session state<br/>[daemon.rs]")]
  node_files["File editing<br/>[daemon.rs]"]
end

subgraph group_automation["Agents and automation"]
  node_forge["Forge agents<br/>[forge.rs]"]
  node_pi["Local pi agents<br/>[pilocal.rs]"]
  node_agent_tools["Agent pane tools<br/>[agenttools.rs]"]
  node_mule["Mule workflows<br/>[mule.rs]"]
  node_triggers["Workflow triggers<br/>[triggers.rs]"]
  node_webhook["Webhook receiver<br/>[index.ts]"]
end

subgraph group_cloud["Identity and relay"]
  node_relay["Cloud relay<br/>[relay.rs]"]
  node_supabase[("Supabase services<br/>[supabase.ts]")]
end

subgraph group_terminal["Terminal core"]
  node_protocol["Frame protocol<br/>[lib.rs]"]
  node_pty["PTY panes<br/>[daemon.rs]"]
  node_vt["Terminal emulator<br/>[lib.rs]"]
end

node_human(("Human user"))
node_forge_service["Forge runtime"]
node_pi_runtime["Pi runtime"]
node_mule_service["Mule service"]
node_external(("External webhook senders"))

node_human -->|"uses"| node_cli
node_human -->|"uses"| node_web
node_human -->|"uses"| node_mobile
node_cli -->|"connects to"| node_daemon_main
node_daemon_main -->|"dispatches"| node_daemon_runtime
node_cli -->|"exchanges frames"| node_protocol
node_web -->|"exchanges frames"| node_relay
node_mobile -->|"exchanges frames"| node_relay
node_daemon_runtime -->|"exchanges frames"| node_protocol
node_daemon_runtime -->|"persists state"| node_state
node_daemon_runtime -->|"manages panes"| node_pty
node_pty -->|"feeds terminal output"| node_vt
node_daemon_runtime -->|"handles file operations"| node_files
node_daemon_runtime -->|"starts and streams"| node_forge
node_forge -->|"uses runtime"| node_forge_service
node_daemon_runtime -->|"starts and controls"| node_pi
node_pi -->|"drives"| node_pi_runtime
node_daemon_runtime -->|"runs workflows"| node_mule
node_mule -->|"calls service"| node_mule_service
node_daemon_runtime -->|"schedules and dispatches"| node_triggers
node_daemon_runtime -->|"serves control API"| node_agent_tools
node_agent_tools -->|"controls panes"| node_daemon_runtime
node_external -->|"posts events"| node_webhook
node_webhook -.->|"routes events"| node_relay
node_relay -->|"uses realtime"| node_supabase
node_daemon_runtime -->|"publishes frames"| node_relay
node_relay -->|"delivers frames"| node_daemon_runtime
node_web -->|"authenticates"| node_supabase
node_mobile -->|"authenticates"| node_supabase

click node_cli "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/client.rs"
click node_web "https://github.com/mule-ai/ranch/blob/main/web/src/pages/WebApp.tsx"
click node_mobile "https://github.com/mule-ai/ranch/blob/main/mobile/App.tsx"
click node_protocol "https://github.com/mule-ai/ranch/blob/main/crates/ranch-protocol/src/lib.rs"
click node_daemon_main "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/main.rs"
click node_daemon_runtime "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs"
click node_state "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs"
click node_pty "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs"
click node_vt "https://github.com/mule-ai/ranch/blob/main/crates/ranch-vt/src/lib.rs"
click node_files "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs"
click node_forge "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/forge.rs"
click node_pi "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/pilocal.rs"
click node_agent_tools "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/agenttools.rs"
click node_mule "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/mule.rs"
click node_triggers "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/triggers.rs"
click node_webhook "https://github.com/mule-ai/ranch/blob/main/supabase/functions/webhook-relay/index.ts"
click node_relay "https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/relay.rs"
click node_supabase "https://github.com/mule-ai/ranch/blob/main/mobile/lib/supabase.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_web,node_mobile,node_human toneBlue
class node_daemon_main,node_daemon_runtime,node_state,node_files toneAmber
class node_forge,node_pi,node_agent_tools,node_mule,node_triggers,node_webhook,node_external toneMint
class node_relay,node_supabase toneRose
class node_protocol,node_pty,node_vt,node_forge_service,node_pi_runtime,node_mule_service toneIndigo
```

## Components

### User clients

- [CLI and TUI](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/client.rs): `crates/ranch/src/client.rs`
- [Web client](https://github.com/mule-ai/ranch/blob/main/web/src/pages/WebApp.tsx): `web/src/pages/WebApp.tsx`
- [Mobile client](https://github.com/mule-ai/ranch/blob/main/mobile/App.tsx): `mobile/App.tsx`

### Daemon runtime

- [Daemon entry](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/main.rs): `crates/ranch/src/main.rs`
- [Session daemon](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs): `crates/ranch/src/daemon.rs`
- [Session state](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs): `crates/ranch/src/daemon.rs`
- [File editing](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs): `crates/ranch/src/daemon.rs`

### Agents and automation

- [Forge agents](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/forge.rs): `crates/ranch/src/forge.rs`
- [Local pi agents](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/pilocal.rs): `crates/ranch/src/pilocal.rs`
- [Agent pane tools](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/agenttools.rs): `crates/ranch/src/agenttools.rs`
- [Mule workflows](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/mule.rs): `crates/ranch/src/mule.rs`
- [Workflow triggers](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/triggers.rs): `crates/ranch/src/triggers.rs`
- [Webhook receiver](https://github.com/mule-ai/ranch/blob/main/supabase/functions/webhook-relay/index.ts): `supabase/functions/webhook-relay/index.ts`

### Identity and relay

- [Cloud relay](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/relay.rs): `crates/ranch/src/relay.rs`
- [Supabase services](https://github.com/mule-ai/ranch/blob/main/mobile/lib/supabase.ts): `mobile/lib/supabase.ts`

### Terminal core

- [Frame protocol](https://github.com/mule-ai/ranch/blob/main/crates/ranch-protocol/src/lib.rs): `crates/ranch-protocol/src/lib.rs`
- [PTY panes](https://github.com/mule-ai/ranch/blob/main/crates/ranch/src/daemon.rs): `crates/ranch/src/daemon.rs`
- [Terminal emulator](https://github.com/mule-ai/ranch/blob/main/crates/ranch-vt/src/lib.rs): `crates/ranch-vt/src/lib.rs`

### Other components

- **Human user**
- **Forge runtime**
- **Pi runtime**
- **Mule service**
- **External webhook senders**

## Connections

- Human user → CLI and TUI: uses
- Human user → Web client: uses
- Human user → Mobile client: uses
- CLI and TUI → Daemon entry: connects to
- Daemon entry → Session daemon: dispatches
- CLI and TUI → Frame protocol: exchanges frames
- Web client → Cloud relay: exchanges frames
- Mobile client → Cloud relay: exchanges frames
- Session daemon → Frame protocol: exchanges frames
- Session daemon → Session state: persists state
- Session daemon → PTY panes: manages panes
- PTY panes → Terminal emulator: feeds terminal output
- Session daemon → File editing: handles file operations
- Session daemon → Forge agents: starts and streams
- Forge agents → Forge runtime: uses runtime
- Session daemon → Local pi agents: starts and controls
- Local pi agents → Pi runtime: drives
- Session daemon → Mule workflows: runs workflows
- Mule workflows → Mule service: calls service
- Session daemon → Workflow triggers: schedules and dispatches
- Session daemon → Agent pane tools: serves control API
- Agent pane tools → Session daemon: controls panes
- External webhook senders → Webhook receiver: posts events
- Webhook receiver → Cloud relay: routes events
- Cloud relay → Supabase services: uses realtime
- Session daemon → Cloud relay: publishes frames
- Cloud relay → Session daemon: delivers frames
- Web client → Supabase services: authenticates
- Mobile client → Supabase services: authenticates
