pub struct CompactionConfig {
pub token_threshold: usize,
pub keep_last_messages: usize,
}Expand description
Tuning knobs for non-destructive context compaction (#2070, §5.4).
Why: A caller (production: AgentLoop; tests: unit tests exercising
Transcript directly) needs to control both “how big is too big” and
“how much of the recent conversation counts as the active work zone” —
bundling them avoids two separate parameters threading through every call
site.
What: token_threshold is compared against the transcript’s
[estimate_tokens] total; keep_last_messages is the number of the most
recent messages (the active work zone, §5.4 item 4) that are never
eligible for compaction regardless of size.
Test: compaction::tests::default_is_sane.
Fields§
§token_threshold: usizeEstimated-token threshold that triggers compaction once exceeded.
keep_last_messages: usizeNumber of most-recent messages kept verbatim (the active work zone).
Implementations§
Source§impl CompactionConfig
impl CompactionConfig
Sourcepub fn for_context_window(context_window: usize) -> Self
pub fn for_context_window(context_window: usize) -> Self
Derive a CompactionConfig proportional to a model’s real context
window (#2308).
Why: This is the production constructor — the flat, model-blind
Default threshold (see that impl’s doc) is the root cause of #2308’s
pathological re-compaction. Every production call site that knows its
model slug must resolve the model’s real context window (via
crate::provider::resolve_context_window) and build its
CompactionConfig through this constructor instead.
What: token_threshold is context_window *
[COMPACTION_THRESHOLD_FRACTION_PCT] / 100; keep_last_messages is
unchanged from the previous default (6).
Test: compaction::tests::for_context_window_scales_threshold_proportionally,
compaction::tests::should_compact_does_not_trigger_at_10k_tokens_against_200k_window,
compaction::tests::should_compact_triggers_near_75pct_of_200k_window.
Trait Implementations§
Source§impl Clone for CompactionConfig
impl Clone for CompactionConfig
impl Copy for CompactionConfig
Source§impl Debug for CompactionConfig
impl Debug for CompactionConfig
Source§impl Default for CompactionConfig
impl Default for CompactionConfig
Source§fn default() -> Self
fn default() -> Self
LEGACY/TEST-ONLY default — a flat, model-blind 6_000-token threshold.
Why: This was the pre-#2308 production default; the root cause of
#2308’s pathological re-compaction (roughly every 2 turns at
4-13K estimated tokens) is exactly this hard-coded constant, which is
only ~3% of a real Bedrock Claude Sonnet 200K-token context window.
Kept for back-compat (tests that want a small, deterministic
threshold without wiring a model slug) but production code must call
CompactionConfig::for_context_window instead so the threshold
scales with the model actually in use.
What: §5.4 gives no exact numbers; these defaults keep several full
tool-call round-trips (a handful of assistant+tool pairs) uncompacted
while triggering well before a typical model’s context window is at
risk — but ONLY for a small/unknown context window; see
CompactionConfig::for_context_window for the model-aware
production path.
Auto Trait Implementations§
impl Freeze for CompactionConfig
impl RefUnwindSafe for CompactionConfig
impl Send for CompactionConfig
impl Sync for CompactionConfig
impl Unpin for CompactionConfig
impl UnsafeUnpin for CompactionConfig
impl UnwindSafe for CompactionConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more