diff --git a/epiphany-core/src/memory_graph/mod.rs b/epiphany-core/src/memory_graph/mod.rs index db0ebebb..627f63eb 100644 --- a/epiphany-core/src/memory_graph/mod.rs +++ b/epiphany-core/src/memory_graph/mod.rs @@ -5,6 +5,7 @@ pub mod freshness; pub mod ids; pub mod profiles; pub mod qdrant; +pub mod semantic_context; pub mod store; pub mod validation; @@ -42,9 +43,9 @@ pub use profiles::memory_graph_from_heartbeat_cognition; pub use qdrant::EpiphanyMemoryGraphEmbeddingCacheReport; pub use qdrant::EpiphanyMemoryGraphEmbeddingCacheRequest; pub use qdrant::EpiphanyMemoryGraphSemanticHit; -pub use qdrant::plan_memory_graph_context_cut_with_semantic_cache; pub use qdrant::query_memory_graph_semantic_cache; pub use qdrant::rebuild_memory_graph_embedding_cache; +pub use semantic_context::plan_memory_graph_context_cut_with_semantic_cache; pub use store::EpiphanyMemoryGraphEntry; pub use store::MEMORY_GRAPH_KEY; pub use store::MEMORY_GRAPH_SCHEMA_VERSION; diff --git a/epiphany-core/src/memory_graph/qdrant.rs b/epiphany-core/src/memory_graph/qdrant.rs index a4efd74b..166c0f24 100644 --- a/epiphany-core/src/memory_graph/qdrant.rs +++ b/epiphany-core/src/memory_graph/qdrant.rs @@ -1,11 +1,8 @@ -use super::EpiphanyMemoryContextPacket; use super::EpiphanyMemoryContextQuery; use super::EpiphanyMemoryEmbeddingManifest; use super::EpiphanyMemoryGraphSnapshot; use super::memory_graph_embedding_documents; use super::memory_graph_embedding_manifest; -use super::plan_memory_graph_context_cut; -use super::push_unique; use crate::semantic_cache::OllamaConfig; use crate::semantic_cache::OllamaEmbedder; use crate::semantic_cache::QdrantClient; @@ -153,73 +150,6 @@ fn rebuild_memory_graph_embedding_cache_with_config( }) } -pub fn plan_memory_graph_context_cut_with_semantic_cache( - snapshot: &EpiphanyMemoryGraphSnapshot, - query: &EpiphanyMemoryContextQuery, -) -> EpiphanyMemoryContextPacket { - let hits = match query_memory_graph_semantic_cache(snapshot, query) { - Ok(hits) => hits, - Err(err) => { - let mut packet = plan_memory_graph_context_cut(snapshot, query); - packet.warnings.push(format!( - "semantic cache unavailable; used typed graph traversal: {err}" - )); - return packet; - } - }; - plan_memory_graph_context_cut_from_semantic_hits(snapshot, query, hits) -} - -fn plan_memory_graph_context_cut_from_semantic_hits( - snapshot: &EpiphanyMemoryGraphSnapshot, - query: &EpiphanyMemoryContextQuery, - hits: Vec, -) -> EpiphanyMemoryContextPacket { - let mut warnings = Vec::new(); - if hits.is_empty() { - return plan_memory_graph_context_cut(snapshot, query); - } - - let mut seeded_query = query.clone(); - for hit in &hits { - match hit.document_kind.as_str() { - "node" => push_unique(&mut seeded_query.node_ids, hit.source_id.clone()), - "edge" => push_unique(&mut seeded_query.edge_ids, hit.source_id.clone()), - "summary" => { - if let Some(summary) = snapshot - .summaries - .iter() - .find(|summary| summary.id == hit.source_id) - { - for node_id in &summary.covers_node_ids { - push_unique(&mut seeded_query.node_ids, node_id.clone()); - } - for edge_id in &summary.covers_edge_ids { - push_unique(&mut seeded_query.edge_ids, edge_id.clone()); - } - } else { - warnings.push(format!( - "semantic cache hit missing summary {}", - hit.source_id - )); - } - } - other => warnings.push(format!( - "semantic cache hit {} had unknown document kind {other}", - hit.document_id - )), - } - } - - let mut packet = plan_memory_graph_context_cut(snapshot, &seeded_query); - packet.warnings.push(format!( - "semantic cache seeded {} typed graph document(s)", - hits.len() - )); - packet.warnings.extend(warnings); - packet -} - pub fn query_memory_graph_semantic_cache( snapshot: &EpiphanyMemoryGraphSnapshot, query: &EpiphanyMemoryContextQuery, @@ -456,25 +386,6 @@ mod tests { assert_eq!(hits[0].source_id, "node-auth-spine"); assert_eq!(hits[0].document_kind, "node"); - let packet = plan_memory_graph_context_cut_from_semantic_hits( - &snapshot, - &EpiphanyMemoryContextQuery { - id: "semantic-query-test".to_string(), - text: Some("who owns auth".to_string()), - budget: Some(3), - ..Default::default() - }, - hits, - ); - assert_eq!(packet.nodes.len(), 1); - assert_eq!(packet.nodes[0].id, "node-auth-spine"); - assert!( - packet - .warnings - .iter() - .any(|warning| warning.contains("semantic cache seeded 1")) - ); - Ok(()) } diff --git a/epiphany-core/src/memory_graph/semantic_context.rs b/epiphany-core/src/memory_graph/semantic_context.rs new file mode 100644 index 00000000..930670a3 --- /dev/null +++ b/epiphany-core/src/memory_graph/semantic_context.rs @@ -0,0 +1,149 @@ +use super::EpiphanyMemoryContextPacket; +use super::EpiphanyMemoryContextQuery; +use super::EpiphanyMemoryGraphSemanticHit; +use super::EpiphanyMemoryGraphSnapshot; +use super::plan_memory_graph_context_cut; +use super::push_unique; +use super::query_memory_graph_semantic_cache; + +pub fn plan_memory_graph_context_cut_with_semantic_cache( + snapshot: &EpiphanyMemoryGraphSnapshot, + query: &EpiphanyMemoryContextQuery, +) -> EpiphanyMemoryContextPacket { + let hits = match query_memory_graph_semantic_cache(snapshot, query) { + Ok(hits) => hits, + Err(err) => { + let mut packet = plan_memory_graph_context_cut(snapshot, query); + packet.warnings.push(format!( + "semantic cache unavailable; used typed graph traversal: {err}" + )); + return packet; + } + }; + plan_memory_graph_context_cut_from_semantic_hits(snapshot, query, hits) +} + +fn plan_memory_graph_context_cut_from_semantic_hits( + snapshot: &EpiphanyMemoryGraphSnapshot, + query: &EpiphanyMemoryContextQuery, + hits: Vec, +) -> EpiphanyMemoryContextPacket { + let mut warnings = Vec::new(); + if hits.is_empty() { + return plan_memory_graph_context_cut(snapshot, query); + } + + let mut seeded_query = query.clone(); + for hit in &hits { + match hit.document_kind.as_str() { + "node" => push_unique(&mut seeded_query.node_ids, hit.source_id.clone()), + "edge" => push_unique(&mut seeded_query.edge_ids, hit.source_id.clone()), + "summary" => { + if let Some(summary) = snapshot + .summaries + .iter() + .find(|summary| summary.id == hit.source_id) + { + for node_id in &summary.covers_node_ids { + push_unique(&mut seeded_query.node_ids, node_id.clone()); + } + for edge_id in &summary.covers_edge_ids { + push_unique(&mut seeded_query.edge_ids, edge_id.clone()); + } + } else { + warnings.push(format!( + "semantic cache hit missing summary {}", + hit.source_id + )); + } + } + other => warnings.push(format!( + "semantic cache hit {} had unknown document kind {other}", + hit.document_id + )), + } + } + + let mut packet = plan_memory_graph_context_cut(snapshot, &seeded_query); + packet.warnings.push(format!( + "semantic cache seeded {} typed graph document(s)", + hits.len() + )); + packet.warnings.extend(warnings); + packet +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::memory_graph::EpiphanyMemoryDomain; + use crate::memory_graph::EpiphanyMemoryLifecycle; + use crate::memory_graph::EpiphanyMemoryNode; + use crate::memory_graph::EpiphanyMemoryNodeKind; + use crate::memory_graph::EpiphanyMemoryProfile; + + #[test] + fn semantic_context_resolves_cache_hits_through_typed_graph() { + let snapshot = test_snapshot(); + let packet = plan_memory_graph_context_cut_from_semantic_hits( + &snapshot, + &EpiphanyMemoryContextQuery { + id: "semantic-context-test".to_string(), + text: Some("memory graph".to_string()), + budget: Some(3), + ..Default::default() + }, + vec![EpiphanyMemoryGraphSemanticHit { + document_id: "memembed-node-test".to_string(), + source_id: "memnode-memory-graph".to_string(), + document_kind: "node".to_string(), + score: 0.91, + }], + ); + + assert!( + packet + .nodes + .iter() + .any(|node| node.id == "memnode-memory-graph") + ); + assert!( + packet + .warnings + .iter() + .any(|warning| warning.contains("semantic cache seeded 1")) + ); + } + + fn test_snapshot() -> EpiphanyMemoryGraphSnapshot { + EpiphanyMemoryGraphSnapshot { + graph_id: "semantic-context-graph".to_string(), + domains: vec![EpiphanyMemoryDomain { + id: "repo".to_string(), + profile: EpiphanyMemoryProfile::RepoArchitecture, + title: "Repo".to_string(), + description: None, + lifecycle: EpiphanyMemoryLifecycle::Accepted, + }], + nodes: vec![EpiphanyMemoryNode { + id: "memnode-memory-graph".to_string(), + domain_id: "repo".to_string(), + profile: EpiphanyMemoryProfile::RepoArchitecture, + kind: EpiphanyMemoryNodeKind::Module, + title: "Memory graph".to_string(), + claim: "Typed graph context owns the real packet.".to_string(), + question: String::new(), + tension: String::new(), + action_implication: String::new(), + anchors: Vec::new(), + source_hashes: vec!["source-a".to_string()], + lifecycle: EpiphanyMemoryLifecycle::Accepted, + salience: 80, + confidence: 90, + created_at: None, + updated_at: None, + }], + ..Default::default() + } + } +}