11use crate :: agent:: RunCtx ;
22use crate :: config:: {
33 HANDOFF_MAX_OUTPUT_TOKENS , HANDOFF_MAX_TOOL_NAMES , HANDOFF_ORIGINAL_TASK_MAX_BYTES ,
4- HANDOFF_PROMPT_MAX_BYTES , HANDOFF_TAIL_ITEMS ,
54} ;
65use crate :: types:: HistoryItem ;
76
@@ -16,8 +15,6 @@ turn of an autonomous agent. Be concise but thorough. Cover: what the original t
1615 you accomplished, key decisions made, what remains, and one concrete next step. Output plain \
1716 text only — no tool calls, no JSON. Stay under 8192 tokens.";
1817
19- const HANDOFF_SNIPPET_BYTES : usize = 2048 ;
20-
2118impl RunCtx < ' _ > {
2219 pub ( crate ) async fn maybe_handoff ( & mut self ) -> HandoffOutcome {
2320 if !self . should_handoff ( ) {
@@ -160,32 +157,46 @@ impl RunCtx<'_> {
160157 Produce a context handoff summary covering: (1) original task, \
161158 (2) what was accomplished, (3) key decisions, (4) what remains, \
162159 (5) one concrete next step. Be concise but thorough. Plain text.\n ";
163- let history_header = "\n # Recent History (most recent last)\n " ;
164-
165- let start = self . history . len ( ) . saturating_sub ( HANDOFF_TAIL_ITEMS ) ;
166- let mut snippets: Vec < String > = self . history [ start..]
167- . iter ( )
168- . map ( |item| {
169- let mut s = String :: new ( ) ;
170- push_history_snippet ( & mut s, item) ;
171- s
172- } )
173- . collect ( ) ;
160+ let history_header = "\n # Session History (oldest first)\n " ;
161+ let prompt_budget = handoff_prompt_budget_bytes (
162+ self . cfg . max_context_tokens ,
163+ HANDOFF_MAX_OUTPUT_TOKENS ,
164+ head. len ( ) + history_header. len ( ) + tail. len ( ) ,
165+ ) ;
174166
175- let fixed = head . len ( ) + history_header . len ( ) + tail . len ( ) ;
176- let mut snippets_bytes: usize = snippets . iter ( ) . map ( String :: len ) . sum ( ) ;
167+ let mut snippets : Vec < String > = Vec :: new ( ) ;
168+ let mut snippets_bytes = 0usize ;
177169 let mut dropped = 0usize ;
178- while fixed + snippets_bytes > HANDOFF_PROMPT_MAX_BYTES && !snippets. is_empty ( ) {
179- let removed = snippets. remove ( 0 ) ;
180- snippets_bytes -= removed. len ( ) ;
181- dropped += 1 ;
170+ for item in self . history . iter ( ) . rev ( ) {
171+ let mut snippet = String :: new ( ) ;
172+ push_history_snippet ( & mut snippet, item) ;
173+ let snippet_bytes = snippet. len ( ) ;
174+ if snippets_bytes. saturating_add ( snippet_bytes) > prompt_budget {
175+ if snippets. is_empty ( ) {
176+ snippets. push ( clamp_bytes ( & snippet, prompt_budget) ) ;
177+ snippets_bytes = prompt_budget;
178+ }
179+ dropped += 1 ;
180+ continue ;
181+ }
182+ snippets_bytes += snippet_bytes;
183+ snippets. push ( snippet) ;
182184 }
185+ snippets. reverse ( ) ;
183186 if dropped > 0 {
184- tracing:: info!( "handoff prompt cap, dropped {dropped} oldest snippets" ) ;
187+ tracing:: info!(
188+ "handoff prompt budget, dropped {dropped} oldest snippets; kept {} bytes" ,
189+ snippets_bytes
190+ ) ;
185191 }
186192
187- let mut out =
188- String :: with_capacity ( fixed + snippets_bytes + if dropped > 0 { 32 } else { 0 } ) ;
193+ let mut out = String :: with_capacity (
194+ head. len ( )
195+ + history_header. len ( )
196+ + tail. len ( )
197+ + snippets_bytes
198+ + if dropped > 0 { 32 } else { 0 } ,
199+ ) ;
189200 out. push_str ( & head) ;
190201 out. push_str ( history_header) ;
191202 if dropped > 0 {
@@ -211,13 +222,13 @@ fn push_history_snippet(out: &mut String, item: &HistoryItem) {
211222 match item {
212223 HistoryItem :: User ( s) => {
213224 out. push_str ( "[user] " ) ;
214- out. push_str ( & clamp_for_snippet ( s ) ) ;
225+ out. push_str ( s ) ;
215226 out. push ( '\n' ) ;
216227 }
217228 HistoryItem :: Assistant { text, tool_calls } => {
218229 out. push_str ( "[assistant] " ) ;
219230 if !text. is_empty ( ) {
220- out. push_str ( & clamp_for_snippet ( text) ) ;
231+ out. push_str ( text) ;
221232 }
222233 for c in tool_calls {
223234 out. push_str ( & format ! ( " tool:{}" , c. name) ) ;
@@ -226,14 +237,30 @@ fn push_history_snippet(out: &mut String, item: &HistoryItem) {
226237 }
227238 HistoryItem :: ToolResult ( r) => {
228239 out. push_str ( if r. is_error { "[tool_err] " } else { "[tool] " } ) ;
229- out. push_str ( & clamp_for_snippet ( & r. text ( ) ) ) ;
240+ out. push_str ( & r. text ( ) ) ;
230241 out. push ( '\n' ) ;
231242 }
232243 }
233244}
234245
235- fn clamp_for_snippet ( s : & str ) -> String {
236- clamp_bytes ( s, HANDOFF_SNIPPET_BYTES )
246+ /// Byte budget for session-history text inside the handoff prompt. The
247+ /// summarizer uses the same provider/model config as normal completion, so
248+ /// derive the input budget from the model context window instead of applying a
249+ /// separate fixed prompt cap. We keep the same 1 byte/token upper-bound
250+ /// estimate used by the handoff gate, which is conservative: it may drop old
251+ /// history early for unusually large sessions, but it should not build a prompt
252+ /// that exceeds the configured context window.
253+ fn handoff_prompt_budget_bytes (
254+ max_context_tokens : u64 ,
255+ max_output_tokens : u32 ,
256+ fixed_prompt_bytes : usize ,
257+ ) -> usize {
258+ max_context_tokens
259+ . saturating_sub ( u64:: from ( max_output_tokens) )
260+ . saturating_mul ( CONSERVATIVE_BYTES_PER_TOKEN )
261+ . saturating_sub ( u64:: try_from ( fixed_prompt_bytes) . unwrap_or ( u64:: MAX ) )
262+ . try_into ( )
263+ . unwrap_or ( usize:: MAX )
237264}
238265
239266pub ( crate ) fn clamp_bytes ( s : & str , max_bytes : usize ) -> String {
@@ -299,7 +326,20 @@ fn byte_fallback_threshold(
299326
300327#[ cfg( test) ]
301328mod tests {
302- use super :: { byte_fallback_threshold, estimate_tokens_from_bytes, token_threshold} ;
329+ use super :: {
330+ byte_fallback_threshold, estimate_tokens_from_bytes, handoff_prompt_budget_bytes,
331+ token_threshold,
332+ } ;
333+
334+ #[ test]
335+ fn handoff_prompt_budget_reserves_summary_output_and_fixed_prompt ( ) {
336+ assert_eq ! ( handoff_prompt_budget_bytes( 25_000 , 8_192 , 1_000 ) , 15_808 ) ;
337+ }
338+
339+ #[ test]
340+ fn handoff_prompt_budget_saturates_when_fixed_prompt_exceeds_window ( ) {
341+ assert_eq ! ( handoff_prompt_budget_bytes( 1_000 , 2_000 , 10_000 ) , 0 ) ;
342+ }
303343
304344 #[ test]
305345 fn token_threshold_uses_fraction_when_output_is_small ( ) {
0 commit comments