Projector (C3a): Select stage, Projector narration, persona-tuning.toml, mind-persona project

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-19 22:16:18 +01:00
commit be3ca005f5
10 changed files with 2186 additions and 14 deletions

View file

@ -23,6 +23,10 @@ publish.workspace = true
# two until the workspace pin and the kit's meet.
# `rmp-serde` is also the lease wire (`lease_client`): the connector's MessagePack frames.
#
# Ghostlight's `ghostlight-persona-projection`, pinned to a revision, builds the Projector's prompt (`projection`), so
# the membrane's text stays Ghostlight's; the Projector asks through the connector kit's client (`model-connector`).
# `toml` reads `persona-tuning.toml`, the Projector's knobs (`projection::tuning`); parse and serde only.
#
# `serde_json` reads Claude Code's stream-json, a foreign owner's wire (`self_run`);
# `uuid`'s v5 makes a Self run's session id derivable from the run's id.
[dependencies]
@ -33,12 +37,14 @@ cultcache-rs.workspace = true
cultnet-rs.workspace = true
eureka-pipeline = { path = "../eureka-pipeline" }
eureka-state = { path = "../eureka-state", default-features = false }
ghostlight-persona-projection = { git = "https://github.com/GameCult/Ghostlight.git", rev = "c43eb54962ed4a09cc737ecac73a16b0d7ce62b4", package = "ghostlight-persona-projection" }
huginn-mind = { path = "../huginn-mind" }
model-connector = { git = "https://eureka.gamecult.org/forge/GameCult/ModelConnector.git", rev = "c1802d85bee3ae04b0c5737563c8343c1fef515d", package = "model-connector" }
rmp-serde = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sha2 = "0.10"
toml = { version = "0.9", default-features = false, features = ["std", "serde", "parse"] }
unicode-general-category = "1"
uuid = { version = "1", default-features = false, features = ["v5"] }
@ -58,6 +64,9 @@ proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["full"] }
tempfile = "3"
# A real kit server (`serve` over a scripted `ProviderBackend`) proves the Projector's connector wire, not a fake of it.
# Same revision as the dependency; the `daemon` feature is test-only.
model-connector = { git = "https://eureka.gamecult.org/forge/GameCult/ModelConnector.git", rev = "c1802d85bee3ae04b0c5737563c8343c1fef515d", package = "model-connector", features = ["daemon"] }
[lib]
name = "mind_body"

View file

