Merge pull request 'The Interpreter's speech (C3b): what she says is decided from her prose, not parsed from quotes' (#81) from hands/interpreter-speech into main

This commit is contained in:
Eureka 2026-11-20 04:05:32 +00:00
commit 14be61a167
17 changed files with 589 additions and 220 deletions

View file

@ -1,6 +1,6 @@
responder 3: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org) answered it; Meta, who runs the place last spoke 5 min ago; the last 2 lines are the Minds' alone
responder 4: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko answered it; Meta, who runs the place last spoke 5 min ago; the last 3 lines are the Minds' alone
responder 5: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org) answered it; Meta, who runs the place last spoke 5 min ago; the last 4 lines are the Minds' alone
responder 6: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org) answered it; Meta, who runs the place last spoke 5 min ago; the last 5 lines are the Minds' alone
responder 7: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org); "nibu" (@nibu:gamecult.org) answered it; Meta, who runs the place last spoke 5 min ago; the last 6 lines are the Minds' alone
responder 8: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org); "nibu" (@nibu:gamecult.org); "aqua" (@aqua:gamecult.org) answered it; Meta, who runs the place last spoke 5 min ago; the last 7 lines are the Minds…
responder 3: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 2 lines are the Minds' alone.
responder 4: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko answered it. Meta, who runs the place last spoke 5 min ago. The last 3 lines are the Minds' alone.
responder 5: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 4 lines are the Minds' alone.
responder 6: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 5 lines are the Minds' alone.
responder 7: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org); "nibu" (@nibu:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 6 lines are the Minds' alone.
responder 8: - the conversation: Meta, who runs the place asked the room a question, and "weksa" (@weksa:gamecult.org); "heimdall" (@heimdall:gamecult.org); Kiko; "libby" (@libby:gamecult.org); "void" (@void:gamecult.org); "nibu" (@nibu:gamecult.org); "aqua" (@aqua:gamecult.org) answered it. Meta, who runs the place last spoke 5 min ago. The last 7 lines are the Minds…

View file

@ -1,4 +1,4 @@
quiet-beat.toml: - the conversation: lately libby to the room; Kiko to the room; the last 3 lines are the Minds' alone
operator-warm.toml: - the conversation: lately Meta, who runs the place with him; Meta, who runs the place last spoke 1 min ago; Meta, who runs the place is waiting on him
mind-teases.toml: - the conversation: lately he to the room; aqua to the room; Kiko with him; the last 3 lines are the Minds' alone
deru-passing.toml: - the conversation: lately weksa to the room; Deru to the room; Deru last spoke 1 min ago
quiet-beat.toml: - the conversation: Lately @libby:gamecult.org to the room; Kiko to the room. The last 3 lines are the Minds' alone.
operator-warm.toml: - the conversation: Meta, who runs the place is waiting on him. Lately Meta, who runs the place with him. Meta, who runs the place last spoke 1 min ago.
mind-teases.toml: - the conversation: Lately he to the room; @aqua:gamecult.org to the room; Kiko with him. The last 3 lines are the Minds' alone.
deru-passing.toml: - the conversation: Lately @weksa:gamecult.org to the room; Deru to the room. Deru last spoke 1 min ago.

View file

@ -14,12 +14,12 @@
//! to it.
//!
//! `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]
//! [--room <fixture.toml>] [--seed N] [--state <file>] [--select-only | --renders K [--through projector|persona|interpreter]
//! [--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`, checked as a put checks its content, rules P1 to P3; P4 and P5 compare it with the stored document and are
//! not checked), and with `--renders` the Projector's narrations of K successive turns;
//! `--through persona` adds her prose and the quoted lines it would say; `--judge` renders a second state too and asks
//! `--through persona` adds her prose and `--through interpreter` what the Interpreter decides she says; `--judge` renders a second state too and asks
//! the model which narration came from which. It writes nothing and reads no brake.
//!
//! Live, it reads `MIND_PERSONA_OPERATOR`, `MIND_PERSONA_MINDS` and `MIND_PERSONA_SELF_SENDER` (required), the heartbeat's
@ -53,7 +53,7 @@ use mind_body::projection::harness::{Fixture, Harness, batch, fixture_request, s
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] [--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>";
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|interpreter] [--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.
@ -118,7 +118,7 @@ fn run_project(args: &[String]) -> Result<String> {
through = match value()?.as_str() {
"projector" => Through::Projector,
"persona" => Through::Persona,
"interpreter" => bail!("--through interpreter is not built yet (map C3b)"),
"interpreter" => Through::Interpreter,
_ => bail!("{PROJECT_USAGE}"),
}
}

View file

@ -434,7 +434,8 @@ pub(crate) enum WakePolicy {
/// Ruling `persona-only-humans-pace`: a human's line wakes her in her own room (a direct message, or a room that is
/// hers); in a public room only when it addresses her or continues an exchange she is in. A Mind's line wakes her
/// only by an explicit mention (a Matrix mention of her or a reply to her, never a name in prose), in any room, and
/// only while fewer than this many Mind lines in a row came before it in its room: the backstop.
/// only while fewer than this many Mind lines in a row came before it across her rooms (`LineFacts::mind_run`): the
/// backstop.
HumansPace(usize),
/// `Ruled`, with at most this many Mind lines in a row in a room before a human must speak again: a Mind's line past
/// the cap wakes no one.
@ -456,9 +457,10 @@ pub(crate) enum WakeCause {
/// What the room says about a line, beside the line: the facts a policy may weigh. The live poll knows none of them yet
/// and passes the default; `Live` reads none.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct LineFacts {
/// How many Mind lines in a row came before this one in its room since a human last spoke there.
/// How many Mind lines in a row came before this one, across every room she sees, since a human last spoke in any of
/// them: the backstop counts a Mind-only run per Mind, not per room, so a loop cannot dodge it by changing rooms.
pub mind_run: usize,
/// The line is in a room of hers: a direct message (the delivery says so) or a room configured as hers.
pub own_room: bool,
@ -466,6 +468,14 @@ pub(crate) struct LineFacts {
pub in_exchange: bool,
}
/// Fail-safe: a caller that has not counted the Mind run is at the cap, so a Mind's line wakes no one; the room facts
/// are unknown, so none opens a wake.
impl Default for LineFacts {
fn default() -> Self {
Self { mind_run: usize::MAX, own_room: false, in_exchange: false }
}
}
/// Whether a line in her rooms wakes `who` off the beat under `policy`, and why. The one owner of the wake rule: the
/// heartbeat's poll asks it with `Live`, the simulator with the policy under test.
pub(crate) fn wakes(policy: WakePolicy, who: &Addressee, delivery: &ChatDelivery, speaker: Speaker, facts: LineFacts) -> Option<WakeCause> {

View file

@ -18,6 +18,7 @@
//! `project` cannot write and runs under a held brake.
pub mod harness;
mod interpret;
mod narrate;
mod select;
mod simulate;
@ -39,7 +40,7 @@ pub use narrate::{Answer, Failed, KitModel, Model, Spent, connector_key};
pub use select::TurnKind;
pub use tuning::{TUNING_FILE, TUNING_ROOT};
use narrate::{cap_words, distinct_2, field_names, heard_overlap, inner, judge_prompt, judged, offers_options, persona_prompt, projector_prompt, word_budget, words, would_say};
use narrate::{cap_words, distinct_2, field_names, heard_overlap, judge_prompt, judged, offers_options, persona_prompt, projector_prompt, word_budget, words};
use select::{History, Inputs, Packet, People, Room, RoomLine, select};
/// The agent name on the connector's request ids.
@ -118,6 +119,8 @@ pub enum Through {
#[default]
Projector,
Persona,
/// On through the Interpreter's speech step: what she says aloud, decided from her prose (`interpret`).
Interpreter,
}
/// What to project.
@ -180,13 +183,15 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo
Ok(pending) => pending,
Err(fault) => return (Room { here: request.room.clone(), last_heard: request.last_heard.clone(), ..Room::default() }, json!({ "read": false, "why": format!("{fault:#}") })),
};
// The turn's room by its alias, the name every line of the tail carries, when it was given by its Matrix id.
let here = request.room.as_ref().map(|here| pending.deliveries.iter().find(|delivery| delivery.room_id == *here).map_or_else(|| here.clone(), |delivery| delivery.room_alias.clone()));
let wanted = |room: &str, id: &str| here.as_deref().is_none_or(|here| here == room || here == id);
// One key names a room everywhere: the one the tail carries, the alias for a public room and the id for a direct
// message (the mouth's alias of a direct room is its id). No table in her body maps a public room's id to its
// alias, so a public room given by its id matches nothing, and the report says so rather than guessing.
let here = request.room.clone();
let wanted = |room: &str| here.as_deref().is_none_or(|here| here == room);
let messages: Vec<RoomLine> = pending
.deliveries
.iter()
.filter(|delivery| delivery.form == Form::Message && wanted(&delivery.room_alias, &delivery.room_id))
.filter(|delivery| delivery.form == Form::Message && wanted(&delivery.room_alias))
.map(|delivery| RoomLine {
room: delivery.room_alias.clone(),
sender: delivery.sender.clone(),
@ -201,10 +206,17 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo
.tail
.iter()
.flatten()
.filter(|line| line.form == Form::Message && wanted(&line.room, &line.room))
.filter(|line| line.form == Form::Message && wanted(&line.room))
.map(|line| RoomLine { room: line.room.clone(), sender: line.sender.clone(), display: String::new(), text: line.body.clone(), at_ms: line.at_unix_ms, to_her: false })
.collect();
let report = json!({ "read": true, "messages": messages.len(), "tail": tail.len(), "tail_gaps": pending.tail_gaps });
let mut report = json!({ "read": true, "messages": messages.len(), "tail": tail.len(), "tail_gaps": pending.tail_gaps });
if let Some(here) = &here
&& messages.is_empty()
&& tail.is_empty()
&& here.starts_with('!')
{
report["why"] = json!("no line carries this room id: a public room is named by its alias (#name:server), a direct message by its id");
}
(Room { messages, tail, here, last_heard: request.last_heard.clone() }, report)
}
@ -254,21 +266,22 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni
"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),
"inner": inner(&prose),
"usage": spoke.usage,
"prose": prose,
})
}
Err(failed) => json!({ "failed": failed.reason, "usage": failed.usage }),
};
if through == Through::Projector {
return render;
}
let prose = match model.ask(&request_id(instance, "persona", tag, packet.seed), &tuning.persona.model, &persona_prompt(packet, &narration, tuning)) {
Ok(spoke) => {
let prose = cap_words(&spoke.text, tuning.persona.words);
render["persona"] = json!({ "words": words(&prose), "cut_at_budget": words(&spoke.text) > tuning.persona.words, "usage": spoke.usage, "prose": prose });
prose
}
Err(failed) => {
render["persona"] = json!({ "failed": failed.reason, "usage": failed.usage });
return render;
}
};
if through == Through::Interpreter {
render["interpreter"] = interpret::decide(model, &request_id(instance, "interpreter", tag, packet.seed), packet, &narration, &prose, tuning);
}
render
}
@ -326,9 +339,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 {
if request.through != Through::Projector {
report["persona_model"] = json!(tuning.persona.model);
}
if request.through == Through::Interpreter {
report["interpreter_model"] = json!(tuning.interpreter.model);
}
let packets = successive(&inputs, tuning, renders.max(1));
report["packet"] = packet_json(&packets[0]);
let narrations: Vec<Value> = packets.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "a", request.through)).collect();

