Skip to main content

context_guard

Function context_guard 

Source
pub fn context_guard(
    messages: &[ChatMessage],
    tools: &[ToolSchema],
    context_limit: u64,
) -> (bool, u64)
Expand description

PARITY-18 D1/D2/D4 — the single context-guard decision, shared by every call site that must decide whether a request is safe to send: the CLI’s resume_cmd preflight check AND Agent::run_loop’s per-send check (D4 — the guard is a session invariant, not a one-shot preflight, so turn 2+ and /expand all are covered too). Because both call through this one function, a request can never pass one gate and fail the other — there is only one formula.

fits is true iff the with_guard_margin-adjusted estimate_request_tokens estimate, plus the CONTEXT_RESPONSE_RESERVE_TOKENS completion reserve, is still within context_limit — i.e. gates on the reduce TARGET (context_limit - CONTEXT_RESPONSE_RESERVE_TOKENS), not the raw limit, closing the “blind band between reduce target and pass/fail boundary” gap. Returns the margin-adjusted projected total either way so callers can report it (dev/02) regardless of verdict.