Prove concurrent keyed Mind commits

This commit is contained in:
Meta Vens 2026-08-18 06:30:54 +02:00
commit 26b6a5bfe4
3 changed files with 253 additions and 18 deletions

View file

@ -4108,24 +4108,43 @@ mod tests {
let hands_provenance = accepted_cache
.get_envelope::<crate::HandsCommitReceipt>(&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(

View file

@ -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,

View file

@ -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::<Result<Vec<_>>>()?;
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::<Vec<_>>()
.into_iter()
.map(|handle| handle.join().expect("concurrent node writer"))
.collect::<Result<Vec<_>>>()
})?;
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::<Vec<_>>(),
["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::<Result<Vec<_>>>()?;
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::<Vec<_>>()
.into_iter()
.map(|handle| handle.join().expect("competing node writer"))
.collect::<Result<Vec<_>>>()
})?;
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()?;