Skip to main content

trim_history

Function trim_history 

Source
pub fn trim_history(
    system: Option<Message>,
    history: Vec<Message>,
    strategy: &ContextStrategy,
) -> Vec<Message>
Expand description

Trim history under strategy, keeping system pinned at index 0.

This is the structural (message-count) half of history reduction. The system message is never passed to a reducer, so no strategy can drop the agent’s identity, its policy summary, or its skill overlays; only conversation turns are reduced. The pinned message is prepended to the result when present.

§Examples

use universal_agent_runtime::llm::{Message, MessageContent, MessageRole};
use universal_agent_runtime::uar::context::{trim_history, ContextStrategy};

let system = Message {
    role: MessageRole::System,
    content: MessageContent::text("identity"),
    tool_call_id: None,
    tool_calls: None,
};
let history: Vec<Message> = (0..40)
    .map(|i| Message {
        role: MessageRole::User,
        content: MessageContent::text(format!("turn-{i}")),
        tool_call_id: None,
        tool_calls: None,
    })
    .collect();

let out = trim_history(
    Some(system),
    history,
    &ContextStrategy::SlidingWindow { max_messages: 10 },
);
assert_eq!(out.len(), 11);
assert_eq!(out[0].role, MessageRole::System);