with_scene records what it read: SceneRead says no scene, no addressee, self-addressed, no relation or selected at a revision

The view no longer collapses five cases into one empty feature list. Scene::relation
returns None for an absent relation. Pins the item speaker over the first
participant and V16 without an addressee.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Weksa 2026-11-10 03:10:21 +00:00
commit bb152d02ce
4 changed files with 192 additions and 62 deletions

View file

@ -38,4 +38,4 @@ pub use store::{
AddOutcome, CorpusStore, EntityBound, EntityStore, InventoryStore, PacketStore,
SpeakerStateStore, StoreError, StoreRefusal,
};
pub use validate::{LoweringInput, Refusal, validate};
pub use validate::{LoweringInput, Refusal, SceneRead, validate};

View file

@ -83,9 +83,14 @@ pub struct Scene {
impl Scene {
/// The features of the relation from `speaker` to the named `addressee`, or
/// to `Unnamed` when there is none; empty when the scene holds no such
/// relation. A named addressee the scene lacks never falls back to `Unnamed`.
pub fn relation(&self, speaker: &EntityId, addressee: Option<&EntityId>) -> &[FeatureValue] {
/// to `Unnamed` when there is none; `None` when the scene holds no such
/// relation (a relation with no features is `Some(&[])`). A named
/// addressee the scene lacks never falls back to `Unnamed`.
pub fn relation(
&self,
speaker: &EntityId,
addressee: Option<&EntityId>,
) -> Option<&[FeatureValue]> {
self.relations
.iter()
.find(|r| {
@ -96,7 +101,7 @@ impl Scene {
_ => false,
}
})
.map_or(&[], |r| r.features.as_slice())
.map(|r| r.features.as_slice())
}
}

View file

@ -499,6 +499,28 @@ fn fits(kind: Option<&ValueKind>, value: &FeatValue) -> bool {
}
}
/// What `with_scene` found for a view: the scene revision `rev` it read, or why
/// it selected no relation. Each case is distinct so a missing annotation or a
/// store at the wrong path cannot look like a scene that held nothing.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SceneRead {
/// `with_scene` has not run.
Unread,
/// The store holds no scene for the item's group.
NoScene,
/// The scene was read and the packet names no addressee.
NoAddressee { rev: u32 },
/// The addressee referent is the speaker themself.
SelfAddressed { rev: u32 },
/// The scene holds no relation from the speaker to this addressee.
NoRelation { rev: u32 },
/// The relation was found; its features may be none.
Selected {
rev: u32,
features: Vec<FeatureValue>,
},
}
/// What the lowerer sees: a validated packet's meaning, with no packet id, no
/// provenance (so no corpus item key, encoder or source) and no trace. Its
/// fields are private and the constructor is crate-private, called only by
@ -623,7 +645,7 @@ pub struct LoweringInput {
/// only source of whose relation `with_scene` selects, and from which scene.
speaker: EntityId,
group: String,
scene: Vec<FeatureValue>,
scene: SceneRead,
}
impl LoweringInput {
@ -642,44 +664,61 @@ impl LoweringInput {
stance: p.stance.clone(),
speaker: item.speaker.clone(),
group: item.group.clone(),
scene: Vec::new(),
scene: SceneRead::Unread,
}
}
/// Adds the speaker's relation to the addressee, the only path for scene
/// 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. No scene for the group selects nothing; a scene that does not
/// list the item's speaker is V16. With no addressee nothing is selected;
/// otherwise the relation from the speaker to the addressee referent's
/// entity (to `Unnamed` when the referent has none) is checked like every
/// feature list, in slot `Scene` at this view's inventory revision, and
/// replaces any earlier scene. The English lowerer ignores it.
/// 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
/// [`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
/// referent's entity (to `Unnamed` when the referent has none), whose
/// features are checked like every feature list in slot `Scene` at this
/// view's inventory revision. A read replaces any earlier one. The English
/// lowerer ignores it.
pub fn with_scene(
mut self,
inventory: &Inventory,
state: &SpeakerStateStore,
) -> Result<Self, StoreError> {
self.scene = Vec::new();
self.scene = SceneRead::Unread;
let Some(scene) = state.scene(&self.group)? else {
self.scene = SceneRead::NoScene;
return Ok(self);
};
let rev = scene.n;
if !scene.participants.contains(&self.speaker) {
return Err(StoreError::Invalid(vec![Refusal::V16SpeakerNotInScene]));
}
let selected = self
.discourse
.addressee
.as_ref()
.and_then(|a| self.referents.iter().find(|r| &r.id == a))
.map_or(&[][..], |r| {
scene.relation(&self.speaker, r.entity.as_ref())
});
let out = feature_refusals(inventory, self.inventory_rev, selected, Slot::Scene);
let Some(addressee) = self.discourse.addressee.as_ref() else {
self.scene = SceneRead::NoAddressee { rev };
return Ok(self);
};
let entity = self
.referents
.iter()
.find(|r| &r.id == addressee)
.and_then(|r| r.entity.as_ref());
if entity == Some(&self.speaker) {
self.scene = SceneRead::SelfAddressed { rev };
return Ok(self);
}
let Some(features) = scene.relation(&self.speaker, entity) else {
self.scene = SceneRead::NoRelation { rev };
return Ok(self);
};
let out = feature_refusals(inventory, self.inventory_rev, features, Slot::Scene);
if !out.is_empty() {
return Err(StoreError::Invalid(out));
}
self.scene = selected.to_vec();
self.scene = SceneRead::Selected {
rev,
features: features.to_vec(),
};
Ok(self)
}
@ -716,6 +755,13 @@ impl LoweringInput {
/// The features of the scene relation from the speaker to the addressee;
/// empty until `with_scene`, or when the scene holds none.
pub fn scene(&self) -> &[FeatureValue] {
match &self.scene {
SceneRead::Selected { features, .. } => features,
_ => &[],
}
}
/// What `with_scene` read; `Unread` until it ran.
pub fn scene_read(&self) -> &SceneRead {
&self.scene
}
}