@ -12,15 +12,40 @@
//! `persona.env`) are still parsed, so a start refuses the configuration the turn
//! 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_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` and `MIND_PERSONA_SELF_SENDER`; 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.
use std::path::Path;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::time::Duration;
use anyhow::Result;
use anyhow::{Context, Result, anyhow, bail};
use eureka_state::HuginnClient;
use mind_body::chat_client::Chat;
use mind_body::chat_rudp::{RudpChat, connection_id};
use mind_body::cli::instance_and_endpoint;
use mind_body::control::{CONTROL_ROOT, FileSource, control_path, swarm_path};
use mind_body::persona::{Config, carry};
use mind_body::projection::{HuginnPersonaSource, KitModel, Model, Organs as ProjectOrgans, Request as ProjectRequest, TUNING_FILE, TUNING_ROOT, TurnKind, connector_key, project};
use mind_body::queue::HuginnMind;
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>]]";
/// 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.
const CHAT_TIMEOUT: Duration = Duration::from_secs(20);
/// How long the kit may take to answer one Projector or judge turn.
const MODEL_TIMEOUT: Duration = Duration::from_secs(300);
// The binary is the one place that reads the process: its arguments and
// the environment. The library reads neither (clippy.toml, `#![forbid]` in lib.rs).
@ -39,7 +64,102 @@ fn run() -> Result<String> {
Ok(carry(&control, &swarm, &instance).to_string())
}
fn required(name: &str) -> Result<String> {
setting(name).ok_or_else(|| anyhow!("{name} is not set"))
}
/// The text of the credential file named by `name`; never echoed.
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> {
std::env::var_os("CREDENTIALS_DIRECTORY").map(PathBuf::from).ok_or_else(|| anyhow!("CREDENTIALS_DIRECTORY is not set"))
}
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 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() {
"chat" => TurnKind::Chat,
"beat" => TurnKind::Beat,
"reflection" => TurnKind::Reflection,
_ => 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()?)?),
"--select-only" => select_only = true,
"--instance" | "--endpoint" => rest.extend([flag.clone(), value()?]),
_ => bail!("{PROJECT_USAGE}"),
}
}
if select_only && (renders.is_some() || judge.is_some()) {
bail!("{PROJECT_USAGE}");
}
let (instance, address) = instance_and_endpoint(&rest, setting("HUGINN_ENDPOINT"), PROJECT_USAGE)?;
let now = chrono::Utc::now();
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));
let mind = HuginnMind::new(HuginnClient::new(address, instance.clone(), TIMEOUT));
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>"))?;
Some(RudpChat::new(endpoint, connection_id(&credential("BIFROST_CHAT_KEY_FILE")?), instance.0.clone(), CHAT_TIMEOUT))
}
_ => 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 organs = ProjectOrgans { persona: Some(&persona), mind: Some(&mind), chat: chat.as_ref().map(|chat| chat as &dyn Chat), model: model.as_ref().map(|model| model as &dyn Model) };
let request = ProjectRequest {
instance: instance.0.clone(),
kind,
state,
room,
seed: seed.unwrap_or_else(|| u64::try_from(now.timestamp_millis()).unwrap_or_default()),
renders,
judge_against: judge,
operators: setting("MIND_PERSONA_OPERATOR").map(|ids| ids.split(',').map(str::to_owned).collect()).unwrap_or_default(),
own_sender: setting("MIND_PERSONA_SELF_SENDER").unwrap_or_default(),
tuning,
};
Ok(serde_json::to_string_pretty(&project(&organs, &request, now)?)?)
}
#[allow(clippy::disallowed_methods, reason = "the binary reads its own arguments")]
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
if args.first().map(String::as_str) == Some("project") {
match run_project(&args[1..]) {
Ok(report) => println!("{report}"),
Err(error) => {
eprintln!("mind-persona project: {error:#}");
std::process::exit(2);
}
}
return;
}
match run() {
Ok(outcome) => println!("{outcome}"),
Err(error) => {

View file

@ -15,6 +15,10 @@
//! until the three-stage turn lands it carries no turn. It keeps what a turn's input is made of
//! (`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.
//!
//! `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
//! `mind-self-run` binary runs `claude` over stream-json for her one open Self
@ -40,8 +44,8 @@ pub mod heartbeat;
pub mod launch;
pub mod lease_client;
pub mod persona;
pub mod projection;
pub mod queue;
#[allow(dead_code, reason = "kept for the three-stage turn's Select stage (map C3 reuses recall.rs): its first caller removes this allow")]
mod recall;
pub mod self_run;
pub mod tempo;

View file

@ -0,0 +1,290 @@
//! The Projector: her persona state and her room, turned into the lived stream a Persona continues.
//!
//! Two stages. The Select stage (`select`) is code: it decides what she can feel this turn and renders it as a typed
//! projection packet, deterministic for a seed. The narration (`narrate`) is one tool-less model call through the
//! connector that renders the packet as first-person prose under Ghostlight's membrane (`build_projector_prompt`,
//! the pinned `ghostlight-persona-projection`). Every knob of both is `persona-tuning.toml` (`tuning`), read at each
//! wake, its hash carried with the packet.
//!
//! `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. It has no port that writes: it puts no persona state,
//! admits nothing, sends and acks no chat and appends no trace, so it runs under a held brake.
mod narrate;
mod select;
mod tuning;
use std::collections::BTreeMap;
use std::path::PathBuf;
use anyhow::{Result, anyhow, bail};
use chrono::{DateTime, Utc};
use eureka_pipeline::PipelineKind;
use eureka_state::HuginnClient;
use huginn_mind::HuginnMindResponse;
use serde_json::{Value, json};
use crate::chat_client::{Chat, Form};
use crate::queue::MindPort;
use crate::recall;
pub use narrate::{KitModel, Model, 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 select::{History, Inputs, Packet, People, Recalled, Room, RoomLine, select};
/// The agent name on the connector's request ids.
const AGENT: &str = "mind-persona";
/// The touch of a recalled item by the lane that brought it.
const TOUCH_ABOUT: f64 = 1.0;
const TOUCH_SALIENT: f64 = 0.3;
const TOUCH_RELEVANT_UNSCORED: f64 = 0.6;
/// The judge passes at this share of narrations attributed to the right packet.
const JUDGE_PASS: f64 = 0.8;
/// Her persona state, read and never put.
pub trait PersonaSource {
/// Her persona state and the receipt it was stored under.
fn persona(&self) -> Result<(Value, String)>;
}
/// Her persona state from her daemon.
pub struct HuginnPersonaSource {
client: HuginnClient,
}
impl HuginnPersonaSource {
pub fn new(client: HuginnClient) -> Self {
Self { client }
}
}
impl PersonaSource for HuginnPersonaSource {
fn persona(&self) -> Result<(Value, String)> {
match self.client.persona_get().map_err(|_| anyhow!("the mind could not be asked for the persona"))? {
HuginnMindResponse::Persona(Some(view)) => Ok((view.value, view.receipt_id)),
HuginnMindResponse::Persona(None) => bail!("the mind holds no persona state"),
_ => bail!("the mind answered the persona request with something else"),
}
}
}
/// What `project` reads through. None of them is asked to write.
pub struct Organs<'a> {
/// Her stored state; not asked when the request carries a state.
pub persona: Option<&'a dyn PersonaSource>,
/// Her Mind, for recall; none recalls nothing.
pub mind: Option<&'a dyn MindPort>,
/// The mouth, for the room; none shows no room.
pub chat: Option<&'a dyn Chat>,
/// The Projector and the judge; required for renders.
pub model: Option<&'a dyn Model>,
}
/// What to project.
pub struct Request {
pub instance: String,
pub kind: TurnKind,
/// A hypothetical state in place of the stored one.
pub state: Option<Value>,
/// The room the turn is in; none is every room of hers.
pub room: Option<String>,
pub seed: u64,
/// None builds the packet only. Some(k) narrates k successive turns of the same state, each seeded `seed + i` and
/// each under the freshness the ones before it leave.
pub renders: Option<usize>,
/// A second state: renders both and asks the judge which narration came from which.
pub judge_against: Option<Value>,
pub operators: Vec<String>,
pub own_sender: String,
pub tuning: PathBuf,
}
/// The room through the mouth's pending read, which acks nothing: new messages and the tail, own lines kept for the
/// Select stage to drop. A refusal is reported, never hidden.
fn read_room(chat: &dyn Chat, request: &Request, public_name: &str) -> (Room, Value) {
let pending = match chat.pending(0) {
Ok(pending) => pending,
Err(fault) => return (Room { here: request.room.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
.deliveries
.iter()
.filter(|delivery| delivery.form == Form::Message && wanted(&delivery.room_alias, &delivery.room_id))
.map(|delivery| RoomLine {
room: delivery.room_alias.clone(),
sender: delivery.sender.clone(),
display: delivery.sender_display.clone(),
text: delivery.text.clone(),
at_ms: delivery.at_unix_ms,
to_her: delivery.is_direct || delivery.mentions.contains(&request.own_sender) || (!public_name.is_empty() && crate::address::names_whole_word(&delivery.text, public_name, false)),
})
.collect();
let tail: Vec<RoomLine> = pending
.tail
.iter()
.flatten()
.filter(|line| line.form == Form::Message && wanted(&line.room, &line.room))
.map(|line| RoomLine { room: line.room.clone(), sender: line.sender.clone(), display: String::new(), text: line.body.clone(), at_ms: line.at_unix_ms, to_her: false })
.collect();
let report = json!({ "read": true, "messages": messages.len(), "tail": tail.len(), "tail_gaps": pending.tail_gaps });
(Room { messages, tail, here: request.room.clone() }, report)
}
/// What her Mind recalls for this room, as Select candidates: memories and rulings, never follow-ups (work is not hers).
fn read_recall(mind: &dyn MindPort, room: &Room, now: DateTime<Utc>) -> Result<(Vec<Recalled>, Value)> {
let lines = room.messages.iter().chain(room.tail.iter());
let mut senders: Vec<&str> = lines.clone().map(|line| line.sender.as_str()).collect();
senders.dedup();
let names: BTreeMap<&str, &str> = lines.map(|line| (line.sender.as_str(), line.display.as_str())).filter(|(_, name)| !name.is_empty()).collect();
let texts: Vec<&str> = room.messages.iter().rev().map(|line| line.text.as_str()).collect();
let cue = recall::Cue { senders, texts, names, listed: &[], now };
let recalled = recall::select(mind, &cue)?;
let mut out = Vec::new();
for shown in &recalled.lanes {
for entry in shown.entries.iter().filter(|entry| matches!(entry.kind, PipelineKind::Memory | PipelineKind::Ruling)) {
let touch = match shown.lane {
recall::Lane::About => TOUCH_ABOUT,
recall::Lane::Relevant => entry.nearness_permille.map_or(TOUCH_RELEVANT_UNSCORED, |near| f64::from(near.max(1)) / 1000.0),
recall::Lane::Salient => TOUCH_SALIENT,
};
out.push(Recalled {
id: entry.id.clone(),
text: entry.line.trim().trim_start_matches("- ").to_owned(),
salience: f64::from(entry.salience_permille.unwrap_or(500)) / 1000.0,
admitted_at: DateTime::parse_from_rfc3339(&entry.admitted_at).ok().map(|at| at.with_timezone(&Utc)),
touch,
});
}
}
let search = format!("{:?}", recalled.search).to_lowercase();
let report = json!({ "asked": true, "items": out.len(), "search": search });
Ok((out, report))
}
fn packet_json(packet: &Packet) -> Value {
json!({
"kind": packet.kind,
"seed": packet.seed,
"lens": packet.lens,
"present": packet.present,
"selected": packet.selected,
"bytes": { "identity": packet.identity.len(), "context": packet.context.len(), "identity_and_context": packet.identity.len() + packet.context.len(), "stimulus": packet.stimulus.len(), "heard": packet.heard.len() },
"identity": packet.identity,
"context": packet.context,
"stimulus": packet.stimulus,
"heard": packet.heard,
})
}
/// 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.
fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuning::Tuning, tag: &str, now: DateTime<Utc>) -> Result<Value> {
let request_id = format!("{AGENT}:{instance}:project:{tag}:{}:{}", packet.seed, now.timestamp_millis());
let raw = model.ask(&request_id, &tuning.projector.model, &projector_prompt(packet, tuning))?;
let budget = word_budget(packet.kind, tuning);
let narration = cap_words(&raw, budget);
let ids: Vec<String> = packet.selected.iter().map(|pick| pick.id.clone()).collect();
Ok(json!({
"seed": packet.seed,
"lens": packet.lens,
"selected": ids,
"words": words(&narration),
"cut_at_budget": words(&raw) > budget,
"field_names": field_names(&narration, &ids),
"narration": narration,
}))
}
/// K successive turns over one state: each packet under the freshness the earlier ones left.
fn successive(inputs: &Inputs, tuning: &tuning::Tuning, renders: usize) -> Vec<Packet> {
let mut history = inputs.history.clone();
(0..renders)
.map(|at| {
let packet = select(&Inputs { seed: inputs.seed.wrapping_add(at as u64), history: &history, ..*inputs }, tuning);
history.recent.push(packet.selected.iter().map(|pick| pick.id.clone()).collect());
packet
})
.collect()
}
/// What Select builds, and with renders what the Projector narrates, as one JSON report. Writes nothing.
pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result<Value> {
let loaded = tuning::load(&request.tuning)?;
let tuning = &loaded.tuning;
let (state, receipt) = match &request.state {
Some(state) => (state.clone(), Value::Null),
None => {
let (state, receipt) = organs.persona.ok_or_else(|| anyhow!("no state was given and her persona cannot be read"))?.persona()?;
(state, Value::String(receipt))
}
};
let public_name = state.get("publicName").and_then(Value::as_str).unwrap_or_default().to_owned();
let (room, room_report) = match organs.chat {
Some(chat) => read_room(chat, request, &public_name),
None => (Room { here: request.room.clone(), ..Room::default() }, json!({ "read": false, "why": "no mouth is configured" })),
};
let (recall, recall_report) = match organs.mind {
Some(mind) => read_recall(mind, &room, now)?,
None => (Vec::new(), json!({ "asked": false })),
};
// Freshness and her own recent words come from trace v2, which the commit writes (C3c); until then there is none.
let history = History::default();
let people = People { operators: &request.operators, own_sender: &request.own_sender };
let inputs = Inputs { kind: request.kind, state: &state, room: &room, recall: &recall, history: &history, observation: None, people, now, seed: request.seed };
let mut report = json!({
"instance": request.instance,
"persona_receipt": receipt,
"state": if request.state.is_some() { "given" } else { "stored" },
"tuning": { "path": request.tuning.display().to_string(), "source": loaded.source, "sha256": loaded.sha256 },
"room": room_report,
"recall": recall_report,
"history": "none: trace v2 is not written yet",
});
let Some(renders) = request.renders else {
report["packet"] = packet_json(&select(&inputs, tuning));
return Ok(report);
};
let model = organs.model.ok_or_else(|| anyhow!("renders need the connector, and none is configured"))?;
report["model"] = json!(tuning.projector.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::<Result<_>>()?;
let texts = |narrations: &[Value]| narrations.iter().map(|one| one["narration"].as_str().unwrap_or_default().to_owned()).collect::<Vec<_>>();
report["distinct_2"] = json!(distinct_2(&texts(&narrations)));
report["embedding_distance"] = json!("not measured: the daemon offers no embedding verb");
let leaked = narrations.iter().any(|one| one["field_names"].as_array().is_some_and(|names| !names.is_empty()));
report["field_names_found"] = json!(leaked);
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::<Result<_>>()?;
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() {
let (a, b) = (&packets[at % packets.len()], &others[at % others.len()]);
// The two descriptions swap places on odd turns, so a judge that always answers one letter scores half.
let swapped = at % 2 == 1;
let (first, second) = if swapped { (b, a) } else { (a, b) };
let text = narration["narration"].as_str().unwrap_or_default();
let answer = model.ask(&format!("{AGENT}:{}:judge:{at}:{}", request.instance, now.timestamp_millis()), &tuning.projector.model, &judge_prompt(first, second, text))?;
let letter = judged(&answer).map(|letter| if swapped { if letter == 'A' { 'B' } else { 'A' } } else { letter });
let correct = letter == Some(truth);
right += usize::from(correct);
verdicts.push(json!({ "from": truth.to_string(), "judged": letter.map(String::from), "correct": correct }));
}
let accuracy = right as f64 / verdicts.len().max(1) as f64;
report["against"] = json!({ "packet": packet_json(&others[0]), "renders": other_narrations, "distinct_2": distinct_2(&texts(&other_narrations)) });
report["judge"] = json!({ "accuracy": accuracy, "pass_at": JUDGE_PASS, "passed": accuracy >= JUDGE_PASS && !leaked, "verdicts": verdicts });
}
report["renders"] = Value::Array(narrations);
Ok(report)
}
#[cfg(test)]
mod tests;

View file

@ -0,0 +1,24 @@
lens: PersonPresent("Kiko")
selected: libby-sounds, t1-m0, curse, need-relatedness, t1-w0, stance-deletion, coherence
present: Kiko, Libby
--- identity
Eureka (she/her). Dry, direct, almost feral; zeal aimed at revealing.
--- context
- now: Thursday 19 November, 14:05 UTC
- bond [bond-kiko] Kiko, sparring partner: fair trust, heavy friction. Kiko pokes her to stop massaging the pipeline.
- bond [bond-libby] Libby, collaborator: fair trust, a little friction. Libby holds the sounds and teases her about the pipeline.
- mood, with no earlier turn to measure from: lighter, more settled than middling
- memory [libby-sounds]: Libby laughed at the door fix.
- memory [t1-m0]: The heartbeat stalled at noon.
- memory [curse]: Her curse is the knife. The same drive makes her good and will lead her wrong.
- need [need-relatedness]: company, strong
- wondering [t1-w0]: Whether the trace could be smaller.
- stance [stance-deletion]: Deleting is progress.
- value [coherence] Coherence: Coherence over velocity.
--- stimulus
New in the room:
- Libby in #swarm:gamecult.org, to her: "Eureka, did you see the door fix?"
- Kiko in #swarm:gamecult.org: "She saw it. She hated it."
--- heard
Libby, in #swarm:gamecult.org: "Eureka, did you see the door fix?"
Kiko, in #swarm:gamecult.org: "She saw it. She hated it."

View file

@ -0,0 +1,200 @@
//! The Projector's narration, and the judge that measures it: 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.
use std::collections::BTreeSet;
use std::net::SocketAddr;
use std::path::Path;
use std::time::Duration;
use anyhow::{Result, anyhow, bail};
use chrono::Utc;
use ghostlight_persona_projection::{ProjectorPrompt, build_projector_prompt};
use model_connector::{ConnectorClient, InputItem, ProviderRequest, ReasoningEffort, StopReason, TransportDisposition, TransportEventPayload, TransportInvocation, TransportOutcome};
use sha2::{Digest, Sha256};
use super::select::{Packet, TurnKind};
use super::tuning::Tuning;
/// One tool-less model turn. `KitModel` is the one live owner; a test answers with recorded text.
pub trait Model {
/// One turn of `model`: `prompt` is its one user text and `request_id` its identity on the connector.
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> Result<String>;
}
/// What the connector's request asks of the model beside the user text. It names no membrane text: that is Ghostlight's.
pub(crate) const INSTRUCTIONS: &str = "Write only the text the request asks for.";
/// Room for the model's adaptive thinking; the narration itself is bounded by its word budget.
pub(crate) const MAX_OUTPUT_TOKENS: u32 = 1024;
const CONNECTOR_FRAME_BYTES: usize = 256 * 1024;
const INVOCATION_LIFE_MS: u64 = 60_000;
/// The connector key: the credential file `connector-key` in systemd's credentials directory, read once and trimmed. Its
/// error names the credential and never a byte of the file.
pub fn connector_key(credentials: &Path) -> Result<String> {
let key = std::fs::read_to_string(credentials.join("connector-key")).map_err(|_| anyhow!("the connector-key credential could not be read"))?;
Ok(key.trim().to_owned())
}
/// One tool-less connector turn by `caller` (her instance). Loopback only, as the kit is. Each call is its own
/// conversation: nothing is carried from one to the next. The model is the tuning's, named per call.
pub struct KitModel {
client: ConnectorClient,
caller: String,
}
impl KitModel {
/// `endpoint` is the kit's loopback address; `connection_key` is the caller's key, read by the binary and never printed.
pub fn open(endpoint: SocketAddr, connection_key: String, caller: &str, timeout: Duration) -> Result<Self> {
if !endpoint.ip().is_loopback() {
bail!("the connector endpoint is not a loopback address");
}
let client = ConnectorClient::new(endpoint, connection_key, CONNECTOR_FRAME_BYTES, Some(timeout)).map_err(|_| anyhow!("the connector client could not be configured"))?;
Ok(Self { client, caller: caller.to_owned() })
}
}
impl Model for KitModel {
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> Result<String> {
let mut request = ProviderRequest::new(request_id, request_id, model, INSTRUCTIONS, MAX_OUTPUT_TOKENS);
request.input = vec![InputItem::UserText { text: prompt.to_owned() }];
request.reasoning_effort = Some(ReasoningEffort::Low);
let expires = u64::try_from(Utc::now().timestamp_millis()).unwrap_or_default() + INVOCATION_LIFE_MS;
let invocation = TransportInvocation::new(&self.caller, expires, Sha256::digest(prompt.as_bytes()).into(), request).map_err(|_| anyhow!("the request is not a valid connector request"))?;
let result = self.client.execute(&invocation).map_err(|fault| anyhow!("the connector did not answer: {fault}"))?;
match result.disposition {
TransportDisposition::Refused(reason) => bail!("the connector refused the call: {reason:?}"),
TransportDisposition::Transported { events, receipt } => match receipt.outcome {
TransportOutcome::Completed { stop_reason: StopReason::EndTurn, .. } => Ok(events
.into_iter()
.filter_map(|event| match event.payload {
TransportEventPayload::TextDelta { text } => Some(text),
_ => None,
})
.collect()),
TransportOutcome::Completed { stop_reason, .. } => bail!("the model's turn ended early: {stop_reason:?}"),
TransportOutcome::Failed { kind, .. } => bail!("the provider failed the call: {kind:?}"),
},
}
}
}
// ---------------------------------------------------------------------------------------------
// Narration.
pub(crate) fn word_budget(kind: TurnKind, tuning: &Tuning) -> usize {
if kind == TurnKind::Beat { tuning.projector.beat_words } else { tuning.projector.words }
}
/// The Projector's prompt: Ghostlight's membrane over the packet, with the lens as where to begin.
pub(crate) fn projector_prompt(packet: &Packet, tuning: &Tuning) -> String {
let guidance = format!("{}\n\nBegin from {}.", tuning.projector.guidance.trim(), packet.lens.words());
build_projector_prompt(&ProjectorPrompt { identity: &packet.identity, typed_context: &packet.context, visible_stimulus: &packet.stimulus, domain_guidance: &guidance, word_budget: word_budget(packet.kind, tuning) })
}
/// `text` cut after its `max`-th word; the model's count is not trusted.
pub(crate) fn cap_words(text: &str, max: usize) -> String {
let text = text.trim();
let mut words = 0;
let mut in_word = false;
for (at, c) in text.char_indices() {
if c.is_whitespace() {
in_word = false;
} else if !in_word {
in_word = true;
words += 1;
if words > max {
return text[..at].trim_end().to_owned();
}
}
}
text.to_owned()
}
pub(crate) fn words(text: &str) -> usize {
text.split_whitespace().count()
}
/// Schema keys of her persona state, as identifiers. A narration that carries one leaked the storage.
const SCHEMA_KEYS: [&str; 22] = [
"currentActivation", "socialBonds", "doctrineStances", "thoughtMemory", "shortTerm", "agencyPressure", "activationProfile", "situationalState", "moodDimensions", "candidateActions", "privateNotes",
"publicName", "publicDescription", "voiceSummary", "relationshipKind", "lastEvidence", "actionImplication", "schemaVersion", "updatedAt", "createdAt", "salience_permille", "persona_state",
];
/// Id prefixes of typed state, and the organ labels a `mind-` id begins with (a bare `mind-` is prose: "mind-numbing").
const ID_PREFIXES: [&str; 10] = ["bond-", "need-", "stance-", "mind-commit", "mind-persona", "mind-heartbeat", "mind-stream", "mind-control", "mind-self", "mind-eureka"];
/// Keys a number is stored under: the word is prose, the word followed by `:` or `=` is a field.
const VALUE_KEYS: [&str; 8] = ["trust", "tension", "intensity", "plasticity", "valence", "arousal", "dominance", "salience"];
/// The identifiers of typed state a narration carries: schema keys, typed ids, `key: value` fields, and the packet's own
/// selected ids where an id is no plain word. A small deterministic guard on identifiers, never on meaning.
pub(crate) fn field_names(narration: &str, selected_ids: &[String]) -> Vec<String> {
let mut found = BTreeSet::new();
let tokens: Vec<&str> = narration.split(|c: char| !(c.is_alphanumeric() || c == '-' || c == '_' || c == ':')).filter(|token| !token.is_empty()).collect();
for token in &tokens {
let bare = token.trim_matches(|c: char| c == ':' || c == '-');
if SCHEMA_KEYS.contains(&bare) {
found.insert(bare.to_owned());
}
if ID_PREFIXES.iter().any(|prefix| bare.to_lowercase().starts_with(prefix) && bare.trim_end_matches('-').len() > "bond".len()) {
found.insert(bare.to_owned());
}
}
let lower = narration.to_lowercase();
for key in VALUE_KEYS {
let mut from = 0;
while let Some(at) = lower[from..].find(key) {
let start = from + at;
let end = start + key.len();
let word_start = start == 0 || !lower[..start].chars().next_back().is_some_and(char::is_alphanumeric);
let rest = lower[end..].trim_start_matches([' ', '"', '\'']);
if word_start && (rest.starts_with(':') || rest.starts_with('=')) {
found.insert(key.to_owned());
}
from = end;
}
}
for id in selected_ids.iter().filter(|id| id.contains(['-', '_', ':']) || id.chars().any(|c| c.is_ascii_digit())) {
if narration.contains(id.as_str()) {
found.insert(id.clone());
}
}
if narration.contains('[') && narration.contains(']') {
for id in selected_ids {
if narration.contains(&format!("[{id}]")) {
found.insert(id.clone());
}
}
}
found.into_iter().collect()
}
// ---------------------------------------------------------------------------------------------
// The eval: distinct-2 and the judge.
/// Distinct bigrams over all bigrams of the narrations, lowercased words: 1 is no repetition.
pub(crate) fn distinct_2(narrations: &[String]) -> f64 {
let mut all = 0usize;
let mut distinct = BTreeSet::new();
for narration in narrations {
let words: Vec<String> = narration.split_whitespace().map(|word| word.trim_matches(|c: char| !c.is_alphanumeric()).to_lowercase()).filter(|word| !word.is_empty()).collect();
for pair in words.windows(2) {
all += 1;
distinct.insert((pair[0].clone(), pair[1].clone()));
}
}
if all == 0 { 0.0 } else { distinct.len() as f64 / all as f64 }
}
/// The judge's question: which of two packets a narration was written from.
pub(crate) fn judge_prompt(first: &Packet, second: &Packet, narration: &str) -> String {
format!(
"Two descriptions, A and B, of one person's present moment differ in what she carries. Below them is a first-person passage written from exactly one of them. Answer with the single letter of the description the passage was written from: A or B.\n\nA:\n{}\n{}\n\nB:\n{}\n{}\n\nPassage:\n{narration}\n\nAnswer A or B.",
first.context, first.stimulus, second.context, second.stimulus
)
}
/// The judge's letter, or None when its answer does not start with one.
pub(crate) fn judged(answer: &str) -> Option<char> {
answer.trim().trim_start_matches(['*', '"', '\'', '(']).chars().next().map(|c| c.to_ascii_uppercase()).filter(|c| *c == 'A' || *c == 'B')
}

View file

@ -0,0 +1,648 @@
//! The Select stage: what she can feel this turn. Code, deterministic for a seed, and the one owner of the
//! projection packet; the Projector model only renders what this chose.
//!
//! Every packet carries its fixed slots: identity, the hour, the stimulus, a bond line for each person present and
//! her mood as change. The rest is sampled without replacement from the softmax of `log(score) / T`, pool by pool
//! under its cap, until the context budget is spent, where
//! `score = salience × recency × touch × unresolved × freshness`.
//!
//! Never read: `agencyPressure`, `candidateActions`, `privateNotes` and `provenance`. A memory tagged with a direct
//! message room (`room:!…`) is selectable only in that room.
use std::collections::BTreeSet;
use std::fmt::Write as _;
use chrono::{DateTime, Utc};
use serde::Serialize;
use serde_json::Value;
use super::tuning::{LensWeights, Tuning};
use crate::address::localpart;
use crate::persona::quoted;
/// Touch: what the room is touching.
const TOUCH_PRESENT: f64 = 1.0;
const TOUCH_ROOM: f64 = 0.6;
const TOUCH_ELSE: f64 = 0.3;
/// The salience of an item that carries none.
const SALIENCE_UNSTATED: f64 = 0.5;
/// A move of mood or need below this is no move.
const MOVE_MIN: f64 = 0.03;
/// Incubation's unresolved factor caps here.
const INCUBATION_UNRESOLVED_MAX: f64 = 2.0;
/// What kind of turn the packet is for. The stimulus and nothing else differs between them.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TurnKind {
Chat,
Beat,
FollowUp,
Reflection,
}
/// One line of a room: a message or a line of its tail.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct RoomLine {
pub room: String,
pub sender: String,
pub display: String,
pub text: String,
pub at_ms: u64,
pub to_her: bool,
}
/// The room as the mouth showed it: new messages, and the recent tail.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Room {
pub messages: Vec<RoomLine>,
pub tail: Vec<RoomLine>,
/// The room the turn is in, when it is one room.
pub here: Option<String>,
}
/// One item her Mind recalled, as `recall::select` read it.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Recalled {
pub id: String,
pub text: String,
pub salience: f64,
pub admitted_at: Option<DateTime<Utc>>,
pub touch: f64,
}
/// One thing she said, from the trace.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Said {
pub id: String,
pub room: String,
pub text: String,
pub at: DateTime<Utc>,
}
/// What earlier turns left: the ids each of the last projections selected, oldest first, what she said, and her state
/// at her last turn (movement is measured against it).
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct History {
pub recent: Vec<Vec<String>>,
pub said: Vec<Said>,
pub previous: Option<Value>,
}
/// Who the people of a room are to her.
#[derive(Clone, Copy)]
pub(crate) struct People<'a> {
pub operators: &'a [String],
pub own_sender: &'a str,
}
impl People<'_> {
/// The id a bond names a person by: `operator` for the operator, the Matrix localpart for anyone else.
pub(crate) fn person_of(&self, sender: &str) -> String {
if self.operators.iter().any(|operator| operator == sender) { "operator".into() } else { localpart(sender).to_lowercase() }
}
}
pub(crate) struct Inputs<'a> {
pub kind: TurnKind,
pub state: &'a Value,
pub room: &'a Room,
pub recall: &'a [Recalled],
pub history: &'a History,
/// A follow-up's finding.
pub observation: Option<&'a str>,
pub people: People<'a>,
pub now: DateTime<Utc>,
pub seed: u64,
}
/// Where a turn begins.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum Lens {
BodyAndHour,
PersonPresent(String),
UnresolvedWant,
MemorySurfacing,
Wondering,
}
impl Lens {
/// The lens as the domain guidance's "begin from …".
pub(crate) fn words(&self) -> String {
match self {
Self::BodyAndHour => "how she is, here, at this hour".into(),
Self::PersonPresent(name) => format!("{name}, who is here"),
Self::UnresolvedWant => "something she still wants and has not got".into(),
Self::MemorySurfacing => "a memory coming back to her".into(),
Self::Wondering => "something she has been wondering about".into(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum Pool {
MemoriesAboutPresent,
OtherMemories,
Needs,
Incubation,
Stance,
Recall,
Value,
Said,
}
/// One selected item: its id goes to the trace, never to the narration.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct Pick {
pub id: String,
pub pool: Pool,
/// `ln(score)`.
pub log_score: f64,
}
/// The projection packet: what the Projector is shown, and what chose it.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct Packet {
pub kind: TurnKind,
pub seed: u64,
pub lens: Lens,
pub identity: String,
pub context: String,
pub stimulus: String,
/// What was said to her, verbatim, rendered by code for the Persona stage.
pub heard: String,
pub present: Vec<String>,
pub selected: Vec<Pick>,
}
struct Candidate {
id: String,
pool: Pool,
line: String,
log_score: f64,
}
// ---------------------------------------------------------------------------------------------
// Reading her state.
fn text<'a>(value: &'a Value, path: &[&str]) -> &'a str {
path.iter().try_fold(value, |at, key| at.get(*key)).and_then(Value::as_str).unwrap_or_default().trim()
}
fn number(value: &Value, path: &[&str]) -> Option<f64> {
path.iter().try_fold(value, |at, key| at.get(*key)).and_then(Value::as_f64)
}
fn items<'a>(value: &'a Value, path: &[&str]) -> &'a [Value] {
path.iter().try_fold(value, |at, key| at.get(*key)).and_then(Value::as_array).map_or(&[], Vec::as_slice)
}
fn live(item: &Value) -> bool {
!matches!(text(item, &["status"]), "retired" | "cooled" | "resolved")
}
fn time(item: &Value) -> Option<DateTime<Utc>> {
["updatedAt", "createdAt"].iter().find_map(|key| DateTime::parse_from_rfc3339(text(item, &[key])).ok()).map(|at| at.with_timezone(&Utc))
}
/// `text` cut to at most `max` bytes on a character boundary, with an ellipsis where it was cut.
pub(crate) fn clip(text: &str, max: usize) -> String {
let text = text.trim();
if text.len() <= max {
return text.to_owned();
}
let end = (0..=max.saturating_sub(3)).rev().find(|at| text.is_char_boundary(*at)).unwrap_or(0);
format!("{}…", &text[..end])
}
fn one_line(text: &str) -> String {
text.split_whitespace().collect::<Vec<_>>().join(" ")
}
// ---------------------------------------------------------------------------------------------
// Numbers as words.
fn trust_words(trust: f64) -> &'static str {
match trust {
t if t < 0.2 => "little trust",
t if t < 0.45 => "guarded trust",
t if t < 0.7 => "fair trust",
t if t < 0.85 => "solid trust",
_ => "deep trust",
}
}
fn friction_words(tension: f64) -> &'static str {
match tension {
t if t < 0.05 => "easy between them",
t if t < 0.25 => "a little friction",
t if t < 0.5 => "real friction",
_ => "heavy friction",
}
}
fn strength_words(intensity: f64) -> &'static str {
match intensity {
i if i < 0.25 => "faint",
i if i < 0.5 => "present",
i if i < 0.75 => "strong",
_ => "pressing",
}
}
/// The three mood axes: name, the word for higher, the word for lower.
const MOOD_AXES: [(&str, &str, &str); 3] = [("valence", "lighter", "heavier"), ("arousal", "more keyed up", "more settled"), ("dominance", "more in command of things", "more at their mercy")];
fn activation(state: &Value, axis: &str) -> Option<f64> {
number(state, &["activationProfile", "situationalState", axis, "currentActivation"])
}
/// Mood as change since her last turn; as a level only when there is no last turn to measure from.
fn mood_line(state: &Value, previous: Option<&Value>) -> String {
let now: Vec<(usize, f64)> = MOOD_AXES.iter().enumerate().filter_map(|(at, (axis, ..))| activation(state, axis).map(|value| (at, value))).collect();
if now.is_empty() {
return "- mood: none recorded".into();
}
let Some(previous) = previous else {
let words: Vec<&str> = now
.iter()
.map(|(at, value)| {
let (_, up, down) = MOOD_AXES[*at];
if *value >= 0.5 { up } else { down }
})
.collect();
return format!("- mood, with no earlier turn to measure from: {} than middling", words.join(", "));
};
let moves: Vec<&str> = now
.iter()
.filter_map(|(at, value)| {
let (axis, up, down) = MOOD_AXES[*at];
let before = activation(previous, axis)?;
match value - before {
change if change >= MOVE_MIN => Some(up),
change if change <= -MOVE_MIN => Some(down),
_ => None,
}
})
.collect();
if moves.is_empty() { "- mood: where it was at her last turn".into() } else { format!("- mood: {} than at her last turn", moves.join(", ")) }
}
fn need_movement(need: &Value, previous: Option<&Value>) -> String {
let intensity = number(need, &["intensity"]).unwrap_or(SALIENCE_UNSTATED);
let before = previous.and_then(|previous| items(previous, &["affect", "needs"]).iter().find(|old| text(old, &["id"]) == text(need, &["id"])).and_then(|old| number(old, &["intensity"])));
match before.map(|before| intensity - before) {
Some(change) if change >= MOVE_MIN => "growing since her last turn".into(),
Some(change) if change <= -MOVE_MIN => "easing since her last turn".into(),
Some(_) => "where it was".into(),
None => strength_words(intensity).into(),
}
}
// ---------------------------------------------------------------------------------------------
// The seeded draw: splitmix64, so a turn id replays.
struct Draw(u64);
impl Draw {
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);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
/// Uniform in (0, 1).
fn unit(&mut self) -> f64 {
((self.next() >> 11) as f64 + 0.5) / (1u64 << 53) as f64
}
fn gumbel(&mut self) -> f64 {
-(-self.unit().ln()).ln()
}
}
fn draw_lens(weights: &LensWeights, present: &[String], candidates: &[Candidate], draw: &mut Draw) -> Lens {
let has = |pools: &[Pool]| candidates.iter().any(|candidate| pools.contains(&candidate.pool));
let options = [
(weights.body_and_hour, Lens::BodyAndHour),
(if present.is_empty() { 0.0 } else { weights.person_present }, Lens::PersonPresent(present.first().cloned().unwrap_or_default())),
(if has(&[Pool::Needs, Pool::Incubation]) { weights.unresolved_want } else { 0.0 }, Lens::UnresolvedWant),
(if has(&[Pool::MemoriesAboutPresent, Pool::OtherMemories, Pool::Recall]) { weights.memory_surfacing } else { 0.0 }, Lens::MemorySurfacing),
(if has(&[Pool::Incubation]) { weights.wondering } else { 0.0 }, Lens::Wondering),
];
let total: f64 = options.iter().map(|(weight, _)| weight.max(0.0)).sum();
let mut at = draw.unit() * total;
for (weight, lens) in &options {
let weight = weight.max(0.0);
if weight > 0.0 && at < weight {
return lens.clone();
}
at -= weight;
}
Lens::BodyAndHour
}
// ---------------------------------------------------------------------------------------------
// Candidates.
fn freshness_log(id: &str, history: &History, tuning: &Tuning) -> f64 {
let window = &history.recent[history.recent.len().saturating_sub(tuning.select.freshness_window)..];
let shown = window.iter().filter(|ids| ids.iter().any(|seen| seen == id)).count();
shown as f64 * tuning.select.freshness_base.ln()
}
fn recency_log(at: Option<DateTime<Utc>>, now: DateTime<Utc>, tau_h: f64) -> f64 {
at.map_or(0.0, |at| -((now - at).num_seconds().max(0) as f64 / 3600.0) / tau_h)
}
/// The memory's tags name a direct-message room other than the turn's: private matter stays in its room.
fn private_elsewhere(item: &Value, here: Option<&str>) -> bool {
items(item, &["tags"]).iter().filter_map(Value::as_str).filter_map(|tag| tag.strip_prefix("room:")).any(|room| room.starts_with('!') && Some(room) != here)
}
fn in_room(item: &Value, here: Option<&str>) -> bool {
here.is_some_and(|here| items(item, &["tags"]).iter().filter_map(Value::as_str).any(|tag| tag.strip_prefix("room:") == Some(here)))
}
fn about_present(item: &Value, present: &BTreeSet<String>) -> bool {
let target = text(item, &["target", "id"]).to_lowercase();
present.contains(&target) || items(item, &["tags"]).iter().filter_map(Value::as_str).filter_map(|tag| tag.strip_prefix("about:")).any(|id| present.contains(&id.to_lowercase()))
}
fn candidates(inputs: &Inputs, tuning: &Tuning, present: &BTreeSet<String>) -> Vec<Candidate> {
let state = inputs.state;
let here = inputs.room.here.as_deref();
let item_bytes = tuning.select.item_bytes;
let mut out = Vec::new();
let mut push = |id: &str, pool: Pool, line: String, salience: f64, recency: f64, touch: f64, unresolved: f64| {
if id.is_empty() || salience <= 0.0 {
return;
}
let log_score = salience.ln() + recency + touch.ln() + unresolved.ln() + freshness_log(id, inputs.history, tuning);
out.push(Candidate { id: id.to_owned(), pool, line, log_score });
};
let memories = items(state, &["thoughtMemory", "memories"]).iter().chain(items(state, &["thoughtMemory", "shortTerm"]));
for memory in memories.filter(|memory| live(memory) && !private_elsewhere(memory, here)) {
let about = about_present(memory, present);
let pool = if about { Pool::MemoriesAboutPresent } else { Pool::OtherMemories };
let touch = if about { TOUCH_PRESENT } else if in_room(memory, here) { TOUCH_ROOM } else { TOUCH_ELSE };
let mut body = text(memory, &["summary"]).to_owned();
let tension = text(memory, &["tension"]);
if !tension.is_empty() {
body = format!("{body} {tension}");
}
let line = format!("- memory [{}]: {}", text(memory, &["id"]), clip(&one_line(&body), item_bytes));
let salience = number(memory, &["intensity"]).unwrap_or(SALIENCE_UNSTATED);
push(text(memory, &["id"]), pool, line, salience, recency_log(time(memory), inputs.now, tuning.select.memory_tau_h), touch, 1.0);
}
for need in items(state, &["affect", "needs"]).iter().filter(|need| live(need)) {
let intensity = number(need, &["intensity"]).unwrap_or(SALIENCE_UNSTATED);
let name = [text(need, &["label"]), text(need, &["summary"]), text(need, &["id"])].into_iter().find(|name| !name.is_empty()).unwrap_or_default();
let line = format!("- need [{}]: {}, {}", text(need, &["id"]), clip(&one_line(name), item_bytes), need_movement(need, inputs.history.previous.as_ref()));
push(text(need, &["id"]), Pool::Needs, line, intensity, 0.0, TOUCH_ELSE, 1.0 + intensity);
}
for wondering in items(state, &["thoughtMemory", "incubation"]).iter().filter(|item| live(item) && !private_elsewhere(item, here)) {
let days = time(wondering).map_or(0.0, |at| (inputs.now - at).num_seconds().max(0) as f64 / 86_400.0);
let touch = if about_present(wondering, present) { TOUCH_PRESENT } else { TOUCH_ELSE };
let line = format!("- wondering [{}]: {}", text(wondering, &["id"]), clip(&one_line(text(wondering, &["summary"])), item_bytes));
push(text(wondering, &["id"]), Pool::Incubation, line, number(wondering, &["intensity"]).unwrap_or(SALIENCE_UNSTATED), 0.0, touch, (1.0 + days / 7.0).min(INCUBATION_UNRESOLVED_MAX));
}
for stance in items(state, &["affect", "doctrineStances"]).iter().filter(|stance| live(stance)) {
let touch = if about_present(stance, present) { TOUCH_PRESENT } else { TOUCH_ELSE };
let line = format!("- stance [{}]: {}", text(stance, &["id"]), clip(&one_line(text(stance, &["summary"])), item_bytes));
push(text(stance, &["id"]), Pool::Stance, line, number(stance, &["intensity"]).unwrap_or(SALIENCE_UNSTATED), 0.0, touch, 1.0);
}
for recalled in inputs.recall {
let line = format!("- her mind recalls [{}]: {}", recalled.id, clip(&one_line(&recalled.text), item_bytes));
push(&recalled.id, Pool::Recall, line, recalled.salience, recency_log(recalled.admitted_at, inputs.now, tuning.select.recall_tau_h), recalled.touch, 1.0);
}
for value in items(state, &["values"]) {
let line = format!("- value [{}] {}: {}", text(value, &["id"]), text(value, &["label"]), clip(&one_line(text(value, &["summary"])), item_bytes));
push(text(value, &["id"]), Pool::Value, line, number(value, &["priority"]).unwrap_or(SALIENCE_UNSTATED), 0.0, TOUCH_ELSE, 1.0);
}
for said in &inputs.history.said {
let touch = if Some(said.room.as_str()) == here { TOUCH_ROOM } else { TOUCH_ELSE };
let line = format!("- she said [{}] in {}: {}", said.id, said.room, quoted(&clip(&said.text, item_bytes)));
push(&said.id, Pool::Said, line, SALIENCE_UNSTATED, recency_log(Some(said.at), inputs.now, tuning.select.memory_tau_h), touch, 1.0);
}
out
}
fn cap_of(pool: Pool, tuning: &Tuning) -> usize {
let caps = &tuning.select.caps;
match pool {
Pool::MemoriesAboutPresent => caps.memories_about_present,
Pool::OtherMemories => caps.other_memories,
Pool::Needs => caps.needs,
Pool::Incubation => caps.incubation,
Pool::Stance => caps.stance,
Pool::Recall => caps.recall,
Pool::Value => caps.value,
Pool::Said => caps.said,
}
}
// ---------------------------------------------------------------------------------------------
// The stimulus, the people present and the fixed slots.
fn name_of(line: &RoomLine, inputs: &Inputs) -> String {
let person = inputs.people.person_of(&line.sender);
let bonded = items(inputs.state, &["affect", "socialBonds"]).iter().find(|bond| text(bond, &["object", "id"]).to_lowercase() == person).map(|bond| text(bond, &["object", "label"]).to_owned());
bonded.filter(|label| !label.is_empty()).or_else(|| Some(line.display.trim().to_owned()).filter(|name| !name.is_empty())).unwrap_or_else(|| localpart(&line.sender).to_owned())
}
/// The newest lines that fit `max` bytes after `head`, oldest first, and a note when older ones did not.
fn fitted(head: &str, lines: Vec<String>, max: usize) -> String {
let mut kept: Vec<String> = Vec::new();
let mut used = head.len();
let reserve = 48;
for line in lines.iter().rev() {
if used + line.len() + 1 + reserve > max {
break;
}
used += line.len() + 1;
kept.push(line.clone());
}
kept.reverse();
let dropped = lines.len() - kept.len();
let mut out = head.to_owned();
if dropped > 0 {
let _ = writeln!(out, "({dropped} earlier not shown)");
}
for line in kept {
out.push_str(&line);
out.push('\n');
}
out.trim_end().to_owned()
}
fn chat_stimulus(inputs: &Inputs, tuning: &Tuning) -> (String, String) {
let messages: Vec<&RoomLine> = inputs.room.messages.iter().filter(|line| line.sender != inputs.people.own_sender).collect();
if messages.is_empty() {
return ("Nothing new was said.".into(), String::new());
}
let typed = messages.iter().map(|line| format!("- {} in {}{}: {}", name_of(line, inputs), line.room, if line.to_her { ", to her" } else { "" }, quoted(&line.text))).collect();
// Heard lines are prose for the Persona: the words exactly as said, inside plain quotation marks.
let heard = messages.iter().map(|line| format!("{}, in {}: \"{}\"", name_of(line, inputs), line.room, line.text)).collect();
(fitted("New in the room:\n", typed, tuning.select.stimulus_bytes), fitted("", heard, tuning.select.heard_bytes))
}
fn hours(now: DateTime<Utc>, at_ms: u64) -> i64 {
let at = i64::try_from(at_ms).ok().and_then(DateTime::<Utc>::from_timestamp_millis).unwrap_or(now);
(now - at).num_hours().max(0)
}
/// A beat: nothing addressed her, when she last heard from each person she is bound to (always shown: it is the
/// relatedness cue), then the newest lines of her rooms in what the budget leaves.
fn beat_stimulus(inputs: &Inputs, tuning: &Tuning) -> String {
let mut head = String::from("Nothing addressed her.
");
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 = [text(bond, &["object", "label"]), text(bond, &["object", "id"])].into_iter().find(|name| !name.is_empty()).unwrap_or_default();
let last = inputs.room.tail.iter().filter(|line| inputs.people.person_of(&line.sender) == person).map(|line| line.at_ms).max();
let _ = match last {
Some(at) => writeln!(head, "- last heard from {label} {} h ago", hours(inputs.now, at)),
None => writeln!(head, "- nothing from {label} in her rooms lately"),
};
}
let head = clip_lines(&head, tuning.select.stimulus_bytes);
let others: Vec<&RoomLine> = inputs.room.tail.iter().filter(|line| line.sender != inputs.people.own_sender).collect();
let shown = &others[others.len().saturating_sub(tuning.select.beat_tail_lines)..];
if shown.is_empty() {
return head;
}
let lines = shown.iter().map(|line| format!("- {} in {}, {} h ago: {}", name_of(line, inputs), line.room, hours(inputs.now, line.at_ms), quoted(&clip(&line.text, tuning.select.item_bytes / 2)))).collect();
fitted(&format!("{head}
Lately in her rooms:
"), lines, tuning.select.stimulus_bytes)
}
/// `text` cut at a line boundary to `max` bytes.
fn clip_lines(text: &str, max: usize) -> String {
let mut out = String::new();
for line in text.lines() {
if out.len() + line.len() + 1 > max {
break;
}
out.push_str(line);
out.push('\n');
}
out.trim_end().to_owned()
}
fn reflection_stimulus(inputs: &Inputs, tuning: &Tuning) -> String {
let lines = items(inputs.state, &["thoughtMemory", "shortTerm"]).iter().filter(|item| live(item)).map(|item| format!("- {}", clip(&one_line(text(item, &["summary"])), tuning.select.item_bytes))).collect();
fitted("Since she last took stock:\n", lines, tuning.select.stimulus_bytes)
}
/// The people present, newest first: who spoke to her or in her room this turn, or in the beat's lines.
fn present_people(inputs: &Inputs, tuning: &Tuning) -> Vec<(String, String)> {
let lines: Vec<&RoomLine> = match inputs.kind {
TurnKind::Chat => inputs.room.messages.iter().collect(),
TurnKind::Beat => {
let tail = &inputs.room.tail;
tail[tail.len().saturating_sub(tuning.select.beat_tail_lines)..].iter().collect()
}
TurnKind::FollowUp | TurnKind::Reflection => Vec::new(),
};
let mut out: Vec<(String, String)> = Vec::new();
for line in lines.iter().rev().filter(|line| line.sender != inputs.people.own_sender) {
let person = inputs.people.person_of(&line.sender);
if !out.iter().any(|(id, _)| *id == person) {
out.push((person, name_of(line, inputs)));
}
}
out
}
fn bond_line(bond: &Value, name: &str, tuning: &Tuning) -> String {
let mut feel = vec![friction_words(number(bond, &["tension"]).unwrap_or(0.0))];
if let Some(trust) = number(bond, &["trust"]) {
feel.insert(0, trust_words(trust));
}
let kind = text(bond, &["relationshipKind"]);
let kind = if kind.is_empty() { String::new() } else { format!(", {kind}") };
format!("- bond [{}] {name}{kind}: {}. {}", text(bond, &["id"]), feel.join(", "), clip(&one_line(text(bond, &["summary"])), tuning.select.bond_summary_bytes))
}
fn identity_of(state: &Value, tuning: &Tuning) -> String {
let name = text(state, &["publicName"]);
let pronouns = text(state, &["presentation", "pronouns"]);
let head = if pronouns.is_empty() { name.to_owned() } else { format!("{name} ({pronouns})") };
let voice = text(state, &["presentation", "voiceSummary"]);
let whole = if voice.is_empty() { head } else { format!("{head}. {}", one_line(voice)) };
clip(&whole, tuning.select.identity_bytes)
}
// ---------------------------------------------------------------------------------------------
// The packet.
pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet {
let mut draw = Draw(inputs.seed);
let identity = identity_of(inputs.state, tuning);
let (stimulus, heard) = match inputs.kind {
TurnKind::Chat => chat_stimulus(inputs, tuning),
TurnKind::Beat => (beat_stimulus(inputs, tuning), String::new()),
TurnKind::FollowUp => (clip(&format!("She just found: {}", inputs.observation.unwrap_or("nothing")), tuning.select.stimulus_bytes), String::new()),
TurnKind::Reflection => (reflection_stimulus(inputs, tuning), String::new()),
};
let present = present_people(inputs, tuning);
let present_ids: BTreeSet<String> = present.iter().map(|(id, _)| id.clone()).collect();
let bonds = items(inputs.state, &["affect", "socialBonds"]);
let mut fixed = vec![format!("- now: {}", inputs.now.format("%A %-d %B, %H:%M UTC"))];
let mut named = Vec::new();
let mut full = 0;
for (id, name) in &present {
match bonds.iter().find(|bond| live(bond) && text(bond, &["object", "id"]).to_lowercase() == *id) {
Some(bond) if full < tuning.select.people_full => {
full += 1;
fixed.push(bond_line(bond, name, tuning));
}
_ => named.push(name.clone()),
}
}
if !named.is_empty() {
fixed.push(format!("- also here: {}", named.join(", ")));
}
fixed.push(mood_line(inputs.state, inputs.history.previous.as_ref()));
let mut pool = candidates(inputs, tuning, &present_ids);
let present_names: Vec<String> = present.iter().map(|(_, name)| name.clone()).collect();
let lens = draw_lens(&tuning.lens, &present_names, &pool, &mut draw);
// Gumbel-top-k over log(score)/T is sampling without replacement from the softmax.
let temperature = tuning.select.temperature.max(1e-6);
let mut keyed: Vec<(f64, Candidate)> = pool.drain(..).map(|candidate| (candidate.log_score / temperature + draw.gumbel(), candidate)).collect();
keyed.sort_by(|a, b| b.0.total_cmp(&a.0));
let mut used = identity.len() + fixed.iter().map(|line| line.len() + 1).sum::<usize>();
let mut taken: Vec<Candidate> = Vec::new();
for (_, candidate) in keyed {
if taken.iter().filter(|kept| kept.pool == candidate.pool).count() >= cap_of(candidate.pool, tuning) {
continue;
}
if used + candidate.line.len() + 1 > tuning.select.context_bytes {
continue;
}
used += candidate.line.len() + 1;
taken.push(candidate);
}
taken.sort_by_key(|candidate| candidate.pool);
let mut lines = fixed;
lines.extend(taken.iter().map(|candidate| candidate.line.clone()));
let context = clip_lines(&lines.join("\n"), tuning.select.context_bytes.saturating_sub(identity.len()));
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, stimulus, heard, present: present_names, selected }
}

View file

@ -0,0 +1,702 @@
use std::cell::{Cell, RefCell};
use std::collections::BTreeSet;
use std::net::TcpListener;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use anyhow::{Result, anyhow};
use chrono::{DateTime, Utc};
use cultnet_rs::Selection;
use eureka_pipeline::{MemorySort, PipelineDocument, PipelineKind, Slug};
use huginn_mind::{PipelineAdmissionOutcome, PipelineSelectionPage};
use serde_json::{Value, json};
use super::narrate::{INSTRUCTIONS, MAX_OUTPUT_TOKENS};
use super::select::Pool;
use super::tuning::Tuning;
use super::*;
use crate::chat_client::{ChatDelivery, ChatFetchReceipt, ChatFetchRequest, ChatOpenDirectReceipt, ChatOpenDirectRequest, ChatPending, ChatRead, ChatSendReceipt, ChatSendRequest, TailLine};
use crate::testkit::{INSTANCE, TestMind, memory, reference};
const OWN: &str = "@eureka:gamecult.org";
const OPERATOR: &str = "@meta:gamecult.org";
fn now() -> DateTime<Utc> {
DateTime::parse_from_rfc3339("2026-11-19T14:05:00Z").unwrap().with_timezone(&Utc)
}
fn hours_ago(hours: i64) -> String {
(now() - chrono::Duration::hours(hours)).to_rfc3339()
}
/// A state shaped like hers: bonds, memories, values, a need, a wondering, a stance, mood, and the mission pressure,
/// private notes and candidate actions Select must never read.
fn state() -> Value {
json!({
"publicName": "Eureka",
"publicDescription": "Firstborn and keeper of the living substrate.",
"presentation": { "pronouns": "she/her", "voiceSummary": "Dry, direct, almost feral; zeal aimed at revealing." },
"activationProfile": { "situationalState": {
"valence": { "mean": 0.5, "plasticity": 0.5, "currentActivation": 0.62 },
"arousal": { "mean": 0.5, "plasticity": 0.5, "currentActivation": 0.41 },
} },
"affect": {
"socialBonds": [
{ "id": "bond-libby", "object": { "id": "libby", "kind": "person", "label": "Libby" }, "relationshipKind": "collaborator", "status": "active", "trust": 0.6, "tension": 0.15, "summary": "Libby holds the sounds and teases her about the pipeline." },
{ "id": "bond-kiko", "object": { "id": "kiko", "kind": "person", "label": "Kiko" }, "relationshipKind": "sparring partner", "status": "active", "trust": 0.6, "tension": 0.5, "summary": "Kiko pokes her to stop massaging the pipeline." },
{ "id": "bond-operator", "object": { "id": "operator", "kind": "person", "label": "Meta, the operator" }, "relationshipKind": "collaborator", "status": "active", "trust": 0.5, "tension": 0.0, "summary": "The operator rules on what the work is for." },
],
"needs": [{ "id": "need-relatedness", "label": "company", "intensity": 0.6, "status": "active" }],
"doctrineStances": [{ "id": "stance-deletion", "summary": "Deleting is progress.", "intensity": 0.7, "status": "active" }],
},
"thoughtMemory": {
"memories": [
{ "id": "curse", "summary": "Her curse is the knife.", "tension": "The same drive makes her good and will lead her wrong.", "createdAt": hours_ago(400), "status": "crystallized", "target": { "id": "eureka", "kind": "self" } },
{ "id": "libby-sounds", "summary": "Libby laughed at the door fix.", "createdAt": hours_ago(20), "status": "crystallized", "target": { "id": "libby", "kind": "person" } },
{ "id": "dm-secret", "summary": "Deru told her about the move, privately.", "createdAt": hours_ago(5), "status": "crystallized", "tags": ["room:!dm-deru:gamecult.org"], "target": { "id": "deru", "kind": "person" } },
],
"shortTerm": [{ "id": "t1-m0", "summary": "The heartbeat stalled at noon.", "createdAt": hours_ago(2), "intensity": 0.6 }],
"incubation": [{ "id": "t1-w0", "summary": "Whether the trace could be smaller.", "createdAt": hours_ago(30), "status": "active" }],
},
"values": [
{ "id": "coherence", "label": "Coherence", "priority": 0.9, "summary": "Coherence over velocity." },
{ "id": "reveal", "label": "Reveal", "priority": 0.8, "summary": "Reveal, never obscure." },
],
"agencyPressure": { "pressures": [{ "id": "ambition-0", "summary": "MISSION-PRESSURE ships the autonomous swarm.", "intensity": 0.9 }] },
"privateNotes": ["PRIVATE-NOTE rehydrate from typed state."],
"candidateActions": { "actions": [{ "id": "act-0", "summary": "CANDIDATE-ACTION start a run." }] },
})
}
fn message(room: &str, sender: &str, display: &str, text: &str, minutes_ago: i64, to_her: bool) -> RoomLine {
let at = now() - chrono::Duration::minutes(minutes_ago);
RoomLine { room: room.into(), sender: sender.into(), display: display.into(), text: text.into(), at_ms: u64::try_from(at.timestamp_millis()).unwrap(), to_her }
}
fn chat_room() -> 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()),
}
}
const OPERATORS: [&str; 1] = [OPERATOR];
fn operators() -> Vec<String> {
OPERATORS.iter().map(|id| (*id).to_owned()).collect()
}
/// Select over one state, room and history with the default tuning.
fn packet_of(kind: TurnKind, state: &Value, room: &Room, history: &History, seed: u64) -> Packet {
packet_tuned(kind, state, room, history, seed, &Tuning::default())
}
fn packet_tuned(kind: TurnKind, state: &Value, room: &Room, history: &History, seed: u64, tuning: &Tuning) -> Packet {
let operators = operators();
let inputs = Inputs { kind, state, room, recall: &[], history, observation: None, people: People { operators: &operators, own_sender: OWN }, now: now(), seed };
select(&inputs, tuning)
}
fn ids(packet: &Packet) -> Vec<String> {
packet.selected.iter().map(|pick| pick.id.clone()).collect()
}
fn whole(packet: &Packet) -> String {
format!("{}\n{}\n{}\n{}", packet.identity, packet.context, packet.stimulus, packet.heard)
}
// ---------------------------------------------------------------------------------------------
// Golden fixture.
#[test]
fn golden_chat_packet() {
let packet = packet_of(TurnKind::Chat, &state(), &chat_room(), &History::default(), 7);
let rendered = format!(
"lens: {:?}\nselected: {}\npresent: {}\n--- identity\n{}\n--- context\n{}\n--- stimulus\n{}\n--- heard\n{}\n",
packet.lens,
ids(&packet).join(", "),
packet.present.join(", "),
packet.identity,
packet.context,
packet.stimulus,
packet.heard
);
let golden = include_str!("fixtures/golden-chat.txt").replace("\r\n", "\n");
assert_eq!(rendered, golden, "the golden packet moved; if deliberately, the new one is:\n{rendered}");
}
#[test]
fn the_same_seed_replays_the_same_packet() {
let room = chat_room();
assert_eq!(packet_of(TurnKind::Chat, &state(), &room, &History::default(), 11), packet_of(TurnKind::Chat, &state(), &room, &History::default(), 11));
}
// ---------------------------------------------------------------------------------------------
// What reaches the packet.
#[test]
fn present_people_get_their_bond_line_in_words_and_absent_people_none() {
let packet = packet_of(TurnKind::Chat, &state(), &chat_room(), &History::default(), 1);
assert_eq!(packet.present, ["Kiko", "Libby"], "newest first");
assert!(packet.context.contains("- bond [bond-kiko] Kiko, sparring partner: fair trust, heavy friction."), "{}", packet.context);
assert!(packet.context.contains("- bond [bond-libby] Libby, collaborator: fair trust, a little friction."), "{}", packet.context);
assert!(!packet.context.contains("bond-operator"), "the operator is not here");
assert!(!packet.context.contains("0.6") && !packet.context.contains("0.15"), "numbers reach the packet as words: {}", packet.context);
}
#[test]
fn two_states_differing_in_one_bonds_tension_give_different_packets() {
let mut eased = state();
eased["affect"]["socialBonds"][1]["tension"] = json!(0.0);
let room = chat_room();
let (tense, easy) = (packet_of(TurnKind::Chat, &state(), &room, &History::default(), 3), packet_of(TurnKind::Chat, &eased, &room, &History::default(), 3));
assert_ne!(tense.context, easy.context);
assert!(tense.context.contains("Kiko, sparring partner: fair trust, heavy friction") && easy.context.contains("Kiko, sparring partner: fair trust, easy between them"));
}
#[test]
fn mood_is_movement_since_her_last_turn() {
let room = chat_room();
let first = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1);
assert!(first.context.contains("- mood, with no earlier turn to measure from: lighter, more settled than middling"), "{}", first.context);
let mut before = state();
before["activationProfile"]["situationalState"]["valence"]["currentActivation"] = json!(0.5);
before["activationProfile"]["situationalState"]["arousal"]["currentActivation"] = json!(0.41);
let moved = packet_of(TurnKind::Chat, &state(), &room, &History { previous: Some(before), ..History::default() }, 1);
assert!(moved.context.contains("- mood: lighter than at her last turn"), "{}", moved.context);
let none = packet_of(TurnKind::Chat, &json!({ "publicName": "Eureka" }), &room, &History::default(), 1);
assert!(none.context.contains("- mood: none recorded"));
}
#[test]
fn mission_pressure_private_notes_and_candidate_actions_never_reach_a_packet() {
let room = chat_room();
for kind in [TurnKind::Chat, TurnKind::Beat, TurnKind::Reflection, TurnKind::FollowUp] {
for seed in 1..=20 {
let text = whole(&packet_of(kind, &state(), &room, &History::default(), seed));
for never in ["MISSION-PRESSURE", "ambition-0", "PRIVATE-NOTE", "CANDIDATE-ACTION", "act-0"] {
assert!(!text.contains(never), "{kind:?} seed {seed} carried {never}");
}
}
}
}
#[test]
fn a_direct_message_memory_stays_in_its_room() {
let shown = |room: Option<&str>| (1..=40).any(|seed| ids(&packet_of(TurnKind::Chat, &state(), &Room { here: room.map(str::to_owned), ..chat_room() }, &History::default(), seed)).iter().any(|id| id == "dm-secret"));
assert!(!shown(Some("#swarm:gamecult.org")), "a public room never sees the direct message's memory");
assert!(!shown(None));
assert!(shown(Some("!dm-deru:gamecult.org")), "its own room may");
}
#[test]
fn pool_caps_hold() {
let mut big = state();
big["values"] = Value::Array((0..30).map(|at| json!({ "id": format!("v{at}"), "label": "V", "priority": 0.9, "summary": "short" })).collect());
big["thoughtMemory"]["memories"] = Value::Array((0..30).map(|at| json!({ "id": format!("m{at}"), "summary": "short", "createdAt": hours_ago(1) })).collect());
for seed in 1..=20 {
let packet = packet_of(TurnKind::Beat, &big, &Room::default(), &History::default(), seed);
let count = |pool| packet.selected.iter().filter(|pick| pick.pool == pool).count();
assert!(count(Pool::Value) <= 1 && count(Pool::OtherMemories) <= 2 && count(Pool::Needs) <= 1 && count(Pool::Stance) <= 1 && count(Pool::Incubation) <= 1, "seed {seed}: {:?}", packet.selected);
}
}
// ---------------------------------------------------------------------------------------------
// Variation: rotation and freshness.
#[test]
fn twenty_seeds_rotate_through_at_least_three_selections() {
let room = chat_room();
let mut rich = state();
for at in 0..6 {
rich["thoughtMemory"]["memories"].as_array_mut().unwrap().push(json!({ "id": format!("day-{at}"), "summary": format!("Something from day {at}."), "createdAt": hours_ago(10 + at) }));
}
let distinct: BTreeSet<Vec<String>> = (1..=20).map(|seed| ids(&packet_of(TurnKind::Chat, &rich, &room, &History::default(), seed))).collect();
assert!(distinct.len() >= 3, "{distinct:?}");
let lenses: BTreeSet<String> = (1..=20).map(|seed| format!("{:?}", packet_of(TurnKind::Chat, &state(), &room, &History::default(), seed).lens)).collect();
assert!(lenses.len() >= 3, "the lens rotates too: {lenses:?}");
}
#[test]
fn an_item_shown_five_times_in_a_row_drops_out() {
let room = Room::default();
let picked = |history: &History| (1..=20).filter(|seed| ids(&packet_of(TurnKind::Beat, &state(), &room, history, *seed)).iter().any(|id| id == "coherence")).count();
let fresh = picked(&History::default());
let worn = picked(&History { recent: vec![vec!["coherence".to_owned()]; 5], ..History::default() });
assert!(fresh >= 5, "the heavier value usually wins when fresh: {fresh}/20");
assert!(worn <= 1, "after five showings it has dropped out: {worn}/20");
let older = picked(&History { recent: [vec![vec!["coherence".to_owned()]; 5], vec![vec![]; 5]].concat(), ..History::default() });
assert!(older >= 5, "showings older than the window are forgotten: {older}/20");
}
// ---------------------------------------------------------------------------------------------
// The stimulus, and what was said to her.
#[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 packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1);
assert_eq!(packet.heard, format!("Libby, in #swarm:gamecult.org: \"{said}\""), "the Persona hears the exact words");
assert!(packet.stimulus.contains(&crate::persona::quoted(said)) && packet.stimulus.contains(", to her"), "{}", packet.stimulus);
let own = Room { messages: vec![message("#swarm:gamecult.org", OWN, "Eureka", "my own line", 1, false)], tail: vec![], here: None };
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");
}
#[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 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);
assert_eq!(packet.present, ["Meta, the operator", "Libby"]);
assert!(packet.heard.is_empty(), "a beat hears nothing");
let long = Room { tail: (0..6).map(|at| message("#swarm:gamecult.org", "@kiko:gamecult.org", "", &"pipeline ".repeat(200), 60 - at, false)).collect(), ..Room::default() };
let packet = packet_of(TurnKind::Beat, &state(), &long, &History::default(), 1);
assert!(packet.stimulus.len() <= 1500 && packet.stimulus.contains("- last heard from Kiko 0 h ago") && packet.stimulus.contains("- nothing from Libby in her rooms lately"), "long room lines never crowd out the relatedness cue: {}", packet.stimulus);
}
#[test]
fn reflection_and_follow_up_stimuli() {
let reflection = packet_of(TurnKind::Reflection, &state(), &Room::default(), &History::default(), 1);
assert!(reflection.stimulus.starts_with("Since she last took stock:\n- The heartbeat stalled at noon."), "{}", reflection.stimulus);
let operators = operators();
let room = Room::default();
let history = History::default();
let inputs = Inputs { kind: TurnKind::FollowUp, state: &state(), room: &room, recall: &[], history: &history, observation: Some("Libby's change deleted 40 lines."), people: People { operators: &operators, own_sender: OWN }, now: now(), seed: 1 };
let state_value = state();
let follow = select(&Inputs { state: &state_value, ..inputs }, &Tuning::default());
assert_eq!(follow.stimulus, "She just found: Libby's change deleted 40 lines.");
}
// ---------------------------------------------------------------------------------------------
// Budgets.
#[test]
fn budgets_hold_under_adversarial_sizes() {
let huge = "x".repeat(20_000);
let mut big = state();
big["publicName"] = json!(huge);
big["presentation"]["voiceSummary"] = json!(huge);
big["values"] = Value::Array((0..200).map(|at| json!({ "id": format!("v{at}"), "label": huge, "priority": 0.9, "summary": huge })).collect());
big["thoughtMemory"]["memories"] = Value::Array((0..200).map(|at| json!({ "id": format!("m{at}"), "summary": huge, "createdAt": hours_ago(1), "target": { "id": "libby" } })).collect());
for bond in big["affect"]["socialBonds"].as_array_mut().unwrap() {
bond["summary"] = json!(huge);
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, true)).collect();
let room = Room { messages, tail: vec![], here: None };
let tuning = Tuning::default();
for kind in [TurnKind::Chat, TurnKind::Beat, TurnKind::Reflection] {
for seed in 1..=10 {
let packet = packet_of(kind, &big, &room, &History::default(), seed);
assert!(packet.identity.len() <= tuning.select.identity_bytes, "identity {}", packet.identity.len());
assert!(packet.identity.len() + packet.context.len() <= 2500, "{kind:?}: packet {} bytes", packet.identity.len() + packet.context.len());
assert!(packet.stimulus.len() <= 1500 && packet.heard.len() <= 1500, "{kind:?}: stimulus {} heard {}", packet.stimulus.len(), packet.heard.len());
}
}
let packet = packet_of(TurnKind::Chat, &big, &room, &History::default(), 1);
assert!(packet.stimulus.contains("earlier not shown") && packet.stimulus.contains("49 word"), "the newest messages are the ones kept: {}", &packet.stimulus[..200]);
}
#[test]
fn the_narration_is_cut_at_its_word_budget() {
let long = (0..400).map(|at| format!("w{at}")).collect::<Vec<_>>().join(" ");
let cut = cap_words(&long, 150);
assert_eq!(words(&cut), 150);
assert!(cut.ends_with("w149"));
assert_eq!(cap_words(" a b\n c ", 150), "a b\n c");
let tuning = Tuning::default();
assert_eq!((word_budget(TurnKind::Chat, &tuning), word_budget(TurnKind::Beat, &tuning)), (150, 90));
}
// ---------------------------------------------------------------------------------------------
// The field-name guard.
#[test]
fn field_names_are_caught_and_prose_is_not() {
let selected = vec!["bond-libby".to_owned(), "t1-m0".to_owned(), "curse".to_owned()];
for leak in ["Her socialBonds hum.", "bond-kiko pulls at her", "trust: 0.6 with Libby", "Tension = high", "the t1-m0 note", "a [curse] tag", "mind-persona wakes", "currentActivation rises"] {
assert!(!field_names(leak, &selected).is_empty(), "{leak:?} leaks an identifier");
}
for prose in ["She trusts Libby, and the tension between them is old.", "Her curse is the knife.", "A mind-numbing hour.", "She feels her trust thin; valence is a word she would never use aloud."] {
assert!(field_names(prose, &selected).is_empty(), "{prose:?} is prose: {:?}", field_names(prose, &selected));
}
}
// ---------------------------------------------------------------------------------------------
// The tuning file.
#[test]
fn tuning_absent_is_the_defaults_and_a_file_overrides_and_an_unknown_key_refuses() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(TUNING_FILE);
let absent = tuning::load(&path).unwrap();
assert_eq!((absent.tuning.clone(), absent.source), (Tuning::default(), "defaults"));
std::fs::write(&path, "[select]\ntemperature = 0.3\n\n[select.caps]\nvalue = 3\n\n[projector]\nwords = 120\n").unwrap();
let file = tuning::load(&path).unwrap();
assert_eq!((file.tuning.select.temperature, file.tuning.select.caps.value, file.tuning.projector.words, file.source), (0.3, 3, 120, "file"));
assert_eq!(file.tuning.select.context_bytes, 2500, "an unnamed key keeps its default");
assert_ne!(file.sha256, absent.sha256);
std::fs::write(&path, "[select]\ntemprature = 0.3\n").unwrap();
assert!(format!("{:#}", tuning::load(&path).unwrap_err()).contains("temprature"), "a mistyped knob refuses the file");
}
#[test]
fn a_lower_temperature_concentrates_the_selection() {
let room = chat_room();
let mut cold = Tuning::default();
cold.select.temperature = 0.01;
let distinct: BTreeSet<Vec<String>> = (1..=20).map(|seed| ids(&packet_tuned(TurnKind::Chat, &state(), &room, &History::default(), seed, &cold))).collect();
assert_eq!(distinct.len(), 1, "near zero temperature takes the top scores every time: {distinct:?}");
}
// ---------------------------------------------------------------------------------------------
// `project`: recorded models, the ports it reads through, and the negative check.
/// Answers the Projector by the packet it was shown, and the judge by which description holds the passage's mark.
struct Recorded {
calls: RefCell<Vec<(String, String, String)>>,
narration: Box<dyn Fn(&str) -> String>,
judge: Box<dyn Fn(&str) -> String>,
}
impl Recorded {
fn new(narration: impl Fn(&str) -> String + 'static, judge: impl Fn(&str) -> String + 'static) -> Self {
Self { calls: RefCell::default(), narration: Box::new(narration), judge: Box::new(judge) }
}
/// Narrates "grinds" from a packet with heavy friction and "easy" otherwise; judges by finding which description has it.
fn discriminating() -> Self {
Self::new(
|prompt| if prompt.contains("heavy friction") { "Everything grinds today.".into() } else { "It is easy today.".into() },
|prompt| {
let (descriptions, passage) = prompt.split_once("Passage:\n").unwrap();
let (a, b) = descriptions.split_once("\nB:\n").unwrap();
let grinds = passage.contains("grinds");
let letter = if a.contains("heavy friction") == grinds { "A" } else { "B" };
let _ = b;
letter.into()
},
)
}
}
impl Model for Recorded {
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> Result<String> {
self.calls.borrow_mut().push((request_id.to_owned(), model.to_owned(), prompt.to_owned()));
Ok(if prompt.starts_with("<!-- membrane:") { (self.narration)(prompt) } else { (self.judge)(prompt) })
}
}
/// A mouth that answers the pending read and fails the test on anything that would write.
struct ReadOnlyMouth {
pending: ChatPending,
reads: Cell<u32>,
}
impl Chat for ReadOnlyMouth {
fn pending(&self, after_seq: u64) -> Result<ChatPending> {
assert_eq!(after_seq, 0);
self.reads.set(self.reads.get() + 1);
Ok(self.pending.clone())
}
fn send(&self, _: &ChatSendRequest) -> Result<ChatSendReceipt> {
panic!("project sent chat")
}
fn open_direct(&self, _: &ChatOpenDirectRequest) -> Result<ChatOpenDirectReceipt> {
panic!("project opened a direct message")
}
fn fetch(&self, _: &ChatFetchRequest) -> Result<ChatFetchReceipt> {
panic!("project fetched an attachment")
}
fn ack(&self, _: u64) -> Result<()> {
panic!("project acked chat")
}
fn read(&self, _: &ChatRead) -> Result<()> {
panic!("project reported a read")
}
}
fn delivery(seq: u64, room: &str, sender: &str, display: &str, text: &str, mentions: &[&str]) -> ChatDelivery {
ChatDelivery {
seq,
room_alias: room.into(),
room_id: format!("!{seq}:gamecult.org"),
event_id: format!("$e{seq}"),
sender: sender.into(),
sender_display: display.into(),
text: text.into(),
at_unix_ms: u64::try_from((now() - chrono::Duration::minutes(5)).timestamp_millis()).unwrap() + seq,
context: vec![],
mentions: mentions.iter().map(|id| (*id).to_owned()).collect(),
is_direct: false,
image: None,
basis: None,
form: Form::Message,
reply_to: Default::default(),
}
}
fn mouth() -> ReadOnlyMouth {
let tail = vec![TailLine { room: "#swarm:gamecult.org".into(), event_id: "$t1".into(), at_unix_ms: u64::try_from((now() - chrono::Duration::hours(2)).timestamp_millis()).unwrap(), sender: "@kiko:gamecult.org".into(), form: Form::Message, basis: None, body: "pipeline again".into() }];
let deliveries = vec![delivery(1, "#swarm:gamecult.org", "@libby:gamecult.org", "Libby", "Eureka, the door?", &[OWN]), delivery(2, "#general:gamecult.org", "@kiko:gamecult.org", "Kiko", "elsewhere", &[])];
ReadOnlyMouth { pending: ChatPending { persona: "eureka".into(), deliveries, tail: Some(tail), tail_gaps: vec![] }, reads: Cell::new(0) }
}
/// A mind that counts admissions over a real one's reads.
struct Counting<'a> {
inner: &'a TestMind,
admits: Cell<u32>,
}
impl MindPort for Counting<'_> {
fn instance(&self) -> &Slug {
self.inner.instance()
}
fn query(&self, selection: &Selection) -> Result<PipelineSelectionPage> {
self.inner.query(selection)
}
fn query_near(&self, selection: &Selection, text: &str, top_k: u32) -> Result<PipelineSelectionPage> {
self.inner.query_near(selection, text, top_k)
}
fn admit(&self, _: &str, _: &str, _: Vec<PipelineDocument>) -> Result<PipelineAdmissionOutcome> {
self.admits.set(self.admits.get() + 1);
Err(anyhow!("project admitted"))
}
}
struct Stored(Value);
impl PersonaSource for Stored {
fn persona(&self) -> Result<(Value, String)> {
Ok((self.0.clone(), "mind-commit-1".into()))
}
}
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(), own_sender: OWN.into(), tuning: dir.join(TUNING_FILE) }
}
fn mind() -> TestMind {
let mind = TestMind::seeded();
mind.committed(vec![memory("libby-door", MemorySort::Learned, "Libby fixed the door at last", &["@libby:gamecult.org"]), crate::testkit::follow_up("work-item", &reference(PipelineKind::Memory, &format!("{INSTANCE}:memory:libby-door")))]);
mind
}
#[test]
fn project_in_every_mode_writes_nothing() {
let dir = tempfile::tempdir().unwrap();
let trace = dir.path().join("persona-trace.cc");
std::fs::write(&trace, b"trace bytes").unwrap();
let test_mind = mind();
let before: Vec<Vec<String>> = [PipelineKind::Memory, PipelineKind::FollowUp, PipelineKind::Thought, PipelineKind::Run].iter().map(|kind| test_mind.ids(*kind)).collect();
let counting = Counting { inner: &test_mind, admits: Cell::new(0) };
let stored = Stored(state());
let chat = mouth();
let model = Recorded::discriminating();
let organs = Organs { persona: Some(&stored), mind: Some(&counting), chat: Some(&chat), model: Some(&model) };
let mut other = state();
other["affect"]["socialBonds"][1]["tension"] = json!(0.0);
for (renders, judge) in [(None, None), (Some(3), None), (Some(2), Some(other))] {
let report = project(&organs, &request(dir.path(), renders, judge), now()).unwrap();
assert_eq!(report["persona_receipt"], "mind-commit-1");
}
assert_eq!(counting.admits.get(), 0, "nothing was admitted");
let after: Vec<Vec<String>> = [PipelineKind::Memory, PipelineKind::FollowUp, PipelineKind::Thought, PipelineKind::Run].iter().map(|kind| test_mind.ids(*kind)).collect();
assert_eq!(before, after, "her mind holds what it held");
assert_eq!(std::fs::read(&trace).unwrap(), b"trace bytes", "the trace is untouched");
assert_eq!(chat.reads.get(), 3, "the mouth was only read");
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1, "no file was written beside the trace");
}
#[test]
fn project_select_only_reads_the_room_and_recall_and_never_shows_work() {
let dir = tempfile::tempdir().unwrap();
let test_mind = mind();
let stored = Stored(state());
let chat = mouth();
let organs = Organs { persona: Some(&stored), mind: Some(&test_mind), chat: Some(&chat), model: None };
let report = project(&organs, &request(dir.path(), None, None), now()).unwrap();
assert_eq!(report["tuning"]["source"], "defaults");
assert_eq!((report["room"]["read"].as_bool(), report["room"]["messages"].as_u64()), (Some(true), Some(1)), "only the room asked for");
let packet = &report["packet"];
assert!(packet["heard"].as_str().unwrap().contains("Libby, in #swarm:gamecult.org: \"Eureka, the door?\""), "{packet}");
assert!(!packet.to_string().contains("elsewhere"), "another room's message is not this turn's");
assert!(!packet.to_string().contains("work-item") && !packet.to_string().contains("Later."), "a follow-up is work, never recalled to her");
assert!(report.get("renders").is_none(), "select-only calls no model");
}
#[test]
fn project_recall_reaches_the_packet_when_drawn() {
let dir = tempfile::tempdir().unwrap();
let test_mind = mind();
let stored = Stored(state());
let chat = mouth();
let organs = Organs { persona: Some(&stored), mind: Some(&test_mind), chat: Some(&chat), model: None };
let drawn = (1..=20).any(|seed| {
let report = project(&organs, &Request { seed, ..request(dir.path(), None, None) }, now()).unwrap();
report["packet"]["context"].as_str().unwrap().contains("her mind recalls")
});
assert!(drawn, "a memory about the person speaking is recalled on some seed");
}
#[test]
fn renders_are_successive_turns_under_freshness_and_carry_the_guard() {
let dir = tempfile::tempdir().unwrap();
let stored = Stored(state());
let model = Recorded::new(|_| "She waits. socialBonds hum.".into(), |_| "A".into());
let organs = Organs { persona: Some(&stored), mind: None, chat: None, model: Some(&model) };
let report = project(&organs, &Request { kind: TurnKind::Beat, ..request(dir.path(), Some(4), None) }, now()).unwrap();
let renders = report["renders"].as_array().unwrap();
assert_eq!(renders.len(), 4);
assert_eq!(renders.iter().map(|one| one["seed"].as_u64().unwrap()).collect::<Vec<_>>(), [5, 6, 7, 8]);
assert_eq!(renders[0]["field_names"], json!(["socialBonds"]));
assert_eq!(report["field_names_found"], true);
let calls = model.calls.borrow();
assert!(calls.iter().all(|(_, model, _)| model == "claude-sonnet-5-5"), "the tuning's model");
assert!(calls.iter().map(|(id, ..)| id).collect::<BTreeSet<_>>().len() == 4, "each call is its own request");
assert!(calls[0].2.contains("Begin from ") && calls[0].2.contains("Use at most 90 words"), "a beat's budget and its lens reach the prompt");
for (_, _, prompt) in calls.iter() {
assert!(!prompt.contains("MISSION-PRESSURE") && !prompt.contains("PRIVATE-NOTE"));
}
}
#[test]
fn the_judge_scores_a_discriminating_projector_and_fails_a_blind_one() {
let dir = tempfile::tempdir().unwrap();
let stored = Stored(state());
// Libby is the one present (the mouth's message to her); the second state is heavy friction with her.
let mut eased = state();
eased["affect"]["socialBonds"][0]["tension"] = json!(0.9);
let chat = mouth();
let model = Recorded::discriminating();
let organs = Organs { persona: Some(&stored), mind: None, chat: Some(&chat), model: Some(&model) };
let report = project(&organs, &request(dir.path(), Some(5), Some(eased.clone())), now()).unwrap();
assert_eq!((report["judge"]["accuracy"].as_f64(), report["judge"]["passed"].as_bool()), (Some(1.0), Some(true)), "{}", report["judge"]);
assert_eq!(report["judge"]["verdicts"].as_array().unwrap().len(), 10);
let blind = Recorded::new(|_| "It is a day.".into(), |_| "A".into());
let organs = Organs { model: Some(&blind), ..organs };
let report = project(&organs, &request(dir.path(), Some(5), Some(eased)), now()).unwrap();
assert_eq!(report["judge"]["passed"], false, "a judge that always answers A is right half the time: {}", report["judge"]);
assert_eq!(report["distinct_2"].as_f64(), Some(0.2), "five identical four-word narrations: three distinct bigrams of fifteen");
}
#[test]
fn renders_without_a_connector_are_refused() {
let dir = tempfile::tempdir().unwrap();
let stored = Stored(state());
let organs = Organs { persona: Some(&stored), mind: None, chat: None, model: None };
assert!(format!("{:#}", project(&organs, &request(dir.path(), Some(1), None), now()).unwrap_err()).contains("connector"));
}
#[test]
fn a_given_state_is_projected_without_reading_the_stored_one() {
let dir = tempfile::tempdir().unwrap();
let organs = Organs { persona: None, mind: None, chat: None, model: None };
let mut given = state();
given["publicName"] = json!("Hypothetical");
let report = project(&organs, &Request { state: Some(given), ..request(dir.path(), None, None) }, now()).unwrap();
assert_eq!((report["state"].as_str(), report["persona_receipt"].is_null()), (Some("given"), true));
assert!(report["packet"]["identity"].as_str().unwrap().starts_with("Hypothetical"));
}
// ---------------------------------------------------------------------------------------------
// KitModel against a real kit server (ported with it from the stream organ).
use model_connector::{CallerAdmission, InputItem, ProviderBackend, ProviderCall, ProviderRequest, ReasoningEffort, ServeLimits, StopReason, TransportEvent, TransportEventPayload, TransportOutcome, TransportResult, Usage, serve as serve_kit};
const KIT_KEY: &str = "projector-test-key";
const KIT_MODEL: &str = "claude-haiku-5-5";
struct Scripted {
outcome: TransportOutcome,
words: Vec<&'static str>,
seen: Mutex<Vec<(String, ProviderRequest)>>,
}
impl Scripted {
fn ending(stop_reason: StopReason, words: &[&'static str]) -> Arc<Self> {
let usage = Usage { input_tokens: 1, cache_read_tokens: 0, cache_write_tokens: 0, output_tokens: 1, reasoning_tokens: None };
Arc::new(Self { outcome: TransportOutcome::Completed { provider_response_id: Some("msg-1".into()), stop_reason, usage }, words: words.to_vec(), seen: Mutex::default() })
}
}
impl ProviderBackend for Scripted {
fn ready(&self) -> Result<(), &'static str> {
Ok(())
}
fn provider(&self) -> &str {
"anthropic-messages"
}
fn execute(&self, call: &ProviderCall) -> TransportResult {
self.seen.lock().unwrap().push((call.caller_runtime_id().to_owned(), call.request().clone()));
let events = self.words.iter().enumerate().map(|(at, text)| TransportEvent { sequence: at as u64, payload: TransportEventPayload::TextDelta { text: (*text).into() } }).collect();
call.transported(events, self.outcome.clone())
}
}
fn kit(backend: Arc<Scripted>, allowed: &str) -> (KitModel, tempfile::TempDir) {
let root = tempfile::tempdir().unwrap();
let store = root.path().join("replay.cc");
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let endpoint = listener.local_addr().unwrap();
let admission = CallerAdmission::new(INSTANCE, KIT_KEY, 1, [allowed.to_owned()], 2, 64 * 1024, 4096).unwrap();
let limits = ServeLimits { max_frame_bytes: 512 * 1024, max_connections: 8, socket_timeout_ms: 5000, max_expiry_skew_ms: 300_000 };
std::thread::spawn(move || serve_kit(listener, limits, vec![admission], &store, backend));
(KitModel::open(endpoint, KIT_KEY.into(), INSTANCE, Duration::from_secs(5)).unwrap(), root)
}
#[test]
fn a_kit_call_is_one_tool_less_turn_by_her_instance() {
let backend = Scripted::ending(StopReason::EndTurn, &["I turn ", "it over."]);
let (model, _root) = kit(Arc::clone(&backend), KIT_MODEL);
let id = "mind-persona:eureka:project:a:1:1";
assert_eq!(model.ask(id, KIT_MODEL, "The projector prompt.").unwrap(), "I turn it over.");
let seen = backend.seen.lock().unwrap();
let (caller, request) = &seen[0];
assert_eq!(caller, INSTANCE);
assert_eq!((request.request_id.as_str(), request.conversation_id.as_str(), request.model.as_str()), (id, id, KIT_MODEL));
assert_eq!(request.input, [InputItem::UserText { text: "The projector prompt.".into() }]);
assert_eq!((request.instructions.as_str(), request.reasoning_effort, request.max_output_tokens), (INSTRUCTIONS, Some(ReasoningEffort::Low), MAX_OUTPUT_TOKENS));
assert!(request.tools.is_empty() && request.output_schema_json.is_none());
}
#[test]
fn a_kit_refusal_or_a_cut_short_turn_is_a_fault() {
let backend = Scripted::ending(StopReason::EndTurn, &["Never said."]);
let (model, _root) = kit(Arc::clone(&backend), "some-other-model");
assert!(format!("{:#}", model.ask("id-1", KIT_MODEL, "p").unwrap_err()).contains("ModelNotAllowed"));
assert!(backend.seen.lock().unwrap().is_empty(), "the provider was never reached");
for stop in [StopReason::MaxOutputTokens, StopReason::Refusal] {
let (model, _root) = kit(Scripted::ending(stop, &["Half a thou"]), KIT_MODEL);
let fault = format!("{:#}", model.ask("id-2", KIT_MODEL, "p").unwrap_err());
assert!(fault.contains("ended early") && !fault.contains("Half a thou"), "{stop:?}: {fault}");
}
}
#[test]
fn the_kit_must_be_loopback_and_no_error_carries_the_key() {
let key = "zq7Kx-9wVtm3Pd";
for endpoint in ["10.77.0.1:4110", "192.0.2.1:4110", "127.0.0.1:0"] {
let fault = format!("{:#}", KitModel::open(endpoint.parse().unwrap(), key.into(), INSTANCE, Duration::from_secs(1)).err().unwrap());
for window in key.as_bytes().windows(4) {
assert!(!fault.contains(std::str::from_utf8(window).unwrap()), "{endpoint}: {fault}");
}
}
assert!(KitModel::open("[::1]:4110".parse().unwrap(), KIT_KEY.into(), INSTANCE, Duration::from_secs(1)).is_ok());
}
#[test]
fn the_connector_key_is_read_trimmed_and_its_absence_leaks_nothing() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("connector-key"), " canary-key-7f3a91 \r\n").unwrap();
assert_eq!(connector_key(dir.path()).unwrap(), "canary-key-7f3a91");
let empty = tempfile::tempdir().unwrap();
let fault = format!("{:#}", connector_key(empty.path()).unwrap_err());
assert!(fault.contains("connector-key") && !fault.contains(empty.path().to_str().unwrap()), "{fault}");
}

