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 <fixture.toml>`: 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 <plan> --out <md>`: 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 <plan> --out <md>`: 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 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 00:33:13 +01:00
commit 8d69d3bded
21 changed files with 1956 additions and 96 deletions

View file

@ -268,17 +268,9 @@ impl<S: MindStore> Mind<S> {
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 {

View file

@ -92,7 +92,7 @@ fn declared_names(node: &Value, names: &mut BTreeSet<String>) {
/// 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<Value, MindRefusal> {
rmp_serde::from_slice::<HuginnPersonaEntry>(&envelope.payload)

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"
}

View file

@ -13,19 +13,29 @@
//! will need. Without `--endpoint` the daemon is `HUGINN_ENDPOINT`; no call is made
//! to it.
//!
//! `mind-persona project --instance <slug> [--endpoint rudp://<ip>:<port>] [--kind chat|beat|reflection] [--room <r>]
//! [--seed N] [--state <file>] [--select-only | --renders K [--judge <state-file>]]` is the Projector's experiment tool
//! `mind-persona project --instance <slug> [--endpoint rudp://<ip>:<port>] [--kind chat|beat|reflection] [--in <room>]
//! [--room <fixture.toml>] [--seed N] [--state <file>] [--select-only | --renders K [--through projector|persona]
//! [--judge <state-file>]]` 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/<instance>/persona-tuning.toml`; renders need `MIND_PERSONA_CONNECTOR_ENDPOINT`
//! (`<loopback ip>:<port>`) 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 <fixture>` 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 <slug> (--batch <plan.toml> | --simulate <plan.toml>) --out <report.md>` 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/<instance>/persona-tuning.toml`; renders and plans need
//! `MIND_PERSONA_CONNECTOR_ENDPOINT` (`<loopback ip>:<port>`) 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 <slug> [--endpoint rudp://<ip>:<port>] [--rooms a,b] [--operator id[,id]] [--minds id[,id]] [--self-sender id]";
const PROJECT_USAGE: &str = "usage: mind-persona project --instance <slug> [--endpoint rudp://<ip>:<port>] [--kind chat|beat|reflection] [--room <r>] [--seed N] [--state <file>] [--select-only | --renders K [--judge <state-file>]]";
const PROJECT_USAGE: &str = "usage: mind-persona project --instance <slug> [--endpoint rudp://<ip>:<port>] [--kind chat|beat|reflection] [--in <room>] [--room <fixture.toml>] [--seed N] [--state <file>] [--select-only | --renders K [--through projector|persona] [--judge <state-file>]] | (--batch <plan> | --simulate <plan>) --out <report.md>";
/// 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<String> {
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<serde_json::Value> {
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<PathBuf> {
@ -87,36 +93,82 @@ fn credentials_directory() -> Result<PathBuf> {
}
fn run_project(args: &[String]) -> Result<String> {
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::<u64>().map_err(|_| anyhow!("{PROJECT_USAGE}"))?),
"--state" => state = Some(value()?),
"--renders" => renders = Some(value()?.parse::<usize>().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::<u64>().map_err(|_| anyhow!("{PROJECT_USAGE}"))?),
"--state" => state = Some(json_file(&value()?)?),
"--renders" => renders = Some(value()?.parse::<usize>().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 <ip>:<port>"))?;
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://<ip>:<port>"))?;
@ -124,35 +176,30 @@ fn run_project(args: &[String]) -> Result<String> {
}
_ => 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 <ip>:<port>"))?;
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!(

View file

@ -45,6 +45,13 @@ const USAGE: &str =
/// `rudp://<ip>:<port>` is refused with `usage` or a fixed message, and no
/// value is echoed.
pub fn instance_and_endpoint(args: &[String], env_endpoint: Option<String>, 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<String>, usage: &str) -> Result<(Slug, Option<SocketAddr>)> {
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<String>, 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::<SocketAddr>().ok())
.ok_or_else(|| anyhow!("the endpoint is not rudp://<ip>:<port>"))?;
Ok((instance, address))
Ok((instance, Some(address)))
}
/// Who made the change: `SUDO_USER`, else `USER`, else `unknown`. An empty

View file

@ -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@<instance>`: the

View file

@ -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<Vec<String>>,
}
/// 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<RoomLine> = 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<Utc>) -> 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<String> = 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<String> = 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<Utc>) -> 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<Utc>) -> 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<Utc>) -> 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<Value> = packets.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "a", now)).collect();
let narrations: Vec<Value> = 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::<Vec<_>>();
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<Utc>) -> 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<Value> = others.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "b", now)).collect();
let other_narrations: Vec<Value> = 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<Utc>) -> 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 }));

View file

@ -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 <file>`) 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 <plan>`) runs `project` once per state, scenario and sample and writes one Markdown report.
//! - A **simulation** (`--simulate <plan>`, `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<Value> {
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<Duration> {
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<Utc>) -> 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<String>,
/// The round it is said at; 0 is before the first round. Simulations only.
#[serde(default)]
pub round: Option<usize>,
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<String>,
/// 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<usize>,
}
/// 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<String>,
pub dm: bool,
pub follow_up: bool,
/// The index of the line it replies to.
pub reply_to: Option<usize>,
pub new: bool,
}
impl Line {
/// A line from a script message; `her` is whom `tag` names.
pub(crate) fn of(message: &Message, at: DateTime<Utc>, her: Option<&str>, index: usize) -> Result<Self> {
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<Basis> {
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<TailLine> = 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<String>,
kind: TurnKind,
now: DateTime<Utc>,
rooms: Vec<String>,
#[serde(default)]
here: Option<String>,
#[serde(default)]
operators: Vec<String>,
#[serde(default)]
minds: Vec<String>,
#[serde(default)]
last_heard: BTreeMap<String, String>,
#[serde(default, rename = "message")]
messages: Vec<Message>,
}
/// 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<String>,
pub kind: TurnKind,
pub now: DateTime<Utc>,
pub here: Option<String>,
pub operators: Vec<String>,
pub minds: Vec<String>,
pub last_heard: Vec<(String, u64)>,
lines: Vec<Line>,
}
impl Fixture {
pub fn load(path: &Path) -> Result<Self> {
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<Self> {
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::<Result<_>>()?;
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<ChatPending> {
Ok(pending_for(&self.lines, &self.her, &[], |at| self.lines[at].new))
}
}
impl HeartbeatView for Fixture {
fn addressing(&self) -> Result<Option<Addressing>> {
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<Value>, seed: u64, renders: Option<usize>, 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<StateEntry>,
#[serde(rename = "scenario")]
scenarios: Vec<ScenarioEntry>,
}
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<PathBuf>,
}
#[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<String> {
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<Value>)> = 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::<Result<_>>()?;
let scenarios: Vec<(String, Fixture)> = plan.scenarios.iter().map(|entry| Ok((entry.name.clone(), Fixture::load(&relative(plan_path, &entry.room))?))).collect::<Result<_>>()?;
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<u64>| mine.iter().filter_map(|case| key(render_of(case))).sum::<u64>() 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<String>)> = 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::<Vec<_>>()));
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<details><summary>packet</summary>\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, "</details>\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<String, BTreeMap<String, u64>> {
fn walk(value: &Value, into: &mut BTreeMap<String, BTreeMap<String, u64>>) {
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, BTreeMap<String, u64>>) -> 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<String, f64> {
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<String, f64>| counts.values().map(|count| count * count).sum::<f64>().sqrt();
let denominator = norm(&a) * norm(&b);
if denominator == 0.0 { 0.0 } else { dot / denominator }
}
#[cfg(test)]
pub(crate) mod tests;

View file

@ -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<Vec<(String, String)>>,
pub persona: Box<dyn Fn(&str) -> 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<Answer, Failed> {
self.calls.borrow_mut().push((model.to_owned(), prompt.to_owned()));
let text = if prompt.starts_with("<!-- membrane:") { "The room is slow and so is he.".to_owned() } else { (self.persona)(prompt) };
Ok(Answer { text, usage: Some(Spent { model: model.to_owned(), input_tokens: 10, output_tokens: 2, cache_read_tokens: 1, cache_write_tokens: 0, reasoning_tokens: None }) })
}
}
/// Fails the test if her stored state is asked for.
pub(crate) struct NeverAsked;
impl PersonaSource for NeverAsked {
fn persona(&self) -> Result<(Value, String)> {
panic!("the stored state was read")
}
}
fn harness<'a>(dir: &Path, persona: Option<&'a dyn PersonaSource>, model: Option<&'a dyn Model>) -> Harness<'a> {
Harness { instance: "eureka".into(), persona, model, tuning: dir.join("persona-tuning.toml") }
}
#[test]
fn a_room_fixture_parses_into_the_mouths_pending_read() {
let fixture = room("operator-warm.toml");
assert_eq!((fixture.her.as_str(), fixture.kind, fixture.here.as_deref()), (HER, TurnKind::Chat, Some("!dm-meta:gamecult.org")));
let pending = MouthReader::pending(&fixture).unwrap();
assert_eq!(pending.deliveries.len(), 1, "only the new message is delivered");
let dm = &pending.deliveries[0];
assert_eq!((dm.sender.as_str(), dm.sender_display.as_str(), dm.is_direct, dm.basis), ("@meta:gamecult.org", "Meta", true, Some(Basis::Direct)));
assert!(dm.text.contains("\"busy\""), "a quoted body survives TOML: {}", dm.text);
assert_eq!(dm.at_unix_ms, unix_ms(fixture.now - Duration::minutes(1)));
assert_eq!(pending.tail.unwrap().len(), 2, "the tail holds every line, delivered or not");
assert_eq!(fixture.last_heard, vec![("@meta:gamecult.org".to_owned(), unix_ms(fixture.now - Duration::days(2)))]);
}
#[test]
fn is_for_decides_whether_a_fixture_message_is_for_her() {
let her = Addressee::new(HER, ["eureka"]);
let for_her = |fixture: &Fixture| MouthReader::pending(fixture).unwrap().deliveries.iter().map(|delivery| is_for(&her, delivery)).collect::<Vec<_>>();
// Kiko's reply tags her and answers her own line.
let teased = room("mind-teases.toml");
let delivery = &MouthReader::pending(&teased).unwrap().deliveries[0];
assert_eq!((delivery.basis, delivery.mentions.as_slice()), (Some(Basis::Tag), [HER.to_owned()].as_slice()));
assert_eq!(for_her(&teased), vec![true]);
// Deru's aside is not.
assert_eq!(for_her(&room("deru-passing.toml")), vec![false]);
// A name in the text is, a reply to her line is, and an aside is not.
let fixture = Fixture::parse(
r##"
her = "@eureka:gamecult.org"
kind = "chat"
now = "2026-11-20T15:00:00Z"
rooms = ["#minds:gamecult.org"]
[[message]]
from = "@eureka:gamecult.org"
room = "#minds:gamecult.org"
ago = "1h"
says = "mine"
[[message]]
from = "@deru:gamecult.org"
room = "#minds:gamecult.org"
new = true
says = "Eureka, tea?"
[[message]]
from = "@deru:gamecult.org"
room = "#minds:gamecult.org"
new = true
reply_to = 1
says = "agreed"
[[message]]
from = "@deru:gamecult.org"
room = "#minds:gamecult.org"
new = true
says = "the kettle again"
"##,
)
.unwrap();
assert_eq!(for_her(&fixture), vec![true, true, false]);
}
#[test]
fn a_fixture_that_is_not_a_room_is_refused_naming_what_is_wrong() {
let head = "her = \"@eureka:gamecult.org\"\nkind = \"chat\"\nnow = \"2026-11-20T15:00:00Z\"\nrooms = [\"#a:x\"]\n";
for (tail, names) in [
("[[message]]\nfrom = \"@d:x\"\nroom = \"#b:x\"\nsays = \"hi\"\n", "not one of the rooms"),
("[[message]]\nfrom = \"@d:x\"\nroom = \"#a:x\"\nreply_to = 1\nsays = \"hi\"\n", "not an earlier one"),
("[[message]]\nfrom = \"@d:x\"\nroom = \"#a:x\"\nround = 2\nsays = \"hi\"\n", "round"),
("[[message]]\nfrom = \"@d:x\"\nroom = \"#a:x\"\nago = \"soon\"\nsays = \"hi\"\n", "90s"),
("[[message]]\nfrom = \"deru\"\nroom = \"#a:x\"\nsays = \"hi\"\n", "Matrix id"),
("mood = \"sunny\"\n", "unknown field"),
] {
let fault = format!("{:#}", Fixture::parse(&format!("{head}{tail}")).unwrap_err());
assert!(fault.contains(names), "{tail}: {fault}");
}
}
#[test]
fn would_say_is_the_quoted_spans_of_her_prose() {
assert_eq!(would_say("She looks up. \"Fine,\" she says, then \u{201C}mostly.\u{201D}"), vec!["Fine,", "mostly."]);
assert!(would_say("She says nothing, and means it.").is_empty());
assert!(would_say("She starts: \"well").is_empty(), "an unclosed quote says nothing");
assert!(would_say("\"\" and \" \"").is_empty());
}
#[test]
fn through_persona_continues_the_narration_in_her_prose_and_reports_what_she_would_say() {
let dir = tempfile::tempdir().unwrap();
let model = Scripted::new(|_| "He smiles at the screen. \"Missed you too, friend,\" he types. He does not mention the kettle.".into());
let fixture = room("operator-warm.toml");
let harness = harness(dir.path(), Some(&NeverAsked), Some(&model));
let organs = Organs { persona: harness.persona, chat: Some(&fixture), addressing: fixture.addressee(), model: harness.model };
let report = project(&organs, &fixture_request(&harness, &fixture, Some(control()), 3, Some(1), Through::Persona), fixture.now).unwrap();
let render = &report["renders"][0];
assert_eq!(render["persona"]["would_say"], json!(["Missed you too, friend,"]));
assert_eq!(render["persona"]["words"], 17);
assert_eq!((render["usage"]["input_tokens"].as_u64(), render["persona"]["usage"]["model"].as_str()), (Some(10), Some("claude-sonnet-5-5")));
assert_eq!(report["persona_model"], "claude-sonnet-5-5");
let calls = model.calls.borrow();
assert_eq!(calls.len(), 2, "one Projector call and one Persona call");
let persona_prompt = &calls[1].1;
assert!(persona_prompt.starts_with("Everything below is prose from inside one person's life."), "Ghostlight's Persona membrane");
assert!(persona_prompt.contains("The room is slow and so is he.") && persona_prompt.contains("hey you. I missed talking to you this week"), "the narration, then what she heard verbatim: {persona_prompt}");
assert!(persona_prompt.contains("Use at most 180 words"), "the map's Persona budget");
for field in ["socialBonds", "currentActivation", "bond-operator", "trust\":"] {
assert!(!persona_prompt.contains(field), "no field name reaches the Persona: {field}");
}
}
#[test]
fn prose_over_budget_is_cut_and_silence_says_nothing() {
let dir = tempfile::tempdir().unwrap();
let long = format!("{} \"late words\"", "word ".repeat(200));
let model = Scripted::new(move |_| long.clone());
let fixture = room("deru-passing.toml");
let harness = harness(dir.path(), None, Some(&model));
let organs = Organs { persona: None, chat: Some(&fixture), addressing: None, model: harness.model };
let report = project(&organs, &fixture_request(&harness, &fixture, Some(control()), 1, Some(1), Through::Persona), fixture.now).unwrap();
let persona = &report["renders"][0]["persona"];
assert_eq!((persona["words"].as_u64(), persona["cut_at_budget"].as_bool()), (Some(180), Some(true)));
assert_eq!(persona["would_say"], json!([]), "the quote past the budget is never said");
}
#[test]
fn a_batch_runs_every_state_by_scenario_by_sample_into_one_report() {
let dir = tempfile::tempdir().unwrap();
let plan = dir.path().join("plan.toml");
let rooms = fixtures().join("rooms");
std::fs::write(
&plan,
format!(
"samples = 2\nseed = 7\nthrough = \"persona\"\n[[state]]\nname = \"control\"\nfile = {:?}\n[[scenario]]\nname = \"quiet\"\nroom = {:?}\n[[scenario]]\nname = \"teased\"\nroom = {:?}\n",
fixtures().join("states/control.json").display().to_string(),
rooms.join("quiet-beat.toml").display().to_string(),
rooms.join("mind-teases.toml").display().to_string()
),
)
.unwrap();
let model = Scripted::new(|prompt| if prompt.contains("Kiko") { "\"Winter, still.\" He grins.".into() } else { "He lets the hour pass.".into() });
let report = batch(&harness(dir.path(), Some(&NeverAsked), Some(&model)), &plan).unwrap();
for heading in ["# Persona seed batch", "## Summary", "## control × quiet, sample 1 (seed 7)", "## control × quiet, sample 2 (seed 8)", "## control × teased, sample 2 (seed 8)", "## Total usage"] {
assert!(report.contains(heading), "{heading}\n{report}");
}
assert!(report.contains("| control | teased | 2/2 |") && report.contains("| control | quiet | 0/2 |"), "{report}");
assert!(report.contains("- \"Winter, still.\"") && report.contains("**Would say:** nothing (silence)"), "{report}");
assert!(report.contains("| claude-sonnet-5-5 | 8 | 80 | 16 |"), "eight calls, two a case, in the footer: {report}");
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1, "the batch wrote nothing; the caller writes the report");
}
#[test]
fn a_batch_state_without_a_file_is_her_stored_state() {
struct Stored;
impl PersonaSource for Stored {
fn persona(&self) -> Result<(Value, String)> {
Ok((control(), "mind-commit-x".into()))
}
}
let dir = tempfile::tempdir().unwrap();
let plan = dir.path().join("plan.toml");
std::fs::write(&plan, format!("[[state]]\nname = \"stored\"\n[[scenario]]\nname = \"quiet\"\nroom = {:?}\n", fixtures().join("rooms/quiet-beat.toml").display().to_string())).unwrap();
let model = Scripted::new(|_| String::new());
let report = batch(&harness(dir.path(), Some(&Stored), Some(&model)), &plan).unwrap();
assert!(report.contains("## stored × quiet, sample 1") && !report.contains("failed"), "{report}");
}

