Skip to main content

Agent

Struct Agent 

Source
pub struct Agent { /* private fields */ }
Expand description

Agent for deterministic AI-driven code operations.

The agent follows a strict loop:

  1. Observe: Gather context from the graph
  2. Constrain: Apply policy rules
  3. Plan: Generate execution steps
  4. Mutate: Apply changes
  5. Verify: Validate results
  6. 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

Source

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?;
Source

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 ForgeRuntime
  • 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, mut runtime) = Agent::with_runtime("./project").await?;
let result = agent.run_with_runtime(&mut runtime, "add error handling").await?;
Source

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.

Source

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

Source

pub async fn new(codebase_path: impl AsRef<Path>) -> Result<Self>

Creates a new agent for the given codebase.

§Arguments
  • codebase_path - Path to the codebase
Source

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
Source

pub async fn constrain( &self, observation: Observation, policies: Vec<Policy>, ) -> Result<ConstrainedPlan>

Applies policy constraints to the observation.

§Arguments
  • observation - The observation to constrain
  • policies - The policies to validate
Source

pub async fn plan(&self, constrained: ConstrainedPlan) -> Result<ExecutionPlan>

Generates an execution plan from the constrained observation.

Source

pub async fn mutate(&self, plan: ExecutionPlan) -> Result<MutationResult>

Executes the mutation phase of the plan.

Source

pub async fn verify( &self, _result: MutationResult, ) -> Result<VerificationResult>

Verifies the mutation result.

Source

pub async fn commit(&self, result: VerificationResult) -> Result<CommitResult>

Commits the verified mutation.

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more