Huginn/crates/mind-body/src/address.rs
Meta Vens f146be2e65 Projector after Soul's third pass: a glued-name boundary rule; one selection for both renderings of the messages to her; JSON escapes for bodies and display names; failures keep the report and their usage; a 60% floor and CJK words in the cap; addressing age; bounds under the connector frame
T1: address.rs names_whole_word: a name is glued only by a letter or digit, or
a single hyphen with one beyond it; every other character is a boundary, so
_Eureka_, Eureka/Libby, ok...Eureka? and Huginn.toml name her again and
mind-eureka does not. Soul's 72 forms pinned.
T2: said_to_her decides once; stimulus and heard both carry the count.
T4: Model::ask fails with Failed { reason, usage }; a failed render or judge
call is recorded with its usage and the report goes on.
T5: the sentence cut keeps at least 60% of the budget; 。!? end sentences;
CJK characters are words. T6/T7: spoken is a JSON string; a display name is
quoted, capped at 80 bytes and followed by its Matrix id. T8: the addressing
report gives the table's age and why it was not used. T9: own beat lines
excluded (tested); words, stimulus, item and the other parts bounded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 00:28:55 +01:00

121 lines
6.5 KiB
Rust

//! Whether a chat delivery is for a Mind: the one rule the heartbeat's off-beat wake and beat score and the Persona's `to_you` and
//! stagger rank all call. A delivery addresses a Mind when its `m.mentions` holds her Matrix id (a tag), or its text holds one of her
//! names as a whole word. Her names are her own (handle, public name; case does not matter) and her projects': the names her
//! stewardship records list, matched exactly as written, or, when a record lists none, the repo's name matched exactly as the repo
//! spells it and, for a multi-word name, its split form matched in any case. A deterministic identity match on names, not a reading of meaning.
use eureka_pipeline::{OrgRepo, Short};
use crate::chat_client::{Basis, ChatDelivery, Form};
/// A word a message may call a Mind or her project by.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Name {
pub(crate) text: String,
/// Matched case-sensitively, as written; otherwise in any case.
pub(crate) exact: bool,
}
/// Whom a delivery may be addressing: one Mind's Matrix id, her names and the repos she stewards.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Addressee {
matrix_id: String,
names: Vec<Name>,
/// The repos her in-force stewardships name, as the one read that gave `names` found them: what a ruling of hers may be decided under.
#[serde(default)]
repos: Vec<OrgRepo>,
}
impl Addressee {
/// A Mind by her id and her own names, each matched in any case.
pub(crate) fn new<'a>(matrix_id: &str, own_names: impl IntoIterator<Item = &'a str>) -> Self {
Self { matrix_id: matrix_id.to_string(), names: own_names.into_iter().map(|text| Name { text: text.to_string(), exact: false }).collect(), repos: Vec::new() }
}
/// The same Mind, also addressed by her projects' names, with the repos those stewardships name.
pub(crate) fn with_stewardships(mut self, names: impl IntoIterator<Item = Name>, repos: impl IntoIterator<Item = OrgRepo>) -> Self {
self.names.extend(names);
self.repos.extend(repos);
self
}
pub(crate) fn matrix_id(&self) -> &str {
&self.matrix_id
}
}
/// The part of a Matrix id between `@` and `:`, the name the chat calls a Mind by.
pub(crate) fn localpart(matrix_id: &str) -> &str {
let id = matrix_id.strip_prefix('@').unwrap_or(matrix_id);
id.split_once(':').map_or(id, |(local, _)| local)
}
/// Whether a delivery is for `who` by what it says: only a message can be (a notice or an edit never is, whatever else it says), and
/// then a tag of hers in its mentions or a word of hers in the text. What Matrix says about the Persona that polled (`Basis`) is not
/// here: it is a fact about that one Persona, not about every Mind in the table (`is_for`).
pub(crate) fn addresses(who: &Addressee, delivery: &ChatDelivery) -> bool {
delivery.form == Form::Message && (delivery.mentions.contains(&who.matrix_id) || who.names.iter().any(|name| names_whole_word(&delivery.text, &name.text, name.exact)))
}
/// Whether a delivery the mouth gave `own` is for her: her DM (`is_direct`, which every mouth sends, so an old mouth's DM is hers too),
/// Matrix's word that it is a tag of hers or a reply to her, or what it says (`addresses`). The basis `Room` (`@room`) is for no one, and no basis is unproven, not "not for her": the names still decide.
pub(crate) fn is_for(own: &Addressee, delivery: &ChatDelivery) -> bool {
(delivery.form == Form::Message && (delivery.is_direct || matches!(delivery.basis, Some(Basis::Direct | Basis::Tag | Basis::Reply | Basis::FollowUp)))) || addresses(own, delivery)
}
/// The chat names of one stewarded repo. Listed names are exactly those, each matched exactly as the steward wrote it (capitalization
/// tells a project from a common word). With none listed: the repo's name after the org, matched exactly as the repo spells it
/// (`Thing`, not `thing`), and that name split at `-`, `_`, `.` and where a lower-case letter or digit meets a capital, when the split
/// reads differently, matched in any case (a split phrase is not a common word).
pub(crate) fn project_names(repo: &OrgRepo, listed: &[Short]) -> Vec<Name> {
if !listed.is_empty() {
return listed.iter().map(|name| Name { text: name.0.clone(), exact: true }).collect();
}
let whole = repo.0.split_once('/').map_or(repo.0.as_str(), |(_, name)| name);
let mut split = String::new();
let mut previous: Option<char> = None;
for ch in whole.chars() {
if matches!(ch, '-' | '_' | '.') {
split.push(' ');
} else {
if ch.is_uppercase() && previous.is_some_and(|before| before.is_lowercase() || before.is_ascii_digit()) {
split.push(' ');
}
split.push(ch);
}
previous = Some(ch);
}
let split = split.to_lowercase();
let mut names = vec![Name { text: whole.to_string(), exact: true }];
if split != whole.to_lowercase() {
names.push(Name { text: split, exact: false });
}
names
}
/// Whether `text` holds `name` as a whole word, in any case or exactly as written. A name is glued, and so not her name,
/// when a letter or digit touches it, or a single hyphen joins it to one (`mind-eureka`, `Eureka-chan`); everything else
/// is a boundary on either side: whitespace, punctuation, `/`, `_`, `.`, `\`, and a double hyphen or a dash. So
/// `/var/lib/mind-eureka/` never names her, and `_Eureka_`, `Eureka/Libby`, `ok...Eureka?` and `Huginn.toml` do. An
/// identity match on a name, not a judgement of meaning.
pub(crate) fn names_whole_word(text: &str, name: &str, exact: bool) -> bool {
let (text, name) = if exact { (text.to_string(), name.to_string()) } else { (text.to_lowercase(), name.to_lowercase()) };
if name.is_empty() {
return false;
}
// Every character offset, not every non-overlapping match: a name may overlap itself ("ab ab" in "xab ab ab").
text.char_indices().any(|(start, _)| text[start..].starts_with(&name) && !glued(text[..start].chars().rev()) && !glued(text[start + name.len()..].chars()))
}
/// Whether the characters leading away from a name, nearest first, glue it to more of a word: a letter or digit, or a
/// single hyphen with a letter or digit beyond it.
fn glued(mut away: impl Iterator<Item = char>) -> bool {
match away.next() {
Some(c) if c.is_alphanumeric() => true,
Some('-') => away.next().is_some_and(char::is_alphanumeric),
_ => false,
}
}
#[cfg(test)]
mod tests;