diff --git a/epiphany-core/src/current_work.rs b/epiphany-core/src/current_work.rs index 84b63a6a..df11a450 100644 --- a/epiphany-core/src/current_work.rs +++ b/epiphany-core/src/current_work.rs @@ -4108,24 +4108,43 @@ mod tests { let hands_provenance = accepted_cache .get_envelope::(&hands_commit.receipt_id)? .expect("typed Hands provenance"); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(2)); + let (modeling_outcome, verification_commit) = std::thread::scope(|scope| { + let modeling_barrier = barrier.clone(); + let modeling_store = store.clone(); + let modeling = scope.spawn(move || { + modeling_barrier.wait(); + crate::commit_typed_organ_mind_mutation( + &modeling_store, + "Modeling", + hands_provenance, + "Modeling.verification_concurrent_fixture", + concurrent_plan.strong_reads, + concurrent_plan.writes, + "2026-08-17T00:00:15.100Z", + ) + }); + let verification_barrier = barrier.clone(); + let verification_store = store.clone(); + let verification_job_id = verification_job_id.clone(); + let verification = scope.spawn(move || { + verification_barrier.wait(); + accept_frontier_verification_result( + &verification_store, + &verification_job_id, + "2026-08-17T00:00:16Z", + ) + }); + ( + modeling.join().expect("concurrent Modeling commit"), + verification.join().expect("concurrent Verification commit"), + ) + }); assert!(matches!( - crate::commit_typed_organ_mind_mutation( - &store, - "Modeling", - hands_provenance, - "Modeling.verification_concurrent_fixture", - concurrent_plan.strong_reads, - concurrent_plan.writes, - "2026-08-17T00:00:15.100Z", - )?, + modeling_outcome?, crate::EpiphanyMindCommitOutcome::Committed(_) )); - - let verification_commit = accept_frontier_verification_result( - &store, - &verification_job_id, - "2026-08-17T00:00:16Z", - )?; + let verification_commit = verification_commit?; assert_eq!(verification_commit.invariant_owner, "Soul.verification"); assert!(project_current_work(&store)?.verification.is_none()); let replayed_verification_commit = accept_frontier_verification_result( diff --git a/epiphany-core/src/reasoning_context.rs b/epiphany-core/src/reasoning_context.rs index b482eed6..b0823eb5 100644 --- a/epiphany-core/src/reasoning_context.rs +++ b/epiphany-core/src/reasoning_context.rs @@ -822,7 +822,7 @@ fn validate_context_store_ownership( Ok(()) } -pub fn commit_mind_mutation( +pub(crate) fn commit_mind_mutation( store_path: &Path, decision_context_id: &str, invariant_owner: &str, @@ -886,7 +886,7 @@ pub(crate) fn commit_mind_mutation_with_derived_companions( ) } -pub fn commit_operator_mind_mutation( +pub(crate) fn commit_operator_mind_mutation( store_path: &Path, provenance: CultCacheEnvelope, invariant_owner: &str, @@ -933,7 +933,7 @@ pub(crate) fn commit_operator_mind_mutation_with_derived_companions( ) } -pub fn commit_typed_organ_mind_mutation( +pub(crate) fn commit_typed_organ_mind_mutation( store_path: &Path, organ: &str, provenance: CultCacheEnvelope, diff --git a/epiphany-core/src/repo_model_documents.rs b/epiphany-core/src/repo_model_documents.rs index 46bfbff7..69094d39 100644 --- a/epiphany-core/src/repo_model_documents.rs +++ b/epiphany-core/src/repo_model_documents.rs @@ -2211,6 +2211,222 @@ mod tests { Ok(()) } + #[test] + fn concurrent_distinct_nodes_merge_same_identity_conflicts_and_restart_replays() -> Result<()> { + let temp = tempfile::tempdir()?; + let store = temp.path().join("repo-model-concurrency.cc"); + initialize_runtime_spine( + &store, + RuntimeSpineInitOptions { + runtime_id: "repo-model-concurrency".into(), + display_name: "RepoModel concurrency".into(), + created_at: "2026-08-18T00:00:00Z".into(), + }, + )?; + let body = bind_test_body(&store, "swarm-concurrency", "workspace-concurrency")?; + let domain = EpiphanyMemoryDomain { + id: "domain-concurrency".into(), + profile: EpiphanyMemoryProfile::RepoArchitecture, + title: "Concurrent domain".into(), + lifecycle: EpiphanyMemoryLifecycle::Accepted, + ..Default::default() + }; + initialize_keyed_repo_model( + &store, + &EpiphanyRepoModelSeed::new( + "seed-concurrency", + "graph-concurrency", + "swarm-concurrency", + "workspace-concurrency", + body.body_binding_sha256.clone(), + EpiphanyRepoModelSeedDocuments { + domains: vec![domain.clone()], + nodes: Vec::new(), + edges: Vec::new(), + summaries: Vec::new(), + frontier: Vec::new(), + lifecycle_receipts: Vec::new(), + }, + )?, + "2026-08-18T00:00:01Z", + )?; + let node = |id: &str, claim: &str| EpiphanyMemoryNode { + id: id.into(), + domain_id: domain.id.clone(), + profile: EpiphanyMemoryProfile::RepoArchitecture, + kind: EpiphanyMemoryNodeKind::Module, + title: id.into(), + claim: claim.into(), + question: "Can this identity commit independently?".into(), + action_implication: "Use exact-envelope CAS".into(), + source_hashes: vec!["anchor:missing".into()], + lifecycle: EpiphanyMemoryLifecycle::Accepted, + ..Default::default() + }; + let proposals = [ + make_proposal( + "proposal-node-left", + &body, + vec![EpiphanyRepoModelMutationOperation::PutNode { + node: node("node-left", "Left is independently owned"), + }], + )?, + make_proposal( + "proposal-node-right", + &body, + vec![EpiphanyRepoModelMutationOperation::PutNode { + node: node("node-right", "Right is independently owned"), + }], + )?, + ]; + let cache = runtime_spine_cache(&store)?; + let prepared = proposals + .iter() + .map(|proposal| { + Ok(( + plan_repo_model_mutation(&store, proposal)?, + cache.prepare_entry(&proposal.proposal_id, proposal)?.0, + )) + }) + .collect::>>()?; + let barrier = std::sync::Arc::new(std::sync::Barrier::new(2)); + let outcomes = std::thread::scope(|scope| { + prepared + .into_iter() + .enumerate() + .map(|(index, (plan, provenance))| { + let barrier = barrier.clone(); + let store = store.clone(); + scope.spawn(move || { + barrier.wait(); + crate::commit_operator_mind_mutation( + &store, + provenance, + "Modeling.repo_model_mutation", + plan.strong_reads, + plan.writes, + if index == 0 { + "2026-08-18T00:00:02Z" + } else { + "2026-08-18T00:00:03Z" + }, + ) + }) + }) + .collect::>() + .into_iter() + .map(|handle| handle.join().expect("concurrent node writer")) + .collect::>>() + })?; + assert_eq!( + outcomes + .iter() + .filter(|outcome| matches!(outcome, crate::EpiphanyMindCommitOutcome::Committed(_))) + .count(), + 2 + ); + let reopened = assemble_repo_model_view(&store)?; + assert_eq!( + reopened + .nodes + .iter() + .map(|node| node.id.as_str()) + .collect::>(), + ["node-left", "node-right"] + ); + + let competing = [ + make_proposal( + "proposal-collision-left", + &body, + vec![EpiphanyRepoModelMutationOperation::PutNode { + node: node("node-collision", "Left claim"), + }], + )?, + make_proposal( + "proposal-collision-right", + &body, + vec![EpiphanyRepoModelMutationOperation::PutNode { + node: node("node-collision", "Right claim"), + }], + )?, + ]; + let prepared = competing + .iter() + .map(|proposal| { + Ok(( + plan_repo_model_mutation(&store, proposal)?, + cache.prepare_entry(&proposal.proposal_id, proposal)?.0, + )) + }) + .collect::>>()?; + let replay_inputs = prepared.clone(); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(2)); + let outcomes = std::thread::scope(|scope| { + prepared + .into_iter() + .enumerate() + .map(|(index, (plan, provenance))| { + let barrier = barrier.clone(); + let store = store.clone(); + scope.spawn(move || { + barrier.wait(); + crate::commit_operator_mind_mutation( + &store, + provenance, + "Modeling.repo_model_mutation", + plan.strong_reads, + plan.writes, + if index == 0 { + "2026-08-18T00:00:04Z" + } else { + "2026-08-18T00:00:05Z" + }, + ) + }) + }) + .collect::>() + .into_iter() + .map(|handle| handle.join().expect("competing node writer")) + .collect::>>() + })?; + let winner = outcomes + .iter() + .position(|outcome| matches!(outcome, crate::EpiphanyMindCommitOutcome::Committed(_))) + .expect("one same-identity writer wins"); + assert_eq!( + outcomes + .iter() + .filter(|outcome| matches!(outcome, crate::EpiphanyMindCommitOutcome::Conflict { .. })) + .count(), + 1 + ); + let original_receipt = match &outcomes[winner] { + crate::EpiphanyMindCommitOutcome::Committed(receipt) => receipt.clone(), + _ => unreachable!(), + }; + let (plan, provenance) = replay_inputs[winner].clone(); + assert_eq!( + crate::commit_operator_mind_mutation( + &store, + provenance, + "Modeling.repo_model_mutation", + plan.strong_reads, + plan.writes, + if winner == 0 { + "2026-08-18T00:00:04Z" + } else { + "2026-08-18T00:00:05Z" + }, + )?, + crate::EpiphanyMindCommitOutcome::Committed(original_receipt) + ); + let restarted = assemble_repo_model_view(&store)?; + assert_eq!(restarted.nodes.len(), 3); + assert!(restarted.nodes.iter().any(|node| node.id == "node-collision")); + Ok(()) + } + #[test] fn modeling_operations_own_local_atlas_offer_and_claim_lifecycles() -> Result<()> { let temp = tempfile::tempdir()?;