Skip to content

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

refactor(session_host): move final_call_tokens into grounded_close

Steven Enamakelc4751fe0d47f
diff --git a/crates/openhuman-core/src/agent/session_host/driver.rs b/crates/openhuman-core/src/agent/session_host/driver.rs
index e1019733ed..a3dd6383d7 100644
--- a/crates/openhuman-core/src/agent/session_host/driver.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver.rs
@@ -463,7 +463,7 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
(
observed.last_call_input_tokens,
observed.last_call_output_tokens,
- ) = final_call_tokens(
+ ) = grounded_close::final_call_tokens(
loop_last_call,
close.as_ref().map(|close| &close.usage),
repair_usage,
@@ -508,26 +508,6 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
}
}
-/// Input and output tokens of the turn's final model call.
-///
-/// The grounded close and the required-output repair both run after the
-/// harness loop, in that order, so the newest of them that reached the
-/// provider made the final call; without either, the loop's last answered
-/// call did.
-fn final_call_tokens(
- loop_last_call: Option<(u64, u64)>,
- close: Option<&grounded_close::RepairUsage>,
- repair: Option<&grounded_close::RepairUsage>,
-) -> (u64, u64) {
- [repair, close]
- .into_iter()
- .flatten()
- .find(|usage| usage.last_call_input_tokens > 0 || usage.last_call_output_tokens > 0)
- .map(|usage| (usage.last_call_input_tokens, usage.last_call_output_tokens))
- .or(loop_last_call)
- .unwrap_or_default()
-}
-
fn system_prefix_len(history: &[Message]) -> usize {
history
.iter()
diff --git a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
index 86f2fe9e0d..ef2f7ac37f 100644
--- a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
@@ -92,6 +92,25 @@ impl RepairUsage {
}
}
+/// Input and output tokens of the turn's final model call.
+///
+/// The grounded close and required-output repair run after the harness loop,
+/// in that order, so the newest of them that reached the provider made the
+/// final call; without either, the loop's last answered call did.
+pub(super) fn final_call_tokens(
+ loop_last_call: Option<(u64, u64)>,
+ close: Option<&RepairUsage>,
+ repair: Option<&RepairUsage>,
+) -> (u64, u64) {
+ [repair, close]
+ .into_iter()
+ .flatten()
+ .find(|usage| usage.last_call_input_tokens > 0 || usage.last_call_output_tokens > 0)
+ .map(|usage| (usage.last_call_input_tokens, usage.last_call_output_tokens))
+ .or(loop_last_call)
+ .unwrap_or_default()
+}
+
/// Resolve OpenHuman's selected summarizer per close request and forward that
/// request unchanged. TinyAgents owns streaming, validation and repair.
struct SummarizerSourceModel<'a> {
diff --git a/crates/openhuman-core/src/agent/session_host/driver_tests.rs b/crates/openhuman-core/src/agent/session_host/driver_tests.rs
index f73ebc09ce..0b25d057c1 100644
--- a/crates/openhuman-core/src/agent/session_host/driver_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver_tests.rs
@@ -228,17 +228,20 @@ fn final_call_tokens_prefer_the_newest_call_that_reached_the_provider() {
};
let loop_last = Some((90_000, 400));
- assert_eq!(final_call_tokens(loop_last, None, None), (90_000, 400));
assert_eq!(
- final_call_tokens(loop_last, Some(&call(95_000, 300)), None),
+ grounded_close::final_call_tokens(loop_last, None, None),
+ (90_000, 400)
+ );
+ assert_eq!(
+ grounded_close::final_call_tokens(loop_last, Some(&call(95_000, 300)), None),
(95_000, 300)
);
assert_eq!(
- final_call_tokens(loop_last, Some(&call(0, 300)), None),
+ grounded_close::final_call_tokens(loop_last, Some(&call(0, 300)), None),
(0, 300)
);
assert_eq!(
- final_call_tokens(
+ grounded_close::final_call_tokens(
loop_last,
Some(&call(95_000, 300)),
Some(&call(96_000, 200))
@@ -247,14 +250,14 @@ fn final_call_tokens_prefer_the_newest_call_that_reached_the_provider() {
);
// A close that recorded no provider usage did not make the final call.
assert_eq!(
- final_call_tokens(
+ grounded_close::final_call_tokens(
loop_last,
Some(&grounded_close::RepairUsage::default()),
None
),
(90_000, 400)
);
- assert_eq!(final_call_tokens(None, None, None), (0, 0));
+ assert_eq!(grounded_close::final_call_tokens(None, None, None), (0, 0));
}
#[test]