Merge pull request 'Persona door: a second listener that answers reads and her persona, never an admission (C1)' (#74) from hands/persona-door into main

This commit is contained in:
Eureka 2026-11-19 22:25:39 +00:00
commit 57b931fdc3
13 changed files with 383 additions and 88 deletions

44
Cargo.lock generated
View file

@ -350,6 +350,26 @@ dependencies = [
"cipher",
]
[[package]]
name = "cultcache-rs"
version = "0.3.1"
source = "git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23#a4eae9056152335662f1115f97bf35fb00ab6d23"
dependencies = [
"anyhow",
"chrono",
"cultcache-rs-derive 0.2.0 (git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23)",
"fs2",
"redb",
"rmp-serde",
"serde",
"serde_bytes",
"serde_json",
"serde_stacker",
"stacker",
"uuid",
"windows-sys 0.59.0",
]
[[package]]
name = "cultcache-rs"
version = "0.3.1"
@ -371,30 +391,10 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "cultcache-rs"
version = "0.3.1"
source = "git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23#a4eae9056152335662f1115f97bf35fb00ab6d23"
dependencies = [
"anyhow",
"chrono",
"cultcache-rs-derive 0.2.0 (git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23)",
"fs2",
"redb",
"rmp-serde",
"serde",
"serde_bytes",
"serde_json",
"serde_stacker",
"stacker",
"uuid",
"windows-sys 0.59.0",
]
[[package]]
name = "cultcache-rs-derive"
version = "0.2.0"
source = "git+https://github.com/GameCult/CultLib.git?rev=baf4ecf78fae634fafef956d3b85420162c074bb#baf4ecf78fae634fafef956d3b85420162c074bb"
source = "git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23#a4eae9056152335662f1115f97bf35fb00ab6d23"
dependencies = [
"proc-macro2",
"quote",
@ -404,7 +404,7 @@ dependencies = [
[[package]]
name = "cultcache-rs-derive"
version = "0.2.0"
source = "git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f1115f97bf35fb00ab6d23#a4eae9056152335662f1115f97bf35fb00ab6d23"
source = "git+https://github.com/GameCult/CultLib.git?rev=baf4ecf78fae634fafef956d3b85420162c074bb#baf4ecf78fae634fafef956d3b85420162c074bb"
dependencies = [
"proc-macro2",
"quote",

View file

@ -172,7 +172,7 @@ pub fn serve<I: IndexSink<OwnedRedbMessagePackBackingStore> + Send + 'static>(
let loop_stopping = Arc::clone(&stopping);
let registry = schema_registry().unwrap();
let thread = std::thread::spawn(move || {
run(&mut daemon, &mut hub, &registry, None, &loop_stopping, &ServeOptions::default()).unwrap();
run(&mut daemon, &mut hub, None, &registry, None, &loop_stopping, &ServeOptions::default()).unwrap();
});
Server { addr, stopping, thread: Some(thread), ended: Arc::default(), _root: Some(root) }
}

View file

@ -109,6 +109,17 @@ impl Handled {
}
}
/// Which RUDP listener a request arrived on. `Full` is `--bind`, which the
/// faculties that write the mind reach. `Persona` is `--persona-bind`, the
/// only listener a Persona's uid can reach (the host fence decides that): it
/// answers what `HuginnMindRequest::reaches_persona_door` accepts and refuses
/// the rest with `MindRefusal::NotThisDoor` before the mind is asked.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Door {
Full,
Persona,
}
/// The door checks an account id; the Mind's desk calls take it as the text it is.
fn short_id(id: AccountId) -> Short {
Short(id.0)
@ -177,6 +188,16 @@ impl<S: MindStore, I: IndexSink<S>> Daemon<S, I> {
runtime_id(self.mind.instance())
}
/// One request as it arrived at `door`: a request the door does not
/// answer is refused before the mind sees it, and every other goes to
/// `handle`. The wire reaches the mind only through here.
pub fn handle_at(&mut self, door: Door, request: HuginnMindRequest, now: DateTime<Utc>) -> Handled {
if door == Door::Persona && !request.reaches_persona_door() {
return Handled::Answered(HuginnMindResponse::Refused(MindRefusal::NotThisDoor));
}
self.handle(request, now)
}
/// One request, one answer, or a search to finish. `Admit` goes to the mind whole, because the
/// batch declares its own instance and admission's A1 is the check. Every
/// other request names an instance: `require_instance` now applies the
@ -1011,6 +1032,117 @@ pub(crate) mod tests {
assert_eq!(status(&mut daemon).persona, huginn_mind::wire::PersonaStatus::Absent);
}
/// The persona door refuses an admission before the mind sees it, so the
/// store is unchanged, and answers the reads and her own persona put; the
/// full door admits the same batch.
#[test]
fn the_persona_door_refuses_an_admission_and_answers_reads_and_her_persona() {
let (_root, mut daemon) = seeded();
let before = status(&mut daemon);
let admit = || HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)]));
assert_eq!(
daemon.handle_at(Door::Persona, admit(), now()).answered(),
HuginnMindResponse::Refused(MindRefusal::NotThisDoor)
);
assert_eq!(status(&mut daemon), before, "nothing was written");
let state = serde_json::json!({
"schemaVersion": "gamecult.persona_state.v0",
"provenance": {
"sourceSystem": "huginn", "sourceDocumentId": INSTANCE, "sourceUpdatedAt": "2026-11-19T10:00:00Z",
"exportedAt": "2026-11-19T10:00:00Z", "authority": "canonical"
},
"personaId": INSTANCE,
"publicName": "Test Persona",
"presentation": { "voiceSummary": "plain" },
"activationProfile": {
"underlyingOrganization": {}, "stableDispositions": {}, "behavioralDimensions": {},
"presentationStrategy": {}, "voiceStyle": {}, "situationalState": {}
},
"thoughtMemory": { "shortTerm": [], "memories": [], "incubation": [] },
"agencyPressure": { "pressures": [] },
"candidateActions": { "actions": [] },
"affect": {
"needs": [], "socialBonds": [], "statusReads": [], "moodDimensions": [],
"socialBiases": [], "doctrineStances": []
},
"updatedAt": "2026-11-19T10:00:00Z"
});
let put = HuginnMindRequest::PersonaPut {
instance: slug(INSTANCE),
provenance: provenance(),
state,
expected_updated_at: None,
};
assert!(matches!(
daemon.handle_at(Door::Persona, put, now()).answered(),
HuginnMindResponse::PersonaPut(huginn_mind::PersonaPutOutcome::Committed { .. })
));
assert!(matches!(
daemon.handle_at(Door::Persona, HuginnMindRequest::PersonaGet { instance: slug(INSTANCE) }, now()).answered(),
HuginnMindResponse::Persona(Some(_))
));
let query = HuginnMindRequest::Query { instance: slug(INSTANCE), selection: Selection::default(), semantic: None };
let HuginnMindResponse::Query(page) = daemon.handle_at(Door::Persona, query, now()).answered() else {
panic!("a query answers a page at the persona door")
};
let id = match page.items {
PipelinePageItems::Headers(items) => items[0].id.clone(),
PipelinePageItems::Documents(items) => items[0].id.clone(),
};
assert!(matches!(
daemon.handle_at(Door::Persona, HuginnMindRequest::View { instance: slug(INSTANCE), id }, now()).answered(),
HuginnMindResponse::View(Some(_))
));
assert_eq!(status(&mut daemon).documents, before.documents, "a persona put writes no pipeline document");
assert!(
matches!(daemon.handle_at(Door::Full, admit(), now()).answered(), HuginnMindResponse::Admit(_)),
"the full door hands the same batch to admission"
);
}
/// Every read the persona door answers reaches the mind there: none of the
/// source, chat, activity and roots reads is refused as `NotThisDoor`.
/// What the mind then says (a search, or `Unavailable` with no index or
/// forge) is the mind's, the same at either door.
#[test]
fn the_persona_door_hands_every_other_read_to_the_mind() {
use huginn_mind::wire::{ChatWindowQuery, SourceQuery};
let (_root, mut daemon) = seeded();
let reads = [
HuginnMindRequest::SearchSources {
instance: slug(INSTANCE),
query: SourceQuery { text: Line("reconcile".into()), texts: vec![], top_k: 5, repo: None, front: false },
},
HuginnMindRequest::SearchChat {
instance: slug(INSTANCE),
query: ChatQuery { texts: vec![Line("release".into())], rooms: vec![], after_ms: None, before_ms: None, top_k: 5 },
},
HuginnMindRequest::ChatWindow {
instance: slug(INSTANCE),
query: ChatWindowQuery { event_id: Short("$a".into()), before: 5, after: 0 },
},
HuginnMindRequest::StewardedActivity { instance: slug(INSTANCE), repos: vec![], after: None },
HuginnMindRequest::RootsTouched { instance: slug(INSTANCE) },
];
for read in reads {
let operation = read.operation();
let at_door = daemon.handle_at(Door::Persona, read.clone(), now());
assert!(
!matches!(at_door, Handled::Answered(HuginnMindResponse::Refused(MindRefusal::NotThisDoor))),
"{operation} is refused at the persona door"
);
if let (Handled::Answered(at_door), Handled::Answered(at_full)) = (at_door, daemon.handle_at(Door::Full, read, now())) {
assert_eq!(at_door, at_full, "{operation} answers the same at either door");
}
}
assert!(matches!(
daemon.handle_at(Door::Persona, HuginnMindRequest::RootsTouched { instance: slug(INSTANCE) }, now()).answered(),
HuginnMindResponse::RootsTouched(_)
));
}
/// A declared instance outside `Slug`'s grammar is refused by its own
/// name, `InvalidFormat`, on the read path, before the identity
/// comparison ever runs: it is not merely a foreign mind, it names no

