Complete API reference for ForgeKit SDK. This documents the working APIs as of v0.2.
Enum for selecting storage backend:
pub enum BackendKind {
SQLite, // SQLite database (.forge/graph.db)
NativeV3, // Native V3 binary format (.forge/graph.v3)
}Default: BackendKind::SQLite
Example:
let forge = Forge::open_with_backend("./project", BackendKind::NativeV3).await?;Represents a code symbol (function, struct, etc.):
pub struct Symbol {
pub id: SymbolId,
pub name: String,
pub fully_qualified_name: String,
pub kind: SymbolKind,
pub language: Language,
pub location: Location,
pub parent_id: Option<SymbolId>,
pub metadata: Value,
}pub enum SymbolKind {
Function,
Method,
Struct,
Enum,
Trait,
Impl,
Module,
TypeAlias,
Constant,
Static,
// Variable types
Parameter,
LocalVariable,
Field,
// Other
Macro,
Use,
}pub struct Location {
pub file_path: PathBuf,
pub byte_start: u32,
pub byte_end: u32,
pub line_number: usize,
}Represents a reference from one symbol to another:
pub struct Reference {
pub from: SymbolId,
pub to: SymbolId,
pub kind: ReferenceKind,
pub location: Location,
}pub enum ReferenceKind {
Call, // Function call
Use, // Variable usage
TypeReference, // Type reference
Inherit, // Inheritance
Implementation, // Trait implementation
}Main entry point for the SDK.
Open a codebase with default backend (SQLite).
pub async fn open(path: impl AsRef<Path>) -> Result<Self>Example:
let forge = Forge::open("./my-project").await?;Open with specific backend.
pub async fn open_with_backend(
path: impl AsRef<Path>,
backend: BackendKind
) -> Result<Self>Example:
let forge = Forge::open_with_backend("./project", BackendKind::NativeV3).await?;Get graph module for symbol queries.
pub fn graph(&self) -> GraphModuleGet search module for code search.
pub fn search(&self) -> SearchModuleGet CFG module for control flow analysis.
pub fn cfg(&self) -> CfgModuleGet edit module for code modifications.
pub fn edit(&self) -> EditModuleGet analysis module for composite operations.
pub fn analysis(&self) -> AnalysisModuleSymbol and reference queries using the graph database.
Find symbols by name (fuzzy search).
pub async fn find_symbol(&self, name: &str) -> Result<Vec<Symbol>>Example:
let symbols = forge.graph().find_symbol("main").await?;
for symbol in symbols {
println!("Found: {} ({:?}) at {:?}",
symbol.name, symbol.kind, symbol.location);
}Find a symbol by its stable ID.
pub async fn find_symbol_by_id(&self, id: SymbolId) -> Result<Symbol>Find all callers of a symbol.
pub async fn callers_of(&self, name: &str) -> Result<Vec<Reference>>Example:
let callers = forge.graph().callers_of("process_request").await?;
println!("Called by {} functions", callers.len());
for caller in callers {
println!(" - {} at line {}",
caller.from, caller.location.line_number);
}Find all references to a symbol.
pub async fn references(&self, name: &str) -> Result<Vec<Reference>>Example:
let refs = forge.graph().references("MyStruct").await?;
println!("Referenced {} times", refs.len());Perform k-hop traversal to find all symbols impacted by changing a symbol.
pub async fn impact_analysis(
&self,
symbol_name: &str,
max_hops: Option<u32>
) -> Result<Vec<ImpactedSymbol>>Example:
// Find all symbols within 2 hops of "process_request"
let impacted = forge.graph()
.impact_analysis("process_request", Some(2))
.await?;
for symbol in impacted {
println!("{} ({} hops away)",
symbol.name, symbol.hop_distance);
}Returns: ImpactedSymbol contains:
symbol_id: i64- Entity IDname: String- Symbol namekind: String- Entity kind (fn, struct, etc.)file_path: String- Source filehop_distance: u32- Distance from targetedge_type: String- Type of relationship
Find all symbols reachable from a given symbol via BFS.
pub async fn reachable_from(&self, id: SymbolId) -> Result<Vec<SymbolId>>Get the total number of symbols in the graph.
pub async fn symbol_count(&self) -> Result<usize>Index the codebase using magellan (requires magellan feature).
pub async fn index(&self) -> Result<()>Composite analysis operations combining graph, CFG, and edit modules.
Analyze the impact of changing a symbol.
pub async fn analyze_impact(&self, symbol_name: &str) -> Result<ImpactAnalysis>Returns:
affected_symbols: Vec<Symbol>- Directly affected symbolscall_sites: usize- Total number of call sites
Deep impact analysis with k-hop traversal.
pub async fn deep_impact_analysis(
&self,
symbol_name: &str,
depth: u32
) -> Result<Vec<ImpactedSymbol>>Find dead code (symbols with no references).
pub async fn find_dead_code(&self) -> Result<Vec<Symbol>>Example:
let dead = forge.analysis().find_dead_code().await?;
for symbol in dead {
println!("Unused: {} in {:?}",
symbol.name, symbol.location.file_path);
}Calculate complexity metrics for a function (placeholder in v0.2).
pub async fn complexity_metrics(&self, symbol_name: &str) -> Result<ComplexityMetrics>Calculate complexity from source code directly.
pub fn analyze_source_complexity(&self, source: &str) -> ComplexityMetricsExample:
let source = r#"
fn example(x: i32) -> i32 {
if x > 0 { 1 } else { 0 }
}
"#;
let metrics = analysis.analyze_source_complexity(source);
println!("Complexity: {}", metrics.cyclomatic_complexity);
println!("Risk: {:?}", metrics.risk_level());Returns: ComplexityMetrics contains:
cyclomatic_complexity: usize- McCabe complexitydecision_points: usize- Number of branchesmax_nesting_depth: usize- Maximum nesting levellines_of_code: usize- Lines of code
Get cross-references (both callers and callees).
pub async fn cross_references(&self, symbol_name: &str) -> Result<CrossReferences>Analyze module dependencies.
pub async fn module_dependencies(&self) -> Result<Vec<ModuleDependency>>Find circular dependencies between modules.
pub async fn find_dependency_cycles(&self) -> Result<Vec<Vec<String>>>Semantic code search via LLMGrep integration.
Regex pattern search.
pub async fn pattern(&self, pattern: &str) -> Result<Vec<Symbol>>Example:
let results = forge.search().pattern(r"fn.*test.*\(").await?;
for symbol in results {
println!("Test function: {}", symbol.name);
}Semantic search (requires indexing).
pub async fn semantic(&self, query: &str) -> Result<Vec<Symbol>>Find a specific symbol by exact name.
pub async fn symbol_by_name(&self, name: &str) -> Result<Option<Symbol>>Find all symbols of a specific kind.
pub async fn symbols_by_kind(&self, kind: SymbolKind) -> Result<Vec<Symbol>>Index the codebase for semantic search (requires llmgrep feature).
pub async fn index(&self) -> Result<()>Control flow graph analysis.
Index source files for CFG extraction.
pub async fn index(&self) -> Result<()>Create a path enumeration builder.
pub fn paths(&self, function: SymbolId) -> PathBuilderExample:
let paths = forge.cfg()
.paths(symbol_id)
.normal_only()
.max_length(10)
.execute()
.await?;Compute dominator tree for a function.
pub async fn dominators(&self, function: SymbolId) -> Result<DominatorTree>Returns: DominatorTree contains:
root: BlockId- Entry blockdominators: HashMap<BlockId, BlockId>- Immediate dominator mapping
Example:
let doms = forge.cfg().dominators(symbol_id).await?;
println!("Root: {:?}", doms.root);
if let Some(idom) = doms.immediate_dominator(block_id) {
println!("Immediate dominator: {:?}", idom);
}Detect natural loops in a function.
pub async fn loops(&self, function: SymbolId) -> Result<Vec<Loop>>Returns: Loop contains:
header: BlockId- Loop header blockblocks: Vec<BlockId>- Blocks in the loop bodydepth: usize- Nesting depth
Builder for path enumeration queries.
pub fn normal_only(self) -> Self // Filter to normal paths only
pub fn error_only(self) -> Self // Filter to error paths only
pub fn max_length(self, n: usize) -> Self // Limit path length
pub fn limit(self, n: usize) -> Self // Limit number of paths
pub async fn execute(self) -> Result<Vec<Path>> // Execute queryTest CFG structure for unit testing.
// Create a chain: 0 -> 1 -> 2 -> 3 -> 4
let cfg = TestCfg::chain(0, 5);
// Create an if-else structure
let cfg = TestCfg::if_else();
// Create a simple loop
let cfg = TestCfg::simple_loop();
// Compute dominators
let dom_tree = cfg.compute_dominators();
// Detect loops
let loops = cfg.detect_loops();
// Enumerate all paths
let paths = cfg.enumerate_paths();Span-safe code editing via Splice integration.
Rename a symbol across all files.
pub async fn rename_symbol(
&self,
old_name: &str,
new_name: &str
) -> Result<EditResult>Apply an edit operation.
pub async fn apply(&mut self, op: EditOperation) -> Result<()>pub enum BackendKind {
SQLite,
NativeV3,
}Low-level storage access (advanced usage).
pub struct UnifiedGraphStore {
// ...
}pub fn db_path(&self) -> &Path
pub fn backend_kind(&self) -> BackendKind
pub fn is_connected(&self) -> boolThe workflow module provides DAG-based task orchestration with rollback, checkpointing, and audit logging. Available via forgekit_agent::workflow.
DAG of tasks with topological execution ordering.
pub struct Workflow { /* ... */ }
impl Workflow {
pub fn new() -> Self;
pub fn add_task(&mut self, task: Box<dyn WorkflowTask>) -> TaskId;
pub fn task_count(&self) -> usize;
pub fn task_ids(&self) -> Vec<TaskId>;
pub fn execution_order(&self) -> Result<Vec<TaskId>, WorkflowError>;
}Executes tasks sequentially with audit logging and rollback.
pub struct WorkflowExecutor { /* ... */ }
impl WorkflowExecutor {
pub fn new(workflow: Workflow) -> Self;
pub fn with_forge(self, forge: Arc<Forge>) -> Self;
pub fn with_rollback_strategy(self, strategy: RollbackStrategy) -> Self;
pub fn with_checkpoint_service(self, service: WorkflowCheckpointService) -> Self;
pub fn with_timeout_config(self, config: TimeoutConfig) -> Self;
pub fn with_tool_registry(self, registry: ToolRegistry) -> Self;
pub fn with_cancellation_source(self, source: CancellationTokenSource) -> Self;
}Result of workflow execution.
pub struct WorkflowResult {
pub completed_tasks: Vec<TaskId>,
pub failed_task: Option<TaskId>,
pub error: Option<String>,
pub rollback_report: Option<RollbackReport>,
}Trait for executable workflow tasks.
#[async_trait]
pub trait WorkflowTask: Send + Sync {
async fn execute(&self, context: &TaskContext) -> Result<TaskResult, TaskError>;
fn id(&self) -> TaskId;
fn name(&self) -> &str;
fn dependencies(&self) -> Vec<TaskId>;
}pub enum TaskResult {
Success,
Skipped,
Failed(String),
WithCompensation { result: Box<TaskResult>, compensation: ExecutableCompensation },
}pub enum RollbackStrategy {
AllDependent,
DirectDependentsOnly,
None,
}Tracks compensation actions for rollback.
pub struct CompensationRegistry { /* ... */ }
impl CompensationRegistry {
pub fn new() -> Self;
pub fn register(&mut self, task_id: TaskId, compensation: ToolCompensation);
pub fn validate_coverage(&self, task_ids: &[TaskId]) -> CompensationReport;
}In-memory checkpoint storage with SHA-256 integrity validation.
pub struct WorkflowCheckpointService { /* ... */ }
impl WorkflowCheckpointService {
pub fn new(namespace: impl Into<String>) -> Self;
pub fn new_default() -> Self;
pub fn save(&self, checkpoint: &WorkflowCheckpoint) -> Result<(), WorkflowError>;
pub fn load(&self, id: &CheckpointId) -> Result<Option<WorkflowCheckpoint>, WorkflowError>;
pub fn get_latest(&self, workflow_id: &str) -> Result<Option<WorkflowCheckpoint>, WorkflowError>;
pub fn list_by_workflow(&self, workflow_id: &str) -> Result<Vec<CheckpointSummary>, WorkflowError>;
pub fn delete(&self, id: &CheckpointId) -> Result<(), WorkflowError>;
}Confidence threshold configuration for task validation.
pub struct ValidationCheckpoint {
pub min_confidence: f64, // default: 0.7
pub warning_threshold: f64, // default: 0.85
pub rollback_on_failure: bool, // default: true
}Central registry for external tool invocation with RAII process guards.
pub struct ToolRegistry { /* ... */ }
impl ToolRegistry {
pub fn new() -> Self;
pub fn register(&mut self, tool: Tool) -> Result<(), ToolError>;
pub fn get(&self, name: &str) -> Option<&Tool>;
pub async fn invoke(&self, invocation: &ToolInvocation) -> Result<ToolInvocationResult, ToolError>;
pub fn with_standard_tools() -> Self;
}pub trait FallbackHandler: Send + Sync {
async fn handle(&self, error: &ToolError, invocation: &ToolInvocation) -> FallbackResult;
}
pub struct RetryFallback { /* retries on transient errors */ }
pub struct SkipFallback { /* returns fixed result */ }
pub struct ChainFallback { /* tries multiple handlers in sequence */ }Stores plan artifacts as sqlitegraph nodes and edges for dashboard queries.
#[cfg(feature = "sqlite")]
pub struct PlanGraph { /* ... */ }
impl PlanGraph {
pub fn open(path: &Path) -> Result<Self>;
pub fn open_in_memory() -> Result<Self>;
pub fn add_requirement(&mut self, title: &str, description: &str) -> Result<i64>;
pub fn add_plan(&mut self, name: &str, requirement_ids: &[i64]) -> Result<i64>;
pub fn approve(&mut self, plan_id: i64, approver: &str) -> Result<i64>;
pub fn reject(&mut self, plan_id: i64, reason: &str) -> Result<i64>;
pub fn trace_forward(&self, requirement_id: i64) -> Result<CustodyChain>;
pub fn trace_backward(&self, node_id: i64) -> Result<Vec<CustodyNode>>;
pub fn find_gaps(&self, plan_id: i64) -> Result<Vec<i64>>;
}pub struct CancellationTokenSource { /* ... */ }
pub struct CancellationToken { /* ... */ }
impl CancellationTokenSource {
pub fn new() -> Self;
pub fn token(&self) -> CancellationToken;
pub fn cancel(&self);
}pub struct Gate { /* ... */ }
pub struct GateRunner { /* ... */ }
impl GateRunner {
pub fn new(gate: Gate) -> Self;
pub async fn run(&self, context: &TaskContext) -> Result<GateResult, WorkflowError>;
}All operations return Result<T, ForgeError>.
pub enum ForgeError {
DatabaseError(String),
GraphError(String),
SearchError(String),
CfgError(String),
EditError(String),
SymbolNotFound(SymbolId),
InvalidInput(String),
IoError(std::io::Error),
}use forgekit_core::{Forge, ForgeError};
match forge.graph().find_symbol("main").await {
Ok(symbols) => println!("Found {} symbols", symbols.len()),
Err(ForgeError::SymbolNotFound(_)) => println!("Symbol not found"),
Err(ForgeError::DatabaseError(e)) => eprintln!("Database error: {}", e),
Err(e) => eprintln!("Error: {}", e),
}For more examples, see the User Manual.