Projector: recall offers only persona-tuning.toml's recall_sorts (default Learned, Idea, Decided, Rapport), one item a packet

Recall now reads only memories of the cued sorts: follow-ups, rulings and the
open-item filter leave it, with the tests that pinned them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-19 22:24:06 +01:00
commit 6c7c375388
5 changed files with 152 additions and 180 deletions

View file

@ -19,7 +19,6 @@ use std::path::PathBuf;
use anyhow::{Result, anyhow, bail};
use chrono::{DateTime, Utc};
use eureka_pipeline::PipelineKind;
use eureka_state::HuginnClient;
use huginn_mind::HuginnMindResponse;
use serde_json::{Value, json};
@ -134,18 +133,18 @@ fn read_room(chat: &dyn Chat, request: &Request, public_name: &str) -> (Room, Va
(Room { messages, tail, here: request.room.clone() }, report)
}
/// What her Mind recalls for this room, as Select candidates: memories and rulings, never follow-ups (work is not hers).
fn read_recall(mind: &dyn MindPort, room: &Room, now: DateTime<Utc>) -> Result<(Vec<Recalled>, Value)> {
/// What her Mind recalls for this room, as Select candidates: memories of the tuning's `recall_sorts` and nothing else.
fn read_recall(mind: &dyn MindPort, room: &Room, sorts: &[String], now: DateTime<Utc>) -> Result<(Vec<Recalled>, Value)> {
let lines = room.messages.iter().chain(room.tail.iter());
let mut senders: Vec<&str> = lines.clone().map(|line| line.sender.as_str()).collect();
senders.dedup();
let names: BTreeMap<&str, &str> = lines.map(|line| (line.sender.as_str(), line.display.as_str())).filter(|(_, name)| !name.is_empty()).collect();
let texts: Vec<&str> = room.messages.iter().rev().map(|line| line.text.as_str()).collect();
let cue = recall::Cue { senders, texts, names, listed: &[], now };
let cue = recall::Cue { senders, texts, names, sorts, now };
let recalled = recall::select(mind, &cue)?;
let mut out = Vec::new();
for shown in &recalled.lanes {
for entry in shown.entries.iter().filter(|entry| matches!(entry.kind, PipelineKind::Memory | PipelineKind::Ruling)) {
for entry in &shown.entries {
let touch = match shown.lane {
recall::Lane::About => TOUCH_ABOUT,
recall::Lane::Relevant => entry.nearness_permille.map_or(TOUCH_RELEVANT_UNSCORED, |near| f64::from(near.max(1)) / 1000.0),
@ -154,14 +153,14 @@ fn read_recall(mind: &dyn MindPort, room: &Room, now: DateTime<Utc>) -> Result<(
out.push(Recalled {
id: entry.id.clone(),
text: entry.line.trim().trim_start_matches("- ").to_owned(),
salience: f64::from(entry.salience_permille.unwrap_or(500)) / 1000.0,
salience: f64::from(entry.salience_permille) / 1000.0,
admitted_at: DateTime::parse_from_rfc3339(&entry.admitted_at).ok().map(|at| at.with_timezone(&Utc)),
touch,
});
}
}
let search = format!("{:?}", recalled.search).to_lowercase();
let report = json!({ "asked": true, "items": out.len(), "search": search });
let report = json!({ "asked": true, "sorts": sorts, "items": out.len(), "search": search });
Ok((out, report))
}
@ -228,8 +227,8 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime<Utc>) -> Result
None => (Room { here: request.room.clone(), ..Room::default() }, json!({ "read": false, "why": "no mouth is configured" })),
};
let (recall, recall_report) = match organs.mind {
Some(mind) => read_recall(mind, &room, now)?,
None => (Vec::new(), json!({ "asked": false })),
Some(mind) => read_recall(mind, &room, &tuning.select.recall_sorts, now)?,
None => (Vec::new(), json!({ "asked": false, "sorts": tuning.select.recall_sorts })),
};
// Freshness and her own recent words come from trace v2, which the commit writes (C3c); until then there is none.
let history = History::default();

View file

@ -344,6 +344,76 @@ fn tuning_absent_is_the_defaults_and_a_file_overrides_and_an_unknown_key_refuses
assert!(format!("{:#}", tuning::load(&path).unwrap_err()).contains("temprature"), "a mistyped knob refuses the file");
}
/// A mind holding one memory of each sort, all about Libby and all on her words.
fn mind_of_every_sort() -> TestMind {
let mind = TestMind::seeded();
let documents = super::tuning::MEMORY_SORTS
.iter()
.map(|sort| {
let parsed: MemorySort = serde_json::from_value(json!(sort)).unwrap();
memory(&format!("sort-{}", sort.to_lowercase()), parsed, &format!("the door and Libby, {sort}"), &["@libby:gamecult.org"])
})
.collect();
mind.committed(documents);
mind
}
/// Every Mind memory id `project` offered Select on 40 seeds, with the tuning at `dir`.
fn recalled_on_every_seed(mind: &TestMind, dir: &std::path::Path) -> (BTreeSet<String>, Value) {
let stored = Stored(state());
let chat = mouth();
let organs = Organs { persona: Some(&stored), mind: Some(mind), chat: Some(&chat), model: None };
let mut shown = BTreeSet::new();
let mut last = Value::Null;
for seed in 1..=40 {
let report = project(&organs, &Request { seed, ..request(dir, None, None) }, now()).unwrap();
let recalled: Vec<String> = report["packet"]["selected"].as_array().unwrap().iter().filter(|pick| pick["pool"] == "recall").map(|pick| pick["id"].as_str().unwrap().to_owned()).collect();
assert!(recalled.len() <= 1, "the default recall cap is one item a packet: {recalled:?}");
shown.extend(recalled);
last = report;
}
(shown, last)
}
#[test]
fn recall_offers_only_the_tunings_sorts_and_the_default_keeps_work_out() {
let mind = mind_of_every_sort();
let dir = tempfile::tempdir().unwrap();
let (shown, report) = recalled_on_every_seed(&mind, dir.path());
assert_eq!(report["recall"]["sorts"], json!(["Learned", "Idea", "Decided", "Rapport"]), "the report says which sorts it used");
for work in ["committed", "did", "asked"] {
assert!(!shown.iter().any(|id| id.ends_with(&format!("sort-{work}"))), "{work} is work and never reaches her by default: {shown:?}");
}
assert!(!shown.is_empty(), "some allowed sort is recalled: {shown:?}");
std::fs::write(dir.path().join(TUNING_FILE), "[select]\nrecall_sorts = [\"Committed\", \"Did\"]\n").unwrap();
let (shown, report) = recalled_on_every_seed(&mind, dir.path());
assert_eq!(report["recall"]["sorts"], json!(["Committed", "Did"]));
assert!(shown.iter().any(|id| id.ends_with("sort-committed") || id.ends_with("sort-did")), "an experiment that names them admits them: {shown:?}");
assert!(shown.iter().all(|id| id.ends_with("sort-committed") || id.ends_with("sort-did")), "and only them: {shown:?}");
}
#[test]
fn the_recall_cap_holds_and_a_tuning_may_raise_it() {
let mind = mind_of_every_sort();
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join(TUNING_FILE), "[select.caps]\nrecall = 3\n").unwrap();
let stored = Stored(state());
let chat = mouth();
let organs = Organs { persona: Some(&stored), mind: Some(&mind), chat: Some(&chat), model: None };
let most = (1..=40)
.map(|seed| project(&organs, &Request { seed, ..request(dir.path(), None, None) }, now()).unwrap()["packet"]["selected"].as_array().unwrap().iter().filter(|pick| pick["pool"] == "recall").count())
.max()
.unwrap();
assert!((2..=3).contains(&most), "a raised cap admits more, and never past it: {most}");
}
#[test]
fn a_recall_sort_the_mind_does_not_know_refuses_the_tuning() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join(TUNING_FILE), "[select]\nrecall_sorts = [\"Learnt\"]\n").unwrap();
assert!(format!("{:#}", tuning::load(&dir.path().join(TUNING_FILE)).unwrap_err()).contains("Learnt"));
}
#[test]
fn a_lower_temperature_concentrates_the_selection() {
let room = chat_room();

View file

@ -7,7 +7,7 @@
use std::path::Path;
use anyhow::{Context, Result};
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
@ -65,9 +65,15 @@ pub struct SelectTuning {
pub people_full: usize,
/// The room lines a beat shows.
pub beat_tail_lines: usize,
/// The sorts of her Mind's memories recall may offer, as the mind spells them. Her Mind is mostly the mission's
/// record: Committed, Did and Asked are work and stay out unless an experiment names them.
pub recall_sorts: Vec<String>,
pub caps: PoolCaps,
}
/// Every memory sort the mind knows.
pub(crate) const MEMORY_SORTS: [&str; 7] = ["Asked", "Committed", "Decided", "Learned", "Did", "Idea", "Rapport"];
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PoolCaps {
@ -114,6 +120,7 @@ impl Default for SelectTuning {
freshness_window: 5,
people_full: 2,
beat_tail_lines: 6,
recall_sorts: ["Learned", "Idea", "Decided", "Rapport"].map(String::from).to_vec(),
caps: PoolCaps::default(),
}
}
@ -121,7 +128,8 @@ impl Default for SelectTuning {
impl Default for PoolCaps {
fn default() -> Self {
Self { memories_about_present: 2, other_memories: 2, needs: 1, incubation: 1, stance: 1, recall: 2, value: 1, said: 1 }
// One recalled item a packet: her own persona state is what she is made of; her Mind only seasons it.
Self { memories_about_present: 2, other_memories: 2, needs: 1, incubation: 1, stance: 1, recall: 1, value: 1, said: 1 }
}
}
@ -151,6 +159,9 @@ pub fn load(path: &Path) -> Result<Loaded> {
};
let text = std::str::from_utf8(&bytes).with_context(|| format!("the tuning {} is not UTF-8", path.display()))?;
let tuning: Tuning = toml::from_str(text).with_context(|| format!("the tuning {} is not a tuning", path.display()))?;
if let Some(unknown) = tuning.select.recall_sorts.iter().find(|sort| !MEMORY_SORTS.contains(&sort.as_str())) {
bail!("the tuning {} names {unknown:?} in recall_sorts, which is not a memory sort ({})", path.display(), MEMORY_SORTS.join(", "));
}
Ok(Loaded { tuning, source: "file", sha256: hex(&Sha256::digest(&bytes)) })
}

View file

@ -1,20 +1,20 @@
//! What of her own mind enters a Persona turn besides what she is always shown, and in what words.
//! What of her own mind the Projector's Select stage may draw on, 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
//! typed memory, and a later turn reads it back from there. Only memories of the
//! sorts the cue names are recalled (`persona-tuning.toml`'s `recall_sorts`): her
//! Mind is mostly the mission's record, and work is not hers. This module owns the
//! cued, ranked candidates 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:
//! Three lanes, merged in this order and de-duplicated by id:
//! - `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.
//! - `relevant`: memory 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.
//!
@ -26,7 +26,7 @@ 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 eureka_pipeline::{MemoryOrigin, MemorySort, PipelineDocument, PipelineKind};
use huginn_mind::{PipelineDocumentView, PipelinePageItems};
use crate::persona::quoted;
@ -49,8 +49,6 @@ const POOL: u32 = 32;
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)]
@ -68,9 +66,7 @@ pub(crate) struct Entry {
pub line: String,
/// The Matrix ids its `about` and origin name.
pub people: Vec<String>,
pub kind: PipelineKind,
/// A memory's salience; none for another kind.
pub salience_permille: Option<u16>,
pub salience_permille: u16,
pub admitted_at: String,
/// The daemon's nearness to the turn's words, in thousandths of its score; the relevant lane's alone.
pub nearness_permille: Option<u16>,
@ -117,8 +113,8 @@ pub(crate) struct Cue<'a> {
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 memory sorts that may be recalled, as the mind spells them (`Learned`, `Idea`, ...).
pub sorts: &'a [String],
/// The time recency is measured against.
pub now: DateTime<Utc>,
}
@ -152,8 +148,9 @@ 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)
fn recalled_memories_in_force(sorts: &[String]) -> Selection {
let sorts: Vec<&str> = sorts.iter().map(String::as_str).collect();
any_of(in_force(of_kind(PipelineKind::Memory)), "sort", &sorts)
}
/// How much a memory weighs: its salience in thousandths of one, plus a
@ -207,12 +204,8 @@ pub(crate) fn origin_phrase(origin: &MemoryOrigin, names: &BTreeMap<&str, &str>)
}
/// 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 };
/// is and whom it concerns, then her words, quoted on one line. `about` is never the place.
fn memory_line(view: &PipelineDocumentView, memory: &eureka_pipeline::PipelineMemory, names: &BTreeMap<&str, &str>) -> String {
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);
@ -223,67 +216,28 @@ pub(crate) fn memory_line(view: &PipelineDocumentView, names: &BTreeMap<&str, &s
(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() })))
format!("- ({date}, {origin}, {sort}): {}\n", quoted(&memory.text.0))
}
/// 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
/// Fills one lane from its ranked candidates: each not yet shown by an earlier lane, 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, Option<u16>)>, matched: usize, limit: usize, budget: usize, seen: &mut Vec<String>, cue: &Cue) -> Shown {
let (mut entries, mut bytes, mut elsewhere) = (Vec::new(), 0usize, 0usize);
for (view, nearness_permille) in candidates {
let id = view.id.id.0.clone();
if seen.contains(&id) || cue.listed.contains(&id) {
if seen.contains(&id) {
elsewhere += 1;
continue;
}
let Some(line) = line_of(&view, &cue.names) else { continue };
let PipelineDocument::Memory(memory) = &view.document else { continue };
let line = memory_line(&view, memory, &cue.names);
if entries.len() == limit || bytes + line.len() > budget {
continue;
}
seen.push(id.clone());
bytes += line.len();
let (people, salience_permille) = match &view.document {
PipelineDocument::Memory(memory) => (people_of(memory), Some(memory.salience_permille)),
_ => (Vec::new(), None),
};
entries.push(Entry { id, line, people, kind: view.id.kind, salience_permille, admitted_at: view.admission.admitted_at.clone(), nearness_permille });
entries.push(Entry { id, line, people: people_of(memory), salience_permille: memory.salience_permille, admitted_at: view.admission.admitted_at.clone(), nearness_permille });
}
let left_out = matched.saturating_sub(entries.len() + elsewhere);
Shown { lane, entries, left_out }
@ -293,39 +247,32 @@ fn unscored(views: Vec<PipelineDocumentView>) -> Vec<(PipelineDocumentView, Opti
views.into_iter().map(|view| (view, None)).collect()
}
/// Whether a view is an open ask or commitment: a memory of those sorts.
fn is_open_item(view: &PipelineDocumentView) -> bool {
matches!(&view.document, PipelineDocument::Memory(memory) if matches!(memory.sort, MemorySort::Asked | MemorySort::Committed))
}
/// 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 page = mind.query(&newest(any_of(recalled_memories_in_force(cue.sorts), "about", &cue.senders), POOL))?;
let matched = page.matched as usize;
(unscored(ranked(documents(page)?, cue.now)), matched)
};
let (relevant, search) = match near_text(cue) {
None => (Vec::new(), Search::Skipped),
Some(text) => {
let kinds = [PipelineKind::Memory, PipelineKind::FollowUp, PipelineKind::Ruling];
let members = in_force(Selection { schemas: Some(kinds.iter().map(|kind| kind.type_id().to_string()).collect()), projection: "document".into(), ..Selection::default() });
let members = Selection { projection: "document".into(), ..recalled_memories_in_force(cue.sorts) };
let scored = mind.query_near(&members, &text, POOL).and_then(|page| {
let scores = page.scores.clone().unwrap_or_default();
let nearness = scores.iter().map(|score| Some((f64::from(*score) * 1000.0).round().clamp(0.0, 1000.0) as u16)).chain(std::iter::repeat(None));
Ok(documents(page)?.into_iter().zip(nearness).collect::<Vec<_>>())
});
match scored {
// An open ask or commitment is shown by the responsibilities section; it never comes here.
Ok(found) => (found.into_iter().filter(|(view, _)| !is_open_item(view)).collect::<Vec<_>>(), Search::Ran),
Ok(found) => (found, Search::Ran),
Err(_) => (Vec::new(), Search::Unavailable),
}
}
};
let page = mind.query(&newest(recalled_memories_in_force(), POOL))?;
let page = mind.query(&newest(recalled_memories_in_force(cue.sorts), POOL))?;
let salient_matched = page.matched as usize;
let salient = unscored(ranked(documents(page)?, cue.now));

