Skip to main content

compact_split

Function compact_split 

Source
pub async fn compact_split(
    messages: &[Message],
    provider: &dyn LlmProvider,
    keep_recent_turns: usize,
    compact_to_tokens: u64,
) -> Result<(Vec<Message>, Vec<Message>)>
Expand description

Hierarchical compaction used when primary compact() fails.

Strategy:

  1. Isolate the recent verbatim tail (last keep_recent_turns turns).
  2. Split the remaining older messages into two halves.
  3. Summarise each half independently with the LLM (two API calls).
  4. Combine both summaries + recent tail into the new window.
  5. Trim new_window to ≤ 50 % of context_window.

Because each half is at most ~50 % of the full history, the summarisation requests are well within the context window even when the full history is not.