From 396bc6a63ff2a4add62c6ab4bab2769f8d4f4470 Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 20 Nov 2026 13:17:05 +0100 Subject: [PATCH] aetherlight: import keys and affordance kinds are read through indexes Subjects gain a ByImportKey index and the affordance catalog a ByKind index. held_import_keys and the per-patch kind_names set are deleted: import_key_fault takes a held predicate, and admit_resolved, resolve_patch and verify_state_shape's open-time check ask the index (or nothing, at open). Co-Authored-By: Claude Sonnet 5.5 --- native/aetherlight/src/golden_tests.rs | 34 ++++++++++++++ native/aetherlight/src/journal.rs | 3 +- native/aetherlight/src/lib.rs | 65 +++++++++++++++++++++++--- native/aetherlight/src/patch.rs | 41 ++++++---------- 4 files changed, 108 insertions(+), 35 deletions(-) diff --git a/native/aetherlight/src/golden_tests.rs b/native/aetherlight/src/golden_tests.rs index 4a801b748..382e728bc 100644 --- a/native/aetherlight/src/golden_tests.rs +++ b/native/aetherlight/src/golden_tests.rs @@ -673,6 +673,16 @@ fn populated_worlds() -> Vec { WorldKernel::create(creation(title), &auth_principal(owner())).unwrap().0 }; let mut worlds = vec![golden_session().0.state]; + // Two subjects holding import keys, so the subjects' `ByImportKey` index has + // points to read and to damage. + let mut keyed = crate::tests::creation("Keyed"); + for declaration in &mut keyed.patch.declarations { + if let Declaration::Subject(subject) = declaration { + let record = subject.label.clone(); + subject.import_key = Some(crate::ImportKey::new("aetherlight.fixture", record)); + } + } + worlds.push(WorldKernel::create(keyed, &auth_principal(owner())).unwrap().0.state); let mut kernel = created("Civic"); civic_world(&mut kernel); worlds.push(kernel.state); @@ -1345,6 +1355,28 @@ fn reference_scan_disagreement(state: &crate::WorldState) -> Option { return Some(format!("dependents of {target:?}")); } } + let strange = crate::ImportKey::new("aetherlight.fixture", "held by nobody"); + for key in state + .subjects + .values() + .filter_map(|subject| subject.import_key.as_ref()) + .chain(std::iter::once(&strange)) + { + if state.import_key_held(key) != crate::import_key_held_by_scan(state, key) { + return Some(format!("import key {key:?} held")); + } + } + let unnamed = crate::patch::AffordanceKindName("no such kind".into()); + for kind in state + .affordance_catalog + .values() + .map(|entry| &entry.kind) + .chain(std::iter::once(&unnamed)) + { + if state.kind_taken(kind) != crate::kind_taken_by_scan(state, kind) { + return Some(format!("affordance kind {kind:?} taken")); + } + } for place in &places { if crate::under_place(state, *place) != crate::under_place_by_scan(state, *place) { return Some(format!("under_place {place:?}")); @@ -1465,6 +1497,8 @@ fn a_corrupted_index_is_detected() { } } let indexed: BTreeSet<&str> = [ + "aetherlight.subject.v1", + "aetherlight.affordance.v1", "aetherlight.entity.v1", "aetherlight.edge.v1", "aetherlight.position.v1", diff --git a/native/aetherlight/src/journal.rs b/native/aetherlight/src/journal.rs index 1d795e086..929ce057c 100644 --- a/native/aetherlight/src/journal.rs +++ b/native/aetherlight/src/journal.rs @@ -520,10 +520,9 @@ pub(super) fn verify_state_shape(state: &WorldState) -> Result<(), JournalError> )); } let mut declared_keys = BTreeSet::new(); - let no_held = BTreeSet::new(); for subject in state.subjects.values() { if let Some(key) = &subject.import_key - && super::patch::import_key_fault(key, &no_held, &mut declared_keys).is_some() + && super::patch::import_key_fault(key, |_| false, &mut declared_keys).is_some() { return Err(JournalError::Corrupt( "subject import key is malformed or held by two subjects".into(), diff --git a/native/aetherlight/src/lib.rs b/native/aetherlight/src/lib.rs index a50c58a79..9aa732548 100644 --- a/native/aetherlight/src/lib.rs +++ b/native/aetherlight/src/lib.rs @@ -96,10 +96,12 @@ use patch::{ }; #[cfg(test)] pub(crate) use patch::{ - AffordanceKindName, AuthorityGrantRef, AuthorityKindName, AuthorityTargetRef, ComponentOpKind, + AuthorityGrantRef, AuthorityKindName, AuthorityTargetRef, ComponentOpKind, EffectSlot, OutcomeBand, Precondition, RoleSpec, }; -use patch::{EdgeRecord, EntityRecord, LedgerDelta, ResolvedOp, ResolvedPatch}; +use patch::{ + AffordanceKindName, EdgeRecord, EntityRecord, LedgerDelta, ResolvedOp, ResolvedPatch, +}; use digest::{LtHash16, Partition}; use docs::{Changed, DocKey, Docs, Tracked}; @@ -606,8 +608,32 @@ impl docs::Projection> for ByIncumbent { } } +/// `ByImportKey`: the subject that holds an import key. Retired subjects keep +/// theirs, so a key is never reissued. +#[derive(Clone, Debug)] +struct ByImportKey; + +impl docs::Projection for ByImportKey { + type Point = ImportKey; + fn points(value: &SubjectState, out: &mut Vec) { + out.extend(value.import_key.clone()); + } +} + +/// `ByKind`: the catalog entry that names an affordance kind, so a kind is +/// taken exactly when a point holds an entry. +#[derive(Clone, Debug)] +struct ByKind; + +impl docs::Projection for ByKind { + type Point = AffordanceKindName; + fn points(value: &Affordance, out: &mut Vec) { + out.push(value.kind.clone()); + } +} + world_state! { - subjects: Docs = "aetherlight.subject.v1"; + subjects: Docs = "aetherlight.subject.v1"; entities: Docs = "aetherlight.entity.v1"; edges: Docs = "aetherlight.edge.v1"; positions: Docs = "aetherlight.position.v1"; @@ -649,7 +675,7 @@ world_state! { pressures: Docs, BySource> = "aetherlight.pressures.v1"; /// What an affordance *is*. World-authored and written by `admit_resolved` /// alone. - affordance_catalog: Docs = "aetherlight.affordance.v1"; + affordance_catalog: Docs = "aetherlight.affordance.v1"; /// Who may exercise which entry: a row exists if and only if the subject /// holds at least one verb. A grant carries no payload, so a duplicate /// grant is unrepresentable rather than checked. @@ -1271,7 +1297,7 @@ fn require_play(caller: &CallerId) -> Result<(), KernelError> { /// `WorldState` so the genesis lane, which resolves declarations before any /// `WorldState` exists, can call the same owner instead of hand-copying the /// literal it would otherwise always evaluate to. -fn is_genesis(revision: u64, subjects: &Docs) -> bool { +fn is_genesis(revision: u64, subjects: &Docs) -> bool { revision == 0 && subjects.is_empty() } @@ -1542,11 +1568,11 @@ fn admit_resolved( // The subjects map's only writer holds the same injectivity the resolver // checks, decided by the same function. { - let held = patch::held_import_keys(state.subjects.values()); let mut declared = BTreeSet::new(); for subject in &resolved.subjects { if let Some(key) = &subject.subject.import_key - && patch::import_key_fault(key, &held, &mut declared).is_some() + && patch::import_key_fault(key, |key| state.import_key_held(key), &mut declared) + .is_some() { return Err(KernelError::Invariant( "admitted subject's import key is malformed, held or repeated".into(), @@ -2668,6 +2694,31 @@ fn channel_referents_exist(state: &WorldState, record: &ChannelRecord) -> bool { .is_none_or(|subject_id| state.subjects.contains_key(&subject_id)) } +impl WorldState { + /// Whether a subject, retired or not, holds this import key. One point read + /// of `ByImportKey`. + fn import_key_held(&self, key: &ImportKey) -> bool { + !self.subjects.indexed(key.clone()..=key.clone()).is_empty() + } + + /// Whether a catalog entry already names this affordance kind. + fn kind_taken(&self, kind: &AffordanceKindName) -> bool { + !self.affordance_catalog.indexed(kind.clone()..=kind.clone()).is_empty() + } +} + +/// What `import_key_held` and `kind_taken` were before their indexes: every +/// subject, every catalog entry. The index reads must equal them. +#[cfg(test)] +fn import_key_held_by_scan(state: &WorldState, key: &ImportKey) -> bool { + state.subjects.values().any(|subject| subject.import_key.as_ref() == Some(key)) +} + +#[cfg(test)] +fn kind_taken_by_scan(state: &WorldState, kind: &AffordanceKindName) -> bool { + state.affordance_catalog.values().any(|entry| &entry.kind == kind) +} + /// Whether a subject is retired: the one statement of retirement, read by /// audience derivation, preconditions and resolvers. Only an actual `Retire` /// sets `SubjectState.retired`. diff --git a/native/aetherlight/src/patch.rs b/native/aetherlight/src/patch.rs index 7bf53d956..31dbb7ba1 100644 --- a/native/aetherlight/src/patch.rs +++ b/native/aetherlight/src/patch.rs @@ -958,28 +958,19 @@ pub(super) enum ImportKeyFault { Duplicate, } -/// The keys the subjects hold, retired ones included, so a key is never -/// reissued. Derived on demand; no stored index. -pub(super) fn held_import_keys<'a>( - subjects: impl IntoIterator, -) -> BTreeSet<&'a ImportKey> { - subjects - .into_iter() - .filter_map(|subject| subject.import_key.as_ref()) - .collect() -} - /// The one decision on a key: malformed, held by a subject already in the -/// world, or already claimed in `declared` (which it joins). Every door that -/// admits or loads subjects calls this, so none can decide otherwise. +/// world (`held` asks the subjects' `ByImportKey` index, retired ones +/// included, so a key is never reissued), or already claimed in `declared` +/// (which it joins). Every door that admits or loads subjects calls this, so +/// none can decide otherwise. pub(super) fn import_key_fault<'a>( key: &'a ImportKey, - held: &BTreeSet<&ImportKey>, + held: impl Fn(&ImportKey) -> bool, declared: &mut BTreeSet<&'a ImportKey>, ) -> Option { if !key.is_canonical() { Some(ImportKeyFault::Malformed) - } else if held.contains(key) { + } else if held(key) { Some(ImportKeyFault::Held) } else if !declared.insert(key) { Some(ImportKeyFault::Duplicate) @@ -2414,7 +2405,7 @@ fn resolve_affordance( site: Site, reference: &Ref, index: &BTreeMap, - catalog: &Docs, + catalog: &Docs, mismatches: &mut Vec, ) -> Option> { match reference { @@ -2726,7 +2717,7 @@ fn resolve_subject( site: Site, reference: &Ref, index: &BTreeMap, - subjects: &Docs, + subjects: &Docs, mismatches: &mut Vec, ) -> Option> { match reference { @@ -3083,16 +3074,13 @@ pub(super) fn resolve_patch( let speak = kernel_speak_entry(); check_against_grammar(state, &speak_handle, &speak.kind, &speak.roles, &mut mismatches); } - let mut kind_names: BTreeSet = state - .affordance_catalog - .values() - .map(|entry| entry.kind.clone()) - .collect(); + // The kinds this patch declares; a kind the catalog already names is asked + // of its `ByKind` index. + let mut declared_kinds: BTreeSet = BTreeSet::new(); if genesis { - kind_names.insert(kernel_speak_entry().kind); + declared_kinds.insert(kernel_speak_entry().kind); } - let held_keys = held_import_keys(state.subjects.values()); let mut declared_keys: BTreeSet<&ImportKey> = BTreeSet::new(); for (position, declaration) in patch.declarations.iter().enumerate() { let (handle, label, kind) = match declaration { @@ -3137,7 +3125,7 @@ pub(super) fn resolve_patch( && let Some(key) = &subject.import_key { let handle = subject.handle.clone(); - match import_key_fault(key, &held_keys, &mut declared_keys) { + match import_key_fault(key, |key| state.import_key_held(key), &mut declared_keys) { Some(ImportKeyFault::Malformed) => { mismatches.push(Mismatch::MalformedImportKey { handle }); } @@ -3166,7 +3154,8 @@ pub(super) fn resolve_patch( &affordance.roles, &mut mismatches, ); - if !kind_names.insert(affordance.kind.clone()) { + let taken = state.kind_taken(&affordance.kind); + if !declared_kinds.insert(affordance.kind.clone()) || taken { mismatches.push(Mismatch::DuplicateAffordanceKind { handle: affordance.handle.clone(), });