# Backend Architecture ## Provider Layer The provider abstraction in `dto/provider/` and `service/provider.rs` wraps LLM API calls: - **Configuration**: `model/app_config.rs` loads Anthropic/OpenAI-compatible endpoint settings - **Authentication**: `service/oauth/` handles OAuth 2.0 with PKCE flow and token management - **Requests**: `dto/provider/request.rs` builds provider-agnostic request structs - **Responses**: `dto/provider/response.rs` parses streaming and non-streaming responses - **Token tracking**: `dto/provider/usage.rs` tracks token consumption ## IPC (Inter-Process Communication) The daemon-client protocol in `src/ipc/`: - **Transport**: Unix domain sockets - **Framing**: Length-prefixed frames with `serde_json` serialization (`ipc/frame.rs`) - **State Sync**: Full state push from daemon after each action (`ipc/snapshot.rs`); diff-based updates for efficiency (`ipc/diff.rs`) - **Protocol**: `ipc/protocol.rs` defines message types (Action, StateSnapshot, etc.) Flow: ``` Client ──Action──▶ Daemon ──apply_action()──▶ State mutated │ └──StatePayload──▶ Client (render) ``` ## Workflow Engine Located in `src/app/workflow/`: - **Script DSL** (`engine.rs`): Executes the workflow script language (agent/parallel/pipeline/phase). Supports subagent spawning with schema-validated output, concurrency limiting, and budget tracking. - **Hive Mind** (`hive_mind.rs`): Core Intelligence spawns a CognitiveCyclePlan — ordered cycles of parallel processing nodes. Each node has a directive and access tier (`read`/`write`/`full`). Node outputs merge into a shared collective state in real time. Final consensus synthesis completes the convergence. - **Docs** (`docs.rs`): Deterministic (not LLM) convergence writer — records every node's output + final consensus to `docs/runs/`. ## MCP (Model Context Protocol) `src/app/mcp/manager.rs` manages MCP client connections: - Uses the `rmcp` crate for the MCP protocol - Supports stdio-based transport (child process) and streamable HTTP - Tool discovery via `list_tools()` and dynamic tool registration ## LSP Integration `src/app/lsp/` provides Language Server Protocol support: - **Auto-provisioner** (`provisioner.rs`): Detects and starts LSP servers for Rust, TypeScript, Python, Go, and other languages - **Client** (`client.rs`): JSON-RPC-based LSP client with typed notifications - **Tools** (`tool/lsp/mod.rs`): 7 LSP tools (connect, hover, completion, definition, references, diagnostics, disconnect) ## Background Bash `src/app/bgbash/` manages long-running shell jobs: - **Control** (`control.rs`): Job lifecycle management (spawn, signal, terminate) using Unix process groups - **Job** (`job.rs`): Individual job state tracking with output buffering and progress monitoring ## Review System `src/app/subagent/auto.rs` spawns background reviews: - Quick review after every edit - Background test generation - Architecture review - Security review - All retry once on failure, escalate to blocking error if retry also fails