pub trait CtxBuilder: Send + Sync {
// Required methods
fn build_messages(
&self,
conv: &Conversation,
system_prompt: &str,
turn_reminder: &str,
) -> (Vec<Message>, ContextStats);
fn truncate_tool_output(&self, result: &mut ToolResult, tool_name: &str);
fn ctx_window(&self) -> usize;
fn name(&self) -> &'static str;
// Provided methods
fn needs_compression(
&self,
_conv: &Conversation,
_system_tokens: usize,
) -> bool { ... }
fn compression_plan(&self, _conv: &Conversation) -> Option<(String, usize)> { ... }
}Expand description
Per-session context construction strategy. Selected once at
AgentLoop::new via for_provider and rebuilt on ReloadConfig.
Required Methods§
Sourcefn build_messages(
&self,
conv: &Conversation,
system_prompt: &str,
turn_reminder: &str,
) -> (Vec<Message>, ContextStats)
fn build_messages( &self, conv: &Conversation, system_prompt: &str, turn_reminder: &str, ) -> (Vec<Message>, ContextStats)
Build the messages array to send to the LLM for this turn.
Implementations are free to:
- Transform
system_prompt(strip tool schemas for small models, add cache-friendly markers for Claude, replace entirely for fine-tuned models) - Choose any render pipeline (delegate to
crate::ctx::render::build_messages, call shared helpers directly, or roll their own) - Decide how to handle
turn_reminder— per-turn dynamic context (git status, current task, prev edited files). The default policy prepends it to the last User message for system-prompt cache stability; a small-window model might drop it to save tokens; a cache-sensitive model might insert it as its own System message to keep the stable prefix clean. Pass""when no reminder applies.
Sourcefn truncate_tool_output(&self, result: &mut ToolResult, tool_name: &str)
fn truncate_tool_output(&self, result: &mut ToolResult, tool_name: &str)
Truncate a single tool output in place.
Sourcefn ctx_window(&self) -> usize
fn ctx_window(&self) -> usize
Effective token budget for this strategy.
Reflects any defensive clamps the impl applies (e.g. OllamaCtx
floors at 4K even if provider.context_window == 0). Callers
that need the actual budget — ctx_budget_hint reset, datalog,
per-tool truncation — should use this instead of
Config::default_context_window(), which returns the raw,
unclamped value and may diverge for degenerate configs.
Provided Methods§
Sourcefn needs_compression(&self, _conv: &Conversation, _system_tokens: usize) -> bool
fn needs_compression(&self, _conv: &Conversation, _system_tokens: usize) -> bool
Whether the conversation should be compressed. Default: never.
Sourcefn compression_plan(&self, _conv: &Conversation) -> Option<(String, usize)>
fn compression_plan(&self, _conv: &Conversation) -> Option<(String, usize)>
Produce a compression plan (content_to_summarize, messages_to_remove). Default: None (no compression).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".