pub struct Agent { /* private fields */ }Expand description
Agent for deterministic AI-driven code operations.
The agent follows a strict loop:
- Observe: Gather context from the graph
- Constrain: Apply policy rules
- Plan: Generate execution steps
- Mutate: Apply changes
- Verify: Validate results
- Commit: Finalize transaction
§Runtime Integration
The agent can integrate with ForgeRuntime for coordinated file watching
and caching:
let (agent, mut runtime) = Agent::with_runtime("./project").await?;
let result = agent.run_with_runtime(&mut runtime, "refactor function").await?;Implementations§
Source§impl Agent
impl Agent
Sourcepub async fn with_runtime(
codebase_path: impl AsRef<Path>,
) -> Result<(Self, ForgeRuntime), AgentError>
pub async fn with_runtime( codebase_path: impl AsRef<Path>, ) -> Result<(Self, ForgeRuntime), AgentError>
Creates agent with runtime for file watching and caching.
This method initializes both the Agent and ForgeRuntime, allowing the agent to leverage runtime services like query caching and coordinated file watching.
§Arguments
codebase_path- Path to the codebase
§Returns
Returns a tuple of (Agent, ForgeRuntime) on success.
§Example
use forge_agent::Agent;
let (agent, mut runtime) = Agent::with_runtime("./project").await?;
// Start file watching
runtime.watch().await?;
// Run agent with runtime coordination
let result = agent.run_with_runtime(&mut runtime, "refactor function").await?;Sourcepub async fn run_with_runtime(
&self,
_runtime: &mut ForgeRuntime,
query: &str,
) -> Result<LoopResult>
pub async fn run_with_runtime( &self, _runtime: &mut ForgeRuntime, query: &str, ) -> Result<LoopResult>
Runs agent loop with runtime coordination.
This method coordinates with ForgeRuntime for optimal performance:
- Query cache access for faster graph operations
- Metrics collection for observability
Note: For v0.4, watcher pause/resume is deferred to future versions. The runtime provides cache access and metrics, but file watching coordination is a future enhancement.
§Arguments
runtime- Mutable reference to the ForgeRuntimequery- The natural language query or request
§Returns
Returns LoopResult with transaction ID, modified files, and audit trail.
§Example
use forge_agent::Agent;
let (agent, mut runtime) = Agent::with_runtime("./project").await?;
let result = agent.run_with_runtime(&mut runtime, "add error handling").await?;Sourcepub fn runtime_cache(&self) -> Option<()>
pub fn runtime_cache(&self) -> Option<()>
Gets reference to runtime cache if available.
This method provides access to the runtime’s query cache for optimization of repeated graph queries.
§Returns
Returns None - cache access requires runtime association (not yet implemented).
Note: This is a placeholder for future functionality.
Sourcepub fn runtime_stats(&self) -> Option<()>
pub fn runtime_stats(&self) -> Option<()>
Gets runtime statistics if available.
This method provides access to runtime metrics including cache size, watch status, and reindex count.
§Returns
Returns None - stats access requires runtime association (not yet implemented).
Note: This is a placeholder for future functionality.
Source§impl Agent
impl Agent
Sourcepub async fn observe(&self, query: &str) -> Result<Observation>
pub async fn observe(&self, query: &str) -> Result<Observation>
Observes the codebase to gather context for a query.
§Arguments
query- The natural language query or request
Sourcepub async fn constrain(
&self,
observation: Observation,
policies: Vec<Policy>,
) -> Result<ConstrainedPlan>
pub async fn constrain( &self, observation: Observation, policies: Vec<Policy>, ) -> Result<ConstrainedPlan>
Applies policy constraints to the observation.
§Arguments
observation- The observation to constrainpolicies- The policies to validate
Sourcepub async fn plan(&self, constrained: ConstrainedPlan) -> Result<ExecutionPlan>
pub async fn plan(&self, constrained: ConstrainedPlan) -> Result<ExecutionPlan>
Generates an execution plan from the constrained observation.
Sourcepub async fn mutate(&self, plan: ExecutionPlan) -> Result<MutationResult>
pub async fn mutate(&self, plan: ExecutionPlan) -> Result<MutationResult>
Executes the mutation phase of the plan.
Sourcepub async fn verify(
&self,
_result: MutationResult,
) -> Result<VerificationResult>
pub async fn verify( &self, _result: MutationResult, ) -> Result<VerificationResult>
Verifies the mutation result.
Sourcepub async fn commit(&self, result: VerificationResult) -> Result<CommitResult>
pub async fn commit(&self, result: VerificationResult) -> Result<CommitResult>
Commits the verified mutation.
Sourcepub async fn run(&self, query: &str) -> Result<LoopResult>
pub async fn run(&self, query: &str) -> Result<LoopResult>
Runs the full agent loop: Observe -> Constrain -> Plan -> Mutate -> Verify -> Commit
This is the main entry point for executing a complete agent operation. Each phase receives the output of the previous phase, and failures trigger rollback with audit trail entries.
§Arguments
query- The natural language query or request
§Returns
Returns LoopResult with transaction ID, modified files, and audit trail.
§Example
use forge_agent::Agent;
let agent = Agent::new(".").await?;
let result = agent.run("Add error handling to the parser").await?;
println!("Transaction ID: {}", result.transaction_id);Auto Trait Implementations§
impl Freeze for Agent
impl RefUnwindSafe for Agent
impl Send for Agent
impl Sync for Agent
impl Unpin for Agent
impl UnsafeUnpin for Agent
impl UnwindSafe for Agent
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more