View file

@ -0,0 +1,159 @@
//! `persona-tuning.toml`: every knob of the Select stage and the Projector, 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
//! loudly instead of running on the default it meant to change. The file's SHA-256 is carried with every packet,
//! so a reader of the trace can tell which tuning a turn ran under.
use std::path::Path;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
/// Where the tuning of an instance lives.
pub const TUNING_ROOT: &str = "/etc/gamecult/minds";
pub const TUNING_FILE: &str = "persona-tuning.toml";
/// 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.";
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Tuning {
pub projector: ProjectorTuning,
pub select: SelectTuning,
pub lens: LensWeights,
}
/// The Projector's call: its model, its domain guidance and its word budgets.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ProjectorTuning {
pub model: String,
pub guidance: String,
/// The lived stream's words on a chat, follow-up or reflection turn.
pub words: usize,
/// On a beat: a thin moment gets a thin stream.
pub beat_words: usize,
}
/// The selection: its temperature, its budgets, its half-lives, the freshness decay and the pool caps.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct SelectTuning {
/// The softmax temperature of the sampled remainder.
pub temperature: f64,
/// Identity plus typed context, in bytes.
pub context_bytes: usize,
pub stimulus_bytes: usize,
/// What was said to her, verbatim, for the Persona stage.
pub heard_bytes: usize,
pub identity_bytes: usize,
/// One bond line's summary, in bytes.
pub bond_summary_bytes: usize,
/// One selected item's text, in bytes.
pub item_bytes: usize,
/// The recency time constant of her own memories and short-term items, in hours.
pub memory_tau_h: f64,
/// The recency time constant of what her Mind recalls, in hours.
pub recall_tau_h: f64,
/// freshness = base ^ (times shown in the last `freshness_window` projections).
pub freshness_base: f64,
pub freshness_window: usize,
/// People present shown with a full bond line; the rest are named.
pub people_full: usize,
/// The room lines a beat shows.
pub beat_tail_lines: usize,
pub caps: PoolCaps,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PoolCaps {
pub memories_about_present: usize,
pub other_memories: usize,
pub needs: usize,
pub incubation: usize,
pub stance: usize,
pub recall: usize,
pub value: usize,
pub said: usize,
}
/// The weighted draw of the lens a turn begins from.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LensWeights {
pub body_and_hour: f64,
pub person_present: f64,
pub unresolved_want: f64,
pub memory_surfacing: f64,
pub wondering: f64,
}
impl Default for ProjectorTuning {
fn default() -> Self {
Self { model: "claude-sonnet-5-5".into(), guidance: GUIDANCE.into(), words: 150, beat_words: 90 }
}
}
impl Default for SelectTuning {
fn default() -> Self {
Self {
temperature: 0.7,
context_bytes: 2500,
stimulus_bytes: 1500,
heard_bytes: 1500,
identity_bytes: 300,
bond_summary_bytes: 300,
item_bytes: 400,
memory_tau_h: 24.0,
recall_tau_h: 72.0,
freshness_base: 0.5,
freshness_window: 5,
people_full: 2,
beat_tail_lines: 6,
caps: PoolCaps::default(),
}
}
}
impl Default for PoolCaps {
fn default() -> Self {
Self { memories_about_present: 2, other_memories: 2, needs: 1, incubation: 1, stance: 1, recall: 2, value: 1, said: 1 }
}
}
impl Default for LensWeights {
fn default() -> Self {
Self { body_and_hour: 1.0, person_present: 1.0, unresolved_want: 1.0, memory_surfacing: 1.0, wondering: 1.0 }
}
}
/// The tuning a wake ran under, and which file it was.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Loaded {
pub tuning: Tuning,
/// `file` or `defaults`.
pub source: &'static str,
/// The file's SHA-256, hex; for the defaults, the SHA-256 of no bytes.
pub sha256: String,
}
/// The tuning at `path`, or the defaults when no file is there. A file that cannot be read or parsed, or that names a
/// key the code does not know, is an error: the wake does not run on a tuning nobody wrote.
pub fn load(path: &Path) -> Result<Loaded> {
let bytes = match std::fs::read(path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Loaded { tuning: Tuning::default(), source: "defaults", sha256: hex(&Sha256::digest(b"")) }),
Err(error) => return Err(error).with_context(|| format!("the tuning {} could not be read", path.display())),
};
let text = std::str::from_utf8(&bytes).with_context(|| format!("the tuning {} is not UTF-8", path.display()))?;
let tuning: Tuning = toml::from_str(text).with_context(|| format!("the tuning {} is not a tuning", path.display()))?;
Ok(Loaded { tuning, source: "file", sha256: hex(&Sha256::digest(&bytes)) })
}
fn hex(digest: &[u8]) -> String {
digest.iter().map(|byte| format!("{byte:02x}")).collect()
}

