Add memory graph patch review law

This commit is contained in:
Meta Vens 2026-05-18 03:47:49 +01:00
commit cef55e34f8
9 changed files with 334 additions and 5 deletions

View file

@ -101,9 +101,12 @@ pub use memory_graph::EpiphanyMemoryGraphValidationError;
pub use memory_graph::EpiphanyMemoryLifecycleReceipt;
pub use memory_graph::EpiphanyMemoryNodeKind;
pub use memory_graph::EpiphanyMemoryPatchCandidate;
pub use memory_graph::EpiphanyMemoryPatchReview;
pub use memory_graph::EpiphanyMemoryPatchReviewStatus;
pub use memory_graph::MEMORY_GRAPH_KEY;
pub use memory_graph::MEMORY_GRAPH_SCHEMA_VERSION;
pub use memory_graph::MEMORY_GRAPH_TYPE;
pub use memory_graph::apply_memory_patch_candidate;
pub use memory_graph::compose_memory_graph_snapshots;
pub use memory_graph::derive_memory_graph_freshness;
pub use memory_graph::lifecycle_allowed_for_profile;
@ -122,6 +125,7 @@ pub use memory_graph::plan_memory_graph_context_cut;
pub use memory_graph::plan_memory_graph_context_cut_with_semantic_cache;
pub use memory_graph::query_memory_graph_semantic_cache;
pub use memory_graph::rebuild_memory_graph_embedding_cache;
pub use memory_graph::review_memory_patch_candidate;
pub use memory_graph::validate_memory_graph_entry;
pub use memory_graph::validate_memory_graph_snapshot;
pub use memory_graph::write_memory_graph_entry;

View file

@ -3,6 +3,7 @@ pub mod context_cut;
pub mod embedding;
pub mod freshness;
pub mod ids;
pub mod patch;
pub mod profiles;
pub mod qdrant;
pub mod semantic_context;
@ -24,6 +25,8 @@ pub use epiphany_state_model::EpiphanyMemoryLifecycleReceipt;
pub use epiphany_state_model::EpiphanyMemoryNode;
pub use epiphany_state_model::EpiphanyMemoryNodeKind;
pub use epiphany_state_model::EpiphanyMemoryPatchCandidate;
pub use epiphany_state_model::EpiphanyMemoryPatchReview;
pub use epiphany_state_model::EpiphanyMemoryPatchReviewStatus;
pub use epiphany_state_model::EpiphanyMemoryProfile;
pub use epiphany_state_model::EpiphanyMemorySummary;
@ -37,6 +40,8 @@ pub use freshness::derive_memory_graph_freshness;
pub use ids::memory_graph_domain_id;
pub use ids::memory_graph_edge_id;
pub use ids::memory_graph_node_id;
pub use patch::apply_memory_patch_candidate;
pub use patch::review_memory_patch_candidate;
pub use profiles::memory_graph_from_agent_memories;
pub use profiles::memory_graph_from_epiphany_graphs;
pub use profiles::memory_graph_from_heartbeat_cognition;

View file

