Merge pull request 'C2: cut the mission organ and every Persona-side Mind write' (#75) from hands/persona-only-subtraction into main
This commit is contained in:
commit
66b85c8b67
34 changed files with 712 additions and 21654 deletions
19
Cargo.lock
generated
19
Cargo.lock
generated
|
|
@ -851,16 +851,6 @@ dependencies = [
|
|||
"polyval",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ghostlight-persona-projection"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/GameCult/Ghostlight.git?rev=c43eb54962ed4a09cc737ecac73a16b0d7ce62b4#c43eb54962ed4a09cc737ecac73a16b0d7ce62b4"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"sha2",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.17.1"
|
||||
|
|
@ -1108,7 +1098,6 @@ dependencies = [
|
|||
"cultnet-rs 0.3.0",
|
||||
"eureka-pipeline",
|
||||
"eureka-state",
|
||||
"ghostlight-persona-projection",
|
||||
"huginn-mind",
|
||||
"libc",
|
||||
"model-connector",
|
||||
|
|
@ -1122,7 +1111,6 @@ dependencies = [
|
|||
"syn 2.0.119",
|
||||
"tempfile",
|
||||
"unicode-general-category",
|
||||
"ureq",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
|
|
@ -1133,7 +1121,6 @@ source = "git+https://eureka.gamecult.org/forge/GameCult/ModelConnector.git?rev=
|
|||
dependencies = [
|
||||
"aes-gcm",
|
||||
"base64",
|
||||
"cultcache-rs 0.3.1 (git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23)",
|
||||
"cultnet-rs 0.2.0",
|
||||
"rand_core 0.6.4",
|
||||
"rmp-serde",
|
||||
|
|
@ -1993,12 +1980,6 @@ dependencies = [
|
|||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8"
|
||||
|
||||
[[package]]
|
||||
name = "universal-hash"
|
||||
version = "0.5.1"
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -9,13 +9,13 @@ publish.workspace = true
|
|||
# operator's brakes: a root-owned per-instance CultCache
|
||||
# store (`control`), the operator's CLI over it (`cli`, the `mind-control`
|
||||
# binary) and the one fail-closed reader every acting organ calls
|
||||
# (`control::read_effective`). It also holds the launch primitive (`launch`,
|
||||
# `queue`): the one way her organs open a run and start its unit. The heartbeat
|
||||
# (`heartbeat`, the `mind-heartbeat` binary) runs the shared heartbeat over them. The actuator
|
||||
# (`control::read_effective`). It also holds her units under systemd (`launch`)
|
||||
# and her port to her mind (`queue`). The heartbeat (`heartbeat`, the
|
||||
# `mind-heartbeat` binary) wakes her by starting her unit. The actuator
|
||||
# (`self_run`, the `mind-self-run` binary) carries her one open Self run.
|
||||
#
|
||||
# The Persona organ (`persona`, the `mind-persona` binary) drains her pending chat
|
||||
# through ports. The connector transport is ModelConnector's kit client, pinned to
|
||||
# The Persona organ (`persona`, the `mind-persona` binary) reads the brakes and
|
||||
# carries no turn until the three-stage turn lands (map C3). The connector transport is ModelConnector's kit client, pinned to
|
||||
# the kit's revision (`connector`); the chat transport is cultnet-rs's RUDP client
|
||||
# to Bifrost's mouth (`chat_rudp`). `rmp-serde` is the chat documents' positional
|
||||
# MessagePack codec (`chat_client`); `sha2` derives the mouth's connection id from
|
||||
|
|
@ -23,12 +23,6 @@ publish.workspace = true
|
|||
# two until the workspace pin and the kit's meet.
|
||||
# `rmp-serde` is also the lease wire (`lease_client`): the connector's MessagePack frames.
|
||||
#
|
||||
# The stream organ (`stream`, the `mind-stream` binary) builds its Projector prompt with Ghostlight's
|
||||
# `ghostlight-persona-projection`, pinned to a revision, so the membrane's text stays Ghostlight's; it
|
||||
# asks its Projector through the connector kit's client (`model-connector`, one turn per tick) and speaks to the base
|
||||
# model's host on loopback over `ureq` 3 with default features off (plain HTTP, no TLS), as the
|
||||
# daemon's embedder does, and stays below 3.4 for the same reason.
|
||||
#
|
||||
# `serde_json` reads Claude Code's stream-json, a foreign owner's wire (`self_run`);
|
||||
# `uuid`'s v5 makes a Self run's session id derivable from the run's id.
|
||||
[dependencies]
|
||||
|
|
@ -39,7 +33,6 @@ cultcache-rs.workspace = true
|
|||
cultnet-rs.workspace = true
|
||||
eureka-pipeline = { path = "../eureka-pipeline" }
|
||||
eureka-state = { path = "../eureka-state", default-features = false }
|
||||
ghostlight-persona-projection = { git = "https://github.com/GameCult/Ghostlight.git", rev = "c43eb54962ed4a09cc737ecac73a16b0d7ce62b4", package = "ghostlight-persona-projection" }
|
||||
huginn-mind = { path = "../huginn-mind" }
|
||||
model-connector = { git = "https://eureka.gamecult.org/forge/GameCult/ModelConnector.git", rev = "c1802d85bee3ae04b0c5737563c8343c1fef515d", package = "model-connector" }
|
||||
rmp-serde = "1"
|
||||
|
|
@ -47,7 +40,6 @@ serde = { version = "1", features = ["derive"] }
|
|||
serde_json = "1"
|
||||
sha2 = "0.10"
|
||||
unicode-general-category = "1"
|
||||
ureq = { version = "~3.3", default-features = false }
|
||||
uuid = { version = "1", default-features = false, features = ["v5"] }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
|
|
@ -66,9 +58,6 @@ proc-macro2 = "1"
|
|||
quote = "1"
|
||||
syn = { version = "2", features = ["full"] }
|
||||
tempfile = "3"
|
||||
# A real kit server (`serve` over a scripted `ProviderBackend`) proves the stream's Projector wire, not a fake of it.
|
||||
# Same revision as the dependency; the `daemon` feature is test-only.
|
||||
model-connector = { git = "https://eureka.gamecult.org/forge/GameCult/ModelConnector.git", rev = "c1802d85bee3ae04b0c5737563c8343c1fef515d", package = "model-connector", features = ["daemon"] }
|
||||
|
||||
[lib]
|
||||
name = "mind_body"
|
||||
|
|
@ -86,10 +75,6 @@ path = "src/bin/mind-self-run.rs"
|
|||
name = "mind-persona"
|
||||
path = "src/bin/mind-persona.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "mind-stream"
|
||||
path = "src/bin/mind-stream.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "mind-heartbeat"
|
||||
path = "src/bin/mind-heartbeat.rs"
|
||||
|
|
|
|||
|
|
@ -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, 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())
|
||||
|
|
@ -61,8 +58,8 @@ fn config() -> Result<Config> {
|
|||
.map(PersonaId::parse)
|
||||
.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, beat_s)
|
||||
}
|
||||
|
||||
fn store() -> Result<FileStore> {
|
||||
|
|
@ -94,8 +91,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() {
|
||||
|
|
|
|||
|
|
@ -1,124 +0,0 @@
|
|||
//! `mind-stream --instance <slug> [--once] [--dry-run]`: her stream of thought.
|
||||
//!
|
||||
//! Without `--once` it runs forever: one `mind_body::stream::tick` per `STREAM_POLL_S`, or per
|
||||
//! `STREAM_MIN_GAP_S` after a blank or a fault. `--dry-run` runs one tick that admits nothing and
|
||||
//! prints the thought it would admit as JSON. A model or mind fault ends the tick, never the
|
||||
//! process: it is one line on stderr. This reads the process's arguments and environment, which
|
||||
//! the library never does.
|
||||
//!
|
||||
//! Environment: `HUGINN_ENDPOINT` (`rudp://<ip>:<port>`, her own daemon), `EUREKA_INSTANCE`
|
||||
//! (when `--instance` is not given), `MIND_STREAM_CONNECTOR_ENDPOINT` (`<loopback ip>:<port>`, the
|
||||
//! connector kit the Projector is asked through), `MIND_STREAM_PROJECTOR_MODEL` (the model id the
|
||||
//! kit's caller row allows), `MIND_STREAM_ENDPOINT` (`http://<loopback>:<port>`, the model host) and
|
||||
//! `MIND_STREAM_BASE_MODEL`. The connector key is the file `connector-key` in `CREDENTIALS_DIRECTORY`
|
||||
//! (systemd's `LoadCredential`), read once; the caller id is the instance.
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::path::PathBuf;
|
||||
use std::process::ExitCode;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use chrono::Utc;
|
||||
use eureka_pipeline::Slug;
|
||||
use eureka_state::HuginnClient;
|
||||
use mind_body::control::read_effective;
|
||||
use mind_body::queue::HuginnMind;
|
||||
use mind_body::stream::{HuginnPersonaSource, KitProjector, Mode, OllamaContinuer, Organs, Outcome, STREAM_MIN_GAP_S, STREAM_POLL_S, connector_key, tick};
|
||||
|
||||
const TIMEOUT: Duration = Duration::from_secs(30);
|
||||
/// How long the kit may take to answer one Projector turn.
|
||||
const PROJECTOR_TIMEOUT: Duration = Duration::from_secs(300);
|
||||
|
||||
#[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())
|
||||
}
|
||||
|
||||
/// The credentials directory systemd hands the unit; its absence names the variable.
|
||||
#[allow(clippy::disallowed_methods, reason = "the binary reads its own environment, and hands it to the library")]
|
||||
fn credentials_directory() -> Result<PathBuf> {
|
||||
std::env::var_os("CREDENTIALS_DIRECTORY").map(PathBuf::from).ok_or_else(|| anyhow!("CREDENTIALS_DIRECTORY is not set"))
|
||||
}
|
||||
|
||||
fn required(name: &str) -> Result<String> {
|
||||
setting(name).ok_or_else(|| anyhow!("{name} is not set"))
|
||||
}
|
||||
|
||||
struct Args {
|
||||
instance: Option<String>,
|
||||
once: bool,
|
||||
dry_run: bool,
|
||||
}
|
||||
|
||||
#[allow(clippy::disallowed_methods, reason = "the binary reads its own arguments")]
|
||||
fn arguments() -> Result<Args> {
|
||||
let mut args = Args { instance: None, once: false, dry_run: false };
|
||||
let mut given = std::env::args().skip(1);
|
||||
while let Some(argument) = given.next() {
|
||||
match argument.as_str() {
|
||||
"--instance" => args.instance = Some(given.next().ok_or_else(|| anyhow!("--instance needs a slug"))?),
|
||||
"--once" => args.once = true,
|
||||
"--dry-run" => args.dry_run = true,
|
||||
other => bail!("unknown argument {other}; usage: mind-stream --instance <slug> [--once] [--dry-run]"),
|
||||
}
|
||||
}
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
fn run() -> Result<()> {
|
||||
let args = arguments()?;
|
||||
let instance = Slug(args.instance.map_or_else(|| required("EUREKA_INSTANCE"), Ok)?);
|
||||
let address: SocketAddr = required("HUGINN_ENDPOINT")?
|
||||
.strip_prefix("rudp://")
|
||||
.and_then(|address| address.parse().ok())
|
||||
.ok_or_else(|| anyhow!("HUGINN_ENDPOINT is not rudp://<ip>:<port>"))?;
|
||||
let projector_model = required("MIND_STREAM_PROJECTOR_MODEL")?;
|
||||
let base_model = required("MIND_STREAM_BASE_MODEL")?;
|
||||
let connector: SocketAddr = required("MIND_STREAM_CONNECTOR_ENDPOINT")?.parse().map_err(|_| anyhow!("MIND_STREAM_CONNECTOR_ENDPOINT is not <ip>:<port>"))?;
|
||||
let projector = KitProjector::open(connector, connector_key(&credentials_directory()?)?, &instance.0, &projector_model, PROJECTOR_TIMEOUT)?;
|
||||
let continuer = OllamaContinuer::open(&required("MIND_STREAM_ENDPOINT")?, &base_model)?;
|
||||
let mind = HuginnMind::new(HuginnClient::new(address, instance.clone(), TIMEOUT));
|
||||
let persona = HuginnPersonaSource::new(HuginnClient::new(address, instance.clone(), TIMEOUT));
|
||||
let organs = Organs { mind: &mind, persona: &persona, projector: &projector, continuer: &continuer, base_model: &base_model };
|
||||
let mode = if args.dry_run { Mode::DryRun } else { Mode::Live };
|
||||
let once = args.once || args.dry_run;
|
||||
loop {
|
||||
let mut faulted = false;
|
||||
let waits = match tick(&organs, &|| read_effective(&instance), mode, Utc::now()) {
|
||||
Ok(Outcome::Thought { thought, admitted }) => {
|
||||
if admitted {
|
||||
println!("admitted {}:thought:{}", instance.0, thought.label.0);
|
||||
} else {
|
||||
println!("{}", serde_json::to_string_pretty(&*thought)?);
|
||||
}
|
||||
false
|
||||
}
|
||||
Ok(Outcome::Blank) => true,
|
||||
Ok(Outcome::Held(reason)) => {
|
||||
eprintln!("mind-stream: held ({reason:?}); nothing ticked");
|
||||
false
|
||||
}
|
||||
Ok(Outcome::NotDue) => false,
|
||||
Err(fault) => {
|
||||
eprintln!("mind-stream: {}", format!("{fault:#}").replace('\n', " "));
|
||||
faulted = true;
|
||||
true
|
||||
}
|
||||
};
|
||||
if once {
|
||||
return if faulted { Err(anyhow!("the tick faulted")) } else { Ok(()) };
|
||||
}
|
||||
std::thread::sleep(Duration::from_secs(if waits { STREAM_MIN_GAP_S as u64 } else { STREAM_POLL_S as u64 }));
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
match run() {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(fault) => {
|
||||
eprintln!("mind-stream: {}", format!("{fault:#}").replace('\n', " "));
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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,8 +1,8 @@
|
|||
//! The operator's brakes, and the one reader of them.
|
||||
//!
|
||||
//! `Released` carries a `Grant`, the only value that opens a launch: it cannot
|
||||
//! be built outside this module, so a launch exists only as the reading of a
|
||||
//! released instance brake and a released swarm brake. The Grant names the
|
||||
//! `Released` carries a `Grant`: it cannot be built outside this module, so it
|
||||
//! exists only as the reading of a released instance brake and a released
|
||||
//! swarm brake. The Grant names the
|
||||
//! instance whose store was read and carries nothing else: no money, no cap. The store is at a fixed place, `CONTROL_ROOT`,
|
||||
//! and the one reader takes the instance only: no function, argument, variable
|
||||
//! or flag lets a caller choose a root, a path or a source, so a Grant is the
|
||||
|
|
@ -92,9 +92,7 @@ pub enum Effective {
|
|||
|
||||
/// The proof that the instance brake and the swarm brake were released when
|
||||
/// read, in the root-owned stores of `instance`. Its fields are private and only
|
||||
/// `read_effective` makes one; `open_and_launch` takes it by value and refuses
|
||||
/// one read for another instance, so one reading opens at most one run, of the
|
||||
/// instance it was read for.
|
||||
/// `read_effective` makes one.
|
||||
///
|
||||
/// Outside this crate the type system leaves no way to make one: the fields are
|
||||
/// private, there is no public constructor, and the derives give no path (no
|
||||
|
|
@ -113,7 +111,7 @@ pub enum Effective {
|
|||
/// ```
|
||||
///
|
||||
/// ```compile_fail,E0599
|
||||
/// // not Clone, so a reading cannot be copied into a second run
|
||||
/// // not Clone, so a reading cannot be copied
|
||||
/// fn copy(grant: &mind_body::control::Grant) -> mind_body::control::Grant {
|
||||
/// grant.clone()
|
||||
/// }
|
||||
|
|
@ -140,13 +138,6 @@ impl Grant {
|
|||
pub fn instance(&self) -> &Slug {
|
||||
&self.instance
|
||||
}
|
||||
|
||||
/// A Grant for a launch test that has no store. It names its instance like
|
||||
/// any other, so the launch's instance check judges it.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn for_test(instance: Slug) -> Self {
|
||||
Self { instance }
|
||||
}
|
||||
}
|
||||
|
||||
/// The instance store's contents, decoded and not yet judged.
|
||||
|
|
@ -237,11 +228,6 @@ pub fn load_state(source: &impl ControlSource) -> Result<ControlState> {
|
|||
/// let _ = mind_body::control::read_effective_from;
|
||||
/// ```
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// // and the test constructor is not in the library build
|
||||
/// let _ = mind_body::control::Grant::for_test;
|
||||
/// ```
|
||||
///
|
||||
/// The same preamble with the real call compiles:
|
||||
///
|
||||
/// ```
|
||||
|
|
|
|||
|
|
@ -67,96 +67,6 @@ fn a_brake_payload_that_does_not_decode_makes_the_store_undecodable() {
|
|||
assert_eq!(effective(&path), held(HeldReason::Undecodable));
|
||||
}
|
||||
|
||||
|
||||
|
||||
mod grant {
|
||||
//! A Grant is the proof that this instance's own control store released: the
|
||||
//! reader derives the path from the instance, and a launch refuses a Grant
|
||||
//! read for another instance before it asks the mind anything.
|
||||
|
||||
use anyhow::{Result, anyhow};
|
||||
use cultnet_rs::Selection;
|
||||
use eureka_pipeline::{PipelineDocument, RunTurn};
|
||||
use huginn_mind::{PipelineAdmissionOutcome, PipelineSelectionPage};
|
||||
|
||||
use super::*;
|
||||
use crate::launch::{Clock, Declined, LaunchOutcome, LaunchRequest, Launcher, Ports, open_and_launch};
|
||||
use crate::queue::MindPort;
|
||||
|
||||
fn slug(name: &str) -> Slug {
|
||||
Slug(name.into())
|
||||
}
|
||||
|
||||
fn read(root: &Path, instance: &str) -> Effective {
|
||||
with_root(root, || read_effective(&slug(instance)))
|
||||
}
|
||||
|
||||
/// A mind that answers nothing: a launch that reaches it has gone past the
|
||||
/// instance check.
|
||||
struct SilentMind(Slug);
|
||||
|
||||
impl MindPort for SilentMind {
|
||||
fn instance(&self) -> &Slug {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn query(&self, _selection: &Selection) -> Result<PipelineSelectionPage> {
|
||||
Err(anyhow!("the launch asked the mind"))
|
||||
}
|
||||
|
||||
fn admit(&self, _agent: &str, _session: &str, _documents: Vec<PipelineDocument>) -> Result<PipelineAdmissionOutcome> {
|
||||
Err(anyhow!("the launch admitted"))
|
||||
}
|
||||
}
|
||||
|
||||
struct NoUnits;
|
||||
|
||||
impl Launcher for NoUnits {
|
||||
fn start(&self, _instance: &Slug, _turn: RunTurn) -> Result<()> {
|
||||
Err(anyhow!("the launch started a unit"))
|
||||
}
|
||||
|
||||
fn alive(&self, _instance: &Slug, _turn: RunTurn) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
struct Now;
|
||||
|
||||
impl Clock for Now {
|
||||
fn now(&self) -> DateTime<Utc> {
|
||||
at(100)
|
||||
}
|
||||
}
|
||||
|
||||
fn launch_as(mind_instance: &str, grant: Grant, turn: RunTurn) -> Result<LaunchOutcome> {
|
||||
let mind = SilentMind(slug(mind_instance));
|
||||
let ports = Ports { mind: &mind, launcher: &NoUnits, clock: &Now, host: "host" };
|
||||
open_and_launch(&ports, LaunchRequest { turn, claims: vec![], agent: "agent".into(), grant })
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_grant_read_for_one_instance_cannot_launch_another() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
release(root.path(), "void");
|
||||
for turn in [RunTurn::SelfRun, RunTurn::PersonaTurn] {
|
||||
let Effective::Released(grant) = read(root.path(), "void") else { panic!("void's store is released") };
|
||||
// The mind is eureka's; the silent mind and the unit-less launcher fail loudly if the launch goes on.
|
||||
let outcome = launch_as("eureka", grant, turn).unwrap();
|
||||
assert_eq!(outcome, LaunchOutcome::Refused(Declined::GrantForOtherInstance), "{turn:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_matching_grant_is_not_stopped_by_the_instance_check() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
release(root.path(), "void");
|
||||
let Effective::Released(grant) = read(root.path(), "void") else { panic!("void's store is released") };
|
||||
let error = launch_as("void", grant, RunTurn::SelfRun).unwrap_err();
|
||||
assert!(format!("{error:#}").contains("asked the mind"), "{error:#}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The swarm store with its brake released, and nothing else in it.
|
||||
fn release_swarm(root: &Path) {
|
||||
SwarmWriter::open(swarm_path(root)).unwrap().set_brake(true, at(0), "op").unwrap();
|
||||
|
|
@ -250,12 +160,6 @@ fn the_stores_are_written_world_readable_and_root_only_writable() {
|
|||
let _ = path;
|
||||
}
|
||||
|
||||
/// A named instance store with its brake released, and a released swarm.
|
||||
fn release(root: &Path, name: &str) {
|
||||
ControlWriter::open(root.join(name).join("control.cc")).unwrap().set_brake(true, at(0), "op").unwrap();
|
||||
release_swarm(root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_writer_refuses_to_open_over_a_store_holding_a_record_it_does_not_read() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
|
|
|||
|
|
@ -1,60 +1,49 @@
|
|||
//! 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. There is no night wake until reflection and
|
||||
//! consolidation return (map C4). Nothing is metered here: no meter and no model call.
|
||||
//!
|
||||
//! 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.
|
||||
//! 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. 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 chrono::{DateTime, SecondsFormat, 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
|
||||
|
|
@ -104,71 +93,19 @@ pub struct Config {
|
|||
pub personas: Vec<PersonaId>,
|
||||
/// Sender ids that are the operator.
|
||||
pub operators: Vec<String>,
|
||||
/// The nightly window and the Minds that sleep in it; none, no one sleeps.
|
||||
pub night: Option<Night>,
|
||||
}
|
||||
|
||||
/// The heartbeat's night: a daily window in UTC minutes (it may wrap midnight) and the Minds that sleep in it.
|
||||
/// The heartbeat alone decides who sleeps and when: one Sleep run per Mind per window, never made up if the window
|
||||
/// passes without one.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Night {
|
||||
pub from_min: u32,
|
||||
pub to_min: u32,
|
||||
pub minds: Vec<Slug>,
|
||||
}
|
||||
|
||||
impl Night {
|
||||
/// The nightly sleep from the two settings that name it: both or neither, so a half-set deploy fails at start
|
||||
/// instead of quietly never sleeping, or sleeping no one.
|
||||
pub fn from_settings(window: Option<&str>, minds: Option<&str>) -> Result<Option<Self>> {
|
||||
match (window, minds) {
|
||||
(Some(window), Some(minds)) => Self::parse(window, minds).map(Some),
|
||||
(None, None) => Ok(None),
|
||||
_ => bail!("HEARTBEAT_SLEEP_UTC and HEARTBEAT_SLEEP_MINDS are set together or not at all"),
|
||||
}
|
||||
}
|
||||
|
||||
/// `window` is `HH:MM-HH:MM` in UTC, `minds` the slugs comma-separated.
|
||||
pub fn parse(window: &str, minds: &str) -> Result<Self> {
|
||||
let minute = |text: &str| -> Result<u32> {
|
||||
let (hour, minute) = text.trim().split_once(':').ok_or_else(|| anyhow!("the night window is HH:MM-HH:MM"))?;
|
||||
let (hour, minute) = (hour.parse::<u32>()?, minute.parse::<u32>()?);
|
||||
if hour > 23 || minute > 59 {
|
||||
bail!("the night window is HH:MM-HH:MM");
|
||||
}
|
||||
Ok(hour * 60 + minute)
|
||||
};
|
||||
let (from, to) = window.split_once('-').ok_or_else(|| anyhow!("the night window is HH:MM-HH:MM"))?;
|
||||
let (from_min, to_min) = (minute(from)?, minute(to)?);
|
||||
if from_min == to_min {
|
||||
bail!("the night window is empty");
|
||||
}
|
||||
let minds: Vec<Slug> = minds.split(',').map(|slug| Slug(slug.trim().to_string())).filter(|slug| !slug.0.is_empty()).collect();
|
||||
if minds.is_empty() {
|
||||
bail!("no Mind is listed to sleep");
|
||||
}
|
||||
Ok(Self { from_min, to_min, minds })
|
||||
}
|
||||
|
||||
/// When the window `now` is inside opened, or none outside it.
|
||||
fn opened(&self, now: DateTime<Utc>) -> Option<DateTime<Utc>> {
|
||||
let minute = now.hour() * 60 + now.minute();
|
||||
let inside = if self.from_min < self.to_min { (self.from_min..self.to_min).contains(&minute) } else { minute >= self.from_min || minute < self.to_min };
|
||||
if !inside {
|
||||
return None;
|
||||
}
|
||||
let day = if minute >= self.from_min { now.date_naive() } else { now.date_naive().pred_opt()? };
|
||||
Some(day.and_hms_opt(self.from_min / 60, self.from_min % 60, 0)?.and_utc())
|
||||
}
|
||||
/// Seconds between beats: `HEARTBEAT_BEAT_S`, else `DEFAULT_BEAT_S`.
|
||||
pub beat_s: u32,
|
||||
}
|
||||
|
||||
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>, 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()
|
||||
|
|
@ -181,22 +118,25 @@ impl Config {
|
|||
Ok(Self {
|
||||
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.
|
||||
#[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>>,
|
||||
}
|
||||
|
||||
const STATE_KEY: &str = "state";
|
||||
|
|
@ -217,9 +157,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 +178,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 +193,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 +217,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 +294,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 +312,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 +349,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 +373,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 +413,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 +430,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 +439,26 @@ 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: 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) -> Woke {
|
||||
let turn = RunTurn::PersonaTurn;
|
||||
if !matches!(organs.control.effective(row.slug()), Effective::Released(_)) {
|
||||
return Woke::Refused("the brake is held".into());
|
||||
}
|
||||
}
|
||||
|
||||
/// 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(),
|
||||
});
|
||||
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}")),
|
||||
}
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
/// One poll.
|
||||
|
|
@ -745,7 +468,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() });
|
||||
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 +479,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 +504,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 +524,33 @@ 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) {
|
||||
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(slug.0.clone());
|
||||
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.
|
||||
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)) {
|
||||
continue;
|
||||
}
|
||||
match launch(organs, row, RunTurn::Sleep, vec![])? {
|
||||
Launched::Run(label) => {
|
||||
row.note = format!("asleep {label}");
|
||||
row.busy = true;
|
||||
launched.push(row.slug().clone());
|
||||
launches.push(label);
|
||||
break;
|
||||
}
|
||||
Launched::Busy => row.busy = true,
|
||||
Launched::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 +558,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]) {
|
||||
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(slug.0.clone());
|
||||
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 +600,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
File diff suppressed because it is too large
Load diff
|
|
@ -4,22 +4,16 @@
|
|||
//! hand on them. The brakes are the operator's consent, not her body: the store
|
||||
//! is root-owned and no organ of hers writes it.
|
||||
//!
|
||||
//! `launch` and `queue` are the launch primitive: the work queue, the
|
||||
//! 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.
|
||||
//! `launch` holds her units under systemd and the reads of the runs a Self run
|
||||
//! holds; `queue` is her port to her mind and what an open question holds.
|
||||
//!
|
||||
//! `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).
|
||||
//!
|
||||
//! `stream` is the stream organ: the `mind-stream` binary projects her own state through
|
||||
//! Ghostlight's Projector, lets a base model continue it, and admits the least familiar
|
||||
//! continuation as a `thought` with its whole trace. It admits nothing else.
|
||||
//! `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
|
||||
|
|
@ -35,8 +29,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;
|
||||
|
|
@ -47,10 +41,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 stream;
|
||||
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
|
|
@ -1,34 +1,24 @@
|
|||
//! The Persona's view of open work, and the guard on it: what may be worked
|
||||
//! (`queue`) and whether an item has stopped paying for its runs (`breaker`).
|
||||
//! Her organs' port to her mind (`MindPort`, and `HuginnMind` over a daemon),
|
||||
//! the paged reads built on it, and what an open question holds (`on_hold`),
|
||||
//! which her Self's inbox reads before it tells a brief.
|
||||
//!
|
||||
//! Nothing here keeps state. Both answers are derived from the mind on every
|
||||
//! call, through `MindPort`: the mind is the only store, so a breaker that
|
||||
//! needed a file of its own would be a second owner of what the runs already
|
||||
//! record (target invariant run-is-the-grant).
|
||||
//! Nothing here keeps state: every answer is derived from the mind on every
|
||||
//! call. The work queue and the repetition breaker that guarded Self-run
|
||||
//! launches are parked with them at tag `parked/self-run-launch`.
|
||||
|
||||
use anyhow::{Result, anyhow, bail};
|
||||
use cultnet_rs::{Incoming, Selection};
|
||||
use eureka_pipeline::{
|
||||
Line, OrgRepo, PipelineDocument, PipelineKind, PipelineRef, ResolutionOutcome, RunOperator, RunTurn,
|
||||
Slug,
|
||||
};
|
||||
use cultnet_rs::Selection;
|
||||
use eureka_pipeline::{Line, OrgRepo, PipelineDocument, PipelineRef, Slug};
|
||||
use eureka_state::hold::{self, Hold};
|
||||
use eureka_state::{ClientError, HuginnClient};
|
||||
use huginn_mind::{
|
||||
Faculty, HuginnMindRequest, HuginnMindResponse, PipelineAdmissionBatch,
|
||||
PipelineAdmissionOutcome, PipelineDocumentSummary, PipelineFacts, PipelineProvenance,
|
||||
PipelineSelectionPage, PipelineStatusSummary, RepoActivity, RootTouched, SemanticQuery,
|
||||
PipelineAdmissionOutcome, PipelineDocumentSummary, PipelineProvenance,
|
||||
PipelineSelectionPage, RepoActivity, RootTouched, SemanticQuery,
|
||||
};
|
||||
|
||||
/// The last `CONSECUTIVE` ended runs over an item, all empty, trip its breaker.
|
||||
pub const CONSECUTIVE: usize = 3;
|
||||
/// So do `EMPTY_SHARE` empty ones among the last `WINDOW` (Codex auto-review's
|
||||
/// per-turn breaker, `docs/research/autonomous-harness-prior-art.md` section 0).
|
||||
pub const WINDOW: usize = 50;
|
||||
pub const EMPTY_SHARE: usize = 10;
|
||||
|
||||
/// The two things her launch organs ask of the mind: a typed selection, and
|
||||
/// an admission as herself. The impl for a daemon is `HuginnMind`; a test
|
||||
/// What her organs ask of the mind: a typed selection, and an admission as
|
||||
/// herself. The impl for a daemon is `HuginnMind`; a test
|
||||
/// hands in a mind opened over a temporary directory.
|
||||
pub trait MindPort {
|
||||
/// The instance this port acts for.
|
||||
|
|
@ -152,27 +142,6 @@ pub(crate) fn headers(
|
|||
hold::headers(&|asking| mind.query(asking), selection)
|
||||
}
|
||||
|
||||
/// The claims of the runs of `instance` that are still in force and not in
|
||||
/// `ending`: the work someone holds.
|
||||
pub(crate) fn live_claims(mind: &dyn MindPort, instance: &Slug, ending: &[PipelineRef]) -> Result<Vec<PipelineRef>> {
|
||||
let runs = in_force(any_of(of_kind(PipelineKind::Run), "root", &[&instance.0]));
|
||||
let mut claims = Vec::new();
|
||||
for header in headers(mind, &runs)? {
|
||||
if let PipelineFacts::Run { claims: held, .. } = header.facts
|
||||
&& !ending.contains(&header.id)
|
||||
{
|
||||
claims.extend(held);
|
||||
}
|
||||
}
|
||||
Ok(claims)
|
||||
}
|
||||
|
||||
/// The claims of the run `run`, resolved or not (a citation outlives its
|
||||
/// resolution). Empty when the mind has no such run.
|
||||
pub(crate) fn claims_of(mind: &dyn MindPort, run: &PipelineRef) -> Result<Vec<PipelineRef>> {
|
||||
hold::claims_of(&|asking| mind.query(asking), run)
|
||||
}
|
||||
|
||||
/// Every question holding anything and what it holds: `eureka_state::hold`,
|
||||
/// the one decider, which `huginn blocked` reads too.
|
||||
pub(crate) fn holds(mind: &dyn MindPort) -> Result<Vec<Hold>> {
|
||||
|
|
@ -205,303 +174,16 @@ pub fn on_hold(mind: &dyn MindPort) -> Result<Held> {
|
|||
))
|
||||
}
|
||||
|
||||
/// What may be worked, oldest first: the cut specs in force that no report
|
||||
/// answers, that no open question holds, and that no live run claims. A run in
|
||||
/// `ending` is one the caller closes in the same admission that opens the next,
|
||||
/// so it claims nothing here.
|
||||
pub fn queue(
|
||||
mind: &dyn MindPort,
|
||||
instance: &Slug,
|
||||
ending: &[PipelineRef],
|
||||
held: &Held,
|
||||
) -> Result<Vec<PipelineRef>> {
|
||||
let unreported = Selection {
|
||||
cited: Some(Incoming {
|
||||
role: "cut_spec".into(),
|
||||
exists: false,
|
||||
}),
|
||||
..in_force(of_kind(PipelineKind::CutSpec))
|
||||
};
|
||||
let claimed = live_claims(mind, instance, ending)?;
|
||||
Ok(headers(mind, &unreported)?
|
||||
.into_iter()
|
||||
.map(|header| header.id)
|
||||
.filter(|spec| !held.contains(spec) && !claimed.contains(spec))
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Whether an item may still be launched on.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Breaker {
|
||||
Clear,
|
||||
/// Tripped, and the trip belongs to `run`: the newest ended counted run on
|
||||
/// the item, the run whose end completed the streak or the share.
|
||||
Trip { run: PipelineRef },
|
||||
}
|
||||
|
||||
/// The repetition breaker, derived from the documents that cite the item.
|
||||
///
|
||||
/// One role-less citation selection over every schema gives the set: the runs
|
||||
/// that claim the item, and whatever else points at it (a follow-up sourced
|
||||
/// from it, a report, a resolution, a question raised in it). The newest
|
||||
/// question raised in the item is the boundary: only runs admitted after it
|
||||
/// count, so answering a trip starts a fresh count. A counted run is a Self run
|
||||
/// of hers (typed `turn` and `operated_by`) that was not withdrawn; a
|
||||
/// withdrawn run never ran and is no run here. A counted run is ended when
|
||||
/// resolved or listed in `ending`, and is empty when no document of the set
|
||||
/// other than a run was admitted from its own admission up to the next counted
|
||||
/// run's (or to now). Trip, naming the newest ended counted run, when the last `CONSECUTIVE` ended runs are empty, or
|
||||
/// `EMPTY_SHARE` of the last `WINDOW`. What else the mind admitted, such as a
|
||||
/// Persona turn's run or an unrelated ruling, cites nothing here and counts for
|
||||
/// nothing.
|
||||
pub fn breaker(mind: &dyn MindPort, item: &PipelineRef, ending: &[PipelineRef]) -> Result<Breaker> {
|
||||
let mut set = headers(mind, &Selection { cites: Some(citing(item, None)), ..Selection::default() })?;
|
||||
set.sort_by_key(|header| header.admission.ordinal);
|
||||
let asked_in_item = set
|
||||
.iter()
|
||||
.filter(|header| matches!(&header.facts, PipelineFacts::Question { raised_in: Some(raised), .. } if raised == item))
|
||||
.map(|header| header.admission.ordinal);
|
||||
let asked_in_run = Selection {
|
||||
cites: Some(citing(item, Some("claims"))),
|
||||
cited: Some(Incoming { role: "raised_in".into(), exists: true }),
|
||||
..of_kind(PipelineKind::Run)
|
||||
};
|
||||
let boundary = asked_in_item.chain(headers(mind, &asked_in_run)?.into_iter().map(|run| run.admission.ordinal)).max();
|
||||
let after = |header: &&PipelineDocumentSummary| boundary.is_none_or(|boundary| header.admission.ordinal > boundary);
|
||||
let withdrawn =
|
||||
|run: &PipelineDocumentSummary| matches!(run.status, PipelineStatusSummary::Resolved { outcome: ResolutionOutcome::Withdrawn { .. }, .. });
|
||||
// (admission ordinal, ended, ref) of each counted run, oldest first.
|
||||
let ran: Vec<(u64, bool, PipelineRef)> = set
|
||||
.iter()
|
||||
.filter(after)
|
||||
.filter(|header| {
|
||||
matches!(header.facts, PipelineFacts::Run { turn: RunTurn::SelfRun, operated_by: RunOperator::Mind, .. }) && !withdrawn(header)
|
||||
})
|
||||
.map(|run| (run.admission.ordinal, matches!(run.status, PipelineStatusSummary::Resolved { .. }) || ending.contains(&run.id), run.id.clone()))
|
||||
.collect();
|
||||
let ended: Vec<usize> = (0..ran.len()).filter(|index| ran[*index].1).collect();
|
||||
let considered: Vec<usize> = ended.iter().rev().take(WINDOW).copied().collect();
|
||||
let admitted: Vec<u64> =
|
||||
set.iter().filter(|header| !matches!(header.facts, PipelineFacts::Run { .. })).map(|header| header.admission.ordinal).collect();
|
||||
|
||||
// Newest first, like `considered`.
|
||||
let empty: Vec<bool> = considered
|
||||
.iter()
|
||||
.map(|index| {
|
||||
let end = ran.get(index + 1).map_or(u64::MAX, |next| next.0);
|
||||
!admitted.iter().any(|ordinal| *ordinal >= ran[*index].0 && *ordinal < end)
|
||||
})
|
||||
.collect();
|
||||
let consecutive = empty.len() >= CONSECUTIVE && empty[..CONSECUTIVE].iter().all(|empty| *empty);
|
||||
let share = empty.iter().filter(|empty| **empty).count() >= EMPTY_SHARE;
|
||||
Ok(match ended.last() {
|
||||
Some(newest) if consecutive || share => Breaker::Trip { run: ran[*newest].2.clone() },
|
||||
_ => Breaker::Clear,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
use eureka_pipeline::{RunOperator, RunTurn};
|
||||
use eureka_pipeline::{PipelineKind, RunOperator, RunTurn};
|
||||
|
||||
use super::*;
|
||||
|
||||
/// `queue` against a hold read now.
|
||||
fn queued(
|
||||
mind: &dyn MindPort,
|
||||
instance: &Slug,
|
||||
ending: &[PipelineRef],
|
||||
) -> Result<Vec<PipelineRef>> {
|
||||
queue(mind, instance, ending, &on_hold(mind)?)
|
||||
}
|
||||
use crate::testkit::*;
|
||||
|
||||
/// A mind holding the spec `x` and a history of ended runs on it.
|
||||
fn worked(runs: &[bool]) -> (TestMind, PipelineRef) {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![spec("x")]);
|
||||
let item = spec_ref("x");
|
||||
history(&mind, &item, runs);
|
||||
(mind, item)
|
||||
}
|
||||
|
||||
const E: bool = false;
|
||||
const N: bool = true;
|
||||
|
||||
fn tripped(runs: &[bool]) -> bool {
|
||||
let (mind, item) = worked(runs);
|
||||
matches!(breaker(&mind, &item, &[]).unwrap(), Breaker::Trip { .. })
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn three_consecutive_empty_runs_trip_and_two_do_not() {
|
||||
assert!(!tripped(&[]));
|
||||
assert!(!tripped(&[E, E]));
|
||||
assert!(tripped(&[E, E, E]));
|
||||
assert!(tripped(&[N, E, E, E]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_document_in_a_runs_window_makes_that_run_not_empty() {
|
||||
assert!(!tripped(&[E, E, N, E, E]), "one run with a document in its window breaks the streak");
|
||||
assert!(tripped(&[E, E, N, E, E, E]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ten_empty_of_the_last_fifty_trip_with_gaps_and_nine_do_not() {
|
||||
let ten: Vec<bool> = [E, E, N].repeat(5);
|
||||
assert!(tripped(&ten), "10 empty, never 3 in a row");
|
||||
let mut nine: Vec<bool> = [E, E, N].repeat(4);
|
||||
nine.push(E);
|
||||
assert!(!tripped(&nine), "9 empty, never 3 in a row");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_last_fifty_ended_runs_are_counted() {
|
||||
let mut runs = vec![E];
|
||||
for _ in 0..9 {
|
||||
runs.extend([E, N]);
|
||||
}
|
||||
runs.extend([N].repeat(32));
|
||||
assert_eq!(runs.len(), 51);
|
||||
assert!(!tripped(&runs), "the tenth empty run is the 51st newest");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_operators_self_runs_on_the_item_are_not_hers_to_count() {
|
||||
let after_a_run_by = |operated_by: RunOperator| {
|
||||
let (mind, item) = worked(&[E, E]);
|
||||
mind.committed(vec![run("third", RunTurn::SelfRun, operated_by, std::slice::from_ref(&item))]);
|
||||
mind.committed(vec![close(run_ref("third"), recorded())]);
|
||||
breaker(&mind, &item, &[]).unwrap()
|
||||
};
|
||||
assert!(matches!(after_a_run_by(RunOperator::Mind), Breaker::Trip { .. }), "hers, the third empty one");
|
||||
assert_eq!(after_a_run_by(RunOperator::Operator), Breaker::Clear, "his, counted for nothing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_fiftieth_newest_ended_run_is_counted() {
|
||||
// Ten empty of exactly fifty, the tenth the oldest, never three in a row.
|
||||
let mut runs = vec![E];
|
||||
for _ in 0..9 {
|
||||
runs.extend([E, N]);
|
||||
}
|
||||
runs.extend([N].repeat(31));
|
||||
assert_eq!(runs.len(), WINDOW);
|
||||
assert!(tripped(&runs));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runs_born_closed_in_the_questions_batch_are_before_the_count() {
|
||||
// One batch holds the question raised in x and two runs of hers on x, each closed at birth.
|
||||
let (mind, item) = worked(&[]);
|
||||
mind.committed(vec![
|
||||
question_in("fork", &item),
|
||||
run("pa", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item)),
|
||||
close(run_ref("pa"), recorded()),
|
||||
run("pb", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item)),
|
||||
close(run_ref("pb"), recorded()),
|
||||
]);
|
||||
// Then thirteen ended runs, nine of them empty and never three in a row.
|
||||
let mut after: Vec<bool> = [E, E, N].repeat(4);
|
||||
after.push(E);
|
||||
history(&mind, &item, &after);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Clear, "the pair is not after the question, so the tenth empty run is not counted");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn three_empty_runs_trip_after_any_history_of_non_empty_ones() {
|
||||
for before in [0usize, 1, 12, 20] {
|
||||
let mut runs = vec![N; before];
|
||||
runs.extend([E, E, E]);
|
||||
assert!(tripped(&runs), "{before} non-empty runs then three empty ones");
|
||||
assert!(!tripped(&[vec![N; before], vec![E, E]].concat()), "two empty ones never trip");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_documents_that_cite_the_item_make_a_run_non_empty() {
|
||||
let (mind, item) = worked(&[]);
|
||||
mind.committed(vec![spec("other")]);
|
||||
for n in 0..3 {
|
||||
// A Persona turn's run, a ruling and a follow-up of another item all land in the window.
|
||||
mind.committed(vec![run(&format!("p{n}"), RunTurn::PersonaTurn, RunOperator::Mind, &[])]);
|
||||
mind.committed(vec![ruling(&format!("r{n}"), None), follow_up(&format!("o{n}"), &spec_ref("other"))]);
|
||||
ended_run(&mind, &item, "h", n, false);
|
||||
mind.committed(vec![close(run_ref(&format!("p{n}")), recorded())]);
|
||||
}
|
||||
assert!(matches!(breaker(&mind, &item, &[]).unwrap(), Breaker::Trip { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_run_in_ending_is_an_ended_empty_run() {
|
||||
let (mind, item) = worked(&[E, E]);
|
||||
mind.committed(vec![run("dying", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item))]);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Clear, "open, it is not yet ended");
|
||||
assert!(matches!(breaker(&mind, &item, &[run_ref("dying")]).unwrap(), Breaker::Trip { .. }));
|
||||
let instance = Slug(INSTANCE.into());
|
||||
assert!(
|
||||
queued(&mind, &instance, &[])
|
||||
.unwrap()
|
||||
.iter()
|
||||
.all(|spec| *spec != item),
|
||||
"it claims the item"
|
||||
);
|
||||
assert!(
|
||||
queued(&mind, &instance, &[run_ref("dying")])
|
||||
.unwrap()
|
||||
.contains(&item),
|
||||
"an ending run claims nothing"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_newest_question_in_the_item_starts_a_fresh_count() {
|
||||
// Nine empty of the last fifty, then a question about the item is raised and answered.
|
||||
let (mind, item) = worked(&[E, E, N, E, E, N, E, E, N, E, E, N, E]);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Clear);
|
||||
mind.committed(vec![question_in("fork", &item)]);
|
||||
mind.committed(vec![ruling("answer", Some((&id("question", "fork"), "A")))]);
|
||||
ended_run(&mind, &item, "g", 0, false);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Clear, "the tenth empty run, but nine of them are before the question");
|
||||
ended_run(&mind, &item, "g", 1, false);
|
||||
ended_run(&mind, &item, "g", 2, false);
|
||||
assert!(matches!(breaker(&mind, &item, &[]).unwrap(), Breaker::Trip { .. }), "three empty runs after it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_withdrawn_run_never_ran_and_is_no_boundary() {
|
||||
let withdrawn_between = |ruling_after: bool| {
|
||||
let (mind, item) = worked(&[E]);
|
||||
mind.committed(vec![run("w", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item))]);
|
||||
mind.committed(vec![close(run_ref("w"), withdrawn())]);
|
||||
if ruling_after {
|
||||
mind.committed(vec![follow_up("after", &item)]);
|
||||
}
|
||||
ended_run(&mind, &item, "h", 1, false);
|
||||
ended_run(&mind, &item, "h", 2, false);
|
||||
breaker(&mind, &item, &[]).unwrap()
|
||||
};
|
||||
assert!(matches!(withdrawn_between(false), Breaker::Trip { .. }), "the withdrawn run is neither counted nor does it break the streak");
|
||||
assert_eq!(withdrawn_between(true), Breaker::Clear, "a document after the withdrawn run still lands in the first run's window");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_runs_window_includes_its_own_batch_and_stops_before_the_next_runs() {
|
||||
let (mind, item) = worked(&[E, E]);
|
||||
// The third run opens with a ruling in its batch; it is still open.
|
||||
mind.committed(vec![run("h2", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item)), follow_up("with-run", &item)]);
|
||||
mind.committed(vec![close(run_ref("h2"), recorded())]);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Clear, "the ruling in its own batch is in its window");
|
||||
|
||||
let (mind, item) = worked(&[E, E, E]);
|
||||
mind.committed(vec![run("live", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item)), follow_up("with-next", &item)]);
|
||||
assert!(matches!(breaker(&mind, &item, &[]).unwrap(), Breaker::Trip { .. }), "the next run's batch is not the previous run's window");
|
||||
}
|
||||
|
||||
/// The adapter over the wire vocabulary, answered by a real mind.
|
||||
#[test]
|
||||
fn the_daemon_port_asks_and_admits_in_the_wire_vocabulary() {
|
||||
|
|
@ -509,10 +191,8 @@ mod tests {
|
|||
mind.committed(vec![spec("x")]);
|
||||
let port = HuginnMind::over(Slug(INSTANCE.into()), Box::new(|request| Ok(mind.answer(request))));
|
||||
assert_eq!(port.instance().0, INSTANCE);
|
||||
assert_eq!(
|
||||
queued(&port, port.instance(), &[]).unwrap(),
|
||||
vec![spec_ref("x")]
|
||||
);
|
||||
let specs: Vec<PipelineRef> = headers(&port, &in_force(of_kind(PipelineKind::CutSpec))).unwrap().into_iter().map(|header| header.id).collect();
|
||||
assert_eq!(specs, vec![spec_ref("x")]);
|
||||
let outcome = port
|
||||
.admit(
|
||||
"agent-x",
|
||||
|
|
@ -621,81 +301,6 @@ mod tests {
|
|||
assert!(format!("{down:#}").contains("could not be asked") && !format!("{down:#}").contains("canary"), "{down:#}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runs_of_another_item_are_not_this_items_runs() {
|
||||
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![spec("x"), spec("y")]);
|
||||
history_as(&mind, &spec_ref("x"), "x", &[E, E]);
|
||||
history_as(&mind, &spec_ref("y"), "y", &[E, E, E]);
|
||||
assert_eq!(breaker(&mind, &spec_ref("x"), &[]).unwrap(), Breaker::Clear);
|
||||
assert!(matches!(breaker(&mind, &spec_ref("y"), &[]).unwrap(), Breaker::Trip { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_oldest_counted_runs_own_batch_is_in_its_window() {
|
||||
let (mind, item) = worked(&[]);
|
||||
mind.committed(vec![run("h0", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item)), follow_up("with-oldest", &item)]);
|
||||
mind.committed(vec![close(run_ref("h0"), recorded())]);
|
||||
ended_run(&mind, &item, "h", 1, false);
|
||||
ended_run(&mind, &item, "h", 2, false);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Clear);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_queue_is_the_unreported_unblocked_unclaimed_specs_oldest_first() {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![spec("a"), spec("b"), spec("c"), spec("d"), spec("e")]);
|
||||
let instance = Slug(INSTANCE.into());
|
||||
let all: Vec<PipelineRef> = ["a", "b", "c", "d", "e"]
|
||||
.iter()
|
||||
.map(|cut| spec_ref(cut))
|
||||
.collect();
|
||||
assert_eq!(queued(&mind, &instance, &[]).unwrap(), all);
|
||||
|
||||
mind.committed(vec![report("b")]);
|
||||
mind.committed(vec![question_in("fork", &spec_ref("c"))]);
|
||||
mind.committed(vec![run(
|
||||
"held",
|
||||
RunTurn::SelfRun,
|
||||
RunOperator::Operator,
|
||||
&[spec_ref("d")],
|
||||
)]);
|
||||
assert_eq!(
|
||||
queued(&mind, &instance, &[]).unwrap(),
|
||||
vec![spec_ref("a"), spec_ref("e")]
|
||||
);
|
||||
|
||||
mind.committed(vec![close(run_ref("held"), recorded())]);
|
||||
assert_eq!(
|
||||
queued(&mind, &instance, &[]).unwrap(),
|
||||
vec![spec_ref("a"), spec_ref("d"), spec_ref("e")],
|
||||
"a closed run claims nothing"
|
||||
);
|
||||
let other = Slug("another-instance".into());
|
||||
mind.committed(vec![run(
|
||||
"again",
|
||||
RunTurn::SelfRun,
|
||||
RunOperator::Operator,
|
||||
&[spec_ref("a")],
|
||||
)]);
|
||||
assert_eq!(
|
||||
queued(&mind, &other, &[]).unwrap().len(),
|
||||
3,
|
||||
"claims are read for the instance asked"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_trip_names_the_newest_ended_counted_run() {
|
||||
let (mind, item) = worked(&[E, E, E, E]);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Trip { run: run_ref("h3") });
|
||||
mind.committed(vec![run("live", RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(&item))]);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Trip { run: run_ref("h3") }, "a run still open is not ended");
|
||||
assert_eq!(breaker(&mind, &item, &[run_ref("live")]).unwrap(), Breaker::Trip { run: run_ref("live") }, "a run in ending is");
|
||||
}
|
||||
|
||||
/// Two items claimed together by every run of a history; the last one ended empty.
|
||||
fn bundle() -> (TestMind, PipelineRef, PipelineRef) {
|
||||
let mind = TestMind::seeded();
|
||||
mind.committed(vec![spec("a"), spec("b")]);
|
||||
|
|
@ -718,11 +323,6 @@ mod tests {
|
|||
held.contains(&a) && held.contains(&b) && held.items().len() == 2,
|
||||
"{held:?}: the claims of the resolved run"
|
||||
);
|
||||
assert_eq!(
|
||||
queued(&mind, &Slug(INSTANCE.into()), &[]).unwrap(),
|
||||
Vec::<PipelineRef>::new(),
|
||||
"queue reads the same hold"
|
||||
);
|
||||
let question = id("question", "fork");
|
||||
if withdraw {
|
||||
mind.committed(vec![close(reference(PipelineKind::Question, &question), withdrawn())]);
|
||||
|
|
@ -789,35 +389,6 @@ mod tests {
|
|||
assert!(!on_hold(&mind).unwrap().contains(&b));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn answering_a_question_in_the_run_gives_every_claim_of_it_a_fresh_count() {
|
||||
let (mind, a, b) = bundle();
|
||||
assert_eq!(breaker(&mind, &a, &[]).unwrap(), Breaker::Trip { run: run_ref("f2") });
|
||||
assert_eq!(breaker(&mind, &b, &[]).unwrap(), Breaker::Trip { run: run_ref("f2") });
|
||||
mind.committed(vec![question_in("fork", &run_ref("f2"))]);
|
||||
mind.committed(vec![ruling("answer", Some((&id("question", "fork"), "A")))]);
|
||||
for item in [&a, &b] {
|
||||
assert_eq!(breaker(&mind, item, &[]).unwrap(), Breaker::Clear, "counts only runs after f2");
|
||||
}
|
||||
for n in 0..3 {
|
||||
mind.committed(vec![run(&format!("g{n}"), RunTurn::SelfRun, RunOperator::Mind, &[a.clone(), b.clone()])]);
|
||||
mind.committed(vec![close(run_ref(&format!("g{n}")), recorded())]);
|
||||
let expected = if n == 2 { Breaker::Trip { run: run_ref("g2") } } else { Breaker::Clear };
|
||||
assert_eq!(breaker(&mind, &b, &[]).unwrap(), expected, "b has no question of its own and trips after three fresh empty runs, never before");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_question_in_a_run_that_did_not_claim_the_item_is_no_boundary() {
|
||||
let (mind, item) = worked(&[E, E, E]);
|
||||
mind.committed(vec![spec("y")]);
|
||||
mind.committed(vec![run("other", RunTurn::SelfRun, RunOperator::Mind, &[spec_ref("y")])]);
|
||||
mind.committed(vec![question_in("fork", &run_ref("other"))]);
|
||||
assert_eq!(breaker(&mind, &item, &[]).unwrap(), Breaker::Trip { run: run_ref("h2") }, "the question holds y's run, not x's count");
|
||||
}
|
||||
|
||||
/// A daemon that is not there: the failure names no input, endpoint or
|
||||
/// instance.
|
||||
#[test]
|
||||
fn an_unreachable_mind_names_no_input() {
|
||||
let canary = "canary-instance-7f3a91";
|
||||
|
|
@ -827,7 +398,7 @@ mod tests {
|
|||
Slug(canary.into()),
|
||||
Duration::from_millis(300),
|
||||
));
|
||||
let error = queued(&port, &Slug(canary.into()), &[]).unwrap_err();
|
||||
let error = on_hold(&port).unwrap_err();
|
||||
let shown = format!("{error:#}");
|
||||
assert!(shown.contains("unavailable"), "{shown}");
|
||||
assert!(!shown.contains(canary) && !shown.contains("127.0.0.1"), "{shown}");
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -229,8 +229,8 @@ fn flush_text(why: Why) -> String {
|
|||
format!("{cause} {FLUSH_MESSAGE} Close each brief that is done by the closing rule; a brief whose change is not on its default branch stays open, or is superseded by your own follow-up when it waits on someone else, and the next session continues it.")
|
||||
}
|
||||
|
||||
/// The label of a run, the last segment of its id (`<instance>:run:<label>`): what the opening receipt of every run already carries as its session
|
||||
/// (`launch::run_label`), so a `huginn` admission and the run's own receipts name one session in one spelling.
|
||||
/// The label of a run, the last segment of its id (`<instance>:run:<label>`): what the opening receipt of every run already carries as its session,
|
||||
/// so a `huginn` admission and the run's own receipts name one session in one spelling.
|
||||
fn run_label(run: &PipelineRef) -> &str {
|
||||
run.id.0.rsplit(':').next().unwrap_or(&run.id.0)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,723 +0,0 @@
|
|||
//! The stream organ: her stream of thought, offline. Once per tick it projects her own
|
||||
//! state through Ghostlight's Projector, lets a base model continue the projection a few ways,
|
||||
//! keeps the continuation least like anything she has thought before, and admits it as one
|
||||
//! `thought` with its whole trace. `mind-stream@<instance>` (agent `mind-stream`) alone decides
|
||||
//! whether a thought exists and what it says.
|
||||
//!
|
||||
//! Grounding: no selection the seed makes names kind `thought`. A thought's words reach a later
|
||||
//! seed only through something she wrote (a memory, a reply, a run). Cadence is derived from her
|
||||
//! newest thought's admission time and nothing else is kept: no state file, no embedder (the
|
||||
//! daemon scores nearness), no second prompt builder.
|
||||
//!
|
||||
//! The stream never admits anything but thoughts, never puts her persona state, never sends chat
|
||||
//! and never starts a run. Delivery to a Persona is another organ's.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::net::{IpAddr, SocketAddr};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use chrono::{DateTime, Duration as Span, Utc};
|
||||
use eureka_pipeline::{
|
||||
Label, Line, Para, PipelineDocument, PipelineKind, PipelineRef, PipelineThought, ResolutionOutcome, Short, Slug, THOUGHT_CANDIDATES_MAX,
|
||||
THOUGHT_NEAREST_NONE_PERMILLE,
|
||||
};
|
||||
use eureka_state::HuginnClient;
|
||||
use ghostlight_persona_projection::{ProjectorPrompt, build_projector_prompt};
|
||||
use huginn_mind::{HuginnMindResponse, PipelineAdmissionOutcome, PipelineDocumentView, PipelinePageItems};
|
||||
use model_connector::{ConnectorClient, InputItem, ProviderRequest, ReasoningEffort, StopReason, TransportDisposition, TransportEventPayload, TransportInvocation, TransportOutcome};
|
||||
use serde_json::{Value, json};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::control::Effective;
|
||||
use crate::persona::{Responsibilities, outcome_words, quoted, recent_runs, render_responsibilities, repo_notice_line};
|
||||
use crate::queue::{MindPort, any_of, headers, in_force, of_kind};
|
||||
use crate::recall::{self, Cue};
|
||||
|
||||
pub const AGENT: &str = "mind-stream";
|
||||
|
||||
/// No tick sooner than this after her newest thought, whatever has happened. The gap counts from the thought's label
|
||||
/// (the tick time it was made for), not from its admission: a model run of M seconds leaves `STREAM_MIN_GAP_S - M`
|
||||
/// seconds of quiet after the thought is admitted.
|
||||
pub const STREAM_MIN_GAP_S: i64 = 900;
|
||||
/// How often the stream process polls: the unit in which a poll "crosses" a `STREAM_MIN_GAP_S` mark.
|
||||
pub const STREAM_POLL_S: i64 = 60;
|
||||
/// A tick is due after this long even when nothing has happened.
|
||||
pub const STREAM_IDLE_GAP_S: i64 = 3600;
|
||||
/// The most bytes of context and of stimulus the Projector is shown.
|
||||
const SECTION_MAX_BYTES: usize = 3800;
|
||||
/// An in-force memory this old may come unbidden when nothing is new.
|
||||
const UNBIDDEN_AFTER_DAYS: i64 = 7;
|
||||
|
||||
const PROJECTOR_WORDS: usize = 180;
|
||||
const CONTINUATIONS: usize = THOUGHT_CANDIDATES_MAX;
|
||||
const TEMPERATURE_PERMILLE: u16 = 1000;
|
||||
const MIN_P_PERMILLE: u16 = 50;
|
||||
const MAX_TOKENS: u16 = 160;
|
||||
const NUM_THREAD: u32 = 4;
|
||||
|
||||
/// The Ghostlight revision `build_projector_prompt` comes from; a test holds it to the manifest's pin.
|
||||
const GHOSTLIGHT_REV: &str = "c43eb54962ed4a09cc737ecac73a16b0d7ce62b4";
|
||||
/// How the continuer frame writes the date.
|
||||
const FRAME_DATE_FORMAT: &str = "%-d %B %Y";
|
||||
|
||||
/// The setting the Projector renders for: where she is between conversations.
|
||||
const STREAM_GUIDANCE: &str = "This person is a Mind of the GameCult swarm in the quiet between conversations. What she holds is her repos, her fellow Minds and the people she works with. Render what is on her mind in this quiet.";
|
||||
|
||||
/// What each part of the context may take. Their sum and the separators stay under
|
||||
/// `SECTION_MAX_BYTES`, so no section can crowd another out.
|
||||
const BUDGETS: [(&str, usize); 7] = [
|
||||
("values", 350),
|
||||
("pressures", 450),
|
||||
("bonds", 500),
|
||||
("runs", 250),
|
||||
("responsibilities", 800),
|
||||
("recall", 800),
|
||||
("replies", 600),
|
||||
];
|
||||
|
||||
const REPLIES_SHOWN: u32 = 3;
|
||||
const RECENT_READ: u32 = 30;
|
||||
const READS_MAX: usize = 64;
|
||||
const LINE_MAX_BYTES: usize = 1000;
|
||||
const PARA_MAX_BYTES: usize = 4000;
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// The model port.
|
||||
|
||||
/// How a continuation is sampled.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Sampling {
|
||||
pub temperature_permille: u16,
|
||||
pub min_p_permille: u16,
|
||||
pub max_tokens: u16,
|
||||
}
|
||||
|
||||
impl Sampling {
|
||||
const STREAM: Self = Self { temperature_permille: TEMPERATURE_PERMILLE, min_p_permille: MIN_P_PERMILLE, max_tokens: MAX_TOKENS };
|
||||
}
|
||||
|
||||
/// The Projector: state to narrative. Its one owner is `KitProjector`, her own caller on the connector. A fault is an error:
|
||||
/// the tick ends, the process does not.
|
||||
pub trait Projector {
|
||||
/// The model id every projection of this Projector names; the thought records it, so it is the model that ran.
|
||||
fn model(&self) -> &str;
|
||||
/// One turn: `prompt` is the turn's one user text and `request_id` is its identity on the connector.
|
||||
fn project(&self, request_id: &str, prompt: &str) -> Result<String>;
|
||||
}
|
||||
|
||||
/// The continuer: raw continuation of a frame. Its one owner is `OllamaContinuer`, the base model on this host.
|
||||
pub trait Continuer {
|
||||
/// `n` raw continuations of `prompt`, no chat template.
|
||||
fn continue_raw(&self, prompt: &str, sampling: &Sampling, n: usize) -> Result<Vec<String>>;
|
||||
}
|
||||
|
||||
/// The base model on Ollama on this host. Loopback only, as Ghostlight's local binding is.
|
||||
pub struct OllamaContinuer {
|
||||
base: String,
|
||||
base_model: String,
|
||||
agent: ureq::Agent,
|
||||
}
|
||||
|
||||
impl OllamaContinuer {
|
||||
/// `endpoint` is `http://<loopback host>:<port>`; any other address is refused here.
|
||||
pub fn open(endpoint: &str, base_model: &str) -> Result<Self> {
|
||||
// Parsed as the HTTP client parses it, so the host checked is the host connected to.
|
||||
let uri: ureq::http::Uri = endpoint.parse().map_err(|_| anyhow!("the stream endpoint is not a URL"))?;
|
||||
let authority = uri.authority().filter(|_| uri.scheme_str() == Some("http")).ok_or_else(|| anyhow!("the stream endpoint must be http://<loopback>:<port>"))?;
|
||||
if authority.as_str().contains('@') {
|
||||
bail!("the stream endpoint carries userinfo");
|
||||
}
|
||||
if uri.path() != "/" || uri.query().is_some() {
|
||||
bail!("the stream endpoint is an origin, not a path");
|
||||
}
|
||||
let host = authority.host().trim_start_matches('[').trim_end_matches(']');
|
||||
let loopback = host.eq_ignore_ascii_case("localhost") || host.parse::<IpAddr>().is_ok_and(|ip| ip.is_loopback());
|
||||
if !loopback {
|
||||
bail!("the stream endpoint is not a loopback address");
|
||||
}
|
||||
let agent: ureq::Agent = ureq::Agent::config_builder()
|
||||
.timeout_global(Some(Duration::from_secs(900)))
|
||||
.timeout_connect(Some(Duration::from_secs(3)))
|
||||
.http_status_as_error(false)
|
||||
// The host checked above is the host connected to: no proxy from the environment, no redirect to elsewhere.
|
||||
.proxy(None)
|
||||
.max_redirects(0)
|
||||
.build()
|
||||
.into();
|
||||
Ok(Self { base: format!("http://{authority}"), base_model: base_model.to_owned(), agent })
|
||||
}
|
||||
|
||||
fn post(&self, path: &str, body: &Value) -> Result<Value> {
|
||||
let url = format!("{}{path}", self.base);
|
||||
let mut response = self.agent.post(&url).header("content-type", "application/json").send(body.to_string()).with_context(|| format!("no answer from {url}"))?;
|
||||
let status = response.status().as_u16();
|
||||
let text = response.body_mut().read_to_string().with_context(|| format!("reading the answer from {url}"))?;
|
||||
if status != 200 {
|
||||
bail!("the model host answered {status} to {path}: {}", text.chars().take(300).collect::<String>());
|
||||
}
|
||||
serde_json::from_str(&text).with_context(|| format!("the answer to {path} is not JSON"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Continuer for OllamaContinuer {
|
||||
fn continue_raw(&self, prompt: &str, sampling: &Sampling, n: usize) -> Result<Vec<String>> {
|
||||
let body = json!({
|
||||
"model": self.base_model,
|
||||
"prompt": prompt,
|
||||
"raw": true,
|
||||
"stream": false,
|
||||
"options": {
|
||||
"temperature": f64::from(sampling.temperature_permille) / 1000.0,
|
||||
"min_p": f64::from(sampling.min_p_permille) / 1000.0,
|
||||
"num_predict": sampling.max_tokens,
|
||||
"stop": ["\n\n"],
|
||||
"num_thread": NUM_THREAD,
|
||||
},
|
||||
});
|
||||
(0..n)
|
||||
.map(|_| self.post("/api/generate", &body)?["response"].as_str().map(str::to_owned).ok_or_else(|| anyhow!("a continuation's answer has no response")))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// The Projector: her own caller on the connector.
|
||||
|
||||
/// What the connector's request asks of the model beside the user text. It names no membrane text: that is Ghostlight's.
|
||||
const PROJECTOR_INSTRUCTIONS: &str = "Write only the text the request asks for.";
|
||||
/// Room for the model's adaptive thinking; the projection itself is at most `PROJECTOR_WORDS` words.
|
||||
const PROJECTOR_MAX_OUTPUT_TOKENS: u32 = 1024;
|
||||
/// The sealed frame the client will carry: the prompt out and the projection back fit with wide room.
|
||||
const CONNECTOR_FRAME_BYTES: usize = 256 * 1024;
|
||||
/// How long an invocation stays admissible at the connector.
|
||||
const INVOCATION_LIFE_MS: u64 = 60_000;
|
||||
|
||||
/// The identity of one projection on the connector. A live tick's is derived from the instance and the tick label alone; a
|
||||
/// dry run's also carries the moment, because the kit's replay store answers a repeated id byte-identically.
|
||||
fn request_id(instance: &Slug, label: &str, mode: Mode, now: DateTime<Utc>) -> String {
|
||||
match mode {
|
||||
Mode::Live => format!("{AGENT}:{}:{label}", instance.0),
|
||||
Mode::DryRun => format!("{AGENT}:{}:{label}:dry{}", instance.0, now.timestamp_millis()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The connector key: the credential file in systemd's credentials directory, read once and trimmed. Its error names the
|
||||
/// credential and never a byte of the file.
|
||||
pub fn connector_key(credentials: &std::path::Path) -> Result<String> {
|
||||
let key = std::fs::read_to_string(credentials.join("connector-key")).map_err(|_| anyhow!("the connector-key credential could not be read"))?;
|
||||
Ok(key.trim().to_owned())
|
||||
}
|
||||
|
||||
/// The Projector as one tool-less connector turn by `caller` (her instance) on `model`. Loopback only, as the kit is.
|
||||
/// Each projection is its own conversation: nothing is carried from one tick to the next.
|
||||
pub struct KitProjector {
|
||||
client: ConnectorClient,
|
||||
caller: String,
|
||||
model: String,
|
||||
}
|
||||
|
||||
impl KitProjector {
|
||||
/// `endpoint` is the kit's loopback address; `connection_key` is the caller's key, read by the binary and never printed.
|
||||
pub fn open(endpoint: SocketAddr, connection_key: String, caller: &str, model: &str, timeout: Duration) -> Result<Self> {
|
||||
if !endpoint.ip().is_loopback() {
|
||||
bail!("the connector endpoint is not a loopback address");
|
||||
}
|
||||
let client = ConnectorClient::new(endpoint, connection_key, CONNECTOR_FRAME_BYTES, Some(timeout)).map_err(|_| anyhow!("the connector client could not be configured"))?;
|
||||
Ok(Self { client, caller: caller.to_owned(), model: model.to_owned() })
|
||||
}
|
||||
}
|
||||
|
||||
impl Projector for KitProjector {
|
||||
fn model(&self) -> &str {
|
||||
&self.model
|
||||
}
|
||||
|
||||
fn project(&self, request_id: &str, prompt: &str) -> Result<String> {
|
||||
let mut request = ProviderRequest::new(request_id, request_id, &self.model, PROJECTOR_INSTRUCTIONS, PROJECTOR_MAX_OUTPUT_TOKENS);
|
||||
request.input = vec![InputItem::UserText { text: prompt.to_owned() }];
|
||||
request.reasoning_effort = Some(ReasoningEffort::Low);
|
||||
let expires = u64::try_from(Utc::now().timestamp_millis()).unwrap_or_default() + INVOCATION_LIFE_MS;
|
||||
let invocation = TransportInvocation::new(&self.caller, expires, Sha256::digest(prompt.as_bytes()).into(), request).map_err(|_| anyhow!("the Projector's request is not a valid connector request"))?;
|
||||
let result = self.client.execute(&invocation).map_err(|fault| anyhow!("the connector did not answer the Projector: {fault}"))?;
|
||||
match result.disposition {
|
||||
TransportDisposition::Refused(reason) => bail!("the connector refused the Projector: {reason:?}"),
|
||||
TransportDisposition::Transported { events, receipt } => match receipt.outcome {
|
||||
TransportOutcome::Completed { stop_reason: StopReason::EndTurn, .. } => Ok(events
|
||||
.into_iter()
|
||||
.filter_map(|event| match event.payload {
|
||||
TransportEventPayload::TextDelta { text } => Some(text),
|
||||
_ => None,
|
||||
})
|
||||
.collect()),
|
||||
TransportOutcome::Completed { stop_reason, .. } => bail!("the Projector's turn ended early: {stop_reason:?}"),
|
||||
TransportOutcome::Failed { kind, .. } => bail!("the provider failed the Projector: {kind:?}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Her persona state, read and never put.
|
||||
|
||||
pub struct PersonaRead {
|
||||
pub state: Value,
|
||||
pub receipt_id: String,
|
||||
}
|
||||
|
||||
pub trait PersonaSource {
|
||||
fn persona(&self) -> Result<PersonaRead>;
|
||||
}
|
||||
|
||||
/// Her persona state from the daemon, as the Persona organ reads it.
|
||||
pub struct HuginnPersonaSource {
|
||||
client: HuginnClient,
|
||||
}
|
||||
|
||||
impl HuginnPersonaSource {
|
||||
pub fn new(client: HuginnClient) -> Self {
|
||||
Self { client }
|
||||
}
|
||||
}
|
||||
|
||||
impl PersonaSource for HuginnPersonaSource {
|
||||
fn persona(&self) -> Result<PersonaRead> {
|
||||
match self.client.persona_get().map_err(|_| anyhow!("the mind could not be asked for the persona"))? {
|
||||
HuginnMindResponse::Persona(Some(view)) => Ok(PersonaRead { state: view.value, receipt_id: view.receipt_id }),
|
||||
HuginnMindResponse::Persona(None) => bail!("the mind holds no persona state"),
|
||||
_ => bail!("the mind answered the persona request with something else"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// The seed.
|
||||
|
||||
/// What the Projector is shown, and the documents it came from.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct Seed {
|
||||
pub identity: String,
|
||||
pub context: String,
|
||||
pub stimulus: String,
|
||||
/// Whether anything new was admitted since her newest thought (the unbidden memory and the
|
||||
/// nothing-new line are not news).
|
||||
pub news: bool,
|
||||
pub reads: Vec<PipelineRef>,
|
||||
}
|
||||
|
||||
impl Seed {
|
||||
fn projector_prompt(&self) -> String {
|
||||
build_projector_prompt(&ProjectorPrompt {
|
||||
identity: &self.identity,
|
||||
typed_context: &self.context,
|
||||
visible_stimulus: &self.stimulus,
|
||||
domain_guidance: STREAM_GUIDANCE,
|
||||
word_budget: PROJECTOR_WORDS,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn cap(text: &str, max: usize) -> String {
|
||||
if text.len() <= max {
|
||||
return text.to_owned();
|
||||
}
|
||||
let mut end = max;
|
||||
while !text.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
let cut = &text[..end];
|
||||
match cut.rfind('\n') {
|
||||
Some(at) if at > 0 => cut[..at].to_owned(),
|
||||
_ => cut.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn text_of<'a>(value: &'a Value, path: &[&str]) -> &'a str {
|
||||
path.iter().try_fold(value, |at, key| at.get(*key)).and_then(Value::as_str).unwrap_or_default().trim()
|
||||
}
|
||||
|
||||
fn items<'a>(value: &'a Value, path: &[&str]) -> &'a [Value] {
|
||||
path.iter().try_fold(value, |at, key| at.get(*key)).and_then(Value::as_array).map_or(&[], Vec::as_slice)
|
||||
}
|
||||
|
||||
fn identity_of(state: &Value) -> String {
|
||||
let mut parts = vec![text_of(state, &["publicName"]).to_owned()];
|
||||
for text in [text_of(state, &["publicDescription"]), text_of(state, &["presentation", "pronouns"]), text_of(state, &["presentation", "voiceSummary"])] {
|
||||
if !text.is_empty() {
|
||||
parts.push(text.to_owned());
|
||||
}
|
||||
}
|
||||
cap(&parts.join(". "), LINE_MAX_BYTES)
|
||||
}
|
||||
|
||||
fn values_section(state: &Value) -> String {
|
||||
let mut values: Vec<&Value> = items(state, &["values"]).iter().collect();
|
||||
values.sort_by(|a, b| b["priority"].as_f64().unwrap_or(0.0).total_cmp(&a["priority"].as_f64().unwrap_or(0.0)));
|
||||
let lines: Vec<String> = values.iter().map(|value| format!("- {}: {}", text_of(value, &["label"]), text_of(value, &["summary"]))).collect();
|
||||
section("What she values:", lines)
|
||||
}
|
||||
|
||||
fn pressures_section(state: &Value) -> String {
|
||||
let lines: Vec<String> = items(state, &["agencyPressure", "pressures"]).iter().map(|pressure| format!("- {}", text_of(pressure, &["summary"]))).collect();
|
||||
section("What presses on her:", lines)
|
||||
}
|
||||
|
||||
fn bond_line(bond: &Value) -> String {
|
||||
let who = [text_of(bond, &["object", "label"]), text_of(bond, &["object", "id"])].into_iter().find(|name| !name.is_empty()).unwrap_or("someone");
|
||||
format!("- {who} ({}): {}", text_of(bond, &["relationshipKind"]), text_of(bond, &["summary"]))
|
||||
}
|
||||
|
||||
fn bonds_section(state: &Value) -> String {
|
||||
section("Who she is bound to:", items(state, &["affect", "socialBonds"]).iter().map(bond_line).collect())
|
||||
}
|
||||
|
||||
fn section(heading: &str, lines: Vec<String>) -> String {
|
||||
if lines.is_empty() { String::new() } else { format!("{heading}\n{}", lines.join("\n")) }
|
||||
}
|
||||
|
||||
/// The people her bonds name, by the ids her memories say they are `about`.
|
||||
fn bonded_people(state: &Value) -> Vec<String> {
|
||||
items(state, &["affect", "socialBonds"])
|
||||
.iter()
|
||||
.filter(|bond| text_of(bond, &["object", "kind"]) == "person")
|
||||
.map(|bond| text_of(bond, &["object", "id"]).to_owned())
|
||||
.filter(|id| !id.is_empty())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn documents(mind: &dyn MindPort, mut selection: cultnet_rs::Selection, limit: u32) -> Result<Vec<PipelineDocumentView>> {
|
||||
selection.projection = "document".into();
|
||||
selection.descending = true;
|
||||
selection.limit = Some(limit);
|
||||
match mind.query(&selection)?.items {
|
||||
PipelinePageItems::Documents(views) => Ok(views),
|
||||
PipelinePageItems::Headers(_) => bail!("the mind answered headers to a document selection"),
|
||||
}
|
||||
}
|
||||
|
||||
fn admitted_at(view: &PipelineDocumentView) -> Option<DateTime<Utc>> {
|
||||
DateTime::parse_from_rfc3339(&view.admission.admitted_at).ok().map(|at| at.with_timezone(&Utc))
|
||||
}
|
||||
|
||||
fn mine(kind: PipelineKind, instance: &Slug) -> cultnet_rs::Selection {
|
||||
any_of(of_kind(kind), "root", &[&instance.0])
|
||||
}
|
||||
|
||||
/// Her Self's newest replies, whether or not her Persona has heard them.
|
||||
fn replies(mind: &dyn MindPort, instance: &Slug) -> Result<Vec<(PipelineRef, String)>> {
|
||||
let mut found: Vec<_> = documents(mind, mine(PipelineKind::Reply, instance), REPLIES_SHOWN)?
|
||||
.into_iter()
|
||||
.filter_map(|view| match view.document {
|
||||
PipelineDocument::Reply(reply) => Some((view.admission.ordinal, view.id, reply.text.0)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
found.sort_by_key(|(ordinal, ..)| std::cmp::Reverse(*ordinal));
|
||||
Ok(found.into_iter().take(REPLIES_SHOWN as usize).map(|(_, id, text)| (id, text)).collect())
|
||||
}
|
||||
|
||||
/// What her mind admitted since `since`, newest first: one line and one citation each.
|
||||
fn mind_news(mind: &dyn MindPort, instance: &Slug, since: Option<DateTime<Utc>>) -> Result<Vec<(String, Option<PipelineRef>)>> {
|
||||
let after = |view: &PipelineDocumentView| since.is_none_or(|since| admitted_at(view).is_some_and(|at| at > since));
|
||||
let mut found: Vec<(u64, String, PipelineRef)> = Vec::new();
|
||||
for view in documents(mind, in_force(mine(PipelineKind::Memory, instance)), RECENT_READ)?.into_iter().filter(&after) {
|
||||
if let PipelineDocument::Memory(memory) = &view.document {
|
||||
found.push((view.admission.ordinal, format!("- You noted ({:?}): {}", memory.sort, quoted(&memory.text.0)), view.id.clone()));
|
||||
}
|
||||
}
|
||||
for view in documents(mind, mine(PipelineKind::Reply, instance), RECENT_READ)?.into_iter().filter(&after) {
|
||||
if let PipelineDocument::Reply(reply) = &view.document {
|
||||
found.push((view.admission.ordinal, format!("- Your Self said: {}", quoted(&reply.text.0)), view.id.clone()));
|
||||
}
|
||||
}
|
||||
for view in documents(mind, mine(PipelineKind::Resolution, instance), RECENT_READ)?.into_iter().filter(&after) {
|
||||
if let PipelineDocument::Resolution(resolution) = &view.document
|
||||
&& resolution.subject.kind == PipelineKind::Run
|
||||
{
|
||||
let outcome: &ResolutionOutcome = &resolution.outcome;
|
||||
found.push((view.admission.ordinal, format!("- Run {} closed: {}", resolution.subject.id.0, outcome_words(outcome)), view.id.clone()));
|
||||
}
|
||||
}
|
||||
found.sort_by_key(|(ordinal, ..)| std::cmp::Reverse(*ordinal));
|
||||
Ok(found.into_iter().map(|(_, line, id)| (line, Some(id))).collect())
|
||||
}
|
||||
|
||||
/// Her stewarded repos' activity by others is the forge's word, not a document; a daemon with no forge adds nothing.
|
||||
/// Only what happened after `since` is news, and the notice is stamped with when it happened.
|
||||
fn repo_news(mind: &dyn MindPort, since: Option<DateTime<Utc>>, responsibilities: &Responsibilities, now: DateTime<Utc>) -> Option<(String, Option<PipelineRef>)> {
|
||||
if responsibilities.repos.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut activity = mind.stewarded_activity(&responsibilities.repos, None).ok()?;
|
||||
if let Some(since) = since {
|
||||
let since_ms = u64::try_from(since.timestamp_millis()).unwrap_or(0);
|
||||
// A capped repo's unread entries are older than the oldest it showed. Once the read shows an event of the repo at or before
|
||||
// `since` they are older than `since` too; until then the cap is disclosed, whatever other repos showed.
|
||||
let decided: Vec<String> = activity.events.iter().filter(|event| event.at <= since_ms).map(|event| event.repo.identity()).collect();
|
||||
activity.capped.retain(|repo| !decided.contains(&repo.identity()));
|
||||
activity.events.retain(|event| event.at > since_ms);
|
||||
}
|
||||
let happened = activity.events.iter().map(|event| event.at).max().unwrap_or_else(|| u64::try_from(now.timestamp_millis()).unwrap_or(0));
|
||||
repo_notice_line(&activity, happened).map(|line| (format!("- {line}"), None))
|
||||
}
|
||||
|
||||
/// One in-force memory of hers older than `UNBIDDEN_AFTER_DAYS`, picked uniformly by `pick`.
|
||||
fn unbidden(mind: &dyn MindPort, instance: &Slug, now: DateTime<Utc>, pick: u64) -> Result<Option<(String, PipelineRef)>> {
|
||||
let mut old: Vec<_> = headers(mind, &in_force(mine(PipelineKind::Memory, instance)))?
|
||||
.into_iter()
|
||||
.filter(|header| DateTime::parse_from_rfc3339(&header.admission.admitted_at).is_ok_and(|at| now - at.with_timezone(&Utc) > Span::days(UNBIDDEN_AFTER_DAYS)))
|
||||
.map(|header| (header.admission.ordinal, header.id))
|
||||
.collect();
|
||||
if old.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
old.sort_by_key(|(ordinal, _)| *ordinal);
|
||||
let (_, id) = old.swap_remove(usize::try_from(pick % old.len() as u64).unwrap_or(0));
|
||||
let one = cultnet_rs::Selection { keys: Some(vec![id.id.0.clone()]), projection: "document".into(), ..of_kind(PipelineKind::Memory) };
|
||||
let PipelinePageItems::Documents(views) = mind.query(&one)?.items else { bail!("the mind answered headers to a document selection") };
|
||||
Ok(views.into_iter().find_map(|view| match view.document {
|
||||
PipelineDocument::Memory(memory) => Some((format!("Out of nowhere you recall: {}", quoted(&memory.text.0)), id.clone())),
|
||||
_ => None,
|
||||
}))
|
||||
}
|
||||
|
||||
/// The seed, rebuilt from her mind and nothing else. `since` is her newest thought's label tick time (never later than its admission) and `fresh` what `news` found.
|
||||
fn build_seed(mind: &dyn MindPort, persona: &PersonaRead, since: Option<DateTime<Utc>>, now: DateTime<Utc>, responsibilities: &Responsibilities, fresh: Vec<(String, Option<PipelineRef>)>) -> Result<Seed> {
|
||||
let instance = mind.instance().clone();
|
||||
let state = &persona.state;
|
||||
let mut reads: Vec<PipelineRef> = Vec::new();
|
||||
let mut cite = |id: PipelineRef| {
|
||||
if !reads.contains(&id) {
|
||||
reads.push(id);
|
||||
}
|
||||
};
|
||||
|
||||
let runs = recent_runs(mind, &instance)?;
|
||||
let people = bonded_people(state);
|
||||
let cue = Cue { senders: people.iter().map(String::as_str).collect(), texts: Vec::new(), names: BTreeMap::new(), listed: &responsibilities.listed, now };
|
||||
let recalled = recall::select(mind, &cue)?;
|
||||
let heard = replies(mind, &instance)?;
|
||||
let heard_section = section("What your Self said lately:", heard.iter().map(|(_, text)| format!("- {}", quoted(text))).collect());
|
||||
|
||||
let sections = [
|
||||
values_section(state),
|
||||
pressures_section(state),
|
||||
bonds_section(state),
|
||||
section("Your latest runs:", runs.iter().map(|run| format!("- {run}")).collect()),
|
||||
responsibilities.text.clone(),
|
||||
recall::render(&recalled),
|
||||
heard_section,
|
||||
];
|
||||
let kept: Vec<String> = sections.iter().zip(BUDGETS).map(|(text, (_, budget))| cap(text, budget)).collect();
|
||||
// The reads are what the Projector was shown after the bounds: the recall lines and replies the capped sections kept.
|
||||
// Each shown line is one citation: a memory reading like another takes its own line, not a share of the first's.
|
||||
let mut shown: Vec<&str> = kept[5].lines().collect();
|
||||
for entry in recalled.entries() {
|
||||
if let Some(at) = shown.iter().position(|line| *line == entry.line.trim_end()) {
|
||||
shown.remove(at);
|
||||
cite(PipelineRef { kind: PipelineKind::Memory, id: Short(entry.id.clone()) });
|
||||
}
|
||||
}
|
||||
let replies_shown = kept[6].lines().count().saturating_sub(1);
|
||||
heard.iter().take(replies_shown).for_each(|(id, _)| cite(id.clone()));
|
||||
let context = kept.iter().filter(|text| !text.trim().is_empty()).cloned().collect::<Vec<_>>().join("\n");
|
||||
|
||||
let has_news = !fresh.is_empty();
|
||||
let stimulus = if has_news {
|
||||
let body: Vec<&str> = fresh.iter().map(|(line, _)| line.as_str()).collect();
|
||||
let kept = cap(&format!("Since your last thought:\n{}", body.join("\n")), SECTION_MAX_BYTES);
|
||||
// Each line is one line (`quoted` escapes newlines), so the lines the bound kept are what the thought read.
|
||||
let shown = kept.lines().count().saturating_sub(1);
|
||||
fresh.into_iter().take(shown).filter_map(|(_, id)| id).for_each(&mut cite);
|
||||
kept
|
||||
} else if let Some((line, id)) = unbidden(mind, &instance, now, u64::try_from(now.timestamp()).unwrap_or(0))? {
|
||||
cite(id);
|
||||
line
|
||||
} else {
|
||||
let at = since.map_or_else(|| "you began".to_owned(), |at| at.format("%-d %B %H:%M UTC").to_string());
|
||||
format!("Nothing has happened since {at}.")
|
||||
};
|
||||
reads.truncate(READS_MAX);
|
||||
Ok(Seed { identity: identity_of(state), context: cap(&context, SECTION_MAX_BYTES), stimulus: cap(&stimulus, SECTION_MAX_BYTES), news: has_news, reads })
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// Continuation, selection and the tick.
|
||||
|
||||
/// The frame the base model continues: her notebook heading, the date, the projection verbatim
|
||||
/// and an open paragraph. It instructs nothing.
|
||||
fn continuer_frame(name: &str, date: &str, projection: &str) -> String {
|
||||
format!("{name}'s notebook\n\n{date}\n\n{projection}\n\n")
|
||||
}
|
||||
|
||||
/// The fixed parts of both prompts, recorded in every thought: the frame is the function's own output over placeholders.
|
||||
fn prompt_version() -> String {
|
||||
format!(
|
||||
"ghostlight={GHOSTLIGHT_REV}; words={PROJECTOR_WORDS}; guidance={STREAM_GUIDANCE:?}; frame={:?}",
|
||||
continuer_frame("<name>", &format!("<date {FRAME_DATE_FORMAT}>"), "<projection>")
|
||||
)
|
||||
}
|
||||
|
||||
/// Each candidate's nearness to her earlier thoughts, in thousandths of the daemon's top score;
|
||||
/// 1000 where she has none.
|
||||
fn nearness(mind: &dyn MindPort, candidates: &[String]) -> Result<Vec<u16>> {
|
||||
let members = in_force(mine(PipelineKind::Thought, mind.instance()));
|
||||
let any = mind.query(&cultnet_rs::Selection { limit: Some(1), ..members.clone() })?;
|
||||
if any.matched == 0 {
|
||||
return Ok(vec![THOUGHT_NEAREST_NONE_PERMILLE; candidates.len()]);
|
||||
}
|
||||
candidates
|
||||
.iter()
|
||||
.map(|text| {
|
||||
let page = mind.query_near(&members, text, 1)?;
|
||||
let top = page.scores.as_ref().and_then(|scores| scores.first().copied()).ok_or_else(|| anyhow!("the index scored no earlier thought"))?;
|
||||
Ok((f64::from(top) * 1000.0).round().clamp(0.0, 1000.0) as u16)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The least near candidate, the first on a tie.
|
||||
fn least_near(nearest: &[u16]) -> usize {
|
||||
nearest.iter().enumerate().min_by_key(|(at, near)| (**near, *at)).map_or(0, |(at, _)| at)
|
||||
}
|
||||
|
||||
/// How a tick ended.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Outcome {
|
||||
/// A brake is held: nothing was read, asked or admitted.
|
||||
Held(crate::control::HeldReason),
|
||||
/// Not due by the cadence.
|
||||
NotDue,
|
||||
/// Every continuation was blank; nothing was admitted. The loop waits `STREAM_MIN_GAP_S`.
|
||||
Blank,
|
||||
/// The thought; admitted unless the tick was a dry run.
|
||||
Thought { thought: Box<PipelineThought>, admitted: bool },
|
||||
}
|
||||
|
||||
/// Whether a tick is a real one or the operator's look at what one would say.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Mode {
|
||||
Live,
|
||||
/// Both models run and nothing is admitted; the cadence does not hold it back.
|
||||
DryRun,
|
||||
}
|
||||
|
||||
pub struct Organs<'a> {
|
||||
pub mind: &'a dyn MindPort,
|
||||
pub persona: &'a dyn PersonaSource,
|
||||
pub projector: &'a dyn Projector,
|
||||
pub continuer: &'a dyn Continuer,
|
||||
pub base_model: &'a str,
|
||||
}
|
||||
|
||||
/// Whether a tick is due: `STREAM_MIN_GAP_S` has passed and there is news, or `STREAM_IDLE_GAP_S` has.
|
||||
fn due(since: Option<DateTime<Utc>>, now: DateTime<Utc>, news: bool) -> bool {
|
||||
match since {
|
||||
None => true,
|
||||
Some(at) => {
|
||||
let gap = (now - at).num_seconds();
|
||||
gap >= STREAM_IDLE_GAP_S || (gap >= STREAM_MIN_GAP_S && news)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// When her newest thought was ticked: its label's instant, never later than its admission. The models take minutes between the
|
||||
/// two, and what the mind or the forge admitted meanwhile is news to the next thought, which the admission time would hide.
|
||||
fn newest_thought(mind: &dyn MindPort) -> Result<Option<DateTime<Utc>>> {
|
||||
let newest = cultnet_rs::Selection { descending: true, limit: Some(1), ..mine(PipelineKind::Thought, mind.instance()) };
|
||||
let PipelinePageItems::Headers(found) = mind.query(&newest)?.items else { bail!("the mind answered documents to a header selection") };
|
||||
Ok(found
|
||||
.iter()
|
||||
.filter_map(|header| {
|
||||
let admitted = DateTime::parse_from_rfc3339(&header.admission.admitted_at).ok()?.with_timezone(&Utc);
|
||||
let label = header.id.id.0.rsplit(':').next().and_then(|label| label.strip_prefix('t')).and_then(|label| label.parse::<i64>().ok()).and_then(|label| DateTime::from_timestamp(label, 0));
|
||||
Some(label.map_or(admitted, |at| at.min(admitted)))
|
||||
})
|
||||
.max())
|
||||
}
|
||||
|
||||
fn bounded(text: &str, max: usize) -> String {
|
||||
cap(text.trim(), max)
|
||||
}
|
||||
|
||||
/// One tick. A fault is an `Err`; the caller prints it and the process goes on.
|
||||
pub fn tick(organs: &Organs, brake: &dyn Fn() -> Effective, mode: Mode, now: DateTime<Utc>) -> Result<Outcome> {
|
||||
if let Effective::Held { reason } = brake() {
|
||||
return Ok(Outcome::Held(reason));
|
||||
}
|
||||
let mind = organs.mind;
|
||||
let since = newest_thought(mind)?;
|
||||
if mode == Mode::Live && since.is_some_and(|at| (now - at).num_seconds() < STREAM_MIN_GAP_S) {
|
||||
return Ok(Outcome::NotDue);
|
||||
}
|
||||
// Due-ness is decided on the cheap reads; the seed (recall, runs, replies) is built only for a tick that may run.
|
||||
let responsibilities = render_responsibilities(mind, mind.instance(), &BTreeMap::new())?;
|
||||
let mut fresh = mind_news(mind, mind.instance(), since)?;
|
||||
// The forge is read when a tick will run anyway, and on a quiet stretch only by the poll that crosses each minimum-gap mark.
|
||||
let quiet = mode == Mode::Live && !due(since, now, !fresh.is_empty());
|
||||
if quiet && since.is_some_and(|at| (now - at).num_seconds() % STREAM_MIN_GAP_S >= STREAM_POLL_S) {
|
||||
return Ok(Outcome::NotDue);
|
||||
}
|
||||
fresh.extend(repo_news(mind, since, &responsibilities, now));
|
||||
if quiet && fresh.is_empty() {
|
||||
return Ok(Outcome::NotDue);
|
||||
}
|
||||
let persona = organs.persona.persona()?;
|
||||
let seed = build_seed(mind, &persona, since, now, &responsibilities, fresh)?;
|
||||
|
||||
let label = format!("t{}", now.timestamp());
|
||||
let began = std::time::Instant::now();
|
||||
let projection = bounded(&organs.projector.project(&request_id(mind.instance(), &label, mode, now), &seed.projector_prompt())?, PARA_MAX_BYTES);
|
||||
let projector_ms = u32::try_from(began.elapsed().as_millis()).unwrap_or(u32::MAX);
|
||||
if projection.is_empty() {
|
||||
return Ok(Outcome::Blank);
|
||||
}
|
||||
let frame = continuer_frame(text_of(&persona.state, &["publicName"]), &now.format(FRAME_DATE_FORMAT).to_string(), &projection);
|
||||
let began = std::time::Instant::now();
|
||||
let candidates: Vec<String> = organs
|
||||
.continuer
|
||||
.continue_raw(&frame, &Sampling::STREAM, CONTINUATIONS)?
|
||||
.iter()
|
||||
.map(|text| bounded(text, LINE_MAX_BYTES))
|
||||
.filter(|text| !text.is_empty())
|
||||
.take(THOUGHT_CANDIDATES_MAX)
|
||||
.collect();
|
||||
let continuer_ms = u32::try_from(began.elapsed().as_millis()).unwrap_or(u32::MAX);
|
||||
if candidates.is_empty() {
|
||||
return Ok(Outcome::Blank);
|
||||
}
|
||||
let nearest_permille = nearness(mind, &candidates)?;
|
||||
let chosen = least_near(&nearest_permille);
|
||||
let thought = PipelineThought {
|
||||
instance: mind.instance().clone(),
|
||||
label: Label::from(label.as_str()),
|
||||
text: Para::from(candidates[chosen].as_str()),
|
||||
reads: seed.reads.clone(),
|
||||
persona_receipt: Short(persona.receipt_id.clone()),
|
||||
identity: Line::from(seed.identity.as_str()),
|
||||
context: Para::from(seed.context.as_str()),
|
||||
stimulus: Para::from(seed.stimulus.as_str()),
|
||||
projector_model: Short(organs.projector.model().to_owned()),
|
||||
projection: Para::from(projection.as_str()),
|
||||
base_model: Short(organs.base_model.to_owned()),
|
||||
temperature_permille: Sampling::STREAM.temperature_permille,
|
||||
min_p_permille: Sampling::STREAM.min_p_permille,
|
||||
max_tokens: Sampling::STREAM.max_tokens,
|
||||
candidates: candidates.iter().map(|text| Line::from(text.as_str())).collect(),
|
||||
nearest_permille,
|
||||
chosen: u8::try_from(chosen).unwrap_or(0),
|
||||
projector_ms,
|
||||
continuer_ms,
|
||||
prompt_version: Line::from(prompt_version().as_str()),
|
||||
};
|
||||
if mode == Mode::DryRun {
|
||||
return Ok(Outcome::Thought { thought: Box::new(thought), admitted: false });
|
||||
}
|
||||
match mind.admit(AGENT, &format!("stream-{label}"), vec![PipelineDocument::Thought(thought.clone())])? {
|
||||
PipelineAdmissionOutcome::Committed { .. } => Ok(Outcome::Thought { thought: Box::new(thought), admitted: true }),
|
||||
other => Err(anyhow!("the mind did not admit the thought: {other:?}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,12 +13,12 @@ use model_connector::PlanQuota;
|
|||
use chrono::{DateTime, Duration, TimeZone, Utc};
|
||||
use cultnet_rs::Selection;
|
||||
use eureka_pipeline::{
|
||||
CommitRange, CutVerification, Date, DocRef, MemoryOrigin, MemorySort, PipelineCampaign, PipelineCutReport, PipelineCutSpec, PipelineDocument, PipelineFollowUp,
|
||||
CutVerification, Date, DocRef, MemoryOrigin, MemorySort, PipelineCampaign, PipelineCutSpec, PipelineDocument, PipelineFollowUp,
|
||||
PipelineInstance, PipelineKind, PipelineMemory, PipelineQuestion, PipelineRef, PipelineResolution, PipelineRuling, PipelineRun, PipelineStewardship,
|
||||
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;
|
||||
|
|
@ -59,20 +59,12 @@ pub(crate) struct Recording {
|
|||
}
|
||||
|
||||
impl Recording {
|
||||
pub(crate) fn kill(&self, turn: RunTurn) {
|
||||
self.running.borrow_mut().retain(|unit| *unit != (Slug(INSTANCE.into()), turn.slot()));
|
||||
}
|
||||
|
||||
/// `kill` for a named instance.
|
||||
/// Her unit of `turn`'s slot ends.
|
||||
pub(crate) fn kill_as(&self, instance: &Slug, turn: RunTurn) {
|
||||
self.running.borrow_mut().retain(|unit| *unit != (instance.clone(), turn.slot()));
|
||||
}
|
||||
|
||||
pub(crate) fn revive(&self, turn: RunTurn) {
|
||||
self.revive_as(&Slug(INSTANCE.into()), turn);
|
||||
}
|
||||
|
||||
/// `revive` for a named instance.
|
||||
/// Her unit of `turn`'s slot is active.
|
||||
pub(crate) fn revive_as(&self, instance: &Slug, turn: RunTurn) {
|
||||
self.running.borrow_mut().push((instance.clone(), turn.slot()));
|
||||
}
|
||||
|
|
@ -116,15 +108,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 +138,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:?}");
|
||||
|
|
@ -258,26 +221,6 @@ impl MindPort for TestMind {
|
|||
}
|
||||
}
|
||||
|
||||
/// A mind whose admissions answer as told, over a real mind's reads.
|
||||
pub(crate) struct Answering<'a> {
|
||||
pub(crate) inner: &'a TestMind,
|
||||
pub(crate) outcome: PipelineAdmissionOutcome,
|
||||
}
|
||||
|
||||
impl MindPort for Answering<'_> {
|
||||
fn instance(&self) -> &Slug {
|
||||
self.inner.instance()
|
||||
}
|
||||
|
||||
fn query(&self, selection: &Selection) -> Result<PipelineSelectionPage> {
|
||||
self.inner.query(selection)
|
||||
}
|
||||
|
||||
fn admit(&self, _agent: &str, _session: &str, _documents: Vec<PipelineDocument>) -> Result<PipelineAdmissionOutcome> {
|
||||
Ok(self.outcome.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// One of her memories: `sort` and `text`, about `about`, learned in #swarm, salience 500.
|
||||
pub(crate) fn memory(label: &str, sort: MemorySort, text: &str, about: &[&str]) -> PipelineDocument {
|
||||
memory_from(label, sort, text, about, MemoryOrigin::Room { rooms: "swarm".into(), heard_from: vec![] }, 500)
|
||||
|
|
@ -336,15 +279,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()),
|
||||
|
|
@ -406,26 +340,6 @@ fn delta() -> StructuralDelta {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn report(cut: &str) -> PipelineDocument {
|
||||
PipelineDocument::CutReport(PipelineCutReport {
|
||||
campaign: Slug(CAMPAIGN.into()),
|
||||
cut_spec: id("cut_spec", &format!("cut-{cut}.r1")).as_str().into(),
|
||||
attempt: 1,
|
||||
repo: REPO.into(),
|
||||
branch: "eureka-body/x".into(),
|
||||
commits: vec![eureka_pipeline::ReportCommit { sha: Sha("df7507b".into()), subject: "Land the cut".into(), builds: true }],
|
||||
range: CommitRange { base: Sha("df7507b".into()), head: Sha("df7507b".into()) },
|
||||
verification: vec![],
|
||||
mutations: vec![],
|
||||
deviations: vec![],
|
||||
forks: vec![],
|
||||
structural_delta: delta(),
|
||||
landed_names: vec![],
|
||||
undone: vec![],
|
||||
promises: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
/// A question raised in `item`, which blocks it.
|
||||
pub(crate) fn question_in(label: &str, item: &PipelineRef) -> PipelineDocument {
|
||||
PipelineDocument::Question(PipelineQuestion {
|
||||
|
|
@ -499,32 +413,6 @@ pub(crate) fn withdrawn() -> ResolutionOutcome {
|
|||
ResolutionOutcome::Withdrawn { reason: "never ran".into() }
|
||||
}
|
||||
|
||||
/// Her Self run on `item`, ended, as `n` in a history: opened, closed, and
|
||||
/// followed by a follow-up sourced from `item` when `leaves_something` (the
|
||||
/// document that cites the item, which makes the run non-empty).
|
||||
pub(crate) fn ended_run(mind: &TestMind, item: &PipelineRef, prefix: &str, n: usize, leaves_something: bool) {
|
||||
let label = format!("{prefix}{n}");
|
||||
mind.committed(vec![run(&label, RunTurn::SelfRun, RunOperator::Mind, std::slice::from_ref(item))]);
|
||||
let mut closing = vec![close(run_ref(&label), recorded())];
|
||||
if leaves_something {
|
||||
closing.push(follow_up(&format!("{prefix}left{n}"), item));
|
||||
}
|
||||
mind.committed(closing);
|
||||
}
|
||||
|
||||
/// A history of ended runs on `item`, oldest first; `true` leaves a document.
|
||||
pub(crate) fn history(mind: &TestMind, item: &PipelineRef, runs: &[bool]) {
|
||||
history_as(mind, item, "h", runs);
|
||||
}
|
||||
|
||||
/// The same, with run labels `<prefix>0`, `<prefix>1`, and so on, for a mind
|
||||
/// holding the histories of several items.
|
||||
pub(crate) fn history_as(mind: &TestMind, item: &PipelineRef, prefix: &str, runs: &[bool]) {
|
||||
for (n, leaves_something) in runs.iter().enumerate() {
|
||||
ended_run(mind, item, prefix, n, *leaves_something);
|
||||
}
|
||||
}
|
||||
|
||||
/// A follow-up sourced from `source`: work a run may claim that the queue does
|
||||
/// not list.
|
||||
pub(crate) fn follow_up(label: &str, source: &PipelineRef) -> PipelineDocument {
|
||||
|
|
|
|||
|
|
@ -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())));
|
||||
|
|
@ -44,34 +36,21 @@ fn a_unit_configured_from_its_environment_alone_reaches_the_gate_and_ends_dead()
|
|||
assert!(text(&output.stdout).starts_with("dead:"), "{}", text(&output.stdout));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_flag_overrides_the_environment() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
// The rooms flag replaces the rooms the environment names.
|
||||
let flagged = run(dir.path(), &[], &[], &["--instance", "eureka", "--rooms", "#swarm:chat"]);
|
||||
assert_eq!(flagged.status.code(), Some(0), "{}", text(&flagged.stderr));
|
||||
}
|
||||
|
||||
#[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 +62,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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,13 +9,12 @@ mind_body: mod lease_client
|
|||
mind_body: mod persona
|
||||
mind_body: mod queue
|
||||
mind_body: mod self_run
|
||||
mind_body: mod stream
|
||||
mind_body: mod tempo
|
||||
mind_body::address: impl Addressee
|
||||
mind_body::address: struct Addressee # [derive (Clone , Debug , PartialEq , Eq , serde :: Serialize , serde :: Deserialize)] pub-fields[]
|
||||
mind_body::address: struct Name # [derive (Clone , Debug , PartialEq , Eq , serde :: Serialize , serde :: Deserialize)] pub-fields[]
|
||||
mind_body::chat_client: # [derive (Clone , Copy , Debug , Default , PartialEq , Eq)] pub enum Form { # [default] Message , Notice , Edit , }
|
||||
mind_body::chat_client: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum Basis { Direct , Tag , Reply , Room , }
|
||||
mind_body::chat_client: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum Basis { Direct , Tag , Reply , FollowUp , Room , }
|
||||
mind_body::chat_client: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum DirectOutcome { Opened , Existing , Brake , PersonaMismatch , UnknownPersona , EmptyText , ForeignUser , NotAPerson , NoSuchUser , InvitePending , Declined , DailyLimit , SendFailed , }
|
||||
mind_body::chat_client: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum FetchOutcome { Stored , StoredUnverified , NotAMember , BridgedRoom , TooLarge , TypeRefused , NoAttachment , HashMismatch , RateLimited , Brake , PersonaMismatch , UnknownPersona , FetchFailed , StoreFailed , }
|
||||
mind_body::chat_client: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum ReceiptMode { Posted , Existing , }
|
||||
|
|
@ -25,6 +24,7 @@ mind_body::chat_client: # [derive (Clone , Debug , Default , PartialEq , Eq)] pu
|
|||
mind_body::chat_client: const ACK : & str
|
||||
mind_body::chat_client: const FETCH_RECEIPT : & str
|
||||
mind_body::chat_client: const FETCH_REQUEST : & str
|
||||
mind_body::chat_client: const FOLLOW_UP_BASIS : & str
|
||||
mind_body::chat_client: const OPEN_DIRECT_RECEIPT : & str
|
||||
mind_body::chat_client: const OPEN_DIRECT_REQUEST : & str
|
||||
mind_body::chat_client: const PENDING : & str
|
||||
|
|
@ -146,18 +146,16 @@ mind_body::control: struct SwarmState # [derive (Clone , Debug , PartialEq , Eq
|
|||
mind_body::control: struct SwarmWriter pub-fields[]
|
||||
mind_body::control: struct UnsafeStore # [derive (Debug)] pub-fields[path: PathBuf, mode: u32, uid: u32]
|
||||
mind_body::heartbeat: # [derive (Clone , Debug , PartialEq , Eq)] pub enum Outcome { Held (HeldReason) , Polled { launched : Vec < Slug > , } , }
|
||||
mind_body::heartbeat: const AGENT : & str
|
||||
mind_body::heartbeat: const DEFAULT_BEAT_S : u32
|
||||
mind_body::heartbeat: fn poll (organs : & Organs , config : & Config) -> Result < Outcome >
|
||||
mind_body::heartbeat: impl Config
|
||||
mind_body::heartbeat: impl Config: fn new (personas : Vec < PersonaId > , operators : Vec < String > , night : Option < Night >) -> Result < Self >
|
||||
mind_body::heartbeat: impl Config: fn new (personas : Vec < PersonaId > , operators : Vec < String > , beat_s : u32) -> Result < Self >
|
||||
mind_body::heartbeat: impl Control for FileControl
|
||||
mind_body::heartbeat: impl FileStore
|
||||
mind_body::heartbeat: impl FileStore: fn check (& self) -> Result < () >
|
||||
mind_body::heartbeat: impl FileStore: fn latest (& self) -> Result < Option < PollTrace > >
|
||||
mind_body::heartbeat: impl FileStore: fn new (path : impl Into < PathBuf >) -> Self
|
||||
mind_body::heartbeat: impl HeartbeatView for FileStore
|
||||
mind_body::heartbeat: impl Night
|
||||
mind_body::heartbeat: impl Night: fn from_settings (window : Option < & str > , minds : Option < & str >) -> Result < Option < Self > >
|
||||
mind_body::heartbeat: impl Night: fn parse (window : & str , minds : & str) -> Result < Self >
|
||||
mind_body::heartbeat: impl PersonaId
|
||||
mind_body::heartbeat: impl PersonaId: fn parse (spec : & str) -> Result < Self >
|
||||
mind_body::heartbeat: impl Row < '_ >
|
||||
|
|
@ -165,25 +163,19 @@ mind_body::heartbeat: impl Store for FileStore
|
|||
mind_body::heartbeat: impl fmt :: Display for Outcome
|
||||
mind_body::heartbeat: impl fmt :: Display for PollTrace
|
||||
mind_body::heartbeat: pub trait Control { fn envelope (& self) -> SwarmEffective ; fn effective (& self , instance : & Slug) -> Effective ; }
|
||||
mind_body::heartbeat: pub trait HeartbeatView { fn latest (& self) -> Result < Option < PollTrace > > ; fn addressing (& self) -> Result < Option < Addressing > > ; }
|
||||
mind_body::heartbeat: pub trait HeartbeatView { fn addressing (& self) -> Result < Option < Addressing > > ; }
|
||||
mind_body::heartbeat: pub trait Store { fn load (& self) -> Result < Option < HeartbeatState > > ; fn save (& self , state : & HeartbeatState) -> Result < () > ; fn record (& self , trace : & PollTrace) -> Result < () > ; fn load_addressing (& self) -> Result < Option < Addressing > > ; fn save_addressing (& self , addressing : & Addressing) -> Result < () > ; }
|
||||
mind_body::heartbeat: struct Addressing # [derive (Clone , Debug , Default , PartialEq , Eq , DatabaseEntry)] pub-fields[minds: Vec < Addressee >]
|
||||
mind_body::heartbeat: struct Config # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[personas: Vec < PersonaId >, operators: Vec < String >, night: Option < Night >]
|
||||
mind_body::heartbeat: struct Config # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[personas: Vec < PersonaId >, operators: Vec < String >, beat_s: u32]
|
||||
mind_body::heartbeat: struct FileControl pub-fields[]
|
||||
mind_body::heartbeat: struct FileStore pub-fields[]
|
||||
mind_body::heartbeat: struct HeartbeatState # [derive (Clone , Debug , PartialEq , Eq , DatabaseEntry)] pub-fields[last_beat_at: DateTime < Utc >, handled_mentions: Vec < String >]
|
||||
mind_body::heartbeat: struct HeartbeatState # [derive (Clone , Debug , PartialEq , Eq , DatabaseEntry)] pub-fields[last_beat_at: DateTime < Utc >, handled_mentions: Vec < String >, last_turn_at: BTreeMap < String , DateTime < Utc > >]
|
||||
mind_body::heartbeat: struct InstanceTrace # [derive (Clone , Debug , PartialEq , Eq , serde :: Serialize , serde :: Deserialize)] pub-fields[instance: String, vote_beat_s: Option < u32 >, urgency_permille: u16, addressed: bool, busy: bool, released: bool, idle_s: i64, score: String, note: String]
|
||||
mind_body::heartbeat: struct Night # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[from_min: u32, to_min: u32, minds: Vec < Slug >]
|
||||
mind_body::heartbeat: struct Organs< 'a > pub-fields[seats: & 'a [Seat < 'a >], control: & 'a dyn Control, launcher: & 'a dyn Launcher, clock: & 'a dyn Clock, host: & 'a str, store: & 'a dyn Store]
|
||||
mind_body::heartbeat: struct Organs< 'a > pub-fields[seats: & 'a [Seat < 'a >], control: & 'a dyn Control, launcher: & 'a dyn Launcher, clock: & 'a dyn Clock, store: & 'a dyn Store]
|
||||
mind_body::heartbeat: struct PersonaId # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[slug: Slug, public_name: String, handle: String, matrix_id: String, huginn: SocketAddr]
|
||||
mind_body::heartbeat: struct PollTrace # [derive (Clone , Debug , PartialEq , Eq , DatabaseEntry)] pub-fields[at: DateTime < Utc >, tempo_s: u32, instances: Vec < InstanceTrace >, launches: Vec < String >]
|
||||
mind_body::heartbeat: struct Seat< 'a > pub-fields[who: PersonaId, mind: & 'a dyn MindPort, chat: & 'a dyn Chat]
|
||||
mind_body::launch: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum StopScope < 'a > { Mind (& 'a Slug) , Swarm , }
|
||||
mind_body::launch: # [derive (Clone , Debug , PartialEq , Eq)] pub enum Declined { Admission (MindRefusal) , Conflict , AlreadyAdmitted , StartFailed , GrantForOtherInstance , PersonaRunTakesNoClaims , }
|
||||
mind_body::launch: # [derive (Clone , Debug , PartialEq , Eq)] pub enum LaunchOutcome { Launched { run : PipelineRef , } , NotQueued { item : PipelineRef , } , Tripped { run : PipelineRef , question : PipelineRef , } , Busy { run : PipelineRef , } , Refused (Declined) , }
|
||||
mind_body::launch: const START_GRACE_S : i64
|
||||
mind_body::launch: fn open_and_launch (ports : & Ports , request : LaunchRequest) -> Result < LaunchOutcome >
|
||||
mind_body::launch: fn run_label (now : DateTime < Utc >) -> Label
|
||||
mind_body::launch: impl Clock for SystemClock
|
||||
mind_body::launch: impl Default for SystemdLauncher
|
||||
mind_body::launch: impl Launcher for SystemdLauncher
|
||||
|
|
@ -194,8 +186,6 @@ mind_body::launch: impl SystemdLauncher: fn stop (& self , scope : StopScope < '
|
|||
mind_body::launch: pub trait Clock { fn now (& self) -> DateTime < Utc > ; }
|
||||
mind_body::launch: pub trait Launcher { fn start (& self , instance : & Slug , turn : RunTurn) -> Result < () > ; fn alive (& self , instance : & Slug , turn : RunTurn) -> bool ; }
|
||||
mind_body::launch: pub trait Systemctl { fn run (& self , args : & [& str]) -> Result < SystemctlReply > ; }
|
||||
mind_body::launch: struct LaunchRequest pub-fields[turn: RunTurn, claims: Vec < PipelineRef >, agent: String, grant: Grant]
|
||||
mind_body::launch: struct Ports< 'a > pub-fields[mind: & 'a dyn MindPort, launcher: & 'a dyn Launcher, clock: & 'a dyn Clock, host: & 'a str]
|
||||
mind_body::launch: struct SystemClock pub-fields[]
|
||||
mind_body::launch: struct SystemctlReply pub-fields[success: bool, code: Option < i32 >, stdout: String]
|
||||
mind_body::launch: struct SystemdLauncher pub-fields[]
|
||||
|
|
@ -219,68 +209,29 @@ mind_body::lease_client: struct LeaseRequest # [derive (Clone , Debug , PartialE
|
|||
mind_body::lease_client: struct SocketLender pub-fields[]
|
||||
mind_body::lease_client: struct TokenFd pub-fields[]
|
||||
mind_body::lease_client::socket: impl Lease for Granted
|
||||
mind_body::persona: # [derive (Clone , Copy , Debug , PartialEq , Eq , Serialize , Deserialize)] # [serde (rename_all = "lowercase")] pub enum SettleOutcome { Done , Declined , Wrong , }
|
||||
mind_body::persona: # [derive (Clone , Debug , PartialEq , Eq)] pub enum Failure { Held (HeldReason) , Chat , Persona , Doctrine , DoctrineTooLarge , Lease , Session , Undecodable , OutOfBounds (& 'static str) , RuleRepo (String) , RoomNotAllowed , Send , SelfRun , Memory , Ack , }
|
||||
mind_body::persona: # [derive (Debug , PartialEq , Eq)] pub enum Outcome { Dead (HeldReason) , NoRun , Served { run : PipelineRef , answered : u32 , failure : Option < Failure > , self_run : Option < PipelineRef > } , }
|
||||
mind_body::persona: # [derive (Debug , PartialEq , Eq)] pub enum Outcome { Dead (HeldReason) , NoOrgan , }
|
||||
mind_body::persona: const AGENT : & str
|
||||
mind_body::persona: const DOCTRINE_MAX_BYTES : usize
|
||||
mind_body::persona: const ENV_CONFIG_SEED : & str
|
||||
mind_body::persona: const ENV_DOCTRINE : & str
|
||||
mind_body::persona: const ENV_IMAGE_STORE : & str
|
||||
mind_body::persona: const ENV_MINDS : & str
|
||||
mind_body::persona: const ENV_MODEL : & str
|
||||
mind_body::persona: const ENV_OPERATOR : & str
|
||||
mind_body::persona: const ENV_ROOMS : & str
|
||||
mind_body::persona: const ENV_RUN_CAP : & str
|
||||
mind_body::persona: const ENV_SELF_SENDER : & str
|
||||
mind_body::persona: const PERSONA_POLL_S : i64
|
||||
mind_body::persona: const SANDBOX_DIR : & str
|
||||
mind_body::persona: fn carry (organs : & Organs , control : & impl ControlSource , swarm : & impl ControlSource , config : & Config) -> Result < Outcome >
|
||||
mind_body::persona: fn reply_schema (rooms : & [String] , campaigns : & [String] , may_open : bool , fetch : bool) -> String
|
||||
mind_body::persona: fn carry (control : & impl ControlSource , swarm : & impl ControlSource , instance : & Slug) -> Outcome
|
||||
mind_body::persona: impl Config
|
||||
mind_body::persona: impl Config: fn from_args (args : & [String] , usage : & str) -> Result < (Self , Vec < String >) >
|
||||
mind_body::persona: impl Config: fn from_args_and_env (args : & [String] , lookup : impl Fn (& str) -> Option < String > , usage : & str) -> Result < (Self , Vec < String >) >
|
||||
mind_body::persona: impl Conversation
|
||||
mind_body::persona: impl FileTrace
|
||||
mind_body::persona: impl FileTrace: fn new (path : impl Into < PathBuf >) -> Self
|
||||
mind_body::persona: impl HuginnPersona
|
||||
mind_body::persona: impl HuginnPersona: fn new (client : HuginnClient) -> Self
|
||||
mind_body::persona: impl PersonaState for HuginnPersona
|
||||
mind_body::persona: impl Saved
|
||||
mind_body::persona: impl Sender
|
||||
mind_body::persona: impl Shown
|
||||
mind_body::persona: impl Trace for FileTrace
|
||||
mind_body::persona: impl Trace for Noted < '_ >
|
||||
mind_body::persona: impl WearFact
|
||||
mind_body::persona: impl fmt :: Display for Failure
|
||||
mind_body::persona: impl fmt :: Display for Outcome
|
||||
mind_body::persona: impl< 'a , C : ControlSource , S : ControlSource > PersonaInbox < 'a , C , S >
|
||||
mind_body::persona: impl< C : ControlSource , S : ControlSource > PersonaInbox < '_ , C , S >
|
||||
mind_body::persona: impl< C : ControlSource , S : ControlSource > SessionInbox for PersonaInbox < '_ , C , S >
|
||||
mind_body::persona: pub trait PersonaState { fn instructions (& self) -> Result < String > ; }
|
||||
mind_body::persona: pub trait Trace { fn record (& self , trace : & TurnTrace) -> Result < () > ; }
|
||||
mind_body::persona: pub use model_connector :: PlanQuota ;
|
||||
mind_body::persona: struct Config # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[rooms: Vec < String >, operators: Vec < String >, minds: Vec < String >, own_sender: String, model: String, doctrine: Vec < PathBuf >, image_store: Option < PathBuf >, fetch: bool]
|
||||
mind_body::persona: struct Config # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[rooms: Vec < String >, operators: Vec < String >, minds: Vec < String >, own_sender: String, model: String]
|
||||
mind_body::persona: struct FileTrace pub-fields[]
|
||||
mind_body::persona: struct HuginnPersona pub-fields[]
|
||||
mind_body::persona: struct OpenDirect # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[with: String, text: String]
|
||||
mind_body::persona: struct Organs< 'a > pub-fields[mind: & 'a dyn MindPort, launcher: & 'a dyn Launcher, clock: & 'a dyn Clock, host: & 'a str, chat: & 'a dyn Chat, persona: & 'a dyn PersonaState, trace: & 'a dyn Trace, heartbeat: Option < & 'a dyn HeartbeatView >, lender: & 'a dyn Lender, spawner: & 'a dyn Spawner, run_cap_s: u64, sandbox: & 'a Path, scratch: & 'a Path]
|
||||
mind_body::persona: struct PersonaReply # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[intent: String, say: Vec < Say >, open_direct: Option < OpenDirect >, start_self_run: Option < StartSelfRun >, remember: Vec < Remember >, rule: Vec < Rule >, settle: Vec < Settle >, tempo_vote_beat_s: Option < Option < u32 > >, urgency_permille: Option < u16 >, save_attachment: Option < Option < SaveAttachment > >]
|
||||
mind_body::persona: struct Remember # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[sort: MemorySort, text: String, about: Vec < String >, salience_permille: u16]
|
||||
mind_body::persona: struct Rule # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[by: String, words: String, repo: String, campaign: String, title: String, ruling: String]
|
||||
mind_body::persona: struct SaveAttachment # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[room: String, event: String]
|
||||
mind_body::persona: struct Say # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[room: String, text: String]
|
||||
mind_body::persona: struct Settle # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[item: String, outcome: SettleOutcome, why: String]
|
||||
mind_body::persona: struct StartSelfRun # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[brief: String, repo: String, campaign: String, for_item: Option < String >]
|
||||
mind_body::persona: struct TraceItem # [derive (Clone , Debug , PartialEq , Eq , Serialize , Deserialize)] pub-fields[provenance: String, room: String, text: String]
|
||||
mind_body::persona: struct TurnTrace # [derive (Clone , Debug , PartialEq , Eq , DatabaseEntry)] pub-fields[run: String, turn: u32, at: DateTime < Utc >, inputs: Vec < TraceItem >, outputs: Vec < TraceItem >, outcome: String]
|
||||
mind_body::queue: # [derive (Clone , Debug , PartialEq , Eq)] pub enum Breaker { Clear , Trip { run : PipelineRef } , }
|
||||
mind_body::queue: const CONSECUTIVE : usize
|
||||
mind_body::queue: const EMPTY_SHARE : usize
|
||||
mind_body::queue: const WINDOW : usize
|
||||
mind_body::queue: fn breaker (mind : & dyn MindPort , item : & PipelineRef , ending : & [PipelineRef]) -> Result < Breaker >
|
||||
mind_body::queue: fn on_hold (mind : & dyn MindPort) -> Result < Held >
|
||||
mind_body::queue: fn queue (mind : & dyn MindPort , instance : & Slug , ending : & [PipelineRef] , held : & Held ,) -> Result < Vec < PipelineRef > >
|
||||
mind_body::queue: impl Held
|
||||
mind_body::queue: impl Held: fn contains (& self , item : & PipelineRef) -> bool
|
||||
mind_body::queue: impl Held: fn items (& self) -> & [PipelineRef]
|
||||
|
|
@ -353,47 +304,8 @@ mind_body::self_run::inbox: fn derive_counting_held (mind : & dyn MindPort , hol
|
|||
mind_body::self_run::inbox: fn render (briefs : & [Brief] , plan : & str) -> String
|
||||
mind_body::self_run::inbox: fn resolving_sentence (id : & str) -> String
|
||||
mind_body::self_run::inbox: struct Brief # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[id: String, campaign: String, repo: String, item: String, ordinal: u64]
|
||||
mind_body::sleep: impl< C : ControlSource , S : ControlSource > SessionInbox for SleepInbox < '_ , C , S >
|
||||
mind_body::sleep: impl< C : ControlSource , S : ControlSource > SleepInbox < '_ , C , S >
|
||||
mind_body::stream: # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub enum Mode { Live , DryRun , }
|
||||
mind_body::stream: # [derive (Clone , Debug , PartialEq)] pub enum Outcome { Held (crate :: control :: HeldReason) , NotDue , Blank , Thought { thought : Box < PipelineThought > , admitted : bool } , }
|
||||
mind_body::stream: const AGENT : & str
|
||||
mind_body::stream: const STREAM_IDLE_GAP_S : i64
|
||||
mind_body::stream: const STREAM_MIN_GAP_S : i64
|
||||
mind_body::stream: const STREAM_POLL_S : i64
|
||||
mind_body::stream: fn connector_key (credentials : & std :: path :: Path) -> Result < String >
|
||||
mind_body::stream: fn tick (organs : & Organs , brake : & dyn Fn () -> Effective , mode : Mode , now : DateTime < Utc >) -> Result < Outcome >
|
||||
mind_body::stream: impl Continuer for OllamaContinuer
|
||||
mind_body::stream: impl HuginnPersonaSource
|
||||
mind_body::stream: impl HuginnPersonaSource: fn new (client : HuginnClient) -> Self
|
||||
mind_body::stream: impl KitProjector
|
||||
mind_body::stream: impl KitProjector: fn open (endpoint : SocketAddr , connection_key : String , caller : & str , model : & str , timeout : Duration) -> Result < Self >
|
||||
mind_body::stream: impl OllamaContinuer
|
||||
mind_body::stream: impl OllamaContinuer: fn open (endpoint : & str , base_model : & str) -> Result < Self >
|
||||
mind_body::stream: impl PersonaSource for HuginnPersonaSource
|
||||
mind_body::stream: impl Projector for KitProjector
|
||||
mind_body::stream: impl Sampling
|
||||
mind_body::stream: impl Seed
|
||||
mind_body::stream: pub trait Continuer { fn continue_raw (& self , prompt : & str , sampling : & Sampling , n : usize) -> Result < Vec < String > > ; }
|
||||
mind_body::stream: pub trait PersonaSource { fn persona (& self) -> Result < PersonaRead > ; }
|
||||
mind_body::stream: pub trait Projector { fn model (& self) -> & str ; fn project (& self , request_id : & str , prompt : & str) -> Result < String > ; }
|
||||
mind_body::stream: struct HuginnPersonaSource pub-fields[]
|
||||
mind_body::stream: struct KitProjector pub-fields[]
|
||||
mind_body::stream: struct OllamaContinuer pub-fields[]
|
||||
mind_body::stream: struct Organs< 'a > pub-fields[mind: & 'a dyn MindPort, persona: & 'a dyn PersonaSource, projector: & 'a dyn Projector, continuer: & 'a dyn Continuer, base_model: & 'a str]
|
||||
mind_body::stream: struct PersonaRead pub-fields[state: Value, receipt_id: String]
|
||||
mind_body::stream: struct Sampling # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub-fields[temperature_permille: u16, min_p_permille: u16, max_tokens: u16]
|
||||
mind_body::tempo: const DEFAULT_BEAT_S : u32
|
||||
mind_body::tempo: const FALLBACK_MAX_S : u32
|
||||
mind_body::tempo: const FALLBACK_MIN_S : u32
|
||||
mind_body::tempo: const GAINS : Gains
|
||||
mind_body::tempo: const LOG_BOUND_ORDERS : f64
|
||||
mind_body::tempo: fn fallback_beat (state : & mut Fallback , reading : Option < & Reading > , now_ms : i64 , target_permille : u32 , voted : bool , saturated : bool ,) -> u32
|
||||
mind_body::tempo: fn pick (rows : & [Standing] , beat_s : u32) -> Option < usize >
|
||||
mind_body::tempo: fn score (standing : & Standing , beat_s : u32) -> f64
|
||||
mind_body::tempo: fn tally (votes : & [Option < u32 >]) -> u32
|
||||
mind_body::tempo: fn urgency_permille (standing : & Standing) -> u16
|
||||
mind_body::tempo: struct Fallback # [derive (Clone , Debug , Default , PartialEq)] pub-fields[integral: f64, last_used_permille: u32, last_observed_at_ms: i64, last_now_ms: i64, carry_per_h: f64, window_resets_at_ms: i64, rate_per_h: f64]
|
||||
mind_body::tempo: struct Gains # [derive (Clone , Copy , Debug , PartialEq)] pub-fields[kp: f64, ki: f64, kd: f64]
|
||||
mind_body::tempo: struct Reading # [derive (Clone , Copy , Debug , PartialEq , Eq)] pub-fields[used_permille: u32, observed_at_ms: i64, resets_at_ms: i64]
|
||||
mind_body::tempo: struct Standing # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[slug: String, eligible: bool, waited_s: Option < u64 >, last_turn_ms: Option < i64 >, self_permille: u16, topic_permille: u16, addressed_by_member: bool]
|
||||
mind_body::tempo: struct Standing # [derive (Clone , Debug , PartialEq , Eq)] pub-fields[slug: String, eligible: bool, waited_s: Option < u64 >, last_turn_ms: Option < i64 >, topic_permille: u16, addressed_by_member: bool]
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
//! The `mind-stream` binary's startup reads the connector key from systemd's credentials directory:
|
||||
//! an absent directory names the variable, and the key's bytes never reach stderr.
|
||||
|
||||
use std::process::{Command, Output};
|
||||
|
||||
const CANARY: &str = "zq7Kx-9wVtm3Pd";
|
||||
|
||||
fn run(credentials: Option<&std::path::Path>, connector: &str) -> Output {
|
||||
let mut command = Command::new(env!("CARGO_BIN_EXE_mind-stream"));
|
||||
command
|
||||
.args(["--instance", "eureka", "--once"])
|
||||
.env_clear()
|
||||
.env("HUGINN_ENDPOINT", "rudp://127.0.0.1:9")
|
||||
.env("MIND_STREAM_CONNECTOR_ENDPOINT", connector)
|
||||
.env("MIND_STREAM_PROJECTOR_MODEL", "claude-haiku-5-5")
|
||||
.env("MIND_STREAM_BASE_MODEL", "base:test")
|
||||
.env("MIND_STREAM_ENDPOINT", "http://127.0.0.1:1");
|
||||
if let Some(dir) = credentials {
|
||||
command.env("CREDENTIALS_DIRECTORY", dir);
|
||||
}
|
||||
command.output().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_credentials_directory_names_the_variable_and_a_missing_key_names_the_credential() {
|
||||
let output = run(None, "127.0.0.1:4103");
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
assert_eq!(output.status.code(), Some(1));
|
||||
assert!(stderr.contains("CREDENTIALS_DIRECTORY"), "{stderr}");
|
||||
let empty = tempfile::tempdir().unwrap();
|
||||
let stderr = String::from_utf8_lossy(&run(Some(empty.path()), "127.0.0.1:4103").stderr).to_string();
|
||||
assert!(stderr.contains("connector-key"), "{stderr}");
|
||||
}
|
||||
|
||||
/// Every four-byte run of the key: a partial key is as much a leak as the whole.
|
||||
fn leaks(shown: &str) -> Option<&str> {
|
||||
CANARY.as_bytes().windows(4).map(|window| std::str::from_utf8(window).unwrap()).find(|run| shown.contains(run))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_byte_run_of_the_key_is_printed_on_any_startup_path() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::fs::write(dir.path().join("connector-key"), format!("{CANARY}\n")).unwrap();
|
||||
// A non-loopback connector and a configuration the client refuses both end startup after the key was read and handed over.
|
||||
for (connector, ending) in [("10.77.0.1:4103", "not a loopback address"), ("127.0.0.1:0", "could not be configured")] {
|
||||
let output = run(Some(dir.path()), connector);
|
||||
let shown = format!("{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
|
||||
assert!(shown.contains(ending), "{connector}: the key was read and startup went on: {shown}");
|
||||
assert_eq!(leaks(&shown), None, "{connector}: {shown}");
|
||||
}
|
||||
}
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
//! The stream's HTTP agent ignores the process environment's proxy settings and follows no redirects.
|
||||
//!
|
||||
//! An agent reads `ALL_PROXY` and friends when it is built, so the check runs in a child process
|
||||
//! that has them. The parent hosts three loopback listeners: the model host (it answers every
|
||||
//! request with a redirect), the proxy the child's environment names, and the redirect's target.
|
||||
|
||||
// The suite re-executes its own test binary as the child and hands it the endpoint through the
|
||||
// environment. That is test plumbing in its own crate: it reaches no library code, which
|
||||
// `#![forbid]`s these readers.
|
||||
#![allow(clippy::disallowed_methods, reason = "the test re-executes itself and hands the child its role through the environment")]
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpListener;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
use mind_body::stream::{Continuer, OllamaContinuer, Sampling};
|
||||
|
||||
/// Counts the connections a loopback listener accepts, answering each with `reply` when given.
|
||||
fn listen(reply: Option<String>) -> (String, Arc<AtomicUsize>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = listener.local_addr().unwrap();
|
||||
let hits = Arc::new(AtomicUsize::new(0));
|
||||
let counted = Arc::clone(&hits);
|
||||
std::thread::spawn(move || {
|
||||
for connection in listener.incoming().flatten() {
|
||||
let mut connection = connection;
|
||||
counted.fetch_add(1, Ordering::SeqCst);
|
||||
if let Some(reply) = &reply {
|
||||
let mut seen = [0u8; 4096];
|
||||
let _ = connection.read(&mut seen);
|
||||
let _ = connection.write_all(reply.as_bytes());
|
||||
}
|
||||
}
|
||||
});
|
||||
(format!("http://{address}"), hits)
|
||||
}
|
||||
|
||||
fn settled(hits: &AtomicUsize, wanted: bool) -> bool {
|
||||
for _ in 0..40 {
|
||||
if (hits.load(Ordering::SeqCst) > 0) == wanted {
|
||||
return true;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn child(role: &str, endpoint: &str, proxy: &str) {
|
||||
let status = std::process::Command::new(std::env::current_exe().unwrap())
|
||||
.args(["--exact", "the_stream_agent_ignores_the_proxy_environment", "--nocapture"])
|
||||
.env("STREAM_PROXY_CHILD", role)
|
||||
.env("STREAM_PROXY_ENDPOINT", endpoint)
|
||||
.env("ALL_PROXY", proxy)
|
||||
.env("HTTP_PROXY", proxy)
|
||||
.status()
|
||||
.unwrap();
|
||||
assert!(status.success(), "the {role} child failed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_stream_agent_ignores_the_proxy_environment() {
|
||||
if let Ok(role) = std::env::var("STREAM_PROXY_CHILD") {
|
||||
let endpoint = std::env::var("STREAM_PROXY_ENDPOINT").unwrap();
|
||||
if role == "plain" {
|
||||
let plain: ureq::Agent = ureq::Agent::config_builder().timeout_global(Some(Duration::from_secs(2))).build().into();
|
||||
let _ = plain.get(&endpoint).call();
|
||||
} else {
|
||||
let sampling = Sampling { temperature_permille: 1000, min_p_permille: 50, max_tokens: 16 };
|
||||
let _ = OllamaContinuer::open(&endpoint, "b").unwrap().continue_raw("hello", &sampling, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let (target, target_hits) = listen(None);
|
||||
let (proxy, proxy_hits) = listen(None);
|
||||
let redirect = format!("HTTP/1.1 302 Found\r\nLocation: {target}/elsewhere\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
|
||||
let (endpoint, endpoint_hits) = listen(Some(redirect));
|
||||
|
||||
child("stream", &endpoint, &proxy);
|
||||
assert!(settled(&endpoint_hits, true), "the stream never reached its own endpoint");
|
||||
std::thread::sleep(Duration::from_millis(300));
|
||||
assert_eq!(proxy_hits.load(Ordering::SeqCst), 0, "the stream's agent took the environment's proxy");
|
||||
assert_eq!(target_hits.load(Ordering::SeqCst), 0, "the stream's agent followed a redirect");
|
||||
|
||||
child("plain", &endpoint, &proxy);
|
||||
assert!(settled(&proxy_hits, true), "the child's environment names a proxy");
|
||||
}
|
||||
Loading…
Reference in a new issue