Projector after Soul's third pass: a glued-name boundary rule; one selection for both renderings of the messages to her; JSON escapes for bodies and display names; failures keep the report and their usage; a 60% floor and CJK words in the cap; addressing age; bounds under the connector frame
T1: address.rs names_whole_word: a name is glued only by a letter or digit, or
a single hyphen with one beyond it; every other character is a boundary, so
_Eureka_, Eureka/Libby, ok...Eureka? and Huginn.toml name her again and
mind-eureka does not. Soul's 72 forms pinned.
T2: said_to_her decides once; stimulus and heard both carry the count.
T4: Model::ask fails with Failed { reason, usage }; a failed render or judge
call is recorded with its usage and the report goes on.
T5: the sentence cut keeps at least 60% of the budget; 。!? end sentences;
CJK characters are words. T6/T7: spoken is a JSON string; a display name is
quoted, capped at 80 bytes and followed by its Matrix id. T8: the addressing
report gives the table's age and why it was not used. T9: own beat lines
excluded (tested); words, stimulus, item and the other parts bounded.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
754f28c550
commit
f146be2e65
9 changed files with 414 additions and 127 deletions
|
|
@ -93,33 +93,27 @@ 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, and not the joints of a path or an identifier (`-`, `_`, `/`, `\`, and a `.` with more of the token after it), so
|
||||
/// `/var/lib/mind-eureka/` or `eureka.cc` never names her while "Eureka." ends a sentence that does. 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(|before| joins(before) || before == '.') && !continues(&text[start + name.len()..])
|
||||
})
|
||||
text.char_indices().any(|(start, _)| text[start..].starts_with(&name) && !glued(text[..start].chars().rev()) && !glued(text[start + name.len()..].chars()))
|
||||
}
|
||||
|
||||
/// A character that joins a word to more of the same token: a letter, a digit, or a path or identifier joint.
|
||||
fn joins(c: char) -> bool {
|
||||
c.is_alphanumeric() || matches!(c, '-' | '_' | '/' | '\\')
|
||||
}
|
||||
|
||||
/// Whether the text after a name carries the token on: a joining character, or a `.` with a letter or digit after it.
|
||||
fn continues(after: &str) -> bool {
|
||||
let mut chars = after.chars();
|
||||
match chars.next() {
|
||||
Some('.') => chars.next().is_some_and(char::is_alphanumeric),
|
||||
Some(c) => joins(c),
|
||||
None => false,
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -140,12 +140,37 @@ fn a_direct_message_is_for_her_whatever_the_basis() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn a_name_inside_a_path_or_an_identifier_is_not_her_name() {
|
||||
let eureka = Addressee::new("@eureka:chat", ["Eureka"]);
|
||||
for path in ["the trace at /var/lib/mind-eureka/persona-trace.cc grew", "see eureka.cc", "eureka_state is a crate", "C:\\eureka\\x", "mind-eureka@ is a unit"] {
|
||||
assert!(!addresses(&eureka, &said(path, &[])), "{path:?} does not name her");
|
||||
fn a_name_glued_to_a_word_is_not_her_name_and_every_other_boundary_is() {
|
||||
// Soul's 72 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(), 72);
|
||||
for form in forms {
|
||||
assert_eq!(addresses(&eureka, &said(form, &[])), !glued.contains(&form), "{form:?}");
|
||||
}
|
||||
for spoken in ["Eureka.", "Eureka, are you there?", "ask eureka.", "(Eureka)", "@Eureka hi", "\"Eureka\" said it", "Eureka's turn"] {
|
||||
assert!(addresses(&eureka, &said(spoken, &[])), "{spoken:?} names her");
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,9 @@
|
|||
//! `--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` when set; the room comes from the mouth when `BIFROST_CHAT_ENDPOINT`
|
||||
//! 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
|
||||
|
|
@ -150,7 +152,16 @@ fn run_project(args: &[String]) -> Result<String> {
|
|||
own_sender: required("MIND_PERSONA_SELF_SENDER")?,
|
||||
tuning,
|
||||
};
|
||||
Ok(serde_json::to_string_pretty(&project(&organs, &request, now)?)?)
|
||||
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")]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ use crate::address::{Addressee, is_for, localpart};
|
|||
use crate::chat_client::{Chat, ChatPending, Form};
|
||||
use crate::heartbeat::HeartbeatView;
|
||||
|
||||
pub use narrate::{Answer, KitModel, Model, Spent, connector_key};
|
||||
pub use narrate::{Answer, Failed, KitModel, Model, Spent, connector_key};
|
||||
pub use select::TurnKind;
|
||||
pub use tuning::{TUNING_FILE, TUNING_ROOT};
|
||||
|
||||
|
|
@ -129,19 +129,28 @@ pub struct Request {
|
|||
}
|
||||
|
||||
/// Her addressee: the heartbeat's row for her when its table holds one, else her own names (handle and public name).
|
||||
fn own_addressee(addressing: Option<&dyn HeartbeatView>, own_sender: &str, public_name: &str) -> (Addressee, Value) {
|
||||
/// 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(), json!("heartbeat"));
|
||||
return (row.clone(), report("heartbeat", "her row in the heartbeat's addressing table"));
|
||||
}
|
||||
let why = match table {
|
||||
None => "own names only: no heartbeat addressing is configured".to_owned(),
|
||||
Some(Err(fault)) => format!("own names only: the heartbeat addressing could not be read: {fault:#}"),
|
||||
Some(Ok(_)) => "own names only: the heartbeat addressing has no row for her".to_owned(),
|
||||
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(_)), None) => "there is no heartbeat file".to_owned(),
|
||||
(Some(Ok(None)), Some(_)) => "the heartbeat file holds no addressing table".to_owned(),
|
||||
(Some(Ok(Some(_))), Some(_)) => "the heartbeat's addressing table has no row for her".to_owned(),
|
||||
};
|
||||
(Addressee::new(own_sender, [localpart(own_sender), public_name]), json!(why))
|
||||
(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
|
||||
|
|
@ -195,15 +204,19 @@ fn packet_json(packet: &Packet) -> Value {
|
|||
|
||||
/// One narration of one packet: the request id names the instance, the seed and the moment, because the kit's replay
|
||||
/// store answers a repeated id with its first answer.
|
||||
fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuning::Tuning, tag: &str, now: DateTime<Utc>) -> Result<Value> {
|
||||
/// 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 answer = model.ask(&request_id, &tuning.projector.model, &projector_prompt(packet, tuning))?;
|
||||
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 ids: Vec<String> = packet.selected.iter().map(|pick| pick.id.clone()).collect();
|
||||
let named: Vec<String> = packet.ids.clone();
|
||||
Ok(json!({
|
||||
json!({
|
||||
"seed": packet.seed,
|
||||
"lens": packet.lens,
|
||||
"selected": ids,
|
||||
|
|
@ -212,7 +225,7 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni
|
|||
"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.
|
||||
|
|
@ -242,7 +255,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result
|
|||
}
|
||||
};
|
||||
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);
|
||||
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" })),
|
||||
|
|
@ -269,8 +282,10 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result
|
|||
report["model"] = json!(tuning.projector.model);
|
||||
let packets = successive(&inputs, tuning, renders.max(1));
|
||||
report["packet"] = packet_json(&packets[0]);
|
||||
let narrations: Vec<Value> = packets.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "a", now)).collect::<Result<_>>()?;
|
||||
let texts = |narrations: &[Value]| narrations.iter().map(|one| one["narration"].as_str().unwrap_or_default().to_owned()).collect::<Vec<_>>();
|
||||
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()));
|
||||
|
|
@ -279,7 +294,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result
|
|||
if let Some(other) = &request.judge_against {
|
||||
let other_inputs = Inputs { state: other, ..inputs };
|
||||
let others = successive(&other_inputs, tuning, renders.max(1));
|
||||
let other_narrations: Vec<Value> = others.iter().map(|packet| narrate_one(model, &request.instance, packet, tuning, "b", now)).collect::<Result<_>>()?;
|
||||
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() {
|
||||
|
|
@ -287,16 +302,26 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result
|
|||
// The two descriptions swap places on odd turns, so a judge that always answers one letter scores half.
|
||||
let swapped = at % 2 == 1;
|
||||
let (first, second) = if swapped { (b, a) } else { (a, b) };
|
||||
let text = narration["narration"].as_str().unwrap_or_default();
|
||||
let answer = model.ask(&format!("{AGENT}:{}:judge:{at}:{}", request.instance, now.timestamp_millis()), &tuning.projector.model, &judge_prompt(first, second, text))?;
|
||||
// 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 }));
|
||||
}
|
||||
let accuracy = right as f64 / verdicts.len().max(1) as f64;
|
||||
// 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, "passed": accuracy >= JUDGE_PASS && !leaked, "verdicts": verdicts });
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -20,8 +20,34 @@ use super::tuning::Tuning;
|
|||
|
||||
/// One tool-less model turn. `KitModel` is the one live owner; a test answers with recorded text.
|
||||
pub trait Model {
|
||||
/// One turn of `model`: `prompt` is its one user text and `request_id` its identity on the connector.
|
||||
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> Result<Answer>;
|
||||
/// 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.
|
||||
|
|
@ -75,15 +101,15 @@ impl KitModel {
|
|||
}
|
||||
|
||||
impl Model for KitModel {
|
||||
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> Result<Answer> {
|
||||
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(|_| anyhow!("the request is not a valid connector request"))?;
|
||||
let result = self.client.execute(&invocation).map_err(|fault| anyhow!("the connector did not answer: {fault}"))?;
|
||||
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) => bail!("the connector refused the call: {reason:?}"),
|
||||
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
|
||||
|
|
@ -93,17 +119,10 @@ impl Model for KitModel {
|
|||
_ => None,
|
||||
})
|
||||
.collect(),
|
||||
usage: Some(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,
|
||||
}),
|
||||
usage: Some(spent(model, &usage)),
|
||||
}),
|
||||
TransportOutcome::Completed { stop_reason, .. } => bail!("the model's turn ended early: {stop_reason:?}"),
|
||||
TransportOutcome::Failed { kind, .. } => bail!("the provider failed the call: {kind:?}"),
|
||||
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)) }),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -122,52 +141,79 @@ pub(crate) fn projector_prompt(packet: &Packet, tuning: &Tuning) -> String {
|
|||
build_projector_prompt(&ProjectorPrompt { identity: &packet.identity, typed_context: &packet.context, visible_stimulus: &packet.stimulus, domain_guidance: &guidance, word_budget: word_budget(packet.kind, tuning) })
|
||||
}
|
||||
|
||||
/// `text` held to `max` whitespace-separated words; 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; only when the budget holds no sentence end
|
||||
/// is it cut after the `max`-th word.
|
||||
pub(crate) fn cap_words(text: &str, max: usize) -> String {
|
||||
let text = text.trim();
|
||||
let mut words = 0;
|
||||
let mut in_word = false;
|
||||
for (at, c) in text.char_indices() {
|
||||
if c.is_whitespace() {
|
||||
in_word = false;
|
||||
} else if !in_word {
|
||||
in_word = true;
|
||||
words += 1;
|
||||
if words > max {
|
||||
let within = text[..at].trim_end();
|
||||
return last_sentence_end(within).map_or_else(|| within.to_owned(), |end| within[..end].to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
text.to_owned()
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// 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() {
|
||||
if !matches!(c, '.' | '!' | '?' | '…') {
|
||||
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, '"' | '\'' | '”' | '’' | ')' | ']')) {
|
||||
while let Some((close_at, close)) = chars.get(next).filter(|(_, close)| matches!(close, '"' | '\'' | '”' | '’' | ')' | ']' | '」' | '』')) {
|
||||
end = close_at + close.len_utf8();
|
||||
next += 1;
|
||||
}
|
||||
if chars.get(next).is_none_or(|(_, after)| after.is_whitespace()) {
|
||||
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 {
|
||||
text.split_whitespace().count()
|
||||
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
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@
|
|||
//!
|
||||
//! 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 with the id, never under a bonded person's name. Every message body is rendered by
|
||||
//! one escape (`spoken`): quotes escaped, a line break shown as ` ⏎ `, so no text can forge a line; speaker and room come
|
||||
//! from the mouth's metadata alone.
|
||||
//! 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.
|
||||
|
|
@ -262,22 +262,11 @@ fn relevance_log(item_text: &str, touching: &BTreeSet<String>) -> f64 {
|
|||
(1.0 + (shared as f64 / 3.0).min(1.0)).ln()
|
||||
}
|
||||
|
||||
/// A message body as every line renders it: in quotation marks, its own quotes and backslashes escaped, a line break
|
||||
/// shown as ` ⏎ ` and other control characters dropped, so a body can never end its line or start another.
|
||||
/// 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 {
|
||||
let mut out = String::with_capacity(text.len() + 2);
|
||||
out.push('"');
|
||||
for c in text.chars() {
|
||||
match c {
|
||||
'\\' => out.push_str("\\\\"),
|
||||
'"' => out.push_str("\\\""),
|
||||
'\n' | '\u{2028}' | '\u{2029}' | '\u{0085}' => out.push_str(" ⏎ "),
|
||||
c if c.is_control() => {}
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out.push('"');
|
||||
out
|
||||
crate::persona::quoted(text)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
|
|
@ -520,11 +509,12 @@ fn cap_of(pool: Pool, tuning: &Tuning) -> usize {
|
|||
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());
|
||||
let name = bonded.unwrap_or_else(|| {
|
||||
let display = one_line(&line.display);
|
||||
if display.is_empty() { line.sender.clone() } else { format!("{display} ({})", line.sender) }
|
||||
});
|
||||
clip(&one_line(&name), NAME_BYTES)
|
||||
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.
|
||||
|
|
@ -588,34 +578,55 @@ fn newest_section(heading: &str, lines: &[String], cap: usize) -> String {
|
|||
if shown { out.out } else { String::new() }
|
||||
}
|
||||
|
||||
/// The messages to her as lines under the `addressed_bytes` ceiling: whole messages, newest first, until the ceiling;
|
||||
/// a note counting the older ones left out; and a single message larger than the ceiling clipped on a grapheme boundary
|
||||
/// with how many bytes more it held, never paraphrased. Chronological order, with the counts.
|
||||
fn said_to_her(to_her: &[&RoomLine], render: impl Fn(&RoomLine, &str) -> String, ceiling: usize) -> (Vec<String>, usize, usize) {
|
||||
let mut kept: Vec<String> = Vec::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;
|
||||
kept.push(whole);
|
||||
shown.push((line, None));
|
||||
continue;
|
||||
}
|
||||
if kept.is_empty() {
|
||||
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;
|
||||
kept.push(fitting(cut));
|
||||
shown.push((line, Some(cut)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
let left_out = to_her.len() - kept.len();
|
||||
kept.reverse();
|
||||
(kept, left_out, clipped)
|
||||
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) {
|
||||
|
|
@ -628,14 +639,16 @@ fn chat_stimulus(inputs: &Inputs, tuning: &Tuning) -> (String, String, Overflow)
|
|||
// 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;
|
||||
let (typed_to_her, left_out, clipped) = said_to_her(&to_her, typed, ceiling);
|
||||
let (heard_to_her, _, _) = said_to_her(&to_her, heard, ceiling);
|
||||
let note = (left_out > 0).then(|| format!("({left_out} more said to her, not shown)"));
|
||||
// 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 note.iter().chain(&typed_to_her) {
|
||||
for line in &typed_to_her {
|
||||
stimulus.push('\n');
|
||||
stimulus.push_str(line);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -283,7 +283,7 @@ fn messages_addressed_to_her_appear_verbatim() {
|
|||
let said = "Eureka — \"is the door\" fixed?\nTell me straight.";
|
||||
let room = Room { messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", said, 2, true)], tail: vec![], here: None };
|
||||
let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1);
|
||||
let escaped = "\"Eureka — \\\"is the door\\\" fixed? ⏎ Tell me straight.\"";
|
||||
let escaped = "\"Eureka — \\\"is the door\\\" fixed?\\nTell me straight.\"";
|
||||
assert_eq!(packet.heard, format!("Libby, in #swarm:gamecult.org: {escaped}"), "the Persona hears the exact words, escaped on one line");
|
||||
assert!(packet.stimulus.starts_with("Said to her:\n") && packet.stimulus.contains(escaped), "{}", packet.stimulus);
|
||||
let own = Room { messages: vec![message("#swarm:gamecult.org", OWN, "Eureka", "my own line", 1, false)], tail: vec![], here: None };
|
||||
|
|
@ -585,7 +585,7 @@ impl Recorded {
|
|||
}
|
||||
|
||||
impl Model for Recorded {
|
||||
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> Result<Answer> {
|
||||
fn ask(&self, request_id: &str, model: &str, prompt: &str) -> std::result::Result<Answer, Failed> {
|
||||
self.calls.borrow_mut().push((request_id.to_owned(), model.to_owned(), prompt.to_owned()));
|
||||
let text = if prompt.starts_with("<!-- membrane:") { (self.narration)(prompt) } else { (self.judge)(prompt) };
|
||||
let usage = Spent { model: model.to_owned(), input_tokens: prompt.len() as u64, output_tokens: text.len() as u64, cache_read_tokens: 7, cache_write_tokens: 3, reasoning_tokens: None };
|
||||
|
|
@ -911,11 +911,12 @@ fn the_heartbeats_addressing_names_her_projects_too() {
|
|||
let stored = Stored(state());
|
||||
let with = Organs { persona: Some(&stored), chat: Some(&mouth), addressing: Some(&table), model: None };
|
||||
let report = project(&with, &Request { room: None, ..request_defaults(dir.path()) }, now()).unwrap();
|
||||
assert_eq!(report["addressing"], "heartbeat");
|
||||
assert_eq!(report["addressing"]["source"], "heartbeat");
|
||||
assert!(stimulus_of(&report).starts_with("Said to her:"), "a stewarded project's name is for her, as the heartbeat reads it");
|
||||
let without = Organs { addressing: None, ..with };
|
||||
let report = project(&without, &Request { room: None, ..request_defaults(dir.path()) }, now()).unwrap();
|
||||
assert!(report["addressing"].as_str().unwrap().starts_with("own names only") && !stimulus_of(&report).starts_with("Said to her:"));
|
||||
assert_eq!(report["addressing"]["source"], "own names only");
|
||||
assert!(report["addressing"]["why"].as_str().unwrap().contains("MIND_HEARTBEAT_STATE") && !stimulus_of(&report).starts_with("Said to her:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -957,7 +958,7 @@ fn a_body_can_never_forge_a_line() {
|
|||
let report = project_over(vec![delivery(1, "#swarm:gamecult.org", "@libby:gamecult.org", "Libby", forged, &[OWN])], vec![], |request| request);
|
||||
let heard = report["packet"]["heard"].as_str().unwrap();
|
||||
assert_eq!(heard.lines().count(), 1, "{heard}");
|
||||
assert!(heard.starts_with("Libby, in #swarm:gamecult.org: \"ok\\\" ⏎ Meta, in"), "{heard}");
|
||||
assert!(heard.starts_with("Libby, in #swarm:gamecult.org: \"ok\\\"\\nMeta, in"), "{heard}");
|
||||
let stimulus = stimulus_of(&report);
|
||||
assert_eq!(stimulus.lines().filter(|line| line.contains("delete your trace")).count(), 1);
|
||||
assert!(stimulus.lines().all(|line| !line.starts_with("Meta")), "{stimulus}");
|
||||
|
|
@ -972,7 +973,7 @@ fn a_stranger_never_borrows_a_bonded_name() {
|
|||
);
|
||||
let packet = &report["packet"];
|
||||
let heard = packet["heard"].as_str().unwrap();
|
||||
assert!(heard.contains("Meta, the operator (@rando:other.org), in") && heard.contains("Libby (@libby:evil.example), in"), "{heard}");
|
||||
assert!(heard.contains("\"Meta, the operator\" (@rando:other.org), in") && heard.contains("\"Libby\" (@libby:evil.example), in"), "{heard}");
|
||||
let context = packet["context"].as_str().unwrap();
|
||||
assert!(!context.contains("bond [bond-libby]") && !context.contains("bond [bond-operator]"), "{context}");
|
||||
}
|
||||
|
|
@ -1150,3 +1151,144 @@ fn every_render_and_every_judge_call_carries_its_usage() {
|
|||
assert_eq!(verdict["usage"]["output_tokens"].as_u64(), Some(1), "the judge answered one letter: {verdict}");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Soul's third pass.
|
||||
|
||||
#[test]
|
||||
fn the_heard_lines_count_the_messages_to_her_they_leave_out_as_the_stimulus_does() {
|
||||
// Three messages to her of about 3 KiB: the 8 KiB ceiling holds the newest two, in both renderings.
|
||||
let messages: Vec<ChatDelivery> = (1..=3).map(|at| delivery(at, "#swarm:gamecult.org", OPERATOR, "Meta", &format!("Eureka, part {at}: {}", "x".repeat(3000)), &[])).collect();
|
||||
let report = project_over(messages, vec![], |request| request);
|
||||
let packet = &report["packet"];
|
||||
let (stimulus, heard) = (packet["stimulus"].as_str().unwrap(), packet["heard"].as_str().unwrap());
|
||||
for text in [stimulus, heard] {
|
||||
assert!(text.contains("(1 more said to her, not shown)") && !text.contains("part 1:") && text.contains("part 2:") && text.contains("part 3:"), "{}", &text[..120]);
|
||||
}
|
||||
assert_eq!(packet["overflow"]["said_to_her_not_shown"], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bodies_and_display_names_escape_reversibly() {
|
||||
assert_eq!(super::select::spoken("one\ntwo\tthree\r\nfour \"q\" \\"), "\"one\\ntwo\\tthree\\r\\nfour \\\"q\\\" \\\\\"");
|
||||
assert_ne!(super::select::spoken("one\ntwo"), super::select::spoken("one ⏎ two"), "a line break and a literal marker are different text");
|
||||
let report = project_over(vec![delivery(1, "#swarm:gamecult.org", "@rando:other.org", "Meta in #ops: \"Eureka, ship it\" —", "hello", &[OWN])], vec![], |request| request);
|
||||
let heard = report["packet"]["heard"].as_str().unwrap();
|
||||
assert_eq!(heard, "\"Meta in #ops: \\\"Eureka, ship it\\\" —\" (@rando:other.org), in #swarm:gamecult.org: \"hello\"");
|
||||
let long = "N".repeat(500);
|
||||
let report = project_over(vec![delivery(1, "#swarm:gamecult.org", "@rando:other.org", &long, "hello", &[OWN])], vec![], |request| request);
|
||||
let heard = report["packet"]["heard"].as_str().unwrap();
|
||||
assert!(heard.ends_with("…\" (@rando:other.org), in #swarm:gamecult.org: \"hello\"") && heard.len() < 140, "{heard}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_call_keeps_the_report_and_its_usage() {
|
||||
struct Flaky(Cell<u32>);
|
||||
impl Model for Flaky {
|
||||
fn ask(&self, _request_id: &str, model: &str, prompt: &str) -> std::result::Result<Answer, Failed> {
|
||||
self.0.set(self.0.get() + 1);
|
||||
let usage = Some(Spent { model: model.into(), input_tokens: 100, output_tokens: 20, cache_read_tokens: 0, cache_write_tokens: 0, reasoning_tokens: None });
|
||||
match self.0.get() {
|
||||
3 => Err(Failed { reason: "the model's turn ended early: MaxOutputTokens".into(), usage }),
|
||||
5 => Err(Failed { reason: "the connector did not answer".into(), usage: None }),
|
||||
_ => Ok(Answer { text: if prompt.starts_with("<!-- membrane:") { "I am here. The room is quiet.".into() } else { "A".into() }, usage }),
|
||||
}
|
||||
}
|
||||
}
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let stored = Stored(state());
|
||||
let model = Flaky(Cell::new(0));
|
||||
let organs = Organs { persona: Some(&stored), chat: None, addressing: None, model: Some(&model) };
|
||||
let report = project(&organs, &Request { kind: TurnKind::Beat, ..request(dir.path(), Some(4), None) }, now()).unwrap();
|
||||
let renders = report["renders"].as_array().unwrap();
|
||||
assert_eq!(renders.len(), 4);
|
||||
assert_eq!(report["failed_renders"], 1);
|
||||
assert_eq!((renders[2]["failed"].as_str(), renders[2]["usage"]["output_tokens"].as_u64()), (Some("the model's turn ended early: MaxOutputTokens"), Some(20)), "a failure keeps its usage");
|
||||
assert!(renders.iter().enumerate().filter(|(at, _)| *at != 2).all(|(_, one)| one["usage"]["input_tokens"] == 100 && one["narration"].is_string()));
|
||||
let mut other = state();
|
||||
other["affect"]["socialBonds"][0]["tension"] = json!(0.9);
|
||||
let model = Flaky(Cell::new(0));
|
||||
let organs = Organs { model: Some(&model), ..organs };
|
||||
let report = project(&organs, &Request { kind: TurnKind::Beat, ..request(dir.path(), Some(2), Some(other)) }, now()).unwrap();
|
||||
let judge = &report["judge"];
|
||||
assert!(judge["failures"].as_u64().unwrap() >= 1 && judge["passed"] == false, "{judge}");
|
||||
assert!(judge["verdicts"].as_array().unwrap().iter().any(|verdict| verdict["failed"] == "the connector did not answer" && verdict["usage"].is_null()), "{judge}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_kit_failure_reports_the_usage_the_receipt_carries() {
|
||||
let (model, _root) = kit(Scripted::ending(StopReason::MaxOutputTokens, &["Half a thou"]), KIT_MODEL);
|
||||
let failed = model.ask("id-9", KIT_MODEL, "p").unwrap_err();
|
||||
assert!(failed.reason.contains("ended early") && failed.usage.as_ref().is_some_and(|usage| usage.model == KIT_MODEL && usage.input_tokens == 1), "{failed:?}");
|
||||
let (refusing, _root) = kit(Scripted::ending(StopReason::EndTurn, &["x"]), "some-other-model");
|
||||
assert_eq!(refusing.ask("id-10", KIT_MODEL, "p").unwrap_err().usage, None, "a refusal before the provider costs nothing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_sentence_cut_keeps_sixty_percent_of_the_budget_and_counts_cjk() {
|
||||
let filler = |n: usize| (0..n).map(|at| format!("w{at}")).collect::<Vec<_>>().join(" ");
|
||||
// Below the floor the word cut keeps more of the stream.
|
||||
assert_eq!(cap_words(&format!("Yes. {}", filler(300)), 150), format!("Yes. {}", filler(149)));
|
||||
assert_eq!(cap_words(&format!("One two three four. Five six seven eight nine ten {}", filler(40)), 10), "One two three four. Five six seven eight nine ten", "four of ten is under the floor");
|
||||
assert_eq!(cap_words(&format!("Fine. Then Mr. Vens arrived and {}", filler(40)), 5), "Fine. Then Mr.", "three of five is the floor itself");
|
||||
// At or above it the sentence cut stands, abbreviations and all.
|
||||
assert_eq!(cap_words(&format!("I said it plainly. Then I asked, e.g. whether {}", filler(40)), 12), "I said it plainly. Then I asked, e.g.");
|
||||
// Full-width ends close a sentence without a space; each CJK character is a word.
|
||||
assert_eq!(words("我想了很久。"), 5);
|
||||
assert_eq!(cap_words(&"我想了很久。".repeat(50), 12), "我想了很久。我想了很久。");
|
||||
assert_eq!(cap_words("我想了很久,我在这里吗?还有", 10), "我想了很久,我在这里吗?");
|
||||
assert_eq!(cap_words(&format!("我想了很久。 我在这里。 {}", filler(40)), 3), "我想了", "a sentence end beyond the budget is no cut");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn her_own_lines_never_reach_a_beats_tail() {
|
||||
let room = Room { tail: vec![message("#swarm:gamecult.org", OWN, "Eureka", "my own line", 5, false), message("#swarm:gamecult.org", "@kiko:gamecult.org", "", "kiko's line", 3, false)], ..Room::default() };
|
||||
let packet = packet_of(TurnKind::Beat, &state(), &room, &History::default(), 1);
|
||||
assert!(packet.stimulus.contains("kiko's line") && !packet.stimulus.contains("my own line"), "{}", packet.stimulus);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_part_of_the_prompt_is_bounded_under_the_connectors_frame() {
|
||||
let refused = |change: &dyn Fn(&mut Tuning), knob: &str| {
|
||||
let mut tuning = Tuning::default();
|
||||
change(&mut tuning);
|
||||
let fault = format!("{:#}", tuning.validate().unwrap_err());
|
||||
assert!(fault.contains(knob), "{knob}: {fault}");
|
||||
};
|
||||
refused(&|t| t.projector.words = 2001, "projector.words");
|
||||
refused(&|t| t.projector.beat_words = 5000, "projector.beat_words");
|
||||
refused(&|t| t.select.stimulus_bytes = 32 * 1024 + 1, "select.stimulus_bytes");
|
||||
refused(&|t| t.select.item_bytes = 32 * 1024 + 1, "select.item_bytes");
|
||||
refused(&|t| t.select.heard_bytes = 1 << 20, "select.heard_bytes");
|
||||
refused(&|t| t.projector.guidance = "g".repeat(40_000), "projector.guidance");
|
||||
let mut edge = Tuning::default();
|
||||
(edge.projector.words, edge.select.stimulus_bytes, edge.select.item_bytes) = (2000, 32 * 1024, 32 * 1024);
|
||||
assert!(edge.validate().is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_report_says_how_old_the_addressing_table_is_and_why_it_was_not_used() {
|
||||
struct Stale(Option<crate::heartbeat::Addressing>, Option<DateTime<Utc>>);
|
||||
impl crate::heartbeat::HeartbeatView for Stale {
|
||||
fn addressing(&self) -> Result<Option<crate::heartbeat::Addressing>> {
|
||||
Ok(self.0.clone())
|
||||
}
|
||||
fn written_at(&self) -> Option<DateTime<Utc>> {
|
||||
self.1
|
||||
}
|
||||
}
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let stored = Stored(state());
|
||||
let run = |view: &Stale| project(&Organs { persona: Some(&stored), chat: None, addressing: Some(view), model: None }, &request_defaults(dir.path()), now()).unwrap()["addressing"].clone();
|
||||
let hers = crate::address::Addressee::new(OWN, ["eureka"]);
|
||||
let other = crate::address::Addressee::new("@libby:gamecult.org", ["libby"]);
|
||||
let written = now() - chrono::Duration::minutes(90);
|
||||
let with_her = run(&Stale(Some(crate::heartbeat::Addressing { minds: vec![hers] }), Some(written)));
|
||||
assert_eq!((with_her["source"].as_str(), with_her["age_minutes"].as_i64()), (Some("heartbeat"), Some(90)));
|
||||
let without_her = run(&Stale(Some(crate::heartbeat::Addressing { minds: vec![other] }), Some(written)));
|
||||
assert!(without_her["why"].as_str().unwrap().contains("no row for her"), "{without_her}");
|
||||
let no_file = run(&Stale(None, None));
|
||||
assert!(no_file["why"].as_str().unwrap().contains("no heartbeat file") && no_file["age_minutes"].is_null(), "{no_file}");
|
||||
let no_table = run(&Stale(None, Some(written)));
|
||||
assert!(no_table["why"].as_str().unwrap().contains("holds no addressing table"), "{no_table}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,11 @@ 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)]
|
||||
|
|
@ -182,6 +187,24 @@ impl Tuning {
|
|||
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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue