Stewardship: rival_in_force is the one owner of one-in-force; admission and reinstatement ask it

AlreadyStewarded and the withdrawal-over-later check each decided it with their own comparison, and disagreed when a later record was reinstated beside an earlier one. later_than's stewardship arm is gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-22 21:58:06 +02:00
commit e1332fbd30
2 changed files with 54 additions and 14 deletions

View file

@ -598,7 +598,7 @@ fn check(docs: &Docs, staged: &Staged, mind: &Slug) -> Result<(), MindRefusal> {
actual: stewardship.sequence,
});
}
if docs.stewardships_of(mind, &stewardship.repo, None).iter().any(|(other, _)| *other != key) {
if docs.rival_in_force(key, &staged.document).is_some() {
return Err(MindRefusal::AlreadyStewarded { repo: stewardship.repo.0.clone() });
}
Ok(())
@ -770,11 +770,19 @@ fn resolution_rule(docs: &Docs, resolution: &PipelineResolution) -> Result<(), M
&& subject_kind == K::Resolution
&& let Some(D::Resolution(reinstating)) = docs.find(K::Resolution, subject_id)
&& let Some(base) = docs.find(reinstating.subject.kind, &reinstating.subject.id.0)
&& let Some(later) = docs.later_in_force(base)
&& let Some(rival) = docs.rival_in_force(&reinstating.subject.id.0, base)
{
// A stewardship put back in force beside an earlier one meets the
// admission refusal for the same fact: the repo already has a steward.
if let D::Stewardship(base) = base
&& let Some(D::Stewardship(standing)) = docs.find(K::Stewardship, rival)
&& standing.sequence < base.sequence
{
return Err(MindRefusal::AlreadyStewarded { repo: base.repo.0.clone() });
}
return Err(MindRefusal::WouldReinstateOverLater {
subject: reinstating.subject.id.0.clone(),
later: later.into(),
later: rival.into(),
});
}
// Withdrawing a run's closure puts the run back in force, and a run in
@ -1682,6 +1690,33 @@ mod tests {
assert_eq!(in_force, vec![stewardship_key(1)]);
}
/// One assignment stands per repo, whichever record a withdrawal would put
/// back: reinstating the later of two withdrawn assignments while the
/// earlier is back in force is refused as the assignment itself would be,
/// and the next assignment then meets exactly the one that stands.
#[test]
fn a_withdrawal_does_not_reinstate_a_stewardship_beside_an_earlier_one() {
let mut mind = seeded();
let n1 = stewardship_key(1);
let n2 = stewardship_key(2);
let first = format!("{INSTANCE}:resolution:stewardship.gamecult_-epiphany.n1.n1");
let second = format!("{INSTANCE}:resolution:stewardship.gamecult_-epiphany.n2.n1");
committed(admit(&mut mind, vec![resolution(r(K::Stewardship, &n1), withdrawn())]));
committed(admit(&mut mind, vec![stewardship_n(INSTANCE, REPO, 2)]));
committed(admit(&mut mind, vec![resolution(r(K::Stewardship, &n2), withdrawn())]));
committed(admit(&mut mind, vec![resolution(r(K::Resolution, &first), withdrawn())]));
assert_eq!(
refusal(admit(&mut mind, vec![resolution(r(K::Resolution, &second), withdrawn())])),
MindRefusal::AlreadyStewarded { repo: REPO.into() }
);
let in_force = docs_of(&mind).stewardships_of(&slug(INSTANCE), &repo(REPO), None).iter().map(|(key, _)| key.to_string()).collect::<Vec<_>>();
assert_eq!(in_force, vec![n1], "the reinstated assignment stands alone");
assert_eq!(
refusal(admit(&mut mind, vec![stewardship_n(INSTANCE, REPO, 3)])),
MindRefusal::AlreadyStewarded { repo: REPO.into() }
);
}
/// And it reads the batch: the assignment that stands later than the one
/// a withdrawal would reinstate may be landing beside that withdrawal,
/// derived by a hand-off in the same batch.

View file

@ -224,12 +224,17 @@ impl Docs {
.collect()
}
/// The in-force record that stands later than `base` in the scope `base`
/// is sequenced in, if any: what a withdrawal that put `base` back in
/// force would leave standing beside it.
pub(crate) fn later_in_force(&self, base: &PipelineDocument) -> Option<&str> {
/// The in-force record, other than `base_key`, that cannot stand beside
/// `base` on its line: the one question "may this record be in force"
/// asks, whether `base` is being admitted (a stewardship's
/// `AlreadyStewarded`) or put back in force by a withdrawal. A line holds
/// one stewardship at a time, so any other stands in its way; a revision
/// is only displaced by a later one.
pub(crate) fn rival_in_force(&self, base_key: &str, base: &PipelineDocument) -> Option<&str> {
let kind = base.kind();
self.of_kind(kind).find(|(key, other)| later_than(base, other) && self.in_force(kind, key)).map(|(key, _)| key)
self.of_kind(kind)
.find(|(key, other)| *key != base_key && rivals(base, other) && self.in_force(kind, key))
.map(|(key, _)| key)
}
/// The in-force run, other than `run_key`, that holds `claim`: the one
@ -336,18 +341,18 @@ pub(crate) fn line_of(document: &PipelineDocument) -> Option<Lineage<'_>> {
}
}
/// Whether `other` is a later record than `base` on the line `base` is
/// sequenced in. Kinds that carry no sequence have no order, and a resolution
/// is never a base here: the cap above refuses the withdrawal that would
/// reinstate one before the question of a later record arises.
fn later_than(base: &PipelineDocument, other: &PipelineDocument) -> bool {
/// Whether `other` cannot stand in force beside `base`, being on `base`'s line:
/// any other stewardship of an instance and repo, a later revision of a target
/// or cut spec. A resolution is never a base here: the cap in `resolution_rule`
/// refuses the withdrawal that would reinstate one first.
fn rivals(base: &PipelineDocument, other: &PipelineDocument) -> bool {
use PipelineDocument as D;
let Some(line) = line_of(base) else { return false };
if line_of(other) != Some(line) {
return false;
}
match (base, other) {
(D::Stewardship(base), D::Stewardship(other)) => other.sequence > base.sequence,
(D::Stewardship(_), D::Stewardship(_)) => true,
(D::Target(base), D::Target(other)) => other.revision > base.revision,
(D::CutSpec(base), D::CutSpec(other)) => other.revision > base.revision,
_ => false,