Skip to main content

AgentBuilder

Struct AgentBuilder 

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

Builder for configuring an Agent before connecting to the network.

The builder allows customization of the agent’s identity:

  • Machine key path: Where to store/load the machine keypair
  • Agent keypair: Import a portable agent identity from another machine
  • User keypair: Bind a human identity to this agent

§Example

use x0x::Agent;

// Default: auto-generates both keypairs
let agent = Agent::builder()
    .build()
    .await?;

// Custom machine key path
let agent = Agent::builder()
    .with_machine_key("/custom/path/machine.key")
    .build()
    .await?;

// Import agent keypair
let agent_kp = load_agent_keypair()?;
let agent = Agent::builder()
    .with_agent_key(agent_kp)
    .build()
    .await?;

// With user identity (three-layer)
let agent = Agent::builder()
    .with_user_key_path("~/.x0x/user.key")
    .build()
    .await?;

Implementations§

Source§

impl AgentBuilder

Source

pub fn with_machine_key<P: AsRef<Path>>(self, path: P) -> Self

Set a custom path for the machine keypair.

If not set, the machine keypair is stored in ~/.x0x/machine.key.

§Arguments
  • path - The path to store the machine keypair.
§Returns

Self for chaining.

Source

pub fn with_agent_key(self, keypair: AgentKeypair) -> Self

Import an agent keypair.

If not set, the agent keypair is loaded from storage (or generated fresh if no stored key exists).

This enables running the same agent on multiple machines by importing the same agent keypair (but with different machine keypairs).

Note: When an explicit keypair is provided via this method, it takes precedence over with_agent_key_path().

§Arguments
  • keypair - The agent keypair to import.
§Returns

Self for chaining.

Source

pub fn with_agent_key_path<P: AsRef<Path>>(self, path: P) -> Self

Set a custom path for the agent keypair.

If not set, the agent keypair is stored in ~/.x0x/agent.key. If no stored key is found at the path, a fresh one is generated and saved.

This is ignored when with_agent_key() provides an explicit keypair.

§Arguments
  • path - The path to store/load the agent keypair.
§Returns

Self for chaining.

Source

pub fn with_agent_cert_path<P: AsRef<Path>>(self, path: P) -> Self

Set a custom path for the agent certificate (agent.cert).

Required for multi-daemon setups that share a host — the default path (~/.x0x/agent.cert) is shared across daemons, causing last-writer-wins trampling that makes peers reject identity announcements as agent certificate agent_id mismatch.

§Arguments
  • path - The path to use for the agent certificate file.
§Returns

Self for chaining.

Source

pub fn with_network_config(self, config: NetworkConfig) -> Self

Set network configuration for P2P communication.

If not set, the agent is built without a network node or gossip runtime. Use NetworkConfig::default() to connect through the default bootstrap nodes.

§Arguments
  • config - The network configuration to use.
§Returns

Self for chaining.

Source

pub fn with_gossip_config(self, config: GossipConfig) -> Self

Set gossip overlay configuration.

This is primarily used by x0xd to expose operational knobs such as gossip.dispatch_workers. If the agent is built without a network configuration, the value is retained by the builder but has no runtime effect.

Source

pub fn with_peer_cache_dir<P: AsRef<Path>>(self, path: P) -> Self

Set the directory for the bootstrap peer cache.

The cache persists peer quality metrics across restarts, enabling cache-first join strategy. Defaults to ~/.x0x/peers/ if not set. Falls back to ./.x0x/peers/ (relative to CWD) if $HOME is unset.

Source

pub fn with_peer_cache_disabled(self) -> Self

Disable the bootstrap peer cache entirely.

When set, the agent will not open or load any cached peers on startup. This ensures complete network isolation from previously seen peers for embedders and dedicated test harnesses.

Note: the x0xd daemon’s --no-hard-coded-bootstrap flag does not call this; it only clears configured seed peers.

Source

pub fn with_user_key(self, keypair: UserKeypair) -> Self

Import a user keypair for three-layer identity.

This binds a human identity to this agent. When provided, an identity::AgentCertificate is automatically issued (if one doesn’t already exist in storage) to cryptographically attest that this agent belongs to the user.

Note: When an explicit keypair is provided via this method, it takes precedence over with_user_key_path().

§Arguments
  • keypair - The user keypair to import.
§Returns

Self for chaining.

Source

pub fn with_user_key_path<P: AsRef<Path>>(self, path: P) -> Self

Set a custom path for the user keypair.

Unlike machine and agent keys, user keys are not auto-generated. If the file at this path doesn’t exist, no user identity is set (the agent operates with two-layer identity).

This is ignored when with_user_key() provides an explicit keypair.

§Arguments
  • path - The path to load the user keypair from.
§Returns

Self for chaining.

Source

pub fn with_heartbeat_interval(self, secs: u64) -> Self

Set the identity heartbeat re-announcement interval.

Defaults to IDENTITY_HEARTBEAT_INTERVAL_SECS (300 seconds).

§Arguments
  • secs - Interval in seconds between identity re-announcements.
Source

pub fn with_identity_ttl(self, secs: u64) -> Self

Set the identity cache TTL.

Cache entries with last_seen older than this threshold are filtered from Agent::presence and Agent::discovered_agents.

Defaults to IDENTITY_TTL_SECS (900 seconds).

§Arguments
  • secs - Time-to-live in seconds for discovered agent entries.
Source

pub fn with_presence_beacon_interval(self, secs: u64) -> Self

Override the presence beacon broadcast interval in seconds.

Source

pub fn with_presence_event_poll_interval(self, secs: u64) -> Self

Override the presence event poll interval in seconds.

Source

pub fn with_presence_offline_timeout(self, secs: u64) -> Self

Override the fallback offline timeout used by presence events.

Source

pub fn with_contact_store_path<P: AsRef<Path>>(self, path: P) -> Self

Set a custom path for the contacts file.

The contacts file persists trust levels and machine records for known agents. Defaults to ~/.x0x/contacts.json if not set.

§Arguments
  • path - The path for the contacts file.
Source

pub async fn build(self) -> Result<Agent>

Build and initialise the agent.

This performs the following:

  1. Loads or generates the machine keypair (stored in ~/.x0x/machine.key by default)
  2. Uses provided agent keypair or generates a fresh one
  3. Combines both into a unified Identity

The machine keypair is automatically persisted to storage.

§Errors

Returns an error if:

  • Machine keypair generation fails
  • Storage I/O fails
  • Keypair deserialization fails

Trait Implementations§

Source§

impl Debug for AgentBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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