Skip to content

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

feat(memory): bound fallback recency and document list to avoid unbounded provider calls

Steven Enamakel8cea02678dde
diff --git a/crates/openhuman-core/src/memory/ops/fallback.rs b/crates/openhuman-core/src/memory/ops/fallback.rs
index b139e9ba9e..68be7a57c7 100644
--- a/crates/openhuman-core/src/memory/ops/fallback.rs
+++ b/crates/openhuman-core/src/memory/ops/fallback.rs
@@ -8,8 +8,16 @@
//!
//! - namespaces: `MemoryCore::namespaces`
//! - ranked recall: `MemoryRecall::recall`
-//! - recency recall: `MemoryCore::list`, newest first
-//! - document list: `MemoryCore::list`, mapped to document summaries
+//! - recency recall: at most [`RECENT_MAX_PAGES`] bounded `export_page` calls
+//! filtered to the namespace, newest first
+//! - document list: `MemoryCore::list` for one namespace, or a bounded scan of
+//! at most [`DOC_LIST_MAX_NAMESPACES`] namespaces, capped at
+//! [`DOC_LIST_CAP`] documents with a `truncated` flag
+//!
+//! Every helper is bounded in provider calls and records: none ever lists every
+//! scope of a remote engine. The proper fix is a bounded
+//! `recent(namespace, limit)` on the tinymemory contract, an upstream follow-up
+//! this module will switch to when it exists.
//!
//! Graph and tree stay capability-gated; [`unsupported_family`] builds the
//! stable error they return, and [`is_unsupported_family_error`] is the
@@ -18,7 +26,7 @@
use chrono::{DateTime, Utc};
use serde_json::{json, Value};
-use crate::memory::api::provider::{MemoryCore, MemoryRecall};
+use crate::memory::api::provider::{MemoryCore, MemoryPortability, MemoryRecall};
use crate::memory::api::recall::OwnedRecallOpts;
use crate::memory::api::types::{
MemoryEntry, MemoryItemKind, NamespaceMemoryHit, RetrievalScoreBreakdown,
@@ -35,11 +43,30 @@ pub(crate) fn unsupported_family(family: &str) -> String {
}
/// Whether `message` is a capability-gate error built by [`unsupported_family`].
-#[allow(dead_code)]
pub(crate) fn is_unsupported_family_error(message: &str) -> bool {
message.contains(UNSUPPORTED_FAMILY_PREFIX) && message.contains(" family")
}
+/// Route an error from a memory RPC handler through the engine error
+/// vocabulary when the bound engine is remote, so a hosted 402 reads
+/// `INSUFFICIENT_CREDITS:` and a rejected session `SESSION_EXPIRED:`. Local
+/// engines and capability-gate errors pass through unchanged.
+pub(crate) fn classify_for_engine(external: bool, message: String) -> String {
+ if !external || is_unsupported_family_error(&message) {
+ return message;
+ }
+ super::engine::classify_engine_message(&message)
+}
+
+/// Records fetched per page by [`recent_hits`], at most.
+pub(crate) const RECENT_PAGE_CAP: usize = 200;
+/// Pages [`recent_hits`] reads, at most.
+pub(crate) const RECENT_MAX_PAGES: usize = 2;
+/// Documents [`document_list`] returns, at most.
+pub(crate) const DOC_LIST_CAP: usize = 200;
+/// Namespaces [`document_list`] scans when none is given, at most.
+pub(crate) const DOC_LIST_MAX_NAMESPACES: usize = 10;
+
fn entry_epoch(entry: &MemoryEntry) -> f64 {
DateTime::parse_from_rfc3339(&entry.timestamp)
.map(|t| t.with_timezone(&Utc).timestamp() as f64)
@@ -115,22 +142,72 @@ pub(crate) async fn recall_hits<P: MemoryRecall + ?Sized>(
.collect())
}
-/// Most recently updated hits via `MemoryCore::list`, newest first.
+/// Read one export record back as an entry (the mandatory driver's payload
+/// shape: `key`, `content`, `category`, `session_id`, `timestamp`).
+fn record_to_entry(record: crate::memory::api::provider::types::ExportRecord) -> Option<MemoryEntry> {
+ let payload = &record.payload;
+ let key = payload.get("key")?.as_str()?.to_string();
+ let content = payload.get("content")?.as_str()?.to_string();
+ let category = payload
+ .get("category")
+ .and_then(Value::as_str)
+ .map(|c| c.parse().unwrap_or_default())
+ .unwrap_or_default();
+ Some(MemoryEntry {
+ id: record.id,
+ key,
+ content,
+ namespace: record.namespace,
+ category,
+ timestamp: payload
+ .get("timestamp")
+ .and_then(Value::as_str)
+ .unwrap_or_default()
+ .to_string(),
+ session_id: payload
+ .get("session_id")
+ .and_then(Value::as_str)
+ .map(str::to_string),
+ score: None,
+ taint: record.taint,
+ })
+}
+
+/// Most recently updated hits for `namespace`, newest first.
///
/// There is no query, so a ranked `recall` cannot answer this: the contract
-/// says an empty query yields nothing.
+/// says an empty query yields nothing. Served from at most
+/// [`RECENT_MAX_PAGES`] `export_page` calls of `min(limit * 4, 200)` records
+/// each, filtered to the namespace — never a full `list`. A page that holds no
+/// record of the namespace triggers the second read; recency is therefore
+/// best-effort within the engine's first pages.
pub(crate) async fn recent_hits(
guard: &MemoryGuard,
namespace: &str,
limit: usize,
) -> Result<Vec<NamespaceMemoryHit>, String> {
+ let page_size = limit.saturating_mul(4).clamp(1, RECENT_PAGE_CAP);
log::debug!(
- "[memory:fallback] recency recall via MemoryCore::list namespace={namespace} limit={limit}"
+ "[memory:fallback] recency recall via bounded export namespace={namespace} limit={limit} page_size={page_size}"
);
- let mut entries = guard
- .list(Some(namespace), None, None)
- .await
- .map_err(|error| error.to_string())?;
+ let mut entries: Vec<MemoryEntry> = Vec::new();
+ let mut cursor: Option<String> = None;
+ for _ in 0..RECENT_MAX_PAGES {
+ let page = guard
+ .export_page(cursor.as_deref(), page_size)
+ .await
+ .map_err(|error| error.to_string())?;
+ entries.extend(
+ page.records
+ .into_iter()
+ .filter(|r| r.namespace.as_deref() == Some(namespace))
+ .filter_map(record_to_entry),
+ );
+ cursor = page.next_cursor;
+ if cursor.is_none() || !entries.is_empty() {
+ break;
+ }
+ }
entries.sort_by(|a, b| entry_epoch(b).total_cmp(&entry_epoch(a)));
entries.truncate(limit);
Ok(entries
@@ -139,34 +216,60 @@ pub(crate) async fn recent_hits(
.collect())
}
-/// Document list in the `{"documents": [...]}` shape `list_documents`
-/// returns, built from `MemoryCore::list`.
+fn document_json(entry: MemoryEntry, namespace: Option<&str>) -> Value {
+ let ts = entry_epoch(&entry);
+ json!({
+ "documentId": entry.id,
+ "namespace": entry.namespace.or_else(|| namespace.map(str::to_string)).unwrap_or_default(),
+ "key": entry.key,
+ "title": entry.key,
+ "sourceType": "memory",
+ "priority": "medium",
+ "createdAt": ts,
+ "updatedAt": ts,
+ })
+}
+
+/// Document list in the `{"documents": [...], "truncated": bool}` shape
+/// `list_documents` returns, built from `MemoryCore`.
+///
+/// With a namespace: one `list` call, capped at [`DOC_LIST_CAP`]. Without: one
+/// `namespaces` call, then at most [`DOC_LIST_MAX_NAMESPACES`] `list` calls,
+/// stopping at the cap. `truncated` says anything was left out.
pub(crate) async fn document_list(
guard: &MemoryGuard,
namespace: Option<&str>,
) -> Result<Value, String> {
log::debug!("[memory:fallback] doc_list via MemoryCore::list namespace={namespace:?}");
- let entries = guard
- .list(namespace, None, None)
- .await
- .map_err(|error| error.to_string())?;
- let documents = entries
- .into_iter()
- .map(|entry| {
- let ts = entry_epoch(&entry);
- json!({
- "documentId": entry.id,
- "namespace": entry.namespace.or_else(|| namespace.map(str::to_string)).unwrap_or_default(),
- "key": entry.key,
- "title": entry.key,
- "sourceType": "memory",
- "priority": "medium",
- "createdAt": ts,
- "updatedAt": ts,
- })
- })
- .collect::<Vec<_>>();
- Ok(json!({ "documents": documents }))
+ let mut documents: Vec<Value> = Vec::new();
+ let mut truncated = false;
+ let scopes: Vec<String> = match namespace {
+ Some(ns) => vec![ns.to_string()],
+ None => {
+ let all = namespace_names(guard).await?;
+ if all.len() > DOC_LIST_MAX_NAMESPACES {
+ truncated = true;
+ }
+ all.into_iter().take(DOC_LIST_MAX_NAMESPACES).collect()
+ }
+ };
+ for scope in scopes {
+ let entries = guard
+ .list(Some(&scope), None, None)
+ .await
+ .map_err(|error| error.to_string())?;
+ for entry in entries {
+ if documents.len() >= DOC_LIST_CAP {
+ truncated = true;
+ break;
+ }
+ documents.push(document_json(entry, Some(&scope)));
+ }
+ if documents.len() >= DOC_LIST_CAP {
+ break;
+ }
+ }
+ Ok(json!({ "documents": documents, "truncated": truncated }))
}
#[cfg(test)]