From f1a44493004128c95177b4e0be8eb9b6515b5178 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 9 Oct 2026 16:30:14 +0200 Subject: [PATCH] Runs: reinstating a run goes through claim exclusivity; a run claims an item once Withdrawing a run's closure now asks docs.claim_holder, the same owner the opening rule uses, and refuses AlreadyClaimed when another live run holds a claim. Duplicate claims within a run are refused at validate. Tests pin the reinstatement sequence, second-claim holders, and same-key different-content IdentityCollision. Co-Authored-By: Claude Opus 5.5 --- crates/eureka-pipeline/src/lib.rs | 15 +++++++ crates/huginn-mind/src/admission.rs | 70 +++++++++++++++++++++++++++-- crates/huginn-mind/src/docs.rs | 14 ++++++ 3 files changed, 95 insertions(+), 4 deletions(-) diff --git a/crates/eureka-pipeline/src/lib.rs b/crates/eureka-pipeline/src/lib.rs index 72ae409..dd8763d 100644 --- a/crates/eureka-pipeline/src/lib.rs +++ b/crates/eureka-pipeline/src/lib.rs @@ -481,6 +481,9 @@ fn run_is_well_formed(run: &PipelineRun, at: &str) -> Result<(), PipelineRefusal if !matches!(claim.kind, PipelineKind::CutSpec | PipelineKind::Finding | PipelineKind::FollowUp) { return Err(format_error(&format!("{at}.claims[{index}].kind"), claim.kind.name())); } + if run.claims[..index].contains(claim) { + return Err(format_error(&format!("{at}.claims[{index}]"), &claim.id.0)); + } } let (whole, fraction) = run.budget_usd.0.split_once('.').map_or((run.budget_usd.0.as_str(), None), |(whole, fraction)| (whole, Some(fraction))); let digits = |text: &str| !text.is_empty() && text.bytes().all(|byte| byte.is_ascii_digit()); @@ -1525,6 +1528,18 @@ mod tests { } } + /// A run claims an item once: a repeated claim is refused at validate. + #[test] + fn a_run_cannot_claim_one_item_twice() { + let claim = PipelineRef { kind: PipelineKind::FollowUp, id: id("follow_up", "F1") }; + let other = PipelineRef { kind: PipelineKind::FollowUp, id: id("follow_up", "F2") }; + let mut run = run_sample(); + run.claims = vec![claim.clone(), other.clone()]; + assert_eq!(PipelineDocument::Run(run.clone()).validate(), Ok(())); + run.claims = vec![claim.clone(), other, claim]; + assert!(matches!(PipelineDocument::Run(run).validate(), Err(PipelineRefusal::InvalidFormat { field, .. }) if field == "run.claims[2]")); + } + /// The budget has one spelling per amount. #[test] fn a_runs_budget_is_a_canonical_decimal() { diff --git a/crates/huginn-mind/src/admission.rs b/crates/huginn-mind/src/admission.rs index e4d9c74..1f6c4a1 100644 --- a/crates/huginn-mind/src/admission.rs +++ b/crates/huginn-mind/src/admission.rs @@ -529,10 +529,7 @@ fn check(docs: &Docs, staged: &Staged, mind: &Slug) -> Result<(), MindRefusal> { if !replay && !docs.in_force(claim.kind, &claim.id.0) { return Err(MindRefusal::CitesResolvedDocument { kind: claim.kind, id: claim.id.0.clone() }); } - let holder = docs.of_kind(K::Run).find_map(|(other_key, document)| match document { - D::Run(other) if other_key != key && other.claims.contains(claim) && docs.in_force(K::Run, other_key) => Some(other_key), - _ => None, - }); + let holder = docs.claim_holder(key, claim); if let Some(holder) = holder { return Err(MindRefusal::AlreadyClaimed { item: claim.id.0.clone(), run: holder.into() }); } @@ -679,6 +676,20 @@ fn resolution_rule(docs: &Docs, resolution: &PipelineResolution) -> Result<(), M later: later.into(), }); } + // Withdrawing a run's closure puts the run back in force, and a run in + // force holds its claims: refuse while another run in force holds one. + if matches!(resolution.outcome, ResolutionOutcome::Withdrawn { .. }) + && subject_kind == K::Resolution + && let Some(D::Resolution(reinstating)) = docs.find(K::Resolution, subject_id) + && reinstating.subject.kind == K::Run + && let Some(D::Run(run)) = docs.find(K::Run, &reinstating.subject.id.0) + { + for claim in &run.claims { + if let Some(holder) = docs.claim_holder(&reinstating.subject.id.0, claim) { + return Err(MindRefusal::AlreadyClaimed { item: claim.id.0.clone(), run: holder.into() }); + } + } + } for (_, referent) in outcome_citations(&resolution.outcome) { if !docs.in_force(referent.kind, &referent.id.0) { return Err(MindRefusal::CitesResolvedDocument { kind: referent.kind, id: referent.id.0 }); @@ -2032,6 +2043,57 @@ mod tests { ); } + /// Withdrawing a run's Recorded resolution puts the run back in force, so + /// it goes through the same exclusivity as an opening: refused while + /// another live run holds one of its claims, allowed when none does. + #[test] + fn reinstating_a_run_over_a_live_holder_is_already_claimed() { + let closure = r(K::Resolution, &format!("{INSTANCE}:resolution:run.a.n1")); + let mut mind = with_specs(); + committed(admit(&mut mind, vec![run("a", &[spec_ref("1")])])); + committed(admit(&mut mind, vec![recorded("a")])); + committed(admit(&mut mind, vec![run("c", &[spec_ref("1")])])); + assert_eq!( + refusal(admit(&mut mind, vec![resolution(closure.clone(), withdrawn())])), + MindRefusal::AlreadyClaimed { item: spec_ref("1").id.0, run: run_key("c") } + ); + + let mut free = with_specs(); + committed(admit(&mut free, vec![run("a", &[spec_ref("1")])])); + committed(admit(&mut free, vec![recorded("a")])); + committed(admit(&mut free, vec![resolution(closure, withdrawn())])); + } + + /// A holder is found by any of its claims, not only the first. + #[test] + fn a_holder_is_found_by_its_second_claim() { + let mut mind = with_specs(); + committed(admit(&mut mind, vec![run("a", &[spec_ref("2"), spec_ref("1")])])); + assert_eq!( + refusal(admit(&mut mind, vec![run("b", &[spec_ref("1")])])), + MindRefusal::AlreadyClaimed { item: spec_ref("1").id.0, run: run_key("a") } + ); + } + + /// A key already holding a run is taken: the same key with different + /// content is an IdentityCollision, never a replay; only byte-identical + /// content answers AlreadyAdmitted. + #[test] + fn a_run_key_reused_with_different_content_is_an_identity_collision() { + let mut mind = with_specs(); + committed(admit(&mut mind, vec![run("a", &[spec_ref("1")])])); + let D::Run(mut changed) = run("a", &[spec_ref("1")]) else { panic!() }; + changed.budget_usd = "6".into(); + assert_eq!( + refusal(admit(&mut mind, vec![D::Run(changed)])), + MindRefusal::IdentityCollision { kind: K::Run, id: run_key("a") } + ); + assert_eq!( + refusal(admit(&mut mind, vec![run("a", &[spec_ref("2")])])), + MindRefusal::IdentityCollision { kind: K::Run, id: run_key("a") } + ); + } + /// A5 for runs: a run of another instance is a ForeignInstance. #[test] fn a_run_of_another_mind_is_a_foreign_instance() { diff --git a/crates/huginn-mind/src/docs.rs b/crates/huginn-mind/src/docs.rs index ad94aa7..a3bae9a 100644 --- a/crates/huginn-mind/src/docs.rs +++ b/crates/huginn-mind/src/docs.rs @@ -215,6 +215,20 @@ impl Docs { self.of_kind(kind).find(|(key, other)| later_than(base, other) && self.in_force(kind, key)).map(|(key, _)| key) } + /// The in-force run, other than `run_key`, that holds `claim`: the one + /// consumption fact run-is-the-grant rests on, asked by a run's opening + /// and by the withdrawal that would put a closed run back in force. + pub(crate) fn claim_holder(&self, run_key: &str, claim: &PipelineRef) -> Option<&str> { + self.of_kind(PipelineKind::Run).find_map(|(other_key, document)| match document { + PipelineDocument::Run(other) + if other_key != run_key && other.claims.contains(claim) && self.in_force(PipelineKind::Run, other_key) => + { + Some(other_key) + } + _ => None, + }) + } + /// The stewardship of `(mind, repo)` a hand-off acts on: the one whose /// withdrawal already carries `hand_off_key`, when image or batch holds /// that withdrawal, so a replay derives the same withdrawal again and A9