Skip to main content

StateStore

Struct StateStore 

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

Thread-safe state store with transition logging.

All reads and writes go through this store. Every write produces a StateTransition record for audit and replay. Optionally backed by a JSONL journal file for durability across process restarts (see StateStore::durable).

Implementations§

Source§

impl StateStore

Source

pub fn new() -> StateStore

Source

pub fn durable(path: impl Into<PathBuf>) -> Result<StateStore, Error>

Open a durable, JSONL-backed StateStore. If the file exists, its transitions are replayed (last-write-wins per key, with TTLs honored) to rebuild current state. Subsequent writes append to the same file.

Returns an error only on filesystem-level failures (parent directory missing, permission denied, etc.). Malformed lines inside the journal are skipped with a warning rather than failing the open — agent persistence shouldn’t refuse to start over a single bad line.

Source

pub fn sync(&self) -> Result<(), Error>

Fsync the journal writer. Call after a batch of writes when you need durability guarantees beyond best-effort flush.

Source

pub fn reap_expired(&self, now: DateTime<Utc>) -> Result<Vec<String>, Error>

Drop expired keys (per ttl_secs on their last write) and rewrite the journal as a compacted snapshot of the surviving state. Returns the keys that were reaped.

TTL semantics: a ttl_secs of 0 means “expired immediately” — the key is reapable on the next call. There is no “0 = forever” sentinel; use set (no TTL) for keys that should never auto-expire.

Latest-write-wins: a key rewritten WITHOUT a TTL after a TTL’d write is NOT reaped — the more recent write effectively cancels the TTL.

Single-pass over the transitions log via a key→latest index, so cost is O(n) in journal length (not O(n²)).

Source

pub fn get(&self, key: &str) -> Option<Value>

Source

pub fn get_or(&self, key: &str, default: Value) -> Value

Source

pub fn exists(&self, key: &str) -> bool

Source

pub fn set(&self, key: &str, value: Value, action_id: &str) -> StateTransition

Source

pub fn set_with_ttl( &self, key: &str, value: Value, action_id: &str, ttl_secs: u64, ) -> StateTransition

Set a key with a TTL (seconds from now). reap_expired drops the key once the deadline passes; re-setting the key without a TTL (set) cancels the TTL.

ttl_secs == 0 means “expire immediately” (reapable on the next reap_expired call). It is NOT a “no expiry” sentinel — use the plain set(...) method for keys that should never auto-expire. This differs from the Unix/Redis convention; the distinction matters because a TTL passed from untrusted input could otherwise silently mean “forever” when the caller intended “never store.”

Source

pub fn delete(&self, key: &str, action_id: &str) -> Option<StateTransition>

Source

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

Deep clone of current state.

Source

pub fn restore(&self, snapshot: HashMap<String, Value>, transition_count: usize)

Restore state from a snapshot, truncating transitions.

Source

pub fn transition_count(&self) -> usize

Source

pub fn transitions(&self) -> Vec<StateTransition>

Source

pub fn transitions_since(&self, index: usize) -> Vec<StateTransition>

Source

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

Source

pub fn replace_all(&self, snapshot: HashMap<String, Value>)

Replace the entire state map without recording transitions. Used by checkpoint restore to avoid synthetic transition history. Also clears the transitions log so callers of transitions_since() don’t see stale history from the discarded state.

Source

pub fn scoped<'a>(&'a self, tenant: Option<&'a str>) -> ScopedStateView<'a>

Build a tenant-scoped view over this store (Parslee-ai/car#187 phase 3 enforcement).

All reads / writes go through tenant:<tenant_id>:<key> so distinct tenants can’t see each other’s keys. tenant = None returns a view that hits the unscoped (legacy) namespace — callers that don’t yet have a RuntimeScope get pre-#187 behaviour automatically.

Cheap to construct; holds a &self borrow plus the tenant string. The view’s methods take the parking-lot lock the same way the unscoped methods do.

Trait Implementations§

Source§

impl Default for StateStore

Source§

fn default() -> StateStore

Returns the “default value” for a type. Read more
Source§

impl StateView for StateStore

Source§

fn get_value(&self, key: &str) -> Option<Value>

Get a value by key. Returns None if the key doesn’t exist.
Source§

fn key_exists(&self, key: &str) -> bool

Check if a key exists in state.
Source§

fn is_unknown(&self, _key: &str) -> bool

Whether a key’s value is unknown (symbolic analysis only). Returns false by default — runtime state is always known.

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

impl<T> ErasedDestructor for T
where T: 'static,