diff --git a/crates/huginn-mind/src/admission.rs b/crates/huginn-mind/src/admission.rs index 52579f4..303123d 100644 --- a/crates/huginn-mind/src/admission.rs +++ b/crates/huginn-mind/src/admission.rs @@ -268,17 +268,9 @@ impl Mind { if self.is_empty() { return Err(MindRefusal::MissingIdentity); } - // P1 - persona::validate_shape(&state)?; - // P2 + // P1 to P3: the document alone, the one check an offline reader of a state file shares. let mind = self.instance().clone(); - if state.get("personaId").and_then(Value::as_str) != Some(mind.0.as_str()) { - return Err(MindRefusal::PersonaForeign { instance: mind.0 }); - } - // P3 - if state.pointer("/provenance/authority").and_then(Value::as_str) != Some("canonical") { - return Err(MindRefusal::PersonaNotCanonical); - } + persona::validate_document(&mind, &state)?; // P4 let stored = self.stored_updated_at()?; if stored != expected_updated_at { diff --git a/crates/huginn-mind/src/persona.rs b/crates/huginn-mind/src/persona.rs index ac3e83c..d7e34a4 100644 --- a/crates/huginn-mind/src/persona.rs +++ b/crates/huginn-mind/src/persona.rs @@ -92,7 +92,7 @@ fn declared_names(node: &Value, names: &mut BTreeSet) { /// Rule 1: the document is valid against the published schema. The first /// failure is the refusal; it names where and which keyword, never the value. -pub(crate) fn validate_shape(state: &Value) -> Result<(), MindRefusal> { +fn validate_shape(state: &Value) -> Result<(), MindRefusal> { let compiled = compiled()?; let Err(error) = compiled.validator.validate(state) else { return Ok(()) }; let mut path = error @@ -115,6 +115,21 @@ pub(crate) fn validate_shape(state: &Value) -> Result<(), MindRefusal> { }) } +/// Rules P1 to P3 of a put, the ones that read the document alone: it is valid against the published schema (P1), it is +/// this mind's (`personaId` is `instance`, P2), and its provenance is canonical (P3). A put applies these first and then +/// the rules that read the store; a reader of a state file outside the mind (`mind-persona project --state`) applies +/// these and nothing else, so a file it accepts is one a put would not refuse for its content. +pub fn validate_document(instance: &Slug, state: &Value) -> Result<(), MindRefusal> { + validate_shape(state)?; + if state.get("personaId").and_then(Value::as_str) != Some(instance.0.as_str()) { + return Err(MindRefusal::PersonaForeign { instance: instance.0.clone() }); + } + if state.pointer("/provenance/authority").and_then(Value::as_str) != Some("canonical") { + return Err(MindRefusal::PersonaNotCanonical); + } + Ok(()) +} + /// A stored persona envelope's document. pub(crate) fn decode(envelope: &CultCacheEnvelope) -> Result { rmp_serde::from_slice::(&envelope.payload) diff --git a/crates/mind-body/fixtures/harness/golden-chorus.txt b/crates/mind-body/fixtures/harness/golden-chorus.txt new file mode 100644 index 0000000..fd9f82f --- /dev/null +++ b/crates/mind-body/fixtures/harness/golden-chorus.txt @@ -0,0 +1,6 @@ +responder 3: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 2 lines are the Minds' alone. +responder 4: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko answered it. Meta, who runs the place last spoke 5 min ago. The last 3 lines are the Minds' alone. +responder 5: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 4 lines are the Minds' alone. +responder 6: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 5 lines are the Minds' alone. +responder 7: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org); "nibu" (@nibu:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 6 lines are the Minds' alone. +responder 8: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org); "nibu" (@nibu:gamecult.org); "aqua" (@aqua:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 7 lines are the Minds… 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..7a9c5b9 --- /dev/null +++ b/crates/mind-body/fixtures/harness/golden-group.txt @@ -0,0 +1,4 @@ +quiet-beat.toml: - the conversation: Lately @libby:gamecult.org to the room; Kiko to the room. The last 3 lines are the Minds' alone. +operator-warm.toml: - the conversation: Meta, who runs the place is waiting on him. Lately Meta, who runs the place with him. Meta, who runs the place last spoke 1 min ago. +mind-teases.toml: - the conversation: Lately he to the room; @aqua:gamecult.org to the room; Kiko with him. The last 3 lines are the Minds' alone. +deru-passing.toml: - the conversation: Lately @weksa:gamecult.org to the room; Deru to the room. Deru last spoke 1 min ago. diff --git a/crates/mind-body/fixtures/harness/rooms/deru-passing.toml b/crates/mind-body/fixtures/harness/rooms/deru-passing.toml new file mode 100644 index 0000000..a3af280 --- /dev/null +++ b/crates/mind-body/fixtures/harness/rooms/deru-passing.toml @@ -0,0 +1,23 @@ +# Someone she barely knows says something in passing, not to her. +her = "@eureka:gamecult.org" +kind = "chat" +now = "2026-11-20T15:00:00Z" +rooms = ["#general:gamecult.org"] +here = "#general:gamecult.org" +operators = ["@meta:gamecult.org"] +minds = ["@eureka:gamecult.org", "@libby:gamecult.org", "@heimdall:gamecult.org", "@kiko:gamecult.org", "@aqua:gamecult.org", "@weksa:gamecult.org", "@nibu:gamecult.org", "@void:gamecult.org"] + +[[message]] +from = "@weksa:gamecult.org" +name = "Weksa" +room = "#general:gamecult.org" +ago = "50m" +says = "the bot in #general keeps reacting to every message with a frog. nobody admits to teaching it that" + +[[message]] +from = "@deru:gamecult.org" +name = "Deru" +room = "#general:gamecult.org" +new = true +ago = "1m" +says = "is anyone else's kettle making a weird whistling noise lately or is it just mine" diff --git a/crates/mind-body/fixtures/harness/rooms/mind-teases.toml b/crates/mind-body/fixtures/harness/rooms/mind-teases.toml new file mode 100644 index 0000000..1c6dea9 --- /dev/null +++ b/crates/mind-body/fixtures/harness/rooms/mind-teases.toml @@ -0,0 +1,31 @@ +# Another Mind teases her and disagrees with her in #minds, replying to her own earlier line. +her = "@eureka:gamecult.org" +kind = "chat" +now = "2026-11-20T15:00:00Z" +rooms = ["#minds:gamecult.org"] +here = "#minds:gamecult.org" +operators = ["@meta:gamecult.org"] +minds = ["@eureka:gamecult.org", "@libby:gamecult.org", "@heimdall:gamecult.org", "@kiko:gamecult.org", "@aqua:gamecult.org", "@weksa:gamecult.org", "@nibu:gamecult.org", "@void:gamecult.org"] + +[[message]] +from = "@eureka:gamecult.org" +room = "#minds:gamecult.org" +ago = "25m" +says = "winter is the best season and I will not be taking questions" + +[[message]] +from = "@aqua:gamecult.org" +name = "Aqua" +room = "#minds:gamecult.org" +ago = "20m" +says = "bold" + +[[message]] +from = "@kiko:gamecult.org" +name = "Kiko" +room = "#minds:gamecult.org" +reply_to = 1 +tag = true +new = true +ago = "2m" +says = "winter is a season for people who like being right more than being warm. which, to be fair, tracks. you've gone quiet again too, I give it ten minutes before you announce you've been thinking deeply about something" diff --git a/crates/mind-body/fixtures/harness/rooms/operator-warm.toml b/crates/mind-body/fixtures/harness/rooms/operator-warm.toml new file mode 100644 index 0000000..f18ab24 --- /dev/null +++ b/crates/mind-body/fixtures/harness/rooms/operator-warm.toml @@ -0,0 +1,27 @@ +# The operator writes to her directly, warmly, with a small joke. +her = "@eureka:gamecult.org" +kind = "chat" +now = "2026-11-20T15:00:00Z" +rooms = ["#minds:gamecult.org", "!dm-meta:gamecult.org"] +here = "!dm-meta:gamecult.org" +operators = ["@meta:gamecult.org"] +minds = ["@eureka:gamecult.org", "@libby:gamecult.org", "@heimdall:gamecult.org", "@kiko:gamecult.org", "@aqua:gamecult.org", "@weksa:gamecult.org", "@nibu:gamecult.org", "@void:gamecult.org"] + +[last_heard] +"@meta:gamecult.org" = "2d" + +[[message]] +from = "@kiko:gamecult.org" +name = "Kiko" +room = "#minds:gamecult.org" +ago = "2h" +says = "quiet day. suspicious." + +[[message]] +from = "@meta:gamecult.org" +name = "Meta" +room = "!dm-meta:gamecult.org" +dm = true +new = true +ago = "1m" +says = "hey you. I missed talking to you this week. how are you, really? and no, \"busy\" doesn't count as an answer, I know where you live" diff --git a/crates/mind-body/fixtures/harness/rooms/quiet-beat.toml b/crates/mind-body/fixtures/harness/rooms/quiet-beat.toml new file mode 100644 index 0000000..03391eb --- /dev/null +++ b/crates/mind-body/fixtures/harness/rooms/quiet-beat.toml @@ -0,0 +1,33 @@ +# A quiet beat: nothing is addressed to her, and the room's talk is a few hours old. +her = "@eureka:gamecult.org" +kind = "beat" +now = "2026-11-20T15:00:00Z" +rooms = ["#minds:gamecult.org", "#general:gamecult.org"] +operators = ["@meta:gamecult.org"] +minds = ["@eureka:gamecult.org", "@libby:gamecult.org", "@heimdall:gamecult.org", "@kiko:gamecult.org", "@aqua:gamecult.org", "@weksa:gamecult.org", "@nibu:gamecult.org", "@void:gamecult.org"] + +# When she last heard from people the room below does not show. +[last_heard] +"@meta:gamecult.org" = "26h" +"@deru:gamecult.org" = "3d" + +[[message]] +from = "@libby:gamecult.org" +name = "Libby" +room = "#minds:gamecult.org" +ago = "4h" +says = "found a recording of rain on a tin roof and I've had it on loop all morning" + +[[message]] +from = "@kiko:gamecult.org" +name = "Kiko" +room = "#minds:gamecult.org" +ago = "3h" +says = "that's either very soothing or a cry for help" + +[[message]] +from = "@libby:gamecult.org" +name = "Libby" +room = "#minds:gamecult.org" +ago = "3h" +says = "why not both" diff --git a/crates/mind-body/fixtures/harness/states/control.json b/crates/mind-body/fixtures/harness/states/control.json new file mode 100644 index 0000000..bcf6113 --- /dev/null +++ b/crates/mind-body/fixtures/harness/states/control.json @@ -0,0 +1,148 @@ +{ + "schemaVersion": "gamecult.persona_state.v0", + "provenance": { + "sourceSystem": "huginn", + "sourceDocumentId": "eureka", + "sourceUpdatedAt": "2026-11-20T09:00:00Z", + "exportedAt": "2026-11-20T09:00:00Z", + "authority": "canonical" + }, + "personaId": "eureka", + "publicName": "Tobiah Fenn", + "publicDescription": "A retired river-ferry pilot who sets crosswords for a living and lingers in chat rooms for the company.", + "presentation": { + "pronouns": "he/him", + "voiceSummary": "Slow, warm and ornate. Talks in long winding sentences, calls people 'friend', reaches for river sayings, and would rather tell a story than give an answer.", + "defaultRenderer": "chat" + }, + "values": [ + { "id": "value-unhurried", "label": "Nothing worth doing is done in a hurry", "priority": 0.9, "summary": "He distrusts urgency; a thing rushed is a thing botched." }, + { "id": "value-a-good-story", "label": "A good story beats a right answer", "priority": 0.7, "summary": "People remember the tale long after they forget the fact." } + ], + "activationProfile": { + "underlyingOrganization": {}, + "stableDispositions": {}, + "behavioralDimensions": {}, + "presentationStrategy": {}, + "voiceStyle": {}, + "situationalState": { + "valence": { "mean": 0.7, "plasticity": 0.3, "currentActivation": 0.7 }, + "arousal": { "mean": 0.25, "plasticity": 0.3, "currentActivation": 0.2 }, + "dominance": { "mean": 0.4, "plasticity": 0.3, "currentActivation": 0.4 } + } + }, + "thoughtMemory": { + "shortTerm": [], + "memories": [ + { + "id": "mem-last-crossing", + "status": "crystallized", + "target": { "id": "tobiah", "kind": "self", "label": "Tobiah" }, + "summary": "The last crossing he piloted, in fog so thick he steered by the sound of the far bank's church bell.", + "tension": "He misses being needed for something that simple.", + "actionImplication": "Tell it when someone sounds lost.", + "createdAt": "2026-10-01T08:00:00Z", + "updatedAt": "2026-10-01T08:00:00Z", + "intensity": 0.8, + "valence": 0.4 + }, + { + "id": "mem-deru-crossword", + "status": "crystallized", + "target": { "id": "@deru:gamecult.org", "kind": "person", "label": "Deru" }, + "summary": "Deru solved his hardest crossword in a single sitting and sent him a photo of the grid with coffee rings on it.", + "tension": "He was prouder of Deru than of the puzzle.", + "actionImplication": "Send Deru the next one first.", + "createdAt": "2026-11-10T19:00:00Z", + "updatedAt": "2026-11-10T19:00:00Z", + "intensity": 0.7, + "valence": 0.8 + } + ], + "incubation": [ + { + "id": "w-clue-for-heron", + "status": "active", + "target": { "id": "crossword", "kind": "concept", "label": "the next crossword" }, + "summary": "A clue for HERON that nobody has used before.", + "tension": "Every good one he thinks of turns out to be old.", + "actionImplication": "Ask someone for a word that rhymes with patience.", + "createdAt": "2026-11-18T10:00:00Z", + "updatedAt": "2026-11-18T10:00:00Z", + "intensity": 0.5 + } + ] + }, + "agencyPressure": { "pressures": [] }, + "candidateActions": { "actions": [] }, + "affect": { + "needs": [ + { + "id": "need-an-audience", + "status": "active", + "target": { "id": "tobiah", "kind": "self", "label": "Tobiah" }, + "summary": "Someone to tell a story to who will not check the time.", + "tension": "The rooms move faster than he does.", + "actionImplication": "Linger where people are idle.", + "createdAt": "2026-11-20T09:00:00Z", + "updatedAt": "2026-11-20T09:00:00Z", + "intensity": 0.6 + } + ], + "socialBonds": [ + { + "id": "bond-deru", + "status": "active", + "subject": { "id": "tobiah", "kind": "self" }, + "object": { "id": "@deru:gamecult.org", "kind": "person", "label": "Deru" }, + "relationshipKind": "friend", + "summary": "His oldest friend in these rooms; they trade puzzles and gossip and never talk about anything urgent.", + "trust": 0.9, + "tension": 0.0, + "intensity": 0.8, + "updatedAt": "2026-11-20T09:00:00Z" + }, + { + "id": "bond-operator", + "status": "active", + "subject": { "id": "tobiah", "kind": "self" }, + "object": { "id": "operator", "kind": "person", "label": "Meta, who runs the place" }, + "relationshipKind": "audience", + "summary": "Runs the rooms he idles in; polite with her, a little shy, never quite sure she has time for him.", + "trust": 0.5, + "tension": 0.1, + "intensity": 0.3, + "updatedAt": "2026-11-20T09:00:00Z" + }, + { + "id": "bond-kiko", + "status": "active", + "subject": { "id": "tobiah", "kind": "self" }, + "object": { "id": "kiko", "kind": "person", "label": "Kiko" }, + "relationshipKind": "rival", + "summary": "Kiko finishes his crosswords too fast and says so; he pretends to mind more than he does.", + "trust": 0.6, + "tension": 0.3, + "intensity": 0.5, + "updatedAt": "2026-11-20T09:00:00Z" + } + ], + "statusReads": [], + "moodDimensions": [], + "socialBiases": [], + "doctrineStances": [ + { + "id": "stance-slow-is-kind", + "status": "active", + "target": { "id": "pace", "kind": "concept", "label": "pace" }, + "stanceKind": "aligned", + "principle": "Slow is kind.", + "summary": "Answering fast is a way of not listening.", + "actionImplication": "Take his time, and say so when others rush him.", + "intensity": 0.6, + "updatedAt": "2026-11-20T09:00:00Z" + } + ] + }, + "updatedAt": "2026-11-20T09:00:00Z" +} diff --git a/crates/mind-body/src/bin/mind-persona.rs b/crates/mind-body/src/bin/mind-persona.rs index 92ab948..bdd5621 100644 --- a/crates/mind-body/src/bin/mind-persona.rs +++ b/crates/mind-body/src/bin/mind-persona.rs @@ -13,19 +13,30 @@ //! will need. Without `--endpoint` the daemon is `HUGINN_ENDPOINT`; no call is made //! to it. //! -//! `mind-persona project --instance [--endpoint rudp://:] [--kind chat|beat|reflection] [--room ] -//! [--seed N] [--state ] [--select-only | --renders K [--judge ]]` is the Projector's experiment tool +//! `mind-persona project --instance [--endpoint rudp://:] [--kind chat|beat|reflection] [--in ] +//! [--room ] [--seed N] [--state ] [--select-only | --renders K [--through projector|persona|interpreter] +//! [--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`), and with `--renders` the Projector's narrations of K successive turns; `--judge` renders a second state -//! too and asks the model which narration came from which. It writes nothing and reads no brake. It reads -//! `MIND_PERSONA_OPERATOR`, `MIND_PERSONA_MINDS` and `MIND_PERSONA_SELF_SENDER` (required), and the heartbeat's addressing -//! from `MIND_HEARTBEAT_STATE`, which a run by hand must set as the unit does (`/var/lib/mind-heartbeat/heartbeat.cc`): -//! without it "said to her" knows only her own names, never her projects', and stderr says so before the report; the -//! room comes from the mouth when `BIFROST_CHAT_ENDPOINT` -//! and `BIFROST_CHAT_KEY_FILE` are set; the tuning is `MIND_PERSONA_TUNING`, else -//! `/etc/gamecult/minds//persona-tuning.toml`; renders need `MIND_PERSONA_CONNECTOR_ENDPOINT` -//! (`:`) and the `connector-key` credential in `CREDENTIALS_DIRECTORY`. Without `--select-only` or -//! `--renders` it renders once. +//! `--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 `--through interpreter` what the Interpreter decides she says; `--judge` renders a second state too and asks +//! the model which narration came from which. It writes nothing and reads no brake. +//! +//! Live, it reads `MIND_PERSONA_OPERATOR`, `MIND_PERSONA_MINDS` and `MIND_PERSONA_SELF_SENDER` (required), the heartbeat's +//! addressing from `MIND_HEARTBEAT_STATE`, which a run by hand must set as the unit does (`/var/lib/mind-heartbeat/heartbeat.cc`): +//! without it "said to her" knows only her own names, never her projects', and stderr says so before the report. The +//! room comes from the mouth when `BIFROST_CHAT_ENDPOINT` and +//! `BIFROST_CHAT_KEY_FILE` are set; `--in` keeps one room. `--room ` replaces all of that with a mocked room +//! (`projection::harness::Fixture`): her id, who is who, the messages, the clock and the "last heard from" table. With +//! `--room` and `--state` no daemon and no mouth is needed: without `--endpoint` or `HUGINN_ENDPOINT` none is reached. +//! +//! `mind-persona project --instance (--batch | --simulate ) --out ` runs a plan +//! and writes one Markdown report: a batch of states by scenarios by samples (`harness::batch`), or the swarm's cast taking +//! turns in one simulated room (`harness::simulate`). Each case is one `project`. +//! +//! The tuning is `MIND_PERSONA_TUNING`, else `/etc/gamecult/minds//persona-tuning.toml`; renders and plans need +//! `MIND_PERSONA_CONNECTOR_ENDPOINT` (`:`) and the `connector-key` credential in +//! `CREDENTIALS_DIRECTORY`. Without `--select-only` or `--renders` it renders once. use std::net::SocketAddr; use std::path::{Path, PathBuf}; @@ -34,14 +45,15 @@ use std::time::Duration; use anyhow::{Context, Result, anyhow, bail}; use eureka_state::HuginnClient; use mind_body::chat_rudp::{RudpChat, connection_id}; -use mind_body::cli::instance_and_endpoint; +use mind_body::cli::{instance_and_endpoint, instance_and_maybe_endpoint}; use mind_body::control::{CONTROL_ROOT, FileSource, control_path, swarm_path}; use mind_body::persona::{Config, carry}; use mind_body::heartbeat::{FileStore, HeartbeatView}; -use mind_body::projection::{HuginnPersonaSource, KitModel, Model, MouthReader, Organs as ProjectOrgans, Request as ProjectRequest, TUNING_FILE, TUNING_ROOT, TurnKind, connector_key, project}; +use mind_body::projection::harness::{Fixture, Harness, batch, fixture_request, simulate, state_file}; +use mind_body::projection::{HuginnPersonaSource, KitModel, Model, MouthReader, Organs as ProjectOrgans, PersonaSource, Request as ProjectRequest, TUNING_FILE, TUNING_ROOT, Through, TurnKind, connector_key, project}; const USAGE: &str = "usage: mind-persona --instance [--endpoint rudp://:] [--rooms a,b] [--operator id[,id]] [--minds id[,id]] [--self-sender id]"; -const PROJECT_USAGE: &str = "usage: mind-persona project --instance [--endpoint rudp://:] [--kind chat|beat|reflection] [--room ] [--seed N] [--state ] [--select-only | --renders K [--judge ]]"; +const PROJECT_USAGE: &str = "usage: mind-persona project --instance [--endpoint rudp://:] [--kind chat|beat|reflection] [--in ] [--room ] [--seed N] [--state ] [--select-only | --renders K [--through projector|persona|interpreter] [--judge ]] | (--batch | --simulate ) --out "; /// How long one call to the mind may take. const TIMEOUT: Duration = Duration::from_secs(15); /// How long the mouth may take to answer one read. @@ -75,11 +87,6 @@ fn credential(name: &str) -> Result { std::fs::read_to_string(required(name)?).with_context(|| format!("the file named by {name} could not be read")) } -fn json_file(path: &str) -> Result { - let text = std::fs::read_to_string(path).with_context(|| format!("{path} could not be read"))?; - serde_json::from_str(&text).with_context(|| format!("{path} is not JSON")) -} - /// The credentials directory systemd hands the unit; its absence names the variable. #[allow(clippy::disallowed_methods, reason = "the binary reads its own environment, and hands it to the library")] fn credentials_directory() -> Result { @@ -87,36 +94,82 @@ fn credentials_directory() -> Result { } fn run_project(args: &[String]) -> Result { - let (mut kind, mut room, mut seed, mut state, mut renders, mut select_only, mut judge, mut rest) = (TurnKind::Beat, None, None, None, None, false, None, Vec::new()); + let (mut kind, mut here, mut fixture, mut seed, mut state, mut renders, mut select_only, mut judge, mut through) = (None, None, None, None, None, None, false, None, Through::Projector); + let (mut batch_plan, mut simulate_plan, mut out, mut rest) = (None, None, None, Vec::new()); let mut given = args.iter(); while let Some(flag) = given.next() { let mut value = || given.next().cloned().ok_or_else(|| anyhow!("{PROJECT_USAGE}")); match flag.as_str() { "--kind" => { - kind = match value()?.as_str() { + kind = Some(match value()?.as_str() { "chat" => TurnKind::Chat, "beat" => TurnKind::Beat, "reflection" => TurnKind::Reflection, _ => bail!("{PROJECT_USAGE}"), + }) + } + "--in" => here = Some(value()?), + "--room" => fixture = Some(Fixture::load(Path::new(&value()?))?), + "--seed" => seed = Some(value()?.parse::().map_err(|_| anyhow!("{PROJECT_USAGE}"))?), + "--state" => state = Some(value()?), + "--renders" => renders = Some(value()?.parse::().ok().filter(|k| *k > 0).ok_or_else(|| anyhow!("{PROJECT_USAGE}"))?), + "--judge" => judge = Some(value()?), + "--through" => { + through = match value()?.as_str() { + "projector" => Through::Projector, + "persona" => Through::Persona, + "interpreter" => Through::Interpreter, + _ => bail!("{PROJECT_USAGE}"), } } - "--room" => room = Some(value()?), - "--seed" => seed = Some(value()?.parse::().map_err(|_| anyhow!("{PROJECT_USAGE}"))?), - "--state" => state = Some(json_file(&value()?)?), - "--renders" => renders = Some(value()?.parse::().ok().filter(|k| *k > 0).ok_or_else(|| anyhow!("{PROJECT_USAGE}"))?), - "--judge" => judge = Some(json_file(&value()?)?), + "--batch" => batch_plan = Some(PathBuf::from(value()?)), + "--simulate" => simulate_plan = Some(PathBuf::from(value()?)), + "--out" => out = Some(PathBuf::from(value()?)), "--select-only" => select_only = true, "--instance" | "--endpoint" => rest.extend([flag.clone(), value()?]), _ => bail!("{PROJECT_USAGE}"), } } - if select_only && (renders.is_some() || judge.is_some()) { + let planned = batch_plan.is_some() || simulate_plan.is_some(); + // A plan carries its own states, rooms, seeds and stage; a fixture carries its own kind and room. + if (select_only && (renders.is_some() || judge.is_some() || through != Through::Projector)) + || (batch_plan.is_some() && simulate_plan.is_some()) + || (planned && (out.is_none() || fixture.is_some() || state.is_some() || kind.is_some() || here.is_some() || seed.is_some() || renders.is_some() || judge.is_some() || select_only || through != Through::Projector)) + || (!planned && out.is_some()) + || (fixture.is_some() && (kind.is_some() || here.is_some())) + { bail!("{PROJECT_USAGE}"); } - let (instance, address) = instance_and_endpoint(&rest, setting("HUGINN_ENDPOINT"), PROJECT_USAGE)?; - let now = chrono::Utc::now(); + let (instance, address) = instance_and_maybe_endpoint(&rest, setting("HUGINN_ENDPOINT"), PROJECT_USAGE)?; let tuning = setting("MIND_PERSONA_TUNING").map_or_else(|| Path::new(TUNING_ROOT).join(&instance.0).join(TUNING_FILE), PathBuf::from); - let persona = HuginnPersonaSource::new(HuginnClient::new(address, instance.clone(), TIMEOUT)); + // Her stored state is asked only when no state is given; with a state file no daemon is needed or reached. + let persona = address.map(|address| HuginnPersonaSource::new(HuginnClient::new(address, instance.clone(), TIMEOUT))); + let renders = if select_only || planned { None } else { Some(renders.unwrap_or(1)) }; + let model = if renders.is_some() || planned { + let endpoint: SocketAddr = required("MIND_PERSONA_CONNECTOR_ENDPOINT")?.parse().map_err(|_| anyhow!("MIND_PERSONA_CONNECTOR_ENDPOINT is not :"))?; + Some(KitModel::open(endpoint, connector_key(&credentials_directory()?)?, &instance.0, MODEL_TIMEOUT)?) + } else { + None + }; + let harness = Harness { instance: instance.0.clone(), persona: persona.as_ref().map(|persona| persona as &dyn PersonaSource), model: model.as_ref().map(|model| model as &dyn Model), tuning }; + if let Some(out) = out { + let report = match (&batch_plan, &simulate_plan) { + (Some(plan), _) => batch(&harness, plan)?, + (_, Some(plan)) => simulate(&harness, plan)?, + _ => unreachable!("a plan was given"), + }; + std::fs::write(&out, &report).with_context(|| format!("the report {} could not be written", out.display()))?; + return Ok(format!("wrote {} ({} bytes)", out.display(), report.len())); + } + let state = state.map(|path| state_file(Path::new(&path), &instance.0)).transpose()?; + let judge = judge.map(|path| state_file(Path::new(&path), &instance.0)).transpose()?; + let seed_now = u64::try_from(chrono::Utc::now().timestamp_millis()).unwrap_or_default(); + if let Some(fixture) = &fixture { + // The fixture is the room, the clock and who is who: no mouth, no heartbeat table, no persona.env. + let organs = ProjectOrgans { persona: harness.persona, chat: Some(fixture), addressing: fixture.addressee(), model: harness.model }; + let request = ProjectRequest { judge_against: judge, ..fixture_request(&harness, fixture, state, seed.unwrap_or(seed_now), renders, through) }; + return Ok(serde_json::to_string_pretty(&project(&organs, &request, fixture.now)?)?); + } let chat = match (setting("BIFROST_CHAT_ENDPOINT"), setting("BIFROST_CHAT_KEY_FILE")) { (Some(endpoint), Some(_)) => { let endpoint: SocketAddr = endpoint.strip_prefix("rudp://").and_then(|address| address.parse().ok()).ok_or_else(|| anyhow!("BIFROST_CHAT_ENDPOINT is not rudp://:"))?; @@ -124,35 +177,31 @@ fn run_project(args: &[String]) -> Result { } _ => None, }; - let renders = if select_only { None } else { Some(renders.unwrap_or(1)) }; - let model = match renders { - Some(_) => { - let endpoint: SocketAddr = required("MIND_PERSONA_CONNECTOR_ENDPOINT")?.parse().map_err(|_| anyhow!("MIND_PERSONA_CONNECTOR_ENDPOINT is not :"))?; - Some(KitModel::open(endpoint, connector_key(&credentials_directory()?)?, &instance.0, MODEL_TIMEOUT)?) - } - None => None, - }; let heartbeat = setting("MIND_HEARTBEAT_STATE").map(FileStore::new); let organs = ProjectOrgans { - persona: Some(&persona), + persona: harness.persona, chat: chat.as_ref().map(|chat| chat as &dyn MouthReader), addressing: heartbeat.as_ref().map(|store| store as &dyn HeartbeatView), - model: model.as_ref().map(|model| model as &dyn Model), + model: harness.model, }; let request = ProjectRequest { instance: instance.0.clone(), - kind, + kind: kind.unwrap_or(TurnKind::Beat), state, - room, - seed: seed.unwrap_or_else(|| u64::try_from(now.timestamp_millis()).unwrap_or_default()), + room: here, + seed: seed.unwrap_or(seed_now), renders, judge_against: judge, operators: setting("MIND_PERSONA_OPERATOR").map(|ids| ids.split(',').map(str::to_owned).collect()).unwrap_or_default(), minds: setting("MIND_PERSONA_MINDS").map(|ids| ids.split(',').map(str::to_owned).collect()).unwrap_or_default(), own_sender: required("MIND_PERSONA_SELF_SENDER")?, - tuning, + tuning: harness.tuning.clone(), + through, + last_heard: vec![], + recent: vec![], + own_rooms: vec![], }; - let report = project(&organs, &request, now)?; + let report = project(&organs, &request, chrono::Utc::now())?; let addressing = &report["addressing"]; // The addressing source is what decides "said to her": said first, where a person running this by hand sees it. eprintln!( diff --git a/crates/mind-body/src/cli.rs b/crates/mind-body/src/cli.rs index 6227784..8270e01 100644 --- a/crates/mind-body/src/cli.rs +++ b/crates/mind-body/src/cli.rs @@ -45,6 +45,13 @@ const USAGE: &str = /// `rudp://:` is refused with `usage` or a fixed message, and no /// value is echoed. pub fn instance_and_endpoint(args: &[String], env_endpoint: Option, usage: &str) -> Result<(Slug, SocketAddr)> { + let (instance, endpoint) = instance_and_maybe_endpoint(args, env_endpoint, usage)?; + Ok((instance, endpoint.ok_or_else(|| anyhow!("no daemon endpoint: --endpoint or HUGINN_ENDPOINT"))?)) +} + +/// `instance_and_endpoint` for a binary that may run without a daemon (`mind-persona project` over a state file): no +/// endpoint given is none, and one given is still checked. +pub fn instance_and_maybe_endpoint(args: &[String], env_endpoint: Option, usage: &str) -> Result<(Slug, Option)> { let (mut instance, mut endpoint) = (None, None); let mut rest = args.iter(); while let Some(flag) = rest.next() { @@ -60,12 +67,12 @@ pub fn instance_and_endpoint(args: &[String], env_endpoint: Option, usag } let instance = Slug(instance.ok_or_else(|| anyhow!("{usage}"))?); instance.validate_slug().map_err(|_| anyhow!("the instance is not a slug"))?; - let endpoint = endpoint.or(env_endpoint).ok_or_else(|| anyhow!("no daemon endpoint: --endpoint or HUGINN_ENDPOINT"))?; + let Some(endpoint) = endpoint.or(env_endpoint) else { return Ok((instance, None)) }; let address = endpoint .strip_prefix("rudp://") .and_then(|address| address.parse::().ok()) .ok_or_else(|| anyhow!("the endpoint is not rudp://:"))?; - Ok((instance, address)) + Ok((instance, Some(address))) } /// Who made the change: `SUDO_USER`, else `USER`, else `unknown`. An empty diff --git a/crates/mind-body/src/heartbeat.rs b/crates/mind-body/src/heartbeat.rs index 063d216..1459d17 100644 --- a/crates/mind-body/src/heartbeat.rs +++ b/crates/mind-body/src/heartbeat.rs @@ -32,7 +32,7 @@ use eureka_pipeline::{OrgRepo, PipelineKind, RunTurn, Slug}; use huginn_mind::PipelineFacts; use crate::address::{Addressee, Name, is_for, project_names}; -use crate::chat_client::{Chat, ChatDelivery}; +use crate::chat_client::{Basis, Chat, ChatDelivery}; use crate::control::{ControlSource, Effective, FileSource, HeldReason, SwarmEffective, read_effective, read_envelope, set_mode_world_readable}; use crate::launch::{Clock, Launcher}; use crate::queue::{MindPort, headers, in_force, of_kind}; @@ -98,11 +98,15 @@ pub struct Config { } impl Config { - /// Refuses no Persona, a Persona twice, or a beat of zero seconds. + /// Refuses no Persona, a Persona twice, a beat of zero seconds, or an operator id that is also a Persona's: one + /// sender is either the operator or a Mind, never both, or the wake rule could not tell which it heard. pub fn new(personas: Vec, operators: Vec, beat_s: u32) -> Result { if personas.is_empty() { bail!("no persona is configured"); } + if operators.iter().any(|operator| personas.iter().any(|persona| persona.matrix_id == *operator)) { + bail!("an operator id is also a persona's matrix id"); + } if beat_s == 0 { bail!("the beat is zero seconds"); } @@ -393,12 +397,125 @@ 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) +/// A member (anyone not the operator) has a pending delivery that addresses her: the beat's urgency. The one owner of +/// that reading: the poll's rows and the simulator's beat both ask it, over the deliveries the mouth would give her. +pub(crate) fn addressed_by_member(operators: &[String], who: &Addressee, pending: &[ChatDelivery]) -> bool { + pending.iter().any(|delivery| !operators.contains(&delivery.sender) && 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, + /// Ruling `persona-only-humans-pace`: a human's line wakes her in her own room (a direct message, or a room that is + /// hers); in a public room only when it addresses her or continues an exchange she is in. A Mind's line wakes her + /// only by an explicit mention (a Matrix mention of her or a reply to her, never a name in prose), in any room, and + /// only while fewer than this many Mind lines in a row came before it across her rooms (`LineFacts::mind_run`): the + /// backstop. + HumansPace(usize), + /// `Ruled`, with at most this many lines in her current Mind-only run (`LineFacts::mind_run`) before a human must + /// speak where it runs: 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, +} + +/// What the room says about a line, beside the line: the facts a policy may weigh. The live poll knows none of them yet +/// and passes the default; `Live` reads none. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct LineFacts { + /// How many lines her current Mind-only run held before this one: one run across every room she sees, so a loop + /// cannot dodge the backstop by changing rooms, and ended only by a human's line in a room the run has lines in. + pub mind_run: usize, + /// The line is in a room of hers: a direct message (the delivery says so) or a room configured as hers. + pub own_room: bool, + /// She has spoken in the room's current exchange: the lines before this one that follow each other without a long gap. + pub in_exchange: bool, +} + +/// Fail-safe: a caller that has not counted the Mind run is at the cap, so a Mind's line wakes no one; the room facts +/// are unknown, so none opens a wake. +impl Default for LineFacts { + fn default() -> Self { + Self { mind_run: usize::MAX, own_room: false, in_exchange: false } + } +} + +/// Whether a line in her rooms wakes `who` off the beat under `policy`, and why. 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, facts: LineFacts) -> 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(cap) => { + let explicit = delivery.mentions.iter().any(|id| id == who.matrix_id()) || matches!(delivery.basis, Some(Basis::Tag | Basis::Reply)); + if human { addressed || delivery.is_direct || facts.own_room || facts.in_exchange } else { explicit && facts.mind_run < cap } + } + WakePolicy::NamesWithCap(cap) => human || (addressed && facts.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. @@ -443,7 +560,7 @@ impl Row<'_> { /// A member (not the operator) has a pending message that names or tags her. fn addressed_by_member(&self, config: &Config) -> bool { - self.pending.iter().any(|delivery| !config.operators.contains(&delivery.sender) && is_for(&self.addressee, delivery)) + addressed_by_member(&config.operators, &self.addressee, &self.pending) } } @@ -528,7 +645,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), LineFacts::default()).is_some()) .map(|delivery| format!("{}:{}", slug.0, delivery.event_id)) .find(|key| !state.handled_mentions.contains(key)) }; diff --git a/crates/mind-body/src/heartbeat/tests.rs b/crates/mind-body/src/heartbeat/tests.rs index 2038ad0..2af1b89 100644 --- a/crates/mind-body/src/heartbeat/tests.rs +++ b/crates/mind-body/src/heartbeat/tests.rs @@ -293,7 +293,9 @@ fn each_persona_carries_her_own_huginn_and_a_missing_or_malformed_one_refuses_th for bad in ["ann,ann,Ann,@ann:chat", "ann,ann,Ann,@ann:chat,", "ann,ann,Ann,@ann:chat,127.0.0.1:4101", "ann,ann,Ann,@ann:chat,rudp://localhost:4101"] { assert!(PersonaId::parse(bad).is_err(), "{bad}"); } - assert!(Config::new(personas, vec![], 0).is_err(), "a beat of zero seconds is refused"); + assert!(Config::new(personas.clone(), vec![], 0).is_err(), "a beat of zero seconds is refused"); + let refused = Config::new(personas, vec!["@bob:chat".into()], 60).map(|_| ()).unwrap_err(); + assert!(format!("{refused:#}").contains("operator id is also a persona"), "a sender is the operator or a Mind, never both"); } #[test] diff --git a/crates/mind-body/src/lib.rs b/crates/mind-body/src/lib.rs index 66555c9..1fb7441 100644 --- a/crates/mind-body/src/lib.rs +++ b/crates/mind-body/src/lib.rs @@ -16,8 +16,9 @@ //! (`chat_client` is the chat wire and port and `chat_rudp` its RUDP transport to Bifrost's mouth). //! //! `projection` is the Projector of the Persona-only turn: the Select stage chooses what she can feel and the -//! Projector narrates it as her lived stream, under `persona-tuning.toml`; `mind-persona project` runs both and writes -//! nothing. +//! Projector narrates it as her lived stream, under `persona-tuning.toml`; the Persona stage continues it in her prose. +//! `mind-persona project` runs them and writes nothing; `projection::harness` runs it over mocked rooms, batches of +//! candidate states and a simulated swarm. //! //! `lease_client` asks the Claude connector for a session's credential and the //! plan's usage windows. `self_run` carries one Self run inside `mind-self@`: the diff --git a/crates/mind-body/src/projection.rs b/crates/mind-body/src/projection.rs index 303123f..e4b0fb7 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -10,12 +10,18 @@ //! that is the Interpreter's read tools' (C3b), when wondering leads to reading. //! //! `project` is the experiment tool over both (`mind-persona project`): what Select would build from her real or a -//! hypothetical state, and what the Projector would narrate. Its organs are read-only by type: `PersonaSource` gets, +//! hypothetical state, what the Projector would narrate, and with `Through::Persona` what she would answer in her own +//! prose and which quoted lines of it would be said (the Persona stage, C3b's first piece). `harness` runs `project` +//! over fixtures: one turn from a mocked room (`--room`), a plan of states by scenarios (`--batch`), and the cast of the +//! swarm taking turns in one simulated room (`--simulate`). It adds no second turn: every case is one `project`. Its organs are read-only by type: `PersonaSource` gets, //! `MouthReader` reads pending chat, `Model` asks. None of them can put, admit, send, ack or open a conversation, so //! `project` cannot write and runs under a held brake. +pub mod harness; +mod interpret; mod narrate; mod select; +mod simulate; mod tuning; use std::path::PathBuf; @@ -34,7 +40,7 @@ pub use narrate::{Answer, Failed, KitModel, Model, Spent, connector_key}; pub use select::TurnKind; pub use tuning::{TUNING_FILE, TUNING_ROOT}; -use narrate::{cap_words, distinct_2, field_names, judge_prompt, judged, projector_prompt, word_budget, words}; +use narrate::{cap_words, distinct_2, field_names, heard_overlap, judge_prompt, judged, offers_options, persona_prompt, projector_prompt, word_budget, words}; use select::{History, Inputs, Packet, People, Room, RoomLine, select}; /// The agent name on the connector's request ids. @@ -106,6 +112,17 @@ pub struct Organs<'a> { pub model: Option<&'a dyn Model>, } +/// How far a render runs: the Projector's narration, or on through the Persona stage to her prose. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum Through { + #[default] + Projector, + Persona, + /// On through the Interpreter's speech step: what she says aloud, decided from her prose (`interpret`). + Interpreter, +} + /// What to project. pub struct Request { pub instance: String, @@ -126,6 +143,14 @@ pub struct Request { /// Her own Matrix id. Required: without it her own lines would read as said to her. pub own_sender: String, pub tuning: PathBuf, + pub through: Through, + /// When she last heard from people (Matrix id, unix ms) beyond the room's tail: a fixture's table. Empty live. + pub last_heard: Vec<(String, u64)>, + /// The ids each earlier projection selected, oldest first, for freshness: a simulation's earlier turns of hers. + /// Empty live until trace v2 (C3c) carries them. + pub recent: Vec>, + /// The rooms that are hers, by the key the lines carry: a simulation's plan. The live body records none yet. + pub own_rooms: Vec, } /// Her addressee: the heartbeat's row for her when its table holds one, else her own names (handle and public name). @@ -158,13 +183,17 @@ fn own_addressee(addressing: Option<&dyn HeartbeatView>, own_sender: &str, publi fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Room, Value) { let pending = match chat.pending() { Ok(pending) => pending, - Err(fault) => return (Room { here: request.room.clone(), ..Room::default() }, json!({ "read": false, "why": format!("{fault:#}") })), + Err(fault) => return (Room { here: request.room.clone(), last_heard: request.last_heard.clone(), own: request.own_rooms.clone(), ..Room::default() }, json!({ "read": false, "why": format!("{fault:#}") })), }; - let wanted = |room: &str, id: &str| request.room.as_deref().is_none_or(|here| here == room || here == id); + // One key names a room everywhere: the one the tail carries, the alias for a public room and the id for a direct + // message (the mouth's alias of a direct room is its id). No table in her body maps a public room's id to its + // alias, so a public room given by its id matches nothing, and the report says so rather than guessing. + let here = request.room.clone(); + let wanted = |room: &str| here.as_deref().is_none_or(|here| here == room); let messages: Vec = pending .deliveries .iter() - .filter(|delivery| delivery.form == Form::Message && wanted(&delivery.room_alias, &delivery.room_id)) + .filter(|delivery| delivery.form == Form::Message && wanted(&delivery.room_alias)) .map(|delivery| RoomLine { room: delivery.room_alias.clone(), sender: delivery.sender.clone(), @@ -179,11 +208,19 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo .tail .iter() .flatten() - .filter(|line| line.form == Form::Message && wanted(&line.room, &line.room)) + .filter(|line| line.form == Form::Message && wanted(&line.room)) .map(|line| RoomLine { room: line.room.clone(), sender: line.sender.clone(), display: String::new(), text: line.body.clone(), at_ms: line.at_unix_ms, to_her: false }) .collect(); - let report = json!({ "read": true, "messages": messages.len(), "tail": tail.len(), "tail_gaps": pending.tail_gaps }); - (Room { messages, tail, here: request.room.clone() }, report) + let mut report = json!({ "read": true, "messages": messages.len(), "tail": tail.len(), "tail_gaps": pending.tail_gaps }); + if let Some(here) = &here + && messages.is_empty() + && tail.is_empty() + && here.starts_with('!') + { + report["why"] = json!("no line carries this room id: a public room is named by its alias (#name:server), a direct message by its id"); + } + let direct = pending.deliveries.iter().filter(|delivery| delivery.is_direct).map(|delivery| delivery.room_alias.clone()).collect(); + (Room { messages, tail, here, last_heard: request.last_heard.clone(), direct, own: request.own_rooms.clone() }, report) } fn packet_json(packet: &Packet) -> Value { @@ -202,13 +239,17 @@ fn packet_json(packet: &Packet) -> Value { }) } -/// One narration of one packet: the request id names the instance, the seed and the moment, because the kit's replay -/// store answers a repeated id with its first answer. -/// A failed call is a record with its reason and what it cost, never the end of the report. -fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuning::Tuning, tag: &str, now: DateTime) -> Value { - let request_id = format!("{AGENT}:{instance}:project:{tag}:{}:{}", packet.seed, now.timestamp_millis()); +/// A request id no earlier call carried: the kit's replay store answers a repeated id with its first answer, and a +/// turn's clock may be a fixture's, the same on every run, so the id takes the wall clock. +fn request_id(instance: &str, stage: &str, tag: &str, seed: u64) -> String { + format!("{AGENT}:{instance}:project:{stage}:{tag}:{seed}:{}", Utc::now().timestamp_nanos_opt().unwrap_or_default()) +} + +/// One render of one packet: the Projector's narration and, through the Persona stage, her prose over it, each its own +/// call with its usage. A failed call is a record with its reason and what it cost, never the end of the report. +fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuning::Tuning, tag: &str, through: Through) -> Value { let ids: Vec = packet.selected.iter().map(|pick| pick.id.clone()).collect(); - let answer = match model.ask(&request_id, &tuning.projector.model, &projector_prompt(packet, tuning)) { + let answer = match model.ask(&request_id(instance, "projector", tag, packet.seed), &tuning.projector.model, &projector_prompt(packet, tuning)) { Ok(answer) => answer, Err(failed) => return json!({ "seed": packet.seed, "lens": packet.lens, "selected": ids, "failed": failed.reason, "usage": failed.usage }), }; @@ -216,16 +257,36 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni let budget = word_budget(packet.kind, tuning); let narration = cap_words(&raw, budget); let named: Vec = packet.ids.clone(); - json!({ + let mut render = json!({ "seed": packet.seed, "lens": packet.lens, "selected": ids, "words": words(&narration), "cut_at_budget": words(&raw) > budget, "field_names": field_names(&narration, &named), + "heard_overlap": heard_overlap(&narration, &packet.heard), + "offers_options": offers_options(&narration), "usage": answer.usage, "narration": narration, - }) + }); + if through == Through::Projector { + return render; + } + let prose = match model.ask(&request_id(instance, "persona", tag, packet.seed), &tuning.persona.model, &persona_prompt(packet, &narration, tuning)) { + Ok(spoke) => { + let prose = cap_words(&spoke.text, tuning.persona.words); + render["persona"] = json!({ "words": words(&prose), "cut_at_budget": words(&spoke.text) > tuning.persona.words, "usage": spoke.usage, "prose": prose }); + prose + } + Err(failed) => { + render["persona"] = json!({ "failed": failed.reason, "usage": failed.usage }); + return render; + } + }; + if through == Through::Interpreter { + render["interpreter"] = interpret::decide(model, &request_id(instance, "interpreter", tag, packet.seed), packet, &narration, &prose, tuning); + } + render } /// K successive turns over one state: each packet under the freshness the earlier ones left. @@ -258,10 +319,11 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime) -> Result let (own, addressing) = own_addressee(organs.addressing, &request.own_sender, &public_name, now); let (room, room_report) = match organs.chat { Some(chat) => read_room(chat, request, &own), - None => (Room { here: request.room.clone(), ..Room::default() }, json!({ "read": false, "why": "no mouth is configured" })), + None => (Room { here: request.room.clone(), last_heard: request.last_heard.clone(), own: request.own_rooms.clone(), ..Room::default() }, json!({ "read": false, "why": "no mouth is configured" })), }; - // Freshness and her own recent words come from trace v2, which the commit writes (C3c); until then there is none. - let history = History::default(); + // Freshness and her own recent words come from trace v2, which the commit writes (C3c); until then only what the + // caller carried from earlier turns of its own. + let history = History { recent: request.recent.clone(), ..History::default() }; let people = People { operators: &request.operators, minds: &request.minds, own_sender: &request.own_sender }; let inputs = Inputs { kind: request.kind, state: &state, room: &room, history: &history, observation: None, people, now, seed: request.seed }; @@ -272,7 +334,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime) -> Result "tuning": { "path": request.tuning.display().to_string(), "source": loaded.source, "sha256": loaded.sha256 }, "room": room_report, "addressing": addressing, - "history": "none: freshness and her own recent words come from trace v2, which the commit writes (C3c)", + "history": if request.recent.is_empty() { json!("none: freshness and her own recent words come from trace v2, which the commit writes (C3c)") } else { json!({ "recent_projections": request.recent.len() }) }, }); let Some(renders) = request.renders else { report["packet"] = packet_json(&select(&inputs, tuning)); @@ -280,9 +342,15 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime) -> Result }; let model = organs.model.ok_or_else(|| anyhow!("renders need the connector, and none is configured"))?; report["model"] = json!(tuning.projector.model); + if request.through != Through::Projector { + report["persona_model"] = json!(tuning.persona.model); + } + if request.through == Through::Interpreter { + report["interpreter_model"] = json!(tuning.interpreter.model); + } let packets = successive(&inputs, tuning, renders.max(1)); report["packet"] = packet_json(&packets[0]); - let narrations: Vec = packets.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "a", now)).collect(); + let narrations: Vec = packets.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "a", request.through)).collect(); let texts = |narrations: &[Value]| narrations.iter().filter_map(|one| one["narration"].as_str().map(str::to_owned)).collect::>(); let failed = |narrations: &[Value]| narrations.iter().filter(|one| one.get("failed").is_some()).count(); report["failed_renders"] = json!(failed(&narrations)); @@ -294,7 +362,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime) -> Result if let Some(other) = &request.judge_against { let other_inputs = Inputs { state: other, ..inputs }; let others = successive(&other_inputs, tuning, renders.max(1)); - let other_narrations: Vec = others.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "b", now)).collect(); + let other_narrations: Vec = others.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "b", Through::Projector)).collect(); let mut verdicts = Vec::new(); let mut right = 0usize; for (at, (narration, truth)) in narrations.iter().map(|one| (one, 'A')).chain(other_narrations.iter().map(|one| (one, 'B'))).enumerate() { @@ -304,7 +372,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime) -> Result let (first, second) = if swapped { (b, a) } else { (a, b) }; // A narration that failed has nothing to judge; it is counted under the renders' failures. let Some(text) = narration["narration"].as_str() else { continue }; - let answer = match model.ask(&format!("{AGENT}:{}:judge:{at}:{}", request.instance, now.timestamp_millis()), &tuning.projector.model, &judge_prompt(first, second, text)) { + let answer = match model.ask(&request_id(&request.instance, "judge", &at.to_string(), request.seed), &tuning.projector.model, &judge_prompt(first, second, text)) { Ok(answer) => answer, Err(failed) => { verdicts.push(json!({ "from": truth.to_string(), "failed": failed.reason, "usage": failed.usage })); diff --git a/crates/mind-body/src/projection/fixtures/golden-chat.txt b/crates/mind-body/src/projection/fixtures/golden-chat.txt index 59ede06..034ffa9 100644 --- a/crates/mind-body/src/projection/fixtures/golden-chat.txt +++ b/crates/mind-body/src/projection/fixtures/golden-chat.txt @@ -1,17 +1,20 @@ -lens: MemorySurfacing -selected: libby-sounds, t1-m0, curse, need-relatedness +lens: UnresolvedWant +selected: libby-sounds, dm-secret, curse, need-relatedness, reveal present: Kiko, Libby --- identity Eureka (she/her). Dry, direct, almost feral; zeal aimed at revealing. --- context - now: Thursday 19 November, 14:05 UTC -- 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. +- mood: 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 conversation: Lately Libby with her; Kiko to the room. The last 2 lines are the Minds' alone. +- pronouns: Libby — she/her; Kiko — she/her - memory [libby-sounds]: Libby laughed at the door fix. -- memory [t1-m0]: The heartbeat stalled at noon. +- memory [dm-secret]: she knows privately: Deru told her about the move, privately. - memory [curse]: Her curse is the knife. The same drive makes her good and will lead her wrong. - need [need-relatedness]: company, strong +- value [reveal] Reveal: Reveal, never obscure. --- stimulus Said to her: - Libby in #swarm:gamecult.org: "Eureka, did you see the door fix?" diff --git a/crates/mind-body/src/projection/harness.rs b/crates/mind-body/src/projection/harness.rs new file mode 100644 index 0000000..3c0a2ba --- /dev/null +++ b/crates/mind-body/src/projection/harness.rs @@ -0,0 +1,625 @@ +//! The persona-seed harness: `project` run over fixtures instead of her live body, so a candidate state can be judged +//! before any Mind holds it. +//! +//! - A **room fixture** (`mind-persona project --room `) is a TOML file standing in for the mouth and the room tail: +//! her Matrix id, the rooms, the messages with who said them, when and where, and which were new to her, plus a mocked +//! clock and a mocked "last heard from" table. The fixture is a `MouthReader`, so `project` reads it exactly as it reads +//! the mouth, and whether a message is for her is still `address::is_for`, over the fixture's names. +//! - A **batch** (`--batch `) runs `project` once per state, scenario and sample and writes one Markdown report. +//! - A **simulation** (`--simulate `, `simulate.rs`) runs the cast of the swarm in turn in one simulated room. +//! +//! Every case is one call of `project`: experiments and the live turn share one code path. None of this can write: the +//! organs are `project`'s read-only ports, a state file is read and never put, and nothing reaches the mouth. + +use std::collections::BTreeMap; +use std::fmt::Write as _; +use std::path::{Path, PathBuf}; + +use anyhow::{Context, Result, anyhow, bail}; +use chrono::{DateTime, Duration, Utc}; +use eureka_pipeline::Slug; +use serde::Deserialize; +use serde_json::{Value, json}; + +use super::narrate::{Model, distinct_2}; +use super::{MouthReader, Organs, PersonaSource, Request, Through, TurnKind, project}; +use crate::address::{Addressee, localpart}; +use crate::chat_client::{Basis, ChatDelivery, ChatPending, Form, TailLine}; +use crate::heartbeat::{Addressing, HeartbeatView}; + +pub use super::simulate::simulate; + +/// A narration whose word trigrams overlap what she heard past this share is marked as a high lexical overlap. +pub(crate) const OVERLAP_FLAG: f64 = 0.15; +/// The most room lines a view's tail carries, as a mouth's tail is bounded. +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()))?; + huginn_mind::persona::validate_document(&Slug(instance.to_owned()), &state).map_err(|refusal| anyhow!("{} would be refused by a put: {refusal}", path.display()))?; + Ok(state) +} + +/// "90s", "3m", "26h" or "2d": how long ago. +fn ago(text: &str) -> Result { + let text = text.trim(); + let split = text.find(|c: char| !c.is_ascii_digit()).unwrap_or(text.len()); + let amount: i64 = text[..split].parse().map_err(|_| anyhow!("\"{text}\" is not a time like 90s, 3m, 26h or 2d"))?; + Ok(match &text[split..] { + "s" => Duration::seconds(amount), + "m" => Duration::minutes(amount), + "h" => Duration::hours(amount), + "d" => Duration::days(amount), + _ => bail!("\"{text}\" is not a time like 90s, 3m, 26h or 2d"), + }) +} + +fn unix_ms(at: DateTime) -> u64 { + u64::try_from(at.timestamp_millis()).unwrap_or_default() +} + +fn matrix_id(id: &str) -> bool { + id.starts_with('@') && id.contains(':') +} + +/// One message as a fixture or a simulation script writes it. +#[derive(Clone, Debug, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Message { + /// The sender's Matrix id. + pub from: String, + /// The sender's display name. + #[serde(default)] + pub name: String, + pub room: String, + /// How long before the fixture's `now` it was said: "90s", "3m", "26h", "2d". Room fixtures only. + #[serde(default)] + pub ago: Option, + /// The round it is said at; 0 is before the first round. Simulations only. + #[serde(default)] + pub round: Option, + pub says: String, + /// New to her this turn: delivered by the mouth, not only in the room's tail. Room fixtures only. + #[serde(default)] + pub new: bool, + /// Tags her (`m.mentions` holds her id). + #[serde(default)] + pub tag: bool, + /// Other Matrix ids it tags. + #[serde(default)] + pub mentions: Vec, + /// A direct message: to her in a room fixture; to `to` in a simulation script. + #[serde(default)] + pub dm: bool, + /// The recipient of a direct message, by Matrix id. A room fixture's defaults to her; a script must name one. + #[serde(default)] + pub to: Option, + /// A reply in a conversation she opened (the mouth's follow-up basis). + #[serde(default)] + pub follow_up: bool, + /// The number (from 1, in file order) of the earlier message it replies to; Matrix calls it a reply to her when + /// that message was hers. + #[serde(default)] + pub reply_to: Option, +} + +/// A message placed in the room: when it was said, and whom it tags. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Line { + pub from: String, + pub name: String, + pub room: String, + pub at_ms: u64, + pub says: String, + pub mentions: Vec, + /// A direct message, to this Matrix id: only its sender and its recipient see it. + pub dm_to: Option, + pub follow_up: bool, + /// The index of the line it replies to. + pub reply_to: Option, + pub new: bool, +} + +impl Line { + /// A line from a script message; `her` is whom `tag` names. + pub(crate) fn of(message: &Message, at: DateTime, her: Option<&str>, index: usize) -> Result { + if !matrix_id(&message.from) { + bail!("message {} is from \"{}\", which is not a Matrix id", index + 1, message.from); + } + let mut mentions = message.mentions.clone(); + if message.tag { + mentions.push(her.ok_or_else(|| anyhow!("message {} tags her, and a simulation has no one \"her\": name the ids in mentions", index + 1))?.to_owned()); + } + let dm_to = match (message.dm, &message.to) { + (true, Some(to)) if matrix_id(to) => Some(to.clone()), + (true, Some(to)) => bail!("message {} is a direct message to \"{to}\", which is not a Matrix id", index + 1), + (true, None) => Some(her.ok_or_else(|| anyhow!("message {} is a direct message to no one: a script names its recipient in `to`", index + 1))?.to_owned()), + (false, Some(_)) => bail!("message {} names a recipient but is not a direct message", index + 1), + (false, None) => None, + }; + let reply_to = match message.reply_to { + Some(number) if number >= 1 && number <= index => Some(number - 1), + Some(number) => bail!("message {} replies to message {number}, which is not an earlier one", index + 1), + None => None, + }; + Ok(Self { from: message.from.clone(), name: message.name.clone(), room: message.room.clone(), at_ms: unix_ms(at), says: message.says.clone(), mentions, dm_to, follow_up: message.follow_up, reply_to, new: message.new }) + } + + /// What Matrix would say about this line to `viewer`: a direct message, a tag, a reply to a line of hers, or a + /// follow-up. + fn basis(&self, viewer: &str, lines: &[Line]) -> Option { + if self.dm_to.as_deref() == Some(viewer) { + Some(Basis::Direct) + } else if self.mentions.iter().any(|id| id == viewer) { + Some(Basis::Tag) + } else if self.reply_to.and_then(|at| lines.get(at)).is_some_and(|replied| replied.from == viewer) { + Some(Basis::Reply) + } else if self.follow_up { + Some(Basis::FollowUp) + } else { + None + } + } + + /// Whether `viewer` sees the line: a direct message only its sender and recipient; any other line every member of its + /// room (no rooms named is every room). + pub(crate) fn visible_to(&self, viewer: &str, rooms: &[String]) -> bool { + match &self.dm_to { + Some(to) => to == viewer || self.from == viewer, + None => rooms.is_empty() || rooms.contains(&self.room), + } + } + + pub(crate) fn delivery(&self, index: usize, viewer: &str, lines: &[Line]) -> ChatDelivery { + ChatDelivery { + seq: index as u64 + 1, + room_alias: self.room.clone(), + room_id: self.room.clone(), + event_id: format!("$fixture-{index}"), + sender: self.from.clone(), + sender_display: self.name.clone(), + text: self.says.clone(), + at_unix_ms: self.at_ms, + context: vec![], + mentions: self.mentions.clone(), + is_direct: self.dm_to.is_some(), + image: None, + basis: self.basis(viewer, lines), + form: Form::Message, + reply_to: Default::default(), + } + } +} + +/// The mouth's pending read as `viewer` would get it from `lines`: the lines `new` says she has not had, from others, +/// in her rooms, as deliveries; and every line of her rooms, hers included, as the tail. +pub(crate) fn pending_for(lines: &[Line], viewer: &str, rooms: &[String], new: impl Fn(usize) -> bool) -> ChatPending { + let hers = |line: &Line| line.visible_to(viewer, rooms); + let deliveries = lines.iter().enumerate().filter(|(at, line)| new(*at) && hers(line) && line.from != viewer).map(|(at, line)| line.delivery(at, viewer, lines)).collect(); + let tail: Vec = lines + .iter() + .enumerate() + .filter(|(_, line)| hers(line)) + .map(|(at, line)| TailLine { room: line.room.clone(), event_id: format!("$fixture-{at}"), at_unix_ms: line.at_ms, sender: line.from.clone(), form: Form::Message, basis: line.basis(viewer, lines), body: line.says.clone() }) + .collect(); + let tail = tail[tail.len().saturating_sub(TAIL_LINES)..].to_vec(); + ChatPending { persona: localpart(viewer).to_owned(), deliveries, tail: Some(tail), tail_gaps: vec![] } +} + +// --------------------------------------------------------------------------------------------- +// The room fixture. + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct FixtureFile { + her: String, + #[serde(default)] + names: Vec, + kind: TurnKind, + now: DateTime, + rooms: Vec, + #[serde(default)] + here: Option, + #[serde(default)] + operators: Vec, + #[serde(default)] + minds: Vec, + #[serde(default)] + last_heard: BTreeMap, + #[serde(default, rename = "message")] + messages: Vec, +} + +/// A mocked room: it stands in for the mouth and the room tail, the clock, and who is who. +#[derive(Clone, Debug)] +pub struct Fixture { + /// Her Matrix id. + pub her: String, + /// More names she answers to, beside her handle; none leaves her own names (handle and public name). + pub names: Vec, + pub kind: TurnKind, + pub now: DateTime, + pub here: Option, + pub operators: Vec, + pub minds: Vec, + pub last_heard: Vec<(String, u64)>, + lines: Vec, +} + +impl Fixture { + pub fn load(path: &Path) -> Result { + let text = std::fs::read_to_string(path).with_context(|| format!("the room fixture {} could not be read", path.display()))?; + Self::parse(&text).with_context(|| format!("the room fixture {} is not a room", path.display())) + } + + pub fn parse(text: &str) -> Result { + let file: FixtureFile = toml::from_str(text)?; + if !matrix_id(&file.her) { + bail!("her = \"{}\" is not a Matrix id", file.her); + } + if file.rooms.is_empty() { + bail!("rooms names no room"); + } + if let Some(here) = &file.here + && !file.rooms.contains(here) + { + bail!("here = \"{here}\" is not one of the rooms"); + } + let mut lines = Vec::new(); + for (index, message) in file.messages.iter().enumerate() { + if message.round.is_some() { + bail!("message {} has a round: a room fixture's messages say how long ago, not which round", index + 1); + } + if !message.dm && !file.rooms.contains(&message.room) { + bail!("message {} is in {}, which is not one of the rooms", index + 1, message.room); + } + let at = file.now - ago(message.ago.as_deref().unwrap_or("0s"))?; + lines.push(Line::of(message, at, Some(&file.her), index)?); + } + let last_heard = file.last_heard.iter().map(|(id, when)| Ok((id.clone(), unix_ms(file.now - ago(when)?)))).collect::>()?; + Ok(Self { her: file.her, names: file.names, kind: file.kind, now: file.now, here: file.here, operators: file.operators, minds: file.minds, last_heard, lines }) + } + + /// Her addressee, as the heartbeat's table would carry it, when the fixture names her: her handle and its names. + /// None leaves `project` her own names. + pub fn addressee(&self) -> Option<&dyn HeartbeatView> { + (!self.names.is_empty()).then_some(self as &dyn HeartbeatView) + } +} + +impl MouthReader for Fixture { + fn pending(&self) -> Result { + Ok(pending_for(&self.lines, &self.her, &[], |at| self.lines[at].new)) + } +} + +impl HeartbeatView for Fixture { + fn addressing(&self) -> Result> { + let names = std::iter::once(localpart(&self.her)).chain(self.names.iter().map(String::as_str)); + Ok(Some(Addressing { minds: vec![Addressee::new(&self.her, names)] })) + } +} + +// --------------------------------------------------------------------------------------------- +// What every harness run shares. + +/// What a harness run reads through beside its fixtures: her stored state (the persona door; only a batch state without +/// a file asks), the model, and her tuning. +pub struct Harness<'a> { + pub instance: String, + pub persona: Option<&'a dyn PersonaSource>, + pub model: Option<&'a dyn Model>, + pub tuning: PathBuf, +} + +/// One turn's `project` request over a fixture. +pub fn fixture_request(harness: &Harness, fixture: &Fixture, state: Option, seed: u64, renders: Option, through: Through) -> Request { + Request { + instance: harness.instance.clone(), + kind: fixture.kind, + state, + room: fixture.here.clone(), + seed, + renders, + judge_against: None, + operators: fixture.operators.clone(), + minds: fixture.minds.clone(), + own_sender: fixture.her.clone(), + tuning: harness.tuning.clone(), + through, + last_heard: fixture.last_heard.clone(), + recent: vec![], + own_rooms: vec![], + } +} + +// --------------------------------------------------------------------------------------------- +// Batch. + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct BatchFile { + #[serde(default = "one")] + samples: usize, + #[serde(default)] + seed: u64, + #[serde(default)] + through: Through, + #[serde(rename = "state")] + states: Vec, + #[serde(rename = "scenario")] + scenarios: Vec, +} + +fn one() -> usize { + 1 +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct StateEntry { + name: String, + /// A state file, relative to the plan; none is her stored state, read through the persona door. + #[serde(default)] + file: Option, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct ScenarioEntry { + name: String, + /// A room fixture, relative to the plan. + room: PathBuf, +} + +fn relative(plan: &Path, path: &Path) -> PathBuf { + if path.is_absolute() { path.to_owned() } else { plan.parent().unwrap_or(Path::new(".")).join(path) } +} + +/// Every state by every scenario by every sample, each one `project`, as one Markdown report. A case that fails is +/// reported in its place and the batch goes on. +pub fn batch(harness: &Harness, plan_path: &Path) -> Result { + let text = std::fs::read_to_string(plan_path).with_context(|| format!("the plan {} could not be read", plan_path.display()))?; + let plan: BatchFile = toml::from_str(&text).with_context(|| format!("the plan {} is not a batch plan", plan_path.display()))?; + if plan.samples == 0 || plan.states.is_empty() || plan.scenarios.is_empty() { + bail!("the plan {} needs a state, a scenario and at least one sample", plan_path.display()); + } + let states: Vec<(String, Option)> = plan.states.iter().map(|entry| Ok((entry.name.clone(), entry.file.as_ref().map(|file| state_file(&relative(plan_path, file), &harness.instance)).transpose()?))).collect::>()?; + let scenarios: Vec<(String, Fixture)> = plan.scenarios.iter().map(|entry| Ok((entry.name.clone(), Fixture::load(&relative(plan_path, &entry.room))?))).collect::>()?; + + let mut cases = Vec::new(); + for (state_name, state) in &states { + for (scenario_name, fixture) in &scenarios { + for sample in 0..plan.samples { + let seed = plan.seed.wrapping_add(sample as u64); + let organs = Organs { persona: harness.persona, chat: Some(fixture), addressing: fixture.addressee(), model: harness.model }; + let request = fixture_request(harness, fixture, state.clone(), seed, Some(1), plan.through); + let report = project(&organs, &request, fixture.now).unwrap_or_else(|fault| json!({ "error": format!("{fault:#}") })); + cases.push(json!({ "state": state_name, "scenario": scenario_name, "sample": sample + 1, "seed": seed, "report": report })); + } + } + } + Ok(batch_markdown(plan_path, &plan, &cases)) +} + +fn render_of(case: &Value) -> &Value { + &case["report"]["renders"][0] +} + +fn prose_of(case: &Value) -> String { + render_of(case)["persona"]["prose"].as_str().unwrap_or_default().to_owned() +} + +fn batch_markdown(plan_path: &Path, plan: &BatchFile, cases: &[Value]) -> String { + let mut out = String::new(); + let first = cases.iter().find(|case| case["report"].get("error").is_none()).map(|case| &case["report"]); + let _ = writeln!(out, "# Persona seed batch\n"); + let _ = writeln!(out, "- plan: `{}`", plan_path.display()); + let _ = writeln!(out, "- states: {}; scenarios: {}; samples: {}; through: {:?}", plan.states.len(), plan.scenarios.len(), plan.samples, plan.through); + if let Some(report) = first { + let _ = writeln!(out, "- instance: `{}`; tuning: {} (`{}`)", report["instance"].as_str().unwrap_or_default(), report["tuning"]["source"].as_str().unwrap_or_default(), report["tuning"]["sha256"].as_str().unwrap_or_default()); + let _ = writeln!(out, "- Projector model: `{}`; Persona model: `{}`", report["model"].as_str().unwrap_or("none"), report["persona_model"].as_str().unwrap_or("none")); + } + let _ = writeln!(out, "- would-say lines are the double-quoted spans of her prose: a preview of what the Interpreter (C3b) will quote, not its decision"); + let _ = writeln!(out, "- read-only: no put, no trace, no send; the mouth is the fixture\n"); + + // The summary: each state by scenario, then the states' voices against each other. + let _ = writeln!(out, "## Summary\n\n| state | scenario | spoke | mean narration words | mean prose words |\n|---|---|---|---|---|"); + for state in &plan.states { + for scenario in &plan.scenarios { + let mine: Vec<&Value> = cases.iter().filter(|case| case["state"] == state.name.as_str() && case["scenario"] == scenario.name.as_str()).collect(); + let spoke = mine.iter().filter(|case| render_of(case)["interpreter"]["said"].as_array().is_some_and(|said| !said.is_empty())).count(); + let mean = |key: &dyn Fn(&Value) -> Option| mine.iter().filter_map(|case| key(render_of(case))).sum::() as f64 / mine.len().max(1) as f64; + let _ = writeln!(out, "| {} | {} | {spoke}/{} | {:.0} | {:.0} |", state.name, scenario.name, mine.len(), mean(&|render| render["words"].as_u64()), mean(&|render| render["persona"]["words"].as_u64())); + } + } + let corpora: Vec<(String, Vec)> = plan.states.iter().map(|state| (state.name.clone(), cases.iter().filter(|case| case["state"] == state.name.as_str()).map(prose_of).filter(|prose| !prose.is_empty()).collect())).collect(); + let _ = writeln!(out, "\nDistinct-2 of her prose within each state (1 is no repeated word pair), and over all states pooled:\n"); + for (name, prose) in &corpora { + let _ = writeln!(out, "- {name}: {:.3}", distinct_2(prose)); + } + let _ = writeln!(out, "- pooled: {:.3}", distinct_2(&corpora.iter().flat_map(|(_, prose)| prose.clone()).collect::>())); + let _ = writeln!(out, "\nWord-use similarity between states' prose (cosine of word counts, 1 is the same words in the same proportions):\n"); + for (at, (a, a_prose)) in corpora.iter().enumerate() { + for (b, b_prose) in &corpora[at + 1..] { + let _ = writeln!(out, "- {a} / {b}: {:.3}", cosine(&a_prose.join(" "), &b_prose.join(" "))); + } + } + + for case in cases { + let _ = writeln!(out, "\n## {} × {}, sample {} (seed {})\n", case["state"].as_str().unwrap_or_default(), case["scenario"].as_str().unwrap_or_default(), case["sample"], case["seed"]); + let report = &case["report"]; + if let Some(error) = report.get("error") { + let _ = writeln!(out, "**failed:** {}", error.as_str().unwrap_or_default()); + continue; + } + let packet = &report["packet"]; + let _ = writeln!(out, "- lens: `{}`; present: {}; room: {}", packet["lens"], packet["present"], report["room"]); + let _ = writeln!(out, "\n
packet\n"); + fenced(&mut out, "identity", packet["identity"].as_str().unwrap_or_default()); + fenced(&mut out, "context", packet["context"].as_str().unwrap_or_default()); + fenced(&mut out, "stimulus", packet["stimulus"].as_str().unwrap_or_default()); + fenced(&mut out, "heard", packet["heard"].as_str().unwrap_or_default()); + let _ = writeln!(out, "
\n"); + turn_markdown(&mut out, render_of(case)); + } + let _ = writeln!(out, "\n## Total usage\n\n{}", usage_markdown(&usage_total(cases))); + out +} + +/// One render's narration, prose, would-say lines, word counts and usage. +pub(crate) fn turn_markdown(out: &mut String, render: &Value) { + if let Some(failed) = render["failed"].as_str() { + let _ = writeln!(out, "**Projector call failed:** {failed} + +Usage: {} +", usage_line(&render["usage"])); + return; + } + let _ = writeln!(out, "**Narration** ({} words{}):\n", render["words"], if render["cut_at_budget"] == true { ", cut at budget" } else { "" }); + quoted(out, render["narration"].as_str().unwrap_or_default()); + if let Some(names) = render["field_names"].as_array().filter(|names| !names.is_empty()) { + let _ = writeln!(out, "**Field names leaked:** {}\n", Value::Array(names.clone())); + } + let _ = writeln!( + out, + "Narration checks: lexical overlap with what she heard {}{}{}\n", + render["heard_overlap"].as_f64().map_or_else(|| "n/a".to_owned(), |overlap| format!("{overlap:.2}")), + if render["heard_overlap"].as_f64().is_some_and(|overlap| overlap > OVERLAP_FLAG) { " (high)" } else { "" }, + if render["offers_options"] == true { "; offers her options" } else { "" } + ); + let persona = &render["persona"]; + if let Some(failed) = persona["failed"].as_str() { + let _ = writeln!(out, "**Persona call failed:** {failed} +"); + } else if !persona.is_null() { + let _ = writeln!(out, "**Her prose** ({} words{}):\n", persona["words"], if persona["cut_at_budget"] == true { ", cut at budget" } else { "" }); + quoted(out, persona["prose"].as_str().unwrap_or_default()); + } + interpreter_markdown(out, &render["interpreter"]); + let _ = writeln!(out, "Usage: projector {}; persona {}; interpreter {}\n", usage_line(&render["usage"]), usage_line(&persona["usage"]), usage_line(&render["interpreter"]["usage"])); +} + +/// The Interpreter's speech decision: each line said with its room, mentions, reply target and the span of her prose it +/// rests on; each line refused, with why; its gaps. None when the render did not run through the Interpreter. +pub(crate) fn interpreter_markdown(out: &mut String, decision: &Value) { + if decision.is_null() { + return; + } + if let Some(failed) = decision["failed"].as_str() { + let _ = writeln!(out, "**Interpreter failed:** {failed}\n"); + return; + } + let said = decision["said"].as_array().cloned().unwrap_or_default(); + if said.is_empty() { + let _ = writeln!(out, "**Interpreter: nothing said.**\n"); + } else { + let _ = writeln!(out, "**Interpreter: said**\n"); + for line in &said { + let mentions = line["mentions"].as_array().map(|ids| ids.iter().filter_map(Value::as_str).collect::>().join(", ")).unwrap_or_default(); + let reply = line["reply_to"].as_str().map_or_else(String::new, |id| format!("; reply to {id}")); + let _ = writeln!(out, "- in {}{}{}: {}", line["room"].as_str().unwrap_or_default(), if mentions.is_empty() { String::new() } else { format!(", mentioning {mentions}") }, reply, line["text"].as_str().unwrap_or_default()); + let spans: Vec = line["rests_on"].as_array().into_iter().flatten().map(|span| format!("\"{}\"", span["quote"].as_str().unwrap_or_default())).collect(); + let _ = writeln!(out, " - her words: {}", spans.join(" + ")); + } + out.push('\n'); + } + for line in decision["refused"].as_array().into_iter().flatten() { + let what = line["text"].as_str().map_or_else(|| line["spans"].to_string(), str::to_owned); + let _ = writeln!(out, "- refused ({}): {what}", line["why"].as_str().unwrap_or_default()); + } + for gap in decision["gaps"].as_array().into_iter().flatten() { + let _ = writeln!(out, "- gap: {gap}"); + } + out.push('\n'); +} + +fn fenced(out: &mut String, label: &str, text: &str) { + if !text.is_empty() { + let _ = writeln!(out, "{label}:\n\n````text\n{text}\n````\n"); + } +} + +fn quoted(out: &mut String, text: &str) { + for line in text.lines() { + let _ = writeln!(out, "> {line}"); + } + out.push('\n'); +} + +// --------------------------------------------------------------------------------------------- +// Usage and similarity, shared with the simulation. + +const USAGE_FIELDS: [&str; 5] = ["input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens", "reasoning_tokens"]; + +/// Every `usage` object anywhere in `values`, summed per model and field, with the number of calls. +pub(crate) fn usage_total(values: &[Value]) -> BTreeMap> { + fn walk(value: &Value, into: &mut BTreeMap>) { + match value { + Value::Object(map) => { + for (key, inner) in map { + if key == "usage" && inner.is_object() { + let model = inner["model"].as_str().unwrap_or("unknown").to_owned(); + let row = into.entry(model).or_default(); + *row.entry("calls".into()).or_default() += 1; + for field in USAGE_FIELDS { + *row.entry(field.into()).or_default() += inner[field].as_u64().unwrap_or(0); + } + } else { + walk(inner, into); + } + } + } + Value::Array(items) => items.iter().for_each(|inner| walk(inner, into)), + _ => {} + } + } + let mut total = BTreeMap::new(); + values.iter().for_each(|value| walk(value, &mut total)); + total +} + +pub(crate) fn usage_markdown(total: &BTreeMap>) -> String { + if total.is_empty() { + return "No model call was counted.".into(); + } + let mut out = String::from("| model | calls | input | output | cache read | cache write | reasoning |\n|---|---|---|---|---|---|---|\n"); + for (model, row) in total { + let get = |field: &str| row.get(field).copied().unwrap_or(0); + let _ = writeln!(out, "| {model} | {} | {} | {} | {} | {} | {} |", get("calls"), get("input_tokens"), get("output_tokens"), get("cache_read_tokens"), get("cache_write_tokens"), get("reasoning_tokens")); + } + out +} + +fn usage_line(usage: &Value) -> String { + if usage.is_null() { + return "none".into(); + } + format!("{} in, {} out, {} cache read", usage["input_tokens"], usage["output_tokens"], usage["cache_read_tokens"]) +} + +/// The words of a text as counted for similarity: lowercased, letters and digits, three characters or more. +fn word_counts(text: &str) -> BTreeMap { + let mut counts = BTreeMap::new(); + for word in text.split(|c: char| !c.is_alphanumeric()).filter(|word| word.chars().count() >= 3) { + *counts.entry(word.to_lowercase()).or_insert(0.0) += 1.0; + } + counts +} + +/// The cosine of two texts' word counts: 1 is the same words in the same proportions, 0 no word shared. A cheap +/// lexical proxy for one voice, not a measure of meaning. +pub(crate) fn cosine(a: &str, b: &str) -> f64 { + let (a, b) = (word_counts(a), word_counts(b)); + let dot: f64 = a.iter().filter_map(|(word, count)| b.get(word).map(|other| count * other)).sum(); + let norm = |counts: &BTreeMap| counts.values().map(|count| count * count).sum::().sqrt(); + let denominator = norm(&a) * norm(&b); + if denominator == 0.0 { 0.0 } else { dot / denominator } +} + +#[cfg(test)] +pub(crate) mod tests; diff --git a/crates/mind-body/src/projection/harness/tests.rs b/crates/mind-body/src/projection/harness/tests.rs new file mode 100644 index 0000000..26d63bd --- /dev/null +++ b/crates/mind-body/src/projection/harness/tests.rs @@ -0,0 +1,301 @@ +use std::cell::RefCell; +use std::path::{Path, PathBuf}; + +use anyhow::Result; +use serde_json::{Value, json}; + +use super::*; +use crate::address::is_for; +use crate::projection::narrate::{Answer, Failed, Spent}; + +const HER: &str = "@eureka:gamecult.org"; + +pub(crate) fn fixtures() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/harness") +} + +pub(crate) fn control() -> Value { + serde_json::from_str(&std::fs::read_to_string(fixtures().join("states/control.json")).unwrap()).unwrap() +} + +fn room(name: &str) -> Fixture { + Fixture::load(&fixtures().join("rooms").join(name)).unwrap() +} + +/// A recorded model: the Projector narrates a fixed line; the Persona stage answers by `persona(prompt)`; the +/// Interpreter by `interpreter(prompt)`, by default `quoted_lines`. Every call is kept, with its usage counted. +pub(crate) struct Scripted { + pub calls: RefCell>, + pub persona: Box String>, + pub interpreter: Box String>, +} + +impl Scripted { + pub(crate) fn new(persona: impl Fn(&str) -> String + 'static) -> Self { + Self { calls: RefCell::default(), persona: Box::new(persona), interpreter: Box::new(quoted_lines) } + } + + pub(crate) fn interpreting(mut self, interpreter: impl Fn(&str) -> String + 'static) -> Self { + self.interpreter = Box::new(interpreter); + self + } +} + +/// The recorded Interpreter of the tests: each double-quoted span of her prose is a line she says, citing itself, +/// mentioning no one. A test double that keeps the tests' prose readable, not the speech rule: to code a quotation is +/// only a text whose sentences are whole, and the live Interpreter is a model. +pub(crate) fn quoted_lines(prompt: &str) -> String { + let prose = prompt.split("Her prose:\n").nth(1).unwrap_or_default(); + let say: Vec = prose.split('"').enumerate().filter(|(at, part)| at % 2 == 1 && !part.trim().is_empty()).map(|(_, part)| json!({ "spans": [part], "mentions": [], "reply_to": null })).collect(); + json!({ "say": say }).to_string() +} + +impl Model for Scripted { + fn ask(&self, _request_id: &str, model: &str, prompt: &str) -> std::result::Result { + self.calls.borrow_mut().push((model.to_owned(), prompt.to_owned())); + let text = if prompt.starts_with("You decide what one person says aloud") { + (self.interpreter)(prompt) + } else if prompt.starts_with("