Park the divergence stream organ (C2, persona-only-divergence-parked)
Deletes stream.rs, its tests, the mind-stream binary and its integration tests, and the dependencies only it used (ghostlight-persona-projection, ureq, the model-connector daemon dev feature). The organ is kept at tag parked/divergence-stream, whose note names the ruling's return condition. A brake hold still stops mind-stream@ units a host has installed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
58a320c6ee
commit
609696f43e
9 changed files with 2 additions and 2059 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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -124,7 +124,8 @@ fn template(turn: RunTurn) -> &'static str {
|
|||
}
|
||||
}
|
||||
|
||||
/// The stream's unit template: not a run slot, so only a brake hold names it.
|
||||
/// The stream's unit template: not a run slot, so only a brake hold names it. The organ is parked
|
||||
/// (tag `parked/divergence-stream`); a hold still stops its unit wherever a host has it installed.
|
||||
const STREAM_TEMPLATE: &str = "mind-stream";
|
||||
|
||||
/// A template's unit for one instance, the instance held to the leaf's grammar
|
||||
|
|
|
|||
|
|
@ -17,10 +17,6 @@
|
|||
//! 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.
|
||||
//!
|
||||
//! `lease_client` asks the Claude connector for a session's credential and the
|
||||
//! plan's usage windows. `self_run` carries one Self run inside `mind-self@<instance>`: the
|
||||
//! `mind-self-run` binary runs `claude` over stream-json for her one open Self
|
||||
|
|
@ -50,7 +46,6 @@ pub mod queue;
|
|||
mod recall;
|
||||
pub mod self_run;
|
||||
mod sleep;
|
||||
pub mod stream;
|
||||
pub mod tempo;
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -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,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