From 8d69d3bded59122ebb0c80827f0fae64f1a18a4a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 00:33:13 +0100 Subject: [PATCH 1/2] mind-persona project: the persona seed harness (--room, --through persona, --batch, --simulate) The harness judges candidate persona states before any Mind holds them, through the same `project` the live turn will use: every case is one `project` call. - `--room `: a mocked room in place of the mouth and the room tail: her id, rooms, messages (sender, display name, room, ago, body; new, tag, mentions, dm, follow_up, reply_to), a mocked now and a "last heard from" table. The fixture is a MouthReader, so addressing is still `address::is_for`. The live room filter that `--room` used to name is now `--in`. - `--state` is checked as a put checks content: huginn-mind's P1 to P3 now live in one owner, `persona::validate_document`, which the put calls. - `--through persona`: the Persona stage (C3b's first piece). One tool-less connector call under Ghostlight's persona membrane over identity, narration and the heard lines; prose capped at 180 words; would-say lines are the double-quoted spans; usage per call. Tuning gains `[persona]` (model, guidance, words). - `--batch --out `: states x scenarios x samples, one Markdown report with packets, narration, prose, would-say lines, word counts, distinct-2 and cross-state word similarity, and a total-usage footer. - `--simulate --out `: up to eight personas taking turns in one room. Wake is is_for plus the ruled set (addressed, any human in her rooms, the beat); `wake = "all_messages"` is the old loop as a control. Woken personas run in a seeded order; quoted lines join the room as from her id. State does not evolve (`state_after_turn` is the seam for C3b/C3c). Metrics: messages, silence, who addresses whom, Mind-to-Mind chains, lexical convergence, a work-talk heuristic, usage. - With a state file and a fixture no daemon and no mouth is needed (bin test). - Request ids take the wall clock, so a fixture's fixed now cannot replay. Co-Authored-By: Claude Opus 5.5 --- crates/huginn-mind/src/admission.rs | 12 +- crates/huginn-mind/src/persona.rs | 17 +- .../fixtures/harness/rooms/deru-passing.toml | 23 + .../fixtures/harness/rooms/mind-teases.toml | 31 + .../fixtures/harness/rooms/operator-warm.toml | 27 + .../fixtures/harness/rooms/quiet-beat.toml | 33 + .../fixtures/harness/states/control.json | 148 +++++ crates/mind-body/src/bin/mind-persona.rs | 139 +++-- crates/mind-body/src/cli.rs | 11 +- crates/mind-body/src/lib.rs | 5 +- crates/mind-body/src/projection.rs | 82 ++- crates/mind-body/src/projection/harness.rs | 571 ++++++++++++++++++ .../mind-body/src/projection/harness/tests.rs | 220 +++++++ crates/mind-body/src/projection/narrate.rs | 43 +- crates/mind-body/src/projection/select.rs | 12 +- crates/mind-body/src/projection/simulate.rs | 403 ++++++++++++ .../src/projection/simulate/tests.rs | 173 ++++++ crates/mind-body/src/projection/tests.rs | 16 +- crates/mind-body/src/projection/tuning.rs | 26 +- crates/mind-body/tests/persona_bin.rs | 34 ++ crates/mind-body/tests/public_surface.txt | 28 +- 21 files changed, 1957 insertions(+), 97 deletions(-) create mode 100644 crates/mind-body/fixtures/harness/rooms/deru-passing.toml create mode 100644 crates/mind-body/fixtures/harness/rooms/mind-teases.toml create mode 100644 crates/mind-body/fixtures/harness/rooms/operator-warm.toml create mode 100644 crates/mind-body/fixtures/harness/rooms/quiet-beat.toml create mode 100644 crates/mind-body/fixtures/harness/states/control.json create mode 100644 crates/mind-body/src/projection/harness.rs create mode 100644 crates/mind-body/src/projection/harness/tests.rs create mode 100644 crates/mind-body/src/projection/simulate.rs create mode 100644 crates/mind-body/src/projection/simulate/tests.rs 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/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..ce802ca 100644 --- a/crates/mind-body/src/bin/mind-persona.rs +++ b/crates/mind-body/src/bin/mind-persona.rs @@ -13,19 +13,29 @@ //! 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] +//! [--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 would check it), 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. +//! +//! 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 +44,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] [--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 +86,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 +93,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" => bail!("--through interpreter is not built yet (map C3b)"), + _ => 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 +176,30 @@ 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![], }; - 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/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..2fbd449 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -10,12 +10,17 @@ //! 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 narrate; mod select; +mod simulate; mod tuning; use std::path::PathBuf; @@ -34,7 +39,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, judge_prompt, judged, persona_prompt, projector_prompt, word_budget, words, would_say}; use select::{History, Inputs, Packet, People, Room, RoomLine, select}; /// The agent name on the connector's request ids. @@ -106,6 +111,15 @@ 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, +} + /// What to project. pub struct Request { pub instance: String, @@ -126,6 +140,12 @@ 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>, } /// Her addressee: the heartbeat's row for her when its table holds one, else her own names (handle and public name). @@ -158,7 +178,7 @@ 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(), ..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); let messages: Vec = pending @@ -183,7 +203,7 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo .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) + (Room { messages, tail, here: request.room.clone(), last_heard: request.last_heard.clone() }, report) } fn packet_json(packet: &Packet) -> Value { @@ -202,13 +222,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,7 +240,7 @@ 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, @@ -225,7 +249,23 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni "field_names": field_names(&narration, &named), "usage": answer.usage, "narration": narration, - }) + }); + if through == Through::Persona { + render["persona"] = 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); + json!({ + "words": words(&prose), + "cut_at_budget": words(&spoke.text) > tuning.persona.words, + "would_say": would_say(&prose), + "usage": spoke.usage, + "prose": prose, + }) + } + Err(failed) => json!({ "failed": failed.reason, "usage": failed.usage }), + }; + } + render } /// K successive turns over one state: each packet under the freshness the earlier ones left. @@ -258,10 +298,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(), ..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 +313,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 +321,12 @@ 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::Persona { + report["persona_model"] = json!(tuning.persona.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 +338,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 +348,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/harness.rs b/crates/mind-body/src/projection/harness.rs new file mode 100644 index 0000000..797d929 --- /dev/null +++ b/crates/mind-body/src/projection/harness.rs @@ -0,0 +1,571 @@ +//! 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; + +/// 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. +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. + #[serde(default)] + pub dm: bool, + /// 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, + pub dm: bool, + 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 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: message.dm, 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 { + 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 + } + } + + 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, + 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| rooms.is_empty() || rooms.contains(&line.room) || line.dm; + 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![], + } +} + +// --------------------------------------------------------------------------------------------- +// 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)["persona"]["would_say"].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 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()); + match persona["would_say"].as_array().filter(|said| !said.is_empty()) { + Some(said) => { + let _ = writeln!(out, "**Would say:**\n"); + for line in said { + let _ = writeln!(out, "- \"{}\"", line.as_str().unwrap_or_default()); + } + out.push('\n'); + } + None => { + let _ = writeln!(out, "**Would say:** nothing (silence)\n"); + } + } + } + let _ = writeln!(out, "Usage: projector {}; persona {}\n", usage_line(&render["usage"]), usage_line(&persona["usage"])); +} + +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..b11a727 --- /dev/null +++ b/crates/mind-body/src/projection/harness/tests.rs @@ -0,0 +1,220 @@ +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, would_say}; + +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)`. Every call is +/// kept, with its usage counted. +pub(crate) struct Scripted { + pub calls: RefCell>, + pub persona: Box String>, +} + +impl Scripted { + pub(crate) fn new(persona: impl Fn(&str) -> String + 'static) -> Self { + Self { calls: RefCell::default(), persona: Box::new(persona) } + } +} + +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("