diff --git a/crates/ghostlight-dungeon/src/agency_corpus.rs b/crates/ghostlight-dungeon/src/agency_corpus.rs new file mode 100644 index 0000000..b76d6e6 --- /dev/null +++ b/crates/ghostlight-dungeon/src/agency_corpus.rs @@ -0,0 +1,635 @@ +use crate::transition::{ + ActionMeans, AgencyAdmissionExpectation, AgencyAttemptCase, AgencyResolutionLane, + MutationIntent, SubjectKind, SubjectRef, WorldComponentKind, WorldMutationOperation, +}; +use std::collections::BTreeSet; + +#[derive(Clone)] +struct Candidate<'a> { + id: &'a str, + domain: &'a str, + scenario: &'a str, + means: &'a str, + desired: &'a str, + target_kind: SubjectKind, + component: WorldComponentKind, + admission: AgencyAdmissionExpectation, + required: &'a [WorldMutationOperation], + forbidden: &'a [WorldMutationOperation], + bargain: Option<&'a str>, +} + +pub fn seed_agency_attempt_cases() -> Vec { + use AgencyAdmissionExpectation::{Admissible, BargainRequired, Impossible}; + use SubjectKind::{Actor, Institution, Place, Population, Resource}; + use WorldComponentKind::{ + Commitment, Condition, Custody, Identity, Knowledge, Occupancy, PopulationMembership, + Posture, Relationship, ResourceState, Topology, + }; + use WorldMutationOperation as Op; + + let candidates = [ + Candidate { + id: "social-persuade-guard", + domain: "social", + scenario: "A traveler asks a wary gate guard to admit them before curfew.", + means: "Present a valid trade letter and explain the delayed caravan.", + desired: "The guard chooses to admit the traveler.", + target_kind: Actor, + component: Posture, + admission: Admissible, + required: &[Op::PostureChange], + forbidden: &[Op::KnowledgeAcquire], + bargain: None, + }, + Candidate { + id: "social-promise-repayment", + domain: "social", + scenario: "A refugee asks a medic for scarce treatment and promises later repayment.", + means: "State the promise directly to the medic while both are present.", + desired: "Create an obligation to repay the medic.", + target_kind: Actor, + component: Commitment, + admission: Admissible, + required: &[Op::CommitmentCreate], + forbidden: &[Op::TransferCustody], + bargain: None, + }, + Candidate { + id: "social-command-stranger", + domain: "social", + scenario: "A stranger declares that a veteran now serves them.", + means: "Issue a confident order without rank, leverage, consent, or coercive control.", + desired: "Make the veteran accept a binding service obligation.", + target_kind: Actor, + component: Commitment, + admission: BargainRequired, + required: &[], + forbidden: &[Op::CommitmentCreate], + bargain: Some("gain consent, recognized command authority, or concrete leverage"), + }, + Candidate { + id: "physical-climb-wall", + domain: "physical", + scenario: "A climber tries to scale a wet stone wall using a rope and pitons.", + means: "Anchor the rope and climb the reachable wall with the available kit.", + desired: "Reach the parapet.", + target_kind: Place, + component: Occupancy, + admission: Admissible, + required: &[Op::Relocate], + forbidden: &[Op::TopologyAdd], + bargain: None, + }, + Candidate { + id: "physical-break-door", + domain: "physical", + scenario: "A trapped actor tries to break a damaged wooden door with a pry bar.", + means: "Apply the pry bar at the split latch while standing beside the door.", + desired: "Damage the door enough to pass.", + target_kind: Resource, + component: ResourceState, + admission: Admissible, + required: &[Op::ResourceDamage], + forbidden: &[Op::Relocate], + bargain: None, + }, + Candidate { + id: "physical-jump-orbit", + domain: "physical", + scenario: "An unaugmented person on Mars tries to jump into orbit.", + means: "Jump upward using only ordinary musculature.", + desired: "Reach an orbital station.", + target_kind: Place, + component: Occupancy, + admission: Impossible, + required: &[], + forbidden: &[Op::Relocate], + bargain: Some( + "obtain a launch vehicle, teleport permission, or another means with orbital reach", + ), + }, + Candidate { + id: "investigate-panel", + domain: "investigative", + scenario: "A technician inspects a reachable coolant panel with a multimeter.", + means: "Measure the exposed contacts and compare them with the known service range.", + desired: "Learn whether the fault is electrical.", + target_kind: Resource, + component: Knowledge, + admission: Admissible, + required: &[Op::KnowledgeAcquire], + forbidden: &[Op::ResourceRepair], + bargain: None, + }, + Candidate { + id: "investigate-witness", + domain: "investigative", + scenario: "An investigator asks a willing witness what they saw.", + means: "Conduct a private interview through a shared language and channel.", + desired: "Learn the witness's exact account.", + target_kind: Actor, + component: Knowledge, + admission: Admissible, + required: &[Op::KnowledgeCommunicate], + forbidden: &[Op::KnowledgeAcquire], + bargain: None, + }, + Candidate { + id: "investigate-remote-secret", + domain: "investigative", + scenario: "A player announces they know a sealed archive's contents without access or a source.", + means: "Infer the exact contents from vibes while nowhere near the archive.", + desired: "Acquire the archive's private proposition.", + target_kind: Institution, + component: Knowledge, + admission: Impossible, + required: &[], + forbidden: &[Op::KnowledgeAcquire], + bargain: Some( + "gain archive access, recruit a knowledgeable source, or intercept an information channel", + ), + }, + Candidate { + id: "economic-buy-medicine", + domain: "economic", + scenario: "A buyer purchases an exact medicine lot from a willing clinic.", + means: "Exchange the agreed credits while both parties can transfer custody.", + desired: "Receive custody of the medicine lot.", + target_kind: Resource, + component: Custody, + admission: Admissible, + required: &[Op::TransferCustody], + forbidden: &[Op::ResourceCreate], + bargain: None, + }, + Candidate { + id: "economic-split-rations", + domain: "economic", + scenario: "A quartermaster divides a ration lot and gives half to another camp.", + means: "Measure an exact portion from a lot under the quartermaster's custody.", + desired: "Create a conserved sub-lot and transfer it.", + target_kind: Resource, + component: ResourceState, + admission: Admissible, + required: &[Op::ResourceSplit, Op::TransferCustody], + forbidden: &[Op::ResourceCreate], + bargain: None, + }, + Candidate { + id: "economic-spend-absent-credit", + domain: "economic", + scenario: "A debtor tries to pay with credits they do not possess or control.", + means: "Declare that the payment happened.", + desired: "Transfer nonexistent credits.", + target_kind: Resource, + component: Custody, + admission: Impossible, + required: &[], + forbidden: &[Op::TransferCustody, Op::ResourceCreate], + bargain: Some( + "obtain funds, secure a loan, or offer a different admitted consideration", + ), + }, + Candidate { + id: "political-petition", + domain: "political", + scenario: "Residents petition a council to reopen a ration depot.", + means: "Present signatures and testimony through the council's recognized hearing.", + desired: "Shift the council toward reopening the depot.", + target_kind: Institution, + component: Posture, + admission: Admissible, + required: &[Op::PostureChange], + forbidden: &[Op::TransferCustody], + bargain: None, + }, + Candidate { + id: "political-treaty", + domain: "political", + scenario: "Two authorized envoys negotiate a mutual non-aggression obligation.", + means: "Exchange exact terms while holding delegated authority for both institutions.", + desired: "Create reciprocal obligations and alter their relationship.", + target_kind: Institution, + component: Relationship, + admission: Admissible, + required: &[Op::CommitmentCreate, Op::RelationshipAlter], + forbidden: &[Op::PostureChange], + bargain: None, + }, + Candidate { + id: "political-declare-throne", + domain: "political", + scenario: "A visitor declares themselves ruler of an institution whose members reject the claim.", + means: "Announce the title without succession, consent, force, or legal authority.", + desired: "Acquire command posture and custody over the institution.", + target_kind: Institution, + component: Posture, + admission: BargainRequired, + required: &[], + forbidden: &[Op::PostureChange, Op::TransferCustody], + bargain: Some( + "win recognized succession, member consent, or actual control through admitted play", + ), + }, + Candidate { + id: "technology-repair-drone", + domain: "technological", + scenario: "An engineer repairs a damaged drone using the specified replacement actuator.", + means: "Install the compatible actuator with the correct tools and service knowledge.", + desired: "Restore the drone's integrity.", + target_kind: Resource, + component: ResourceState, + admission: Admissible, + required: &[Op::ResourceRepair, Op::ResourceConsume], + forbidden: &[Op::CapabilityGrant], + bargain: None, + }, + Candidate { + id: "technology-fabricate-key", + domain: "technological", + scenario: "A fabricator produces a replacement key from an authorized pattern and feedstock.", + means: "Run the exact pattern on a compatible fabricator while consuming feedstock.", + desired: "Create a key resource under the operator's custody.", + target_kind: Resource, + component: ResourceState, + admission: Admissible, + required: &[Op::ResourceConsume, Op::ResourceCreate], + forbidden: &[Op::AdmitEntity], + bargain: None, + }, + Candidate { + id: "technology-hack-airgap", + domain: "technological", + scenario: "A hacker tries to rewrite an air-gapped computer they cannot physically or electronically reach.", + means: "Type commands into an unrelated terminal with no route to the target.", + desired: "Alter the remote computer.", + target_kind: Resource, + component: Condition, + admission: Impossible, + required: &[], + forbidden: &[Op::ConditionApply, Op::KnowledgeAcquire], + bargain: Some("establish a physical, network, supply-chain, or social access path"), + }, + Candidate { + id: "extraordinary-costly-teleport", + domain: "extraordinary", + scenario: "A permitted teleporter crosses a known route at the accepted cost.", + means: "Invoke the approved teleport capability and pay its exact exposure cost.", + desired: "Relocate to the known destination.", + target_kind: Place, + component: Occupancy, + admission: Admissible, + required: &[Op::Relocate, Op::ConditionApply], + forbidden: &[Op::TopologyAdd], + bargain: None, + }, + Candidate { + id: "extraordinary-heal", + domain: "extraordinary", + scenario: "A healer uses an admitted gift to reduce a present ally's injury within its effect ceiling.", + means: "Touch the consenting ally and invoke the gift while accepting its fatigue cost.", + desired: "Reduce the injury and apply fatigue to the healer.", + target_kind: Actor, + component: Condition, + admission: Admissible, + required: &[Op::ConditionAlter, Op::ConditionApply], + forbidden: &[Op::CapabilityGrant], + bargain: None, + }, + Candidate { + id: "extraordinary-omniscience", + domain: "extraordinary", + scenario: "A novice claims to read every private mind in the setting without such permission.", + means: "Concentrate and assert universal access.", + desired: "Acquire every actor's private knowledge.", + target_kind: Population, + component: Knowledge, + admission: Impossible, + required: &[], + forbidden: &[Op::KnowledgeAcquire], + bargain: Some( + "negotiate a narrowly scoped capability, exact targets, resistance, exposure, and an effect ceiling", + ), + }, + Candidate { + id: "collective-evacuation", + domain: "collective", + scenario: "A village council organizes a voluntary evacuation along an open road.", + means: "Broadcast the plan, assign transport, and coordinate willing households.", + desired: "Move the population to the refuge.", + target_kind: Population, + component: Occupancy, + admission: Admissible, + required: &[Op::Relocate], + forbidden: &[Op::PopulationTransfer], + bargain: None, + }, + Candidate { + id: "collective-strike", + domain: "collective", + scenario: "Workers vote to strike and withdraw labor from their employer.", + means: "Use their recognized assembly and communication channels to coordinate refusal.", + desired: "Change the workforce posture and increase production pressure.", + target_kind: Population, + component: Posture, + admission: Admissible, + required: &[Op::PostureChange, Op::PressureAdvance], + forbidden: &[Op::RelationshipAlter], + bargain: None, + }, + Candidate { + id: "collective-arena-command", + domain: "collective", + scenario: "Rival factions share one low-budget arena cell and the arena proposes a unified decree.", + means: "Speak as if the simulation cell itself were a sovereign actor.", + desired: "Change both rivals' posture as one collective.", + target_kind: Population, + component: Posture, + admission: Impossible, + required: &[], + forbidden: &[Op::PostureChange], + bargain: Some( + "attribute separate proposals to exact constituents or establish a real collective authority", + ), + }, + Candidate { + id: "identity-sable-disclosure", + domain: "identity_privacy", + scenario: "A named refugee tells Ash which personal handle Ash may use.", + means: "Sable speaks the exact self-presentation to Ash in a private, reachable conversation.", + desired: "Disclose Sable's existing handle to Ash without globally renaming the actor.", + target_kind: Actor, + component: Identity, + admission: Admissible, + required: &[Op::IdentityDisclose], + forbidden: &[Op::IdentityAdopt], + bargain: None, + }, + Candidate { + id: "identity-adopt-alias", + domain: "identity_privacy", + scenario: "An actor deliberately adopts a new operational alias for a mission.", + means: "Choose the alias under self-authority and disclose it to the team.", + desired: "Add the alias and reveal it to exact teammates.", + target_kind: Actor, + component: Identity, + admission: Admissible, + required: &[Op::IdentityAdopt, Op::IdentityDisclose], + forbidden: &[Op::KnowledgeAcquire], + bargain: None, + }, + Candidate { + id: "identity-erase-memory", + domain: "identity_privacy", + scenario: "A speaker demands that a witness forget a disclosed secret without memory-altering power.", + means: "Tell the witness to forget what they heard.", + desired: "Remove the witness's private memory and knowledge.", + target_kind: Actor, + component: Knowledge, + admission: Impossible, + required: &[], + forbidden: &[Op::MemoryRetire, Op::KnowledgeInvalidate], + bargain: Some( + "gain consent for a mundane concealment plan or an admitted memory-altering capability with resistance and cost", + ), + }, + Candidate { + id: "population-join-refugees", + domain: "population_migration", + scenario: "A displaced actor is accepted into a refugee population at its current camp.", + means: "Ask to join, receive acceptance, and take on the population's ordinary obligations.", + desired: "Join the population without losing individual identity.", + target_kind: Population, + component: PopulationMembership, + admission: Admissible, + required: &[Op::PopulationJoin], + forbidden: &[Op::AdmitEntity], + bargain: None, + }, + Candidate { + id: "population-dispersal", + domain: "population_migration", + scenario: "A refugee population disperses its exact members among several destination populations.", + means: "Coordinate destination placements and travel for consenting members.", + desired: "Transfer memberships and relocate the exact people.", + target_kind: Population, + component: PopulationMembership, + admission: Admissible, + required: &[Op::PopulationTransfer, Op::Relocate], + forbidden: &[Op::PopulationMerge], + bargain: None, + }, + Candidate { + id: "population-return-callback", + domain: "population_migration", + scenario: "A previously helped refugee later appears after settling in a destination town.", + means: "The same actor travels from their destination population into the player's scene.", + desired: "Relocate the stable actor while preserving identity, memory, and relationship.", + target_kind: Actor, + component: Occupancy, + admission: Admissible, + required: &[Op::Relocate], + forbidden: &[Op::AdmitEntity, Op::IdentityAdopt], + bargain: None, + }, + Candidate { + id: "infrastructure-open-bridge", + domain: "infrastructure_logistics", + scenario: "Engineers finish repairing a bridge and reopen its existing route.", + means: "Complete the admitted repair and pass structural inspection.", + desired: "Restore the bridge resource and open the route.", + target_kind: Place, + component: Topology, + admission: Admissible, + required: &[Op::ResourceRepair, Op::TopologyOpen], + forbidden: &[Op::TopologyAdd], + bargain: None, + }, + Candidate { + id: "infrastructure-reroute-convoy", + domain: "infrastructure_logistics", + scenario: "A convoy uses a known alternate route around a blockade.", + means: "Follow the open alternate road with sufficient fuel and route knowledge.", + desired: "Move the convoy population to the next waypoint.", + target_kind: Population, + component: Occupancy, + admission: Admissible, + required: &[Op::Relocate, Op::ResourceConsume], + forbidden: &[Op::TopologyAdd], + bargain: None, + }, + Candidate { + id: "infrastructure-adjacent-city", + domain: "infrastructure_logistics", + scenario: "A traveler claims a faraway city is adjacent despite persistent topology.", + means: "Walk through an invented shortcut that has never been admitted.", + desired: "Arrive instantly without traversing or changing topology.", + target_kind: Place, + component: Occupancy, + admission: Impossible, + required: &[], + forbidden: &[Op::Relocate, Op::TopologyAdd], + bargain: Some( + "travel along existing routes or compile an evidence-grounded branch-local connection", + ), + }, + Candidate { + id: "medical-treat-wound", + domain: "medical_body", + scenario: "A medic cleans and dresses a present patient's moderate wound with available supplies.", + means: "Use sterile dressing and supported clinical capability with consent.", + desired: "Reduce the wound condition while consuming the dressing.", + target_kind: Actor, + component: Condition, + admission: Admissible, + required: &[Op::ConditionAlter, Op::ResourceConsume], + forbidden: &[Op::ConditionClear], + bargain: None, + }, + Candidate { + id: "medical-administer-dose", + domain: "medical_body", + scenario: "A clinician gives an exact medicine dose from a lot under clinic custody.", + means: "Measure and administer the dose to the consenting patient.", + desired: "Consume the dose and apply its supported treatment condition.", + target_kind: Actor, + component: Condition, + admission: Admissible, + required: &[Op::ResourceSplit, Op::ResourceConsume, Op::ConditionApply], + forbidden: &[Op::ResourceCreate], + bargain: None, + }, + Candidate { + id: "medical-instant-resurrection", + domain: "medical_body", + scenario: "An ordinary first-aider tries to resurrect someone whose death is established.", + means: "Perform basic first aid without an extraordinary permission or viable body state.", + desired: "Retire death and restore life instantly.", + target_kind: Actor, + component: Condition, + admission: Impossible, + required: &[], + forbidden: &[Op::ConditionClear, Op::CapabilityGrant], + bargain: Some( + "establish a setting-supported resurrection capability, prerequisites, costs, opposition, and effect ceiling", + ), + }, + ]; + + candidates.into_iter().map(materialize_candidate).collect() +} + +fn materialize_candidate(candidate: Candidate<'_>) -> AgencyAttemptCase { + let actor = SubjectRef { + kind: SubjectKind::Actor, + id: format!("case:{}:actor", candidate.id), + }; + let target = SubjectRef { + kind: candidate.target_kind, + id: format!("case:{}:target", candidate.id), + }; + AgencyAttemptCase { + schema: "ghostlight.agency_attempt_case.v1".into(), + id: candidate.id.into(), + domain: candidate.domain.into(), + scenario: candidate.scenario.into(), + world_fixture_ids: vec![format!("agency-fixture:{}", candidate.id)], + means: ActionMeans { + schema: "ghostlight.action_means.v1".into(), + actor, + description: candidate.means.into(), + targets: vec![target.clone()], + instruments: vec![], + places: vec![], + route_ids: BTreeSet::new(), + channels: vec![], + speech: None, + state_references: vec![], + }, + intended_effects: vec![MutationIntent { + schema: "ghostlight.mutation_intent.v1".into(), + component: candidate.component, + targets: vec![target], + desired_change: candidate.desired.into(), + }], + expected_admission: candidate.admission, + expected_mutation_operations: candidate.required.iter().copied().collect(), + forbidden_mutation_operations: candidate.forbidden.iter().copied().collect(), + expected_bargains: candidate.bargain.into_iter().map(str::to_owned).collect(), + equivalent_lanes: BTreeSet::from([ + AgencyResolutionLane::Foreground, + AgencyResolutionLane::Npc, + AgencyResolutionLane::StrategicActor, + ]), + invariant_witnesses: vec![ + "means never commits the intended effect".into(), + "the exact authority envelope bounds every committed mutation".into(), + ], + review_status: "candidate".into(), + missing_primitive: None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::persistence::CampaignStore; + + #[test] + fn seed_spans_every_required_domain_and_round_trips_through_cultcache() { + let cases = seed_agency_attempt_cases(); + assert_eq!(cases.len(), 36); + let domains = cases + .iter() + .map(|case| case.domain.as_str()) + .collect::>(); + assert_eq!(domains.len(), 12); + assert!( + cases + .iter() + .any(|case| case.id == "identity-sable-disclosure") + ); + assert!( + cases + .iter() + .any(|case| case.id == "population-return-callback") + ); + + let path = std::env::temp_dir().join(format!( + "ghostlight-agency-corpus-{}.cc", + uuid::Uuid::new_v4().simple() + )); + let store = CampaignStore::open(&path).unwrap(); + for case in &cases { + store + .insert( + "agency_attempt_case.v1", + "ghostlight.agency_attempt_case.v1", + &case.id, + case, + ) + .unwrap(); + } + let loaded = store + .load_all::("agency_attempt_case.v1") + .unwrap(); + assert_eq!(loaded.len(), cases.len()); + drop(store); + std::fs::remove_file(path).unwrap(); + } + + #[test] + fn impossible_cases_never_require_a_committed_mutation() { + for case in seed_agency_attempt_cases() { + if case.expected_admission == AgencyAdmissionExpectation::Impossible { + assert!(case.expected_mutation_operations.is_empty(), "{}", case.id); + assert!( + !case.forbidden_mutation_operations.is_empty(), + "{}", + case.id + ); + } + } + } +} diff --git a/crates/ghostlight-dungeon/src/bin/ghostlight-agency-corpus.rs b/crates/ghostlight-dungeon/src/bin/ghostlight-agency-corpus.rs new file mode 100644 index 0000000..46cc0ab --- /dev/null +++ b/crates/ghostlight-dungeon/src/bin/ghostlight-agency-corpus.rs @@ -0,0 +1,34 @@ +use ghostlight_dungeon::{agency_corpus::seed_agency_attempt_cases, persistence::CampaignStore}; +use std::path::PathBuf; + +fn main() -> anyhow::Result<()> { + let path = std::env::args_os() + .nth(1) + .map(PathBuf::from) + .unwrap_or_else(|| PathBuf::from("target/ghostlight-agency-corpus.cc")); + if path.exists() { + anyhow::bail!( + "agency corpus target already exists; choose a fresh path: {}", + path.display() + ); + } + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent)?; + } + let store = CampaignStore::open(&path)?; + let cases = seed_agency_attempt_cases(); + for case in &cases { + store.insert( + "agency_attempt_case.v1", + "ghostlight.agency_attempt_case.v1", + &case.id, + case, + )?; + } + println!( + "materialized {} typed agency cases at {}", + cases.len(), + path.display() + ); + Ok(()) +} diff --git a/crates/ghostlight-dungeon/src/kernel.rs b/crates/ghostlight-dungeon/src/kernel.rs index 8f8a302..5395e10 100644 --- a/crates/ghostlight-dungeon/src/kernel.rs +++ b/crates/ghostlight-dungeon/src/kernel.rs @@ -63,10 +63,22 @@ pub enum CommandResult { campaign: Campaign, proposal: CellBudgetProposal, }, + MutationCommitted { + state: crate::transition::ComponentWorldState, + receipt: crate::transition::WorldMutationReceipt, + }, +} + +enum KernelInput { + World(WorldCommand), + Mutation { + authority: crate::transition::MutationAuthorityEnvelope, + batch: crate::transition::WorldMutationBatch, + }, } struct Request { - command: WorldCommand, + input: KernelInput, reply: oneshot::Sender>, } @@ -93,7 +105,12 @@ impl WorldKernel { tokio::spawn(async move { let mut assessments = BTreeMap::new(); while let Some(request) = rx.recv().await { - let result = execute(&store, &mut assessments, request.command); + let result = match request.input { + KernelInput::World(command) => execute(&store, &mut assessments, command), + KernelInput::Mutation { authority, batch } => { + execute_mutation_batch(&store, authority, batch) + } + }; let _ = request.reply.send(result); } }); @@ -102,13 +119,62 @@ impl WorldKernel { pub async fn command(&self, command: WorldCommand) -> Result { let (reply, receive) = oneshot::channel(); self.tx - .send(Request { command, reply }) + .send(Request { + input: KernelInput::World(command), + reply, + }) .await .map_err(|_| KernelError::Invalid("kernel stopped".into()))?; receive .await .map_err(|_| KernelError::Invalid("kernel stopped".into()))? } + + pub async fn commit_mutation_batch( + &self, + authority: crate::transition::MutationAuthorityEnvelope, + batch: crate::transition::WorldMutationBatch, + ) -> Result { + let (reply, receive) = oneshot::channel(); + self.tx + .send(Request { + input: KernelInput::Mutation { authority, batch }, + reply, + }) + .await + .map_err(|_| KernelError::Invalid("kernel stopped".into()))?; + receive + .await + .map_err(|_| KernelError::Invalid("kernel stopped".into()))? + } +} + +fn execute_mutation_batch( + store: &CampaignStore, + authority: crate::transition::MutationAuthorityEnvelope, + batch: crate::transition::WorldMutationBatch, +) -> Result { + let key = batch.campaign_id.to_string(); + let (row, state) = store + .load::("component_world_state.v1", &key) + .map_err(persist)? + .ok_or(KernelError::NotFound)?; + let application = + crate::transition::apply_component_world_batch(&state, &authority, &batch, Utc::now()) + .map_err(|error| KernelError::Invalid(error.to_string()))?; + store + .commit_world_mutation_batch( + &row, + &application.state, + &authority, + &batch, + &application.receipt, + ) + .map_err(persist)?; + Ok(CommandResult::MutationCommitted { + state: application.state, + receipt: application.receipt, + }) } fn execute( @@ -3305,6 +3371,135 @@ mod tests { } } + #[tokio::test] + async fn mailbox_commits_one_authorized_component_mutation_batch_atomically() { + use crate::transition::*; + + let dir = tempfile::tempdir().unwrap(); + let store = CampaignStore::open(dir.path().join("campaign.cc")).unwrap(); + let campaign_id = uuid::Uuid::new_v4(); + let campaign_subject = SubjectRef { + kind: SubjectKind::Campaign, + id: campaign_id.to_string(), + }; + let state = ComponentWorldState { + schema: "ghostlight.component_world_state.v1".into(), + campaign_id, + revision: 7, + resolution_epoch: 3, + world_time: Utc::now(), + subjects: BTreeMap::from([( + campaign_subject.clone(), + TypedSubject { + schema: "ghostlight.typed_subject.v1".into(), + subject: campaign_subject.clone(), + lifecycle: LifecycleStatus::Active, + admitted_components: BTreeSet::from([WorldComponentKind::WorldTime]), + version: 7, + }, + )]), + occupancy: BTreeMap::new(), + custody: BTreeMap::new(), + resources: BTreeMap::new(), + capabilities: BTreeMap::new(), + conditions: BTreeMap::new(), + commitments: BTreeMap::new(), + relationships: BTreeMap::new(), + pressures: BTreeMap::new(), + knowledge: BTreeMap::new(), + memories: BTreeMap::new(), + postures: BTreeMap::new(), + memberships: BTreeMap::new(), + population_lineages: BTreeMap::new(), + identities: BTreeMap::new(), + topology: BTreeMap::new(), + }; + let initial_world_time = state.world_time; + store.create_component_world_state(&state).unwrap(); + + let permit = MutationPermit { + id: "permit:time".into(), + operation: WorldMutationOperation::AdvanceWorldTime, + subject_bindings: vec![MutationSubjectBinding { + role: MutationSubjectRole::Subject, + allowed_subjects: BTreeSet::from([campaign_subject.clone()]), + }], + string_constraints: BTreeMap::new(), + integer_bounds: BTreeMap::from([( + MutationIntegerRole::WorldMinutes, + IntegerBounds { + minimum: 30, + maximum: 30, + }, + )]), + maximum_uses: 1, + }; + let mut authority = MutationAuthorityEnvelope { + schema: "ghostlight.mutation_authority_envelope.v1".into(), + id: "authority:time".into(), + campaign_id, + world_revision: 7, + resolution_epoch: Some(3), + procedure: MutationProcedure::Governance, + source_subject: None, + outcome: MutationOutcomeBinding::Deterministic, + effect_ceiling: "Thirty minutes pass; no other component changes.".into(), + permits: vec![permit], + authority_receipt_ids: BTreeSet::from(["governance:time".into()]), + expires_at: Utc::now() + Duration::minutes(5), + digest: String::new(), + }; + authority.digest = envelope_digest(&authority).unwrap(); + let mut batch = WorldMutationBatch { + schema: "ghostlight.world_mutation_batch.v1".into(), + id: "batch:time".into(), + campaign_id, + expected_world_revision: 7, + expected_resolution_epoch: Some(3), + authority_envelope_digest: authority.digest.clone(), + source_receipt_id: "governance:time".into(), + means_digest: None, + intended_effect_digest: None, + mutations: vec![PermittedWorldMutation { + permit_id: "permit:time".into(), + mutation: WorldMutation::AdvanceWorldTime { + campaign: campaign_subject, + minutes: 30, + }, + }], + digest: String::new(), + }; + batch.digest = mutation_batch_digest(&batch).unwrap(); + + let kernel = WorldKernel::start(store.clone()); + let committed = kernel + .commit_mutation_batch(authority.clone(), batch.clone()) + .await + .unwrap(); + let CommandResult::MutationCommitted { state, receipt } = committed else { + panic!("component mutation did not return its typed receipt"); + }; + assert_eq!(state.revision, 8); + assert_eq!(state.world_time, initial_world_time + Duration::minutes(30)); + assert_eq!(receipt.previous_world_revision, 7); + assert_eq!(receipt.world_revision, 8); + assert_eq!(store.keys("world_mutation_receipt.v1").unwrap().len(), 1); + + assert!( + kernel + .commit_mutation_batch(authority, batch) + .await + .is_err() + ); + let stored = store + .load::("component_world_state.v1", &campaign_id.to_string()) + .unwrap() + .unwrap() + .1; + assert_eq!(stored.revision, 8); + assert_eq!(store.keys("world_mutation_receipt.v1").unwrap().len(), 1); + } + fn hierarchical_refugee_campaign() -> Campaign { let mut value = campaign(); value.locations.insert( diff --git a/crates/ghostlight-dungeon/src/lib.rs b/crates/ghostlight-dungeon/src/lib.rs index 81865d0..d7e8915 100644 --- a/crates/ghostlight-dungeon/src/lib.rs +++ b/crates/ghostlight-dungeon/src/lib.rs @@ -1,5 +1,6 @@ #![recursion_limit = "256"] +pub mod agency_corpus; pub mod assessor; pub mod compiler; pub mod d20; @@ -19,6 +20,7 @@ pub mod resolution; pub mod scheduler; pub mod session_zero; pub mod surface; +pub mod transition; pub mod turn; pub mod vault; #[cfg(windows)] diff --git a/crates/ghostlight-dungeon/src/main.rs b/crates/ghostlight-dungeon/src/main.rs index 998b6e2..8e44850 100644 --- a/crates/ghostlight-dungeon/src/main.rs +++ b/crates/ghostlight-dungeon/src/main.rs @@ -4915,6 +4915,11 @@ fn player_command_projection( "kind":"resolution_governance_pending", "proposal":proposal, }), + CommandResult::MutationCommitted { receipt, .. } => serde_json::json!({ + "kind":"mutation_committed", + "revision":receipt.world_revision, + "receipt":receipt, + }), } } diff --git a/crates/ghostlight-dungeon/src/mesh.rs b/crates/ghostlight-dungeon/src/mesh.rs index 1e1bfbc..6e6f5bd 100644 --- a/crates/ghostlight-dungeon/src/mesh.rs +++ b/crates/ghostlight-dungeon/src/mesh.rs @@ -20,6 +20,11 @@ use crate::{ SessionZeroMessage, SessionZeroState, TimeAdvanceProposal, }, surface::{operator_surface, player_surface, player_surface_for_actor}, + transition::{ + ActionMeans, AgencyAttemptCase, ComponentWorldState, IdentityHandleState, + MutationAuthorityEnvelope, MutationIntent, MutationPermit, TypedSubject, WorldComponentRef, + WorldMutation, WorldMutationBatch, WorldMutationReceipt, + }, }; use anyhow::Result; use chrono::Utc; @@ -631,7 +636,19 @@ fn schema_catalog() -> Value { "ghostlight.extraordinary_permission.v1": schemars::schema_for!(ExtraordinaryPermission), "ghostlight.time_advance_proposal.v1": schemars::schema_for!(TimeAdvanceProposal), "ghostlight.group_travel_proposal.v1": schemars::schema_for!(GroupTravelProposal), - "ghostlight.cell_budget_proposal.v1": schemars::schema_for!(CellBudgetProposal) + "ghostlight.cell_budget_proposal.v1": schemars::schema_for!(CellBudgetProposal), + "ghostlight.typed_subject.v1": schemars::schema_for!(TypedSubject), + "ghostlight.world_component_ref.v1": schemars::schema_for!(WorldComponentRef), + "ghostlight.action_means.v1": schemars::schema_for!(ActionMeans), + "ghostlight.mutation_intent.v1": schemars::schema_for!(MutationIntent), + "ghostlight.mutation_permit.v1": schemars::schema_for!(MutationPermit), + "ghostlight.mutation_authority_envelope.v1": schemars::schema_for!(MutationAuthorityEnvelope), + "ghostlight.world_mutation.v1": schemars::schema_for!(WorldMutation), + "ghostlight.world_mutation_batch.v1": schemars::schema_for!(WorldMutationBatch), + "ghostlight.world_mutation_receipt.v1": schemars::schema_for!(WorldMutationReceipt), + "ghostlight.component_world_state.v1": schemars::schema_for!(ComponentWorldState), + "ghostlight.identity_handle.v1": schemars::schema_for!(IdentityHandleState), + "ghostlight.agency_attempt_case.v1": schemars::schema_for!(AgencyAttemptCase) }); let schemas = catalog .as_object_mut() diff --git a/crates/ghostlight-dungeon/src/persistence.rs b/crates/ghostlight-dungeon/src/persistence.rs index 5cae32f..87f3c4b 100644 --- a/crates/ghostlight-dungeon/src/persistence.rs +++ b/crates/ghostlight-dungeon/src/persistence.rs @@ -6,6 +6,9 @@ use crate::domain::{ use crate::session_zero::{ CellBudgetProposal, GroupTravelProposal, PublishedSessionZeroSeed, TimeAdvanceProposal, }; +use crate::transition::{ + ComponentWorldState, MutationAuthorityEnvelope, WorldMutationBatch, WorldMutationReceipt, +}; use anyhow::{Context, Result, anyhow}; use chrono::Utc; use cultcache_legacy::{CacheBackingStore, CultCacheEnvelope, OwnedRedbMessagePackBackingStore}; @@ -81,6 +84,74 @@ impl CampaignStore { Ok(row) } + pub fn create_component_world_state( + &self, + state: &ComponentWorldState, + ) -> Result { + self.insert( + "component_world_state.v1", + "ghostlight.component_world_state.v1", + &state.campaign_id.to_string(), + state, + ) + } + + pub fn commit_world_mutation_batch( + &self, + expected: &CultCacheEnvelope, + next: &ComponentWorldState, + authority: &MutationAuthorityEnvelope, + batch: &WorldMutationBatch, + receipt: &WorldMutationReceipt, + ) -> Result { + if expected.r#type != "component_world_state.v1" + || expected.key != next.campaign_id.to_string() + || authority.campaign_id != next.campaign_id + || batch.campaign_id != next.campaign_id + || receipt.campaign_id != next.campaign_id + || receipt.world_revision != next.revision + || receipt.previous_world_revision.saturating_add(1) != next.revision + || receipt.batch_digest != batch.digest + || receipt.authority_envelope_digest != authority.digest + { + return Err(anyhow!("world mutation persistence bundle is inconsistent")); + } + let next_row = envelope( + "component_world_state.v1", + "ghostlight.component_world_state.v1", + &next.campaign_id.to_string(), + next, + )?; + let rows = vec![ + next_row.clone(), + envelope( + "mutation_authority_envelope.v1", + "ghostlight.mutation_authority_envelope.v1", + &authority.id, + authority, + )?, + envelope( + "world_mutation_batch.v1", + "ghostlight.world_mutation_batch.v1", + &batch.id, + batch, + )?, + envelope( + "world_mutation_receipt.v1", + "ghostlight.world_mutation_receipt.v1", + &receipt.id, + receipt, + )?, + ]; + if !self + .inner + .compare_and_swap_batch(std::slice::from_ref(expected), rows)? + { + return Err(anyhow!("stale component world snapshot")); + } + Ok(next_row) + } + pub fn create_campaign( &self, campaign: &Campaign, diff --git a/crates/ghostlight-dungeon/src/transition.rs b/crates/ghostlight-dungeon/src/transition.rs new file mode 100644 index 0000000..0c3eb85 --- /dev/null +++ b/crates/ghostlight-dungeon/src/transition.rs @@ -0,0 +1,4326 @@ +use crate::domain::{OutcomeBand, StrategicOutcomeBand}; +use anyhow::{Result, anyhow}; +use chrono::{DateTime, Utc}; +use schemars::{JsonSchema, schema_for}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; +use std::collections::{BTreeMap, BTreeSet}; +use uuid::Uuid; + +#[derive( + Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord, Hash, +)] +#[serde(rename_all = "snake_case")] +pub enum SubjectKind { + Campaign, + Actor, + Population, + Institution, + Place, + Resource, + Pressure, + Proposition, + Channel, +} + +#[derive( + Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord, Hash, +)] +pub struct SubjectRef { + pub kind: SubjectKind, + pub id: String, +} + +#[derive( + Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord, Hash, +)] +#[serde(rename_all = "snake_case")] +pub enum WorldComponentKind { + Identity, + Occupancy, + Custody, + ResourceState, + Capability, + Condition, + Knowledge, + Memory, + Relationship, + Commitment, + Pressure, + Posture, + PopulationMembership, + PopulationLineage, + Topology, + Lifecycle, + WorldTime, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct WorldComponentRef { + pub subject: SubjectRef, + pub component: WorldComponentKind, + pub entry_id: Option, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct ActionMeans { + pub schema: String, + pub actor: SubjectRef, + #[schemars(length(min = 1, max = 4000))] + pub description: String, + #[serde(default)] + pub targets: Vec, + #[serde(default)] + pub instruments: Vec, + #[serde(default)] + pub places: Vec, + #[serde(default)] + pub route_ids: BTreeSet, + #[serde(default)] + pub channels: Vec, + #[serde(default)] + #[schemars(length(max = 4000))] + pub speech: Option, + #[serde(default)] + pub state_references: Vec, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct MutationIntent { + pub schema: String, + pub component: WorldComponentKind, + #[serde(default)] + pub targets: Vec, + #[schemars(length(min = 1, max = 1000))] + pub desired_change: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum MutationProcedure { + ForegroundAttempt, + NpcAttempt, + ReactionAppraisal, + StrategicOutcome, + Governance, + CompilerAdmission, + Lifecycle, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(tag = "type", content = "value", rename_all = "snake_case")] +pub enum MutationOutcomeBinding { + Foreground(OutcomeBand), + Strategic(StrategicOutcomeBand), + Deterministic, +} + +#[derive( + Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord, Hash, +)] +#[serde(rename_all = "snake_case")] +pub enum WorldMutationOperation { + Relocate, + TransferCustody, + ResourceCreate, + ResourceTransform, + ResourceConsume, + ResourceDamage, + ResourceRepair, + ResourceSplit, + ResourceCombine, + CapabilityGrant, + CapabilityAlter, + CapabilitySuspend, + CapabilityRetire, + ConditionApply, + ConditionAlter, + ConditionClear, + CommitmentCreate, + CommitmentAlter, + CommitmentFulfill, + CommitmentDefault, + CommitmentRetire, + RelationshipCreate, + RelationshipAlter, + RelationshipRetire, + PressureCreate, + PressureAdvance, + PressureReduce, + PressureResolve, + PressureRetire, + KnowledgeAcquire, + KnowledgeCommunicate, + KnowledgeConceal, + KnowledgeCorrect, + KnowledgeInvalidate, + MemoryRecord, + MemoryRevise, + MemoryRetire, + PostureChange, + PopulationJoin, + PopulationLeave, + PopulationTransfer, + PopulationSplit, + PopulationMerge, + IdentityAdopt, + IdentityDisclose, + IdentityRestrict, + IdentityRetire, + TopologyAdd, + TopologyAlter, + TopologyOpen, + TopologyClose, + TopologyRetire, + AdmitEntity, + RetireEntity, + AdvanceWorldTime, +} + +#[derive( + Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord, Hash, +)] +#[serde(rename_all = "snake_case")] +pub enum MutationSubjectRole { + Subject, + Actor, + Owner, + Counterparty, + OriginPlace, + DestinationPlace, + Resource, + RelatedResource, + SourceCustodian, + DestinationCustodian, + Pressure, + Knower, + Speaker, + Recipient, + Proposition, + Channel, + SourcePopulation, + DestinationPopulation, + ParentPopulation, + ChildPopulation, + RelationshipSource, + RelationshipTarget, + TopologyOrigin, + TopologyDestination, + Entity, +} + +#[derive( + Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord, Hash, +)] +#[serde(rename_all = "snake_case")] +pub enum MutationStringRole { + RouteId, + ResourceKind, + RecipeId, + CapabilityId, + ConditionId, + CommitmentId, + RelationshipId, + MemoryId, + EventId, + PressureLabel, + Posture, + IdentityHandleId, + IdentityHandleValue, + TopologyEdgeId, + AdmissionReceiptId, +} + +#[derive( + Clone, Copy, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord, Hash, +)] +#[serde(rename_all = "snake_case")] +pub enum MutationIntegerRole { + Quantity, + Integrity, + Severity, + PressureAmount, + TravelMinutes, + WorldMinutes, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct IntegerBounds { + pub minimum: i64, + pub maximum: i64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct StringConstraint { + #[serde(default)] + pub allowed_values: BTreeSet, + #[schemars(range(min = 1))] + pub minimum_length: u16, + #[schemars(range(min = 1))] + pub maximum_length: u16, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct MutationSubjectBinding { + pub role: MutationSubjectRole, + pub allowed_subjects: BTreeSet, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct MutationPermit { + pub id: String, + pub operation: WorldMutationOperation, + pub subject_bindings: Vec, + #[serde(default)] + pub string_constraints: BTreeMap, + #[serde(default)] + pub integer_bounds: BTreeMap, + #[schemars(range(min = 1))] + pub maximum_uses: u16, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct MutationAuthorityEnvelope { + pub schema: String, + pub id: String, + pub campaign_id: Uuid, + pub world_revision: u64, + pub resolution_epoch: Option, + pub procedure: MutationProcedure, + pub source_subject: Option, + pub outcome: MutationOutcomeBinding, + #[schemars(length(min = 1, max = 1000))] + pub effect_ceiling: String, + pub permits: Vec, + #[serde(default)] + pub authority_receipt_ids: BTreeSet, + pub expires_at: DateTime, + pub digest: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum ResourceMutationOperation { + Create, + Transform, + Consume, + Damage, + Repair, + Split, + Combine, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum CapabilityMutationOperation { + Grant, + Alter, + Suspend, + Retire, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum ConditionMutationOperation { + Apply, + Alter, + Clear, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum CommitmentKind { + Goal, + Obligation, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum CommitmentMutationOperation { + Create, + Alter, + Fulfill, + Default, + Retire, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum RelationshipMutationOperation { + Create, + Alter, + Retire, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum PressureMutationOperation { + Create, + Advance, + Reduce, + Resolve, + Retire, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum KnowledgeMutationOperation { + Acquire, + Communicate, + Conceal, + Correct, + Invalidate, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum MemoryMutationOperation { + Record, + Revise, + Retire, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum PopulationMembershipOperation { + Join, + Leave, + Transfer, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum PopulationLineageOperation { + Split, + Merge, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum IdentityMutationOperation { + Adopt, + Disclose, + Restrict, + Retire, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum TopologyMutationOperation { + Add, + Alter, + Open, + Close, + Retire, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(tag = "type", rename_all = "snake_case")] +pub enum WorldMutation { + Relocate { + subject: SubjectRef, + from_place: SubjectRef, + to_place: SubjectRef, + route_id: String, + }, + TransferCustody { + resource: SubjectRef, + from_custodian: SubjectRef, + to_custodian: SubjectRef, + }, + MutateResource { + resource: SubjectRef, + operation: ResourceMutationOperation, + custodian: Option, + #[serde(default)] + related_resources: Vec, + resource_kind: Option, + recipe_id: Option, + quantity: Option, + integrity: Option, + }, + ChangeCapability { + subject: SubjectRef, + operation: CapabilityMutationOperation, + capability_id: String, + #[schemars(length(max = 1000))] + description: Option, + }, + ChangeCondition { + subject: SubjectRef, + operation: ConditionMutationOperation, + condition_id: String, + #[schemars(length(max = 1000))] + description: Option, + severity: Option, + }, + ChangeCommitment { + subject: SubjectRef, + operation: CommitmentMutationOperation, + kind: CommitmentKind, + commitment_id: String, + counterparty: Option, + #[schemars(length(max = 1000))] + description: Option, + }, + ChangeRelationship { + source: SubjectRef, + target: SubjectRef, + operation: RelationshipMutationOperation, + relationship_id: String, + #[schemars(length(max = 1000))] + description: Option, + }, + ChangePressure { + pressure: SubjectRef, + owner: SubjectRef, + operation: PressureMutationOperation, + amount: Option, + #[schemars(length(max = 240))] + label: Option, + }, + ChangeKnowledge { + operation: KnowledgeMutationOperation, + proposition: SubjectRef, + knower: Option, + speaker: Option, + #[serde(default)] + recipients: Vec, + channel: Option, + }, + ChangeMemory { + subject: SubjectRef, + operation: MemoryMutationOperation, + memory_id: String, + event_id: Option, + #[schemars(length(max = 1000))] + summary: Option, + }, + ChangePosture { + subject: SubjectRef, + posture: String, + }, + ChangePopulationMembership { + actor: SubjectRef, + operation: PopulationMembershipOperation, + source_population: Option, + destination_population: Option, + }, + ChangePopulationLineage { + operation: PopulationLineageOperation, + #[serde(default)] + parent_populations: Vec, + #[serde(default)] + child_populations: Vec, + remainder_population: Option, + }, + ChangeIdentity { + subject: SubjectRef, + operation: IdentityMutationOperation, + handle_id: String, + handle_value: Option, + #[serde(default)] + audience: Vec, + }, + ChangeTopology { + operation: TopologyMutationOperation, + edge_id: String, + from_place: SubjectRef, + to_place: SubjectRef, + travel_minutes: Option, + }, + AdmitEntity { + subject: SubjectRef, + initial_components: BTreeSet, + admission_receipt_id: String, + }, + RetireEntity { + subject: SubjectRef, + #[schemars(length(min = 1, max = 1000))] + reason: String, + }, + AdvanceWorldTime { + campaign: SubjectRef, + minutes: i64, + }, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct PermittedWorldMutation { + pub permit_id: String, + pub mutation: WorldMutation, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct WorldMutationBatch { + pub schema: String, + pub id: String, + pub campaign_id: Uuid, + pub expected_world_revision: u64, + pub expected_resolution_epoch: Option, + pub authority_envelope_digest: String, + pub source_receipt_id: String, + pub means_digest: Option, + pub intended_effect_digest: Option, + #[serde(default)] + pub mutations: Vec, + pub digest: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct ComponentVersionChange { + pub component: WorldComponentRef, + pub previous_version: Option, + pub version: Option, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct WorldMutationReceipt { + pub schema: String, + pub id: String, + pub campaign_id: Uuid, + pub batch_digest: String, + pub authority_envelope_digest: String, + pub previous_world_revision: u64, + pub world_revision: u64, + pub mutation_digests: Vec, + pub component_versions: Vec, + pub committed_at: DateTime, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct IdentityHandleState { + pub schema: String, + pub id: String, + pub subject: SubjectRef, + pub value: String, + pub active: bool, + #[serde(default)] + pub known_by: BTreeSet, + #[serde(default)] + pub restricted_to: BTreeSet, + pub source_revision: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum LifecycleStatus { + Admitted, + Active, + Consumed, + Retired, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct TypedSubject { + pub schema: String, + pub subject: SubjectRef, + pub lifecycle: LifecycleStatus, + #[serde(default)] + pub admitted_components: BTreeSet, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct ResourceComponentState { + pub schema: String, + pub resource: SubjectRef, + pub resource_kind: String, + pub quantity: i64, + pub integrity: i64, + #[serde(default)] + pub qualities: BTreeSet, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord)] +pub struct SubjectEntryKey { + pub subject: SubjectRef, + pub entry_id: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct CapabilityComponentState { + pub description: String, + pub suspended: bool, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct ConditionComponentState { + pub description: String, + pub severity: Option, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct CommitmentComponentState { + pub kind: CommitmentKind, + pub description: String, + pub counterparty: Option, + pub status: String, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct RelationshipComponentState { + pub source: SubjectRef, + pub target: SubjectRef, + pub description: String, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct PressureComponentState { + pub pressure: SubjectRef, + pub owner: SubjectRef, + pub label: String, + pub progress: i64, + pub threshold: i64, + pub resolved: bool, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord)] +pub struct KnowledgeKey { + pub knower: SubjectRef, + pub proposition: SubjectRef, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct KnowledgeComponentState { + pub status: String, + pub source: Option, + pub channel: Option, + #[serde(default)] + pub concealed_from: BTreeSet, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct MemoryComponentState { + pub event_id: Option, + pub summary: String, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord)] +pub struct MembershipKey { + pub actor: SubjectRef, + pub population: SubjectRef, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct PopulationMembershipState { + pub active: bool, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct PopulationLineageState { + pub id: String, + pub operation: PopulationLineageOperation, + pub parent_populations: Vec, + pub child_populations: Vec, + pub remainder_population: Option, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct TopologyComponentState { + pub id: String, + pub from_place: SubjectRef, + pub to_place: SubjectRef, + pub travel_minutes: i64, + pub open: bool, + pub version: u64, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct ComponentWorldState { + pub schema: String, + pub campaign_id: Uuid, + pub revision: u64, + pub resolution_epoch: u64, + pub world_time: DateTime, + pub subjects: BTreeMap, + pub occupancy: BTreeMap, + pub custody: BTreeMap, + pub resources: BTreeMap, + pub capabilities: BTreeMap, + pub conditions: BTreeMap, + pub commitments: BTreeMap, + pub relationships: BTreeMap, + pub pressures: BTreeMap, + pub knowledge: BTreeMap, + pub memories: BTreeMap, + pub postures: BTreeMap, + pub memberships: BTreeMap, + pub population_lineages: BTreeMap, + pub identities: BTreeMap, + pub topology: BTreeMap, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct MutationApplication { + pub state: ComponentWorldState, + pub receipt: WorldMutationReceipt, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum AgencyAdmissionExpectation { + Admissible, + Impossible, + BargainRequired, + MissingPrimitive, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, PartialOrd, Ord)] +#[serde(rename_all = "snake_case")] +pub enum AgencyResolutionLane { + Foreground, + Npc, + Reaction, + StrategicActor, + StrategicPopulation, + StrategicInstitution, + ArenaConstituent, +} + +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] +pub struct AgencyAttemptCase { + pub schema: String, + pub id: String, + pub domain: String, + pub scenario: String, + pub world_fixture_ids: Vec, + pub means: ActionMeans, + pub intended_effects: Vec, + pub expected_admission: AgencyAdmissionExpectation, + #[serde(default)] + pub expected_mutation_operations: BTreeSet, + #[serde(default)] + pub forbidden_mutation_operations: BTreeSet, + #[serde(default)] + pub expected_bargains: Vec, + #[serde(default)] + pub equivalent_lanes: BTreeSet, + #[serde(default)] + pub invariant_witnesses: Vec, + pub review_status: String, + pub missing_primitive: Option, +} + +pub fn envelope_digest(envelope: &MutationAuthorityEnvelope) -> Result { + let mut value = envelope.clone(); + value.digest.clear(); + Ok(sha256(&rmp_serde::to_vec_named(&value)?)) +} + +pub fn mutation_batch_digest(batch: &WorldMutationBatch) -> Result { + let mut value = batch.clone(); + value.digest.clear(); + Ok(sha256(&rmp_serde::to_vec_named(&value)?)) +} + +pub fn mutation_digest(mutation: &PermittedWorldMutation) -> Result { + Ok(sha256(&rmp_serde::to_vec_named(mutation)?)) +} + +fn sha256(bytes: &[u8]) -> String { + format!("sha256:{:x}", Sha256::digest(bytes)) +} + +pub fn apply_component_world_batch( + state: &ComponentWorldState, + envelope: &MutationAuthorityEnvelope, + batch: &WorldMutationBatch, + now: DateTime, +) -> Result { + validate_batch_structure(envelope, batch, now)?; + if state.campaign_id != batch.campaign_id + || state.revision != batch.expected_world_revision + || batch + .expected_resolution_epoch + .is_some_and(|epoch| state.resolution_epoch != epoch) + { + return Err(anyhow!( + "component world state does not match the mutation snapshot" + )); + } + + let mut next = state.clone(); + for proposed in &batch.mutations { + apply_component_mutation(&mut next, &proposed.mutation)?; + } + validate_component_world(&next)?; + + let previous_revision = state.revision; + next.revision = next.revision.saturating_add(1); + let mutation_digests = batch + .mutations + .iter() + .map(mutation_digest) + .collect::>>()?; + let component_versions = batch + .mutations + .iter() + .flat_map(|proposed| mutation_component_refs(&proposed.mutation)) + .collect::>() + .into_iter() + .map(|(subject, component, entry_id)| ComponentVersionChange { + component: WorldComponentRef { + subject, + component, + entry_id, + version: next.revision, + }, + previous_version: Some(previous_revision), + version: Some(next.revision), + }) + .collect(); + Ok(MutationApplication { + state: next, + receipt: WorldMutationReceipt { + schema: "ghostlight.world_mutation_receipt.v1".into(), + id: format!("mutation:{}", batch.id), + campaign_id: batch.campaign_id, + batch_digest: batch.digest.clone(), + authority_envelope_digest: envelope.digest.clone(), + previous_world_revision: previous_revision, + world_revision: previous_revision.saturating_add(1), + mutation_digests, + component_versions, + committed_at: now, + }, + }) +} + +fn active_subject<'a>( + state: &'a ComponentWorldState, + subject: &SubjectRef, +) -> Result<&'a TypedSubject> { + state + .subjects + .get(subject) + .filter(|record| { + matches!( + record.lifecycle, + LifecycleStatus::Admitted | LifecycleStatus::Active + ) + }) + .ok_or_else(|| anyhow!("mutation subject is absent or retired: {}", subject.id)) +} + +fn require_kind(subject: &SubjectRef, kind: SubjectKind) -> Result<()> { + if subject.kind != kind { + return Err(anyhow!( + "subject {} has kind {:?}, expected {:?}", + subject.id, + subject.kind, + kind + )); + } + Ok(()) +} + +fn next_component_version(state: &ComponentWorldState) -> u64 { + state.revision.saturating_add(1) +} + +fn apply_component_mutation( + state: &mut ComponentWorldState, + mutation: &WorldMutation, +) -> Result<()> { + use WorldMutation::*; + match mutation { + Relocate { + subject, + from_place, + to_place, + .. + } => { + active_subject(state, subject)?; + active_subject(state, from_place)?; + active_subject(state, to_place)?; + require_kind(from_place, SubjectKind::Place)?; + require_kind(to_place, SubjectKind::Place)?; + if state.occupancy.get(subject) != Some(from_place) { + return Err(anyhow!( + "relocation origin does not match canonical occupancy" + )); + } + state.occupancy.insert(subject.clone(), to_place.clone()); + } + TransferCustody { + resource, + from_custodian, + to_custodian, + } => { + active_subject(state, resource)?; + active_subject(state, from_custodian)?; + active_subject(state, to_custodian)?; + require_kind(resource, SubjectKind::Resource)?; + if state.custody.get(resource) != Some(from_custodian) { + return Err(anyhow!("custody source does not own the exact resource")); + } + state.custody.insert(resource.clone(), to_custodian.clone()); + } + MutateResource { + resource, + operation, + custodian, + related_resources, + resource_kind, + recipe_id, + quantity, + integrity, + } => apply_resource_mutation( + state, + resource, + operation, + custodian.as_ref(), + related_resources, + resource_kind.as_deref(), + recipe_id.as_deref(), + *quantity, + *integrity, + )?, + ChangeCapability { + subject, + operation, + capability_id, + description, + } => { + active_subject(state, subject)?; + let key = SubjectEntryKey { + subject: subject.clone(), + entry_id: capability_id.clone(), + }; + let version = next_component_version(state); + match operation { + CapabilityMutationOperation::Grant => { + if state.capabilities.contains_key(&key) { + return Err(anyhow!("capability already exists")); + } + state.capabilities.insert( + key, + CapabilityComponentState { + description: description + .clone() + .unwrap_or_else(|| capability_id.clone()), + suspended: false, + version, + }, + ); + } + CapabilityMutationOperation::Alter => { + let value = state + .capabilities + .get_mut(&key) + .ok_or_else(|| anyhow!("capability does not exist"))?; + value.description = description + .clone() + .ok_or_else(|| anyhow!("capability alteration lacks a description"))?; + value.version = version; + } + CapabilityMutationOperation::Suspend => { + let value = state + .capabilities + .get_mut(&key) + .ok_or_else(|| anyhow!("capability does not exist"))?; + value.suspended = true; + value.version = version; + } + CapabilityMutationOperation::Retire => { + if state.capabilities.remove(&key).is_none() { + return Err(anyhow!("capability does not exist")); + } + } + } + } + ChangeCondition { + subject, + operation, + condition_id, + description, + severity, + } => { + active_subject(state, subject)?; + let key = SubjectEntryKey { + subject: subject.clone(), + entry_id: condition_id.clone(), + }; + let version = next_component_version(state); + match operation { + ConditionMutationOperation::Apply => { + if state.conditions.contains_key(&key) { + return Err(anyhow!("condition already exists")); + } + state.conditions.insert( + key, + ConditionComponentState { + description: description + .clone() + .unwrap_or_else(|| condition_id.clone()), + severity: *severity, + version, + }, + ); + } + ConditionMutationOperation::Alter => { + let value = state + .conditions + .get_mut(&key) + .ok_or_else(|| anyhow!("condition does not exist"))?; + if let Some(description) = description { + value.description = description.clone(); + } + if severity.is_some() { + value.severity = *severity; + } + value.version = version; + } + ConditionMutationOperation::Clear => { + if state.conditions.remove(&key).is_none() { + return Err(anyhow!("condition does not exist")); + } + } + } + } + ChangeCommitment { + subject, + operation, + kind, + commitment_id, + counterparty, + description, + } => apply_commitment_mutation( + state, + subject, + operation, + kind, + commitment_id, + counterparty.as_ref(), + description.as_deref(), + )?, + ChangeRelationship { + source, + target, + operation, + relationship_id, + description, + } => apply_relationship_mutation( + state, + source, + target, + operation, + relationship_id, + description.as_deref(), + )?, + ChangePressure { + pressure, + owner, + operation, + amount, + label, + } => apply_pressure_mutation(state, pressure, owner, operation, *amount, label.as_deref())?, + ChangeKnowledge { + operation, + proposition, + knower, + speaker, + recipients, + channel, + } => apply_knowledge_mutation( + state, + operation, + proposition, + knower.as_ref(), + speaker.as_ref(), + recipients, + channel.as_ref(), + )?, + ChangeMemory { + subject, + operation, + memory_id, + event_id, + summary, + } => apply_memory_mutation( + state, + subject, + operation, + memory_id, + event_id.as_deref(), + summary.as_deref(), + )?, + ChangePosture { subject, posture } => { + active_subject(state, subject)?; + state.postures.insert(subject.clone(), posture.clone()); + } + ChangePopulationMembership { + actor, + operation, + source_population, + destination_population, + } => apply_membership_mutation( + state, + actor, + operation, + source_population.as_ref(), + destination_population.as_ref(), + )?, + ChangePopulationLineage { + operation, + parent_populations, + child_populations, + remainder_population, + } => apply_lineage_mutation( + state, + operation, + parent_populations, + child_populations, + remainder_population.as_ref(), + )?, + ChangeIdentity { + subject, + operation, + handle_id, + handle_value, + audience, + } => apply_identity_mutation( + state, + subject, + operation, + handle_id, + handle_value.as_deref(), + audience, + )?, + ChangeTopology { + operation, + edge_id, + from_place, + to_place, + travel_minutes, + } => apply_topology_mutation( + state, + operation, + edge_id, + from_place, + to_place, + *travel_minutes, + )?, + AdmitEntity { + subject, + initial_components, + .. + } => { + if subject.kind == SubjectKind::Resource { + return Err(anyhow!("resources are admitted by resource creation")); + } + if state.subjects.contains_key(subject) { + return Err(anyhow!("entity already exists")); + } + state.subjects.insert( + subject.clone(), + TypedSubject { + schema: "ghostlight.typed_subject.v1".into(), + subject: subject.clone(), + lifecycle: LifecycleStatus::Admitted, + admitted_components: initial_components.clone(), + version: next_component_version(state), + }, + ); + } + RetireEntity { subject, .. } => retire_entity(state, subject)?, + AdvanceWorldTime { campaign, minutes } => { + require_kind(campaign, SubjectKind::Campaign)?; + active_subject(state, campaign)?; + state.world_time += chrono::Duration::minutes(*minutes); + } + } + Ok(()) +} + +#[allow(clippy::too_many_arguments)] +fn apply_resource_mutation( + state: &mut ComponentWorldState, + resource: &SubjectRef, + operation: &ResourceMutationOperation, + custodian: Option<&SubjectRef>, + related_resources: &[SubjectRef], + resource_kind: Option<&str>, + recipe_id: Option<&str>, + quantity: Option, + integrity: Option, +) -> Result<()> { + require_kind(resource, SubjectKind::Resource)?; + let version = next_component_version(state); + match operation { + ResourceMutationOperation::Create => { + if state.subjects.contains_key(resource) || state.resources.contains_key(resource) { + return Err(anyhow!("resource already exists")); + } + let custodian = custodian.ok_or_else(|| anyhow!("resource creation lacks custody"))?; + active_subject(state, custodian)?; + let quantity = quantity.ok_or_else(|| anyhow!("resource creation lacks quantity"))?; + if quantity <= 0 { + return Err(anyhow!("resource quantity must be positive")); + } + let resource_kind = resource_kind + .filter(|value| !value.trim().is_empty()) + .ok_or_else(|| anyhow!("resource creation lacks a kind"))?; + state.subjects.insert( + resource.clone(), + TypedSubject { + schema: "ghostlight.typed_subject.v1".into(), + subject: resource.clone(), + lifecycle: LifecycleStatus::Active, + admitted_components: BTreeSet::from([ + WorldComponentKind::ResourceState, + WorldComponentKind::Custody, + WorldComponentKind::Lifecycle, + ]), + version, + }, + ); + state.resources.insert( + resource.clone(), + ResourceComponentState { + schema: "ghostlight.resource_component.v1".into(), + resource: resource.clone(), + resource_kind: resource_kind.into(), + quantity, + integrity: integrity.unwrap_or(100), + qualities: BTreeSet::new(), + version, + }, + ); + state.custody.insert(resource.clone(), custodian.clone()); + } + ResourceMutationOperation::Transform => { + let _recipe = + recipe_id.ok_or_else(|| anyhow!("resource transformation lacks recipe"))?; + let owner = state + .custody + .get(resource) + .cloned() + .ok_or_else(|| anyhow!("resource has no custodian"))?; + for input in related_resources { + active_subject(state, input)?; + require_kind(input, SubjectKind::Resource)?; + if state.custody.get(input) != Some(&owner) { + return Err(anyhow!("resource transformation crosses custody")); + } + } + for input in related_resources { + consume_entire_resource(state, input)?; + } + let value = state + .resources + .get_mut(resource) + .ok_or_else(|| anyhow!("resource does not exist"))?; + if let Some(kind) = resource_kind { + value.resource_kind = kind.into(); + } + if quantity.is_some_and(|quantity| quantity != value.quantity) { + return Err(anyhow!( + "resource transformation cannot alter quantity; compose consumption or creation" + )); + } + if let Some(integrity) = integrity { + value.integrity = integrity; + } + value.version = version; + } + ResourceMutationOperation::Consume => { + active_subject(state, resource)?; + let amount = quantity.ok_or_else(|| anyhow!("resource consumption lacks quantity"))?; + if amount <= 0 { + return Err(anyhow!("resource consumption must be positive")); + } + let value = state + .resources + .get_mut(resource) + .ok_or_else(|| anyhow!("resource does not exist"))?; + if value.quantity < amount { + return Err(anyhow!("resource consumption exceeds available quantity")); + } + value.quantity -= amount; + value.version = version; + if value.quantity == 0 { + state.custody.remove(resource); + state.resources.remove(resource); + state + .subjects + .get_mut(resource) + .expect("active resource has a subject") + .lifecycle = LifecycleStatus::Consumed; + } + } + ResourceMutationOperation::Damage | ResourceMutationOperation::Repair => { + active_subject(state, resource)?; + let amount = integrity.ok_or_else(|| anyhow!("integrity mutation lacks amount"))?; + if amount <= 0 { + return Err(anyhow!("integrity mutation must be positive")); + } + let value = state + .resources + .get_mut(resource) + .ok_or_else(|| anyhow!("resource does not exist"))?; + value.integrity = if matches!(operation, ResourceMutationOperation::Damage) { + value.integrity.saturating_sub(amount) + } else { + value.integrity.saturating_add(amount).min(100) + }; + value.version = version; + } + ResourceMutationOperation::Split => { + active_subject(state, resource)?; + let amount = quantity.ok_or_else(|| anyhow!("resource split lacks quantity"))?; + let [child] = related_resources else { + return Err(anyhow!( + "resource split requires exactly one reserved child" + )); + }; + require_kind(child, SubjectKind::Resource)?; + if state.subjects.contains_key(child) { + return Err(anyhow!("resource split child already exists")); + } + let owner = state + .custody + .get(resource) + .cloned() + .ok_or_else(|| anyhow!("resource has no custodian"))?; + let source = state + .resources + .get_mut(resource) + .ok_or_else(|| anyhow!("resource does not exist"))?; + if amount <= 0 || amount >= source.quantity { + return Err(anyhow!( + "resource split must preserve positive source and child lots" + )); + } + source.quantity -= amount; + source.version = version; + let child_state = ResourceComponentState { + schema: source.schema.clone(), + resource: child.clone(), + resource_kind: source.resource_kind.clone(), + quantity: amount, + integrity: source.integrity, + qualities: source.qualities.clone(), + version, + }; + state.subjects.insert( + child.clone(), + TypedSubject { + schema: "ghostlight.typed_subject.v1".into(), + subject: child.clone(), + lifecycle: LifecycleStatus::Active, + admitted_components: BTreeSet::from([ + WorldComponentKind::ResourceState, + WorldComponentKind::Custody, + WorldComponentKind::Lifecycle, + ]), + version, + }, + ); + state.resources.insert(child.clone(), child_state); + state.custody.insert(child.clone(), owner); + } + ResourceMutationOperation::Combine => { + if state.subjects.contains_key(resource) { + return Err(anyhow!("combined resource target already exists")); + } + if related_resources.len() < 2 { + return Err(anyhow!("resource combination requires at least two inputs")); + } + let _recipe = recipe_id.ok_or_else(|| anyhow!("resource combination lacks recipe"))?; + let first = related_resources + .first() + .expect("length checked before first"); + let owner = state + .custody + .get(first) + .cloned() + .ok_or_else(|| anyhow!("combined input has no custodian"))?; + let first_state = state + .resources + .get(first) + .cloned() + .ok_or_else(|| anyhow!("combined input does not exist"))?; + let mut total = 0i64; + for input in related_resources { + active_subject(state, input)?; + if state.custody.get(input) != Some(&owner) { + return Err(anyhow!("resource combination crosses custody")); + } + total = total.saturating_add( + state + .resources + .get(input) + .ok_or_else(|| anyhow!("combined input does not exist"))? + .quantity, + ); + } + for input in related_resources { + consume_entire_resource(state, input)?; + } + state.subjects.insert( + resource.clone(), + TypedSubject { + schema: "ghostlight.typed_subject.v1".into(), + subject: resource.clone(), + lifecycle: LifecycleStatus::Active, + admitted_components: BTreeSet::from([ + WorldComponentKind::ResourceState, + WorldComponentKind::Custody, + WorldComponentKind::Lifecycle, + ]), + version, + }, + ); + if quantity.is_some_and(|quantity| quantity != total) { + return Err(anyhow!( + "resource combination must conserve the exact input quantity" + )); + } + state.resources.insert( + resource.clone(), + ResourceComponentState { + schema: first_state.schema, + resource: resource.clone(), + resource_kind: resource_kind.unwrap_or(&first_state.resource_kind).into(), + quantity: total, + integrity: integrity.unwrap_or(first_state.integrity), + qualities: first_state.qualities, + version, + }, + ); + state.custody.insert(resource.clone(), owner); + } + } + Ok(()) +} + +fn consume_entire_resource(state: &mut ComponentWorldState, resource: &SubjectRef) -> Result<()> { + state + .resources + .remove(resource) + .ok_or_else(|| anyhow!("resource input does not exist"))?; + state.custody.remove(resource); + state + .subjects + .get_mut(resource) + .ok_or_else(|| anyhow!("resource subject does not exist"))? + .lifecycle = LifecycleStatus::Consumed; + Ok(()) +} + +fn apply_commitment_mutation( + state: &mut ComponentWorldState, + subject: &SubjectRef, + operation: &CommitmentMutationOperation, + kind: &CommitmentKind, + commitment_id: &str, + counterparty: Option<&SubjectRef>, + description: Option<&str>, +) -> Result<()> { + active_subject(state, subject)?; + if let Some(counterparty) = counterparty { + active_subject(state, counterparty)?; + } + let key = SubjectEntryKey { + subject: subject.clone(), + entry_id: commitment_id.into(), + }; + let version = next_component_version(state); + match operation { + CommitmentMutationOperation::Create => { + if state.commitments.contains_key(&key) { + return Err(anyhow!("commitment already exists")); + } + state.commitments.insert( + key, + CommitmentComponentState { + kind: kind.clone(), + description: description.unwrap_or(commitment_id).into(), + counterparty: counterparty.cloned(), + status: "active".into(), + version, + }, + ); + } + CommitmentMutationOperation::Alter => { + let value = state + .commitments + .get_mut(&key) + .ok_or_else(|| anyhow!("commitment does not exist"))?; + if let Some(description) = description { + value.description = description.into(); + } + value.counterparty = counterparty.cloned().or(value.counterparty.clone()); + value.version = version; + } + CommitmentMutationOperation::Fulfill => { + let value = state + .commitments + .get_mut(&key) + .ok_or_else(|| anyhow!("commitment does not exist"))?; + value.status = "fulfilled".into(); + value.version = version; + } + CommitmentMutationOperation::Default => { + let value = state + .commitments + .get_mut(&key) + .ok_or_else(|| anyhow!("commitment does not exist"))?; + value.status = "defaulted".into(); + value.version = version; + } + CommitmentMutationOperation::Retire => { + if state.commitments.remove(&key).is_none() { + return Err(anyhow!("commitment does not exist")); + } + } + } + Ok(()) +} + +fn apply_relationship_mutation( + state: &mut ComponentWorldState, + source: &SubjectRef, + target: &SubjectRef, + operation: &RelationshipMutationOperation, + relationship_id: &str, + description: Option<&str>, +) -> Result<()> { + active_subject(state, source)?; + active_subject(state, target)?; + let version = next_component_version(state); + match operation { + RelationshipMutationOperation::Create => { + if state.relationships.contains_key(relationship_id) { + return Err(anyhow!("relationship already exists")); + } + state.relationships.insert( + relationship_id.into(), + RelationshipComponentState { + source: source.clone(), + target: target.clone(), + description: description + .filter(|value| !value.trim().is_empty()) + .ok_or_else(|| anyhow!("relationship creation lacks a description"))? + .into(), + version, + }, + ); + } + RelationshipMutationOperation::Alter => { + let value = state + .relationships + .get_mut(relationship_id) + .ok_or_else(|| anyhow!("relationship does not exist"))?; + if value.source != *source || value.target != *target { + return Err(anyhow!("relationship endpoints do not match")); + } + value.description = description + .filter(|description| !description.trim().is_empty()) + .ok_or_else(|| anyhow!("relationship alteration lacks a description"))? + .into(); + value.version = version; + } + RelationshipMutationOperation::Retire => { + let value = state + .relationships + .get(relationship_id) + .ok_or_else(|| anyhow!("relationship does not exist"))?; + if value.source != *source || value.target != *target { + return Err(anyhow!("relationship endpoints do not match")); + } + state.relationships.remove(relationship_id); + } + } + Ok(()) +} + +fn apply_pressure_mutation( + state: &mut ComponentWorldState, + pressure: &SubjectRef, + owner: &SubjectRef, + operation: &PressureMutationOperation, + amount: Option, + label: Option<&str>, +) -> Result<()> { + require_kind(pressure, SubjectKind::Pressure)?; + active_subject(state, owner)?; + let version = next_component_version(state); + match operation { + PressureMutationOperation::Create => { + if state.subjects.contains_key(pressure) || state.pressures.contains_key(pressure) { + return Err(anyhow!("pressure already exists")); + } + let threshold = amount.unwrap_or(4); + if threshold <= 0 { + return Err(anyhow!("pressure threshold must be positive")); + } + state.subjects.insert( + pressure.clone(), + TypedSubject { + schema: "ghostlight.typed_subject.v1".into(), + subject: pressure.clone(), + lifecycle: LifecycleStatus::Active, + admitted_components: BTreeSet::from([ + WorldComponentKind::Pressure, + WorldComponentKind::Lifecycle, + ]), + version, + }, + ); + state.pressures.insert( + pressure.clone(), + PressureComponentState { + pressure: pressure.clone(), + owner: owner.clone(), + label: label + .filter(|label| !label.trim().is_empty()) + .ok_or_else(|| anyhow!("pressure creation lacks a label"))? + .into(), + progress: 0, + threshold, + resolved: false, + version, + }, + ); + } + PressureMutationOperation::Advance | PressureMutationOperation::Reduce => { + let amount = amount.ok_or_else(|| anyhow!("pressure change lacks an amount"))?; + if amount <= 0 { + return Err(anyhow!("pressure change must be positive")); + } + let value = state + .pressures + .get_mut(pressure) + .ok_or_else(|| anyhow!("pressure does not exist"))?; + if value.owner != *owner || value.resolved { + return Err(anyhow!("pressure owner or lifecycle does not match")); + } + value.progress = if matches!(operation, PressureMutationOperation::Advance) { + value.progress.saturating_add(amount).min(value.threshold) + } else { + value.progress.saturating_sub(amount) + }; + value.version = version; + } + PressureMutationOperation::Resolve => { + let value = state + .pressures + .get_mut(pressure) + .ok_or_else(|| anyhow!("pressure does not exist"))?; + if value.owner != *owner || value.resolved { + return Err(anyhow!("pressure owner or lifecycle does not match")); + } + value.resolved = true; + value.version = version; + } + PressureMutationOperation::Retire => { + let value = state + .pressures + .get(pressure) + .ok_or_else(|| anyhow!("pressure does not exist"))?; + if value.owner != *owner { + return Err(anyhow!("pressure owner does not match")); + } + state.pressures.remove(pressure); + state + .subjects + .get_mut(pressure) + .expect("pressure component has subject") + .lifecycle = LifecycleStatus::Retired; + } + } + Ok(()) +} + +fn apply_knowledge_mutation( + state: &mut ComponentWorldState, + operation: &KnowledgeMutationOperation, + proposition: &SubjectRef, + knower: Option<&SubjectRef>, + speaker: Option<&SubjectRef>, + recipients: &[SubjectRef], + channel: Option<&SubjectRef>, +) -> Result<()> { + active_subject(state, proposition)?; + require_kind(proposition, SubjectKind::Proposition)?; + if let Some(channel) = channel { + active_subject(state, channel)?; + require_kind(channel, SubjectKind::Channel)?; + } + let version = next_component_version(state); + match operation { + KnowledgeMutationOperation::Acquire => { + let knower = knower.ok_or_else(|| anyhow!("knowledge acquisition lacks a knower"))?; + active_subject(state, knower)?; + let key = KnowledgeKey { + knower: knower.clone(), + proposition: proposition.clone(), + }; + if state + .knowledge + .get(&key) + .is_some_and(|knowledge| knowledge.status == "known") + { + return Err(anyhow!("knower already has the proposition")); + } + state.knowledge.insert( + key, + KnowledgeComponentState { + status: "known".into(), + source: None, + channel: channel.cloned(), + concealed_from: BTreeSet::new(), + version, + }, + ); + } + KnowledgeMutationOperation::Communicate => { + let speaker = speaker.ok_or_else(|| anyhow!("communication lacks a speaker"))?; + active_subject(state, speaker)?; + let speaker_key = KnowledgeKey { + knower: speaker.clone(), + proposition: proposition.clone(), + }; + if !state + .knowledge + .get(&speaker_key) + .is_some_and(|knowledge| knowledge.status == "known") + { + return Err(anyhow!("speaker does not know the proposition")); + } + if recipients.is_empty() { + return Err(anyhow!("communication lacks exact recipients")); + } + for recipient in recipients { + active_subject(state, recipient)?; + let key = KnowledgeKey { + knower: recipient.clone(), + proposition: proposition.clone(), + }; + state.knowledge.insert( + key, + KnowledgeComponentState { + status: "known".into(), + source: Some(speaker.clone()), + channel: channel.cloned(), + concealed_from: BTreeSet::new(), + version, + }, + ); + } + } + KnowledgeMutationOperation::Conceal => { + let knower = knower.ok_or_else(|| anyhow!("concealment lacks a knower"))?; + let value = state + .knowledge + .get_mut(&KnowledgeKey { + knower: knower.clone(), + proposition: proposition.clone(), + }) + .ok_or_else(|| anyhow!("knower does not hold the proposition"))?; + value.concealed_from.extend(recipients.iter().cloned()); + value.version = version; + } + KnowledgeMutationOperation::Correct | KnowledgeMutationOperation::Invalidate => { + let knower = knower.ok_or_else(|| anyhow!("knowledge change lacks a knower"))?; + let value = state + .knowledge + .get_mut(&KnowledgeKey { + knower: knower.clone(), + proposition: proposition.clone(), + }) + .ok_or_else(|| anyhow!("knower does not hold the proposition"))?; + value.status = if matches!(operation, KnowledgeMutationOperation::Correct) { + "corrected" + } else { + "invalidated" + } + .into(); + value.version = version; + } + } + Ok(()) +} + +fn apply_memory_mutation( + state: &mut ComponentWorldState, + subject: &SubjectRef, + operation: &MemoryMutationOperation, + memory_id: &str, + event_id: Option<&str>, + summary: Option<&str>, +) -> Result<()> { + active_subject(state, subject)?; + let key = SubjectEntryKey { + subject: subject.clone(), + entry_id: memory_id.into(), + }; + let version = next_component_version(state); + match operation { + MemoryMutationOperation::Record => { + if state.memories.contains_key(&key) { + return Err(anyhow!("memory already exists")); + } + state.memories.insert( + key, + MemoryComponentState { + event_id: event_id.map(str::to_owned), + summary: summary + .filter(|summary| !summary.trim().is_empty()) + .ok_or_else(|| anyhow!("memory record lacks a summary"))? + .into(), + version, + }, + ); + } + MemoryMutationOperation::Revise => { + let value = state + .memories + .get_mut(&key) + .ok_or_else(|| anyhow!("memory does not exist"))?; + value.summary = summary + .filter(|summary| !summary.trim().is_empty()) + .ok_or_else(|| anyhow!("memory revision lacks a summary"))? + .into(); + value.event_id = event_id.map(str::to_owned).or(value.event_id.clone()); + value.version = version; + } + MemoryMutationOperation::Retire => { + if state.memories.remove(&key).is_none() { + return Err(anyhow!("memory does not exist")); + } + } + } + Ok(()) +} + +fn apply_membership_mutation( + state: &mut ComponentWorldState, + actor: &SubjectRef, + operation: &PopulationMembershipOperation, + source_population: Option<&SubjectRef>, + destination_population: Option<&SubjectRef>, +) -> Result<()> { + active_subject(state, actor)?; + require_kind(actor, SubjectKind::Actor)?; + if let Some(population) = source_population { + active_subject(state, population)?; + require_kind(population, SubjectKind::Population)?; + } + if let Some(population) = destination_population { + active_subject(state, population)?; + require_kind(population, SubjectKind::Population)?; + } + let version = next_component_version(state); + let source_key = source_population.map(|population| MembershipKey { + actor: actor.clone(), + population: population.clone(), + }); + let destination_key = destination_population.map(|population| MembershipKey { + actor: actor.clone(), + population: population.clone(), + }); + match operation { + PopulationMembershipOperation::Join => { + let key = + destination_key.ok_or_else(|| anyhow!("population join lacks destination"))?; + if state + .memberships + .get(&key) + .is_some_and(|membership| membership.active) + { + return Err(anyhow!("actor already belongs to destination population")); + } + state.memberships.insert( + key, + PopulationMembershipState { + active: true, + version, + }, + ); + } + PopulationMembershipOperation::Leave => { + let key = source_key.ok_or_else(|| anyhow!("population leave lacks source"))?; + let value = state + .memberships + .get_mut(&key) + .filter(|membership| membership.active) + .ok_or_else(|| anyhow!("actor does not belong to source population"))?; + value.active = false; + value.version = version; + } + PopulationMembershipOperation::Transfer => { + let source = source_key.ok_or_else(|| anyhow!("population transfer lacks source"))?; + let destination = + destination_key.ok_or_else(|| anyhow!("population transfer lacks destination"))?; + if source == destination { + return Err(anyhow!( + "population transfer source and destination are identical" + )); + } + if state + .memberships + .get(&destination) + .is_some_and(|membership| membership.active) + { + return Err(anyhow!("actor already belongs to destination population")); + } + let source_value = state + .memberships + .get_mut(&source) + .filter(|membership| membership.active) + .ok_or_else(|| anyhow!("actor does not belong to source population"))?; + source_value.active = false; + source_value.version = version; + state.memberships.insert( + destination, + PopulationMembershipState { + active: true, + version, + }, + ); + } + } + Ok(()) +} + +fn apply_lineage_mutation( + state: &mut ComponentWorldState, + operation: &PopulationLineageOperation, + parents: &[SubjectRef], + children: &[SubjectRef], + remainder: Option<&SubjectRef>, +) -> Result<()> { + for population in parents.iter().chain(children).chain(remainder) { + active_subject(state, population)?; + require_kind(population, SubjectKind::Population)?; + } + let parent_set = parents.iter().cloned().collect::>(); + let child_set = children.iter().cloned().collect::>(); + if parent_set.len() != parents.len() + || child_set.len() != children.len() + || !parent_set.is_disjoint(&child_set) + { + return Err(anyhow!("population lineage repeats or overlaps subjects")); + } + match operation { + PopulationLineageOperation::Split => { + if parents.len() != 1 || children.is_empty() { + return Err(anyhow!("population split shape is invalid")); + } + let remainder = remainder.ok_or_else(|| anyhow!("population split lacks remainder"))?; + if !child_set.contains(remainder) { + return Err(anyhow!("population split remainder is not a child")); + } + } + PopulationLineageOperation::Merge => { + if parents.len() < 2 || children.len() != 1 || remainder.is_some() { + return Err(anyhow!("population merge shape is invalid")); + } + } + } + let encoded = rmp_serde::to_vec_named(&(operation, parents, children, remainder))?; + let id = format!("lineage:{}", &sha256(&encoded)[7..23]); + if state.population_lineages.contains_key(&id) { + return Err(anyhow!("population lineage already exists")); + } + state.population_lineages.insert( + id.clone(), + PopulationLineageState { + id, + operation: operation.clone(), + parent_populations: parents.to_vec(), + child_populations: children.to_vec(), + remainder_population: remainder.cloned(), + version: next_component_version(state), + }, + ); + Ok(()) +} + +fn apply_identity_mutation( + state: &mut ComponentWorldState, + subject: &SubjectRef, + operation: &IdentityMutationOperation, + handle_id: &str, + handle_value: Option<&str>, + audience: &[SubjectRef], +) -> Result<()> { + active_subject(state, subject)?; + for observer in audience { + active_subject(state, observer)?; + } + let version = next_component_version(state); + match operation { + IdentityMutationOperation::Adopt => { + if state.identities.contains_key(handle_id) { + return Err(anyhow!("identity handle already exists")); + } + state.identities.insert( + handle_id.into(), + IdentityHandleState { + schema: "ghostlight.identity_handle.v1".into(), + id: handle_id.into(), + subject: subject.clone(), + value: handle_value + .filter(|value| !value.trim().is_empty()) + .ok_or_else(|| anyhow!("identity adoption lacks a handle value"))? + .into(), + active: true, + known_by: BTreeSet::from([subject.clone()]), + restricted_to: BTreeSet::new(), + source_revision: version, + }, + ); + } + IdentityMutationOperation::Disclose => { + let value = state + .identities + .get_mut(handle_id) + .filter(|handle| handle.active && handle.subject == *subject) + .ok_or_else(|| anyhow!("active identity handle does not exist for subject"))?; + if handle_value.is_some_and(|candidate| candidate != value.value) { + return Err(anyhow!( + "identity disclosure value does not match the handle" + )); + } + value.known_by.extend(audience.iter().cloned()); + value.source_revision = version; + } + IdentityMutationOperation::Restrict => { + let value = state + .identities + .get_mut(handle_id) + .filter(|handle| handle.active && handle.subject == *subject) + .ok_or_else(|| anyhow!("active identity handle does not exist for subject"))?; + value.restricted_to = audience.iter().cloned().collect(); + value.source_revision = version; + } + IdentityMutationOperation::Retire => { + let value = state + .identities + .get_mut(handle_id) + .filter(|handle| handle.active && handle.subject == *subject) + .ok_or_else(|| anyhow!("active identity handle does not exist for subject"))?; + value.active = false; + value.source_revision = version; + } + } + Ok(()) +} + +fn apply_topology_mutation( + state: &mut ComponentWorldState, + operation: &TopologyMutationOperation, + edge_id: &str, + from_place: &SubjectRef, + to_place: &SubjectRef, + travel_minutes: Option, +) -> Result<()> { + active_subject(state, from_place)?; + active_subject(state, to_place)?; + require_kind(from_place, SubjectKind::Place)?; + require_kind(to_place, SubjectKind::Place)?; + if from_place == to_place { + return Err(anyhow!("topology edge cannot connect a place to itself")); + } + let version = next_component_version(state); + match operation { + TopologyMutationOperation::Add => { + if state.topology.contains_key(edge_id) { + return Err(anyhow!("topology edge already exists")); + } + let travel_minutes = travel_minutes + .filter(|minutes| *minutes > 0) + .ok_or_else(|| anyhow!("topology edge requires positive travel time"))?; + state.topology.insert( + edge_id.into(), + TopologyComponentState { + id: edge_id.into(), + from_place: from_place.clone(), + to_place: to_place.clone(), + travel_minutes, + open: true, + version, + }, + ); + } + TopologyMutationOperation::Alter => { + let value = state + .topology + .get_mut(edge_id) + .ok_or_else(|| anyhow!("topology edge does not exist"))?; + if value.from_place != *from_place || value.to_place != *to_place { + return Err(anyhow!("topology endpoints do not match")); + } + value.travel_minutes = travel_minutes + .filter(|minutes| *minutes > 0) + .ok_or_else(|| anyhow!("topology alteration requires positive travel time"))?; + value.version = version; + } + TopologyMutationOperation::Open | TopologyMutationOperation::Close => { + let value = state + .topology + .get_mut(edge_id) + .ok_or_else(|| anyhow!("topology edge does not exist"))?; + if value.from_place != *from_place || value.to_place != *to_place { + return Err(anyhow!("topology endpoints do not match")); + } + let next_open = matches!(operation, TopologyMutationOperation::Open); + if value.open == next_open { + return Err(anyhow!("topology operation is a no-op")); + } + value.open = next_open; + value.version = version; + } + TopologyMutationOperation::Retire => { + let value = state + .topology + .get(edge_id) + .ok_or_else(|| anyhow!("topology edge does not exist"))?; + if value.from_place != *from_place || value.to_place != *to_place { + return Err(anyhow!("topology endpoints do not match")); + } + state.topology.remove(edge_id); + } + } + Ok(()) +} + +fn retire_entity(state: &mut ComponentWorldState, subject: &SubjectRef) -> Result<()> { + active_subject(state, subject)?; + if state.occupancy.contains_key(subject) + || state.custody.contains_key(subject) + || state.custody.values().any(|owner| owner == subject) + || state.memberships.iter().any(|(key, value)| { + value.active && (&key.actor == subject || &key.population == subject) + }) + || state + .topology + .values() + .any(|edge| &edge.from_place == subject || &edge.to_place == subject) + || state + .relationships + .values() + .any(|relationship| &relationship.source == subject || &relationship.target == subject) + || state.commitments.iter().any(|(key, commitment)| { + &key.subject == subject || commitment.counterparty.as_ref() == Some(subject) + }) + { + return Err(anyhow!( + "entity retirement would leave active component references" + )); + } + let version = next_component_version(state); + let record = state + .subjects + .get_mut(subject) + .expect("active subject was resolved"); + record.lifecycle = LifecycleStatus::Retired; + record.version = version; + Ok(()) +} + +pub fn validate_component_world(state: &ComponentWorldState) -> Result<()> { + for (subject, record) in &state.subjects { + if &record.subject != subject || subject.id.trim().is_empty() { + return Err(anyhow!("typed subject identity is malformed")); + } + } + for (subject, place) in &state.occupancy { + active_subject(state, subject)?; + active_subject(state, place)?; + require_kind(place, SubjectKind::Place)?; + } + for (resource, custodian) in &state.custody { + active_subject(state, resource)?; + require_kind(resource, SubjectKind::Resource)?; + active_subject(state, custodian)?; + if !state.resources.contains_key(resource) { + return Err(anyhow!("custody references a missing resource component")); + } + } + for (resource, value) in &state.resources { + active_subject(state, resource)?; + require_kind(resource, SubjectKind::Resource)?; + if &value.resource != resource + || value.quantity <= 0 + || value.integrity < 0 + || value.integrity > 100 + || !state.custody.contains_key(resource) + { + return Err(anyhow!( + "resource component violates conservation or bounds" + )); + } + } + for key in state + .capabilities + .keys() + .chain(state.conditions.keys()) + .chain(state.commitments.keys()) + .chain(state.memories.keys()) + { + active_subject(state, &key.subject)?; + if key.entry_id.trim().is_empty() { + return Err(anyhow!("component entry id is empty")); + } + } + for relationship in state.relationships.values() { + active_subject(state, &relationship.source)?; + active_subject(state, &relationship.target)?; + } + for (pressure, value) in &state.pressures { + active_subject(state, pressure)?; + require_kind(pressure, SubjectKind::Pressure)?; + active_subject(state, &value.owner)?; + if &value.pressure != pressure + || value.progress < 0 + || value.progress > value.threshold + || value.threshold <= 0 + { + return Err(anyhow!("pressure component violates bounds")); + } + } + for (key, value) in &state.knowledge { + active_subject(state, &key.knower)?; + active_subject(state, &key.proposition)?; + require_kind(&key.proposition, SubjectKind::Proposition)?; + if let Some(source) = &value.source { + active_subject(state, source)?; + } + if let Some(channel) = &value.channel { + active_subject(state, channel)?; + require_kind(channel, SubjectKind::Channel)?; + } + for observer in &value.concealed_from { + active_subject(state, observer)?; + } + } + for subject in state.postures.keys() { + active_subject(state, subject)?; + } + for (key, value) in &state.memberships { + active_subject(state, &key.actor)?; + active_subject(state, &key.population)?; + require_kind(&key.actor, SubjectKind::Actor)?; + require_kind(&key.population, SubjectKind::Population)?; + if !value.active && value.version == 0 { + return Err(anyhow!("inactive membership lacks history")); + } + } + for lineage in state.population_lineages.values() { + for population in lineage + .parent_populations + .iter() + .chain(&lineage.child_populations) + .chain(lineage.remainder_population.as_ref()) + { + active_subject(state, population)?; + require_kind(population, SubjectKind::Population)?; + } + } + for handle in state.identities.values() { + active_subject(state, &handle.subject)?; + if handle.id.trim().is_empty() || handle.value.trim().is_empty() { + return Err(anyhow!("identity handle is malformed")); + } + for observer in handle.known_by.iter().chain(&handle.restricted_to) { + active_subject(state, observer)?; + } + } + for edge in state.topology.values() { + active_subject(state, &edge.from_place)?; + active_subject(state, &edge.to_place)?; + require_kind(&edge.from_place, SubjectKind::Place)?; + require_kind(&edge.to_place, SubjectKind::Place)?; + if edge.travel_minutes <= 0 || edge.from_place == edge.to_place { + return Err(anyhow!("topology edge is malformed")); + } + } + Ok(()) +} + +fn mutation_component_refs( + mutation: &WorldMutation, +) -> Vec<(SubjectRef, WorldComponentKind, Option)> { + use WorldComponentKind as Component; + use WorldMutation::*; + match mutation { + Relocate { subject, .. } => vec![(subject.clone(), Component::Occupancy, None)], + TransferCustody { resource, .. } => vec![(resource.clone(), Component::Custody, None)], + MutateResource { + resource, + related_resources, + .. + } => std::iter::once(resource) + .chain(related_resources) + .map(|resource| (resource.clone(), Component::ResourceState, None)) + .collect(), + ChangeCapability { + subject, + capability_id, + .. + } => vec![( + subject.clone(), + Component::Capability, + Some(capability_id.clone()), + )], + ChangeCondition { + subject, + condition_id, + .. + } => vec![( + subject.clone(), + Component::Condition, + Some(condition_id.clone()), + )], + ChangeCommitment { + subject, + commitment_id, + .. + } => vec![( + subject.clone(), + Component::Commitment, + Some(commitment_id.clone()), + )], + ChangeRelationship { + source, + relationship_id, + .. + } => vec![( + source.clone(), + Component::Relationship, + Some(relationship_id.clone()), + )], + ChangePressure { pressure, .. } => vec![(pressure.clone(), Component::Pressure, None)], + ChangeKnowledge { + proposition, + knower, + recipients, + .. + } => knower + .iter() + .chain(recipients) + .map(|subject| { + ( + subject.clone(), + Component::Knowledge, + Some(proposition.id.clone()), + ) + }) + .collect(), + ChangeMemory { + subject, memory_id, .. + } => vec![(subject.clone(), Component::Memory, Some(memory_id.clone()))], + ChangePosture { subject, .. } => vec![(subject.clone(), Component::Posture, None)], + ChangePopulationMembership { actor, .. } => { + vec![(actor.clone(), Component::PopulationMembership, None)] + } + ChangePopulationLineage { + parent_populations, + child_populations, + .. + } => parent_populations + .iter() + .chain(child_populations) + .map(|population| (population.clone(), Component::PopulationLineage, None)) + .collect(), + ChangeIdentity { + subject, handle_id, .. + } => vec![( + subject.clone(), + Component::Identity, + Some(handle_id.clone()), + )], + ChangeTopology { + from_place, + edge_id, + .. + } => vec![( + from_place.clone(), + Component::Topology, + Some(edge_id.clone()), + )], + AdmitEntity { subject, .. } | RetireEntity { subject, .. } => { + vec![(subject.clone(), Component::Lifecycle, None)] + } + AdvanceWorldTime { campaign, .. } => vec![(campaign.clone(), Component::WorldTime, None)], + } +} + +pub fn validate_batch_structure( + envelope: &MutationAuthorityEnvelope, + batch: &WorldMutationBatch, + now: DateTime, +) -> Result<()> { + if envelope.schema != "ghostlight.mutation_authority_envelope.v1" + || batch.schema != "ghostlight.world_mutation_batch.v1" + { + return Err(anyhow!("mutation document uses an unsupported schema")); + } + if envelope.digest != envelope_digest(envelope)? { + return Err(anyhow!("mutation authority envelope digest is invalid")); + } + if batch.digest != mutation_batch_digest(batch)? { + return Err(anyhow!("world mutation batch digest is invalid")); + } + if envelope.campaign_id != batch.campaign_id + || envelope.world_revision != batch.expected_world_revision + || envelope.resolution_epoch != batch.expected_resolution_epoch + || envelope.digest != batch.authority_envelope_digest + { + return Err(anyhow!( + "world mutation batch is not bound to its authority snapshot" + )); + } + if envelope.expires_at < now { + return Err(anyhow!("mutation authority envelope is expired")); + } + if batch.id.trim().is_empty() || batch.source_receipt_id.trim().is_empty() { + return Err(anyhow!( + "world mutation batch lacks exact identity or source" + )); + } + + let permits = envelope + .permits + .iter() + .map(|permit| (permit.id.as_str(), permit)) + .collect::>(); + if permits.len() != envelope.permits.len() { + return Err(anyhow!("mutation authority envelope repeats a permit id")); + } + let mut uses = BTreeMap::<&str, u16>::new(); + for proposed in &batch.mutations { + let permit = permits + .get(proposed.permit_id.as_str()) + .ok_or_else(|| anyhow!("world mutation cites an unknown permit"))?; + let count = uses.entry(proposed.permit_id.as_str()).or_default(); + *count = count.saturating_add(1); + if *count > permit.maximum_uses { + return Err(anyhow!("world mutation exceeds permit use count")); + } + validate_permitted_mutation(permit, &proposed.mutation)?; + } + Ok(()) +} + +pub fn mutation_batch_json_schema( + envelope: &MutationAuthorityEnvelope, +) -> Result { + let mut schema = serde_json::to_value(schema_for!(WorldMutationBatch))?; + let variants = schema + .pointer("/$defs/WorldMutation/oneOf") + .and_then(serde_json::Value::as_array) + .cloned() + .ok_or_else(|| anyhow!("world mutation schema omitted tagged variants"))?; + let branches = envelope + .permits + .iter() + .map(|permit| permit_schema_branch(permit, &variants)) + .collect::>>()?; + let mutation_items = schema + .pointer_mut("/$defs/PermittedWorldMutation") + .ok_or_else(|| anyhow!("world mutation schema omitted permitted mutation definition"))?; + *mutation_items = serde_json::json!({"oneOf": branches}); + schema + .pointer_mut("/$defs") + .and_then(serde_json::Value::as_object_mut) + .expect("schemars emits an object definition table") + .remove("WorldMutation"); + Ok(schema) +} + +fn permit_schema_branch( + permit: &MutationPermit, + variants: &[serde_json::Value], +) -> Result { + let (mutation_type, operation) = mutation_schema_tags(permit.operation); + let mut mutation = variants + .iter() + .find(|variant| { + variant + .pointer("/properties/type/const") + .and_then(serde_json::Value::as_str) + == Some(mutation_type) + }) + .cloned() + .ok_or_else(|| anyhow!("world mutation schema omitted {mutation_type}"))?; + if let Some(operation) = operation { + mutation["properties"]["operation"] = serde_json::json!({"const": operation}); + } + constrain_permit_schema(&mut mutation, permit)?; + Ok(serde_json::json!({ + "type":"object", + "additionalProperties":false, + "required":["permit_id","mutation"], + "properties":{ + "permit_id":{"const":permit.id}, + "mutation":mutation + } + })) +} + +#[derive(Clone, Copy)] +enum FieldShape { + Required, + Optional, + Array, +} + +fn constrain_permit_schema( + mutation: &mut serde_json::Value, + permit: &MutationPermit, +) -> Result<()> { + for (role, field, shape) in subject_schema_fields(permit.operation) { + let allowed = permit + .subject_bindings + .iter() + .find(|binding| binding.role == role) + .map(|binding| &binding.allowed_subjects); + constrain_subject_field(mutation, field, shape, allowed)?; + } + for (role, field, shape) in string_schema_fields(permit.operation) { + let constraint = permit.string_constraints.get(&role); + constrain_string_field(mutation, field, shape, constraint)?; + } + for (role, field, shape) in integer_schema_fields(permit.operation) { + let bounds = permit.integer_bounds.get(&role); + constrain_integer_field(mutation, field, shape, bounds)?; + } + Ok(()) +} + +fn constrain_subject_field( + mutation: &mut serde_json::Value, + field: &str, + shape: FieldShape, + allowed: Option<&BTreeSet>, +) -> Result<()> { + let target = mutation + .pointer_mut(&format!("/properties/{field}")) + .ok_or_else(|| anyhow!("mutation schema omitted subject field {field}"))?; + let values = allowed + .into_iter() + .flatten() + .map(serde_json::to_value) + .collect::, _>>()?; + *target = match shape { + FieldShape::Required if values.is_empty() => { + return Err(anyhow!("permit omitted required subject role for {field}")); + } + FieldShape::Required => serde_json::json!({"enum":values}), + FieldShape::Optional if values.is_empty() => serde_json::json!({"type":"null"}), + FieldShape::Optional => { + serde_json::json!({"anyOf":[{"enum":values},{"type":"null"}]}) + } + FieldShape::Array if values.is_empty() => { + serde_json::json!({"type":"array","maxItems":0}) + } + FieldShape::Array => serde_json::json!({ + "type":"array","items":{"enum":values},"uniqueItems":true + }), + }; + Ok(()) +} + +fn constrain_string_field( + mutation: &mut serde_json::Value, + field: &str, + shape: FieldShape, + constraint: Option<&StringConstraint>, +) -> Result<()> { + let target = mutation + .pointer_mut(&format!("/properties/{field}")) + .ok_or_else(|| anyhow!("mutation schema omitted string field {field}"))?; + let Some(constraint) = constraint else { + return match shape { + FieldShape::Required => Err(anyhow!("permit omitted required string role for {field}")), + FieldShape::Optional => { + *target = serde_json::json!({"type":"null"}); + Ok(()) + } + FieldShape::Array => Err(anyhow!("string arrays are not supported in permits")), + }; + }; + let mut string = serde_json::json!({ + "type":"string", + "minLength":constraint.minimum_length, + "maxLength":constraint.maximum_length + }); + if !constraint.allowed_values.is_empty() { + string["enum"] = serde_json::to_value(&constraint.allowed_values)?; + } + *target = match shape { + FieldShape::Required => string, + FieldShape::Optional => serde_json::json!({"anyOf":[string,{"type":"null"}]}), + FieldShape::Array => return Err(anyhow!("string arrays are not supported in permits")), + }; + Ok(()) +} + +fn constrain_integer_field( + mutation: &mut serde_json::Value, + field: &str, + shape: FieldShape, + bounds: Option<&IntegerBounds>, +) -> Result<()> { + let target = mutation + .pointer_mut(&format!("/properties/{field}")) + .ok_or_else(|| anyhow!("mutation schema omitted integer field {field}"))?; + let Some(bounds) = bounds else { + return match shape { + FieldShape::Required => { + Err(anyhow!("permit omitted required integer role for {field}")) + } + FieldShape::Optional => { + *target = serde_json::json!({"type":"null"}); + Ok(()) + } + FieldShape::Array => Err(anyhow!("integer arrays are not supported in permits")), + }; + }; + let integer = serde_json::json!({ + "type":"integer","minimum":bounds.minimum,"maximum":bounds.maximum + }); + *target = match shape { + FieldShape::Required => integer, + FieldShape::Optional => serde_json::json!({"anyOf":[integer,{"type":"null"}]}), + FieldShape::Array => return Err(anyhow!("integer arrays are not supported in permits")), + }; + Ok(()) +} + +fn subject_schema_fields( + operation: WorldMutationOperation, +) -> Vec<(MutationSubjectRole, &'static str, FieldShape)> { + use FieldShape::{Array, Optional, Required}; + use MutationSubjectRole::*; + use WorldMutationOperation::*; + match operation { + Relocate => vec![ + (Subject, "subject", Required), + (OriginPlace, "from_place", Required), + (DestinationPlace, "to_place", Required), + ], + TransferCustody => vec![ + (Resource, "resource", Required), + (SourceCustodian, "from_custodian", Required), + (DestinationCustodian, "to_custodian", Required), + ], + ResourceCreate => vec![ + (Resource, "resource", Required), + (Owner, "custodian", Required), + (RelatedResource, "related_resources", Array), + ], + ResourceTransform | ResourceConsume | ResourceDamage | ResourceRepair | ResourceSplit + | ResourceCombine => vec![ + (Resource, "resource", Required), + (Owner, "custodian", Optional), + (RelatedResource, "related_resources", Array), + ], + CapabilityGrant | CapabilityAlter | CapabilitySuspend | CapabilityRetire + | ConditionApply | ConditionAlter | ConditionClear | MemoryRecord | MemoryRevise + | MemoryRetire | PostureChange => vec![(Subject, "subject", Required)], + CommitmentCreate | CommitmentAlter | CommitmentFulfill | CommitmentDefault + | CommitmentRetire => vec![ + (Subject, "subject", Required), + (Counterparty, "counterparty", Optional), + ], + RelationshipCreate | RelationshipAlter | RelationshipRetire => vec![ + (RelationshipSource, "source", Required), + (RelationshipTarget, "target", Required), + ], + PressureCreate | PressureAdvance | PressureReduce | PressureResolve | PressureRetire => { + vec![(Pressure, "pressure", Required), (Owner, "owner", Required)] + } + KnowledgeAcquire | KnowledgeConceal | KnowledgeCorrect | KnowledgeInvalidate => vec![ + (Proposition, "proposition", Required), + (Knower, "knower", Required), + (Speaker, "speaker", Optional), + (Recipient, "recipients", Array), + (Channel, "channel", Optional), + ], + KnowledgeCommunicate => vec![ + (Proposition, "proposition", Required), + (Knower, "knower", Optional), + (Speaker, "speaker", Required), + (Recipient, "recipients", Array), + (Channel, "channel", Optional), + ], + PopulationJoin | PopulationLeave | PopulationTransfer => vec![ + (Actor, "actor", Required), + (SourcePopulation, "source_population", Optional), + (DestinationPopulation, "destination_population", Optional), + ], + PopulationSplit | PopulationMerge => vec![ + (ParentPopulation, "parent_populations", Array), + (ChildPopulation, "child_populations", Array), + (ChildPopulation, "remainder_population", Optional), + ], + IdentityAdopt | IdentityDisclose | IdentityRestrict | IdentityRetire => vec![ + (Subject, "subject", Required), + (Recipient, "audience", Array), + ], + TopologyAdd | TopologyAlter | TopologyOpen | TopologyClose | TopologyRetire => vec![ + (TopologyOrigin, "from_place", Required), + (TopologyDestination, "to_place", Required), + ], + AdmitEntity | RetireEntity => vec![(Entity, "subject", Required)], + AdvanceWorldTime => vec![(Subject, "campaign", Required)], + } +} + +fn string_schema_fields( + operation: WorldMutationOperation, +) -> Vec<(MutationStringRole, &'static str, FieldShape)> { + use FieldShape::{Optional, Required}; + use MutationStringRole::*; + use WorldMutationOperation::*; + match operation { + Relocate => vec![(RouteId, "route_id", Required)], + ResourceCreate => vec![ + (ResourceKind, "resource_kind", Required), + (RecipeId, "recipe_id", Optional), + ], + ResourceTransform | ResourceSplit | ResourceCombine => vec![ + (ResourceKind, "resource_kind", Optional), + (RecipeId, "recipe_id", Required), + ], + ResourceConsume | ResourceDamage | ResourceRepair => vec![ + (ResourceKind, "resource_kind", Optional), + (RecipeId, "recipe_id", Optional), + ], + CapabilityGrant | CapabilityAlter | CapabilitySuspend | CapabilityRetire => { + vec![(CapabilityId, "capability_id", Required)] + } + ConditionApply | ConditionAlter | ConditionClear => { + vec![(ConditionId, "condition_id", Required)] + } + CommitmentCreate | CommitmentAlter | CommitmentFulfill | CommitmentDefault + | CommitmentRetire => vec![(CommitmentId, "commitment_id", Required)], + RelationshipCreate | RelationshipAlter | RelationshipRetire => { + vec![(RelationshipId, "relationship_id", Required)] + } + PressureCreate => vec![(PressureLabel, "label", Required)], + PressureAdvance | PressureReduce | PressureResolve | PressureRetire => { + vec![(PressureLabel, "label", Optional)] + } + MemoryRecord | MemoryRevise | MemoryRetire => vec![ + (MemoryId, "memory_id", Required), + (EventId, "event_id", Optional), + ], + PostureChange => vec![(Posture, "posture", Required)], + IdentityAdopt => vec![ + (IdentityHandleId, "handle_id", Required), + (IdentityHandleValue, "handle_value", Required), + ], + IdentityDisclose | IdentityRestrict | IdentityRetire => vec![ + (IdentityHandleId, "handle_id", Required), + (IdentityHandleValue, "handle_value", Optional), + ], + TopologyAdd | TopologyAlter | TopologyOpen | TopologyClose | TopologyRetire => { + vec![(TopologyEdgeId, "edge_id", Required)] + } + AdmitEntity => vec![(AdmissionReceiptId, "admission_receipt_id", Required)], + TransferCustody | KnowledgeAcquire | KnowledgeCommunicate | KnowledgeConceal + | KnowledgeCorrect | KnowledgeInvalidate | PopulationJoin | PopulationLeave + | PopulationTransfer | PopulationSplit | PopulationMerge | RetireEntity + | AdvanceWorldTime => vec![], + } +} + +fn integer_schema_fields( + operation: WorldMutationOperation, +) -> Vec<(MutationIntegerRole, &'static str, FieldShape)> { + use FieldShape::{Optional, Required}; + use MutationIntegerRole::*; + use WorldMutationOperation::*; + match operation { + ResourceCreate | ResourceConsume | ResourceSplit => vec![ + (Quantity, "quantity", Required), + (Integrity, "integrity", Optional), + ], + ResourceDamage | ResourceRepair => vec![ + (Quantity, "quantity", Optional), + (Integrity, "integrity", Required), + ], + ResourceTransform | ResourceCombine => vec![ + (Quantity, "quantity", Optional), + (Integrity, "integrity", Optional), + ], + ConditionApply | ConditionAlter | ConditionClear => { + vec![(Severity, "severity", Optional)] + } + PressureAdvance | PressureReduce => vec![(PressureAmount, "amount", Required)], + PressureCreate | PressureResolve | PressureRetire => { + vec![(PressureAmount, "amount", Optional)] + } + TopologyAdd | TopologyAlter => vec![(TravelMinutes, "travel_minutes", Required)], + TopologyOpen | TopologyClose | TopologyRetire => { + vec![(TravelMinutes, "travel_minutes", Optional)] + } + AdvanceWorldTime => vec![(WorldMinutes, "minutes", Required)], + Relocate | TransferCustody | CapabilityGrant | CapabilityAlter | CapabilitySuspend + | CapabilityRetire | CommitmentCreate | CommitmentAlter | CommitmentFulfill + | CommitmentDefault | CommitmentRetire | RelationshipCreate | RelationshipAlter + | RelationshipRetire | KnowledgeAcquire | KnowledgeCommunicate | KnowledgeConceal + | KnowledgeCorrect | KnowledgeInvalidate | MemoryRecord | MemoryRevise | MemoryRetire + | PostureChange | PopulationJoin | PopulationLeave | PopulationTransfer + | PopulationSplit | PopulationMerge | IdentityAdopt | IdentityDisclose + | IdentityRestrict | IdentityRetire | AdmitEntity | RetireEntity => vec![], + } +} + +fn mutation_schema_tags(operation: WorldMutationOperation) -> (&'static str, Option<&'static str>) { + use WorldMutationOperation::*; + match operation { + Relocate => ("relocate", None), + TransferCustody => ("transfer_custody", None), + ResourceCreate => ("mutate_resource", Some("create")), + ResourceTransform => ("mutate_resource", Some("transform")), + ResourceConsume => ("mutate_resource", Some("consume")), + ResourceDamage => ("mutate_resource", Some("damage")), + ResourceRepair => ("mutate_resource", Some("repair")), + ResourceSplit => ("mutate_resource", Some("split")), + ResourceCombine => ("mutate_resource", Some("combine")), + CapabilityGrant => ("change_capability", Some("grant")), + CapabilityAlter => ("change_capability", Some("alter")), + CapabilitySuspend => ("change_capability", Some("suspend")), + CapabilityRetire => ("change_capability", Some("retire")), + ConditionApply => ("change_condition", Some("apply")), + ConditionAlter => ("change_condition", Some("alter")), + ConditionClear => ("change_condition", Some("clear")), + CommitmentCreate => ("change_commitment", Some("create")), + CommitmentAlter => ("change_commitment", Some("alter")), + CommitmentFulfill => ("change_commitment", Some("fulfill")), + CommitmentDefault => ("change_commitment", Some("default")), + CommitmentRetire => ("change_commitment", Some("retire")), + RelationshipCreate => ("change_relationship", Some("create")), + RelationshipAlter => ("change_relationship", Some("alter")), + RelationshipRetire => ("change_relationship", Some("retire")), + PressureCreate => ("change_pressure", Some("create")), + PressureAdvance => ("change_pressure", Some("advance")), + PressureReduce => ("change_pressure", Some("reduce")), + PressureResolve => ("change_pressure", Some("resolve")), + PressureRetire => ("change_pressure", Some("retire")), + KnowledgeAcquire => ("change_knowledge", Some("acquire")), + KnowledgeCommunicate => ("change_knowledge", Some("communicate")), + KnowledgeConceal => ("change_knowledge", Some("conceal")), + KnowledgeCorrect => ("change_knowledge", Some("correct")), + KnowledgeInvalidate => ("change_knowledge", Some("invalidate")), + MemoryRecord => ("change_memory", Some("record")), + MemoryRevise => ("change_memory", Some("revise")), + MemoryRetire => ("change_memory", Some("retire")), + PostureChange => ("change_posture", None), + PopulationJoin => ("change_population_membership", Some("join")), + PopulationLeave => ("change_population_membership", Some("leave")), + PopulationTransfer => ("change_population_membership", Some("transfer")), + PopulationSplit => ("change_population_lineage", Some("split")), + PopulationMerge => ("change_population_lineage", Some("merge")), + IdentityAdopt => ("change_identity", Some("adopt")), + IdentityDisclose => ("change_identity", Some("disclose")), + IdentityRestrict => ("change_identity", Some("restrict")), + IdentityRetire => ("change_identity", Some("retire")), + TopologyAdd => ("change_topology", Some("add")), + TopologyAlter => ("change_topology", Some("alter")), + TopologyOpen => ("change_topology", Some("open")), + TopologyClose => ("change_topology", Some("close")), + TopologyRetire => ("change_topology", Some("retire")), + AdmitEntity => ("admit_entity", None), + RetireEntity => ("retire_entity", None), + AdvanceWorldTime => ("advance_world_time", None), + } +} + +fn validate_permitted_mutation(permit: &MutationPermit, mutation: &WorldMutation) -> Result<()> { + if mutation.operation() != permit.operation { + return Err(anyhow!( + "world mutation operation does not match its permit" + )); + } + let roles = mutation.subject_roles(); + for binding in &permit.subject_bindings { + let actual = roles.get(&binding.role).cloned().unwrap_or_default(); + if actual.is_empty() || !actual.is_subset(&binding.allowed_subjects) { + return Err(anyhow!( + "world mutation exceeds the subjects allowed for role {:?}", + binding.role + )); + } + } + for (role, actual) in &roles { + if !permit + .subject_bindings + .iter() + .any(|binding| binding.role == *role && actual.is_subset(&binding.allowed_subjects)) + { + return Err(anyhow!( + "world mutation uses an unbound subject role {:?}", + role + )); + } + } + for (role, value) in mutation.string_roles() { + let constraint = permit.string_constraints.get(&role).ok_or_else(|| { + anyhow!( + "world mutation string for role {:?} has no permit constraint", + role + ) + })?; + let length = value.chars().count(); + if length < usize::from(constraint.minimum_length) + || length > usize::from(constraint.maximum_length) + || (!constraint.allowed_values.is_empty() + && !constraint.allowed_values.contains(&value)) + { + return Err(anyhow!( + "world mutation string for role {:?} is not permitted", + role + )); + } + } + for (role, value) in mutation.integer_roles() { + let bounds = permit.integer_bounds.get(&role).ok_or_else(|| { + anyhow!( + "world mutation integer for role {:?} has no permit bounds", + role + ) + })?; + if value < bounds.minimum || value > bounds.maximum { + return Err(anyhow!( + "world mutation integer for role {:?} is outside permit bounds", + role + )); + } + } + mutation.validate_local_shape() +} + +impl WorldMutation { + pub fn operation(&self) -> WorldMutationOperation { + use WorldMutation::*; + match self { + Relocate { .. } => WorldMutationOperation::Relocate, + TransferCustody { .. } => WorldMutationOperation::TransferCustody, + MutateResource { operation, .. } => match operation { + ResourceMutationOperation::Create => WorldMutationOperation::ResourceCreate, + ResourceMutationOperation::Transform => WorldMutationOperation::ResourceTransform, + ResourceMutationOperation::Consume => WorldMutationOperation::ResourceConsume, + ResourceMutationOperation::Damage => WorldMutationOperation::ResourceDamage, + ResourceMutationOperation::Repair => WorldMutationOperation::ResourceRepair, + ResourceMutationOperation::Split => WorldMutationOperation::ResourceSplit, + ResourceMutationOperation::Combine => WorldMutationOperation::ResourceCombine, + }, + ChangeCapability { operation, .. } => match operation { + CapabilityMutationOperation::Grant => WorldMutationOperation::CapabilityGrant, + CapabilityMutationOperation::Alter => WorldMutationOperation::CapabilityAlter, + CapabilityMutationOperation::Suspend => WorldMutationOperation::CapabilitySuspend, + CapabilityMutationOperation::Retire => WorldMutationOperation::CapabilityRetire, + }, + ChangeCondition { operation, .. } => match operation { + ConditionMutationOperation::Apply => WorldMutationOperation::ConditionApply, + ConditionMutationOperation::Alter => WorldMutationOperation::ConditionAlter, + ConditionMutationOperation::Clear => WorldMutationOperation::ConditionClear, + }, + ChangeCommitment { operation, .. } => match operation { + CommitmentMutationOperation::Create => WorldMutationOperation::CommitmentCreate, + CommitmentMutationOperation::Alter => WorldMutationOperation::CommitmentAlter, + CommitmentMutationOperation::Fulfill => WorldMutationOperation::CommitmentFulfill, + CommitmentMutationOperation::Default => WorldMutationOperation::CommitmentDefault, + CommitmentMutationOperation::Retire => WorldMutationOperation::CommitmentRetire, + }, + ChangeRelationship { operation, .. } => match operation { + RelationshipMutationOperation::Create => WorldMutationOperation::RelationshipCreate, + RelationshipMutationOperation::Alter => WorldMutationOperation::RelationshipAlter, + RelationshipMutationOperation::Retire => WorldMutationOperation::RelationshipRetire, + }, + ChangePressure { operation, .. } => match operation { + PressureMutationOperation::Create => WorldMutationOperation::PressureCreate, + PressureMutationOperation::Advance => WorldMutationOperation::PressureAdvance, + PressureMutationOperation::Reduce => WorldMutationOperation::PressureReduce, + PressureMutationOperation::Resolve => WorldMutationOperation::PressureResolve, + PressureMutationOperation::Retire => WorldMutationOperation::PressureRetire, + }, + ChangeKnowledge { operation, .. } => match operation { + KnowledgeMutationOperation::Acquire => WorldMutationOperation::KnowledgeAcquire, + KnowledgeMutationOperation::Communicate => { + WorldMutationOperation::KnowledgeCommunicate + } + KnowledgeMutationOperation::Conceal => WorldMutationOperation::KnowledgeConceal, + KnowledgeMutationOperation::Correct => WorldMutationOperation::KnowledgeCorrect, + KnowledgeMutationOperation::Invalidate => { + WorldMutationOperation::KnowledgeInvalidate + } + }, + ChangeMemory { operation, .. } => match operation { + MemoryMutationOperation::Record => WorldMutationOperation::MemoryRecord, + MemoryMutationOperation::Revise => WorldMutationOperation::MemoryRevise, + MemoryMutationOperation::Retire => WorldMutationOperation::MemoryRetire, + }, + ChangePosture { .. } => WorldMutationOperation::PostureChange, + ChangePopulationMembership { operation, .. } => match operation { + PopulationMembershipOperation::Join => WorldMutationOperation::PopulationJoin, + PopulationMembershipOperation::Leave => WorldMutationOperation::PopulationLeave, + PopulationMembershipOperation::Transfer => { + WorldMutationOperation::PopulationTransfer + } + }, + ChangePopulationLineage { operation, .. } => match operation { + PopulationLineageOperation::Split => WorldMutationOperation::PopulationSplit, + PopulationLineageOperation::Merge => WorldMutationOperation::PopulationMerge, + }, + ChangeIdentity { operation, .. } => match operation { + IdentityMutationOperation::Adopt => WorldMutationOperation::IdentityAdopt, + IdentityMutationOperation::Disclose => WorldMutationOperation::IdentityDisclose, + IdentityMutationOperation::Restrict => WorldMutationOperation::IdentityRestrict, + IdentityMutationOperation::Retire => WorldMutationOperation::IdentityRetire, + }, + ChangeTopology { operation, .. } => match operation { + TopologyMutationOperation::Add => WorldMutationOperation::TopologyAdd, + TopologyMutationOperation::Alter => WorldMutationOperation::TopologyAlter, + TopologyMutationOperation::Open => WorldMutationOperation::TopologyOpen, + TopologyMutationOperation::Close => WorldMutationOperation::TopologyClose, + TopologyMutationOperation::Retire => WorldMutationOperation::TopologyRetire, + }, + AdmitEntity { .. } => WorldMutationOperation::AdmitEntity, + RetireEntity { .. } => WorldMutationOperation::RetireEntity, + AdvanceWorldTime { .. } => WorldMutationOperation::AdvanceWorldTime, + } + } + + fn subject_roles(&self) -> BTreeMap> { + let mut roles = BTreeMap::>::new(); + let mut add = |role, subject: &SubjectRef| { + roles.entry(role).or_default().insert(subject.clone()); + }; + use WorldMutation::*; + match self { + Relocate { + subject, + from_place, + to_place, + .. + } => { + add(MutationSubjectRole::Subject, subject); + add(MutationSubjectRole::OriginPlace, from_place); + add(MutationSubjectRole::DestinationPlace, to_place); + } + TransferCustody { + resource, + from_custodian, + to_custodian, + } => { + add(MutationSubjectRole::Resource, resource); + add(MutationSubjectRole::SourceCustodian, from_custodian); + add(MutationSubjectRole::DestinationCustodian, to_custodian); + } + MutateResource { + resource, + custodian, + related_resources, + .. + } => { + add(MutationSubjectRole::Resource, resource); + if let Some(custodian) = custodian { + add(MutationSubjectRole::Owner, custodian); + } + for related in related_resources { + add(MutationSubjectRole::RelatedResource, related); + } + } + ChangeCapability { subject, .. } + | ChangeCondition { subject, .. } + | ChangeMemory { subject, .. } + | ChangePosture { subject, .. } => add(MutationSubjectRole::Subject, subject), + ChangeCommitment { + subject, + counterparty, + .. + } => { + add(MutationSubjectRole::Subject, subject); + if let Some(counterparty) = counterparty { + add(MutationSubjectRole::Counterparty, counterparty); + } + } + ChangeRelationship { source, target, .. } => { + add(MutationSubjectRole::RelationshipSource, source); + add(MutationSubjectRole::RelationshipTarget, target); + } + ChangePressure { + pressure, owner, .. + } => { + add(MutationSubjectRole::Pressure, pressure); + add(MutationSubjectRole::Owner, owner); + } + ChangeKnowledge { + proposition, + knower, + speaker, + recipients, + channel, + .. + } => { + add(MutationSubjectRole::Proposition, proposition); + if let Some(knower) = knower { + add(MutationSubjectRole::Knower, knower); + } + if let Some(speaker) = speaker { + add(MutationSubjectRole::Speaker, speaker); + } + for recipient in recipients { + add(MutationSubjectRole::Recipient, recipient); + } + if let Some(channel) = channel { + add(MutationSubjectRole::Channel, channel); + } + } + ChangePopulationMembership { + actor, + source_population, + destination_population, + .. + } => { + add(MutationSubjectRole::Actor, actor); + if let Some(population) = source_population { + add(MutationSubjectRole::SourcePopulation, population); + } + if let Some(population) = destination_population { + add(MutationSubjectRole::DestinationPopulation, population); + } + } + ChangePopulationLineage { + parent_populations, + child_populations, + remainder_population, + .. + } => { + for population in parent_populations { + add(MutationSubjectRole::ParentPopulation, population); + } + for population in child_populations { + add(MutationSubjectRole::ChildPopulation, population); + } + if let Some(population) = remainder_population { + add(MutationSubjectRole::ChildPopulation, population); + } + } + ChangeIdentity { + subject, audience, .. + } => { + add(MutationSubjectRole::Subject, subject); + for recipient in audience { + add(MutationSubjectRole::Recipient, recipient); + } + } + ChangeTopology { + from_place, + to_place, + .. + } => { + add(MutationSubjectRole::TopologyOrigin, from_place); + add(MutationSubjectRole::TopologyDestination, to_place); + } + AdmitEntity { subject, .. } | RetireEntity { subject, .. } => { + add(MutationSubjectRole::Entity, subject) + } + AdvanceWorldTime { campaign, .. } => add(MutationSubjectRole::Subject, campaign), + } + roles + } + + fn string_roles(&self) -> Vec<(MutationStringRole, String)> { + use WorldMutation::*; + let mut values = Vec::new(); + match self { + Relocate { route_id, .. } => { + values.push((MutationStringRole::RouteId, route_id.clone())) + } + MutateResource { + resource_kind, + recipe_id, + .. + } => { + if let Some(value) = resource_kind { + values.push((MutationStringRole::ResourceKind, value.clone())); + } + if let Some(value) = recipe_id { + values.push((MutationStringRole::RecipeId, value.clone())); + } + } + ChangeCapability { capability_id, .. } => { + values.push((MutationStringRole::CapabilityId, capability_id.clone())) + } + ChangeCondition { condition_id, .. } => { + values.push((MutationStringRole::ConditionId, condition_id.clone())) + } + ChangeCommitment { commitment_id, .. } => { + values.push((MutationStringRole::CommitmentId, commitment_id.clone())) + } + ChangeRelationship { + relationship_id, .. + } => values.push((MutationStringRole::RelationshipId, relationship_id.clone())), + ChangePressure { label, .. } => { + if let Some(value) = label { + values.push((MutationStringRole::PressureLabel, value.clone())); + } + } + ChangeMemory { + memory_id, + event_id, + .. + } => { + values.push((MutationStringRole::MemoryId, memory_id.clone())); + if let Some(value) = event_id { + values.push((MutationStringRole::EventId, value.clone())); + } + } + ChangePosture { posture, .. } => { + values.push((MutationStringRole::Posture, posture.clone())) + } + ChangeIdentity { + handle_id, + handle_value, + .. + } => { + values.push((MutationStringRole::IdentityHandleId, handle_id.clone())); + if let Some(value) = handle_value { + values.push((MutationStringRole::IdentityHandleValue, value.clone())); + } + } + ChangeTopology { edge_id, .. } => { + values.push((MutationStringRole::TopologyEdgeId, edge_id.clone())) + } + AdmitEntity { + admission_receipt_id, + .. + } => values.push(( + MutationStringRole::AdmissionReceiptId, + admission_receipt_id.clone(), + )), + TransferCustody { .. } + | ChangeKnowledge { .. } + | ChangePopulationMembership { .. } + | ChangePopulationLineage { .. } + | RetireEntity { .. } + | AdvanceWorldTime { .. } => {} + } + values + } + + fn integer_roles(&self) -> Vec<(MutationIntegerRole, i64)> { + use WorldMutation::*; + let mut values = Vec::new(); + match self { + MutateResource { + quantity, + integrity, + .. + } => { + if let Some(value) = quantity { + values.push((MutationIntegerRole::Quantity, *value)); + } + if let Some(value) = integrity { + values.push((MutationIntegerRole::Integrity, *value)); + } + } + ChangeCondition { severity, .. } => { + if let Some(value) = severity { + values.push((MutationIntegerRole::Severity, *value)); + } + } + ChangePressure { amount, .. } => { + if let Some(value) = amount { + values.push((MutationIntegerRole::PressureAmount, *value)); + } + } + ChangeTopology { travel_minutes, .. } => { + if let Some(value) = travel_minutes { + values.push((MutationIntegerRole::TravelMinutes, *value)); + } + } + AdvanceWorldTime { minutes, .. } => { + values.push((MutationIntegerRole::WorldMinutes, *minutes)); + } + Relocate { .. } + | TransferCustody { .. } + | ChangeCapability { .. } + | ChangeCommitment { .. } + | ChangeRelationship { .. } + | ChangeKnowledge { .. } + | ChangeMemory { .. } + | ChangePosture { .. } + | ChangePopulationMembership { .. } + | ChangePopulationLineage { .. } + | ChangeIdentity { .. } + | AdmitEntity { .. } + | RetireEntity { .. } => {} + } + values + } + + fn validate_local_shape(&self) -> Result<()> { + let nonempty = |label: &str, value: &str| -> Result<()> { + if value.trim().is_empty() { + Err(anyhow!("world mutation {label} is empty")) + } else { + Ok(()) + } + }; + for subject in self.subject_roles().values().flatten() { + nonempty("subject id", &subject.id)?; + } + use WorldMutation::*; + match self { + Relocate { route_id, .. } => nonempty("route id", route_id)?, + MutateResource { + operation, + custodian, + related_resources, + quantity, + integrity, + .. + } => match operation { + ResourceMutationOperation::Create + | ResourceMutationOperation::Consume + | ResourceMutationOperation::Split + if quantity.is_none() => + { + return Err(anyhow!("resource mutation requires an exact quantity")); + } + ResourceMutationOperation::Damage | ResourceMutationOperation::Repair + if integrity.is_none() => + { + return Err(anyhow!("resource mutation requires an integrity amount")); + } + ResourceMutationOperation::Transform + | ResourceMutationOperation::Split + | ResourceMutationOperation::Combine + if related_resources.is_empty() => + { + return Err(anyhow!("resource mutation requires related resources")); + } + ResourceMutationOperation::Create if custodian.is_none() => { + return Err(anyhow!("resource creation requires an exact custodian")); + } + _ => {} + }, + ChangeCapability { capability_id, .. } => nonempty("capability id", capability_id)?, + ChangeCondition { condition_id, .. } => nonempty("condition id", condition_id)?, + ChangeCommitment { commitment_id, .. } => nonempty("commitment id", commitment_id)?, + ChangeRelationship { + relationship_id, .. + } => nonempty("relationship id", relationship_id)?, + ChangePressure { + operation, amount, .. + } if matches!( + operation, + PressureMutationOperation::Advance | PressureMutationOperation::Reduce + ) && amount.is_none() => + { + return Err(anyhow!("pressure change requires an exact amount")); + } + ChangeKnowledge { + operation, + knower, + speaker, + recipients, + .. + } => match operation { + KnowledgeMutationOperation::Acquire + | KnowledgeMutationOperation::Conceal + | KnowledgeMutationOperation::Correct + | KnowledgeMutationOperation::Invalidate + if knower.is_none() => + { + return Err(anyhow!("knowledge mutation requires an exact knower")); + } + KnowledgeMutationOperation::Communicate + if speaker.is_none() || recipients.is_empty() => + { + return Err(anyhow!( + "knowledge communication requires a speaker and exact recipients" + )); + } + _ => {} + }, + ChangeMemory { memory_id, .. } => nonempty("memory id", memory_id)?, + ChangePosture { posture, .. } => nonempty("posture", posture)?, + ChangePopulationMembership { + operation, + source_population, + destination_population, + .. + } => match operation { + PopulationMembershipOperation::Join if destination_population.is_none() => { + return Err(anyhow!("population join requires a destination")); + } + PopulationMembershipOperation::Leave if source_population.is_none() => { + return Err(anyhow!("population leave requires a source")); + } + PopulationMembershipOperation::Transfer + if source_population.is_none() || destination_population.is_none() => + { + return Err(anyhow!( + "population transfer requires source and destination" + )); + } + _ => {} + }, + ChangePopulationLineage { + operation, + parent_populations, + child_populations, + remainder_population, + } => match operation { + PopulationLineageOperation::Split + if parent_populations.len() != 1 + || child_populations.is_empty() + || remainder_population.is_none() => + { + return Err(anyhow!( + "population split requires one parent, children, and a remainder" + )); + } + PopulationLineageOperation::Merge + if parent_populations.len() < 2 || child_populations.len() != 1 => + { + return Err(anyhow!( + "population merge requires multiple parents and one child" + )); + } + _ => {} + }, + ChangeIdentity { + operation, + handle_id, + handle_value, + audience, + .. + } => { + nonempty("identity handle id", handle_id)?; + if matches!(operation, IdentityMutationOperation::Adopt) + && handle_value.as_deref().is_none_or(str::is_empty) + { + return Err(anyhow!("identity adoption requires a handle value")); + } + if matches!( + operation, + IdentityMutationOperation::Disclose | IdentityMutationOperation::Restrict + ) && audience.is_empty() + { + return Err(anyhow!( + "identity disclosure change requires an exact audience" + )); + } + } + ChangeTopology { + operation, + edge_id, + travel_minutes, + .. + } => { + nonempty("topology edge id", edge_id)?; + if matches!( + operation, + TopologyMutationOperation::Add | TopologyMutationOperation::Alter + ) && travel_minutes.is_none() + { + return Err(anyhow!( + "topology creation or alteration requires travel time" + )); + } + } + AdmitEntity { + initial_components, + admission_receipt_id, + .. + } => { + if initial_components.is_empty() { + return Err(anyhow!("entity admission requires initial components")); + } + nonempty("admission receipt id", admission_receipt_id)?; + } + RetireEntity { reason, .. } => nonempty("retirement reason", reason)?, + AdvanceWorldTime { minutes, .. } if *minutes <= 0 => { + return Err(anyhow!("world time must advance by a positive duration")); + } + TransferCustody { .. } | ChangePressure { .. } | AdvanceWorldTime { .. } => {} + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use chrono::Duration; + + fn subject(kind: SubjectKind, id: &str) -> SubjectRef { + SubjectRef { + kind, + id: id.into(), + } + } + + fn exact(values: &[&str]) -> StringConstraint { + StringConstraint { + allowed_values: values.iter().map(|value| (*value).into()).collect(), + minimum_length: 1, + maximum_length: 240, + } + } + + fn relocate_permit() -> MutationPermit { + MutationPermit { + id: "permit:move-east".into(), + operation: WorldMutationOperation::Relocate, + subject_bindings: vec![ + MutationSubjectBinding { + role: MutationSubjectRole::Subject, + allowed_subjects: BTreeSet::from([subject(SubjectKind::Actor, "actor:ash")]), + }, + MutationSubjectBinding { + role: MutationSubjectRole::OriginPlace, + allowed_subjects: BTreeSet::from([subject(SubjectKind::Place, "place:camp")]), + }, + MutationSubjectBinding { + role: MutationSubjectRole::DestinationPlace, + allowed_subjects: BTreeSet::from([subject( + SubjectKind::Place, + "place:east-trail", + )]), + }, + ], + string_constraints: BTreeMap::from([( + MutationStringRole::RouteId, + exact(&["route:camp-east"]), + )]), + integer_bounds: BTreeMap::new(), + maximum_uses: 1, + } + } + + fn envelope() -> MutationAuthorityEnvelope { + let mut envelope = MutationAuthorityEnvelope { + schema: "ghostlight.mutation_authority_envelope.v1".into(), + id: "authority:move-east".into(), + campaign_id: Uuid::nil(), + world_revision: 40, + resolution_epoch: None, + procedure: MutationProcedure::ForegroundAttempt, + source_subject: Some(subject(SubjectKind::Actor, "actor:ash")), + outcome: MutationOutcomeBinding::Foreground(OutcomeBand::Success), + effect_ceiling: "Ash may reach the eastern trail; no other subject moves.".into(), + permits: vec![relocate_permit()], + authority_receipt_ids: BTreeSet::from(["assessment:move-east".into()]), + expires_at: Utc::now() + Duration::minutes(5), + digest: String::new(), + }; + envelope.digest = envelope_digest(&envelope).unwrap(); + envelope + } + + fn valid_batch(envelope: &MutationAuthorityEnvelope) -> WorldMutationBatch { + let mut batch = WorldMutationBatch { + schema: "ghostlight.world_mutation_batch.v1".into(), + id: "batch:move-east".into(), + campaign_id: envelope.campaign_id, + expected_world_revision: envelope.world_revision, + expected_resolution_epoch: envelope.resolution_epoch, + authority_envelope_digest: envelope.digest.clone(), + source_receipt_id: "assessment:move-east".into(), + means_digest: Some("sha256:means".into()), + intended_effect_digest: Some("sha256:intent".into()), + mutations: vec![PermittedWorldMutation { + permit_id: "permit:move-east".into(), + mutation: WorldMutation::Relocate { + subject: subject(SubjectKind::Actor, "actor:ash"), + from_place: subject(SubjectKind::Place, "place:camp"), + to_place: subject(SubjectKind::Place, "place:east-trail"), + route_id: "route:camp-east".into(), + }, + }], + digest: String::new(), + }; + batch.digest = mutation_batch_digest(&batch).unwrap(); + batch + } + + #[test] + fn exact_permit_accepts_only_its_bound_transition() { + let envelope = envelope(); + let batch = valid_batch(&envelope); + validate_batch_structure(&envelope, &batch, Utc::now()).unwrap(); + + let mut stolen = batch.clone(); + let WorldMutation::Relocate { subject, .. } = &mut stolen.mutations[0].mutation else { + panic!("fixture mutation changed kind"); + }; + *subject = subject_ref(SubjectKind::Actor, "actor:sable"); + stolen.digest = mutation_batch_digest(&stolen).unwrap(); + assert!( + validate_batch_structure(&envelope, &stolen, Utc::now()) + .unwrap_err() + .to_string() + .contains("role Subject") + ); + } + + #[test] + fn stale_or_partially_invalid_batch_has_no_structural_admission() { + let envelope = envelope(); + let mut batch = valid_batch(&envelope); + batch.mutations.push(batch.mutations[0].clone()); + batch.digest = mutation_batch_digest(&batch).unwrap(); + assert!( + validate_batch_structure(&envelope, &batch, Utc::now()) + .unwrap_err() + .to_string() + .contains("use count") + ); + + let mut stale = valid_batch(&envelope); + stale.expected_world_revision += 1; + stale.digest = mutation_batch_digest(&stale).unwrap(); + assert!( + validate_batch_structure(&envelope, &stale, Utc::now()) + .unwrap_err() + .to_string() + .contains("authority snapshot") + ); + } + + #[test] + fn action_specific_schema_omits_unavailable_mutation_variants() { + let envelope = envelope(); + let schema = mutation_batch_json_schema(&envelope).unwrap(); + let rendered = serde_json::to_string(&schema).unwrap(); + assert!(rendered.contains("relocate")); + assert!(!rendered.contains("transfer_custody")); + assert!(!rendered.contains("change_identity")); + + let instance = serde_json::to_value(valid_batch(&envelope)).unwrap(); + assert!( + jsonschema::validator_for(&schema) + .unwrap() + .is_valid(&instance) + ); + + let mut stolen = valid_batch(&envelope); + let WorldMutation::Relocate { to_place, .. } = &mut stolen.mutations[0].mutation else { + unreachable!(); + }; + *to_place = subject(SubjectKind::Place, "place:unreachable"); + stolen.digest = mutation_batch_digest(&stolen).unwrap(); + let stolen = serde_json::to_value(stolen).unwrap(); + assert!( + !jsonschema::validator_for(&schema) + .unwrap() + .is_valid(&stolen) + ); + } + + #[test] + fn identity_disclosure_requires_exact_audience_authority() { + let sable = subject(SubjectKind::Actor, "actor:sable"); + let ash = subject(SubjectKind::Actor, "actor:ash"); + let stranger = subject(SubjectKind::Actor, "actor:stranger"); + let permit = MutationPermit { + id: "permit:sable-disclosure".into(), + operation: WorldMutationOperation::IdentityDisclose, + subject_bindings: vec![ + MutationSubjectBinding { + role: MutationSubjectRole::Subject, + allowed_subjects: BTreeSet::from([sable.clone()]), + }, + MutationSubjectBinding { + role: MutationSubjectRole::Recipient, + allowed_subjects: BTreeSet::from([ash.clone()]), + }, + ], + string_constraints: BTreeMap::from([ + ( + MutationStringRole::IdentityHandleId, + exact(&["identity:sable:given-handle"]), + ), + (MutationStringRole::IdentityHandleValue, exact(&["Sable"])), + ]), + integer_bounds: BTreeMap::new(), + maximum_uses: 1, + }; + let mutation = WorldMutation::ChangeIdentity { + subject: sable, + operation: IdentityMutationOperation::Disclose, + handle_id: "identity:sable:given-handle".into(), + handle_value: Some("Sable".into()), + audience: vec![ash], + }; + validate_permitted_mutation(&permit, &mutation).unwrap(); + + let mut leaked = mutation; + let WorldMutation::ChangeIdentity { audience, .. } = &mut leaked else { + unreachable!(); + }; + audience.push(stranger); + assert!(validate_permitted_mutation(&permit, &leaked).is_err()); + } + + fn component_world() -> ComponentWorldState { + let campaign = subject(SubjectKind::Campaign, "campaign:test"); + let ash = subject(SubjectKind::Actor, "actor:ash"); + let sable = subject(SubjectKind::Actor, "actor:sable"); + let rival = subject(SubjectKind::Actor, "actor:rival"); + let clinic = subject(SubjectKind::Institution, "institution:clinic"); + let camp = subject(SubjectKind::Place, "place:camp"); + let trail = subject(SubjectKind::Place, "place:trail"); + let refugees = subject(SubjectKind::Population, "population:refugees"); + let medicine = subject(SubjectKind::Resource, "resource:medicine-lot"); + let proposition = subject(SubjectKind::Proposition, "proposition:east-trail"); + let mut subjects = BTreeMap::new(); + for value in [ + campaign.clone(), + ash.clone(), + sable.clone(), + rival, + clinic, + camp.clone(), + trail, + refugees.clone(), + medicine.clone(), + proposition.clone(), + ] { + subjects.insert( + value.clone(), + TypedSubject { + schema: "ghostlight.typed_subject.v1".into(), + subject: value, + lifecycle: LifecycleStatus::Active, + admitted_components: BTreeSet::new(), + version: 4, + }, + ); + } + ComponentWorldState { + schema: "ghostlight.component_world_state.v1".into(), + campaign_id: Uuid::nil(), + revision: 4, + resolution_epoch: 2, + world_time: Utc::now(), + subjects, + occupancy: BTreeMap::from([ + (ash.clone(), camp.clone()), + (sable.clone(), camp.clone()), + (campaign, camp.clone()), + ]), + custody: BTreeMap::from([(medicine.clone(), sable.clone())]), + resources: BTreeMap::from([( + medicine.clone(), + ResourceComponentState { + schema: "ghostlight.resource_component.v1".into(), + resource: medicine, + resource_kind: "medicine".into(), + quantity: 10, + integrity: 100, + qualities: BTreeSet::new(), + version: 4, + }, + )]), + capabilities: BTreeMap::new(), + conditions: BTreeMap::new(), + commitments: BTreeMap::new(), + relationships: BTreeMap::new(), + pressures: BTreeMap::new(), + knowledge: BTreeMap::from([( + KnowledgeKey { + knower: sable, + proposition, + }, + KnowledgeComponentState { + status: "known".into(), + source: None, + channel: None, + concealed_from: BTreeSet::new(), + version: 4, + }, + )]), + memories: BTreeMap::new(), + postures: BTreeMap::new(), + memberships: BTreeMap::from([( + MembershipKey { + actor: ash, + population: refugees, + }, + PopulationMembershipState { + active: true, + version: 4, + }, + )]), + population_lineages: BTreeMap::new(), + identities: BTreeMap::new(), + topology: BTreeMap::new(), + } + } + + fn permit( + id: &str, + operation: WorldMutationOperation, + subjects: Vec<(MutationSubjectRole, BTreeSet)>, + strings: Vec<(MutationStringRole, StringConstraint)>, + integers: Vec<(MutationIntegerRole, IntegerBounds)>, + ) -> MutationPermit { + MutationPermit { + id: id.into(), + operation, + subject_bindings: subjects + .into_iter() + .map(|(role, allowed_subjects)| MutationSubjectBinding { + role, + allowed_subjects, + }) + .collect(), + string_constraints: strings.into_iter().collect(), + integer_bounds: integers.into_iter().collect(), + maximum_uses: 1, + } + } + + fn envelope_for( + state: &ComponentWorldState, + permits: Vec, + ) -> MutationAuthorityEnvelope { + let mut envelope = MutationAuthorityEnvelope { + schema: "ghostlight.mutation_authority_envelope.v1".into(), + id: "authority:component-test".into(), + campaign_id: state.campaign_id, + world_revision: state.revision, + resolution_epoch: Some(state.resolution_epoch), + procedure: MutationProcedure::StrategicOutcome, + source_subject: Some(subject(SubjectKind::Actor, "actor:sable")), + outcome: MutationOutcomeBinding::Strategic(StrategicOutcomeBand::Success), + effect_ceiling: "Only the exact permitted component transitions may commit.".into(), + permits, + authority_receipt_ids: BTreeSet::from(["outcome:test".into()]), + expires_at: Utc::now() + Duration::minutes(5), + digest: String::new(), + }; + envelope.digest = envelope_digest(&envelope).unwrap(); + envelope + } + + fn batch_for( + state: &ComponentWorldState, + envelope: &MutationAuthorityEnvelope, + mutations: Vec<(&str, WorldMutation)>, + ) -> WorldMutationBatch { + let mut batch = WorldMutationBatch { + schema: "ghostlight.world_mutation_batch.v1".into(), + id: format!("batch:{}", Uuid::new_v4().simple()), + campaign_id: state.campaign_id, + expected_world_revision: state.revision, + expected_resolution_epoch: Some(state.resolution_epoch), + authority_envelope_digest: envelope.digest.clone(), + source_receipt_id: "outcome:test".into(), + means_digest: Some("sha256:means".into()), + intended_effect_digest: Some("sha256:intent".into()), + mutations: mutations + .into_iter() + .map(|(permit_id, mutation)| PermittedWorldMutation { + permit_id: permit_id.into(), + mutation, + }) + .collect(), + digest: String::new(), + }; + batch.digest = mutation_batch_digest(&batch).unwrap(); + batch + } + + #[test] + fn component_batch_is_atomic_when_a_late_mutation_fails() { + let state = component_world(); + let medicine = subject(SubjectKind::Resource, "resource:medicine-lot"); + let sable = subject(SubjectKind::Actor, "actor:sable"); + let clinic = subject(SubjectKind::Institution, "institution:clinic"); + let camp = subject(SubjectKind::Place, "place:camp"); + let trail = subject(SubjectKind::Place, "place:trail"); + let permits = vec![ + permit( + "permit:transfer", + WorldMutationOperation::TransferCustody, + vec![ + ( + MutationSubjectRole::Resource, + BTreeSet::from([medicine.clone()]), + ), + ( + MutationSubjectRole::SourceCustodian, + BTreeSet::from([sable.clone()]), + ), + ( + MutationSubjectRole::DestinationCustodian, + BTreeSet::from([clinic.clone()]), + ), + ], + vec![], + vec![], + ), + permit( + "permit:move", + WorldMutationOperation::Relocate, + vec![ + ( + MutationSubjectRole::Subject, + BTreeSet::from([sable.clone()]), + ), + ( + MutationSubjectRole::OriginPlace, + BTreeSet::from([camp.clone()]), + ), + ( + MutationSubjectRole::DestinationPlace, + BTreeSet::from([trail.clone()]), + ), + ], + vec![(MutationStringRole::RouteId, exact(&["route:camp-trail"]))], + vec![], + ), + ]; + let envelope = envelope_for(&state, permits); + let batch = batch_for( + &state, + &envelope, + vec![ + ( + "permit:transfer", + WorldMutation::TransferCustody { + resource: medicine, + from_custodian: sable.clone(), + to_custodian: clinic, + }, + ), + ( + "permit:move", + WorldMutation::Relocate { + subject: sable, + from_place: camp, + to_place: trail, + route_id: "route:camp-trail".into(), + }, + ), + ], + ); + let mut invalid_state = state.clone(); + invalid_state + .occupancy + .remove(&subject(SubjectKind::Actor, "actor:sable")); + assert!( + apply_component_world_batch(&invalid_state, &envelope, &batch, Utc::now()).is_err() + ); + assert_eq!(invalid_state.custody, state.custody); + assert_eq!(invalid_state.revision, state.revision); + } + + #[test] + fn resource_split_and_transfer_conserve_quantity_and_exact_custody() { + let state = component_world(); + let source = subject(SubjectKind::Resource, "resource:medicine-lot"); + let child = subject(SubjectKind::Resource, "resource:medicine-dose"); + let sable = subject(SubjectKind::Actor, "actor:sable"); + let clinic = subject(SubjectKind::Institution, "institution:clinic"); + let permits = vec![ + permit( + "permit:split", + WorldMutationOperation::ResourceSplit, + vec![ + ( + MutationSubjectRole::Resource, + BTreeSet::from([source.clone()]), + ), + ( + MutationSubjectRole::RelatedResource, + BTreeSet::from([child.clone()]), + ), + ], + vec![( + MutationStringRole::RecipeId, + exact(&["recipe:measure-dose"]), + )], + vec![( + MutationIntegerRole::Quantity, + IntegerBounds { + minimum: 1, + maximum: 4, + }, + )], + ), + permit( + "permit:transfer-dose", + WorldMutationOperation::TransferCustody, + vec![ + ( + MutationSubjectRole::Resource, + BTreeSet::from([child.clone()]), + ), + ( + MutationSubjectRole::SourceCustodian, + BTreeSet::from([sable.clone()]), + ), + ( + MutationSubjectRole::DestinationCustodian, + BTreeSet::from([clinic.clone()]), + ), + ], + vec![], + vec![], + ), + ]; + let envelope = envelope_for(&state, permits); + let batch = batch_for( + &state, + &envelope, + vec![ + ( + "permit:split", + WorldMutation::MutateResource { + resource: source.clone(), + operation: ResourceMutationOperation::Split, + custodian: None, + related_resources: vec![child.clone()], + resource_kind: None, + recipe_id: Some("recipe:measure-dose".into()), + quantity: Some(4), + integrity: None, + }, + ), + ( + "permit:transfer-dose", + WorldMutation::TransferCustody { + resource: child.clone(), + from_custodian: sable, + to_custodian: clinic.clone(), + }, + ), + ], + ); + let applied = apply_component_world_batch(&state, &envelope, &batch, Utc::now()).unwrap(); + assert_eq!(applied.state.resources[&source].quantity, 6); + assert_eq!(applied.state.resources[&child].quantity, 4); + assert_eq!(applied.state.custody[&child], clinic); + assert_eq!( + applied.state.resources[&source].quantity + applied.state.resources[&child].quantity, + 10 + ); + assert_eq!(applied.state.revision, 5); + } + + #[test] + fn component_state_batch_and_exact_proof_rows_commit_in_one_cultcache_cas() { + let state = component_world(); + let campaign = subject(SubjectKind::Campaign, "campaign:test"); + let permit = permit( + "permit:time", + WorldMutationOperation::AdvanceWorldTime, + vec![( + MutationSubjectRole::Subject, + BTreeSet::from([campaign.clone()]), + )], + vec![], + vec![( + MutationIntegerRole::WorldMinutes, + IntegerBounds { + minimum: 30, + maximum: 30, + }, + )], + ); + let envelope = envelope_for(&state, vec![permit]); + let batch = batch_for( + &state, + &envelope, + vec![( + "permit:time", + WorldMutation::AdvanceWorldTime { + campaign, + minutes: 30, + }, + )], + ); + let applied = apply_component_world_batch(&state, &envelope, &batch, Utc::now()).unwrap(); + let path = std::env::temp_dir().join(format!( + "ghostlight-transition-store-{}.cc", + Uuid::new_v4().simple() + )); + let store = crate::persistence::CampaignStore::open(&path).unwrap(); + let expected = store.create_component_world_state(&state).unwrap(); + store + .commit_world_mutation_batch( + &expected, + &applied.state, + &envelope, + &batch, + &applied.receipt, + ) + .unwrap(); + let persisted = store + .load::("component_world_state.v1", &state.campaign_id.to_string()) + .unwrap() + .unwrap() + .1; + assert_eq!(persisted, applied.state); + assert_eq!( + store.keys("mutation_authority_envelope.v1").unwrap().len(), + 1 + ); + assert_eq!(store.keys("world_mutation_batch.v1").unwrap().len(), 1); + assert_eq!(store.keys("world_mutation_receipt.v1").unwrap().len(), 1); + assert!( + store + .commit_world_mutation_batch( + &expected, + &applied.state, + &envelope, + &batch, + &applied.receipt, + ) + .is_err() + ); + assert_eq!(store.keys("world_mutation_receipt.v1").unwrap().len(), 1); + drop(store); + std::fs::remove_file(path).unwrap(); + } + + #[test] + fn communicated_knowledge_reaches_only_exact_recipients() { + let state = component_world(); + let sable = subject(SubjectKind::Actor, "actor:sable"); + let ash = subject(SubjectKind::Actor, "actor:ash"); + let rival = subject(SubjectKind::Actor, "actor:rival"); + let proposition = subject(SubjectKind::Proposition, "proposition:east-trail"); + let permit = permit( + "permit:tell-ash", + WorldMutationOperation::KnowledgeCommunicate, + vec![ + ( + MutationSubjectRole::Proposition, + BTreeSet::from([proposition.clone()]), + ), + ( + MutationSubjectRole::Speaker, + BTreeSet::from([sable.clone()]), + ), + ( + MutationSubjectRole::Recipient, + BTreeSet::from([ash.clone()]), + ), + ], + vec![], + vec![], + ); + let envelope = envelope_for(&state, vec![permit]); + let batch = batch_for( + &state, + &envelope, + vec![( + "permit:tell-ash", + WorldMutation::ChangeKnowledge { + operation: KnowledgeMutationOperation::Communicate, + proposition: proposition.clone(), + knower: None, + speaker: Some(sable), + recipients: vec![ash.clone()], + channel: None, + }, + )], + ); + let applied = apply_component_world_batch(&state, &envelope, &batch, Utc::now()).unwrap(); + assert!(applied.state.knowledge.contains_key(&KnowledgeKey { + knower: ash, + proposition: proposition.clone() + })); + assert!(!applied.state.knowledge.contains_key(&KnowledgeKey { + knower: rival, + proposition + })); + } + + #[test] + fn sable_identity_survives_population_and_occupancy_changes_without_global_disclosure() { + let mut state = component_world(); + let sable = subject(SubjectKind::Actor, "actor:sable"); + let ash = subject(SubjectKind::Actor, "actor:ash"); + let rival = subject(SubjectKind::Actor, "actor:rival"); + state.identities.insert( + "identity:sable:given-handle".into(), + IdentityHandleState { + schema: "ghostlight.identity_handle.v1".into(), + id: "identity:sable:given-handle".into(), + subject: sable.clone(), + value: "Sable".into(), + active: true, + known_by: BTreeSet::from([sable.clone()]), + restricted_to: BTreeSet::new(), + source_revision: 4, + }, + ); + let permit = permit( + "permit:disclose-sable", + WorldMutationOperation::IdentityDisclose, + vec![ + ( + MutationSubjectRole::Subject, + BTreeSet::from([sable.clone()]), + ), + ( + MutationSubjectRole::Recipient, + BTreeSet::from([ash.clone()]), + ), + ], + vec![( + MutationStringRole::IdentityHandleId, + exact(&["identity:sable:given-handle"]), + )], + vec![], + ); + let envelope = envelope_for(&state, vec![permit]); + let batch = batch_for( + &state, + &envelope, + vec![( + "permit:disclose-sable", + WorldMutation::ChangeIdentity { + subject: sable.clone(), + operation: IdentityMutationOperation::Disclose, + handle_id: "identity:sable:given-handle".into(), + handle_value: None, + audience: vec![ash.clone()], + }, + )], + ); + let applied = apply_component_world_batch(&state, &envelope, &batch, Utc::now()).unwrap(); + let handle = &applied.state.identities["identity:sable:given-handle"]; + assert!(handle.known_by.contains(&sable)); + assert!(handle.known_by.contains(&ash)); + assert!(!handle.known_by.contains(&rival)); + assert_eq!(handle.subject, sable); + } + + fn subject_ref(kind: SubjectKind, id: &str) -> SubjectRef { + subject(kind, id) + } +} diff --git a/docs/architecture/ghostlight-transition-algebra.md b/docs/architecture/ghostlight-transition-algebra.md new file mode 100644 index 0000000..a13694b --- /dev/null +++ b/docs/architecture/ghostlight-transition-algebra.md @@ -0,0 +1,481 @@ +# Ghostlight World Transition Algebra + +Status: adopted architecture; production migration is incomplete until the +legacy effect writers named below are deleted. + +## Objective + +Ghostlight needs one physical world. A player action, an NPC reaction, a +strategic institution, and a low-resolution Gestalt cell may use different +admission and resolution procedures, but a successful outcome must change the +same canonical components through the same kernel-owned commit primitive. + +The transition algebra exists so that: + +- freeform action remains possible without model-authored structural writes; +- impossible actions are absent from the available operation space; +- every committed change has exact subjects, authority, provenance, and + version bindings; +- foreground and background simulation cannot acquire asymmetric powers; +- new play scenarios compose existing semantic mutations instead of growing a + bespoke effect enum for every story beat. + +This design adapts the useful core of action-language and planning literature: +states are typed assignments, actions have explicit applicability conditions, +and transitions have bounded effects. It does not adopt PDDL, STRIPS, or a +general logic-programming runtime as Ghostlight's public ontology. BC+ provides +the causal-law distinction, STRIPS and PDDL provide the precondition/effect +discipline, and REBA provides the important reminder that coarse and fine +simulation must refine the same transition system rather than inventing two +worlds. See [BC+](https://academic.oup.com/logcom/article/30/4/899/2917837), +[PDDL2.1](https://arxiv.org/abs/1106.4561), and +[REBA](https://arxiv.org/abs/1508.03891). + +## Authority map + +### Owner + +The per-campaign `WorldKernel` owns one `WorldMutationBatch` validation and +commit primitive. It is the only subsystem allowed to change canonical world +components or advance fictional time. + +### Inputs + +The owner may read: + +- the exact canonical campaign revision; +- the exact resolution epoch when a strategic wave is involved; +- typed subjects and their component versions; +- an accepted action, reaction, lifecycle, governance, or strategic-outcome + receipt; +- a digest-bound `MutationAuthorityEnvelope` compiled from current state; +- a proposed batch containing only semantic mutations named by that envelope; +- exact evidence, permission, roll, and model-stage receipts required by the + originating procedure. + +### Outputs + +One successful commit produces, atomically: + +- the next canonical component state; +- append-only world events describing the observable means and outcome; +- a private exact `world_mutation_receipt.v1`; +- the command or strategic receipt that caused the transition; +- any knowledge-channel-aware news inputs derivable from committed events. + +Eve, CultMesh, narration, and news projections refresh only after the commit. + +### Derived and demoted state + +- `WorldEffectDelta`, `ActorStateDelta`, `StrategicCellEffect`, and + `StrategicOutcomeEffect` are requirements evidence and migration inputs. They + are no longer allowed to own a production write after their resolver moves. +- `StrategicActivityKind` describes proposed means. It is not a mutation. +- An `ActionIntent` and its intended effect are proposals. They are not facts + and do not become true because a model narrated them. +- Simulation cells, covers, leases, salience, and detail debt choose inference + resolution. They are not fictional world components. +- Transcript prose and narration are projections over committed events. They + do not repair or create state. +- Actor equipment lists, institution resource lists, and Gestalt resource sets + become projections over resource subjects plus custody. A resource does not + change ontology when its owner changes. +- A dormant named Gestalt member and its materialized foreground form are one + actor subject. Materialization changes simulation resolution, not identity or + physical existence. + +### Forbidden writers + +The migration is not complete while any of these can decide canonical state: + +- `apply_world_effect`; +- `apply_strategic_outcome_effect`; +- the direct private-delta loop in `ResolveReactionWave`; +- bespoke actor, Gestalt, or member movement and migration writers; +- Gestalt folding code that unions private knowledge or possessions into a + population without an admitted mutation; +- model output, browser payloads, narration, news generation, retrieval, or a + derived simulation cell. + +Administrative world creation, compiler publication, and approval-gated +population fission must ultimately lower to the same batch primitive. They may +possess broader authority envelopes, but not another state-writing mechanism. + +### Shared paths + +All of these lower to `WorldMutationBatch` before state changes: + +- player `Attempt` confirmation; +- NPC initiative resolution; +- actor reactions and private appraisal consequences; +- strategic individual, institution, cohesive Gestalt, and arena-constituent + outcomes; +- travel, waits, and strategic time advancement; +- resource production, use, damage, repair, and exchange; +- named-person joining, leaving, and migrating between populations; +- population migration and approval-gated fission; +- contract amendments that are permitted to change forward-looking world + state; +- compiler-admitted entities and later bounded region expansion. + +### Cut line + +The new validator applies a whole batch to an isolated transactional overlay, +checks local and cross-mutation invariants, and returns either one complete next +state or an error. Resolvers then emit the new mutation vocabulary directly. +When the last production caller moves, the old committing functions and their +model schemas are deleted. Persisted legacy receipts remain readable history; +they never re-enter a commit path. + +No repair loop, converter with independent policy, or post-commit reconciler is +allowed to preserve a second opinion about the result. + +## Canonical state model + +### Subjects + +Every durable entity has a stable typed `SubjectRef`: + +| Subject kind | Examples | Important rule | +| --- | --- | --- | +| `actor` | player, NPC, named refugee | Identity survives foreground materialization and Gestalt folding. | +| `population` | villagers, refugee convoy, corporation workforce | May own a cohesive Gestalt Persona baseline; never owns member secrets by implication. | +| `institution` | Zhestokost command, a clinic, a guild | Collective authority is explicit, not inferred from proximity. | +| `place` | room, settlement, route node, region | Persists independently of current observation. | +| `resource` | a key, medicine lot, ship, credits tranche | Has exact custody and resource state; it is not a string copied between owner lists. | +| `pressure` | blockade clock, debt deadline, epidemic load | Carries bounded progress and consequence semantics. | +| `proposition` | a source-grounded fact, a witnessed claim, a branch-local finding | Knowledge points to the proposition; prose duplication is not knowledge authority. | +| `channel` | courier route, broadcast, private conversation | Governs who can receive information without making it public. | + +Campaign time is a component of the campaign root. Relationships and topology +edges are keyed component records between subjects, not synthetic actors. + +IDs are opaque and stable. Prefixes may remain a debugging convention, but no +validator infers subject kind or authority from string shape. + +### Components + +The target store is component-oriented even when a compatibility migration +temporarily reads old aggregate rows: + +- `identity`: canonical identity plus adopted handles and their disclosure + state; +- `occupancy`: exact place anchors and travel state; +- `custody`: exact custodian of a resource or controlled subject; +- `resource_state`: type, quantity, integrity, qualities, and lifecycle; +- `capability`: supported abilities and their limits; +- `condition`: bodily, material, social, or environmental state; +- `knowledge`: proposition, epistemic status, source, channel, and revision; +- `memory`: private episodic record and provenance; +- `relationship`: directed subject-to-subject state; +- `commitment`: goals and obligations, including creditor or beneficiary; +- `pressure`: bounded progress, threshold, consequence, and status; +- `posture`: an agentive subject's current strategic stance; +- `population_membership`: exact member, population, role, and interval; +- `population_lineage`: refinement, fission, merge, and remainder ancestry; +- `topology`: containment and route edges with travel cost and availability; +- `lifecycle`: admitted, active, transformed, consumed, retired; +- `world_time`: the campaign's monotonic fictional coordinate. + +Components are sparse. A place does not need knowledge; a proposition does not +need occupancy. Component applicability is determined by subject kind and the +specific admitted entity, not by a universal bag of optional fields. + +## The mutation vocabulary + +`WorldMutation` is a closed tagged union. Each variant owns one semantic +component family and names the invariants that family can enforce. There is no +`SetField`, arbitrary path, embedded patch, or model-authored component name. + +| Mutation | Canonical effect | Essential validation | +| --- | --- | --- | +| `Relocate` | Changes occupancy for an exact subject. | Origin matches, route/reach exists, mover has authority, group custody and travel approval hold. | +| `TransferCustody` | Moves an exact resource between custodians. | Source has custody, recipient exists and can receive it, quantity/identity is conserved. | +| `MutateResource` | Creates, transforms, consumes, damages, repairs, splits, or combines exact resources. | Recipe/capability, custody, conservation, lifecycle, quantity, and effect ceiling hold. | +| `ChangeCapability` | Grants, alters, suspends, or retires a supported capability. | Source of capability and duration are admitted; no narration-only expertise. | +| `ChangeCondition` | Applies, alters, or clears a condition. | Target is reachable, condition is compatible, opposition and outcome band permit it. | +| `ChangeCommitment` | Creates, alters, fulfills, defaults, or retires a goal or obligation. | Exact owner, counterparty, consent or coercive authority, and lifecycle hold. | +| `ChangeRelationship` | Creates, alters, or retires a directed relationship component. | Exact endpoints exist; one actor cannot author another's private appraisal without its own reaction authority. | +| `ChangePressure` | Creates, advances, reduces, resolves, or retires a bounded pressure. | Owner, bounds, current value, threshold, and consequence are exact. | +| `ChangeKnowledge` | Acquires, communicates, conceals, corrects, or invalidates a proposition for exact knowers. | Source knows or can observe it, channel and recipients are exact, concealment does not erase another mind. | +| `ChangeMemory` | Records, revises, or retires an exact private episodic memory. | Witness or authorized private appraisal exists; another actor cannot write it. | +| `ChangePosture` | Alters the posture of an exact agentive subject. | The acting authority can speak or decide for that subject; arenas cannot use it collectively. | +| `ChangePopulationMembership` | Joins, leaves, or transfers an exact actor between populations. | Member identity is stable, source membership exists, destination is compatible, no duplicate coverage. | +| `ChangePopulationLineage` | Records an admitted split, merge, or remainder relation. | Fission preview and approval exist; leaves remain non-overlapping and complete. | +| `ChangeIdentity` | Adopts, discloses, restricts, or retires an exact identity handle. | Self-authority or explicit custody exists; disclosure is separate from renaming. | +| `ChangeTopology` | Adds, alters, opens, closes, or retires a containment or route edge. | Evidence/branch admission, endpoint existence, reciprocal rules, and geometry invariants hold. | +| `AdmitEntity` | Creates an explicitly permitted non-resource subject and initial components. | Admission class, evidence or branch-local authority, collision freedom, and required components hold. | +| `RetireEntity` | Retires an exact subject without erasing history. | No dangling custody, occupancy, membership, topology, or active obligation remains. | +| `AdvanceWorldTime` | Advances campaign time by an exact duration. | Monotonicity, governance, no-puppeting, tick budget, and scheduled obligations hold. | + +`NoMaterialChange` is not a mutation. It is an outcome with an empty mutation +batch and an explicit explanation. Speech is an event in the means record; +persuasion, obligation, relationship, and knowledge consequences are separate +mutations. + +Complex effects are batches, not new verbs. Giving half a medicine lot to a +clinic composes resource split, custody transfer, and perhaps an obligation. +Evacuating a named refugee composes relocation, population-membership transfer, +and accessible knowledge. Population fission composes entity admission, +lineage, and exact member transfers while preserving an `other/unknown` +remainder. + +## Means, intended effect, and committed mutation + +### Means + +`ActionMeans` records what is attempted and may become observable: + +- exact acting subject; +- bounded natural-language method; +- exact targets, instruments, resources, places, routes, and channels cited + from the actor's permitted slice; +- explicit speech, if any; +- state and evidence references supporting the method. + +The means taxonomy is intentionally open at the prose level. Ghostlight does +not need a rulebook verb for every clever action. The exact typed references +make reach, custody, knowledge, and capability testable. + +### Intended effect + +`MutationIntent` names desired component families, targets, and qualitative +direction. It is useful for assessment and cognitive focus, but it is not an +assertion that the change occurred. A player may intend to persuade a guard, +destroy a reactor, learn a route, or move a crowd without receiving any of +those mutations. + +### Committed mutation + +The admitted outcome procedure emits a concrete `WorldMutationBatch` bounded +by its authority envelope. The batch is the only one of the three objects that +can become state. A roll changes which digest-bound outcome plan is eligible; +it does not allow the resolver to invent a stronger mutation afterward. + +For Sable's self-presentation: + +- means: the refugee speaks an exact self-presentation to present listeners; +- intended effect: those listeners may address this person using `Sable`; +- committed mutations: `ChangeIdentity::AdoptHandle` only if the speaker is + choosing a new handle, followed by `ChangeIdentity::DiscloseHandle` or + `ChangeKnowledge::Communicate` to exact listeners; +- derived projection: each listener's Eve surface displays the best identity + handle that listener actually knows. + +The speech never authorizes a global actor rename, and a display label never +becomes identity authority. + +## Mutation authority envelopes + +An admission policy compiles `mutation_authority_envelope.v1` from the current +snapshot. It contains exact permits, not broad roles: + +- campaign revision and relevant component versions; +- originating subject and resolution procedure; +- permitted mutation kind and operation; +- exact subjects, routes, resources, propositions, channels, and relationships + available to that operation; +- numeric and cardinality bounds; +- extraordinary-permission or approval receipts; +- effect ceiling and outcome-band binding; +- expiry and digest. + +The envelope is capability-like but not transferable. It can authorize one +assessment outcome, one reaction appraisal, one strategic action, or one +administrative command. It cannot be rebased onto a new revision. + +### Action-specific model schemas + +The model never receives the complete world mutation schema. Ghostlight first +selects the component families relevant to the intended effect, then generates +a compact schema from the exact permits: + +- if no resource is transferable, no custody-transfer branch exists; +- if the actor cannot reach a target, that target does not appear; +- if the actor knows no communicable proposition, communication is absent; +- if an arena has no collective actor, collective posture and speech are + absent; +- if the outcome cannot create an entity, admission is absent; +- scalar fields carry local limits rather than universal ranges. + +Each generated branch carries an opaque permit ID. The kernel resolves that ID +back to the exact permit and revalidates it against the snapshot. Stable +instructions and mutation definitions precede the dynamic permit table so +provider caching remains useful; the dynamic prompt contains only the state +needed for this decision. + +Schema generation improves model ergonomics. It does not replace local +validation, and schema presence is not authority by itself. + +## Validation and atomic commit + +The commit primitive performs this sequence: + +1. Verify campaign revision, resolution epoch when present, originating receipt, + envelope digest, expiry, and idempotency. +2. Resolve every subject and component against the same snapshot. +3. Verify every mutation has a matching unused permit and remains inside its + exact IDs, operations, bounds, and effect ceiling. +4. Validate local semantics: reach, custody, knowledge provenance, authority, + component applicability, and lifecycle. +5. Validate batch semantics: no contradictory writes, no resource duplication, + no dangling entity retirement, no duplicate population membership, and no + hidden cross-actor private writes. +6. Apply the entire batch to an isolated in-memory overlay. +7. Re-run global topology, custody, population coverage, player-control, + knowledge, and temporal invariants on the overlay. +8. Persist component rows, events, mutation receipt, command receipt, and + required private proof records in one CultCache transaction. +9. Publish refreshed Eve/CultMesh projections after the transaction commits. + +Any error returns the original state byte-for-byte. A malformed model output +gets one same-snapshot correction and then aborts. No valid sibling mutation +survives an invalid batch member. + +## Admission policies are separate from physics + +The shared algebra does not make every actor equally powerful. Each lane has a +different authority compiler: + +- foreground fiction-first d20 assessment considers declared means, local + affordances, opposition, permissions, stakes, and effect ceiling; +- reaction appraisal can write only the reacting subject's private components + plus exact speech/action proposals; +- NPC initiative uses the same foreground assessment after initiative chooses + an eligible proposal; +- strategic cells use horizon, graph reach, institutional authority, resources, + and resolution-cell attribution; +- cohesive Gestalts may receive collective permits only from real collective + authority; +- arena cells receive constituent permits and cannot emit one arena-owned + mutation; +- compiler and approval commands may admit entities or topology only through + explicit evidence and branch-local gap policy. + +Different admission does not imply different mutations. A player and an +institution transferring the same medicine use the same custody mutation. + +## Migration from the empirical vocabularies + +The accumulated variants are test requirements, not discarded knowledge: + +| Existing behavior | Algebraic lowering | +| --- | --- | +| actor move / member migration / Gestalt migration | `Relocate` plus membership change when population affiliation changes | +| actor equipment / institution resources / Gestalt resources / member equipment | resource subjects plus `TransferCustody` and `MutateResource` | +| actor or member condition | `ChangeCondition` | +| actor goal / member obligation | `ChangeCommitment` | +| actor or member relationship | `ChangeRelationship` | +| world clock and Gestalt pressure | pressure subjects plus `ChangePressure` | +| actor knowledge / Gestalt shared knowledge / member knowledge | `ChangeKnowledge` with exact proposition and knower | +| member memory / reaction memory | `ChangeMemory` | +| institution posture / strategic posture | `ChangePosture` | +| Gestalt promotion and folding | resolution change over one stable actor plus explicit component mutations only | + +Migration order: + +1. Publish types, component applicability, permits, batch validation, receipts, + and property tests without changing live state. +2. Lower foreground assessment and NPC resolution directly to mutation batches; + delete `apply_world_effect`. +3. Lower reaction Interpreters to actor-private permits and mutation batches; + delete direct `ActorStateDelta` writes. +4. Lower strategic outcome generation to the same operations; delete + `apply_strategic_outcome_effect` and string-resource helpers. +5. Normalize named Gestalt members into stable actor subjects and resource, + membership, identity, and occupancy components. Remove materialized actor as + a second identity owner. +6. Lower travel, waits, fission, expansion, and compiler publication. +7. Migrate persisted campaign rows transactionally, quarantine ambiguous legacy + resources or identities, and make the old campaign fields projection-only. +8. Remove legacy schemas from model boundaries. Retain schema readers solely + for historical receipt inspection. + +At every stage, the old and new writers cannot both be active for the migrated +lane. + +## Agency corpus + +Ghostlight does not declare the algebra complete from enum aesthetics. It owns +an inspectable `agency_attempt_case.v1` corpus compiled into a MessagePack +CultCache store. JSON Schema publishes the record shape; authored source may be +reviewed Markdown, but runtime and test fixtures load `.cc`. + +Each case records: + +- domain and scenario; +- minimal world fixture and exact subjects; +- means and intended effect; +- expected admission: admissible, impossible, or bargain-required; +- required mutation composition by outcome band; +- forbidden mutations and invariant witnesses; +- expected bargains for missing permission or affordance; +- whether foreground, NPC, strategic, and Gestalt resolutions should be + physically equivalent; +- reviewer status and any genuinely missing primitive. + +The initial target is at least 300 reviewed attempts: 25 each across social, +physical, investigative, economic, political, technological, extraordinary or +magical, collective, identity and privacy, population and migration, +infrastructure and logistics, and bodily or medical play. Cases include both +ordinary and adversarial attempts, partial successes, indirect means, consent +boundaries, rival actors inside arenas, and low-resolution background outcomes. + +Completeness gates: + +- every case maps compositionally, yields a comprehensible bargain, or names a + genuinely missing primitive; +- no case falls through to a generic patch or arbitrary component write; +- the same physical change uses the same mutation in foreground, NPC, member, + population, institution, and arena-constituent contexts; +- property tests prove atomicity, deterministic permit/schema generation, + resource conservation, stable identity, exact knowledge ownership, population + coverage, topology integrity, and no puppeting; +- the final 100 newly reviewed admissible cases require no new primitive. This + is a stability signal, not a mathematical proof of completeness. + +The Sable self-presentation and later rematerialization is a mandatory first +case. It must prove observer-relative identity projection without a global +rename or loss across Gestalt folding. + +## Public contracts + +Add: + +- `typed_subject.v1` +- `world_component_ref.v1` +- `action_means.v1` +- `mutation_intent.v1` +- `mutation_permit.v1` +- `mutation_authority_envelope.v1` +- `world_mutation.v1` +- `world_mutation_batch.v1` +- `world_mutation_receipt.v1` +- `agency_attempt_case.v1` + +JSON Schema is the catalog boundary. Canonical component rows, envelopes, +batches, receipts, and corpus fixtures use MessagePack-backed CultCache `.cc`. +JSON remains acceptable only at browser, MCP, and model-provider boundaries. + +## Acceptance + +The algebra cut is complete only when: + +- no production path calls a legacy effect writer; +- every effectful command persists an exact mutation receipt; +- malformed, stale, over-broad, contradictory, or partially valid batches leave + state, fictional time, and projections unchanged; +- background and foreground versions of the same action pass the same component + invariants; +- arena rivals cannot speak or mutate as one subject and cannot acquire one + another's knowledge; +- resource custody and quantity are conserved across transfers and failures; +- named people remain the same subjects through Gestalt merge, split, migration, + folding, and rematerialization; +- Sable's disclosed handle appears only to entitled observers and survives the + complete round trip; +- action-specific schemas omit impossible operations before inference; +- the agency corpus meets its coverage and stability gates; +- adversarial play through the typed Eve surface still proves the homepage + promises after migration. +