Skip to content

feat(engine): expose context_input_budget_for_route for external budget reuse#3968

Merged
Hmbown merged 1 commit into
Hmbown:mainfrom
h3c-hexin:feat/pub-context-input-budget
Jul 3, 2026
Merged

feat(engine): expose context_input_budget_for_route for external budget reuse#3968
Hmbown merged 1 commit into
Hmbown:mainfrom
h3c-hexin:feat/pub-context-input-budget

Conversation

@h3c-hexin

Copy link
Copy Markdown
Contributor

Problem

context_input_budget_for_route — the internal input-budget computation (window − window-dependent output reservation − headroom) that is the single source of truth for the preflight / emergency-recovery / capacity-trim decisions — is pub(super) to core::engine.

External hosts / wrappers that embed the engine and derive their own compaction or pressure trigger line have no way to reuse it, so they end up mirroring the constants: the ≥500K large-window threshold, TURN_MAX_OUTPUT_TOKENS, the output-reservation split in route_output_reservation_for_window, and the headroom. When the engine later changes any of these, the mirrored copy still compiles but silently diverges — the host computes a different budget than the engine actually enforces. (In our case the host's "compact now" line drifted above the engine's emergency line, so the graceful summarize path stopped firing and every long session fell through to emergency recovery.)

Change

Make context_input_budget_for_route pub and re-export it from core::engine, so a host can compute the exact same budget the engine uses. Pass input_tokens = 0 to get the full route budget.

Purely a visibility change:

  • No behavior change; the function body is untouched.
  • All parameters and the return type are already public (ApiProvider, RouteLimits, usize).
  • cargo check -p codewhale-tui passes.

This lets downstream embedders reuse the engine's budget math instead of re-deriving (and drifting from) its internal constants.

…et reuse

The internal input-budget math (window minus the window-dependent output
reservation minus headroom) is private to core::engine. External hosts/wrappers
that derive their own compaction trigger line end up mirroring those constants
(the 500K large-window threshold, TURN_MAX_OUTPUT_TOKENS, the output-reservation
split, the headroom) and silently drift when the engine changes them. Make
context_input_budget_for_route pub and re-export it from core::engine so callers
reuse the exact same computation (pass input_tokens = 0 for the full budget).
No behavior change.
@h3c-hexin h3c-hexin requested a review from Hmbown as a code owner July 3, 2026 10:06
@Hmbown Hmbown merged commit 25cce40 into Hmbown:main Jul 3, 2026
14 checks passed
@Hmbown

Hmbown commented Jul 3, 2026

Copy link
Copy Markdown
Owner

Merged into main for the 0.8.67 finalization train as #3968 (25cce4074096b22dc9b531a7810f67dc0391e3e6). Thanks @h3c-hexin. I treated this as behavior-neutral API reuse surface, and it compiled cleanly in the local scratch train with the other accepted PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants