Scene coverage has one owner, Scene::uncovered, applied at put and at with_scene

with_scene now takes the corpus store and refuses (V16) a scene that no longer
lists the speaker of every live item of its group.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Weksa 2026-11-10 08:40:17 +00:00
commit da407e23af
5 changed files with 75 additions and 18 deletions

View file

@ -82,6 +82,18 @@ pub struct Scene {
}
impl Scene {
/// The first of `speakers` the scene does not list as a participant: the
/// one rule of scene coverage, applied where a scene is put and where it is
/// read.
pub fn uncovered<'a>(
&self,
speakers: impl IntoIterator<Item = &'a EntityId>,
) -> Option<&'a EntityId> {
speakers
.into_iter()
.find(|s| !self.participants.contains(s))
}
/// The features of the relation from `speaker` to the named `addressee`, or
/// to `Unnamed` when there is none; `None` when the scene holds no such
/// relation (a relation with no features is `Some(&[])`). A named

View file

@ -1106,13 +1106,11 @@ impl SpeakerStateStore {
return Err(bad("an ordered pair has one relation"));
}
}
if let Some(i) = items
.iter()
.find(|i| !i.withdrawn && !seen.contains(&i.speaker))
if let Some(entity) = s.uncovered(items.iter().filter(|i| !i.withdrawn).map(|i| &i.speaker))
{
return Err(StoreRefusal::SceneSpeakerMissing {
entity: entity.clone(),
group: s.group,
entity: i.speaker.clone(),
}
.into());
}

View file

@ -34,7 +34,7 @@ use crate::packet::{
Constraints, Context, Discourse, EntityId, Extension, FeatValue, FeatureValue, NodeId, Packet,
PacketKind, Predication, ProvenanceSource, Referent, Relation, SCHEMA_VERSION, Stance,
};
use crate::store::{SpeakerStateStore, StoreError};
use crate::store::{CorpusStore, SpeakerStateStore, StoreError};
/// A broken rule, named.
#[derive(Clone, Debug, PartialEq, Eq)]
@ -582,8 +582,13 @@ pub enum SceneRead {
/// scene value and a speaker and fails for that alone.
///
/// ```
/// fn pick(l: weksa::LoweringInput, i: &weksa::Inventory, s: &weksa::SpeakerStateStore) {
/// let _ = l.with_scene(i, s);
/// fn pick(
/// l: weksa::LoweringInput,
/// i: &weksa::Inventory,
/// s: &weksa::SpeakerStateStore,
/// c: &weksa::CorpusStore,
/// ) {
/// let _ = l.with_scene(i, s, c);
/// }
/// ```
///
@ -673,8 +678,9 @@ impl LoweringInput {
/// Reads the speaker's relation to the addressee, the only path for scene
/// values into a view. The speaker is the item's speaker and the scene is
/// the latest the store holds for the item's group; the caller names
/// neither. A scene that does not list the item's speaker is V16, with or
/// without an addressee. What the read found is recorded on the view as a
/// neither. A scene that does not list the item's speaker, or the speaker of
/// any live item of the group (the rule `put_scene` applies), is V16, with
/// or without an addressee. What the read found is recorded on the view as a
/// [`SceneRead`], never an empty feature list standing for five cases:
/// no scene for the group, no addressee, an addressee who is the speaker,
/// no such relation, or the relation from the speaker to the addressee
@ -686,6 +692,7 @@ impl LoweringInput {
mut self,
inventory: &Inventory,
state: &SpeakerStateStore,
corpus: &CorpusStore,
) -> Result<Self, StoreError> {
self.scene = SceneRead::Unread;
let Some(scene) = state.scene(&self.group)? else {
@ -693,7 +700,15 @@ impl LoweringInput {
return Ok(self);
};
let rev = scene.n;
if !scene.participants.contains(&self.speaker) {
let live = corpus.all()?;
let live = live
.iter()
.filter(|i| i.group == self.group && !i.withdrawn)
.map(|i| &i.speaker);
if scene
.uncovered(live.chain(std::iter::once(&self.speaker)))
.is_some()
{
return Err(StoreError::Invalid(vec![Refusal::V16SpeakerNotInScene]));
}
let Some(addressee) = self.discourse.addressee.as_ref() else {

View file

@ -147,7 +147,7 @@ fn rules(r: Result<LoweringInput, StoreError>) -> Vec<&'static str> {
}
fn read(w: &W, view: LoweringInput) -> SceneRead {
view.with_scene(&inv(w), &w.state)
view.with_scene(&inv(w), &w.state, &w.corpus)
.unwrap()
.scene_read()
.clone()
@ -164,7 +164,10 @@ fn a_scene_relation_reaches_the_lowering_view_and_changes_no_english() {
let plain = view_of(&w, 3, Some("n3"));
assert_eq!(plain.scene_read(), &SceneRead::Unread);
assert!(plain.scene().is_empty());
let with = plain.clone().with_scene(&inv(&w), &w.state).unwrap();
let with = plain
.clone()
.with_scene(&inv(&w), &w.state, &w.corpus)
.unwrap();
assert_eq!(with.scene(), status().as_slice());
assert_eq!(with.scene_read(), &selected(1, status()));
assert_eq!(
@ -300,13 +303,13 @@ fn the_scene_is_the_items_group_and_its_latest_revision() {
second.relations[0].features = intimacy(5);
put(&mut w, second);
let got = view_of(&w, 3, Some("n3"))
.with_scene(&inv(&w), &w.state)
.with_scene(&inv(&w), &w.state, &w.corpus)
.unwrap();
assert_eq!(got.scene_read(), &selected(2, intimacy(5)));
assert_eq!(got.scene(), intimacy(5).as_slice());
// A read replaces the earlier one: a store without the scene says so.
let empty = SpeakerStateStore::open(&w.dir.path().join("empty.cc")).unwrap();
let cleared = got.with_scene(&inv(&w), &empty).unwrap();
let cleared = got.with_scene(&inv(&w), &empty, &w.corpus).unwrap();
assert_eq!(cleared.scene_read(), &SceneRead::NoScene);
assert!(cleared.scene().is_empty());
}
@ -318,7 +321,7 @@ fn with_scene_refusals() {
// The scene was admitted at revision 3; a view of revision 2 predates the
// Scene features, so the selected relation's ids do not resolve there.
assert_eq!(
rules(view_of(&w, 2, Some("n3")).with_scene(&inv(&w), &w.state)),
rules(view_of(&w, 2, Some("n3")).with_scene(&inv(&w), &w.state, &w.corpus)),
["V5", "V5", "V5"]
);
// A scene that does not list the item's speaker: group k admitted while
@ -345,9 +348,36 @@ fn with_scene_refusals() {
.admit(p, &w.inventory, &w.corpus, &w.entities)
.unwrap();
assert_eq!(
rules(view.with_scene(&inv(&w), &w.state)),
rules(view.with_scene(&inv(&w), &w.state, &w.corpus)),
["V16"],
"{addressee:?}"
);
}
}
#[test]
fn read_refuses_a_scene_that_stopped_covering_a_live_speaker_of_its_group() {
// Group g's scene covers Nibu alone, and Nibu's is the only item: admitted.
let mut w = world();
put(&mut w, scene("g", 1, &["nibu"], vec![]));
assert_eq!(
read(&w, view_of(&w, 3, None)),
SceneRead::NoAddressee { rev: 1 }
);
// Brokkr then speaks in g. The corpus door lets him in; the view of Nibu's
// own line, whose speaker the scene still lists, reads the stale scene no more.
let mut item = item_in("item:brokkr:1", "Hello.", "g", cast());
item.speaker = eid("brokkr");
w.corpus.add(item, &w.entities).unwrap();
assert_eq!(
rules(view_of(&w, 3, None).with_scene(&inv(&w), &w.state, &w.corpus)),
["V16"]
);
// The put door applies the same rule, so the repair is a revision that
// covers him.
put(&mut w, scene("g", 2, &["nibu", "brokkr"], vec![]));
assert_eq!(
read(&w, view_of(&w, 3, None)),
SceneRead::NoAddressee { rev: 2 }
);
}

View file

@ -228,9 +228,11 @@ latest plus one, and the latest revision is the highest `n`. Each carries an
Both hold typed feature values only, never text. A feature of slot `Stance`,
`Speaker` or `Scene` sits in that slot alone (each layer has one writer and one
lifetime), which `admit_revision` enforces. `LoweringInput::with_scene` takes the
store and names neither speaker nor scene: the view knows its corpus item, so
speaker-state and corpus stores and names neither speaker nor scene: the view knows its corpus item, so
the speaker is the item's speaker and the scene is the store's latest for the
item's group. The speaker must be a participant (V16), and the relation
item's group. The scene must list the item's speaker and the speaker of every live item of the
group (V16; `Scene::uncovered`, the rule `put_scene` applies too, so a scene a
later corpus item outgrew is refused at the read), and the relation
selected is the speaker's to the entity of the addressee referent (to
`Unnamed` when that referent has no entity, to nothing when the packet has no
addressee or the scene holds no such relation). V13 is retired.