huginn-mind: an import carries only a campaign's history, under a root only imports have written

Import mode turns derivation off, so it is open to any document whose derived writes native admission would have made: an imported hand-off left the steward's stewardship in force beside it, an imported ruling answered a question the Answered of which was never written and let a native ruling answer it again. A8 now refuses an import (ImportOutsideCampaign) unless each document's root names a campaign this mind holds or the batch carries and no native write has touched that root. Instance-rooted records have no campaign and never qualify. Replays that build a campaign from imports alone are untouched; the move tests that built the receiver natively now build it by import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-08 19:11:56 +01:00
commit 1533755425
6 changed files with 123 additions and 3 deletions

View file

@ -176,6 +176,9 @@ impl<S: MindStore> Mind<S> {
}
let candidate = receipt::candidate(&mind, provenance, &strong_reads, &writes, receipt::head(self)? + 1, now)?;
// A8
if import.is_some() {
refuse_unscoped_import(&docs)?;
}
for staged in &docs.batch {
check(&docs, staged, &mind, import.as_ref())?;
}
@ -503,6 +506,22 @@ fn derive(docs: &Docs<'_>, mind: &Slug) -> Vec<PipelineDocument> {
derived
}
/// An import replays a campaign's history: every document is rooted at a campaign this mind holds
/// or the batch carries, and the mind holds no native write under that root, so the source's own
/// derivations stand alone and no native rule is bypassed for a record the mind could have written.
/// Instance-rooted records (hand-offs, stewardships, runs, memories, thoughts) are never history
/// to replay.
fn refuse_unscoped_import(docs: &Docs<'_>) -> Result<(), MindRefusal> {
for staged in &docs.batch {
let root = root_and_local(&staged.key).0;
let campaign = format!("{root}:campaign:self");
if docs.find(PipelineKind::Campaign, &campaign).is_none() || docs.root_holds_native_write(root) {
return Err(MindRefusal::ImportOutsideCampaign { root: root.into() });
}
}
Ok(())
}
fn is_external(external: &[PipelineRef], kind: PipelineKind, id: &str) -> bool {
external.iter().any(|referent| referent.kind == kind && referent.id.0 == id)
}

View file