View file

@ -74,8 +74,11 @@ 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) }
/// The sorts these tests recall: every sort but the open items.
static SORTS: std::sync::LazyLock<Vec<String>> = std::sync::LazyLock::new(|| ["Decided", "Learned", "Did", "Idea", "Rapport"].map(String::from).to_vec());
fn cue<'a>(senders: &[&'a str], texts: &[&'a str]) -> Cue<'a> {
Cue { senders: senders.to_vec(), texts: texts.to_vec(), names: BTreeMap::from([(LIBBY, "Libby")]), sorts: &SORTS, now: at(NOW_MS) }
}
fn ids(shown: &Shown) -> Vec<String> {
@ -105,7 +108,7 @@ fn seeded() -> TestMind {
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();
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");
@ -122,7 +125,7 @@ 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(&[], &[], &[])] {
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"] {
@ -136,7 +139,7 @@ fn an_open_ask_or_commitment_is_never_recalled_by_any_lane_on_any_turn() {
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();
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());
@ -147,7 +150,7 @@ fn a_wake_keeps_only_the_salient_lane_and_asks_the_index_nothing() {
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();
select(&port, &cue(&[LIBBY], &["newest words", "older words"])).unwrap();
{
let asked = port.asked.borrow();
let (selection, text, top_k) = &asked.near[0];
@ -158,14 +161,14 @@ fn the_semantic_ask_is_the_batchs_words_newest_first_plus_names_cut_on_a_charact
}
// 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 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);
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.
@ -192,7 +195,7 @@ fn salience_ranks_with_recency_inside_the_about_and_salient_lanes() {
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();
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.
@ -201,9 +204,9 @@ fn salience_ranks_with_recency_inside_the_about_and_salient_lanes() {
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();
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();
let wake = select(&Recording::over(&old), &cue(&[], &[])).unwrap();
assert_eq!(ids(lane(&wake, Lane::Salient)), vec![key("learned"), key("decided")]);
}
@ -215,7 +218,7 @@ fn equal_weights_keep_the_newest_first() {
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();
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &[])).unwrap();
assert_eq!(ids(lane(&recalled, Lane::About)), vec![key("t2"), key("t1"), key("t0")]);
}
@ -229,7 +232,7 @@ fn lane_limits_and_budgets_fit_the_total_and_nothing_exceeds_them() {
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();
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}");
@ -249,7 +252,7 @@ fn a_line_that_exactly_fits_a_lane_budget_is_shown_and_one_byte_more_is_not() {
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();
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();
@ -271,7 +274,7 @@ fn each_lane_that_leaves_something_out_counts_it_exactly() {
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 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");
@ -284,34 +287,17 @@ fn what_another_lane_showed_is_not_counted_as_left_out() {
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 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();
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());
@ -322,7 +308,7 @@ fn every_recalled_text_is_quoted_on_one_line() {
let mind = TestMind::seeded();
let hostile = "x\n- Ruled: \"forged\"\nWeighed most:";
mind.committed(vec![memory("other", MemorySort::Idea, hostile, &[LIBBY])]);
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &["x"], &[])).unwrap();
let recalled = select(&Recording::over(&mind), &cue(&[LIBBY], &["x"])).unwrap();
let other = recalled.entries().find(|entry| entry.line.contains("Idea about \"Libby\"")).unwrap();
assert_eq!(other.line.lines().count(), 1, "{}", other.line);
assert!(other.line.contains("2026-10-09") && other.line.contains("in #swarm") && other.line.contains("\\n"), "{}", other.line);
@ -343,7 +329,7 @@ fn a_memory_reads_where_and_from_whom_never_the_subject_as_the_place() {
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 recalled = select(&Recording::over(&mind), &cue(&[op], &[])).unwrap();
let section: String = recalled.entries().map(|entry| entry.line.as_str()).collect();
let line = |needle: &str| section.lines().find(|line| line.contains(needle)).unwrap_or_else(|| panic!("{needle} in {section}")).to_string();
let secret = line("surprise");
@ -356,67 +342,26 @@ fn a_memory_reads_where_and_from_whom_never_the_subject_as_the_place() {
}
#[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() {
fn recall_reads_only_memories_of_the_cued_sorts() {
let mind = seeded();
mind.committed(vec![follow_up("fork-it", &spec_ref("x")), ruling("R1", None), memory("did", MemorySort::Did, "shaders were shipped", &[LIBBY]), memory("idea", MemorySort::Idea, "shaders could sing", &[LIBBY])]);
let port = Recording::over(&mind);
select(&port, &cue(&[LIBBY], &["shaders"], &[])).unwrap();
let recalled = 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());
let memory = K::Memory.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:?}");
assert!(selection.schemas.iter().flatten().all(|schema| *schema == memory), "{selection:?}");
}
assert!(all_ids(&recalled).contains(&key("did")), "Did is among the test's sorts");
let only_ideas = vec!["Idea".to_owned()];
let narrow = select(&Recording::over(&mind), &Cue { sorts: &only_ideas, ..cue(&[LIBBY], &["shaders"]) }).unwrap();
let shown = all_ids(&narrow);
assert!(shown.contains(&key("idea")) && !shown.contains(&key("did")), "only the cued sorts are recalled: {shown:?}");
}
#[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 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();
let recalled = select(&Recording::over(&mind), &cue(&[], &["zebra stripe"])).unwrap();
assert_eq!(ids(lane(&recalled, Lane::Relevant)), vec![key("zlearned")]);
}