From cdffad484aea359d813d4a5c003931acc1dfa1fc Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Dec 2026 01:42:21 +0100 Subject: [PATCH] Pin react's room derivation, empty-room result, handle split and co_located in each owning crate Co-Authored-By: Claude Sonnet 5.5 --- crates/ghostlight-dungeon/src/play.rs | 74 +++++++++++++++++++ crates/ghostlight/src/controllers.rs | 102 ++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/crates/ghostlight-dungeon/src/play.rs b/crates/ghostlight-dungeon/src/play.rs index 9094992..eea15f6 100644 --- a/crates/ghostlight-dungeon/src/play.rs +++ b/crates/ghostlight-dungeon/src/play.rs @@ -6786,6 +6786,80 @@ pub(crate) mod tests { assert!(!prompt.contains(&format!("=== {}:", handle_for(borin))), "{prompt}"); } + + /// Naming subjects reacts exactly those, whoever else stands in the room. + /// Mutation: `react_omits_subjects` returning true. + #[tokio::test] + async fn react_with_named_subjects_reacts_only_them() { + let fixture = play_world(Some("Mara"), "player-react-named").await; + let commons = commons_of(&fixture.world.snapshot().await.unwrap()); + declare_standing(&fixture.world, "borin", "Borin", "person", "narrative_persona", &commons).await; + let snapshot = fixture.world.snapshot().await.unwrap(); + let (mara, borin) = (subject_by_label(&snapshot, "Mara"), subject_by_label(&snapshot, "Borin")); + let reply = format!("=== {}: Mara ===\nMara nods.\n", handle_for(mara)); + let directory = tempfile::tempdir().unwrap(); + let (table, port) = react_table( + &fixture, + vec![output("gestalt", vec![text_event(&reply)]), output("narrate", vec![text_event("Quiet.")])], + vec![react_then_end(serde_json::json!({"subjects": [subject_id_text(&snapshot, mara)]}))], + &directory.path().join("play-turn-v1.cc"), + ); + table.run(&fixture.principal, test_turn_id(1307), "Hello.".into()).await.unwrap(); + let prompt = gestalt_prompt(&port).unwrap(); + assert!(!prompt.contains(&handle_for(borin)), "{prompt}"); + } + + /// `dispatch` never derives a room: with no `subjects` it is refused, as + /// before. Mutation: deriving for any tool. + #[tokio::test] + async fn dispatch_without_subjects_is_still_refused() { + let fixture = play_world(Some("Mara"), "player-dispatch-omits").await; + let round = output( + "r0", + vec![ + call_event("c0", DISPATCH_TOOL, serde_json::json!({})), + call_event("c1", END_TURN_TOOL, serde_json::json!({})), + ], + ); + let directory = tempfile::tempdir().unwrap(); + let (table, _port) = react_table( + &fixture, + vec![output("narrate", vec![text_event("Quiet.")])], + vec![round], + &directory.path().join("play-turn-v1.cc"), + ); + table.run(&fixture.principal, test_turn_id(1308), "Hello.".into()).await.unwrap(); + let result = call_result(&table, 0).await; + assert!(result.contains("no `subjects` key"), "{result}"); + let stored = table.store.lock().await; + assert!(stored.current().unwrap().persona_turns.is_empty()); + } + + /// With nobody else present, react says so; an explicit empty dispatch does + /// not claim a room. Mutations on the empty-result guard. + #[tokio::test] + async fn react_in_an_empty_room_says_so() { + let fixture = play_world(None, "player-react-empty").await; + let round = output( + "r0", + vec![ + call_event("c0", REACT_TOOL, serde_json::json!({})), + call_event("c1", DISPATCH_TOOL, serde_json::json!({"subjects": []})), + call_event("c2", END_TURN_TOOL, serde_json::json!({})), + ], + ); + let directory = tempfile::tempdir().unwrap(); + let (table, port) = react_table( + &fixture, + vec![output("narrate", vec![text_event("Quiet.")])], + vec![round], + &directory.path().join("play-turn-v1.cc"), + ); + table.run(&fixture.principal, test_turn_id(1309), "Hello.".into()).await.unwrap(); + assert!(call_result(&table, 0).await.contains("no one else is in the room")); + assert!(!call_result(&table, 1).await.contains("no one else")); + assert!(gestalt_prompt(&port).is_none()); + } /// `co_located` is the owner's room query: live non-Human subjects sharing /// the subject's place, never the subject itself, ascending. #[tokio::test] diff --git a/crates/ghostlight/src/controllers.rs b/crates/ghostlight/src/controllers.rs index 7fde87c..2444c1a 100644 --- a/crates/ghostlight/src/controllers.rs +++ b/crates/ghostlight/src/controllers.rs @@ -16716,6 +16716,108 @@ assert!(projector_text.contains("I ask about the hinge."), "the player's own tur task.await.unwrap(); } + /// The owner's room query over a real world: Iris and Mara share the + /// commons, Otho stands alone in the vault. + #[tokio::test] + async fn co_located_lists_the_others_standing_in_the_same_place() { + let (_directory, mailbox, _task, _stale, _mara, _a, _b) = persona_lane_world().await; + let snapshot = mailbox.snapshot().await.unwrap(); + let by = |prefix: &str| { + snapshot.subjects.iter().find(|subject| subject.label.starts_with(prefix)).unwrap().id + }; + let (iris, mara, otho) = (by("Iris"), by("Mara"), by("Otho")); + assert_eq!(snapshot.co_located(iris), vec![mara]); + assert_eq!(snapshot.co_located(mara), vec![iris]); + assert!(snapshot.co_located(otho).is_empty(), "Otho is alone in the vault"); + let mut room = vec![iris, mara]; + room.sort(); + assert_eq!(room.iter().filter(|id| snapshot.co_located(**id).len() == 1).count(), 2); + } + + /// The Human is the player, not an onlooker: no human subject is ever listed. + #[tokio::test] + async fn co_located_never_lists_a_human_or_the_subject_itself() { + let (_directory, _mailbox, _task, snapshot, human, _operational, _heard, _secret) = + mixed_controller_world().await; + let room = snapshot.co_located(human); + assert!(!room.contains(&human)); + for id in &room { + let mode = snapshot.subjects.iter().find(|subject| subject.id == *id).unwrap().controller_mode; + assert_ne!(mode, Some(ControllerMode::Human)); + } + assert!(!room.is_empty(), "the fixture's other subjects share the human's place"); + } + + /// Runs one Gestalt round over the named people of `persona_lane_world`, + /// the scripted reply built from their handles in order. + async fn gestalt_round( + who: &[&str], + reply: impl Fn(&[String]) -> String, + ) -> (Vec, String, Vec) { + let (_directory, mailbox, _task, _stale, _mara, _a, _b) = persona_lane_world().await; + let snapshot = mailbox.snapshot().await.unwrap(); + let ids: Vec = who + .iter() + .map(|prefix| snapshot.subjects.iter().find(|s| s.label.starts_with(prefix)).unwrap().id) + .collect(); + let handles: Vec = ids.iter().map(|id| handle_for(*id)).collect(); + let opportunities: Vec = ids.iter().map(|id| opportunity_for(&snapshot, *id)).collect(); + let capturing = Arc::new(CapturingPort { + prepared: Mutex::new(Vec::new()), + outputs: Mutex::new(vec![output(vec![InferenceEvent::Text(reply(&handles))], "gestalt")]), + }); + let lane = PersonaLane::new( + ControllerPort::new(mailbox.clone()), + capturing.clone(), + "projector-model".into(), + "persona-model".into(), + ) + .unwrap(); + let turns = lane.gestalt(CommandId::new(), &opportunities).await.unwrap(); + let prompt = request_text(&capturing.prepared.lock().unwrap()[0]); + (turns, prompt, ids) + } + + /// Each section is the text between its handle's header line and the next + /// handle's; a header whose label the model rewrote still splits; the prompt + /// names every handle with its label. + #[tokio::test] + async fn gestalt_splits_the_reply_on_handles() { + let (turns, prompt, ids) = gestalt_round(&["Iris", "Mara"], |h| { + format!( + "=== {}: Iris, renamed by the model ===\nIris counts the coins twice.\nShe frowns.\n=== {}: Mara at the rain gate ===\nMara laughs.", + h[0], h[1] + ) + }) + .await; + assert_eq!(turns.len(), 2); + assert_eq!(turns[0].source_prose(), "Iris counts the coins twice.\nShe frowns."); + assert_eq!(turns[1].source_prose(), "Mara laughs."); + assert!(prompt.contains(&format!("=== {}: Iris in the tollhouse ===", handle_for(ids[0]))), "{prompt}"); + assert!(prompt.contains(&format!("=== {}: Mara at the rain gate ===", handle_for(ids[1]))), "{prompt}"); + } + + /// A person the reply leaves out, or who writes nothing, is silent. + #[tokio::test] + async fn gestalt_keeps_silent_for_a_missing_or_empty_section() { + let (turns, _prompt, _ids) = + gestalt_round(&["Iris", "Mara"], |h| format!("=== {}: Iris ===\n\n", h[0])).await; + assert_eq!(turns[0].source_prose(), "Silent."); + assert_eq!(turns[1].source_prose(), "Silent."); + } + + /// The handle is the first 8 hex digits of the id's own text. + #[tokio::test] + async fn handle_for_is_eight_hex_digits_of_the_id() { + let (_directory, mailbox, _task, _stale, mara, _a, _b) = persona_lane_world().await; + let _ = mailbox; + let handle = handle_for(mara); + assert_eq!(handle.len(), 8); + assert!(handle.chars().all(|c| c.is_ascii_hexdigit())); + assert!(crate::id_text(mara).replace('-', "").starts_with(&handle)); + assert_ne!(handle, "xyzzy"); + } + /// Adjacency is a scheduler projection, not a subject view. Two subjects in /// one room are related through the kernel's own containment closure, which /// no union of `SubjectSnapshot`s could produce for a channel, and the