View file

@ -70,13 +70,20 @@ impl Lane {
}
}
/// One recalled document as a line of the turn's input, and the people it names.
/// One recalled document as a line of the turn's input, the people it names, and what the Projector's Select stage
/// scores it by.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Entry {
pub id: String,
pub line: String,
/// The Matrix ids its `about` and origin name.
pub people: Vec<String>,
pub kind: PipelineKind,
/// A memory's salience; none for another kind.
pub salience_permille: Option<u16>,
pub admitted_at: String,
/// The daemon's nearness to the turn's words, in thousandths of its score; the relevant lane's alone.
pub nearness_permille: Option<u16>,
}
/// One lane's result: what it shows and how many it left out.
@ -267,9 +274,9 @@ fn line_of(view: &PipelineDocumentView, names: &BTreeMap<&str, &str>) -> Option<
/// elsewhere, within the lane's item limit and byte budget (a line that does
/// not fit is skipped, a later shorter one may). `matched` is how many the
/// lane's query matched, so what it left out is counted exactly.
fn fill(lane: Lane, candidates: Vec<PipelineDocumentView>, matched: usize, limit: usize, budget: usize, seen: &mut Vec<String>, cue: &Cue) -> Shown {
fn fill(lane: Lane, candidates: Vec<(PipelineDocumentView, Option<u16>)>, matched: usize, limit: usize, budget: usize, seen: &mut Vec<String>, cue: &Cue) -> Shown {
let (mut entries, mut bytes, mut elsewhere) = (Vec::new(), 0usize, 0usize);
for view in candidates {
for (view, nearness_permille) in candidates {
let id = view.id.id.0.clone();
if seen.contains(&id) || cue.listed.contains(&id) {
elsewhere += 1;
@ -281,16 +288,20 @@ fn fill(lane: Lane, candidates: Vec<PipelineDocumentView>, matched: usize, limit
}
seen.push(id.clone());
bytes += line.len();
let people = match &view.document {
PipelineDocument::Memory(memory) => people_of(memory),
_ => Vec::new(),
let (people, salience_permille) = match &view.document {
PipelineDocument::Memory(memory) => (people_of(memory), Some(memory.salience_permille)),
_ => (Vec::new(), None),
};
entries.push(Entry { id, line, people });
entries.push(Entry { id, line, people, kind: view.id.kind, salience_permille, admitted_at: view.admission.admitted_at.clone(), nearness_permille });
}
let left_out = matched.saturating_sub(entries.len() + elsewhere);
Shown { lane, entries, left_out }
}
fn unscored(views: Vec<PipelineDocumentView>) -> Vec<(PipelineDocumentView, Option<u16>)> {
views.into_iter().map(|view| (view, None)).collect()
}
/// Whether a view is an open ask or commitment: a memory of those sorts.
fn is_open_item(view: &PipelineDocumentView) -> bool {
matches!(&view.document, PipelineDocument::Memory(memory) if matches!(memory.sort, MemorySort::Asked | MemorySort::Committed))
@ -304,23 +315,28 @@ pub(crate) fn select(mind: &dyn MindPort, cue: &Cue) -> Result<Recalled> {
} else {
let page = mind.query(&newest(any_of(recalled_memories_in_force(), "about", &cue.senders), POOL))?;
let matched = page.matched as usize;
(ranked(documents(page)?, cue.now), matched)
(unscored(ranked(documents(page)?, cue.now)), matched)
};
let (relevant, search) = match near_text(cue) {
None => (Vec::new(), Search::Skipped),
Some(text) => {
let kinds = [PipelineKind::Memory, PipelineKind::FollowUp, PipelineKind::Ruling];
let members = in_force(Selection { schemas: Some(kinds.iter().map(|kind| kind.type_id().to_string()).collect()), projection: "document".into(), ..Selection::default() });
match mind.query_near(&members, &text, POOL).and_then(documents) {
let scored = mind.query_near(&members, &text, POOL).and_then(|page| {
let scores = page.scores.clone().unwrap_or_default();
let nearness = scores.iter().map(|score| Some((f64::from(*score) * 1000.0).round().clamp(0.0, 1000.0) as u16)).chain(std::iter::repeat(None));
Ok(documents(page)?.into_iter().zip(nearness).collect::<Vec<_>>())
});
match scored {
// An open ask or commitment is shown by the responsibilities section; it never comes here.
Ok(found) => (found.into_iter().filter(|view| !is_open_item(view)).collect::<Vec<_>>(), Search::Ran),
Ok(found) => (found.into_iter().filter(|(view, _)| !is_open_item(view)).collect::<Vec<_>>(), Search::Ran),
Err(_) => (Vec::new(), Search::Unavailable),
}
}
};
let page = mind.query(&newest(recalled_memories_in_force(), POOL))?;
let salient_matched = page.matched as usize;
let salient = ranked(documents(page)?, cue.now);
let salient = unscored(ranked(documents(page)?, cue.now));
let mut seen = Vec::<String>::new();
let relevant_found = relevant.len();