Skip to main content

PrefixStabilityManager

Struct PrefixStabilityManager 

Source
pub struct PrefixStabilityManager { /* private fields */ }
Expand description

Monitors and manages prefix-cache stability across turns.

This is the core abstraction, mirroring Reasonix’s ImmutablePrefix concept but adapted to CodeWhale’s existing architecture where the system prompt is rebuilt each turn and tools are registered at startup.

Usage:

ⓘ
let mgr = PrefixStabilityManager::new(system_text, tools);
if mgr.check_and_update(system_text, tools) {
    println!("Prefix is stable (cache-friendly)");
} else {
    let change = mgr.last_change().unwrap();
    println!("Prefix drifted: {}", change.description());
}

Implementations§

Source§

impl PrefixStabilityManager

Process-local fallback cache used by PrefixFingerprint::compute (when available). Callers that maintain their own cache (e.g. PrefixStabilityManager) should prefer PrefixFingerprint::compute_with_tool_cache and pass the cache in directly, both to share state and to avoid the thread-local lookup on the hot path.

Source

pub fn new(system_text: &str, tools: Option<&[Tool]>) -> Self

Create a new manager and immediately pin the first fingerprint.

Source

pub fn new_unpinned() -> Self

Create a manager in “unpinned” state — no initial fingerprint. Call pin() or check_and_update() to establish the baseline.

Source

pub fn pin(&mut self, system_text: &str, tools: Option<&[Tool]>) -> bool

Explicitly pin a fingerprint, replacing any prior pinned state. Returns true if this is the first pin, or false if replacing. Note: does NOT increment check_count — that counter is reserved for check_and_update calls so stability_ratio() stays accurate.

Source

pub fn pin_with_reason( &mut self, system_text: &str, tools: Option<&[Tool]>, reason: &str, ) -> bool

Pin under an explicit reason (initial, resume, change:<what>).

Source

pub fn note_history_reset(&mut self, what: &str)

Record an expected miss that is not a header change (compaction, /clear, an edited turn). The pin is untouched; the reason is kept so /cache stats can explain the next low hit-rate turn.

Source

pub fn note_context_update(&mut self)

Record that workspace drift was delivered as a <context_update> history append. Not a miss: the pin and the prefix are unchanged.

Source

pub fn context_update_count(&self) -> u64

Number of <context_update> snapshots appended this session.

Source

pub fn check( &mut self, system_text: &str, tools: Option<&[Tool]>, declared_change: Option<&str>, ) -> PrefixCheck

Verify the request prefix against the pin, attributing any change.

declared_change names a header change the engine performed on purpose (model, mode, goal, tools:+web_fetch, mcp, …). When the prefix changed and a reason is declared, the pin moves and the change is logged as change:<reason>. When it changed with no declared reason, that is drift: it is logged as drift:<component> and the pin stays put, so the same undeclared prefix keeps counting as a miss instead of becoming the new baseline.

Source

pub fn pin_reason(&self) -> Option<&str>

Why the current pin exists.

Source

pub fn history(&self) -> impl Iterator<Item = &PrefixHistoryEntry>

Attributed change/drift/reset history, oldest first.

Source

pub fn last_miss_reason(&self) -> Option<&str>

Explanation of the most recent expected miss, if any.

Source

pub fn check_and_update( &mut self, system_text: &str, tools: Option<&[Tool]>, ) -> Result<bool, Box<PrefixChange>>

Check whether the current prefix matches the pinned fingerprint without a declared header change.

  • Ok(true) when the prefix is stable (or this was the first pin).
  • Err(change) when the prefix drifted. The pin is kept: an undeclared change never becomes the new baseline. Use Self::check with a declared reason to move the pin on purpose.
Source

pub fn last_change(&self) -> Option<&PrefixChange>

Returns the most recent prefix change, if any.

Source

pub fn pinned_fingerprint(&self) -> Option<&PrefixFingerprint>

Returns the pinned fingerprint.

Source

pub fn current_fingerprint(&self) -> Option<&PrefixFingerprint>

Returns the current (most recently computed) fingerprint.

Source

pub fn change_count(&self) -> u64

Returns the total number of prefix changes detected.

Source

pub fn check_count(&self) -> u64

Returns the total number of stability checks performed.

Source

pub fn stability_ratio(&self) -> f64

Returns the prefix stability rate as a fraction (0.0 – 1.0). 1.0 means the prefix has never changed. Returns 1.0 when no checks have been performed (to avoid division by zero).

Source

pub fn summary(&self) -> String

Returns a human-readable stability summary.

Trait Implementations§

Source§

impl Clone for PrefixStabilityManager

Source§

fn clone(&self) -> PrefixStabilityManager

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 PrefixStabilityManager

Source§

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

Formats the value using the given formatter. 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<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> 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> 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