Skip to content

Commit fa1cade

Browse files
tlongwell-blocknpub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf
andauthored
handoff: pass full session history to summarizer (#1033)
Signed-off-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co>
1 parent 6e4c868 commit fa1cade

3 files changed

Lines changed: 195 additions & 33 deletions

File tree

‎crates/buzz-agent/src/config.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ pub const MAX_TOOL_CALLS_PER_TURN: usize = 64;
1818

1919
pub const HANDOFF_MAX_OUTPUT_TOKENS: u32 = 8192;
2020

21-
pub const HANDOFF_TAIL_ITEMS: usize = 5;
22-
2321
pub const HANDOFF_ORIGINAL_TASK_MAX_BYTES: usize = 16 * 1024;
2422

25-
pub const HANDOFF_PROMPT_MAX_BYTES: usize = 32 * 1024;
26-
2723
pub const HANDOFF_MAX_TOOL_NAMES: usize = 20;
2824

2925
const DEFAULT_SYSTEM_PROMPT: &str =

‎crates/buzz-agent/src/handoff.rs‎

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::agent::RunCtx;
22
use 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
};
65
use 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-
2118
impl 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

239266
pub(crate) fn clamp_bytes(s: &str, max_bytes: usize) -> String {
@@ -299,7 +326,20 @@ fn byte_fallback_threshold(
299326

300327
#[cfg(test)]
301328
mod 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() {

‎crates/buzz-agent/tests/regressions.rs‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,132 @@ async fn hook_post_compact_injects_after_handoff() {
11321132
h.shutdown().await;
11331133
}
11341134

1135+
/// The handoff summary prompt should include all session history when that
1136+
/// history fits the summarizer context budget. This protects against regressing
1137+
/// to the old fixed tail of five tiny snippets.
1138+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1139+
async fn handoff_summary_prompt_includes_full_history_within_context_budget() {
1140+
let llm = spawn_capturing_llm(vec![
1141+
openai_text_with_usage("ack-0", 9500),
1142+
openai_text("handoff summary text"),
1143+
openai_text_with_usage("done", 10),
1144+
])
1145+
.await;
1146+
let mut h = Harness::spawn_with_env(
1147+
&llm.url,
1148+
&[
1149+
("BUZZ_AGENT_MAX_CONTEXT_TOKENS", "10000"),
1150+
("BUZZ_AGENT_MAX_OUTPUT_TOKENS", "1000"),
1151+
("BUZZ_AGENT_MAX_HANDOFFS", "3"),
1152+
(
1153+
"BUZZ_AGENT_MAX_HISTORY_BYTES",
1154+
&(16 * 1024 * 1024).to_string(),
1155+
),
1156+
],
1157+
)
1158+
.await;
1159+
let sid = init_session(&mut h, json!([])).await;
1160+
1161+
let p0 = h
1162+
.send(
1163+
"session/prompt",
1164+
json!({"sessionId": sid, "prompt": [{"type":"text","text":"early-history-marker"}]}),
1165+
)
1166+
.await;
1167+
let _ = h.recv_until(|v| v["id"] == json!(p0)).await;
1168+
1169+
let p1 = h
1170+
.send(
1171+
"session/prompt",
1172+
json!({"sessionId": sid, "prompt": [{"type":"text","text":"late-history-marker"}]}),
1173+
)
1174+
.await;
1175+
let _ = h.recv_until(|v| v["id"] == json!(p1)).await;
1176+
1177+
let captured = llm.captured.lock().await;
1178+
assert_eq!(captured.len(), 3, "expected prompt, handoff, prompt");
1179+
let handoff_messages = captured[1]["messages"].as_array().unwrap();
1180+
let handoff_prompt = handoff_messages[1]["content"].as_str().unwrap();
1181+
assert!(
1182+
handoff_prompt.contains("# Session History (oldest first)"),
1183+
"handoff prompt should describe full session history: {handoff_prompt}"
1184+
);
1185+
assert!(
1186+
handoff_prompt.contains("early-history-marker"),
1187+
"oldest prompt was omitted despite fitting budget: {handoff_prompt}"
1188+
);
1189+
assert!(
1190+
handoff_prompt.contains("ack-0"),
1191+
"assistant response was omitted despite fitting budget: {handoff_prompt}"
1192+
);
1193+
assert!(
1194+
handoff_prompt.contains("late-history-marker"),
1195+
"latest prompt was omitted despite fitting budget: {handoff_prompt}"
1196+
);
1197+
assert!(
1198+
!handoff_prompt.contains("older items omitted"),
1199+
"handoff should not report truncation when full history fits: {handoff_prompt}"
1200+
);
1201+
h.shutdown().await;
1202+
}
1203+
1204+
/// If one item is larger than the derived summarizer budget, keep a truncated
1205+
/// form of the most recent item instead of sending an empty history block.
1206+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1207+
async fn handoff_summary_prompt_keeps_latest_item_when_one_item_exceeds_budget() {
1208+
let llm = spawn_capturing_llm(vec![
1209+
openai_text_with_usage("ack-0", 9500),
1210+
openai_text("handoff summary text"),
1211+
openai_text_with_usage("done", 10),
1212+
])
1213+
.await;
1214+
let mut h = Harness::spawn_with_env(
1215+
&llm.url,
1216+
&[
1217+
("BUZZ_AGENT_MAX_CONTEXT_TOKENS", "10000"),
1218+
("BUZZ_AGENT_MAX_OUTPUT_TOKENS", "1000"),
1219+
("BUZZ_AGENT_MAX_HANDOFFS", "3"),
1220+
(
1221+
"BUZZ_AGENT_MAX_HISTORY_BYTES",
1222+
&(16 * 1024 * 1024).to_string(),
1223+
),
1224+
],
1225+
)
1226+
.await;
1227+
let sid = init_session(&mut h, json!([])).await;
1228+
1229+
let huge = format!("oversize-latest-marker {}", "x".repeat(12000));
1230+
let p0 = h
1231+
.send(
1232+
"session/prompt",
1233+
json!({"sessionId": sid, "prompt": [{"type":"text","text":"early-history-marker"}]}),
1234+
)
1235+
.await;
1236+
let _ = h.recv_until(|v| v["id"] == json!(p0)).await;
1237+
1238+
let p1 = h
1239+
.send(
1240+
"session/prompt",
1241+
json!({"sessionId": sid, "prompt": [{"type":"text","text": huge}]}),
1242+
)
1243+
.await;
1244+
let _ = h.recv_until(|v| v["id"] == json!(p1)).await;
1245+
1246+
let captured = llm.captured.lock().await;
1247+
assert_eq!(captured.len(), 3, "expected prompt, handoff, prompt");
1248+
let handoff_messages = captured[1]["messages"].as_array().unwrap();
1249+
let handoff_prompt = handoff_messages[1]["content"].as_str().unwrap();
1250+
assert!(
1251+
handoff_prompt.contains("oversize-latest-marker"),
1252+
"latest oversized item should be kept in truncated form: {handoff_prompt}"
1253+
);
1254+
assert!(
1255+
handoff_prompt.contains("older items omitted"),
1256+
"handoff should report truncation when history exceeds budget: {handoff_prompt}"
1257+
);
1258+
h.shutdown().await;
1259+
}
1260+
11351261
/// Regression for the original bug: context fills, the provider 400s on the
11361262
/// next request, and the handoff never fires because the old gate measured
11371263
/// BYTES (16 MiB threshold) while the limit is in TOKENS. The fix gates on

0 commit comments

Comments
 (0)