View file

@ -1,4 +1,5 @@
//! The Projector's narration, and the judge that measures it: one tool-less connector call each.
//! The Projector's narration, the Persona stage's continuation, and the judge that measures them: one tool-less
//! connector call each.
//!
//! `KitModel` is the stream organ's `KitProjector`, copied here when the stream was parked
//! (`persona-only-divergence-parked`), so the Projector has one caller on the connector.
@ -10,7 +11,7 @@ use std::time::Duration;
use anyhow::{Result, anyhow, bail};
use chrono::Utc;
use ghostlight_persona_projection::{ProjectorPrompt, build_projector_prompt};
use ghostlight_persona_projection::{PersonaPrompt, ProjectorPrompt, build_persona_prompt, build_projector_prompt};
use model_connector::{ConnectorClient, InputItem, ProviderRequest, ReasoningEffort, StopReason, TransportDisposition, TransportEventPayload, TransportInvocation, TransportOutcome};
use serde::Serialize;
use sha2::{Digest, Sha256};
@ -173,6 +174,44 @@ fn word_starts(text: &str) -> Vec<usize> {
/// The share of the budget a sentence cut must keep; below it the word cut keeps more of the stream.
const SENTENCE_CUT_FLOOR: f64 = 0.6;
// ---------------------------------------------------------------------------------------------
// The Persona stage.
/// What the Persona is shown as her moment: the Projector's narration, then what was said to her and in her room,
/// verbatim, as code rendered it (map section 3.2: the Projector never paraphrases what she heard).
pub(crate) fn lived_stream(narration: &str, heard: &str) -> String {
if heard.trim().is_empty() { narration.to_owned() } else { format!("{narration}\n\n{heard}") }
}
/// The Persona stage's prompt: Ghostlight's prose-only membrane over her identity and her lived stream. No field name,
/// number, schema or instruction of hers reaches it (invariant 7); the guidance is the tuning's setting line.
pub(crate) fn persona_prompt(packet: &Packet, narration: &str, tuning: &Tuning) -> String {
build_persona_prompt(&PersonaPrompt { identity: &packet.identity, lived_stream: &lived_stream(narration, &packet.heard), domain_guidance: tuning.persona.guidance.trim(), word_budget: tuning.persona.words })
}
/// The words her prose says aloud: each span inside straight or curly double quotation marks, in prose order (map
/// section 5.5: speech needs quoted spoken words). A preview of what the Interpreter (C3b) will be asked to quote, not
/// its decision: a quotation she only recalls reads here as said. An unclosed quote says nothing.
pub(crate) fn would_say(prose: &str) -> Vec<String> {
let mut said = Vec::new();
let mut open: Option<(char, usize)> = None;
for (at, c) in prose.char_indices() {
match (open, c) {
(None, '"') => open = Some(('"', at + 1)),
(None, '\u{201C}') => open = Some(('\u{201D}', at + c.len_utf8())),
(Some((close, start)), c) if c == close => {
let span = prose[start..at].trim();
if !span.is_empty() {
said.push(span.to_owned());
}
open = None;
}
_ => {}
}
}
said
}
/// `text` held to `max` words (whitespace-separated, each CJK character one); the model's count is not trusted. An
/// overrun is cut at the last sentence end inside the budget, so the stream ends on a whole sentence, when that keeps
/// at least 60% of the budget; otherwise, or with no sentence end inside it, it is cut after the `max`-th word. No

View file

@ -48,7 +48,7 @@ const NAME_BYTES: usize = 80;
const ALSO_HERE_BYTES: usize = 300;
/// What kind of turn the packet is for. The stimulus and nothing else differs between them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TurnKind {
Chat,
@ -75,6 +75,9 @@ pub(crate) struct Room {
pub tail: Vec<RoomLine>,
/// The room the turn is in, when it is one room.
pub here: Option<String>,
/// When she last heard from people, by Matrix id, beyond what the tail shows: a fixture's table (`mind-persona project
/// --room`). The live turn has none yet; the tail is its only source.
pub last_heard: Vec<(String, u64)>,
}
/// One thing she said, from the trace.
@ -352,10 +355,10 @@ fn need_movement(need: &Value, previous: Option<&Value>) -> String {
// ---------------------------------------------------------------------------------------------
// The seeded draw: splitmix64, so a turn id replays.
struct Draw(u64);
pub(crate) struct Draw(pub(crate) u64);
impl Draw {
fn next(&mut self) -> u64 {
pub(crate) fn next(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
@ -711,7 +714,8 @@ fn beat_stimulus(inputs: &Inputs, tuning: &Tuning) -> String {
for bond in items(inputs.state, &["affect", "socialBonds"]).iter().filter(|bond| live(bond) && text(bond, &["object", "kind"]) == "person") {
let person = text(bond, &["object", "id"]).to_lowercase();
let label = clip(&one_line([text(bond, &["object", "label"]), text(bond, &["object", "id"])].into_iter().find(|name| !name.is_empty()).unwrap_or_default()), NAME_BYTES);
let last = inputs.room.tail.iter().filter(|line| inputs.people.person_of(&line.sender) == person).map(|line| line.at_ms).max();
let heard = inputs.room.tail.iter().map(|line| (line.sender.as_str(), line.at_ms)).chain(inputs.room.last_heard.iter().map(|(sender, at)| (sender.as_str(), *at)));
let last = heard.filter(|(sender, _)| inputs.people.person_of(sender) == person).map(|(_, at)| at).max();
contacts.push((last, match last {
Some(at) => format!("- last heard from {label} {} h ago", hours(inputs.now, at)),
None => format!("- nothing from {label} in her rooms lately"),

View file

@ -0,0 +1,403 @@
//! The swarm in one simulated room (`mind-persona project --simulate <plan>`): up to eight personas take turns, each turn
//! one `project` through the Persona stage, and each one's quoted lines join the room as from her Matrix id, so the
//! next one hears them.
//!
//! Who wakes is the real rule: `address::is_for` over each persona's names, plus ruling
//! `persona-only-wake-humans-in-rooms` (an addressed message, any human message in her rooms, the beat). Another Mind's
//! unaddressed message wakes no one, unless the plan asks for the swarm's old loop (`wake = "all_messages"`), kept as a
//! control. Those woken in one round go in an order drawn from the seed, each against the room as it stands at her turn.
//!
//! Her state does not move during a run: the Interpreter (C3b) and the commit (C3c) are not built. `state_after_turn`
//! is the seam where they will.
use std::collections::BTreeMap;
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail};
use chrono::{DateTime, Duration, Utc};
use serde::Deserialize;
use serde_json::{Value, json};
use super::harness::{Harness, Line, Message, cosine, pending_for, state_file, turn_markdown, usage_markdown, usage_total};
use super::select::Draw;
use super::{MouthReader, Organs, Request, Through, TurnKind, project};
use crate::address::{Addressee, is_for, localpart};
use crate::chat_client::ChatPending;
use crate::heartbeat::{Addressing, HeartbeatView};
/// The most personas a simulation holds: the swarm.
const CAST_MAX: usize = 8;
/// Who a message wakes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
enum Wake {
/// The ruled set: a message for her, any human message in her rooms, the beat.
#[default]
HumansInRooms,
/// The swarm's old loop, as a control: any message in her rooms, a Mind's too.
AllMessages,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SimFile {
rounds: usize,
#[serde(default = "one")]
repeats: usize,
#[serde(default)]
seed: u64,
#[serde(default)]
wake: Wake,
/// Every this many rounds the beat wakes everyone who was not woken otherwise; 0 is no beat.
#[serde(default)]
beat_every: usize,
#[serde(default = "two")]
round_minutes: i64,
start: DateTime<Utc>,
rooms: Vec<String>,
#[serde(default)]
operators: Vec<String>,
cast: Vec<CastEntry>,
#[serde(default, rename = "message")]
script: Vec<Message>,
}
fn one() -> usize {
1
}
fn two() -> i64 {
2
}
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields)]
struct CastEntry {
id: String,
name: String,
/// Her persona state, relative to the plan; its `personaId` is her id's localpart.
state: PathBuf,
/// Her rooms; none is every room of the plan.
#[serde(default)]
rooms: Vec<String>,
/// More names she answers to, beside her handle and display name.
#[serde(default)]
names: Vec<String>,
}
struct Member {
entry: CastEntry,
state: Value,
addressee: Addressee,
rooms: Vec<String>,
}
/// One persona's view of the room at her turn: the lines since her last turn are new.
struct View<'a> {
lines: &'a [Line],
viewer: &'a str,
rooms: &'a [String],
seen: usize,
}
impl MouthReader for View<'_> {
fn pending(&self) -> Result<ChatPending> {
Ok(pending_for(self.lines, self.viewer, self.rooms, |at| at >= self.seen))
}
}
struct Names<'a>(&'a Addressee);
impl HeartbeatView for Names<'_> {
fn addressing(&self) -> Result<Option<Addressing>> {
Ok(Some(Addressing { minds: vec![self.0.clone()] }))
}
}
/// The seam where the Interpreter (C3b) will turn her prose into proposals and the commit (C3c) will apply them, so her
/// next turn starts from the state this one left. Until they exist it returns her state unchanged.
fn state_after_turn(state: Value, _turn: &Value) -> Value {
state
}
/// Why she wakes this round, by the plan's rule; none leaves her asleep.
fn wake_reason(lines: &[Line], seen: usize, member: &Member, cast: &[String], wake: Wake, beat: bool) -> Option<&'static str> {
let mut reason = None;
for (at, line) in lines.iter().enumerate().skip(seen) {
if line.from == member.entry.id || !(member.rooms.contains(&line.room) || line.dm) {
continue;
}
if is_for(&member.addressee, &line.delivery(at, &member.entry.id, lines)) {
return Some("addressed");
}
if !cast.contains(&line.from) {
reason = Some("a human in her rooms");
} else if wake == Wake::AllMessages && reason.is_none() {
reason = Some("a message in her rooms");
}
}
reason.or(beat.then_some("the beat"))
}
/// A seeded shuffle, so the order of those woken together is drawn, and replays.
fn shuffled<T>(mut items: Vec<T>, seed: u64) -> Vec<T> {
let mut draw = Draw(seed);
for at in (1..items.len()).rev() {
let pick = (draw.next() % (at as u64 + 1)) as usize;
items.swap(at, pick);
}
items
}
/// Runs the plan: every repeat one simulation, all of them in one Markdown report.
pub fn simulate(harness: &Harness, plan_path: &Path) -> Result<String> {
let text = std::fs::read_to_string(plan_path).with_context(|| format!("the plan {} could not be read", plan_path.display()))?;
let plan: SimFile = toml::from_str(&text).with_context(|| format!("the plan {} is not a simulation plan", plan_path.display()))?;
if plan.cast.is_empty() || plan.cast.len() > CAST_MAX {
bail!("a simulation holds 1 to {CAST_MAX} personas, not {}", plan.cast.len());
}
if plan.rounds == 0 || plan.repeats == 0 || plan.rooms.is_empty() || plan.round_minutes <= 0 {
bail!("a simulation needs rounds, repeats, a room and a positive round_minutes");
}
let base = plan_path.parent().unwrap_or(Path::new("."));
let mut members = Vec::new();
for entry in &plan.cast {
let state = state_file(&base.join(&entry.state), localpart(&entry.id))?;
let rooms = if entry.rooms.is_empty() { plan.rooms.clone() } else { entry.rooms.clone() };
let public = state["publicName"].as_str().unwrap_or_default().to_owned();
let names = [localpart(&entry.id), entry.name.as_str(), public.as_str()].into_iter().chain(entry.names.iter().map(String::as_str)).filter(|name| !name.is_empty());
members.push(Member { addressee: Addressee::new(&entry.id, names), entry: entry.clone(), state, rooms });
}
for (at, message) in plan.script.iter().enumerate() {
if message.ago.is_some() || message.new || message.tag {
bail!("script message {} uses a room fixture's ago, new or tag: a script names its round and its mentions", at + 1);
}
if !plan.rooms.contains(&message.room) {
bail!("script message {} is in {}, which is not one of the rooms", at + 1, message.room);
}
}
let runs: Vec<Value> = (0..plan.repeats).map(|repeat| run(harness, &plan, &mut members, repeat)).collect::<Result<_>>()?;
Ok(markdown(plan_path, &plan, &runs))
}
fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) -> Result<Value> {
let seed = plan.seed.wrapping_add(repeat as u64);
let cast: Vec<String> = members.iter().map(|member| member.entry.id.clone()).collect();
let at_round = |round: usize| plan.start + Duration::minutes(plan.round_minutes * round as i64);
let mut lines: Vec<Line> = Vec::new();
let mut rounds_of: Vec<usize> = Vec::new();
let scripted = |round: usize, lines: &mut Vec<Line>, rounds_of: &mut Vec<usize>| -> Result<()> {
for (at, message) in plan.script.iter().enumerate().filter(|(_, message)| message.round.unwrap_or(0) == round) {
lines.push(Line::of(message, at_round(round) + Duration::seconds(at as i64), None, at)?);
rounds_of.push(round);
}
Ok(())
};
scripted(0, &mut lines, &mut rounds_of)?;
let opening = lines.len();
let mut seen = vec![0usize; members.len()];
let mut recent: Vec<Vec<Vec<String>>> = vec![Vec::new(); members.len()];
let mut turns = Vec::new();
let mut counter = 0u64;
for round in 1..=plan.rounds {
scripted(round, &mut lines, &mut rounds_of)?;
let beat = plan.beat_every > 0 && round % plan.beat_every == 0;
let woken: Vec<(usize, &'static str)> = members.iter().enumerate().filter_map(|(at, member)| wake_reason(&lines, seen[at], member, &cast, plan.wake, beat).map(|why| (at, why))).collect();
for (at, why) in shuffled(woken, seed ^ (round as u64).wrapping_mul(0x9E37_79B9)) {
counter += 1;
let member = &members[at];
let id = member.entry.id.clone();
let fresh: Vec<&Line> = lines[seen[at]..].iter().filter(|line| line.from != id && (member.rooms.contains(&line.room) || line.dm)).collect();
let here = fresh.iter().rev().find(|line| line.mentions.contains(&id)).or(fresh.last()).map_or_else(|| member.rooms[0].clone(), |line| line.room.clone());
let kind = if fresh.is_empty() { TurnKind::Beat } else { TurnKind::Chat };
let now = at_round(round) + Duration::seconds(30 + counter as i64);
let request = Request {
instance: localpart(&id).to_owned(),
kind,
state: Some(member.state.clone()),
room: Some(here.clone()),
seed: seed.wrapping_mul(1_000_003).wrapping_add(counter),
renders: Some(1),
judge_against: None,
operators: plan.operators.clone(),
minds: cast.clone(),
own_sender: id.clone(),
tuning: harness.tuning.clone(),
through: Through::Persona,
last_heard: vec![],
recent: recent[at].clone(),
};
let view = View { lines: &lines, viewer: &id, rooms: &member.rooms, seen: seen[at] };
let names = Names(&member.addressee);
let organs = Organs { persona: None, chat: Some(&view), addressing: Some(&names), model: harness.model };
let report = project(&organs, &request, now).unwrap_or_else(|fault| json!({ "error": format!("{fault:#}") }));
seen[at] = lines.len();
let render = &report["renders"][0];
if let Some(selected) = render["selected"].as_array() {
recent[at].push(selected.iter().filter_map(Value::as_str).map(str::to_owned).collect());
}
let said: Vec<String> = render["persona"]["would_say"].as_array().map(|said| said.iter().filter_map(Value::as_str).map(str::to_owned).collect()).unwrap_or_default();
let line = (!said.is_empty()).then(|| said.join(" "));
if let Some(says) = &line {
lines.push(Line { from: id.clone(), name: member.entry.name.clone(), room: here.clone(), at_ms: u64::try_from(now.timestamp_millis()).unwrap_or_default(), says: says.clone(), mentions: vec![], dm: false, follow_up: false, reply_to: None, new: false });
rounds_of.push(round);
}
let turn = json!({ "round": round, "persona": member.entry.name, "id": id, "woke": why, "kind": kind, "room": here, "said": line, "report": report });
let member = &mut members[at];
member.state = state_after_turn(std::mem::take(&mut member.state), &turn);
turns.push(turn);
}
}
let metrics = metrics(members, &lines, &rounds_of, opening, &turns, plan.rounds);
let transcript: Vec<Value> = lines.iter().zip(&rounds_of).map(|(line, round)| json!({ "round": round, "room": line.room, "from": line.from, "name": line.name, "says": line.says, "mind": cast.contains(&line.from) })).collect();
Ok(json!({ "repeat": repeat + 1, "seed": seed, "transcript": transcript, "turns": turns, "metrics": metrics }))
}
/// Words that mark talk about the work. A cheap lexical heuristic for the work-talk share, not a classifier: it counts
/// a line as work talk when any of these appears as a word in it.
const WORK_WORDS: [&str; 26] = [
"repo", "repos", "pr", "commit", "merge", "deploy", "build", "test", "tests", "bug", "branch", "code", "release", "issue", "pipeline", "crate", "schema", "spec", "cargo", "clippy", "daemon", "patch", "refactor", "migration", "server", "ticket",
];
fn work_talk(text: &str) -> bool {
text.split(|c: char| !c.is_alphanumeric()).any(|word| WORK_WORDS.contains(&word.to_lowercase().as_str()))
}
/// Mean pairwise cosine between the speakers' texts; speakers with no text are left out.
fn mean_pairwise(texts: &BTreeMap<String, String>) -> Option<f64> {
let spoken: Vec<&String> = texts.values().filter(|text| !text.trim().is_empty()).collect();
let mut sum = 0.0;
let mut pairs = 0usize;
for (at, a) in spoken.iter().enumerate() {
for b in &spoken[at + 1..] {
sum += cosine(a, b);
pairs += 1;
}
}
(pairs > 0).then(|| sum / pairs as f64)
}
/// Which rounds a measure keeps.
type Keep<'a> = &'a dyn Fn(usize) -> bool;
fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], opening: usize, turns: &[Value], rounds: usize) -> Value {
let cast: Vec<&str> = members.iter().map(|member| member.entry.id.as_str()).collect();
let mut per = BTreeMap::new();
for member in members {
let mine: Vec<&Value> = turns.iter().filter(|turn| turn["id"] == member.entry.id.as_str()).collect();
let spoke = mine.iter().filter(|turn| !turn["said"].is_null()).count();
let mut addressed = BTreeMap::new();
for (at, line) in lines.iter().enumerate().skip(opening).filter(|(_, line)| line.from == member.entry.id) {
for other in members.iter().filter(|other| other.entry.id != member.entry.id) {
if is_for(&other.addressee, &line.delivery(at, &other.entry.id, lines)) {
*addressed.entry(other.entry.name.clone()).or_insert(0u64) += 1;
}
}
}
per.insert(member.entry.name.clone(), json!({ "turns": mine.len(), "messages": spoke, "silence_rate": if mine.is_empty() { Value::Null } else { json!(1.0 - spoke as f64 / mine.len() as f64) }, "addressed": addressed }));
}
// Mind-to-Mind chains: runs of Mind lines with no human line between them.
let (mut longest, mut long_chains, mut run) = (0usize, 0usize, 0usize);
for line in &lines[opening..] {
if cast.contains(&line.from.as_str()) {
run += 1;
longest = longest.max(run);
} else {
long_chains += usize::from(run >= 3);
run = 0;
}
}
long_chains += usize::from(run >= 3);
let half = rounds.div_ceil(2);
let spoken_by = |keep: &dyn Fn(usize) -> bool| -> BTreeMap<String, String> {
let mut texts = BTreeMap::new();
for (line, _) in lines.iter().zip(rounds_of).skip(opening).filter(|(line, round)| cast.contains(&line.from.as_str()) && keep(**round)) {
let text: &mut String = texts.entry(line.from.clone()).or_default();
text.push(' ');
text.push_str(&line.says);
}
texts
};
let prose_by = |keep: &dyn Fn(usize) -> bool| -> BTreeMap<String, String> {
let mut texts = BTreeMap::new();
for turn in turns.iter().filter(|turn| turn["round"].as_u64().is_some_and(|round| keep(round as usize))) {
let text: &mut String = texts.entry(turn["id"].as_str().unwrap_or_default().to_owned()).or_default();
text.push(' ');
text.push_str(turn["report"]["renders"][0]["persona"]["prose"].as_str().unwrap_or_default());
}
texts
};
let (all, early, late): (Keep, Keep, Keep) = (&|_| true, &|round| round <= half, &|round| round > half);
let mind_lines: Vec<&Line> = lines[opening..].iter().filter(|line| cast.contains(&line.from.as_str())).collect();
let work = mind_lines.iter().filter(|line| work_talk(&line.says)).count();
json!({
"personas": per,
"turns": turns.len(),
"mind_messages": mind_lines.len(),
"silence_rate": if turns.is_empty() { Value::Null } else { json!(turns.iter().filter(|turn| turn["said"].is_null()).count() as f64 / turns.len() as f64) },
"mind_chain_longest": longest,
"mind_chains_of_3_or_more": long_chains,
"convergence_lines": { "all": mean_pairwise(&spoken_by(all)), "first_half": mean_pairwise(&spoken_by(early)), "second_half": mean_pairwise(&spoken_by(late)) },
"convergence_prose": { "all": mean_pairwise(&prose_by(all)), "first_half": mean_pairwise(&prose_by(early)), "second_half": mean_pairwise(&prose_by(late)) },
"work_talk_share": if mind_lines.is_empty() { Value::Null } else { json!(work as f64 / mind_lines.len() as f64) },
})
}
fn number(value: &Value) -> String {
value.as_f64().map_or_else(|| "n/a".into(), |number| format!("{number:.3}"))
}
fn markdown(plan_path: &Path, plan: &SimFile, runs: &[Value]) -> String {
let mut out = String::new();
let _ = writeln!(out, "# Swarm simulation\n");
let _ = writeln!(out, "> **State does not evolve during a run.** The Interpreter (C3b) and the commit (C3c) are not built, so every persona starts each turn from the state the plan gave her. `state_after_turn` in `projection/simulate.rs` is the seam where they will move it between rounds. Freshness carries over: each persona's earlier packets in the run count against repeating an item.\n");
let _ = writeln!(out, "- plan: `{}`", plan_path.display());
let _ = writeln!(out, "- cast: {}", plan.cast.iter().map(|entry| format!("{} (`{}`, `{}`)", entry.name, entry.id, entry.state.display())).collect::<Vec<_>>().join(", "));
let _ = writeln!(out, "- rooms: {}; rounds: {}; repeats: {}; seed: {}; minutes a round: {}", plan.rooms.join(", "), plan.rounds, plan.repeats, plan.seed, plan.round_minutes);
let _ = writeln!(out, "- wake: {}; beat: {}", match plan.wake { Wake::HumansInRooms => "ruled set (addressed, any human message in her rooms, the beat; another Mind's unaddressed message wakes no one)", Wake::AllMessages => "control: any message in her rooms wakes her, a Mind's too (the old loop)" }, if plan.beat_every == 0 { "off".to_owned() } else { format!("every {} rounds", plan.beat_every) });
let _ = writeln!(out, "- speech: the double-quoted spans of her prose, joined, posted to the room she woke in; none is silence. A preview of the Interpreter's speech, not its decision");
let _ = writeln!(out, "- convergence is the mean pairwise cosine of speakers' word counts (lexical, cheap); work-talk share is a lexical heuristic over a fixed word list, not a classifier");
let _ = writeln!(out, "- read-only: nothing is posted, no state is put, no Mind is asked\n");
for run in runs {
let metrics = &run["metrics"];
let _ = writeln!(out, "## Repeat {} (seed {})\n", run["repeat"], run["seed"]);
let _ = writeln!(out, "### Metrics\n");
let _ = writeln!(out, "- turns: {}; Mind messages: {}; silence rate: {}", metrics["turns"], metrics["mind_messages"], number(&metrics["silence_rate"]));
let _ = writeln!(out, "- longest Mind-to-Mind chain with no human between: {}; chains of 3 or more: {}", metrics["mind_chain_longest"], metrics["mind_chains_of_3_or_more"]);
let _ = writeln!(out, "- convergence of spoken lines: all {}, first half {}, second half {}", number(&metrics["convergence_lines"]["all"]), number(&metrics["convergence_lines"]["first_half"]), number(&metrics["convergence_lines"]["second_half"]));
let _ = writeln!(out, "- convergence of prose: all {}, first half {}, second half {}", number(&metrics["convergence_prose"]["all"]), number(&metrics["convergence_prose"]["first_half"]), number(&metrics["convergence_prose"]["second_half"]));
let _ = writeln!(out, "- work-talk share of Mind lines (heuristic): {}\n", number(&metrics["work_talk_share"]));
let _ = writeln!(out, "| persona | turns | messages | silence rate | addressed |\n|---|---|---|---|---|");
if let Some(personas) = metrics["personas"].as_object() {
for (name, row) in personas {
let addressed = row["addressed"].as_object().map(|map| map.iter().map(|(who, count)| format!("{who} {count}")).collect::<Vec<_>>().join(", ")).unwrap_or_default();
let _ = writeln!(out, "| {name} | {} | {} | {} | {addressed} |", row["turns"], row["messages"], number(&row["silence_rate"]));
}
}
let _ = writeln!(out, "\n### Transcript\n");
for line in run["transcript"].as_array().into_iter().flatten() {
let who = if line["mind"] == true { line["name"].as_str().unwrap_or_default().to_owned() } else { format!("{} (human)", line["name"].as_str().unwrap_or_default()) };
let _ = writeln!(out, "- r{} {} **{who}**: {}", line["round"], line["room"].as_str().unwrap_or_default(), line["says"].as_str().unwrap_or_default());
}
let _ = writeln!(out, "\n### Turns\n");
for turn in run["turns"].as_array().into_iter().flatten() {
let said = if turn["said"].is_null() { "silent".to_owned() } else { "spoke".to_owned() };
let _ = writeln!(out, "<details><summary>round {}, {} (woke: {}, {} turn, {}): {said}</summary>\n", turn["round"], turn["persona"].as_str().unwrap_or_default(), turn["woke"].as_str().unwrap_or_default(), turn["kind"].as_str().unwrap_or_default(), turn["room"].as_str().unwrap_or_default());
let report = &turn["report"];
if let Some(error) = report.get("error") {
let _ = writeln!(out, "**failed:** {}\n", error.as_str().unwrap_or_default());
} else {
let _ = writeln!(out, "Stimulus:\n\n````text\n{}\n````\n", report["packet"]["stimulus"].as_str().unwrap_or_default());
turn_markdown(&mut out, &report["renders"][0]);
}
let _ = writeln!(out, "</details>\n");
}
}
let _ = writeln!(out, "## Total usage\n\n{}", usage_markdown(&usage_total(runs)));
out
}
#[cfg(test)]
mod tests;

View file

@ -0,0 +1,173 @@
use std::path::Path;
use serde_json::Value;
use super::*;
use crate::projection::harness::tests::{Scripted, control};
const TOBIAH: &str = "@eureka:gamecult.org";
const LIBBY: &str = "@libby:gamecult.org";
/// A cast of two in a temp dir: the control character as Eureka, and a copy of it as Libby ("Libby Test").
fn cast(dir: &Path) {
std::fs::write(dir.join("tobiah.json"), control().to_string()).unwrap();
let mut libby = control();
libby["personaId"] = "libby".into();
libby["provenance"]["sourceDocumentId"] = "libby".into();
libby["publicName"] = "Libby Test".into();
std::fs::write(dir.join("libby.json"), libby.to_string()).unwrap();
}
fn plan(dir: &Path, wake: &str, rounds: usize, extra: &str) -> PathBuf {
cast(dir);
let path = dir.join("plan.toml");
std::fs::write(
&path,
format!(
r##"rounds = {rounds}
seed = 11
wake = "{wake}"
start = "2026-11-20T15:00:00Z"
rooms = ["#minds:gamecult.org"]
operators = ["@meta:gamecult.org"]
[[cast]]
id = "{TOBIAH}"
name = "Tobiah"
state = "tobiah.json"
[[cast]]
id = "{LIBBY}"
name = "Libby"
state = "libby.json"
[[message]]
from = "@meta:gamecult.org"
name = "Meta"
room = "#minds:gamecult.org"
says = "good afternoon, everyone"
{extra}"##
),
)
.unwrap();
path
}
/// Both speak every turn, neither naming the other: a Mind line wakes no one under the ruled set.
fn chatty() -> Scripted {
Scripted::new(|prompt| if prompt.contains("Who you are:\nTobiah") { "\"what a day\" he says".into() } else { "\"mm, the deploy went fine\" she says".into() })
}
fn run_plan(path: &Path, model: &Scripted) -> (SimFile, Vec<Member>, Value) {
let plan: SimFile = toml::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
let base = path.parent().unwrap();
let mut members: Vec<Member> = plan
.cast
.iter()
.map(|entry| {
let state = state_file(&base.join(&entry.state), localpart(&entry.id)).unwrap();
let public = state["publicName"].as_str().unwrap().to_owned();
Member { addressee: Addressee::new(&entry.id, [localpart(&entry.id), entry.name.as_str(), public.as_str()]), entry: entry.clone(), state, rooms: plan.rooms.clone() }
})
.collect();
let harness = Harness { instance: "eureka".into(), persona: None, model: Some(model), tuning: base.join("persona-tuning.toml") };
let run = run(&harness, &plan, &mut members, 0).unwrap();
(plan, members, run)
}
fn turns(run: &Value) -> Vec<(u64, String, String)> {
run["turns"].as_array().unwrap().iter().map(|turn| (turn["round"].as_u64().unwrap(), turn["persona"].as_str().unwrap().to_owned(), turn["woke"].as_str().unwrap().to_owned())).collect()
}
#[test]
fn under_the_ruled_wake_set_a_minds_unaddressed_line_wakes_no_one() {
let dir = tempfile::tempdir().unwrap();
let (_, _, run) = run_plan(&plan(dir.path(), "humans_in_rooms", 6, ""), &chatty());
let turns = turns(&run);
assert_eq!(turns.len(), 2, "the operator's opening wakes both in round 1, and then nothing does: {turns:?}");
assert!(turns.iter().all(|(round, _, why)| *round == 1 && why == "a human in her rooms"), "{turns:?}");
let transcript = run["transcript"].as_array().unwrap();
assert_eq!(transcript.len(), 3);
assert!(transcript.iter().any(|line| line["from"] == TOBIAH && line["says"] == "what a day" && line["mind"] == true), "her quoted words join the room as from her id: {transcript:?}");
}
#[test]
fn the_old_loop_control_lets_minds_wake_each_other_into_a_chain() {
let dir = tempfile::tempdir().unwrap();
let (_, _, run) = run_plan(&plan(dir.path(), "all_messages", 6, ""), &chatty());
let turns = turns(&run);
assert!(turns.len() >= 6, "every round someone answers a Mind: {turns:?}");
assert!(turns.iter().any(|(_, _, why)| why == "a message in her rooms"));
let metrics = &run["metrics"];
assert!(metrics["mind_chain_longest"].as_u64().unwrap() >= 5, "{metrics}");
assert!(metrics["mind_chains_of_3_or_more"].as_u64().unwrap() >= 1);
assert_eq!(metrics["silence_rate"].as_f64(), Some(0.0));
let work = metrics["work_talk_share"].as_f64().unwrap();
assert!(work > 0.3 && work < 0.7, "Libby's lines name the deploy, Tobiah's do not: {work}");
assert!(metrics["convergence_lines"]["all"].as_f64().unwrap() < 0.5, "two different lines are not one voice: {metrics}");
}
#[test]
fn a_scripted_interjection_wakes_everyone_in_her_room_and_addressing_is_counted() {
let dir = tempfile::tempdir().unwrap();
let interjection = "[[message]]\nround = 4\nfrom = \"@deru:gamecult.org\"\nname = \"Deru\"\nroom = \"#minds:gamecult.org\"\nsays = \"kettle news\"\n";
let model = Scripted::new(|prompt| if prompt.contains("Who you are:\nTobiah") { "\"Libby, tea?\"".into() } else { "She does not answer.".into() });
let (_, members, run) = run_plan(&plan(dir.path(), "humans_in_rooms", 5, interjection), &model);
let turns = turns(&run);
let fourth: Vec<&String> = turns.iter().filter(|(round, _, _)| *round == 4).map(|(_, who, _)| who).collect();
assert_eq!(fourth.len(), 2, "Deru's line at round 4 wakes both: {turns:?}");
assert_eq!(run["metrics"]["personas"]["Tobiah"]["addressed"]["Libby"].as_u64().unwrap(), run["metrics"]["personas"]["Tobiah"]["messages"].as_u64().unwrap());
assert_eq!(run["metrics"]["personas"]["Libby"]["silence_rate"].as_f64(), Some(1.0));
// The seam: no state moved.
assert_eq!(members[0].state, control());
}
#[test]
fn the_same_seed_replays_the_same_order() {
let dir = tempfile::tempdir().unwrap();
let path = plan(dir.path(), "all_messages", 4, "");
let (_, _, first) = run_plan(&path, &chatty());
let (_, _, second) = run_plan(&path, &chatty());
assert_eq!(turns(&first), turns(&second));
}
#[test]
fn the_report_names_the_state_gap_and_carries_the_transcript_and_usage() {
let dir = tempfile::tempdir().unwrap();
let model = chatty();
let harness = Harness { instance: "eureka".into(), persona: None, model: Some(&model), tuning: dir.path().join("persona-tuning.toml") };
let report = simulate(&harness, &plan(dir.path(), "humans_in_rooms", 3, "")).unwrap();
for part in ["# Swarm simulation", "State does not evolve during a run", "## Repeat 1 (seed 11)", "### Transcript", "**Meta (human)**: good afternoon, everyone", "**Tobiah**: what a day", "<details><summary>round 1, ", "## Total usage", "| claude-sonnet-5-5 | 4 |"] {
assert!(report.contains(part), "{part}\n{report}");
}
}
#[test]
fn a_cast_past_eight_or_a_foreign_state_is_refused() {
let dir = tempfile::tempdir().unwrap();
let model = chatty();
let harness = Harness { instance: "eureka".into(), persona: None, model: Some(&model), tuning: dir.path().join("persona-tuning.toml") };
let path = plan(dir.path(), "humans_in_rooms", 2, "");
let text = std::fs::read_to_string(&path).unwrap().replace("state = \"libby.json\"", "state = \"tobiah.json\"");
std::fs::write(&path, text).unwrap();
assert!(format!("{:#}", simulate(&harness, &path).unwrap_err()).contains("refused by a put"), "Libby cannot hold Eureka's state");
let many: String = (0..9).map(|at| format!("[[cast]]\nid = \"@m{at}:x\"\nname = \"M{at}\"\nstate = \"m.json\"\n")).collect();
std::fs::write(&path, format!("rounds = 1\nstart = \"2026-11-20T15:00:00Z\"\nrooms = [\"#a:x\"]\n{many}")).unwrap();
assert!(format!("{:#}", simulate(&harness, &path).unwrap_err()).contains("1 to 8"));
}
#[test]
fn wake_reason_is_the_ruled_set_and_the_control_widens_it_to_minds() {
let libby = Member { addressee: Addressee::new(LIBBY, ["libby", "Libby"]), entry: CastEntry { id: LIBBY.into(), name: "Libby".into(), state: PathBuf::new(), rooms: vec![], names: vec![] }, state: Value::Null, rooms: vec!["#minds:x".into()] };
let cast = vec![TOBIAH.to_owned(), LIBBY.to_owned()];
let line = |from: &str, room: &str, says: &str| Line { from: from.into(), name: String::new(), room: room.into(), at_ms: 0, says: says.into(), mentions: vec![], dm: false, follow_up: false, reply_to: None, new: false };
let reason = |lines: &[Line], wake: Wake, beat: bool| wake_reason(lines, 0, &libby, &cast, wake, beat);
let named = [line(TOBIAH, "#minds:x", "Libby, tea?")];
let unnamed = [line(TOBIAH, "#minds:x", "what a day")];
let human = [line("@deru:x", "#minds:x", "kettle news")];
let elsewhere = [line("@deru:x", "#other:x", "kettle news")];
assert_eq!(reason(&named, Wake::HumansInRooms, false), Some("addressed"));
assert_eq!(reason(&unnamed, Wake::HumansInRooms, false), None, "another Mind's unaddressed line wakes no one");
assert_eq!(reason(&unnamed, Wake::AllMessages, false), Some("a message in her rooms"));
assert_eq!(reason(&human, Wake::HumansInRooms, false), Some("a human in her rooms"));
assert_eq!(reason(&elsewhere, Wake::HumansInRooms, false), None, "not her room");
assert_eq!(reason(&unnamed, Wake::HumansInRooms, true), Some("the beat"));
assert_eq!(wake_reason(&named, 1, &libby, &cast, Wake::HumansInRooms, false), None, "a line she has seen is not new");
}

View file

@ -75,6 +75,7 @@ fn chat_room() -> Room {
messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", "Eureka, did you see the door fix?", 3, true), message("#swarm:gamecult.org", "@kiko:gamecult.org", "Kiko", "She saw it. She hated it.", 1, false)],
tail: vec![],
here: Some("#swarm:gamecult.org".into()),
..Room::default()
}
}
@ -281,12 +282,12 @@ fn freshness_base_zero_never_repeats_inside_the_window() {
#[test]
fn messages_addressed_to_her_appear_verbatim() {
let said = "Eureka — \"is the door\" fixed?\nTell me straight.";
let room = Room { messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", said, 2, true)], tail: vec![], here: None };
let room = Room { messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", said, 2, true)], tail: vec![], here: None, ..Room::default() };
let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1);
let escaped = "\"Eureka — \\\"is the door\\\" fixed?\\nTell me straight.\"";
assert_eq!(packet.heard, format!("Libby, in #swarm:gamecult.org: {escaped}"), "the Persona hears the exact words, escaped on one line");
assert!(packet.stimulus.starts_with("Said to her:\n") && packet.stimulus.contains(escaped), "{}", packet.stimulus);
let own = Room { messages: vec![message("#swarm:gamecult.org", OWN, "Eureka", "my own line", 1, false)], tail: vec![], here: None };
let own = Room { messages: vec![message("#swarm:gamecult.org", OWN, "Eureka", "my own line", 1, false)], tail: vec![], here: None, ..Room::default() };
let packet = packet_of(TurnKind::Chat, &state(), &own, &History::default(), 1);
assert!(!whole(&packet).contains("my own line"), "her own last lines are not shown");
}
@ -297,6 +298,7 @@ fn a_short_message_to_her_survives_long_chatter_after_it() {
messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", "Eureka, are you ok?", 3, true), message("#swarm:gamecult.org", "@kiko:gamecult.org", "Kiko", &"log line spam ".repeat(120), 1, false)],
tail: vec![],
here: None,
..Room::default()
};
let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1);
assert!(packet.stimulus.starts_with("Said to her:\n- Libby in #swarm:gamecult.org: \"Eureka, are you ok?\""), "{}", packet.stimulus);
@ -309,7 +311,7 @@ fn a_short_message_to_her_survives_long_chatter_after_it() {
fn a_message_to_her_over_the_cap_goes_in_whole_and_the_overflow_is_recorded() {
let big = format!("Eureka, {}", "please read this whole thing ".repeat(70));
assert_eq!(big.len(), 2038);
let room = Room { messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", &big, 1, true)], tail: vec![], here: None };
let room = Room { messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", &big, 1, true)], tail: vec![], here: None, ..Room::default() };
let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1);
assert_eq!(packet.heard, format!("Libby, in #swarm:gamecult.org: \"{big}\""));
assert!(packet.stimulus.contains(big.trim_end()) && !packet.stimulus.contains("not shown"), "no note claims messages that do not exist: {}", &packet.stimulus[..80]);
@ -320,7 +322,7 @@ fn a_message_to_her_over_the_cap_goes_in_whole_and_the_overflow_is_recorded() {
#[test]
fn every_message_to_her_survives() {
let messages: Vec<RoomLine> = (0..30).map(|at| message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", &format!("Eureka, message {at} 💜 ok"), 100 - at, true)).chain((0..30).map(|at| message("#swarm:gamecult.org", "@kiko:gamecult.org", "Kiko", &format!("aside {at}"), 50 - at, false))).collect();
let room = Room { messages, tail: vec![], here: None };
let room = Room { messages, tail: vec![], here: None, ..Room::default() };
let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1);
for at in 0..30 {
let words = format!("Eureka, message {at} 💜 ok");
@ -333,7 +335,7 @@ fn every_message_to_her_survives() {
#[test]
fn a_beat_says_nothing_addressed_her_and_when_each_bond_was_last_heard() {
let room = Room { messages: vec![], tail: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "", "lunch?", 180, false), message("#swarm:gamecult.org", OPERATOR, "", "back later", 60, false)], here: None };
let room = Room { messages: vec![], tail: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "", "lunch?", 180, false), message("#swarm:gamecult.org", OPERATOR, "", "back later", 60, false)], here: None, ..Room::default() };
let packet = packet_of(TurnKind::Beat, &state(), &room, &History::default(), 1);
assert!(packet.stimulus.starts_with("Nothing addressed her."), "{}", packet.stimulus);
assert!(packet.stimulus.contains("- last heard from Libby 3 h ago") && packet.stimulus.contains("- last heard from Meta, the operator 1 h ago") && packet.stimulus.contains("- nothing from Kiko in her rooms lately"), "{}", packet.stimulus);
@ -373,7 +375,7 @@ fn budgets_hold_under_adversarial_sizes() {
bond["relationshipKind"] = json!("friend");
}
let messages = (0..50).map(|at| message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", &format!("{at} {}", "word ".repeat(200)), 60 - at, false)).collect();
let room = Room { messages, tail: vec![], here: None };
let room = Room { messages, tail: vec![], here: None, ..Room::default() };
let tuning = Tuning::default();
for kind in [TurnKind::Chat, TurnKind::Beat, TurnKind::Reflection] {
for seed in 1..=10 {
@ -663,7 +665,7 @@ impl PersonaSource for Stored {
}
fn request(dir: &std::path::Path, renders: Option<usize>, judge_against: Option<Value>) -> Request {
Request { instance: INSTANCE.into(), kind: TurnKind::Chat, state: None, room: Some("#swarm:gamecult.org".into()), seed: 5, renders, judge_against, operators: operators(), minds: minds(), own_sender: OWN.into(), tuning: dir.join(TUNING_FILE) }
Request { instance: INSTANCE.into(), kind: TurnKind::Chat, state: None, room: Some("#swarm:gamecult.org".into()), seed: 5, renders, judge_against, operators: operators(), minds: minds(), own_sender: OWN.into(), tuning: dir.join(TUNING_FILE), through: Through::Projector, last_heard: vec![], recent: vec![] }
}
#[test]

View file

@ -1,4 +1,4 @@
//! `persona-tuning.toml`: every knob of the Select stage and the Projector, read at each wake.
//! `persona-tuning.toml`: every knob of the Select stage, the Projector and the Persona stage, read at each wake.
//!
//! The file is optional. Absent, the code's defaults hold and the source reads `defaults`; present, every key it
//! names overrides one default and any key the code does not know refuses the file, so a mistyped experiment fails
@ -17,6 +17,10 @@ use sha2::{Digest, Sha256};
pub const TUNING_ROOT: &str = "/etc/gamecult/minds";
pub const TUNING_FILE: &str = "persona-tuning.toml";
/// The Persona stage's setting: where her words go, and the one convention that makes them speech (map section 5.5,
/// invariant 5). It licenses nothing: Ghostlight's membrane already says a thin moment gets little or nothing.
const PERSONA_GUIDANCE: &str = "She is in a chat room. Words she says aloud are typed into the room, and appear in her prose inside double quotation marks; nothing else she thinks or does leaves her head.";
/// The setting the Projector renders for. No task: in the Persona-only shape nothing is work she owes.
const GUIDANCE: &str = "This person is a Mind of the GameCult swarm. She lives in its chat rooms among the operator, the other Minds and whoever drops by, and no task is waiting on her in this moment. Render the moment as she lives it: who is here and how they sit with her, what she carries, what she wants.";
@ -37,6 +41,7 @@ const WORDS_MAX: usize = 2000;
#[serde(default, deny_unknown_fields)]
pub struct Tuning {
pub projector: ProjectorTuning,
pub persona: PersonaTuning,
pub select: SelectTuning,
pub lens: LensWeights,
}
@ -53,6 +58,15 @@ pub struct ProjectorTuning {
pub beat_words: usize,
}
/// The Persona stage's call: its model, its voice and setting guidance and its word budget (map section 3.2).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PersonaTuning {
pub model: String,
pub guidance: String,
pub words: usize,
}
/// The selection: its temperature and inclusion, its budgets, its half-life, the freshness decay and the pool caps.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
@ -116,6 +130,12 @@ impl Default for ProjectorTuning {
}
}
impl Default for PersonaTuning {
fn default() -> Self {
Self { model: "claude-sonnet-5-5".into(), guidance: PERSONA_GUIDANCE.into(), words: 180 }
}
}
impl Default for SelectTuning {
fn default() -> Self {
Self {
@ -161,6 +181,9 @@ impl Tuning {
if self.projector.model.trim().is_empty() {
bail!("projector.model must name a model");
}
if self.persona.model.trim().is_empty() {
bail!("persona.model must name a model");
}
if !ADDRESSED_RANGE.contains(&select.addressed_bytes) {
bail!("select.addressed_bytes must be from {} to {}, not {}", ADDRESSED_RANGE.start(), ADDRESSED_RANGE.end(), select.addressed_bytes);
}
@ -182,6 +205,7 @@ impl Tuning {
("select.item_bytes", select.item_bytes),
("projector.words", self.projector.words),
("projector.beat_words", self.projector.beat_words),
("persona.words", self.persona.words),
] {
if bytes == 0 {
bail!("{name} must be above 0");

View file

@ -62,3 +62,37 @@ fn the_retired_max_turns_flag_is_refused_and_its_value_is_not_echoed() {
assert_eq!(output.status.code(), Some(2), "{stderr}");
assert!(stderr.contains("usage") && !stderr.contains("canary-turns"), "{stderr}");
}
/// The harness's read-only proof at the binary: with a room fixture and a state file, `project` runs with no persona.env,
/// no mouth and the daemon's endpoint unreachable, and never needs them.
#[test]
fn project_over_a_room_fixture_and_a_state_file_needs_no_daemon_and_no_mouth() {
let fixtures = Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/harness");
let room = fixtures.join("rooms/mind-teases.toml");
let state = fixtures.join("states/control.json");
let args = ["project", "--instance", "eureka", "--room", room.to_str().unwrap(), "--state", state.to_str().unwrap(), "--select-only"];
// Nothing listens on the discard port: a read of her stored state would fail the run.
let output = Command::new(env!("CARGO_BIN_EXE_mind-persona")).args(args).env_clear().env("HUGINN_ENDPOINT", "rudp://127.0.0.1:9").env("MIND_PERSONA_TUNING", "/nonexistent/persona-tuning.toml").output().unwrap();
assert_eq!(output.status.code(), Some(0), "{}", text(&output.stderr));
let report: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
assert_eq!((report["state"].as_str(), report["persona_receipt"].is_null()), (Some("given"), true), "{report}");
assert!(report["packet"]["identity"].as_str().unwrap().starts_with("Tobiah Fenn"), "{report}");
assert!(report["packet"]["stimulus"].as_str().unwrap().contains("Said to her"), "Kiko's tagged reply is for her: {report}");
// Without any endpoint at all it runs the same.
let output = Command::new(env!("CARGO_BIN_EXE_mind-persona")).args(args).env_clear().env("MIND_PERSONA_TUNING", "/nonexistent/persona-tuning.toml").output().unwrap();
assert_eq!(output.status.code(), Some(0), "{}", text(&output.stderr));
}
#[test]
fn a_state_file_a_put_would_refuse_is_refused() {
let dir = tempfile::tempdir().unwrap();
let fixtures = Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures/harness");
let mut state: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(fixtures.join("states/control.json")).unwrap()).unwrap();
state["personaId"] = "libby".into();
let file = dir.path().join("foreign.json");
std::fs::write(&file, state.to_string()).unwrap();
let room = fixtures.join("rooms/quiet-beat.toml");
let output = Command::new(env!("CARGO_BIN_EXE_mind-persona")).args(["project", "--instance", "eureka", "--room", room.to_str().unwrap(), "--state", file.to_str().unwrap(), "--select-only"]).env_clear().output().unwrap();
assert_eq!(output.status.code(), Some(2));
assert!(text(&output.stderr).contains("refused by a put"), "{}", text(&output.stderr));
}

View file

@ -107,6 +107,7 @@ mind_body::chat_rudp: impl RudpChat: fn reply_defects (& self) -> usize
mind_body::chat_rudp: impl RudpChat: fn with_reply_link (mut self , on : bool) -> Self
mind_body::chat_rudp: struct RudpChat pub-fields[]
mind_body::cli: fn instance_and_endpoint (args : & [String] , env_endpoint : Option < String > , usage : & str) -> Result < (Slug , SocketAddr) >
mind_body::cli: fn instance_and_maybe_endpoint (args : & [String] , env_endpoint : Option < String > , usage : & str) -> Result < (Slug , Option < SocketAddr >) >
mind_body::cli: fn operator_name (sudo_user : Option < String > , user : Option < String >) -> String
mind_body::cli: fn run (args : & [String] , root : & Path , set_by : & str , now : DateTime < Utc > , out : & mut impl Write , stop_units : & mut dyn FnMut (StopScope < '_ >) -> Result < () > ,) -> Result < () >
mind_body::connector: fn render_plan (windows : & [PlanQuota] , _now_ms : u64) -> String
@ -232,11 +233,13 @@ mind_body::persona: struct Config # [derive (Clone , Debug , PartialEq , Eq)] pu
mind_body::persona: struct FileTrace pub-fields[]
mind_body::persona: struct TraceItem # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[provenance: String, room: String, text: String]
mind_body::persona: struct TurnTrace # [derive (Clone , Debug , PartialEq , Eq , DatabaseEntry)] pub-fields[run: String, turn: u32, at: DateTime < Utc >, inputs: Vec < TraceItem >, outputs: Vec < TraceItem >, outcome: String]
mind_body::projection: # [derive (Clone , Copy , Debug , Default , PartialEq , Eq , serde :: Deserialize)] # [serde (rename_all = "snake_case")] pub enum Through { # [default] Projector , Persona , }
mind_body::projection: fn project (organs : & Organs , request : & Request , now : DateTime < Utc >) -> Result < Value >
mind_body::projection: impl HuginnPersonaSource
mind_body::projection: impl HuginnPersonaSource: fn new (client : HuginnClient) -> Self
mind_body::projection: impl PersonaSource for HuginnPersonaSource
mind_body::projection: impl< T : Chat > MouthReader for T
mind_body::projection: mod harness
mind_body::projection: pub trait MouthReader { fn pending (& self) -> Result < ChatPending > ; }
mind_body::projection: pub trait PersonaSource { fn persona (& self) -> Result < (Value , String) > ; }
mind_body::projection: pub use narrate :: { Answer , Failed , KitModel , Model , Spent , connector_key } ;
@ -244,7 +247,21 @@ mind_body::projection: pub use select :: TurnKind ;
mind_body::projection: pub use tuning :: { TUNING_FILE , TUNING_ROOT } ;
mind_body::projection: struct HuginnPersonaSource pub-fields[]
mind_body::projection: struct Organs< 'a > pub-fields[persona: Option < & 'a dyn PersonaSource >, chat: Option < & 'a dyn MouthReader >, addressing: Option < & 'a dyn HeartbeatView >, model: Option < & 'a dyn Model >]
mind_body::projection: struct Request pub-fields[instance: String, kind: TurnKind, state: Option < Value >, room: Option < String >, seed: u64, renders: Option < usize >, judge_against: Option < Value >, operators: Vec < String >, minds: Vec < String >, own_sender: String, tuning: PathBuf]
mind_body::projection: struct Request pub-fields[instance: String, kind: TurnKind, state: Option < Value >, room: Option < String >, seed: u64, renders: Option < usize >, judge_against: Option < Value >, operators: Vec < String >, minds: Vec < String >, own_sender: String, tuning: PathBuf, through: Through, last_heard: Vec < (String , u64) >, recent: Vec < Vec < String > >]
mind_body::projection::harness: fn batch (harness : & Harness , plan_path : & Path) -> Result < String >
mind_body::projection::harness: fn fixture_request (harness : & Harness , fixture : & Fixture , state : Option < Value > , seed : u64 , renders : Option < usize > , through : Through) -> Request
mind_body::projection::harness: fn state_file (path : & Path , instance : & str) -> Result < Value >
mind_body::projection::harness: impl Fixture
mind_body::projection::harness: impl Fixture: fn addressee (& self) -> Option < & dyn HeartbeatView >
mind_body::projection::harness: impl Fixture: fn load (path : & Path) -> Result < Self >
mind_body::projection::harness: impl Fixture: fn parse (text : & str) -> Result < Self >
mind_body::projection::harness: impl HeartbeatView for Fixture
mind_body::projection::harness: impl Line
mind_body::projection::harness: impl MouthReader for Fixture
mind_body::projection::harness: pub use super :: simulate :: simulate ;
mind_body::projection::harness: struct Fixture # [derive (Clone , Debug)] pub-fields[her: String, names: Vec < String >, kind: TurnKind, now: DateTime < Utc >, here: Option < String >, operators: Vec < String >, minds: Vec < String >, last_heard: Vec < (String , u64) >]
mind_body::projection::harness: struct Harness< 'a > pub-fields[instance: String, persona: Option < & 'a dyn PersonaSource >, model: Option < & 'a dyn Model >, tuning: PathBuf]
mind_body::projection::harness: struct Message # [derive (Clone , Debug , Deserialize)] pub-fields[from: String, name: String, room: String, ago: Option < String >, round: Option < usize >, says: String, new: bool, tag: bool, mentions: Vec < String >, dm: bool, follow_up: bool, reply_to: Option < usize >]
mind_body::projection::narrate: fn connector_key (credentials : & Path) -> Result < String >
mind_body::projection::narrate: impl Failed
mind_body::projection::narrate: impl KitModel
@ -256,26 +273,31 @@ mind_body::projection::narrate: struct Answer # [derive (Clone , Debug , Partial
mind_body::projection::narrate: struct Failed # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[reason: String, usage: Option < Spent >]
mind_body::projection::narrate: struct KitModel pub-fields[]
mind_body::projection::narrate: struct Spent # [derive (Clone , Debug , PartialEq , Eq , Serialize)] pub-fields[model: String, input_tokens: u64, output_tokens: u64, cache_read_tokens: u64, cache_write_tokens: u64, reasoning_tokens: Option < u64 >]
mind_body::projection::select: # [derive (Clone , Copy , Debug , PartialEq , Eq , Serialize)] # [serde (rename_all = "snake_case")] pub enum TurnKind { Chat , Beat , FollowUp , Reflection , }
mind_body::projection::select: # [derive (Clone , Copy , Debug , PartialEq , Eq , Serialize , serde :: Deserialize)] # [serde (rename_all = "snake_case")] pub enum TurnKind { Chat , Beat , FollowUp , Reflection , }
mind_body::projection::select: impl Addressed < '_ >
mind_body::projection::select: impl Draw
mind_body::projection::select: impl Lens
mind_body::projection::select: impl Lines
mind_body::projection::select: impl People < '_ >
mind_body::projection::simulate: fn simulate (harness : & Harness , plan_path : & Path) -> Result < String >
mind_body::projection::simulate: impl HeartbeatView for Names < '_ >
mind_body::projection::simulate: impl MouthReader for View < '_ >
mind_body::projection::tuning: const TUNING_FILE : & str
mind_body::projection::tuning: const TUNING_ROOT : & str
mind_body::projection::tuning: fn load (path : & Path) -> Result < Loaded >
mind_body::projection::tuning: impl Default for LensWeights
mind_body::projection::tuning: impl Default for PersonaTuning
mind_body::projection::tuning: impl Default for PoolCaps
mind_body::projection::tuning: impl Default for ProjectorTuning
mind_body::projection::tuning: impl Default for SelectTuning
mind_body::projection::tuning: impl Tuning
mind_body::projection::tuning: struct LensWeights # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[body_and_hour: f64, person_present: f64, unresolved_want: f64, memory_surfacing: f64, wondering: f64]
mind_body::projection::tuning: struct Loaded # [derive (Clone , Debug , PartialEq , Serialize)] pub-fields[tuning: Tuning, source: & 'static str, sha256: String]
mind_body::projection::tuning: struct PersonaTuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[model: String, guidance: String, words: usize]
mind_body::projection::tuning: struct PoolCaps # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[memories_about_present: usize, other_memories: usize, needs: usize, incubation: usize, stance: usize, value: usize, said: usize]
mind_body::projection::tuning: struct ProjectorTuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[model: String, guidance: String, words: usize, beat_words: usize]
mind_body::projection::tuning: struct SelectTuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[temperature: f64, inclusion_score: f64, context_bytes: usize, stimulus_bytes: usize, heard_bytes: usize, addressed_bytes: usize, identity_bytes: usize, bond_summary_bytes: usize, item_bytes: usize, memory_tau_h: f64, freshness_base: f64, freshness_window: usize, people_full: usize, beat_tail_lines: usize, caps: PoolCaps]
mind_body::projection::tuning: struct Tuning # [derive (Clone , Debug , Default , PartialEq , Serialize , Deserialize)] pub-fields[projector: ProjectorTuning, select: SelectTuning, lens: LensWeights]
mind_body::projection::tuning: struct Tuning # [derive (Clone , Debug , Default , PartialEq , Serialize , Deserialize)] pub-fields[projector: ProjectorTuning, persona: PersonaTuning, select: SelectTuning, lens: LensWeights]
mind_body::queue: fn on_hold (mind : & dyn MindPort) -> Result < Held >
mind_body::queue: impl Held
mind_body::queue: impl Held: fn contains (& self , item : & PipelineRef) -> bool