@ -68,6 +68,11 @@ impl<'i> Docs<'i> {
self.in_batch(kind, id).or_else(|| self.in_image(kind, id))
}
/// Whether this mind holds a native write under `root` (the batch is not asked).
pub(crate) fn root_holds_native_write(&self, root: &str) -> bool {
self.image.root_holds_native_write(root)
}
pub(crate) fn of_kind(&self, kind: PipelineKind) -> impl Iterator<Item = (&str, &PipelineDocument)> {
let batch = self.batch.iter().filter(move |staged| staged.kind == kind).map(|staged| (staged.key.as_str(), &staged.document));
let image = self

View file

@ -143,6 +143,16 @@ impl Image {
self.closers.get(&kind).and_then(|named| named.get(id)).into_iter().flat_map(|keyed| keyed.iter().map(|(key, id)| (key.as_str(), *id)))
}
/// Whether any document under `root` was admitted natively rather than imported.
pub(crate) fn root_holds_native_write(&self, root: &str) -> bool {
PipelineKind::ALL.iter().any(|kind| {
self.posting(*kind, Alias::Root.name(), root)
.into_iter()
.flatten()
.any(|id| self.row(*id).view.admission.provenance.imported_from.is_none())
})
}
/// Every root with the newest row under it.
pub(crate) fn roots(&self) -> impl Iterator<Item = (&str, &SelectionRow)> {
self.roots.iter().map(|(root, id)| (root.as_str(), self.row(*id)))

View file

@ -54,6 +54,13 @@ fn other_follow_up(label: &str, question: &str) -> D {
follow_up
}
/// The campaign of `OTHER_ROOT`, which a ruling of that root belongs to.
fn other_campaign() -> D {
let D::Campaign(mut value) = campaign(&[REPO]) else { unreachable!() };
value.slug = slug(OTHER_ROOT);
D::Campaign(value)
}
fn view_of(mind: &Mind<MemoryStore>, kind: K, key: &str) -> crate::query::PipelineDocumentView {
mind.view(&r(kind, key)).unwrap().unwrap_or_else(|| panic!("{key} is not held"))
}
@ -301,6 +308,7 @@ fn import_replays_a_receipt_without_derivation() {
// receiver does not derive it back.
let q1 = id("question", "Q1");
let ruling = answering(OTHER_ROOT, "R1", &q1);
committed(import(&mut mind, vec![other_campaign()], vec![]));
let before = head(&mind);
let (_, writes) = committed(import(&mut mind, vec![ruling.clone()], vec![r(K::Question, &q1)]));
assert_eq!(writes, vec![r(K::Ruling, &pipeline_key(&ruling).unwrap())], "only what the source wrote");
@ -317,7 +325,7 @@ fn import_replays_a_receipt_without_derivation() {
#[test]
fn import_admits_a_declared_external_and_refuses_an_undeclared_one() {
let mut mind = receiver();
committed(admit(&mut mind, vec![campaign(&[REPO]), question("Q1", &["A", "B"], "A"), follow_up("FU-1", r(K::Question, &id("question", "Q1")))]));
committed(import(&mut mind, vec![campaign(&[REPO]), question("Q1", &["A", "B"], "A"), follow_up("FU-1", r(K::Question, &id("question", "Q1")))], vec![]));
let report = r(K::CutReport, &format!("{OTHER_ROOT}:cut_report:cut-3.h1"));
let fixed = resolution(
r(K::FollowUp, &id("follow_up", "FU-1")),
@ -334,7 +342,7 @@ fn import_admits_a_declared_external_and_refuses_an_undeclared_one() {
// A listing names the referent it stands in for and no other of its kind.
let mut elsewhere = receiver();
committed(admit(&mut elsewhere, vec![campaign(&[REPO]), question("Q1", &["A", "B"], "A"), follow_up("FU-1", r(K::Question, &id("question", "Q1")))]));
committed(import(&mut elsewhere, vec![campaign(&[REPO]), question("Q1", &["A", "B"], "A"), follow_up("FU-1", r(K::Question, &id("question", "Q1")))], vec![]));
let other = r(K::CutReport, &format!("{OTHER_ROOT}:cut_report:cut-4.h1"));
assert_eq!(
refusal(import(&mut elsewhere, vec![fixed.clone()], vec![other])),
@ -350,7 +358,7 @@ fn import_admits_a_declared_external_and_refuses_an_undeclared_one() {
#[test]
fn import_refuses_a_present_external_a_moved_outcome_and_its_own_instance() {
let mut mind = receiver();
committed(admit(&mut mind, vec![campaign(&[REPO]), question("Q1", &["A", "B"], "A")]));
committed(import(&mut mind, vec![campaign(&[REPO]), question("Q1", &["A", "B"], "A")], vec![]));
let held = r(K::Question, &id("question", "Q1"));
let stray = follow_up("FU-1", held.clone());
// Held in the image.
@ -519,3 +527,58 @@ fn a_moved_resolution_lands_alone_under_its_root() {
}
assert!(recv.view(&r(K::FollowUp, &id("follow_up", "FU-C"))).unwrap().is_none());
}
fn import_from_kiko(mind: &mut Mind<MemoryStore>, documents: Vec<D>) -> PipelineAdmissionOutcome {
import_as(mind, source_of("kiko", vec![]), documents)
}
fn stewardship_status(mind: &Mind<MemoryStore>, instance: &str) -> PipelineStatus {
view_of(mind, K::Stewardship, &pipeline_key(&stewardship(instance, REPO)).unwrap()).status
}
#[test]
fn an_import_refuses_instance_rooted_records() {
// A hand-off from this mind, imported, would leave her stewardship in force beside its own hand-off:
// import mode derives nothing, so the call that resolves a stewardship is not one it may carry.
let mut mind = seeded();
let before = head(&mind);
let handing = hand_off(INSTANCE, OTHER_INSTANCE, REPO, &[]);
assert_eq!(refusal(import_from_kiko(&mut mind, vec![handing.clone()])), MindRefusal::ImportOutsideCampaign { root: INSTANCE.into() });
assert_eq!(
refusal(import_from_kiko(&mut mind, vec![thought("T1", "an import", &[])])),
MindRefusal::ImportOutsideCampaign { root: INSTANCE.into() }
);
assert_eq!(head(&mind), before, "nothing landed");
assert!(matches!(stewardship_status(&mind, INSTANCE), PipelineStatus::InForce));
// The same hand-off, admitted natively, resolves the stewardship.
committed(admit(&mut mind, vec![handing]));
assert!(matches!(stewardship_status(&mind, INSTANCE), PipelineStatus::Resolved { .. }));
}
#[test]
fn an_import_lands_only_under_a_root_that_only_imports_have_written() {
// This mind wrote Q1 and the campaign itself: a ruling imported beside them would answer Q1
// without the Answered a native ruling derives, and a native ruling could then answer it again.
let mut mind = seeded();
committed(admit(&mut mind, vec![question("Q1", &["A", "B"], "A")]));
let before = head(&mind);
let q1 = id("question", "Q1");
let first = answering(CAMPAIGN, "R1", &q1);
assert_eq!(refusal(import_from_kiko(&mut mind, vec![first])), MindRefusal::ImportOutsideCampaign { root: CAMPAIGN.into() });
assert_eq!(head(&mind), before);
assert!(matches!(view_of(&mind, K::Question, &q1).status, PipelineStatus::InForce));
// A root only imports have written takes more imports, and a retry of one is no second write.
let mut recv = receiver();
let history = vec![campaign(&[REPO]), question("Q1", &["A", "B"], "A")];
committed(import_from_kiko(&mut recv, history.clone()));
committed(import_from_kiko(&mut recv, vec![question("Q2", &["A", "B"], "A")]));
assert!(matches!(import_from_kiko(&mut recv, history), PipelineAdmissionOutcome::AlreadyAdmitted { .. }));
// A root that names no campaign, held or carried, is not a campaign's history.
let mut bare = receiver();
assert_eq!(
refusal(import_from_kiko(&mut bare, vec![question("Q1", &["A", "B"], "A")])),
MindRefusal::ImportOutsideCampaign { root: CAMPAIGN.into() }
);
}

View file

@ -66,6 +66,8 @@ pub enum MindRefusal {
MoveIsFinal,
/// An import names this mind as its source.
ImportFromSelf,
/// An import carries a document of `root`, which is no campaign this mind holds or the batch carries, or which this mind holds a native write under.
ImportOutsideCampaign { root: String },
/// An import lists `id` as external, but this mind (or the batch) holds it.
ExternalPresent { id: String },
/// The persona document fails the published schema: `path` is the

View file

@ -1676,6 +1676,27 @@
"type": "string",
"const": "ImportFromSelf"
},
{
"description": "An import carries a document of `root`, which is no campaign this mind holds or the batch carries, or which this mind holds a native write under.",
"type": "object",
"properties": {
"ImportOutsideCampaign": {
"type": "object",
"properties": {
"root": {
"type": "string"
}
},
"required": [
"root"
]
}
},
"additionalProperties": false,
"required": [
"ImportOutsideCampaign"
]
},
{
"description": "An import lists `id` as external, but this mind (or the batch) holds it.",
"type": "object",