Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/openhuman/agent/harness/session/builder/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Utility helpers used during agent construction.

use crate::openhuman::memory::Memory;
use crate::openhuman::memory_tools::{ToolMemoryRule, ToolMemoryStore};
use crate::openhuman::memory_tools::{tool_memory_store, ToolMemoryRule, ToolMemoryStore};
use std::sync::Arc;

/// (#1400) Best-effort synchronous prefetch of eager tool-scoped rules.
Expand Down Expand Up @@ -33,7 +33,7 @@ pub(super) fn prefetch_tool_memory_rules_blocking(
let tool_names = tool_names.to_vec();
tokio::task::block_in_place(|| {
handle.block_on(async move {
let store = ToolMemoryStore::new(memory);
let store = tool_memory_store(memory);
match store.rules_for_prompt(&tool_names).await {
Ok(grouped) => {
let mut flat: Vec<_> = grouped.into_values().flatten().collect();
Expand Down
4 changes: 2 additions & 2 deletions src/openhuman/memory/ops/tool_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::Deserialize;
use serde_json::Value;

use crate::openhuman::memory_tools::{
ToolMemoryPriority, ToolMemoryRule, ToolMemorySource, ToolMemoryStore,
tool_memory_store, ToolMemoryPriority, ToolMemoryRule, ToolMemorySource, ToolMemoryStore,
};
use crate::rpc::RpcOutcome;

Expand Down Expand Up @@ -62,7 +62,7 @@ pub struct ToolRulesForPromptParams {

async fn open_store() -> Result<ToolMemoryStore, String> {
let client = active_memory_client().await?;
Ok(ToolMemoryStore::new(client.memory_handle()))
Ok(tool_memory_store(client.memory_handle()))
}

/// Upsert a tool-scoped memory rule.
Expand Down
117 changes: 47 additions & 70 deletions src/openhuman/memory_search/scoring.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,30 @@
//! Scoring weight profiles for hybrid retrieval.
//! Scoring weight profiles for hybrid retrieval — thin host shim over
//! `tinycortex::memory::WeightProfile` (W5).
//!
//! The weight profile (graph/vector/keyword/freshness weights + the
//! `BALANCED`/`SEMANTIC`/`LEXICAL`/`GRAPH_FIRST` presets + `by_name`) is the
//! crate's, a byte-identical port. The host keeps only [`compose_score`] — the
//! trivial weighted combination the crate expresses via
//! `retrieval::scoring::hybrid_score` at its own call sites; exposed here as a
//! free function so `memory_search::tools::hybrid_search` keeps its call shape.

#[derive(Debug, Clone, Copy)]
pub struct WeightProfile {
pub graph: f64,
pub vector: f64,
pub keyword: f64,
pub freshness: f64,
}

impl WeightProfile {
pub const BALANCED: Self = Self {
graph: 0.35,
vector: 0.35,
keyword: 0.15,
freshness: 0.15,
};

pub const SEMANTIC: Self = Self {
graph: 0.15,
vector: 0.65,
keyword: 0.20,
freshness: 0.0,
};

pub const LEXICAL: Self = Self {
graph: 0.25,
vector: 0.15,
keyword: 0.60,
freshness: 0.0,
};

pub const GRAPH_FIRST: Self = Self {
graph: 0.55,
vector: 0.30,
keyword: 0.15,
freshness: 0.0,
};
pub use tinycortex::memory::WeightProfile;

pub fn from_name(name: &str) -> Option<Self> {
match name {
"balanced" => Some(Self::BALANCED),
"semantic" => Some(Self::SEMANTIC),
"lexical" => Some(Self::LEXICAL),
"graph_first" => Some(Self::GRAPH_FIRST),
_ => None,
}
}

pub fn compose_score(
&self,
graph_relevance: f64,
vector_similarity: f64,
keyword_relevance: f64,
freshness: f64,
) -> f64 {
(self.graph * graph_relevance)
+ (self.vector * vector_similarity)
+ (self.keyword * keyword_relevance)
+ (self.freshness * freshness)
}
/// Weighted composite of the four retrieval signals under `profile`.
///
/// `graph·graph_relevance + vector·vector_similarity + keyword·keyword_relevance
/// + freshness·freshness`.
pub fn compose_score(
profile: &WeightProfile,
graph_relevance: f64,
vector_similarity: f64,
keyword_relevance: f64,
freshness: f64,
) -> f64 {
(profile.graph * graph_relevance)
+ (profile.vector * vector_similarity)
+ (profile.keyword * keyword_relevance)
+ (profile.freshness * freshness)
}

#[cfg(test)]
Expand All @@ -82,19 +48,30 @@ mod tests {
}

#[test]
fn from_name_resolves() {
assert!(WeightProfile::from_name("balanced").is_some());
assert!(WeightProfile::from_name("semantic").is_some());
assert!(WeightProfile::from_name("lexical").is_some());
assert!(WeightProfile::from_name("graph_first").is_some());
assert!(WeightProfile::from_name("unknown").is_none());
fn by_name_resolves_with_balanced_fallback() {
assert_eq!(
WeightProfile::by_name("semantic").vector,
WeightProfile::SEMANTIC.vector
);
assert_eq!(
WeightProfile::by_name("lexical").keyword,
WeightProfile::LEXICAL.keyword
);
assert_eq!(
WeightProfile::by_name("graph_first").graph,
WeightProfile::GRAPH_FIRST.graph
);
// Unknown names fall back to balanced.
assert_eq!(
WeightProfile::by_name("unknown").graph,
WeightProfile::BALANCED.graph
);
}

#[test]
fn compose_score_applies_weights() {
let profile = WeightProfile::SEMANTIC;
let score = profile.compose_score(0.5, 1.0, 0.5, 0.0);
let expected = 0.15 * 0.5 + 0.65 * 1.0 + 0.20 * 0.5;
assert!((score - expected).abs() < f64::EPSILON);
fn compose_score_is_weighted_sum() {
let p = WeightProfile::BALANCED;
let s = compose_score(&p, 1.0, 1.0, 1.0, 1.0);
assert!((s - (p.graph + p.vector + p.keyword + p.freshness)).abs() < 1e-9);
}
}
5 changes: 3 additions & 2 deletions src/openhuman/memory_search/tools/hybrid_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Tool for MemoryHybridSearchTool {
));
}

let profile = WeightProfile::from_name(&parsed.mode).unwrap_or(WeightProfile::BALANCED);
let profile = WeightProfile::by_name(&parsed.mode);
let limit = parsed.limit.clamp(1, 50);

log::debug!(
Expand Down Expand Up @@ -152,7 +152,8 @@ impl Tool for MemoryHybridSearchTool {
.enumerate()
.map(|(i, hit)| {
let bd = &hit.score_breakdown;
let score = profile.compose_score(
let score = crate::openhuman::memory_search::scoring::compose_score(
&profile,
bd.graph_relevance,
bd.vector_similarity,
bd.keyword_relevance,
Expand Down
Loading
Loading