Cut the mission organ and every Persona-side Mind write (C2)
Map: docs/persona-only-heartbeat-map.md (imagination/persona-only-heartbeat, d861574), cut C2. Rulings: eureka-body:ruling:persona-only-heartbeat (she reads the Mind and never writes it; no Self runs from the Persona). Persona organ (persona.rs 3983 -> ~420 lines): deleted the live session organ whole: TURN_INSTRUCTION, TURN_REMINDER, MESSAGES_HEADER, WORK_QUESTION, ANSWER_MESSAGES, WAKE_ASK, WORK_TAIL; PersonaReply with intent, remember, rule, settle, start_self_run, tempo vote and urgency, and reply_schema; the doctrine and persona-JSON system prompt (read_doctrine, render_instructions, campaigns); start_session, persona_args, PersonaInbox; act, decode_reply and every admission (memories, rulings, settles, pulses, follow-ups, run closure); start_self_run; the responsibilities, recent-runs and wear renderers; render_input and render_tempo. carry reads the brakes and answers Dead or NoOrgan; a start writes nothing and records no run. Kept per the map's keep list for C3: Config (doctrine, image store and fetch fields cut with their consumers), the brake read, the chat-addressing helpers, the tail renderer and FileTrace; uncalled ones carry allow(dead_code) naming C3. Night: sleep.rs is deleted. It was a Mind-writing session built on the deleted prelude; C4 retargets its contract from history. attachment.rs is deleted with the reply's save_attachment, its one caller. chat_image's loader and recall.rs stay for C3 under allow(dead_code). Heartbeat: no Mind writes. A wake is a plain unit start through the Launcher (brake, then alive, then start); busy is the unit's own state. Deleted the Self-run branch (claimable briefs), the self-ended wake, tempo votes and declared urgency (standing_vote, pending_urgency, last_self_end) and the run-based last_turn and last_sleep reads; HeartbeatState gains last_turn_at and last_night_at (default slots, so a state written before this cut still decodes). The beat is HEARTBEAT_BEAT_S, default 1800 s. InstanceTrace.vote_beat_s stays as an always-none slot so heartbeat.cc's older trace records still decode. HeartbeatView loses latest; the timeline reads FileStore::latest. tempo.rs: tally, fallback_beat and the PID controller and its sim are deleted; pick and score stay, without self_permille. Tests that pinned deleted behaviour are deleted with it; surviving ones are kept, and two heartbeat tests pin the cut: a Mind with claimable briefs gets a Persona wake and no Self run, and no poll admits anything to any mind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
609696f43e
commit
0fd12325bd
21 changed files with 698 additions and 17473 deletions
|
|
@ -1,13 +1,11 @@
|
|||
//! Credential redaction: the only code that knows the shape of a secret in text.
|
||||
//!
|
||||
//! `redact_url_userinfo` redacts URL credentials (the Self run's journal and reply,
|
||||
//! the Persona's structured output). `scrub` is chat ingest's door: every message body
|
||||
//! `redact_url_userinfo` redacts URL credentials (the Self run's journal and reply). `scrub` is chat ingest's door: every message body
|
||||
//! passes it before it is chunked, embedded or stored, and it also runs the URL rule.
|
||||
//! Shapes only; nothing here reads meaning, and a hit is a kind, never a value.
|
||||
|
||||
/// The one redaction of URL credentials. Call it at each place a string the CLI produced enters the journal, her mind or chat: today
|
||||
/// the Self result (`result_text`: journal line and Reply) and the structured output of a Persona or Sleep result
|
||||
/// (`persona::structured_output`, string by string). It does not cover claude's stderr (inherited) or the CLI's transcript files.
|
||||
/// the Self result (`result_text`: journal line and Reply). It does not cover claude's stderr (inherited) or the CLI's transcript files.
|
||||
///
|
||||
/// A forge token rides in a remote URL, so every `scheme://` and every protocol-relative `//` opens an authority, read as git, curl and
|
||||
/// RFC 3986 read it: it ends at the first `/`, `?`, `#`, backslash, quote, backtick, parenthesis, bracket, angle bracket or ASCII whitespace
|
||||
|
|
@ -466,7 +464,7 @@ mod tests {
|
|||
"123e4567-e89b-12d3-a456-426614174000", "!abc:s", "$Ab3dEf-9_zQ", "user@example.com", "ssh://git@host/r.git",
|
||||
"https://github.com/@user", "pkg@1.2.3", "the bearer of bad news", "Authorization is a header",
|
||||
"padding like dGVzdA== in prose", "```\nlet x = 1;\n```", "the BEGIN PRIVATE KEY header", "a token of thanks",
|
||||
"AKIAshort", "ghp_short", "eyJ is a prefix", "naïve café — 日本語 with Bearer in it",
|
||||
"AKIAshort", "ghp_short", "eyJ is a prefix", "naïve café — 日本語 with Bearer in it",
|
||||
] {
|
||||
let out = scrub(same);
|
||||
assert_eq!(out.text, same);
|
||||
|
|
@ -476,9 +474,9 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn multibyte_text_around_a_secret_never_panics_the_scrub() {
|
||||
let body = format!("日本 \u{a0}é Bearer {} → ünï", "A1b2".repeat(6));
|
||||
assert_eq!(scrub(&body).text, "日本 \u{a0}é <redacted> → ünï");
|
||||
let seed = "é日 sk-aaaaaaaaaaaaaaaaaaaa -----BEGIN PRIVATE KEY----- authorization: é eyJa.b.c https://u:p@h/x";
|
||||
let body = format!("日本 \u{a0}é Bearer {} → ünï", "A1b2".repeat(6));
|
||||
assert_eq!(scrub(&body).text, "日本 \u{a0}é <redacted> → ünï");
|
||||
let seed = "é日 sk-aaaaaaaaaaaaaaaaaaaa -----BEGIN PRIVATE KEY----- authorization: é eyJa.b.c https://u:p@h/x";
|
||||
let chars: Vec<char> = seed.chars().collect();
|
||||
for start in 0..chars.len() {
|
||||
for end in start..=chars.len() {
|
||||
|
|
|
|||
|
|
@ -39,11 +39,6 @@ impl Addressee {
|
|||
self
|
||||
}
|
||||
|
||||
/// The repos she stewards, as the heartbeat last read them.
|
||||
pub(crate) fn repos(&self) -> &[OrgRepo] {
|
||||
&self.repos
|
||||
}
|
||||
|
||||
pub(crate) fn matrix_id(&self) -> &str {
|
||||
&self.matrix_id
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,154 +0,0 @@
|
|||
//! The attachment saver: a verified chat image becomes one file in the Persona's sandbox.
|
||||
//!
|
||||
//! `save` re-verifies what the mouth stored through `chat_image::load_bytes` (the one verifier; nothing here sniffs, hashes or
|
||||
//! sizes a file itself) and writes `attachments/<sha256>.<ext>` below the sandbox root. The name derives from the verified hash
|
||||
//! and one of four extensions; no component comes from the event, the reply, the mouth or the stored path.
|
||||
//!
|
||||
//! Containment is by directory descriptor, not by a path check. The sandbox root is opened once by its configured path (the
|
||||
//! operator's, not hers); every open below it is relative to a held descriptor and refuses a symlink, so a shell that swaps
|
||||
//! `attachments/` for a link between two calls finds the next call refusing it. The bytes go to a temporary name in that directory
|
||||
//! and are linked to the final name, which fails where a rename would replace her file; a crash leaves only the temporary.
|
||||
//!
|
||||
//! The file_received receipt means only that the mouth fetched and stored the file for her. It does not prove the file reached her sandbox. The receipt stays inside the mouth's fetch, so a failed save (Exists, WriteFailed, RepoTree, a Load skip) leaves the receipt standing; she is told the save failed in her own turn.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::chat_client::ChatImage;
|
||||
use crate::chat_image::{self, Skip};
|
||||
|
||||
/// Why a verified image was not saved. `Load` carries the verifier's own refusal, told by its own fixed reason.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum SaveFault {
|
||||
/// A git work tree at the sandbox root or in `attachments/`: a Matrix file does not go into one.
|
||||
RepoTree,
|
||||
/// The sandbox root or `attachments/` is a link or not a directory.
|
||||
OutsideSandbox,
|
||||
/// A different file already holds the name.
|
||||
Exists,
|
||||
WriteFailed,
|
||||
Load(Skip),
|
||||
}
|
||||
|
||||
/// The extension of a verified media type: one of four, never from outside.
|
||||
fn extension(media_type: &str) -> Option<&'static str> {
|
||||
match media_type {
|
||||
"image/png" => Some("png"),
|
||||
"image/jpeg" => Some("jpg"),
|
||||
"image/gif" => Some("gif"),
|
||||
"image/webp" => Some("webp"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Saves `image`, which the mouth stored for `room_id` under `store`, into `sandbox`. The path is relative to the sandbox:
|
||||
/// `attachments/<sha256>.<ext>`. An identical file already there counts as saved.
|
||||
pub(crate) fn save(store: &Path, sandbox: &Path, room_id: &str, image: &ChatImage) -> Result<PathBuf, SaveFault> {
|
||||
let (bytes, media_type) = chat_image::load_bytes(store, room_id, image).map_err(SaveFault::Load)?;
|
||||
let extension = extension(media_type).ok_or(SaveFault::Load(Skip::NotAnImage))?;
|
||||
let name = format!("{}.{extension}", image.sha256);
|
||||
write(sandbox, &name, &bytes)?;
|
||||
Ok(Path::new("attachments").join(name))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn write(_sandbox: &Path, _name: &str, _bytes: &[u8]) -> Result<(), SaveFault> {
|
||||
// The organ runs under bubblewrap on Linux; there is no descriptor-relative write here.
|
||||
Err(SaveFault::WriteFailed)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn write(sandbox: &Path, name: &str, bytes: &[u8]) -> Result<(), SaveFault> {
|
||||
use rustix::fs::{self, AtFlags, Mode, OFlags};
|
||||
use rustix::io::Errno;
|
||||
|
||||
let dir_flags = OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC;
|
||||
let refused = |errno: Errno| if matches!(errno, Errno::LOOP | Errno::NOTDIR) { SaveFault::OutsideSandbox } else { SaveFault::WriteFailed };
|
||||
let work_tree = |dir: &rustix::fd::OwnedFd| match fs::statat(dir, ".git", AtFlags::SYMLINK_NOFOLLOW) {
|
||||
Ok(_) => Err(SaveFault::RepoTree),
|
||||
Err(Errno::NOENT) => Ok(()),
|
||||
Err(_) => Err(SaveFault::WriteFailed),
|
||||
};
|
||||
|
||||
let root = fs::open(sandbox, dir_flags, Mode::empty()).map_err(refused)?;
|
||||
work_tree(&root)?;
|
||||
match fs::mkdirat(&root, "attachments", Mode::RWXU) {
|
||||
Ok(()) | Err(Errno::EXIST) => {}
|
||||
Err(_) => return Err(SaveFault::WriteFailed),
|
||||
}
|
||||
let dir = fs::openat(&root, "attachments", dir_flags, Mode::empty()).map_err(refused)?;
|
||||
work_tree(&dir)?;
|
||||
|
||||
let temp = format!(".{name}.part");
|
||||
// A crashed save's leavings, even a FIFO or a link, are removed without being followed.
|
||||
match fs::unlinkat(&dir, temp.as_str(), AtFlags::empty()) {
|
||||
Ok(()) | Err(Errno::NOENT) => {}
|
||||
Err(_) => return Err(SaveFault::WriteFailed),
|
||||
}
|
||||
#[cfg(test)]
|
||||
tests::between_unlink_and_create();
|
||||
let file = fs::openat(&dir, temp.as_str(), OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC, Mode::RUSR | Mode::WUSR)
|
||||
.map_err(|_| SaveFault::WriteFailed)?;
|
||||
let landed = (|| {
|
||||
let mut rest = bytes;
|
||||
while !rest.is_empty() {
|
||||
#[cfg(test)]
|
||||
if tests::crash_after(bytes.len() - rest.len()) {
|
||||
return Err(Errno::IO);
|
||||
}
|
||||
let chunk = &rest[..rest.len().min(8192)];
|
||||
match rustix::io::write(&file, chunk) {
|
||||
Ok(0) => return Err(Errno::IO),
|
||||
Ok(n) => rest = &rest[n..],
|
||||
Err(Errno::INTR) => {}
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
fs::fsync(&file)?;
|
||||
fs::linkat(&dir, temp.as_str(), &dir, name, AtFlags::empty())
|
||||
})();
|
||||
match landed {
|
||||
Ok(()) => {
|
||||
let _ = fs::unlinkat(&dir, temp.as_str(), AtFlags::empty());
|
||||
Ok(())
|
||||
}
|
||||
Err(Errno::EXIST) => {
|
||||
let _ = fs::unlinkat(&dir, temp.as_str(), AtFlags::empty());
|
||||
same_file(&dir, name, bytes)
|
||||
}
|
||||
// A simulated crash leaves the temporary as a real one would.
|
||||
#[cfg(test)]
|
||||
Err(Errno::IO) if tests::crashed() => Err(SaveFault::WriteFailed),
|
||||
Err(_) => {
|
||||
let _ = fs::unlinkat(&dir, temp.as_str(), AtFlags::empty());
|
||||
Err(SaveFault::WriteFailed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the file at `name` holds exactly `bytes`. It is a name her shell controls, so it is opened without following a link
|
||||
/// and without waiting (a FIFO opens), must be a regular file of exactly the image's length, and is read for at most that length
|
||||
/// plus one byte. Anything else is `Exists` with nothing read.
|
||||
#[cfg(unix)]
|
||||
fn same_file(dir: &rustix::fd::OwnedFd, name: &str, bytes: &[u8]) -> Result<(), SaveFault> {
|
||||
use rustix::fs::{self, FileType, Mode, OFlags};
|
||||
|
||||
let file = fs::openat(dir, name, OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC, Mode::empty()).map_err(|_| SaveFault::Exists)?;
|
||||
let stat = fs::fstat(&file).map_err(|_| SaveFault::Exists)?;
|
||||
if FileType::from_raw_mode(stat.st_mode) != FileType::RegularFile || u64::try_from(stat.st_size) != Ok(bytes.len() as u64) {
|
||||
return Err(SaveFault::Exists);
|
||||
}
|
||||
let mut held = vec![0u8; bytes.len() + 1];
|
||||
let mut filled = 0;
|
||||
while filled < held.len() {
|
||||
match rustix::io::read(&file, &mut held[filled..]) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => filled += n,
|
||||
Err(rustix::io::Errno::INTR) => {}
|
||||
Err(_) => return Err(SaveFault::Exists),
|
||||
}
|
||||
}
|
||||
if held[..filled] == *bytes { Ok(()) } else { Err(SaveFault::Exists) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
@ -1,361 +0,0 @@
|
|||
//! The saver against a real store and a real sandbox on disk: each guard against the one input it exists for.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::os::unix::fs::{PermissionsExt, symlink};
|
||||
use std::time::Duration;
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use super::*;
|
||||
use crate::chat_image::{hex, room_dir};
|
||||
|
||||
const ROOM: &str = "!room:gamecult.test";
|
||||
const PNG: &[u8] = b"\x89PNG\r\n\x1a\nnot a real picture, only its magic bytes and a tail";
|
||||
|
||||
thread_local! {
|
||||
static CRASH_AT: Cell<Option<usize>> = const { Cell::new(None) };
|
||||
static CRASHED: Cell<bool> = const { Cell::new(false) };
|
||||
}
|
||||
|
||||
/// The write loop's seam: true once `written` bytes have gone out and a crash is armed on this thread.
|
||||
pub(super) fn crash_after(written: usize) -> bool {
|
||||
let fire = CRASH_AT.get().is_some_and(|at| written >= at);
|
||||
if fire {
|
||||
CRASHED.set(true);
|
||||
}
|
||||
fire
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static GAP: std::cell::RefCell<Option<Box<dyn Fn()>>> = const { std::cell::RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// The seam between the unlink of the temporary's name and its create: where her shell could plant something at that name.
|
||||
pub(super) fn between_unlink_and_create() {
|
||||
GAP.with(|gap| {
|
||||
if let Some(plant) = gap.borrow().as_ref() {
|
||||
plant();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn crashed() -> bool {
|
||||
CRASHED.get()
|
||||
}
|
||||
|
||||
struct World {
|
||||
store: tempfile::TempDir,
|
||||
sandbox: tempfile::TempDir,
|
||||
}
|
||||
|
||||
impl World {
|
||||
fn new() -> Self {
|
||||
Self { store: tempfile::tempdir().unwrap(), sandbox: tempfile::tempdir().unwrap() }
|
||||
}
|
||||
|
||||
/// Stores `bytes` the way the mouth does under `room`, named by `file_ext`, and describes them honestly.
|
||||
fn put(&self, room: &str, bytes: &[u8], file_ext: &str) -> ChatImage {
|
||||
let sha256 = hex(&Sha256::digest(bytes));
|
||||
let room = room_dir(room);
|
||||
std::fs::create_dir_all(self.store.path().join(&room)).unwrap();
|
||||
let name = if file_ext.is_empty() { sha256.clone() } else { format!("{sha256}.{file_ext}") };
|
||||
std::fs::write(self.store.path().join(&room).join(&name), bytes).unwrap();
|
||||
let mime = match bytes {
|
||||
[0xff, 0xd8, ..] => "image/jpeg",
|
||||
[b'G', b'I', b'F', ..] => "image/gif",
|
||||
[b'R', b'I', b'F', b'F', ..] => "image/webp",
|
||||
_ => "image/png",
|
||||
};
|
||||
ChatImage { path: format!("{room}/{name}"), mime: mime.into(), size: bytes.len() as u64, sha256 }
|
||||
}
|
||||
|
||||
fn save(&self, image: &ChatImage) -> Result<PathBuf, SaveFault> {
|
||||
save(self.store.path(), self.sandbox.path(), ROOM, image)
|
||||
}
|
||||
|
||||
fn attachments(&self) -> PathBuf {
|
||||
self.sandbox.path().join("attachments")
|
||||
}
|
||||
|
||||
fn files(&self) -> Vec<String> {
|
||||
let mut names: Vec<String> = std::fs::read_dir(self.attachments()).map(|dir| dir.map(|e| e.unwrap().file_name().into_string().unwrap()).collect()).unwrap_or_default();
|
||||
names.sort();
|
||||
names
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_saved_name_is_the_hash_and_one_of_four_extensions() {
|
||||
let world = World::new();
|
||||
for (bytes, ext) in [(PNG, "png"), (&b"\xff\xd8\xff\xe0 jpeg tail"[..], "jpg"), (&b"GIF89a gif tail"[..], "gif"), (&b"RIFF\x10\0\0\0WEBPVP8 tail"[..], "webp")] {
|
||||
// The stored file's own extension is the mouth's and is not used.
|
||||
let image = world.put(ROOM, bytes, "exe");
|
||||
let path = world.save(&image).unwrap();
|
||||
assert_eq!(path, Path::new("attachments").join(format!("{}.{ext}", image.sha256)));
|
||||
assert_eq!(std::fs::read(world.sandbox.path().join(&path)).unwrap(), bytes);
|
||||
}
|
||||
assert_eq!(world.files().len(), 4, "{:?}", world.files());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_receipt_path_that_leaves_the_store_or_names_no_stored_file_is_refused_and_writes_nothing() {
|
||||
let world = World::new();
|
||||
let good = world.put(ROOM, PNG, "png");
|
||||
for path in [format!("../{}", good.path), format!("/{}", good.path), good.path.replace('/', "/../"), format!("{}/x", good.path), String::from("x")] {
|
||||
let image = ChatImage { path, ..good.clone() };
|
||||
assert!(matches!(world.save(&image), Err(SaveFault::Load(Skip::BadReference | Skip::OutsideStore))), "{image:?}");
|
||||
}
|
||||
assert!(!world.attachments().exists(), "a refused reference leaves not even the directory");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_saver_reverifies_what_the_mouth_stored() {
|
||||
let world = World::new();
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
let stored = world.store.path().join(&image.path);
|
||||
// Wrong bytes under the right name.
|
||||
std::fs::write(&stored, b"\x89PNG\r\n\x1a\nsome other picture, same magic bytes, same-ish size.....").unwrap();
|
||||
assert!(matches!(world.save(&image), Err(SaveFault::Load(Skip::SizeMismatch | Skip::HashMismatch))));
|
||||
// Not an image at all, honestly hashed.
|
||||
let html = world.put(ROOM, b"<html>not an image</html>", "png");
|
||||
assert_eq!(world.save(&html), Err(SaveFault::Load(Skip::NotAnImage)));
|
||||
// A declared type that is not the sniffed one.
|
||||
let image = ChatImage { mime: "image/gif".into(), ..world.put(ROOM, PNG, "png") };
|
||||
assert_eq!(world.save(&image), Err(SaveFault::Load(Skip::MimeMismatch)));
|
||||
// Evicted by the mouth.
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
std::fs::remove_file(world.store.path().join(&image.path)).unwrap();
|
||||
assert_eq!(world.save(&image), Err(SaveFault::Load(Skip::Missing)));
|
||||
assert!(world.files().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_receipt_path_under_another_rooms_hash_is_not_copied() {
|
||||
let world = World::new();
|
||||
let image = world.put("!other:gamecult.test", PNG, "png");
|
||||
// The room id the mouth resolved is ROOM, but the path sits under another room's hash.
|
||||
assert_eq!(world.save(&image), Err(SaveFault::Load(Skip::OutsideStore)));
|
||||
// The alias is not a room id: loading with it reaches no store.
|
||||
let own = world.put(ROOM, PNG, "png");
|
||||
assert_eq!(save(world.store.path(), world.sandbox.path(), "#room:gamecult.test", &own), Err(SaveFault::Load(Skip::OutsideStore)));
|
||||
assert!(world.files().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_skip_the_verifier_returns_is_a_save_fault_load() {
|
||||
let world = World::new();
|
||||
let big = world.put(ROOM, PNG, "png");
|
||||
assert_eq!(world.save(&ChatImage { size: 6 * 1024 * 1024, ..big.clone() }), Err(SaveFault::Load(Skip::TooLarge)));
|
||||
assert_eq!(world.save(&ChatImage { size: 3, ..big }), Err(SaveFault::Load(Skip::SizeMismatch)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_saver_refuses_a_git_work_tree() {
|
||||
let at_root = World::new();
|
||||
std::fs::create_dir(at_root.sandbox.path().join(".git")).unwrap();
|
||||
assert_eq!(at_root.save(&at_root.put(ROOM, PNG, "png")), Err(SaveFault::RepoTree));
|
||||
assert!(!at_root.attachments().exists());
|
||||
|
||||
let inside = World::new();
|
||||
std::fs::create_dir_all(inside.attachments().join(".git")).unwrap();
|
||||
assert_eq!(inside.save(&inside.put(ROOM, PNG, "png")), Err(SaveFault::RepoTree));
|
||||
assert_eq!(inside.files(), [".git"]);
|
||||
|
||||
// A .git file (a worktree or submodule) is a work tree too, and a link named .git counts without being followed.
|
||||
let linked = World::new();
|
||||
symlink("/nonexistent", linked.sandbox.path().join(".git")).unwrap();
|
||||
assert_eq!(linked.save(&linked.put(ROOM, PNG, "png")), Err(SaveFault::RepoTree));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_symlinked_attachments_directory_or_sandbox_is_outside() {
|
||||
let world = World::new();
|
||||
let outside = tempfile::tempdir().unwrap();
|
||||
symlink(outside.path(), world.attachments()).unwrap();
|
||||
assert_eq!(world.save(&world.put(ROOM, PNG, "png")), Err(SaveFault::OutsideSandbox));
|
||||
assert_eq!(std::fs::read_dir(outside.path()).unwrap().count(), 0);
|
||||
|
||||
let file = World::new();
|
||||
std::fs::write(file.attachments(), b"a file where the directory goes").unwrap();
|
||||
assert_eq!(file.save(&file.put(ROOM, PNG, "png")), Err(SaveFault::OutsideSandbox));
|
||||
|
||||
let linked_root = World::new();
|
||||
let link = linked_root.sandbox.path().join("link");
|
||||
symlink(outside.path(), &link).unwrap();
|
||||
let image = linked_root.put(ROOM, PNG, "png");
|
||||
assert_eq!(save(linked_root.store.path(), &link, ROOM, &image), Err(SaveFault::OutsideSandbox));
|
||||
assert_eq!(std::fs::read_dir(outside.path()).unwrap().count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_saver_cannot_be_walked_out_by_a_swapping_sandbox() {
|
||||
let world = World::new();
|
||||
let outside = tempfile::tempdir().unwrap();
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
let attachments = world.attachments();
|
||||
let parked = world.sandbox.path().join("parked");
|
||||
let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let swapper = {
|
||||
let (stop, outside) = (stop.clone(), outside.path().to_path_buf());
|
||||
let (attachments, parked) = (attachments.clone(), parked.clone());
|
||||
std::thread::spawn(move || {
|
||||
while !stop.load(std::sync::atomic::Ordering::SeqCst) {
|
||||
// Between a real directory and a link to the outside, as her shell could.
|
||||
let _ = std::fs::rename(&attachments, &parked);
|
||||
let _ = symlink(&outside, &attachments);
|
||||
let _ = std::fs::remove_file(&attachments);
|
||||
let _ = std::fs::rename(&parked, &attachments);
|
||||
}
|
||||
})
|
||||
};
|
||||
for _ in 0..200 {
|
||||
match world.save(&image) {
|
||||
Ok(_) | Err(SaveFault::OutsideSandbox | SaveFault::WriteFailed | SaveFault::Exists) => {}
|
||||
Err(other) => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
stop.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
swapper.join().unwrap();
|
||||
assert_eq!(std::fs::read_dir(outside.path()).unwrap().count(), 0, "nothing reached the outside directory");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_identical_existing_file_counts_as_saved_and_leaves_no_part() {
|
||||
let world = World::new();
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
let path = world.save(&image).unwrap();
|
||||
assert_eq!(world.save(&image), Ok(path.clone()), "a replayed request is not an error");
|
||||
assert_eq!(world.files(), [path.file_name().unwrap().to_str().unwrap()]);
|
||||
|
||||
// Same length, other bytes under the name: Exists, and the file is left as it was.
|
||||
let held = world.sandbox.path().join(&path);
|
||||
let mut other = PNG.to_vec();
|
||||
*other.last_mut().unwrap() ^= 1;
|
||||
std::fs::write(&held, &other).unwrap();
|
||||
assert_eq!(world.save(&image), Err(SaveFault::Exists));
|
||||
assert_eq!(std::fs::read(&held).unwrap(), other);
|
||||
// Another length.
|
||||
std::fs::write(&held, b"short").unwrap();
|
||||
assert_eq!(world.save(&image), Err(SaveFault::Exists));
|
||||
assert_eq!(world.files().len(), 1, "no .part left behind");
|
||||
}
|
||||
|
||||
/// Runs `save` on a thread and fails the test if it does not return: a FIFO opened for reading would wait forever.
|
||||
fn save_within(world: &World, image: &ChatImage) -> Result<PathBuf, SaveFault> {
|
||||
let (store, sandbox, image) = (world.store.path().to_path_buf(), world.sandbox.path().to_path_buf(), image.clone());
|
||||
let (send, receive) = std::sync::mpsc::channel();
|
||||
std::thread::spawn(move || {
|
||||
let _ = send.send(save(&store, &sandbox, ROOM, &image));
|
||||
});
|
||||
receive.recv_timeout(Duration::from_secs(10)).expect("the saver hung")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fifo_or_odd_file_at_the_final_name_does_not_block_the_organ() {
|
||||
use rustix::fs::{CWD, FileType, Mode, mknodat};
|
||||
let world = World::new();
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
let held = world.attachments().join(format!("{}.png", image.sha256));
|
||||
std::fs::create_dir(world.attachments()).unwrap();
|
||||
|
||||
mknodat(CWD, &held, FileType::Fifo, Mode::RUSR | Mode::WUSR, 0).unwrap();
|
||||
assert_eq!(save_within(&world, &image), Err(SaveFault::Exists));
|
||||
std::fs::remove_file(&held).unwrap();
|
||||
|
||||
symlink("/dev/zero", &held).unwrap();
|
||||
assert_eq!(save_within(&world, &image), Err(SaveFault::Exists));
|
||||
std::fs::remove_file(&held).unwrap();
|
||||
|
||||
std::fs::create_dir(&held).unwrap();
|
||||
assert_eq!(save_within(&world, &image), Err(SaveFault::Exists));
|
||||
std::fs::remove_dir(&held).unwrap();
|
||||
|
||||
// A regular file of another length is not read at all; of the same length, only that many bytes plus one.
|
||||
std::fs::write(&held, vec![0u8; 5 * 1024 * 1024]).unwrap();
|
||||
assert_eq!(save_within(&world, &image), Err(SaveFault::Exists));
|
||||
std::fs::write(&held, vec![0u8; PNG.len()]).unwrap();
|
||||
assert_eq!(save_within(&world, &image), Err(SaveFault::Exists));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_crashed_write_leaves_no_partial_final_name_and_a_retry_saves_in_full() {
|
||||
use rustix::fs::{CWD, FileType, Mode, mknodat};
|
||||
let world = World::new();
|
||||
let big: Vec<u8> = b"\x89PNG\r\n\x1a\n".iter().copied().chain((0..20_000u32).map(|i| (i % 251) as u8)).collect();
|
||||
let image = world.put(ROOM, &big, "png");
|
||||
let final_name = format!("{}.png", image.sha256);
|
||||
let part = format!(".{final_name}.part");
|
||||
|
||||
CRASH_AT.set(Some(8192));
|
||||
assert_eq!(world.save(&image), Err(SaveFault::WriteFailed));
|
||||
CRASH_AT.set(None);
|
||||
assert_eq!(world.files(), [part.as_str()], "only the temporary is left, and the final name is absent");
|
||||
assert!(std::fs::metadata(world.attachments().join(&part)).unwrap().len() < big.len() as u64);
|
||||
|
||||
// The retry clears the leavings and saves in full.
|
||||
assert!(world.save(&image).is_ok());
|
||||
assert_eq!(world.files(), [final_name.as_str()]);
|
||||
assert_eq!(std::fs::read(world.attachments().join(&final_name)).unwrap(), big);
|
||||
|
||||
// A FIFO or a link at the temporary name does not stop the next save either.
|
||||
std::fs::remove_file(world.attachments().join(&final_name)).unwrap();
|
||||
mknodat(CWD, world.attachments().join(&part), FileType::Fifo, Mode::RUSR | Mode::WUSR, 0).unwrap();
|
||||
assert!(save_within(&world, &image).is_ok());
|
||||
symlink("/dev/zero", world.attachments().join(&part)).unwrap();
|
||||
std::fs::remove_file(world.attachments().join(&final_name)).unwrap();
|
||||
assert!(save_within(&world, &image).is_ok());
|
||||
assert_eq!(world.files(), [final_name]);
|
||||
}
|
||||
|
||||
/// The create call carries 0600, so the runner's umask can only narrow it: a looser mode in that call shows as 0644 or 0666 under the
|
||||
/// common umask 022, and a tighter one as less than 0600. (The library reads no environment and forbids unsafe, so the test does not
|
||||
/// set a umask; the source has no chmod, which the negative scan pins.)
|
||||
#[test]
|
||||
fn the_saved_file_is_mode_0600_and_the_directory_0700() {
|
||||
let world = World::new();
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
let path = world.save(&image).unwrap();
|
||||
assert_eq!(std::fs::metadata(world.sandbox.path().join(path)).unwrap().permissions().mode() & 0o7777, 0o600);
|
||||
assert_eq!(std::fs::metadata(world.attachments()).unwrap().permissions().mode() & 0o7777, 0o700);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_or_link_planted_at_the_temporary_name_after_its_unlink_is_refused_and_never_written_through() {
|
||||
let world = World::new();
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
let victim = world.sandbox.path().join("victim");
|
||||
let part = world.attachments().join(format!(".{}.png.part", image.sha256));
|
||||
for plant_a_link in [false, true] {
|
||||
std::fs::write(&victim, b"keep me").unwrap();
|
||||
let (victim_path, part_path) = (victim.clone(), part.clone());
|
||||
let plant: Box<dyn Fn()> = Box::new(move || {
|
||||
if plant_a_link {
|
||||
symlink(&victim_path, &part_path).unwrap();
|
||||
} else {
|
||||
// A regular file of her own: an create without O_EXCL would open it and truncate.
|
||||
std::fs::write(&part_path, b"hers").unwrap();
|
||||
}
|
||||
});
|
||||
GAP.with(|gap| *gap.borrow_mut() = Some(plant));
|
||||
let saved = world.save(&image);
|
||||
GAP.with(|gap| *gap.borrow_mut() = None);
|
||||
assert_eq!(saved, Err(SaveFault::WriteFailed), "link: {plant_a_link}");
|
||||
assert_eq!(std::fs::read(&victim).unwrap(), b"keep me", "nothing was written through the planted name (link: {plant_a_link})");
|
||||
if !plant_a_link {
|
||||
assert_eq!(std::fs::read(&part).unwrap(), b"hers", "her file is not truncated");
|
||||
}
|
||||
assert!(!world.attachments().join(format!("{}.png", image.sha256)).exists(), "no final name from a refused create");
|
||||
let _ = std::fs::remove_file(&part);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_link_to_a_same_length_regular_file_at_the_final_name_is_exists_not_saved() {
|
||||
let world = World::new();
|
||||
let image = world.put(ROOM, PNG, "png");
|
||||
let outside = world.sandbox.path().join("outside.bin");
|
||||
std::fs::write(&outside, PNG).unwrap();
|
||||
std::fs::create_dir(world.attachments()).unwrap();
|
||||
symlink(&outside, world.attachments().join(format!("{}.png", image.sha256))).unwrap();
|
||||
assert_eq!(world.save(&image), Err(SaveFault::Exists), "a link is not a file in her sandbox, whatever it points at");
|
||||
}
|
||||
|
|
@ -12,8 +12,9 @@
|
|||
//! Environment: `HEARTBEAT_PERSONAS` (`slug,handle,Public Name,@matrix:id,rudp://<ip>:<port>`
|
||||
//! entries separated by `;`, the last field her own Huginn daemon: each Mind is
|
||||
//! its own daemon), `MIND_PERSONA_OPERATOR` (id[,id]), `HEARTBEAT_POLL_S`,
|
||||
//! `BIFROST_CHAT_ENDPOINT` and `BIFROST_CHAT_KEY_FILE`, and `STATE_DIRECTORY`
|
||||
//! (`heartbeat.cc` lives there). The heartbeat holds no connector key.
|
||||
//! `HEARTBEAT_BEAT_S` (seconds between beats, default 1800), `BIFROST_CHAT_ENDPOINT`,
|
||||
//! `BIFROST_CHAT_KEY_FILE`, and `STATE_DIRECTORY` (`heartbeat.cc` lives there).
|
||||
//! The heartbeat holds no connector key.
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::path::Path;
|
||||
|
|
@ -22,17 +23,13 @@ use std::time::Duration;
|
|||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use eureka_state::HuginnClient;
|
||||
use mind_body::chat_rudp::{RudpChat, connection_id};
|
||||
use mind_body::heartbeat::{Config, FileControl, FileStore, HeartbeatView, Night, Organs, PersonaId, Seat, poll};
|
||||
use mind_body::heartbeat::{Config, DEFAULT_BEAT_S, FileControl, FileStore, Night, Organs, PersonaId, Seat, poll};
|
||||
use mind_body::launch::{SystemClock, SystemdLauncher};
|
||||
use mind_body::queue::HuginnMind;
|
||||
|
||||
const TIMEOUT: Duration = Duration::from_secs(15);
|
||||
const CHAT_TIMEOUT: Duration = Duration::from_secs(20);
|
||||
|
||||
fn host() -> String {
|
||||
std::fs::read_to_string("/proc/sys/kernel/hostname").map(|name| name.trim().to_string()).ok().filter(|name| !name.is_empty()).unwrap_or_else(|| "unknown".into())
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods, reason = "the binary reads its own environment, and hands it to the library")]
|
||||
fn setting(name: &str) -> Option<String> {
|
||||
std::env::var(name).ok().filter(|value| !value.is_empty())
|
||||
|
|
@ -62,7 +59,8 @@ fn config() -> Result<Config> {
|
|||
.collect::<Result<Vec<_>>>()?;
|
||||
let operators = required("MIND_PERSONA_OPERATOR")?.split(',').map(|id| id.trim().to_string()).filter(|id| !id.is_empty()).collect();
|
||||
let night = Night::from_settings(setting("HEARTBEAT_SLEEP_UTC").as_deref(), setting("HEARTBEAT_SLEEP_MINDS").as_deref())?;
|
||||
Config::new(personas, operators, night)
|
||||
let beat_s = u32::try_from(whole("HEARTBEAT_BEAT_S", i64::from(DEFAULT_BEAT_S))?).map_err(|_| anyhow!("HEARTBEAT_BEAT_S is out of range"))?;
|
||||
Config::new(personas, operators, night, beat_s)
|
||||
}
|
||||
|
||||
fn store() -> Result<FileStore> {
|
||||
|
|
@ -94,8 +92,7 @@ fn run() -> Result<()> {
|
|||
.map(|(who, (mind, chat))| Seat { who: who.clone(), mind, chat })
|
||||
.collect();
|
||||
let launcher = SystemdLauncher::new();
|
||||
let host = host();
|
||||
let organs = Organs { seats: &seats, control: &FileControl, launcher: &launcher, clock: &SystemClock, host: &host, store: &store };
|
||||
let organs = Organs { seats: &seats, control: &FileControl, launcher: &launcher, clock: &SystemClock, store: &store };
|
||||
loop {
|
||||
match poll(&organs, &config) {
|
||||
Ok(outcome) => println!("{outcome}"),
|
||||
|
|
|
|||
|
|
@ -1,56 +1,27 @@
|
|||
//! `mind-persona --instance <slug> [--endpoint rudp://<ip>:<port>]`:
|
||||
//! one start of the Persona organ (`mind_body::persona::carry`) inside
|
||||
//! `mind-persona@<instance>`. This reads the process's arguments and
|
||||
//! environment, the things the library never reads, and prints the outcome. It
|
||||
//! exits 0 for every outcome, including a unit that is dead because the brake is
|
||||
//! held or the dial out of bounds, and 2 when the mind cannot be reached, the
|
||||
//! arguments or the environment are wrong or a transport is not configured.
|
||||
//! environment, the things the library never reads, and prints the outcome:
|
||||
//! `dead` when a brake is held, `no organ` when they allow a turn, since she has
|
||||
//! none until the three-stage turn lands. It exits 0 for both, and 2 when the
|
||||
//! arguments or the environment are wrong.
|
||||
//!
|
||||
//! The unit's `persona.env` and credentials name everything else:
|
||||
//! `MIND_PERSONA_ROOMS` (a,b), `MIND_PERSONA_OPERATOR`
|
||||
//! (id[,id]), `MIND_PERSONA_MINDS` (id[,id]: every Mind of the swarm, required) and `MIND_PERSONA_SELF_SENDER`, each also a flag that overrides it
|
||||
//! (`--rooms`, `--operator`, `--minds`, `--self-sender`), and `MIND_HEARTBEAT_STATE` (optional: the path of the
|
||||
//! heartbeat's `heartbeat.cc`, which the unit reads and never writes, for the tempo in her input); `MIND_PERSONA_DOCTRINE`
|
||||
//! (optional, absolute paths separated by `:`) names the doctrine files read at each
|
||||
//! process start and put ahead of her persona; `MIND_PERSONA_MODEL` is the model her session runs;
|
||||
//! `MIND_PERSONA_RUN_CAP_S` is the unit's `RuntimeMaxSec` in seconds, the one length of a start;
|
||||
//! `MIND_PERSONA_CONFIG_SEED` optionally names a root-owned directory copied into each
|
||||
//! process's fresh configuration; `CLAUDE_LEASE_SOCKET` is the connector's lease socket (the token itself
|
||||
//! is never in this process's environment, only the descriptor a lease grants); `RUNTIME_DIRECTORY` (the
|
||||
//! unit's `RuntimeDirectory=`) holds each process's configuration and instructions; `BIFROST_CHAT_ENDPOINT`
|
||||
//! (`rudp://<ip>:<port>`, the mouth) and `BIFROST_CHAT_KEY_FILE`; `BIFROST_CHAT_PENDING_V4` set to exactly `1` asks the mouth for the
|
||||
//! reply link (`chat_pending.v4`), anything else or unset does not. `BIFROST_CHAT_FETCH` set to exactly `1` lets her ask the mouth to keep one event's image (`save_attachment`); the operator sets it only once the mouth runs `CHAT_FETCH=1`. Without
|
||||
//! `--endpoint` the daemon is `HUGINN_ENDPOINT`; the trace is `persona-trace.cc`
|
||||
//! under `STATE_DIRECTORY`, and her sandbox is `STATE_DIRECTORY/sandbox`. The key file is read here and its
|
||||
//! text goes to the transport, which holds it; nothing is printed.
|
||||
//! The persona flags (`--rooms`, `--operator`, `--minds`, `--self-sender`, or
|
||||
//! `MIND_PERSONA_ROOMS`, `MIND_PERSONA_OPERATOR`, `MIND_PERSONA_MINDS`,
|
||||
//! `MIND_PERSONA_SELF_SENDER` and `MIND_PERSONA_MODEL` from the unit's
|
||||
//! `persona.env`) are still parsed, so a start refuses the configuration the turn
|
||||
//! will need. Without `--endpoint` the daemon is `HUGINN_ENDPOINT`; no call is made
|
||||
//! to it.
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Duration;
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use eureka_state::HuginnClient;
|
||||
use mind_body::chat_rudp::{RudpChat, connection_id};
|
||||
use anyhow::Result;
|
||||
use mind_body::cli::instance_and_endpoint;
|
||||
use mind_body::control::{CONTROL_ROOT, FileSource, control_path, swarm_path};
|
||||
use mind_body::launch::{SystemClock, SystemdLauncher};
|
||||
use mind_body::heartbeat::FileStore;
|
||||
use mind_body::lease_client::{LEASE_SOCKET_VARIABLE, SocketLender};
|
||||
use mind_body::persona::{Config, ENV_CONFIG_SEED, ENV_RUN_CAP, FileTrace, HuginnPersona, Organs, SANDBOX_DIR, carry};
|
||||
use mind_body::queue::HuginnMind;
|
||||
use mind_body::self_run::{ClaudeSpawner, SCRATCH_VARIABLE};
|
||||
|
||||
/// How long one call to the mind may take.
|
||||
const TIMEOUT: Duration = Duration::from_secs(15);
|
||||
/// How long the mouth may take to answer one chat request (a send is a Matrix PUT).
|
||||
const CHAT_TIMEOUT: Duration = Duration::from_secs(20);
|
||||
use mind_body::persona::{Config, carry};
|
||||
|
||||
const USAGE: &str = "usage: mind-persona --instance <slug> [--endpoint rudp://<ip>:<port>] [--rooms a,b] [--operator id[,id]] [--minds id[,id]] [--self-sender id]";
|
||||
|
||||
fn host() -> String {
|
||||
std::fs::read_to_string("/proc/sys/kernel/hostname").map(|name| name.trim().to_string()).ok().filter(|name| !name.is_empty()).unwrap_or_else(|| "unknown".into())
|
||||
}
|
||||
|
||||
// The binary is the one place that reads the process: its arguments and
|
||||
// the environment. The library reads neither (clippy.toml, `#![forbid]` in lib.rs).
|
||||
#[allow(clippy::disallowed_methods, reason = "the binary reads its own arguments and environment, and hands them to the library")]
|
||||
|
|
@ -58,42 +29,14 @@ fn setting(name: &str) -> Option<String> {
|
|||
std::env::var(name).ok().filter(|value| !value.is_empty())
|
||||
}
|
||||
|
||||
fn required(name: &str) -> Result<String> {
|
||||
setting(name).ok_or_else(|| anyhow!("{name} is not set"))
|
||||
}
|
||||
|
||||
/// The text of the credential file named by `name`; never echoed.
|
||||
fn credential(name: &str) -> Result<String> {
|
||||
std::fs::read_to_string(required(name)?).with_context(|| format!("the file named by {name} could not be read"))
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods, reason = "the binary reads its own arguments and environment, and hands them to the library")]
|
||||
fn run() -> Result<String> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let (config, rest) = Config::from_args_and_env(&args, setting, USAGE)?;
|
||||
let (instance, address) = instance_and_endpoint(&rest, setting("HUGINN_ENDPOINT"), USAGE)?;
|
||||
let Some(state) = setting("STATE_DIRECTORY") else { bail!("no STATE_DIRECTORY") };
|
||||
let chat_endpoint: SocketAddr = required("BIFROST_CHAT_ENDPOINT")?
|
||||
.strip_prefix("rudp://")
|
||||
.and_then(|address| address.parse().ok())
|
||||
.ok_or_else(|| anyhow!("BIFROST_CHAT_ENDPOINT is not rudp://<ip>:<port>"))?;
|
||||
let reply_link = setting("BIFROST_CHAT_PENDING_V4").as_deref() == Some("1");
|
||||
let chat = RudpChat::new(chat_endpoint, connection_id(&credential("BIFROST_CHAT_KEY_FILE")?), instance.0.clone(), CHAT_TIMEOUT).with_reply_link(reply_link);
|
||||
let run_cap_s: u64 = setting(ENV_RUN_CAP).and_then(|seconds| seconds.parse().ok()).filter(|seconds| *seconds > 0).ok_or_else(|| anyhow!("{ENV_RUN_CAP} is not set to the unit's RuntimeMaxSec in seconds"))?;
|
||||
let scratch = PathBuf::from(required(SCRATCH_VARIABLE)?);
|
||||
let sandbox = Path::new(&state).join(SANDBOX_DIR);
|
||||
let lender = SocketLender::new(required(LEASE_SOCKET_VARIABLE)?);
|
||||
let spawner = ClaudeSpawner::in_sandbox(std::env::vars_os(), ENV_CONFIG_SEED, Some(sandbox.clone()))?;
|
||||
let source = FileSource::new(control_path(Path::new(CONTROL_ROOT), &instance)?);
|
||||
let mind = HuginnMind::new(HuginnClient::new(address, instance.clone(), TIMEOUT));
|
||||
let persona = HuginnPersona::new(HuginnClient::new(address, instance, TIMEOUT));
|
||||
let trace = FileTrace::new(Path::new(&state).join("persona-trace.cc"));
|
||||
let launcher = SystemdLauncher::new();
|
||||
let host = host();
|
||||
let heartbeat = setting("MIND_HEARTBEAT_STATE").map(FileStore::new);
|
||||
let organs = Organs { mind: &mind, launcher: &launcher, clock: &SystemClock, host: &host, chat: &chat, persona: &persona, trace: &trace, heartbeat: heartbeat.as_ref().map(|store| store as &dyn mind_body::heartbeat::HeartbeatView), lender: &lender, spawner: &spawner, run_cap_s, sandbox: &sandbox, scratch: &scratch };
|
||||
let (_config, rest) = Config::from_args_and_env(&args, setting, USAGE)?;
|
||||
let (instance, _endpoint) = instance_and_endpoint(&rest, setting("HUGINN_ENDPOINT"), USAGE)?;
|
||||
let control = FileSource::new(control_path(Path::new(CONTROL_ROOT), &instance)?);
|
||||
let swarm = FileSource::new(swarm_path(Path::new(CONTROL_ROOT)));
|
||||
Ok(carry(&organs, &source, &swarm, &config)?.to_string())
|
||||
Ok(carry(&control, &swarm, &instance).to_string())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -69,8 +69,7 @@ pub(crate) fn load(store: Option<&Path>, room_id: &str, image: &ChatImage) -> Re
|
|||
Ok(Image { media_type, base64: STANDARD.encode(&bytes) })
|
||||
}
|
||||
|
||||
/// The verified bytes of `image` and the media type they sniff as: everything `load` checks, before it encodes. The one verifier;
|
||||
/// the attachment saver calls it too, and holds no check of its own.
|
||||
/// The verified bytes of `image` and the media type they sniff as: everything `load` checks, before it encodes. The one verifier.
|
||||
pub(crate) fn load_bytes(store: &Path, room_id: &str, image: &ChatImage) -> Result<(Vec<u8>, &'static str), Skip> {
|
||||
if image.size > MAX_BYTES {
|
||||
return Err(Skip::TooLarge);
|
||||
|
|
|
|||
|
|
@ -1,60 +1,50 @@
|
|||
//! The heartbeat: the one process that decides which Persona takes a turn and
|
||||
//! when (rulings `eureka-body:ruling:shared-heartbeat-joint-lever`,
|
||||
//! `self-declared-urgency-etiquette`, `operator-mention-fills-initiative`).
|
||||
//! The heartbeat: the one process that decides which Persona wakes and when
|
||||
//! (rulings `eureka-body:ruling:persona-only-heartbeat`,
|
||||
//! `operator-mention-fills-initiative`).
|
||||
//!
|
||||
//! The Minds share one heartbeat and jointly hold the lever to it. Its tempo is
|
||||
//! `tempo::tally` of every Mind's newest vote, a `Pulse` in her own mind. When a
|
||||
//! beat falls (the beat has passed since the last one) exactly one Persona takes
|
||||
//! a turn: `tempo::pick` over the idle, released Minds, by how long each has
|
||||
//! waited, bent by the urgency she declared for herself, a member addressing her
|
||||
//! and (a later cut) topics she subscribed to. Two things launch a Persona off
|
||||
//! the beat: the operator tagging or naming her, once per message, and her own
|
||||
//! Self run having ended since her last turn. A beat's launch for a Mind whose
|
||||
//! Self holds briefs (`inbox::derive`) is a Self run claiming them, in place of
|
||||
//! her Persona turn: a session that ended with a brief open, or a brief admitted
|
||||
//! after its flush, is never an orphan, and a Self start takes the beat like any
|
||||
//! launch. A brief an open question holds (raised in it, or in a run that claims it:
|
||||
//! the repetition breaker's trip holds the claims of the run it belongs to) is
|
||||
//! neither claimed nor told until that question is answered or withdrawn; other
|
||||
//! briefs are. Nothing is metered here:
|
||||
//! no meter and no model call.
|
||||
//! The beat is the configured `Config::beat_s`. When a beat falls (the beat has
|
||||
//! passed since the last one) exactly one Persona wakes: `tempo::pick` over the
|
||||
//! idle, released Minds, by how long each has waited since the heartbeat last
|
||||
//! woke her, bent by a member addressing her and (a later cut) topics she
|
||||
//! subscribed to. The operator or another Mind tagging or naming her wakes her
|
||||
//! off the beat, once per message. Inside the night window a listed Mind wakes
|
||||
//! once for her night. Nothing is metered here: no meter and no model call.
|
||||
//!
|
||||
//! A wake starts her unit and nothing else: the heartbeat writes no mind (ruling
|
||||
//! `persona-only-heartbeat`: her Mind is written only by Self sessions). It reads
|
||||
//! her mind only for the names her projects go by. Busy is systemd's own: a
|
||||
//! Persona whose unit is active is busy, and a start of an active unit is a no-op.
|
||||
//!
|
||||
//! The heartbeat owns `heartbeat.cc` and is its only writer; Persona units read
|
||||
//! it. The brakes stay the operator's and are read on every poll. Busy and idle
|
||||
//! are the slot's: a Persona run whose unit lives (or is within its start grace) is busy; a dead holder is not, and the next launch closes it. The launch is
|
||||
//! `open_and_launch`, the only opener. The heartbeat never acks a delivery; the
|
||||
//! Persona turn drains them.
|
||||
//! its addressing. The brakes stay the operator's and are read on every poll. The
|
||||
//! heartbeat never acks a delivery.
|
||||
//!
|
||||
//! A held swarm brake ends the poll before the mind or the mouth is touched.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
use std::collections::HashSet;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use chrono::{DateTime, SecondsFormat, Timelike, Utc};
|
||||
use cultcache_rs::{CultCache, DatabaseEntry, SingleFileMessagePackBackingStore};
|
||||
use cultnet_rs::Selection;
|
||||
use eureka_pipeline::{OrgRepo, PipelineKind, PipelineRef, ResolutionOutcome, RunOperator, RunTurn, Slug};
|
||||
use huginn_mind::{PipelineDocumentSummary, PipelineFacts, PipelinePageItems};
|
||||
use eureka_pipeline::{OrgRepo, PipelineKind, RunTurn, Slug};
|
||||
use huginn_mind::PipelineFacts;
|
||||
|
||||
use crate::address::{Addressee, Name, is_for, project_names};
|
||||
use crate::chat_client::{Chat, ChatDelivery};
|
||||
use crate::control::{ControlSource, Effective, FileSource, HeldReason, SwarmEffective, read_effective, read_envelope, set_mode_world_readable};
|
||||
use crate::launch::{Clock, LaunchOutcome, LaunchRequest, Launcher, Ports, open_and_launch, slot};
|
||||
use crate::queue::{MindPort, any_of, citing, claims_of, headers, in_force, of_kind, on_hold};
|
||||
use crate::self_run::inbox;
|
||||
use crate::launch::{Clock, Launcher};
|
||||
use crate::queue::{MindPort, headers, in_force, of_kind};
|
||||
use crate::tempo::{self, Standing};
|
||||
|
||||
/// The admitting agent's name on every run the heartbeat opens.
|
||||
pub const AGENT: &str = "mind-heartbeat";
|
||||
/// How many handled operator mentions the state remembers.
|
||||
const MENTIONS_KEPT: usize = 64;
|
||||
/// How many of an instance's latest Self runs a poll looks at.
|
||||
const RECENT: u32 = 8;
|
||||
/// The most pages of pending chat one poll reads for one Persona.
|
||||
const PENDING_PAGES: usize = 16;
|
||||
/// The beat, in seconds, when `HEARTBEAT_BEAT_S` is not set.
|
||||
pub const DEFAULT_BEAT_S: u32 = 1800;
|
||||
|
||||
/// Who a Persona is, for the two things the heartbeat matches on: the Matrix
|
||||
/// user id its tags carry, and the names a message may call it by. Each Mind is
|
||||
|
|
@ -106,6 +96,8 @@ pub struct Config {
|
|||
pub operators: Vec<String>,
|
||||
/// The nightly window and the Minds that sleep in it; none, no one sleeps.
|
||||
pub night: Option<Night>,
|
||||
/// Seconds between beats: `HEARTBEAT_BEAT_S`, else `DEFAULT_BEAT_S`.
|
||||
pub beat_s: u32,
|
||||
}
|
||||
|
||||
/// The heartbeat's night: a daily window in UTC minutes (it may wrap midnight) and the Minds that sleep in it.
|
||||
|
|
@ -164,11 +156,14 @@ impl Night {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
/// Refuses no Persona, or a Persona twice.
|
||||
pub fn new(personas: Vec<PersonaId>, operators: Vec<String>, night: Option<Night>) -> Result<Self> {
|
||||
/// Refuses no Persona, a Persona twice, or a beat of zero seconds.
|
||||
pub fn new(personas: Vec<PersonaId>, operators: Vec<String>, night: Option<Night>, beat_s: u32) -> Result<Self> {
|
||||
if personas.is_empty() {
|
||||
bail!("no persona is configured");
|
||||
}
|
||||
if beat_s == 0 {
|
||||
bail!("the beat is zero seconds");
|
||||
}
|
||||
for (index, persona) in personas.iter().enumerate() {
|
||||
if personas
|
||||
.iter()
|
||||
|
|
@ -182,21 +177,28 @@ impl Config {
|
|||
personas,
|
||||
operators,
|
||||
night,
|
||||
beat_s,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `heartbeat.cc`'s state: when the last beat fell, and the operator messages
|
||||
/// already answered with a launch (once per message, bounded). A later cut adds
|
||||
/// the cache of scored deliveries beside them.
|
||||
/// `heartbeat.cc`'s state: when the last beat fell, the operator messages
|
||||
/// already answered with a wake (once per message, bounded), and when the
|
||||
/// heartbeat last woke each Persona and last started each night.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
|
||||
#[cultcache(type = "eureka.heartbeat.state.v1", schema = "HeartbeatState")]
|
||||
pub struct HeartbeatState {
|
||||
#[cultcache(key = 0)]
|
||||
pub last_beat_at: DateTime<Utc>,
|
||||
/// `<instance>:<event id>` of each operator message that launched a Persona.
|
||||
/// `<instance>:<event id>` of each operator message that woke a Persona.
|
||||
#[cultcache(key = 1)]
|
||||
pub handled_mentions: Vec<String>,
|
||||
/// When the heartbeat last started each Persona's turn, by instance slug.
|
||||
#[cultcache(key = 2, default)]
|
||||
pub last_turn_at: BTreeMap<String, DateTime<Utc>>,
|
||||
/// When the heartbeat last started each Persona's night, by instance slug.
|
||||
#[cultcache(key = 3, default)]
|
||||
pub last_night_at: BTreeMap<String, DateTime<Utc>>,
|
||||
}
|
||||
|
||||
const STATE_KEY: &str = "state";
|
||||
|
|
@ -217,9 +219,9 @@ const ADDRESSING_KEY: &str = "addressing";
|
|||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct InstanceTrace {
|
||||
pub instance: String,
|
||||
/// Her newest tempo vote, in seconds per beat.
|
||||
/// Retired: always none. Kept so the records `heartbeat.cc` holds from before the cut still decode (nothing migrates the file).
|
||||
pub vote_beat_s: Option<u32>,
|
||||
/// The urgency in force for her: declared and not yet consumed by a turn.
|
||||
/// The urgency in force for her: 1000 when a member addresses her, else 0.
|
||||
pub urgency_permille: u16,
|
||||
/// A member's pending message addresses her.
|
||||
pub addressed: bool,
|
||||
|
|
@ -238,7 +240,7 @@ pub struct InstanceTrace {
|
|||
pub struct PollTrace {
|
||||
#[cultcache(key = 0)]
|
||||
pub at: DateTime<Utc>,
|
||||
/// The beat in force: the tally of the Minds' votes.
|
||||
/// The beat in force: `Config::beat_s`.
|
||||
#[cultcache(key = 1)]
|
||||
pub tempo_s: u32,
|
||||
#[cultcache(key = 2)]
|
||||
|
|
@ -253,8 +255,8 @@ impl fmt::Display for PollTrace {
|
|||
for row in &self.instances {
|
||||
write!(
|
||||
f,
|
||||
" {}: vote {:?} urgency {} addressed {} busy {} released {} idle {}s score {:?}",
|
||||
row.instance, row.vote_beat_s, row.urgency_permille, row.addressed, row.busy, row.released, row.idle_s, row.score
|
||||
" {}: urgency {} addressed {} busy {} released {} idle {}s score {:?}",
|
||||
row.instance, row.urgency_permille, row.addressed, row.busy, row.released, row.idle_s, row.score
|
||||
)?;
|
||||
if !row.note.is_empty() {
|
||||
write!(f, " note {}", row.note)?;
|
||||
|
|
@ -277,10 +279,8 @@ pub trait Store {
|
|||
fn save_addressing(&self, addressing: &Addressing) -> Result<()>;
|
||||
}
|
||||
|
||||
/// What a Persona unit may read of `heartbeat.cc`: the newest trace, which
|
||||
/// carries the tempo and every Mind's vote and declared urgency, and the swarm's addressing. Read-only.
|
||||
/// What a Persona unit may read of `heartbeat.cc`: the swarm's addressing. Read-only.
|
||||
pub trait HeartbeatView {
|
||||
fn latest(&self) -> Result<Option<PollTrace>>;
|
||||
fn addressing(&self) -> Result<Option<Addressing>>;
|
||||
}
|
||||
|
||||
|
|
@ -356,6 +356,13 @@ impl Store for FileStore {
|
|||
}
|
||||
|
||||
impl FileStore {
|
||||
/// The newest poll trace: what `mind-heartbeat timeline` prints.
|
||||
pub fn latest(&self) -> Result<Option<PollTrace>> {
|
||||
let mut all = self.snapshot()?.get_all_with_keys::<PollTrace>()?;
|
||||
all.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
Ok(all.pop().map(|(_, trace)| trace))
|
||||
}
|
||||
|
||||
/// The file read without a lock and without a write, as the control stores are.
|
||||
fn snapshot(&self) -> Result<CultCache> {
|
||||
let mut cache = Self::registered()?;
|
||||
|
|
@ -367,12 +374,6 @@ impl FileStore {
|
|||
}
|
||||
|
||||
impl HeartbeatView for FileStore {
|
||||
fn latest(&self) -> Result<Option<PollTrace>> {
|
||||
let mut all = self.snapshot()?.get_all_with_keys::<PollTrace>()?;
|
||||
all.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
Ok(all.pop().map(|(_, trace)| trace))
|
||||
}
|
||||
|
||||
fn addressing(&self) -> Result<Option<Addressing>> {
|
||||
self.load_addressing()
|
||||
}
|
||||
|
|
@ -410,12 +411,11 @@ pub struct Organs<'a> {
|
|||
pub control: &'a dyn Control,
|
||||
pub launcher: &'a dyn Launcher,
|
||||
pub clock: &'a dyn Clock,
|
||||
pub host: &'a str,
|
||||
pub store: &'a dyn Store,
|
||||
}
|
||||
|
||||
/// What one poll did. Every outcome is an answer; only a mind, store or
|
||||
/// mouth that cannot be reached is an error.
|
||||
/// What one poll did. Every outcome is an answer; only a store that cannot be
|
||||
/// reached is an error.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Outcome {
|
||||
/// The swarm brake is held; nothing else was touched.
|
||||
|
|
@ -435,77 +435,6 @@ impl fmt::Display for Outcome {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_time(text: &str) -> Result<DateTime<Utc>> {
|
||||
DateTime::parse_from_rfc3339(text)
|
||||
.map(|at| at.with_timezone(&Utc))
|
||||
.map_err(|_| anyhow!("the mind gave a time that is not a timestamp"))
|
||||
}
|
||||
|
||||
/// When a run was last resolved and how: its resolution of the highest sequence.
|
||||
fn latest_resolution(
|
||||
mind: &dyn MindPort,
|
||||
run: &PipelineRef,
|
||||
) -> Result<Option<(DateTime<Utc>, ResolutionOutcome)>> {
|
||||
let closures = Selection {
|
||||
cites: Some(citing(run, Some("subject"))),
|
||||
..of_kind(PipelineKind::Resolution)
|
||||
};
|
||||
let mut ending: Option<(u32, DateTime<Utc>, ResolutionOutcome)> = None;
|
||||
for header in headers(mind, &closures)? {
|
||||
if let PipelineFacts::Resolution {
|
||||
sequence, outcome, ..
|
||||
} = header.facts
|
||||
&& ending
|
||||
.as_ref()
|
||||
.is_none_or(|(latest, ..)| sequence >= *latest)
|
||||
{
|
||||
ending = Some((
|
||||
sequence,
|
||||
parse_time(&header.admission.admitted_at)?,
|
||||
outcome,
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(ending.map(|(_, at, outcome)| (at, outcome)))
|
||||
}
|
||||
|
||||
/// When her newest Persona turn that actually ran was admitted: one bounded read
|
||||
/// of the runs the mind indexes as `ran`. A turn withdrawn unrun never happened,
|
||||
/// so it neither resets her wait nor counts as her last turn, and the cost of
|
||||
/// finding the last real turn does not grow with how many were withdrawn.
|
||||
fn last_turn(mind: &dyn MindPort, instance: &Slug) -> Result<Option<PipelineDocumentSummary>> {
|
||||
let turn = format!("{:?}", RunTurn::PersonaTurn);
|
||||
let operator = format!("{:?}", RunOperator::Mind);
|
||||
let runs = any_of(
|
||||
any_of(
|
||||
any_of(
|
||||
any_of(of_kind(PipelineKind::Run), "root", &[&instance.0]),
|
||||
"turn",
|
||||
&[&turn],
|
||||
),
|
||||
"operated_by",
|
||||
&[&operator],
|
||||
),
|
||||
"ran",
|
||||
&["true"],
|
||||
);
|
||||
newest(mind, runs)
|
||||
}
|
||||
|
||||
/// When her newest Sleep run was admitted, whatever became of it: a night that failed still counts.
|
||||
fn last_sleep(mind: &dyn MindPort, instance: &Slug) -> Result<Option<DateTime<Utc>>> {
|
||||
let operator = format!("{:?}", RunOperator::Mind);
|
||||
let runs = any_of(any_of(any_of(of_kind(PipelineKind::Run), "root", &[&instance.0]), "turn", &["Sleep"]), "operated_by", &[&operator]);
|
||||
newest(mind, runs)?.map(|header| parse_time(&header.admission.admitted_at)).transpose()
|
||||
}
|
||||
|
||||
/// The newest document a selection holds, by admission ordinal: one query.
|
||||
fn newest(mind: &dyn MindPort, selection: Selection) -> Result<Option<PipelineDocumentSummary>> {
|
||||
let asking = Selection { descending: true, limit: Some(1), ..selection };
|
||||
let PipelinePageItems::Headers(found) = mind.query(&asking)?.items else { bail!("the mind answered documents to a header selection") };
|
||||
Ok(found.into_iter().next())
|
||||
}
|
||||
|
||||
/// Every repo she stewards in force and the chat names of those repos: one query on her mind.
|
||||
pub(crate) fn stewardships_of(mind: &dyn MindPort) -> Result<(Vec<Name>, Vec<OrgRepo>)> {
|
||||
let (mut names, mut repos) = (Vec::new(), Vec::new());
|
||||
|
|
@ -546,84 +475,14 @@ fn read_pending(chat: &dyn Chat) -> Result<Vec<ChatDelivery>> {
|
|||
Ok(all)
|
||||
}
|
||||
|
||||
fn label_of(run: &eureka_pipeline::PipelineRef) -> String {
|
||||
run.id.0.rsplit(':').next().unwrap_or_default().to_string()
|
||||
}
|
||||
|
||||
/// Her standing vote: the newest pulse that votes or withdraws, by admission
|
||||
/// ordinal. A vote stands until she casts another or withdraws it, however many
|
||||
/// pulses of other kinds follow; a withdrawal leaves her silent.
|
||||
fn standing_vote(mind: &dyn MindPort, instance: &Slug) -> Result<Option<u32>> {
|
||||
let pulses = any_of(any_of(of_kind(PipelineKind::Pulse), "root", &[&instance.0]), "voted", &["true"]);
|
||||
Ok(newest(mind, pulses)?.and_then(|header| match header.facts {
|
||||
PipelineFacts::Pulse { tempo_vote_beat_s, tempo_vote_withdrawn: false, .. } => tempo_vote_beat_s,
|
||||
_ => None,
|
||||
}))
|
||||
}
|
||||
|
||||
/// The urgency she declared for herself and no turn has consumed: her newest
|
||||
/// declaration, if it was admitted after her newest Persona turn, by admission
|
||||
/// ordinal like the vote: the wall clock never decides what a turn consumed.
|
||||
fn pending_urgency(mind: &dyn MindPort, instance: &Slug, last_turn: Option<&PipelineDocumentSummary>) -> Result<u16> {
|
||||
let pulses = any_of(any_of(of_kind(PipelineKind::Pulse), "root", &[&instance.0]), "urged", &["true"]);
|
||||
let Some(header) = newest(mind, pulses)? else { return Ok(0) };
|
||||
let PipelineFacts::Pulse { urgency_permille: Some(urgency), .. } = header.facts else { return Ok(0) };
|
||||
Ok(if last_turn.is_none_or(|turn| header.admission.ordinal > turn.admission.ordinal) { urgency } else { 0 })
|
||||
}
|
||||
|
||||
/// When her Self runs last ended: the latest resolution among her recent Self runs.
|
||||
fn last_self_end(mind: &dyn MindPort, instance: &Slug) -> Result<Option<DateTime<Utc>>> {
|
||||
let turn = format!("{:?}", RunTurn::SelfRun);
|
||||
let operator = format!("{:?}", RunOperator::Mind);
|
||||
let runs = any_of(any_of(any_of(of_kind(PipelineKind::Run), "root", &[&instance.0]), "turn", &[&turn]), "operated_by", &[&operator]);
|
||||
let selection = Selection { descending: true, limit: Some(RECENT), ..runs };
|
||||
let PipelinePageItems::Headers(runs) = mind.query(&selection)?.items else { bail!("the mind answered documents to a header selection") };
|
||||
let mut newest = None;
|
||||
for run in runs {
|
||||
if let Some((ended, _)) = latest_resolution(mind, &run.id)? {
|
||||
newest = newest.max(Some(ended));
|
||||
}
|
||||
}
|
||||
Ok(newest)
|
||||
}
|
||||
|
||||
/// One Persona reads from her own Mind.
|
||||
struct MindRead {
|
||||
busy: bool,
|
||||
/// When her newest Persona turn was admitted.
|
||||
last_turn: Option<DateTime<Utc>>,
|
||||
/// Her standing vote for a beat.
|
||||
vote: Option<u32>,
|
||||
/// The urgency she declared and no turn has consumed.
|
||||
urgency: u16,
|
||||
self_end: Option<DateTime<Utc>>,
|
||||
/// When her newest Sleep run was admitted.
|
||||
last_sleep: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
fn read_mind(organs: &Organs, mind: &dyn MindPort, slug: &Slug) -> Result<MindRead> {
|
||||
let turn = last_turn(mind, slug)?;
|
||||
Ok(MindRead {
|
||||
// Busy is the slot's own reading of life: a holder whose unit is not active
|
||||
// (past its start grace) is dead, not busy, so the next launch reaches
|
||||
// `slot` step 0 and closes it there.
|
||||
busy: slot(&ports(organs, mind), slug, RunTurn::PersonaTurn, organs.clock.now())?.live.is_some(),
|
||||
last_turn: turn.as_ref().map(|header| parse_time(&header.admission.admitted_at)).transpose()?,
|
||||
vote: standing_vote(mind, slug)?,
|
||||
urgency: pending_urgency(mind, slug, turn.as_ref())?,
|
||||
self_end: last_self_end(mind, slug)?,
|
||||
last_sleep: last_sleep(mind, slug)?,
|
||||
})
|
||||
}
|
||||
|
||||
struct Row<'a> {
|
||||
seat: &'a Seat<'a>,
|
||||
read: Option<MindRead>,
|
||||
released: bool,
|
||||
/// Her unit is active.
|
||||
busy: bool,
|
||||
pending: Vec<ChatDelivery>,
|
||||
/// Whom a delivery may address in her: her roster names and her projects' names as the last poll that read her pending chat left them.
|
||||
addressee: Addressee,
|
||||
busy: bool,
|
||||
note: String,
|
||||
}
|
||||
|
||||
|
|
@ -633,7 +492,7 @@ impl Row<'_> {
|
|||
}
|
||||
|
||||
fn idle(&self) -> bool {
|
||||
self.released && !self.busy && self.read.is_some()
|
||||
self.released && !self.busy
|
||||
}
|
||||
|
||||
/// A member (not the operator) has a pending message that names or tags her.
|
||||
|
|
@ -642,100 +501,31 @@ impl Row<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
enum Launched {
|
||||
Run(String),
|
||||
enum Woke {
|
||||
Started,
|
||||
Busy,
|
||||
Refused(String),
|
||||
}
|
||||
|
||||
fn ports<'a>(organs: &Organs<'a>, mind: &'a dyn MindPort) -> Ports<'a> {
|
||||
Ports {
|
||||
mind,
|
||||
launcher: organs.launcher,
|
||||
clock: organs.clock,
|
||||
host: organs.host,
|
||||
/// The one way the heartbeat wakes a Persona, for a turn or her night: her brake
|
||||
/// read again, then her unit started unless it is active. No run is opened and
|
||||
/// nothing is admitted.
|
||||
fn wake(organs: &Organs, row: &Row, turn: RunTurn) -> Woke {
|
||||
if !matches!(organs.control.effective(row.slug()), Effective::Released(_)) {
|
||||
return Woke::Refused("the brake is held".into());
|
||||
}
|
||||
if organs.launcher.alive(row.slug(), turn) {
|
||||
return Woke::Busy;
|
||||
}
|
||||
match organs.launcher.start(row.slug(), turn) {
|
||||
Ok(()) => Woke::Started,
|
||||
Err(error) => Woke::Refused(format!("start failed: {error}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// The one way the heartbeat starts a run: the same brake read and the same
|
||||
/// opener for a Persona turn (a beat, an operator mention, a Self run that ended)
|
||||
/// and for the Self run a beat gives to the briefs her Self holds.
|
||||
fn launch(
|
||||
organs: &Organs,
|
||||
row: &Row,
|
||||
turn: RunTurn,
|
||||
claims: Vec<PipelineRef>,
|
||||
) -> Result<Launched> {
|
||||
let Effective::Released(grant) = organs.control.effective(row.slug()) else {
|
||||
return Ok(Launched::Refused("the brake is held".into()));
|
||||
};
|
||||
let request = LaunchRequest {
|
||||
turn,
|
||||
claims,
|
||||
agent: AGENT.into(),
|
||||
grant,
|
||||
};
|
||||
let ports = ports(organs, row.seat.mind);
|
||||
Ok(match open_and_launch(&ports, request) {
|
||||
Ok(LaunchOutcome::Launched { run }) => Launched::Run(label_of(&run)),
|
||||
Ok(LaunchOutcome::Busy { .. }) => Launched::Busy,
|
||||
Ok(LaunchOutcome::Tripped { run, question }) => {
|
||||
let held = claims_of(row.seat.mind, &run).map_or(0, |claims| claims.len());
|
||||
Launched::Refused(format!("tripped: {} holds the {held} claims of {}", question.id.0, run.id.0))
|
||||
}
|
||||
Ok(_) => Launched::Refused("launch refused".into()),
|
||||
Err(error) => Launched::Refused(format!("launch failed: {error}")),
|
||||
})
|
||||
}
|
||||
|
||||
/// The most briefs one Self run claims.
|
||||
const BRIEFS_PER_SELF_RUN: usize = 64;
|
||||
|
||||
/// What a Self run would claim for this Mind, read at one instant.
|
||||
struct Claimable {
|
||||
/// The briefs in force that no live Self run holds and no open question holds.
|
||||
claims: Vec<PipelineRef>,
|
||||
/// How many briefs an open question holds.
|
||||
held: usize,
|
||||
/// Self runs in force whose unit is gone: the launch closes them.
|
||||
dead: Vec<PipelineRef>,
|
||||
}
|
||||
|
||||
/// The briefs a Self run would claim for this Mind: those in force that no live
|
||||
/// Self run holds and no open question holds. A Self run claims up to
|
||||
/// `BRIEFS_PER_SELF_RUN` briefs and every run it ends empty counts against all
|
||||
/// of them, so one cause trips them together; a trip holds the claims of its
|
||||
/// run, not the Mind, until the operator answers or withdraws its question, and
|
||||
/// the count starts afresh. Whether a holder lives is `launch::slot`'s answer
|
||||
/// (a dead one is closed by the launch, so it does not stop the successor), and
|
||||
/// what is held is `queue::on_hold`'s. The read here only chooses the claims;
|
||||
/// the launch's gate reads the hold again at admission and decides.
|
||||
fn claimable(organs: &Organs, row: &Row) -> Result<Claimable> {
|
||||
let mind = row.seat.mind;
|
||||
let found = slot(
|
||||
&ports(organs, mind),
|
||||
row.slug(),
|
||||
RunTurn::SelfRun,
|
||||
organs.clock.now(),
|
||||
)?;
|
||||
if found.live.is_some() {
|
||||
return Ok(Claimable {
|
||||
claims: Vec::new(),
|
||||
held: 0,
|
||||
dead: Vec::new(),
|
||||
});
|
||||
}
|
||||
let (free, held) = inbox::derive_counting_held(mind, &on_hold(mind)?, &HashSet::new())?;
|
||||
let claims = free
|
||||
.into_iter()
|
||||
.map(|brief| PipelineRef { kind: PipelineKind::FollowUp, id: brief.id.as_str().into() })
|
||||
.take(BRIEFS_PER_SELF_RUN)
|
||||
.collect();
|
||||
Ok(Claimable {
|
||||
claims,
|
||||
held,
|
||||
dead: found.dead,
|
||||
})
|
||||
/// What a wake is called in the trace: her slug and the turn.
|
||||
fn wake_label(slug: &Slug, turn: RunTurn) -> String {
|
||||
format!("{}:{turn:?}", slug.0)
|
||||
}
|
||||
|
||||
/// One poll.
|
||||
|
|
@ -745,7 +535,7 @@ pub fn poll(organs: &Organs, config: &Config) -> Result<Outcome> {
|
|||
}
|
||||
let now = organs.clock.now();
|
||||
let stored = organs.store.load()?;
|
||||
let mut state = stored.clone().unwrap_or(HeartbeatState { last_beat_at: DateTime::<Utc>::UNIX_EPOCH, handled_mentions: vec![] });
|
||||
let mut state = stored.clone().unwrap_or(HeartbeatState { last_beat_at: DateTime::<Utc>::UNIX_EPOCH, handled_mentions: vec![], last_turn_at: BTreeMap::new(), last_night_at: BTreeMap::new() });
|
||||
let mut eventful = false;
|
||||
// The table as the last poll left it; unreadable counts as none, is noted in the trace, and is not overwritten.
|
||||
let (stored_addressing, addressing_unread) = match organs.store.load_addressing() {
|
||||
|
|
@ -756,23 +546,19 @@ pub fn poll(organs: &Organs, config: &Config) -> Result<Outcome> {
|
|||
let mut rows: Vec<Row> = Vec::new();
|
||||
for seat in organs.seats {
|
||||
let released = matches!(organs.control.effective(&seat.who.slug), Effective::Released(_));
|
||||
// One Mind daemon down skips her for this poll; the others are served.
|
||||
let (read, note) = match read_mind(organs, seat.mind, &seat.who.slug) {
|
||||
Ok(read) => (Some(read), String::new()),
|
||||
Err(error) => {
|
||||
eventful = true;
|
||||
(None, format!("skipped: her mind could not be read: {error}"))
|
||||
}
|
||||
};
|
||||
let busy = organs.launcher.alive(&seat.who.slug, RunTurn::PersonaTurn);
|
||||
// Her last-read names when the table holds her, else her roster names alone.
|
||||
let roster = Addressee::new(&seat.who.matrix_id, [seat.who.public_name.as_str(), seat.who.handle.as_str()]);
|
||||
let kept = stored_addressing.minds.iter().find(|stored| stored.matrix_id() == seat.who.matrix_id).cloned().unwrap_or_else(|| roster.clone());
|
||||
let mut row = Row { seat, busy: read.as_ref().is_some_and(|read| read.busy), read, released, pending: vec![], addressee: kept, note };
|
||||
// Pending chat, read-only, for a released Mind whose mind could be read.
|
||||
if row.released && row.read.is_some() {
|
||||
let mut row = Row { seat, released, busy, pending: vec![], addressee: kept, note: String::new() };
|
||||
// Pending chat, read-only, for a released Mind.
|
||||
if row.released {
|
||||
match read_pending(seat.chat) {
|
||||
Ok(deliveries) => row.pending = deliveries,
|
||||
Err(_) => row.note = "chat: pending failed".into(),
|
||||
Err(_) => {
|
||||
eventful = true;
|
||||
row.note = "chat: pending failed".into();
|
||||
}
|
||||
}
|
||||
// Her projects' names address her too; read only when something is pending. A failed read keeps her last-read row.
|
||||
if !row.pending.is_empty() {
|
||||
|
|
@ -785,16 +571,12 @@ pub fn poll(organs: &Organs, config: &Config) -> Result<Outcome> {
|
|||
rows.push(row);
|
||||
}
|
||||
|
||||
// The tempo is the tally over the votes cast by the Minds of the roster, braked or not; a Mind with no vote or no readable mind abstains and is not counted.
|
||||
let votes: Vec<Option<u32>> = rows.iter().map(|row| row.read.as_ref().and_then(|read| read.vote)).collect();
|
||||
let beat_s = tempo::tally(&votes);
|
||||
|
||||
let beat_s = config.beat_s;
|
||||
let mut launches: Vec<String> = Vec::new();
|
||||
let mut launched: Vec<Slug> = Vec::new();
|
||||
|
||||
// Off the beat: the operator or another Mind of the roster naming her, once per message, and her Self run
|
||||
// having ended since her last turn. Neither waits for the beat; both need her
|
||||
// idle and released.
|
||||
// Off the beat: the operator or another Mind of the roster naming her, once per message. It does not wait for the beat;
|
||||
// it needs her idle and released.
|
||||
for row in &mut rows {
|
||||
if !row.idle() {
|
||||
continue;
|
||||
|
|
@ -809,66 +591,60 @@ pub fn poll(organs: &Organs, config: &Config) -> Result<Outcome> {
|
|||
.map(|delivery| format!("{}:{}", slug.0, delivery.event_id))
|
||||
.find(|key| !state.handled_mentions.contains(key))
|
||||
};
|
||||
let self_ended = row.read.as_ref().is_some_and(|read| match (read.self_end, read.last_turn) {
|
||||
(Some(ended), Some(turn)) => ended > turn,
|
||||
(Some(_), None) => true,
|
||||
_ => false,
|
||||
});
|
||||
if mentioned.is_none() && !self_ended {
|
||||
continue;
|
||||
}
|
||||
match launch(organs, row, RunTurn::PersonaTurn, vec![])? {
|
||||
Launched::Run(label) => {
|
||||
if let Some(key) = mentioned {
|
||||
state.handled_mentions.push(key);
|
||||
let excess = state.handled_mentions.len().saturating_sub(MENTIONS_KEPT);
|
||||
state.handled_mentions.drain(..excess);
|
||||
}
|
||||
let Some(key) = mentioned else { continue };
|
||||
match wake(organs, row, RunTurn::PersonaTurn) {
|
||||
Woke::Started => {
|
||||
state.handled_mentions.push(key);
|
||||
let excess = state.handled_mentions.len().saturating_sub(MENTIONS_KEPT);
|
||||
state.handled_mentions.drain(..excess);
|
||||
state.last_turn_at.insert(slug.0.clone(), now);
|
||||
row.busy = true;
|
||||
launches.push(label);
|
||||
launches.push(wake_label(&slug, RunTurn::PersonaTurn));
|
||||
launched.push(slug);
|
||||
}
|
||||
Launched::Busy => row.busy = true,
|
||||
Launched::Refused(why) => row.note = why,
|
||||
Woke::Busy => row.busy = true,
|
||||
Woke::Refused(why) => row.note = why,
|
||||
}
|
||||
}
|
||||
|
||||
// The night: inside the window, one Sleep run for a listed Mind who is idle and released and has not slept
|
||||
// since the window opened (a failed night counts). The operator's mention went first above: she is busy then.
|
||||
// A window that passes without a night is not made up.
|
||||
// The night: inside the window, one night for a listed Mind who is idle and released and has not been started for one
|
||||
// since the window opened. The operator's mention went first above: she is busy then. A window that passes without a
|
||||
// night is not made up.
|
||||
if let Some(night) = &config.night
|
||||
&& let Some(opened) = night.opened(now)
|
||||
{
|
||||
for row in &mut rows {
|
||||
if !row.idle() || !night.minds.contains(row.slug()) || row.read.as_ref().is_some_and(|read| read.last_sleep.is_some_and(|slept| slept >= opened)) {
|
||||
let slept = state.last_night_at.get(&row.slug().0).is_some_and(|slept| *slept >= opened);
|
||||
if !row.idle() || !night.minds.contains(row.slug()) || slept {
|
||||
continue;
|
||||
}
|
||||
match launch(organs, row, RunTurn::Sleep, vec![])? {
|
||||
Launched::Run(label) => {
|
||||
row.note = format!("asleep {label}");
|
||||
match wake(organs, row, RunTurn::Sleep) {
|
||||
Woke::Started => {
|
||||
let slug = row.slug().clone();
|
||||
state.last_night_at.insert(slug.0.clone(), now);
|
||||
row.note = "asleep".into();
|
||||
row.busy = true;
|
||||
launched.push(row.slug().clone());
|
||||
launches.push(label);
|
||||
launches.push(wake_label(&slug, RunTurn::Sleep));
|
||||
launched.push(slug);
|
||||
break;
|
||||
}
|
||||
Launched::Busy => row.busy = true,
|
||||
Launched::Refused(why) => row.note = why,
|
||||
Woke::Busy => row.busy = true,
|
||||
Woke::Refused(why) => row.note = why,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// On the beat: when the beat has passed, one launch for the winner of the pick.
|
||||
// On the beat: when the beat has passed, one wake for the winner of the pick.
|
||||
let due = (now - state.last_beat_at).num_seconds() >= i64::from(beat_s);
|
||||
let standings: Vec<Standing> = rows
|
||||
.iter()
|
||||
.map(|row| {
|
||||
let read = row.read.as_ref();
|
||||
let last = state.last_turn_at.get(&row.slug().0).copied();
|
||||
Standing {
|
||||
slug: row.slug().0.clone(),
|
||||
eligible: row.idle(),
|
||||
waited_s: read.and_then(|read| read.last_turn).map(|turn| u64::try_from((now - turn).num_seconds()).unwrap_or(0)),
|
||||
last_turn_ms: read.and_then(|read| read.last_turn).map(|turn| turn.timestamp_millis()),
|
||||
self_permille: read.map_or(0, |read| read.urgency),
|
||||
waited_s: last.map(|turn| u64::try_from((now - turn).num_seconds()).unwrap_or(0)),
|
||||
last_turn_ms: last.map(|turn| turn.timestamp_millis()),
|
||||
// The seam for topic subscriptions (a later cut): a Mind's nearest subscription scores her deliveries here.
|
||||
topic_permille: 0,
|
||||
addressed_by_member: row.addressed_by_member(config),
|
||||
|
|
@ -876,51 +652,21 @@ pub fn poll(organs: &Organs, config: &Config) -> Result<Outcome> {
|
|||
})
|
||||
.collect();
|
||||
// A winner whose unit cannot start loses the beat to the next of the pick, so
|
||||
// one failing Mind never starves the others; a launch ends the beat.
|
||||
// one failing Mind never starves the others; a wake ends the beat.
|
||||
let mut passed: Vec<Standing> = standings.clone();
|
||||
while due && let Some(winner) = tempo::pick(&passed, beat_s) {
|
||||
// Her Self holds briefs: the beat is a Self run for them; otherwise a Persona turn.
|
||||
let Claimable {
|
||||
claims,
|
||||
held,
|
||||
dead,
|
||||
} = claimable(organs, &rows[winner]).unwrap_or_else(|error| {
|
||||
rows[winner].note = format!("briefs: {error}");
|
||||
Claimable {
|
||||
claims: Vec::new(),
|
||||
held: 0,
|
||||
dead: Vec::new(),
|
||||
}
|
||||
});
|
||||
let mut notes: Vec<String> = Vec::new();
|
||||
if held > 0 {
|
||||
notes.push(format!("{held} briefs held by open questions"));
|
||||
}
|
||||
if !claims.is_empty() && !dead.is_empty() {
|
||||
let ids: Vec<&str> = dead.iter().map(|run| run.id.0.as_str()).collect();
|
||||
notes.push(format!(
|
||||
"closing the Self run whose unit is gone: {}",
|
||||
ids.join(", ")
|
||||
));
|
||||
}
|
||||
if !notes.is_empty() {
|
||||
rows[winner].note = notes.join("; ");
|
||||
}
|
||||
let turn = if claims.is_empty() {
|
||||
RunTurn::PersonaTurn
|
||||
} else {
|
||||
RunTurn::SelfRun
|
||||
};
|
||||
match launch(organs, &rows[winner], turn, claims)? {
|
||||
Launched::Run(label) => {
|
||||
match wake(organs, &rows[winner], RunTurn::PersonaTurn) {
|
||||
Woke::Started => {
|
||||
let slug = rows[winner].slug().clone();
|
||||
state.last_beat_at = now;
|
||||
rows[winner].busy = turn == RunTurn::PersonaTurn;
|
||||
launches.push(label);
|
||||
launched.push(rows[winner].slug().clone());
|
||||
state.last_turn_at.insert(slug.0.clone(), now);
|
||||
rows[winner].busy = true;
|
||||
launches.push(wake_label(&slug, RunTurn::PersonaTurn));
|
||||
launched.push(slug);
|
||||
break;
|
||||
}
|
||||
Launched::Busy => rows[winner].busy = true,
|
||||
Launched::Refused(why) => rows[winner].note = why,
|
||||
Woke::Busy => rows[winner].busy = true,
|
||||
Woke::Refused(why) => rows[winner].note = why,
|
||||
}
|
||||
passed[winner].eligible = false;
|
||||
}
|
||||
|
|
@ -948,8 +694,8 @@ pub fn poll(organs: &Organs, config: &Config) -> Result<Outcome> {
|
|||
.zip(&standings)
|
||||
.map(|(row, standing)| InstanceTrace {
|
||||
instance: row.slug().0.clone(),
|
||||
vote_beat_s: row.read.as_ref().and_then(|read| read.vote),
|
||||
urgency_permille: standing.self_permille,
|
||||
vote_beat_s: None,
|
||||
urgency_permille: tempo::urgency_permille(standing),
|
||||
addressed: standing.addressed_by_member,
|
||||
busy: row.busy,
|
||||
released: row.released,
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -8,14 +8,13 @@
|
|||
//! repetition breaker, the one-live-Self-run rule, and the single path that
|
||||
//! opens a run in the mind and only then starts its unit.
|
||||
//!
|
||||
//! `heartbeat` runs the swarm's shared heartbeat over them: the one process,
|
||||
//! the `mind-heartbeat` binary, that decides who wakes and when, by the rule in
|
||||
//! `tempo` (the Minds' votes make the beat; the pick chooses who takes it).
|
||||
//! `heartbeat` runs the swarm's shared heartbeat: the one process, the
|
||||
//! `mind-heartbeat` binary, that decides which Persona wakes and when, on the
|
||||
//! configured beat by the pick in `tempo`. It starts her unit and writes no mind.
|
||||
//!
|
||||
//! `persona` is the Persona organ: one start of `mind-persona@<instance>` carries her live
|
||||
//! `claude` session under bubblewrap in her sandbox, driven by the Self's own driver, with her
|
||||
//! pending chat as the inbox and one typed reply per result (`chat_client` is the chat wire and
|
||||
//! port and `chat_rudp` its RUDP transport to Bifrost's mouth).
|
||||
//! `persona` is the Persona organ: one start of `mind-persona@<instance>` reads the brakes, and
|
||||
//! until the three-stage turn lands it carries no turn. It keeps what a turn's input is made of
|
||||
//! (`chat_client` is the chat wire and port and `chat_rudp` its RUDP transport to Bifrost's mouth).
|
||||
//!
|
||||
//! `lease_client` asks the Claude connector for a session's credential and the
|
||||
//! plan's usage windows. `self_run` carries one Self run inside `mind-self@<instance>`: the
|
||||
|
|
@ -31,8 +30,8 @@
|
|||
#![forbid(unsafe_code)]
|
||||
|
||||
mod address;
|
||||
mod attachment;
|
||||
pub mod chat_client;
|
||||
#[allow(dead_code, reason = "the loader is kept for the three-stage turn's perception (map C3): its first caller removes this allow; `Image` is live through `self_run::session`")]
|
||||
mod chat_image;
|
||||
pub mod chat_rudp;
|
||||
pub mod cli;
|
||||
|
|
@ -43,9 +42,9 @@ pub mod launch;
|
|||
pub mod lease_client;
|
||||
pub mod persona;
|
||||
pub mod queue;
|
||||
#[allow(dead_code, reason = "kept for the three-stage turn's Select stage (map C3 reuses recall.rs): its first caller removes this allow")]
|
||||
mod recall;
|
||||
pub mod self_run;
|
||||
mod sleep;
|
||||
pub mod tempo;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -3,9 +3,7 @@
|
|||
//! 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: `persona::render_responsibilities` shows them
|
||||
//! on every turn beside her stewardships, because they stay in front of her
|
||||
//! until a resolution closes them. This module owns the cued, ranked remainder
|
||||
//! commitments are not recalled. This module owns the cued, ranked remainder
|
||||
//! and nothing else. It reads only her own mind through `MindPort`; it reads no
|
||||
//! chat (the lines shown beside a message are conversation, never memory),
|
||||
//! keeps no cache between turns, and holds no index: the daemon's index answers
|
||||
|
|
|
|||
|
|
@ -144,12 +144,3 @@ fn a_credential_split_by_the_reply_limit_is_redacted_before_the_cut() {
|
|||
assert!(reply_of(&mind, 2).is_some(), "the credential's place straddles the cut");
|
||||
assert!(!first.text.0.contains("CANARY") && !reply_of(&mind, 2).unwrap().text.0.contains("CANARY"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn persona_and_sleep_structured_output_is_redacted_string_by_string() {
|
||||
let line = json!({"type": "result", "structured_output": {"say": format!("https://u:{CANARY}@h/x,"), "n": 3, "list": [format!("https://{CANARY}@h")]}}).to_string();
|
||||
let text = crate::persona::structured_output(&line).unwrap();
|
||||
assert!(!text.contains(CANARY), "{text}");
|
||||
let back: Value = serde_json::from_str(&text).unwrap();
|
||||
assert_eq!(back, json!({"say": "https://<redacted>@h/x,", "n": 3, "list": ["https://<redacted>@h"]}));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,626 +0,0 @@
|
|||
//! A Mind's night. Once in the heartbeat's window a Sleep run opens in her Persona slot (the heartbeat decides
|
||||
//! who and when); this module is the one organ that carries it: one session over the same prelude her waking
|
||||
//! session starts with, told her day whole, answering with a [`SleepReply`] that this module turns into
|
||||
//! admissions. The run is the night's memory consolidation and nothing else (rulings
|
||||
//! `eureka-body:ruling:sleep-day-coverage-ruled` and `eureka-body:ruling:contemplation-own-session`: contemplation
|
||||
//! is a session of its own, never this run).
|
||||
//!
|
||||
//! What the code owns is the ledger of her day. Every memory of it is disposed of exactly once (kept, consumed by
|
||||
//! a consolidation, or retired); an id is one of her memories in force; a consolidation merges one conversation's
|
||||
//! memories only and supersedes each source in the batch that admits it (admission refuses the unpaired). Code
|
||||
//! never chooses what to merge and never writes a memory's text. Where the night left her day is its `Slept`
|
||||
//! closing, never words. A night is one process: a missed night is not made up and a failed night is not retried.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
use cultnet_rs::Selection;
|
||||
use eureka_pipeline::{Label, Line, MemoryOrigin, MemorySort, PipelineDocument, PipelineKind, PipelineMemory, PipelineRef, PipelineResolution, ResolutionOutcome, SALIENCE_MAX_PERMILLE, Short};
|
||||
use huginn_mind::{BATCH_MAX, PipelineAdmissionOutcome, PipelinePageItems};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::control::ControlSource;
|
||||
use crate::persona::{
|
||||
ABOUT_MAX, AGENT, Config, Conversation, Failure, Organs, Outcome, PERSONA_POLL_S, REMEMBER_TEXT_MAX_BYTES, Remember, SOURCES_MAX, Started, WHY_MAX_CHARS, date, label_of, next_sequence, remember_item_schema, start_session,
|
||||
structured_output,
|
||||
};
|
||||
use crate::queue::{MindPort, any_of, citing, headers, in_force, of_kind};
|
||||
use crate::recall;
|
||||
use crate::self_run::session::{Polled, SessionInbox, drive};
|
||||
use crate::self_run::{SESSION_NAMESPACE, Why};
|
||||
|
||||
/// The longest a night runs, whatever the unit's cap.
|
||||
pub(crate) const SLEEP_CAP_S: u64 = 3600;
|
||||
/// The most memories of her day one night line shows; the rest are older by the next night.
|
||||
pub(crate) const DAY_MAX: usize = 96;
|
||||
/// The most open Asked and Committed items older than her day the line shows.
|
||||
const OPEN_MAX: usize = 48;
|
||||
/// The older memories of other sorts one night shows, and the step the tide moves each night.
|
||||
const TIDE: usize = 32;
|
||||
const NIGHT_MAX_BYTES: usize = 900;
|
||||
const CONSOLIDATE_MAX: usize = 32;
|
||||
const CONSUMES_MAX: usize = 16;
|
||||
const RETIRE_MAX: usize = 128;
|
||||
const REMEMBER_MAX: usize = 16;
|
||||
const REFUSALS_PER_NIGHT: u32 = 3;
|
||||
|
||||
/// What the night tells her, in her second person: consolidate the day, nothing else.
|
||||
pub(crate) const SLEEP_GUIDANCE: &str = "\
|
||||
Tonight you consolidate your memory. Your day is shown whole above. Every memory of it gets exactly one disposition: keep it (name it in `keep`), fold it into a consolidation (name it in that consolidation's `consumes`), or retire it with an outcome and a reason (`retire`: done, declined, faded or wrong). A consolidation is one new memory in your words that takes the place of the memories it consumes, so write it to carry what those memories were for: the concrete target, the claim or the question, the live tension, why it changes what you do next, and the anchors (ids, names, places) it rests on. It reduces bulk, not meaning. Consolidate only memories of one conversation; a private conversation stays its own. Anything else you want to remember goes in `remember`.
|
||||
|
||||
Older items shown, and any other memory of yours in force, are yours to touch or leave. Name every memory by its id exactly as shown, once in the whole reply.
|
||||
|
||||
End with one line on what you did with your memory tonight (`night`).";
|
||||
|
||||
/// The night's reply, as the one type decoded. Her day's ledger is `keep`, `consolidate` and `retire`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct SleepReply {
|
||||
pub(crate) night: String,
|
||||
pub(crate) keep: Vec<String>,
|
||||
pub(crate) consolidate: Vec<Consolidate>,
|
||||
pub(crate) retire: Vec<Retire>,
|
||||
pub(crate) remember: Vec<Remember>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct Consolidate {
|
||||
pub(crate) consumes: Vec<String>,
|
||||
pub(crate) sort: MemorySort,
|
||||
pub(crate) text: String,
|
||||
pub(crate) about: Vec<String>,
|
||||
pub(crate) salience_permille: u16,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct Retire {
|
||||
pub(crate) item: String,
|
||||
pub(crate) outcome: RetireOutcome,
|
||||
pub(crate) why: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub(crate) enum RetireOutcome {
|
||||
Done,
|
||||
Declined,
|
||||
Faded,
|
||||
Wrong,
|
||||
}
|
||||
|
||||
/// The JSON Schema of [`SleepReply`], with the bounds the decoder holds.
|
||||
pub(crate) fn sleep_schema() -> String {
|
||||
let id = json!({ "type": "string" });
|
||||
let remember = |maximum: usize| json!({ "type": "array", "maxItems": maximum, "items": remember_item_schema() });
|
||||
let mut consolidate = remember_item_schema();
|
||||
consolidate["required"] = json!(["consumes", "sort", "text", "about", "salience_permille"]);
|
||||
consolidate["properties"]["consumes"] = json!({ "type": "array", "minItems": 1, "maxItems": CONSUMES_MAX, "uniqueItems": true, "items": id });
|
||||
json!({
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["night", "keep", "consolidate", "retire", "remember"],
|
||||
"properties": {
|
||||
"night": { "type": "string", "minLength": 1, "description": format!("One line on what you did with your memory tonight. At most {NIGHT_MAX_BYTES} bytes.") },
|
||||
"keep": { "type": "array", "uniqueItems": true, "items": id },
|
||||
"consolidate": { "type": "array", "maxItems": CONSOLIDATE_MAX, "items": consolidate },
|
||||
"retire": {
|
||||
"type": "array",
|
||||
"maxItems": RETIRE_MAX,
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["item", "outcome", "why"],
|
||||
"properties": {
|
||||
"item": id,
|
||||
"outcome": { "type": "string", "enum": ["done", "declined", "faded", "wrong"] },
|
||||
"why": { "type": "string", "minLength": 1, "maxLength": WHY_MAX_CHARS }
|
||||
}
|
||||
}
|
||||
},
|
||||
"remember": remember(REMEMBER_MAX)
|
||||
}
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// One of her memories in force, with when it was admitted.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Held {
|
||||
pub(crate) id: String,
|
||||
pub(crate) memory: PipelineMemory,
|
||||
pub(crate) at: DateTime<Utc>,
|
||||
pub(crate) ordinal: u64,
|
||||
}
|
||||
|
||||
/// What a night is told and what its reply may name: derived from her mind and nothing stored.
|
||||
pub(crate) struct Context {
|
||||
pub(crate) now: DateTime<Utc>,
|
||||
pub(crate) label: String,
|
||||
pub(crate) counts: BTreeMap<String, usize>,
|
||||
pub(crate) day: Vec<Held>,
|
||||
pub(crate) open: Vec<Held>,
|
||||
pub(crate) tide: Vec<Held>,
|
||||
/// Every memory of hers in force, by id.
|
||||
pub(crate) all: BTreeMap<String, Held>,
|
||||
}
|
||||
|
||||
fn parse_time(at: &str) -> Result<DateTime<Utc>> {
|
||||
Ok(DateTime::parse_from_rfc3339(at).map_err(|_| anyhow!("an admission time could not be read"))?.with_timezone(&Utc))
|
||||
}
|
||||
|
||||
/// Every document a selection holds, paged whole.
|
||||
fn documents(mind: &dyn MindPort, selection: &Selection) -> Result<Vec<huginn_mind::PipelineDocumentView>> {
|
||||
let mut found = Vec::new();
|
||||
let mut asking = Selection { projection: "document".into(), ..selection.clone() };
|
||||
loop {
|
||||
let page = mind.query(&asking)?;
|
||||
let PipelinePageItems::Documents(views) = page.items else { bail!("the mind answered headers to a document selection") };
|
||||
found.extend(views);
|
||||
match page.next {
|
||||
Some(cursor) => asking.cursor = Some(cursor),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
/// Her memories in force, oldest admitted first: the one read of what she holds.
|
||||
fn memories_in_force(mind: &dyn MindPort) -> Result<Vec<Held>> {
|
||||
let mut found = Vec::new();
|
||||
for view in documents(mind, &in_force(of_kind(PipelineKind::Memory)))? {
|
||||
if let PipelineDocument::Memory(memory) = view.document {
|
||||
found.push(Held { id: view.id.id.0, memory, at: parse_time(&view.admission.admitted_at)?, ordinal: view.admission.ordinal });
|
||||
}
|
||||
}
|
||||
found.sort_by_key(|held| held.ordinal);
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
/// One of her Sleep runs: which, when it was admitted, and its admission ordinal.
|
||||
struct Night {
|
||||
run: PipelineRef,
|
||||
at: DateTime<Utc>,
|
||||
ordinal: u64,
|
||||
}
|
||||
|
||||
/// Her Sleep runs, any status, oldest first.
|
||||
fn sleeps(mind: &dyn MindPort) -> Result<Vec<Night>> {
|
||||
let runs = any_of(any_of(any_of(of_kind(PipelineKind::Run), "root", &[&mind.instance().0]), "turn", &["Sleep"]), "operated_by", &["Mind"]);
|
||||
let mut found = headers(mind, &runs)?.into_iter().map(|run| Ok(Night { run: run.id, ordinal: run.admission.ordinal, at: parse_time(&run.admission.admitted_at)? })).collect::<Result<Vec<_>>>()?;
|
||||
found.sort_by_key(|night| night.ordinal);
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
/// Where the newest of `earlier` whose closing in force is `Slept` left her day: the admission ordinal of the
|
||||
/// memory it names as the last of the day it was shown, in force or not (its own run's ordinal when it was shown
|
||||
/// none). A night with no such closing (it failed, its run never closed, a hand closed it, or its `Slept` closing
|
||||
/// was withdrawn) left its day uncovered, so it is not an anchor.
|
||||
fn covered_through(mind: &dyn MindPort, earlier: &[&Night]) -> Result<Option<u64>> {
|
||||
for night in earlier.iter().rev() {
|
||||
let closings = in_force(any_of(Selection { cites: Some(citing(&night.run, Some("subject"))), ..of_kind(PipelineKind::Resolution) }, "outcome", &["Slept"]));
|
||||
let slept = documents(mind, &closings)?.into_iter().find_map(|view| match view.document {
|
||||
PipelineDocument::Resolution(PipelineResolution { outcome: ResolutionOutcome::Slept { day_through, .. }, .. }) => Some(day_through),
|
||||
_ => None,
|
||||
});
|
||||
let Some(through) = slept else { continue };
|
||||
let Some(memory) = through else { return Ok(Some(night.ordinal)) };
|
||||
let named = Selection { keys: Some(vec![memory.id.0.clone()]), ..of_kind(PipelineKind::Memory) };
|
||||
let held = headers(mind, &named)?.into_iter().next().ok_or_else(|| anyhow!("the last memory a night was shown could not be read"))?;
|
||||
return Ok(Some(held.admission.ordinal));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn is_open_sort(sort: MemorySort) -> bool {
|
||||
matches!(sort, MemorySort::Asked | MemorySort::Committed)
|
||||
}
|
||||
|
||||
/// Made by a night, not lived in the day: formed in sleep, or a consolidation (a DM's keeps its room and voices,
|
||||
/// so its origin is not Sleep).
|
||||
fn is_sleep_made(held: &Held) -> bool {
|
||||
matches!(held.memory.origin, MemoryOrigin::Sleep { .. }) || !held.memory.consolidates.is_empty()
|
||||
}
|
||||
|
||||
/// The night, derived. Her day: her memories in force admitted after what her newest night whose closing in force is `Slept`
|
||||
/// covered (a night that failed covered nothing), else in the 24 hours before her first night opened; formed
|
||||
/// otherwise than in sleep, oldest first, at most `DAY_MAX`, the rest waiting for the next night. Her open Asked
|
||||
/// and Committed items older than the day. A tide of `TIDE` older memories of other sorts, from an offset that
|
||||
/// moves by `TIDE` with each earlier Sleep run of hers.
|
||||
pub(crate) fn context(mind: &dyn MindPort, run: &PipelineRef, now: DateTime<Utc>) -> Result<Context> {
|
||||
let nights = sleeps(mind)?;
|
||||
let tonight = nights.iter().find(|night| &night.run == run).ok_or_else(|| anyhow!("the night's run is not in her mind"))?;
|
||||
let earlier: Vec<&Night> = nights.iter().filter(|night| night.ordinal < tonight.ordinal).collect();
|
||||
let covered = covered_through(mind, &earlier)?;
|
||||
let first_at = earlier.first().map_or(tonight.at, |night| night.at);
|
||||
let in_day = |held: &Held| covered.map_or(held.at > first_at - Duration::hours(24), |through| held.ordinal > through);
|
||||
let memories = memories_in_force(mind)?;
|
||||
let mut counts = BTreeMap::new();
|
||||
for held in &memories {
|
||||
*counts.entry(format!("{:?}", held.memory.sort)).or_insert(0) += 1;
|
||||
}
|
||||
let day: Vec<Held> = memories.iter().filter(|held| in_day(held) && !is_sleep_made(held)).take(DAY_MAX).cloned().collect();
|
||||
let open: Vec<Held> = memories.iter().filter(|held| !in_day(held) && is_open_sort(held.memory.sort)).take(OPEN_MAX).cloned().collect();
|
||||
let backlog: Vec<&Held> = memories.iter().filter(|held| !in_day(held) && !is_open_sort(held.memory.sort) && !is_sleep_made(held)).collect();
|
||||
let tide: Vec<Held> = if backlog.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
let offset = earlier.len() * TIDE % backlog.len();
|
||||
(0..TIDE.min(backlog.len())).map(|step| backlog[(offset + step) % backlog.len()].clone()).collect()
|
||||
};
|
||||
let all = memories.into_iter().map(|held| (held.id.clone(), held)).collect();
|
||||
Ok(Context { now, label: label_of(run).to_string(), counts, day, open, tide, all })
|
||||
}
|
||||
|
||||
fn age(now: DateTime<Utc>, at: DateTime<Utc>) -> String {
|
||||
let minutes = (now - at).num_minutes().max(0);
|
||||
match minutes {
|
||||
0..=59 => format!("{minutes}m"),
|
||||
60..=2879 => format!("{}h", minutes / 60),
|
||||
_ => format!("{}d", minutes / 1440),
|
||||
}
|
||||
}
|
||||
|
||||
fn entry(now: DateTime<Utc>, held: &Held) -> String {
|
||||
let memory = &held.memory;
|
||||
let about = if memory.about.is_empty() { String::new() } else { format!("; about {}", memory.about.iter().map(|who| who.0.as_str()).collect::<Vec<_>>().join(", ")) };
|
||||
let sources = if memory.sources.is_empty() { String::new() } else { format!("; sources {}", memory.sources.iter().map(|source| source.0.as_str()).collect::<Vec<_>>().join(", ")) };
|
||||
format!(
|
||||
"- {} [{:?}, {} ago, salience {}] {}{about}{sources}\n {}\n",
|
||||
held.id,
|
||||
memory.sort,
|
||||
age(now, held.at),
|
||||
memory.salience_permille,
|
||||
recall::origin_phrase(&memory.origin, &BTreeMap::new()),
|
||||
memory.text.0
|
||||
)
|
||||
}
|
||||
|
||||
/// The night line: the date, her counts in force, her day whole, her open items, the tide, and the guidance.
|
||||
pub(crate) fn night_line(context: &Context) -> String {
|
||||
let now = context.now;
|
||||
let counts = if context.counts.is_empty() { "none".to_string() } else { context.counts.iter().map(|(sort, count)| format!("{sort} {count}")).collect::<Vec<_>>().join(", ") };
|
||||
let mut line = format!("It is night, {} UTC.\n\nYour memories in force: {counts}.\n\n", now.format("%Y-%m-%d %H:%M"));
|
||||
if context.day.is_empty() {
|
||||
line.push_str("Your day: nothing new has been admitted since your last night.\n\n");
|
||||
} else {
|
||||
line.push_str(&format!("Your day, whole, oldest first ({} memories):\n", context.day.len()));
|
||||
context.day.iter().for_each(|held| line.push_str(&entry(now, held)));
|
||||
line.push('\n');
|
||||
}
|
||||
if !context.open.is_empty() {
|
||||
line.push_str(&format!("Your open asks and commitments from before your day, oldest first ({}):\n", context.open.len()));
|
||||
context.open.iter().for_each(|held| line.push_str(&entry(now, held)));
|
||||
line.push('\n');
|
||||
}
|
||||
if !context.tide.is_empty() {
|
||||
line.push_str(&format!("Older memories of yours, as they come round ({}):\n", context.tide.len()));
|
||||
context.tide.iter().for_each(|held| line.push_str(&entry(now, held)));
|
||||
line.push('\n');
|
||||
}
|
||||
line.push_str(SLEEP_GUIDANCE);
|
||||
line
|
||||
}
|
||||
|
||||
fn bytes_fault(field: &str, text: &str, most: usize) -> Option<String> {
|
||||
(text.trim().is_empty() || text.len() > most).then(|| format!("{field}: empty, or over {most} bytes"))
|
||||
}
|
||||
|
||||
/// The reply decoded and held to the ledger. The fault is told back to her whole.
|
||||
pub(crate) fn decode(text: &str, context: &Context) -> std::result::Result<SleepReply, String> {
|
||||
let reply: SleepReply = serde_json::from_str(text).map_err(|error| format!("the reply is not a SleepReply: {error}"))?;
|
||||
let mut faults: Vec<String> = Vec::new();
|
||||
faults.extend(bytes_fault("night", &reply.night, NIGHT_MAX_BYTES));
|
||||
if reply.consolidate.len() > CONSOLIDATE_MAX || reply.retire.len() > RETIRE_MAX || reply.remember.len() > REMEMBER_MAX {
|
||||
faults.push(format!("at most {CONSOLIDATE_MAX} consolidations, {RETIRE_MAX} retirements and {REMEMBER_MAX} remembered"));
|
||||
}
|
||||
let shown: BTreeSet<String> = context.day.iter().chain(&context.open).chain(&context.tide).flat_map(|held| recall::people_of(&held.memory)).collect();
|
||||
let about_fault = |about: &[String]| about.len() > ABOUT_MAX || about.iter().any(|who| !shown.contains(who));
|
||||
for consolidation in &reply.consolidate {
|
||||
if consolidation.consumes.is_empty() || consolidation.consumes.len() > CONSUMES_MAX {
|
||||
faults.push(format!("consumes: between 1 and {CONSUMES_MAX} ids"));
|
||||
}
|
||||
faults.extend(bytes_fault("consolidate.text", &consolidation.text, REMEMBER_TEXT_MAX_BYTES));
|
||||
if consolidation.salience_permille > SALIENCE_MAX_PERMILLE || about_fault(&consolidation.about) {
|
||||
faults.push("consolidate.about or salience_permille: out of bounds (about is an id of a person a memory shown to you names)".into());
|
||||
}
|
||||
let conversations: Vec<Conversation> = consolidation.consumes.iter().filter_map(|id| context.all.get(id)).map(|held| Conversation::of_origin(Some(&held.memory.origin))).collect();
|
||||
if conversations.windows(2).any(|pair| pair[0] != pair[1]) {
|
||||
faults.push(format!("a consolidation merges the memories of one conversation only: {} are of two", consolidation.consumes.join(", ")));
|
||||
}
|
||||
}
|
||||
for retirement in &reply.retire {
|
||||
if retirement.why.trim().is_empty() || retirement.why.chars().count() > WHY_MAX_CHARS {
|
||||
faults.push(format!("retire {}: why is empty or over {WHY_MAX_CHARS} characters", retirement.item));
|
||||
}
|
||||
}
|
||||
for remembered in &reply.remember {
|
||||
faults.extend(bytes_fault("remember.text", &remembered.text, REMEMBER_TEXT_MAX_BYTES));
|
||||
if remembered.salience_permille > SALIENCE_MAX_PERMILLE || about_fault(&remembered.about) {
|
||||
faults.push("remember.about or salience_permille: out of bounds (about is an id of a person a memory shown to you names)".into());
|
||||
}
|
||||
}
|
||||
let named: Vec<&String> = reply.keep.iter().chain(reply.consolidate.iter().flat_map(|consolidation| &consolidation.consumes)).chain(reply.retire.iter().map(|retirement| &retirement.item)).collect();
|
||||
let mut seen = BTreeSet::new();
|
||||
for id in &named {
|
||||
if !seen.insert(id.as_str()) {
|
||||
faults.push(format!("{id} is named more than once; every memory is named once in the whole reply"));
|
||||
}
|
||||
}
|
||||
for id in &named {
|
||||
if !context.all.contains_key(id.as_str()) {
|
||||
faults.push(format!("{id} is not a memory of yours in force"));
|
||||
}
|
||||
}
|
||||
let missing: Vec<&str> = context.day.iter().map(|held| held.id.as_str()).filter(|id| !seen.contains(id)).collect();
|
||||
if !missing.is_empty() {
|
||||
faults.push(format!("your day is not covered; dispose of each of these in keep, consumes or retire: {}", missing.join(", ")));
|
||||
}
|
||||
if faults.is_empty() { Ok(reply) } else { Err(faults.join("\n")) }
|
||||
}
|
||||
|
||||
fn reference(id: &str) -> PipelineRef {
|
||||
PipelineRef { kind: PipelineKind::Memory, id: Short(id.to_string()) }
|
||||
}
|
||||
|
||||
/// The reply as whole operations, each a unit of documents that stands or falls together: a consolidation (its
|
||||
/// memory and a Superseded resolution of each memory it consumes), a retirement, a remembered memory.
|
||||
pub(crate) fn operations(mind: &dyn MindPort, context: &Context, reply: &SleepReply) -> Result<Vec<Vec<PipelineDocument>>> {
|
||||
let instance = mind.instance().clone();
|
||||
let on = date(context.now);
|
||||
let run = Short(context.label.clone());
|
||||
let mut units = Vec::new();
|
||||
for (index, consolidation) in reply.consolidate.iter().enumerate() {
|
||||
let mut consumed: Vec<&Held> = consolidation.consumes.iter().filter_map(|id| context.all.get(id)).collect();
|
||||
consumed.sort_by_key(|held| held.ordinal);
|
||||
let origin = match Conversation::of_origin(consumed.first().map(|held| &held.memory.origin)) {
|
||||
Conversation::Direct(room) => {
|
||||
let mut with: Vec<Short> = Vec::new();
|
||||
for who in consumed.iter().flat_map(|held| match &held.memory.origin {
|
||||
MemoryOrigin::Direct { with, .. } => with.clone(),
|
||||
_ => Vec::new(),
|
||||
}) {
|
||||
if !with.contains(&who) && with.len() < ABOUT_MAX {
|
||||
with.push(who);
|
||||
}
|
||||
}
|
||||
MemoryOrigin::Direct { room: Short(room), with }
|
||||
}
|
||||
Conversation::Public => MemoryOrigin::Sleep { run: run.clone() },
|
||||
};
|
||||
let mut sources: Vec<Short> = Vec::new();
|
||||
for source in consumed.iter().rev().flat_map(|held| held.memory.sources.iter().rev()) {
|
||||
if !sources.contains(source) && sources.len() < SOURCES_MAX {
|
||||
sources.push(source.clone());
|
||||
}
|
||||
}
|
||||
let label = Label(format!("{}-c{index}", context.label));
|
||||
let memory = PipelineMemory {
|
||||
instance: instance.clone(),
|
||||
label: label.clone(),
|
||||
sort: consolidation.sort,
|
||||
text: Line(consolidation.text.clone()),
|
||||
about: consolidation.about.iter().cloned().map(Short).collect(),
|
||||
origin,
|
||||
sources,
|
||||
salience_permille: consolidation.salience_permille,
|
||||
consolidates: consolidation.consumes.iter().map(|id| reference(id)).collect(),
|
||||
};
|
||||
let by = reference(&format!("{}:memory:{}", instance.0, label.0));
|
||||
let mut unit = vec![PipelineDocument::Memory(memory)];
|
||||
for id in &consolidation.consumes {
|
||||
let subject = reference(id);
|
||||
unit.push(PipelineDocument::Resolution(PipelineResolution {
|
||||
sequence: next_sequence(mind, &subject)?,
|
||||
subject,
|
||||
outcome: ResolutionOutcome::Superseded { by: vec![by.clone()] },
|
||||
rationale: "Consolidated in sleep.".into(),
|
||||
resolved_on: on.clone(),
|
||||
}));
|
||||
}
|
||||
units.push(unit);
|
||||
}
|
||||
for retirement in &reply.retire {
|
||||
let subject = reference(&retirement.item);
|
||||
let reason = Line(retirement.why.clone());
|
||||
units.push(vec![PipelineDocument::Resolution(PipelineResolution {
|
||||
sequence: next_sequence(mind, &subject)?,
|
||||
subject,
|
||||
outcome: match retirement.outcome {
|
||||
RetireOutcome::Done | RetireOutcome::Faded => ResolutionOutcome::Recorded { reason },
|
||||
RetireOutcome::Declined | RetireOutcome::Wrong => ResolutionOutcome::Withdrawn { reason },
|
||||
},
|
||||
rationale: retirement.why.as_str().into(),
|
||||
resolved_on: on.clone(),
|
||||
})]);
|
||||
}
|
||||
for (index, remembered) in reply.remember.iter().enumerate() {
|
||||
units.push(vec![PipelineDocument::Memory(PipelineMemory {
|
||||
instance: instance.clone(),
|
||||
label: Label(format!("{}-r{index}", context.label)),
|
||||
sort: remembered.sort,
|
||||
text: Line(remembered.text.clone()),
|
||||
about: remembered.about.iter().cloned().map(Short).collect(),
|
||||
origin: MemoryOrigin::Sleep { run: run.clone() },
|
||||
sources: Vec::new(),
|
||||
salience_permille: remembered.salience_permille,
|
||||
consolidates: Vec::new(),
|
||||
})]);
|
||||
}
|
||||
Ok(units)
|
||||
}
|
||||
|
||||
/// Operations packed in order into batches of at most `BATCH_MAX`, none split.
|
||||
pub(crate) fn pack(units: Vec<Vec<PipelineDocument>>) -> Vec<Vec<PipelineDocument>> {
|
||||
let mut batches: Vec<Vec<PipelineDocument>> = Vec::new();
|
||||
for unit in units {
|
||||
match batches.last_mut() {
|
||||
Some(last) if last.len() + unit.len() <= BATCH_MAX => last.extend(unit),
|
||||
_ => batches.push(unit),
|
||||
}
|
||||
}
|
||||
batches
|
||||
}
|
||||
|
||||
/// How a night ended.
|
||||
enum Finish {
|
||||
/// `through` is the last memory of the day the night was shown, if it was shown any.
|
||||
Slept { reply: SleepReply, through: Option<PipelineRef> },
|
||||
Failed { refusals: u32, why: String },
|
||||
}
|
||||
|
||||
struct SleepInbox<'a, C, S> {
|
||||
organs: &'a Organs<'a>,
|
||||
control: &'a C,
|
||||
swarm: &'a S,
|
||||
context: &'a Context,
|
||||
line: String,
|
||||
told: Option<String>,
|
||||
refusals: u32,
|
||||
finish: Option<Finish>,
|
||||
}
|
||||
|
||||
impl<C: ControlSource, S: ControlSource> SleepInbox<'_, C, S> {
|
||||
/// Her reply admitted whole operations at a time, in batches; a refused batch ends the night, the earlier ones standing.
|
||||
fn admit(&self, reply: &SleepReply) -> Result<()> {
|
||||
let batches = pack(operations(self.organs.mind, self.context, reply)?);
|
||||
for batch in batches {
|
||||
match self.organs.mind.admit(AGENT, &self.context.label, batch)? {
|
||||
PipelineAdmissionOutcome::Committed { .. } | PipelineAdmissionOutcome::AlreadyAdmitted { .. } => {}
|
||||
refused => bail!("her mind refused a batch of the night: {refused:?}"),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn refuse(&mut self, fault: String) -> (bool, bool) {
|
||||
self.refusals += 1;
|
||||
if self.refusals >= REFUSALS_PER_NIGHT {
|
||||
self.finish = Some(Finish::Failed { refusals: self.refusals, why: fault });
|
||||
return (false, true);
|
||||
}
|
||||
self.told = Some(format!("Your reply was not taken:\n{fault}\n\nReply again, whole."));
|
||||
(true, false)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ControlSource, S: ControlSource> SessionInbox for SleepInbox<'_, C, S> {
|
||||
fn start_line(&mut self) -> Result<String> {
|
||||
Ok(self.line.clone())
|
||||
}
|
||||
|
||||
fn poll_every_s(&self) -> i64 {
|
||||
PERSONA_POLL_S
|
||||
}
|
||||
|
||||
/// No chat is read into the night or acked by it; the brakes still stop it.
|
||||
fn poll(&mut self) -> Result<Polled> {
|
||||
if let crate::control::Effective::Held { reason } = crate::persona::read(self.control, self.swarm, self.organs.mind.instance()) {
|
||||
self.finish.get_or_insert(Finish::Failed { refusals: self.refusals, why: format!("held: {reason:?}") });
|
||||
}
|
||||
Ok(Polled { pending: self.told.is_some(), other: false, other_first: false, yield_now: self.finish.is_some() })
|
||||
}
|
||||
|
||||
fn tell(&mut self, _switch: bool) -> Result<String> {
|
||||
Ok(self.told.take().unwrap_or_default())
|
||||
}
|
||||
|
||||
fn flush_text(&mut self, _why: Why) -> String {
|
||||
"Your time is nearly spent. Reply now with your whole SleepReply: every memory of your day disposed of, and the night in one line.".into()
|
||||
}
|
||||
|
||||
fn result(&mut self, line: &str, ok: bool, blocked: bool) -> Result<(bool, bool)> {
|
||||
if self.finish.is_some() {
|
||||
return Ok((false, true));
|
||||
}
|
||||
if blocked || !ok {
|
||||
self.finish = Some(Finish::Failed { refusals: self.refusals, why: "the session failed".into() });
|
||||
return Ok((false, true));
|
||||
}
|
||||
let Some(text) = structured_output(line) else { return Ok(self.refuse("the result carried no reply".into())) };
|
||||
match decode(&text, self.context) {
|
||||
Ok(reply) => match self.admit(&reply) {
|
||||
Ok(()) => {
|
||||
self.finish = Some(Finish::Slept { reply, through: self.context.day.last().map(|held| reference(&held.id)) });
|
||||
Ok((true, false))
|
||||
}
|
||||
Err(error) => {
|
||||
self.finish = Some(Finish::Failed { refusals: self.refusals, why: error.to_string() });
|
||||
Ok((false, true))
|
||||
}
|
||||
},
|
||||
Err(fault) => Ok(self.refuse(fault)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What a night closes with: the outcome (`Slept` with her line and where her day was covered through, or
|
||||
/// `Recorded` with the failure) and the rationale (the counts, display only: nothing reads it).
|
||||
fn closing(finish: &Finish) -> (ResolutionOutcome, String) {
|
||||
match finish {
|
||||
Finish::Slept { reply, through } => (
|
||||
ResolutionOutcome::Slept { line: reply.night.as_str().into(), day_through: through.clone() },
|
||||
format!(
|
||||
"kept {}, consolidated {} from {}, retired {}, formed {}",
|
||||
reply.keep.len(),
|
||||
reply.consolidate.len(),
|
||||
reply.consolidate.iter().map(|consolidation| consolidation.consumes.len()).sum::<usize>(),
|
||||
reply.retire.len(),
|
||||
reply.remember.len()
|
||||
),
|
||||
),
|
||||
Finish::Failed { refusals, why } => (
|
||||
ResolutionOutcome::Recorded { reason: format!("failed after {refusals} refusals: {why}").as_str().into() },
|
||||
"The night ended without a reply the ledger took.".to_string(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Closes the night's run: `Slept` with her line and the cut line, or `Recorded` with the failure.
|
||||
fn close(organs: &Organs, run: &PipelineRef, finish: &Finish) -> Result<()> {
|
||||
let (outcome, rationale) = closing(finish);
|
||||
let resolution = PipelineResolution {
|
||||
subject: run.clone(),
|
||||
sequence: next_sequence(organs.mind, run)?,
|
||||
outcome,
|
||||
rationale: rationale.as_str().into(),
|
||||
resolved_on: date(organs.clock.now()),
|
||||
};
|
||||
match organs.mind.admit(AGENT, label_of(run), vec![PipelineDocument::Resolution(resolution)])? {
|
||||
PipelineAdmissionOutcome::Committed { .. } => Ok(()),
|
||||
_ => bail!("the Sleep run could not be closed"),
|
||||
}
|
||||
}
|
||||
|
||||
/// One night, in the Persona unit, under the Sleep run the heartbeat opened: one process, no retry.
|
||||
pub(crate) fn carry_night(organs: &Organs, control: &impl ControlSource, swarm: &impl ControlSource, config: &Config, run: &PipelineRef) -> Result<Outcome> {
|
||||
let remaining_s = organs.run_cap_s.min(SLEEP_CAP_S);
|
||||
let finish = match context(organs.mind, run, organs.clock.now()) {
|
||||
Err(error) => Finish::Failed { refusals: 0, why: format!("her mind could not be read for the night: {error}") },
|
||||
Ok(context) => {
|
||||
let session_id = uuid::Uuid::new_v5(&SESSION_NAMESPACE, format!("{}:sleep", run.id.0).as_bytes());
|
||||
match start_session(organs, config, session_id, remaining_s, |_| sleep_schema())? {
|
||||
Err(why) => Finish::Failed { refusals: 0, why: format!("the night could not start: {why}") },
|
||||
Ok(Started { child, lease, .. }) => {
|
||||
let mut inbox = SleepInbox { organs, control, swarm, context: &context, line: night_line(&context), told: None, refusals: 0, finish: None };
|
||||
let driven = drive(organs.clock, child, &mut inbox, remaining_s, &mut std::io::stdout());
|
||||
drop(lease);
|
||||
match (inbox.finish.take(), driven) {
|
||||
(Some(finish), _) => finish,
|
||||
(None, Err(error)) => Finish::Failed { refusals: inbox.refusals, why: format!("the session broke: {error}") },
|
||||
(None, Ok(_)) => Finish::Failed { refusals: inbox.refusals, why: "the night ended without an answer".into() },
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
close(organs, run, &finish)?;
|
||||
let failure = matches!(finish, Finish::Failed { .. }).then_some(Failure::Session);
|
||||
Ok(Outcome::Served { run: run.clone(), answered: u32::from(failure.is_none()), failure, self_run: None })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
@ -1,534 +0,0 @@
|
|||
//! The night's ledger rules, one behaviour each: what the decoder refuses and tells back, what an operation
|
||||
//! admits, how operations pack into batches, and which memories a night is shown. The whole night through a
|
||||
//! scripted child is in `persona::tests`.
|
||||
|
||||
use eureka_pipeline::{MemoryOrigin, MemorySort, PipelineDocument, ResolutionOutcome, RunOperator, RunTurn};
|
||||
use serde_json::json;
|
||||
|
||||
use super::*;
|
||||
use crate::testkit::{INSTANCE, TestMind, at, memory_from, run, run_ref};
|
||||
|
||||
const T0: i64 = 1_000_000_000;
|
||||
|
||||
fn dm(room: &str, with: &[&str]) -> MemoryOrigin {
|
||||
MemoryOrigin::Direct { room: room.into(), with: with.iter().map(|who| (*who).into()).collect() }
|
||||
}
|
||||
|
||||
fn wake() -> MemoryOrigin {
|
||||
MemoryOrigin::Wake
|
||||
}
|
||||
|
||||
fn id(label: &str) -> String {
|
||||
format!("{INSTANCE}:memory:{label}")
|
||||
}
|
||||
|
||||
fn held(label: &str, sort: MemorySort, about: &[&str], origin: MemoryOrigin, ordinal: u64) -> Held {
|
||||
let PipelineDocument::Memory(memory) = memory_from(label, sort, "Text.", about, origin, 500) else { unreachable!() };
|
||||
Held { id: id(label), memory, at: at(T0 + i64::try_from(ordinal).unwrap()), ordinal }
|
||||
}
|
||||
|
||||
/// A night over four memories: two in her day, one open ask, three elsewhere (public, and two DMs).
|
||||
fn ledger() -> Context {
|
||||
let day = vec![held("d1", MemorySort::Learned, &["@kiko:chat"], wake(), 4), held("d2", MemorySort::Decided, &[], wake(), 5)];
|
||||
let open = vec![held("ask", MemorySort::Asked, &["@op:chat"], wake(), 1)];
|
||||
let others = vec![held("x1", MemorySort::Learned, &[], wake(), 2), held("dmA", MemorySort::Learned, &[], dm("!a:chat", &["@a:chat"]), 3), held("dmB", MemorySort::Learned, &[], dm("!b:chat", &["@b:chat"]), 6)];
|
||||
let all = day.iter().chain(&open).chain(&others).map(|held| (held.id.clone(), held.clone())).collect();
|
||||
Context { now: at(T0 + 100), label: "mind-night".into(), counts: BTreeMap::new(), day, open, tide: Vec::new(), all }
|
||||
}
|
||||
|
||||
fn reply(keep: &[&str]) -> serde_json::Value {
|
||||
json!({"night": "A night.", "keep": keep.iter().map(|label| id(label)).collect::<Vec<_>>(), "consolidate": [], "retire": [], "remember": []})
|
||||
}
|
||||
|
||||
fn fault(reply: &serde_json::Value) -> String {
|
||||
decode(&reply.to_string(), &ledger()).expect_err("the reply should be refused")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_memory_of_her_day_is_disposed_of_exactly_once() {
|
||||
assert!(decode(&reply(&["d1", "d2"]).to_string(), &ledger()).is_ok());
|
||||
let missing = fault(&reply(&["d1"]));
|
||||
assert!(missing.contains("not covered") && missing.contains(&id("d2")) && !missing.contains(&format!("{}, ", id("d1"))), "{missing}");
|
||||
// Disposed of in any of the three ways counts.
|
||||
let mut by_retire = reply(&["d1"]);
|
||||
by_retire["retire"] = json!([{"item": id("d2"), "outcome": "faded", "why": "It faded."}]);
|
||||
assert!(decode(&by_retire.to_string(), &ledger()).is_ok());
|
||||
let mut by_consolidation = reply(&[]);
|
||||
by_consolidation["consolidate"] = json!([{"consumes": [id("d1"), id("d2")], "sort": "Learned", "text": "Both.", "about": [], "salience_permille": 500}]);
|
||||
assert!(decode(&by_consolidation.to_string(), &ledger()).is_ok());
|
||||
// Twice, within a list or across them, is refused and the id named.
|
||||
let mut twice = reply(&["d1", "d2"]);
|
||||
twice["retire"] = json!([{"item": id("d2"), "outcome": "done", "why": "Done."}]);
|
||||
assert!(fault(&twice).contains(&format!("{} is named more than once", id("d2"))));
|
||||
assert!(fault(&reply(&["d1", "d1", "d2"])).contains("named more than once"));
|
||||
let mut across = reply(&["d2"]);
|
||||
across["consolidate"] = json!([
|
||||
{"consumes": [id("d1")], "sort": "Learned", "text": "One.", "about": [], "salience_permille": 500},
|
||||
{"consumes": [id("d1")], "sort": "Learned", "text": "Again.", "about": [], "salience_permille": 500}
|
||||
]);
|
||||
assert!(fault(&across).contains("named more than once"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_id_must_be_a_memory_of_hers_in_force_whether_or_not_it_is_of_the_day() {
|
||||
assert!(decode(&reply(&["d1", "d2", "ask", "x1"]).to_string(), &ledger()).is_ok(), "older items are hers to touch");
|
||||
let ghost = fault(&reply(&["d1", "d2", "ghost"]));
|
||||
assert!(ghost.contains("ghost is not a memory of yours in force"), "{ghost}");
|
||||
let mut retiring = reply(&["d1", "d2"]);
|
||||
retiring["retire"] = json!([{"item": "yggdrasil:run:not-a-memory", "outcome": "done", "why": "No."}]);
|
||||
assert!(fault(&retiring).contains("not a memory of yours"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_consolidation_merges_one_conversation_only() {
|
||||
let consolidate = |consumes: &[&str]| {
|
||||
let mut r = reply(&["d1", "d2"].iter().filter(|label| !consumes.contains(label)).copied().collect::<Vec<_>>());
|
||||
r["consolidate"] = json!([{"consumes": consumes.iter().map(|label| id(label)).collect::<Vec<_>>(), "sort": "Learned", "text": "Merged.", "about": [], "salience_permille": 500}]);
|
||||
r
|
||||
};
|
||||
assert!(fault(&consolidate(&["d1", "dmA"])).contains("one conversation only"), "public with a DM");
|
||||
assert!(fault(&consolidate(&["dmA", "dmB", "d1", "d2"])).contains("one conversation only"), "two DMs");
|
||||
for same in [&["d1", "d2", "x1"][..], &["dmA"][..]] {
|
||||
let mut r = consolidate(same);
|
||||
r["keep"] = json!(["d1", "d2"].iter().filter(|label| !same.contains(label)).map(|label| id(label)).collect::<Vec<_>>());
|
||||
assert!(decode(&r.to_string(), &ledger()).is_ok(), "{same:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_reply_holds_the_bounds_its_schema_states() {
|
||||
let with = |edit: &dyn Fn(&mut serde_json::Value)| {
|
||||
let mut r = reply(&["d1", "d2"]);
|
||||
edit(&mut r);
|
||||
r
|
||||
};
|
||||
let long = "x".repeat(901);
|
||||
assert!(fault(&with(&|r| r["night"] = json!(long.clone()))).contains("night"));
|
||||
assert!(fault(&with(&|r| r["night"] = json!(" "))).contains("night"));
|
||||
assert!(decode(&with(&|r| r["night"] = json!("x".repeat(900))).to_string(), &ledger()).is_ok());
|
||||
let item = |text: &str| json!({"sort": "Idea", "text": text, "about": [], "salience_permille": 100});
|
||||
assert!(fault(&with(&|r| r["remember"] = json!((0..17).map(|n| item(&format!("n{n}"))).collect::<Vec<_>>()))).contains("at most"));
|
||||
assert!(decode(&with(&|r| r["remember"] = json!((0..16).map(|n| item(&format!("n{n}"))).collect::<Vec<_>>())).to_string(), &ledger()).is_ok());
|
||||
assert!(fault(&with(&|r| r["remember"] = json!([item(&"y".repeat(1001))]))).contains("remember.text"));
|
||||
assert!(fault(&with(&|r| r["remember"] = json!([{"sort": "Idea", "text": "t", "about": [], "salience_permille": 1001}]))).contains("salience"));
|
||||
assert!(fault(&with(&|r| r["retire"] = json!([{"item": id("x1"), "outcome": "done", "why": ""}]))).contains("why"));
|
||||
assert!(fault(&with(&|r| r["retire"] = json!([{"item": id("x1"), "outcome": "done", "why": "w".repeat(301)}]))).contains("why"));
|
||||
let many: Vec<serde_json::Value> = (0..129).map(|n| json!({"item": format!("missing{n}"), "outcome": "done", "why": "w"})).collect();
|
||||
assert!(fault(&with(&|r| r["retire"] = json!(many.clone()))).contains("at most"));
|
||||
let empty = json!({"consumes": [], "sort": "Learned", "text": "t", "about": [], "salience_permille": 1});
|
||||
assert!(fault(&with(&|r| r["consolidate"] = json!([empty.clone()]))).contains("consumes"));
|
||||
let wide = json!({"consumes": (0..17).map(|n| format!("c{n}")).collect::<Vec<_>>(), "sort": "Learned", "text": "t", "about": [], "salience_permille": 1});
|
||||
assert!(fault(&with(&|r| r["consolidate"] = json!([wide.clone()]))).contains("consumes"));
|
||||
assert!(fault(&with(&|r| r["consolidate"] = json!(vec![empty.clone(); 33]))).contains("at most"));
|
||||
assert!(fault(&with(&|r| r["extra"] = json!(1))).contains("not a SleepReply"), "no unknown field");
|
||||
assert!(fault(&json!({"night": "n"})).contains("not a SleepReply"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_about_is_a_person_a_shown_memory_names() {
|
||||
let mut named = reply(&["d1", "d2"]);
|
||||
named["remember"] = json!([{"sort": "Rapport", "text": "She likes it plain.", "about": ["@kiko:chat", "@op:chat"], "salience_permille": 300}]);
|
||||
assert!(decode(&named.to_string(), &ledger()).is_ok(), "d1 is about kiko and the ask is about the operator");
|
||||
named["remember"][0]["about"] = json!(["@stranger:chat"]);
|
||||
assert!(fault(&named).contains("remember.about"));
|
||||
let mut consolidated = reply(&["d2"]);
|
||||
consolidated["consolidate"] = json!([{"consumes": [id("d1")], "sort": "Learned", "text": "t", "about": ["@stranger:chat"], "salience_permille": 100}]);
|
||||
assert!(fault(&consolidated).contains("consolidate.about"));
|
||||
named["remember"][0]["about"] = json!(vec!["@kiko:chat"; 9]);
|
||||
assert!(fault(&named).contains("remember.about"), "more than eight");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn faults_are_told_together_so_one_retell_can_mend_them() {
|
||||
let both = fault(&json!({"night": "", "keep": [id("d1"), "ghost"], "consolidate": [], "retire": [], "remember": []}));
|
||||
assert!(both.contains("night") && both.contains("ghost") && both.contains(&id("d2")), "{both}");
|
||||
}
|
||||
|
||||
fn op(consumes: usize) -> Vec<PipelineDocument> {
|
||||
(0..=consumes).map(|n| memory_from(&format!("u{n}"), MemorySort::Idea, "x", &[], wake(), 100)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operations_pack_into_batches_of_at_most_the_batch_max_and_never_split_one() {
|
||||
let units: Vec<Vec<PipelineDocument>> = vec![op(16), op(16), op(16), op(16), op(1)];
|
||||
let sizes: Vec<usize> = units.iter().map(Vec::len).collect();
|
||||
assert_eq!(sizes, vec![17, 17, 17, 17, 2], "seventy documents");
|
||||
let batches = pack(units);
|
||||
assert_eq!(batches.iter().map(Vec::len).collect::<Vec<_>>(), vec![51, 19], "three whole consolidations fit, the fourth starts the next batch");
|
||||
assert!(batches.iter().all(|batch| batch.len() <= huginn_mind::BATCH_MAX));
|
||||
assert_eq!(pack(vec![op(62), op(0)]).iter().map(Vec::len).collect::<Vec<_>>(), vec![63 + 1], "a batch fills to the max exactly");
|
||||
assert_eq!(pack(vec![op(62), op(1)]).iter().map(Vec::len).collect::<Vec<_>>(), vec![63, 2]);
|
||||
assert!(pack(Vec::new()).is_empty());
|
||||
}
|
||||
|
||||
fn consolidated(context: &Context, consumes: &[&str]) -> PipelineMemory {
|
||||
let reply = SleepReply {
|
||||
night: "n".into(),
|
||||
keep: vec![],
|
||||
consolidate: vec![Consolidate { consumes: consumes.iter().map(|label| id(label)).collect(), sort: MemorySort::Learned, text: "One.".into(), about: vec![], salience_permille: 400 }],
|
||||
retire: vec![],
|
||||
remember: vec![],
|
||||
};
|
||||
let units = operations(&TestMind::seeded(), context, &reply).unwrap();
|
||||
assert_eq!(units.len(), 1);
|
||||
assert_eq!(units[0].len(), 1 + consumes.len(), "the memory and a resolution of each consumed memory");
|
||||
let PipelineDocument::Memory(memory) = units[0][0].clone() else { panic!("the memory comes first") };
|
||||
for (resolution, consumed) in units[0][1..].iter().zip(consumes) {
|
||||
let PipelineDocument::Resolution(resolution) = resolution else { panic!("a resolution") };
|
||||
assert_eq!(resolution.subject.id.0, id(consumed));
|
||||
assert!(matches!(&resolution.outcome, ResolutionOutcome::Superseded { by } if by.len() == 1 && by[0].id.0 == id(&memory.label.0)), "{:?}", resolution.outcome);
|
||||
}
|
||||
memory
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_consolidation_carries_at_most_eight_sources_newest_first_and_eight_voices() {
|
||||
let mut context = ledger();
|
||||
let mut old = held("s0", MemorySort::Learned, &[], dm("!a:chat", &[]), 10);
|
||||
old.memory.sources = (0..6).map(|n| format!("$old{n}").as_str().into()).collect();
|
||||
old.memory.origin = dm("!a:chat", &["@v0:chat", "@v1:chat", "@v2:chat", "@v3:chat", "@v4:chat"]);
|
||||
let mut new = held("s1", MemorySort::Learned, &[], dm("!a:chat", &[]), 11);
|
||||
new.memory.sources = ["$new0", "$old5", "$new1", "$new2", "$new3"].map(Into::into).to_vec();
|
||||
new.memory.origin = dm("!a:chat", &["@v3:chat", "@v5:chat", "@v6:chat", "@v7:chat", "@v8:chat"]);
|
||||
for held in [&old, &new] {
|
||||
context.all.insert(held.id.clone(), held.clone());
|
||||
}
|
||||
let memory = consolidated(&context, &["s0", "s1"]);
|
||||
let sources: Vec<&str> = memory.sources.iter().map(|source| source.0.as_str()).collect();
|
||||
assert_eq!(sources, vec!["$new3", "$new2", "$new1", "$old5", "$new0", "$old4", "$old3", "$old2"], "newest memory's last source first, once each, eight");
|
||||
let MemoryOrigin::Direct { room, with } = &memory.origin else { panic!("a DM stays its own") };
|
||||
assert_eq!(room.0, "!a:chat");
|
||||
assert_eq!(with.iter().map(|who| who.0.as_str()).collect::<Vec<_>>(), vec!["@v0:chat", "@v1:chat", "@v2:chat", "@v3:chat", "@v4:chat", "@v5:chat", "@v6:chat", "@v7:chat"], "the union, in the order they spoke, eight");
|
||||
assert_eq!(memory.consolidates.iter().map(|consumed| consumed.id.0.clone()).collect::<Vec<_>>(), vec![id("s0"), id("s1")]);
|
||||
assert_eq!(memory.label.0, "mind-night-c0");
|
||||
let public = consolidated(&context, &["d1", "d2"]);
|
||||
assert_eq!(public.origin, MemoryOrigin::Sleep { run: "mind-night".into() });
|
||||
assert_eq!((public.sort, public.text.0.as_str(), public.salience_permille), (MemorySort::Learned, "One.", 400));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retirements_and_remembered_things_become_resolutions_and_sleep_memories() {
|
||||
let context = ledger();
|
||||
let reply = SleepReply {
|
||||
night: "n".into(),
|
||||
keep: vec![],
|
||||
consolidate: vec![],
|
||||
retire: [RetireOutcome::Done, RetireOutcome::Declined, RetireOutcome::Faded, RetireOutcome::Wrong]
|
||||
.into_iter()
|
||||
.zip(["d1", "d2", "ask", "x1"])
|
||||
.map(|(outcome, label)| Retire { item: id(label), outcome, why: format!("why {label}") })
|
||||
.collect(),
|
||||
remember: vec![Remember { sort: MemorySort::Idea, text: "Later.".into(), about: vec![], salience_permille: 200 }],
|
||||
};
|
||||
let units = operations(&TestMind::seeded(), &context, &reply).unwrap();
|
||||
assert_eq!(units.len(), 5);
|
||||
let kinds: Vec<(&str, bool)> = units[..4]
|
||||
.iter()
|
||||
.map(|unit| {
|
||||
let PipelineDocument::Resolution(resolution) = &unit[0] else { panic!("a resolution") };
|
||||
assert_eq!(resolution.rationale.0, format!("why {}", resolution.subject.id.0.rsplit(':').next().unwrap()), "her reason");
|
||||
match &resolution.outcome {
|
||||
ResolutionOutcome::Recorded { reason } => (reason.0.as_str().rsplit(' ').next().unwrap(), true),
|
||||
ResolutionOutcome::Withdrawn { reason } => (reason.0.as_str().rsplit(' ').next().unwrap(), false),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(kinds, vec![("d1", true), ("d2", false), ("ask", true), ("x1", false)], "done and faded are Recorded, declined and wrong Withdrawn");
|
||||
let PipelineDocument::Memory(remembered) = &units[4][0] else { panic!("a memory") };
|
||||
assert_eq!((remembered.label.0.as_str(), &remembered.origin), ("mind-night-r0", &MemoryOrigin::Sleep { run: "mind-night".into() }));
|
||||
}
|
||||
|
||||
/// Her mind with a Sleep run opened at `T0 + 1000` (and `earlier` Sleep runs, slept, a hundred hours before it),
|
||||
/// and `memories` admitted at the given offsets: everything admitted in the order of its time.
|
||||
fn night_over(earlier: usize, memories: &[(String, MemorySort, MemoryOrigin, i64)]) -> (TestMind, PipelineRef) {
|
||||
let mind = TestMind::seeded();
|
||||
let mut events: Vec<(i64, Vec<Vec<PipelineDocument>>)> = Vec::new();
|
||||
for n in 0..earlier {
|
||||
let label = format!("before{n}");
|
||||
let closed = crate::testkit::close(run_ref(&label), ResolutionOutcome::Slept { line: "ok".into(), day_through: None });
|
||||
events.push((T0 - 100 * 3_600_000 + n as i64, vec![vec![run(&label, RunTurn::Sleep, RunOperator::Mind, &[])], vec![closed]]));
|
||||
}
|
||||
for (label, sort, origin, offset) in memories {
|
||||
events.push((T0 + offset, vec![vec![memory_from(label, *sort, "Text.", &[], origin.clone(), 500)]]));
|
||||
}
|
||||
events.push((T0 + 1000, vec![vec![run("tonight", RunTurn::Sleep, RunOperator::Mind, &[])]]));
|
||||
events.sort_by_key(|(when, _)| *when);
|
||||
for (when, commits) in events {
|
||||
mind.admit_at(at(when));
|
||||
for commit in commits {
|
||||
mind.committed(commit);
|
||||
}
|
||||
}
|
||||
(mind, run_ref("tonight"))
|
||||
}
|
||||
|
||||
/// A night's own ending, admitted: slept through what it was shown, or failed.
|
||||
fn close_night(mind: &TestMind, run: &PipelineRef, finish: &Finish) {
|
||||
let (outcome, rationale) = closing(finish);
|
||||
mind.committed(vec![PipelineDocument::Resolution(PipelineResolution {
|
||||
subject: run.clone(),
|
||||
sequence: 1,
|
||||
outcome,
|
||||
rationale: rationale.as_str().into(),
|
||||
resolved_on: eureka_pipeline::Date("2026-10-09".into()),
|
||||
})]);
|
||||
}
|
||||
|
||||
fn slept_over(context: &Context) -> Finish {
|
||||
Finish::Slept { reply: SleepReply { night: "n".into(), keep: vec![], consolidate: vec![], retire: vec![], remember: vec![] }, through: context.day.last().map(|held| reference(&held.id)) }
|
||||
}
|
||||
|
||||
/// A closing someone other than the organ wrote: a Recorded one, whatever its words say.
|
||||
fn closed_by_hand(mind: &TestMind, run: &PipelineRef, reason: &str, rationale: &str) {
|
||||
mind.committed(vec![PipelineDocument::Resolution(PipelineResolution {
|
||||
subject: run.clone(),
|
||||
sequence: 1,
|
||||
outcome: ResolutionOutcome::Recorded { reason: reason.into() },
|
||||
rationale: rationale.into(),
|
||||
resolved_on: eureka_pipeline::Date("2026-10-09".into()),
|
||||
})]);
|
||||
}
|
||||
|
||||
fn failed() -> Finish {
|
||||
Finish::Failed { refusals: 3, why: "no reply the ledger took".into() }
|
||||
}
|
||||
|
||||
/// Opens another night of hers at `when` and reads its day.
|
||||
fn next_night(mind: &TestMind, label: &str, when: i64) -> (PipelineRef, Context) {
|
||||
mind.admit_at(at(T0 + when));
|
||||
mind.committed(vec![run(label, RunTurn::Sleep, RunOperator::Mind, &[])]);
|
||||
let next = run_ref(label);
|
||||
let context = context(mind, &next, at(T0 + when + 5000)).unwrap();
|
||||
(next, context)
|
||||
}
|
||||
|
||||
fn day_ids(context: &Context) -> Vec<String> {
|
||||
context.day.iter().map(|held| held.id.clone()).collect()
|
||||
}
|
||||
|
||||
fn remember_at(mind: &TestMind, label: &str, origin: MemoryOrigin, when: i64) {
|
||||
mind.admit_at(at(T0 + when));
|
||||
mind.committed(vec![memory_from(label, MemorySort::Learned, "Text.", &[], origin, 500)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_day_is_capped_oldest_first_the_open_items_too_and_the_rest_wait() {
|
||||
// No earlier night: her day is the 24 hours before the run opened; the asks are older than that.
|
||||
let asks = (0..60).map(|n| (format!("ask{n:03}"), MemorySort::Asked, wake(), -(25 * 3_600_000) + n as i64));
|
||||
let days = (0..100).map(|n| (format!("day{n:03}"), MemorySort::Learned, wake(), 10 + n as i64));
|
||||
let (mind2, run2) = night_over(0, &asks.chain(days).collect::<Vec<_>>());
|
||||
let night = context(&mind2, &run2, at(T0 + 5000)).unwrap();
|
||||
assert_eq!(night.day.len(), DAY_MAX);
|
||||
assert_eq!((night.day[0].id.as_str(), night.day[95].id.as_str()), (id("day000").as_str(), id("day095").as_str()), "oldest first; day096 and on wait");
|
||||
assert_eq!(night.open.len(), OPEN_MAX);
|
||||
assert_eq!(night.open[0].id, id("ask000"));
|
||||
assert_eq!(night.counts.get("Learned"), Some(&100));
|
||||
assert_eq!(night.counts.get("Asked"), Some(&60));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_tide_moves_thirty_two_memories_a_night_over_the_older_backlog_and_is_whole_when_small() {
|
||||
let old = |n: usize| (format!("old{n:02}"), MemorySort::Learned, wake(), -(150 * 3_600_000) + n as i64);
|
||||
let backlog: Vec<_> = (0..40).map(old).collect();
|
||||
let (mind, run) = night_over(2, &backlog);
|
||||
let night = context(&mind, &run, at(T0 + 5000)).unwrap();
|
||||
let ids: Vec<&str> = night.tide.iter().map(|held| held.id.as_str()).collect();
|
||||
assert_eq!(ids.len(), 32);
|
||||
let picked: Vec<String> = [0, 7, 8, 31].map(|at| ids[at].to_string()).to_vec();
|
||||
assert_eq!(picked, vec![id("old24"), id("old31"), id("old32"), id("old15")], "two earlier nights: offset 64 mod 40 is 24, and it wraps");
|
||||
let (mind, run) = night_over(1, &backlog[..10]);
|
||||
assert_eq!(context(&mind, &run, at(T0 + 5000)).unwrap().tide.len(), 10, "a small backlog is shown whole, once each");
|
||||
let (mind, run) = night_over(0, &[]);
|
||||
assert!(context(&mind, &run, at(T0 + 5000)).unwrap().tide.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_day_begins_when_her_previous_night_opened_and_sleep_made_memories_are_not_in_it() {
|
||||
let hour = 3_600_000;
|
||||
let memories = vec![
|
||||
("before".to_string(), MemorySort::Learned, wake(), -(120 * hour)),
|
||||
("formed".to_string(), MemorySort::Idea, MemoryOrigin::Sleep { run: "x".into() }, -hour),
|
||||
("after".to_string(), MemorySort::Learned, wake(), -hour),
|
||||
];
|
||||
// The earlier night opened 100 hours before tonight's anchor: `before` is older than it, `after` newer.
|
||||
let (mind, run) = night_over(1, &memories);
|
||||
let night = context(&mind, &run, at(T0 + 5000)).unwrap();
|
||||
assert_eq!(night.day.iter().map(|held| held.id.clone()).collect::<Vec<_>>(), vec![id("after")]);
|
||||
assert!(night.tide.iter().all(|held| held.id != id("formed")), "no Sleep-origin memory comes round");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_night_schema_states_the_reply_the_decoder_takes() {
|
||||
let schema: serde_json::Value = serde_json::from_str(&sleep_schema()).unwrap();
|
||||
assert_eq!(schema["required"], json!(["night", "keep", "consolidate", "retire", "remember"]));
|
||||
assert_eq!(schema["additionalProperties"], json!(false));
|
||||
assert_eq!(schema["properties"]["consolidate"]["maxItems"], json!(32));
|
||||
assert_eq!(schema["properties"]["consolidate"]["items"]["properties"]["consumes"]["maxItems"], json!(16));
|
||||
assert_eq!(schema["properties"]["retire"]["maxItems"], json!(128));
|
||||
assert_eq!(schema["properties"]["remember"]["maxItems"], json!(16));
|
||||
assert_eq!(schema["properties"]["retire"]["items"]["properties"]["outcome"]["enum"], json!(["done", "declined", "faded", "wrong"]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn what_the_cap_left_waits_for_the_next_night_and_is_not_dropped_to_the_backlog() {
|
||||
let days: Vec<_> = (0..100).map(|n| (format!("day{n:03}"), MemorySort::Learned, wake(), 10 + n as i64)).collect();
|
||||
let (mind, tonight) = night_over(0, &days);
|
||||
let shown = context(&mind, &tonight, at(T0 + 5000)).unwrap();
|
||||
assert_eq!(shown.day.len(), DAY_MAX);
|
||||
assert!(shown.tide.is_empty() && shown.open.is_empty(), "what waits is neither tide nor open");
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
close_night(&mind, &tonight, &slept_over(&shown));
|
||||
let (_, next) = next_night(&mind, "tomorrow", 3000);
|
||||
assert_eq!(day_ids(&next), (96..100).map(|n| id(&format!("day{n:03}"))).collect::<Vec<_>>(), "the four past the cap are tomorrow's day, and only they");
|
||||
assert_eq!(next.tide.len(), 32, "the ninety-six she was shown are backlog now");
|
||||
assert!(next.tide.iter().all(|held| !day_ids(&next).contains(&held.id)), "a day memory is not also in the tide");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_night_that_failed_or_never_ran_covers_nothing_so_its_day_comes_back() {
|
||||
let days: Vec<_> = (0..3).map(|n| (format!("f{n}"), MemorySort::Learned, wake(), 10 + n as i64)).collect();
|
||||
let (mind, first) = night_over(0, &days);
|
||||
let shown = context(&mind, &first, at(T0 + 5000)).unwrap();
|
||||
assert_eq!(day_ids(&shown).len(), 3);
|
||||
// Her first night was withdrawn unrun: the day is still the 24 hours before that first night opened.
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
mind.committed(vec![crate::testkit::close(first.clone(), crate::testkit::withdrawn())]);
|
||||
let (second, again) = next_night(&mind, "second", 3000);
|
||||
assert_eq!(day_ids(&again), vec![id("f0"), id("f1"), id("f2")]);
|
||||
// The second night failed, and f3 arrived while it was open: the third owes all four.
|
||||
remember_at(&mind, "f3", wake(), 3500);
|
||||
mind.admit_at(at(T0 + 3600));
|
||||
close_night(&mind, &second, &failed());
|
||||
let (third, owed) = next_night(&mind, "third", 4000);
|
||||
assert_eq!(day_ids(&owed), vec![id("f0"), id("f1"), id("f2"), id("f3")]);
|
||||
// The third slept through what it was shown; the next day begins after it.
|
||||
remember_at(&mind, "g0", wake(), 4500);
|
||||
mind.admit_at(at(T0 + 4600));
|
||||
close_night(&mind, &third, &slept_over(&owed));
|
||||
remember_at(&mind, "g1", wake(), 5000);
|
||||
let (fourth, owed_again) = next_night(&mind, "fourth", 6000);
|
||||
assert_eq!(day_ids(&owed_again), vec![id("g0"), id("g1")], "g0 arrived while the third night was open and is still owed; f0 to f3 are covered");
|
||||
// A failure after a night that slept leaves that night's cover standing: only the nights' own closings are read.
|
||||
remember_at(&mind, "g2", wake(), 6500);
|
||||
mind.admit_at(at(T0 + 6600));
|
||||
close_night(&mind, &fourth, &failed());
|
||||
let (_, fifth) = next_night(&mind, "fifth", 7000);
|
||||
assert_eq!(day_ids(&fifth), vec![id("g0"), id("g1"), id("g2")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hand_written_closing_moves_no_cut_line() {
|
||||
// Closed Recorded with the words and the numbers the old reader trusted: the day is owed all the same.
|
||||
let days: Vec<_> = (0..3).map(|n| (format!("g{n}"), MemorySort::Learned, wake(), 10 + n as i64)).collect();
|
||||
let (mind, first) = night_over(0, &days);
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
closed_by_hand(&mind, &first, "slept: closed by hand", "kept 0; day through ordinal 999999999");
|
||||
let (_, next) = next_night(&mind, "second", 3000);
|
||||
assert_eq!(day_ids(&next), vec![id("g0"), id("g1"), id("g2")], "nothing was shown, so nothing is dropped");
|
||||
|
||||
// A hundred-memory day closed by hand covers none of it; a Slept close over what was then shown covers it.
|
||||
let days: Vec<_> = (0..100).map(|n| (format!("day{n:03}"), MemorySort::Learned, wake(), 10 + n as i64)).collect();
|
||||
let (mind, first) = night_over(0, &days);
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
closed_by_hand(&mind, &first, "slept: closed by hand", "kept 96 (by hand)");
|
||||
let (second, owed) = next_night(&mind, "second", 3000);
|
||||
assert_eq!(day_ids(&owed), (0..96).map(|n| id(&format!("day{n:03}"))).collect::<Vec<_>>(), "the overflow is not dropped by a hand");
|
||||
mind.admit_at(at(T0 + 3600));
|
||||
close_night(&mind, &second, &slept_over(&owed));
|
||||
let (_, third) = next_night(&mind, "third", 4000);
|
||||
assert_eq!(day_ids(&third), (96..100).map(|n| id(&format!("day{n:03}"))).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_slept_nights_closing_names_the_last_memory_it_was_shown_even_when_that_night_consumed_it() {
|
||||
let days: Vec<_> = (0..100).map(|n| (format!("day{n:03}"), MemorySort::Learned, wake(), 10 + n as i64)).collect();
|
||||
let (mind, tonight) = night_over(0, &days);
|
||||
let shown = context(&mind, &tonight, at(T0 + 5000)).unwrap();
|
||||
let (outcome, rationale) = closing(&slept_over(&shown));
|
||||
assert_eq!(outcome, ResolutionOutcome::Slept { line: "n".into(), day_through: Some(crate::testkit::reference(eureka_pipeline::PipelineKind::Memory, &id("day095"))) });
|
||||
assert_eq!(rationale, "kept 0, consolidated 0 from 0, retired 0, formed 0", "the rationale is the counts and says nothing of the cut line");
|
||||
// The night folds the last memory it was shown into a consolidation, so day095 leaves force.
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
let PipelineDocument::Memory(mut merged) = memory_from("merged", MemorySort::Learned, "Merged.", &[], wake(), 500) else { unreachable!() };
|
||||
let consumed = crate::testkit::reference(eureka_pipeline::PipelineKind::Memory, &id("day095"));
|
||||
merged.consolidates = vec![consumed.clone()];
|
||||
let by = crate::testkit::reference(eureka_pipeline::PipelineKind::Memory, &id("merged"));
|
||||
mind.committed(vec![PipelineDocument::Memory(merged), crate::testkit::close(consumed, ResolutionOutcome::Superseded { by: vec![by] })]);
|
||||
mind.admit_at(at(T0 + 2500));
|
||||
close_night(&mind, &tonight, &slept_over(&shown));
|
||||
let (_, next) = next_night(&mind, "tomorrow", 3000);
|
||||
assert_eq!(day_ids(&next), (96..100).map(|n| id(&format!("day{n:03}"))).collect::<Vec<_>>(), "the cut line is read from the memory that is no longer in force");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_newest_slept_nights_closing_decides_not_an_older_ones() {
|
||||
let days: Vec<_> = (0..2).map(|n| (format!("f{n}"), MemorySort::Learned, wake(), 10 + n as i64)).collect();
|
||||
let (mind, first) = night_over(0, &days);
|
||||
let shown = context(&mind, &first, at(T0 + 5000)).unwrap();
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
close_night(&mind, &first, &slept_over(&shown));
|
||||
remember_at(&mind, "f2", wake(), 2500);
|
||||
remember_at(&mind, "f3", wake(), 2600);
|
||||
let (second, owed) = next_night(&mind, "second", 3000);
|
||||
assert_eq!(day_ids(&owed), vec![id("f2"), id("f3")]);
|
||||
mind.admit_at(at(T0 + 3600));
|
||||
close_night(&mind, &second, &slept_over(&owed));
|
||||
remember_at(&mind, "g0", wake(), 3800);
|
||||
let (_, third) = next_night(&mind, "third", 4000);
|
||||
assert_eq!(day_ids(&third), vec![id("g0")], "both nights slept; the second's cover is the one that counts");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_slept_closing_that_was_withdrawn_covers_nothing() {
|
||||
let days: Vec<_> = (0..3).map(|n| (format!("f{n}"), MemorySort::Learned, wake(), 10 + n as i64)).collect();
|
||||
let (mind, first) = night_over(0, &days);
|
||||
let shown = context(&mind, &first, at(T0 + 5000)).unwrap();
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
close_night(&mind, &first, &slept_over(&shown));
|
||||
// The closing is withdrawn (the run is live again) and the run ends in failure: no Slept closing stands.
|
||||
mind.admit_at(at(T0 + 2100));
|
||||
let closing_ref = crate::testkit::reference(eureka_pipeline::PipelineKind::Resolution, &format!("{INSTANCE}:resolution:run.tonight.n1"));
|
||||
mind.committed(vec![crate::testkit::close(closing_ref, crate::testkit::withdrawn())]);
|
||||
mind.admit_at(at(T0 + 2200));
|
||||
mind.committed(vec![PipelineDocument::Resolution(PipelineResolution {
|
||||
subject: first.clone(),
|
||||
sequence: 2,
|
||||
outcome: ResolutionOutcome::Recorded { reason: "failed after 3 refusals: no reply the ledger took".into() },
|
||||
rationale: "The night ended without a reply the ledger took.".into(),
|
||||
resolved_on: eureka_pipeline::Date("2026-10-09".into()),
|
||||
})]);
|
||||
let (_, next) = next_night(&mind, "second", 3000);
|
||||
assert_eq!(day_ids(&next), vec![id("f0"), id("f1"), id("f2")], "a withdrawn Slept closing leaves its day owed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_first_nights_day_is_the_twenty_four_hours_before_it_opened_to_the_second() {
|
||||
let hour = 3_600_000;
|
||||
let memories = vec![
|
||||
("on_the_line".to_string(), MemorySort::Learned, wake(), 1000 - 24 * hour),
|
||||
("just_after".to_string(), MemorySort::Learned, wake(), 2000 - 24 * hour),
|
||||
];
|
||||
let (mind, run) = night_over(0, &memories);
|
||||
let night = context(&mind, &run, at(T0 + 5000)).unwrap();
|
||||
assert_eq!(day_ids(&night), vec![id("just_after")]);
|
||||
assert!(night.tide.iter().any(|held| held.id == id("on_the_line")), "the older one is backlog");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_consolidation_a_night_made_is_not_the_next_days_memory_whatever_its_origin() {
|
||||
let (mind, tonight) = night_over(0, &[("d1".to_string(), MemorySort::Learned, dm("!a:chat", &["@a:chat"]), 10)]);
|
||||
let shown = context(&mind, &tonight, at(T0 + 5000)).unwrap();
|
||||
// The night consolidates the DM memory: the consolidation keeps the room, so its origin is Direct, admitted after the night opened.
|
||||
mind.admit_at(at(T0 + 2000));
|
||||
let PipelineDocument::Memory(mut merged) = memory_from("dm-merged", MemorySort::Learned, "Merged.", &[], dm("!a:chat", &["@a:chat"]), 500) else { unreachable!() };
|
||||
merged.consolidates = vec![crate::testkit::reference(eureka_pipeline::PipelineKind::Memory, &id("d1"))];
|
||||
let by = crate::testkit::reference(eureka_pipeline::PipelineKind::Memory, &id("dm-merged"));
|
||||
mind.committed(vec![PipelineDocument::Memory(merged), crate::testkit::close(crate::testkit::reference(eureka_pipeline::PipelineKind::Memory, &id("d1")), ResolutionOutcome::Superseded { by: vec![by] })]);
|
||||
mind.admit_at(at(T0 + 2500));
|
||||
close_night(&mind, &tonight, &slept_over(&shown));
|
||||
remember_at(&mind, "fresh", wake(), 2800);
|
||||
let (_, next) = next_night(&mind, "tomorrow", 3000);
|
||||
assert_eq!(day_ids(&next), vec![id("fresh")], "the consolidation is not shown as her day");
|
||||
assert!(next.tide.iter().all(|held| held.id != id("dm-merged")), "nor does it come round as backlog");
|
||||
}
|
||||
|
|
@ -1,267 +1,36 @@
|
|||
//! The tempo rule of the shared heartbeat, pure: no clock, no mind, no I/O
|
||||
//! (rulings `eureka-body:ruling:shared-heartbeat-joint-lever`,
|
||||
//! `self-declared-urgency-etiquette`, `swarm-shares-log-bound`).
|
||||
//! Who takes the beat, pure: no clock, no mind, no I/O (ruling
|
||||
//! `eureka-body:ruling:swarm-shares-log-bound`).
|
||||
//!
|
||||
//! The Minds jointly hold the lever to one heartbeat. `tally` turns their votes
|
||||
//! into the beat; `pick` chooses who takes the turn when a beat falls. The
|
||||
//! heartbeat owns when a beat falls and what it launches; this module owns only
|
||||
//! the decisions: the swarm's tempo, who takes the turn, and the third, the
|
||||
//! beat when no vote is cast (`fallback_beat`, rulings
|
||||
//! `tempo-default-is-usage-fitter`, `tempo-fallback-pid-tuned-by-sim`).
|
||||
//! The heartbeat owns when a beat falls and what it wakes; this module owns
|
||||
//! only the decision of who takes it: `pick`, by `score`.
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// The beat, in seconds, when no vote is cast.
|
||||
pub const DEFAULT_BEAT_S: u32 = 900;
|
||||
|
||||
/// The shortest and longest beat the fallback controller may answer, in seconds.
|
||||
pub const FALLBACK_MIN_S: u32 = 60;
|
||||
pub const FALLBACK_MAX_S: u32 = 3600;
|
||||
|
||||
const WINDOW_MS: i64 = 7 * 24 * 3_600_000;
|
||||
const HOUR_MS: f64 = 3_600_000.0;
|
||||
/// A reading observed longer ago than this is no evidence of where usage is now:
|
||||
/// twice the longest beat, since every turn refreshes it.
|
||||
const STALE_AFTER_MS: i64 = 2 * FALLBACK_MAX_S as i64 * 1000;
|
||||
/// Two `resets_at` values this close name one window: the connector reports
|
||||
/// whole unix seconds, and a new window starts a week on.
|
||||
const RESETS_JITTER_MS: u64 = 60_000;
|
||||
/// How fast, in hours, the controller takes the wheel back from a released
|
||||
/// vote: the sensor's own poll period, since the vote's effect cannot show in a
|
||||
/// reading sooner.
|
||||
const CARRY_FADE_H: f64 = 0.25;
|
||||
/// The longest stretch of time one call may integrate: the longest beat, the
|
||||
/// longest wait between two calls of a running heartbeat. A longer silence (a
|
||||
/// restart, an outage) is not integrated as if the swarm had run through it.
|
||||
const MAX_STEP_MS: i64 = FALLBACK_MAX_S as i64 * 1000;
|
||||
/// The turn rate, in beats per hour, that `DEFAULT_BEAT_S` stands for: the
|
||||
/// controller's bias.
|
||||
const BIAS_PER_H: f64 = 3600.0 / DEFAULT_BEAT_S as f64;
|
||||
|
||||
/// The seven-day plan window as the connector last read it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Reading {
|
||||
pub used_permille: u32,
|
||||
pub observed_at_ms: i64,
|
||||
pub resets_at_ms: i64,
|
||||
}
|
||||
|
||||
/// PID gains. The actuator is the turn rate in beats per hour and the error is
|
||||
/// in permille of the plan, so `kp` is beats/h per permille, `ki` beats/h per
|
||||
/// permille-hour and `kd` beats/h per (permille/h).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Gains {
|
||||
pub kp: f64,
|
||||
pub ki: f64,
|
||||
pub kd: f64,
|
||||
}
|
||||
|
||||
/// Chosen by the committed simulation (`docs/research/2026-11-01-tempo-pid-sim.md`),
|
||||
/// never by a run-time search.
|
||||
pub const GAINS: Gains = Gains {
|
||||
kp: 0.02,
|
||||
ki: 0.01,
|
||||
kd: 0.0,
|
||||
};
|
||||
|
||||
/// The fallback controller's memory. The heartbeat owns persisting it; it is
|
||||
/// never published as truth. `rate_per_h` is the last output as a turn rate
|
||||
/// (zero: none yet); while a vote decides the caller sets it to the voted beat's
|
||||
/// rate, and the controller turns the gap between that and its own answer into
|
||||
/// `carry_per_h`, which fades after release, over the sensor's poll period
|
||||
/// whatever the vote's length.
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Fallback {
|
||||
pub integral: f64,
|
||||
pub last_used_permille: u32,
|
||||
pub last_observed_at_ms: i64,
|
||||
pub last_now_ms: i64,
|
||||
pub carry_per_h: f64,
|
||||
pub window_resets_at_ms: i64,
|
||||
pub rate_per_h: f64,
|
||||
}
|
||||
|
||||
/// The beat when no vote is cast: a PID on the gap between the seven-day usage
|
||||
/// trajectory to `target_permille` and what was used, acting on the turn rate
|
||||
/// (behind the trajectory raises it). Derivative on the measurement, only
|
||||
/// between distinct observations; conditional integration: it does not
|
||||
/// integrate error in the direction of a saturated actuator. `saturated` is the
|
||||
/// caller's one report that a shorter beat cannot buy more spend: the last beat
|
||||
/// was the floor, or launched nothing because every eligible Mind was busy
|
||||
/// (ruling `tempo-controller-knows-the-ceiling`); the slow limit needs no report,
|
||||
/// since the integral's own answer at it is the controller's. One call
|
||||
/// integrates at most `MAX_STEP_MS` of time.
|
||||
/// While `voted` nothing integrates and the controller notes how far the voted
|
||||
/// beat (held inside the beat bounds) is from its own answer, so release starts
|
||||
/// at the voted beat, to within the one integration step of the first free call,
|
||||
/// and the controller takes the wheel back within a poll period or two. The
|
||||
/// carry fades on that period whatever the vote's length, so a vote of a minute
|
||||
/// costs the extra turns of a poll period or two of fast beat. A new window clears the integral and the derivative memory;
|
||||
/// with no usable reading (none, an expired window, or one observed more than
|
||||
/// `STALE_AFTER_MS` ago, which a connector replaying its last answer keeps
|
||||
/// producing) the last beat holds (`DEFAULT_BEAT_S` before any).
|
||||
pub fn fallback_beat(
|
||||
state: &mut Fallback,
|
||||
reading: Option<&Reading>,
|
||||
now_ms: i64,
|
||||
target_permille: u32,
|
||||
voted: bool,
|
||||
saturated: bool,
|
||||
) -> u32 {
|
||||
control(
|
||||
&GAINS,
|
||||
state,
|
||||
reading,
|
||||
now_ms,
|
||||
target_permille,
|
||||
voted,
|
||||
saturated,
|
||||
)
|
||||
}
|
||||
|
||||
fn beat_of(rate_per_h: f64) -> u32 {
|
||||
if rate_per_h > 0.0 {
|
||||
((3600.0 / rate_per_h)
|
||||
.round()
|
||||
.clamp(f64::from(FALLBACK_MIN_S), f64::from(FALLBACK_MAX_S))) as u32
|
||||
} else {
|
||||
FALLBACK_MAX_S
|
||||
}
|
||||
}
|
||||
|
||||
fn held(state: &Fallback) -> u32 {
|
||||
if state.rate_per_h == 0.0 {
|
||||
DEFAULT_BEAT_S
|
||||
} else {
|
||||
beat_of(state.rate_per_h)
|
||||
}
|
||||
}
|
||||
|
||||
/// The one controller; `fallback_beat` is it with `GAINS`, the simulation is it
|
||||
/// with candidates.
|
||||
fn control(
|
||||
gains: &Gains,
|
||||
state: &mut Fallback,
|
||||
reading: Option<&Reading>,
|
||||
now_ms: i64,
|
||||
target_permille: u32,
|
||||
voted: bool,
|
||||
saturated: bool,
|
||||
) -> u32 {
|
||||
let dt_h = if state.last_now_ms > 0 {
|
||||
(now_ms - state.last_now_ms).clamp(0, MAX_STEP_MS) as f64 / HOUR_MS
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
state.last_now_ms = now_ms;
|
||||
let Some(r) = reading.filter(|r| {
|
||||
now_ms < r.resets_at_ms
|
||||
&& r.observed_at_ms >= r.resets_at_ms - WINDOW_MS
|
||||
&& now_ms - r.observed_at_ms <= STALE_AFTER_MS
|
||||
}) else {
|
||||
return held(state);
|
||||
};
|
||||
let rolled = r.resets_at_ms.abs_diff(state.window_resets_at_ms) > RESETS_JITTER_MS;
|
||||
if rolled {
|
||||
state.integral = 0.0;
|
||||
state.last_used_permille = r.used_permille;
|
||||
state.last_observed_at_ms = r.observed_at_ms;
|
||||
}
|
||||
state.window_resets_at_ms = r.resets_at_ms;
|
||||
let elapsed = (now_ms - (r.resets_at_ms - WINDOW_MS)).clamp(0, WINDOW_MS) as f64;
|
||||
let error =
|
||||
f64::from(target_permille) * elapsed / WINDOW_MS as f64 - f64::from(r.used_permille);
|
||||
let fresh = r.observed_at_ms > state.last_observed_at_ms;
|
||||
let slope = if fresh {
|
||||
(f64::from(r.used_permille) - f64::from(state.last_used_permille))
|
||||
/ ((r.observed_at_ms - state.last_observed_at_ms) as f64 / HOUR_MS)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
if fresh {
|
||||
state.last_used_permille = r.used_permille;
|
||||
state.last_observed_at_ms = r.observed_at_ms;
|
||||
}
|
||||
let rate =
|
||||
|integral: f64| BIAS_PER_H + gains.kp * error + gains.ki * integral - gains.kd * slope;
|
||||
let (low, high) = (
|
||||
3600.0 / f64::from(FALLBACK_MAX_S),
|
||||
3600.0 / f64::from(FALLBACK_MIN_S),
|
||||
);
|
||||
if voted {
|
||||
// Nothing integrates. The carry is the gap between the voted rate (held
|
||||
// inside the beat bounds, which are all the controller can answer) and
|
||||
// what the controller would answer now, so release starts at the voted
|
||||
// beat; it fades once the vote is gone.
|
||||
state.carry_per_h = if state.rate_per_h > 0.0 {
|
||||
state.rate_per_h.clamp(low, high) - rate(state.integral)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
return held(state);
|
||||
}
|
||||
// The slow limit is judged on the integral's own answer: the carry is a
|
||||
// transient that fades and must not decide what the integral absorbs.
|
||||
let winding = (saturated && error > 0.0) || (rate(state.integral) <= low && error < 0.0);
|
||||
if !winding && !rolled {
|
||||
state.integral += error * dt_h;
|
||||
}
|
||||
let beat = beat_of(rate(state.integral) + state.carry_per_h);
|
||||
state.rate_per_h = 3600.0 / f64::from(beat);
|
||||
state.carry_per_h *= (-dt_h / CARRY_FADE_H).exp();
|
||||
beat
|
||||
}
|
||||
|
||||
/// How many orders of magnitude a full urgency may multiply a Mind's wait by.
|
||||
/// Interim, per `swarm-shares-log-bound`, until the swarm's governance sets it.
|
||||
pub const LOG_BOUND_ORDERS: f64 = 1.0;
|
||||
|
||||
/// The swarm's tempo (ruling `eureka-body:ruling:tempo-silence-is-abstention`):
|
||||
/// the lower median of the votes actually cast, each a Mind's newest standing
|
||||
/// vote in seconds per beat. Silence is abstention: a Mind with no standing vote
|
||||
/// (never voted, or withdrew) or whose mind cannot be read is `None` and is not
|
||||
/// counted, so the beat follows those who care to vote. With no vote cast it is
|
||||
/// `DEFAULT_BEAT_S`. On one voter that voter decides; on two the lower of them.
|
||||
pub fn tally(votes: &[Option<u32>]) -> u32 {
|
||||
let mut cast: Vec<u32> = votes.iter().flatten().copied().collect();
|
||||
cast.sort_unstable();
|
||||
match cast.len() {
|
||||
0 => DEFAULT_BEAT_S,
|
||||
n => cast[(n - 1) / 2],
|
||||
}
|
||||
}
|
||||
|
||||
/// What the pick sees of one Mind.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Standing {
|
||||
pub slug: String,
|
||||
/// Her brake is released and she is not in a turn.
|
||||
pub eligible: bool,
|
||||
/// Seconds since her last Persona turn; none when she has never had one.
|
||||
/// Seconds since the heartbeat last woke her for a turn; none when it never has.
|
||||
pub waited_s: Option<u64>,
|
||||
/// When her last Persona turn began, in unix milliseconds; none when never.
|
||||
/// When the heartbeat last woke her for a turn, in unix milliseconds; none when never.
|
||||
pub last_turn_ms: Option<i64>,
|
||||
/// The urgency she declared for herself and no turn has consumed, in thousandths.
|
||||
pub self_permille: u16,
|
||||
/// The urgency her subscriptions give her pending deliveries, in thousandths.
|
||||
pub topic_permille: u16,
|
||||
/// A member (not the operator) has a pending message that names or tags her.
|
||||
pub addressed_by_member: bool,
|
||||
}
|
||||
|
||||
/// The urgency in force for a Mind, in thousandths: the strongest of what she
|
||||
/// declared, what her topics say and a member addressing her (which is 1000).
|
||||
/// The urgency in force for a Mind, in thousandths: the stronger of what her
|
||||
/// topics say and a member addressing her (which is 1000).
|
||||
pub fn urgency_permille(standing: &Standing) -> u16 {
|
||||
let addressed = if standing.addressed_by_member {
|
||||
1000
|
||||
} else {
|
||||
0
|
||||
};
|
||||
standing
|
||||
.self_permille
|
||||
.max(standing.topic_permille)
|
||||
.max(addressed)
|
||||
.min(1000)
|
||||
let addressed = if standing.addressed_by_member { 1000 } else { 0 };
|
||||
standing.topic_permille.max(addressed).min(1000)
|
||||
}
|
||||
|
||||
/// `(waited / beat) x 10^(LOG_BOUND_ORDERS x urgency / 1000)`. A Mind that has
|
||||
|
|
@ -292,7 +61,5 @@ pub fn pick(rows: &[Standing], beat_s: u32) -> Option<usize> {
|
|||
.map(|(index, _)| index)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod sim;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
|
|
|||
|
|
@ -1,519 +0,0 @@
|
|||
//! The plant the fallback controller is tuned against: one seven-day plan
|
||||
//! window at one-minute steps, each beat a Persona turn costing a lognormal
|
||||
//! number of permille, plus the operator's own usage on the same subscription.
|
||||
//! The swarm has a throughput ceiling: a turn needs a Mind that is not in one,
|
||||
//! each turn holds its Mind for a lognormal time, and when every Mind is busy
|
||||
//! the beat launches nothing and the controller is told so.
|
||||
//! Test-only. The controller here is `super::control`, the same one the
|
||||
//! heartbeat will call through `fallback_beat`; the sweep passes it candidate
|
||||
//! gains, `GAINS` is committed. Rerun the sweep with
|
||||
//! `cargo test -p mind-body tempo::sim::sweep -- --ignored --nocapture`.
|
||||
|
||||
use super::*;
|
||||
|
||||
const STEP_MS: i64 = 60_000;
|
||||
const POLL_MS: i64 = 15 * 60_000;
|
||||
const W0: i64 = 1_000 * 3_600_000;
|
||||
const TARGET: u32 = 900;
|
||||
/// Sigma of ln(turn cost).
|
||||
const COST_SIGMA: f64 = 0.5;
|
||||
/// Every this-many turns is a Self run, costing `SELF_FACTOR` times a Persona turn.
|
||||
const SELF_EVERY: u32 = 25;
|
||||
const SELF_FACTOR: f64 = 5.0;
|
||||
/// Half-width, in permille, of the band the error must leave to count as a side.
|
||||
const BAND: f64 = 15.0;
|
||||
/// The roster the heartbeat routes (aqua, eureka, heimdall, kiko, libby, nibu,
|
||||
/// void, weksa: the slugs it launched over 2026-10-30..11-01).
|
||||
const MINDS: usize = 8;
|
||||
/// How long a Persona turn holds its Mind, lognormal. Measured on Yggdrasil from
|
||||
/// the `mind-persona@*` start and stop lines over 29 h (1284 turns): mean 15.4 s,
|
||||
/// median 6.4 s, p90 27.7 s, p99 186 s; the fit below has median 7.5 s, p90 35 s.
|
||||
const BUSY_MEAN_S: f64 = 15.4;
|
||||
const BUSY_SIGMA: f64 = 1.2;
|
||||
/// The shortest spacing between two on-beat launches is one heartbeat poll
|
||||
/// (median 30 s over 4100 polls); the sim's step, 60 s, is the beat floor and is
|
||||
/// longer, so the spacing never binds.
|
||||
const POLL_S: i64 = 30;
|
||||
const _: () = assert!(POLL_S * 1000 <= STEP_MS);
|
||||
|
||||
struct Rng(u64);
|
||||
|
||||
impl Rng {
|
||||
fn next(&mut self) -> f64 {
|
||||
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
|
||||
let mut z = self.0;
|
||||
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
||||
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
|
||||
((z ^ (z >> 31)) >> 11) as f64 / (1u64 << 53) as f64
|
||||
}
|
||||
|
||||
fn lognormal(&mut self, mean: f64, sigma: f64) -> f64 {
|
||||
let (a, b) = (self.next().max(1e-12), self.next());
|
||||
let normal = (-2.0 * a.ln()).sqrt() * (std::f64::consts::TAU * b).cos();
|
||||
(mean.ln() - sigma * sigma / 2.0 + sigma * normal).exp()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum Sensor {
|
||||
/// The reading refreshes when a turn ends.
|
||||
Stream,
|
||||
/// The reading refreshes every fifteen minutes.
|
||||
Poll,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Scenario {
|
||||
name: &'static str,
|
||||
windows: u32,
|
||||
start_used: f64,
|
||||
/// Permille per Persona turn.
|
||||
mean_cost: f64,
|
||||
/// (from hour, to hour, permille spread evenly) of the operator's own usage.
|
||||
burst: Option<(f64, f64, f64)>,
|
||||
/// (at hour, factor) on the per-turn cost.
|
||||
cost_step: Option<(f64, f64)>,
|
||||
/// (from hour, to hour, beat) of a standing vote.
|
||||
vote: Option<(f64, f64, u32)>,
|
||||
/// (from hour, to hour) with no reading.
|
||||
gap: Option<(f64, f64)>,
|
||||
/// The cost is too low to reach the target even at the minimum beat.
|
||||
unreachable: bool,
|
||||
/// The vote itself spends the week: no controller can end on target, it
|
||||
/// can only stop burning once the vote is gone.
|
||||
overspent: bool,
|
||||
/// Minds the heartbeat can launch, and the factor on how long a turn holds one.
|
||||
minds: usize,
|
||||
busy_scale: f64,
|
||||
/// The most band crossings the controller may show here.
|
||||
max_crossings: u32,
|
||||
/// The ceiling is below what the target needs for most of the week: no
|
||||
/// controller ends on target, and the scenario only compares the controller
|
||||
/// told of the stalls with the one that is not.
|
||||
stress: bool,
|
||||
}
|
||||
|
||||
const BASE: Scenario = Scenario {
|
||||
name: "quiet",
|
||||
windows: 1,
|
||||
start_used: 0.0,
|
||||
mean_cost: 1.3,
|
||||
burst: None,
|
||||
cost_step: None,
|
||||
vote: None,
|
||||
gap: None,
|
||||
unreachable: false,
|
||||
overspent: false,
|
||||
minds: MINDS,
|
||||
busy_scale: 1.0,
|
||||
max_crossings: 2,
|
||||
stress: false,
|
||||
};
|
||||
|
||||
fn scenarios() -> Vec<Scenario> {
|
||||
vec![
|
||||
BASE,
|
||||
Scenario {
|
||||
name: "cheap-turns",
|
||||
mean_cost: 0.6,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "dear-turns",
|
||||
mean_cost: 3.0,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "burst",
|
||||
burst: Some((48.0, 52.0, 200.0)),
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "cost-x3",
|
||||
cost_step: Some((84.0, 3.0)),
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "vote-300s-24h",
|
||||
vote: Some((48.0, 72.0, 300)),
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "vote-60s-1h",
|
||||
vote: Some((48.0, 49.0, 60)),
|
||||
max_crossings: 3,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "vote-60s-4h",
|
||||
vote: Some((48.0, 52.0, 60)),
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "vote-60s-24h",
|
||||
vote: Some((48.0, 72.0, 60)),
|
||||
overspent: true,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "vote-7200s-24h",
|
||||
vote: Some((48.0, 72.0, 7200)),
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "vote-86400s-24h",
|
||||
vote: Some((48.0, 72.0, 86_400)),
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "cheap-then-x3",
|
||||
mean_cost: 0.6,
|
||||
cost_step: Some((84.0, 3.0)),
|
||||
max_crossings: 3,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "one-slow-mind",
|
||||
mean_cost: 0.6,
|
||||
cost_step: Some((84.0, 3.0)),
|
||||
minds: 1,
|
||||
busy_scale: 45.0,
|
||||
stress: true,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "rollover",
|
||||
windows: 2,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "gap-6h",
|
||||
gap: Some((72.0, 78.0)),
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "start-ahead",
|
||||
start_used: 500.0,
|
||||
..BASE
|
||||
},
|
||||
Scenario {
|
||||
name: "unreachable",
|
||||
mean_cost: 0.05,
|
||||
unreachable: true,
|
||||
..BASE
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
struct Outcome {
|
||||
/// Used at each window's reset.
|
||||
final_used: Vec<f64>,
|
||||
/// Worst |trajectory - used| over the last day of the last window.
|
||||
worst_error_last_day: f64,
|
||||
/// Beat moves of more than 5% between minutes, per hour, outside votes.
|
||||
jumps_per_h: f64,
|
||||
/// Times the error crossed from beyond +BAND to beyond -BAND (or back)
|
||||
/// during the last three days.
|
||||
band_crossings: u32,
|
||||
/// Share of the last day of the last window spent at the minimum beat.
|
||||
frac_at_min: f64,
|
||||
/// The same at the maximum beat.
|
||||
frac_at_max: f64,
|
||||
/// The beat on the last minute.
|
||||
last_beat: u32,
|
||||
/// Minutes the beat was due and every Mind was busy.
|
||||
stalls: u32,
|
||||
}
|
||||
|
||||
fn run(scenario: &Scenario, sensor: Sensor, gains: &Gains, seed: u64) -> Outcome {
|
||||
drive(scenario, sensor, gains, seed, true)
|
||||
}
|
||||
|
||||
/// `report_stalls` false withholds the busy half of the saturation report (the
|
||||
/// floor half is always given): the controller as it was before it was told.
|
||||
fn drive(
|
||||
scenario: &Scenario,
|
||||
sensor: Sensor,
|
||||
gains: &Gains,
|
||||
seed: u64,
|
||||
report_stalls: bool,
|
||||
) -> Outcome {
|
||||
let mut rng = Rng(seed);
|
||||
let mut busy_rng = Rng(seed ^ 0xB05E_B05E_B05E_B05E);
|
||||
let mut busy_until = vec![W0; scenario.minds];
|
||||
// The heartbeat's report: a beat since the last launch found every Mind busy.
|
||||
let (mut stalled, mut found_busy) = (false, false);
|
||||
let mut state = Fallback::default();
|
||||
let mut used = scenario.start_used;
|
||||
let mut resets = W0 + WINDOW_MS;
|
||||
let mut window = 0u32;
|
||||
let sense = |used: f64, now: i64, resets: i64| Reading {
|
||||
used_permille: used.round().clamp(0.0, 1000.0) as u32,
|
||||
observed_at_ms: now,
|
||||
resets_at_ms: resets,
|
||||
};
|
||||
let mut reading = sense(used, W0, resets);
|
||||
let (mut last_turn, mut turns, mut prev_beat) = (W0, 0u32, DEFAULT_BEAT_S);
|
||||
let steps = i64::from(scenario.windows) * WINDOW_MS / STEP_MS;
|
||||
let mut out = Outcome::default();
|
||||
let (mut jumps, mut at_min, mut at_max, mut last_day) = (0u32, 0u32, 0u32, 0u32);
|
||||
let mut side = 0i8;
|
||||
let mut worst_day = 0f64;
|
||||
for i in 1..steps {
|
||||
let now = W0 + i * STEP_MS;
|
||||
if now >= resets {
|
||||
out.final_used.push(used);
|
||||
used = 0.0;
|
||||
resets += WINDOW_MS;
|
||||
window += 1;
|
||||
}
|
||||
let in_window = (now - (resets - WINDOW_MS)) as f64 / HOUR_MS;
|
||||
if let Some((from, to, amount)) = scenario.burst
|
||||
&& in_window >= from
|
||||
&& in_window < to
|
||||
{
|
||||
used += amount * STEP_MS as f64 / HOUR_MS / (to - from);
|
||||
}
|
||||
let gap = scenario
|
||||
.gap
|
||||
.is_some_and(|(from, to)| in_window >= from && in_window < to);
|
||||
if sensor == Sensor::Poll && !gap && (now - W0) % POLL_MS == 0 {
|
||||
reading = sense(used, now, resets);
|
||||
}
|
||||
let vote = scenario
|
||||
.vote
|
||||
.filter(|&(from, to, _)| in_window >= from && in_window < to)
|
||||
.map(|v| v.2);
|
||||
let shown = if gap { None } else { Some(reading) };
|
||||
let beat = match vote {
|
||||
Some(beat) => {
|
||||
state.rate_per_h = 3600.0 / f64::from(beat);
|
||||
control(gains, &mut state, shown.as_ref(), now, TARGET, true, false);
|
||||
beat
|
||||
}
|
||||
None => {
|
||||
let saturated = prev_beat == FALLBACK_MIN_S || (report_stalls && stalled);
|
||||
control(
|
||||
gains,
|
||||
&mut state,
|
||||
shown.as_ref(),
|
||||
now,
|
||||
TARGET,
|
||||
false,
|
||||
saturated,
|
||||
)
|
||||
}
|
||||
};
|
||||
if vote.is_none() {
|
||||
if window + 1 == scenario.windows && in_window >= 144.0 {
|
||||
last_day += 1;
|
||||
at_min += u32::from(beat == FALLBACK_MIN_S);
|
||||
at_max += u32::from(beat == FALLBACK_MAX_S);
|
||||
}
|
||||
jumps += u32::from(f64::from(beat.abs_diff(prev_beat)) > 0.05 * f64::from(prev_beat));
|
||||
}
|
||||
prev_beat = beat;
|
||||
out.last_beat = beat;
|
||||
if now - last_turn >= i64::from(beat) * 1000 {
|
||||
if let Some(mind) = busy_until.iter().position(|&until| until <= now) {
|
||||
stalled = found_busy;
|
||||
found_busy = false;
|
||||
let step =
|
||||
scenario.cost_step.map_or(
|
||||
1.0,
|
||||
|(at, factor)| if in_window >= at { factor } else { 1.0 },
|
||||
);
|
||||
turns += 1;
|
||||
let self_run = if turns % SELF_EVERY == 0 {
|
||||
SELF_FACTOR
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
used += rng.lognormal(scenario.mean_cost * step, COST_SIGMA) * self_run;
|
||||
busy_until[mind] = now
|
||||
+ (busy_rng.lognormal(BUSY_MEAN_S * scenario.busy_scale, BUSY_SIGMA) * 1000.0)
|
||||
as i64;
|
||||
last_turn = now;
|
||||
if sensor == Sensor::Stream && !gap {
|
||||
reading = sense(used.min(1000.0), now, resets);
|
||||
}
|
||||
} else {
|
||||
stalled = true;
|
||||
found_busy = true;
|
||||
out.stalls += 1;
|
||||
}
|
||||
}
|
||||
used = used.min(1000.0);
|
||||
let error =
|
||||
f64::from(TARGET) * (now - (resets - WINDOW_MS)) as f64 / WINDOW_MS as f64 - used;
|
||||
if window + 1 == scenario.windows {
|
||||
if in_window >= 144.0 {
|
||||
worst_day = worst_day.max(error.abs());
|
||||
}
|
||||
if in_window >= 96.0 {
|
||||
let now_side = if error > BAND {
|
||||
1
|
||||
} else if error < -BAND {
|
||||
-1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
if now_side != 0 {
|
||||
out.band_crossings += u32::from(side != 0 && side != now_side);
|
||||
side = now_side;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out.final_used.push(used);
|
||||
out.worst_error_last_day = worst_day;
|
||||
out.jumps_per_h = f64::from(jumps) / (f64::from(scenario.windows) * 168.0);
|
||||
out.frac_at_min = f64::from(at_min) / f64::from(last_day.max(1));
|
||||
out.frac_at_max = f64::from(at_max) / f64::from(last_day.max(1));
|
||||
out
|
||||
}
|
||||
|
||||
const SENSORS: [Sensor; 2] = [Sensor::Stream, Sensor::Poll];
|
||||
const SEEDS: [u64; 3] = [11, 23, 47];
|
||||
|
||||
/// The most chatter, in jumps per hour, `GAINS` may show on any scenario.
|
||||
const JUMPS_PER_H_BOUND: f64 = 0.5;
|
||||
/// How far from the target, in permille, a reachable scenario may end.
|
||||
const FINAL_TOLERANCE: f64 = 30.0;
|
||||
/// The worst |error| on the last day: the vote scenarios' recovery sets it.
|
||||
const LAST_DAY_ERROR_BOUND: f64 = 75.0;
|
||||
|
||||
#[test]
|
||||
fn sim_scenarios_meet_target() {
|
||||
for scenario in scenarios() {
|
||||
for sensor in SENSORS {
|
||||
for seed in SEEDS {
|
||||
let o = run(&scenario, sensor, &GAINS, seed);
|
||||
let tag = format!("{} {sensor:?} seed {seed}: {o:?}", scenario.name);
|
||||
assert!(o.jumps_per_h <= JUMPS_PER_H_BOUND, "chatter {tag}");
|
||||
if scenario.stress {
|
||||
continue;
|
||||
}
|
||||
if scenario.unreachable {
|
||||
assert_eq!(o.last_beat, FALLBACK_MIN_S, "saturated {tag}");
|
||||
assert!(o.frac_at_min > 0.9, "saturated {tag}");
|
||||
assert!(
|
||||
o.final_used.iter().all(|u| *u < f64::from(TARGET) - 100.0),
|
||||
"{tag}"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if scenario.overspent {
|
||||
assert_eq!(o.last_beat, FALLBACK_MAX_S, "backs off {tag}");
|
||||
assert!(o.frac_at_max > 0.9, "backs off {tag}");
|
||||
continue;
|
||||
}
|
||||
for used in &o.final_used {
|
||||
assert!(
|
||||
(used - f64::from(TARGET)).abs() <= FINAL_TOLERANCE,
|
||||
"final {tag}"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
o.worst_error_last_day <= LAST_DAY_ERROR_BOUND,
|
||||
"error {tag}"
|
||||
);
|
||||
assert!(
|
||||
o.band_crossings <= scenario.max_crossings,
|
||||
"oscillation {tag}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `cargo test -p mind-body tempo::sim::sweep -- --ignored --nocapture`
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn sweep() {
|
||||
let scen = scenarios();
|
||||
let mut rows = Vec::new();
|
||||
for kp in [0.01, 0.02, 0.03, 0.05, 0.1] {
|
||||
for ki in [0.002, 0.005, 0.01, 0.02, 0.04] {
|
||||
for kd in [0.0, 0.002, 0.005, 0.01, 0.02, 0.05] {
|
||||
let g = Gains { kp, ki, kd };
|
||||
let (mut final_err, mut err_day, mut jumps, mut cross) = (0f64, 0f64, 0f64, 0u32);
|
||||
let mut sat_ok = true;
|
||||
let mut detail = String::new();
|
||||
for s in &scen {
|
||||
let (mut f, mut e, mut j, mut x) = (0f64, 0f64, 0f64, 0u32);
|
||||
let (mut lastb, mut stalls) = (0, 0);
|
||||
for sensor in SENSORS {
|
||||
for seed in SEEDS {
|
||||
let o = run(s, sensor, &g, seed);
|
||||
j = j.max(o.jumps_per_h);
|
||||
if s.stress {
|
||||
} else if s.unreachable {
|
||||
sat_ok &= o.last_beat == FALLBACK_MIN_S && o.frac_at_min > 0.9;
|
||||
} else if s.overspent {
|
||||
sat_ok &= o.last_beat == FALLBACK_MAX_S && o.frac_at_max > 0.9;
|
||||
} else {
|
||||
f = o
|
||||
.final_used
|
||||
.iter()
|
||||
.fold(f, |m, u| m.max((u - f64::from(TARGET)).abs()));
|
||||
e = e.max(o.worst_error_last_day);
|
||||
cross = cross.max(o.band_crossings);
|
||||
x = x.max(o.band_crossings);
|
||||
}
|
||||
lastb = o.last_beat;
|
||||
stalls = stalls.max(o.stalls);
|
||||
}
|
||||
}
|
||||
final_err = final_err.max(f);
|
||||
err_day = err_day.max(e);
|
||||
jumps = jumps.max(j);
|
||||
detail.push_str(&format!(
|
||||
" {:<14} final_err {f:6.1} worst_err_last_day {e:6.1} jumps/h {j:5.2} crossings {x} last_beat {lastb} stalled_min {stalls}\n",
|
||||
s.name
|
||||
));
|
||||
}
|
||||
let rank = final_err + 0.5 * err_day + 20.0 * jumps + 15.0 * f64::from(cross);
|
||||
rows.push((rank, g, final_err, err_day, jumps, cross, sat_ok, detail));
|
||||
}
|
||||
}
|
||||
}
|
||||
rows.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
println!(
|
||||
"kp ki kd | worst final err | worst err last day | worst jumps/h | worst crossings | saturates when unreachable or overspent"
|
||||
);
|
||||
for r in &rows {
|
||||
println!(
|
||||
"{} {} {} | {:.1} | {:.1} | {:.2} | {} | {}",
|
||||
r.1.kp, r.1.ki, r.1.kd, r.2, r.3, r.4, r.5, r.6
|
||||
);
|
||||
}
|
||||
for r in rows.iter().take(4) {
|
||||
println!("\nBEST {:?}\n{}", r.1, r.7);
|
||||
}
|
||||
if let Some(r) = rows.iter().find(|r| r.1 == GAINS) {
|
||||
println!("\nCHOSEN {:?}\n{}", r.1, r.7);
|
||||
}
|
||||
}
|
||||
|
||||
/// Against a ceiling the target cannot be reached under, the controller told
|
||||
/// that a beat found every Mind busy winds its integral less, so it overspends
|
||||
/// less at the end than the one that is not told.
|
||||
#[test]
|
||||
fn the_busy_report_lowers_the_overshoot_against_a_ceiling() {
|
||||
let scenario = scenarios().into_iter().find(|s| s.stress).unwrap();
|
||||
for sensor in SENSORS {
|
||||
for seed in SEEDS {
|
||||
let told = drive(&scenario, sensor, &GAINS, seed, true);
|
||||
let blind = drive(&scenario, sensor, &GAINS, seed, false);
|
||||
let tag = format!("{sensor:?} seed {seed}: told {told:?} blind {blind:?}");
|
||||
assert!(told.stalls > 0, "the ceiling binds {tag}");
|
||||
assert!(told.final_used[0] <= blind.final_used[0], "final {tag}");
|
||||
assert!(
|
||||
told.worst_error_last_day < blind.worst_error_last_day,
|
||||
"error {tag}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
//! The tempo rule as tables: what `tally` and `pick` answer for the cases that
|
||||
//! carry the rulings. Pure, so nothing here needs a mind, a clock or a daemon.
|
||||
//! The pick as tables: what `pick` and `score` answer for the cases that carry
|
||||
//! the rulings. Pure, so nothing here needs a mind, a clock or a daemon.
|
||||
|
||||
use super::*;
|
||||
|
||||
|
|
@ -9,7 +9,6 @@ fn mind(slug: &str, waited_s: Option<u64>) -> Standing {
|
|||
eligible: true,
|
||||
waited_s,
|
||||
last_turn_ms: waited_s.map(|waited| 1_000_000 - i64::try_from(waited).unwrap() * 1000),
|
||||
self_permille: 0,
|
||||
topic_permille: 0,
|
||||
addressed_by_member: false,
|
||||
}
|
||||
|
|
@ -20,75 +19,17 @@ fn the_score_is_the_wait_in_beats_times_ten_to_the_urgency_in_orders() {
|
|||
let mut standing = mind("a", Some(1800));
|
||||
assert_eq!(score(&standing, 900), 2.0, "two beats waited, no urgency");
|
||||
assert_eq!(score(&standing, 600), 3.0);
|
||||
standing.self_permille = 1000;
|
||||
standing.topic_permille = 1000;
|
||||
assert_eq!(
|
||||
score(&standing, 900),
|
||||
20.0,
|
||||
"full urgency is worth one order of magnitude"
|
||||
);
|
||||
standing.self_permille = 500;
|
||||
standing.topic_permille = 500;
|
||||
assert!((score(&standing, 900) - 2.0 * 10f64.sqrt()).abs() < 1e-9);
|
||||
assert_eq!(score(&mind("never", None), 900), f64::INFINITY);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_votes_is_the_default_beat() {
|
||||
assert_eq!(tally(&[]), DEFAULT_BEAT_S);
|
||||
assert_eq!(tally(&[None, None]), DEFAULT_BEAT_S);
|
||||
assert_eq!(DEFAULT_BEAT_S, 900);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_tally_is_the_lower_median_and_one_outlier_cannot_move_it() {
|
||||
let table: [(&[u32], u32); 7] = [
|
||||
(&[600], 600),
|
||||
(&[300, 900], 300),
|
||||
(&[900, 300, 600], 600),
|
||||
(&[100, 300, 600, 900], 300),
|
||||
(&[600, 600, 86_400], 600),
|
||||
(&[60, 600, 600], 600),
|
||||
(&[60, 60, 86_400, 86_400, 600], 600),
|
||||
];
|
||||
for (votes, want) in table {
|
||||
let votes: Vec<Option<u32>> = votes.iter().copied().map(Some).collect();
|
||||
assert_eq!(tally(&votes), want, "{votes:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn silence_is_abstention_and_only_cast_votes_are_counted() {
|
||||
let mut eight = vec![None; 8];
|
||||
eight[0] = Some(60);
|
||||
assert_eq!(
|
||||
tally(&eight),
|
||||
60,
|
||||
"one voter among eight silent Minds decides"
|
||||
);
|
||||
assert_eq!(
|
||||
tally(&[None; 8]),
|
||||
DEFAULT_BEAT_S,
|
||||
"none cast is the default"
|
||||
);
|
||||
assert_eq!(
|
||||
tally(&[Some(3600), None, Some(60)]),
|
||||
60,
|
||||
"two cast: the lower median"
|
||||
);
|
||||
assert_eq!(
|
||||
tally(&[Some(3600), Some(1800), None, None, Some(60)]),
|
||||
1800,
|
||||
"abstainers do not dilute the median"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_tally_does_not_depend_on_the_order_the_votes_came_in() {
|
||||
assert_eq!(
|
||||
tally(&[Some(900), Some(60), None, Some(600)]),
|
||||
tally(&[None, Some(60), Some(600), Some(900)])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn waiting_alone_round_robins() {
|
||||
let rows = [
|
||||
|
|
@ -115,11 +56,11 @@ fn a_full_urgency_equals_ten_times_the_wait_at_one_order() {
|
|||
assert_eq!(LOG_BOUND_ORDERS, 1.0);
|
||||
let steady = mind("ann", Some(100));
|
||||
let mut urgent = mind("bob", Some(11));
|
||||
urgent.self_permille = 1000;
|
||||
urgent.addressed_by_member = true;
|
||||
assert_eq!(
|
||||
pick(&[steady.clone(), urgent.clone()], 100),
|
||||
Some(1),
|
||||
"eleven seconds at full urgency beat a hundred waiting"
|
||||
"eleven seconds addressed beat a hundred waiting"
|
||||
);
|
||||
urgent.waited_s = Some(9);
|
||||
urgent.last_turn_ms = Some(1_000_000 - 9000);
|
||||
|
|
@ -129,24 +70,22 @@ fn a_full_urgency_equals_ten_times_the_wait_at_one_order() {
|
|||
#[test]
|
||||
fn a_half_urgency_is_the_square_root_of_ten() {
|
||||
let mut half = mind("ann", Some(100));
|
||||
half.self_permille = 500;
|
||||
half.topic_permille = 500;
|
||||
let plain = mind("bob", Some(100));
|
||||
let ratio = score(&half, 100) / score(&plain, 100);
|
||||
assert!((ratio - 10f64.sqrt()).abs() < 1e-9, "{ratio}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn urgency_is_the_strongest_of_self_topic_and_a_member_addressing_her() {
|
||||
fn urgency_is_the_stronger_of_topic_and_a_member_addressing_her() {
|
||||
let mut row = mind("ann", Some(100));
|
||||
assert_eq!(urgency_permille(&row), 0);
|
||||
row.self_permille = 300;
|
||||
row.topic_permille = 700;
|
||||
assert_eq!(urgency_permille(&row), 700);
|
||||
row.addressed_by_member = true;
|
||||
assert_eq!(urgency_permille(&row), 1000);
|
||||
row.addressed_by_member = false;
|
||||
row.topic_permille = 0;
|
||||
row.self_permille = 5000;
|
||||
row.topic_permille = 5000;
|
||||
assert_eq!(
|
||||
urgency_permille(&row),
|
||||
1000,
|
||||
|
|
@ -187,7 +126,7 @@ fn a_mind_that_never_acted_goes_first_and_the_never_acted_tie_by_slug() {
|
|||
fn a_braked_or_busy_mind_is_never_picked_however_high_she_scores() {
|
||||
let mut rows = [mind("ann", Some(10)), mind("bob", Some(100_000))];
|
||||
rows[1].eligible = false;
|
||||
rows[1].self_permille = 1000;
|
||||
rows[1].addressed_by_member = true;
|
||||
assert_eq!(pick(&rows, 100), Some(0));
|
||||
rows[0].eligible = false;
|
||||
assert_eq!(pick(&rows, 100), None, "nobody eligible, nobody picked");
|
||||
|
|
@ -199,717 +138,3 @@ fn a_longer_beat_does_not_change_who_wins_among_equal_urgency() {
|
|||
let rows = [mind("ann", Some(500)), mind("bob", Some(300))];
|
||||
assert_eq!(pick(&rows, 60), pick(&rows, 86_400));
|
||||
}
|
||||
|
||||
// The fallback controller. `control` is the one controller; these tests give it
|
||||
// explicit gains so they pin each term whatever `GAINS` the simulation chose.
|
||||
|
||||
const RESETS: i64 = 1_000_000_000_000;
|
||||
const MID: i64 = RESETS - WINDOW_MS / 2;
|
||||
const HOUR: i64 = 3_600_000;
|
||||
const T: Gains = Gains {
|
||||
kp: 0.02,
|
||||
ki: 0.002,
|
||||
kd: 0.02,
|
||||
};
|
||||
|
||||
fn read(used_permille: u32, observed_at_ms: i64) -> Reading {
|
||||
Reading {
|
||||
used_permille,
|
||||
observed_at_ms,
|
||||
resets_at_ms: RESETS,
|
||||
}
|
||||
}
|
||||
|
||||
fn near(got: u32, want: u32) {
|
||||
assert!(
|
||||
got.abs_diff(want) <= 1,
|
||||
"{got} is not within one second of {want}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn behind_target_shortens_the_beat() {
|
||||
let at = |used| {
|
||||
control(
|
||||
&T,
|
||||
&mut Fallback::default(),
|
||||
Some(&read(used, MID)),
|
||||
MID,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
)
|
||||
};
|
||||
// Halfway through the window the trajectory is 450: error +50, 0, -50 is a
|
||||
// turn rate of 5, 4 and 3 an hour.
|
||||
near(at(400), 720);
|
||||
near(at(450), DEFAULT_BEAT_S);
|
||||
near(at(500), 1200);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derivative_is_on_measurement() {
|
||||
let g = Gains { ki: 0.0, ..T };
|
||||
let mut s = Fallback::default();
|
||||
near(
|
||||
control(&g, &mut s, Some(&read(410, MID)), MID, 900, false, false),
|
||||
750,
|
||||
);
|
||||
// A new observation 10 permille higher a quarter hour on: the slope is 40 an hour.
|
||||
let next = read(420, MID + 15 * 60_000);
|
||||
near(
|
||||
control(
|
||||
&g,
|
||||
&mut s,
|
||||
Some(&next),
|
||||
MID + 15 * 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
941,
|
||||
);
|
||||
// The same reading again carries no derivative term.
|
||||
near(
|
||||
control(
|
||||
&g,
|
||||
&mut s,
|
||||
Some(&next),
|
||||
MID + 16 * 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
778,
|
||||
);
|
||||
// A step in the target with used unchanged moves the beat by kp x error alone.
|
||||
near(
|
||||
control(
|
||||
&g,
|
||||
&mut s,
|
||||
Some(&next),
|
||||
MID + 18 * 60_000,
|
||||
1000,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
639,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saturated_output_does_not_wind_up() {
|
||||
let g = Gains {
|
||||
kp: 0.1,
|
||||
ki: 0.01,
|
||||
kd: 0.0,
|
||||
};
|
||||
let day5 = RESETS - 2 * 24 * HOUR;
|
||||
let mut s = Fallback::default();
|
||||
for i in 0..100 {
|
||||
let now = day5 + i * 60_000;
|
||||
assert_eq!(
|
||||
control(&g, &mut s, Some(&read(0, now)), now, 900, false, true),
|
||||
FALLBACK_MIN_S
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
s.integral, 0.0,
|
||||
"error is positive and the caller reports the floor"
|
||||
);
|
||||
for i in 100..110 {
|
||||
let now = day5 + i * 60_000;
|
||||
control(&g, &mut s, Some(&read(600, now)), now, 900, false, false);
|
||||
}
|
||||
assert!(s.integral > 0.0, "unsaturated again, it integrates");
|
||||
|
||||
// Saturated at the slow limit with the error pulling the other way integrates.
|
||||
let mut s = Fallback {
|
||||
integral: -1000.0,
|
||||
last_used_permille: 400,
|
||||
last_observed_at_ms: MID - 60_000,
|
||||
last_now_ms: MID - 60_000,
|
||||
window_resets_at_ms: RESETS,
|
||||
..Fallback::default()
|
||||
};
|
||||
assert_eq!(
|
||||
control(&g, &mut s, Some(&read(400, MID)), MID, 900, false, false),
|
||||
FALLBACK_MAX_S
|
||||
);
|
||||
assert!(s.integral > -1000.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vote_freezes_and_release_is_bumpless() {
|
||||
let reading = read(300, MID);
|
||||
let mut s = Fallback::default();
|
||||
for i in 0..5 {
|
||||
control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&reading),
|
||||
MID + i * 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
}
|
||||
s.rate_per_h = 3600.0 / 300.0;
|
||||
let (mut short, mut long) = (s.clone(), s.clone());
|
||||
let (t_short, t_long) = (MID + 6 * 60_000, MID + 6 * 60_000 + HOUR);
|
||||
assert_eq!(
|
||||
control(&T, &mut short, Some(&reading), t_short, 900, true, false),
|
||||
300
|
||||
);
|
||||
assert_eq!(
|
||||
control(&T, &mut long, Some(&reading), t_long, 900, true, false),
|
||||
300
|
||||
);
|
||||
// However long the vote held, the first beat after it is the voted beat.
|
||||
near(
|
||||
control(
|
||||
&T,
|
||||
&mut short,
|
||||
Some(&reading),
|
||||
t_short + 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
300,
|
||||
);
|
||||
near(
|
||||
control(
|
||||
&T,
|
||||
&mut long,
|
||||
Some(&reading),
|
||||
t_long + 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
300,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rollover_resets_memory() {
|
||||
let mut s = Fallback::default();
|
||||
let before = RESETS - 2 * HOUR;
|
||||
for i in 0..12 {
|
||||
let now = before + i * 10 * 60_000;
|
||||
control(&T, &mut s, Some(&read(100, now)), now, 900, false, false);
|
||||
}
|
||||
assert!(s.integral > 100.0);
|
||||
let new = Reading {
|
||||
used_permille: 5,
|
||||
observed_at_ms: RESETS + HOUR,
|
||||
resets_at_ms: RESETS + WINDOW_MS,
|
||||
};
|
||||
// Used fell from 100 to 5 across the reset: no derivative kick, no old integral.
|
||||
near(
|
||||
control(&T, &mut s, Some(&new), RESETS + HOUR, 900, false, false),
|
||||
898,
|
||||
);
|
||||
assert!(s.integral < 2.0);
|
||||
assert_eq!(s.window_resets_at_ms, RESETS + WINDOW_MS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_reading_holds() {
|
||||
let mut s = Fallback::default();
|
||||
assert_eq!(
|
||||
control(&T, &mut s, None, MID, 900, false, false),
|
||||
DEFAULT_BEAT_S,
|
||||
"never set"
|
||||
);
|
||||
let live = read(400, MID + 60_000);
|
||||
control(&T, &mut s, Some(&read(400, MID)), MID, 900, false, false);
|
||||
let held = control(&T, &mut s, Some(&live), MID + 60_000, 900, false, false);
|
||||
assert_ne!(held, DEFAULT_BEAT_S);
|
||||
let integral = s.integral;
|
||||
assert!(integral > 0.0, "a fresh reading integrates");
|
||||
assert_eq!(
|
||||
control(&T, &mut s, None, MID + 2 * 60_000, 900, false, false),
|
||||
held
|
||||
);
|
||||
// Observed just before the window it names began: not its reading.
|
||||
let stale = Reading {
|
||||
observed_at_ms: RESETS - WINDOW_MS - 60_000,
|
||||
used_permille: 900,
|
||||
..live
|
||||
};
|
||||
assert_eq!(
|
||||
control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&stale),
|
||||
RESETS - WINDOW_MS + 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
held
|
||||
);
|
||||
let expired = Reading {
|
||||
resets_at_ms: MID + 4 * HOUR,
|
||||
observed_at_ms: MID + 4 * HOUR - WINDOW_MS,
|
||||
..live
|
||||
};
|
||||
assert_eq!(
|
||||
control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&expired),
|
||||
MID + 6 * HOUR,
|
||||
900,
|
||||
false,
|
||||
false
|
||||
),
|
||||
held
|
||||
);
|
||||
assert_eq!(s.integral, integral, "nothing integrated while held");
|
||||
// The six hours without a reading are not integrated when it comes back.
|
||||
control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&read(400, MID + 6 * HOUR + 60_000)),
|
||||
MID + 6 * HOUR + 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
assert!(
|
||||
s.integral - integral < 2.0,
|
||||
"{} then {}",
|
||||
integral,
|
||||
s.integral
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn beat_is_bounded() {
|
||||
let wild = Gains {
|
||||
kp: 1.0,
|
||||
ki: 0.0,
|
||||
kd: 0.0,
|
||||
};
|
||||
let late = RESETS - 12 * HOUR;
|
||||
assert_eq!(
|
||||
control(
|
||||
&wild,
|
||||
&mut Fallback::default(),
|
||||
Some(&read(0, late)),
|
||||
late,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
FALLBACK_MIN_S
|
||||
);
|
||||
assert_eq!(
|
||||
control(
|
||||
&wild,
|
||||
&mut Fallback::default(),
|
||||
Some(&read(1000, MID)),
|
||||
MID,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
FALLBACK_MAX_S
|
||||
);
|
||||
for used in [0, 1, 450, 900, 1000, 5000] {
|
||||
for target in [0, 900, 1000] {
|
||||
for gains in [wild, T, GAINS] {
|
||||
let beat = control(
|
||||
&gains,
|
||||
&mut Fallback::default(),
|
||||
Some(&read(used, MID)),
|
||||
MID,
|
||||
target,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
assert!((FALLBACK_MIN_S..=FALLBACK_MAX_S).contains(&beat), "{beat}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fallback_beat_is_the_controller_with_the_committed_gains() {
|
||||
let live = read(400, MID);
|
||||
let (mut a, mut b) = (Fallback::default(), Fallback::default());
|
||||
let beat = fallback_beat(&mut a, Some(&live), MID, 900, false, false);
|
||||
assert_eq!(
|
||||
beat,
|
||||
control(&GAINS, &mut b, Some(&live), MID, 900, false, false)
|
||||
);
|
||||
assert_ne!(beat, DEFAULT_BEAT_S, "behind the trajectory");
|
||||
assert_eq!(a, b);
|
||||
// On the trajectory with no memory the answer is the bias, whatever the gains.
|
||||
let on = read(450, MID);
|
||||
let beat = fallback_beat(&mut Fallback::default(), Some(&on), MID, 900, false, false);
|
||||
assert_eq!(beat, DEFAULT_BEAT_S);
|
||||
assert_eq!(
|
||||
fallback_beat(&mut Fallback::default(), None, MID, 900, false, false),
|
||||
DEFAULT_BEAT_S
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_window_that_ends_this_instant_is_expired() {
|
||||
let mut s = Fallback::default();
|
||||
let live = read(400, MID);
|
||||
let held = control(&T, &mut s, Some(&live), MID, 900, false, false);
|
||||
let ending = Reading {
|
||||
resets_at_ms: MID + 60_000,
|
||||
observed_at_ms: MID,
|
||||
used_permille: 0,
|
||||
};
|
||||
// At its reset instant the window holds the last beat instead of steering.
|
||||
assert_eq!(
|
||||
control(&T, &mut s, Some(&ending), MID + 60_000, 900, false, false),
|
||||
held
|
||||
);
|
||||
}
|
||||
|
||||
/// A state mid-window with some integral, as after a quiet hour of fresh readings.
|
||||
fn warm(gains: &Gains) -> Fallback {
|
||||
let mut s = Fallback::default();
|
||||
for i in 0..7 {
|
||||
let now = MID + i * 10 * 60_000;
|
||||
control(gains, &mut s, Some(&read(300, now)), now, 900, false, false);
|
||||
}
|
||||
assert!(s.integral > 0.0);
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_frozen_reading_does_not_wind_up() {
|
||||
let frozen = read(300, MID);
|
||||
let at = |s: &mut Fallback, hours: i64| {
|
||||
control(&T, s, Some(&frozen), MID + hours * HOUR, 900, false, false)
|
||||
};
|
||||
let mut s = Fallback::default();
|
||||
at(&mut s, 0);
|
||||
// Inside the horizon it is live; past it, the last beat holds and nothing
|
||||
// integrates, however long the connector keeps replaying it.
|
||||
at(&mut s, 1);
|
||||
assert!(s.integral > 100.0 && s.integral < 200.0, "{}", s.integral);
|
||||
// Observed exactly two hours ago still counts; the horizon is not shorter.
|
||||
let before = s.integral;
|
||||
at(&mut s, 2);
|
||||
assert!(s.integral > before, "{} then {}", before, s.integral);
|
||||
let (held, integral) = (at(&mut s, 3), s.integral);
|
||||
assert_eq!(at(&mut s, 48), held);
|
||||
assert_eq!(at(&mut s, 80), held);
|
||||
assert_eq!(s.integral, integral);
|
||||
// A reading that advances takes over again.
|
||||
let now = MID + 82 * HOUR;
|
||||
control(&T, &mut s, Some(&read(300, now)), now, 900, false, false);
|
||||
assert_ne!(s.rate_per_h, 3600.0 / f64::from(held));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_millisecond_of_resets_jitter_is_the_same_window() {
|
||||
let (mut steady, mut jittery) = (Fallback::default(), Fallback::default());
|
||||
for i in 0..60 {
|
||||
let now = MID + i * 60_000;
|
||||
let r = read(300, now);
|
||||
control(&T, &mut steady, Some(&r), now, 900, false, false);
|
||||
let jitter = Reading {
|
||||
resets_at_ms: RESETS + i % 2,
|
||||
..r
|
||||
};
|
||||
control(&T, &mut jittery, Some(&jitter), now, 900, false, false);
|
||||
}
|
||||
assert!(steady.integral > 100.0);
|
||||
assert!((jittery.integral - steady.integral).abs() < 1e-3);
|
||||
// The jitter allowed is a minute, to the millisecond.
|
||||
for (jitter, same) in [(60_000, true), (60_001, false)] {
|
||||
let mut s = Fallback::default();
|
||||
control(&T, &mut s, Some(&read(300, MID)), MID, 900, false, false);
|
||||
let later = Reading {
|
||||
resets_at_ms: RESETS + jitter,
|
||||
..read(300, MID + HOUR)
|
||||
};
|
||||
control(&T, &mut s, Some(&later), MID + HOUR, 900, false, false);
|
||||
assert_eq!(s.integral > 0.0, same, "{jitter} ms");
|
||||
}
|
||||
// A window a week on is not the same window.
|
||||
let now = RESETS + HOUR;
|
||||
let next = Reading {
|
||||
resets_at_ms: RESETS + WINDOW_MS - 1_000,
|
||||
..read(5, now)
|
||||
};
|
||||
control(&T, &mut jittery, Some(&next), now, 900, false, false);
|
||||
assert!(jittery.integral < 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_vote_beyond_the_beat_bounds_leaves_the_integral_alone() {
|
||||
for voted_beat in [7200.0, 86_400.0] {
|
||||
let mut s = warm(&T);
|
||||
let integral = s.integral;
|
||||
s.rate_per_h = 3600.0 / voted_beat;
|
||||
let t = MID + 3 * HOUR;
|
||||
control(&T, &mut s, Some(&read(300, t)), t, 900, true, false);
|
||||
assert_eq!(s.integral, integral, "a vote integrates nothing");
|
||||
// The controller can answer no more than its longest beat.
|
||||
let t = t + 60_000;
|
||||
assert!(control(&T, &mut s, Some(&read(300, t)), t, 900, false, false) > 3500);
|
||||
// And takes the wheel back: well behind the trajectory, two hours on, the
|
||||
// beat is its own again.
|
||||
let mut beat = 0;
|
||||
for i in 1..=12 {
|
||||
let t = t + i * 10 * 60_000;
|
||||
beat = control(&T, &mut s, Some(&read(300, t)), t, 900, false, false);
|
||||
}
|
||||
assert!(beat < 700, "{beat}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_short_vote_while_ahead_does_not_outlive_itself() {
|
||||
let mut s = Fallback::default();
|
||||
let at = |s: &mut Fallback, ms: i64, voted: bool| {
|
||||
control(
|
||||
&T,
|
||||
s,
|
||||
Some(&read(650, MID + ms)),
|
||||
MID + ms,
|
||||
900,
|
||||
voted,
|
||||
false,
|
||||
)
|
||||
};
|
||||
at(&mut s, 0, false);
|
||||
s.rate_per_h = 60.0;
|
||||
at(&mut s, HOUR, true);
|
||||
near(at(&mut s, HOUR + 60_000, false), 60);
|
||||
// 200 permille ahead of the trajectory: two hours on it is back to the
|
||||
// slowest beat, not the 60 s it was voted.
|
||||
for i in 2..=121 {
|
||||
at(&mut s, HOUR + i * 60_000, false);
|
||||
}
|
||||
assert_eq!(at(&mut s, HOUR + 122 * 60_000, false), FALLBACK_MAX_S);
|
||||
assert_eq!(
|
||||
s.integral, 0.0,
|
||||
"the carry is not an output the integral absorbs"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_vote_carry_needs_no_integral_gain_and_a_set_rate() {
|
||||
// ki = 0: nothing to divide by; release is still the voted beat.
|
||||
let g = Gains { ki: 0.0, ..T };
|
||||
let mut s = warm(&g);
|
||||
s.rate_per_h = 12.0;
|
||||
let t = MID + 3 * HOUR;
|
||||
control(&g, &mut s, Some(&read(300, t)), t, 900, true, false);
|
||||
near(
|
||||
control(
|
||||
&g,
|
||||
&mut s,
|
||||
Some(&read(300, t + 60_000)),
|
||||
t + 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
),
|
||||
300,
|
||||
);
|
||||
// A vote cast before the caller set any rate is no carry at all.
|
||||
let (mut voted, mut plain) = (Fallback::default(), Fallback::default());
|
||||
let r = read(300, MID);
|
||||
control(&T, &mut voted, Some(&r), MID, 900, true, false);
|
||||
let after = read(300, MID + 60_000);
|
||||
assert_eq!(
|
||||
control(
|
||||
&T,
|
||||
&mut voted,
|
||||
Some(&after),
|
||||
MID + 60_000,
|
||||
900,
|
||||
false,
|
||||
false
|
||||
),
|
||||
control(
|
||||
&T,
|
||||
&mut plain,
|
||||
Some(&after),
|
||||
MID + 60_000,
|
||||
900,
|
||||
false,
|
||||
false
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_clock_that_steps_back_integrates_and_fades_nothing_backwards() {
|
||||
let mut s = warm(&T);
|
||||
let integral = s.integral;
|
||||
let t = MID + 2 * HOUR;
|
||||
control(&T, &mut s, Some(&read(300, t)), t, 900, false, false);
|
||||
let after_forward = s.integral;
|
||||
assert!(after_forward > integral);
|
||||
control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&read(300, t + 60_000)),
|
||||
t - HOUR,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
assert_eq!(s.integral, after_forward, "no negative step");
|
||||
s.rate_per_h = 12.0;
|
||||
control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&read(300, t + 120_000)),
|
||||
t,
|
||||
900,
|
||||
true,
|
||||
false,
|
||||
);
|
||||
let carry = s.carry_per_h;
|
||||
control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&read(300, t + 180_000)),
|
||||
t - HOUR,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
assert!(s.carry_per_h.abs() <= carry.abs(), "the carry only fades");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_released_vote_that_saturates_the_output_does_not_wind_the_integral() {
|
||||
let mut s = Fallback::default();
|
||||
let at = |s: &mut Fallback, ms: i64, voted: bool, saturated: bool| {
|
||||
control(
|
||||
&T,
|
||||
s,
|
||||
Some(&read(300, MID + ms)),
|
||||
MID + ms,
|
||||
900,
|
||||
voted,
|
||||
saturated,
|
||||
)
|
||||
};
|
||||
at(&mut s, 0, false, false);
|
||||
s.rate_per_h = 60.0;
|
||||
at(&mut s, 60_000, true, false);
|
||||
// Behind the trajectory, the voted rate is the fastest the controller can
|
||||
// answer and the error pushes further: the carry is part of the output the
|
||||
// integral must not wind against.
|
||||
at(&mut s, 120_000, false, true);
|
||||
assert_eq!(s.integral, 0.0);
|
||||
}
|
||||
|
||||
/// Time spent without a call is not integrated as if the swarm had run through
|
||||
/// it: one fresh reading after two days integrates one longest beat of error.
|
||||
#[test]
|
||||
fn a_gap_between_calls_integrates_at_most_one_longest_beat() {
|
||||
let mut s = warm(&GAINS);
|
||||
let before = s.integral;
|
||||
let now = MID + 48 * HOUR;
|
||||
let error = 900.0 * (now - (RESETS - WINDOW_MS)) as f64 / WINDOW_MS as f64 - 300.0;
|
||||
let beat = control(
|
||||
&GAINS,
|
||||
&mut s,
|
||||
Some(&read(300, now)),
|
||||
now,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
let step = s.integral - before;
|
||||
assert!(
|
||||
(step - error * (MAX_STEP_MS as f64 / HOUR_MS)).abs() < 1e-6,
|
||||
"{step} for error {error}"
|
||||
);
|
||||
assert!(
|
||||
beat > 100,
|
||||
"an outage did not park the controller at its floor: {beat}"
|
||||
);
|
||||
// A gap shorter than the cap is integrated whole.
|
||||
let mut s = warm(&GAINS);
|
||||
let before = s.integral;
|
||||
let now = MID + 90 * 60_000;
|
||||
let error = 900.0 * (now - (RESETS - WINDOW_MS)) as f64 / WINDOW_MS as f64 - 300.0;
|
||||
control(
|
||||
&GAINS,
|
||||
&mut s,
|
||||
Some(&read(300, now)),
|
||||
now,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
assert!((s.integral - before - error * 0.5).abs() < 1e-6);
|
||||
}
|
||||
|
||||
/// The caller's saturation report stops integration toward more spend and
|
||||
/// nothing else.
|
||||
#[test]
|
||||
fn a_saturated_report_stops_integration_toward_more_spend_only() {
|
||||
let g = Gains { kd: 0.0, ..T };
|
||||
let mut s = warm(&g);
|
||||
let integral = s.integral;
|
||||
let t = MID + 2 * HOUR;
|
||||
control(&g, &mut s, Some(&read(300, t)), t, 900, false, true);
|
||||
assert_eq!(s.integral, integral, "behind and saturated: nothing to buy");
|
||||
let t = t + 60_000;
|
||||
control(&g, &mut s, Some(&read(300, t)), t, 900, false, false);
|
||||
assert!(s.integral > integral, "not saturated, it integrates again");
|
||||
let integral = s.integral;
|
||||
let t = t + 60_000;
|
||||
control(&g, &mut s, Some(&read(480, t)), t, 900, false, true);
|
||||
assert!(
|
||||
s.integral < integral,
|
||||
"ahead, saturation does not hold the integral up"
|
||||
);
|
||||
}
|
||||
|
||||
/// The vote's release is the voted beat less the one integration step the first
|
||||
/// free call takes before it answers.
|
||||
#[test]
|
||||
fn release_of_the_slowest_vote_is_within_one_integration_step() {
|
||||
let mut s = warm(&T);
|
||||
s.rate_per_h = 1.0;
|
||||
let t = MID + 3 * HOUR;
|
||||
control(&T, &mut s, Some(&read(300, t)), t, 900, true, false);
|
||||
let beat = control(
|
||||
&T,
|
||||
&mut s,
|
||||
Some(&read(300, t + 60_000)),
|
||||
t + 60_000,
|
||||
900,
|
||||
false,
|
||||
false,
|
||||
);
|
||||
assert!((3400..FALLBACK_MAX_S).contains(&beat), "{beat}");
|
||||
}
|
||||
|
||||
/// A vote under the 60 s floor is held at the floor's rate: the carry is the gap
|
||||
/// to that, not to the faster rate voted.
|
||||
#[test]
|
||||
fn a_vote_under_the_floor_carries_the_floor_not_the_vote() {
|
||||
let mut s = warm(&T);
|
||||
s.rate_per_h = 120.0;
|
||||
let t = MID + 3 * HOUR;
|
||||
control(&T, &mut s, Some(&read(300, t)), t, 900, true, false);
|
||||
let own = BIAS_PER_H + T.kp * 178.0 + T.ki * s.integral;
|
||||
assert!(
|
||||
(s.carry_per_h + own - 60.0).abs() < 2.0,
|
||||
"carry {} over own {own}",
|
||||
s.carry_per_h
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ use eureka_pipeline::{
|
|||
PipelineTarget, QuestionOption, ResolutionOutcome, RulingAuthority, RunOperator, RunTurn, Sha, Slug, StructuralDelta, TargetInvariant,
|
||||
};
|
||||
use huginn_mind::{
|
||||
Faculty, HuginnMindRequest, ImportSource, HuginnMindResponse, Mind, OwnedRedbMessagePackBackingStore, PipelineAdmissionBatch, PipelineAdmissionOutcome,
|
||||
Faculty, HuginnMindRequest, HuginnMindResponse, Mind, OwnedRedbMessagePackBackingStore, PipelineAdmissionBatch, PipelineAdmissionOutcome,
|
||||
PipelineDocumentView, PipelineProvenance, PipelineSelectionPage,
|
||||
};
|
||||
use tempfile::TempDir;
|
||||
|
|
@ -116,15 +116,6 @@ impl TestMind {
|
|||
this
|
||||
}
|
||||
|
||||
/// A mind holding only its identity: no stewardship, no campaign.
|
||||
pub(crate) fn bare() -> Self {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let mind = Mind::open(dir.path(), &Slug(INSTANCE.into())).unwrap();
|
||||
let this = Self { _dir: dir, instance: Slug(INSTANCE.into()), mind: RefCell::new(mind), now: Cell::new(at(0)) };
|
||||
this.put(vec![instance(INSTANCE)]);
|
||||
this
|
||||
}
|
||||
|
||||
/// The same, for another instance name.
|
||||
pub(crate) fn seeded_as(name: &str) -> Self {
|
||||
let dir = TempDir::new().unwrap();
|
||||
|
|
@ -155,26 +146,6 @@ impl TestMind {
|
|||
self.mind.borrow_mut().admit(batch, self.now.get())
|
||||
}
|
||||
|
||||
/// Admits as an import of a giver's receipt committed at `committed_at`, one second after the last admission here.
|
||||
pub(crate) fn committed_imported(&self, committed_at: &str, documents: Vec<PipelineDocument>) {
|
||||
self.now.set(self.now.get() + Duration::seconds(1));
|
||||
let source = ImportSource {
|
||||
instance: Slug("giver".into()),
|
||||
receipt_id: format!("giver-{committed_at}").as_str().into(),
|
||||
ordinal: 1,
|
||||
committed_at: committed_at.into(),
|
||||
faculty: Faculty::Hands,
|
||||
agent: "test".into(),
|
||||
session: "test".into(),
|
||||
tool: "test".into(),
|
||||
external: vec![],
|
||||
};
|
||||
let provenance = PipelineProvenance { faculty: Faculty::Hands, agent: "test".into(), session: "test".into(), tool: "test".into(), imported_from: Some(source) };
|
||||
let batch = PipelineAdmissionBatch { instance: self.instance.clone(), provenance, documents };
|
||||
let outcome = self.mind.borrow_mut().admit(batch, self.now.get());
|
||||
assert!(matches!(outcome, PipelineAdmissionOutcome::Committed { .. }), "{outcome:?}");
|
||||
}
|
||||
|
||||
pub(crate) fn committed(&self, documents: Vec<PipelineDocument>) {
|
||||
let outcome = self.put(documents);
|
||||
assert!(matches!(outcome, PipelineAdmissionOutcome::Committed { .. }), "{outcome:?}");
|
||||
|
|
@ -336,15 +307,6 @@ pub(crate) fn campaign_named(slug: &str, title: &str) -> PipelineDocument {
|
|||
})
|
||||
}
|
||||
|
||||
/// A campaign opened on `repos`, with its first target and the one invariant.
|
||||
pub(crate) fn campaign_on(slug: &str, repos: &[&str]) -> Vec<PipelineDocument> {
|
||||
let PipelineDocument::Campaign(mut opened) = campaign_named(slug, slug) else { unreachable!("a campaign") };
|
||||
opened.repos = repos.iter().map(|repo| (*repo).into()).collect();
|
||||
let PipelineDocument::Target(mut first) = target() else { unreachable!("a target") };
|
||||
first.campaign = Slug(slug.into());
|
||||
vec![PipelineDocument::Campaign(opened), PipelineDocument::Target(first)]
|
||||
}
|
||||
|
||||
fn target() -> PipelineDocument {
|
||||
PipelineDocument::Target(PipelineTarget {
|
||||
campaign: Slug(CAMPAIGN.into()),
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
//! The `mind-persona` binary as the unit runs it: arguments `--instance`, the rest
|
||||
//! from `persona.env` and the credential files. The control root is a constant
|
||||
//! that does not exist where this runs, so a start that is configured right ends
|
||||
//! dead at the gate (exit 0) before any transport is touched; a start configured
|
||||
//! wrong ends 2 and names the setting, never a value.
|
||||
//! from `persona.env`. The control root is a constant that does not exist where
|
||||
//! this runs, so a start that is configured right ends dead at the gate (exit 0);
|
||||
//! a start configured wrong ends 2 and names the setting, never a value.
|
||||
|
||||
use std::path::Path;
|
||||
use std::process::{Command, Output};
|
||||
|
||||
fn run(dir: &Path, drop: &[&str], extra: &[(&str, &str)], args: &[&str]) -> Output {
|
||||
std::fs::write(dir.join("chat-key"), "chat-secret-text\n").unwrap();
|
||||
let key = |name: &str| dir.join(name).to_string_lossy().into_owned();
|
||||
let mut env = vec![
|
||||
("HUGINN_ENDPOINT", "rudp://127.0.0.1:9".to_string()),
|
||||
("STATE_DIRECTORY", dir.to_string_lossy().into_owned()),
|
||||
|
|
@ -18,11 +15,6 @@ fn run(dir: &Path, drop: &[&str], extra: &[(&str, &str)], args: &[&str]) -> Outp
|
|||
("MIND_PERSONA_MINDS", "@eureka:chat,@libby:chat".to_string()),
|
||||
("MIND_PERSONA_SELF_SENDER", "@eureka:chat".to_string()),
|
||||
("MIND_PERSONA_MODEL", "claude-test-model".to_string()),
|
||||
("MIND_PERSONA_RUN_CAP_S", "14400".to_string()),
|
||||
("RUNTIME_DIRECTORY", dir.join("runtime").to_string_lossy().into_owned()),
|
||||
("CLAUDE_LEASE_SOCKET", key("lease.sock")),
|
||||
("BIFROST_CHAT_ENDPOINT", "rudp://127.0.0.1:17884".to_string()),
|
||||
("BIFROST_CHAT_KEY_FILE", key("chat-key")),
|
||||
];
|
||||
env.retain(|(name, _)| !drop.contains(name) && !extra.iter().any(|(set, _)| set == name));
|
||||
env.extend(extra.iter().map(|(name, value)| (*name, (*value).to_string())));
|
||||
|
|
@ -55,23 +47,18 @@ fn a_flag_overrides_the_environment() {
|
|||
#[test]
|
||||
fn a_setting_that_is_missing_or_wrong_ends_two_naming_the_setting_and_no_value() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let cases: [Case; 9] = [
|
||||
let cases: [Case; 4] = [
|
||||
(&["MIND_PERSONA_ROOMS"], &[], "usage"),
|
||||
(&["MIND_PERSONA_MINDS"], &[], "usage"),
|
||||
(&["MIND_PERSONA_MODEL"], &[], "MIND_PERSONA_MODEL"),
|
||||
(&["MIND_PERSONA_RUN_CAP_S"], &[], "MIND_PERSONA_RUN_CAP_S"),
|
||||
(&[], &[("MIND_PERSONA_RUN_CAP_S", "0")], "MIND_PERSONA_RUN_CAP_S"),
|
||||
(&["RUNTIME_DIRECTORY"], &[], "RUNTIME_DIRECTORY"),
|
||||
(&["CLAUDE_LEASE_SOCKET"], &[], "CLAUDE_LEASE_SOCKET"),
|
||||
(&[], &[("BIFROST_CHAT_ENDPOINT", "127.0.0.1:17884")], "BIFROST_CHAT_ENDPOINT"),
|
||||
(&[], &[("BIFROST_CHAT_KEY_FILE", "/nonexistent/chat-key")], "BIFROST_CHAT_KEY_FILE"),
|
||||
(&[], &[("HUGINN_ENDPOINT", "canary-endpoint")], "endpoint"),
|
||||
];
|
||||
for (drop, extra, names) in cases {
|
||||
let output = run(dir.path(), drop, extra, &["--instance", "eureka"]);
|
||||
let stderr = text(&output.stderr);
|
||||
assert_eq!(output.status.code(), Some(2), "{drop:?} {extra:?}: {stderr}");
|
||||
assert!(stderr.contains(names), "{drop:?} {extra:?}: {stderr}");
|
||||
assert!(!stderr.contains("secret-text") && !stderr.contains("127.0.0.1:17884") && !stderr.contains("/nonexistent/chat-key"), "no value is echoed: {stderr}");
|
||||
assert!(!stderr.contains("canary-endpoint") && !stderr.contains("#ops:chat"), "no value is echoed: {stderr}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -83,15 +70,3 @@ fn the_retired_max_turns_flag_is_refused_and_its_value_is_not_echoed() {
|
|||
assert_eq!(output.status.code(), Some(2), "{stderr}");
|
||||
assert!(stderr.contains("usage") && !stderr.contains("canary-turns"), "{stderr}");
|
||||
}
|
||||
|
||||
/// The reply-link setting is the mouth's own rule: exactly `1` is on, anything else is off, and none of it ends the unit or reaches a message.
|
||||
#[test]
|
||||
fn a_v4_setting_other_than_exactly_1_is_off() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
for value in ["", "0", "true", "yes", " 1", "canary-v4"] {
|
||||
let output = run(dir.path(), &[], &[("BIFROST_CHAT_PENDING_V4", value)], &["--instance", "eureka"]);
|
||||
assert_eq!(output.status.code(), Some(0), "{value:?}: {}", text(&output.stderr));
|
||||
assert!(!text(&output.stderr).contains("canary-v4") && !text(&output.stdout).contains("canary-v4"));
|
||||
}
|
||||
assert_eq!(run(dir.path(), &[], &[("BIFROST_CHAT_PENDING_V4", "1")], &["--instance", "eureka"]).status.code(), Some(0));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue