pub struct TrajectoryRiskAccumulator { /* private fields */ }Expand description
Per-session trajectory risk accumulator (MAGE spec 004-16).
Maintains a rolling trajectory_risk score in [0.0, 1.0] that accumulates safety
signals with exponential temporal decay. Designed to detect multi-turn attacks that
evade per-turn controls.
When constructed via new_noop or when
config.enabled = false, all methods are zero-cost no-ops — no allocations and
current_risk() always returns 0.0.
Implementations§
Source§impl TrajectoryRiskAccumulator
impl TrajectoryRiskAccumulator
Sourcepub fn new_noop() -> Self
pub fn new_noop() -> Self
Construct an accumulator that operates as a zero-cost noop.
Use when shadow memory is disabled or during testing scenarios that do not need risk tracking. No heap allocation is performed.
Sourcepub fn new(config: TrajectoryRiskAccumulatorConfig) -> Self
pub fn new(config: TrajectoryRiskAccumulatorConfig) -> Self
Construct an accumulator from configuration.
When config.enabled = false, delegates to new_noop — no
allocation. When enabled, pre-allocates the signal history ring buffer.
Sourcepub fn advance_turn(&mut self)
pub fn advance_turn(&mut self)
Advance the turn counter and apply exponential decay to the accumulated risk.
Must be called once per turn, before ingest is called for
that turn. Decay formula: risk *= exp(-ln(2) / halflife_turns).
No-op when disabled.
Sourcepub fn ingest(&mut self, signal_type: AuditSignalType, severity: Severity)
pub fn ingest(&mut self, signal_type: AuditSignalType, severity: Severity)
Ingest a safety signal and add its weighted contribution to trajectory_risk.
The raw score is base_weight(signal_type) × severity_multiplier(severity).
After addition, trajectory_risk is clamped to [0.0, 1.0]. The event is
appended to the signal history ring buffer; the oldest entry is evicted when
the buffer is full.
Emits shadow_memory_signals_total{type, severity} counter (NFR-007).
No-op when disabled.
Sourcepub fn record_block(&self)
pub fn record_block(&self)
Increment shadow_memory_blocks_total counter (NFR-007).
Call this once when a tool execution is actually blocked due to trajectory risk.
Do not call on every is_blocked() query — only when a block action fires.
Sourcepub fn record_escalation(&self)
pub fn record_escalation(&self)
Increment shadow_memory_escalations_total counter (NFR-007).
Call this once when an escalation-to-human-confirmation is triggered.
Sourcepub fn current_risk(&self) -> f64
pub fn current_risk(&self) -> f64
Returns the current accumulated risk score in [0.0, 1.0].
Always returns 0.0 when disabled.
Sourcepub fn is_blocked(&self) -> bool
pub fn is_blocked(&self) -> bool
Returns true when trajectory_risk >= risk_threshold and shadow memory is enabled.
Always returns false when disabled.
Sourcepub fn should_escalate(&self) -> bool
pub fn should_escalate(&self) -> bool
Returns true when risk is in [escalation_threshold, risk_threshold).
Always returns false when disabled.
Sourcepub fn top_signals(&self, n: usize) -> Vec<&SignalEvent>
pub fn top_signals(&self, n: usize) -> Vec<&SignalEvent>
Returns the top n signals by raw_score descending from recent history.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets trajectory_risk to zero and clears signal history.
Called on context compaction when reset_on_compaction = true. No-op when disabled.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Returns true when shadow memory is enabled (i.e., not in noop mode).
Sourcepub fn turn_count(&self) -> u32
pub fn turn_count(&self) -> u32
Returns the current turn count.
Auto Trait Implementations§
impl Freeze for TrajectoryRiskAccumulator
impl RefUnwindSafe for TrajectoryRiskAccumulator
impl Send for TrajectoryRiskAccumulator
impl Sync for TrajectoryRiskAccumulator
impl Unpin for TrajectoryRiskAccumulator
impl UnsafeUnpin for TrajectoryRiskAccumulator
impl UnwindSafe for TrajectoryRiskAccumulator
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> 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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request