Skip to main content

CompactionConfig

Struct CompactionConfig 

Source
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

Source

pub fn with_auto_enabled(self, enabled: bool) -> Self

设置自动压缩开关。

Source

pub fn with_trigger_mode(self, mode: CompactionTriggerMode) -> Self

设置触发模式。

Source

pub fn with_threshold(self, threshold: CompactionThreshold) -> Self

设置阈值配置。

Source

pub fn with_reserve_tokens(self, tokens: u64) -> Self

设置预留 token 数。

Source

pub fn with_preserve(self, preserve: PreserveConfig) -> Self

设置消息保留策略。

Source

pub fn with_prune(self, prune: PruneConfig) -> Self

设置修剪配置。

Source

pub fn with_strategy(self, strategy: CompactionStrategy) -> Self

设置压缩策略。

Source

pub fn with_auto_continue(self, auto_continue: bool) -> Self

设置是否压缩后自动继续。

Source

pub fn with_max_consecutive_failures(self, max: u32) -> Self

设置连续失败熔断次数。

Source

pub fn with_model(self, model: AgentModelRef) -> Self

设置压缩模型。

Source

pub fn with_summary_max_tokens(self, tokens: u32) -> Self

设置摘要最大输出 token 数。

Trait Implementations§

Source§

impl Clone for CompactionConfig

Source§

fn clone(&self) -> CompactionConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CompactionConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CompactionConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for CompactionConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for CompactionConfig

Source§

fn eq(&self, other: &CompactionConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for CompactionConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for CompactionConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,