pub fn plan_forced(
system: &Message,
history: &[Message],
protect_last_n: usize,
) -> WindowsExpand description
Plan the three windows for one request. Pure arithmetic, no messages are built.
A budget_tokens of 0 disables it all; fixed_tokens (the tool definitions) counts
against the budget. A history that fits is sent untouched. Past the budget the tail is
filled newest-first — at most protect_last_n turns — and the first turn is kept when
there is room for both.
The tail has priority, with one reservation: when the first turn costs no more than half
the budget it is paid for first, so a conversation with slack keeps its goal instead of
spending the last of it on one more recent turn. A first turn that costs more than half
the budget is not reserved for — an enormous pasted prompt is not worth starving the live
state for — and the tail then takes what it can, which is the oldest-turns-go behaviour
this replaced. A turn is never split, so a tool call cannot be separated from its result.
The plan a manual compaction asks for: keep the first turn and the newest protect_last_n
turns verbatim and replace everything between them with the handoff, whether or not the
request would have fitted. It is the same calculation with the budget taken out of the
decision, which is why it cannot drift from plan_windows.