Skip to main content

FidelityScorer

Struct FidelityScorer 

Source
pub struct FidelityScorer;
Expand description

Stateless heuristic scorer that assigns and applies fidelity levels to a message window.

Call FidelityScorer::score_and_apply after apply_prepared_context() returns to enforce the three-level representation (Full / Compressed / Placeholder) on historical messages. The scorer never touches exempt messages (INV-07 through INV-10).

§Examples

use zeph_context::fidelity::{FidelityConfig, FidelityScorer};

let scorer = FidelityScorer;
let cfg = FidelityConfig { enabled: false, ..FidelityConfig::default() };
// With `enabled = false` the scorer is a no-op.
let mut messages = vec![];
scorer.score_and_apply(&mut messages, "query", &[], &cfg, &MockTc, 0, false, None, None).await;

struct MockTc;
impl zeph_common::memory::TokenCounting for MockTc {
    fn count_tokens(&self, text: &str) -> usize { text.len() / 4 }
    fn count_tool_schema_tokens(&self, _: &serde_json::Value) -> usize { 0 }
}

Implementations§

Source§

impl FidelityScorer

Source

pub async fn score_and_apply( &self, messages: &mut Vec<Message>, query: &str, planned_tools: &[PlannedToolHint], config: &FidelityConfig, tc: &dyn TokenCounting, inserted_count: usize, allow_upgrade: bool, embed_provider: Option<&dyn LlmProviderDyn>, compress_provider: Option<&dyn LlmProviderDyn>, )

Score all non-exempt messages and apply fidelity rendering in-place.

Steps (per spec §5 data flow):

  1. Guard: return early when enabled == false.
  2. Build exempt set (INV-07 through INV-10).
  3. Score each non-exempt message with normalized weight sum (INV-05).
  4. Apply floor invariant: a message with a persisted fidelity tag cannot be upgraded to a less restrictive level unless allow_upgrade = true.
  5. Apply tool-pair atomicity — both get min(score_a, score_b) (INV-03).
  6. Render Compressed / Placeholder messages (INV-12).
  7. Merge consecutive same-role Placeholder messages (INV-04).
§Parameters
  • messages — mutable message window (includes system prompt at index 0).
  • query — current user query; drives semantic signal.
  • planned_tools — DAG lookahead hints; empty slice disables plan signal.
  • config — scoring thresholds and weights.
  • tc — token counter used for Placeholder/Compressed rendering.
  • inserted_count — number of memory messages freshly injected at indices 1..1+inserted_count; these are always exempt (INV-10).
  • allow_upgrade — when true the persisted floor invariant is bypassed. Pass true only from the proactive regrade path; pass false everywhere else.
  • embed_provider — optional LLM provider used for query and per-message embeddings when config.semantic_scoring_provider is set. Pass None to fall back to keyword overlap.
  • compress_provider — optional LLM provider used for Compressed rendering when config.compress_provider is set. Pass None to fall back to truncation.

The two providers may be the same instance or different ones (e.g. a fast embedding model for embed_provider and a generative model for compress_provider).

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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