Skip to main content

WorkingMemory

Struct WorkingMemory 

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

A bounded, key-value working memory for transient agent state.

When capacity is exceeded, the oldest entry (by insertion order) is evicted.

§Guarantees

  • Thread-safe via Arc<Mutex<_>>
  • Bounded: never exceeds capacity entries
  • Deterministic eviction: LRU (oldest insertion first)

Implementations§

Source§

impl WorkingMemory

Source

pub fn new(capacity: usize) -> Result<Self, AgentRuntimeError>

Create a new WorkingMemory with the given capacity.

§Returns
  • Ok(WorkingMemory) — on success
  • Err(AgentRuntimeError::Memory) — if capacity == 0
Source

pub fn set( &self, key: impl Into<String> + Debug, value: impl Into<String> + Debug, ) -> Result<(), AgentRuntimeError>

Insert or update a key-value pair, evicting the oldest entry if over capacity.

Source

pub fn get(&self, key: &str) -> Result<Option<String>, AgentRuntimeError>

Retrieve a value by key.

§Returns
  • Some(value) — if the key exists
  • None — if not found
Source

pub fn set_many( &self, pairs: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>, ) -> Result<(), AgentRuntimeError>

Insert multiple key-value pairs with a single lock acquisition.

Each entry follows the same eviction semantics as set: if the key already exists it is updated in-place; if inserting a new key would exceed capacity, the oldest key is evicted first.

Source

pub fn set_if_absent( &self, key: impl Into<String> + Debug, value: impl Into<String> + Debug, ) -> Result<bool, AgentRuntimeError>

Update the value of an existing key without inserting if absent.

Insert key → value only if key is not already present.

Returns Ok(true) if the entry was inserted, Ok(false) if the key was already present (the existing value is left unchanged). Capacity limits and LRU eviction apply as with set.

Source

pub fn update_if_exists( &self, key: &str, value: impl Into<String>, ) -> Result<bool, AgentRuntimeError>

Returns Ok(true) if the key existed and was updated, Ok(false) if not present.

Source

pub fn update_many( &self, pairs: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>, ) -> Result<usize, AgentRuntimeError>

Update multiple existing keys in a single lock acquisition.

Each (key, value) pair is applied only if the key is already present (same semantics as update_if_exists). Returns the number of keys that were found and updated.

Source

pub fn keys_starting_with( &self, prefix: &str, ) -> Result<Vec<String>, AgentRuntimeError>

Return all keys whose names start with prefix, in insertion order.

Useful for namespace-prefixed keys such as "user:name", "user:email" where all user-related keys share the "user:" prefix.

Source

pub fn values_matching( &self, pattern: &str, ) -> Result<Vec<(String, String)>, AgentRuntimeError>

Return all (key, value) pairs whose value contains pattern as a substring. Comparison is case-sensitive.

Useful for scanning working memory for values that match a keyword without iterating the full map externally.

Source

pub fn value_length( &self, key: &str, ) -> Result<Option<usize>, AgentRuntimeError>

Return the byte length of the value stored at key, or None if the key is not present.

Useful for estimating memory usage without cloning the value string.

Source

pub fn contains_all<'a>( &self, keys: impl IntoIterator<Item = &'a str>, ) -> Result<bool, AgentRuntimeError>

Rename a key without changing its value or insertion order.

Return true if all of the given keys are currently stored.

An empty iterator returns true vacuously.

Source

pub fn has_any_key<'a>( &self, keys: impl IntoIterator<Item = &'a str>, ) -> Result<bool, AgentRuntimeError>

Return true if any of the given keys is currently stored.

An empty iterator returns false.

Source

pub fn rename( &self, old_key: &str, new_key: impl Into<String>, ) -> Result<bool, AgentRuntimeError>

Returns true if old_key existed and was renamed. Returns false if old_key is not present. If new_key already exists it is overwritten and its old value is lost.

Source

