diff --git a/crates/ghostlight-dungeon/src/begin.rs b/crates/ghostlight-dungeon/src/begin.rs index 30ff5f4..e3543d9 100644 --- a/crates/ghostlight-dungeon/src/begin.rs +++ b/crates/ghostlight-dungeon/src/begin.rs @@ -375,7 +375,7 @@ async fn finish( /// place-scale target, so the ask rides in the seed's brief; a place is a /// `declare_place` inside the `start` root, and the seed prompt already tells /// the model where its people must stand. -const SEED_PLACES: &str = "Give the world somewhere to be, not only someone to meet: with declare_place, declare three to five places inside the start place (set each place's container to it): a market, a hall, a gate, a shrine, whatever this world's lore names. Stand each person at one of those places, spread across them, and let the people at a place be the ones who belong there. Do not put every person at the start place itself."; +const SEED_PLACES: &str = "Give the world somewhere to be, not only someone to meet: with declare_place, declare three to five places inside the start place (set each place's container to it): a market, a hall, a gate, a shrine, whatever this world's lore names. Stand each person at one of those places, spread across them, and let the people at a place be the ones who belong there. Do not put every person at the start place itself. Then join the places: with declare_route, declare a public route each way between every pair of places a person could walk between, from the market to the hall, from the hall to the gate, and so on, a few minutes each, so nobody is stranded and every place leads somewhere."; /// The brief the seed sessions read: the player's story, or the vault's /// premise, and always the ask for places. @@ -456,6 +456,13 @@ impl Seed { if !world.scale_deficit.is_empty() && world.scale_deficit.iter().all(|row| row.qualified == 0) { return Err(stopped("seed", "seeding authored no one")); } + // Genesis stands the player at the commons, which no seed populates. + // Until Begin chooses a spawn, the player stands among the most + // people the seed placed. + SeedPort::new(self.host.world.clone(), self.principal.clone()) + .stand_first_person_among_people(derived_step_id(self.key, "spawn")?, self.world_id) + .await + .map_err(|error| from_mailbox("spawn", error))?; finish(&self.host, &self.principal, self.key, self.world_id).await } } @@ -695,6 +702,44 @@ pub(crate) mod tests { ) } + /// One seed session that declares a `Hall` inside the root and stands + /// `handles` there. + fn author_in_hall(receipt: &str, world: &WorldSnapshot, handles: &[&str]) -> ghostlight::InferenceOutput { + let root = world.places.iter().find(|place| place.label == "Ashfall").unwrap().id; + let speak = world.affordances.iter().find(|entry| entry.entry.kind.0 == "speak").unwrap().id; + let (root, speak) = (serde_json::to_value(root).unwrap(), serde_json::to_value(speak).unwrap()); + let mut calls = vec![( + "declare_place", + serde_json::json!({"handle": "hall", "label": "Hall", "container": {"ref": "existing", "value": root}}), + )]; + for handle in handles { + calls.push(( + "declare_subject", + serde_json::json!({ + "handle": handle, + "label": format!("Gull-keeper {handle}"), + "kind": "person", + "controller": {"type": "narrative_persona"}, + "affordances": [{"ref": "existing", "value": speak}], + "position": {"ref": "draft", "value": "hall"}, + }), + )); + } + calls.push(("submit", serde_json::json!({}))); + output( + receipt, + calls + .into_iter() + .enumerate() + .map(|(index, (name, arguments))| ghostlight::InferenceEvent::ToolCall { + call_id: format!("call-{index}"), + name: name.to_owned(), + arguments: arguments.to_string(), + }) + .collect(), + ) + } + /// Everything the model was shown in one request, as the request's own /// serialization: purpose, instructions and input. fn prompt_text(request: &ghostlight::InferenceRequest) -> String { @@ -787,6 +832,31 @@ pub(crate) mod tests { .await; } + /// Genesis stands the player at the commons, which no seed populates: a + /// seeded Begin moves them to the most populated seeded place, with no way + /// back to the empty commons left open. + #[tokio::test] + async fn a_seeded_begin_stands_the_player_among_the_people_the_seed_placed() { + let rig = deferred_rig(); + let begun = begin_vault(&rig, Uuid::new_v4()).await; + let host = rig.timelines.host(&begun.timeline).await.unwrap(); + let draft = host.world.snapshot().await.unwrap(); + rig.port.push(Ok(author_in_hall("seed-1", &draft, &["a", "b"]))); + rig.port.push(Ok(author("seed-2", &draft, &["c"]))); + + let world = eventually(async || { + let world = host.world.snapshot().await.unwrap(); + (world.phase == WorldPhase::Active && rig.status(&begun.timeline).await.is_none()).then_some(world) + }) + .await; + let hall = world.places.iter().find(|place| place.label == "Hall").unwrap().id; + let player = world.subjects.iter().find(|subject| subject.label == "Deru").unwrap(); + assert_eq!(player.position, Some(hall), "the player stands in the hall"); + let others = world.subjects.iter().filter(|s| s.id != player.id && s.position == Some(hall)).count(); + assert_eq!(others, 2, "with the two people placed there"); + assert!(world.routes.iter().all(|route| !route.open), "no open way back to the empty commons"); + } + /// Spec test: a seed fault reads Failed with a category and no endpoint /// text, leaves the world a Draft, and the same key seeds it again. #[tokio::test] diff --git a/crates/ghostlight/src/mailbox.rs b/crates/ghostlight/src/mailbox.rs index 6fb7f89..aca0b9a 100644 --- a/crates/ghostlight/src/mailbox.rs +++ b/crates/ghostlight/src/mailbox.rs @@ -8,7 +8,8 @@ use super::{ patch::kernel_speak_grant, prepare_creation, verse::{CreateVerseWorldIntent, VerseGenesisError}, }; -use crate::VerifiedPrincipalEvidence; +use crate::patch::{AccessKind, ComponentOp, Cost, RouteDeclaration}; +use crate::{SubjectId, VerifiedPrincipalEvidence}; use chrono::Utc; use std::path::Path; use thiserror::Error; @@ -717,6 +718,98 @@ impl SeedPort { } } +impl SeedPort { + /// Stands the world's first person among people. Genesis puts the human at + /// the commons, which a seed never populates, so the first turn would open + /// in an empty room. This moves the human to the most populated place the + /// seed declared (ties to the lowest label, then id) through one declared + /// route that the same patch closes behind them, and answers where they + /// now stand, or `None` when there is nothing to change: no seeded place + /// has anyone in it, or the human already stands in one. Draft only, like + /// every seed submission. + pub async fn stand_first_person_among_people( + &self, + command_id: CommandId, + world_id: WorldId, + ) -> Result, MailboxError> { + let snapshot = self.mailbox.snapshot().await?; + let Some(spawn) = first_person_spawn(&snapshot) else { + return Ok(None); + }; + let route = Ref::Draft(DraftHandle::new("spawn-route")); + let patch = WorldPatch { + declarations: vec![Declaration::Route(RouteDeclaration { + handle: DraftHandle::new("spawn-route"), + label: "the way in".into(), + from: Ref::Existing(spawn.from), + to: Ref::Existing(spawn.to), + access: AccessKind::Public, + cost: Cost(1), + })], + operations: vec![ + ComponentOp::Relocate { + subject: Ref::Existing(spawn.subject), + via: route.clone(), + }, + ComponentOp::CloseRoute { route }, + ], + evidence: Vec::new(), + }; + self.submit_seed(command_id, world_id, patch).await?; + Ok(Some(spawn.label)) + } +} + +struct FirstPersonSpawn { + subject: SubjectId, + from: EntityId, + to: EntityId, + label: String, +} + +/// The human's move, read from the snapshot alone: to the seeded place (one +/// with a container; the commons and the jurisdiction roots have none) holding +/// the most other subjects. +fn first_person_spawn(snapshot: &WorldSnapshot) -> Option { + let human = snapshot + .subjects + .iter() + .find(|subject| subject.controller_mode == Some(crate::ControllerMode::Human) && !subject.retired)?; + let from = human.position?; + let seeded = |id: EntityId| { + snapshot + .places + .iter() + .any(|place| place.id == id && place.container.is_some()) + }; + if seeded(from) { + return None; + } + let population = |place: &crate::PlaceSnapshot| { + snapshot + .subjects + .iter() + .filter(|other| other.id != human.id && !other.retired && other.position == Some(place.id)) + .count() + }; + let (count, place) = snapshot + .places + .iter() + .filter(|place| place.container.is_some()) + .map(|place| (population(place), place)) + .min_by(|(a, place_a), (b, place_b)| { + b.cmp(a) + .then_with(|| place_a.label.cmp(&place_b.label)) + .then_with(|| place_a.id.cmp(&place_b.id)) + })?; + (count > 0).then(|| FirstPersonSpawn { + subject: human.id, + from, + to: place.id, + label: place.label.clone(), + }) +} + /// The play table's one door. Minted only by Dungeon, which constructs one /// `PlayPort` in its `AppState`; exclusivity is that single construction site, /// pinned by a negative grep, exactly as every other system capability's is.