speaker-state: packet stance (slot Stance), seed stance features, drop formality/warmth/intensity, admit is the only LoweringInput source, withdrawn items refused (WIP before regeneration)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Eureka 2026-10-29 17:37:02 +01:00
commit 86371d2ce4
10 changed files with 324 additions and 37 deletions

View file

@ -188,6 +188,7 @@ pub enum Slot {
Referent,
Predication,
Relation,
Stance,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
@ -439,10 +440,28 @@ pub fn seed() -> (InventoryRevision, Vec<InventoryEntry>) {
&pred,
);
let nuance = [Slot::Discourse, Slot::Predication];
for g in ["hedge", "certainty", "warmth", "formality", "intensity"] {
for g in ["hedge", "certainty"] {
add(Family::Feature, g, Some(ValueKind::Grade), &nuance);
}
add(Family::Feature, "irony", Some(ValueKind::Flag), &nuance);
// Felt affect (permille of how strongly it is felt) and the restraint the speaker
// puts on it: slot Stance only. Affection covers fondness toward the
// addressee. Displayed affect is each language's ontology's business.
let stance = [Slot::Stance];
for g in [
"anger",
"fear",
"sadness",
"joy",
"disgust",
"surprise",
"contempt",
"affection",
"shame",
"restraint",
] {
add(Family::Feature, g, Some(ValueKind::Grade), &stance);
}
let changes = entries.iter().map(|e| Change::Add(e.id)).collect();
(
InventoryRevision {

View file

@ -22,7 +22,7 @@ pub use inventory::{
Slot, ValueKind,
};
pub use packet::{
EncoderId, EntityId, Grade, NodeId, Packet, PacketId, PacketKind, SCHEMA_VERSION,
EncoderId, EntityId, Grade, NodeId, Packet, PacketId, PacketKind, SCHEMA_VERSION, Stance,
};
pub use score::{Embedder, ItemScore, OllamaEmbedder, Scorer, chrf};
pub use split::{Split, SplitScheme};

View file

@ -16,7 +16,7 @@ pub use crate::inventory::InvId;
use crate::inventory::OptionLabel;
/// Bumped by a commit that changes these types; the published schema moves with it.
pub const SCHEMA_VERSION: u32 = 3;
pub const SCHEMA_VERSION: u32 = 4;
/// Id of a referent or predication within one packet: `n<k>`, `k` a canonical
/// decimal 1..=9999.
@ -208,6 +208,8 @@ pub struct Packet {
pub extensions: Vec<Extension>,
#[cultcache(key = 12)]
pub trace: Trace,
#[cultcache(key = 13)]
pub stance: Stance,
}
impl Packet {
@ -262,6 +264,14 @@ pub struct Discourse {
pub features: Vec<FeatureValue>,
}
/// The speaker's felt affect and restraint, never displayed affect: how the
/// speaker shows a feeling is each language's ontology's business. A
/// held-back feeling is not a claim, so it is never a predication.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Stance {
pub features: Vec<FeatureValue>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct FeatureValue {
/// Feature id.

View file

@ -15,7 +15,7 @@ use crate::entity::Entity;
use crate::form::is_language_tag;
use crate::inventory::{Change, InvId, Inventory, InventoryEntry, InventoryRevision, revision_key};
use crate::packet::{EntityId, Packet};
use crate::validate::{Refusal, validate};
use crate::validate::{LoweringInput, Refusal, admissible};
/// A broken store rule, named.
#[derive(Clone, Debug, PartialEq, Eq)]
@ -73,6 +73,9 @@ pub enum StoreRefusal {
/// A corpus packet's referent cites an entity that is neither named on its
/// item nor the item's speaker or a prior-turn speaker.
EntityNotOnItem { entity: EntityId, item: String },
/// A new packet names a corpus item already withdrawn. Packets admitted
/// before the withdrawal stay stored and readable.
WithdrawnCorpusItem { key: String },
/// Any other malformed document or change.
Malformed { key: String, why: &'static str },
}
@ -598,9 +601,10 @@ impl PacketStore {
self.cache.get::<Packet>(key)
}
/// The only door: a packet exists only if `validate` passes against the
/// inventory the store holds at the packet's revision, its `corpus_item`
/// resolves, every referent's entity is in the entity store and is named on
/// The only door, and the only source of a [`LoweringInput`]: a packet
/// exists only if the validator's rules pass against the inventory the
/// store holds at the packet's revision, its `corpus_item` resolves to an
/// item that is not withdrawn, every referent's entity is in the entity store and is named on
/// that item or is its speaker or a prior-turn speaker (an id-set check). A
/// key holds its packet for good. A packet that names no corpus item has no
/// door here.
@ -610,9 +614,15 @@ impl PacketStore {
inventory: &InventoryStore,
corpus: &CorpusStore,
entities: &EntityStore,
) -> Result<(), StoreError> {
validate(&packet, &inventory.inventory()?).map_err(StoreError::Invalid)?;
) -> Result<LoweringInput, StoreError> {
let view = admissible(&packet, &inventory.inventory()?).map_err(StoreError::Invalid)?;
let item = corpus.resolve(&packet.provenance.corpus_item)?;
if item.withdrawn {
return Err(StoreRefusal::WithdrawnCorpusItem {
key: item.id.clone(),
}
.into());
}
for entity in packet.referents.iter().filter_map(|r| r.entity.as_ref()) {
if entities.get(entity)?.is_none() {
return Err(StoreRefusal::UnknownEntity {
@ -634,11 +644,11 @@ impl PacketStore {
let key = packet.id.to_string();
if let Some(existing) = self.get(&key)? {
if existing == packet {
return Ok(());
return Ok(view);
}
return Err(StoreRefusal::PacketImmutable { key }.into());
}
self.cache.put(key, &packet)?;
Ok(())
Ok(view)
}
}

View file

@ -1,6 +1,9 @@
//! The validator owns whether a packet exists. Every path that admits or lowers a
//! packet calls `validate`, and its Ok value, [`LoweringInput`], is the only
//! thing a lowerer sees. The rules are V1..V8 and V10..V12: the shape of an id
//! The validator owns the inventory and form rules a packet must meet. Every
//! path that admits a packet applies them, but `validate` only judges: it
//! returns `Ok(())` or the refusals. [`LoweringInput`], the only thing a
//! lowerer sees, comes from `PacketStore::admit` alone, which builds it through
//! the crate-private `admissible`. The stance is checked like every feature
//! list, in slot `Stance`. The rules are V1..V8 and V10..V12: the shape of an id
//! or a grade is its type's parse, not a rule, so V9 is retired and not
//! renumbered.
//!
@ -25,7 +28,7 @@ use std::fmt;
use crate::inventory::{Family, InvId, Inventory, InventoryEntry, Slot, ValueKind};
use crate::packet::{
Constraints, Context, Discourse, Extension, FeatValue, FeatureValue, NodeId, Packet,
PacketKind, Predication, Referent, Relation, SCHEMA_VERSION,
PacketKind, Predication, Referent, Relation, SCHEMA_VERSION, Stance,
};
/// A broken rule, named.
@ -91,9 +94,34 @@ impl fmt::Display for Refusal {
}
}
/// Whether `packet` may exist under `inventory`. All broken rules are returned;
/// a packet that passes yields the lowering view, which only this function builds.
pub fn validate(packet: &Packet, inventory: &Inventory) -> Result<LoweringInput, Vec<Refusal>> {
/// Whether `packet` meets the rules under `inventory`. All broken rules are
/// returned. A pass carries no lowering view: that comes from
/// `PacketStore::admit` only.
///
/// ```
/// use weksa::{Inventory, Refusal, packet::Packet};
/// fn judge(p: &Packet, i: &Inventory) -> Result<(), Vec<Refusal>> { weksa::validate(p, i) }
/// ```
///
/// ```compile_fail
/// use weksa::{Inventory, LoweringInput, Refusal, packet::Packet};
/// fn lower(p: &Packet, i: &Inventory) -> Result<LoweringInput, Vec<Refusal>> { weksa::validate(p, i) }
/// ```
pub fn validate(packet: &Packet, inventory: &Inventory) -> Result<(), Vec<Refusal>> {
admissible(packet, inventory).map(|_| ())
}
/// The one implementation of the rules. Crate-private: `PacketStore::admit` is
/// the only caller that keeps the view.
///
/// ```compile_fail
/// use weksa::{Inventory, packet::Packet};
/// fn lower(p: &Packet, i: &Inventory) { let _ = weksa::validate::admissible(p, i); }
/// ```
pub(crate) fn admissible(
packet: &Packet,
inventory: &Inventory,
) -> Result<LoweringInput, Vec<Refusal>> {
let mut out = Vec::new();
if packet.schema_version != SCHEMA_VERSION {
out.push(Refusal::V1SchemaVersion {
@ -221,6 +249,7 @@ impl<'a> Inventoried<'a> {
predications,
relations,
extensions,
stance,
..
} = p;
let Context {
@ -238,6 +267,7 @@ impl<'a> Inventoried<'a> {
self.slot(id, Family::SpeechAct, "speech act");
}
self.features(features, Slot::Discourse);
self.features(&stance.features, Slot::Stance);
for Referent {
kind,
concept,
@ -349,8 +379,9 @@ fn fits(kind: Option<&ValueKind>, value: &FeatValue) -> bool {
/// 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 `validate` is the only constructor, so a lowerer
/// cannot be handed an unvalidated packet or the key to an answer text.
/// fields are private and the constructor is crate-private, called only by
/// `PacketStore::admit`, so a lowerer cannot be handed an unadmitted packet or
/// the key to an answer text.
///
/// Each field has a doctest that reads it from outside the crate. A read
/// compiles exactly when the field is `pub`, so the doctest (which must fail to
@ -394,6 +425,10 @@ fn fits(kind: Option<&ValueKind>, value: &FeatValue) -> bool {
/// fn read(l: weksa::LoweringInput) { let _ = l.extensions; }
/// ```
///
/// ```compile_fail
/// fn read(l: weksa::LoweringInput) { let _ = l.stance; }
/// ```
///
/// The constructor is private: a call from outside the crate compiles only if
/// `LoweringInput::of` is `pub`. The first block is the same code without the
/// call, so the second fails for that call and nothing else.
@ -430,6 +465,7 @@ pub struct LoweringInput {
relations: Vec<Relation>,
constraints: Constraints,
extensions: Vec<Extension>,
stance: Stance,
}
impl LoweringInput {
@ -444,6 +480,7 @@ impl LoweringInput {
relations: p.relations.clone(),
constraints: p.constraints.clone(),
extensions: p.extensions.clone(),
stance: p.stance.clone(),
}
}
@ -474,4 +511,7 @@ impl LoweringInput {
pub fn extensions(&self) -> &[Extension] {
&self.extensions
}
pub fn stance(&self) -> &Stance {
&self.stance
}
}