Skip to main content

CompactState

Struct CompactState 

Source
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_window from 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: usize

Estimated bytes of new messages not yet sent to the model.

§context_window: u64

Context window size in tokens for the current model.

§total_tokens_before_compaction: u64

Cumulative input tokens processed before/during compaction cycles.

§compaction_count: u32

Number of compaction runs in this session.

§auto_compact_threshold_percent: u8

Auto-compact when context_percentage >= this (default: 85).

§consecutive_failures: u32

Number 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

Source

pub fn new(context_window: u64) -> Self

Source

pub fn add_unsent_bytes(&mut self, bytes: usize)

Source

pub fn clear_unsent_bytes(&mut self)

Source

pub fn context_tokens_used(&self, pending_input_bytes: usize) -> u64

Current context size (live context usage).

Source

pub fn context_window_tokens(&self) -> u64

Context window size (context window size).

Source

pub fn context_window_usage(&self, pending_input_bytes: usize) -> u8

Integer percent of the window in use (context window usage percent).

Source

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).

Source

pub fn threshold_level(&self, pending_input_bytes: usize) -> CompactThreshold

Source

pub fn should_autocompact(&self, buffer_tokens: u64) -> bool

Source

pub fn context_percentage(&self, pending_input_bytes: usize) -> u8

Source

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).

Source

pub fn usage_label_compact(&self, pending_input_bytes: usize) -> String

Token counts only — default (non-hover) display.

Source

pub fn usage_label_with_percent(&self, pending_input_bytes: usize) -> String

Token counts + percent — shown while the context chip is hovered.

Source

pub fn update_usage(&mut self, input_tokens: u64)

Record provider usage for this turn (called every stream that reports usage).

Source

pub fn update_usage_full(&mut self, input_tokens: u64, output_tokens: u64)

Record full turn usage and refresh live context metrics.

Source

pub fn turn_usage_label(&self) -> Option<String>

Label shown in the TUI footer — updates every turn after update_usage*.

Source

pub async fn auto_compact( &mut self, messages: &mut Vec<ModelMessage>, model_provider: &dyn ModelProvider, model_name: &str, harness_config: &HarnessConfig, ) -> Result<Option<CompactOutcome>>

Source

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).

Source

pub fn apply_manual_summary( &mut self, messages: &mut Vec<ModelMessage>, summary: String, ) -> CompactOutcome

Trait Implementations§

Source§

impl Clone for CompactState

Source§

fn clone(&self) -> Self

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 CompactState

Source§

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

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

impl Default for CompactState

Source§

fn default() -> Self

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

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> MaybeSend for T

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more