Huginn/crates/mind-body/src/address.rs
Meta Vens 0fd12325bd Cut the mission organ and every Persona-side Mind write (C2)
Map: docs/persona-only-heartbeat-map.md (imagination/persona-only-heartbeat,
d861574), cut C2. Rulings: eureka-body:ruling:persona-only-heartbeat (she
reads the Mind and never writes it; no Self runs from the Persona).

Persona organ (persona.rs 3983 -> ~420 lines): deleted the live session
organ whole: TURN_INSTRUCTION, TURN_REMINDER, MESSAGES_HEADER, WORK_QUESTION,
ANSWER_MESSAGES, WAKE_ASK, WORK_TAIL; PersonaReply with intent, remember,
rule, settle, start_self_run, tempo vote and urgency, and reply_schema; the
doctrine and persona-JSON system prompt (read_doctrine, render_instructions,
campaigns); start_session, persona_args, PersonaInbox; act, decode_reply and
every admission (memories, rulings, settles, pulses, follow-ups, run closure);
start_self_run; the responsibilities, recent-runs and wear renderers;
render_input and render_tempo. carry reads the brakes and answers Dead or
NoOrgan; a start writes nothing and records no run. Kept per the map's keep
list for C3: Config (doctrine, image store and fetch fields cut with their
consumers), the brake read, the chat-addressing helpers, the tail renderer
and FileTrace; uncalled ones carry allow(dead_code) naming C3.

Night: sleep.rs is deleted. It was a Mind-writing session built on the
deleted prelude; C4 retargets its contract from history.
attachment.rs is deleted with the reply's save_attachment, its one caller.
chat_image's loader and recall.rs stay for C3 under allow(dead_code).

Heartbeat: no Mind writes. A wake is a plain unit start through the
Launcher (brake, then alive, then start); busy is the unit's own state.
Deleted the Self-run branch (claimable briefs), the self-ended wake, tempo
votes and declared urgency (standing_vote, pending_urgency, last_self_end)
and the run-based last_turn and last_sleep reads; HeartbeatState gains
last_turn_at and last_night_at (default slots, so a state written before
this cut still decodes). The beat is HEARTBEAT_BEAT_S, default 1800 s.
InstanceTrace.vote_beat_s stays as an always-none slot so heartbeat.cc's
older trace records still decode. HeartbeatView loses latest; the
timeline reads FileStore::latest.

tempo.rs: tally, fallback_beat and the PID controller and its sim are
deleted; pick and score stay, without self_permille.

Tests that pinned deleted behaviour are deleted with it; surviving ones are
kept, and two heartbeat tests pin the cut: a Mind with claimable briefs gets
a Persona wake and no Self run, and no poll admits anything to any mind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 22:06:11 +01:00

112 lines
5.9 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: the characters on either side are not letters or
/// digits. 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)
&& !text[..start].chars().next_back().is_some_and(char::is_alphanumeric)
&& !text[start + name.len()..].chars().next().is_some_and(char::is_alphanumeric)
})
}
#[cfg(test)]
mod tests;