pub struct State { /* private fields */ }Expand description
A concurrent, type-safe state container that agents read from and write to.
By default, set() writes directly to the inner store. When delta tracking
is enabled via with_delta_tracking(), writes go to a separate delta map
(with tombstones) that can be atomically committed or rolled back.
Implementations§
Source§impl State
impl State
Sourcepub fn with_delta_tracking(&self) -> State
pub fn with_delta_tracking(&self) -> State
Create a new State with delta tracking enabled. Writes go to the delta map; reads check delta first, then inner.
Sourcepub fn set_journal_sink(&self, sink: Arc<dyn JournalSink>)
pub fn set_journal_sink(&self, sink: Arc<dyn JournalSink>)
Install a durable JournalSink that receives every state mutation.
The sink is shared with all clones and delta views of this State
(like the in-memory ring) and is invoked synchronously on the write
path — keep it cheap. The in-memory ring keeps serving
recent_mutations/evidence;
the sink adds unbounded durability.
Sourcepub fn with_journal_sink(self, sink: Arc<dyn JournalSink>) -> Self
pub fn with_journal_sink(self, sink: Arc<dyn JournalSink>) -> Self
Builder-style variant of set_journal_sink.
Sourcepub fn set_clock(&self, clock: SharedClock)
pub fn set_clock(&self, clock: SharedClock)
Replace the Clock this state and everything
sharing it read the time from. Defaults to the system clock.
The clock is shared with every clone and delta view, like the journal sink. Swap it before a session starts; components that captured an instant from the old clock compare it against the new one.
Sourcepub fn with_clock(self, clock: SharedClock) -> Self
pub fn with_clock(self, clock: SharedClock) -> Self
Builder-style variant of set_clock.
Sourcepub fn clock(&self) -> SharedClock
pub fn clock(&self) -> SharedClock
The clock this state reads the time from.
Sourcepub fn redact_keys<I, S>(&self, keys: I)
pub fn redact_keys<I, S>(&self, keys: I)
Mark keys as sensitive: wherever this state’s values leave the
process, a redacted key’s value is replaced by REDACTED. That
covers the durable journal sink, persistence snapshots
(to_redacted_hashmap) and the runtime’s
extraction events. Reads inside the process (get, guards, tools)
still see the real value.
A key k also covers its scoped forms: app:k, user:k,
state_meta:k, and any other prefix:k. Shared with clones and
delta views. Adds to the keys already marked.
Sourcepub fn redacted_keys(&self) -> BTreeSet<String>
pub fn redacted_keys(&self) -> BTreeSet<String>
The keys marked sensitive with redact_keys.
Sourcepub fn is_redacted(&self, key: &str) -> bool
pub fn is_redacted(&self, key: &str) -> bool
Whether key (or the base key under its scope prefix) is marked
sensitive.
Sourcepub fn redact_value(&self, key: &str, value: &Value) -> Value
pub fn redact_value(&self, key: &str, value: &Value) -> Value
value as it may leave the process under key.
Sourcepub fn redact_fields(&self, value: &Value) -> Value
pub fn redact_fields(&self, value: &Value) -> Value
Mask the redacted fields of a JSON object (one level deep); other values pass through.
Sourcepub fn to_redacted_hashmap(&self) -> HashMap<String, Value>
pub fn to_redacted_hashmap(&self) -> HashMap<String, Value>
to_hashmap with redacted keys masked: what a
persistence snapshot or an export should hold.
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Get a value by key, attempting to deserialize to the requested type. When delta tracking is enabled, checks delta first, then inner.
This is the lenient read: a value that is present but of the wrong
type is reported as None, indistinguishable from an absent key. Use
try_get when that distinction matters.
Sourcepub fn try_get<T: DeserializeOwned>(
&self,
key: &str,
) -> Result<Option<T>, StateError>
pub fn try_get<T: DeserializeOwned>( &self, key: &str, ) -> Result<Option<T>, StateError>
Get a value by key, distinguishing “absent” from “present but the wrong type”.
Returns Ok(None) when no value is stored at key (after the same
delta → inner → derived: lookup as get), Ok(Some(v))
when the stored value deserializes to T, and
StateError::WrongType when a value exists but does not. This is the
strict read; get is the lenient form that folds the
error case into None.
Sourcepub fn with<F, R>(&self, key: &str, f: F) -> Option<R>
pub fn with<F, R>(&self, key: &str, f: F) -> Option<R>
Borrow a value by key without cloning, applying f to the reference.
This is the zero-copy alternative to get_raw(). The closure receives
a &Value directly from the DashMap ref-guard, avoiding the
Value::clone() + serde_json::from_value() overhead of get().
Lookup order: delta (if tracking) → inner → derived fallback.
Sourcepub fn get_raw(&self, key: &str) -> Option<Value>
pub fn get_raw(&self, key: &str) -> Option<Value>
Get a raw JSON value by key.
When delta tracking is enabled, checks delta first, then inner.
If the key is not found and doesn’t contain a prefix, also checks derived:{key}
as a transparent fallback for computed variables.
Sourcepub fn get_key<T: DeserializeOwned>(&self, key: &StateKey<T>) -> Option<T>
pub fn get_key<T: DeserializeOwned>(&self, key: &StateKey<T>) -> Option<T>
Get a typed value using a StateKey<T> (lenient — a wrong-typed value
reads as None; see get).
Sourcepub fn try_get_key<T: DeserializeOwned>(
&self,
key: &StateKey<T>,
) -> Result<Option<T>, StateError>
pub fn try_get_key<T: DeserializeOwned>( &self, key: &StateKey<T>, ) -> Result<Option<T>, StateError>
Get a typed value using a StateKey<T>, distinguishing “absent” from
“present but the wrong type” (see try_get).
Sourcepub fn set_key<T: Serialize>(
&self,
key: &StateKey<T>,
value: T,
) -> Result<(), StateError>
pub fn set_key<T: Serialize>( &self, key: &StateKey<T>, value: T, ) -> Result<(), StateError>
Set a typed value using a StateKey<T>.
Returns StateError if value cannot be serialized to JSON.
Sourcepub fn with_key<T, F, R>(&self, key: &StateKey<T>, f: F) -> Option<R>
pub fn with_key<T, F, R>(&self, key: &StateKey<T>, f: F) -> Option<R>
Zero-copy borrow using a StateKey<T>.
Sourcepub fn set(
&self,
key: impl Into<String>,
value: impl Serialize,
) -> Result<(), StateError>
pub fn set( &self, key: impl Into<String>, value: impl Serialize, ) -> Result<(), StateError>
Set a value by key.
When delta tracking is enabled, writes to the delta view instead of the
committed store. Returns StateError if value cannot be serialized
to JSON — a public SDK write never panics on caller data.
Sourcepub fn set_committed(
&self,
key: impl Into<String>,
value: impl Serialize,
) -> Result<(), StateError>
pub fn set_committed( &self, key: impl Into<String>, value: impl Serialize, ) -> Result<(), StateError>
Set a value directly in the committed store, bypassing delta tracking.
Returns StateError if value cannot be serialized to JSON.
Sourcepub fn modify<T, F>(&self, key: &str, default: T, f: F) -> Result<T, StateError>
pub fn modify<T, F>(&self, key: &str, default: T, f: F) -> Result<T, StateError>
Atomically read-modify-write a value under a per-key lock.
If the key doesn’t exist, default is used as the initial value. The
function f receives the current value and returns the new value. The
read-modify-write is performed while holding the map shard for key, so
concurrent modify calls on the same key do not lose updates. Returns
the new value, or StateError if it cannot be serialized.
Sourcepub fn contains(&self, key: &str) -> bool
pub fn contains(&self, key: &str) -> bool
Check if a key exists (in delta or inner).
Applies the same transparent derived: fallback as Self::get,
Self::get_raw and Self::with: an unprefixed key also matches the
computed variable derived:{key}. Flow predicates (is_set, captured)
evaluate through this method, so without the fallback a computed value
would read as permanently unknown while get returned it fine.
Sourcepub fn remove(&self, key: &str) -> Option<Value>
pub fn remove(&self, key: &str) -> Option<Value>
Remove a key.
In delta-tracking mode this records a tombstone in the delta view and
leaves the committed store untouched, so a subsequent rollback() fully
restores the base state. Returns the value that was visible before removal.
Sourcepub fn keys(&self) -> Vec<String>
pub fn keys(&self) -> Vec<String>
Get all keys (from both inner and delta when tracking).
Keys tombstoned in the delta are excluded.
Sourcepub fn pick(&self, keys: &[&str]) -> State
pub fn pick(&self, keys: &[&str]) -> State
Create a new State containing only the specified keys.
Sourcepub fn merge(&self, other: &State)
pub fn merge(&self, other: &State)
Merge another state into this one (other’s values overwrite on conflict).
Sourcepub fn is_tracking_delta(&self) -> bool
pub fn is_tracking_delta(&self) -> bool
Whether delta tracking is enabled.
Sourcepub fn delta(&self) -> HashMap<String, Value>
pub fn delta(&self) -> HashMap<String, Value>
Get a snapshot of the current delta’s pending writes (tombstones omitted).
Sourcepub fn commit(&self)
pub fn commit(&self)
Commit delta changes into the inner store, then clear the delta.
Pending puts are applied and tombstones remove the committed key, so a removal made under delta tracking becomes durable only at commit time.
Sourcepub fn rollback(&self)
pub fn rollback(&self)
Discard all uncommitted delta changes, restoring the committed base state.
Because removals and prefix clears under delta tracking only write
tombstones (never mutating inner), dropping the delta is sufficient to
restore the base — including keys that were removed in the transaction.
Sourcepub fn app(&self) -> PrefixedState<'_>
pub fn app(&self) -> PrefixedState<'_>
Access state with the app: prefix scope.
Sourcepub fn user(&self) -> PrefixedState<'_>
pub fn user(&self) -> PrefixedState<'_>
Access state with the user: prefix scope.
Sourcepub fn temp(&self) -> PrefixedState<'_>
pub fn temp(&self) -> PrefixedState<'_>
Access state with the temp: prefix scope.
Sourcepub fn session(&self) -> PrefixedState<'_>
pub fn session(&self) -> PrefixedState<'_>
Access state with the session: prefix scope (auto-tracked signals).
Sourcepub fn turn(&self) -> PrefixedState<'_>
pub fn turn(&self) -> PrefixedState<'_>
Access state with the turn: prefix scope (reset each turn).
Sourcepub fn bg(&self) -> PrefixedState<'_>
pub fn bg(&self) -> PrefixedState<'_>
Access state with the bg: prefix scope (background tasks).
Sourcepub fn derived(&self) -> ReadOnlyPrefixedState<'_>
pub fn derived(&self) -> ReadOnlyPrefixedState<'_>
Access read-only state with the derived: prefix scope (computed vars only).
Sourcepub fn snapshot_values(&self, keys: &[&str]) -> HashMap<String, Value>
pub fn snapshot_values(&self, keys: &[&str]) -> HashMap<String, Value>
Snapshot the values of specific keys. Returns HashMap of key -> current value. Used by watchers to capture state before mutations.
Sourcepub fn diff_values(
&self,
prev: &HashMap<String, Value>,
keys: &[&str],
) -> Vec<(String, Value, Value)>
pub fn diff_values( &self, prev: &HashMap<String, Value>, keys: &[&str], ) -> Vec<(String, Value, Value)>
Diff current state against a previous snapshot. Returns Vec of (key, old_value, new_value) for keys that changed.
Sourcepub fn to_hashmap(&self) -> HashMap<String, Value>
pub fn to_hashmap(&self) -> HashMap<String, Value>
Export all state as a HashMap (for persistence/serialization).
Sourcepub fn from_hashmap(&self, map: HashMap<String, Value>)
pub fn from_hashmap(&self, map: HashMap<String, Value>)
Restore state from a HashMap (for persistence/deserialization).
Sourcepub fn clear_prefix(&self, prefix: &str)
pub fn clear_prefix(&self, prefix: &str)
Remove all keys with the given prefix.
In delta-tracking mode this writes tombstones for matching keys (from both
the committed store and pending delta puts) without mutating the committed
store, so rollback() restores everything that was cleared.
Sourcepub fn recent_mutations(&self) -> Vec<StateMutation>
pub fn recent_mutations(&self) -> Vec<StateMutation>
Return a snapshot of recent state mutations.
Sourcepub fn mutation_cursor(&self) -> u64
pub fn mutation_cursor(&self) -> u64
Return the current monotonic cursor for the mutation journal.
Sourcepub fn mutations_since(&self, cursor: u64) -> Vec<StateMutation>
pub fn mutations_since(&self, cursor: u64) -> Vec<StateMutation>
Return mutations appended after a previously captured cursor.
Sourcepub fn try_mutations_since(&self, cursor: u64) -> Option<Vec<StateMutation>>
pub fn try_mutations_since(&self, cursor: u64) -> Option<Vec<StateMutation>>
Mutations appended after cursor, or None if the bounded journal
has already dropped some of them.
A consumer that must see every change (a derived-value cache, say)
uses this instead of mutations_since and
falls back to a full rescan on None.
Sourcepub fn drain_mutations(&self) -> Vec<StateMutation>
pub fn drain_mutations(&self) -> Vec<StateMutation>
Drain and return all recorded state mutations.
Sourcepub fn evidence(&self, key: &str) -> SlotEvidence
pub fn evidence(&self, key: &str) -> SlotEvidence
Aggregate the SlotEvidence for a key: its current value, provenance
(state_meta:{key}), confidence, and most-recent journal write.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for State
impl !UnwindSafe for State
impl Freeze for State
impl Send for State
impl Sync for State
impl Unpin for State
impl UnsafeUnpin for State
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.