pub struct CompactState {Show 13 fields
pub last_input_tokens: Option<u64>,
pub last_output_tokens: Option<u64>,
pub estimated_unsent_bytes: usize,
pub context_window: u64,
pub total_tokens_before_compaction: u64,
pub compaction_count: u32,
pub auto_compact_threshold_percent: u8,
pub consecutive_failures: u32,
pub summary: Option<String>,
pub summary_message_count: usize,
pub rebuild_context: Option<String>,
pub crossed_thresholds: Vec<f64>,
pub history_synced_message_keys: HashSet<u64>,
}Expand description
Tracks token usage and compact failure state for autocompact decisions.
Measurement model (aligned with modern coding CLI TUI):
- context tokens used = last API
input_tokens(ground truth for the current context) + client preflight for unsent bytes (bytes/4) - window = model
context_windowfrom registry - usage % = used / window
- total before compaction = cumulative input tokens across turns (historical; does not reset when the bar drops after compact)
Fields§
§last_input_tokens: Option<u64>Token count from the last model response usage (current context size).
last_output_tokens: Option<u64>Output tokens from the last model response (UI turn label).
estimated_unsent_bytes: usizeEstimated bytes of new messages not yet sent to the model.
context_window: u64Context window size in tokens for the current model.
total_tokens_before_compaction: u64Cumulative input tokens processed before/during compaction cycles.
compaction_count: u32Number of compaction runs in this session.
auto_compact_threshold_percent: u8Auto-compact when context_percentage >= this (default: 85).
consecutive_failures: u32Number of consecutive compact failures.
summary: Option<String>§summary_message_count: usize§rebuild_context: Option<String>Latest long-horizon rebuild context that must stay attached to the system prompt across subsequent turns.
crossed_thresholds: Vec<f64>List of checkpoint thresholds crossed in the current context cycle.
history_synced_message_keys: HashSet<u64>Fingerprints of messages already copied into long-horizon history.
Implementations§
Source§impl CompactState
impl CompactState
pub fn new(context_window: u64) -> Self
pub fn add_unsent_bytes(&mut self, bytes: usize)
pub fn clear_unsent_bytes(&mut self)
Sourcepub fn context_tokens_used(&self, pending_input_bytes: usize) -> u64
pub fn context_tokens_used(&self, pending_input_bytes: usize) -> u64
Current context size (live context usage).
Sourcepub fn context_window_tokens(&self) -> u64
pub fn context_window_tokens(&self) -> u64
Context window size (context window size).
Sourcepub fn context_window_usage(&self, pending_input_bytes: usize) -> u8
pub fn context_window_usage(&self, pending_input_bytes: usize) -> u8
Integer percent of the window in use (context window usage percent).
Sourcepub fn total_estimated_tokens(&self, pending_input_bytes: usize) -> u64
pub fn total_estimated_tokens(&self, pending_input_bytes: usize) -> u64
Estimate tokens for the next request:
API last-input (server) + unsent/pending client bytes as ceil(bytes/4).
pub fn threshold_level(&self, pending_input_bytes: usize) -> CompactThreshold
pub fn should_autocompact(&self, buffer_tokens: u64) -> bool
pub fn context_percentage(&self, pending_input_bytes: usize) -> u8
Sourcepub fn usage_label(&self, pending_input_bytes: usize) -> String
pub fn usage_label(&self, pending_input_bytes: usize) -> String
Compact context meter for the composer footer: 3.2k / 200k.
Percentage is revealed on hover (see usage_label_with_percent).
Sourcepub fn usage_label_compact(&self, pending_input_bytes: usize) -> String
pub fn usage_label_compact(&self, pending_input_bytes: usize) -> String
Token counts only — default (non-hover) display.
Sourcepub fn usage_label_with_percent(&self, pending_input_bytes: usize) -> String
pub fn usage_label_with_percent(&self, pending_input_bytes: usize) -> String
Token counts + percent — shown while the context chip is hovered.
Sourcepub fn update_usage(&mut self, input_tokens: u64)
pub fn update_usage(&mut self, input_tokens: u64)
Record provider usage for this turn (called every stream that reports usage).
Sourcepub fn update_usage_full(&mut self, input_tokens: u64, output_tokens: u64)
pub fn update_usage_full(&mut self, input_tokens: u64, output_tokens: u64)
Record full turn usage and refresh live context metrics.
Sourcepub fn turn_usage_label(&self) -> Option<String>
pub fn turn_usage_label(&self) -> Option<String>
Label shown in the TUI footer — updates every turn after update_usage*.
pub async fn auto_compact( &mut self, messages: &mut Vec<ModelMessage>, model_provider: &dyn ModelProvider, model_name: &str, harness_config: &HarnessConfig, ) -> Result<Option<CompactOutcome>>
Sourcepub async fn force_compact(
&mut self,
messages: &mut Vec<ModelMessage>,
model_provider: &dyn ModelProvider,
model_name: &str,
harness_config: &HarnessConfig,
) -> Result<Option<CompactOutcome>>
pub async fn force_compact( &mut self, messages: &mut Vec<ModelMessage>, model_provider: &dyn ModelProvider, model_name: &str, harness_config: &HarnessConfig, ) -> Result<Option<CompactOutcome>>
Force compaction with the session model even when below the threshold. Manual Compact always fully replaces conversation history (keep_ratio=0).
pub fn apply_manual_summary( &mut self, messages: &mut Vec<ModelMessage>, summary: String, ) -> CompactOutcome
Trait Implementations§
Source§impl Clone for CompactState
impl Clone for CompactState
Source§impl Debug for CompactState
impl Debug for CompactState
Auto Trait Implementations§
impl Freeze for CompactState
impl RefUnwindSafe for CompactState
impl Send for CompactState
impl Sync for CompactState
impl Unpin for CompactState
impl UnsafeUnpin for CompactState
impl UnwindSafe for CompactState
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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