From e085286126a1a9a080117498f8395374f4249d1a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 17:27:14 +0200 Subject: [PATCH] Import key: the door test checks Draft doors in Draft and Active doors in Active; held and declared keys are separate sets Compile-checked; tests run in the next job. Co-Authored-By: Claude Opus 5.5 --- crates/ghostlight/src/import_key_tests.rs | 119 ++++++++++++---------- crates/ghostlight/src/patch.rs | 6 +- 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/crates/ghostlight/src/import_key_tests.rs b/crates/ghostlight/src/import_key_tests.rs index 8f5a057..9fe522a 100644 --- a/crates/ghostlight/src/import_key_tests.rs +++ b/crates/ghostlight/src/import_key_tests.rs @@ -166,9 +166,9 @@ fn one_patch_cannot_declare_two_subjects_with_one_import_key() { assert_eq!(kernel.state.revision, revision, "a refusal moved the world"); } -/// The fixture world past genesis: the owner adds an unwalked place, and the -/// world is active so Play and the elaborator can act. -fn active_keyed_world() -> (tempfile::TempDir, WorldKernel, EntityId) { +/// The fixture world past genesis but still in Draft: the owner has added an +/// unwalked place the elaborator can later answer. +fn advanced_keyed_world() -> (tempfile::TempDir, WorldKernel, EntityId) { let (dir, mut kernel) = keyed_world(); let place = commons(&kernel); submit_as( @@ -200,14 +200,52 @@ fn active_keyed_world() -> (tempfile::TempDir, WorldKernel, EntityId) { .find(|(_, record)| record.label == "The Unwalked Road") .map(|(id, _)| id) .unwrap(); - activate(&mut kernel); (dir, kernel, dead_end) } +/// Each door names the held key, typed, and the refusal leaves the revision +/// where it was. The owner and the consumer declare in Draft only; Play and the +/// elaborator act in Active. +fn refuse_held_key_at_each_door( + kernel: &mut WorldKernel, + place: EntityId, + doors: Vec<(&str, CallerId, Option)>, +) { + for (door, caller, answers) in doors { + let revision = kernel.state.revision; + let result = submit_as( + kernel, + caller, + answers, + patch_of(vec![mirror("copy", Some(alpha()), place)]), + ); + assert_eq!(rejected(result), vec![held("copy")], "{door}"); + assert_eq!( + kernel.state.revision, revision, + "{door} refusal moved the world" + ); + } +} + +fn consumer() -> CallerId { + CallerId::System(SystemCapability::Consumer { + consumer: ConsumerId::of_name("import-key"), + }) +} + #[test] fn a_held_import_key_is_refused_at_every_door_after_the_revision_has_moved() { - let (_dir, mut kernel, dead_end) = active_keyed_world(); - assert!(kernel.state.revision > 1, "the world has moved past genesis"); + let (_dir, mut kernel, dead_end) = advanced_keyed_world(); + assert!(kernel.state.revision > 0, "the world has moved past genesis"); + refuse_held_key_at_each_door( + &mut kernel, + dead_end, + vec![ + ("owner", CallerId::Principal(owner()), None), + ("consumer", consumer(), None), + ], + ); + activate(&mut kernel); let answer = crate::derive_boundaries(&kernel.state) .unwrap() .into_iter() @@ -216,62 +254,33 @@ fn a_held_import_key_is_refused_at_every_door_after_the_revision_has_moved() { if *place == dead_end) }) .expect("the dead end is an unelaborated destination"); - let doors: Vec<(&str, CallerId, Option)> = vec![ - ("owner", CallerId::Principal(owner()), None), - ( - "consumer", - CallerId::System(SystemCapability::Consumer { - consumer: ConsumerId::of_name("import-key"), - }), - None, - ), - ("play", CallerId::System(SystemCapability::Play), None), - ( - "elaborator", - CallerId::System(SystemCapability::Elaborator { - jurisdiction: JurisdictionKey::PlaceSubtree(dead_end), - }), - Some(PatchAnswer::Boundary(answer)), - ), - ]; - for (door, caller, answers) in doors { - let revision = kernel.state.revision; - let result = submit_as( - &mut kernel, - caller.clone(), - answers.clone(), - patch_of(vec![mirror("copy", Some(alpha()), dead_end)]), - ); - // The owner and the consumer author in Draft only, so in an active - // world those two may refuse for the phase before they resolve; the - // held key is named wherever the declaration is resolved at all. - match result { - Err(KernelError::PatchRejected(set)) => { - assert!(set.contains(&held("copy")), "{door}: {set:?}") - } - other => { - assert!( - matches!(door, "owner" | "consumer"), - "{door} did not resolve the declaration: {other:?}" - ); - } - } - assert_eq!( - kernel.state.revision, revision, - "{door} refusal moved the world" - ); - } - // The same path admits an unheld key, so the refusals are the key's. + refuse_held_key_at_each_door( + &mut kernel, + dead_end, + vec![ + ("play", CallerId::System(SystemCapability::Play), None), + ( + "elaborator", + CallerId::System(SystemCapability::Elaborator { + jurisdiction: JurisdictionKey::PlaceSubtree(dead_end), + }), + Some(PatchAnswer::Boundary(answer.clone())), + ), + ], + ); + // The same doors admit an unheld key, so the refusals are the key's; and + // what a door admitted is held against every later door. let fresh = key("aetheria.ship", "Gamma"); let receipt = submit_as( &mut kernel, - CallerId::System(SystemCapability::Play), - None, + CallerId::System(SystemCapability::Elaborator { + jurisdiction: JurisdictionKey::PlaceSubtree(dead_end), + }), + Some(PatchAnswer::Boundary(answer)), patch_of(vec![mirror("fresh", Some(fresh.clone()), dead_end)]), ) .unwrap(); assert!(matches!(receipt, SubmitReceipt::Applied(_))); - // A later revision holds it too. let again = submit_as( &mut kernel, CallerId::System(SystemCapability::Play), diff --git a/crates/ghostlight/src/patch.rs b/crates/ghostlight/src/patch.rs index feeba17..81b99cc 100644 --- a/crates/ghostlight/src/patch.rs +++ b/crates/ghostlight/src/patch.rs @@ -3194,12 +3194,12 @@ pub(super) fn resolve_patch( // Derived from the subjects the world holds, retired ones included, so a // key is never reissued; no stored index. - let mut import_keys: BTreeSet<&ImportKey> = state + let held_keys: BTreeSet<&ImportKey> = state .subjects .values() .filter_map(|subject| subject.import_key.as_ref()) .collect(); - let mut held_keys = import_keys.clone(); + let mut declared_keys: BTreeSet<&ImportKey> = BTreeSet::new(); for (position, declaration) in patch.declarations.iter().enumerate() { let (handle, label, kind) = match declaration { Declaration::Subject(subject) => ( @@ -3247,7 +3247,7 @@ pub(super) fn resolve_patch( mismatches.push(Mismatch::MalformedImportKey { handle }); } else if held_keys.contains(key) { mismatches.push(Mismatch::ImportKeyHeld { handle }); - } else if !import_keys.insert(key) { + } else if !declared_keys.insert(key) { mismatches.push(Mismatch::DuplicateImportKey { handle }); } }