Skip to main content

CompactionPolicy

Struct CompactionPolicy 

Source
pub struct CompactionPolicy {
    pub summarizer: Summarizer,
    pub tracked: Vec<TrackArgument>,
    pub keep_recent: usize,
    pub keep_asked: usize,
    pub clearing: ClearToolOutputs,
    pub near_window: NearWindow,
}
Expand description

How a conversation is compacted: what the summarizer is asked, the tool arguments a summary keeps track of, such as the files a coding agent’s file tools read and changed, how much of the newest conversation stays as it is, and how old tool outputs are cleared first. Tokens are estimated by the default HeuristicTokenCounter.

Its serde form is one flat table, the summarizer’s prompts and limits beside the other fields; each field missing is the default’s, and an unknown one is refused.

use rig_memory::CompactionPolicy;

let policy: CompactionPolicy =
    serde_json::from_value(serde_json::json!({"keep_recent": 1000, "limits": {"reserve": 1}}))?;
assert_eq!(policy.keep_recent, 1000);
assert_eq!(policy.summarizer.limits.reserve, 1);
assert_eq!(policy.keep_asked, CompactionPolicy::default().keep_asked);

Fields§

§summarizer: Summarizer

The summarizer; its reserve is how much of the model’s window is kept free.

§tracked: Vec<TrackArgument>

The tool arguments tracked across compactions.

§keep_recent: usize

Tokens of the newest messages an automatic compaction keeps as they are, a quarter of the model’s window at most: the work carries on, so its recent part stays.

§keep_asked: usize

Tokens of the newest messages a compaction the user asked for keeps as they are; the newest reply or message always stays.

§clearing: ClearToolOutputs

How old tool outputs are cleared before a summary is asked for.

§near_window: NearWindow

What happens to the tool outputs a clearing near the window drops.

Implementations§

Source§

impl CompactionPolicy

Source

pub fn over_threshold(&self, tokens: u64, spec: &ModelSpec) -> bool

Whether a request of tokens leaves less than the summarizer’s reserve of spec’s window free. Never, when the window is not known.

Source

pub fn keep(&self, reason: &CompactReason, spec: Option<&ModelSpec>) -> usize

Tokens of the newest messages a compaction for reason with spec keeps as they are.

Source

pub fn cut( &self, messages: &[Arc<Message>], from: usize, reason: &CompactReason, spec: Option<&ModelSpec>, ) -> Option<usize>

Where a compaction for reason should end: the index of the first message it keeps, after the newest messages within keep and never between a tool call and its result, or None when nothing new would be summarized. from is how many leading messages are summaries already (summarized_prefix). A short conversation is still summarized when the user asked or the model overflowed.

Source

pub fn plan( &self, state: &SummaryState, messages: &[Arc<Message>], from: usize, upto: usize, spec: &ModelSpec, reason: &CompactReason, ) -> Result<(SummaryState, CompletionRequest), UnsupportedOption>

The summary request for a compaction ending at upto, and the SummaryState the new summary goes into (with the tracked arguments of the newly summarized messages).

Source§

impl CompactionPolicy

Source

pub fn cut_summary( &self, conversation: &Conversation, state: &SummaryState, reason: &CompactReason, spec: &ModelSpec, ) -> Option<SummaryCut>

cut for conversation, with the state the summary goes into. None when nothing new would be summarized.

Source

pub fn plan_summary( &self, conversation: &Conversation, state: &SummaryState, reason: &CompactReason, spec: &ModelSpec, ) -> Result<Option<SummaryPlan>, UnsupportedOption>

cut and plan in one step, for conversation. Returns None when nothing new would be summarized.

§Errors

When spec does not take the summary request’s options.

Trait Implementations§

Source§

impl Clone for CompactionPolicy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for CompactionPolicy

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for CompactionPolicy

Source§

fn default() -> Self

Summarizer::DEFAULT, nothing tracked, the newest 20k tokens kept by an automatic compaction and none beyond the newest reply or message by an asked one, ClearToolOutputs::default, and notes on what clearing drops near the window.

Source§

impl<'de> Deserialize<'de> for CompactionPolicy

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for CompactionPolicy

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for CompactionPolicy

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for CompactionPolicy

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoToolOutput for T
where T: Serialize + 'static,

Source§

fn into_tool_output(self) -> Result<ToolOutput, ToolExecutionError>

Convert this value without routing structured data through a string.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

Source§

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

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