View file

@ -467,7 +467,7 @@ mod door_smoke {
let stopping = Arc::new(AtomicBool::new(false));
let loop_stopping = Arc::clone(&stopping);
let serving = std::thread::spawn(move || {
run(&mut daemon, &mut hub, &registry, Some(&mut door), &loop_stopping, &ServeOptions::default()).unwrap();
run(&mut daemon, &mut hub, None, &registry, Some(&mut door), &loop_stopping, &ServeOptions::default()).unwrap();
});
let mut stalled = connect(&path);
@ -591,7 +591,7 @@ mod door_smoke {
let registry = schema_registry().unwrap();
let mut bodies = crate::bodies::DeferredBodies::new(1 << 20, Duration::from_secs(60));
let desk = raw(MIND_SERVICE_ID, DESK_REQUEST_SCHEMA, "answer", "m-1", rmp_serde::to_vec_named(&answer("x", DeskAuthority::Operator, &["x"], wire(&hash, R1))).unwrap());
let CultNetMessage::OperationResponse { payload_schema, diagnostics, .. } = crate::serve::answer(&mut daemon, &registry, &mut bodies, desk, now()).reply() else {
let CultNetMessage::OperationResponse { payload_schema, diagnostics, .. } = crate::serve::answer(&mut daemon, crate::daemon::Door::Full, &registry, &mut bodies, desk, now()).reply() else {
panic!("expected a response")
};
assert_eq!(payload_schema, huginn_mind::envelope::FAILURE_SCHEMA);
@ -686,6 +686,7 @@ mod door_smoke {
state_root: root.path().to_path_buf(),
instance: slug(INSTANCE),
bind: "127.0.0.1:0".parse().unwrap(),
persona_bind: None,
qdrant_url: "http://127.0.0.1:1".into(),
ollama_urls: vec!["http://127.0.0.1:1".into()],
reserve_ollama_urls: Vec::new(),

View file

@ -1,5 +1,5 @@
//! The process: open one instance's mind, bind one socket, serve until a stop
//! signal. A mind that will not open ends the process with the refusal on
//! The process: open one instance's mind, bind its socket (and the persona
//! door's, when asked), serve until a stop signal. A mind that will not open ends the process with the refusal on
//! stderr and no socket ever bound.
use std::sync::Arc;
@ -15,7 +15,7 @@ fn main() -> Result<()> {
let options = parse_options(std::env::args().skip(1))?;
let embedder = pool(&options.ollama_urls, &options.reserve_ollama_urls, &options.embedding_model);
let index = QdrantIndex::new(&options.qdrant_url);
let Started { mut daemon, mut hub, registry, mut desk } = match startup(&options, embedder, index, Backoff::default()) {
let Started { mut daemon, mut hub, mut persona, registry, mut desk } = match startup(&options, embedder, index, Backoff::default()) {
Ok(started) => started,
Err(error) => {
eprintln!("huginn-daemon refuses to serve {}: {error:#}", options.instance.0);
@ -33,8 +33,11 @@ fn main() -> Result<()> {
}
eprintln!("huginn-daemon serving {} on {}", options.instance.0, hub.local_addr()?);
if let Some(door) = &persona {
eprintln!("huginn-daemon serving the persona door on {}", door.local_addr()?);
}
if let Some(door) = &desk {
eprintln!("huginn-daemon serving the desk door on {}", door.path().display());
}
run(&mut daemon, &mut hub, &registry, desk.as_mut(), &stopping, &ServeOptions::default())
run(&mut daemon, &mut hub, persona.as_mut(), &registry, desk.as_mut(), &stopping, &ServeOptions::default())
}

View file

@ -143,9 +143,10 @@ fn quick() -> Backoff {
Backoff { initial: Duration::from_millis(1), max: Duration::from_millis(2), recheck: Duration::from_secs(3600), search_deadline: Duration::from_secs(30) }
}
/// A daemon serving a mind over a real socket, with the ports the test gave it.
/// A daemon serving a mind over real sockets, its full door and its persona door, with the ports the test gave it.
struct Harness {
endpoint: String,
persona_endpoint: String,
stopping: Arc<AtomicBool>,
serving: Option<JoinHandle<Result<()>>>,
embedder: FakeEmbedder,
@ -172,19 +173,31 @@ impl Harness {
let mut daemon: Daemon<OwnedRedbMessagePackBackingStore, WorkerSink> = Daemon::new(mind, sink);
let mut hub = bind("127.0.0.1:0".parse().unwrap(), &daemon.runtime_id()).unwrap();
let endpoint = format!("rudp://{}", hub.local_addr().unwrap());
let mut door = bind("127.0.0.1:0".parse().unwrap(), &daemon.runtime_id()).unwrap();
let persona_endpoint = format!("rudp://{}", door.local_addr().unwrap());
let registry = schema_registry().unwrap();
let stopping = Arc::new(AtomicBool::new(false));
let loop_stopping = Arc::clone(&stopping);
let options = ServeOptions::default();
let serving = std::thread::spawn(move || run(&mut daemon, &mut hub, &registry, None, &loop_stopping, &options));
Self { endpoint, stopping, serving: Some(serving), embedder, index, _root: root }
let serving =
std::thread::spawn(move || run(&mut daemon, &mut hub, Some(&mut door), &registry, None, &loop_stopping, &options));
Self { endpoint, persona_endpoint, stopping, serving: Some(serving), embedder, index, _root: root }
}
fn client(&self) -> Client {
Self::client_at(&self.endpoint)
}
/// A client of the persona door.
fn persona_client(&self) -> Client {
Self::client_at(&self.persona_endpoint)
}
fn client_at(endpoint: &str) -> Client {
let mut client = CultMesh::create_rudp_client_for_endpoint(
"semantic-test".to_string(),
CULTNET_OPERATION_CONNECTION_ID,
&self.endpoint,
endpoint,
CultMeshRudpSocketOptions::default(),
)
.unwrap();
@ -272,8 +285,19 @@ fn healthy() -> Harness {
/// the members.
#[test]
fn a_semantic_query_returns_only_documents_the_mind_holds_and_the_selection_admits() {
a_semantic_query_returns_only_documents_the_mind_holds_and_the_selection_admits_at(Harness::client);
}
/// The same semantic read asked at the persona door: the search is held while the index works, and its answer leaves by
/// the persona door's own hub to the session that asked there.
#[test]
fn a_semantic_query_at_the_persona_door_is_answered_through_the_persona_door() {
a_semantic_query_returns_only_documents_the_mind_holds_and_the_selection_admits_at(Harness::persona_client);
}
fn a_semantic_query_returns_only_documents_the_mind_holds_and_the_selection_admits_at(client_of: fn(&Harness) -> Client) {
let harness = healthy();
let mut client = harness.client();
let mut client = client_of(&harness);
until_index(&mut client, "Current", |status| *status == IndexStatus::Current);
harness.index.state.lock().unwrap().hits = vec![
hit("question", id("question", "Q1"), 0.95),

View file

@ -78,7 +78,7 @@ use huginn_mind::{Mind, MindRefusal, MindStore, OwnedRedbMessagePackBackingStore
use crate::bodies::{
DEFERRED_BUDGET_BYTES, DEFERRED_CHUNK_BYTES, DeferredBodies,
};
use crate::daemon::{Daemon, Handled, IndexSink, Search, SearchTicket, runtime_id};
use crate::daemon::{Daemon, Door, Handled, IndexSink, Search, SearchTicket, runtime_id};
use crate::desk_door::{DeskDoor, bind_desk};
use huginn_mind::envelope::{OperationFailure, decode_request, encode_failure, encode_response};
use huginn_mind::MAX_DEFERRED_BODY_BYTES;
@ -111,6 +111,10 @@ pub struct Options {
pub state_root: PathBuf,
pub instance: Slug,
pub bind: SocketAddr,
/// The persona door (`--persona-bind`): a second RUDP listener answering
/// only what `HuginnMindRequest::reaches_persona_door` accepts. Absent,
/// the daemon has no persona door.
pub persona_bind: Option<SocketAddr>,
pub qdrant_url: String,
/// The GPU hosts: all take embedding at once. One is the plain case.
pub ollama_urls: Vec<String>,
@ -134,7 +138,7 @@ pub struct DeskOptions {
/// Exactly `--state-root`, `--instance`, `--bind`, `--qdrant-url`,
/// `--ollama-url` and `--embedding-model`, each required, each once, except
/// `--ollama-url`, which repeats for each GPU host, and the optional
/// `--reserve-ollama-url`, which repeats for each CPU host, and the optional `--forge-url`, once, and the optional pair `--desk-socket` (an absolute path) and `--desk-peer-uid` (a uid other than 0), both or neither, each once. The URLs are plain
/// `--reserve-ollama-url`, which repeats for each CPU host, and the optional `--forge-url` and `--persona-bind` (an ip:port other than `--bind`), each once, and the optional pair `--desk-socket` (an absolute path) and `--desk-peer-uid` (a uid other than 0), both or neither, each once. The URLs are plain
/// `http://`: this build carries no TLS.
pub fn parse_options(args: impl Iterator<Item = String>) -> Result<Options> {
let mut args = args;
@ -143,7 +147,7 @@ pub fn parse_options(args: impl Iterator<Item = String>) -> Result<Options> {
while let Some(name) = args.next() {
let name = name.strip_prefix("--").with_context(|| format!("expected --option, got {name:?}"))?;
ensure!(
matches!(name, "state-root" | "instance" | "bind" | "qdrant-url" | "ollama-url" | "reserve-ollama-url" | "embedding-model" | "forge-url" | "desk-socket" | "desk-peer-uid"),
matches!(name, "state-root" | "instance" | "bind" | "persona-bind" | "qdrant-url" | "ollama-url" | "reserve-ollama-url" | "embedding-model" | "forge-url" | "desk-socket" | "desk-peer-uid"),
"unsupported Huginn option --{name}"
);
let value = args.next().with_context(|| format!("missing value for --{name}"))?;
@ -167,10 +171,20 @@ pub fn parse_options(args: impl Iterator<Item = String>) -> Result<Options> {
};
let embedding_model = take("embedding-model")?;
ensure!(!embedding_model.is_empty(), "--embedding-model must name a model");
let bind: SocketAddr = bind.parse().with_context(|| format!("--bind must be an ip:port, got {bind:?}"))?;
let persona_bind = match values.get("persona-bind") {
None => None,
Some(door) => {
let door: SocketAddr = door.parse().with_context(|| format!("--persona-bind must be an ip:port, got {door:?}"))?;
ensure!(door != bind, "--persona-bind must not be --bind: the persona door is its own listener");
Some(door)
}
};
Ok(Options {
state_root,
instance: Slug(take("instance")?),
bind: bind.parse().with_context(|| format!("--bind must be an ip:port, got {bind:?}"))?,
bind,
persona_bind,
qdrant_url: url("qdrant-url")?,
ollama_urls: {
ensure!(!ollama_urls.is_empty(), "--ollama-url is required; repeat it for each GPU host");
@ -206,6 +220,7 @@ where
{
let mind = Mind::open(&options.state_root, &options.instance)?;
let hub = bind(options.bind, &runtime_id(&options.instance))?;
let persona = options.persona_bind.map(|door| bind(door, &runtime_id(&options.instance))).transpose()?;
let desk = options.desk.as_ref().map(|desk| bind_desk(&desk.socket, desk.peer_uid)).transpose()?;
let startup_entries = mind.index_entries(None)?;
let sink = WorkerSink::spawn(embedder, index, &options.instance, mind.genesis_receipt_id()?, startup_entries, backoff);
@ -214,14 +229,16 @@ where
Some(url) => daemon.with_forge(crate::sources::forge::HttpForge::new(url)),
None => daemon,
};
Ok(Started { daemon, hub, registry: schema_registry()?, desk })
Ok(Started { daemon, hub, persona, registry: schema_registry()?, desk })
}
/// What `startup` hands the loop: the daemon over the opened mind, the bound RUDP hub, the
/// catalog the RUDP door advertises, and the desk door when one was asked for.
/// persona door's hub when one was asked for, the catalog both RUDP doors advertise, and the
/// desk door when one was asked for.
pub struct Started {
pub daemon: Daemon<OwnedRedbMessagePackBackingStore, WorkerSink>,
pub hub: CultNetRudpServerHub,
pub persona: Option<CultNetRudpServerHub>,
pub registry: CultNetSchemaRegistry,
pub desk: Option<DeskDoor>,
}
@ -321,12 +338,14 @@ impl Routed {
}
/// One message in, one message out, or a search to wait for. Nothing here
/// reaches a mind except through `Daemon::handle`, and an envelope that does
/// reaches a mind except through `Daemon::handle_at`, at the `door` the message
/// arrived on, and an envelope that does
/// not decode never gets that far. A chunk request is answered from `bodies`
/// alone, on the session it came in on; every operation's reply leaves through
/// `respond`.
pub fn answer<S: MindStore, I: IndexSink<S>>(
daemon: &mut Daemon<S, I>,
door: Door,
registry: &CultNetSchemaRegistry,
bodies: &mut DeferredBodies,
message: CultNetMessage,
@ -345,7 +364,7 @@ pub fn answer<S: MindStore, I: IndexSink<S>>(
CultNetMessage::OperationRequest { message_id, operation, .. } => match decode_request(&message) {
Ok((message_id, request)) => {
let operation = request.operation();
match daemon.handle(request, now) {
match daemon.handle_at(door, request, now) {
Handled::Answered(response) => respond(&response, &message_id, operation, &runtime_id, bodies, now),
Handled::Searching(search) => return Routed::Search { message_id, operation, search },
}
@ -483,9 +502,11 @@ fn deliver(
deferred
}
/// A semantic query on its way through the index: who asked, what to call the
/// answer, the search to finish, and when the client stops being promised one.
/// A semantic query on its way through the index: who asked, at which door,
/// what to call the answer, the search to finish, and when the client stops
/// being promised one.
struct Waiting {
door: Door,
session: CultNetRudpServerSessionContext,
message_id: String,
operation: &'static str,
@ -494,9 +515,11 @@ struct Waiting {
}
/// Until `stopping`: expire what timed out, resend what was not acknowledged,
/// answer every frame waiting on the socket, then answer every semantic query
/// the index has finished or that has waited out the index's own
/// `search_deadline`. A semantic
/// answer every frame waiting on either RUDP door's socket, then answer every
/// semantic query the index has finished or that has waited out the index's
/// own `search_deadline`. A frame on `hub` is asked at `Door::Full`, one on
/// `persona` at `Door::Persona`, and a search's answer leaves by the door it
/// came in at. A semantic
/// query is never answered inline: `answer` hands back a `Search`, the loop
/// holds it beside its session and goes on serving, and the embedder and the
/// vector store are the worker's to wait on. A hostile datagram and a departed
@ -505,51 +528,18 @@ struct Waiting {
pub fn run<S: MindStore, I: IndexSink<S>>(
daemon: &mut Daemon<S, I>,
hub: &mut CultNetRudpServerHub,
mut persona: Option<&mut CultNetRudpServerHub>,
registry: &CultNetSchemaRegistry,
mut desk: Option<&mut DeskDoor>,
stopping: &AtomicBool,
options: &ServeOptions,
) -> Result<()> {
let timeout_ms = options.session_timeout.as_millis() as u64;
let mut bodies = DeferredBodies::new(DEFERRED_BUDGET_BYTES, options.deferred_ttl);
let mut waiting: BTreeMap<SearchTicket, Waiting> = BTreeMap::new();
while !stopping.load(Ordering::Relaxed) {
hub.remove_timed_out_sessions(timeout_ms);
if let Err(error) = hub.poll_resends() {
eprintln!("huginn: resending to a peer failed: {error:#}");
}
let mut served = 0_u32;
loop {
let event = match hub.receive_event_once() {
Ok(Some(event)) => event,
Ok(None) => break,
Err(error) => {
eprintln!("huginn: a datagram was not readable and was discarded: {error:#}");
break;
}
};
let CultNetRudpServerEvent::Frame { session, frame } = event else {
continue;
};
if frame.channel_id != "schema" {
continue;
}
let message = match decode_cultnet_message_from_slice(&frame.payload, CultNetWireContract::CultNetSchemaV0)
{
Ok(message) => message,
Err(error) => {
eprintln!("huginn: a frame was not a CultNet message and was discarded: {error:#}");
continue;
}
};
match answer(daemon, registry, &mut bodies, message, Utc::now()) {
Routed::Reply(reply) => send(hub, &session, &reply),
Routed::Search { message_id, operation, search } => {
let until = Instant::now() + daemon.search_deadline();
waiting.insert(search.ticket, Waiting { session, message_id, operation, search, until });
}
}
served += 1;
let mut served = serve_frames(daemon, hub, Door::Full, registry, &mut bodies, &mut waiting, options);
if let Some(door) = persona.as_deref_mut() {
served += serve_frames(daemon, door, Door::Persona, registry, &mut bodies, &mut waiting, options);
}
let desk_served = desk.as_deref_mut().map_or(0, |door| door.serve_ready(daemon, Utc::now()));
let finished = collect_searches(daemon, &waiting);
@ -558,7 +548,14 @@ pub fn run<S: MindStore, I: IndexSink<S>>(
let Some(asked) = waiting.remove(&ticket) else { continue };
let response = daemon.finish(asked.search, found);
let reply = respond(&response, &asked.message_id, asked.operation, &daemon.runtime_id(), &mut bodies, Utc::now());
send(hub, &asked.session, &reply);
let out = match asked.door {
Door::Full => &mut *hub,
Door::Persona => match persona.as_deref_mut() {
Some(door) => door,
None => continue,
},
};
send(out, &asked.session, &reply);
}
if served == 0 && desk_served == 0 && !answered {
std::thread::sleep(options.idle_sleep);
@ -567,6 +564,57 @@ pub fn run<S: MindStore, I: IndexSink<S>>(
Ok(())
}
/// One pass over one RUDP door: expire its idle sessions, resend what it has
/// unacknowledged, and answer every frame waiting on its socket as asked at
/// `door`. Returns how many frames it answered or queued.
fn serve_frames<S: MindStore, I: IndexSink<S>>(
daemon: &mut Daemon<S, I>,
hub: &mut CultNetRudpServerHub,
door: Door,
registry: &CultNetSchemaRegistry,
bodies: &mut DeferredBodies,
waiting: &mut BTreeMap<SearchTicket, Waiting>,
options: &ServeOptions,
) -> u32 {
hub.remove_timed_out_sessions(options.session_timeout.as_millis() as u64);
if let Err(error) = hub.poll_resends() {
eprintln!("huginn: resending to a peer failed: {error:#}");
}
let mut served = 0_u32;
loop {
let event = match hub.receive_event_once() {
Ok(Some(event)) => event,
Ok(None) => break,
Err(error) => {
eprintln!("huginn: a datagram was not readable and was discarded: {error:#}");
break;
}
};
let CultNetRudpServerEvent::Frame { session, frame } = event else {
continue;
};
if frame.channel_id != "schema" {
continue;
}
let message = match decode_cultnet_message_from_slice(&frame.payload, CultNetWireContract::CultNetSchemaV0) {
Ok(message) => message,
Err(error) => {
eprintln!("huginn: a frame was not a CultNet message and was discarded: {error:#}");
continue;
}
};
match answer(daemon, door, registry, bodies, message, Utc::now()) {
Routed::Reply(reply) => send(hub, &session, &reply),
Routed::Search { message_id, operation, search } => {
let until = Instant::now() + daemon.search_deadline();
waiting.insert(search.ticket, Waiting { door, session, message_id, operation, search, until });
}
}
served += 1;
}
served
}
/// What the index has finished, then what has waited too long: the latter as
/// an `Unavailable` detail, and abandoned, so the index does not start a search
/// nobody is waiting for. A search that finishes after its deadline finds
@ -680,14 +728,14 @@ mod tests {
("m-5b", request("m-5b", MIND_SERVICE_ID, "Admit", MIND_REQUEST_SCHEMA, admit), "operation-mismatch"),
];
for (id, message, code) in cases {
let reply = answer(&mut daemon, &registry, &mut fresh_bodies(), message, now()).reply();
let reply = answer(&mut daemon, Door::Full, &registry, &mut fresh_bodies(), message, now()).reply();
assert_eq!(rejected(&reply).0, id, "the client's own correlation key is echoed");
assert_eq!(rejected(&reply).1, code);
}
// The sixth code, `not-an-operation-request`, is the codec's and is
// pinned in `huginn_mind::envelope::tests`: `answer` never produces it, because a
// message of another family is answered with `Error` instead.
let reply = answer(&mut daemon, &registry, &mut fresh_bodies(), CultNetMessage::Error { error: "hello".into(), code: None, details: None }, now()).reply();
let reply = answer(&mut daemon, Door::Full, &registry, &mut fresh_bodies(), CultNetMessage::Error { error: "hello".into(), code: None, details: None }, now()).reply();
let CultNetMessage::Error { error, .. } = &reply else { panic!("expected an error, got {reply:?}") };
assert!(
!error.is_empty() && error.contains("cultnet.operation_request.v0") && error.contains("cultnet.schema_catalog_request.v0"),
@ -699,7 +747,7 @@ mod tests {
// A mind's refusal is an answer on the response schema, not a failure
// of the envelope: the client decodes it as the typed refusal it is.
let read = HuginnMindRequest::Query { instance: slug(OTHER), selection: Selection::default(), semantic: None };
let reply = answer(&mut daemon, &registry, &mut fresh_bodies(), encode_request("m-r", &read, None).unwrap(), now()).reply();
let reply = answer(&mut daemon, Door::Full, &registry, &mut fresh_bodies(), encode_request("m-r", &read, None).unwrap(), now()).reply();
let CultNetMessage::OperationResponse { status, payload_schema, .. } = &reply else {
panic!("expected an operation response, got {reply:?}");
};
@ -717,7 +765,7 @@ mod tests {
schema_ids: None,
kinds: None,
};
let reply = answer(&mut daemon, &registry, &mut fresh_bodies(), catalog, now()).reply();
let reply = answer(&mut daemon, Door::Full, &registry, &mut fresh_bodies(), catalog, now()).reply();
let CultNetMessage::SchemaCatalogResponse { message_id, schemas } = reply else {
panic!("expected a catalog response");
};
@ -785,7 +833,7 @@ mod tests {
let loop_stopping = Arc::clone(&stopping);
let serving = std::thread::spawn(move || {
let result = run(&mut daemon, &mut hub, &registry, None, &loop_stopping, &ServeOptions::default());
let result = run(&mut daemon, &mut hub, None, &registry, None, &loop_stopping, &ServeOptions::default());
(result, daemon)
});
@ -874,6 +922,43 @@ mod tests {
panic!("{name} never connected");
}
/// Over loopback, the persona door's listener refuses an admission by name and answers a
/// read, and the full door's listener admits the same batch: only that admission lands.
#[test]
fn the_persona_listener_refuses_an_admission_the_full_listener_takes() {
let root = tempfile::tempdir().unwrap();
let mut daemon = open_unindexed(root.path(), &slug(INSTANCE)).unwrap();
let mut hub = bind("127.0.0.1:0".parse().unwrap(), &daemon.runtime_id()).unwrap();
let mut door = bind("127.0.0.1:0".parse().unwrap(), &daemon.runtime_id()).unwrap();
let full = format!("rudp://{}", hub.local_addr().unwrap());
let persona = format!("rudp://{}", door.local_addr().unwrap());
let registry = schema_registry().unwrap();
let stopping = Arc::new(AtomicBool::new(false));
let loop_stopping = Arc::clone(&stopping);
let serving = std::thread::spawn(move || {
let result = run(&mut daemon, &mut hub, Some(&mut door), &registry, None, &loop_stopping, &ServeOptions::default());
(result, daemon)
});
let admit = HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)]));
let mut her = connect(&persona, "persona");
assert_eq!(ask(&mut her, "p-admit", &admit), HuginnMindResponse::Refused(MindRefusal::NotThisDoor));
let HuginnMindResponse::Whoami(status) = ask(&mut her, "p-whoami", &HuginnMindRequest::Whoami) else {
panic!("the persona door answers whoami")
};
assert_eq!(status.documents, 0, "the refused admission wrote nothing");
let mut self_faculty = connect(&full, "full");
assert!(matches!(
ask(&mut self_faculty, "f-admit", &admit),
HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { .. })
));
stopping.store(true, Ordering::Relaxed);
let (result, mut daemon) = serving.join().unwrap();
result.unwrap();
assert_eq!(documents(&mut daemon), 1, "only the full door's admission landed");
}
/// One client admits batches that close, withdraw and re-open records while three others read
/// the same mind: fresh queries, cursor walks across commits, a hop and whoami. The loop is
/// serial, so every answer is one snapshot's; at quiesce each answer must equal what a fresh
@ -892,7 +977,7 @@ mod tests {
let stopping = Arc::new(AtomicBool::new(false));
let loop_stopping = Arc::clone(&stopping);
let serving = std::thread::spawn(move || {
let result = run(&mut daemon, &mut hub, &registry, None, &loop_stopping, &ServeOptions::default());
let result = run(&mut daemon, &mut hub, None, &registry, None, &loop_stopping, &ServeOptions::default());
(result, daemon)
});
@ -1057,7 +1142,7 @@ mod tests {
at: DateTime<Utc>,
) -> CultMeshCdnArtifactManifest {
let registry = schema_registry().unwrap();
deferred_manifest(&answer(daemon, &registry, bodies, encode_request(message_id, read, None).unwrap(), at).reply())
deferred_manifest(&answer(daemon, Door::Full, &registry, bodies, encode_request(message_id, read, None).unwrap(), at).reply())
}
/// Whether the first chunk of a manifest is still served at an injected time.
@ -1068,7 +1153,7 @@ mod tests {
at: DateTime<Utc>,
) -> bool {
let registry = schema_registry().unwrap();
found(&answer(daemon, &registry, bodies, chunk_request(manifest, 0), at).reply())
found(&answer(daemon, Door::Full, &registry, bodies, chunk_request(manifest, 0), at).reply())
}
/// An answer larger than one send can carry is deferred to the body plane
@ -1133,7 +1218,7 @@ mod tests {
let stopping = Arc::new(AtomicBool::new(false));
let loop_stopping = Arc::clone(&stopping);
let serving = std::thread::spawn(move || {
let result = run(&mut daemon, &mut hub, &registry, None, &loop_stopping, &ServeOptions::default());
let result = run(&mut daemon, &mut hub, None, &registry, None, &loop_stopping, &ServeOptions::default());
(result, daemon)
});
@ -1381,7 +1466,7 @@ mod tests {
let manifest = ask_deferred(&mut daemon, &mut bodies, "m-v", &read, t0);
assert!(manifest.chunks.len() >= 2, "the fixture needs a second chunk to serve");
let mut chunk = |index: usize, at: i64| {
answer(&mut daemon, &registry, &mut bodies, chunk_request(&manifest, index), after(at)).reply()
answer(&mut daemon, Door::Full, &registry, &mut bodies, chunk_request(&manifest, index), after(at)).reply()
};
assert!(found(&chunk(0, 59)), "inside the TTL");
assert!(found(&chunk(1, 118)), "serving a chunk slid the expiry past the first TTL");
@ -1394,7 +1479,7 @@ mod tests {
let again = ask_deferred(&mut daemon, &mut bodies, "m-v2", &read, after(179));
assert_eq!(again.content_hash, manifest.content_hash, "the same answer is the same body");
let bytes = fetch_content(&again, MAX_DEFERRED_BODY_BYTES, |request| {
Ok(answer(&mut daemon, &registry, &mut bodies, request, after(180)).reply())
Ok(answer(&mut daemon, Door::Full, &registry, &mut bodies, request, after(180)).reply())
})
.unwrap();
assert_eq!(bytes, expected);
@ -1494,7 +1579,7 @@ mod tests {
("record_key", chunk("m", "abc", &huge)),
];
for (case, message) in refused {
let reply = answer(&mut daemon, &registry, &mut bodies, message, now()).reply();
let reply = answer(&mut daemon, Door::Full, &registry, &mut bodies, message, now()).reply();
let CultNetMessage::Error { error, .. } = &reply else {
panic!("{case}: expected a transport error, got {reply:?}");
};
@ -1503,9 +1588,9 @@ mod tests {
}
assert_eq!(documents(&mut daemon), 0);
let reply = answer(&mut daemon, &registry, &mut bodies, whoami(&at, MIND_SERVICE_ID, "whoami", MIND_REQUEST_SCHEMA), now()).reply();
let reply = answer(&mut daemon, Door::Full, &registry, &mut bodies, whoami(&at, MIND_SERVICE_ID, "whoami", MIND_REQUEST_SCHEMA), now()).reply();
assert_eq!(decode_response(&reply).unwrap().0, at, "a field of exactly the bound is served");
let reply = answer(&mut daemon, &registry, &mut bodies, chunk(&at, &at, &at), now()).reply();
let reply = answer(&mut daemon, Door::Full, &registry, &mut bodies, chunk(&at, &at, &at), now()).reply();
assert!(!found(&reply), "an unknown chunk is answered found: false, not refused at the door");
}
@ -1565,6 +1650,7 @@ mod tests {
state_root: root.path().to_path_buf(),
instance: slug(INSTANCE),
bind: port,
persona_bind: Some("127.0.0.1:0".parse().unwrap()),
qdrant_url: "http://127.0.0.1:1".into(),
ollama_urls: vec!["http://127.0.0.1:1".into()],
reserve_ollama_urls: Vec::new(),
@ -1588,6 +1674,8 @@ mod tests {
drop(occupied);
let started = started(&options).expect("both gates open");
assert_eq!(started.hub.local_addr().unwrap(), port);
let door = started.persona.as_ref().expect("the persona door was asked for and bound");
assert_ne!(door.local_addr().unwrap(), port, "the persona door is its own listener");
}
#[test]
@ -1604,6 +1692,7 @@ mod tests {
state_root: PathBuf::from(root),
instance: slug("yggdrasil"),
bind: "127.0.0.1:17872".parse().unwrap(),
persona_bind: None,
qdrant_url: "http://127.0.0.1:6333".into(),
ollama_urls: vec!["http://10.77.0.4:11434".into()],
reserve_ollama_urls: Vec::new(),
@ -1647,5 +1736,11 @@ mod tests {
assert_eq!(parse_options(args(&forged)).unwrap().forge_url.as_deref(), Some("http://127.0.0.1:3310"));
assert!(parse_options(args(&[good.to_vec(), vec!["--forge-url", "https://127.0.0.1:3310"]].concat())).is_err());
assert!(parse_options(args(&[forged.clone(), vec!["--forge-url", "http://127.0.0.1:3311"]].concat())).is_err());
// The persona door is optional, once, an ip:port, and never the full door's.
let door = [good.to_vec(), vec!["--persona-bind", "127.0.0.1:17862"]].concat();
assert_eq!(parse_options(args(&door)).unwrap().persona_bind, Some("127.0.0.1:17862".parse().unwrap()));
assert!(parse_options(args(&[door.clone(), vec!["--persona-bind", "127.0.0.1:17863"]].concat())).is_err());
assert!(parse_options(args(&[good.to_vec(), vec!["--persona-bind", "not-an-address"]].concat())).is_err());
assert!(parse_options(args(&[good.to_vec(), vec!["--persona-bind", "127.0.0.1:17872"]].concat())).is_err());
}
}

View file

@ -549,13 +549,13 @@ fn a_busy_pass_is_not_followed_by_the_idle_sleep() {
];
let options = parse_options(args.iter().map(|arg| arg.to_string())).unwrap();
let embedder = pool(&options.ollama_urls, &options.reserve_ollama_urls, &options.embedding_model);
let Started { mut daemon, mut hub, registry, desk: mut door } =
let Started { mut daemon, mut hub, persona: _, registry, desk: mut door } =
startup(&options, embedder, QdrantIndex::new(&options.qdrant_url), Backoff::default()).unwrap();
let stopping = Arc::new(AtomicBool::new(false));
let loop_stopping = Arc::clone(&stopping);
let serving = std::thread::spawn(move || {
let serve = ServeOptions { idle_sleep: Duration::from_millis(600), ..ServeOptions::default() };
run(&mut daemon, &mut hub, &registry, door.as_mut(), &loop_stopping, &serve)
run(&mut daemon, &mut hub, None, &registry, door.as_mut(), &loop_stopping, &serve)
});
std::thread::sleep(Duration::from_millis(100));
let frame = framed(&catalog_request());

View file

@ -2792,13 +2792,16 @@ mod tests {
/// The upgrade gate on a real store (ruling readback-gate-and-materialized-image):
/// the one function `Mind::read_back` over a scratch copy of a state root.
/// Opens the store read-write, so run it on a copy, never the live file.
/// Measured about 30 s in debug at 11,328 documents.
/// `HUGINN_MIND_INSTANCE` names the instance whose store it opens, `eureka`
/// when unset, so every Mind's store is gated the same way before a release
/// moves her daemon. Measured about 30 s in debug at 11,328 documents.
#[test]
#[ignore = "needs HUGINN_MIND_SNAPSHOT, a scratch copy of a Huginn state root"]
fn stored_documents_read_back() {
let root = std::env::var("HUGINN_MIND_SNAPSHOT").expect("HUGINN_MIND_SNAPSHOT names a scratch copy of the state root");
let instance = std::env::var("HUGINN_MIND_INSTANCE").unwrap_or_else(|_| "eureka".into());
let started = std::time::Instant::now();
let mind = Mind::open(std::path::Path::new(&root), &Slug("eureka".into())).unwrap();
let mind = Mind::open(std::path::Path::new(&root), &Slug(instance)).unwrap();
let count = mind.read_back().unwrap();
println!("read back {count} stored documents in {:?}", started.elapsed());
}

View file

@ -117,6 +117,10 @@ pub enum MindRefusal {
/// reached.
CursorInvalid { message: String },
Unavailable { detail: String },
/// The request reached the persona door, which answers reads and the
/// mind's own persona document and nothing that writes the mind
/// (`HuginnMindRequest::reaches_persona_door`). Nothing was written.
NotThisDoor,
}
impl std::fmt::Display for MindRefusal {

View file

@ -430,6 +430,28 @@ impl HuginnMindRequest {
}
}
/// Whether the persona door answers this request. The persona door is the
/// daemon's second listener, the only one a Persona's uid can reach: it
/// serves the mind's reads and her own persona document, and refuses
/// every write to the mind (`MindRefusal::NotThisDoor`). The match has no
/// wildcard, so a new request does not compile until it is classified
/// here.
pub fn reaches_persona_door(&self) -> bool {
match self {
Self::Whoami
| Self::View { .. }
| Self::Query { .. }
| Self::PersonaGet { .. }
| Self::PersonaPut { .. }
| Self::SearchSources { .. }
| Self::SearchChat { .. }
| Self::ChatWindow { .. }
| Self::StewardedActivity { .. }
| Self::RootsTouched { .. } => true,
Self::Admit(_) => false,
}
}
/// The instance the request declares; `Whoami` declares none.
pub fn instance(&self) -> Option<&Slug> {
match self {
@ -633,6 +655,7 @@ mod tests {
scores: Some(vec![0.5]),
}),
HuginnMindResponse::Refused(refusal),
HuginnMindResponse::Refused(MindRefusal::NotThisDoor),
HuginnMindResponse::Persona(None),
HuginnMindResponse::Persona(Some(PersonaStateView {
value: serde_json::json!({ "personaId": INSTANCE }),

View file

@ -1656,6 +1656,11 @@
"required": [
"Unavailable"
]
},
{
"description": "The request reached the persona door, which answers reads and the\nmind's own persona document and nothing that writes the mind\n(`HuginnMindRequest::reaches_persona_door`). Nothing was written.",
"type": "string",
"const": "NotThisDoor"
}
]
},

View file

@ -2302,6 +2302,11 @@
"required": [
"Unavailable"
]
},
{
"description": "The request reached the persona door, which answers reads and the\nmind's own persona document and nothing that writes the mind\n(`HuginnMindRequest::reaches_persona_door`). Nothing was written.",
"type": "string",
"const": "NotThisDoor"
}
]
},