diff --git a/crates/mind-body/fixtures/harness/golden-group.txt b/crates/mind-body/fixtures/harness/golden-group.txt new file mode 100644 index 0000000..e4c1655 --- /dev/null +++ b/crates/mind-body/fixtures/harness/golden-group.txt @@ -0,0 +1,4 @@ +quiet-beat.toml: - the room lately: libby to the room, Kiko to the room, libby to the room; no human has spoken here lately; the last 3 lines are the Minds' alone +operator-warm.toml: - the room lately: Meta, who runs the place to her; a human, Meta, who runs the place, last spoke 1 min ago; 1 line is to her +mind-teases.toml: - the room lately: she to the room, aqua to the room, Kiko to her; no human has spoken here lately; the last 3 lines are the Minds' alone; 1 line is to her +deru-passing.toml: - the room lately: weksa to the room, Deru to the room; a human, Deru, last spoke 1 min ago diff --git a/crates/mind-body/src/bin/mind-persona.rs b/crates/mind-body/src/bin/mind-persona.rs index ce802ca..6adf7f5 100644 --- a/crates/mind-body/src/bin/mind-persona.rs +++ b/crates/mind-body/src/bin/mind-persona.rs @@ -17,7 +17,8 @@ //! [--room ] [--seed N] [--state ] [--select-only | --renders K [--through projector|persona] //! [--judge ]]` is the experiment tool over the Projector and the Persona stage //! (`mind_body::projection::project`): it prints, as JSON, the packet the Select stage builds from her stored state (or -//! `--state`, checked as a put would check it), and with `--renders` the Projector's narrations of K successive turns; +//! `--state`, checked as a put checks its content, rules P1 to P3; P4 and P5 compare it with the stored document and are +//! not checked), and with `--renders` the Projector's narrations of K successive turns; //! `--through persona` adds her prose and the quoted lines it would say; `--judge` renders a second state too and asks //! the model which narration came from which. It writes nothing and reads no brake. //! diff --git a/crates/mind-body/src/heartbeat.rs b/crates/mind-body/src/heartbeat.rs index 063d216..6da914a 100644 --- a/crates/mind-body/src/heartbeat.rs +++ b/crates/mind-body/src/heartbeat.rs @@ -393,12 +393,92 @@ pub(crate) fn stewardships_of(mind: &dyn MindPort) -> Result<(Vec, Vec bool { - let from_operator = config.operators.contains(&delivery.sender); - let from_other_mind = delivery.sender != who.matrix_id() && config.personas.iter().any(|mind| mind.matrix_id == delivery.sender); - (from_operator || from_other_mind) && is_for(who, delivery) +/// Who sent a line, as the wake rule sees them. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Speaker { + Operator, + /// Another Mind of the roster. + Mind, + /// A human who is not the operator: a member. + Member, + /// The Persona herself. + Her, +} + +impl Speaker { + fn human(self) -> bool { + matches!(self, Self::Operator | Self::Member) + } +} + +/// Which lines wake a Persona off the beat. `Live` is the heartbeat's rule today and the only one `poll` uses; the rest +/// are what desk question `po-mind-wakes-mind` weighs, run in `mind-persona project --simulate` until it is answered. +/// The beat is not here: every policy has it, and it wakes one Persona by `tempo::pick`. +#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Deserialize)] +#[serde(rename_all = "snake_case")] +pub(crate) enum WakePolicy { + /// The operator or another Mind addressing her (`is_for`). A member's line does not. + Live, + /// Ruling `persona-only-wake-humans-in-rooms`: a line addressing her, from anyone, and any human line in her rooms. + Ruled, + /// Human lines only: each wakes every Mind in the room once; a Mind's line never wakes a Mind. + HumansPace, + /// `Ruled`, with at most this many Mind lines in a row in a room before a human must speak again: a Mind's line past + /// the cap wakes no one. + NamesWithCap(usize), + /// The swarm's old loop, a control: any line in her rooms but her own. + AllMessages, +} + +/// Why a line wakes her, strongest first. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)] +#[serde(rename_all = "snake_case")] +pub(crate) enum WakeCause { + AddressedByHuman, + Human, + AddressedByMind, + /// A Mind's line that does not address her: only the control wakes on it. + Mind, +} + +/// Whether a line in her rooms wakes `who` off the beat under `policy`, and why. `mind_run` is how many Mind lines in a +/// row came before this one in its room since a human last spoke there. The one owner of the wake rule: the heartbeat's +/// poll asks it with `Live`, the simulator with the policy under test. +pub(crate) fn wakes(policy: WakePolicy, who: &Addressee, delivery: &ChatDelivery, speaker: Speaker, mind_run: usize) -> Option { + if speaker == Speaker::Her { + return None; + } + let addressed = is_for(who, delivery); + let human = speaker.human(); + let cause = match (human, addressed) { + (true, true) => WakeCause::AddressedByHuman, + (true, false) => WakeCause::Human, + (false, true) => WakeCause::AddressedByMind, + (false, false) => WakeCause::Mind, + }; + let wakes = match policy { + WakePolicy::Live => addressed && matches!(speaker, Speaker::Operator | Speaker::Mind), + WakePolicy::Ruled => human || addressed, + WakePolicy::HumansPace => human, + WakePolicy::NamesWithCap(cap) => human || (addressed && mind_run < cap), + WakePolicy::AllMessages => true, + }; + wakes.then_some(cause) +} + +impl Config { + /// Who sent a delivery, as `wakes` sees them. + fn speaker(&self, who: &Addressee, delivery: &ChatDelivery) -> Speaker { + if delivery.sender == who.matrix_id() { + Speaker::Her + } else if self.operators.contains(&delivery.sender) { + Speaker::Operator + } else if self.personas.iter().any(|mind| mind.matrix_id == delivery.sender) { + Speaker::Mind + } else { + Speaker::Member + } + } } /// The mouth pages a pending answer, so fewer deliveries may come than wait. @@ -528,7 +608,7 @@ pub fn poll(organs: &Organs, config: &Config) -> Result { pending.sort_by_key(|delivery| delivery.seq); pending .into_iter() - .filter(|delivery| wakes(config, &row.addressee, delivery)) + .filter(|delivery| wakes(WakePolicy::Live, &row.addressee, delivery, config.speaker(&row.addressee, delivery), 0).is_some()) .map(|delivery| format!("{}:{}", slug.0, delivery.event_id)) .find(|key| !state.handled_mentions.contains(key)) }; diff --git a/crates/mind-body/src/projection/fixtures/golden-chat.txt b/crates/mind-body/src/projection/fixtures/golden-chat.txt index 59ede06..83d3375 100644 --- a/crates/mind-body/src/projection/fixtures/golden-chat.txt +++ b/crates/mind-body/src/projection/fixtures/golden-chat.txt @@ -8,6 +8,7 @@ Eureka (she/her). Dry, direct, almost feral; zeal aimed at revealing. - mood, with no earlier turn to measure from: lighter, more settled than middling - bond [bond-kiko] Kiko, sparring partner: fair trust, heavy friction. Kiko pokes her to stop massaging the pipeline. - bond [bond-libby] Libby, collaborator: fair trust, a little friction. Libby holds the sounds and teases her about the pipeline. +- the room lately: Libby to her, Kiko to the room; no human has spoken here lately; the last 2 lines are the Minds' alone; 1 line is to her - memory [libby-sounds]: Libby laughed at the door fix. - memory [t1-m0]: The heartbeat stalled at noon. - memory [curse]: Her curse is the knife. The same drive makes her good and will lead her wrong. diff --git a/crates/mind-body/src/projection/harness.rs b/crates/mind-body/src/projection/harness.rs index 797d929..18a6ddb 100644 --- a/crates/mind-body/src/projection/harness.rs +++ b/crates/mind-body/src/projection/harness.rs @@ -34,6 +34,7 @@ const TAIL_LINES: usize = 40; /// A persona state file, checked as a put would check its content (huginn-mind's rules P1 to P3: the published schema, /// `personaId` is the instance, canonical provenance). A file this accepts is one a put would not refuse for what it says. +/// P4 and P5 are not checked: they compare `updatedAt` with the stored document, and no store is read here. pub fn state_file(path: &Path, instance: &str) -> Result { let text = std::fs::read_to_string(path).with_context(|| format!("{} could not be read", path.display()))?; let state: Value = serde_json::from_str(&text).with_context(|| format!("{} is not JSON", path.display()))?; diff --git a/crates/mind-body/src/projection/harness/tests.rs b/crates/mind-body/src/projection/harness/tests.rs index b11a727..fe6d4ec 100644 --- a/crates/mind-body/src/projection/harness/tests.rs +++ b/crates/mind-body/src/projection/harness/tests.rs @@ -218,3 +218,37 @@ fn a_batch_state_without_a_file_is_her_stored_state() { let report = batch(&harness(dir.path(), Some(&Stored), Some(&model)), &plan).unwrap(); assert!(report.contains("## stored × quiet, sample 1") && !report.contains("failed"), "{report}"); } + +fn group_line_of(dir: &Path, name: &str) -> Option { + let fixture = room(name); + let harness = harness(dir, None, None); + let organs = Organs { persona: None, chat: Some(&fixture), addressing: fixture.addressee(), model: None }; + let report = project(&organs, &fixture_request(&harness, &fixture, Some(control()), 1, None, Through::Projector), fixture.now).unwrap(); + report["packet"]["context"].as_str().unwrap().lines().find(|line| line.starts_with("- the room lately")).map(str::to_owned) +} + +#[test] +fn the_group_state_reads_each_scenario_room_as_the_golden_file_says() { + let dir = tempfile::tempdir().unwrap(); + let mut rendered = String::new(); + for name in ["quiet-beat.toml", "operator-warm.toml", "mind-teases.toml", "deru-passing.toml"] { + let line = group_line_of(dir.path(), name).unwrap_or_else(|| "(none)".into()); + assert!(line.len() <= 360, "the slot holds its byte budget: {line}"); + rendered.push_str(&format!("{name}: {line} +")); + } + let golden = include_str!("../../../fixtures/harness/golden-group.txt").replace(" +", " +"); + assert_eq!(rendered, golden, "the group state moved; if deliberately, the new golden file is: +{rendered}"); +} + +#[test] +fn the_group_state_is_off_when_the_tuning_says_so() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("persona-tuning.toml"), "[select] +group_state = false +").unwrap(); + assert!(group_line_of(dir.path(), "mind-teases.toml").is_none()); +} diff --git a/crates/mind-body/src/projection/select.rs b/crates/mind-body/src/projection/select.rs index c4635aa..87df73c 100644 --- a/crates/mind-body/src/projection/select.rs +++ b/crates/mind-body/src/projection/select.rs @@ -5,7 +5,7 @@ //! here; reaching into it is the Interpreter's, through its read tools, when wondering leads to reading. //! //! Every packet carries its fixed slots: identity, the hour, her mood as change, a bond line for each person -//! present, and the stimulus. The rest comes from her state's pools. Each candidate is ordered by +//! present, the group state (who has been talking to whom, `group_line`), and the stimulus. The rest comes from her state's pools. Each candidate is ordered by //! `log(score) / T` plus Gumbel noise and included with probability `1 - exp(-score / inclusion_score)`, where //! `score = salience × recency × touch × relevance × unresolved × freshness`; then each pool's cap and the context //! budget bound what is kept. A cap is a maximum, never a target: a pool with fewer candidates than its cap still @@ -814,6 +814,95 @@ fn also_here(named: &[String]) -> Option { Some(line) } +// --------------------------------------------------------------------------------------------- +// The group state: what the room's recent lines say about who is talking to whom. + +/// Whether a sender is a Mind of the swarm (she is one); anyone else is a human. +fn is_mind(inputs: &Inputs, sender: &str) -> bool { + sender == inputs.people.own_sender || inputs.people.minds.iter().any(|mind| mind == sender) +} + +/// The label her bond with a sender gives them, when she has a live one. +fn bond_label(inputs: &Inputs, sender: &str) -> Option { + let person = inputs.people.person_of(sender); + items(inputs.state, &["affect", "socialBonds"]).iter().find(|bond| live(bond) && text(bond, &["object", "id"]).to_lowercase() == person).map(|bond| text(bond, &["object", "label"]).to_owned()).filter(|label| !label.is_empty()) +} + +/// The words a line may call its sender by: the Matrix localpart, the display name, and the bond's label when she has one. +fn names_of(line: &RoomLine, inputs: &Inputs) -> Vec { + [Some(localpart(&line.sender).to_owned()), Some(line.display.trim().to_owned()), bond_label(inputs, &line.sender)].into_iter().flatten().filter(|name| !name.is_empty()).collect() +} + +/// The group-state slot (`select.group_state`): over the room's last `group_lines` lines, who spoke to whom (to her, to +/// someone else present by name, or to the room); when a human last spoke; how many lines in a row are the Minds' alone; +/// how many lines are to her; and whether a human's question to everyone stands open. Code reads names and question marks, +/// not meaning: whom a line is to by the names in it is a cue, and the Interpreter (C3b) will know better. None when the +/// room shows no line. +fn group_line(inputs: &Inputs, tuning: &Tuning) -> Option { + let here = inputs.room.here.as_deref(); + let mut lines: Vec<&RoomLine> = Vec::new(); + for line in inputs.room.messages.iter().chain(&inputs.room.tail) { + if here.is_some_and(|here| line.room != here) || lines.iter().any(|seen| seen.sender == line.sender && seen.at_ms == line.at_ms && seen.text == line.text) { + continue; + } + lines.push(line); + } + lines.sort_by_key(|line| line.at_ms); + if lines.is_empty() { + return None; + } + let to_her = |line: &RoomLine| line.sender != inputs.people.own_sender && (line.to_her || [localpart(inputs.people.own_sender), text(inputs.state, &["publicName"])].iter().any(|name| crate::address::names_whole_word(&line.text, name, false))); + let window = &lines[lines.len().saturating_sub(tuning.select.group_lines)..]; + // A tail line carries no display name: an unbonded sender there goes by the handle in her Matrix id. + let label = |line: &RoomLine| { + if line.sender == inputs.people.own_sender { + "she".to_owned() + } else if line.display.trim().is_empty() && bond_label(inputs, &line.sender).is_none() { + localpart(&line.sender).to_owned() + } else { + name_of(line, inputs) + } + }; + let talk: Vec = window + .iter() + .map(|line| { + let target = if to_her(line) { + "her".to_owned() + } else { + window.iter().filter(|other| other.sender != line.sender).find(|other| names_of(other, inputs).iter().any(|name| crate::address::names_whole_word(&line.text, name, false))).map_or_else(|| "the room".to_owned(), |other| label(other)) + }; + format!("{} to {target}", label(line)) + }) + .collect(); + let mut parts = vec![format!("the room lately: {}", talk.join(", "))]; + let last_human = lines.iter().rev().find(|line| !is_mind(inputs, &line.sender)); + parts.push(match last_human { + Some(line) => format!("a human, {}, last spoke {} min ago", label(line), minutes(inputs.now, line.at_ms)), + None => "no human has spoken here lately".to_owned(), + }); + let minds_in_a_row = lines.iter().rev().take_while(|line| is_mind(inputs, &line.sender)).count(); + if minds_in_a_row >= 2 { + parts.push(format!("the last {minds_in_a_row} lines are the Minds' alone")); + } + let addressed = lines.iter().filter(|line| to_her(line)).count(); + if addressed > 0 { + parts.push(format!("{addressed} {} to her", if addressed == 1 { "line is" } else { "lines are" })); + } + if let Some(line) = last_human + && line.text.trim_end().ends_with('?') + && !to_her(line) + && !window.iter().filter(|other| other.sender != line.sender).any(|other| names_of(other, inputs).iter().any(|name| crate::address::names_whole_word(&line.text, name, false))) + { + parts.push(format!("{}'s question to everyone is open", label(line))); + } + Some(clip(&format!("- {}", parts.join("; ")), tuning.select.group_bytes)) +} + +fn minutes(now: DateTime, at_ms: u64) -> i64 { + let at = i64::try_from(at_ms).ok().and_then(DateTime::::from_timestamp_millis).unwrap_or(now); + (now - at).num_minutes().max(0) +} + // --------------------------------------------------------------------------------------------- // The packet. @@ -858,6 +947,12 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet { used += line.len() + 1; fixed.push(line); } + if tuning.select.group_state + && let Some(line) = group_line(inputs, tuning).filter(|line| used + line.len() < budget) + { + used += line.len() + 1; + fixed.push(line); + } // What the room is touching: the content words of what was said there, and of a follow-up's finding. let touching: BTreeSet = inputs.room.messages.iter().chain(&inputs.room.tail).map(|line| line.text.as_str()).chain(inputs.observation).flat_map(content_words).collect(); diff --git a/crates/mind-body/src/projection/simulate.rs b/crates/mind-body/src/projection/simulate.rs index a2c35b2..74c1ca6 100644 --- a/crates/mind-body/src/projection/simulate.rs +++ b/crates/mind-body/src/projection/simulate.rs @@ -2,13 +2,14 @@ //! one `project` through the Persona stage, and each one's quoted lines join the room as from her Matrix id, so the //! next one hears them. //! -//! Who wakes is the real rule: `address::is_for` over each persona's names, plus ruling -//! `persona-only-wake-humans-in-rooms` (an addressed message, any human message in her rooms, the beat). Another Mind's -//! unaddressed message wakes no one, unless the plan asks for the swarm's old loop (`wake = "all_messages"`), kept as a -//! control. Those woken in one round go in an order drawn from the seed, each against the room as it stands at her turn. +//! Who wakes is the heartbeat's: `heartbeat::wakes` under the plan's `WakePolicy`, over each persona's addressee as the +//! heartbeat's addressing table holds it (her stewarded projects' names included) when the plan names the table. The +//! beat is the heartbeat's too: when it falls, one Persona wakes, the winner of `tempo::pick`. Those woken in one round +//! go in an order drawn from the seed, each against the room as it stands at her turn. //! //! Her state does not move during a run: the Interpreter (C3b) and the commit (C3c) are not built. `state_after_turn` -//! is the seam where they will. +//! is the seam where they will. An explicit addressee (the Interpreter naming whom she speaks to) cannot be simulated +//! until then either: a spoken line addresses another persona only by the names in its text. use std::collections::BTreeMap; use std::fmt::Write as _; @@ -21,24 +22,17 @@ use serde_json::{Value, json}; use super::harness::{Harness, Line, Message, cosine, pending_for, state_file, turn_markdown, usage_markdown, usage_total}; use super::select::Draw; +use super::tuning; use super::{MouthReader, Organs, Request, Through, TurnKind, project}; use crate::address::{Addressee, is_for, localpart}; use crate::chat_client::ChatPending; -use crate::heartbeat::{Addressing, HeartbeatView}; +use crate::heartbeat::{Addressing, DEFAULT_BEAT_S, FileStore, HeartbeatView, Speaker, WakeCause, WakePolicy, wakes}; +use crate::tempo::{Standing, pick}; /// The most personas a simulation holds: the swarm. const CAST_MAX: usize = 8; - -/// Who a message wakes. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize)] -#[serde(rename_all = "snake_case")] -enum Wake { - /// The ruled set: a message for her, any human message in her rooms, the beat. - #[default] - HumansInRooms, - /// The swarm's old loop, as a control: any message in her rooms, a Mind's too. - AllMessages, -} +/// How many transcript lines the report quotes as representative. +const REPRESENTATIVE_LINES: usize = 18; #[derive(Deserialize)] #[serde(deny_unknown_fields)] @@ -48,17 +42,21 @@ struct SimFile { repeats: usize, #[serde(default)] seed: u64, - #[serde(default)] - wake: Wake, - /// Every this many rounds the beat wakes everyone who was not woken otherwise; 0 is no beat. - #[serde(default)] - beat_every: usize, + #[serde(default = "ruled")] + wake: WakePolicy, + /// The heartbeat's beat, in seconds of simulated time; 0 is no beat. + #[serde(default = "beat_s")] + beat_s: u32, #[serde(default = "two")] round_minutes: i64, start: DateTime, rooms: Vec, #[serde(default)] operators: Vec, + /// The heartbeat's store (`heartbeat.cc`), read without a lock or a write, for its addressing table: each persona's + /// names and her stewarded projects' names. None addresses each by her own names. + #[serde(default)] + addressing: Option, cast: Vec, #[serde(default, rename = "message")] script: Vec, @@ -72,6 +70,14 @@ fn two() -> i64 { 2 } +fn ruled() -> WakePolicy { + WakePolicy::Ruled +} + +fn beat_s() -> u32 { + DEFAULT_BEAT_S +} + #[derive(Clone, Deserialize)] #[serde(deny_unknown_fields)] struct CastEntry { @@ -82,15 +88,14 @@ struct CastEntry { /// Her rooms; none is every room of the plan. #[serde(default)] rooms: Vec, - /// More names she answers to, beside her handle and display name. - #[serde(default)] - names: Vec, } struct Member { entry: CastEntry, state: Value, addressee: Addressee, + /// Where her addressee came from: the heartbeat's table, or her own names. + names_from: &'static str, rooms: Vec, } @@ -122,8 +127,33 @@ fn state_after_turn(state: Value, _turn: &Value) -> Value { state } -/// How a Mind's line addressed her, by the evidence `is_for` had: a Matrix tag or a reply to her (`mention or reply`), -/// her Matrix id written as plain text (`id as text`), or one of her names in the text (`name in text`). +fn speaker(line: &Line, member: &Member, operators: &[String], cast: &[String]) -> Speaker { + if line.from == member.entry.id { + Speaker::Her + } else if operators.contains(&line.from) { + Speaker::Operator + } else if cast.contains(&line.from) { + Speaker::Mind + } else { + Speaker::Member + } +} + +/// For each line, how many Mind lines in a row came before it in its room since a human last spoke there. +fn mind_runs(lines: &[Line], cast: &[String]) -> Vec { + let mut run: BTreeMap<&str, usize> = BTreeMap::new(); + lines + .iter() + .map(|line| { + let before = run.get(line.room.as_str()).copied().unwrap_or(0); + run.insert(&line.room, if cast.contains(&line.from) { before + 1 } else { 0 }); + before + }) + .collect() +} + +/// How a Mind's line addressed her, by the evidence `is_for` had, strongest first: a Matrix tag or a reply to her, her +/// Matrix id written as plain text, or one of her names in the text. fn how_addressed(line: &Line, at: usize, member: &Member, lines: &[Line]) -> &'static str { let delivery = line.delivery(at, &member.entry.id, lines); if delivery.mentions.contains(&member.entry.id) || delivery.basis.is_some() { @@ -135,39 +165,45 @@ fn how_addressed(line: &Line, at: usize, member: &Member, lines: &[Line]) -> &'s } } -/// Why she wakes this round, by the plan's rule, and the Minds' lines that addressed her; none leaves her asleep. A -/// human outranks a Mind: a wake a human's line would cause is counted as the human's, so the Mind-addressed wakes are -/// exactly those no human caused. -fn wake_reason(lines: &[Line], seen: usize, member: &Member, cast: &[String], wake: Wake, beat: bool) -> Option<(&'static str, Vec)> { - let (mut human_addressed, mut human, mut mind) = (false, false, false); - let mut by_minds = Vec::new(); +/// One wake: why (the strongest cause among her new lines, under the plan's policy) and, when only Minds' lines caused +/// it, the strongest evidence that addressed her and the lines that did. +struct Woken { + cause: WakeCause, + how: Option<&'static str>, + by: Vec, +} + +fn woken(lines: &[Line], runs: &[usize], seen: usize, member: &Member, operators: &[String], cast: &[String], policy: WakePolicy) -> Option { + let mut found: Vec<(usize, WakeCause)> = Vec::new(); for (at, line) in lines.iter().enumerate().skip(seen) { - if line.from == member.entry.id || !(member.rooms.contains(&line.room) || line.dm) { + if !(member.rooms.contains(&line.room) || line.dm) { continue; } - let is_human = !cast.contains(&line.from); - let addressed = is_for(&member.addressee, &line.delivery(at, &member.entry.id, lines)); - match (is_human, addressed) { - (true, true) => human_addressed = true, - (true, false) => human = true, - (false, true) => by_minds.push(json!({ "from": line.from, "how": how_addressed(line, at, member, lines), "says": line.says })), - (false, false) => mind = true, + let delivery = line.delivery(at, &member.entry.id, lines); + if let Some(cause) = wakes(policy, &member.addressee, &delivery, speaker(line, member, operators, cast), runs[at]) { + found.push((at, cause)); } } - let why = if human_addressed { - "addressed by a human" - } else if human { - "a human in her rooms" - } else if !by_minds.is_empty() { - "addressed by a Mind" - } else if wake == Wake::AllMessages && mind { - "a message in her rooms" - } else if beat { - "the beat" - } else { - return None; - }; - Some((why, if why == "addressed by a Mind" { by_minds } else { Vec::new() })) + let cause = found.iter().map(|(_, cause)| *cause).min()?; + if cause != WakeCause::AddressedByMind { + return Some(Woken { cause, how: None, by: vec![] }); + } + let addressing: Vec = found.iter().filter(|(_, cause)| *cause == WakeCause::AddressedByMind).map(|(at, _)| *at).collect(); + let rank = |how: &str| ["mention or reply", "id as text", "name in text"].iter().position(|one| *one == how).unwrap_or(3); + let how = addressing.iter().map(|at| how_addressed(&lines[*at], *at, member, lines)).min_by_key(|how| rank(how)); + let by = addressing.iter().map(|at| json!({ "from": lines[*at].from, "how": how_addressed(&lines[*at], *at, member, lines), "says": lines[*at].says })).collect(); + Some(Woken { cause, how, by }) +} + +/// The wire spelling of a cause, and of the beat. +fn cause_word(cause: Option) -> &'static str { + match cause { + Some(WakeCause::AddressedByHuman) => "addressed_by_human", + Some(WakeCause::Human) => "human", + Some(WakeCause::AddressedByMind) => "addressed_by_mind", + Some(WakeCause::Mind) => "mind", + None => "beat", + } } /// A seeded shuffle, so the order of those woken together is drawn, and replays. @@ -191,13 +227,32 @@ pub fn simulate(harness: &Harness, plan_path: &Path) -> Result { bail!("a simulation needs rounds, repeats, a room and a positive round_minutes"); } let base = plan_path.parent().unwrap_or(Path::new(".")); + let mut members = cast_of(&plan, base)?; + let group_state = tuning::load(&harness.tuning).map(|loaded| loaded.tuning.select.group_state).ok(); + let runs: Vec = (0..plan.repeats).map(|repeat| run(harness, &plan, &mut members, repeat)).collect::>()?; + Ok(markdown(plan_path, &plan, &members, group_state, &runs)) +} + +/// The plan's cast: each persona's state, checked as a put would check it, and her addressee, from the heartbeat's +/// table when the plan names it and the table holds her; and the script checked against the rooms. +fn cast_of(plan: &SimFile, base: &Path) -> Result> { + let table = match &plan.addressing { + Some(path) => FileStore::new(base.join(path)).addressing().with_context(|| format!("the heartbeat store {} could not be read", path.display()))?.unwrap_or_default(), + None => Addressing::default(), + }; let mut members = Vec::new(); for entry in &plan.cast { let state = state_file(&base.join(&entry.state), localpart(&entry.id))?; let rooms = if entry.rooms.is_empty() { plan.rooms.clone() } else { entry.rooms.clone() }; - let public = state["publicName"].as_str().unwrap_or_default().to_owned(); - let names = [localpart(&entry.id), entry.name.as_str(), public.as_str()].into_iter().chain(entry.names.iter().map(String::as_str)).filter(|name| !name.is_empty()); - members.push(Member { addressee: Addressee::new(&entry.id, names), entry: entry.clone(), state, rooms }); + let (addressee, names_from) = match table.minds.iter().find(|row| row.matrix_id() == entry.id) { + Some(row) => (row.clone(), "the heartbeat's table"), + None => { + let public = state["publicName"].as_str().unwrap_or_default().to_owned(); + let names = [localpart(&entry.id), entry.name.as_str(), public.as_str()].into_iter().filter(|name| !name.is_empty()).map(str::to_owned).collect::>(); + (Addressee::new(&entry.id, names.iter().map(String::as_str)), "her own names") + } + }; + members.push(Member { addressee, names_from, entry: entry.clone(), state, rooms }); } for (at, message) in plan.script.iter().enumerate() { if message.ago.is_some() || message.new || message.tag { @@ -207,8 +262,7 @@ pub fn simulate(harness: &Harness, plan_path: &Path) -> Result { bail!("script message {} is in {}, which is not one of the rooms", at + 1, message.room); } } - let runs: Vec = (0..plan.repeats).map(|repeat| run(harness, &plan, &mut members, repeat)).collect::>()?; - Ok(markdown(plan_path, &plan, &runs)) + Ok(members) } fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) -> Result { @@ -216,32 +270,58 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) let cast: Vec = members.iter().map(|member| member.entry.id.clone()).collect(); let at_round = |round: usize| plan.start + Duration::minutes(plan.round_minutes * round as i64); let mut lines: Vec = Vec::new(); + // Per line: the round, and the cause of the turn that spoke it (none for a script line). let mut rounds_of: Vec = Vec::new(); - let scripted = |round: usize, lines: &mut Vec, rounds_of: &mut Vec| -> Result<()> { + let mut causes: Vec> = Vec::new(); + let scripted = |round: usize, lines: &mut Vec, rounds_of: &mut Vec, causes: &mut Vec>| -> Result<()> { for (at, message) in plan.script.iter().enumerate().filter(|(_, message)| message.round.unwrap_or(0) == round) { lines.push(Line::of(message, at_round(round) + Duration::seconds(at as i64), None, at)?); rounds_of.push(round); + causes.push(None); } Ok(()) }; - scripted(0, &mut lines, &mut rounds_of)?; + scripted(0, &mut lines, &mut rounds_of, &mut causes)?; let opening = lines.len(); let mut seen = vec![0usize; members.len()]; + let mut last_turn: Vec>> = vec![None; members.len()]; + let mut last_beat = plan.start; let mut recent: Vec>> = vec![Vec::new(); members.len()]; let mut turns = Vec::new(); let mut counter = 0u64; for round in 1..=plan.rounds { - scripted(round, &mut lines, &mut rounds_of)?; - let beat = plan.beat_every > 0 && round % plan.beat_every == 0; - let woken: Vec<(usize, (&'static str, Vec))> = members.iter().enumerate().filter_map(|(at, member)| wake_reason(&lines, seen[at], member, &cast, plan.wake, beat).map(|why| (at, why))).collect(); - for (at, (why, addressed_by)) in shuffled(woken, seed ^ (round as u64).wrapping_mul(0x9E37_79B9)) { + scripted(round, &mut lines, &mut rounds_of, &mut causes)?; + let round_at = at_round(round); + let runs = mind_runs(&lines, &cast); + let mut woke: Vec<(usize, Option)> = members.iter().enumerate().filter_map(|(at, member)| woken(&lines, &runs, seen[at], member, &plan.operators, &cast, plan.wake).map(|why| (at, Some(why)))).collect(); + // The beat, as the heartbeat's: when it has fallen, one more wake for the winner of `tempo::pick` among those + // not already woken. + if plan.beat_s > 0 && (round_at - last_beat).num_seconds() >= i64::from(plan.beat_s) { + let standings: Vec = members + .iter() + .enumerate() + .map(|(at, member)| Standing { + slug: localpart(&member.entry.id).to_owned(), + eligible: !woke.iter().any(|(woken, _)| *woken == at), + waited_s: last_turn[at].map(|turn| u64::try_from((round_at - turn).num_seconds()).unwrap_or(0)), + last_turn_ms: last_turn[at].map(|turn| turn.timestamp_millis()), + topic_permille: 0, + addressed_by_member: lines.iter().enumerate().skip(seen[at]).any(|(index, line)| !plan.operators.contains(&line.from) && line.from != member.entry.id && is_for(&member.addressee, &line.delivery(index, &member.entry.id, &lines))), + }) + .collect(); + if let Some(winner) = pick(&standings, plan.beat_s) { + woke.push((winner, None)); + last_beat = round_at; + } + } + for (at, why) in shuffled(woke, seed ^ (round as u64).wrapping_mul(0x9E37_79B9)) { counter += 1; let member = &members[at]; let id = member.entry.id.clone(); let fresh: Vec<&Line> = lines[seen[at]..].iter().filter(|line| line.from != id && (member.rooms.contains(&line.room) || line.dm)).collect(); let here = fresh.iter().rev().find(|line| line.mentions.contains(&id)).or(fresh.last()).map_or_else(|| member.rooms[0].clone(), |line| line.room.clone()); let kind = if fresh.is_empty() { TurnKind::Beat } else { TurnKind::Chat }; - let now = at_round(round) + Duration::seconds(30 + counter as i64); + let now = round_at + Duration::seconds(30 + counter as i64); let request = Request { instance: localpart(&id).to_owned(), kind, @@ -263,24 +343,31 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) let organs = Organs { persona: None, chat: Some(&view), addressing: Some(&names), model: harness.model }; let report = project(&organs, &request, now).unwrap_or_else(|fault| json!({ "error": format!("{fault:#}") })); seen[at] = lines.len(); + last_turn[at] = Some(now); let render = &report["renders"][0]; if let Some(selected) = render["selected"].as_array() { recent[at].push(selected.iter().filter_map(Value::as_str).map(str::to_owned).collect()); } + // A failed call is a failure, never a silence. + let failed = report.get("error").or_else(|| render.get("failed")).or_else(|| render["persona"].get("failed")).map(|fault| fault.as_str().unwrap_or_default().to_owned()); let said: Vec = render["persona"]["would_say"].as_array().map(|said| said.iter().filter_map(Value::as_str).map(str::to_owned).collect()).unwrap_or_default(); let line = (!said.is_empty()).then(|| said.join(" ")); + let cause = cause_word(why.as_ref().map(|why| why.cause)); if let Some(says) = &line { lines.push(Line { from: id.clone(), name: member.entry.name.clone(), room: here.clone(), at_ms: u64::try_from(now.timestamp_millis()).unwrap_or_default(), says: says.clone(), mentions: vec![], dm: false, follow_up: false, reply_to: None, new: false }); rounds_of.push(round); + causes.push(Some(cause)); } - let turn = json!({ "round": round, "persona": member.entry.name, "id": id, "woke": why, "addressed_by": addressed_by, "kind": kind, "room": here, "said": line, "report": report }); + let outcome = if failed.is_some() { "failed" } else if line.is_some() { "spoke" } else { "silent" }; + let (how, by) = why.map_or((None, vec![]), |why| (why.how, why.by)); + let turn = json!({ "round": round, "persona": member.entry.name, "id": id, "woke": cause, "how": how, "addressed_by": by, "kind": kind, "room": here, "outcome": outcome, "failed": failed, "said": line, "report": report }); let member = &mut members[at]; member.state = state_after_turn(std::mem::take(&mut member.state), &turn); turns.push(turn); } } - let metrics = metrics(members, &lines, &rounds_of, opening, &turns, plan.rounds); - let transcript: Vec = lines.iter().zip(&rounds_of).map(|(line, round)| json!({ "round": round, "room": line.room, "from": line.from, "name": line.name, "says": line.says, "mind": cast.contains(&line.from) })).collect(); + let metrics = metrics(members, &lines, &rounds_of, &causes, opening, &turns, plan.rounds); + let transcript: Vec = lines.iter().zip(&rounds_of).zip(&causes).map(|((line, round), cause)| json!({ "round": round, "room": line.room, "from": line.from, "name": line.name, "says": line.says, "mind": cast.contains(&line.from), "woke": cause })).collect(); Ok(json!({ "repeat": repeat + 1, "seed": seed, "transcript": transcript, "turns": turns, "metrics": metrics })) } @@ -311,12 +398,39 @@ fn mean_pairwise(texts: &BTreeMap) -> Option { /// Which rounds a measure keeps. type Keep<'a> = &'a dyn Fn(usize) -> bool; -fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], opening: usize, turns: &[Value], rounds: usize) -> Value { +/// A Mind-to-Mind reply: a Mind's line spoken in a turn another Mind's line woke. +fn mind_reply(cause: Option<&str>) -> bool { + matches!(cause, Some("addressed_by_mind" | "mind")) +} + +/// Per room: Mind-to-Mind replies in a row with no human line and no human-woken answer between them. +fn reply_chains(lines: &[Line], causes: &[Option<&str>], opening: usize, cast: &[&str]) -> Value { + let mut rooms: BTreeMap = BTreeMap::new(); + for (line, cause) in lines.iter().zip(causes).skip(opening) { + let (longest, chains, total, run) = rooms.entry(line.room.clone()).or_default(); + if cast.contains(&line.from.as_str()) && mind_reply(*cause) { + *run += 1; + *total += 1; + *longest = (*longest).max(*run); + } else { + *chains += usize::from(*run >= 3); + *run = 0; + } + } + let out: BTreeMap = rooms + .into_iter() + .map(|(room, (longest, chains, total, run))| (room, json!({ "longest": longest, "chains_of_3_or_more": chains + usize::from(run >= 3), "mind_to_mind_replies": total }))) + .collect(); + json!(out) +} + +fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], causes: &[Option<&str>], opening: usize, turns: &[Value], rounds: usize) -> Value { let cast: Vec<&str> = members.iter().map(|member| member.entry.id.as_str()).collect(); + let outcome = |turn: &&Value, what: &str| turn["outcome"] == what; let mut per = BTreeMap::new(); for member in members { let mine: Vec<&Value> = turns.iter().filter(|turn| turn["id"] == member.entry.id.as_str()).collect(); - let spoke = mine.iter().filter(|turn| !turn["said"].is_null()).count(); + let (spoke, silent, failed) = (mine.iter().filter(|turn| outcome(turn, "spoke")).count(), mine.iter().filter(|turn| outcome(turn, "silent")).count(), mine.iter().filter(|turn| outcome(turn, "failed")).count()); let mut addressed = BTreeMap::new(); for (at, line) in lines.iter().enumerate().skip(opening).filter(|(_, line)| line.from == member.entry.id) { for other in members.iter().filter(|other| other.entry.id != member.entry.id) { @@ -325,22 +439,11 @@ fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], opening: usi } } } - per.insert(member.entry.name.clone(), json!({ "turns": mine.len(), "messages": spoke, "silence_rate": if mine.is_empty() { Value::Null } else { json!(1.0 - spoke as f64 / mine.len() as f64) }, "addressed": addressed })); + let answered = spoke + silent; + per.insert(member.entry.name.clone(), json!({ "turns": mine.len(), "messages": spoke, "failed": failed, "silence_rate": if answered == 0 { Value::Null } else { json!(silent as f64 / answered as f64) }, "addressed": addressed })); } - // Mind-to-Mind chains: runs of Mind lines with no human line between them. - let (mut longest, mut long_chains, mut run) = (0usize, 0usize, 0usize); - for line in &lines[opening..] { - if cast.contains(&line.from.as_str()) { - run += 1; - longest = longest.max(run); - } else { - long_chains += usize::from(run >= 3); - run = 0; - } - } - long_chains += usize::from(run >= 3); let half = rounds.div_ceil(2); - let spoken_by = |keep: &dyn Fn(usize) -> bool| -> BTreeMap { + let spoken_by = |keep: Keep| -> BTreeMap { let mut texts = BTreeMap::new(); for (line, _) in lines.iter().zip(rounds_of).skip(opening).filter(|(line, round)| cast.contains(&line.from.as_str()) && keep(**round)) { let text: &mut String = texts.entry(line.from.clone()).or_default(); @@ -349,7 +452,7 @@ fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], opening: usi } texts }; - let prose_by = |keep: &dyn Fn(usize) -> bool| -> BTreeMap { + let prose_by = |keep: Keep| -> BTreeMap { let mut texts = BTreeMap::new(); for turn in turns.iter().filter(|turn| turn["round"].as_u64().is_some_and(|round| keep(round as usize))) { let text: &mut String = texts.entry(turn["id"].as_str().unwrap_or_default().to_owned()).or_default(); @@ -360,31 +463,34 @@ fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], opening: usi }; let (all, early, late): (Keep, Keep, Keep) = (&|_| true, &|round| round <= half, &|round| round > half); let mind_lines: Vec<&Line> = lines[opening..].iter().filter(|line| cast.contains(&line.from.as_str())).collect(); + const CAUSES: [&str; 5] = ["addressed_by_human", "human", "addressed_by_mind", "mind", "beat"]; + let count_causes = |these: &[&Value]| -> BTreeMap<&str, usize> { CAUSES.iter().map(|cause| (*cause, these.iter().filter(|turn| turn["woke"] == *cause).count())).collect() }; let wakes_by_round: Vec = (1..=rounds) .map(|round| { let these: Vec<&Value> = turns.iter().filter(|turn| turn["round"] == round).collect(); - let count = |whys: &[&str]| these.iter().filter(|turn| whys.contains(&turn["woke"].as_str().unwrap_or_default())).count(); - json!({ "round": round, "human": count(&["addressed by a human", "a human in her rooms"]), "mind_addressed": count(&["addressed by a Mind"]), "mind_unaddressed": count(&["a message in her rooms"]), "beat": count(&["the beat"]) }) + json!({ "round": round, "causes": count_causes(&these) }) }) .collect(); - let mind_addressed: Vec = turns - .iter() - .filter(|turn| turn["woke"] == "addressed by a Mind") - .flat_map(|turn| turn["addressed_by"].as_array().into_iter().flatten().map(move |by| json!({ "round": turn["round"], "persona": turn["persona"], "from": by["from"], "how": by["how"], "says": by["says"] }))) - .collect(); + let every: Vec<&Value> = turns.iter().collect(); + let mind_woken: Vec<&Value> = turns.iter().filter(|turn| turn["woke"] == "addressed_by_mind").collect(); + let by_how = |how: &str| mind_woken.iter().filter(|turn| turn["how"] == how).count(); + let id_as_text: Vec = mind_woken.iter().filter(|turn| turn["how"] == "id as text").map(|turn| json!({ "round": turn["round"], "persona": turn["persona"], "by": turn["addressed_by"] })).collect(); + let (silent, failed) = (turns.iter().filter(|turn| turn["outcome"] == "silent").count(), turns.iter().filter(|turn| turn["outcome"] == "failed").count()); + let answered = turns.len() - failed; let work = mind_lines.iter().filter(|line| work_talk(&line.says)).count(); json!({ "personas": per, "turns": turns.len(), + "wakes": count_causes(&every), + "failed": failed, "mind_messages": mind_lines.len(), - "silence_rate": if turns.is_empty() { Value::Null } else { json!(turns.iter().filter(|turn| turn["said"].is_null()).count() as f64 / turns.len() as f64) }, - "mind_chain_longest": longest, - "mind_chains_of_3_or_more": long_chains, + "silence_rate": if answered == 0 { Value::Null } else { json!(silent as f64 / answered as f64) }, + "reply_chains": reply_chains(lines, causes, opening, &cast), "convergence_lines": { "all": mean_pairwise(&spoken_by(all)), "first_half": mean_pairwise(&spoken_by(early)), "second_half": mean_pairwise(&spoken_by(late)) }, "convergence_prose": { "all": mean_pairwise(&prose_by(all)), "first_half": mean_pairwise(&prose_by(early)), "second_half": mean_pairwise(&prose_by(late)) }, "work_talk_share": if mind_lines.is_empty() { Value::Null } else { json!(work as f64 / mind_lines.len() as f64) }, "wakes_by_round": wakes_by_round, - "mind_addressed": mind_addressed, + "mind_addressed_wakes": { "total": mind_woken.len(), "mention_or_reply": by_how("mention or reply"), "id_as_text": by_how("id as text"), "name_in_text": by_how("name in text"), "id_as_text_wakes": id_as_text }, }) } @@ -397,57 +503,100 @@ fn number(value: &Value) -> String { value.as_f64().map_or_else(|| "n/a".into(), |number| format!("{number:.3}")) } -fn markdown(plan_path: &Path, plan: &SimFile, runs: &[Value]) -> String { +fn policy_words(policy: WakePolicy) -> String { + match policy { + WakePolicy::Live => "live: the operator or another Mind addressing her (the heartbeat's rule today)".into(), + WakePolicy::Ruled => "ruled: a line addressing her, from anyone, and any human line in her rooms".into(), + WakePolicy::HumansPace => "humans_pace: human lines only, each waking every Mind in the room once; a Mind's line wakes no one".into(), + WakePolicy::NamesWithCap(cap) => format!("names_with_cap {cap}: ruled, but a Mind's line past {cap} Mind lines in a row in its room wakes no one until a human speaks"), + WakePolicy::AllMessages => "all_messages: any line in her rooms, a Mind's too (the old loop, the control)".into(), + } +} + +/// `count` lines spread evenly over the transcript, the first and the last included. +fn representative(transcript: &[Value], count: usize) -> Vec<&Value> { + if transcript.len() <= count { + return transcript.iter().collect(); + } + let mut at: Vec = (0..count).map(|index| index * (transcript.len() - 1) / (count - 1)).collect(); + at.dedup(); + at.into_iter().map(|index| &transcript[index]).collect() +} + +fn transcript_line(line: &Value) -> String { + let who = if line["mind"] == true { line["name"].as_str().unwrap_or_default().to_owned() } else { format!("{} (human)", line["name"].as_str().unwrap_or_default()) }; + let woke = line["woke"].as_str().map_or_else(String::new, |cause| format!(" _(woke: {cause})_")); + format!("- r{} {} **{who}**{woke}: {}", line["round"], line["room"].as_str().unwrap_or_default(), one_line(line["says"].as_str().unwrap_or_default())) +} + +fn markdown(plan_path: &Path, plan: &SimFile, members: &[Member], group_state: Option, runs: &[Value]) -> String { let mut out = String::new(); let _ = writeln!(out, "# Swarm simulation\n"); let _ = writeln!(out, "> **State does not evolve during a run.** The Interpreter (C3b) and the commit (C3c) are not built, so every persona starts each turn from the state the plan gave her. `state_after_turn` in `projection/simulate.rs` is the seam where they will move it between rounds. Freshness carries over: each persona's earlier packets in the run count against repeating an item.\n"); + let _ = writeln!(out, "> **No explicit addressee.** Whom a line is to is the Interpreter's to say (C3b); until then a spoken line carries no Matrix mention, so one persona addresses another only through names in the text.\n"); let _ = writeln!(out, "- plan: `{}`", plan_path.display()); - let _ = writeln!(out, "- cast: {}", plan.cast.iter().map(|entry| format!("{} (`{}`, `{}`)", entry.name, entry.id, entry.state.display())).collect::>().join(", ")); + let _ = writeln!(out, "- cast: {}", members.iter().map(|member| format!("{} (`{}`, `{}`, names from {})", member.entry.name, member.entry.id, member.entry.state.display(), member.names_from)).collect::>().join(", ")); let _ = writeln!(out, "- rooms: {}; rounds: {}; repeats: {}; seed: {}; minutes a round: {}", plan.rooms.join(", "), plan.rounds, plan.repeats, plan.seed, plan.round_minutes); - let _ = writeln!(out, "- wake: {}; beat: {}", match plan.wake { Wake::HumansInRooms => "ruled set (addressed, any human message in her rooms, the beat; another Mind's unaddressed message wakes no one)", Wake::AllMessages => "control: any message in her rooms wakes her, a Mind's too (the old loop)" }, if plan.beat_every == 0 { "off".to_owned() } else { format!("every {} rounds", plan.beat_every) }); + let _ = writeln!(out, "- wake: {}; beat: {}", policy_words(plan.wake), if plan.beat_s == 0 { "off".to_owned() } else { format!("every {} s of simulated time, one Persona by `tempo::pick`", plan.beat_s) }); + let _ = writeln!(out, "- group state in the Projector's packet: {}", match group_state { Some(true) => "on", Some(false) => "off", None => "unknown (the tuning did not load)" }); let _ = writeln!(out, "- speech: the double-quoted spans of her prose, joined, posted to the room she woke in; none is silence. A preview of the Interpreter's speech, not its decision"); - let _ = writeln!(out, "- wakes: a human-caused wake is counted as the human's even when a Mind also addressed her; Mind-addressed wakes are split by evidence (Matrix mention or reply, her id written as text, a name in the text). Spoken lines here carry no Matrix mention, so a Mind addresses another only through text -- convergence is the mean pairwise cosine of speakers' word counts (lexical, cheap); work-talk share is a lexical heuristic over a fixed word list, not a classifier"); + let _ = writeln!(out, "- wakes are counted per wake, by its strongest cause: addressed by a human, a human line, addressed by a Mind, a Mind's line (control only), the beat. A Mind-addressed wake's evidence is its strongest: a Matrix mention or reply, her id written as text, a name in the text"); + let _ = writeln!(out, "- silence rate is per woken turn that did not fail; a failed call is counted apart. Reply chains are per room: Mind lines spoken in turns another Mind's line woke, in a row"); + let _ = writeln!(out, "- convergence is the mean pairwise cosine of speakers' word counts (lexical, cheap); work-talk share is a lexical heuristic over a fixed word list, not a classifier"); let _ = writeln!(out, "- read-only: nothing is posted, no state is put, no Mind is asked\n"); for run in runs { let metrics = &run["metrics"]; + let wakes = &metrics["wakes"]; let _ = writeln!(out, "## Repeat {} (seed {})\n", run["repeat"], run["seed"]); let _ = writeln!(out, "### Metrics\n"); - let _ = writeln!(out, "- turns: {}; Mind messages: {}; silence rate: {}", metrics["turns"], metrics["mind_messages"], number(&metrics["silence_rate"])); - let _ = writeln!(out, "- longest Mind-to-Mind chain with no human between: {}; chains of 3 or more: {}", metrics["mind_chain_longest"], metrics["mind_chains_of_3_or_more"]); + let _ = writeln!( + out, + "- wakes: {} (addressed by a human {}, a human line {}, addressed by a Mind {}, a Mind's line {}, the beat {}); failed {}", + metrics["turns"], wakes["addressed_by_human"], wakes["human"], wakes["addressed_by_mind"], wakes["mind"], wakes["beat"], metrics["failed"] + ); + let _ = writeln!(out, "- Mind messages: {}; silence rate per woken turn: {}", metrics["mind_messages"], number(&metrics["silence_rate"])); + if let Some(rooms) = metrics["reply_chains"].as_object() { + for (room, chain) in rooms { + let _ = writeln!(out, "- reply chains in {room}: longest {}, chains of 3 or more {}, Mind-to-Mind replies {}", chain["longest"], chain["chains_of_3_or_more"], chain["mind_to_mind_replies"]); + } + } + let addressed = &metrics["mind_addressed_wakes"]; + let _ = writeln!(out, "- Mind-addressed wakes: {} (Matrix mention or reply {}, her id written as text {}, a name in the text {})", addressed["total"], addressed["mention_or_reply"], addressed["id_as_text"], addressed["name_in_text"]); let _ = writeln!(out, "- convergence of spoken lines: all {}, first half {}, second half {}", number(&metrics["convergence_lines"]["all"]), number(&metrics["convergence_lines"]["first_half"]), number(&metrics["convergence_lines"]["second_half"])); let _ = writeln!(out, "- convergence of prose: all {}, first half {}, second half {}", number(&metrics["convergence_prose"]["all"]), number(&metrics["convergence_prose"]["first_half"]), number(&metrics["convergence_prose"]["second_half"])); let _ = writeln!(out, "- work-talk share of Mind lines (heuristic): {}\n", number(&metrics["work_talk_share"])); - let _ = writeln!(out, "| persona | turns | messages | silence rate | addressed |\n|---|---|---|---|---|"); + let _ = writeln!(out, "| persona | wakes | messages | failed | silence rate | addressed |\n|---|---|---|---|---|---|"); if let Some(personas) = metrics["personas"].as_object() { for (name, row) in personas { - let addressed = row["addressed"].as_object().map(|map| map.iter().map(|(who, count)| format!("{who} {count}")).collect::>().join(", ")).unwrap_or_default(); - let _ = writeln!(out, "| {name} | {} | {} | {} | {addressed} |", row["turns"], row["messages"], number(&row["silence_rate"])); + let named = row["addressed"].as_object().map(|map| map.iter().map(|(who, count)| format!("{who} {count}")).collect::>().join(", ")).unwrap_or_default(); + let _ = writeln!(out, "| {name} | {} | {} | {} | {} | {named} |", row["turns"], row["messages"], row["failed"], number(&row["silence_rate"])); } } - let _ = writeln!(out, "\n### Wakes by round\n\nA wake a human's line would cause is counted as the human's; Mind-addressed wakes are those only a Mind's line caused.\n\n| round | human | Mind-addressed | Mind, unaddressed (control) | beat |\n|---|---|---|---|---|"); + let _ = writeln!(out, "\n### Wakes by round\n\n| round | addressed by a human | a human line | addressed by a Mind | a Mind's line | beat |\n|---|---|---|---|---|---|"); for row in metrics["wakes_by_round"].as_array().into_iter().flatten() { - let _ = writeln!(out, "| {} | {} | {} | {} | {} |", row["round"], row["human"], row["mind_addressed"], row["mind_unaddressed"], row["beat"]); + let causes = &row["causes"]; + let _ = writeln!(out, "| {} | {} | {} | {} | {} | {} |", row["round"], causes["addressed_by_human"], causes["human"], causes["addressed_by_mind"], causes["mind"], causes["beat"]); } - let addressed = metrics["mind_addressed"].as_array().cloned().unwrap_or_default(); - let by_how = |how: &str| addressed.iter().filter(|one| one["how"] == how).count(); - let _ = writeln!(out, "\nMind-addressed wake evidence: {} lines; a Matrix mention or reply {}, her Matrix id written as text {}, a name in the text {}.\n", addressed.len(), by_how("mention or reply"), by_how("id as text"), by_how("name in text")); - let as_text: Vec<&Value> = addressed.iter().filter(|one| one["how"] == "id as text").collect(); + let as_text = addressed["id_as_text_wakes"].as_array().cloned().unwrap_or_default(); if !as_text.is_empty() { - let _ = writeln!(out, "Wakes where the only address was an id written as text (no mention, no reply):\n"); + let _ = writeln!(out, "\nWakes whose only address was an id written as text (no mention, no reply):\n"); for one in as_text { - let _ = writeln!(out, "- r{} {} woken by {}: {}", one["round"], one["persona"].as_str().unwrap_or_default(), one["from"].as_str().unwrap_or_default(), one_line(one["says"].as_str().unwrap_or_default())); + let by = one["by"].as_array().into_iter().flatten().map(|by| format!("{}: {}", by["from"].as_str().unwrap_or_default(), one_line(by["says"].as_str().unwrap_or_default()))).collect::>().join("; "); + let _ = writeln!(out, "- r{} {}: {by}", one["round"], one["persona"].as_str().unwrap_or_default()); } } + let transcript = run["transcript"].as_array().cloned().unwrap_or_default(); + let _ = writeln!(out, "\n### Representative lines\n"); + for line in representative(&transcript, REPRESENTATIVE_LINES) { + let _ = writeln!(out, "{}", transcript_line(line)); + } let _ = writeln!(out, "\n### Transcript\n"); - for line in run["transcript"].as_array().into_iter().flatten() { - let who = if line["mind"] == true { line["name"].as_str().unwrap_or_default().to_owned() } else { format!("{} (human)", line["name"].as_str().unwrap_or_default()) }; - let _ = writeln!(out, "- r{} {} **{who}**: {}", line["round"], line["room"].as_str().unwrap_or_default(), one_line(line["says"].as_str().unwrap_or_default())); + for line in &transcript { + let _ = writeln!(out, "{}", transcript_line(line)); } let _ = writeln!(out, "\n### Turns\n"); for turn in run["turns"].as_array().into_iter().flatten() { - let said = if turn["said"].is_null() { "silent".to_owned() } else { "spoke".to_owned() }; - let _ = writeln!(out, "
round {}, {} (woke: {}, {} turn, {}): {said}\n", turn["round"], turn["persona"].as_str().unwrap_or_default(), turn["woke"].as_str().unwrap_or_default(), turn["kind"].as_str().unwrap_or_default(), turn["room"].as_str().unwrap_or_default()); + let _ = writeln!(out, "
round {}, {} (woke: {}, {} turn, {}): {}\n", turn["round"], turn["persona"].as_str().unwrap_or_default(), turn["woke"].as_str().unwrap_or_default(), turn["kind"].as_str().unwrap_or_default(), turn["room"].as_str().unwrap_or_default(), turn["outcome"].as_str().unwrap_or_default()); let report = &turn["report"]; if let Some(error) = report.get("error") { let _ = writeln!(out, "**failed:** {}\n", error.as_str().unwrap_or_default()); diff --git a/crates/mind-body/src/projection/simulate/tests.rs b/crates/mind-body/src/projection/simulate/tests.rs index eb97908..67b0e2f 100644 --- a/crates/mind-body/src/projection/simulate/tests.rs +++ b/crates/mind-body/src/projection/simulate/tests.rs @@ -4,6 +4,7 @@ use serde_json::Value; use super::*; use crate::projection::harness::tests::{Scripted, control}; +use crate::projection::{Answer, Failed, Model, Spent}; const TOBIAH: &str = "@eureka:gamecult.org"; const LIBBY: &str = "@libby:gamecult.org"; @@ -18,18 +19,21 @@ fn cast(dir: &Path) { std::fs::write(dir.join("libby.json"), libby.to_string()).unwrap(); } +/// A plan over the two, the operator opening; `wake` is the TOML value of the policy; no beat unless `extra` sets one. fn plan(dir: &Path, wake: &str, rounds: usize, extra: &str) -> PathBuf { cast(dir); let path = dir.join("plan.toml"); + let beat = if extra.contains("beat_s") { "" } else { "beat_s = 0\n" }; std::fs::write( &path, format!( r##"rounds = {rounds} seed = 11 -wake = "{wake}" +wake = {wake} start = "2026-11-20T15:00:00Z" rooms = ["#minds:gamecult.org"] operators = ["@meta:gamecult.org"] +{beat}{extra} [[cast]] id = "{TOBIAH}" name = "Tobiah" @@ -43,33 +47,38 @@ from = "@meta:gamecult.org" name = "Meta" room = "#minds:gamecult.org" says = "good afternoon, everyone" -{extra}"## +"## ), ) .unwrap(); path } -/// Both speak every turn, neither naming the other: a Mind line wakes no one under the ruled set. -fn chatty() -> Scripted { - Scripted::new(|prompt| if prompt.contains("Who you are:\nTobiah") { "\"what a day\" he says".into() } else { "\"mm, the deploy went fine\" she says".into() }) +fn is_tobiah(prompt: &str) -> bool { + prompt.contains("Who you are:\nTobiah") } -fn run_plan(path: &Path, model: &Scripted) -> (SimFile, Vec, Value) { +/// Both speak every turn, neither naming the other. +fn chatty() -> Scripted { + Scripted::new(|prompt| if is_tobiah(prompt) { "\"what a day\" he says".into() } else { "\"mm, the deploy went fine\" she says".into() }) +} + +/// Both speak every turn, each naming the other. +fn namers() -> Scripted { + Scripted::new(|prompt| if is_tobiah(prompt) { "\"Libby, tea?\"".into() } else { "\"Tobiah, yes please.\"".into() }) +} + +fn silent() -> Scripted { + Scripted::new(|_| "Nobody needs anything from her.".into()) +} + +fn run_plan(path: &Path, model: &dyn Model) -> (Vec, Value) { let plan: SimFile = toml::from_str(&std::fs::read_to_string(path).unwrap()).unwrap(); let base = path.parent().unwrap(); - let mut members: Vec = plan - .cast - .iter() - .map(|entry| { - let state = state_file(&base.join(&entry.state), localpart(&entry.id)).unwrap(); - let public = state["publicName"].as_str().unwrap().to_owned(); - Member { addressee: Addressee::new(&entry.id, [localpart(&entry.id), entry.name.as_str(), public.as_str()]), entry: entry.clone(), state, rooms: plan.rooms.clone() } - }) - .collect(); + let mut members = cast_of(&plan, base).unwrap(); let harness = Harness { instance: "eureka".into(), persona: None, model: Some(model), tuning: base.join("persona-tuning.toml") }; let run = run(&harness, &plan, &mut members, 0).unwrap(); - (plan, members, run) + (members, run) } fn turns(run: &Value) -> Vec<(u64, String, String)> { @@ -77,68 +86,156 @@ fn turns(run: &Value) -> Vec<(u64, String, String)> { } #[test] -fn under_the_ruled_wake_set_a_minds_unaddressed_line_wakes_no_one() { +fn the_wake_policies_are_the_heartbeats_one_function() { + let libby = Addressee::new(LIBBY, ["libby", "Libby"]); + let line = |from: &str, says: &str| Line { from: from.into(), name: String::new(), room: "#minds:x".into(), at_ms: 0, says: says.into(), mentions: vec![], dm: false, follow_up: false, reply_to: None, new: false }; + let ask = |policy: WakePolicy, from: &str, says: &str, speaker: Speaker, run: usize| wakes(policy, &libby, &line(from, says).delivery(0, LIBBY, &[]), speaker, run); + let (named, unnamed) = ("Libby, tea?", "what a day"); + use WakeCause::*; + // Ruled: anyone addressing her, any human line. + assert_eq!(ask(WakePolicy::Ruled, TOBIAH, named, Speaker::Mind, 0), Some(AddressedByMind)); + assert_eq!(ask(WakePolicy::Ruled, TOBIAH, unnamed, Speaker::Mind, 0), None, "another Mind's unaddressed line wakes no one"); + assert_eq!(ask(WakePolicy::Ruled, "@deru:x", unnamed, Speaker::Member, 0), Some(Human)); + assert_eq!(ask(WakePolicy::Ruled, "@deru:x", named, Speaker::Member, 0), Some(AddressedByHuman)); + // Live, the heartbeat's today: a member's line does not wake her, the operator's or a Mind's address does. + assert_eq!(ask(WakePolicy::Live, "@deru:x", named, Speaker::Member, 0), None); + assert_eq!(ask(WakePolicy::Live, "@meta:x", named, Speaker::Operator, 0), Some(AddressedByHuman)); + assert_eq!(ask(WakePolicy::Live, TOBIAH, named, Speaker::Mind, 0), Some(AddressedByMind)); + assert_eq!(ask(WakePolicy::Live, "@meta:x", unnamed, Speaker::Operator, 0), None); + // Humans pace: a Mind's line never wakes a Mind. + assert_eq!(ask(WakePolicy::HumansPace, TOBIAH, named, Speaker::Mind, 0), None); + assert_eq!(ask(WakePolicy::HumansPace, "@meta:x", unnamed, Speaker::Operator, 0), Some(Human)); + // The cap: a Mind's address wakes her only while the Mind run before the line is under it. + assert_eq!(ask(WakePolicy::NamesWithCap(2), TOBIAH, named, Speaker::Mind, 1), Some(AddressedByMind)); + assert_eq!(ask(WakePolicy::NamesWithCap(2), TOBIAH, named, Speaker::Mind, 2), None); + assert_eq!(ask(WakePolicy::NamesWithCap(2), "@meta:x", unnamed, Speaker::Operator, 9), Some(Human), "a human always may"); + // The control, and her own line never. + assert_eq!(ask(WakePolicy::AllMessages, TOBIAH, unnamed, Speaker::Mind, 0), Some(Mind)); + assert_eq!(ask(WakePolicy::AllMessages, LIBBY, named, Speaker::Her, 0), None); +} + +#[test] +fn under_the_ruled_set_unaddressed_mind_lines_wake_no_one() { let dir = tempfile::tempdir().unwrap(); - let (_, _, run) = run_plan(&plan(dir.path(), "humans_in_rooms", 6, ""), &chatty()); + let (_, run) = run_plan(&plan(dir.path(), "\"ruled\"", 6, ""), &chatty()); let turns = turns(&run); assert_eq!(turns.len(), 2, "the operator's opening wakes both in round 1, and then nothing does: {turns:?}"); - assert!(turns.iter().all(|(round, _, why)| *round == 1 && why == "a human in her rooms"), "{turns:?}"); - let transcript = run["transcript"].as_array().unwrap(); - assert_eq!(transcript.len(), 3); - assert!(transcript.iter().any(|line| line["from"] == TOBIAH && line["says"] == "what a day" && line["mind"] == true), "her quoted words join the room as from her id: {transcript:?}"); + assert!(turns.iter().all(|(round, _, why)| *round == 1 && why == "human"), "{turns:?}"); + assert!(run["transcript"].as_array().unwrap().iter().any(|line| line["from"] == TOBIAH && line["says"] == "what a day" && line["woke"] == "human"), "her quoted words join the room as from her id"); } #[test] -fn the_old_loop_control_lets_minds_wake_each_other_into_a_chain() { +fn the_control_lets_minds_wake_each_other_into_a_reply_chain_counted_per_room() { let dir = tempfile::tempdir().unwrap(); - let (_, _, run) = run_plan(&plan(dir.path(), "all_messages", 6, ""), &chatty()); - let turns = turns(&run); - assert!(turns.len() >= 6, "every round someone answers a Mind: {turns:?}"); - assert!(turns.iter().any(|(_, _, why)| why == "a message in her rooms")); + let (_, run) = run_plan(&plan(dir.path(), "\"all_messages\"", 6, ""), &chatty()); let metrics = &run["metrics"]; - assert!(metrics["mind_chain_longest"].as_u64().unwrap() >= 5, "{metrics}"); - assert!(metrics["mind_chains_of_3_or_more"].as_u64().unwrap() >= 1); - assert_eq!(metrics["silence_rate"].as_f64(), Some(0.0)); + assert!(metrics["wakes"]["mind"].as_u64().unwrap() >= 5, "{metrics}"); + let chain = &metrics["reply_chains"]["#minds:gamecult.org"]; + // The two round-1 answers are to the operator; every later line is a reply to a Mind. + assert_eq!(chain["mind_to_mind_replies"].as_u64(), Some(metrics["mind_messages"].as_u64().unwrap() - 2), "{metrics}"); + assert!(chain["longest"].as_u64().unwrap() >= 5 && chain["chains_of_3_or_more"] == 1, "{chain}"); let work = metrics["work_talk_share"].as_f64().unwrap(); assert!(work > 0.3 && work < 0.7, "Libby's lines name the deploy, Tobiah's do not: {work}"); - assert!(metrics["convergence_lines"]["all"].as_f64().unwrap() < 0.5, "two different lines are not one voice: {metrics}"); } #[test] -fn a_scripted_interjection_wakes_everyone_in_her_room_and_addressing_is_counted() { +fn naming_runs_away_under_the_ruled_set_and_stops_at_the_cap() { + let dir = tempfile::tempdir().unwrap(); + let (_, ruled) = run_plan(&plan(dir.path(), "\"ruled\"", 6, ""), &namers()); + assert!(turns(&ruled).iter().filter(|(_, _, why)| why == "addressed_by_mind").count() >= 5, "{:?}", turns(&ruled)); + assert_eq!(ruled["metrics"]["mind_addressed_wakes"]["name_in_text"], ruled["metrics"]["mind_addressed_wakes"]["total"]); + let dir = tempfile::tempdir().unwrap(); + let (_, capped) = run_plan(&plan(dir.path(), "{ names_with_cap = 2 }", 6, ""), &namers()); + let capped = turns(&capped); + assert!(capped.iter().all(|(round, _, _)| *round <= 2), "after two Mind lines in a row no Mind's address wakes anyone: {capped:?}"); + let dir = tempfile::tempdir().unwrap(); + let (_, paced) = run_plan(&plan(dir.path(), "\"humans_pace\"", 6, ""), &namers()); + assert_eq!(turns(&paced).len(), 2, "under humans_pace only the operator's line wakes anyone"); +} + +#[test] +fn a_human_interjection_wakes_everyone_in_her_room_under_humans_pace() { let dir = tempfile::tempdir().unwrap(); let interjection = "[[message]]\nround = 4\nfrom = \"@deru:gamecult.org\"\nname = \"Deru\"\nroom = \"#minds:gamecult.org\"\nsays = \"kettle news\"\n"; - let model = Scripted::new(|prompt| if prompt.contains("Who you are:\nTobiah") { "\"Libby, tea?\"".into() } else { "She does not answer.".into() }); - let (_, members, run) = run_plan(&plan(dir.path(), "humans_in_rooms", 5, interjection), &model); - let turns = turns(&run); - let fourth: Vec<&String> = turns.iter().filter(|(round, _, _)| *round == 4).map(|(_, who, _)| who).collect(); - assert_eq!(fourth.len(), 2, "Deru's line at round 4 wakes both: {turns:?}"); - assert_eq!(run["metrics"]["personas"]["Tobiah"]["addressed"]["Libby"].as_u64().unwrap(), run["metrics"]["personas"]["Tobiah"]["messages"].as_u64().unwrap()); + let path = plan(dir.path(), "\"humans_pace\"", 5, ""); + std::fs::write(&path, format!("{}{interjection}", std::fs::read_to_string(&path).unwrap())).unwrap(); + let (members, run) = run_plan(&path, &namers()); let rounds = run["metrics"]["wakes_by_round"].as_array().unwrap(); - assert_eq!((rounds[0]["human"].as_u64(), rounds[3]["human"].as_u64()), (Some(2), Some(2)), "the opening and Deru wake both: {rounds:?}"); - let by_minds: u64 = rounds.iter().map(|row| row["mind_addressed"].as_u64().unwrap()).sum(); - assert_eq!(by_minds as usize, run["metrics"]["mind_addressed"].as_array().unwrap().len(), "one line addressed each Mind-addressed wake"); - assert_eq!(run["metrics"]["personas"]["Libby"]["silence_rate"].as_f64(), Some(1.0)); - // The seam: no state moved. - assert_eq!(members[0].state, control()); + assert_eq!((rounds[0]["causes"]["human"].as_u64(), rounds[3]["causes"]["human"].as_u64(), rounds[1]["causes"]["addressed_by_mind"].as_u64()), (Some(2), Some(2), Some(0)), "{rounds:?}"); + assert_eq!(run["metrics"]["personas"]["Tobiah"]["addressed"]["Libby"].as_u64(), run["metrics"]["personas"]["Tobiah"]["messages"].as_u64()); + assert_eq!(members[0].state, control(), "the seam: no state moved"); } #[test] -fn the_same_seed_replays_the_same_order() { +fn the_beat_wakes_one_persona_by_the_heartbeats_pick() { let dir = tempfile::tempdir().unwrap(); - let path = plan(dir.path(), "all_messages", 4, ""); - let (_, _, first) = run_plan(&path, &chatty()); - let (_, _, second) = run_plan(&path, &chatty()); - assert_eq!(turns(&first), turns(&second)); + let (_, run) = run_plan(&plan(dir.path(), "\"ruled\"", 4, "beat_s = 120"), &silent()); + let turns = turns(&run); + for round in 2..=4 { + let beats: Vec<&(u64, String, String)> = turns.iter().filter(|(at, _, _)| *at == round).collect(); + assert_eq!(beats.len(), 1, "one wake a beat: {turns:?}"); + assert_eq!(beats[0].2, "beat"); + } + // The pick takes whoever waited longest: the two alternate. + assert_ne!(turns[2].1, turns[3].1, "{turns:?}"); + assert_eq!(run["metrics"]["silence_rate"].as_f64(), Some(1.0)); +} + +/// A model whose Persona stage fails: a failure, never a silence. +struct FailingPersona; + +impl Model for FailingPersona { + fn ask(&self, _request_id: &str, model: &str, prompt: &str) -> std::result::Result { + if prompt.starts_with("