@ -0,0 +1,195 @@
use super::EpiphanyMemoryGraphSnapshot;
use super::EpiphanyMemoryLifecycleReceipt;
use super::EpiphanyMemoryPatchCandidate;
use super::EpiphanyMemoryPatchReview;
use super::EpiphanyMemoryPatchReviewStatus;
use super::validate_memory_graph_snapshot;
use std::collections::HashSet;
pub fn review_memory_patch_candidate(
snapshot: &EpiphanyMemoryGraphSnapshot,
candidate: &EpiphanyMemoryPatchCandidate,
) -> EpiphanyMemoryPatchReview {
let mut errors = Vec::new();
let mut warnings = Vec::new();
if candidate.id.trim().is_empty() {
errors.push("candidate id is required".to_string());
}
if candidate.status.trim().is_empty() {
errors.push("candidate status is required".to_string());
} else if candidate.status != "proposed" {
warnings.push(format!(
"candidate status {:?} is reviewable but not the normal proposed status",
candidate.status
));
}
if candidate.proposed_domains.is_empty()
&& candidate.proposed_nodes.is_empty()
&& candidate.proposed_edges.is_empty()
{
errors.push("candidate must propose at least one domain, node, or edge".to_string());
}
let existing_domain_ids = snapshot
.domains
.iter()
.map(|domain| domain.id.as_str())
.collect::<HashSet<_>>();
let existing_node_ids = snapshot
.nodes
.iter()
.map(|node| node.id.as_str())
.collect::<HashSet<_>>();
let existing_edge_ids = snapshot
.edges
.iter()
.map(|edge| edge.id.as_str())
.collect::<HashSet<_>>();
let mut proposed_domain_ids = HashSet::new();
for domain in &candidate.proposed_domains {
if domain.profile != candidate.profile {
errors.push(format!(
"proposed domain {} profile does not match candidate profile",
domain.id
));
}
if existing_domain_ids.contains(domain.id.as_str()) {
errors.push(format!(
"proposed domain {} already exists; revision candidates need a separate reviewer",
domain.id
));
}
if !proposed_domain_ids.insert(domain.id.as_str()) {
errors.push(format!("candidate repeats proposed domain {}", domain.id));
}
}
let mut proposed_node_ids = HashSet::new();
for node in &candidate.proposed_nodes {
if node.profile != candidate.profile {
errors.push(format!(
"proposed node {} profile does not match candidate profile",
node.id
));
}
if existing_node_ids.contains(node.id.as_str()) {
errors.push(format!(
"proposed node {} already exists; revision candidates need a separate reviewer",
node.id
));
}
if !proposed_node_ids.insert(node.id.as_str()) {
errors.push(format!("candidate repeats proposed node {}", node.id));
}
}
let mut proposed_edge_ids = HashSet::new();
for edge in &candidate.proposed_edges {
if edge.profile != candidate.profile {
errors.push(format!(
"proposed edge {} profile does not match candidate profile",
edge.id
));
}
if existing_edge_ids.contains(edge.id.as_str()) {
errors.push(format!(
"proposed edge {} already exists; revision candidates need a separate reviewer",
edge.id
));
}
if !proposed_edge_ids.insert(edge.id.as_str()) {
errors.push(format!("candidate repeats proposed edge {}", edge.id));
}
}
let mut projected = snapshot.clone();
projected
.domains
.extend(candidate.proposed_domains.iter().cloned());
projected
.nodes
.extend(candidate.proposed_nodes.iter().cloned());
projected
.edges
.extend(candidate.proposed_edges.iter().cloned());
errors.extend(
validate_memory_graph_snapshot(&projected)
.into_iter()
.map(|error| format!("{}: {}", error.path, error.message)),
);
EpiphanyMemoryPatchReview {
candidate_id: candidate.id.clone(),
status: if errors.is_empty() {
EpiphanyMemoryPatchReviewStatus::Accepted
} else {
EpiphanyMemoryPatchReviewStatus::Rejected
},
accepted_domain_ids: if errors.is_empty() {
candidate
.proposed_domains
.iter()
.map(|domain| domain.id.clone())
.collect()
} else {
Vec::new()
},
accepted_node_ids: if errors.is_empty() {
candidate
.proposed_nodes
.iter()
.map(|node| node.id.clone())
.collect()
} else {
Vec::new()
},
accepted_edge_ids: if errors.is_empty() {
candidate
.proposed_edges
.iter()
.map(|edge| edge.id.clone())
.collect()
} else {
Vec::new()
},
errors,
warnings,
}
}
pub fn apply_memory_patch_candidate(
snapshot: &EpiphanyMemoryGraphSnapshot,
candidate: &EpiphanyMemoryPatchCandidate,
accepted_at: Option<String>,
) -> (EpiphanyMemoryGraphSnapshot, EpiphanyMemoryPatchReview) {
let review = review_memory_patch_candidate(snapshot, candidate);
if review.status != EpiphanyMemoryPatchReviewStatus::Accepted {
return (snapshot.clone(), review);
}
let mut accepted = snapshot.clone();
accepted
.domains
.extend(candidate.proposed_domains.iter().cloned());
accepted
.nodes
.extend(candidate.proposed_nodes.iter().cloned());
accepted
.edges
.extend(candidate.proposed_edges.iter().cloned());
accepted
.lifecycle_receipts
.push(EpiphanyMemoryLifecycleReceipt {
id: format!("memreceipt-{}", candidate.id),
operation: "memoryPatchCandidate.accept".to_string(),
status: "accepted".to_string(),
node_ids: review.accepted_node_ids.clone(),
edge_ids: review.accepted_edge_ids.clone(),
reason: (!candidate.reasons.is_empty()).then(|| candidate.reasons.join(" ")),
created_at: accepted_at,
});
(accepted, review)
}

