Skip to content

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

fix(threads): correct envelope handling in thread operations

Steven Enamakelf97064bf9a30
diff --git a/crates/openhuman-core/src/desktop/provider_surfaces/ops.rs b/crates/openhuman-core/src/desktop/provider_surfaces/ops.rs
index 66b8c661f8..75f7467318 100644
--- a/crates/openhuman-core/src/desktop/provider_surfaces/ops.rs
+++ b/crates/openhuman-core/src/desktop/provider_surfaces/ops.rs
@@ -4,42 +4,20 @@
//! can land before the SQLite-backed store arrives.
use crate::core::Outcome;
-use crate::memory::{ApiEnvelope, ApiMeta, EmptyRequest};
+use crate::memory::{ApiEnvelope, EmptyRequest};
use serde::Serialize;
use std::collections::BTreeMap;
use super::store;
use super::types::{ProviderEvent, RespondQueueItem, RespondQueueListResponse};
-fn request_id() -> String {
- uuid::Uuid::new_v4().to_string()
-}
-
-fn counts(entries: impl IntoIterator<Item = (&'static str, usize)>) -> BTreeMap<String, usize> {
- entries
- .into_iter()
- .map(|(k, v)| (k.to_string(), v))
- .collect()
-}
+use crate::memory::ops::envelope::{envelope as shared_envelope, memory_counts as counts};
fn envelope<T: Serialize>(
data: T,
counts: Option<BTreeMap<String, usize>>,
) -> Outcome<ApiEnvelope<T>> {
- Outcome::new(
- ApiEnvelope {
- data: Some(data),
- error: None,
- meta: ApiMeta {
- request_id: request_id(),
- latency_seconds: None,
- cached: None,
- counts,
- pagination: None,
- },
- },
- vec![],
- )
+ shared_envelope(data, counts, None)
}
pub async fn ingest_event(
diff --git a/crates/openhuman-core/src/memory/ops/envelope_tests.rs b/crates/openhuman-core/src/memory/ops/envelope_tests.rs
index fb5e6b7dff..d4fc41f2f1 100644
--- a/crates/openhuman-core/src/memory/ops/envelope_tests.rs
+++ b/crates/openhuman-core/src/memory/ops/envelope_tests.rs
@@ -25,3 +25,33 @@ fn error_envelope_wraps_code_and_message() {
assert_eq!(value["error"]["message"], "boom");
assert!(value["data"].is_null());
}
+
+#[test]
+fn envelope_propagates_counts_and_pagination_and_leaves_the_rest_unset() {
+ let counts = memory_counts([("num_messages", 7)]);
+ let pagination = PaginationMeta {
+ limit: 10,
+ offset: 0,
+ count: 7,
+ };
+ let out = envelope(
+ serde_json::json!({"v": 42}),
+ Some(counts.clone()),
+ Some(pagination),
+ );
+ let env = &out.value;
+ assert_eq!(env.data.as_ref().unwrap()["v"], 42);
+ assert!(env.error.is_none());
+ assert_eq!(env.meta.counts.as_ref().unwrap(), &counts);
+ let pag = env.meta.pagination.as_ref().unwrap();
+ assert_eq!((pag.limit, pag.offset, pag.count), (10, 0, 7));
+ assert!(env.meta.latency_seconds.is_none() && env.meta.cached.is_none());
+ assert!(out.logs.is_empty());
+}
+
+#[test]
+fn memory_request_id_is_a_fresh_v4_uuid() {
+ let (a, b) = (memory_request_id(), memory_request_id());
+ assert_eq!(a.len(), 36);
+ assert_ne!(a, b);
+}
diff --git a/crates/openhuman-core/src/threads/ops/support.rs b/crates/openhuman-core/src/threads/ops/support.rs
index c3468f8c20..eba2414cd4 100644
--- a/crates/openhuman-core/src/threads/ops/support.rs
+++ b/crates/openhuman-core/src/threads/ops/support.rs
@@ -16,39 +16,8 @@ use std::collections::BTreeMap;
use std::path::PathBuf;
use tinyagents_harness::title::{title_from_user_message, title_log_fingerprint};
-pub(super) fn request_id() -> String {
- uuid::Uuid::new_v4().to_string()
-}
-
-pub(super) fn counts(
- entries: impl IntoIterator<Item = (&'static str, usize)>,
-) -> BTreeMap<String, usize> {
- entries
- .into_iter()
- .map(|(k, v)| (k.to_string(), v))
- .collect()
-}
-
-pub(super) fn envelope<T: Serialize>(
- data: T,
- counts: Option<BTreeMap<String, usize>>,
- pagination: Option<PaginationMeta>,
-) -> Outcome<ApiEnvelope<T>> {
- Outcome::new(
- ApiEnvelope {
- data: Some(data),
- error: None,
- meta: ApiMeta {
- request_id: request_id(),
- latency_seconds: None,
- cached: None,
- counts,
- pagination,
- },
- },
- vec![],
- )
-}
+// One envelope/counts implementation for every ApiEnvelope-returning domain.
+pub(super) use crate::memory::ops::envelope::{envelope, memory_counts as counts};
pub(super) async fn workspace_dir() -> Result<PathBuf, String> {
Config::load_or_init()
diff --git a/crates/openhuman-core/src/threads/ops_conversion_tests.rs b/crates/openhuman-core/src/threads/ops_conversion_tests.rs
index b60ff31978..3575005ab7 100644
--- a/crates/openhuman-core/src/threads/ops_conversion_tests.rs
+++ b/crates/openhuman-core/src/threads/ops_conversion_tests.rs
@@ -1,29 +1,5 @@
use super::*;
-// ── request_id ────────────────────────────────────────────────
-
-#[test]
-fn request_id_is_a_non_empty_uuid_and_fresh_per_call() {
- let a = request_id();
- let b = request_id();
- assert!(!a.is_empty());
- // v4 UUID canonical form: 36 chars with 4 hyphens.
- assert_eq!(a.len(), 36);
- assert_eq!(a.chars().filter(|c| *c == '-').count(), 4);
- // Two calls must not collide — catches accidental caching.
- assert_ne!(a, b);
-}
-
-// ── counts ────────────────────────────────────────────────────
-
-#[test]
-fn counts_materialises_entries_as_owned_string_keys() {
- let map = counts([("num_threads", 3), ("num_messages", 7)]);
- assert_eq!(map.get("num_threads"), Some(&3));
- assert_eq!(map.get("num_messages"), Some(&7));
- assert_eq!(map.len(), 2);
-}
-
// NOTE: the title_log_fingerprint / collapse_whitespace copies were removed
// here (plan.md §2.1) — threads/title.rs (the owning module) already covers
// these functions with equivalent cases; the lowercase-hex assertion was
@@ -136,45 +112,6 @@ fn is_auto_generated_thread_title_accepts_only_the_new_chat_format() {
}
}
-// ── envelope ──────────────────────────────────────────────────
-
-#[test]
-fn envelope_sets_data_and_propagates_counts_and_pagination() {
- let pagination = PaginationMeta {
- limit: 10,
- offset: 0,
- count: 7,
- };
- let counts_map = counts([("num_messages", 7)]);
- let out = envelope(
- json!({"v": 42}),
- Some(counts_map.clone()),
- Some(pagination.clone()),
- );
- let env = &out.value;
- assert_eq!(env.data.as_ref().unwrap()["v"], json!(42));
- assert!(env.error.is_none());
- assert!(!env.meta.request_id.is_empty());
- assert_eq!(env.meta.counts.as_ref().unwrap(), &counts_map);
- let pag = env.meta.pagination.as_ref().unwrap();
- assert_eq!(pag.limit, pagination.limit);
- assert_eq!(pag.count, pagination.count);
- assert_eq!(pag.offset, pagination.offset);
- // No implicit latency/cached info — the envelope helper keeps
- // optional fields unset so callers opt in explicitly.
- assert!(env.meta.latency_seconds.is_none());
- assert!(env.meta.cached.is_none());
- // No logs are attached by default.
- assert!(out.logs.is_empty());
-}
-
-#[test]
-fn envelope_omits_counts_and_pagination_when_not_provided() {
- let out = envelope(json!(null), None, None);
- assert!(out.value.meta.counts.is_none());
- assert!(out.value.meta.pagination.is_none());
-}
-
#[test]
fn thread_to_summary_preserves_all_fields() {
let summary = thread_to_summary(sample_thread());