pub struct ContextBudgetSnapshot {
pub context_window_tokens: usize,
pub overhead_tokens: usize,
pub overhead_cap_tokens: usize,
pub working_context_pct: u8,
pub overhead_pct: u8,
pub within_budget: bool,
pub compaction_fired: bool,
pub compaction_rounds: usize,
pub lifetime_compaction_alarm_count: u64,
pub working_context_pct_low_water_mark: Option<u8>,
pub working_context_pct_sample_count: usize,
pub lifetime_cadence_floor_breach_count: u64,
}Expand description
A cached snapshot of the Event::ContextBudget payload (issue #3015 —
session.get_context_budget query RPC; surfaced by PR #3014’s GUI status
bar, which renders “budget: unavailable” waiting for exactly this API).
Why: Event::ContextBudget above is a per-turn STREAM value, not a
queryable one — a client that attaches (or reconnects) between turns has
no way to ask “what is the working-context budget right now”; it can only
ever see events that arrive while it happens to be subscribed. This is
the field-for-field twin of Event::ContextBudget’s payload (deliberately
excluding session_id, contextual to the cache slot it lives in) so
SessionRegistry can cache the last-recorded measurement per session and
session.get_context_budget can hand it back verbatim to a
late-attaching client — mirrors IndexReadinessSnapshot exactly.
What: every field name and type matches the corresponding field on
Event::ContextBudget exactly. SessionRegistry::record_context_budget
builds one of these first and derives the Event::ContextBudget it
publishes from it, so the two can never disagree. All-Copy (unlike
IndexReadinessSnapshot) since every field is a primitive.
Test: session::registry_tests::record_context_budget_caches_snapshot_for_late_query,
session::protocol_budget::tests::get_context_budget_returns_recorded_snapshot_after_recording.
(issue #3868) lifetime_compaction_alarm_count is additive and, unlike
every other field here, has NO twin on Event::ContextBudget — it is
deliberately absent from the per-turn stream. Why: the stream event is
derived once, at the moment a turn’s cadence measurement lands, from
values the caller already computed; the alarm count instead needs a
File::open + line-scan of agent_loop::telemetry’s durable,
cross-session alarm log, which is too expensive to pay on the EVERY-TURN
write path (record_context_budget, called once per cadence tick). It is
therefore read fresh only in SessionRegistry::get_context_budget — the
actual RPC query path, called once per client poll — which overwrites
the field on the cached snapshot right before returning it; the copy
record_context_budget stores is a 0 placeholder never observed by a
caller. This also means a session that never fires threshold compaction
still gets an accurate count reflecting fires from ANY session on this
machine, not a stale per-session value baked in at some earlier turn. It
answers “has threshold compaction EVER fired under cadence, cumulative
across every session on this machine” — a fact with no natural
single-turn “stream” moment to attach to.
Fields§
§context_window_tokens: usize§overhead_tokens: usize§overhead_cap_tokens: usize§working_context_pct: u8§overhead_pct: u8§within_budget: bool§compaction_fired: bool§compaction_rounds: usize§lifetime_compaction_alarm_count: u64(#3868) Lifetime, cross-session count of cadence: Some(_)
threshold-compaction fires — epic #2343’s never-event alarm, made
durable and reachable from the ONE RPC that’s already the front door
for budget state (session.get_context_budget) instead of requiring
a second session.get_transcript call.
working_context_pct_low_water_mark: Option<u8>(issues #3912, #3948) This SESSION’s lowest working_context_pct
across every cadence measurement SessionRegistry has recorded for
it — None until the first measurement lands. It exists because the
load-realistic compression soak (epic #3866) proved the single
most-recent snapshot above (working_context_pct) can read 98-99%
while the same session dipped to 48-60% moments earlier; a coarse
poll reliably misses the turn where the floor was lowest.
(#3948) Unlike lifetime_compaction_alarm_count above, this is NOT
read fresh from a durable log at query time. It is folded in
incrementally by SessionRegistry::record_context_budget and lives
as long as the session does, which means the value is scoped to the
running daemon: a restart drops the session itself, so the RPC
answers session_not_found rather than a reset floor. Offline
history stays in compression.jsonl, readable via
agent_loop::telemetry::session_working_context_floor; that reader
aggregates a different sample set (only turns that did compaction
work, plus threshold fires), so its floor and this one can differ.
working_context_pct_sample_count: usize(issues #3912, #3948) How many cadence measurements
working_context_pct_low_water_mark was computed over for this
session — lets a consumer distinguish “no samples yet” (None above)
from “exactly one, possibly unrepresentative, sample”. Counts live
measurements, not durable JSONL rows.
lifetime_cadence_floor_breach_count: u64(issue #3911) Lifetime, cross-session count of a cadence-level
60%-floor breach (CadenceOutcome::within_budget == false after
cadence::enforce_budget exhausted every eligible entry) — the
backstop’s own alarm, mirroring lifetime_compaction_alarm_count
exactly but keyed to the cadence mechanism’s OWN floor guarantee
rather than the mechanically-independent threshold compactor’s
separate 75%-of-window trigger (see
agent_loop::telemetry::record_cadence_floor_breach’s docs for why
the two alarms cannot be the same counter). Same “placeholder 0 on
the write path, real read on the query path” pattern.
Trait Implementations§
Source§impl Clone for ContextBudgetSnapshot
impl Clone for ContextBudgetSnapshot
Source§fn clone(&self) -> ContextBudgetSnapshot
fn clone(&self) -> ContextBudgetSnapshot
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for ContextBudgetSnapshot
Source§impl Debug for ContextBudgetSnapshot
impl Debug for ContextBudgetSnapshot
Source§impl<'de> Deserialize<'de> for ContextBudgetSnapshot
impl<'de> Deserialize<'de> for ContextBudgetSnapshot
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for ContextBudgetSnapshot
impl PartialEq for ContextBudgetSnapshot
Source§impl Serialize for ContextBudgetSnapshot
impl Serialize for ContextBudgetSnapshot
impl StructuralPartialEq for ContextBudgetSnapshot
Auto Trait Implementations§
impl Freeze for ContextBudgetSnapshot
impl RefUnwindSafe for ContextBudgetSnapshot
impl Send for ContextBudgetSnapshot
impl Sync for ContextBudgetSnapshot
impl Unpin for ContextBudgetSnapshot
impl UnsafeUnpin for ContextBudgetSnapshot
impl UnwindSafe for ContextBudgetSnapshot
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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 more