pub fn get_many( &self, keys: &[&str], ) -> Result<Vec<Option<String>>, AgentRuntimeError>

Retrieve multiple values in a single lock acquisition.

Returns a Vec of the same length as keys. Each entry is Some(value) if the key is present, None if not.

Source

pub fn contains(&self, key: &str) -> Result<bool, AgentRuntimeError>

Return true if a value is stored under key.

Source

pub fn get_or_default( &self, key: &str, default: impl Into<String>, ) -> Result<String, AgentRuntimeError>

Return the value associated with key, or default if not set.

Source

pub fn remove(&self, key: &str) -> Result<bool, AgentRuntimeError>

Remove a single entry by key. Returns true if the key existed.

Source

pub fn keys(&self) -> Result<Vec<String>, AgentRuntimeError>

Return all keys in insertion order.

Source

pub fn values(&self) -> Result<Vec<String>, AgentRuntimeError>

Return all values in insertion order (parallel to keys).

Source

pub fn clear(&self) -> Result<(), AgentRuntimeError>

Remove all entries from working memory.

Source

pub fn iter_sorted(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>

Remove all entries and return them as a Vec<(key, value)> in insertion order.

Return all entries as (key, value) pairs sorted alphabetically by key.

Unlike iter/entries which return entries in insertion order, this always returns a deterministically ordered snapshot.

Source

pub fn drain(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>

After this call the memory is empty. Useful for atomically moving the contents to another data structure.

Source

pub fn snapshot(&self) -> Result<HashMap<String, String>, AgentRuntimeError>

Clone all current entries into a HashMap<String, String>.

Unlike drain, this leaves the working memory unchanged.

Source

pub fn len(&self) -> Result<usize, AgentRuntimeError>

Return the current number of entries.

Source

pub fn is_empty(&self) -> Result<bool, AgentRuntimeError>

Return true if no entries are stored.

Source

pub fn iter(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>

Iterate over all key-value pairs in insertion order.

Equivalent to entries; provided as a more idiomatic name for for-loop patterns.

Source

pub fn entries(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>

Return all key-value pairs in insertion order.

Source

pub fn pop_oldest(&self) -> Result<Option<(String, String)>, AgentRuntimeError>

Remove and return the oldest entry (first inserted that is still present).

Returns None if the memory is empty.

Source

pub fn peek_oldest(&self) -> Result<Option<(String, String)>, AgentRuntimeError>

Peek at the oldest entry without removing it.

Returns None if the memory is empty. Unlike pop_oldest this method does not modify the store.

Source

pub fn capacity(&self) -> usize

Return the maximum number of entries this store can hold.

When len reaches this value, the oldest entry is evicted on the next set call for a new key.

Source

pub fn fill_ratio(&self) -> Result<f64, AgentRuntimeError>

Return the current fill ratio as len / capacity.

Returns a value in [0.0, 1.0]. 1.0 means the memory is full and the next insert will evict the oldest entry.

Source

pub fn is_at_capacity(&self) -> Result<bool, AgentRuntimeError>

Return true when the number of stored entries equals the configured capacity.

When true, the next set call for a new key will evict the oldest entry.

Source

pub fn remove_keys_starting_with( &self, prefix: &str, ) -> Result<usize, AgentRuntimeError>

Remove all entries whose key begins with prefix.

Returns the number of entries removed. Removal preserves insertion order for the surviving entries.

Source

pub fn total_value_bytes(&self) -> Result<usize, AgentRuntimeError>

Return the total number of bytes used by all stored values.

Useful for estimating memory pressure without allocating a full snapshot.

Source

pub fn max_key_length(&self) -> Result<usize, AgentRuntimeError>

Return the byte length of the longest key currently stored.

Returns 0 when the memory is empty.

Source

pub fn max_value_length(&self) -> Result<usize, AgentRuntimeError>

Return the byte length of the longest stored value.

Returns 0 when the memory is empty.

Source

pub fn min_value_length(&self) -> Result<usize, AgentRuntimeError>

Return the byte length of the shortest stored value.

Returns 0 when the memory is empty.

Source

pub fn key_count_matching( &self, substring: &str, ) -> Result<usize, AgentRuntimeError>

Return the number of keys whose text contains substring.

The search is case-sensitive. Returns 0 when the store is empty or no key matches.

Source

pub fn avg_value_length(&self) -> Result<f64, AgentRuntimeError>

Return the mean byte length of all stored values.

Returns 0.0 when the store is empty.

Source

pub fn count_above_value_length( &self, min_bytes: usize, ) -> Result<usize, AgentRuntimeError>

Return the number of entries whose value exceeds min_bytes bytes in length.

Source

pub fn longest_key(&self) -> Result<Option<String>, AgentRuntimeError>

Return the key with the most bytes, or None if the store is empty.

When multiple keys share the maximum byte length, one of them is returned (unspecified which).

Source

pub fn longest_value(&self) -> Result<Option<String>, AgentRuntimeError>

Return the value with the most bytes, or None if the store is empty.

When multiple values share the maximum byte length, one of them is returned (unspecified which).

Source

pub fn pairs_starting_with( &self, prefix: &str, ) -> Result<Vec<(String, String)>, AgentRuntimeError>

Return all (key, value) pairs whose key starts with prefix.

Source

pub fn total_key_bytes(&self) -> Result<usize, AgentRuntimeError>

Return the sum of byte lengths of all stored keys.

Source

pub fn min_key_length(&self) -> Result<usize, AgentRuntimeError>

Return the shortest key length, or 0 if the store is empty.

Source

pub fn count_matching_prefix( &self, prefix: &str, ) -> Result<usize, AgentRuntimeError>

Return the number of keys that start with the given prefix.

Source

pub fn entry_count(&self) -> Result<usize, AgentRuntimeError>

Return a list of (key, value_byte_length) pairs for all entries. Return the number of key-value entries currently stored.

Source

pub fn value_lengths(&self) -> Result<Vec<(String, usize)>, AgentRuntimeError>

Return a list of (key, value_byte_length) pairs for all entries.

Source

pub fn keys_with_value_longer_than( &self, threshold: usize, ) -> Result<Vec<String>, AgentRuntimeError>

Return the keys of all entries whose value byte length is strictly greater than threshold.

Source

pub fn longest_value_key(&self) -> Result<Option<String>, AgentRuntimeError>

Return the key whose associated value has the most bytes, or None if empty.

Source

pub fn retain<F>(&self, predicate: F) -> Result<usize, AgentRuntimeError>
where F: FnMut(&str, &str) -> bool,

Remove all entries for which predicate(key, value) returns false.

Preserves insertion order of the surviving entries. Returns the number of entries removed.

Source

pub fn merge_from( &self, other: &WorkingMemory, ) -> Result<usize, AgentRuntimeError>

Copy all entries from other into self.

Entries from other are inserted in its insertion order. Capacity limits and eviction apply as if each entry were inserted individually via set.

Source

pub fn entry_count_satisfying<F>( &self, predicate: F, ) -> Result<usize, AgentRuntimeError>
where F: FnMut(&str, &str) -> bool,

Return the number of entries for which predicate(key, value) returns true.

Source

pub fn get_all_keys(&self) -> Result<Vec<String>, AgentRuntimeError>

Return all keys in insertion order.

Source

pub fn replace_all( &self, map: HashMap<String, String>, ) -> Result<(), AgentRuntimeError>

Atomically replace all entries with map.

The new entries are stored in iteration order of map. Capacity limits apply: if map.len() > capacity, only the last capacity entries (in iteration order) are retained.

Trait Implementations§

Source§

impl Clone for WorkingMemory

Source§

fn clone(&self) -> WorkingMemory

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WorkingMemory

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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