Skip to main content

DemotingPolicyMemory

Struct DemotingPolicyMemory 

Source
pub struct DemotingPolicyMemory<M, P, H> { /* private fields */ }
Expand description

A ConversationMemory adapter that wraps a backend with a MemoryPolicy and a DemotionHook, so messages truncated by the policy flow into the hook before the active window is returned.

DemotingPolicyMemory is the bridge between the recent-turn store (InMemoryConversationMemory or any other ConversationMemory) and a long-tail store (MemvidPersistHook, vector RAG, archival storage, …). Compose it with any MemoryPolicy that overrides MemoryPolicy::apply_with_demoted; policies that rely on the default implementation will still load correctly but will never demote anything.

§Concurrency

Concurrent ConversationMemory::load calls on the same conversation_id are serialised at the demotion seam: only one call at a time delivers messages to the hook for a given conversation. Other concurrent loads for that conversation observe the in-flight delivery and return the truncated kept history immediately without firing the hook again. Pending demotions that were skipped this way are picked up by the next load after the in-flight delivery completes.

Failure visibility. A hook error is returned only to the caller whose load actually drove the delivery. Concurrent callers that short-circuited on in_flight see Ok(kept) even if the in-flight delivery ultimately failed; the watermark stays unchanged so the next load retries. Callers that rely on the hook for durability should treat a successful load as best-effort with respect to demotion and surface hook failures through the hook’s own observability (logs, metrics, dead-letter buffer) rather than the load return value.

§Persistence

Delivery watermarks are kept in process memory only. Across process restarts, the hook will receive previously-delivered demotions again; see the DemotionHook idempotency contract.

§Example

use rig_memory::{
    DemotingPolicyMemory, DemotionHook, InMemoryConversationMemory,
    MemoryError, NoopDemotionHook, SlidingWindowMemory,
};

let memory = DemotingPolicyMemory::new(
    InMemoryConversationMemory::new(),
    SlidingWindowMemory::last_messages(20),
    NoopDemotionHook,
);

Implementations§

Source§

impl<M, P, H> DemotingPolicyMemory<M, P, H>

Source

pub fn new(inner: M, policy: P, hook: H) -> Self

Wrap inner so every load runs through policy and demoted messages flow into hook.

Source

pub fn inner(&self) -> &M

Return a reference to the wrapped backend.

Source

pub fn policy(&self) -> &P

Return a reference to the wrapped policy.

Source

pub fn hook(&self) -> &H

Return a reference to the demotion hook.

Source

pub fn into_inner(self) -> (M, P, H)

Consume the wrapper and return its three components.

Source

pub fn forget(&self, conversation_id: &str)

Drop the in-process delivery watermark for conversation_id.

Call this when a conversation has ended to bound memory usage. The watermark map is otherwise unbounded — entries persist for the lifetime of the wrapper.

If the internal state lock has been poisoned by a panic in another thread, this is a no-op (the watermark will be dropped naturally when the wrapper itself is dropped).

Source

pub fn tracked_conversations(&self) -> usize

Number of conversations currently tracked in the watermark map. Useful for telemetry and leak detection. Returns 0 if the internal state lock is poisoned.

Trait Implementations§

Source§

impl<M, P, H> ConversationMemory for DemotingPolicyMemory<M, P, H>

Source§

fn load<'a>( &'a self, conversation_id: &'a str, ) -> WasmBoxedFuture<'a, Result<Vec<Message>, MemoryError>>

Load the full conversation history for conversation_id. Read more
Source§

fn append<'a>( &'a self, conversation_id: &'a str, messages: Vec<Message>, ) -> WasmBoxedFuture<'a, Result<(), MemoryError>>

Append messages to the conversation identified by conversation_id. Read more
Source§

fn clear<'a>( &'a self, conversation_id: &'a str, ) -> WasmBoxedFuture<'a, Result<(), MemoryError>>

Remove all stored messages for conversation_id.
Source§

impl<M, P, H> Debug for DemotingPolicyMemory<M, P, H>
where M: Debug, P: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M, P, H> !Freeze for DemotingPolicyMemory<M, P, H>

§

impl<M, P, H> RefUnwindSafe for DemotingPolicyMemory<M, P, H>

§

impl<M, P, H> Send for DemotingPolicyMemory<M, P, H>
where M: Send, P: Send, H: Send,

§

impl<M, P, H> Sync for DemotingPolicyMemory<M, P, H>
where M: Sync, P: Sync, H: Sync,

§

impl<M, P, H> Unpin for DemotingPolicyMemory<M, P, H>
where M: Unpin, P: Unpin, H: Unpin,

§

impl<M, P, H> UnsafeUnpin for DemotingPolicyMemory<M, P, H>

§

impl<M, P, H> UnwindSafe for DemotingPolicyMemory<M, P, H>
where M: UnwindSafe, P: UnwindSafe, H: UnwindSafe,

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> 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> 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, 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> 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> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,