View file

@ -11,7 +11,7 @@ use weksa::packet::{FeatValue, FeatureValue, Packet, proof_packet};
use weksa::speaker::{Annotator, PairRelation, Party, Scene};
use weksa::{
CorpusStore, EntityStore, Inventory, InventoryStore, LoweringInput, NameSpan, PacketStore,
Refusal, SpeakerStateStore, StoreError, lower,
Refusal, SceneRead, SpeakerStateStore, StoreError, lower,
};
struct W {
@ -121,9 +121,15 @@ fn put(w: &mut W, s: Scene) {
/// The line of `item:nibu:1` at `rev`, its addressee node `addressee` (`n1`
/// has no entity, `n3` is Brokkr), through the packet door.
fn view_of(w: &W, rev: u32, addressee: Option<&str>) -> LoweringInput {
view_to(w, rev, addressee, "brokkr")
}
/// As `view_of`, with referent `n3` naming `entity` (Nibu is the item's
/// speaker, so he may be named on the line; Brokkr is named by its text).
fn view_to(w: &W, rev: u32, addressee: Option<&str>, entity: &str) -> LoweringInput {
let mut p: Packet = proof_packet();
p.inventory_rev = rev;
p.referents[2].entity = Some(eid("brokkr"));
p.referents[2].entity = Some(eid(entity));
p.discourse.addressee = addressee.map(nd);
// A packet key holds its packet for good, so each view gets a fresh store.
let scratch = tempfile::tempdir().unwrap();
@ -140,14 +146,27 @@ fn rules(r: Result<LoweringInput, StoreError>) -> Vec<&'static str> {
}
}
fn read(w: &W, view: LoweringInput) -> SceneRead {
view.with_scene(&inv(w), &w.state)
.unwrap()
.scene_read()
.clone()
}
fn selected(rev: u32, features: Vec<FeatureValue>) -> SceneRead {
SceneRead::Selected { rev, features }
}
#[test]
fn a_scene_relation_reaches_the_lowering_view_and_changes_no_english() {
let mut w = world();
put(&mut w, standard("g", 1));
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();
assert_eq!(with.scene(), status().as_slice());
assert_eq!(with.scene_read(), &selected(1, status()));
assert_eq!(
lower(&with, &inv(&w), &w.entities).unwrap(),
lower(&plain, &inv(&w), &w.entities).unwrap()
@ -155,23 +174,54 @@ fn a_scene_relation_reaches_the_lowering_view_and_changes_no_english() {
}
#[test]
fn with_scene_selects_by_addressee() {
fn with_scene_says_which_of_five_things_it_found() {
let mut w = world();
// No scene for the group.
assert_eq!(read(&w, view_of(&w, 3, Some("n3"))), SceneRead::NoScene);
put(&mut w, standard("g", 1));
let take = |w: &W, addressee| {
view_of(w, 3, addressee)
.with_scene(&inv(w), &w.state)
.unwrap()
.scene()
.to_vec()
};
// A referent with an entity selects the relation to that entity; a
// referent without one, the relation to an unnamed party; and the
// speaker's relation, never Brokkr's to Nibu.
assert_eq!(take(&w, Some("n3")), status());
assert_eq!(take(&w, Some("n1")), intimacy(7));
// No addressee selects nothing.
assert!(take(&w, None).is_empty());
assert_eq!(read(&w, view_of(&w, 3, Some("n3"))), selected(1, status()));
assert_eq!(
read(&w, view_of(&w, 3, Some("n1"))),
selected(1, intimacy(7))
);
// The scene was read and the packet names no addressee.
assert_eq!(
read(&w, view_of(&w, 3, None)),
SceneRead::NoAddressee { rev: 1 }
);
// The addressee is the speaker.
assert_eq!(
read(&w, view_to(&w, 3, Some("n3"), "nibu")),
SceneRead::SelfAddressed { rev: 1 }
);
// The scene holds no relation to this addressee.
put(
&mut w,
scene(
"g",
2,
&["nibu", "brokkr"],
vec![relation("nibu", Party::Unnamed, intimacy(7))],
),
);
assert_eq!(
read(&w, view_of(&w, 3, Some("n3"))),
SceneRead::NoRelation { rev: 2 }
);
// A relation with no features is found, not absent.
put(
&mut w,
scene(
"g",
3,
&["nibu", "brokkr"],
vec![relation("nibu", Party::Named(eid("brokkr")), vec![])],
),
);
assert_eq!(read(&w, view_of(&w, 3, Some("n3"))), selected(3, vec![]));
}
#[test]
@ -187,10 +237,10 @@ fn a_named_addressee_the_scene_lacks_never_falls_back_to_unnamed() {
vec![relation("nibu", Party::Unnamed, intimacy(7))],
),
);
let got = view_of(&w, 3, Some("n3"))
.with_scene(&inv(&w), &w.state)
.unwrap();
assert!(got.scene().is_empty());
assert_eq!(
read(&w, view_of(&w, 3, Some("n3"))),
SceneRead::NoRelation { rev: 1 }
);
// A participant without a relation behaves the same.
put(
&mut w,
@ -201,10 +251,32 @@ fn a_named_addressee_the_scene_lacks_never_falls_back_to_unnamed() {
vec![relation("nibu", Party::Unnamed, intimacy(7))],
),
);
let got = view_of(&w, 3, Some("n3"))
.with_scene(&inv(&w), &w.state)
.unwrap();
assert!(got.scene().is_empty());
assert_eq!(
read(&w, view_of(&w, 3, Some("n3"))),
SceneRead::NoRelation { rev: 2 }
);
}
#[test]
fn the_speaker_is_the_items_not_the_first_participant() {
let mut w = world();
// Brokkr is listed first and has an unnamed relation of his own.
put(
&mut w,
scene(
"g",
1,
&["brokkr", "nibu"],
vec![
relation("brokkr", Party::Unnamed, intimacy(99)),
relation("nibu", Party::Unnamed, intimacy(7)),
],
),
);
assert_eq!(
read(&w, view_of(&w, 3, Some("n1"))),
selected(1, intimacy(7))
);
}
#[test]
@ -220,11 +292,8 @@ fn the_scene_is_the_items_group_and_its_latest_revision() {
vec![relation("nibu", Party::Named(eid("brokkr")), intimacy(1))],
),
);
// No scene for the item's group selects nothing, whatever other groups hold.
let got = view_of(&w, 3, Some("n3"))
.with_scene(&inv(&w), &w.state)
.unwrap();
assert!(got.scene().is_empty());
// No scene for the item's group, whatever other groups hold.
assert_eq!(read(&w, view_of(&w, 3, Some("n3"))), SceneRead::NoScene);
// Revisions: the highest n is read.
put(&mut w, standard("g", 1));
let mut second = standard("g", 2);
@ -233,10 +302,12 @@ fn the_scene_is_the_items_group_and_its_latest_revision() {
let got = view_of(&w, 3, Some("n3"))
.with_scene(&inv(&w), &w.state)
.unwrap();
assert_eq!(got.scene_read(), &selected(2, intimacy(5)));
assert_eq!(got.scene(), intimacy(5).as_slice());
// A replacement replaces: a store without the scene clears the selection.
// 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();
assert_eq!(cleared.scene_read(), &SceneRead::NoScene);
assert!(cleared.scene().is_empty());
}
@ -262,13 +333,21 @@ fn with_scene_refusals() {
&w.entities,
)
.unwrap();
let mut p = proof_packet();
p.inventory_rev = 3;
p.provenance.corpus_item = weksa::CorpusItemId::parse("item:nibu:3").unwrap();
p.referents[2].entity = Some(eid("nibu"));
let view = PacketStore::open(&w.dir.path().join("p3.cc"))
.unwrap()
.admit(p, &w.inventory, &w.corpus, &w.entities)
.unwrap();
assert_eq!(rules(view.with_scene(&inv(&w), &w.state)), ["V16"]);
for addressee in [Some("n3"), None] {
let mut p = proof_packet();
p.inventory_rev = 3;
p.provenance.corpus_item = weksa::CorpusItemId::parse("item:nibu:3").unwrap();
p.referents[2].entity = Some(eid("nibu"));
p.discourse.addressee = addressee.map(nd);
let scratch = tempfile::tempdir().unwrap();
let view = PacketStore::open(&scratch.path().join("p.cc"))
.unwrap()
.admit(p, &w.inventory, &w.corpus, &w.entities)
.unwrap();
assert_eq!(
rules(view.with_scene(&inv(&w), &w.state)),
["V16"],
"{addressee:?}"
);
}
}