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: SummarizerThe 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: usizeTokens 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: usizeTokens of the newest messages a compaction the user asked for keeps as they are; the newest reply or message always stays.
clearing: ClearToolOutputsHow old tool outputs are cleared before a summary is asked for.
near_window: NearWindowWhat happens to the tool outputs a clearing near the window drops.
Implementations§
Source§impl CompactionPolicy
impl CompactionPolicy
Sourcepub fn over_threshold(&self, tokens: u64, spec: &ModelSpec) -> bool
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.
Sourcepub fn keep(&self, reason: &CompactReason, spec: Option<&ModelSpec>) -> usize
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.
Sourcepub fn cut(
&self,
messages: &[Arc<Message>],
from: usize,
reason: &CompactReason,
spec: Option<&ModelSpec>,
) -> Option<usize>
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.
Sourcepub fn plan(
&self,
state: &SummaryState,
messages: &[Arc<Message>],
from: usize,
upto: usize,
spec: &ModelSpec,
reason: &CompactReason,
) -> Result<(SummaryState, CompletionRequest), UnsupportedOption>
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
impl CompactionPolicy
Sourcepub fn cut_summary(
&self,
conversation: &Conversation,
state: &SummaryState,
reason: &CompactReason,
spec: &ModelSpec,
) -> Option<SummaryCut>
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.
Sourcepub fn plan_summary(
&self,
conversation: &Conversation,
state: &SummaryState,
reason: &CompactReason,
spec: &ModelSpec,
) -> Result<Option<SummaryPlan>, UnsupportedOption>
pub fn plan_summary( &self, conversation: &Conversation, state: &SummaryState, reason: &CompactReason, spec: &ModelSpec, ) -> Result<Option<SummaryPlan>, UnsupportedOption>
Trait Implementations§
Source§impl Clone for CompactionPolicy
impl Clone for CompactionPolicy
Source§impl Debug for CompactionPolicy
impl Debug for CompactionPolicy
Source§impl Default for CompactionPolicy
impl Default for CompactionPolicy
Source§fn default() -> Self
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.