Architecture (Kernel Perspective)
This page is primarily based on the official
Architecturedocumentation (docs/architecture).
14-crate Cargo Workspace (Downward Layered Dependencies)
OpenFang is organized as a Cargo workspace with 14 crates (13 code + xtask). The dependencies are structured top-down, strictly relying on layers below them.
Kernel Boot Sequence
OpenFangKernel::boot_with_config() (The documentation lists 14 steps, summarizing the core sequence below):
- Read and validate config.toml (
#[serde(default)]ensures forward compatibility). - Initialize data directories.
- Initialize SQLite memory substrate, and execute schema migrations.
- Initialize LLM driver (reading keys from environment variables).
- Initialize model catalog (includes models/providers/aliases).
- Initialize metering engine (cost tracking/billing).
- Initialize model router (routes based on task complexity).
- Assemble core subsystems: registry/scheduler/capability/eventbus/supervisor/workflow/trigger/background executor/wasm sandbox…
- Setup RBAC auth manager.
- Setup skill registry (load bundled + disk-installed skills).
- Setup web tools context (search + SSRF-protected fetch).
- Restore agents from persistent storage.
- Publish
KernelStartedevent. - Return the kernel instance.
Agent Lifecycle (spawn / message / kill)
State Machine
- Running: Can receive messages and tick events.
- Suspended: Paused during situations like daemon shutdown; can be resumed after state persistence.
- Terminated: Killed, final state.
Core Spawn Flow
- Allocate
AgentId/SessionId. - Create session.
- Parse manifest and extract capabilities.
- Validate capability inheritance (preventing sub-agents from privilege escalation).
- Authorize capabilities.
- Register with scheduler (quota management).
- Register with registry.
- Persist to SQLite.
- If there is a parent, update its children.
- Register proactive triggers.
- Publish
Lifecycle::Spawnedevent.
Core Message Flow (Highly Systematized)
- RBAC check (AuthManager).
- Channel policy check.
- Quota check (AgentScheduler).
- Dispatch according to manifest.module: builtin chat / wasm / python subprocess.
- LLM loop: Load session, inject canonical summary, collect available tools, loop guard, session repair, iterate through tool calls, perform auto compaction, write back to storage.
- Metering cost estimation + usage record.
Stability Subsystems (Preventing "Runaway Intelligent Agents")
The documentation outlines multiple hardening layers in the Agent Loop Stability section:
- LoopGuard: Warns, blocks, or circuit-breaks repeated tool calls (SHA-256 based).
- Session repair: Fixes unpaired tool_use/tool_result, merges consecutive roles, etc.
- Tool timeout: Global 60-second timeout.
- Tool output truncation: Upper limit of 50,000 characters for output.
- Max continuations: Limits "please continue" loops.
- Inter-agent depth limit: Restricts recursive calling depth.
- Block-aware compaction: Compresses sessions containing tool/image blocks strategically.
Memory Substrate (SQLite)
Details are available in the "Core Concepts / Memory" section. One key emphasis: OpenFang unifies "sessions, embeddings, knowledge graphs, usage, and canonical sessions" onto a single SQLite schema, handling version migrations seamlessly.