# Backend / Service Layer ## AI Provider `src/service/provider.rs` (310 lines) - `LlmClient::new(api_key, model, base_url)` — constructs blocking reqwest client - `chat_with_tools()` — non-streaming with tool definitions - `chat_stream()` — SSE streaming, returns `SseParser` yielding `StreamEvent` - Retry logic: up to 3 attempts on transient errors, exponential backoff ## OAuth `src/service/oauth/manager.rs` (113 lines) + `loopback.rs` + `pkce.rs` - PKCE flow: `CodeVerifier` → challenge → browser auth → loopback server → token exchange - Configurable via `app_config.json` provider definitions (auth URL, token URL, scopes) ## IPC / Daemon `src/ipc/` (7 files, ~350 lines total) - Unix domain socket, length-prefixed JSON frames - Daemon sends `DaemonFrame` (state payload, stream tokens, system notes) - Clients send `ClientRequest` (key presses, resize, submit, scroll) - State sync uses full-state push from daemon to client after each action ## Workflow Engine `src/app/workflow/engine.rs` (648 lines) + `script.rs` - Inline JS-style DSL executed by a lightweight runtime - `agent()`, `parallel()`, `pipeline()`, `phase()`, `log()` — spawns sub-agents - Max concurrency configurable via `workflow_max_concurrency` setting - Hive-mind orchestrator in `hive_mind.rs`: Core Intelligence compiles a `CognitiveCyclePlan` per task — cycle count and nodes-per-cycle are decided fresh each time based on what the task actually needs ## Sub-Agent System `src/app/subagent/` (6 files: `spawn.rs`, `engine.rs`, `context.rs`, `event.rs`, `division.rs`, `auto.rs`, ~450 lines total) - `run_subagent()` — spawns independent agent with its own tool set and context - Communicates via `mpsc` channel (tool calls, results, completion) - Uses `LlmClient` (same as main agent) with tool-use API - Auto-healing: on build/test failure, spawns auto-fix sub-agent - Node access tiers (`division.rs`'s `tool_scope` module): `read`, `write`, `full` — granted per node by the Core Intelligence based on what its directive needs ## MCP Client `src/app/mcp/manager.rs` (441+ lines) - Stdio transport: spawns child process, JSON-RPC via stdin/stdout - HTTP transport: streaming HTTP with JSON-RPC - Dynamic tool list refresh and error recovery - Persistent child handle for stdio (reuses connection across calls) ## Self-Review `src/app/review/mod.rs` (495 lines) - Post-tool execution quality check against learned lessons - Invokes `run_subagent()` with reviewer prompt - Staleness detection: skips review after N consecutive empty results - Three review types: code quality, architecture, security ## Background Bash `src/app/bgbash/` (2 files: `job.rs`, `control.rs`) - `spawn_bash_job()` — runs `sh -c` in a thread, collects stdout line-by-line - Channels: output via `mpsc`, PID via `mpsc` - Killable via PID (SIGTERM) - Output buffering capped at 10,000 lines to prevent memory issues ## Gate Guard / Harness `src/app/harness.rs` (495 lines) - `Harness::gate_tool_call()` — verdict-based tool gating (allow/block) - Path traversal, credential read, and destructive command detection - Pattern detection for stub code, denial language, and assumptions in write/edit content - Reason validation for mutating tools (minimum 8 characters, rejects generic non-answers) - Includes 8 unit tests for verdict parsing formats