Apply memory patch candidates on modeling acceptance

This commit is contained in:
Meta Vens 2026-05-18 04:12:46 +01:00
commit b48725159f
8 changed files with 202 additions and 7 deletions

View file

@ -10,6 +10,31 @@ pub fn review_memory_patch_candidate(
snapshot: &EpiphanyMemoryGraphSnapshot,
candidate: &EpiphanyMemoryPatchCandidate,
) -> EpiphanyMemoryPatchReview {
let accepted_receipt_id = accepted_receipt_id(candidate);
if candidate_already_applied(snapshot, &accepted_receipt_id) {
return EpiphanyMemoryPatchReview {
candidate_id: candidate.id.clone(),
status: EpiphanyMemoryPatchReviewStatus::Accepted,
accepted_domain_ids: candidate
.proposed_domains
.iter()
.map(|domain| domain.id.clone())
.collect(),
accepted_node_ids: candidate
.proposed_nodes
.iter()
.map(|node| node.id.clone())
.collect(),
accepted_edge_ids: candidate
.proposed_edges
.iter()
.map(|edge| edge.id.clone())
.collect(),
errors: Vec::new(),
warnings: vec!["candidate was already applied".to_string()],
};
}
let mut errors = Vec::new();
let mut warnings = Vec::new();
@ -168,6 +193,9 @@ pub fn apply_memory_patch_candidate(
if review.status != EpiphanyMemoryPatchReviewStatus::Accepted {
return (snapshot.clone(), review);
}
if candidate_already_applied(snapshot, &accepted_receipt_id(candidate)) {
return (snapshot.clone(), review);
}
let mut accepted = snapshot.clone();
accepted
@ -182,7 +210,7 @@ pub fn apply_memory_patch_candidate(
accepted
.lifecycle_receipts
.push(EpiphanyMemoryLifecycleReceipt {
id: format!("memreceipt-{}", candidate.id),
id: accepted_receipt_id(candidate),
operation: "memoryPatchCandidate.accept".to_string(),
status: "accepted".to_string(),
node_ids: review.accepted_node_ids.clone(),
@ -193,3 +221,15 @@ pub fn apply_memory_patch_candidate(
(accepted, review)
}
fn accepted_receipt_id(candidate: &EpiphanyMemoryPatchCandidate) -> String {
format!("memreceipt-{}", candidate.id)
}
fn candidate_already_applied(snapshot: &EpiphanyMemoryGraphSnapshot, receipt_id: &str) -> bool {
snapshot.lifecycle_receipts.iter().any(|receipt| {
receipt.id == receipt_id
&& receipt.operation == "memoryPatchCandidate.accept"
&& receipt.status == "accepted"
})
}

View file

@ -498,6 +498,57 @@ fn memory_patch_candidate_review_rejects_duplicate_or_orphan_growth() {
);
}
#[test]
fn memory_patch_candidate_apply_is_idempotent_after_receipt() {
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("idempotent"),
);
let candidate = EpiphanyMemoryPatchCandidate {
id: "candidate-idempotent".to_string(),
profile: EpiphanyMemoryProfile::RepoArchitecture,
status: "proposed".to_string(),
proposed_nodes: vec![EpiphanyMemoryNode {
id: node_id,
domain_id,
profile: EpiphanyMemoryProfile::RepoArchitecture,
kind: EpiphanyMemoryNodeKind::RuntimeContract,
title: "Idempotent candidate".to_string(),
claim: "Accepted candidates can be retried without duplicate rejection.".to_string(),
question: "Did the previous graph write complete?".to_string(),
tension: String::new(),
action_implication: "Treat receipt-backed retries as accepted.".to_string(),
source_hashes: vec!["anchor:missing".to_string()],
lifecycle: EpiphanyMemoryLifecycle::Proposed,
..Default::default()
}],
..Default::default()
};
let (accepted, first_review) = apply_memory_patch_candidate(&snapshot, &candidate, None);
let (retried, second_review) = apply_memory_patch_candidate(&accepted, &candidate, None);
assert_eq!(
first_review.status,
EpiphanyMemoryPatchReviewStatus::Accepted
);
assert_eq!(
second_review.status,
EpiphanyMemoryPatchReviewStatus::Accepted
);
assert_eq!(retried, accepted);
assert!(
second_review
.warnings
.iter()
.any(|warning| warning.contains("already applied"))
);
}
#[test]
fn memory_patch_candidate_deserializes_camel_case_edge_json() {
let candidate: EpiphanyMemoryPatchCandidate = serde_json::from_str(