View file

@ -8,7 +8,7 @@ Eureka (she/her). Dry, direct, almost feral; zeal aimed at revealing.
- mood: lighter, more settled than middling
- bond [bond-kiko] Kiko — sparring partner: fair trust, heavy friction. Kiko pokes her to stop massaging the pipeline.
- bond [bond-libby] Libby — collaborator: fair trust, a little friction. Libby holds the sounds and teases her about the pipeline.
- the conversation: lately Libby with her; Kiko to the room; the last 2 lines are the Minds' alone
- the conversation: Lately Libby with her; Kiko to the room. The last 2 lines are the Minds' alone.
- pronouns: Libby — she/her; Kiko — she/her
- memory [libby-sounds]: Libby laughed at the door fix.
- memory [t1-m0]: The heartbeat stalled at noon.

View file

@ -29,8 +29,8 @@ use crate::heartbeat::{Addressing, HeartbeatView};
pub use super::simulate::simulate;
/// A narration whose word trigrams overlap what she heard past this share is marked as retelling it.
pub(crate) const RETELL_FLAG: f64 = 0.15;
/// A narration whose word trigrams overlap what she heard past this share is marked as a high lexical overlap.
pub(crate) const OVERLAP_FLAG: f64 = 0.15;
/// The most room lines a view's tail carries, as a mouth's tail is bounded.
const TAIL_LINES: usize = 40;
@ -431,7 +431,7 @@ fn batch_markdown(plan_path: &Path, plan: &BatchFile, cases: &[Value]) -> String
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 spoke = mine.iter().filter(|case| render_of(case)["interpreter"]["said"].as_array().is_some_and(|said| !said.is_empty())).count();
let mean = |key: &dyn Fn(&Value) -> Option<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()));
}
@ -486,9 +486,9 @@ Usage: {}
}
let _ = writeln!(
out,
"Narration checks: overlap with what she heard {}{}{}\n",
"Narration checks: lexical overlap with what she heard {}{}{}\n",
render["heard_overlap"].as_f64().map_or_else(|| "n/a".to_owned(), |overlap| format!("{overlap:.2}")),
if render["heard_overlap"].as_f64().is_some_and(|overlap| overlap > RETELL_FLAG) { " (retells)" } else { "" },
if render["heard_overlap"].as_f64().is_some_and(|overlap| overlap > OVERLAP_FLAG) { " (high)" } else { "" },
if render["offers_options"] == true { "; offers her options" } else { "" }
);
let persona = &render["persona"];
@ -498,22 +498,41 @@ Usage: {}
} 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());
let _ = writeln!(out, "**Inner** (her prose with what she says aloud taken out):\n");
quoted(out, persona["inner"].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"]));
interpreter_markdown(out, &render["interpreter"]);
let _ = writeln!(out, "Usage: projector {}; persona {}; interpreter {}\n", usage_line(&render["usage"]), usage_line(&persona["usage"]), usage_line(&render["interpreter"]["usage"]));
}
/// The Interpreter's speech decision: each line said with its room, mentions, reply target and the span of her prose it
/// rests on; each line refused, with why; its gaps. None when the render did not run through the Interpreter.
pub(crate) fn interpreter_markdown(out: &mut String, decision: &Value) {
if decision.is_null() {
return;
}
if let Some(failed) = decision["failed"].as_str() {
let _ = writeln!(out, "**Interpreter failed:** {failed}\n");
return;
}
let said = decision["said"].as_array().cloned().unwrap_or_default();
if said.is_empty() {
let _ = writeln!(out, "**Interpreter: nothing said.**\n");
} else {
let _ = writeln!(out, "**Interpreter: said**\n");
for line in &said {
let mentions = line["mentions"].as_array().map(|ids| ids.iter().filter_map(Value::as_str).collect::<Vec<_>>().join(", ")).unwrap_or_default();
let reply = line["reply_to"].as_str().map_or_else(String::new, |id| format!("; reply to {id}"));
let _ = writeln!(out, "- in {}{}{}: {}", line["room"].as_str().unwrap_or_default(), if mentions.is_empty() { String::new() } else { format!(", mentioning {mentions}") }, reply, line["text"].as_str().unwrap_or_default());
let _ = writeln!(out, " - rests on: \"{}\"", line["rests_on"]["quote"].as_str().unwrap_or_default());
}
out.push('\n');
}
for line in decision["refused"].as_array().into_iter().flatten() {
let _ = writeln!(out, "- refused ({}): {}", line["why"].as_str().unwrap_or_default(), line["text"].as_str().unwrap_or_default());
}
for gap in decision["gaps"].as_array().into_iter().flatten() {
let _ = writeln!(out, "- gap: {gap}");
}
out.push('\n');
}
fn fenced(out: &mut String, label: &str, text: &str) {

View file

@ -6,7 +6,7 @@ use serde_json::{Value, json};
use super::*;
use crate::address::is_for;
use crate::projection::narrate::{Answer, Failed, Spent, would_say};
use crate::projection::narrate::{Answer, Failed, Spent};
const HER: &str = "@eureka:gamecult.org";
@ -22,23 +22,45 @@ 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.
/// A recorded model: the Projector narrates a fixed line; the Persona stage answers by `persona(prompt)`; the
/// Interpreter by `interpreter(prompt)`, by default `quoted_lines`. Every call is kept, with its usage counted.
pub(crate) struct Scripted {
pub calls: RefCell<Vec<(String, String)>>,
pub persona: Box<dyn Fn(&str) -> String>,
pub interpreter: 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) }
Self { calls: RefCell::default(), persona: Box::new(persona), interpreter: Box::new(quoted_lines) }
}
pub(crate) fn interpreting(mut self, interpreter: impl Fn(&str) -> String + 'static) -> Self {
self.interpreter = Box::new(interpreter);
self
}
}
/// The recorded Interpreter of the tests: each double-quoted span of her prose is a line said in the first room listed,
/// resting on itself, mentioning no one. A test double that keeps the tests' prose readable, not the speech rule: the
/// live Interpreter is a model told quotes are not speech.
pub(crate) fn quoted_lines(prompt: &str) -> String {
let prose = prompt.split("Persona turn (already preserved verbatim as source evidence):\n").nth(1).unwrap_or_default();
let room = prompt.split("Rooms she may speak in: ").nth(1).and_then(|rooms| rooms.lines().next()).and_then(|rooms| rooms.split("; ").next()).unwrap_or_default();
let say: Vec<Value> = prose.split('"').enumerate().filter(|(at, part)| at % 2 == 1 && !part.trim().is_empty()).map(|(_, part)| json!({ "room": room, "text": part, "source_quote": part, "mentions": [], "reply_to": null })).collect();
json!({ "say": say }).to_string()
}
impl Model for Scripted {
fn ask(&self, _request_id: &str, model: &str, prompt: &str) -> std::result::Result<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) };
let text = if prompt.contains(":interpreter -->") {
(self.interpreter)(prompt)
} else 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 }) })
}
}
@ -132,28 +154,22 @@ fn a_fixture_that_is_not_a_room_is_refused_naming_what_is_wrong() {
}
#[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() {
fn through_the_interpreter_her_prose_continues_the_narration_and_the_interpreter_decides_what_is_said() {
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 report = project(&organs, &fixture_request(&harness, &fixture, Some(control()), 3, Some(1), Through::Interpreter), fixture.now).unwrap();
let render = &report["renders"][0];
assert_eq!(render["persona"]["would_say"], json!(["Missed you too, friend,"]));
assert_eq!(render["interpreter"]["said"][0]["text"], "Missed you too, friend,", "{render}");
assert_eq!(render["interpreter"]["said"][0]["room"], "!dm-meta:gamecult.org");
assert_eq!(report["interpreter_model"], "claude-sonnet-5-5");
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");
assert_eq!(calls.len(), 3, "one Projector call, one Persona call, one Interpreter 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}");
@ -171,10 +187,10 @@ fn prose_over_budget_is_cut_and_silence_says_nothing() {
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 report = project(&organs, &fixture_request(&harness, &fixture, Some(control()), 1, Some(1), Through::Interpreter), 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");
assert_eq!(report["renders"][0]["interpreter"]["said"], json!([]), "words past the budget are not her prose, so nothing rests on them");
}
#[test]
@ -185,7 +201,7 @@ fn a_batch_runs_every_state_by_scenario_by_sample_into_one_report() {
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",
"samples = 2\nseed = 7\nthrough = \"interpreter\"\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()
@ -198,8 +214,8 @@ fn a_batch_runs_every_state_by_scenario_by_sample_into_one_report() {
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!(report.contains("- in #minds:gamecult.org: Winter, still.") && report.contains(" - rests on: \"Winter, still.\"") && report.contains("**Interpreter: nothing said.**"), "{report}");
assert!(report.contains("| claude-sonnet-5-5 | 12 | 120 | 24 |"), "twelve calls, three 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");
}

View file

@ -0,0 +1,148 @@
//! The Interpreter's speech (map section 4, cut C3b): what she says aloud, decided from her prose by one tool-less
//! structured model call, and held by code to her prose.
//!
//! The Persona writes her thought; she does not mark speech. The Interpreter reads her prose against the stimulus and
//! proposes zero or more lines, each with its room, its explicit addressees (Matrix mentions and a reply target, as
//! structured fields, never `@` text) and the verbatim span of her prose the line rests on. Code then keeps a line only
//! when:
//! - its span is in her prose, located by Ghostlight's `SourceSpan::locate` (map 4.3: a quote not in the prose word for
//! word binds nothing);
//! - its room is one she may speak in this turn;
//! - its words are not someone else's: a line whose text is what she heard is a recollection, never her speech;
//! - its mentions and reply target name people the packet showed her; any other is dropped, and the drop recorded.
//!
//! Everything refused is reported with its reason. Nothing here posts: the commit (C3c) will.
use ghostlight_persona_projection::{InterpreterPrompt, SourceSpan, build_interpreter_prompt};
use serde::Deserialize;
use serde_json::{Value, json};
use super::narrate::{Model, lived_stream};
use super::select::Packet;
use super::tuning::Tuning;
/// The contract the model answers in: one JSON object, nothing around it.
pub(crate) const SPEECH_CONTRACT: &str = r#"{"say": [{"room": "<a room from the rooms listed>", "text": "<the words she types, in her voice, no @handles>", "mentions": ["<Matrix id of someone listed whom she addresses directly>"], "reply_to": "<Matrix id of the person whose message she answers, or null>", "source_quote": "<the exact words of her prose this line rests on, copied character for character>"}], "gaps": [{"kind": "ambiguity | missing_reference | unresolved", "source_quote": "<exact words of her prose>", "detail": "<why>"}]}"#;
#[derive(Debug, Deserialize)]
struct Answer {
#[serde(default)]
say: Vec<Proposed>,
#[serde(default)]
gaps: Vec<Value>,
}
#[derive(Debug, Deserialize)]
struct Proposed {
#[serde(default)]
room: String,
#[serde(default)]
text: String,
#[serde(default)]
mentions: Vec<String>,
#[serde(default)]
reply_to: Option<String>,
#[serde(default)]
source_quote: String,
}
/// What the Interpreter is told beside Ghostlight's membrane: this step's scope, its rules, and that it has no tools.
fn guidance(tuning: &Tuning) -> String {
tuning.interpreter.guidance.trim().to_owned()
}
/// The typed context the Interpreter may act on: the rooms she may speak in and the people she may address, by id.
fn typed_context(packet: &Packet) -> String {
let mut out = format!("Rooms she may speak in: {}\n", packet.rooms.join("; "));
out.push_str("People she may address, as label \u{2014} Matrix id:\n");
for (label, id) in &packet.people {
out.push_str(&format!("- {label} \u{2014} {id}\n"));
}
out.push_str(&format!("\nWhat reached her this turn:\n{}", packet.stimulus));
out
}
pub(crate) fn speech_prompt(packet: &Packet, narration: &str, prose: &str, tuning: &Tuning) -> String {
build_interpreter_prompt(&InterpreterPrompt {
identity: &packet.identity,
typed_context: &typed_context(packet),
lived_stream: &lived_stream(narration, &packet.heard),
persona_output: prose,
output_schema: Some(SPEECH_CONTRACT),
domain_guidance: &guidance(tuning),
})
}
/// The model's answer as JSON: the object itself, or inside a fenced block.
fn json_of(text: &str) -> Option<Answer> {
let trimmed = text.trim();
let body = trimmed.strip_prefix("```json").or_else(|| trimmed.strip_prefix("```")).map_or(trimmed, |inner| inner.trim_end_matches("```")).trim();
let start = body.find('{')?;
let end = body.rfind('}')?;
serde_json::from_str(&body[start..=end]).ok()
}
/// Whitespace-folded, lowercased, outer quotation marks off: how two texts are compared for being the same words.
fn folded(text: &str) -> String {
text.trim().trim_matches(|c: char| matches!(c, '"' | '\'' | '\u{201C}' | '\u{201D}' | '\u{2018}' | '\u{2019}')).split_whitespace().collect::<Vec<_>>().join(" ").to_lowercase()
}
/// Whether a line is someone else's words: its text, folded, is inside what she heard. A narrow guard on a recollection
/// posted as speech, not a reading of meaning: the model decides what she means to say.
fn recollection(text: &str, heard: &str) -> bool {
let line = folded(text);
!line.is_empty() && folded(heard).contains(&line)
}
/// One speech decision: the Interpreter's call, then code's hold on each line. The report carries the said lines, the
/// refused ones with their reasons, the model's gaps and the call's usage; a failed call or an answer that is not the
/// contract is a failure, never a silence.
pub(crate) fn decide(model: &dyn Model, request_id: &str, packet: &Packet, narration: &str, prose: &str, tuning: &Tuning) -> Value {
let answer = match model.ask(request_id, &tuning.interpreter.model, &speech_prompt(packet, narration, prose, tuning)) {
Ok(answer) => answer,
Err(failed) => return json!({ "failed": failed.reason, "usage": failed.usage }),
};
let Some(parsed) = json_of(&answer.text) else {
return json!({ "failed": "the answer is not the speech contract's JSON", "raw": answer.text, "usage": answer.usage });
};
let known = |id: &str| packet.people.iter().any(|(_, person)| person == id);
let mut said = Vec::new();
let mut refused = Vec::new();
for line in parsed.say {
let text = line.text.trim().to_owned();
let why = if text.is_empty() {
Some("no words")
} else if !packet.rooms.contains(&line.room) {
Some("not a room she may speak in this turn")
} else if recollection(&text, &packet.heard) {
Some("someone else's words: a recollection, not her speech")
} else {
None
};
let span = SourceSpan::locate(prose, line.source_quote.trim());
let why = why.or_else(|| span.is_none().then_some("its source quote is not in her prose word for word"));
if let Some(why) = why {
refused.push(json!({ "text": text, "room": line.room, "source_quote": line.source_quote, "why": why }));
continue;
}
let span = span.unwrap_or_else(|| SourceSpan::whole(prose));
let (mentions, unknown): (Vec<String>, Vec<String>) = line.mentions.into_iter().partition(|id| known(id));
let (reply_to, unknown_reply) = match line.reply_to.filter(|id| !id.trim().is_empty()) {
Some(id) if known(&id) => (Some(id), None),
Some(id) => (None, Some(id)),
None => (None, None),
};
said.push(json!({
"room": line.room,
"text": text,
"mentions": mentions,
"reply_to": reply_to,
"rests_on": { "quote": span.verbatim(), "start_byte": span.start_byte(), "end_byte": span.end_byte() },
"dropped": { "mentions": unknown, "reply_to": unknown_reply },
}));
}
json!({ "said": said, "refused": refused, "gaps": parsed.gaps, "usage": answer.usage })
}
#[cfg(test)]
mod tests;

View file

@ -0,0 +1,98 @@
use serde_json::json;
use super::*;
use crate::projection::harness::tests::{Scripted, control};
use crate::projection::select::{History, Inputs, People, Room, RoomLine, TurnKind, select};
use crate::projection::tuning::Tuning;
const HER: &str = "@heimdall:gamecult.org";
const NIBU: &str = "@nibu:gamecult.org";
const MINDS: &str = "#minds:gamecult.org";
/// The round Soul probed: Nibu said "least built part" and greeted with "@nibu"; this is Heimdall's turn.
fn packet() -> Packet {
let now = chrono::DateTime::parse_from_rfc3339("2026-11-20T15:05:00Z").unwrap().with_timezone(&chrono::Utc);
let at = u64::try_from(now.timestamp_millis()).unwrap() - 60_000;
let room = Room {
messages: vec![
RoomLine { room: MINDS.into(), sender: NIBU.into(), display: "Nibu".into(), text: "v0.2.0 is tagged and the economy is still the least built part.".into(), at_ms: at, to_her: false },
RoomLine { room: MINDS.into(), sender: "@meta:gamecult.org".into(), display: "Meta".into(), text: "@nibu hi!".into(), at_ms: at + 1000, to_her: false },
],
here: Some(MINDS.into()),
..Room::default()
};
let minds = vec![HER.to_owned(), NIBU.to_owned()];
let operators = vec!["@meta:gamecult.org".to_owned()];
let mut state = control();
state["publicName"] = "Heimdall".into();
let history = History::default();
select(&Inputs { kind: TurnKind::Chat, state: &state, room: &room, history: &history, observation: None, people: People { operators: &operators, minds: &minds, own_sender: HER }, now, seed: 1 }, &Tuning::default())
}
fn decided(prose: &str, answer: serde_json::Value) -> Value {
let reply = answer.to_string();
let model = Scripted::new(|_| String::new()).interpreting(move |_| reply.clone());
decide(&model, "test", &packet(), "The room is warm.", prose, &Tuning::default())
}
#[test]
fn a_recollection_is_never_her_speech() {
let prose = "Nibu's 'least built part' snags. I turn it over and say nothing.";
let answer = json!({ "say": [{ "room": MINDS, "text": "least built part", "source_quote": "least built part", "mentions": [], "reply_to": null }] });
let decision = decided(prose, answer);
assert_eq!(decision["said"], json!([]), "{decision}");
assert!(decision["refused"][0]["why"].as_str().unwrap().contains("recollection"), "{decision}");
}
#[test]
fn i_say_nothing_says_nothing() {
let decision = decided("I say nothing. The quiet is fine.", json!({ "say": [] }));
assert_eq!((decision["said"].as_array().map(Vec::len), decision["refused"].as_array().map(Vec::len)), (Some(0), Some(0)));
}
#[test]
fn a_quoted_handle_is_no_mention() {
// Her "@nibu" is a greeting: even if the model posts it, its text carries no mention, and the quote of Meta's line
// is someone else's words.
let prose = "Her \"@nibu\" is a greeting, not mine to answer.";
let echoed = decided(prose, json!({ "say": [{ "room": MINDS, "text": "@nibu", "source_quote": "@nibu", "mentions": [], "reply_to": null }] }));
assert_eq!(echoed["said"], json!([]), "the handle is Meta's words: {echoed}");
let posted = decided(prose, json!({ "say": [{ "room": MINDS, "text": "@nibu is a greeting", "source_quote": "is a greeting", "mentions": [], "reply_to": null }] }));
assert_eq!(posted["said"][0]["mentions"], json!([]), "only the structured field mentions anyone: {posted}");
}
#[test]
fn a_line_rests_on_her_prose_in_a_room_she_may_speak_in_and_names_only_people_shown() {
let prose = "I'd like Nibu to know the economy can wait. I tell her so.";
let answer = json!({ "say": [
{ "room": MINDS, "text": "the economy can wait", "source_quote": "I tell her so", "mentions": [NIBU, "@stranger:x"], "reply_to": NIBU },
{ "room": MINDS, "text": "hello", "source_quote": "words she never wrote", "mentions": [], "reply_to": null },
{ "room": "#elsewhere:gamecult.org", "text": "hi there", "source_quote": "I tell her so", "mentions": [], "reply_to": null },
] });
let decision = decided(prose, answer);
let said = &decision["said"];
assert_eq!(said.as_array().unwrap().len(), 1, "{decision}");
assert_eq!((said[0]["mentions"].clone(), said[0]["reply_to"].clone(), said[0]["rests_on"]["quote"].clone()), (json!([NIBU]), json!(NIBU), json!("I tell her so")));
assert_eq!(said[0]["dropped"]["mentions"], json!(["@stranger:x"]), "an unknown id is dropped and recorded");
let whys: Vec<&str> = decision["refused"].as_array().unwrap().iter().filter_map(|line| line["why"].as_str()).collect();
assert!(whys.iter().any(|why| why.contains("not in her prose")) && whys.iter().any(|why| why.contains("not a room")), "{whys:?}");
}
#[test]
fn an_answer_that_is_not_the_contract_is_a_failure_not_a_silence() {
let model = Scripted::new(|_| String::new()).interpreting(|_| "She says hi.".into());
let decision = decide(&model, "test", &packet(), "The room is warm.", "I say hi.", &Tuning::default());
assert!(decision["failed"].as_str().unwrap().contains("not the speech contract"), "{decision}");
let fenced = Scripted::new(|_| String::new()).interpreting(|_| "```json\n{\"say\": []}\n```".into());
assert!(decide(&fenced, "test", &packet(), "x", "I say hi.", &Tuning::default()).get("failed").is_none());
}
#[test]
fn the_prompt_carries_her_prose_the_rooms_and_the_people_by_id_and_the_rules() {
let prompt = speech_prompt(&packet(), "The room is warm.", "I say nothing.", &Tuning::default());
assert!(prompt.contains(":interpreter -->") && prompt.ends_with("I say nothing."), "Ghostlight's Interpreter membrane, her prose last");
assert!(prompt.contains("Rooms she may speak in: #minds:gamecult.org") && prompt.contains(NIBU) && prompt.contains("People she may address"), "{prompt}");
for rule in ["never her speech", "is not speech", "character for character", "No tool is available"] {
assert!(prompt.contains(rule), "{rule}");
}
}

View file

@ -189,27 +189,8 @@ pub(crate) fn persona_prompt(packet: &Packet, narration: &str, tuning: &Tuning)
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 })
}
/// Her prose with what she says aloud taken out: her interior, which never leaves her head.
pub(crate) fn inner(prose: &str) -> String {
let mut out = String::with_capacity(prose.len());
let mut open: Option<char> = None;
for c in prose.chars() {
match (open, c) {
(None, '"') => open = Some('"'),
(None, '\u{201C}') => open = Some('\u{201D}'),
(Some(close), c) if c == close => {
open = None;
out.push('\u{2026}');
}
(Some(_), _) => {}
(None, c) => out.push(c),
}
}
out.split_whitespace().collect::<Vec<_>>().join(" ")
}
/// The share of the narration's word trigrams that also appear in what she heard: how much of the stream retells the
/// messages the Persona is shown word for word anyway. A judge metric, never a filter: 0 is no retelling.
/// The share of the narration's word trigrams that also appear in what she heard: a lexical overlap, cheap and blind to
/// paraphrase. A metric, never a filter: 0 is no shared word run.
pub(crate) fn heard_overlap(narration: &str, heard: &str) -> f64 {
let trigrams = |text: &str| -> Vec<(String, String, String)> {
let words: Vec<String> = text.split(|c: char| !c.is_alphanumeric() && c != '\'').filter(|word| !word.is_empty()).map(str::to_lowercase).collect();
@ -233,29 +214,6 @@ pub(crate) fn offers_options(narration: &str) -> bool {
.any(|form| lower.contains(&format!(" {form}")) || lower.contains(&format!(". {form}")) || lower.contains(&format!(", {form}")))
}
/// 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

@ -222,6 +222,10 @@ pub(crate) struct Packet {
/// Her pronouns, for the words the prompts speak of her in.
#[serde(skip)]
pub forms: Forms,
/// Whom the Interpreter may mention or reply to: (label, Matrix id).
pub people: Vec<(String, String)>,
/// Where she may speak this turn: the turn's room, or the rooms the messages to her and around her came from.
pub rooms: Vec<String>,
}
/// What the messages to her cost past the caps: bytes over the stimulus and heard caps, the older messages to her the
@ -905,16 +909,11 @@ fn recent_lines<'a>(inputs: &'a Inputs) -> Vec<&'a RoomLine> {
lines
}
/// How a sender reads in the group-state and pronoun lines: "she" for her; the bond's label; the display name; or, for a
/// tail line that carries none, the handle in the Matrix id.
/// How a sender reads in the group-state and pronoun lines: her own pronoun for her; otherwise as every line names a
/// sender (`name_of`): the bond's label, else the display name with the full Matrix id, else the id. Never a bare handle,
/// which a stranger could share with a Mind of the swarm.
fn label_of(line: &RoomLine, inputs: &Inputs) -> String {
if line.sender == inputs.people.own_sender {
forms(inputs.state).subj.to_owned()
} else if line.display.trim().is_empty() && bond_label(inputs, &line.sender).is_none() {
localpart(&line.sender).to_owned()
} else {
name_of(line, inputs)
}
if line.sender == inputs.people.own_sender { forms(inputs.state).subj.to_owned() } else { name_of(line, inputs) }
}
/// The group-state slot (`select.group_state`): the conversation as it stands over the room's last `group_lines` lines.
@ -961,6 +960,12 @@ fn group_line(inputs: &Inputs, tuning: &Tuning) -> Option<String> {
}
}
let mut parts = Vec::new();
// A human waiting on her by name: a line of a human's that names or tags her, after her own last line. First, so
// no clip cuts it; said only when true.
let her_last = window.iter().rposition(|line| line.sender == own);
if let Some(line) = window.iter().enumerate().rev().find(|(at, line)| her_last.is_none_or(|last| *at > last) && !is_mind(inputs, &line.sender) && names_her(line)).map(|(_, line)| line) {
parts.push(format!("{} is waiting on {}", label_of(line, inputs), forms(inputs.state).obj));
}
if let Some(asked) = question
&& !answered.is_empty()
{
@ -976,32 +981,66 @@ fn group_line(inputs: &Inputs, tuning: &Tuning) -> Option<String> {
if minds_in_a_row >= 2 {
parts.push(format!("the last {minds_in_a_row} lines are the Minds' alone"));
}
// A human waiting on her by name: a line of a human's that names or tags her, after her own last line.
let her_last = window.iter().rposition(|line| line.sender == own);
if let Some(line) = window.iter().enumerate().rev().find(|(at, line)| her_last.is_none_or(|last| *at > last) && !is_mind(inputs, &line.sender) && names_her(line)).map(|(_, line)| line) {
parts.push(format!("{} is waiting on {}", label_of(line, inputs), forms(inputs.state).obj));
}
(!parts.is_empty()).then(|| clip(&format!("- the conversation: {}", parts.join("; ")), tuning.select.group_bytes))
// Each part a sentence of its own: the lists inside a part use semicolons, and a label may hold a comma.
let sentences: Vec<String> = parts.iter().map(|part| sentence(part)).collect();
(!sentences.is_empty()).then(|| clip(&format!("- the conversation: {}", sentences.join(" ")), tuning.select.group_bytes))
}
/// The pronouns of everyone the packet names from the room: `tuning.pronouns` by Matrix id (the persona state has no
/// pronoun field for another person; `presentation.pronouns` is hers alone), and they/them for anyone it does not list.
fn pronoun_line(inputs: &Inputs, tuning: &Tuning) -> Option<String> {
let mut seen: Vec<&str> = Vec::new();
let mut parts: Vec<String> = Vec::new();
/// Everyone the packet shows from the room, as (label, Matrix id), newest messages first, then the recent room lines:
/// whom the pronoun line names, and whom the Interpreter may mention.
fn people_in_view(inputs: &Inputs, tuning: &Tuning) -> Vec<(String, String)> {
let mut people: Vec<(String, String)> = Vec::new();
let messages = inputs.room.messages.iter();
let shown_tail = recent_lines(inputs).into_iter().rev().take(tuning.select.group_lines.max(tuning.select.beat_tail_lines));
for line in messages.chain(shown_tail) {
if line.sender == inputs.people.own_sender || seen.contains(&line.sender.as_str()) {
if line.sender == inputs.people.own_sender || people.iter().any(|(_, id)| *id == line.sender) {
continue;
}
seen.push(&line.sender);
let pronouns = tuning.pronouns.get(&line.sender).map_or(UNKNOWN_PRONOUNS, String::as_str);
parts.push(format!("{} \u{2014} {pronouns}", label_of(line, inputs)));
people.push((label_of(line, inputs), line.sender.clone()));
}
people
}
fn pronoun_line(inputs: &Inputs, tuning: &Tuning) -> Option<String> {
let parts: Vec<String> = people_in_view(inputs, tuning).iter().map(|(label, id)| format!("{label} \u{2014} {}", tuning.pronouns.get(id).map_or(UNKNOWN_PRONOUNS, String::as_str))).collect();
(!parts.is_empty()).then(|| clip(&format!("- pronouns: {}", parts.join("; ")), ALSO_HERE_BYTES))
}
/// A part as a sentence: its first letter upper-cased, a full stop after it.
fn sentence(part: &str) -> String {
let mut chars = part.chars();
chars.next().map_or_else(String::new, |first| format!("{}{}.", first.to_uppercase(), chars.as_str()))
}
/// Whom the Interpreter may address: everyone the packet shows from the room, then the swarm's other Minds, who are in
/// her rooms whether or not they spoke; a Mind she is bound to goes by the bond's label, any other by the id alone.
fn addressable(inputs: &Inputs, tuning: &Tuning) -> Vec<(String, String)> {
let mut people = people_in_view(inputs, tuning);
for mind in inputs.people.minds.iter().filter(|mind| *mind != inputs.people.own_sender) {
if !people.iter().any(|(_, id)| id == mind) {
people.push((bond_label(inputs, mind).unwrap_or_else(|| mind.clone()), mind.clone()));
}
}
people
}
fn speaking_rooms(inputs: &Inputs) -> Vec<String> {
let mut rooms: Vec<String> = inputs.room.here.iter().cloned().collect();
for line in &inputs.room.messages {
if !rooms.contains(&line.room) {
rooms.push(line.room.clone());
}
}
if rooms.is_empty()
&& let Some(line) = recent_lines(inputs).last()
{
rooms.push(line.room.clone());
}
rooms
}
/// The pronouns of a person nobody listed.
const UNKNOWN_PRONOUNS: &str = "they/them";
@ -1100,5 +1139,5 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet {
lines.extend(taken.iter().map(|candidate| candidate.line.clone()));
ids.extend(taken.iter().map(|candidate| candidate.id.clone()));
let selected = taken.into_iter().map(|candidate| Pick { id: candidate.id, pool: candidate.pool, log_score: candidate.log_score }).collect();
Packet { kind: inputs.kind, seed: inputs.seed, lens, identity, context: lines.join("\n"), stimulus, heard, overflow, present: present_names, ids, selected, forms: forms(inputs.state) }
Packet { kind: inputs.kind, seed: inputs.seed, lens, identity, context: lines.join("\n"), stimulus, heard, overflow, present: present_names, ids, selected, forms: forms(inputs.state), people: addressable(inputs, tuning), rooms: speaking_rooms(inputs) }
}

View file

@ -20,7 +20,7 @@ use chrono::{DateTime, Duration, Utc};
use serde::Deserialize;
use serde_json::{Value, json};
use super::harness::{Harness, Line, Message, RETELL_FLAG, cosine, pending_for, state_file, turn_markdown, usage_markdown, usage_total};
use super::harness::{Harness, Line, Message, OVERLAP_FLAG, cosine, pending_for, state_file, turn_markdown, usage_markdown, usage_total};
use super::select::Draw;
use super::tuning;
use super::{MouthReader, Organs, Request, Through, TurnKind, project};
@ -49,10 +49,10 @@ struct SimFile {
beat_s: u32,
#[serde(default = "two")]
round_minutes: i64,
/// An exchange in a room is its run of lines with no gap longer than this between one and the next; she is in it when
/// one of them is hers (`humans_pace`).
#[serde(default = "exchange_gap")]
exchange_gap_minutes: i64,
/// She is in a room's exchange when she has spoken there within this many minutes before the line (`humans_pace`):
/// others talking on does not keep her in it.
#[serde(default = "exchange_minutes")]
exchange_minutes: i64,
start: DateTime<Utc>,
rooms: Vec<String>,
#[serde(default)]
@ -74,8 +74,8 @@ fn two() -> i64 {
2
}
fn exchange_gap() -> i64 {
20
fn exchange_minutes() -> i64 {
30
}
fn ruled() -> WakePolicy {
@ -152,13 +152,15 @@ fn speaker(line: &Line, member: &Member, operators: &[String], cast: &[String])
}
/// For each line, how many Mind lines in a row came before it in its room since a human last spoke there.
fn mind_runs(lines: &[Line], cast: &[String]) -> Vec<usize> {
let mut run: BTreeMap<&str, usize> = BTreeMap::new();
fn mind_runs(lines: &[Line], cast: &[String], member: &Member) -> Vec<usize> {
let mut run = 0usize;
lines
.iter()
.map(|line| {
let before = run.get(line.room.as_str()).copied().unwrap_or(0);
run.insert(&line.room, if cast.contains(&line.from) { before + 1 } else { 0 });
let before = run;
if line.visible_to(&member.entry.id, &member.rooms) {
run = if cast.contains(&line.from) { run + 1 } else { 0 };
}
before
})
.collect()
@ -187,19 +189,10 @@ struct Woken {
/// Whether she spoke in the exchange a line continues: walking back through the lines of its room while each is within
/// `gap` of the one after it.
fn in_exchange(lines: &[Line], at: usize, her: &str, gap: Duration) -> bool {
let room = &lines[at].room;
let mut after = lines[at].at_ms;
for line in lines[..at].iter().rev().filter(|line| line.room == *room) {
if after.saturating_sub(line.at_ms) > u64::try_from(gap.num_milliseconds()).unwrap_or(0) {
return false;
}
if line.from == her {
return true;
}
after = line.at_ms;
}
false
fn in_exchange(lines: &[Line], at: usize, her: &str, window: Duration) -> bool {
let line = &lines[at];
let since = line.at_ms.saturating_sub(u64::try_from(window.num_milliseconds()).unwrap_or(0));
lines[..at].iter().any(|earlier| earlier.room == line.room && earlier.from == her && earlier.at_ms >= since)
}
#[allow(clippy::too_many_arguments, reason = "one wake decision's inputs, each a plan or room fact")]
@ -237,13 +230,6 @@ fn cause_word(cause: Option<WakeCause>) -> &'static str {
}
}
/// The cast members a said line tags: an `@handle` of theirs in the text. The stand-in for the Interpreter's explicit
/// addressee (C3b), which will carry a Matrix mention; a name without the `@` tags no one.
fn explicit_mentions(says: &str, cast: &[String]) -> Vec<String> {
let tagged: Vec<String> = says.split(|c: char| !(c.is_alphanumeric() || c == '@' || c == '_' || c == '-' || c == '.')).filter_map(|word| word.strip_prefix('@')).map(|handle| handle.trim_end_matches('.').to_lowercase()).collect();
cast.iter().filter(|id| tagged.iter().any(|handle| handle == &localpart(id).to_lowercase() || id.to_lowercase() == format!("@{handle}"))).cloned().collect()
}
/// 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);
@ -302,6 +288,9 @@ fn cast_of(plan: &SimFile, base: &Path) -> Result<Vec<Member>> {
if message.dm && message.to.as_ref().is_none_or(|to| !plan.cast.iter().any(|entry| entry.id == *to)) {
bail!("script message {} is a direct message: name its recipient, a cast member, in `to`", at + 1);
}
if message.dm && plan.rooms.contains(&message.room) {
bail!("script message {} is a direct message in {}, one of the plan's rooms: a direct message has its own room", at + 1, message.room);
}
}
Ok(members)
}
@ -333,8 +322,7 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize)
for round in 1..=plan.rounds {
scripted(round, &mut lines, &mut rounds_of, &mut causes)?;
let round_at = at_round(round);
let runs = mind_runs(&lines, &cast);
let mut woke: Vec<(usize, Option<Woken>)> = members.iter().enumerate().filter_map(|(at, member)| woken(&lines, &runs, seen[at], member, &plan.operators, &cast, plan.wake, Duration::minutes(plan.exchange_gap_minutes)).map(|why| (at, Some(why)))).collect();
let mut woke: Vec<(usize, Option<Woken>)> = members.iter().enumerate().filter_map(|(at, member)| woken(&lines, &mind_runs(&lines, &cast, member), seen[at], member, &plan.operators, &cast, plan.wake, Duration::minutes(plan.exchange_minutes)).map(|why| (at, Some(why)))).collect();
// The beat, as the heartbeat's: when it has fallen, one more wake for the winner of `tempo::pick` among those
// not already woken.
if plan.beat_s > 0 && (round_at - last_beat).num_seconds() >= i64::from(plan.beat_s) {
@ -378,7 +366,7 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize)
minds: cast.clone(),
own_sender: id.clone(),
tuning: harness.tuning.clone(),
through: Through::Persona,
through: Through::Interpreter,
last_heard: vec![],
recent: recent[at].clone(),
};
@ -393,16 +381,24 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize)
recent[at].push(selected.iter().filter_map(Value::as_str).map(str::to_owned).collect());
}
// A failed call is a failure, never a silence.
let failed = report.get("error").or_else(|| render.get("failed")).or_else(|| render["persona"].get("failed")).map(|fault| fault.as_str().unwrap_or_default().to_owned());
let said: Vec<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(" "));
let failed = report.get("error").or_else(|| render.get("failed")).or_else(|| render["persona"].get("failed")).or_else(|| render["interpreter"].get("failed")).map(|fault| fault.as_str().unwrap_or_default().to_owned());
// What she says is the Interpreter's decision alone; who it wakes is its structured mentions and reply target.
let said: Vec<Value> = if failed.is_some() { vec![] } else { render["interpreter"]["said"].as_array().cloned().unwrap_or_default() };
let cause = cause_word(why.as_ref().map(|why| why.cause));
if let Some(says) = &line {
let mentions = explicit_mentions(says, &cast);
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, dm_to: dm_to.clone(), follow_up: false, reply_to: None, new: false });
let mut spoken: Vec<String> = Vec::new();
for (offset, decided) in said.iter().enumerate() {
let room = decided["room"].as_str().unwrap_or(&here).to_owned();
let says = decided["text"].as_str().unwrap_or_default().to_owned();
let mentions: Vec<String> = decided["mentions"].as_array().map(|ids| ids.iter().filter_map(Value::as_str).filter(|id| cast.contains(&(*id).to_owned()) || plan.operators.contains(&(*id).to_owned())).map(str::to_owned).collect()).unwrap_or_default();
let reply_to = decided["reply_to"].as_str().and_then(|person| lines.iter().rposition(|line| line.from == person && line.room == room));
let at_ms = u64::try_from(now.timestamp_millis()).unwrap_or_default() + offset as u64;
let to = if room == here { dm_to.clone() } else { None };
lines.push(Line { from: id.clone(), name: member.entry.name.clone(), room, at_ms, says: says.clone(), mentions, dm_to: to, follow_up: false, reply_to, new: false });
rounds_of.push(round);
causes.push(Some(cause));
spoken.push(says);
}
let line = (!spoken.is_empty()).then(|| spoken.join(" / "));
let outcome = if failed.is_some() { "failed" } else if line.is_some() { "spoke" } else { "silent" };
let (how, by) = why.map_or((None, vec![]), |why| (why.how, why.by));
let turn = json!({ "round": round, "persona": member.entry.name, "id": id, "woke": cause, "how": how, "addressed_by": by, "kind": kind, "room": here, "outcome": outcome, "failed": failed, "said": line, "report": report });
@ -526,7 +522,7 @@ fn metrics(members: &[Member], lines: &[Line], rounds_of: &[usize], causes: &[Op
let overlaps: Vec<f64> = turns.iter().filter_map(|turn| turn["report"]["renders"][0]["heard_overlap"].as_f64()).collect();
let narration = json!({
"mean_heard_overlap": if overlaps.is_empty() { Value::Null } else { json!(overlaps.iter().sum::<f64>() / overlaps.len() as f64) },
"retelling": overlaps.iter().filter(|overlap| **overlap > RETELL_FLAG).count(),
"high_overlap": overlaps.iter().filter(|overlap| **overlap > OVERLAP_FLAG).count(),
"offers_options": turns.iter().filter(|turn| turn["report"]["renders"][0]["offers_options"] == true).count(),
"narrations": overlaps.len(),
});
@ -586,15 +582,15 @@ fn markdown(plan_path: &Path, plan: &SimFile, members: &[Member], group_state: O
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, "> **Explicit addressee, stand-in.** Whom a line is to is the Interpreter's to say (C3b). Until then a said line that holds `@handle` of a cast member counts as a Matrix mention of that member; any other name in it is prose and tags no one.\n");
let _ = writeln!(out, "> **Speech is the Interpreter's.** What she says is decided from her prose by the Interpreter's speech step (C3b, one tool-less structured call), held by code to her prose: each line rests on a verbatim span of it, in a room she may speak in, and is never someone else's words. Its structured mentions and reply target are the only explicit addressees; an `@` in the text tags no one.\n");
let _ = writeln!(out, "- plan: `{}`", plan_path.display());
let _ = writeln!(out, "- cast: {}", members.iter().map(|member| format!("{} (`{}`, `{}`, names from {})", member.entry.name, member.entry.id, member.entry.state.display(), member.names_from)).collect::<Vec<_>>().join(", "));
let _ = writeln!(out, "- rooms: {}; rounds: {}; repeats: {}; seed: {}; minutes a round: {}; an exchange ends at a gap over {} min", plan.rooms.join(", "), plan.rounds, plan.repeats, plan.seed, plan.round_minutes, plan.exchange_gap_minutes);
let _ = writeln!(out, "- rooms: {}; rounds: {}; repeats: {}; seed: {}; minutes a round: {}; she is in a room's exchange when she spoke there within {} min", plan.rooms.join(", "), plan.rounds, plan.repeats, plan.seed, plan.round_minutes, plan.exchange_minutes);
let owners: Vec<String> = members.iter().filter(|member| !member.entry.own_rooms.is_empty()).map(|member| format!("{}: {}", member.entry.name, member.entry.own_rooms.join(", "))).collect();
let _ = writeln!(out, "- own rooms (from the plan: neither the heartbeat's table nor `persona.env` records room ownership): {}", if owners.is_empty() { "none".to_owned() } else { owners.join("; ") });
let _ = writeln!(out, "- wake: {}; beat: {}", policy_words(plan.wake), if plan.beat_s == 0 { "off".to_owned() } else { format!("every {} s of simulated time, one Persona by `tempo::pick`", plan.beat_s) });
let _ = writeln!(out, "- group state in the Projector's packet: {}", match group_state { Some(true) => "on", Some(false) => "off", None => "unknown (the tuning did not load)" });
let _ = writeln!(out, "- speech: the double-quoted spans of her prose, joined, posted to the room she woke in; none is silence. A preview of the Interpreter's speech, not its decision");
let _ = writeln!(out, "- speech: the Interpreter's lines, each posted to its room; none is silence");
let _ = writeln!(out, "- wakes are counted per wake, by its strongest cause: addressed by a human, a human line, addressed by a Mind, a Mind's line (control only), the beat. A Mind-addressed wake's evidence is its strongest: a Matrix mention or reply, her id written as text, a name in the text");
let _ = writeln!(out, "- silence rate is per woken turn that did not fail; a failed call is counted apart. Reply chains are per room: Mind lines spoken in turns another Mind's line woke, in a row");
let _ = writeln!(out, "- convergence is the mean pairwise cosine of speakers' word counts (lexical, cheap); work-talk share is a lexical heuristic over a fixed word list, not a classifier");
@ -621,7 +617,7 @@ fn markdown(plan_path: &Path, plan: &SimFile, members: &[Member], group_state: O
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): {}", number(&metrics["work_talk_share"]));
let narration = &metrics["narration"];
let _ = writeln!(out, "- narrations: {}; mean overlap with what she heard {}; retelling (over {RETELL_FLAG}) {}; offering her options {}\n", narration["narrations"], number(&narration["mean_heard_overlap"]), narration["retelling"], narration["offers_options"]);
let _ = writeln!(out, "- narrations: {}; mean lexical overlap with what she heard {}; over {OVERLAP_FLAG}: {}; offering her options {}\n", narration["narrations"], number(&narration["mean_heard_overlap"]), narration["high_overlap"], narration["offers_options"]);
let _ = writeln!(out, "| persona | wakes | messages | failed | silence rate | addressed |\n|---|---|---|---|---|---|");
if let Some(personas) = metrics["personas"].as_object() {
for (name, row) in personas {

View file

@ -4,6 +4,7 @@ use serde_json::Value;
use super::*;
use crate::projection::harness::tests::{Scripted, control};
use serde_json::json;
use crate::projection::{Answer, Failed, Model, Spent};
const TOBIAH: &str = "@eureka:gamecult.org";
@ -238,19 +239,59 @@ fn a_seed_replays_its_order_and_another_seed_changes_it() {
assert_eq!(turns(&run_plan(&path, &chatty()).1), turns(&run_plan(&path, &chatty()).1));
}
/// Tobiah says a line that names Libby and holds her id as text; the Interpreter gives it `mentions` or not.
fn tagging(mention: bool) -> Scripted {
let mentions = if mention { json!([LIBBY]) } else { json!([]) };
Scripted::new(|prompt| if is_tobiah(prompt) { "\"ask @libby:gamecult.org, and Libby too\"".into() } else { "She lets it be.".into() }).interpreting(move |prompt| {
if !prompt.contains("Identity:\nTobiah") {
return json!({ "say": [] }).to_string();
}
json!({ "say": [{ "room": "#minds:gamecult.org", "text": "ask @libby:gamecult.org, and Libby too", "source_quote": "ask @libby:gamecult.org, and Libby too", "mentions": mentions, "reply_to": null }] }).to_string()
})
}
#[test]
fn an_at_handle_in_a_said_line_is_a_mention_and_its_wakes_are_counted_once() {
fn only_the_interpreters_structured_mention_is_a_mention_and_an_at_in_the_text_is_not() {
let dir = tempfile::tempdir().unwrap();
let model = Scripted::new(|prompt| if is_tobiah(prompt) { "\"ask @libby:gamecult.org, and Libby too\"".into() } else { "\"Tobiah?\"".into() });
assert_eq!(explicit_mentions("ask @libby:gamecult.org, and Libby too", &[TOBIAH.into(), LIBBY.into()]), [LIBBY.to_owned()]);
assert_eq!(explicit_mentions("@Libby, tea? Tobiah says hi", &[TOBIAH.into(), LIBBY.into()]), [LIBBY.to_owned()], "an @handle tags, a bare name does not");
let (_, run) = run_plan(&plan(dir.path(), "\"ruled\"", 3, ""), &model);
let addressed = &run["metrics"]["mind_addressed_wakes"];
let libby_mind_wakes = turns(&run).iter().filter(|(_, who, why)| who == "Libby" && why == "addressed_by_mind").count();
assert!(libby_mind_wakes >= 1, "{:?}", turns(&run));
assert_eq!(addressed["total"].as_u64().unwrap() as usize, turns(&run).iter().filter(|(_, _, why)| why == "addressed_by_mind").count());
assert_eq!(addressed["mention_or_reply"].as_u64().unwrap() as usize, libby_mind_wakes, "the stand-in mention is the strongest evidence of each wake, once per wake: {addressed}");
assert_eq!(addressed["id_as_text"].as_u64(), Some(0));
let (_, plain) = run_plan(&plan(dir.path(), "\"ruled\"", 3, ""), &tagging(false));
let addressed = &plain["metrics"]["mind_addressed_wakes"];
assert_eq!(addressed["mention_or_reply"].as_u64(), Some(0), "an @id in the words tags no one: {addressed}");
let dir = tempfile::tempdir().unwrap();
let (_, tagged) = run_plan(&plan(dir.path(), "\"ruled\"", 3, ""), &tagging(true));
let addressed = &tagged["metrics"]["mind_addressed_wakes"];
let libby_mind_wakes = turns(&tagged).iter().filter(|(_, who, why)| who == "Libby" && why == "addressed_by_mind").count();
assert_eq!(addressed["mention_or_reply"].as_u64().unwrap() as usize, libby_mind_wakes, "{addressed}");
// Under humans_pace a name or an @ in the words wakes no one; the structured mention does.
let dir = tempfile::tempdir().unwrap();
let path = plan(dir.path(), "{ humans_pace = 4 }", 3, "");
let opening = "says = \"good afternoon, everyone\"\n";
std::fs::write(&path, std::fs::read_to_string(&path).unwrap().replace(opening, &format!("{opening}mentions = [\"{TOBIAH}\"]\n"))).unwrap();
let woken_libby = |model: &Scripted| turns(&run_plan(&path, model).1).iter().any(|(_, who, _)| who == "Libby");
assert!(!woken_libby(&tagging(false)));
assert!(woken_libby(&tagging(true)));
}
#[test]
fn a_direct_message_in_one_of_the_plans_rooms_is_refused() {
let dir = tempfile::tempdir().unwrap();
let path = plan(dir.path(), "\"ruled\"", 2, "");
let dm = "[[message]]\nround = 1\nfrom = \"@meta:gamecult.org\"\nname = \"Meta\"\nroom = \"#minds:gamecult.org\"\ndm = true\nto = \"@libby:gamecult.org\"\nsays = \"psst\"\n";
std::fs::write(&path, format!("{}{dm}", std::fs::read_to_string(&path).unwrap())).unwrap();
let model = silent();
let harness = Harness { instance: "eureka".into(), persona: None, model: Some(&model), tuning: dir.path().join("persona-tuning.toml") };
assert!(format!("{:#}", simulate(&harness, &path).unwrap_err()).contains("has its own room"));
}
#[test]
fn the_mind_run_is_counted_across_her_rooms_and_an_uncounted_run_is_at_the_cap() {
let her = Member { names_from: "test", addressee: Addressee::new(LIBBY, ["libby"]), entry: CastEntry { id: LIBBY.into(), name: "Libby".into(), state: PathBuf::new(), rooms: vec![], own_rooms: vec![] }, state: Value::Null, rooms: vec!["#a:x".into(), "#b:x".into()] };
let cast = vec![TOBIAH.to_owned(), LIBBY.to_owned(), "@kiko:x".to_owned()];
let line = |from: &str, room: &str| Line { from: from.into(), name: String::new(), room: room.into(), at_ms: 0, says: "hi".into(), mentions: vec![], dm_to: None, follow_up: false, reply_to: None, new: false };
let lines = [line("@meta:x", "#a:x"), line(TOBIAH, "#a:x"), line("@kiko:x", "#b:x"), line(TOBIAH, "#b:x"), line("@meta:x", "#c:x")];
assert_eq!(mind_runs(&lines, &cast, &her), vec![0, 0, 1, 2, 3], "one run across #a and #b; a room she is not in does not break it");
assert_eq!(LineFacts::default().mind_run, usize::MAX, "fail-safe: an uncounted run is at the cap");
let tagged = Line { mentions: vec![LIBBY.into()], ..line(TOBIAH, "#a:x") };
assert_eq!(wakes(WakePolicy::HumansPace(4), &her.addressee, &tagged.delivery(0, LIBBY, &[]), Speaker::Mind, LineFacts::default()), None);
}
#[test]
@ -259,7 +300,7 @@ fn the_report_names_its_gaps_and_carries_representative_lines_and_usage() {
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(), "\"ruled\"", 3, "")).unwrap();
for part in ["# Swarm simulation", "State does not evolve during a run", "Explicit addressee, stand-in", "group state in the Projector's packet: on", "### Representative lines", "**Meta (human)**: good afternoon, everyone", "**Tobiah** _(woke: human)_: what a day", "### Wakes by round", "| 1 | 0 | 2 | 0 | 0 | 0 |", "## Total usage", "| claude-sonnet-5-5 | 4 |"] {
for part in ["# Swarm simulation", "State does not evolve during a run", "Speech is the Interpreter's", "group state in the Projector's packet: on", "### Representative lines", "**Meta (human)**: good afternoon, everyone", "**Tobiah** _(woke: human)_: what a day", "### Wakes by round", "| 1 | 0 | 2 | 0 | 0 | 0 |", "## Total usage", "| claude-sonnet-5-5 | 6 |"] {
assert!(report.contains(part), "{part}\n{report}");
}
}

View file

@ -1347,11 +1347,15 @@ fn every_person_the_packet_names_from_the_room_has_pronouns_and_an_unknown_one_i
fn the_projector_is_told_not_to_retell_not_to_offer_options_and_not_to_owe() {
let packet = packet_of(TurnKind::Chat, &state(), &chat_room(), &History::default(), 1);
let prompt = narrate::projector_prompt(&packet, &Tuning::default());
for rule in ["never retell or summarise it", "Do not weigh options", "never as a debt", "an absence is not part of her moment", "pronouns"] {
for absent in ["no task is waiting", "owed", "debt"] {
assert!(!prompt.contains(absent), "the guidance names no duty, even to deny one: {absent}");
}
for rule in ["never retell or summarise it", "Do not weigh options", "an absence is not part of her moment", "pronouns"] {
assert!(prompt.contains(rule), "{rule}");
}
let persona = narrate::persona_prompt(&packet, "The room is warm.", &Tuning::default());
assert!(persona.contains("Continue her inner stream") && persona.contains("This is not a message to the room") && persona.contains("most moments pass with nothing said"), "{persona}");
assert!(!persona.contains("quotation marks") && !persona.contains("@"), "no speech convention: the Interpreter decides what is said");
}
#[test]
@ -1368,11 +1372,6 @@ fn the_narration_checks_flag_an_option_menu_and_a_retelling_and_pass_a_reaction(
assert!(narrate::heard_overlap(reaction, heard) < 0.05);
}
#[test]
fn inner_is_her_prose_without_what_she_says_aloud() {
assert_eq!(narrate::inner("She grins. \"Winter, still.\" Then she lets it go."), "She grins. \u{2026} Then she lets it go.");
assert_eq!(narrate::inner("Only thought here."), "Only thought here.");
}
#[test]
fn the_group_state_reads_one_room_and_never_folds_a_direct_message_in() {
@ -1402,12 +1401,14 @@ fn the_packet_speaks_of_her_in_her_own_pronouns() {
}
#[test]
fn a_room_given_by_its_matrix_id_is_read_by_its_alias() {
fn a_room_is_named_by_the_key_the_tail_carries_and_a_public_rooms_id_is_reported_not_guessed() {
let dir = tempfile::tempdir().unwrap();
let stored = Stored(state());
let chat = mouth();
let organs = Organs { persona: Some(&stored), chat: Some(&chat), addressing: None, model: None };
// The mouth's first delivery is in #swarm, room id !1:gamecult.org; its tail line carries the alias.
let report = project(&organs, &Request { room: Some("!1:gamecult.org".into()), ..request(dir.path(), None, None) }, now()).unwrap();
assert_eq!((report["room"]["messages"].as_u64(), report["room"]["tail"].as_u64()), (Some(1), Some(1)), "{}", report["room"]);
// The mouth's first delivery is in #swarm, room id !1:gamecult.org; the tail names #swarm by its alias.
let by_alias = project(&organs, &Request { room: Some("#swarm:gamecult.org".into()), ..request(dir.path(), None, None) }, now()).unwrap();
assert_eq!((by_alias["room"]["messages"].as_u64(), by_alias["room"]["tail"].as_u64()), (Some(1), Some(1)), "{}", by_alias["room"]);
let by_id = project(&organs, &Request { room: Some("!1:gamecult.org".into()), ..request(dir.path(), None, None) }, now()).unwrap();
assert!(by_id["room"]["why"].as_str().unwrap().contains("named by its alias"), "{}", by_id["room"]);
}

View file

@ -20,10 +20,14 @@ pub const TUNING_FILE: &str = "persona-tuning.toml";
/// The Persona stage's setting (map section 2): she continues the Projector's paragraph, her interior, with speech only
/// where her thought moves her to it, quoted (map section 5.5, invariant 5). It is not a reply to the room.
const PERSONA_GUIDANCE: &str = "Continue her inner stream where the paragraph leaves off: her own next thoughts, in the first person. This is not a message to the room. Speech is rare: words she says aloud appear in double quotation marks, and only when something in her thought moves her to say them; anyone else's words she recalls go in single quotation marks. In a busy room most moments pass with nothing said. To make sure someone hears her, she writes @ and their handle in what she says. Nothing else she thinks leaves her head.";
const PERSONA_GUIDANCE: &str = "Continue her inner stream where the paragraph leaves off: her own next thoughts, in the first person, in her own words. This is not a message to the room. If she means to say something aloud, she thinks it as she would, what and to whom; whether anything is said, and what, is decided after her. In a busy room most moments pass with nothing said.";
/// 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. What was said reaches her word for word after this stream, so never retell or summarise it: render what it stirs in her and what she makes of the people who said it. Render the conversation as it stands, never as a debt: nobody is owed an answer unless a person is waiting on her by name, and a question others have answered is covered. Do not weigh options or list what she might do or say; what she does is hers to decide. Leave out whatever the context does not carry: an absence is not part of her moment. Use each person's pronouns as given.";
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. Render the moment as she lives it: who is here and how they sit with her, what she carries, what she wants. What was said reaches her word for word after this stream, so never retell or summarise it: render what it stirs in her and what she makes of the people who said it. Render the conversation as it stands. Do not weigh options or list what she might do or say; what she does is hers to decide. Leave out whatever the context does not carry: an absence is not part of her moment. Use each person's pronouns as given.";
/// The Interpreter's speech step (map section 4, C3b): its scope and rules beside Ghostlight's membrane. It has no tools;
/// what the membrane calls the gap tool is the `gaps` list of the JSON.
const INTERPRETER_GUIDANCE: &str = "This step decides only what she says aloud in chat this turn, from her prose. No tool is available in this step: report every translation and every gap in the JSON object the contract describes, and return that object alone. A line is speech only when her prose shows her choosing to say it now, to the room or to someone in it. Words she recalls, quotes, or heard from someone else are never her speech, even when her prose repeats them. Thinking about saying something, deciding to stay quiet, or saying that she says nothing, is not speech. Write each line as she would type it, in her voice, without @handles or quotation marks around it, and copy into source_quote the exact words of her prose the line rests on, character for character. Name in mentions, by Matrix id from the people listed, only someone she addresses directly; in reply_to, the Matrix id of the person whose message she answers, when she answers one. The room is one of the rooms listed. An empty say list is the usual answer in a busy room.";
/// The context room the fixed slots need beside the identity: the hour and the mood lines always fit.
const FIXED_ROOM: usize = 256;
@ -45,6 +49,7 @@ const GROUP_LINES_MAX: usize = 40;
pub struct Tuning {
pub projector: ProjectorTuning,
pub persona: PersonaTuning,
pub interpreter: InterpreterTuning,
pub select: SelectTuning,
pub lens: LensWeights,
/// Each person's pronouns by Matrix id, for everyone the packet names: the persona schema has no pronoun field for
@ -72,6 +77,7 @@ impl Default for Tuning {
Self {
projector: ProjectorTuning::default(),
persona: PersonaTuning::default(),
interpreter: InterpreterTuning::default(),
select: SelectTuning::default(),
lens: LensWeights::default(),
pronouns: PRONOUNS.iter().map(|(id, pronouns)| ((*id).to_owned(), (*pronouns).to_owned())).collect(),
@ -171,6 +177,20 @@ impl Default for ProjectorTuning {
}
}
/// The Interpreter's speech call: its model and its guidance.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct InterpreterTuning {
pub model: String,
pub guidance: String,
}
impl Default for InterpreterTuning {
fn default() -> Self {
Self { model: "claude-sonnet-5-5".into(), guidance: INTERPRETER_GUIDANCE.into() }
}
}
impl Default for PersonaTuning {
fn default() -> Self {
Self { model: "claude-sonnet-5-5".into(), guidance: PERSONA_GUIDANCE.into(), words: 180 }
@ -225,6 +245,9 @@ impl Tuning {
if self.projector.model.trim().is_empty() {
bail!("projector.model must name a model");
}
if self.interpreter.model.trim().is_empty() {
bail!("interpreter.model must name a model");
}
if self.persona.model.trim().is_empty() {
bail!("persona.model must name a model");
}
@ -267,6 +290,7 @@ impl Tuning {
("select.group_bytes", select.group_bytes),
("projector.guidance", self.projector.guidance.len()),
("persona.guidance", self.persona.guidance.len()),
("interpreter.guidance", self.interpreter.guidance.len()),
] {
if bytes > PART_BYTES_MAX {
bail!("{name} must be at most {PART_BYTES_MAX} bytes, not {bytes}: the prompt must fit the connector's frame");

View file

@ -153,6 +153,7 @@ mind_body::heartbeat: fn poll (organs : & Organs , config : & Config) -> Result
mind_body::heartbeat: impl Config
mind_body::heartbeat: impl Config: fn new (personas : Vec < PersonaId > , operators : Vec < String > , beat_s : u32) -> Result < Self >
mind_body::heartbeat: impl Control for FileControl
mind_body::heartbeat: impl Default for LineFacts
mind_body::heartbeat: impl FileStore
mind_body::heartbeat: impl FileStore: fn check (& self) -> Result < () >
mind_body::heartbeat: impl FileStore: fn latest (& self) -> Result < Option < PollTrace > >
@ -234,7 +235,7 @@ 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: # [derive (Clone , Copy , Debug , Default , PartialEq , Eq , serde :: Deserialize)] # [serde (rename_all = "snake_case")] pub enum Through { # [default] Projector , Persona , Interpreter , }
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
@ -286,6 +287,7 @@ 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 InterpreterTuning
mind_body::projection::tuning: impl Default for LensWeights
mind_body::projection::tuning: impl Default for PersonaTuning
mind_body::projection::tuning: impl Default for PoolCaps
@ -293,13 +295,14 @@ mind_body::projection::tuning: impl Default for ProjectorTuning
mind_body::projection::tuning: impl Default for SelectTuning
mind_body::projection::tuning: impl Default for Tuning
mind_body::projection::tuning: impl Tuning
mind_body::projection::tuning: struct InterpreterTuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[model: String, guidance: String]
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, group_state: bool, group_lines: usize, group_bytes: usize, caps: PoolCaps]
mind_body::projection::tuning: struct Tuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[projector: ProjectorTuning, persona: PersonaTuning, select: SelectTuning, lens: LensWeights, pronouns: BTreeMap < String , String >]
mind_body::projection::tuning: struct Tuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[projector: ProjectorTuning, persona: PersonaTuning, interpreter: InterpreterTuning, select: SelectTuning, lens: LensWeights, pronouns: BTreeMap < String , String >]
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