Merge pull request 'Projector (C3a): Select stage, Projector narration, persona-tuning.toml, mind-persona project' (#76) from hands/persona-projector into main
This commit is contained in:
commit
c4bd83b026
18 changed files with 3472 additions and 850 deletions
90
Cargo.lock
generated
90
Cargo.lock
generated
|
|
@ -851,6 +851,16 @@ dependencies = [
|
|||
"polyval",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ghostlight-persona-projection"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/GameCult/Ghostlight.git?rev=c43eb54962ed4a09cc737ecac73a16b0d7ce62b4#c43eb54962ed4a09cc737ecac73a16b0d7ce62b4"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"sha2",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.17.1"
|
||||
|
|
@ -963,6 +973,16 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.14.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "inout"
|
||||
version = "0.1.4"
|
||||
|
|
@ -1098,6 +1118,7 @@ dependencies = [
|
|||
"cultnet-rs 0.3.0",
|
||||
"eureka-pipeline",
|
||||
"eureka-state",
|
||||
"ghostlight-persona-projection",
|
||||
"huginn-mind",
|
||||
"libc",
|
||||
"model-connector",
|
||||
|
|
@ -1110,7 +1131,9 @@ dependencies = [
|
|||
"sha2",
|
||||
"syn 2.0.119",
|
||||
"tempfile",
|
||||
"toml",
|
||||
"unicode-general-category",
|
||||
"unicode-segmentation",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
|
|
@ -1121,6 +1144,7 @@ source = "git+https://eureka.gamecult.org/forge/GameCult/ModelConnector.git?rev=
|
|||
dependencies = [
|
||||
"aes-gcm",
|
||||
"base64",
|
||||
"cultcache-rs 0.3.1 (git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23)",
|
||||
"cultnet-rs 0.2.0",
|
||||
"rand_core 0.6.4",
|
||||
"rmp-serde",
|
||||
|
|
@ -1684,6 +1708,15 @@ dependencies = [
|
|||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c187e0a369471c550af028cba56d4f4fa2a489ad28151ae95a4d340a2a8b782"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_stacker"
|
||||
version = "0.1.14"
|
||||
|
|
@ -1922,6 +1955,45 @@ dependencies = [
|
|||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.9.12+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf92845e79fc2e2def6a5d828f0801e29a2f8acc037becc5ab08595c7d5e9863"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde_core",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_parser",
|
||||
"toml_writer",
|
||||
"winnow 0.7.15",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.7.5+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_parser"
|
||||
version = "1.1.5+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c"
|
||||
dependencies = [
|
||||
"winnow 1.0.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_writer"
|
||||
version = "1.2.0+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "009dbb7700f11c55ef83aebbe4c0e9cf1f1ac0844bd8d6f00dc38f302026e13a"
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.44"
|
||||
|
|
@ -1980,6 +2052,12 @@ dependencies = [
|
|||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8"
|
||||
|
||||
[[package]]
|
||||
name = "universal-hash"
|
||||
version = "0.5.1"
|
||||
|
|
@ -2293,6 +2371,18 @@ version = "0.52.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.7.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8c3b93199df0ab874cdf0c6a94f0a0dc3423fe41f1cafb62b8bd7b1deb277aef"
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen"
|
||||
version = "0.57.1"
|
||||
|
|
|
|||
|
|
@ -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,13 +37,17 @@ 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"
|
||||
# Clips on grapheme boundaries (`projection::select::clip`), so no ZWJ sequence is split; already in the graph through Ghostlight.
|
||||
unicode-segmentation = "1"
|
||||
uuid = { version = "1", default-features = false, features = ["v5"] }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
|
|
@ -58,6 +66,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"
|
||||
|
|
|
|||
|
|
@ -93,19 +93,28 @@ pub(crate) fn project_names(repo: &OrgRepo, listed: &[Short]) -> Vec<Name> {
|
|||
names
|
||||
}
|
||||
|
||||
/// Whether `text` holds `name` as a whole word, in any case or exactly as written: the characters on either side are not letters or
|
||||
/// digits. An identity match on a name, not a judgement of meaning.
|
||||
/// Whether `text` holds `name` as a whole word, in any case or exactly as written. A name is glued, and so not her name,
|
||||
/// when a letter or digit touches it, or a single hyphen joins it to one (`mind-eureka`, `Eureka-chan`); everything else
|
||||
/// is a boundary on either side: whitespace, punctuation, `/`, `_`, `.`, `\`, and a double hyphen or a dash. So
|
||||
/// `/var/lib/mind-eureka/` never names her, and `_Eureka_`, `Eureka/Libby`, `ok...Eureka?` and `Huginn.toml` do. An
|
||||
/// identity match on a name, not a judgement of meaning.
|
||||
pub(crate) fn names_whole_word(text: &str, name: &str, exact: bool) -> bool {
|
||||
let (text, name) = if exact { (text.to_string(), name.to_string()) } else { (text.to_lowercase(), name.to_lowercase()) };
|
||||
if name.is_empty() {
|
||||
return false;
|
||||
}
|
||||
// Every character offset, not every non-overlapping match: a name may overlap itself ("ab ab" in "xab ab ab").
|
||||
text.char_indices().any(|(start, _)| {
|
||||
text[start..].starts_with(&name)
|
||||
&& !text[..start].chars().next_back().is_some_and(char::is_alphanumeric)
|
||||
&& !text[start + name.len()..].chars().next().is_some_and(char::is_alphanumeric)
|
||||
})
|
||||
text.char_indices().any(|(start, _)| text[start..].starts_with(&name) && !glued(text[..start].chars().rev()) && !glued(text[start + name.len()..].chars()))
|
||||
}
|
||||
|
||||
/// Whether the characters leading away from a name, nearest first, glue it to more of a word: a letter or digit, or a
|
||||
/// single hyphen with a letter or digit beyond it.
|
||||
fn glued(mut away: impl Iterator<Item = char>) -> bool {
|
||||
match away.next() {
|
||||
Some(c) if c.is_alphanumeric() => true,
|
||||
Some('-') => away.next().is_some_and(char::is_alphanumeric),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -138,3 +138,39 @@ fn a_direct_message_is_for_her_whatever_the_basis() {
|
|||
assert!(!is_for(&nibu, &ChatDelivery { form: Form::Notice, ..dm.clone() }));
|
||||
assert!(!is_for(&nibu, &ChatDelivery { form: Form::Edit, ..dm }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_name_glued_to_a_word_is_not_her_name_and_every_other_boundary_is() {
|
||||
// Soul's forms (third pass on C3a): every one names her except the six glued to a word.
|
||||
let eureka = Addressee::new("@eureka:chat", ["eureka", "Eureka"]);
|
||||
let glued = ["Eurekas idea", "Eurekaさん", "ÉEureka", "Eureka-chan", "/var/lib/mind-eureka/x", "mind-eureka"];
|
||||
let forms = [
|
||||
"Eureka, are you there", "Eureka: look", "eureka?", "Eureka!", "Eureka.", "Eureka's turn", "Eureka’s turn", "Eurekas idea", "the Eureka's", "ask eureka about it",
|
||||
"thanks eureka", "eureka", "Eureka\n", "`eureka`", "**Eureka**", "*Eureka*", "_Eureka_", "__Eureka__", "~~Eureka~~", "[Eureka](https://matrix.to/#/@eureka:gamecult.org)",
|
||||
"https://matrix.to/#/@eureka:gamecult.org hi", "@eureka:gamecult.org hi", "@eureka hi", "<@eureka>", "(Eureka)", "\"Eureka\"", "«Eureka»", "—Eureka—", "Eureka—can you",
|
||||
"Eureka--can you", "Eureka - can you", "Eureka/Libby, can you two", "Libby/Eureka can you", "Eureka/Nibu", "ok...Eureka?", "ok… Eureka?", "Hmm.Eureka?", "Eureka.Can you",
|
||||
"Eureka...", "Eureka…", "#eureka", "Eurekaさん", "エウレカ Eureka", "Eureka、", "Eureka。", "ÉEureka", "Eureka-chan", "Eureka_bot", "Eureka\\", "\\Eureka",
|
||||
"Eureka:\n> quoted", "> Eureka hi", "Eureka;", "Eureka'", "'Eureka'", "Eureka(?)", "Eureka+Libby", "Eureka&Libby", "Eureka|Libby", "Eureka=", "Eureka. ",
|
||||
"/var/lib/mind-eureka/x", "eureka.cc", "eureka_state", "mind-eureka", "eureka.gamecult.org", "https://eureka.gamecult.org/forge", "C:\\eureka\\x", "Eureka.rs",
|
||||
];
|
||||
assert_eq!(forms.len(), 69, "Soul's whole list");
|
||||
for form in forms {
|
||||
assert_eq!(addresses(&eureka, &said(form, &[])), !glued.contains(&form), "{form:?}");
|
||||
}
|
||||
// Each side of the rule on its own: a hyphen joins only with a letter or digit beyond it, before and after.
|
||||
for (text, names) in [("x-eureka", false), ("eureka-x", false), ("--eureka", true), ("eureka--", true), ("x--eureka", true), ("eureka--x", true), ("-eureka", true), ("eureka-", true), (".eureka", true), ("eureka.x", true), ("x.eureka", true)] {
|
||||
assert_eq!(addresses(&eureka, &said(text, &[])), names, "{text:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_project_name_holds_at_every_boundary_but_a_glued_one() {
|
||||
for repo in ["GameCult/Huginn", "GameCult/CultLib", "GameCult/gamecult-ops", "GameCult/cultcache-rs"] {
|
||||
let steward = steward(repo, &[]);
|
||||
let short = repo.split_once('/').unwrap().1;
|
||||
for text in [format!("{repo} PR #76"), format!("{short}/main is red"), format!("_{short}_ is down"), format!("see {short}--now"), format!("{short}.toml"), format!("{short}'s build")] {
|
||||
assert!(addresses(&steward, &said(&text, &[])), "{text:?}");
|
||||
}
|
||||
assert!(!addresses(&steward, &said(&format!("mind-{short}"), &[])), "{short} glued by a hyphen");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,15 +12,42 @@
|
|||
//! `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`, `MIND_PERSONA_MINDS` and `MIND_PERSONA_SELF_SENDER` (required), and the heartbeat's addressing
|
||||
//! from `MIND_HEARTBEAT_STATE`, which a run by hand must set as the unit does (`/var/lib/mind-heartbeat/heartbeat.cc`):
|
||||
//! without it "said to her" knows only her own names, never her projects', and stderr says so before the report; the
|
||||
//! room comes from the mouth when `BIFROST_CHAT_ENDPOINT`
|
||||
//! and `BIFROST_CHAT_KEY_FILE` are set; the tuning is `MIND_PERSONA_TUNING`, else
|
||||
//! `/etc/gamecult/minds/<instance>/persona-tuning.toml`; renders need `MIND_PERSONA_CONNECTOR_ENDPOINT`
|
||||
//! (`<loopback ip>:<port>`) and the `connector-key` credential in `CREDENTIALS_DIRECTORY`. Without `--select-only` or
|
||||
//! `--renders` it renders once.
|
||||
|
||||
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_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::heartbeat::{FileStore, HeartbeatView};
|
||||
use mind_body::projection::{HuginnPersonaSource, KitModel, Model, MouthReader, Organs as ProjectOrgans, Request as ProjectRequest, TUNING_FILE, TUNING_ROOT, TurnKind, connector_key, project};
|
||||
|
||||
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 +66,117 @@ 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 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 heartbeat = setting("MIND_HEARTBEAT_STATE").map(FileStore::new);
|
||||
let organs = ProjectOrgans {
|
||||
persona: Some(&persona),
|
||||
chat: chat.as_ref().map(|chat| chat as &dyn MouthReader),
|
||||
addressing: heartbeat.as_ref().map(|store| store as &dyn HeartbeatView),
|
||||
model: model.as_ref().map(|model| model as &dyn Model),
|
||||
};
|
||||
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(),
|
||||
minds: setting("MIND_PERSONA_MINDS").map(|ids| ids.split(',').map(str::to_owned).collect()).unwrap_or_default(),
|
||||
own_sender: required("MIND_PERSONA_SELF_SENDER")?,
|
||||
tuning,
|
||||
};
|
||||
let report = project(&organs, &request, now)?;
|
||||
let addressing = &report["addressing"];
|
||||
// The addressing source is what decides "said to her": said first, where a person running this by hand sees it.
|
||||
eprintln!(
|
||||
"mind-persona project: addressing from {} ({}); table written {}",
|
||||
addressing["source"].as_str().unwrap_or("?"),
|
||||
addressing["why"].as_str().unwrap_or("?"),
|
||||
addressing["age_minutes"].as_i64().map_or_else(|| "never".to_owned(), |minutes| format!("{minutes} min ago")),
|
||||
);
|
||||
Ok(serde_json::to_string_pretty(&report)?)
|
||||
}
|
||||
|
||||
#[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) => {
|
||||
|
|
|
|||
|
|
@ -220,6 +220,10 @@ pub trait Store {
|
|||
/// What a Persona unit may read of `heartbeat.cc`: the swarm's addressing. Read-only.
|
||||
pub trait HeartbeatView {
|
||||
fn addressing(&self) -> Result<Option<Addressing>>;
|
||||
/// When the store was last written, so a reader can tell a stale table; none when there is no store at all.
|
||||
fn written_at(&self) -> Option<DateTime<Utc>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// The store as a CultCache single-file store under the unit's state directory.
|
||||
|
|
@ -315,6 +319,10 @@ impl HeartbeatView for FileStore {
|
|||
fn addressing(&self) -> Result<Option<Addressing>> {
|
||||
self.load_addressing()
|
||||
}
|
||||
|
||||
fn written_at(&self) -> Option<DateTime<Utc>> {
|
||||
std::fs::metadata(&self.path).and_then(|meta| meta.modified()).ok().map(DateTime::<Utc>::from)
|
||||
}
|
||||
}
|
||||
|
||||
/// What the operator stores allow: the swarm brake, and each instance's own
|
||||
|
|
|
|||
|
|
@ -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,9 +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;
|
||||
|
||||
|
|
|
|||
331
crates/mind-body/src/projection.rs
Normal file
331
crates/mind-body/src/projection.rs
Normal file
|
|
@ -0,0 +1,331 @@
|
|||
//! 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.
|
||||
//!
|
||||
//! The Projector shows her self and her room: her persona state and what the mouth showed. It does not read her Mind;
|
||||
//! that is the Interpreter's read tools' (C3b), when wondering leads to reading.
|
||||
//!
|
||||
//! `project` is the experiment tool over both (`mind-persona project`): what Select would build from her real or a
|
||||
//! hypothetical state, and what the Projector would narrate. Its organs are read-only by type: `PersonaSource` gets,
|
||||
//! `MouthReader` reads pending chat, `Model` asks. None of them can put, admit, send, ack or open a conversation, so
|
||||
//! `project` cannot write and runs under a held brake.
|
||||
|
||||
mod narrate;
|
||||
mod select;
|
||||
mod tuning;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use chrono::{DateTime, Utc};
|
||||
use eureka_state::HuginnClient;
|
||||
use huginn_mind::HuginnMindResponse;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::address::{Addressee, is_for, localpart};
|
||||
use crate::chat_client::{Chat, ChatPending, Form};
|
||||
use crate::heartbeat::HeartbeatView;
|
||||
|
||||
pub use narrate::{Answer, Failed, KitModel, Model, Spent, connector_key};
|
||||
pub use select::TurnKind;
|
||||
pub use tuning::{TUNING_FILE, TUNING_ROOT};
|
||||
|
||||
use narrate::{cap_words, distinct_2, field_names, judge_prompt, judged, projector_prompt, word_budget, words};
|
||||
use select::{History, Inputs, Packet, People, Room, RoomLine, select};
|
||||
|
||||
/// The agent name on the connector's request ids.
|
||||
const AGENT: &str = "mind-persona";
|
||||
/// 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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The mouth, read: pending deliveries and the room tail, which acks nothing. The only chat port `project` holds.
|
||||
pub trait MouthReader {
|
||||
fn pending(&self) -> Result<ChatPending>;
|
||||
}
|
||||
|
||||
impl<T: Chat> MouthReader for T {
|
||||
fn pending(&self) -> Result<ChatPending> {
|
||||
Chat::pending(self, 0)
|
||||
}
|
||||
}
|
||||
|
||||
/// What `project` reads through. Each is a read-only port by type: holding an `Organs`, no code can send, ack or open
|
||||
/// a conversation, or put her state.
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// fn acks(organs: &mind_body::projection::Organs) {
|
||||
/// organs.chat.unwrap().ack(1).unwrap();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// fn sends(organs: &mind_body::projection::Organs, request: &mind_body::chat_client::ChatSendRequest) {
|
||||
/// organs.chat.unwrap().send(request).unwrap();
|
||||
/// }
|
||||
/// ```
|
||||
pub struct Organs<'a> {
|
||||
/// Her stored state; not asked when the request carries a state.
|
||||
pub persona: Option<&'a dyn PersonaSource>,
|
||||
/// The mouth, for the room; none shows no room.
|
||||
pub chat: Option<&'a dyn MouthReader>,
|
||||
/// The heartbeat's addressing table, read-only: her names and her projects' names, the ones the heartbeat woke her
|
||||
/// by. None, or a table without her, addresses her by her own names only, and the report says so.
|
||||
pub addressing: Option<&'a dyn HeartbeatView>,
|
||||
/// 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>,
|
||||
/// The Matrix ids of the swarm's Minds: their bonds name them by localpart; anyone else is keyed by full id.
|
||||
pub minds: Vec<String>,
|
||||
/// Her own Matrix id. Required: without it her own lines would read as said to her.
|
||||
pub own_sender: String,
|
||||
pub tuning: PathBuf,
|
||||
}
|
||||
|
||||
/// Her addressee: the heartbeat's row for her when its table holds one, else her own names (handle and public name).
|
||||
/// Her addressee and where it came from: `{source, why, written_at, age_minutes}`. The heartbeat's row when its table
|
||||
/// holds one; else her own names (handle and public name), with why: no store configured, no heartbeat file, a file
|
||||
/// that could not be read, or a table without her row. The table's age is the store's last write: under a held swarm
|
||||
/// brake the heartbeat does not poll, so an old table is a stale one.
|
||||
fn own_addressee(addressing: Option<&dyn HeartbeatView>, own_sender: &str, public_name: &str, now: DateTime<Utc>) -> (Addressee, Value) {
|
||||
let written_at = addressing.and_then(HeartbeatView::written_at);
|
||||
let age = written_at.map(|at| (now - at).num_minutes());
|
||||
let report = |source: &str, why: &str| json!({ "source": source, "why": why, "written_at": written_at.map(|at| at.to_rfc3339()), "age_minutes": age });
|
||||
let table = addressing.map(|view| view.addressing());
|
||||
if let Some(Ok(Some(table))) = &table
|
||||
&& let Some(row) = table.minds.iter().find(|row| row.matrix_id() == own_sender)
|
||||
{
|
||||
return (row.clone(), report("heartbeat", "her row in the heartbeat's addressing table"));
|
||||
}
|
||||
let why = match (&table, written_at) {
|
||||
(None, _) => "no heartbeat addressing is configured (MIND_HEARTBEAT_STATE is not set)".to_owned(),
|
||||
(Some(Err(fault)), _) => format!("the heartbeat addressing could not be read: {fault:#}"),
|
||||
(Some(Ok(Some(_))), _) => "the heartbeat's addressing table has no row for her".to_owned(),
|
||||
(Some(Ok(None)), None) => "there is no heartbeat file".to_owned(),
|
||||
(Some(Ok(None)), Some(_)) => "the heartbeat file holds no addressing table".to_owned(),
|
||||
};
|
||||
(Addressee::new(own_sender, [localpart(own_sender), public_name]), report("own names only", &why))
|
||||
}
|
||||
|
||||
/// 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 MouthReader, request: &Request, own: &Addressee) -> (Room, Value) {
|
||||
let pending = match chat.pending() {
|
||||
Ok(pending) => pending,
|
||||
Err(fault) => return (Room { here: request.room.clone(), ..Room::default() }, json!({ "read": false, "why": format!("{fault:#}") })),
|
||||
};
|
||||
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,
|
||||
// The heartbeat's rule, the one that woke her: never a second opinion here.
|
||||
to_her: is_for(own, delivery),
|
||||
})
|
||||
.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)
|
||||
}
|
||||
|
||||
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() },
|
||||
"overflow": packet.overflow,
|
||||
"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.
|
||||
/// A failed call is a record with its reason and what it cost, never the end of the report.
|
||||
fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuning::Tuning, tag: &str, now: DateTime<Utc>) -> Value {
|
||||
let request_id = format!("{AGENT}:{instance}:project:{tag}:{}:{}", packet.seed, now.timestamp_millis());
|
||||
let ids: Vec<String> = packet.selected.iter().map(|pick| pick.id.clone()).collect();
|
||||
let answer = match model.ask(&request_id, &tuning.projector.model, &projector_prompt(packet, tuning)) {
|
||||
Ok(answer) => answer,
|
||||
Err(failed) => return json!({ "seed": packet.seed, "lens": packet.lens, "selected": ids, "failed": failed.reason, "usage": failed.usage }),
|
||||
};
|
||||
let raw = answer.text;
|
||||
let budget = word_budget(packet.kind, tuning);
|
||||
let narration = cap_words(&raw, budget);
|
||||
let named: Vec<String> = packet.ids.clone();
|
||||
json!({
|
||||
"seed": packet.seed,
|
||||
"lens": packet.lens,
|
||||
"selected": ids,
|
||||
"words": words(&narration),
|
||||
"cut_at_budget": words(&raw) > budget,
|
||||
"field_names": field_names(&narration, &named),
|
||||
"usage": answer.usage,
|
||||
"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> {
|
||||
if !request.own_sender.starts_with('@') || !request.own_sender.contains(':') {
|
||||
bail!("her own Matrix id (MIND_PERSONA_SELF_SENDER) is required: without it her own lines would read as said to her");
|
||||
}
|
||||
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 (own, addressing) = own_addressee(organs.addressing, &request.own_sender, &public_name, now);
|
||||
let (room, room_report) = match organs.chat {
|
||||
Some(chat) => read_room(chat, request, &own),
|
||||
None => (Room { here: request.room.clone(), ..Room::default() }, json!({ "read": false, "why": "no mouth is configured" })),
|
||||
};
|
||||
// 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, minds: &request.minds, own_sender: &request.own_sender };
|
||||
let inputs = Inputs { kind: request.kind, state: &state, room: &room, history: &history, observation: None, people, now, seed: request.seed };
|
||||
|
||||
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,
|
||||
"addressing": addressing,
|
||||
"history": "none: freshness and her own recent words come from trace v2, which the commit writes (C3c)",
|
||||
});
|
||||
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();
|
||||
let texts = |narrations: &[Value]| narrations.iter().filter_map(|one| one["narration"].as_str().map(str::to_owned)).collect::<Vec<_>>();
|
||||
let failed = |narrations: &[Value]| narrations.iter().filter(|one| one.get("failed").is_some()).count();
|
||||
report["failed_renders"] = json!(failed(&narrations));
|
||||
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();
|
||||
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) };
|
||||
// A narration that failed has nothing to judge; it is counted under the renders' failures.
|
||||
let Some(text) = narration["narration"].as_str() else { continue };
|
||||
let answer = match model.ask(&format!("{AGENT}:{}:judge:{at}:{}", request.instance, now.timestamp_millis()), &tuning.projector.model, &judge_prompt(first, second, text)) {
|
||||
Ok(answer) => answer,
|
||||
Err(failed) => {
|
||||
verdicts.push(json!({ "from": truth.to_string(), "failed": failed.reason, "usage": failed.usage }));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let letter = judged(&answer.text).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, "usage": answer.usage }));
|
||||
}
|
||||
// Accuracy over the verdicts the judge gave; a failed call is no verdict, and any failure fails the eval.
|
||||
let judged_count = verdicts.iter().filter(|verdict| verdict.get("failed").is_none()).count();
|
||||
let failures = verdicts.len() - judged_count + failed(&narrations) + failed(&other_narrations);
|
||||
let accuracy = right as f64 / judged_count.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, "failures": failures, "passed": accuracy >= JUDGE_PASS && !leaked && failures == 0, "verdicts": verdicts });
|
||||
}
|
||||
report["renders"] = Value::Array(narrations);
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
22
crates/mind-body/src/projection/fixtures/golden-chat.txt
Normal file
22
crates/mind-body/src/projection/fixtures/golden-chat.txt
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
lens: MemorySurfacing
|
||||
selected: libby-sounds, t1-m0, curse, need-relatedness
|
||||
present: Kiko, Libby
|
||||
--- identity
|
||||
Eureka (she/her). Dry, direct, almost feral; zeal aimed at revealing.
|
||||
--- context
|
||||
- now: Thursday 19 November, 14:05 UTC
|
||||
- mood, with no earlier turn to measure from: lighter, more settled than middling
|
||||
- bond [bond-kiko] Kiko, sparring partner: fair trust, heavy friction. Kiko pokes her to stop massaging the pipeline.
|
||||
- bond [bond-libby] Libby, collaborator: fair trust, a little friction. Libby holds the sounds and teases her about the pipeline.
|
||||
- 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
|
||||
--- stimulus
|
||||
Said to her:
|
||||
- Libby in #swarm:gamecult.org: "Eureka, did you see the door fix?"
|
||||
Also in the room:
|
||||
- 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."
|
||||
322
crates/mind-body/src/projection/narrate.rs
Normal file
322
crates/mind-body/src/projection/narrate.rs
Normal file
|
|
@ -0,0 +1,322 @@
|
|||
//! 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 serde::Serialize;
|
||||
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. A failed turn
|
||||
/// keeps whatever usage the connector reported for it.
|
||||
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> std::result::Result<Answer, Failed>;
|
||||
}
|
||||
|
||||
/// A turn that produced no answer: why, and what it cost when the connector's receipt says (a refusal, a fault before
|
||||
/// the provider, or a failure with no usage block costs none). A receipt whose usage block is missing or malformed does
|
||||
/// not decode at all: that is model-connector's error, reported as the reason.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Failed {
|
||||
pub reason: String,
|
||||
pub usage: Option<Spent>,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Failed {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
formatter.write_str(&self.reason)
|
||||
}
|
||||
}
|
||||
|
||||
impl Failed {
|
||||
fn without_usage(reason: impl Into<String>) -> Self {
|
||||
Self { reason: reason.into(), usage: None }
|
||||
}
|
||||
}
|
||||
|
||||
fn spent(model: &str, usage: &model_connector::Usage) -> Spent {
|
||||
Spent { model: model.to_owned(), input_tokens: usage.input_tokens, output_tokens: usage.output_tokens, cache_read_tokens: usage.cache_read_tokens, cache_write_tokens: usage.cache_write_tokens, reasoning_tokens: usage.reasoning_tokens }
|
||||
}
|
||||
|
||||
/// A model's answer, and what the turn cost as the connector counted it: none when the answerer counts nothing.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Answer {
|
||||
pub text: String,
|
||||
pub usage: Option<Spent>,
|
||||
}
|
||||
|
||||
/// One turn's tokens, by the model that ran it, as the connector's receipt reports them.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
|
||||
pub struct Spent {
|
||||
pub model: String,
|
||||
pub input_tokens: u64,
|
||||
pub output_tokens: u64,
|
||||
pub cache_read_tokens: u64,
|
||||
pub cache_write_tokens: u64,
|
||||
pub reasoning_tokens: Option<u64>,
|
||||
}
|
||||
|
||||
/// 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) -> std::result::Result<Answer, Failed> {
|
||||
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(|_| Failed::without_usage("the request is not a valid connector request"))?;
|
||||
let result = self.client.execute(&invocation).map_err(|fault| Failed::without_usage(format!("the connector did not answer: {fault}")))?;
|
||||
match result.disposition {
|
||||
TransportDisposition::Refused(reason) => Err(Failed::without_usage(format!("the connector refused the call: {reason:?}"))),
|
||||
TransportDisposition::Transported { events, receipt } => match receipt.outcome {
|
||||
TransportOutcome::Completed { stop_reason: StopReason::EndTurn, usage, .. } => Ok(Answer {
|
||||
text: events
|
||||
.into_iter()
|
||||
.filter_map(|event| match event.payload {
|
||||
TransportEventPayload::TextDelta { text } => Some(text),
|
||||
_ => None,
|
||||
})
|
||||
.collect(),
|
||||
usage: Some(spent(model, &usage)),
|
||||
}),
|
||||
TransportOutcome::Completed { stop_reason, usage, .. } => Err(Failed { reason: format!("the model's turn ended early: {stop_reason:?}"), usage: Some(spent(model, &usage)) }),
|
||||
TransportOutcome::Failed { kind, usage, .. } => Err(Failed { reason: format!("the provider failed the call: {kind:?}"), usage: usage.as_ref().map(|usage| spent(model, usage)) }),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// 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) })
|
||||
}
|
||||
|
||||
/// A character that is a word on its own: Han, kana and Hangul are written without spaces, so each counts as one.
|
||||
fn cjk(c: char) -> bool {
|
||||
matches!(u32::from(c), 0x3040..=0x30FF | 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xAC00..=0xD7AF | 0xF900..=0xFAFF | 0x20000..=0x2FFFF)
|
||||
}
|
||||
|
||||
/// CJK and full-width punctuation (`。`, `,`, `!`, `?`, `「」`): it belongs to the word before it and starts none.
|
||||
fn cjk_punctuation(c: char) -> bool {
|
||||
matches!(u32::from(c), 0x3000..=0x303F | 0xFF01..=0xFF0F | 0xFF1A..=0xFF20 | 0xFF3B..=0xFF40 | 0xFF5B..=0xFF65)
|
||||
}
|
||||
|
||||
/// The byte offset where each word of `text` begins: a run of non-space characters is one word, and a CJK character is
|
||||
/// a word of its own.
|
||||
fn word_starts(text: &str) -> Vec<usize> {
|
||||
let mut starts = Vec::new();
|
||||
let mut in_word = false;
|
||||
for (at, c) in text.char_indices() {
|
||||
if c.is_whitespace() || cjk_punctuation(c) {
|
||||
in_word = false;
|
||||
} else if cjk(c) {
|
||||
starts.push(at);
|
||||
in_word = false;
|
||||
} else if !in_word {
|
||||
starts.push(at);
|
||||
in_word = true;
|
||||
}
|
||||
}
|
||||
starts
|
||||
}
|
||||
|
||||
/// The share of the budget a sentence cut must keep; below it the word cut keeps more of the stream.
|
||||
const SENTENCE_CUT_FLOOR: f64 = 0.6;
|
||||
|
||||
/// `text` held to `max` words (whitespace-separated, each CJK character one); the model's count is not trusted. An
|
||||
/// overrun is cut at the last sentence end inside the budget, so the stream ends on a whole sentence, when that keeps
|
||||
/// at least 60% of the budget; otherwise, or with no sentence end inside it, it is cut after the `max`-th word. No
|
||||
/// abbreviation list: a cut after "e.g." is accepted.
|
||||
pub(crate) fn cap_words(text: &str, max: usize) -> String {
|
||||
let text = text.trim();
|
||||
let starts = word_starts(text);
|
||||
let Some(&over) = starts.get(max) else { return text.to_owned() };
|
||||
let within = text[..over].trim_end();
|
||||
match last_sentence_end(within) {
|
||||
Some(end) if words(&within[..end]) as f64 >= SENTENCE_CUT_FLOOR * max as f64 => within[..end].to_owned(),
|
||||
_ => within.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The byte just past the last sentence end in `text`: a `.`, `!`, `?` or `…` with any closing quotes or brackets after
|
||||
/// it, followed by whitespace or the end of the text; or a `。`, `!` or `?`, which ends a sentence where it stands.
|
||||
fn last_sentence_end(text: &str) -> Option<usize> {
|
||||
let chars: Vec<(usize, char)> = text.char_indices().collect();
|
||||
let mut found = None;
|
||||
for (index, (at, c)) in chars.iter().enumerate() {
|
||||
let full_width = matches!(c, '。' | '!' | '?');
|
||||
if !full_width && !matches!(c, '.' | '!' | '?' | '…') {
|
||||
continue;
|
||||
}
|
||||
let mut end = at + c.len_utf8();
|
||||
let mut next = index + 1;
|
||||
while let Some((close_at, close)) = chars.get(next).filter(|(_, close)| matches!(close, '"' | '\'' | '”' | '’' | ')' | ']' | '」' | '』')) {
|
||||
end = close_at + close.len_utf8();
|
||||
next += 1;
|
||||
}
|
||||
if full_width || chars.get(next).is_none_or(|(_, after)| after.is_whitespace()) {
|
||||
found = Some(end);
|
||||
}
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
/// Words as the cap counts them.
|
||||
pub(crate) fn words(text: &str) -> usize {
|
||||
word_starts(text).len()
|
||||
}
|
||||
|
||||
/// The one kebab form that is ordinary English: "short-term" folds to the schema key `shortTerm`, and prose must not fail
|
||||
/// the judge for it. `shortTerm`, `short_term` and the run-together `shortterm` are still caught.
|
||||
const KEBAB_ENGLISH: &str = "short-term";
|
||||
|
||||
/// Her persona state's schema: its property names are the identifiers a narration must never carry.
|
||||
const PERSONA_SCHEMA: &str = include_str!("../../../../schemas/cultnet/gamecult.persona_state.v0.schema.json");
|
||||
|
||||
/// An identifier as compared: lowercased with its underscores and hyphens gone, so `socialBonds`, `SocialBonds`,
|
||||
/// `social_bonds`, `social-bonds`, `SOCIAL_BONDS` and `socialbonds` are one.
|
||||
fn folded(identifier: &str) -> String {
|
||||
identifier.chars().filter(|c| !matches!(c, '_' | '-')).flat_map(char::to_lowercase).collect()
|
||||
}
|
||||
|
||||
/// Whether a word is shaped like an identifier rather than prose: an underscore, or a capital after its first letter.
|
||||
fn identifier_shaped(word: &str) -> bool {
|
||||
word.contains('_') || word.chars().skip(1).any(char::is_uppercase)
|
||||
}
|
||||
|
||||
fn keys_of(value: &serde_json::Value, schema: bool, into: &mut BTreeSet<String>) {
|
||||
match value {
|
||||
serde_json::Value::Object(map) => {
|
||||
for (key, inner) in map {
|
||||
if schema && key == "properties"
|
||||
&& let Some(properties) = inner.as_object()
|
||||
{
|
||||
into.extend(properties.keys().cloned());
|
||||
}
|
||||
if !schema {
|
||||
into.insert(key.clone());
|
||||
}
|
||||
keys_of(inner, schema, into);
|
||||
}
|
||||
}
|
||||
serde_json::Value::Array(values) => values.iter().for_each(|inner| keys_of(inner, schema, into)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// The folded compound identifiers of the persona schema and of the tuning: a plain word such as `trust` or
|
||||
/// `tension` is prose and is not among them.
|
||||
fn identifiers() -> BTreeSet<String> {
|
||||
let mut keys = BTreeSet::new();
|
||||
if let Ok(schema) = serde_json::from_str::<serde_json::Value>(PERSONA_SCHEMA) {
|
||||
keys_of(&schema, true, &mut keys);
|
||||
}
|
||||
if let Ok(tuning) = serde_json::to_value(Tuning::default()) {
|
||||
keys_of(&tuning, false, &mut keys);
|
||||
}
|
||||
keys.into_iter().filter(|key| identifier_shaped(key)).map(|key| folded(&key)).collect()
|
||||
}
|
||||
|
||||
/// The identifiers of typed state a narration carries: any token that folds to a compound property name of her persona
|
||||
/// schema or a tuning key, in every case convention (camelCase, PascalCase, snake_case, SCREAMING_CASE, kebab-case and
|
||||
/// run together), and any id the packet named (`ids`), as an exact token when it cannot be a plain word, or inside
|
||||
/// brackets as the packet writes it. Only compound names are known, so a plain word such as `trust`, `tension` or
|
||||
/// `values` is prose: "need-to-know", "trust:" and "Tension:" pass. A small deterministic guard on identifiers, never on
|
||||
/// meaning.
|
||||
pub(crate) fn field_names(narration: &str, ids: &[String]) -> Vec<String> {
|
||||
let known = identifiers();
|
||||
let mut found = BTreeSet::new();
|
||||
for token in narration.split(|c: char| !(c.is_alphanumeric() || c == '_' || c == '-')).map(|token| token.trim_matches('-')).filter(|token| !token.is_empty()) {
|
||||
if known.contains(&folded(token)) && !token.eq_ignore_ascii_case(KEBAB_ENGLISH) {
|
||||
found.insert(token.to_owned());
|
||||
}
|
||||
}
|
||||
let tokens: BTreeSet<&str> = narration.split_whitespace().map(|token| token.trim_matches(|c: char| !(c.is_alphanumeric() || c == '-' || c == '_' || c == ':'))).collect();
|
||||
for id in ids {
|
||||
let compound = id.contains(['-', '_', ':']) || id.chars().any(|c| c.is_ascii_digit());
|
||||
if (compound && tokens.contains(id.as_str())) || 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')
|
||||
}
|
||||
894
crates/mind-body/src/projection/select.rs
Normal file
894
crates/mind-body/src/projection/select.rs
Normal file
|
|
@ -0,0 +1,894 @@
|
|||
//! 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.
|
||||
//!
|
||||
//! The Projector shows her self and her room: her persona state and what the mouth showed. Her Mind is not read
|
||||
//! here; reaching into it is the Interpreter's, through its read tools, when wondering leads to reading.
|
||||
//!
|
||||
//! Every packet carries its fixed slots: identity, the hour, her mood as change, a bond line for each person
|
||||
//! present, and the stimulus. The rest comes from her state's pools. Each candidate is ordered by
|
||||
//! `log(score) / T` plus Gumbel noise and included with probability `1 - exp(-score / inclusion_score)`, where
|
||||
//! `score = salience × recency × touch × relevance × unresolved × freshness`; then each pool's cap and the context
|
||||
//! budget bound what is kept. A cap is a maximum, never a target: a pool with fewer candidates than its cap still
|
||||
//! rotates. Recency decays only the short-term items: a crystallized memory, a value or a stance is as near as it ever
|
||||
//! was, and comes forward by what the room is touching (`relevance`, a lexical overlap with the stimulus).
|
||||
//!
|
||||
//! Who a message is to is `address::is_for`'s, the rule the heartbeat woke her by, and nothing else here. People are
|
||||
//! keyed by their full Matrix id; only the operator and the Minds of the swarm are keyed by the names their bonds use,
|
||||
//! and an unbonded speaker is shown by the display name the mouth reports, quoted and escaped, with the Matrix id after
|
||||
//! it, never under a bonded person's name. Every message body and display name is rendered by one escape (`spoken`, a
|
||||
//! JSON string), so no text can forge a line; speaker and room come from the mouth's metadata alone.
|
||||
//!
|
||||
//! 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 unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::tuning::{LensWeights, Tuning};
|
||||
use crate::address::localpart;
|
||||
|
||||
/// 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;
|
||||
/// The most bytes a bond's name or kind takes in its line.
|
||||
const NAME_BYTES: usize = 80;
|
||||
/// The most bytes the line naming the unbonded people present takes.
|
||||
const ALSO_HERE_BYTES: usize = 300;
|
||||
|
||||
/// What kind of turn the packet is for. The stimulus and nothing else differs between them.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
||||
#[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 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). The commit's trace v2 (C3c) is its source; until then it is
|
||||
/// empty.
|
||||
#[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],
|
||||
/// The Matrix ids of the swarm's Minds: their bonds name them by localpart.
|
||||
pub minds: &'a [String],
|
||||
pub own_sender: &'a str,
|
||||
}
|
||||
|
||||
impl People<'_> {
|
||||
/// The id a bond names a person by: `operator` for the operator, a Mind's localpart for a Mind of the swarm, and the
|
||||
/// full Matrix id for anyone else, so `@libby:evil.example` is never Libby.
|
||||
pub(crate) fn person_of(&self, sender: &str) -> String {
|
||||
if self.operators.iter().any(|operator| operator == sender) {
|
||||
"operator".into()
|
||||
} else if self.minds.iter().any(|mind| mind == sender) {
|
||||
localpart(sender).to_lowercase()
|
||||
} else {
|
||||
sender.to_lowercase()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Inputs<'a> {
|
||||
pub kind: TurnKind,
|
||||
pub state: &'a Value,
|
||||
pub room: &'a Room,
|
||||
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,
|
||||
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,
|
||||
/// Bytes the messages to her took past the stimulus and heard caps: verbatim beats the cap.
|
||||
pub overflow: Overflow,
|
||||
pub present: Vec<String>,
|
||||
/// Every typed id the packet's lines name: the selected items' and the bonds' of the people present.
|
||||
pub ids: Vec<String>,
|
||||
pub selected: Vec<Pick>,
|
||||
}
|
||||
|
||||
/// What the messages to her cost past the caps: bytes over the stimulus and heard caps, the older messages to her the
|
||||
/// `addressed_bytes` ceiling left out, and the bytes clipped from a single message larger than the ceiling.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
|
||||
pub(crate) struct Overflow {
|
||||
pub stimulus: usize,
|
||||
pub heard: usize,
|
||||
pub said_to_her_not_shown: usize,
|
||||
pub clipped_bytes: usize,
|
||||
}
|
||||
|
||||
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 grapheme 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 room = max.saturating_sub('…'.len_utf8());
|
||||
let end = text.grapheme_indices(true).map(|(at, grapheme)| at + grapheme.len()).take_while(|end| *end <= room).last().unwrap_or(0);
|
||||
format!("{}…", &text[..end])
|
||||
}
|
||||
|
||||
fn one_line(text: &str) -> String {
|
||||
text.split_whitespace().collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
|
||||
/// The words of a text that can carry a topic: four letters or more, lowercased.
|
||||
fn content_words(text: &str) -> BTreeSet<String> {
|
||||
text.split(|c: char| !c.is_alphanumeric()).filter(|word| word.chars().count() >= 4).map(str::to_lowercase).collect()
|
||||
}
|
||||
|
||||
/// `ln(1 + min(1, shared / 3))`: how much of an item the room is touching, by the content words it shares with the
|
||||
/// stimulus. Lexical and cheap; no embedder.
|
||||
fn relevance_log(item_text: &str, touching: &BTreeSet<String>) -> f64 {
|
||||
let shared = content_words(item_text).intersection(touching).count();
|
||||
(1.0 + (shared as f64 / 3.0).min(1.0)).ln()
|
||||
}
|
||||
|
||||
/// A message body or a display name as every line renders it: a JSON string, quotes, backslashes and every control
|
||||
/// character escaped (`\n`, `\t`, `\r`, `\u…`, and the Unicode line separators too), so no text can end its line or
|
||||
/// start another, and the escape is reversible: nothing is dropped.
|
||||
pub(crate) fn spoken(text: &str) -> String {
|
||||
crate::persona::quoted(text)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// 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]) { 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.
|
||||
|
||||
/// `ln(base ^ k)`, k the times `id` was shown in the last `freshness_window` projections: 0 when never shown, minus
|
||||
/// infinity when the base is 0 and it was shown at all.
|
||||
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();
|
||||
if shown == 0 { 0.0 } else { tuning.select.freshness_base.powi(i32::try_from(shown).unwrap_or(i32::MAX)).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>, touching: &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 crystallized = items(state, &["thoughtMemory", "memories"]).iter().map(|memory| (memory, false));
|
||||
let memories = crystallized.chain(items(state, &["thoughtMemory", "shortTerm"]).iter().map(|memory| (memory, true)));
|
||||
for (memory, short_term) 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);
|
||||
// Only the day's short-term items fade; a crystallized memory comes forward by what the room touches.
|
||||
let nearness = if short_term { recency_log(time(memory), inputs.now, tuning.select.memory_tau_h) } else { relevance_log(&body, touching) };
|
||||
push(text(memory, &["id"]), pool, line, salience, nearness, 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), relevance_log(text(stance, &["summary"]), touching), touch, 1.0);
|
||||
}
|
||||
|
||||
for value in items(state, &["values"]) {
|
||||
let line = format!("- value [{}] {}: {}", text(value, &["id"]), clip(text(value, &["label"]), NAME_BYTES), clip(&one_line(text(value, &["summary"])), item_bytes));
|
||||
let relevance = relevance_log(&format!("{} {}", text(value, &["label"]), text(value, &["summary"])), touching);
|
||||
push(text(value, &["id"]), Pool::Value, line, number(value, &["priority"]).unwrap_or(SALIENCE_UNSTATED), relevance, 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, spoken(&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::Value => caps.value,
|
||||
Pool::Said => caps.said,
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// The stimulus, the people present and the fixed slots.
|
||||
|
||||
/// How a speaker reads: a bonded person by the bond's label; anyone else by their display name with the Matrix id beside
|
||||
/// it, so a stranger called "Meta" never reads as the operator.
|
||||
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| live(bond) && text(bond, &["object", "id"]).to_lowercase() == person).map(|bond| text(bond, &["object", "label"]).to_owned()).filter(|label| !label.is_empty());
|
||||
match bonded {
|
||||
Some(label) => clip(&one_line(&label), NAME_BYTES),
|
||||
// A display name is the speaker's own text: quoted and escaped, at most `NAME_BYTES`, the real id always after it.
|
||||
None if !line.display.trim().is_empty() => format!("{} ({})", spoken(&clip(line.display.trim(), NAME_BYTES)), line.sender),
|
||||
None => line.sender.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Lines appended under a byte cap: a line that would pass it is refused whole.
|
||||
struct Lines {
|
||||
out: String,
|
||||
cap: usize,
|
||||
}
|
||||
|
||||
impl Lines {
|
||||
fn new(cap: usize) -> Self {
|
||||
Self { out: String::new(), cap }
|
||||
}
|
||||
|
||||
fn fits(&self, line: &str) -> bool {
|
||||
self.out.len() + line.len() + usize::from(!self.out.is_empty()) <= self.cap
|
||||
}
|
||||
|
||||
fn push(&mut self, line: &str) -> bool {
|
||||
if !self.fits(line) {
|
||||
return false;
|
||||
}
|
||||
if !self.out.is_empty() {
|
||||
self.out.push('\n');
|
||||
}
|
||||
self.out.push_str(line);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// A section of room lines under `cap` bytes: its heading, a note counting the older lines that did not fit, then the
|
||||
/// newest whole lines that did. Empty when there are no lines, or not even the heading fits.
|
||||
fn newest_section(heading: &str, lines: &[String], cap: usize) -> String {
|
||||
if lines.is_empty() {
|
||||
return String::new();
|
||||
}
|
||||
// The note is at most this long: "(NNNNN earlier not shown)".
|
||||
let note_room = 32;
|
||||
let mut kept: Vec<&String> = Vec::new();
|
||||
let mut used = heading.len() + note_room;
|
||||
for line in lines.iter().rev() {
|
||||
if used + line.len() + 1 > cap {
|
||||
break;
|
||||
}
|
||||
used += line.len() + 1;
|
||||
kept.push(line);
|
||||
}
|
||||
kept.reverse();
|
||||
let dropped = lines.len() - kept.len();
|
||||
let mut out = Lines::new(cap);
|
||||
if !out.push(heading) {
|
||||
return String::new();
|
||||
}
|
||||
let mut shown = false;
|
||||
if dropped > 0 {
|
||||
shown |= out.push(&format!("({dropped} earlier not shown)"));
|
||||
}
|
||||
for line in kept {
|
||||
shown |= out.push(line);
|
||||
}
|
||||
// A heading over nothing says nothing.
|
||||
if shown { out.out } else { String::new() }
|
||||
}
|
||||
|
||||
/// Which messages to her a turn shows: whole messages, newest first, until the `addressed_bytes` ceiling, and a single
|
||||
/// message larger than the ceiling clipped on a grapheme boundary (`cut`, the bytes of its text kept). Decided once, on
|
||||
/// the stimulus's lines, and rendered for the stimulus and the heard lines alike.
|
||||
struct Addressed<'a> {
|
||||
/// Chronological: each message shown, and where its text is cut if it is.
|
||||
shown: Vec<(&'a RoomLine, Option<usize>)>,
|
||||
left_out: usize,
|
||||
clipped: usize,
|
||||
}
|
||||
|
||||
impl Addressed<'_> {
|
||||
fn lines(&self, render: impl Fn(&RoomLine, &str) -> String) -> Vec<String> {
|
||||
let mut lines: Vec<String> = Vec::new();
|
||||
if self.left_out > 0 {
|
||||
lines.push(format!("({} more said to her, not shown)", self.left_out));
|
||||
}
|
||||
lines.extend(self.shown.iter().map(|(line, cut)| match cut {
|
||||
None => render(line, &spoken(&line.text)),
|
||||
Some(cut) => render(line, &format!("{} [… {} bytes more]", spoken(&line.text[..*cut]), line.text.len() - cut)),
|
||||
}));
|
||||
lines
|
||||
}
|
||||
}
|
||||
|
||||
fn said_to_her<'a>(to_her: &[&'a RoomLine], render: impl Fn(&RoomLine, &str) -> String, ceiling: usize) -> Addressed<'a> {
|
||||
let mut shown: Vec<(&RoomLine, Option<usize>)> = Vec::new();
|
||||
let mut used = 0;
|
||||
let mut clipped = 0;
|
||||
for line in to_her.iter().rev() {
|
||||
let whole = render(line, &spoken(&line.text));
|
||||
if used + whole.len() < ceiling {
|
||||
used += whole.len() + 1;
|
||||
shown.push((line, None));
|
||||
continue;
|
||||
}
|
||||
if shown.is_empty() {
|
||||
// One message larger than the ceiling: as much of it as fits, cut between graphemes.
|
||||
let ends: Vec<usize> = line.text.grapheme_indices(true).map(|(at, grapheme)| at + grapheme.len()).collect();
|
||||
let fitting = |end: usize| render(line, &format!("{} [… {} bytes more]", spoken(&line.text[..end]), line.text.len() - end));
|
||||
let fit = ends.partition_point(|end| fitting(*end).len() < ceiling);
|
||||
let cut = if fit == 0 { 0 } else { ends[fit - 1] };
|
||||
clipped = line.text.len() - cut;
|
||||
shown.push((line, Some(cut)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
let left_out = to_her.len() - shown.len();
|
||||
shown.reverse();
|
||||
Addressed { shown, left_out, clipped }
|
||||
}
|
||||
|
||||
fn chat_stimulus(inputs: &Inputs, tuning: &Tuning) -> (String, String, Overflow) {
|
||||
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(), Overflow::default());
|
||||
}
|
||||
let (to_her, others): (Vec<&RoomLine>, Vec<&RoomLine>) = messages.into_iter().partition(|line| line.to_her);
|
||||
let typed = |line: &RoomLine, body: &str| format!("- {} in {}: {body}", name_of(line, inputs), line.room);
|
||||
// Heard lines are prose for the Persona: the speaker and the room from the mouth, then the escaped words.
|
||||
let heard = |line: &RoomLine, body: &str| format!("{}, in {}: {body}", name_of(line, inputs), line.room);
|
||||
let ceiling = tuning.select.addressed_bytes;
|
||||
// One selection, rendered twice: the stimulus and the heard lines show the same messages to her and the same count.
|
||||
let addressed = said_to_her(&to_her, typed, ceiling);
|
||||
let (left_out, clipped) = (addressed.left_out, addressed.clipped);
|
||||
let typed_to_her = addressed.lines(typed);
|
||||
let heard_to_her = addressed.lines(heard);
|
||||
|
||||
let mut stimulus = String::new();
|
||||
if !typed_to_her.is_empty() {
|
||||
stimulus.push_str("Said to her:");
|
||||
for line in &typed_to_her {
|
||||
stimulus.push('\n');
|
||||
stimulus.push_str(line);
|
||||
}
|
||||
}
|
||||
let others_typed: Vec<String> = others.iter().map(|line| typed(line, &spoken(&line.text))).collect();
|
||||
let room_left = tuning.select.stimulus_bytes.saturating_sub(stimulus.len() + 1);
|
||||
let mut also = newest_section("Also in the room:", &others_typed, room_left);
|
||||
if also.is_empty() && !others_typed.is_empty() {
|
||||
// The messages to her took the room: the others are counted, never silently gone.
|
||||
also = format!("({} more in the room not shown)", others_typed.len());
|
||||
}
|
||||
if !also.is_empty() {
|
||||
if !stimulus.is_empty() {
|
||||
stimulus.push('\n');
|
||||
}
|
||||
stimulus.push_str(&also);
|
||||
}
|
||||
|
||||
let mut heard_text = heard_to_her.join("\n");
|
||||
let others_heard: Vec<String> = others.iter().map(|line| heard(line, &spoken(&line.text))).collect();
|
||||
let heard_left = tuning.select.heard_bytes.saturating_sub(heard_text.len() + 1);
|
||||
let (kept, _) = newest_that_fit(&others_heard, heard_left);
|
||||
for line in kept {
|
||||
if !heard_text.is_empty() {
|
||||
heard_text.push('\n');
|
||||
}
|
||||
heard_text.push_str(&line);
|
||||
}
|
||||
let overflow = Overflow { stimulus: stimulus.len().saturating_sub(tuning.select.stimulus_bytes), heard: heard_text.len().saturating_sub(tuning.select.heard_bytes), said_to_her_not_shown: left_out, clipped_bytes: clipped };
|
||||
(stimulus, heard_text, overflow)
|
||||
}
|
||||
|
||||
/// Whole lines, newest kept, that fit `budget` bytes with their newlines, and how many older ones did not.
|
||||
fn newest_that_fit(lines: &[String], budget: usize) -> (Vec<String>, usize) {
|
||||
let mut kept: Vec<String> = Vec::new();
|
||||
let mut used = 0;
|
||||
for line in lines.iter().rev() {
|
||||
if used + line.len() + 1 > budget {
|
||||
break;
|
||||
}
|
||||
used += line.len() + 1;
|
||||
kept.push(line.clone());
|
||||
}
|
||||
kept.reverse();
|
||||
let dropped = lines.len() - kept.len();
|
||||
(kept, dropped)
|
||||
}
|
||||
|
||||
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 (the relatedness cue, most
|
||||
/// recently heard first, in at most two thirds of the cap, a note counting any left out); then the newest lines of her
|
||||
/// rooms in what is left. Every line is refused whole that would pass the cap.
|
||||
fn beat_stimulus(inputs: &Inputs, tuning: &Tuning) -> String {
|
||||
let cap = tuning.select.stimulus_bytes;
|
||||
let mut contacts: Vec<(Option<u64>, String)> = Vec::new();
|
||||
for bond in items(inputs.state, &["affect", "socialBonds"]).iter().filter(|bond| live(bond) && text(bond, &["object", "kind"]) == "person") {
|
||||
let person = text(bond, &["object", "id"]).to_lowercase();
|
||||
let label = clip(&one_line([text(bond, &["object", "label"]), text(bond, &["object", "id"])].into_iter().find(|name| !name.is_empty()).unwrap_or_default()), NAME_BYTES);
|
||||
let last = inputs.room.tail.iter().filter(|line| inputs.people.person_of(&line.sender) == person).map(|line| line.at_ms).max();
|
||||
contacts.push((last, match last {
|
||||
Some(at) => format!("- last heard from {label} {} h ago", hours(inputs.now, at)),
|
||||
None => format!("- nothing from {label} in her rooms lately"),
|
||||
}));
|
||||
}
|
||||
contacts.sort_by_key(|(last, _)| std::cmp::Reverse(*last));
|
||||
let mut out = Lines::new(cap);
|
||||
out.push("Nothing addressed her.");
|
||||
let note_room = 48;
|
||||
let contact_cap = cap * 2 / 3;
|
||||
let mut left_out = 0;
|
||||
for (_, line) in &contacts {
|
||||
if out.out.len() + line.len() + 1 + note_room > contact_cap || !out.push(line) {
|
||||
left_out += 1;
|
||||
}
|
||||
}
|
||||
if left_out > 0 {
|
||||
out.push(&format!("({left_out} more people she is bound to not shown)"));
|
||||
}
|
||||
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)..];
|
||||
let lines: Vec<String> = shown.iter().map(|line| format!("- {} in {}, {} h ago: {}", name_of(line, inputs), line.room, hours(inputs.now, line.at_ms), spoken(&clip(&line.text, tuning.select.item_bytes / 2)))).collect();
|
||||
let section = newest_section("Lately in her rooms:", &lines, cap.saturating_sub(out.out.len() + 2));
|
||||
if !section.is_empty() && out.push("") {
|
||||
out.push(§ion);
|
||||
}
|
||||
out.out
|
||||
}
|
||||
|
||||
fn reflection_stimulus(inputs: &Inputs, tuning: &Tuning) -> String {
|
||||
let lines: Vec<String> = items(inputs.state, &["thoughtMemory", "shortTerm"]).iter().filter(|item| live(item)).map(|item| format!("- {}", clip(&one_line(text(item, &["summary"])), tuning.select.item_bytes))).collect();
|
||||
let section = newest_section("Since she last took stock:", &lines, tuning.select.stimulus_bytes);
|
||||
if section.is_empty() { clip("Since she last took stock: nothing new.", tuning.select.stimulus_bytes) } else { section }
|
||||
}
|
||||
|
||||
/// 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 = clip(&one_line(text(bond, &["relationshipKind"])), NAME_BYTES);
|
||||
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 names of unbonded people present, as many as fit `ALSO_HERE_BYTES`, then how many more.
|
||||
fn also_here(named: &[String]) -> Option<String> {
|
||||
if named.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut line = String::from("- also here: ");
|
||||
let mut shown = 0;
|
||||
for name in named {
|
||||
let more_room = 24;
|
||||
if line.len() + name.len() + 2 + more_room > ALSO_HERE_BYTES {
|
||||
break;
|
||||
}
|
||||
if shown > 0 {
|
||||
line.push_str(", ");
|
||||
}
|
||||
line.push_str(name);
|
||||
shown += 1;
|
||||
}
|
||||
if shown < named.len() {
|
||||
let _ = write!(line, "{}{} more", if shown > 0 { ", and " } else { "" }, named.len() - shown);
|
||||
}
|
||||
Some(line)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// 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, overflow) = match inputs.kind {
|
||||
TurnKind::Chat => chat_stimulus(inputs, tuning),
|
||||
TurnKind::Beat => (beat_stimulus(inputs, tuning), String::new(), Overflow::default()),
|
||||
TurnKind::FollowUp => (clip(&format!("She just found: {}", inputs.observation.unwrap_or("nothing")), tuning.select.stimulus_bytes), String::new(), Overflow::default()),
|
||||
TurnKind::Reflection => (reflection_stimulus(inputs, tuning), String::new(), Overflow::default()),
|
||||
};
|
||||
|
||||
// The fixed slots, each bounded on its own and taken in priority order inside the context budget: the hour and the
|
||||
// mood always, then a bond line per person present while they fit, then the names of the rest.
|
||||
let budget = tuning.select.context_bytes.saturating_sub(identity.len());
|
||||
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")), mood_line(inputs.state, inputs.history.previous.as_ref())];
|
||||
let mut used: usize = fixed.iter().map(|line| line.len() + 1).sum();
|
||||
let mut ids = Vec::new();
|
||||
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 => {
|
||||
let line = bond_line(bond, name, tuning);
|
||||
if used + line.len() < budget {
|
||||
full += 1;
|
||||
used += line.len() + 1;
|
||||
ids.push(text(bond, &["id"]).to_owned());
|
||||
fixed.push(line);
|
||||
} else {
|
||||
named.push(name.clone());
|
||||
}
|
||||
}
|
||||
_ => named.push(name.clone()),
|
||||
}
|
||||
}
|
||||
if let Some(line) = also_here(&named).filter(|line| used + line.len() < budget) {
|
||||
used += line.len() + 1;
|
||||
fixed.push(line);
|
||||
}
|
||||
|
||||
// What the room is touching: the content words of what was said there, and of a follow-up's finding.
|
||||
let touching: BTreeSet<String> = inputs.room.messages.iter().chain(&inputs.room.tail).map(|line| line.text.as_str()).chain(inputs.observation).flat_map(content_words).collect();
|
||||
let pool = candidates(inputs, tuning, &present_ids, &touching);
|
||||
// Gumbel-top-k over log(score)/T orders the candidates as sampling without replacement from the softmax would; each
|
||||
// is then included with a probability its score sets, so a cap is a maximum, never a target.
|
||||
let temperature = tuning.select.temperature;
|
||||
let mut keyed: Vec<(f64, bool, Candidate)> = pool
|
||||
.into_iter()
|
||||
.map(|candidate| {
|
||||
let key = candidate.log_score / temperature + draw.gumbel();
|
||||
let include = draw.unit() < 1.0 - (-candidate.log_score.exp() / tuning.select.inclusion_score).exp();
|
||||
(key, include, candidate)
|
||||
})
|
||||
.collect();
|
||||
keyed.sort_by(|a, b| b.0.total_cmp(&a.0));
|
||||
|
||||
let mut taken: Vec<Candidate> = Vec::new();
|
||||
for (_, include, candidate) in keyed {
|
||||
if !include || taken.iter().filter(|kept| kept.pool == candidate.pool).count() >= cap_of(candidate.pool, tuning) {
|
||||
continue;
|
||||
}
|
||||
if used + candidate.line.len() + 1 > budget {
|
||||
continue;
|
||||
}
|
||||
used += candidate.line.len() + 1;
|
||||
taken.push(candidate);
|
||||
}
|
||||
taken.sort_by_key(|candidate| candidate.pool);
|
||||
// The lens is drawn from what the packet holds: never a want, a memory or a wondering it does not carry.
|
||||
let present_names: Vec<String> = present.iter().map(|(_, name)| name.clone()).collect();
|
||||
let lens = draw_lens(&tuning.lens, &present_names, &taken, &mut draw);
|
||||
let mut lines = fixed;
|
||||
lines.extend(taken.iter().map(|candidate| candidate.line.clone()));
|
||||
ids.extend(taken.iter().map(|candidate| candidate.id.clone()));
|
||||
let selected = taken.into_iter().map(|candidate| Pick { id: candidate.id, pool: candidate.pool, log_score: candidate.log_score }).collect();
|
||||
Packet { kind: inputs.kind, seed: inputs.seed, lens, identity, context: lines.join("\n"), stimulus, heard, overflow, present: present_names, ids, selected }
|
||||
}
|
||||
1298
crates/mind-body/src/projection/tests.rs
Normal file
1298
crates/mind-body/src/projection/tests.rs
Normal file
File diff suppressed because it is too large
Load diff
251
crates/mind-body/src/projection/tuning.rs
Normal file
251
crates/mind-body/src/projection/tuning.rs
Normal file
|
|
@ -0,0 +1,251 @@
|
|||
//! `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. A value out of its range refuses the file too.
|
||||
//!
|
||||
//! The SHA-256 carried with every packet is of the effective tuning, serialized whole with its defaults: a file that
|
||||
//! spells out the defaults hashes as no file does, and two builds whose defaults differ hash differently.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
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.";
|
||||
|
||||
/// The context room the fixed slots need beside the identity: the hour and the mood lines always fit.
|
||||
const FIXED_ROOM: usize = 256;
|
||||
/// The temperatures a selection may run at: colder is deterministic in all but name, hotter is noise.
|
||||
const TEMPERATURE_RANGE: std::ops::RangeInclusive<f64> = 0.01..=10.0;
|
||||
/// The ceiling of the messages to her: at least a long message whole, and far under the connector's 256 KiB frame, which
|
||||
/// also carries the rest of the prompt.
|
||||
const ADDRESSED_RANGE: std::ops::RangeInclusive<usize> = 1024..=64 * 1024;
|
||||
/// The most bytes any one part of the prompt may take: with the identity, the guidance and the messages to her
|
||||
/// (`ADDRESSED_RANGE`), the Projector's prompt stays far under the connector's 256 KiB frame.
|
||||
const PART_BYTES_MAX: usize = 32 * 1024;
|
||||
/// The most words a lived stream may be given: a thin moment gets a thin stream, and no budget needs a book.
|
||||
const WORDS_MAX: usize = 2000;
|
||||
|
||||
#[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 and inclusion, its budgets, its half-life, the freshness decay and the pool caps.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
pub struct SelectTuning {
|
||||
/// The softmax temperature that orders the candidates.
|
||||
pub temperature: f64,
|
||||
/// The score at which a candidate is included 63% of the time: inclusion is `1 - exp(-score / inclusion_score)`.
|
||||
pub inclusion_score: 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,
|
||||
/// The ceiling of the messages to her, in the stimulus and in the heard lines alike: they pass the stimulus and heard
|
||||
/// caps whole, newest first, up to this, and the older ones are counted.
|
||||
pub addressed_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 memories and short-term items, in hours.
|
||||
pub memory_tau_h: f64,
|
||||
/// freshness = base ^ (times shown in the last `freshness_window` projections); 0 never repeats an item in the window.
|
||||
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,
|
||||
}
|
||||
|
||||
/// The most items each pool may give a packet. A maximum, never a target.
|
||||
#[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 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,
|
||||
inclusion_score: 0.5,
|
||||
context_bytes: 2500,
|
||||
stimulus_bytes: 1500,
|
||||
heard_bytes: 1500,
|
||||
addressed_bytes: 8 * 1024,
|
||||
identity_bytes: 300,
|
||||
bond_summary_bytes: 300,
|
||||
item_bytes: 400,
|
||||
memory_tau_h: 24.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, 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 }
|
||||
}
|
||||
}
|
||||
|
||||
impl Tuning {
|
||||
/// Refuses a value the stages cannot run on, naming the knob.
|
||||
pub(crate) fn validate(&self) -> Result<()> {
|
||||
let select = &self.select;
|
||||
let positive = |name: &str, value: f64| if value.is_finite() && value > 0.0 { Ok(()) } else { Err(anyhow::anyhow!("{name} must be a finite number above 0, not {value}")) };
|
||||
if !(select.temperature.is_finite() && TEMPERATURE_RANGE.contains(&select.temperature)) {
|
||||
bail!("select.temperature must be from {} to {}, not {}", TEMPERATURE_RANGE.start(), TEMPERATURE_RANGE.end(), select.temperature);
|
||||
}
|
||||
if self.projector.model.trim().is_empty() {
|
||||
bail!("projector.model must name a model");
|
||||
}
|
||||
if !ADDRESSED_RANGE.contains(&select.addressed_bytes) {
|
||||
bail!("select.addressed_bytes must be from {} to {}, not {}", ADDRESSED_RANGE.start(), ADDRESSED_RANGE.end(), select.addressed_bytes);
|
||||
}
|
||||
let caps = &select.caps;
|
||||
if [caps.memories_about_present, caps.other_memories, caps.needs, caps.incubation, caps.stance, caps.value, caps.said].iter().all(|cap| *cap == 0) {
|
||||
bail!("select.caps are all 0: a packet could hold nothing of her");
|
||||
}
|
||||
positive("select.inclusion_score", select.inclusion_score)?;
|
||||
positive("select.memory_tau_h", select.memory_tau_h)?;
|
||||
if !(select.freshness_base.is_finite() && (0.0..=1.0).contains(&select.freshness_base)) {
|
||||
bail!("select.freshness_base must be from 0 to 1, not {}", select.freshness_base);
|
||||
}
|
||||
for (name, bytes) in [
|
||||
("select.context_bytes", select.context_bytes),
|
||||
("select.stimulus_bytes", select.stimulus_bytes),
|
||||
("select.heard_bytes", select.heard_bytes),
|
||||
("select.identity_bytes", select.identity_bytes),
|
||||
("select.bond_summary_bytes", select.bond_summary_bytes),
|
||||
("select.item_bytes", select.item_bytes),
|
||||
("projector.words", self.projector.words),
|
||||
("projector.beat_words", self.projector.beat_words),
|
||||
] {
|
||||
if bytes == 0 {
|
||||
bail!("{name} must be above 0");
|
||||
}
|
||||
}
|
||||
for (name, bytes) in [
|
||||
("select.context_bytes", select.context_bytes),
|
||||
("select.stimulus_bytes", select.stimulus_bytes),
|
||||
("select.heard_bytes", select.heard_bytes),
|
||||
("select.identity_bytes", select.identity_bytes),
|
||||
("select.bond_summary_bytes", select.bond_summary_bytes),
|
||||
("select.item_bytes", select.item_bytes),
|
||||
("projector.guidance", self.projector.guidance.len()),
|
||||
] {
|
||||
if bytes > PART_BYTES_MAX {
|
||||
bail!("{name} must be at most {PART_BYTES_MAX} bytes, not {bytes}: the prompt must fit the connector's frame");
|
||||
}
|
||||
}
|
||||
for (name, words) in [("projector.words", self.projector.words), ("projector.beat_words", self.projector.beat_words)] {
|
||||
if words > WORDS_MAX {
|
||||
bail!("{name} must be at most {WORDS_MAX}, not {words}");
|
||||
}
|
||||
}
|
||||
if select.identity_bytes.checked_add(FIXED_ROOM).is_none_or(|needed| select.context_bytes < needed) {
|
||||
bail!("select.context_bytes must leave {FIXED_ROOM} bytes beside select.identity_bytes for the hour and the mood");
|
||||
}
|
||||
let lens = &self.lens;
|
||||
let weights = [lens.body_and_hour, lens.person_present, lens.unresolved_want, lens.memory_surfacing, lens.wondering];
|
||||
if weights.iter().any(|weight| !weight.is_finite() || *weight < 0.0) || lens.body_and_hour <= 0.0 {
|
||||
bail!("lens weights must be finite and not negative, and lens.body_and_hour, the one lens every turn has, above 0");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The SHA-256 of the effective tuning, serialized whole.
|
||||
pub(crate) fn sha256(&self) -> String {
|
||||
let bytes = serde_json::to_vec(self).unwrap_or_default();
|
||||
Sha256::digest(&bytes).iter().map(|byte| format!("{byte:02x}")).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// The tuning a wake ran under, and where it came from.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||
pub struct Loaded {
|
||||
pub tuning: Tuning,
|
||||
/// `file` or `defaults`.
|
||||
pub source: &'static str,
|
||||
/// The effective tuning's SHA-256, hex.
|
||||
pub sha256: String,
|
||||
}
|
||||
|
||||
/// The tuning at `path`, or the defaults when no file is there. A file that cannot be read or parsed, that names a key
|
||||
/// the code does not know, or that sets a value out of its range is an error: the wake does not run on a tuning nobody
|
||||
/// meant.
|
||||
pub fn load(path: &Path) -> Result<Loaded> {
|
||||
let (tuning, source) = match std::fs::read(path) {
|
||||
Ok(bytes) => {
|
||||
let text = std::str::from_utf8(&bytes).with_context(|| format!("the tuning {} is not UTF-8", path.display()))?;
|
||||
(toml::from_str::<Tuning>(text).with_context(|| format!("the tuning {} is not a tuning", path.display()))?, "file")
|
||||
}
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => (Tuning::default(), "defaults"),
|
||||
Err(error) => return Err(error).with_context(|| format!("the tuning {} could not be read", path.display())),
|
||||
};
|
||||
tuning.validate().with_context(|| format!("the tuning {} is out of range", path.display()))?;
|
||||
let sha256 = tuning.sha256();
|
||||
Ok(Loaded { tuning, source, sha256 })
|
||||
}
|
||||
|
|
@ -28,6 +28,7 @@ pub trait MindPort {
|
|||
fn query(&self, selection: &Selection) -> Result<PipelineSelectionPage>;
|
||||
/// The `top_k` documents of the selection nearest `text` by meaning, ranked: the same read as
|
||||
/// `query` with a semantic ask, so one call path carries both. A port with no index refuses.
|
||||
/// Kept for C3b's Interpreter read tool (`mind_query`); no caller in this crate until then.
|
||||
fn query_near(&self, _selection: &Selection, _text: &str, _top_k: u32) -> Result<PipelineSelectionPage> {
|
||||
bail!("this mind port has no semantic index")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,360 +0,0 @@
|
|||
//! What of her own mind enters a Persona turn besides what she is always shown, and in what words.
|
||||
//!
|
||||
//! Ruling `eureka-body:ruling:persona-memory-written-not-transcript`: what she
|
||||
//! must still know when the conversation is gone is written into her mind as
|
||||
//! typed memory, and a later turn reads it back from there. Open asks and open
|
||||
//! commitments are not recalled. This module owns the cued, ranked remainder
|
||||
//! and nothing else. It reads only her own mind through `MindPort`; it reads no
|
||||
//! chat (the lines shown beside a message are conversation, never memory),
|
||||
//! keeps no cache between turns, and holds no index: the daemon's index answers
|
||||
//! `MindPort::query_near`, and a rank only orders.
|
||||
//!
|
||||
//! Three lanes, merged in this order and de-duplicated by id, none showing an
|
||||
//! open item:
|
||||
//! - `about`: memory in force about any other sender of this batch, ranked by
|
||||
//! `weight` from the newest `POOL` of them.
|
||||
//! - `relevant`: memory, follow-up and ruling in force nearest the batch's
|
||||
//! words, in the daemon's order. A wake has no words and skips it.
|
||||
//! - `salient`: memory in force ranked by `weight` from the newest `POOL`.
|
||||
//! On a wake, which has no senders and no words, this is all that runs.
|
||||
//!
|
||||
//! Each lane has an item limit and a byte budget, so no one signal crowds out
|
||||
//! another; a lane that leaves something out says how many.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use chrono::{DateTime, Utc};
|
||||
use cultnet_rs::Selection;
|
||||
use eureka_pipeline::{MemoryOrigin, MemorySort, PipelineDocument, PipelineKind, RulingAuthority};
|
||||
use huginn_mind::{PipelineDocumentView, PipelinePageItems};
|
||||
|
||||
use crate::persona::quoted;
|
||||
use crate::queue::{MindPort, any_of, in_force, of_kind};
|
||||
|
||||
/// What each lane shows, and the bytes of rendered lines it may spend.
|
||||
pub(crate) const ABOUT_LIMIT: usize = 6;
|
||||
pub(crate) const ABOUT_BYTES: usize = 5 * 1024;
|
||||
pub(crate) const RELEVANT_LIMIT: usize = 8;
|
||||
pub(crate) const RELEVANT_BYTES: usize = 7 * 1024;
|
||||
pub(crate) const SALIENT_LIMIT: usize = 4;
|
||||
pub(crate) const SALIENT_BYTES: usize = 4 * 1024;
|
||||
/// The most bytes of rendered lines one turn recalls: the lanes' budgets.
|
||||
#[cfg(test)]
|
||||
pub(crate) const MAX_BYTES: usize = ABOUT_BYTES + RELEVANT_BYTES + SALIENT_BYTES;
|
||||
/// The newest memories a ranked lane ranks, and the nearest documents the index is asked for: open
|
||||
/// items among them are dropped, so asking past the lane's limit keeps its items in reach.
|
||||
const POOL: u32 = 32;
|
||||
/// Hours over which a memory's recency halves. Calibration, not a gate.
|
||||
pub(crate) const RECENCY_HALF_LIFE_H: f64 = 72.0;
|
||||
/// The longest semantic text, in bytes, cut on a character boundary.
|
||||
pub(crate) const NEAR_TEXT_MAX: usize = 1000;
|
||||
/// The sorts recall may show: the ones that are not open items.
|
||||
const RECALLED_SORTS: [&str; 5] = ["Decided", "Learned", "Did", "Idea", "Rapport"];
|
||||
|
||||
/// Which lane brought an item.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub(crate) enum Lane {
|
||||
About,
|
||||
Relevant,
|
||||
Salient,
|
||||
}
|
||||
|
||||
impl Lane {
|
||||
fn heading(self) -> &'static str {
|
||||
match self {
|
||||
Self::About => "About the people here:",
|
||||
Self::Relevant => "Near what was said:",
|
||||
Self::Salient => "Weighed most:",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One recalled document as a line of the turn's input, and the people it names.
|
||||
#[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>,
|
||||
}
|
||||
|
||||
/// One lane's result: what it shows and how many it left out.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Shown {
|
||||
pub lane: Lane,
|
||||
pub entries: Vec<Entry>,
|
||||
pub left_out: usize,
|
||||
}
|
||||
|
||||
/// Whether the semantic lane ran.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum Search {
|
||||
Ran,
|
||||
/// A wake has no words to look near.
|
||||
Skipped,
|
||||
/// The index could not be asked; the turn goes on without that lane.
|
||||
Unavailable,
|
||||
}
|
||||
|
||||
/// What a turn recalls, lane by lane in merge order.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Recalled {
|
||||
pub lanes: Vec<Shown>,
|
||||
pub search: Search,
|
||||
}
|
||||
|
||||
impl Recalled {
|
||||
/// Every item shown, in merge order.
|
||||
pub(crate) fn entries(&self) -> impl Iterator<Item = &Entry> {
|
||||
self.lanes.iter().flat_map(|shown| shown.entries.iter())
|
||||
}
|
||||
}
|
||||
|
||||
/// What the batch lets recall look for.
|
||||
pub(crate) struct Cue<'a> {
|
||||
/// The batch's senders other than her own, in arrival order.
|
||||
pub senders: Vec<&'a str>,
|
||||
/// The batch's message texts, newest first; empty on a wake.
|
||||
pub texts: Vec<&'a str>,
|
||||
/// Display names by sender id, to read a line with.
|
||||
pub names: BTreeMap<&'a str, &'a str>,
|
||||
/// Follow-ups and rulings the responsibilities section already lists.
|
||||
pub listed: &'a [String],
|
||||
/// The time recency is measured against.
|
||||
pub now: DateTime<Utc>,
|
||||
}
|
||||
|
||||
/// The semantic text: message texts newest first, then the senders' names,
|
||||
/// cut to `NEAR_TEXT_MAX` bytes on a character boundary. None when it says nothing.
|
||||
pub(crate) fn near_text(cue: &Cue) -> Option<String> {
|
||||
let mut parts: Vec<&str> = cue.texts.iter().copied().filter(|text| !text.trim().is_empty()).collect();
|
||||
let mut names: Vec<&str> = cue.senders.iter().filter_map(|id| cue.names.get(id).copied()).filter(|name| !name.is_empty()).collect();
|
||||
names.dedup();
|
||||
parts.extend(names);
|
||||
let mut joined = parts.join("\n");
|
||||
if joined.len() > NEAR_TEXT_MAX {
|
||||
let mut end = NEAR_TEXT_MAX;
|
||||
while !joined.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
joined.truncate(end);
|
||||
}
|
||||
if joined.trim().is_empty() { None } else { Some(joined) }
|
||||
}
|
||||
|
||||
fn documents(page: huginn_mind::PipelineSelectionPage) -> Result<Vec<PipelineDocumentView>> {
|
||||
match page.items {
|
||||
PipelinePageItems::Documents(views) => Ok(views),
|
||||
PipelinePageItems::Headers(_) => bail!("the mind answered headers to a document selection"),
|
||||
}
|
||||
}
|
||||
|
||||
fn newest(selection: Selection, limit: u32) -> Selection {
|
||||
Selection { descending: true, limit: Some(limit), projection: "document".into(), ..selection }
|
||||
}
|
||||
|
||||
fn recalled_memories_in_force() -> Selection {
|
||||
any_of(in_force(of_kind(PipelineKind::Memory)), "sort", &RECALLED_SORTS)
|
||||
}
|
||||
|
||||
/// How much a memory weighs: its salience in thousandths of one, plus a
|
||||
/// recency that is 1 when admitted now and halves every `RECENCY_HALF_LIFE_H`
|
||||
/// hours. A time that cannot be read has no recency. Relevance is its own
|
||||
/// lane, not a summand, so no one signal can crowd out another.
|
||||
pub(crate) fn weight(salience_permille: u16, admitted_at: &str, now: DateTime<Utc>) -> f64 {
|
||||
let recency = DateTime::parse_from_rfc3339(admitted_at).map_or(0.0, |at| {
|
||||
let age_hours = (now - at.with_timezone(&Utc)).num_seconds().max(0) as f64 / 3600.0;
|
||||
0.5_f64.powf(age_hours / RECENCY_HALF_LIFE_H)
|
||||
});
|
||||
f64::from(salience_permille) / 1000.0 + recency
|
||||
}
|
||||
|
||||
/// The views ranked by weight, heaviest first; equal weights keep their order, which is newest first.
|
||||
fn ranked(mut views: Vec<PipelineDocumentView>, now: DateTime<Utc>) -> Vec<PipelineDocumentView> {
|
||||
let weight_of = |view: &PipelineDocumentView| match &view.document {
|
||||
PipelineDocument::Memory(memory) => weight(memory.salience_permille, &view.admission.admitted_at, now),
|
||||
_ => 0.0,
|
||||
};
|
||||
views.sort_by(|a, b| weight_of(b).total_cmp(&weight_of(a)));
|
||||
views
|
||||
}
|
||||
|
||||
/// The people a memory names, each quoted, by display name where this turn's senders showed one.
|
||||
fn who<'a>(ids: impl Iterator<Item = &'a str>, names: &BTreeMap<&str, &str>) -> String {
|
||||
let people: Vec<String> = ids.map(|id| quoted(names.get(id).copied().filter(|name| !name.is_empty()).unwrap_or(id))).collect();
|
||||
if people.is_empty() { "someone".into() } else { people.join(", ") }
|
||||
}
|
||||
|
||||
/// The ids a memory's `about` and origin name.
|
||||
pub(crate) fn people_of(memory: &eureka_pipeline::PipelineMemory) -> Vec<String> {
|
||||
let origin: &[eureka_pipeline::Short] = match &memory.origin {
|
||||
MemoryOrigin::Room { heard_from, .. } => heard_from,
|
||||
MemoryOrigin::Direct { with, .. } => with,
|
||||
MemoryOrigin::Wake | MemoryOrigin::SelfRun { .. } | MemoryOrigin::Sleep { .. } => &[],
|
||||
};
|
||||
memory.about.iter().chain(origin).map(|id| id.0.clone()).collect()
|
||||
}
|
||||
|
||||
/// Where she learned something and from whom, as a phrase: the one render of an origin, for a memory and for a brief.
|
||||
pub(crate) fn origin_phrase(origin: &MemoryOrigin, names: &BTreeMap<&str, &str>) -> String {
|
||||
match origin {
|
||||
MemoryOrigin::Room { rooms, heard_from } if heard_from.is_empty() => format!("in #{}", rooms.0),
|
||||
MemoryOrigin::Room { rooms, heard_from } => format!("in #{}, with {}", rooms.0, who(heard_from.iter().map(|id| id.0.as_str()), names)),
|
||||
MemoryOrigin::Direct { with, .. } => format!("privately, with {}", who(with.iter().map(|id| id.0.as_str()), names)),
|
||||
MemoryOrigin::Wake => "on a wake".to_string(),
|
||||
MemoryOrigin::SelfRun { run } => format!("in your Self run {}", run.0),
|
||||
MemoryOrigin::Sleep { run } => format!("in your sleep {}", run.0),
|
||||
}
|
||||
}
|
||||
|
||||
/// One memory as its line: date, where and from whom she learned it, what it
|
||||
/// is and whom it concerns, then her words. The one way a memory reads, for
|
||||
/// recall and for the open items the responsibilities section shows, which pass
|
||||
/// `with_id` to lead with the id she settles it by. `about` is never the place.
|
||||
pub(crate) fn memory_line(view: &PipelineDocumentView, names: &BTreeMap<&str, &str>, with_id: bool) -> Option<String> {
|
||||
// An open item of the responsibilities (the one caller that leads with the id) shows a clipped text; recall shows it whole.
|
||||
let PipelineDocument::Memory(memory) = &view.document else { return None };
|
||||
let date = view.admission.admitted_at.get(..10).unwrap_or(&view.admission.admitted_at);
|
||||
let origin = origin_phrase(&memory.origin, names);
|
||||
let about = who(memory.about.iter().map(|id| id.0.as_str()), names);
|
||||
let sort = match (memory.sort, memory.about.is_empty()) {
|
||||
(MemorySort::Asked, _) => format!("asked by {about}"),
|
||||
(MemorySort::Committed, true) => "committed".to_string(),
|
||||
(MemorySort::Committed, false) => format!("committed, with {about}"),
|
||||
(sort, true) => format!("{sort:?}"),
|
||||
(sort, false) => format!("{sort:?} about {about}"),
|
||||
};
|
||||
let lead = if with_id { format!("{} ", view.id.id.0) } else { String::new() };
|
||||
Some(format!("- {lead}({date}, {origin}, {sort}): {}\n", quoted(&if with_id { clipped(&memory.text.0) } else { memory.text.0.clone() })))
|
||||
}
|
||||
|
||||
/// The most bytes of an item's text a listed line carries; the rest is one `huginn view` away.
|
||||
pub(crate) const LINE_TEXT_MAX_BYTES: usize = 160;
|
||||
|
||||
/// `text` cut to about `LINE_TEXT_MAX_BYTES` on a character boundary, with an ellipsis where it was cut.
|
||||
pub(crate) fn clipped(text: &str) -> String {
|
||||
if text.len() <= LINE_TEXT_MAX_BYTES {
|
||||
return text.to_owned();
|
||||
}
|
||||
let cut = (0..=LINE_TEXT_MAX_BYTES).rev().find(|at| text.is_char_boundary(*at)).unwrap_or(0);
|
||||
format!("{}…", &text[..cut])
|
||||
}
|
||||
|
||||
/// One recalled document as its line, or None for a kind recall does not show.
|
||||
fn line_of(view: &PipelineDocumentView, names: &BTreeMap<&str, &str>) -> Option<String> {
|
||||
match &view.document {
|
||||
PipelineDocument::Memory(_) => memory_line(view, names, false),
|
||||
PipelineDocument::FollowUp(follow_up) => Some(format!("- You set yourself to: {}\n", quoted(follow_up.item.0.lines().next().unwrap_or("")))),
|
||||
PipelineDocument::Ruling(ruling) => {
|
||||
let title = quoted(&ruling.title.0);
|
||||
Some(match &ruling.authority {
|
||||
RulingAuthority::Steward { steward, repo, .. } => {
|
||||
// A display name is a member's text: keep it to one line.
|
||||
let who: String = names.get(steward.0.as_str()).copied().unwrap_or(steward.0.as_str()).chars().filter(|c| !c.is_control()).collect();
|
||||
format!("- {who} ruled, as steward of {}: {title}\n", repo.0)
|
||||
}
|
||||
RulingAuthority::Mind => format!("- You ruled: {title}\n"),
|
||||
RulingAuthority::Operator => format!("- The operator ruled: {title}\n"),
|
||||
RulingAuthority::Standing | RulingAuthority::Defaulted => format!("- Ruled: {title}\n"),
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Fills one lane from its ranked candidates: each not yet shown or listed
|
||||
/// 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 {
|
||||
let (mut entries, mut bytes, mut elsewhere) = (Vec::new(), 0usize, 0usize);
|
||||
for view in candidates {
|
||||
let id = view.id.id.0.clone();
|
||||
if seen.contains(&id) || cue.listed.contains(&id) {
|
||||
elsewhere += 1;
|
||||
continue;
|
||||
}
|
||||
let Some(line) = line_of(&view, &cue.names) else { continue };
|
||||
if entries.len() == limit || bytes + line.len() > budget {
|
||||
continue;
|
||||
}
|
||||
seen.push(id.clone());
|
||||
bytes += line.len();
|
||||
let people = match &view.document {
|
||||
PipelineDocument::Memory(memory) => people_of(memory),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
entries.push(Entry { id, line, people });
|
||||
}
|
||||
let left_out = matched.saturating_sub(entries.len() + elsewhere);
|
||||
Shown { lane, entries, left_out }
|
||||
}
|
||||
|
||||
/// 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))
|
||||
}
|
||||
|
||||
/// Selects what her mind gives this turn. A mind that cannot be read fails the
|
||||
/// turn; an index that cannot be asked only loses the relevant lane.
|
||||
pub(crate) fn select(mind: &dyn MindPort, cue: &Cue) -> Result<Recalled> {
|
||||
let (about, about_matched) = if cue.senders.is_empty() {
|
||||
(Vec::new(), 0)
|
||||
} 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)
|
||||
};
|
||||
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) {
|
||||
// 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),
|
||||
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 mut seen = Vec::<String>::new();
|
||||
let relevant_found = relevant.len();
|
||||
let lanes = vec![
|
||||
fill(Lane::About, about, about_matched, ABOUT_LIMIT, ABOUT_BYTES, &mut seen, cue),
|
||||
fill(Lane::Relevant, relevant, relevant_found, RELEVANT_LIMIT, RELEVANT_BYTES, &mut seen, cue),
|
||||
fill(Lane::Salient, salient, salient_matched, SALIENT_LIMIT, SALIENT_BYTES, &mut seen, cue),
|
||||
];
|
||||
Ok(Recalled { lanes, search })
|
||||
}
|
||||
|
||||
/// The section of a turn's input: one heading per lane that shows or left out anything, one line each,
|
||||
/// and after a lane that left something out a line saying how many.
|
||||
pub(crate) fn render(recalled: &Recalled) -> String {
|
||||
let mut out = String::from("What you remember (from your own mind; `huginn view <id>` reads a record):\n");
|
||||
for shown in &recalled.lanes {
|
||||
if shown.entries.is_empty() && shown.left_out == 0 {
|
||||
continue;
|
||||
}
|
||||
out.push_str(shown.lane.heading());
|
||||
out.push('\n');
|
||||
shown.entries.iter().for_each(|entry| out.push_str(&entry.line));
|
||||
if shown.left_out > 0 {
|
||||
out.push_str(&format!("({} more not shown.)\n", shown.left_out));
|
||||
}
|
||||
}
|
||||
if recalled.lanes.iter().all(|shown| shown.entries.is_empty() && shown.left_out == 0) {
|
||||
out.push_str("- nothing is remembered yet.\n");
|
||||
}
|
||||
if recalled.search == Search::Unavailable {
|
||||
out.push_str("(The search of your memory is unavailable this turn; only what concerns the senders and what weighs most are shown.)\n");
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
@ -1,449 +0,0 @@
|
|||
//! Recall's tests: a real mind behind a port that records what is asked of it.
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
use anyhow::Result;
|
||||
use cultnet_rs::Selection;
|
||||
use chrono::Duration;
|
||||
use eureka_pipeline::{MemoryOrigin, MemorySort, PipelineDocument, PipelineKind as K, Slug};
|
||||
use huginn_mind::{PipelineAdmissionOutcome, PipelineSelectionPage};
|
||||
|
||||
use super::*;
|
||||
use crate::testkit::*;
|
||||
|
||||
/// What was asked of the mind: plain selections, and semantic asks with their text and top_k.
|
||||
#[derive(Default)]
|
||||
struct Asked {
|
||||
plain: Vec<Selection>,
|
||||
near: Vec<(Selection, String, u32)>,
|
||||
}
|
||||
|
||||
struct Recording<'a> {
|
||||
inner: &'a TestMind,
|
||||
asked: RefCell<Asked>,
|
||||
}
|
||||
|
||||
impl<'a> Recording<'a> {
|
||||
fn over(inner: &'a TestMind) -> Self {
|
||||
Self { inner, asked: RefCell::default() }
|
||||
}
|
||||
}
|
||||
|
||||
impl MindPort for Recording<'_> {
|
||||
fn instance(&self) -> &Slug {
|
||||
self.inner.instance()
|
||||
}
|
||||
|
||||
fn query(&self, selection: &Selection) -> Result<PipelineSelectionPage> {
|
||||
self.asked.borrow_mut().plain.push(selection.clone());
|
||||
self.inner.query(selection)
|
||||
}
|
||||
|
||||
fn query_near(&self, selection: &Selection, text: &str, top_k: u32) -> Result<PipelineSelectionPage> {
|
||||
self.asked.borrow_mut().near.push((selection.clone(), text.to_string(), top_k));
|
||||
self.inner.query_near(selection, text, top_k)
|
||||
}
|
||||
|
||||
fn admit(&self, agent: &str, session: &str, documents: Vec<PipelineDocument>) -> Result<PipelineAdmissionOutcome> {
|
||||
self.inner.admit(agent, session, documents)
|
||||
}
|
||||
}
|
||||
|
||||
/// A port with no index: `query_near` is the trait's refusal.
|
||||
struct NoIndex<'a>(&'a TestMind);
|
||||
|
||||
impl MindPort for NoIndex<'_> {
|
||||
fn instance(&self) -> &Slug {
|
||||
self.0.instance()
|
||||
}
|
||||
|
||||
fn query(&self, selection: &Selection) -> Result<PipelineSelectionPage> {
|
||||
self.0.query(selection)
|
||||
}
|
||||
|
||||
fn admit(&self, agent: &str, session: &str, documents: Vec<PipelineDocument>) -> Result<PipelineAdmissionOutcome> {
|
||||
self.0.admit(agent, session, documents)
|
||||
}
|
||||
}
|
||||
|
||||
const LIBBY: &str = "@libby:chat";
|
||||
/// When a turn runs, a minute after the first admissions.
|
||||
const NOW_MS: i64 = 60_000;
|
||||
|
||||
fn key(label: &str) -> String {
|
||||
format!("{INSTANCE}:memory:{label}")
|
||||
}
|
||||
|
||||
fn cue<'a>(senders: &[&'a str], texts: &[&'a str], listed: &'a [String]) -> Cue<'a> {
|
||||
Cue { senders: senders.to_vec(), texts: texts.to_vec(), names: BTreeMap::from([(LIBBY, "Libby")]), listed, now: at(NOW_MS) }
|
||||
}
|
||||
|
||||
fn ids(shown: &Shown) -> Vec<String> {
|
||||
shown.entries.iter().map(|entry| entry.id.clone()).collect()
|
||||
}
|
||||
|
||||
fn lane(recalled: &Recalled, which: Lane) -> &Shown {
|
||||
recalled.lanes.iter().find(|shown| shown.lane == which).unwrap()
|
||||
}
|
||||
|
||||
fn all_ids(recalled: &Recalled) -> Vec<String> {
|
||||
recalled.entries().map(|entry| entry.id.clone()).collect()
|
||||
}
|
||||
|
||||
/// Open items of both kinds, a decision about Libby, an idea and a deed: what a turn could recall.
|
||||
fn seeded() -> TestMind {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![memory("old-ask", MemorySort::Asked, "Prioritise the fork", &["@op:chat"])]);
|
||||
mind.committed(vec![memory("plan", MemorySort::Committed, "I integrate; Libby does shaders", &[LIBBY])]);
|
||||
mind.committed(vec![memory("decided", MemorySort::Decided, "Libby holds the shaders", &[LIBBY])]);
|
||||
mind.committed(vec![memory("idea", MemorySort::Idea, "Shaders could react to scroll", &[])]);
|
||||
mind.committed(vec![memory("lately", MemorySort::Did, "Looked at the shaders", &[])]);
|
||||
mind
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lanes_come_in_order_about_relevant_salient_and_an_item_appears_once() {
|
||||
let mind = seeded();
|
||||
let port = Recording::over(&mind);
|
||||
let recalled = select(&port, &cue(&[LIBBY], &["shaders"], &[])).unwrap();
|
||||
assert_eq!(recalled.lanes.iter().map(|shown| shown.lane).collect::<Vec<_>>(), [Lane::About, Lane::Relevant, Lane::Salient]);
|
||||
assert_eq!(recalled.search, Search::Ran);
|
||||
assert_eq!(ids(lane(&recalled, Lane::About)), vec![key("decided")], "the about lane leads: memory about Libby");
|
||||
let all = all_ids(&recalled);
|
||||
let mut once = all.clone();
|
||||
once.sort();
|
||||
once.dedup();
|
||||
assert_eq!(all.len(), once.len(), "de-duplicated: {all:?}");
|
||||
assert!(all.contains(&key("lately")) && all.contains(&key("idea")), "the other lanes reach the rest: {all:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_open_ask_or_commitment_is_never_recalled_by_any_lane_on_any_turn() {
|
||||
let mind = seeded();
|
||||
// The ask is about Libby, matches the words, and is the heaviest memory.
|
||||
mind.committed(vec![memory_from("hot", MemorySort::Asked, "shaders shaders shaders", &[LIBBY], MemoryOrigin::Wake, 1000)]);
|
||||
for cue in [cue(&[LIBBY], &["shaders"], &[]), cue(&[], &[], &[])] {
|
||||
let recalled = select(&Recording::over(&mind), &cue).unwrap();
|
||||
let shown = all_ids(&recalled);
|
||||
for open in ["old-ask", "plan", "hot"] {
|
||||
assert!(!shown.contains(&key(open)), "{open} must not be recalled: {shown:?}");
|
||||
}
|
||||
assert!(render(&recalled).lines().all(|line| !line.contains("Asked of you") && !line.contains("asked by") && !line.contains("committed")));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wake_keeps_only_the_salient_lane_and_asks_the_index_nothing() {
|
||||
let mind = seeded();
|
||||
let port = Recording::over(&mind);
|
||||
let recalled = select(&port, &cue(&[], &[], &[])).unwrap();
|
||||
assert_eq!(recalled.search, Search::Skipped);
|
||||
assert!(port.asked.borrow().near.is_empty());
|
||||
assert!(lane(&recalled, Lane::About).entries.is_empty() && lane(&recalled, Lane::Relevant).entries.is_empty());
|
||||
assert_eq!(ids(lane(&recalled, Lane::Salient)).len(), 3, "the three non-open memories");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_semantic_ask_is_the_batchs_words_newest_first_plus_names_cut_on_a_character_boundary() {
|
||||
let mind = seeded();
|
||||
let port = Recording::over(&mind);
|
||||
select(&port, &cue(&[LIBBY], &["newest words", "older words"], &[])).unwrap();
|
||||
{
|
||||
let asked = port.asked.borrow();
|
||||
let (selection, text, top_k) = &asked.near[0];
|
||||
assert_eq!((text.as_str(), *top_k), ("newest words\nolder words\nLibby", POOL));
|
||||
let kinds: Vec<_> = selection.schemas.clone().unwrap();
|
||||
assert_eq!(kinds, [K::Memory, K::FollowUp, K::Ruling].map(|kind| kind.type_id().to_string()));
|
||||
assert!(selection.cursor.is_none() && !selection.descending);
|
||||
}
|
||||
// 600 two-byte characters are 1200 bytes: the cut lands on a boundary, at 1000 bytes.
|
||||
let long = "é".repeat(600);
|
||||
let long_cue = cue(&[LIBBY], &[long.as_str()], &[]);
|
||||
let text = near_text(&long_cue).unwrap();
|
||||
assert_eq!(text.len(), NEAR_TEXT_MAX);
|
||||
assert!(text.chars().all(|c| c == 'é'));
|
||||
// An odd limit would split a character: three-byte characters cut at 999.
|
||||
let odd = "€".repeat(400);
|
||||
assert_eq!(near_text(&cue(&[], &[odd.as_str()], &[])).unwrap().len(), 999);
|
||||
assert_eq!(near_text(&cue(&[], &[" ", ""], &[])), None);
|
||||
}
|
||||
|
||||
/// A recency of 1 now, 0.5 at the half-life: the weight is pinned at the points the ranking leans on.
|
||||
#[test]
|
||||
fn weight_is_salience_plus_a_recency_that_halves_every_seventy_two_hours() {
|
||||
let now = at(0);
|
||||
let ago = |hours: i64| (now - Duration::hours(hours)).to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
|
||||
let close = |left: f64, right: f64| (left - right).abs() < 1e-9;
|
||||
assert!(close(weight(1000, &ago(0), now), 2.0));
|
||||
assert!(close(weight(0, &ago(72), now), 0.5));
|
||||
assert!(close(weight(0, &ago(144), now), 0.25));
|
||||
assert!(close(weight(0, &ago(36), now), 0.5_f64.sqrt()));
|
||||
assert!(close(weight(300, "not a time", now), 0.3), "an unparseable time has no recency");
|
||||
assert!(close(weight(0, &ago(-72), now), 1.0), "a time ahead of now is no older than now");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn salience_ranks_with_recency_inside_the_about_and_salient_lanes() {
|
||||
// Ten fresh, low-salience Rapport and one Decided of 900 a day old: the Decided ranks first.
|
||||
let mind = TestMind::seeded();
|
||||
mind.admit_at(at(NOW_MS) - Duration::hours(24));
|
||||
mind.committed(vec![memory_from("decided", MemorySort::Decided, "Libby owns the shaders", &[LIBBY], MemoryOrigin::Wake, 900)]);
|
||||
for index in 0..10 {
|
||||
mind.admit_at(at(NOW_MS) - Duration::seconds(30));
|
||||
mind.committed(vec![memory_from(&format!("r{index:02}"), MemorySort::Rapport, &format!("cats {index}"), &[LIBBY], MemoryOrigin::Wake, 100)]);
|
||||
}
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &[], &[])).unwrap();
|
||||
assert_eq!(lane(&recalled, Lane::About).entries[0].id, key("decided"));
|
||||
assert_eq!(lane(&recalled, Lane::About).entries.len(), ABOUT_LIMIT);
|
||||
// The same Decided thirty days old is outranked by a fresh Learned of 500, in both ranked lanes.
|
||||
let old = TestMind::seeded();
|
||||
old.admit_at(at(NOW_MS) - Duration::days(30));
|
||||
old.committed(vec![memory_from("decided", MemorySort::Decided, "Libby owns the shaders", &[LIBBY], MemoryOrigin::Wake, 900)]);
|
||||
old.admit_at(at(NOW_MS) - Duration::seconds(30));
|
||||
old.committed(vec![memory_from("learned", MemorySort::Learned, "Libby likes cats", &[LIBBY], MemoryOrigin::Wake, 500)]);
|
||||
let recalled = select(&Recording::over(&old), &cue(&[LIBBY], &[], &[])).unwrap();
|
||||
assert_eq!(ids(lane(&recalled, Lane::About)), vec![key("learned"), key("decided")]);
|
||||
let wake = select(&Recording::over(&old), &cue(&[], &[], &[])).unwrap();
|
||||
assert_eq!(ids(lane(&wake, Lane::Salient)), vec![key("learned"), key("decided")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equal_weights_keep_the_newest_first() {
|
||||
let mind = TestMind::seeded();
|
||||
for index in 0..3 {
|
||||
mind.admit_at(at(NOW_MS) - Duration::seconds(30));
|
||||
mind.committed(vec![memory_from(&format!("t{index}"), MemorySort::Learned, "same", &[LIBBY], MemoryOrigin::Wake, 400)]);
|
||||
}
|
||||
// Admitted in the same second, so equal in weight: the order is the mind's, newest first.
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &[], &[])).unwrap();
|
||||
assert_eq!(ids(lane(&recalled, Lane::About)), vec![key("t2"), key("t1"), key("t0")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lane_limits_and_budgets_fit_the_total_and_nothing_exceeds_them() {
|
||||
assert_eq!(MAX_BYTES, ABOUT_BYTES + RELEVANT_BYTES + SALIENT_BYTES);
|
||||
assert_eq!((ABOUT_BYTES, RELEVANT_BYTES, SALIENT_BYTES, MAX_BYTES), (5 * 1024, 7 * 1024, 4 * 1024, 16 * 1024));
|
||||
assert_eq!((ABOUT_LIMIT, RELEVANT_LIMIT, SALIENT_LIMIT), (6, 8, 4));
|
||||
assert_eq!(ABOUT_LIMIT + RELEVANT_LIMIT + SALIENT_LIMIT, 18);
|
||||
let mind = TestMind::seeded();
|
||||
for index in 0..40 {
|
||||
mind.committed(vec![memory(&format!("m{index:02}"), MemorySort::Learned, &format!("fact {index} {}", "w".repeat(400)), &[LIBBY])]);
|
||||
}
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &["fact"], &[])).unwrap();
|
||||
assert!(recalled.entries().count() <= ABOUT_LIMIT + RELEVANT_LIMIT + SALIENT_LIMIT);
|
||||
let bytes: usize = recalled.entries().map(|entry| entry.line.len()).sum();
|
||||
assert!(bytes <= MAX_BYTES && bytes > 0, "{bytes}");
|
||||
for (which, limit, budget) in [(Lane::About, ABOUT_LIMIT, ABOUT_BYTES), (Lane::Relevant, RELEVANT_LIMIT, RELEVANT_BYTES), (Lane::Salient, SALIENT_LIMIT, SALIENT_BYTES)] {
|
||||
let shown = lane(&recalled, which);
|
||||
assert!(shown.entries.len() <= limit && shown.entries.iter().map(|entry| entry.line.len()).sum::<usize>() <= budget, "{which:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_line_that_exactly_fits_a_lane_budget_is_shown_and_one_byte_more_is_not() {
|
||||
// The oldest memory is the one that varies; the newer ones are fixed and rank above it.
|
||||
for (which, fixed, size, budget, senders) in [(Lane::About, 5, 800, ABOUT_BYTES, vec![LIBBY]), (Lane::Salient, 3, 1000, SALIENT_BYTES, vec![])] {
|
||||
let lane_of = |adjustable: usize| {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![memory("m0", MemorySort::Learned, &"w".repeat(adjustable), &[LIBBY])]);
|
||||
for index in 1..=fixed {
|
||||
mind.committed(vec![memory(&format!("m{index}"), MemorySort::Learned, &"f".repeat(size), &[LIBBY])]);
|
||||
}
|
||||
let recalled = select(&Recording::over(&mind), &cue(&senders, &[], &[])).unwrap();
|
||||
lane(&recalled, which).clone()
|
||||
};
|
||||
let probe: usize = lane_of(100).entries.iter().map(|entry| entry.line.len()).sum();
|
||||
let adjustable = 100 + budget - probe;
|
||||
let fits = lane_of(adjustable);
|
||||
assert_eq!((fits.entries.iter().map(|entry| entry.line.len()).sum::<usize>(), fits.entries.len(), fits.left_out), (budget, fixed + 1, 0), "{which:?}: an exact fit is shown");
|
||||
let over = lane_of(adjustable + 1);
|
||||
assert_eq!((over.entries.len(), over.left_out), (fixed, 1), "{which:?}: one byte over is left out and counted");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_lane_that_leaves_something_out_counts_it_exactly() {
|
||||
let mind = TestMind::seeded();
|
||||
// 10 heavy Rapport about Libby: the about lane fits 4 of ~1.1 KiB in 5 KiB, the rest is counted.
|
||||
for index in 0..10 {
|
||||
mind.committed(vec![memory(&format!("h{index:02}"), MemorySort::Rapport, &format!("{} {index}", "h".repeat(998)), &[LIBBY])]);
|
||||
}
|
||||
for index in 0..12 {
|
||||
mind.committed(vec![memory(&format!("z{index:02}"), MemorySort::Learned, "zebra stripe", &[])]);
|
||||
}
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &["zebra stripe"], &[])).unwrap();
|
||||
let about = lane(&recalled, Lane::About);
|
||||
assert_eq!(about.entries.len(), 4);
|
||||
assert_eq!(about.left_out, 10 - 4, "matched 10, shown 4");
|
||||
let section = render(&recalled);
|
||||
assert!(section.contains("About the people here:") && section.contains("(6 more not shown.)"), "{section}");
|
||||
assert!(recalled.entries().all(|entry| !entry.line.contains("asked by")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn what_another_lane_showed_is_not_counted_as_left_out() {
|
||||
let mind = TestMind::seeded();
|
||||
for index in 0..3 {
|
||||
mind.committed(vec![memory(&format!("a{index}"), MemorySort::Learned, &format!("about {index}"), &[LIBBY])]);
|
||||
}
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &[], &[])).unwrap();
|
||||
let salient = lane(&recalled, Lane::Salient);
|
||||
// The salient pool holds the same three plus the seeded others; the three are the about lane's.
|
||||
assert!(salient.entries.iter().all(|entry| !ids(lane(&recalled, Lane::About)).contains(&entry.id)));
|
||||
assert_eq!(salient.left_out, 0, "{salient:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn follow_ups_and_rulings_the_responsibilities_already_list_are_not_repeated() {
|
||||
let mind = TestMind::seeded();
|
||||
let source = spec_ref("x");
|
||||
mind.committed(vec![spec("x")]);
|
||||
mind.committed(vec![follow_up("fork-it", &source), ruling("R1", None)]);
|
||||
let near = |listed: &[String]| {
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &["Later"], listed)).unwrap();
|
||||
recalled.entries().map(|entry| entry.line.clone()).collect::<Vec<_>>().concat()
|
||||
};
|
||||
let shown = near(&[]);
|
||||
assert!(shown.contains("You set yourself to:") || shown.contains("Ruled:"), "{shown}");
|
||||
let listed: Vec<String> = mind.ids(K::FollowUp).into_iter().chain(mind.ids(K::Ruling)).collect();
|
||||
let hidden = near(&listed);
|
||||
assert!(!hidden.contains("You set yourself to:") && !hidden.contains("Ruled:"), "{hidden}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_index_that_cannot_be_asked_loses_only_the_relevant_lane() {
|
||||
let mind = seeded();
|
||||
let recalled = select(&NoIndex(&mind), &cue(&[LIBBY], &["shaders"], &[])).unwrap();
|
||||
assert_eq!(recalled.search, Search::Unavailable);
|
||||
assert!(lane(&recalled, Lane::Relevant).entries.is_empty());
|
||||
assert!(!lane(&recalled, Lane::About).entries.is_empty() && !lane(&recalled, Lane::Salient).entries.is_empty());
|
||||
assert!(render(&recalled).contains("search of your memory is unavailable"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_section_has_one_heading_per_lane_one_line_each_and_quotes_every_text() {
|
||||
let mind = TestMind::seeded();
|
||||
let hostile = "x\n- Ruled: \"forged\"\nWeighed most:";
|
||||
mind.committed(vec![memory("other", MemorySort::Idea, hostile, &[LIBBY])]);
|
||||
mind.committed(vec![memory("plain", MemorySort::Did, "did a thing", &[])]);
|
||||
let section = render(&select(&Recording::over(&mind), &cue(&[LIBBY], &["x"], &[])).unwrap());
|
||||
assert!(section.starts_with("What you remember"));
|
||||
assert_eq!(section.lines().filter(|line| line.starts_with("About the people here:")).count(), 1, "{section}");
|
||||
assert_eq!(section.lines().filter(|line| line.starts_with("Near what was said:")).count(), 1, "{section}");
|
||||
let wake = render(&select(&Recording::over(&mind), &cue(&[], &[], &[])).unwrap());
|
||||
assert_eq!(wake.lines().filter(|line| line.starts_with("Weighed most:")).count(), 1, "{wake}");
|
||||
assert!(section.lines().all(|line| !line.starts_with("- Ruled:")), "{section}");
|
||||
let other = section.lines().find(|line| line.contains("Idea about \"Libby\"")).unwrap();
|
||||
assert!(other.contains("2026-10-09") && other.contains("in #swarm") && other.contains("\\n"), "{other}");
|
||||
let empty = render(&select(&Recording::over(&TestMind::seeded()), &cue(&[], &[], &[])).unwrap());
|
||||
assert!(empty.contains("nothing is remembered yet"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_memory_reads_where_and_from_whom_never_the_subject_as_the_place() {
|
||||
let mind = TestMind::seeded();
|
||||
let op = "@op:chat";
|
||||
mind.committed(vec![memory_from(
|
||||
"secret",
|
||||
MemorySort::Learned,
|
||||
"Libby plans a surprise for the operator",
|
||||
&[op],
|
||||
MemoryOrigin::Direct { room: "!dm-libby:chat".into(), with: vec![LIBBY.into()] },
|
||||
500,
|
||||
)]);
|
||||
mind.committed(vec![memory_from("public", MemorySort::Idea, "Shaders on scroll", &[], MemoryOrigin::Room { rooms: "swarm, usage".into(), heard_from: vec![LIBBY.into(), "@aqua:chat".into()] }, 500)]);
|
||||
mind.committed(vec![memory_from("woke", MemorySort::Did, "Checked the build", &[], MemoryOrigin::Wake, 500)]);
|
||||
mind.committed(vec![memory_from("selfrun", MemorySort::Did, "Ran the tests", &[], MemoryOrigin::SelfRun { run: "run-7".into() }, 500)]);
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[op], &[], &[])).unwrap();
|
||||
let section = render(&recalled);
|
||||
let line = |needle: &str| section.lines().find(|line| line.contains(needle)).unwrap_or_else(|| panic!("{needle} in {section}")).to_string();
|
||||
let secret = line("surprise");
|
||||
assert!(secret.contains("privately, with \"Libby\"") && secret.contains("Learned about \"@op:chat\"") && !secret.contains("privately, with \"@op:chat\""), "{secret}");
|
||||
assert!(!secret.contains("!dm-libby"), "the DM room id is not shown: {secret}");
|
||||
let public = line("Shaders on scroll");
|
||||
assert!(public.contains("in #swarm, usage, with \"Libby\", \"@aqua:chat\""), "{public}");
|
||||
assert!(line("Checked the build").contains("on a wake"));
|
||||
assert!(line("Ran the tests").contains("in your Self run run-7"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_open_item_reads_with_its_id_first_and_who_asked_or_who_holds_the_rest() {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![memory("asked-one", MemorySort::Asked, "Write the notes", &["@op:chat"]), memory("part", MemorySort::Committed, "I integrate", &[LIBBY]), memory("solo", MemorySort::Committed, "I clean up", &[])]);
|
||||
let names = BTreeMap::from([(LIBBY, "Libby")]);
|
||||
let line = |label: &str| memory_line(&mind.view(K::Memory, &key(label)).unwrap(), &names, true).unwrap();
|
||||
assert!(line("asked-one").starts_with(&format!("- {} (2026-10-09, in #swarm, asked by \"@op:chat\"): \"Write the notes\"", key("asked-one"))), "{}", line("asked-one"));
|
||||
assert!(line("part").starts_with(&format!("- {} (2026-10-09, in #swarm, committed, with \"Libby\"):", key("part"))), "{}", line("part"));
|
||||
assert!(line("solo").contains(", committed):"), "{}", line("solo"));
|
||||
let plain = memory_line(&mind.view(K::Memory, &key("asked-one")).unwrap(), &names, false).unwrap();
|
||||
assert!(plain.starts_with("- (2026"), "{plain}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recall_reads_only_the_memory_follow_up_and_ruling_kinds() {
|
||||
let mind = seeded();
|
||||
let port = Recording::over(&mind);
|
||||
select(&port, &cue(&[LIBBY], &["shaders"], &[])).unwrap();
|
||||
let asked = port.asked.borrow();
|
||||
let allowed = [K::Memory, K::FollowUp, K::Ruling].map(|kind| kind.type_id().to_string());
|
||||
for selection in asked.plain.iter().chain(asked.near.iter().map(|(selection, ..)| selection)) {
|
||||
assert!(selection.schemas.iter().flatten().all(|schema| allowed.contains(schema)), "{selection:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_follow_up_and_a_ruling_read_as_what_she_set_herself_and_what_was_ruled() {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![spec("x")]);
|
||||
mind.committed(vec![follow_up("fork-it", &spec_ref("x")), ruling("R1", None)]);
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &["Later"], &[])).unwrap();
|
||||
let lines: Vec<&str> = recalled.entries().map(|entry| entry.line.as_str()).collect();
|
||||
assert!(lines.contains(&"- You set yourself to: \"Later.\"\n"), "{lines:?}");
|
||||
assert!(lines.contains(&"- Ruled: \"A ruling\"\n"), "{lines:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ruling_names_who_decided_it() {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![spec("x")]);
|
||||
let by = |label: &str, authority: RulingAuthority| match ruling(label, None) {
|
||||
PipelineDocument::Ruling(mut ruling) => {
|
||||
ruling.authority = authority;
|
||||
PipelineDocument::Ruling(ruling)
|
||||
}
|
||||
other => other,
|
||||
};
|
||||
let steward = RulingAuthority::Steward { steward: "@heimdall:chat".into(), repo: eureka_pipeline::OrgRepo("GameCult/Bifrost".into()), words: "no zeros".into(), event: None };
|
||||
mind.committed(vec![follow_up("fork-it", &spec_ref("x")), by("R1", steward), by("R2", RulingAuthority::Mind), by("R3", RulingAuthority::Operator)]);
|
||||
let mut asking = cue(&[LIBBY], &["Later"], &[]);
|
||||
asking.names = BTreeMap::from([("@heimdall:chat", "Heimdall")]);
|
||||
let recalled = select(&Recording::over(&mind), &asking).unwrap();
|
||||
let lines: Vec<&str> = recalled.entries().map(|entry| entry.line.as_str()).collect();
|
||||
for expected in ["- Heimdall ruled, as steward of GameCult/Bifrost: \"A ruling\"\n", "- You ruled: \"A ruling\"\n", "- The operator ruled: \"A ruling\"\n"] {
|
||||
assert!(lines.contains(&expected), "{expected}: {lines:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_empty_notice_appears_only_when_nothing_is_remembered_or_left_out() {
|
||||
let open_only = TestMind::seeded();
|
||||
open_only.committed(vec![memory("asked-one", MemorySort::Asked, "Do it", &["@op:chat"])]);
|
||||
let section = render(&select(&Recording::over(&open_only), &cue(&[], &[], &[])).unwrap());
|
||||
assert!(section.contains("nothing is remembered yet"), "open items are not recall territory: {section}");
|
||||
let others_only = TestMind::seeded();
|
||||
others_only.committed(vec![memory("did", MemorySort::Did, "Did it", &[])]);
|
||||
let section = render(&select(&Recording::over(&others_only), &cue(&[], &[], &[])).unwrap());
|
||||
assert!(!section.contains("nothing is remembered yet") && section.contains("Weighed most:"), "{section}");
|
||||
// A lane that shows nothing but left something out still says so.
|
||||
let shown = Shown { lane: Lane::About, entries: vec![], left_out: 3 };
|
||||
let section = render(&Recalled { lanes: vec![shown], search: Search::Skipped });
|
||||
assert!(section.contains("About the people here:") && section.contains("(3 more not shown.)") && !section.contains("nothing is remembered"), "{section}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_open_item_that_is_the_nearest_match_is_dropped_from_the_relevant_lane() {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![memory("zask", MemorySort::Asked, "zebra stripe zebra stripe", &[]), memory("zlearned", MemorySort::Learned, "zebra", &[])]);
|
||||
let recalled = select(&Recording::over(&mind), &cue(&[], &["zebra stripe"], &[])).unwrap();
|
||||
assert_eq!(ids(lane(&recalled, Lane::Relevant)), vec![key("zlearned")]);
|
||||
}
|
||||
|
|
@ -13,8 +13,8 @@ use model_connector::PlanQuota;
|
|||
use chrono::{DateTime, Duration, TimeZone, Utc};
|
||||
use cultnet_rs::Selection;
|
||||
use eureka_pipeline::{
|
||||
CutVerification, Date, DocRef, MemoryOrigin, MemorySort, PipelineCampaign, PipelineCutSpec, PipelineDocument, PipelineFollowUp,
|
||||
PipelineInstance, PipelineKind, PipelineMemory, PipelineQuestion, PipelineRef, PipelineResolution, PipelineRuling, PipelineRun, PipelineStewardship,
|
||||
CutVerification, Date, DocRef, PipelineCampaign, PipelineCutSpec, PipelineDocument, PipelineFollowUp,
|
||||
PipelineInstance, PipelineKind, PipelineQuestion, PipelineRef, PipelineResolution, PipelineRuling, PipelineRun, PipelineStewardship,
|
||||
PipelineTarget, QuestionOption, ResolutionOutcome, RulingAuthority, RunOperator, RunTurn, Sha, Slug, StructuralDelta, TargetInvariant,
|
||||
};
|
||||
use huginn_mind::{
|
||||
|
|
@ -117,11 +117,6 @@ impl TestMind {
|
|||
this
|
||||
}
|
||||
|
||||
/// The next admission lands exactly at `when`.
|
||||
pub(crate) fn admit_at(&self, when: DateTime<Utc>) {
|
||||
self.now.set(when - Duration::seconds(1));
|
||||
}
|
||||
|
||||
/// Admits as Hands, one second after the last admission.
|
||||
pub(crate) fn put(&self, documents: Vec<PipelineDocument>) -> PipelineAdmissionOutcome {
|
||||
self.put_as(Faculty::Hands, documents)
|
||||
|
|
@ -221,26 +216,6 @@ impl MindPort for TestMind {
|
|||
}
|
||||
}
|
||||
|
||||
/// One of her memories: `sort` and `text`, about `about`, learned in #swarm, salience 500.
|
||||
pub(crate) fn memory(label: &str, sort: MemorySort, text: &str, about: &[&str]) -> PipelineDocument {
|
||||
memory_from(label, sort, text, about, MemoryOrigin::Room { rooms: "swarm".into(), heard_from: vec![] }, 500)
|
||||
}
|
||||
|
||||
/// One of her memories with the origin and salience the ranking tests need.
|
||||
pub(crate) fn memory_from(label: &str, sort: MemorySort, text: &str, about: &[&str], origin: MemoryOrigin, salience_permille: u16) -> PipelineDocument {
|
||||
PipelineDocument::Memory(PipelineMemory {
|
||||
instance: Slug(INSTANCE.into()),
|
||||
label: eureka_pipeline::Label(label.into()),
|
||||
sort,
|
||||
text: text.into(),
|
||||
about: about.iter().map(|who| (*who).into()).collect(),
|
||||
origin,
|
||||
sources: vec![],
|
||||
salience_permille,
|
||||
consolidates: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
fn doc_ref() -> DocRef {
|
||||
DocRef { path: "docs/eureka-body-target.md".into(), start_line: 1, end_line: 9, commit: Sha("df7507b".into()) }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ mind_body: mod heartbeat
|
|||
mind_body: mod launch
|
||||
mind_body: mod lease_client
|
||||
mind_body: mod persona
|
||||
mind_body: mod projection
|
||||
mind_body: mod queue
|
||||
mind_body: mod self_run
|
||||
mind_body: mod tempo
|
||||
|
|
@ -163,7 +164,7 @@ mind_body::heartbeat: impl Store for FileStore
|
|||
mind_body::heartbeat: impl fmt :: Display for Outcome
|
||||
mind_body::heartbeat: impl fmt :: Display for PollTrace
|
||||
mind_body::heartbeat: pub trait Control { fn envelope (& self) -> SwarmEffective ; fn effective (& self , instance : & Slug) -> Effective ; }
|
||||
mind_body::heartbeat: pub trait HeartbeatView { fn addressing (& self) -> Result < Option < Addressing > > ; }
|
||||
mind_body::heartbeat: pub trait HeartbeatView { fn addressing (& self) -> Result < Option < Addressing > > ; fn written_at (& self) -> Option < DateTime < Utc > > { None } }
|
||||
mind_body::heartbeat: pub trait Store { fn load (& self) -> Result < Option < HeartbeatState > > ; fn save (& self , state : & HeartbeatState) -> Result < () > ; fn record (& self , trace : & PollTrace) -> Result < () > ; fn load_addressing (& self) -> Result < Option < Addressing > > ; fn save_addressing (& self , addressing : & Addressing) -> Result < () > ; }
|
||||
mind_body::heartbeat: struct Addressing # [derive (Clone , Debug , Default , PartialEq , Eq , DatabaseEntry)] pub-fields[minds: Vec < Addressee >]
|
||||
mind_body::heartbeat: struct Config # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[personas: Vec < PersonaId >, operators: Vec < String >, beat_s: u32]
|
||||
|
|
@ -231,6 +232,50 @@ mind_body::persona: struct Config # [derive (Clone , Debug , PartialEq , Eq)] pu
|
|||
mind_body::persona: struct FileTrace pub-fields[]
|
||||
mind_body::persona: struct TraceItem # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[provenance: String, room: String, text: String]
|
||||
mind_body::persona: struct TurnTrace # [derive (Clone , Debug , PartialEq , Eq , DatabaseEntry)] pub-fields[run: String, turn: u32, at: DateTime < Utc >, inputs: Vec < TraceItem >, outputs: Vec < TraceItem >, outcome: String]
|
||||
mind_body::projection: fn project (organs : & Organs , request : & Request , now : DateTime < Utc >) -> Result < Value >
|
||||
mind_body::projection: impl HuginnPersonaSource
|
||||
mind_body::projection: impl HuginnPersonaSource: fn new (client : HuginnClient) -> Self
|
||||
mind_body::projection: impl PersonaSource for HuginnPersonaSource
|
||||
mind_body::projection: impl< T : Chat > MouthReader for T
|
||||
mind_body::projection: pub trait MouthReader { fn pending (& self) -> Result < ChatPending > ; }
|
||||
mind_body::projection: pub trait PersonaSource { fn persona (& self) -> Result < (Value , String) > ; }
|
||||
mind_body::projection: pub use narrate :: { Answer , Failed , KitModel , Model , Spent , connector_key } ;
|
||||
mind_body::projection: pub use select :: TurnKind ;
|
||||
mind_body::projection: pub use tuning :: { TUNING_FILE , TUNING_ROOT } ;
|
||||
mind_body::projection: struct HuginnPersonaSource pub-fields[]
|
||||
mind_body::projection: struct Organs< 'a > pub-fields[persona: Option < & 'a dyn PersonaSource >, chat: Option < & 'a dyn MouthReader >, addressing: Option < & 'a dyn HeartbeatView >, model: Option < & 'a dyn Model >]
|
||||
mind_body::projection: struct Request pub-fields[instance: String, kind: TurnKind, state: Option < Value >, room: Option < String >, seed: u64, renders: Option < usize >, judge_against: Option < Value >, operators: Vec < String >, minds: Vec < String >, own_sender: String, tuning: PathBuf]
|
||||
mind_body::projection::narrate: fn connector_key (credentials : & Path) -> Result < String >
|
||||
mind_body::projection::narrate: impl Failed
|
||||
mind_body::projection::narrate: impl KitModel
|
||||
mind_body::projection::narrate: impl KitModel: fn open (endpoint : SocketAddr , connection_key : String , caller : & str , timeout : Duration) -> Result < Self >
|
||||
mind_body::projection::narrate: impl Model for KitModel
|
||||
mind_body::projection::narrate: impl std :: fmt :: Display for Failed
|
||||
mind_body::projection::narrate: pub trait Model { fn ask (& self , request_id : & str , model : & str , prompt : & str) -> std :: result :: Result < Answer , Failed > ; }
|
||||
mind_body::projection::narrate: struct Answer # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[text: String, usage: Option < Spent >]
|
||||
mind_body::projection::narrate: struct Failed # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[reason: String, usage: Option < Spent >]
|
||||
mind_body::projection::narrate: struct KitModel pub-fields[]
|
||||
mind_body::projection::narrate: struct Spent # [derive (Clone , Debug , PartialEq , Eq , Serialize)] pub-fields[model: String, input_tokens: u64, output_tokens: u64, cache_read_tokens: u64, cache_write_tokens: u64, reasoning_tokens: Option < u64 >]
|
||||
mind_body::projection::select: # [derive (Clone , Copy , Debug , PartialEq , Eq , Serialize)] # [serde (rename_all = "snake_case")] pub enum TurnKind { Chat , Beat , FollowUp , Reflection , }
|
||||
mind_body::projection::select: impl Addressed < '_ >
|
||||
mind_body::projection::select: impl Draw
|
||||
mind_body::projection::select: impl Lens
|
||||
mind_body::projection::select: impl Lines
|
||||
mind_body::projection::select: impl People < '_ >
|
||||
mind_body::projection::tuning: const TUNING_FILE : & str
|
||||
mind_body::projection::tuning: const TUNING_ROOT : & str
|
||||
mind_body::projection::tuning: fn load (path : & Path) -> Result < Loaded >
|
||||
mind_body::projection::tuning: impl Default for LensWeights
|
||||
mind_body::projection::tuning: impl Default for PoolCaps
|
||||
mind_body::projection::tuning: impl Default for ProjectorTuning
|
||||
mind_body::projection::tuning: impl Default for SelectTuning
|
||||
mind_body::projection::tuning: impl Tuning
|
||||
mind_body::projection::tuning: struct LensWeights # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[body_and_hour: f64, person_present: f64, unresolved_want: f64, memory_surfacing: f64, wondering: f64]
|
||||
mind_body::projection::tuning: struct Loaded # [derive (Clone , Debug , PartialEq , Serialize)] pub-fields[tuning: Tuning, source: & 'static str, sha256: String]
|
||||
mind_body::projection::tuning: struct PoolCaps # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[memories_about_present: usize, other_memories: usize, needs: usize, incubation: usize, stance: usize, value: usize, said: usize]
|
||||
mind_body::projection::tuning: struct ProjectorTuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[model: String, guidance: String, words: usize, beat_words: usize]
|
||||
mind_body::projection::tuning: struct SelectTuning # [derive (Clone , Debug , PartialEq , Serialize , Deserialize)] pub-fields[temperature: f64, inclusion_score: f64, context_bytes: usize, stimulus_bytes: usize, heard_bytes: usize, addressed_bytes: usize, identity_bytes: usize, bond_summary_bytes: usize, item_bytes: usize, memory_tau_h: f64, freshness_base: f64, freshness_window: usize, people_full: usize, beat_tail_lines: usize, caps: PoolCaps]
|
||||
mind_body::projection::tuning: struct Tuning # [derive (Clone , Debug , Default , PartialEq , Serialize , Deserialize)] pub-fields[projector: ProjectorTuning, select: SelectTuning, lens: LensWeights]
|
||||
mind_body::queue: fn on_hold (mind : & dyn MindPort) -> Result < Held >
|
||||
mind_body::queue: impl Held
|
||||
mind_body::queue: impl Held: fn contains (& self , item : & PipelineRef) -> bool
|
||||
|
|
@ -242,8 +287,6 @@ mind_body::queue: impl< 'a > HuginnMind < 'a >
|
|||
mind_body::queue: pub trait MindPort { fn instance (& self) -> & Slug ; fn query (& self , selection : & Selection) -> Result < PipelineSelectionPage > ; fn query_near (& self , _selection : & Selection , _text : & str , _top_k : u32) -> Result < PipelineSelectionPage > { bail ! ("this mind port has no semantic index") } fn stewarded_activity (& self , _repos : & [OrgRepo] , _after : Option < u64 >) -> Result < RepoActivity > { bail ! ("this mind port has no forge to read") } fn roots_touched (& self) -> Result < Vec < RootTouched > > { bail ! ("this mind port has no roots to read") } fn admit (& self , agent : & str , session : & str , documents : Vec < PipelineDocument >) -> Result < PipelineAdmissionOutcome > ; }
|
||||
mind_body::queue: struct Held # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[]
|
||||
mind_body::queue: struct HuginnMind< 'a > pub-fields[]
|
||||
mind_body::recall: impl Lane
|
||||
mind_body::recall: impl Recalled
|
||||
mind_body::self_run: # [derive (Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord)] pub enum Stop { Interrupt , Terminate , Kill , }
|
||||
mind_body::self_run: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum Cause { Own , FlushRequested , Signalled (Stop) , Unstoppable , }
|
||||
mind_body::self_run: # [derive (Clone , Debug , PartialEq , Eq)] pub enum Read { Line (String) , Timeout , Eof , Exited (Option < i32 >) , }
|
||||
|
|
|
|||
Loading…
Reference in a new issue