zeph_core/lib.rs
1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4// tracing::instrument on large async fns in zeph-agent-context increases the
5// Future type depth beyond the default limit of 128.
6#![recursion_limit = "256"]
7
8//! Zeph core agent: multi-model inference, semantic memory, skills orchestration, and tool execution.
9//!
10//! This crate provides the [`Agent`] struct — the autonomous AI system at the heart of Zeph.
11//! It integrates LLM providers (Claude, `OpenAI`, Ollama, Candle), semantic memory (Qdrant),
12//! skill registry and matching, tool execution (shell, web, custom), MCP client support, and
13//! security/compliance subsystems into a single composable agent framework.
14//!
15//! # Usage
16//!
17//! The main entry point is [`Agent::new`] or [`Agent::new_with_registry_arc`]. After creating
18//! an agent, call [`Agent::run`] to execute the main loop.
19//! Always call [`Agent::shutdown`] before dropping to persist state.
20//!
21//! See the `bootstrap` module in the `zeph` binary crate for config loading and provider setup examples.
22//!
23//! # Key Components
24//!
25//! - [`Agent`] — Main struct that runs the agent loop
26//! - [`Channel`] — Abstraction for user interaction (send/receive messages and events)
27//! - [`channel::ChannelMessage`] — Structured messages flowing to/from the user
28//! - `config` — Configuration schema (LLM providers, memory, skills, etc.)
29//! - `agent::session_config` — Per-session configuration (budget, timeouts, etc.)
30//! - `agent::context` — Context assembly and token budgeting utilities
31//! - [`pipeline`] — Structured execution pipelines for complex workflows
32//! - [`project`] — Project indexing and semantic retrieval
33//! - [`memory_tools`] — Memory search and management utilities
34//!
35//! Note: The `bootstrap` module (`AppBuilder`, provider setup, etc.) lives in the `zeph` binary crate.
36//!
37//! # Architecture
38//!
39//! The agent operates as a **single-turn finite state machine** that processes each user
40//! message through a series of stages:
41//!
42//! 1. **Input** — Receive user message via channel
43//! 2. **Context assembly** — Build prompt from conversation history, memory, and skills
44//! 3. **LLM inference** — Call the model with multi-tool calling support
45//! 4. **Tool execution** — Run tool calls concurrently with streaming output
46//! 5. **Feedback loop** — Feed tool results back to LLM for synthesis
47//! 6. **Output** — Send agent response via channel
48//! 7. **Persistence** — Save messages and state (async, deferred)
49//!
50//! All async operations (`await` points) are bounded with timeouts to prevent stalls.
51//!
52//! # Channel Contract
53//!
54//! Implementing the [`Channel`] trait allows the agent to integrate with any I/O system:
55//!
56//! - **CLI** — `cargo run -- --config config.toml`
57//! - **Telegram** — Bot interface with streaming updates
58//! - **TUI** — Multi-panel dashboard with real-time metrics
59//! - **HTTP gateway** — Webhook ingestion and agent event streaming
60//! - **Custom** — Implement [`Channel`] for domain-specific systems
61//!
62//! # Feature Flags
63//!
64//! - `candle` — Local inference via Candle (default off, requires CUDA/Metal)
65//! - `classifiers` — ML-based content classification and trust scoring
66//! - `metal` — Candle with Metal acceleration (macOS)
67//! - `cuda` — Candle with CUDA acceleration (Linux/Windows)
68//! - `scheduler` — Cron-based periodic task scheduler
69
70pub mod agent;
71#[cfg(feature = "profiling-alloc")]
72pub mod alloc_layer;
73pub mod anchor_store;
74pub mod channel;
75pub mod config;
76pub mod config_watcher;
77pub mod context;
78pub mod cost;
79pub mod daemon;
80pub mod debug_dump;
81pub mod durable;
82pub mod file_watcher;
83pub mod goal;
84pub mod history_integrity;
85pub mod instructions;
86pub mod instrumented_channel;
87pub mod metrics;
88#[cfg(feature = "profiling")]
89pub mod metrics_bridge;
90pub mod notifications;
91pub mod pipeline;
92pub mod project;
93pub mod provider_factory;
94pub mod quality;
95pub mod redact;
96pub mod serve;
97pub mod session_resume;
98#[cfg(feature = "sysinfo")]
99pub mod system_metrics;
100
101pub mod http;
102pub mod json_event_layer;
103pub mod json_event_sink;
104pub mod lsp_hooks;
105pub mod memory_tools;
106pub mod overflow_tools;
107pub mod runtime_context;
108pub mod runtime_layer;
109pub mod skill_invoker;
110pub mod skill_loader;
111mod skill_trust_gate;
112pub use zeph_common::text;
113
114#[cfg(test)]
115pub mod testing;
116
117pub use agent::Agent;
118pub use agent::DurableKeyMaterial;
119pub use agent::SecurityWiringSnapshot;
120pub use agent::SkillConfigParams;
121pub use agent::error::AgentError;
122pub use agent::session_config::{AgentSessionConfig, CONTEXT_BUDGET_RESERVE_RATIO};
123pub use agent::state::AdversarialPolicyInfo;
124pub use agent::state::ProviderConfigSnapshot;
125pub use agent::state::ShellOverlaySnapshot;
126pub use channel::{
127 Attachment, AttachmentKind, Channel, ChannelError, ChannelMessage, LoopbackChannel,
128 LoopbackEvent, LoopbackHandle, StopHint, ToolOutputData, ToolOutputEvent, ToolStartData,
129 ToolStartEvent,
130};
131pub use config::{Config, ConfigError};
132pub use runtime_context::RuntimeContext;
133pub use skill_invoker::{InvokeSkillParams, SkillInvokeExecutor, SkillTrustSnapshot};
134pub use skill_loader::SkillLoaderExecutor;
135pub use skill_trust_gate::{SkillBodyResolution, SkillTrustGate, resolve_require_check};
136pub use zeph_common::hash::blake3_hex as content_hash;
137pub use zeph_sanitizer::exfiltration::{
138 ExfiltrationEvent, ExfiltrationGuard, ExfiltrationGuardConfig, extract_flagged_urls,
139};
140pub use zeph_sanitizer::{
141 ContentIsolationConfig, ContentSanitizer, ContentSource, ContentSourceKind, ContentTrustLevel,
142 InjectionFlag, QuarantineConfig, SanitizedContent,
143};
144pub use zeph_tools::executor::DiffData;
145
146// Re-export vault module to preserve internal import paths (e.g., `crate::vault::VaultProvider`).
147pub mod vault {
148 pub use zeph_vault::{
149 AgeVaultError, AgeVaultProvider, ArcAgeVaultProvider, Secret, VaultError, VaultProvider,
150 default_vault_dir,
151 };
152
153 /// Environment-variable backed vault provider.
154 ///
155 /// # Security
156 ///
157 /// This provider reads secrets from process environment variables and is
158 /// intended **exclusively for development and testing**. Never use in
159 /// production builds. Use [`AgeVaultProvider`] instead.
160 pub use zeph_vault::EnvVaultProvider;
161
162 #[cfg(any(test, feature = "mock"))]
163 pub use zeph_vault::MockVaultProvider;
164}