Skip to content

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

fix(archivist): scan past an exhausted queue head

Shanud15a3a5c09c0
diff --git a/src/openhuman/agent/harness/archivist/resummarise.rs b/src/openhuman/agent/harness/archivist/resummarise.rs
index e3755be913..ca09826978 100644
--- a/src/openhuman/agent/harness/archivist/resummarise.rs
+++ b/src/openhuman/agent/harness/archivist/resummarise.rs
@@ -34,7 +34,24 @@ use crate::openhuman::memory::api::provider::episodic::EpisodicTurn;
/// the queue drains across many passes rather than in one long stall; a large
/// batch would put a run of inference calls behind a single close, and
/// `flush_open_segment` is awaited at session wind-down.
-const RESUMMARISE_BATCH: u32 = 3;
+const RESUMMARISE_BATCH: usize = 3;
+
+/// How far down the pending queue one pass will look to find those segments.
+///
+/// The batch cap alone is not enough, and this is the correction to the first
+/// version of this pass. `segments_pending_summary` orders oldest-first and
+/// takes a `limit`, so asking for exactly [`RESUMMARISE_BATCH`] returns the
+/// three oldest rows *before* the attempt ledger filters them. Once those three
+/// exhaust their attempts they still occupy every result, and a fourth pending
+/// segment can never be reached until the process restarts — the head-of-queue
+/// trap, moved one layer out rather than solved.
+///
+/// So the pass reads a window and stops after [`RESUMMARISE_BATCH`] **eligible**
+/// segments. Bounded rather than unbounded because the point is to make
+/// progress past a stuck head, not to walk an arbitrarily long backlog inside
+/// one segment close; a queue deeper than this drains across passes, which is
+/// what the batch cap is for.
+const RESUMMARISE_SCAN: u32 = 25;
/// How many times one segment may be attempted before this process gives up on
/// it.
@@ -90,7 +107,7 @@ impl ArchivistHook {
return;
};
- let pending = match episodic.segments_pending_summary(RESUMMARISE_BATCH).await {
+ let pending = match episodic.segments_pending_summary(RESUMMARISE_SCAN).await {
Ok(pending) => pending,
Err(e) => {
tracing::debug!("[archivist] resummarise: cannot read the pending queue: {e}");
@@ -106,7 +123,11 @@ impl ArchivistHook {
pending.len()
);
+ let mut attempted = 0_usize;
for segment in pending {
+ if attempted >= RESUMMARISE_BATCH {
+ break;
+ }
if exhausted(&segment.segment_id) {
tracing::debug!(
"[archivist] resummarise: segment={} already attempted {} times in this \
@@ -126,6 +147,10 @@ impl ArchivistHook {
// belongs to a segment by stable per-session sequence or row id,
// not by timestamp, because the md store rounds to milliseconds and
// can sort a fast turn just before its own segment's start.
+ // Counted here rather than at the top of the loop: a skipped
+ // exhausted segment must not consume the batch, which is the whole
+ // point of scanning past it.
+ attempted += 1;
let entries = self.read_session_entries(&segment.session_id).await;
let segment_entries: Vec<&EpisodicTurn> = entries
.iter()
diff --git a/src/openhuman/agent/harness/archivist/resummarise_tests.rs b/src/openhuman/agent/harness/archivist/resummarise_tests.rs
index c3fe803086..b5a706566a 100644
--- a/src/openhuman/agent/harness/archivist/resummarise_tests.rs
+++ b/src/openhuman/agent/harness/archivist/resummarise_tests.rs
@@ -214,3 +214,56 @@ async fn a_segment_with_no_turns_left_is_skipped() {
"a segment with no turns was summarised anyway: {methods:?}"
);
}
+
+/// A queue whose head is exhausted still lets the segments behind it recover.
+///
+/// The batch cap alone did not do this, and that was a real bug in the first
+/// version of this pass (caught in review of #6183). `segments_pending_summary`
+/// orders oldest-first and takes a `limit`, so asking for exactly the batch size
+/// returns the oldest rows *before* the ledger filters them: once those exhaust
+/// their attempts they occupy every result forever and nothing behind them is
+/// ever reached. The pass reads a window and counts only eligible segments
+/// against the batch.
+///
+/// Asserted on the **count** of `episodic.session_turns` calls, because that is
+/// the call every attempted segment makes and the recording driver does not
+/// carry arguments. Against the old code the third pass records zero.
+#[tokio::test]
+async fn an_exhausted_queue_head_does_not_block_the_segments_behind_it() {
+ let recording = Arc::new(
+ RecordingProvider::new()
+ .with_session_turns(turns())
+ .with_pending_segments(vec![
+ pending("seg-head-a"),
+ pending("seg-head-b"),
+ pending("seg-head-c"),
+ pending("seg-behind"),
+ ]),
+ );
+ let hook = hook_over(&recording);
+
+ let turn_reads = |recording: &RecordingProvider| {
+ methods(recording)
+ .iter()
+ .filter(|m| *m == "episodic.session_turns")
+ .count()
+ };
+
+ // Two passes spend the three heads' whole two-attempt budget.
+ hook.resummarise_pending(300.0).await;
+ hook.resummarise_pending(301.0).await;
+ let after_two = turn_reads(&recording);
+ assert_eq!(
+ after_two, 6,
+ "the first two passes should each have attempted the three heads"
+ );
+
+ // The third finds them exhausted and must scan past them.
+ hook.resummarise_pending(302.0).await;
+
+ assert_eq!(
+ turn_reads(&recording) - after_two,
+ 1,
+ "the segment behind three exhausted heads was never reached"
+ );
+}
diff --git a/src/openhuman/memory/guard/test_support_part_02.rs b/src/openhuman/memory/guard/test_support_part_02.rs
index 15b902f797..779d648fab 100644
--- a/src/openhuman/memory/guard/test_support_part_02.rs
+++ b/src/openhuman/memory/guard/test_support_part_02.rs
@@ -263,13 +263,22 @@ impl MemoryEpisodic for RecordingProvider {
async fn segments_pending_summary(
&self,
- _limit: u32,
+ limit: u32,
) -> Result<
Vec<crate::openhuman::memory::api::provider::episodic::ConversationSegment>,
MemoryError,
> {
self.record(Call::plain("episodic.segments_pending_summary"));
- Ok(self.pending_segments.lock().unwrap().clone())
+ // Honour `limit` — a fake that ignored it would let a test drive more
+ // segments than the caller asked for and hide a bounded-recovery bug.
+ Ok(self
+ .pending_segments
+ .lock()
+ .unwrap()
+ .iter()
+ .take(limit as usize)
+ .cloned()
+ .collect())
}
async fn create_segment(