pub struct TokenBudgetCore { /* private fields */ }Expand description
Pure decision core for the token-budget window strategy.
Implementations§
Source§impl TokenBudgetCore
impl TokenBudgetCore
Sourcepub fn new(
config: TokenBudgetConfig,
system_prompt: Option<&str>,
) -> TokenBudgetCore
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.
Sourcepub fn base_overhead(&self) -> usize
pub fn base_overhead(&self) -> usize
Fixed per-window overhead (estimated tokens).
Sourcepub fn hard_limit(&self) -> usize
pub fn hard_limit(&self) -> usize
Absolute reset threshold (base + work + buffer).
Sourcepub fn max_result_tokens(&self) -> usize
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.
pub fn config(&self) -> &TokenBudgetConfig
pub fn state(&self) -> &TokenBudgetState
Sourcepub fn work_used(&self, total_tokens: usize) -> usize
pub fn work_used(&self, total_tokens: usize) -> usize
Work-room tokens consumed so far (total − base).
Sourcepub fn work_remaining(&self, total_tokens: usize) -> usize
pub fn work_remaining(&self, total_tokens: usize) -> usize
Work-room tokens remaining.
Sourcepub fn evaluate(&self, total_tokens: usize) -> TokenBudgetAction
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.
Sourcepub fn build_reset_messages(
&self,
old_messages: &[ChatMessage],
thread_hint: Option<String>,
previous_window: usize,
) -> Vec<ChatMessage>
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.
Sourcepub fn commit_reset(&self) -> usize
pub fn commit_reset(&self) -> usize
Advance the window state after a successful reset. Returns the new window ID.