2026-07-12 11:10:16 +07:00
<!-- Generated: 2026-07-12 | Files scanned: 124 | Token estimate: ~850 -->
# Backend / Service Layer
## AI Provider
2026-07-13 14:35:42 +07:00
`src/service/provider.rs` (310 lines)
2026-07-12 11:10:16 +07:00
- `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
2026-07-13 14:35:42 +07:00
`src/ipc/` (7 files, ~350 lines total)
2026-07-12 11:10:16 +07:00
- Unix domain socket, length-prefixed JSON frames
2026-07-13 14:35:42 +07:00
- 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
2026-07-12 11:10:16 +07:00
## Workflow Engine
2026-07-13 14:35:42 +07:00
`src/app/workflow/engine.rs` (648 lines) + `script.rs`
2026-07-12 11:10:16 +07:00
- 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
2026-07-14 08:12:37 +07:00
- 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
2026-07-12 11:10:16 +07:00
## Sub-Agent System
2026-07-13 14:35:42 +07:00
`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
2026-07-12 11:10:16 +07:00
- Communicates via `mpsc<SubagentEvent>` channel (tool calls, results, completion)
- Uses `LlmClient` (same as main agent) with tool-use API
2026-07-13 14:35:42 +07:00
- Auto-healing: on build/test failure, spawns auto-fix sub-agent
2026-07-14 08:12:37 +07:00
- 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
2026-07-12 11:10:16 +07:00
## MCP Client
2026-07-13 14:35:42 +07:00
`src/app/mcp/manager.rs` (441+ lines)
2026-07-12 11:10:16 +07:00
- Stdio transport: spawns child process, JSON-RPC via stdin/stdout
- HTTP transport: streaming HTTP with JSON-RPC
2026-07-13 14:35:42 +07:00
- Dynamic tool list refresh and error recovery
2026-07-12 11:10:16 +07:00
- Persistent child handle for stdio (reuses connection across calls)
## Self-Review
2026-07-13 14:35:42 +07:00
`src/app/review/mod.rs` (495 lines)
2026-07-12 11:10:16 +07:00
- Post-tool execution quality check against learned lessons
- Invokes `run_subagent()` with reviewer prompt
- Staleness detection: skips review after N consecutive empty results
2026-07-13 14:35:42 +07:00
- Three review types: code quality, architecture, security
2026-07-12 11:10:16 +07:00
## Background Bash
2026-07-13 14:35:42 +07:00
`src/app/bgbash/` (2 files: `job.rs` , `control.rs` )
2026-07-12 11:10:16 +07:00
- `spawn_bash_job()` — runs `sh -c` in a thread, collects stdout line-by-line
- Channels: output via `mpsc<String>` , PID via `mpsc<u32>`
2026-07-13 14:35:42 +07:00
- Killable via PID (SIGTERM)
- Output buffering capped at 10,000 lines to prevent memory issues
2026-07-12 11:10:16 +07:00
## Gate Guard / Harness
2026-07-13 14:35:42 +07:00
`src/app/harness.rs` (495 lines)
2026-07-12 11:10:16 +07:00
- `Harness::gate_tool_call()` — verdict-based tool gating (allow/block)
2026-07-13 14:35:42 +07:00
- 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