View file

@ -409,3 +409,91 @@ fn memory_graph_embedding_documents_are_stable_typed_cache_cargo() {
assert!(manifest.source_hashes.contains(&"hash-a".to_string()));
assert!(manifest.source_hashes.contains(&"hash-b".to_string()));
}
#[test]
fn memory_patch_candidate_review_accepts_append_only_graph_growth() {
let snapshot = fixture_snapshot();
let domain_id = snapshot.domains[0].id.clone();
let node_id = memory_graph_node_id(&domain_id, "module", "src/runtime_spine.rs", Some("role"));
let candidate = EpiphanyMemoryPatchCandidate {
id: "candidate-runtime-spine".to_string(),
profile: EpiphanyMemoryProfile::RepoArchitecture,
status: "proposed".to_string(),
proposed_nodes: vec![EpiphanyMemoryNode {
id: node_id.clone(),
domain_id,
profile: EpiphanyMemoryProfile::RepoArchitecture,
kind: EpiphanyMemoryNodeKind::RuntimeContract,
title: "Runtime spine role results".to_string(),
claim: "Runtime role results carry typed memory patch candidates.".to_string(),
question: "When should candidate review apply them?".to_string(),
tension: String::new(),
action_implication: "Review candidates against the typed graph before mutation."
.to_string(),
source_hashes: vec!["anchor:missing".to_string()],
lifecycle: EpiphanyMemoryLifecycle::Proposed,
confidence: 72,
..Default::default()
}],
reasons: vec!["Typed result cargo needs a graph-owned review law.".to_string()],
..Default::default()
};
let (accepted, review) = apply_memory_patch_candidate(
&snapshot,
&candidate,
Some("2026-05-18T00:00:00Z".to_string()),
);
assert_eq!(review.status, EpiphanyMemoryPatchReviewStatus::Accepted);
assert_eq!(review.accepted_node_ids, vec![node_id.clone()]);
assert!(accepted.nodes.iter().any(|node| node.id == node_id));
assert!(
accepted
.lifecycle_receipts
.iter()
.any(|receipt| receipt.operation == "memoryPatchCandidate.accept")
);
}
#[test]
fn memory_patch_candidate_review_rejects_duplicate_or_orphan_growth() {
let snapshot = fixture_snapshot();
let candidate = EpiphanyMemoryPatchCandidate {
id: "candidate-bad".to_string(),
profile: EpiphanyMemoryProfile::RepoArchitecture,
status: "proposed".to_string(),
proposed_nodes: vec![EpiphanyMemoryNode {
id: snapshot.nodes[0].id.clone(),
domain_id: "missing-domain".to_string(),
profile: EpiphanyMemoryProfile::RepoArchitecture,
kind: EpiphanyMemoryNodeKind::Module,
title: "Duplicate".to_string(),
claim: "Duplicate nodes must not be laundered through candidate review.".to_string(),
question: "Where should revisions live?".to_string(),
tension: String::new(),
action_implication: "Reject duplicate append candidates.".to_string(),
source_hashes: vec!["anchor:missing".to_string()],
lifecycle: EpiphanyMemoryLifecycle::Proposed,
..Default::default()
}],
..Default::default()
};
let (unchanged, review) = apply_memory_patch_candidate(&snapshot, &candidate, None);
assert_eq!(unchanged, snapshot);
assert_eq!(review.status, EpiphanyMemoryPatchReviewStatus::Rejected);
assert!(
review
.errors
.iter()
.any(|error| error.contains("already exists"))
);
assert!(
review
.errors
.iter()
.any(|error| error.contains("missing domain"))
);
}