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
capacityentries - Deterministic eviction: LRU (oldest insertion first)
Implementations§
Source§impl WorkingMemory
impl WorkingMemory
Sourcepub fn new(capacity: usize) -> Result<Self, AgentRuntimeError>
pub fn new(capacity: usize) -> Result<Self, AgentRuntimeError>
Create a new WorkingMemory with the given capacity.
§Returns
Ok(WorkingMemory)— on successErr(AgentRuntimeError::Memory)— ifcapacity == 0
Sourcepub fn set(
&self,
key: impl Into<String> + Debug,
value: impl Into<String> + Debug,
) -> Result<(), AgentRuntimeError>
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.
Sourcepub fn set_many(
&self,
pairs: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Result<(), AgentRuntimeError>
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.
Sourcepub fn set_if_absent(
&self,
key: impl Into<String> + Debug,
value: impl Into<String> + Debug,
) -> Result<bool, AgentRuntimeError>
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.
Sourcepub fn update_if_exists(
&self,
key: &str,
value: impl Into<String>,
) -> Result<bool, AgentRuntimeError>
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.
Sourcepub fn update_many(
&self,
pairs: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn keys_starting_with(
&self,
prefix: &str,
) -> Result<Vec<String>, AgentRuntimeError>
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.
Sourcepub fn values_matching(
&self,
pattern: &str,
) -> Result<Vec<(String, String)>, AgentRuntimeError>
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.
Sourcepub fn value_length(
&self,
key: &str,
) -> Result<Option<usize>, AgentRuntimeError>
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.
Sourcepub fn contains_all<'a>(
&self,
keys: impl IntoIterator<Item = &'a str>,
) -> Result<bool, AgentRuntimeError>
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.
Sourcepub fn has_any_key<'a>(
&self,
keys: impl IntoIterator<Item = &'a str>,
) -> Result<bool, AgentRuntimeError>
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.
Sourcepub fn rename(
&self,
old_key: &str,
new_key: impl Into<String>,
) -> Result<bool, AgentRuntimeError>
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.
Sourcepub fn get_many(
&self,
keys: &[&str],
) -> Result<Vec<Option<String>>, AgentRuntimeError>
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.
Sourcepub fn contains(&self, key: &str) -> Result<bool, AgentRuntimeError>
pub fn contains(&self, key: &str) -> Result<bool, AgentRuntimeError>
Return true if a value is stored under key.
Sourcepub fn get_or_default(
&self,
key: &str,
default: impl Into<String>,
) -> Result<String, AgentRuntimeError>
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.
Sourcepub fn remove(&self, key: &str) -> Result<bool, AgentRuntimeError>
pub fn remove(&self, key: &str) -> Result<bool, AgentRuntimeError>
Remove a single entry by key. Returns true if the key existed.
Sourcepub fn keys(&self) -> Result<Vec<String>, AgentRuntimeError>
pub fn keys(&self) -> Result<Vec<String>, AgentRuntimeError>
Return all keys in insertion order.
Sourcepub fn values(&self) -> Result<Vec<String>, AgentRuntimeError>
pub fn values(&self) -> Result<Vec<String>, AgentRuntimeError>
Return all values in insertion order (parallel to keys).
Sourcepub fn clear(&self) -> Result<(), AgentRuntimeError>
pub fn clear(&self) -> Result<(), AgentRuntimeError>
Remove all entries from working memory.
Sourcepub fn iter_sorted(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>
pub fn iter_sorted(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>
Sourcepub fn drain(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>
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.
Sourcepub fn snapshot(&self) -> Result<HashMap<String, String>, AgentRuntimeError>
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.
Sourcepub fn len(&self) -> Result<usize, AgentRuntimeError>
pub fn len(&self) -> Result<usize, AgentRuntimeError>
Return the current number of entries.
Sourcepub fn is_empty(&self) -> Result<bool, AgentRuntimeError>
pub fn is_empty(&self) -> Result<bool, AgentRuntimeError>
Return true if no entries are stored.
Sourcepub fn iter(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>
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.
Sourcepub fn entries(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>
pub fn entries(&self) -> Result<Vec<(String, String)>, AgentRuntimeError>
Return all key-value pairs in insertion order.
Sourcepub fn pop_oldest(&self) -> Result<Option<(String, String)>, AgentRuntimeError>
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.
Sourcepub fn peek_oldest(&self) -> Result<Option<(String, String)>, AgentRuntimeError>
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.
Sourcepub fn fill_ratio(&self) -> Result<f64, AgentRuntimeError>
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.
Sourcepub fn is_at_capacity(&self) -> Result<bool, AgentRuntimeError>
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.
Sourcepub fn remove_keys_starting_with(
&self,
prefix: &str,
) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn total_value_bytes(&self) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn max_key_length(&self) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn max_value_length(&self) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn min_value_length(&self) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn key_count_matching(
&self,
substring: &str,
) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn avg_value_length(&self) -> Result<f64, AgentRuntimeError>
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.
Sourcepub fn count_above_value_length(
&self,
min_bytes: usize,
) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn longest_key(&self) -> Result<Option<String>, AgentRuntimeError>
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).
Sourcepub fn longest_value(&self) -> Result<Option<String>, AgentRuntimeError>
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).
Sourcepub fn pairs_starting_with(
&self,
prefix: &str,
) -> Result<Vec<(String, String)>, AgentRuntimeError>
pub fn pairs_starting_with( &self, prefix: &str, ) -> Result<Vec<(String, String)>, AgentRuntimeError>
Return all (key, value) pairs whose key starts with prefix.
Sourcepub fn total_key_bytes(&self) -> Result<usize, AgentRuntimeError>
pub fn total_key_bytes(&self) -> Result<usize, AgentRuntimeError>
Return the sum of byte lengths of all stored keys.
Sourcepub fn min_key_length(&self) -> Result<usize, AgentRuntimeError>
pub fn min_key_length(&self) -> Result<usize, AgentRuntimeError>
Return the shortest key length, or 0 if the store is empty.
Sourcepub fn count_matching_prefix(
&self,
prefix: &str,
) -> Result<usize, AgentRuntimeError>
pub fn count_matching_prefix( &self, prefix: &str, ) -> Result<usize, AgentRuntimeError>
Return the number of keys that start with the given prefix.
Sourcepub fn entry_count(&self) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn value_lengths(&self) -> Result<Vec<(String, usize)>, AgentRuntimeError>
pub fn value_lengths(&self) -> Result<Vec<(String, usize)>, AgentRuntimeError>
Return a list of (key, value_byte_length) pairs for all entries.
Sourcepub fn keys_with_value_longer_than(
&self,
threshold: usize,
) -> Result<Vec<String>, AgentRuntimeError>
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.
Sourcepub fn longest_value_key(&self) -> Result<Option<String>, AgentRuntimeError>
pub fn longest_value_key(&self) -> Result<Option<String>, AgentRuntimeError>
Return the key whose associated value has the most bytes, or None if empty.
Sourcepub fn retain<F>(&self, predicate: F) -> Result<usize, AgentRuntimeError>
pub fn retain<F>(&self, predicate: F) -> Result<usize, AgentRuntimeError>
Remove all entries for which predicate(key, value) returns false.
Preserves insertion order of the surviving entries. Returns the number of entries removed.
Sourcepub fn merge_from(
&self,
other: &WorkingMemory,
) -> Result<usize, AgentRuntimeError>
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.
Sourcepub fn entry_count_satisfying<F>(
&self,
predicate: F,
) -> Result<usize, AgentRuntimeError>
pub fn entry_count_satisfying<F>( &self, predicate: F, ) -> Result<usize, AgentRuntimeError>
Return the number of entries for which predicate(key, value) returns true.
Sourcepub fn get_all_keys(&self) -> Result<Vec<String>, AgentRuntimeError>
pub fn get_all_keys(&self) -> Result<Vec<String>, AgentRuntimeError>
Return all keys in insertion order.
Sourcepub fn replace_all(
&self,
map: HashMap<String, String>,
) -> Result<(), AgentRuntimeError>
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
impl Clone for WorkingMemory
Source§fn clone(&self) -> WorkingMemory
fn clone(&self) -> WorkingMemory
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more