@@ -4,6 +4,18 @@ use crate::config::{
44};
55use crate::types::HistoryItem;
66
7+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
8+ struct HandoffTokenCounts {
9+ before: u64,
10+ after: u64,
11+ }
12+
13+ impl std::fmt::Display for HandoffTokenCounts {
14+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15+ write!(f, "{} -> {} tokens", self.before, self.after)
16+ }
17+ }
18+
719pub(crate) enum HandoffOutcome {
820 Performed,
921 Skipped,
@@ -28,6 +40,7 @@ impl RunCtx<'_> {
2840 return HandoffOutcome::Skipped;
2941 }
3042 let prompt = self.build_handoff_prompt();
43+ let tokens_before = self.projected_handoff_input_tokens();
3144 let summary = tokio::select! {
3245 biased;
3346 _ = self.cancel.changed() => return HandoffOutcome::Cancelled,
@@ -81,15 +94,42 @@ impl RunCtx<'_> {
8194 self.history.push(HistoryItem::User(prompt));
8295 }
8396 *self.handoff_count += 1;
97+ let token_counts = HandoffTokenCounts {
98+ before: tokens_before,
99+ after: estimate_history_tokens(self.history),
100+ };
84101 tracing::info!(
85- "handoff #{} (history {prior} -> {} items)",
102+ "handoff #{} (history {prior} -> {} items; {token_counts} )",
86103 *self.handoff_count,
87104 self.history.len()
88105 );
89106 HandoffOutcome::Performed
90107 }
91108
92109 fn should_handoff(&self) -> bool {
110+ match *self.last_request_input_tokens {
111+ Some(_) => {
112+ self.projected_handoff_input_tokens()
113+ >= token_threshold(self.cfg.max_context_tokens, self.cfg.max_output_tokens)
114+ }
115+ None => {
116+ let bytes: usize = self
117+ .history
118+ .iter()
119+ .map(HistoryItem::context_pressure_bytes)
120+ .sum();
121+ bytes
122+ > byte_fallback_threshold(
123+ self.cfg.max_context_tokens,
124+ self.cfg.max_output_tokens,
125+ self.cfg.max_history_bytes,
126+ )
127+ }
128+ }
129+ }
130+
131+ fn projected_handoff_input_tokens(&self) -> u64 {
132+ let current_tokens = estimate_history_tokens(self.history);
93133 match *self.last_request_input_tokens {
94134 // Token-first: the provider told us exactly how many input tokens
95135 // the PREVIOUS request used. But history has grown since that
@@ -107,9 +147,7 @@ impl RunCtx<'_> {
107147 .map(HistoryItem::context_pressure_bytes)
108148 .sum();
109149 let grown = current_bytes.saturating_sub(measured_bytes);
110- let projected = measured_tokens.saturating_add(estimate_tokens_from_bytes(grown));
111- projected
112- >= token_threshold(self.cfg.max_context_tokens, self.cfg.max_output_tokens)
150+ measured_tokens.saturating_add(estimate_tokens_from_bytes(grown))
113151 }
114152 // No usage yet (first request, or just after a handoff reset).
115153 // Fall back to the byte heuristic, capped conservatively so a
@@ -122,19 +160,7 @@ impl RunCtx<'_> {
122160 // Caveat: this can't shrink a single oversized current prompt,
123161 // since a handoff re-adds the current prompt verbatim — that is a
124162 // prompt-cap concern (MAX_PROMPT_BYTES), not this gate.
125- None => {
126- let bytes: usize = self
127- .history
128- .iter()
129- .map(HistoryItem::context_pressure_bytes)
130- .sum();
131- bytes
132- > byte_fallback_threshold(
133- self.cfg.max_context_tokens,
134- self.cfg.max_output_tokens,
135- self.cfg.max_history_bytes,
136- )
137- }
163+ None => current_tokens,
138164 }
139165 }
140166
@@ -299,6 +325,15 @@ pub(crate) fn clamp_bytes(s: &str, max_bytes: usize) -> String {
299325/// than risk the next request exceeding the window.
300326const CONSERVATIVE_BYTES_PER_TOKEN: u64 = 1;
301327
328+ fn estimate_history_tokens(history: &[HistoryItem]) -> u64 {
329+ estimate_tokens_from_bytes(
330+ history
331+ .iter()
332+ .map(HistoryItem::context_pressure_bytes)
333+ .sum(),
334+ )
335+ }
336+
302337/// Estimate tokens from a byte count at the conservative ratio (rounding up,
303338/// so a partial token still counts). At a 1:1 ratio this is just the byte
304339/// count — a guaranteed upper bound on tokens.
0 commit comments