Skip to content

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

fix(archivist): hold the host recap to one deadline and its budget

Shanu8f1ac5563ea6
diff --git a/crates/openhuman-core/src/agent/harness/archivist/recap.rs b/crates/openhuman-core/src/agent/harness/archivist/recap.rs
index 6c82991fe7..497afe72d6 100644
--- a/crates/openhuman-core/src/agent/harness/archivist/recap.rs
+++ b/crates/openhuman-core/src/agent/harness/archivist/recap.rs
@@ -105,6 +105,11 @@ Use only what the transcript says.";
/// The transcript a host-side fold reads: one `role: text` paragraph per turn,
/// oldest first, keeping the newest turns that fit in `max_chars`.
+///
+/// The bound holds for every turn, the newest included: a single turn longer
+/// than the whole budget (a pasted document, say) is cut at a character
+/// boundary rather than sent whole, since a fold over an input the model
+/// refuses leaves the segment with no recap at all.
pub(super) fn transcript(entries: &[&EpisodicTurn], max_chars: usize) -> String {
let mut kept = Vec::new();
let mut used = 0usize;
@@ -114,16 +119,33 @@ pub(super) fn transcript(entries: &[&EpisodicTurn], max_chars: usize) -> String
continue;
}
let paragraph = format!("{}: {text}", entry.role);
- used = used.saturating_add(paragraph.len() + 2);
- if used > max_chars && !kept.is_empty() {
+ let separator = if kept.is_empty() { 0 } else { 2 };
+ if used + separator + paragraph.len() > max_chars {
+ if kept.is_empty() {
+ kept.push(cut_at_char_boundary(&paragraph, max_chars).to_string());
+ }
break;
}
+ used += separator + paragraph.len();
kept.push(paragraph);
}
kept.reverse();
kept.join("\n\n")
}
+/// The longest prefix of `text` no longer than `max` bytes that ends on a
+/// character boundary.
+fn cut_at_char_boundary(text: &str, max: usize) -> &str {
+ if text.len() <= max {
+ return text;
+ }
+ let mut end = max;
+ while !text.is_char_boundary(end) {
+ end -= 1;
+ }
+ &text[..end]
+}
+
/// Folds a segment's turns with the host's own summarisation model.
///
/// For a driver with no model to fold with: hosted memory answers `summarise`
@@ -376,8 +398,12 @@ impl ArchivistHook {
// on. `elapsed_ms` rides every arm so the number above can be
// re-tuned from real folds.
let started = std::time::Instant::now();
- let summary_result = match tokio::time::timeout(
- RECAP_DEADLINE,
+ // One deadline for the whole recap: a driver that answers
+ // `Unsupported` late leaves the host's fold only what is left
+ // of it, so the turn never waits longer than `RECAP_DEADLINE`.
+ let deadline = tokio::time::Instant::now() + RECAP_DEADLINE;
+ let summary_result = match tokio::time::timeout_at(
+ deadline,
fold_through_driver(&corpus_inputs, &summary_ctx),
)
.await
@@ -427,9 +453,8 @@ impl ArchivistHook {
"[archivist] summarize_entries: driver has no summariser — \
folding with the host model segment={segment_id}: {e}"
);
- let started = std::time::Instant::now();
- match tokio::time::timeout(
- RECAP_DEADLINE,
+ match tokio::time::timeout_at(
+ deadline,
fold_with_host_model(config, entries),
)
.await
diff --git a/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rs b/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rs
index c3d88ec1d1..20392654f1 100644
--- a/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rs
+++ b/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rs
@@ -120,11 +120,26 @@ fn a_transcript_keeps_the_newest_turns_that_fit() {
assert_eq!(tight, "user: second question\n\nassistant: the answer");
assert_eq!(
transcript(&refs[..1], 5),
- "user: first question",
- "one turn always fits, whatever the budget"
+ "user:",
+ "a turn longer than the whole budget is cut to it, not sent whole"
);
}
+/// A cut never splits a character, however the budget falls.
+#[test]
+fn an_oversized_turn_is_cut_on_a_character_boundary() {
+ let turns = [turn("user", "café ☕ au lait")];
+ let refs: Vec<&EpisodicTurn> = turns.iter().collect();
+ for max in 0..40 {
+ let cut = transcript(&refs, max);
+ assert!(cut.len() <= max, "max={max} gave {} bytes", cut.len());
+ assert!(
+ "user: café ☕ au lait".starts_with(&cut),
+ "max={max}: {cut}"
+ );
+ }
+}
+
struct ScriptedRecap;
#[async_trait::async_trait]