Skip to content

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

chore: files changed crates/openhuman-core/src/agent/session_host/driver.rs,crates/openhuman-core/sr

Steven Enamakel862dd7b3c6fa
diff --git a/crates/openhuman-core/src/agent/session_host/driver.rs b/crates/openhuman-core/src/agent/session_host/driver.rs
index d5222132c7..60bd857cd4 100644
--- a/crates/openhuman-core/src/agent/session_host/driver.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver.rs
@@ -424,21 +424,50 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
}
}
+/// Share of `max_history_messages` a cut keeps, as `KEEP_NUMERATOR /
+/// KEEP_DENOMINATOR`.
+const HISTORY_TRIM_KEEP_NUMERATOR: usize = 3;
+const HISTORY_TRIM_KEEP_DENOMINATOR: usize = 5;
+
+/// How many non-system messages a cut keeps for a given bound: 60% of it (at
+/// least one message), or none when the bound itself is zero.
+fn history_trim_target(max_history_messages: usize) -> usize {
+ if max_history_messages == 0 {
+ return 0;
+ }
+ (max_history_messages * HISTORY_TRIM_KEEP_NUMERATOR / HISTORY_TRIM_KEEP_DENOMINATOR).max(1)
+}
+
/// Preserve the stable system prefix while bounding durable conversational
/// history. The runtime owns history replacement, so this must happen before
/// its successful `DriverOutcome` is committed.
///
+/// The cut is hysteretic: once history exceeds `max_history_messages` it drops
+/// to 60% of the bound in one step, rather than shedding the oldest messages
+/// every turn. The messages right after the system prompt are part of the
+/// provider's cached prompt prefix; a cut that moved them every turn made every
+/// turn of a long thread a full cache miss. With a step cut the prefix stays
+/// byte-identical until history grows back past the bound.
+///
/// The cut never splits an assistant tool-call turn from its results (#6721):
/// a history opening on an orphaned `tool` message is rejected by the provider
/// on every later turn. `find_safe_cutoff_point` moves the cut back to keep the
-/// owning assistant turn (so the bound may be exceeded by one tool group).
+/// owning assistant turn (so the target may be exceeded by one tool group).
fn trim_history(history: &mut Vec<Message>, max_history_messages: usize) {
let prefix_len = system_prefix_len(history);
let retained = history.len().saturating_sub(prefix_len);
if retained > max_history_messages {
+ let target = history_trim_target(max_history_messages);
let cut = tinyagents_harness::summarization::find_safe_cutoff_point(
&history[prefix_len..],
- retained - max_history_messages,
+ retained - target,
+ );
+ tracing::debug!(
+ retained,
+ max_history_messages,
+ target,
+ dropped = cut,
+ "[session_host::driver] history over bound; step-trimmed to target (cached prefix moves once)"
);
history.drain(prefix_len..prefix_len + cut);
}
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 e9f3e4f5cf..6f289042a2 100644
--- a/crates/openhuman-core/src/agent/session_host/driver_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver_tests.rs
@@ -165,8 +165,8 @@ fn tool_snapshot_with_no_executable_source_fails_closed_before_graph() {
}
/// #6721: the req-029 → req-031 shape — an assistant issued 3 tool calls and
-/// the 50-message cut lands on the second result. The trimmed history must not
-/// open on an orphaned `tool` message.
+/// the cut lands on the second result. The trimmed history must not open on an
+/// orphaned `tool` message.
#[test]
fn trim_history_keeps_tool_call_group_whole() {
let mut calling = Message::assistant("");
@@ -177,6 +177,7 @@ fn trim_history_keeps_tool_call_group_whole() {
.collect();
}
let mut history = vec![Message::system("s0"), Message::system("s1")];
+ history.extend((0..20).map(|i| Message::user(format!("early {i}"))));
history.extend([
Message::user("u0"),
calling,
@@ -185,8 +186,9 @@ fn trim_history_keeps_tool_call_group_whole() {
Message::tool("c", "rc"),
Message::assistant("done"),
]);
- history.extend((0..47).map(|i| Message::user(format!("filler {i}"))));
- // 53 non-system messages → the raw cut drops 3 and would land on tool "b".
+ history.extend((0..27).map(|i| Message::user(format!("filler {i}"))));
+ // 53 non-system messages, bound 50 → the step cut keeps 30, so it drops 23:
+ // the 20 early messages, u0, the calling turn and tool "a" — landing on "b".
trim_history(&mut history, 50);
@@ -201,10 +203,56 @@ fn trim_history_keeps_tool_call_group_whole() {
}
#[test]
-fn trim_history_without_tool_group_trims_to_the_bound() {
+fn trim_history_without_tool_group_step_trims_to_sixty_percent() {
let mut history = vec![Message::system("s0")];
history.extend((0..60).map(|i| Message::user(format!("m{i}"))));
trim_history(&mut history, 50);
- assert_eq!(history.len(), 51);
- assert_eq!(history[1].text(), "m10");
+ assert_eq!(history.len(), 31);
+ assert_eq!(history[1].text(), "m30");
+}
+
+#[test]
+fn trim_history_leaves_history_within_the_bound_untouched() {
+ let mut history = vec![Message::system("s0")];
+ history.extend((0..50).map(|i| Message::user(format!("m{i}"))));
+ let before = history.clone();
+ trim_history(&mut history, 50);
+ assert_eq!(history, before);
+}
+
+#[test]
+fn history_trim_target_edges() {
+ assert_eq!(history_trim_target(0), 0);
+ assert_eq!(history_trim_target(1), 1);
+ assert_eq!(history_trim_target(50), 30);
+}
+
+/// Prompt-cache regression: the message right after the system prompt is the
+/// head of the provider's cached prefix. Before the step cut, a thread past the
+/// bound shed its oldest messages every turn, so the head moved (and the whole
+/// prompt missed the cache) on every one of these 30 turns.
+#[test]
+fn trim_history_keeps_the_cached_prefix_stable_across_turns() {
+ let mut history = vec![Message::system("s0")];
+ history.extend((0..50).map(|i| Message::user(format!("m{i}"))));
+ let mut next = 50;
+ let mut head_changes = 0;
+ let mut head = history[1].text();
+ for _turn in 0..30 {
+ // One turn: user message + assistant reply.
+ history.push(Message::user(format!("m{next}")));
+ history.push(Message::assistant(format!("r{next}")));
+ next += 1;
+ trim_history(&mut history, 50);
+ assert!(history.len() - 1 <= 50, "history stays within the bound");
+ if history[1].text() != head {
+ head_changes += 1;
+ head = history[1].text();
+ }
+ }
+ // Each cut frees 20 slots = 10 turns, so 30 turns cut at most 3 times.
+ assert!(
+ head_changes <= 3,
+ "cached prefix moved {head_changes} times in 30 turns"
+ );
}