Skip to content

Commit ca8324d

Browse files
tlongwell-blocknpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757
andauthored
Log handoff token counts (#1954)
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
1 parent 7ed6422 commit ca8324d

2 files changed

Lines changed: 89 additions & 19 deletions

File tree

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

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ use crate::config::{
44
};
55
use 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+
719
pub(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.
300326
const 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.

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use std::collections::VecDeque;
99
use std::process::Stdio;
10-
use std::sync::Arc;
10+
use std::sync::{Arc, Mutex as StdMutex};
1111
use std::time::{Duration, Instant};
1212

1313
use serde_json::{json, Value};
@@ -88,6 +88,7 @@ struct Harness {
8888
child: tokio::process::Child,
8989
stdin: tokio::process::ChildStdin,
9090
stdout: BufReader<tokio::process::ChildStdout>,
91+
stderr: Arc<StdMutex<String>>,
9192
next_id: i64,
9293
}
9394

@@ -108,15 +109,36 @@ impl Harness {
108109
}
109110
cmd.stdin(Stdio::piped())
110111
.stdout(Stdio::piped())
111-
.stderr(Stdio::inherit())
112+
.stderr(Stdio::piped())
112113
.kill_on_drop(true);
113114
let mut child = cmd.spawn().expect("spawn buzz-agent");
114115
let stdin = child.stdin.take().unwrap();
115116
let stdout = BufReader::new(child.stdout.take().unwrap());
117+
let stderr = child.stderr.take().unwrap();
118+
let stderr_buf = Arc::new(StdMutex::new(String::new()));
119+
let stderr_out = Arc::clone(&stderr_buf);
120+
tokio::spawn(async move {
121+
let mut reader = BufReader::new(stderr);
122+
let mut line = String::new();
123+
loop {
124+
line.clear();
125+
let n = match reader.read_line(&mut line).await {
126+
Ok(n) => n,
127+
Err(_) => break,
128+
};
129+
if n == 0 {
130+
break;
131+
}
132+
if let Ok(mut out) = stderr_out.lock() {
133+
out.push_str(&line);
134+
}
135+
}
136+
});
116137
Self {
117138
child,
118139
stdin,
119140
stdout,
141+
stderr: stderr_buf,
120142
next_id: 1,
121143
}
122144
}
@@ -169,6 +191,10 @@ impl Harness {
169191
let _ = tokio::time::timeout(Duration::from_secs(2), self.child.wait()).await;
170192
let _ = self.child.start_kill();
171193
}
194+
195+
fn stderr_text(&self) -> String {
196+
self.stderr.lock().map(|s| s.clone()).unwrap_or_default()
197+
}
172198
}
173199

174200
fn openai_text(content: &str) -> Value {
@@ -1381,6 +1407,15 @@ async fn token_usage_over_budget_triggers_handoff() {
13811407
"expected handoff summarize() between the two prompts (3 reqs), saw {captured} — \
13821408
token gate did not fire on usage over budget"
13831409
);
1410+
let stderr = h.stderr_text();
1411+
assert!(
1412+
stderr.contains("handoff #1 (history"),
1413+
"expected handoff log line in stderr, got: {stderr}"
1414+
);
1415+
assert!(
1416+
stderr.contains(" -> ") && stderr.contains(" tokens"),
1417+
"expected handoff log to include before/after token counts, got: {stderr}"
1418+
);
13841419
h.shutdown().await;
13851420
}
13861421

0 commit comments

Comments
 (0)