Skip to main content

TokenBudgetCore

Struct TokenBudgetCore 

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

Pure decision core for the token-budget window strategy.

Implementations§

Source§

impl TokenBudgetCore

Source

pub fn new( config: TokenBudgetConfig, system_prompt: Option<&str>, ) -> TokenBudgetCore

Build the core. system_prompt is the composed prompt (constant for the process lifetime); the base overhead is estimated from it once, here.

A work budget below the viability floor (max(min_work_multiple × base, min_absolute_work_room)) cannot hold even one turn of work — a single tool result overflows the room, every window dies to its first reconstruction step, and the session thrashes (reset → seed → reconstruct → reset). The budget is clamped up to the floor (with the reminder/buffer bands rescaled) rather than proceeding into a known-broken configuration. The absolute term protects small-prompt apps: tool output doesn’t shrink with the system prompt.

Source

pub fn base_overhead(&self) -> usize

Fixed per-window overhead (estimated tokens).

Source

pub fn hard_limit(&self) -> usize

Absolute reset threshold (base + work + buffer).

Source

pub fn max_result_tokens(&self) -> usize

Per-tool-result soft cap: a single tool result may consume at most a third of the work room, so a fresh window always holds room for 2-3 results plus the model’s own turn (session 20260909_e7053736: untruncated 3-4k reads at a 6.5k room left space for exactly one). Floored at 512 so tiny rooms still get usable output; the engine’s max_message_tokens valve stays the hard upper bound on top of this.

Source

pub fn window_id(&self) -> usize

Current window ID.

Source

pub fn braked(&self) -> bool

Whether the futility brake has paused window rotation.

Source

pub fn config(&self) -> &TokenBudgetConfig

Source

pub fn state(&self) -> &TokenBudgetState

Source

pub fn work_used(&self, total_tokens: usize) -> usize

Work-room tokens consumed so far (total − base).

Source

pub fn work_remaining(&self, total_tokens: usize) -> usize

Work-room tokens remaining.

Source

pub fn evaluate(&self, total_tokens: usize) -> TokenBudgetAction

Run one phase check. Marks the reminder flag when Reminder/Fallback fires (once per window); the shell injects the returned message at the END of the list — right after the latest tool result, where the model’s attention actually is (a front-positioned nudge is lost in the middle and ignored).

One-turn reset hold: a window that crosses every band in a single jump still gets exactly one Fallback turn (“save state now”) before the Reset — the handoff note is what makes the next window cheap.

Futility brake: when consecutive windows die before their first quiet evaluation (None), the room evidently cannot hold even one turn of work — the tool results alone overflow it. Resetting again would just burn API calls and shred history (session 20260908_8b5fcb45: 41 resets in 3 minutes), so after [FUTILITY_RESET_LIMIT] breathless resets the rotation pauses permanently and evaluate returns TokenBudgetAction::None.

Source

pub fn build_reset_messages( &self, old_messages: &[ChatMessage], thread_hint: Option<String>, previous_window: usize, ) -> Vec<ChatMessage>

Assemble the fresh window’s message list: preserved system prompt + window info + optional app-provided thread hint + guidance + seed.

A thread hint large enough to push the fresh window past the hard limit is dropped (with a warning) — the base must always fit. Does NOT advance window state; call TokenBudgetCore::commit_reset after the shell has archived and installed the list.

Source

pub fn commit_reset(&self) -> usize

Advance the window state after a successful reset. Returns the new window ID.

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = !

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