Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(harness): charge the checkpoint budget what each result renders

sanil-239ed0e43da9f4
diff --git a/src/openhuman/agent/harness/session/turn_checkpoint.rs b/src/openhuman/agent/harness/session/turn_checkpoint.rs
index a81ac9bfb4..96505b7a59 100644
--- a/src/openhuman/agent/harness/session/turn_checkpoint.rs
+++ b/src/openhuman/agent/harness/session/turn_checkpoint.rs
@@ -183,13 +183,31 @@ pub(super) fn build_deterministic_checkpoint(
if results.is_empty() {
out.push_str("\n- (no tools completed yet)\n");
} else {
+ // Render each block before choosing, so the budget is charged what
+ // the checkpoint will actually contain (CodeRabbit on #6068). Walking
+ // `r.content` alone undercounts by the per-result header and the `" > "`
+ // on every line — a newline-heavy result costs well over its body, and
+ // several of them overran the limit while the walk believed it had room.
+ let blocks: Vec<String> = results
+ .iter()
+ .map(|r| {
+ let status = if r.success { "ok" } else { "failed" };
+ let mut block = format!("\n- `{}` — {}\n", r.name, status);
+ for line in r.content.lines() {
+ block.push_str(" > ");
+ block.push_str(line);
+ block.push('\n');
+ }
+ block
+ })
+ .collect();
// Choose how far back the budget reaches by walking from the newest
// result, then render in the original order — a checkpoint read
// backwards is harder to follow than one that simply starts later.
let mut budget = CHECKPOINT_TOTAL_CHARS;
let mut first_shown = results.len();
- for (idx, r) in results.iter().enumerate().rev() {
- let cost = r.content.chars().count();
+ for (idx, block) in blocks.iter().enumerate().rev() {
+ let cost = block.chars().count();
if cost > budget && idx + 1 < results.len() {
// The newest result is always shown, however long, so a single
// oversized payload cannot empty the checkpoint entirely; every
@@ -204,16 +222,8 @@ pub(super) fn build_deterministic_checkpoint(
"\n_({first_shown} earlier tool result(s) omitted for length — the most recent are shown.)_\n"
));
}
- for r in &results[first_shown..] {
- let status = if r.success { "ok" } else { "failed" };
- out.push_str(&format!("\n- `{}` — {}\n", r.name, status));
- if !r.content.is_empty() {
- for line in r.content.lines() {
- out.push_str(" > ");
- out.push_str(line);
- out.push('\n');
- }
- }
+ for block in &blocks[first_shown..] {
+ out.push_str(block);
}
}
out.push_str(
diff --git a/src/openhuman/agent/harness/session/turn_checkpoint_tests.rs b/src/openhuman/agent/harness/session/turn_checkpoint_tests.rs
index 0919a4c11c..0683447387 100644
--- a/src/openhuman/agent/harness/session/turn_checkpoint_tests.rs
+++ b/src/openhuman/agent/harness/session/turn_checkpoint_tests.rs
@@ -82,3 +82,42 @@ fn no_completed_tools_says_so() {
assert!(out.contains("(no tools completed yet)"));
assert!(out.contains("**Next steps:**"));
}
+
+/// The budget must be charged what the checkpoint renders, not the raw body.
+/// Every result costs a header line and `" > "` on each of its lines, so a
+/// newline-heavy payload runs well over its body length — and several of them
+/// overran the limit while the walk still believed it had room (CodeRabbit on
+/// #6068).
+#[test]
+fn newline_heavy_results_are_charged_what_they_render() {
+ // Many one-character lines is the shape that maximises the overhead:
+ // the body is ~400 chars and renders to ~1220, since every line pays
+ // `" > "` plus its newline. A gentler shape does not discriminate — 40
+ // lines of 20 chars renders 4100 against a 4000 budget, inside this
+ // assertion's slack, so the test would have passed against the very code
+ // it exists to catch. Verified by replaying both walks over this input.
+ let body = (0..200).map(|_| "y").collect::<Vec<_>>().join("\n");
+ let results: Vec<CheckpointToolResult> = (0..20)
+ .map(|i| result(&format!("list_issues_{i}"), true, &body))
+ .collect();
+
+ let out = build_deterministic_checkpoint(&results, 25);
+
+ // The preamble and the "next steps" footer are unconditional; everything
+ // beyond them is what the budget governs. Taking the empty-results render
+ // as their upper bound keeps this from restating the format string.
+ let fixed = build_deterministic_checkpoint(&[], 25).chars().count();
+ assert!(
+ out.chars().count() <= CHECKPOINT_TOTAL_CHARS + fixed,
+ "the rendered checkpoint must respect the budget: {} > {}",
+ out.chars().count(),
+ CHECKPOINT_TOTAL_CHARS + fixed
+ );
+ assert!(
+ out.contains("earlier tool result(s) omitted for length"),
+ "20 results cannot fit, so the omission must be disclosed: {out}"
+ );
+ // Still useful: the newest result is present with its body.
+ assert!(out.contains("`list_issues_19` — ok"));
+ assert!(out.contains(" > y\n"), "the newest body must be rendered");
+}