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.
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.
Sourcepub fn new(system_text: &str, tools: Option<&[Tool]>) -> Self
pub fn new(system_text: &str, tools: Option<&[Tool]>) -> Self
Create a new manager and immediately pin the first fingerprint.
Sourcepub fn new_unpinned() -> Self
pub fn new_unpinned() -> Self
Create a manager in “unpinned” state — no initial fingerprint.
Call pin() or check_and_update() to establish the baseline.
Sourcepub fn pin(&mut self, system_text: &str, tools: Option<&[Tool]>) -> bool
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.
Sourcepub fn pin_with_reason(
&mut self,
system_text: &str,
tools: Option<&[Tool]>,
reason: &str,
) -> bool
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>).
Sourcepub fn note_history_reset(&mut self, what: &str)
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.
Sourcepub fn note_context_update(&mut self)
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.
Sourcepub fn context_update_count(&self) -> u64
pub fn context_update_count(&self) -> u64
Number of <context_update> snapshots appended this session.
Sourcepub fn check(
&mut self,
system_text: &str,
tools: Option<&[Tool]>,
declared_change: Option<&str>,
) -> PrefixCheck
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.
Sourcepub fn pin_reason(&self) -> Option<&str>
pub fn pin_reason(&self) -> Option<&str>
Why the current pin exists.
Sourcepub fn history(&self) -> impl Iterator<Item = &PrefixHistoryEntry>
pub fn history(&self) -> impl Iterator<Item = &PrefixHistoryEntry>
Attributed change/drift/reset history, oldest first.
Sourcepub fn last_miss_reason(&self) -> Option<&str>
pub fn last_miss_reason(&self) -> Option<&str>
Explanation of the most recent expected miss, if any.
Sourcepub fn check_and_update(
&mut self,
system_text: &str,
tools: Option<&[Tool]>,
) -> Result<bool, Box<PrefixChange>>
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. UseSelf::checkwith a declared reason to move the pin on purpose.
Sourcepub fn last_change(&self) -> Option<&PrefixChange>
pub fn last_change(&self) -> Option<&PrefixChange>
Returns the most recent prefix change, if any.
Sourcepub fn pinned_fingerprint(&self) -> Option<&PrefixFingerprint>
pub fn pinned_fingerprint(&self) -> Option<&PrefixFingerprint>
Returns the pinned fingerprint.
Sourcepub fn current_fingerprint(&self) -> Option<&PrefixFingerprint>
pub fn current_fingerprint(&self) -> Option<&PrefixFingerprint>
Returns the current (most recently computed) fingerprint.
Sourcepub fn change_count(&self) -> u64
pub fn change_count(&self) -> u64
Returns the total number of prefix changes detected.
Sourcepub fn check_count(&self) -> u64
pub fn check_count(&self) -> u64
Returns the total number of stability checks performed.
Sourcepub fn stability_ratio(&self) -> f64
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).
Trait Implementations§
Source§impl Clone for PrefixStabilityManager
impl Clone for PrefixStabilityManager
Source§fn clone(&self) -> PrefixStabilityManager
fn clone(&self) -> PrefixStabilityManager
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more