pub struct Agent { /* private fields */ }Expand description
A CBCL agent (REQ-030).
An agent is a participant in the CBCL protocol with:
- a unique identifier
- a set of beliefs (S-expressions)
- a dialect registry (always containing the base dialect at index 0)
- a message queue for incoming messages
- conversation threads indexed by thread id
§Well-formedness Invariant
An agent is well-formed iff self.dialect_registry.is_well_formed(),
i.e. the base dialect is always installed at index 0. Agent::new()
guarantees this invariant, and no method on Agent can violate it.
Implementations§
Source§impl Agent
impl Agent
Sourcepub fn new(id: impl Into<String>) -> Self
pub fn new(id: impl Into<String>) -> Self
Create a new agent with the given id, bootstrapped with the base dialect (REQ-031).
The returned agent is always well-formed: the dialect registry contains exactly the base dialect.
Sourcepub fn with_policy(
id: impl Into<String>,
policy: UnknownPredecessorPolicy,
) -> Self
pub fn with_policy( id: impl Into<String>, policy: UnknownPredecessorPolicy, ) -> Self
Create a new agent with the given id and unknown-predecessor policy.
Sourcepub fn with_merge_policy(self, merge_policy: MergePolicy) -> Self
pub fn with_merge_policy(self, merge_policy: MergePolicy) -> Self
Set how outcomes from multiple matching protocols are combined.
Defaults to MergePolicy::Conjunction (fail-closed).
Sourcepub fn merge_policy(&self) -> MergePolicy
pub fn merge_policy(&self) -> MergePolicy
Returns the configured merge policy.
Sourcepub fn with_clock<C: Clock + 'static>(self, clock: C) -> Self
pub fn with_clock<C: Clock + 'static>(self, clock: C) -> Self
Inject a Clock for buffered-entry timestamps and TTL expiry.
Without this, the agent uses NoClock which stamps every buffered
entry with u64::MAX, so TTL never fires. Pass crate::clock::SystemClock
(with the std feature) to use real time, or any custom Clock
implementation — useful for deterministic tests.
Sourcepub fn message_store(&self) -> &ThreadedMessageStore
pub fn message_store(&self) -> &ThreadedMessageStore
Returns a reference to the agent’s message store.
Sourcepub fn message_store_mut(&mut self) -> &mut ThreadedMessageStore
pub fn message_store_mut(&mut self) -> &mut ThreadedMessageStore
Mutable access to the agent’s message store. Callers manage content hashing — the agent itself does not synthesize hashes.
Sourcepub fn policy(&self) -> &UnknownPredecessorPolicy
pub fn policy(&self) -> &UnknownPredecessorPolicy
Returns the configured unknown-predecessor policy.
Sourcepub fn pending_len(&self) -> usize
pub fn pending_len(&self) -> usize
Number of buffered (pending) messages awaiting predecessor arrival.
Sourcepub fn is_well_formed(&self) -> bool
pub fn is_well_formed(&self) -> bool
Check whether this agent is well-formed (REQ-032).
An agent is well-formed iff the base dialect is installed at index 0
of the dialect registry. Mirrors Agent.isWellFormed in Agent.lean.
Sourcepub fn dialect_registry(&self) -> &DialectRegistry
pub fn dialect_registry(&self) -> &DialectRegistry
Returns a reference to the dialect registry.
Sourcepub fn dialects_defining<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = &'a Dialect>
pub fn dialects_defining<'a>( &'a self, name: &'a str, ) -> impl Iterator<Item = &'a Dialect>
Every installed dialect defining name, in installation order
(REQ-033).
Introspection only. Dispatch never uses it: a custom performative is
resolved against the dialect named by its (lang …) wrapper, so more
than one result here is normal rather than a conflict.
Sourcepub fn install_dialect(&mut self, d: Dialect) -> Result<(), DialectInstallError>
pub fn install_dialect(&mut self, d: Dialect) -> Result<(), DialectInstallError>
Install a dialect after verifying R1, R2, and R3 (REQ-034).
Delegates to DialectRegistry::install. The well-formedness
invariant is preserved because install only appends.
Sourcepub fn add_belief(&mut self, belief: SExpr) -> bool
pub fn add_belief(&mut self, belief: SExpr) -> bool
Add a belief to the agent’s belief set.
Returns true if the belief was newly inserted.
Sourcepub fn remove_belief(&mut self, belief: &SExpr) -> bool
pub fn remove_belief(&mut self, belief: &SExpr) -> bool
Remove a belief from the agent’s belief set.
Returns true if the belief was present.
Sourcepub fn has_belief(&self, belief: &SExpr) -> bool
pub fn has_belief(&self, belief: &SExpr) -> bool
Check whether the agent holds a given belief.
Sourcepub fn message_queue(&self) -> &VecDeque<Message>
pub fn message_queue(&self) -> &VecDeque<Message>
Returns a reference to the message queue.
Sourcepub fn enqueue_message(&mut self, msg: Message)
pub fn enqueue_message(&mut self, msg: Message)
Enqueue a message at the back of the message queue.
Sourcepub fn dequeue_message(&mut self) -> Option<Message>
pub fn dequeue_message(&mut self) -> Option<Message>
Dequeue a message from the front of the message queue.
Sourcepub fn message_queue_len(&self) -> usize
pub fn message_queue_len(&self) -> usize
Returns the number of messages in the queue.
Sourcepub fn conversation_threads(&self) -> &BTreeMap<String, Vec<Message>>
pub fn conversation_threads(&self) -> &BTreeMap<String, Vec<Message>>
Returns a reference to all conversation threads.
Sourcepub fn thread(&self, thread_id: &str) -> Option<&[Message]>
pub fn thread(&self, thread_id: &str) -> Option<&[Message]>
Get the messages in a specific conversation thread.
Sourcepub fn append_to_thread(&mut self, thread_id: impl Into<String>, msg: Message)
pub fn append_to_thread(&mut self, thread_id: impl Into<String>, msg: Message)
Append a message to a conversation thread, creating the thread if needed.
Sourcepub fn evaluate_and_apply(&mut self, msg: &Message) -> AgentOutcome
pub fn evaluate_and_apply(&mut self, msg: &Message) -> AgentOutcome
Evaluate a message and apply its effects to this agent.
Pipeline (REQ-231 fail-closed):
- Causal verification against the agent’s dialect registry, message
store, and configured
UnknownPredecessorPolicy. - On
Accept: evaluate the message and apply its effects. - On a resolved
Violation: returnAgentOutcome::CausalRejectwithout applying. A provisionalViolation— some named:caused-byhash still absent from the store — is downgraded toUnknownfirst (SPEC-003 REQ-315), so it follows 4/5 instead. - On
Unknown+Rejectpolicy: returnAgentOutcome::Pending. - On
Unknown+Bufferpolicy: enqueue in the pending queue and returnAgentOutcome::Buffered; the message is not applied.
Buffered entries are timestamped via the injected Clock. With the
default NoClock this is u64::MAX, so Self::expire will never
drain them. Inject a real clock via Self::with_clock (or call
Self::evaluate_and_apply_at for an explicit one-off override) to
enable TTL-based eviction.
Sourcepub fn evaluate_and_apply_at(&mut self, msg: &Message, now: u64) -> AgentOutcome
pub fn evaluate_and_apply_at(&mut self, msg: &Message, now: u64) -> AgentOutcome
Like Self::evaluate_and_apply but stamps any newly-buffered entry
with the supplied now (seconds since epoch), bypassing the injected
clock. Useful for tests or when the caller wants per-message control
over the timestamp.
Sourcepub fn expire(&mut self, now: u64) -> Vec<(PendingEntry, DropReason)>
pub fn expire(&mut self, now: u64) -> Vec<(PendingEntry, DropReason)>
Drop buffered entries whose TTL has elapsed at now (seconds since
epoch). Returns the dropped entries with DropReason::CausalTimeout.
Entries stamped with u64::MAX (the default when the agent is
constructed without a clock and Self::evaluate_and_apply is used)
never expire. To get TTL behaviour, either inject a real clock via
Self::with_clock or pass an explicit timestamp via
Self::evaluate_and_apply_at / Self::step_at.
Eviction is pull-based. The agent does not call expire on its
own — the embedder is responsible for invoking it on a schedule that
matches the configured TTL (e.g. once per second, once per minute).
Without that, expired entries linger in the queue until the next
caller-driven invocation, holding memory but not causing
correctness issues. A future revision could opt-in to expire-on-each
evaluate_and_apply if it becomes a footgun in practice.
Sourcepub fn reevaluate_pending(&mut self) -> Vec<(PendingEntry, PolicyOutcome)>
pub fn reevaluate_pending(&mut self) -> Vec<(PendingEntry, PolicyOutcome)>
Re-evaluate buffered messages against the (possibly grown) message store.
Returns the entries that now resolve to either Accept or Reject.
Entries whose performative isn’t constrained by any installed
protocol stay buffered (rather than being spuriously accepted by an
unrelated protocol — verify_causal returns Valid for performatives
outside a protocol’s step set, so iterating naively over every
installed protocol would flush entries that are still waiting on
their real predecessor).
When multiple protocols constrain the same performative, results
compose by conjunction (any rejection rejects, any Unknown keeps
pending) — the same semantics as the live verification path.
The caller is responsible for re-applying accepted messages; the
agent does not auto-apply them, since Agent::evaluate_and_apply
mutates state and the caller may want explicit control over re-entry.
Sourcepub fn step(&mut self) -> Option<AgentOutcome>
pub fn step(&mut self) -> Option<AgentOutcome>
Dequeue the next message, evaluate it, and apply effects.
Returns None if the queue is empty. Buffered entries inherit the
u64::MAX timestamp from Self::evaluate_and_apply; use
Self::step_at when TTL semantics are required.
Sourcepub fn step_at(&mut self, now: u64) -> Option<AgentOutcome>
pub fn step_at(&mut self, now: u64) -> Option<AgentOutcome>
Like Self::step but stamps any newly-buffered entry with now.