pub struct CompactionConfig {
pub auto_enabled: bool,
pub trigger_mode: CompactionTriggerMode,
pub threshold: CompactionThreshold,
pub reserve_tokens: u64,
pub preserve: PreserveConfig,
pub prune: PruneConfig,
pub strategy: CompactionStrategy,
pub auto_continue: bool,
pub max_consecutive_failures: u32,
pub model: Option<AgentModelRef>,
pub summary_max_tokens: Option<u32>,
}Expand description
上下文压缩完整配置。
控制 Agent loop 何时触发压缩、如何保留近期上下文、是否修剪旧内容、 以及压缩失败时的熔断行为。
§配置合并优先级
AgentDefinition.compaction > SessionConfig.compaction > 全局默认§Examples
use katu_core::compaction::CompactionConfig;
// 默认配置:自动压缩开启,threshold 模式
let config = CompactionConfig::default();
assert!(config.auto_enabled);
// 禁用自动压缩
let manual_only = CompactionConfig::default().with_auto_enabled(false);
assert!(!manual_only.auto_enabled);
// 只在 overflow 时被动压缩(opencode 风格)
use katu_core::compaction::CompactionTriggerMode;
let passive = CompactionConfig::default()
.with_trigger_mode(CompactionTriggerMode::Overflow);Fields§
§auto_enabled: bool是否启用自动压缩。
false 时仅支持手动触发(如 /compact 命令)。
三个参考项目都默认开启。
trigger_mode: CompactionTriggerMode触发模式 — 何时启动自动压缩。
threshold: CompactionThreshold阈值配置 — 在 Threshold 模式下生效。
reserve_tokens: u64为输出预留的 token 缓冲。
阈值 fallback 计算: threshold = context_window - reserve_tokens。 同时确保压缩过程本身不会因为摘要输出而 overflow。
- oh-my-pi: 16,384
- opencode: min(20,000, max_output_tokens)
- claude-code: 13,000 (auto) / 3,000 (manual)
preserve: PreserveConfig消息保留策略 — 压缩时哪些近期内容保持原文不总结。
prune: PruneConfig旧工具输出修剪配置。
独立于压缩的轻量级优化:截断旧工具调用的输出内容, 释放 token 空间,延迟全量压缩的触发。 来源: opencode 的 prune 机制。
strategy: CompactionStrategy压缩策略 — 如何处理旧消息。
auto_continue: bool压缩完成后是否自动继续 Agent loop。
true: 压缩后自动发送 “continue” 消息继续执行。 false: 压缩后等待用户输入。 oh-my-pi 和 opencode 都默认 true。
max_consecutive_failures: u32连续失败熔断次数。
连续 N 次自动压缩失败后停止尝试,防止无限循环。 手动压缩不受此限制。 来源: claude-code 的 MAX_CONSECUTIVE_AUTOCOMPACT_FAILURES = 3。 0 = 不限制。
model: Option<AgentModelRef>用于执行压缩摘要的模型。
None = 使用 Agent 当前的主模型。 来源: opencode 有独立的 “compaction” agent 配置。
summary_max_tokens: Option<u32>摘要的最大输出 token 数。
限制 LLM 生成摘要时的输出长度。 来源: claude-code MAX_OUTPUT_TOKENS_FOR_SUMMARY = 20,000。
Implementations§
Source§impl CompactionConfig
impl CompactionConfig
Sourcepub fn with_auto_enabled(self, enabled: bool) -> Self
pub fn with_auto_enabled(self, enabled: bool) -> Self
设置自动压缩开关。
Sourcepub fn with_trigger_mode(self, mode: CompactionTriggerMode) -> Self
pub fn with_trigger_mode(self, mode: CompactionTriggerMode) -> Self
设置触发模式。
Sourcepub fn with_threshold(self, threshold: CompactionThreshold) -> Self
pub fn with_threshold(self, threshold: CompactionThreshold) -> Self
设置阈值配置。
Sourcepub fn with_reserve_tokens(self, tokens: u64) -> Self
pub fn with_reserve_tokens(self, tokens: u64) -> Self
设置预留 token 数。
Sourcepub fn with_preserve(self, preserve: PreserveConfig) -> Self
pub fn with_preserve(self, preserve: PreserveConfig) -> Self
设置消息保留策略。
Sourcepub fn with_prune(self, prune: PruneConfig) -> Self
pub fn with_prune(self, prune: PruneConfig) -> Self
设置修剪配置。
Sourcepub fn with_strategy(self, strategy: CompactionStrategy) -> Self
pub fn with_strategy(self, strategy: CompactionStrategy) -> Self
设置压缩策略。
Sourcepub fn with_auto_continue(self, auto_continue: bool) -> Self
pub fn with_auto_continue(self, auto_continue: bool) -> Self
设置是否压缩后自动继续。
Sourcepub fn with_max_consecutive_failures(self, max: u32) -> Self
pub fn with_max_consecutive_failures(self, max: u32) -> Self
设置连续失败熔断次数。
Sourcepub fn with_model(self, model: AgentModelRef) -> Self
pub fn with_model(self, model: AgentModelRef) -> Self
设置压缩模型。
Sourcepub fn with_summary_max_tokens(self, tokens: u32) -> Self
pub fn with_summary_max_tokens(self, tokens: u32) -> Self
设置摘要最大输出 token 数。
Trait Implementations§
Source§impl Clone for CompactionConfig
impl Clone for CompactionConfig
Source§fn clone(&self) -> CompactionConfig
fn clone(&self) -> CompactionConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CompactionConfig
impl Debug for CompactionConfig
Source§impl Default for CompactionConfig
impl Default for CompactionConfig
Source§impl<'de> Deserialize<'de> for CompactionConfig
impl<'de> Deserialize<'de> for CompactionConfig
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for CompactionConfig
impl PartialEq for CompactionConfig
Source§fn eq(&self, other: &CompactionConfig) -> bool
fn eq(&self, other: &CompactionConfig) -> bool
self and other values to be equal, and is used by ==.