pub struct Microcompactor {
pub trigger_tool_count: usize,
pub keep_recent: usize,
}Expand description
Configuration for count-based proactive pruning of old tool results.
Unlike crate::compact::Compactor (LLM-driven), this compactor requires
no provider access — it simply replaces the content of old tool-result
messages with a placeholder string when the total number of tool messages
exceeds a threshold.
§Pairing safety (invariant #8)
This compactor never removes messages — only replaces the content
field of Role::Tool messages. The tool-call ↔ tool-result pairing is
preserved because every Role::Tool message stays at its index,
immediately after the Role::Assistant message whose tool_calls list
its id.
Fields§
§trigger_tool_count: usizePrune when the number of Role::Tool messages exceeds this.
keep_recent: usizeKeep this many most-recent tool results verbatim.
Implementations§
Source§impl Microcompactor
impl Microcompactor
Sourcepub fn new(trigger_tool_count: usize, keep_recent: usize) -> Self
pub fn new(trigger_tool_count: usize, keep_recent: usize) -> Self
Create a new Microcompactor with explicit trigger and keep counts.
Sourcepub fn prune(&self, messages: &mut [Message]) -> usize
pub fn prune(&self, messages: &mut [Message]) -> usize
Prune oldest tool-result contents in place. Returns the number pruned.
Does NOT remove messages — only replaces content of Role::Tool
messages older than keep_recent when total tool count exceeds
trigger_tool_count. Tool messages shorter than MIN_PRUNE_LENGTH
are left untouched (not worth the placeholder swap).
§Logic
- Collect indices of all
Role::Toolmessages. - If
count <= self.trigger_tool_count, return0. - Otherwise, the candidates for pruning are all tool messages
except the last
self.keep_recent. Iterate them oldest-first; for each whosecontent.len() > MIN_PRUNE_LENGTHand whose content is not already the placeholder, replacecontentwithMICROCOMPACT_PLACEHOLDER. Stop oncecount - pruned <= self.trigger_tool_count(prune only enough to get back under the trigger — don’t prune everything eligible). - Return the number pruned.