Serve one mind over CultNet, and no rule beside it
`huginn-daemon` becomes a library and a binary. `Daemon` holds one mind and dispatches a `HuginnMindRequest` to the `Mind` method that answers it, with no socket and no clock: `now` is the caller's. Every read that names an instance is put to `Mind::require_instance` before the method runs, so ruling 14 is checked once, by its owner, whichever side of the wire the request came from. `IndexSink` is where Cut 11 attaches; its error is logged and the outcome is what the mind decided. `envelope` carries the vocabulary in CultNet's `cultnet.operation_request.v0` and `cultnet.operation_response.v0` as base64 of named MessagePack, which is what the C# reference and Eve's plugin ABI speak. The envelope's operation string is checked against the decoded payload rather than trusted. A message that does not decode is answered with a typed failure naming its code, and reaches no mind. `serve` binds one non-blocking socket on CultNet's operation connection id and answers each frame on the session it arrived on. A refusal is an answer with a `rejected` status, never a transport error. The two wire schemas go out on a schema catalog request. A hostile datagram and a departed peer are logged and served past. `startup` is the one place the mind opens and the socket binds, in that order, so a mind that will not open leaves the port free. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
c4b2a0fa2e
commit
6b619d6bbd
7 changed files with 1600 additions and 4 deletions
|
|
@ -5,4 +5,28 @@ edition.workspace = true
|
|||
license.workspace = true
|
||||
publish.workspace = true
|
||||
|
||||
# The organ's CultNet surface: the socket, the sessions, the process lifetime
|
||||
# and the operation envelope, and no rule. Every rule, the instance check, the
|
||||
# receipt, the status and the wire vocabulary are `huginn-mind`'s, and the leaf
|
||||
# and the store type are reached through it so one crate pins one revision.
|
||||
[lib]
|
||||
name = "huginn_daemon"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "huginn-daemon"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
base64 = "0.22"
|
||||
chrono = "0.4.44"
|
||||
cultnet-rs = { git = "https://github.com/GameCult/CultLib.git", rev = "a0813c6eed24d30bf88073ef615b633c77ebfcd6", package = "cultnet-rs" }
|
||||
huginn-mind = { path = "../huginn-mind" }
|
||||
rmp-serde = "1"
|
||||
schemars = "1"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
signal-hook = "0.3"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
|
|
|||
377
crates/huginn-daemon/src/daemon.rs
Normal file
377
crates/huginn-daemon/src/daemon.rs
Normal file
|
|
@ -0,0 +1,377 @@
|
|||
//! The dispatch: one mind, one request, one response. No socket, no clock, no
|
||||
//! rule. `now` is the caller's, so a test pins it and the loop is the only
|
||||
//! thing that reads a wall clock.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use huginn_mind::epiphany_pipeline::{PipelineRef, Slug};
|
||||
use huginn_mind::wire::{HuginnMindRequest, HuginnMindResponse};
|
||||
use huginn_mind::{Mind, MindRefusal, MindStore, OwnedRedbMessagePackBackingStore, PipelineAdmissionOutcome};
|
||||
|
||||
/// Where Cut 11's index attaches. Called after a batch commits, with the refs
|
||||
/// the batch landed, derived writes included; the sink may read each through
|
||||
/// `mind.view`. Its error is logged and never reaches the caller: the index
|
||||
/// does not decide whether a batch was admitted.
|
||||
pub trait IndexSink<S: MindStore> {
|
||||
fn committed(&mut self, mind: &Mind<S>, writes: &[PipelineRef]) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
/// This cut's index: there is none.
|
||||
pub struct NoIndex;
|
||||
|
||||
impl<S: MindStore> IndexSink<S> for NoIndex {
|
||||
fn committed(&mut self, _mind: &Mind<S>, _writes: &[PipelineRef]) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// One mind and whatever indexes what lands in it.
|
||||
pub struct Daemon<S: MindStore, I: IndexSink<S>> {
|
||||
mind: Mind<S>,
|
||||
index: I,
|
||||
}
|
||||
|
||||
impl Daemon<OwnedRedbMessagePackBackingStore, NoIndex> {
|
||||
/// Opens the instance's mind, taking the store's exclusive lock for the
|
||||
/// daemon's lifetime. Ruling 15: a mind that will not open is a refusal
|
||||
/// here, before any socket exists; this function takes no address and
|
||||
/// cannot bind one.
|
||||
pub fn open(state_root: &Path, instance: &Slug) -> Result<Self, MindRefusal> {
|
||||
Ok(Self { mind: Mind::open(state_root, instance)?, index: NoIndex })
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: MindStore, I: IndexSink<S>> Daemon<S, I> {
|
||||
pub fn with_index<J: IndexSink<S>>(self, index: J) -> Daemon<S, J> {
|
||||
Daemon { mind: self.mind, index }
|
||||
}
|
||||
|
||||
pub fn mind(&self) -> &Mind<S> {
|
||||
&self.mind
|
||||
}
|
||||
|
||||
/// The envelope's `source_runtime_id` on every response this daemon sends.
|
||||
pub fn runtime_id(&self) -> String {
|
||||
format!("huginn-{}", self.mind.instance().0)
|
||||
}
|
||||
|
||||
/// One request, one answer. `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, and the mind is asked whether that is
|
||||
/// its own before the method runs.
|
||||
pub fn handle(&mut self, request: HuginnMindRequest, now: DateTime<Utc>) -> HuginnMindResponse {
|
||||
if let Some(declared) = request.instance()
|
||||
&& !matches!(request, HuginnMindRequest::Admit(_))
|
||||
&& let Err(refusal) = self.mind.require_instance(declared)
|
||||
{
|
||||
return HuginnMindResponse::Refused(refusal);
|
||||
}
|
||||
match request {
|
||||
HuginnMindRequest::Whoami => HuginnMindResponse::Whoami(self.mind.status()),
|
||||
HuginnMindRequest::Admit(batch) => {
|
||||
let outcome = self.mind.admit(batch, now);
|
||||
if let PipelineAdmissionOutcome::Committed { writes, .. } = &outcome
|
||||
&& let Err(error) = self.index.committed(&self.mind, writes)
|
||||
{
|
||||
let refs = writes.iter().map(|write| write.id.0.clone()).collect::<Vec<_>>().join(", ");
|
||||
eprintln!("huginn: the index refused the landed writes [{refs}]: {error:#}");
|
||||
}
|
||||
HuginnMindResponse::Admit(outcome)
|
||||
}
|
||||
HuginnMindRequest::View { id, .. } => match self.mind.view(&id) {
|
||||
Ok(view) => HuginnMindResponse::View(view),
|
||||
Err(refusal) => HuginnMindResponse::Refused(refusal),
|
||||
},
|
||||
HuginnMindRequest::Query { query, .. } => match self.mind.query(&query) {
|
||||
Ok(page) => HuginnMindResponse::Query(page),
|
||||
Err(refusal) => HuginnMindResponse::Refused(refusal),
|
||||
},
|
||||
HuginnMindRequest::OpenItems { campaign, .. } => match self.mind.open_items(&campaign) {
|
||||
Ok(items) => HuginnMindResponse::OpenItems(items),
|
||||
Err(refusal) => HuginnMindResponse::Refused(refusal),
|
||||
},
|
||||
HuginnMindRequest::History { scope, .. } => match self.mind.history(&scope) {
|
||||
Ok(views) => HuginnMindResponse::History(views),
|
||||
Err(refusal) => HuginnMindResponse::Refused(refusal),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use chrono::TimeZone;
|
||||
use huginn_mind::epiphany_pipeline::{
|
||||
Date, OrgRepo, PipelineDocument, PipelineInstance, PipelineKind, PipelineQuestion, PipelineRuling,
|
||||
QuestionOption, RulingAuthority, Short,
|
||||
};
|
||||
use huginn_mind::wire::MindStatus;
|
||||
use huginn_mind::{
|
||||
Faculty, HistoryScope, PipelineAdmissionBatch, PipelineProvenance, PipelineQuery, PipelineStatus,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tempfile::TempDir;
|
||||
|
||||
pub(crate) const INSTANCE: &str = "yggdrasil";
|
||||
pub(crate) const OTHER: &str = "thought-cage";
|
||||
pub(crate) const CAMPAIGN: &str = "eureka-state";
|
||||
|
||||
pub(crate) fn slug(value: &str) -> Slug {
|
||||
value.into()
|
||||
}
|
||||
|
||||
pub(crate) fn now() -> DateTime<Utc> {
|
||||
Utc.with_ymd_and_hms(2026, 9, 16, 12, 0, 0).unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn provenance() -> PipelineProvenance {
|
||||
PipelineProvenance {
|
||||
faculty: Faculty::Hands,
|
||||
agent: Short("claude".into()),
|
||||
session: Short("session-1".into()),
|
||||
tool: Short("admit".into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn identity(name: &str) -> PipelineDocument {
|
||||
PipelineDocument::Instance(PipelineInstance {
|
||||
instance: slug(name),
|
||||
display_name: Short(format!("{name} mind")),
|
||||
created_at: Date("2026-09-16".into()),
|
||||
host: Short(name.into()),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn batch(instance: &str, documents: Vec<PipelineDocument>) -> PipelineAdmissionBatch {
|
||||
PipelineAdmissionBatch { instance: slug(instance), provenance: provenance(), documents }
|
||||
}
|
||||
|
||||
/// A question and the ruling that answers it: admitting the ruling derives
|
||||
/// the question's resolution, so one batch lands two writes.
|
||||
pub(crate) fn question_and_ruling() -> (PipelineDocument, PipelineDocument) {
|
||||
let question = PipelineDocument::Question(PipelineQuestion {
|
||||
campaign: slug(CAMPAIGN),
|
||||
label: "Q1".into(),
|
||||
question: "Who owns the state?".into(),
|
||||
options: vec![
|
||||
QuestionOption { label: "A".into(), text: "the organ".into() },
|
||||
QuestionOption { label: "B".into(), text: "the client".into() },
|
||||
],
|
||||
recommended: "A".into(),
|
||||
depends: vec![],
|
||||
raised_in: None,
|
||||
asked_on: Date("2026-09-16".into()),
|
||||
});
|
||||
let ruling = PipelineDocument::Ruling(PipelineRuling {
|
||||
campaign: slug(CAMPAIGN),
|
||||
label: "R1".into(),
|
||||
answers: Some(Short(format!("{CAMPAIGN}:question:Q1"))),
|
||||
choice: Some("A".into()),
|
||||
ruling: "An instance owns its mind.".into(),
|
||||
operator_quote: None,
|
||||
ruled_on: Date("2026-09-16".into()),
|
||||
precedents: vec![],
|
||||
authority: RulingAuthority::Operator,
|
||||
});
|
||||
(question, ruling)
|
||||
}
|
||||
|
||||
/// A daemon over a real redb mind in a temporary directory, with its
|
||||
/// identity admitted. The directory is returned because dropping it
|
||||
/// removes the store.
|
||||
pub(crate) fn seeded() -> (TempDir, Daemon<OwnedRedbMessagePackBackingStore, NoIndex>) {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mut daemon = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let outcome = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now());
|
||||
assert!(
|
||||
matches!(outcome, HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { .. })),
|
||||
"{outcome:?}"
|
||||
);
|
||||
(root, daemon)
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct RecordingIndex {
|
||||
seen: Arc<Mutex<Vec<(Vec<String>, Vec<bool>)>>>,
|
||||
}
|
||||
|
||||
impl<S: MindStore> IndexSink<S> for RecordingIndex {
|
||||
fn committed(&mut self, mind: &Mind<S>, writes: &[PipelineRef]) -> anyhow::Result<()> {
|
||||
let ids = writes.iter().map(|write| write.id.0.clone()).collect();
|
||||
let readable = writes.iter().map(|write| mind.view(write).unwrap().is_some()).collect();
|
||||
self.seen.lock().unwrap().push((ids, readable));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct FailingIndex;
|
||||
|
||||
impl<S: MindStore> IndexSink<S> for FailingIndex {
|
||||
fn committed(&mut self, _mind: &Mind<S>, _writes: &[PipelineRef]) -> anyhow::Result<()> {
|
||||
Err(anyhow::anyhow!("the index is not there"))
|
||||
}
|
||||
}
|
||||
|
||||
fn status(daemon: &mut Daemon<OwnedRedbMessagePackBackingStore, impl IndexSink<OwnedRedbMessagePackBackingStore>>) -> MindStatus {
|
||||
match daemon.handle(HuginnMindRequest::Whoami, now()) {
|
||||
HuginnMindResponse::Whoami(status) => status,
|
||||
other => panic!("expected a status, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Typed hand-off between the organs without a socket: a batch enters and
|
||||
/// every read answers over what it landed, at a clock the test pins.
|
||||
#[test]
|
||||
fn a_batch_round_trips_typed_through_handle_without_a_socket() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mut daemon = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let outcome = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now());
|
||||
let HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { writes, .. }) = outcome else {
|
||||
panic!("expected a commit");
|
||||
};
|
||||
assert_eq!(writes.len(), 1);
|
||||
assert_eq!(writes[0].kind, PipelineKind::Instance);
|
||||
|
||||
assert_eq!(
|
||||
status(&mut daemon),
|
||||
MindStatus {
|
||||
instance: slug(INSTANCE),
|
||||
schema_epoch: "epiphany.pipeline.epoch.v1".into(),
|
||||
documents: 1,
|
||||
receipts: 1,
|
||||
}
|
||||
);
|
||||
assert_eq!(daemon.runtime_id(), "huginn-yggdrasil");
|
||||
|
||||
let query = PipelineQuery { kinds: vec![PipelineKind::Instance], ..PipelineQuery::default() };
|
||||
let HuginnMindResponse::Query(page) =
|
||||
daemon.handle(HuginnMindRequest::Query { instance: slug(INSTANCE), query }, now())
|
||||
else {
|
||||
panic!("expected a page");
|
||||
};
|
||||
assert_eq!(page.matched, 1);
|
||||
assert_eq!(page.items[0].admission.provenance, provenance());
|
||||
assert_eq!(page.items[0].status, PipelineStatus::InForce);
|
||||
|
||||
let id = writes[0].clone();
|
||||
let view = daemon.handle(HuginnMindRequest::View { instance: slug(INSTANCE), id: id.clone() }, now());
|
||||
assert_eq!(view, HuginnMindResponse::View(Some(page.items[0].clone())));
|
||||
|
||||
let HuginnMindResponse::OpenItems(items) =
|
||||
daemon.handle(HuginnMindRequest::OpenItems { instance: slug(INSTANCE), campaign: slug(CAMPAIGN) }, now())
|
||||
else {
|
||||
panic!("expected open items");
|
||||
};
|
||||
assert_eq!(
|
||||
(items.questions.len(), items.findings.len(), items.follow_ups.len()),
|
||||
(0, 0, 0),
|
||||
"a mind holding only its identity has nothing open"
|
||||
);
|
||||
assert!(items.specs_without_report.is_empty() && items.reports_without_verdict.is_empty());
|
||||
|
||||
let scope = HistoryScope::Repo(OrgRepo("GameCult/Huginn".into()));
|
||||
assert_eq!(
|
||||
daemon.handle(HuginnMindRequest::History { instance: slug(INSTANCE), scope }, now()),
|
||||
HuginnMindResponse::History(vec![])
|
||||
);
|
||||
}
|
||||
|
||||
/// Ruling 14 across the transport, on both sides: the mind refuses a read
|
||||
/// and a write that name another instance, and nothing lands.
|
||||
#[test]
|
||||
fn a_read_or_a_write_naming_another_instance_is_refused_by_the_mind_and_writes_nothing() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mut daemon = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let landed = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now());
|
||||
let HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { writes, .. }) = landed else {
|
||||
panic!("expected a commit");
|
||||
};
|
||||
let foreign = MindRefusal::ForeignInstance { declared: OTHER.into(), mind: INSTANCE.into() };
|
||||
|
||||
let query = HuginnMindRequest::Query { instance: slug(OTHER), query: PipelineQuery::default() };
|
||||
assert_eq!(daemon.handle(query, now()), HuginnMindResponse::Refused(foreign.clone()));
|
||||
|
||||
let admit = HuginnMindRequest::Admit(batch(OTHER, vec![identity(OTHER)]));
|
||||
assert_eq!(
|
||||
daemon.handle(admit, now()),
|
||||
HuginnMindResponse::Admit(PipelineAdmissionOutcome::Refused(foreign.clone()))
|
||||
);
|
||||
|
||||
let view = HuginnMindRequest::View { instance: slug(OTHER), id: writes[0].clone() };
|
||||
assert_eq!(daemon.handle(view, now()), HuginnMindResponse::Refused(foreign.clone()));
|
||||
|
||||
let items = HuginnMindRequest::OpenItems { instance: slug(OTHER), campaign: slug(CAMPAIGN) };
|
||||
assert_eq!(daemon.handle(items, now()), HuginnMindResponse::Refused(foreign.clone()));
|
||||
|
||||
let scope = HistoryScope::Repo(OrgRepo("GameCult/Huginn".into()));
|
||||
let history = HuginnMindRequest::History { instance: slug(OTHER), scope };
|
||||
assert_eq!(daemon.handle(history, now()), HuginnMindResponse::Refused(foreign));
|
||||
|
||||
assert_eq!(status(&mut daemon).documents, 1, "nothing landed");
|
||||
}
|
||||
|
||||
/// Cut 11's seam: the index is handed every landed write, derived ones
|
||||
/// included, and can read each; an index that fails changes nothing.
|
||||
#[test]
|
||||
fn the_index_is_handed_every_landed_write_after_commit_and_never_decides_the_outcome() {
|
||||
let (_root, daemon) = seeded();
|
||||
let recording = RecordingIndex::default();
|
||||
let mut daemon = daemon.with_index(recording.clone());
|
||||
let (question, ruling) = question_and_ruling();
|
||||
|
||||
let outcome = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![question, ruling])), now());
|
||||
let HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { writes, .. }) = outcome else {
|
||||
panic!("expected a commit");
|
||||
};
|
||||
let landed: Vec<String> = writes.iter().map(|write| write.id.0.clone()).collect();
|
||||
assert_eq!(landed.len(), 3, "the ruling answers the question, so a resolution is derived: {landed:?}");
|
||||
|
||||
let seen = recording.seen.lock().unwrap().clone();
|
||||
assert_eq!(seen.len(), 1);
|
||||
assert_eq!(seen[0].0, landed, "the sink sees exactly the landed writes");
|
||||
assert_eq!(seen[0].1, vec![true, true, true], "and can read each one when it is called");
|
||||
|
||||
let (question, ruling) = question_and_ruling();
|
||||
let mut daemon = daemon.with_index(FailingIndex);
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mut fresh = Daemon::open(root.path(), &slug(INSTANCE)).unwrap().with_index(FailingIndex);
|
||||
fresh.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now());
|
||||
let outcome = fresh.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![question, ruling])), now());
|
||||
let HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { writes, .. }) = outcome else {
|
||||
panic!("a failing index does not refuse the batch");
|
||||
};
|
||||
for write in &writes {
|
||||
let view = fresh.handle(HuginnMindRequest::View { instance: slug(INSTANCE), id: write.clone() }, now());
|
||||
assert!(matches!(view, HuginnMindResponse::View(Some(_))), "{write:?} is readable");
|
||||
}
|
||||
assert_eq!(status(&mut daemon).documents, 4, "identity, question, ruling and the derived resolution");
|
||||
}
|
||||
|
||||
/// Ruling 15: a mind that will not open is refused, and `Daemon::open`
|
||||
/// takes no address, so no socket can precede it. `startup`'s order is
|
||||
/// pinned in `serve::tests`.
|
||||
#[test]
|
||||
fn the_daemon_refuses_loudly_when_it_cannot_open_the_mind_and_binds_nothing() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let held = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let path = Mind::<OwnedRedbMessagePackBackingStore>::path_for(root.path(), &slug(INSTANCE));
|
||||
assert_eq!(
|
||||
Daemon::open(root.path(), &slug(INSTANCE)).err(),
|
||||
Some(MindRefusal::MindAlreadyOwned { path: path.display().to_string() })
|
||||
);
|
||||
drop(held);
|
||||
|
||||
let mut planted = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
planted.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now());
|
||||
drop(planted);
|
||||
let moved = Mind::<OwnedRedbMessagePackBackingStore>::path_for(root.path(), &slug(OTHER));
|
||||
std::fs::create_dir_all(moved.parent().unwrap()).unwrap();
|
||||
std::fs::copy(&path, &moved).unwrap();
|
||||
assert_eq!(
|
||||
Daemon::open(root.path(), &slug(OTHER)).err(),
|
||||
Some(MindRefusal::ForeignInstance { declared: OTHER.into(), mind: INSTANCE.into() })
|
||||
);
|
||||
}
|
||||
}
|
||||
223
crates/huginn-daemon/src/envelope.rs
Normal file
223
crates/huginn-daemon/src/envelope.rs
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
//! CultNet's operation envelope around the mind's vocabulary. Pure functions:
|
||||
//! a request goes out as `cultnet.operation_request.v0` and an answer comes
|
||||
//! back as `cultnet.operation_response.v0`, both carrying base64 of named
|
||||
//! MessagePack, which is what the C# reference and Eve's plugin ABI speak.
|
||||
//!
|
||||
//! A message this module cannot decode is a typed failure with a code, never
|
||||
//! a transport error and never a call into a mind. `payload_encoding` is
|
||||
//! CultNet's own check in `validate_message` and is not re-checked here.
|
||||
//! `source_runtime_id` and `target_runtime_id` on a request are read by
|
||||
//! nothing: ruling 18 says an identity is declared in the payload, not
|
||||
//! granted by the envelope.
|
||||
|
||||
use anyhow::Result;
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use cultnet_rs::CultNetMessage;
|
||||
use huginn_mind::wire::{HuginnMindRequest, HuginnMindResponse, MIND_REQUEST_SCHEMA, MIND_RESPONSE_SCHEMA, MIND_SERVICE_ID};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The C# reference's `CultNetOperationServer.FailureSchemaId`. The Rust
|
||||
/// `cultnet-rs` publishes no type for it, so this two-field mirror carries the
|
||||
/// reference's keys; parity belongs in CultLib.
|
||||
pub const FAILURE_SCHEMA: &str = "gamecult.cultnet.operation_failure.v1";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
|
||||
pub struct OperationFailure {
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl OperationFailure {
|
||||
fn new(code: &str, message: impl Into<String>) -> Self {
|
||||
Self { code: code.into(), message: message.into() }
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_payload<T: Serialize>(value: &T) -> Result<String> {
|
||||
Ok(STANDARD.encode(rmp_serde::to_vec_named(value)?))
|
||||
}
|
||||
|
||||
/// One request, addressed to the mind service and naming its own operation.
|
||||
pub fn encode_request(
|
||||
message_id: &str,
|
||||
request: &HuginnMindRequest,
|
||||
source_runtime_id: Option<String>,
|
||||
) -> Result<CultNetMessage> {
|
||||
Ok(CultNetMessage::OperationRequest {
|
||||
message_id: message_id.to_string(),
|
||||
service_id: MIND_SERVICE_ID.to_string(),
|
||||
operation: request.operation().to_string(),
|
||||
payload_schema: MIND_REQUEST_SCHEMA.to_string(),
|
||||
payload_encoding: "messagepack-base64".to_string(),
|
||||
payload: encode_payload(request)?,
|
||||
source_runtime_id,
|
||||
target_runtime_id: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The message, or the failure that says why it is not a request of this
|
||||
/// service. The envelope's `operation` string is checked against the decoded
|
||||
/// request rather than trusted, so a payload cannot arrive under another
|
||||
/// operation's name.
|
||||
pub fn decode_request(message: &CultNetMessage) -> Result<(String, HuginnMindRequest), OperationFailure> {
|
||||
let CultNetMessage::OperationRequest { message_id, service_id, operation, payload_schema, payload, .. } = message
|
||||
else {
|
||||
return Err(OperationFailure::new(
|
||||
"not-an-operation-request",
|
||||
"huginn.mind reads cultnet.operation_request.v0",
|
||||
));
|
||||
};
|
||||
if service_id != MIND_SERVICE_ID {
|
||||
return Err(OperationFailure::new("wrong-service", format!("{service_id} is not {MIND_SERVICE_ID}")));
|
||||
}
|
||||
if payload_schema != MIND_REQUEST_SCHEMA {
|
||||
return Err(OperationFailure::new(
|
||||
"wrong-payload-schema",
|
||||
format!("{payload_schema} is not {MIND_REQUEST_SCHEMA}"),
|
||||
));
|
||||
}
|
||||
let bytes = STANDARD
|
||||
.decode(payload)
|
||||
.map_err(|error| OperationFailure::new("payload-not-base64", error.to_string()))?;
|
||||
let request: HuginnMindRequest = rmp_serde::from_slice(&bytes)
|
||||
.map_err(|error| OperationFailure::new("payload-not-a-request", error.to_string()))?;
|
||||
if operation != request.operation() {
|
||||
return Err(OperationFailure::new(
|
||||
"operation-mismatch",
|
||||
format!("the envelope says {operation} and the payload is {}", request.operation()),
|
||||
));
|
||||
}
|
||||
Ok((message_id.clone(), request))
|
||||
}
|
||||
|
||||
/// One answer, whose status the response derives.
|
||||
pub fn encode_response(
|
||||
message_id: &str,
|
||||
operation: &str,
|
||||
response: &HuginnMindResponse,
|
||||
source_runtime_id: &str,
|
||||
) -> Result<CultNetMessage> {
|
||||
Ok(CultNetMessage::OperationResponse {
|
||||
message_id: message_id.to_string(),
|
||||
service_id: MIND_SERVICE_ID.to_string(),
|
||||
operation: operation.to_string(),
|
||||
status: response.status().to_string(),
|
||||
payload_schema: MIND_RESPONSE_SCHEMA.to_string(),
|
||||
payload_encoding: "messagepack-base64".to_string(),
|
||||
payload: encode_payload(response)?,
|
||||
diagnostics: vec![],
|
||||
source_runtime_id: Some(source_runtime_id.to_string()),
|
||||
})
|
||||
}
|
||||
|
||||
/// The answer to an envelope that never reached a mind. Infallible: the
|
||||
/// failure is two strings and the daemon must always be able to say no.
|
||||
pub fn encode_failure(
|
||||
message_id: &str,
|
||||
operation: &str,
|
||||
failure: &OperationFailure,
|
||||
source_runtime_id: &str,
|
||||
) -> CultNetMessage {
|
||||
CultNetMessage::OperationResponse {
|
||||
message_id: message_id.to_string(),
|
||||
service_id: MIND_SERVICE_ID.to_string(),
|
||||
operation: operation.to_string(),
|
||||
status: "rejected".to_string(),
|
||||
payload_schema: FAILURE_SCHEMA.to_string(),
|
||||
payload_encoding: "messagepack-base64".to_string(),
|
||||
payload: STANDARD.encode(rmp_serde::to_vec_named(failure).expect("two strings encode")),
|
||||
diagnostics: vec![failure.code.clone()],
|
||||
source_runtime_id: Some(source_runtime_id.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The client's side: the correlation key and either the mind's answer or the
|
||||
/// failure that says the envelope never reached one.
|
||||
pub fn decode_response(message: &CultNetMessage) -> Result<(String, Result<HuginnMindResponse, OperationFailure>)> {
|
||||
let CultNetMessage::OperationResponse { message_id, payload_schema, payload, .. } = message else {
|
||||
anyhow::bail!("huginn.mind answers with cultnet.operation_response.v0");
|
||||
};
|
||||
let bytes = STANDARD.decode(payload)?;
|
||||
let answer = match payload_schema.as_str() {
|
||||
MIND_RESPONSE_SCHEMA => Ok(rmp_serde::from_slice::<HuginnMindResponse>(&bytes)?),
|
||||
FAILURE_SCHEMA => Err(rmp_serde::from_slice::<OperationFailure>(&bytes)?),
|
||||
other => anyhow::bail!("{other} is neither {MIND_RESPONSE_SCHEMA} nor {FAILURE_SCHEMA}"),
|
||||
};
|
||||
Ok((message_id.clone(), answer))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::daemon::tests::{INSTANCE, OTHER, batch, identity, slug};
|
||||
use huginn_mind::wire::MindStatus;
|
||||
use huginn_mind::{MindRefusal, PipelineAdmissionOutcome};
|
||||
|
||||
fn refusal() -> MindRefusal {
|
||||
MindRefusal::ForeignInstance { declared: OTHER.into(), mind: INSTANCE.into() }
|
||||
}
|
||||
|
||||
/// A refusal is an answer: it rides the response schema with a `rejected`
|
||||
/// status and decodes back to the same value. A conflict and a replay are
|
||||
/// answers about the mind, not rejections of the request.
|
||||
#[test]
|
||||
fn a_refusal_is_a_typed_response_not_a_transport_failure() {
|
||||
let cases = [
|
||||
(HuginnMindResponse::Refused(refusal()), "rejected"),
|
||||
(HuginnMindResponse::Admit(PipelineAdmissionOutcome::Refused(refusal())), "rejected"),
|
||||
(
|
||||
HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed {
|
||||
receipt_id: "r1".into(),
|
||||
committed_at: "2026-09-16T12:00:00Z".into(),
|
||||
writes: vec![],
|
||||
}),
|
||||
"accepted",
|
||||
),
|
||||
(HuginnMindResponse::Admit(PipelineAdmissionOutcome::AlreadyAdmitted { receipt_id: "r1".into() }), "accepted"),
|
||||
(HuginnMindResponse::Admit(PipelineAdmissionOutcome::Conflict { identities: vec![] }), "accepted"),
|
||||
(
|
||||
HuginnMindResponse::Whoami(MindStatus {
|
||||
instance: slug(INSTANCE),
|
||||
schema_epoch: "epiphany.pipeline.epoch.v1".into(),
|
||||
documents: 0,
|
||||
receipts: 0,
|
||||
}),
|
||||
"accepted",
|
||||
),
|
||||
];
|
||||
for (response, expected) in cases {
|
||||
let message = encode_response("m-1", "admit", &response, "huginn-yggdrasil").unwrap();
|
||||
let CultNetMessage::OperationResponse { status, payload_schema, diagnostics, .. } = &message else {
|
||||
panic!("not an operation response");
|
||||
};
|
||||
assert_eq!(status, expected, "{response:?}");
|
||||
assert_eq!(payload_schema, MIND_RESPONSE_SCHEMA);
|
||||
assert!(diagnostics.is_empty());
|
||||
assert_eq!(decode_response(&message).unwrap(), ("m-1".into(), Ok(response)));
|
||||
}
|
||||
|
||||
let failure = OperationFailure::new("wrong-service", "odin.catalog is not huginn.mind");
|
||||
let message = encode_failure("m-2", "query", &failure, "huginn-yggdrasil");
|
||||
let CultNetMessage::OperationResponse { status, payload_schema, diagnostics, .. } = &message else {
|
||||
panic!("not an operation response");
|
||||
};
|
||||
assert_eq!((status.as_str(), payload_schema.as_str()), ("rejected", FAILURE_SCHEMA));
|
||||
assert_eq!(diagnostics, &vec!["wrong-service".to_string()]);
|
||||
assert_eq!(decode_response(&message).unwrap(), ("m-2".into(), Err(failure)));
|
||||
}
|
||||
|
||||
/// The round trip the client and the daemon share, and the one code that
|
||||
/// `serve::answer` cannot reach because it answers a foreign message with
|
||||
/// `Error` instead.
|
||||
#[test]
|
||||
fn a_request_round_trips_and_a_foreign_message_is_not_a_request() {
|
||||
let request = HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)]));
|
||||
let message = encode_request("m-3", &request, Some("eureka-state".into())).unwrap();
|
||||
assert_eq!(decode_request(&message).unwrap(), ("m-3".into(), request));
|
||||
|
||||
let foreign = CultNetMessage::Error { error: "not for you".into() };
|
||||
assert_eq!(decode_request(&foreign).unwrap_err().code, "not-an-operation-request");
|
||||
}
|
||||
}
|
||||
21
crates/huginn-daemon/src/lib.rs
Normal file
21
crates/huginn-daemon/src/lib.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
//! `huginn-daemon`: one process that serves one mind over CultNet.
|
||||
//!
|
||||
//! The crate owns four things and no rule. `Daemon::open` owns "may this
|
||||
//! process serve this mind", by delegating to `Mind::open` and refusing before
|
||||
//! anything listens. `Daemon::handle` owns "which `Mind` method answers this
|
||||
//! request": it dispatches, it asks `Mind::require_instance` for every read
|
||||
//! that names an instance, and it compares nothing itself. `envelope` owns "is
|
||||
//! this a request at all": a message it cannot decode is answered with a typed
|
||||
//! failure and touches no mind. `serve` owns "which session gets which reply".
|
||||
//!
|
||||
//! Every admission rule, the instance check, the receipt, the derived status
|
||||
//! and the wire vocabulary are `huginn-mind`'s. The leaf and the store type
|
||||
//! are reached through `huginn_mind`, so one crate pins one revision of each.
|
||||
|
||||
pub mod daemon;
|
||||
pub mod envelope;
|
||||
pub mod serve;
|
||||
|
||||
pub use daemon::{Daemon, IndexSink, NoIndex};
|
||||
pub use envelope::{FAILURE_SCHEMA, OperationFailure};
|
||||
pub use serve::{Options, ServeOptions, parse_options, run, startup};
|
||||
|
|
@ -1 +1,32 @@
|
|||
fn main() {}
|
||||
//! 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
|
||||
//! stderr and no socket ever bound.
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use huginn_daemon::serve::{ServeOptions, parse_options, run, startup};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let options = parse_options(std::env::args().skip(1))?;
|
||||
let (mut daemon, mut hub, registry) = match startup(&options) {
|
||||
Ok(started) => started,
|
||||
Err(error) => {
|
||||
eprintln!("huginn-daemon refuses to serve {}: {error:#}", options.instance.0);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
// Idunn runs this process as PID 1 of its own PID namespace, and a
|
||||
// namespace init ignores every signal it has not caught. Without this,
|
||||
// a stop is a ninety-second wait for SIGKILL.
|
||||
let stopping = Arc::new(AtomicBool::new(false));
|
||||
for signal in [signal_hook::consts::SIGTERM, signal_hook::consts::SIGINT] {
|
||||
signal_hook::flag::register(signal, Arc::clone(&stopping))
|
||||
.context("registering Huginn's stop signal handler")?;
|
||||
}
|
||||
|
||||
eprintln!("huginn-daemon serving {} on {}", options.instance.0, hub.local_addr()?);
|
||||
run(&mut daemon, &mut hub, ®istry, &stopping, &ServeOptions::default())
|
||||
}
|
||||
|
|
|
|||
463
crates/huginn-daemon/src/serve.rs
Normal file
463
crates/huginn-daemon/src/serve.rs
Normal file
|
|
@ -0,0 +1,463 @@
|
|||
//! The loop: one UDP socket, one reliable session per peer, one reply per
|
||||
//! frame on the frame's own session. The hub is CultLib's; what is here is the
|
||||
//! routing, the schema catalog and the order the process starts in.
|
||||
//!
|
||||
//! Stated limits. A socket error and a hostile datagram are indistinguishable
|
||||
//! at `receive_event_once`, so both are logged and served past: a dead socket
|
||||
//! spins with logging rather than exiting, and Cut 14's health check is the
|
||||
//! observer. The reliable window is `max_pending_reliable_packets` times
|
||||
//! `max_fragment_bytes` per session, about 1.2 MB in flight; a page larger
|
||||
//! than that fails to send and the client times out rather than receiving a
|
||||
//! typed answer.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::net::{SocketAddr, UdpSocket};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::{Context, Result, ensure};
|
||||
use chrono::{DateTime, Utc};
|
||||
use cultnet_rs::{
|
||||
CULTNET_OPERATION_CONNECTION_ID, CultNetMessage, CultNetRudpServerEvent, CultNetRudpServerHub,
|
||||
CultNetRudpServerHubOptions, CultNetSchemaKind, CultNetSchemaRegistration, CultNetSchemaRegistry,
|
||||
CultNetWireContract, decode_cultnet_message_from_slice,
|
||||
};
|
||||
use huginn_mind::epiphany_pipeline::Slug;
|
||||
use huginn_mind::wire::{
|
||||
MIND_REQUEST_SCHEMA, MIND_REQUEST_SCHEMA_JSON, MIND_RESPONSE_SCHEMA, MIND_RESPONSE_SCHEMA_JSON, MIND_SERVICE_ID,
|
||||
};
|
||||
use huginn_mind::{MindStore, OwnedRedbMessagePackBackingStore};
|
||||
|
||||
use crate::daemon::{Daemon, IndexSink, NoIndex};
|
||||
use crate::envelope::{decode_request, encode_failure, encode_response};
|
||||
|
||||
/// What the loop does when nothing is waiting.
|
||||
pub struct ServeOptions {
|
||||
pub session_timeout: Duration,
|
||||
pub idle_sleep: Duration,
|
||||
}
|
||||
|
||||
impl Default for ServeOptions {
|
||||
fn default() -> Self {
|
||||
Self { session_timeout: Duration::from_secs(30), idle_sleep: Duration::from_millis(2) }
|
||||
}
|
||||
}
|
||||
|
||||
/// The three things the operator supplies. Nothing here reads the environment:
|
||||
/// how Idunn supplies `--bind` is Cut 14's.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Options {
|
||||
pub state_root: PathBuf,
|
||||
pub instance: Slug,
|
||||
pub bind: SocketAddr,
|
||||
}
|
||||
|
||||
/// Exactly `--state-root`, `--instance` and `--bind`, each required, each once.
|
||||
pub fn parse_options(args: impl Iterator<Item = String>) -> Result<Options> {
|
||||
let mut args = args;
|
||||
let mut values: BTreeMap<String, String> = BTreeMap::new();
|
||||
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"), "unsupported Huginn option --{name}");
|
||||
let value = args.next().with_context(|| format!("missing value for --{name}"))?;
|
||||
ensure!(values.insert(name.to_owned(), value).is_none(), "duplicate Huginn option --{name}");
|
||||
}
|
||||
let take = |name: &str| -> Result<String> {
|
||||
values.get(name).cloned().with_context(|| format!("--{name} is required"))
|
||||
};
|
||||
let state_root = PathBuf::from(take("state-root")?);
|
||||
ensure!(state_root.is_absolute(), "--state-root must be an absolute path");
|
||||
let bind = take("bind")?;
|
||||
Ok(Options {
|
||||
state_root,
|
||||
instance: Slug(take("instance")?),
|
||||
bind: bind.parse().with_context(|| format!("--bind must be an ip:port, got {bind:?}"))?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Ruling 15, as an order: the mind opens first, and only a mind that opened
|
||||
/// gets a socket. This is the one place both happen, so nothing can bind ahead
|
||||
/// of the refusal.
|
||||
pub fn startup(
|
||||
options: &Options,
|
||||
) -> Result<(Daemon<OwnedRedbMessagePackBackingStore, NoIndex>, CultNetRudpServerHub, CultNetSchemaRegistry)> {
|
||||
let daemon = Daemon::open(&options.state_root, &options.instance)?;
|
||||
let hub = bind(options.bind, &daemon.runtime_id())?;
|
||||
Ok((daemon, hub, schema_registry()?))
|
||||
}
|
||||
|
||||
/// One non-blocking socket serving the operation connection id. The hub drops
|
||||
/// every datagram carrying another one.
|
||||
pub fn bind(addr: SocketAddr, runtime_id: &str) -> Result<CultNetRudpServerHub> {
|
||||
let socket = UdpSocket::bind(addr).with_context(|| format!("binding {addr}"))?;
|
||||
socket.set_nonblocking(true)?;
|
||||
let mut options = CultNetRudpServerHubOptions::new(runtime_id, socket, CULTNET_OPERATION_CONNECTION_ID);
|
||||
options.max_fragment_bytes = Some(1200);
|
||||
options.max_pending_reliable_packets = Some(1024);
|
||||
options.max_peers = 256;
|
||||
CultNetRudpServerHub::new(options)
|
||||
}
|
||||
|
||||
/// The organ's two contracts, as the catalog advertises them.
|
||||
pub fn schema_registry() -> Result<CultNetSchemaRegistry> {
|
||||
let mut registry = CultNetSchemaRegistry::new();
|
||||
for (schema_id, title, schema_json) in [
|
||||
(MIND_REQUEST_SCHEMA, "Huginn mind request", MIND_REQUEST_SCHEMA_JSON),
|
||||
(MIND_RESPONSE_SCHEMA, "Huginn mind response", MIND_RESPONSE_SCHEMA_JSON),
|
||||
] {
|
||||
registry.register(CultNetSchemaRegistration {
|
||||
schema_id: schema_id.to_string(),
|
||||
kind: CultNetSchemaKind::WireMessage,
|
||||
wire_contracts: vec![CultNetWireContract::CultNetSchemaV0],
|
||||
schema_version: Some(schema_id.to_string()),
|
||||
document_type: None,
|
||||
title: Some(title.to_string()),
|
||||
schema_json: Some(schema_json.to_string()),
|
||||
})?;
|
||||
}
|
||||
Ok(registry)
|
||||
}
|
||||
|
||||
/// One message in, one message out. Nothing here reaches a mind except through
|
||||
/// `Daemon::handle`, and an envelope that does not decode never gets that far.
|
||||
pub fn answer<S: MindStore, I: IndexSink<S>>(
|
||||
daemon: &mut Daemon<S, I>,
|
||||
registry: &CultNetSchemaRegistry,
|
||||
message: CultNetMessage,
|
||||
now: DateTime<Utc>,
|
||||
) -> CultNetMessage {
|
||||
let runtime_id = daemon.runtime_id();
|
||||
match &message {
|
||||
CultNetMessage::OperationRequest { message_id, operation, .. } => match decode_request(&message) {
|
||||
Ok((message_id, request)) => {
|
||||
let operation = request.operation();
|
||||
let response = daemon.handle(request, now);
|
||||
match encode_response(&message_id, operation, &response, &runtime_id) {
|
||||
Ok(message) => message,
|
||||
Err(error) => encode_failure(
|
||||
&message_id,
|
||||
operation,
|
||||
&crate::envelope::OperationFailure {
|
||||
code: "response-not-encodable".into(),
|
||||
message: format!("{error:#}"),
|
||||
},
|
||||
&runtime_id,
|
||||
),
|
||||
}
|
||||
}
|
||||
Err(failure) => encode_failure(message_id, operation, &failure, &runtime_id),
|
||||
},
|
||||
CultNetMessage::SchemaCatalogRequest { .. } => match registry.create_catalog_response(&message) {
|
||||
Ok(response) => response,
|
||||
Err(error) => CultNetMessage::Error { error: format!("{error:#}") },
|
||||
},
|
||||
_ => CultNetMessage::Error {
|
||||
error: format!(
|
||||
"{MIND_SERVICE_ID} answers cultnet.operation_request.v0 and cultnet.schema_catalog_request.v0"
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Until `stopping`: expire what timed out, resend what was not acknowledged,
|
||||
/// then answer every frame waiting on the socket. A hostile datagram and a
|
||||
/// departed session are logged and served past, never fatal. This is the
|
||||
/// crate's only clock read, once per frame.
|
||||
pub fn run<S: MindStore, I: IndexSink<S>>(
|
||||
daemon: &mut Daemon<S, I>,
|
||||
hub: &mut CultNetRudpServerHub,
|
||||
registry: &CultNetSchemaRegistry,
|
||||
stopping: &AtomicBool,
|
||||
options: &ServeOptions,
|
||||
) -> Result<()> {
|
||||
let timeout_ms = options.session_timeout.as_millis() as u64;
|
||||
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;
|
||||
}
|
||||
};
|
||||
let reply = answer(daemon, registry, message, Utc::now());
|
||||
if let Err(error) = hub.send_schema_message(&session, &reply) {
|
||||
eprintln!("huginn: {} did not receive its reply: {error:#}", session.remote_addr);
|
||||
}
|
||||
served += 1;
|
||||
}
|
||||
if served == 0 {
|
||||
std::thread::sleep(options.idle_sleep);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::daemon::tests::{INSTANCE, batch, identity, now, slug};
|
||||
use crate::envelope::{FAILURE_SCHEMA, decode_response, encode_request};
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD;
|
||||
use cultnet_rs::{CultMesh, CultMeshRudpSocketOptions};
|
||||
use huginn_mind::wire::{HuginnMindRequest, HuginnMindResponse, MindStatus};
|
||||
use huginn_mind::{PipelineAdmissionOutcome, PipelineQuery};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
|
||||
fn documents(daemon: &mut Daemon<OwnedRedbMessagePackBackingStore, NoIndex>) -> u32 {
|
||||
match daemon.handle(HuginnMindRequest::Whoami, now()) {
|
||||
HuginnMindResponse::Whoami(MindStatus { documents, .. }) => documents,
|
||||
other => panic!("expected a status, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
fn request(
|
||||
message_id: &str,
|
||||
service_id: &str,
|
||||
operation: &str,
|
||||
payload_schema: &str,
|
||||
payload: String,
|
||||
) -> CultNetMessage {
|
||||
CultNetMessage::OperationRequest {
|
||||
message_id: message_id.into(),
|
||||
service_id: service_id.into(),
|
||||
operation: operation.into(),
|
||||
payload_schema: payload_schema.into(),
|
||||
payload_encoding: "messagepack-base64".into(),
|
||||
payload,
|
||||
source_runtime_id: None,
|
||||
target_runtime_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn rejected(message: &CultNetMessage) -> (String, String, String) {
|
||||
let CultNetMessage::OperationResponse { message_id, status, payload_schema, payload, .. } = message else {
|
||||
panic!("expected an operation response, got {message:?}");
|
||||
};
|
||||
assert_eq!(status, "rejected");
|
||||
assert_eq!(payload_schema, FAILURE_SCHEMA);
|
||||
let failure = decode_response(message).unwrap().1.unwrap_err();
|
||||
(message_id.clone(), failure.code, payload.clone())
|
||||
}
|
||||
|
||||
/// Six malformed envelopes, each with its own code, none of which reaches
|
||||
/// a mind; a message that is not an operation at all is answered rather
|
||||
/// than dropped; and the catalog answers with both schemas.
|
||||
#[test]
|
||||
fn a_malformed_envelope_is_answered_with_a_failure_and_touches_no_mind() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mut daemon = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let registry = schema_registry().unwrap();
|
||||
let admit = STANDARD.encode(
|
||||
rmp_serde::to_vec_named(&HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)]))).unwrap(),
|
||||
);
|
||||
|
||||
let cases = [
|
||||
("m-1", request("m-1", "odin.catalog", "admit", MIND_REQUEST_SCHEMA, admit.clone()), "wrong-service"),
|
||||
("m-2", request("m-2", MIND_SERVICE_ID, "admit", "odin.topology.v1", admit.clone()), "wrong-payload-schema"),
|
||||
(
|
||||
"m-3",
|
||||
request("m-3", MIND_SERVICE_ID, "admit", MIND_REQUEST_SCHEMA, "not base64!!".into()),
|
||||
"payload-not-base64",
|
||||
),
|
||||
(
|
||||
"m-4",
|
||||
request("m-4", MIND_SERVICE_ID, "admit", MIND_REQUEST_SCHEMA, STANDARD.encode([0xc1_u8, 0xc1])),
|
||||
"payload-not-a-request",
|
||||
),
|
||||
("m-5", request("m-5", MIND_SERVICE_ID, "query", MIND_REQUEST_SCHEMA, admit), "operation-mismatch"),
|
||||
];
|
||||
for (id, message, code) in cases {
|
||||
let reply = answer(&mut daemon, ®istry, message, now());
|
||||
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 `envelope::tests`: `answer` never produces it, because a
|
||||
// message of another family is answered with `Error` instead.
|
||||
let reply = answer(&mut daemon, ®istry, CultNetMessage::Error { error: "hello".into() }, now());
|
||||
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"),
|
||||
"{error}"
|
||||
);
|
||||
|
||||
assert_eq!(documents(&mut daemon), 0, "no malformed envelope reached the mind");
|
||||
|
||||
let catalog = CultNetMessage::SchemaCatalogRequest {
|
||||
message_id: "m-6".into(),
|
||||
include_schema_json: Some(true),
|
||||
schema_ids: None,
|
||||
kinds: None,
|
||||
};
|
||||
let reply = answer(&mut daemon, ®istry, catalog, now());
|
||||
let CultNetMessage::SchemaCatalogResponse { message_id, schemas } = reply else {
|
||||
panic!("expected a catalog response");
|
||||
};
|
||||
assert_eq!(message_id, "m-6");
|
||||
let ids: Vec<&str> = schemas.iter().map(|schema| schema.schema_id.as_str()).collect();
|
||||
assert_eq!(ids, [MIND_REQUEST_SCHEMA, MIND_RESPONSE_SCHEMA]);
|
||||
for schema in &schemas {
|
||||
assert_eq!(schema.content_hash, registry.get(&schema.schema_id, true).unwrap().content_hash);
|
||||
assert!(schema.schema_json.as_deref().unwrap().contains("HuginnMind"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Two clients over loopback, each answered on its own session; a client on
|
||||
/// another connection id is not served at all; the stop flag ends the loop.
|
||||
#[test]
|
||||
fn two_clients_get_their_own_replies_over_loopback() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mut daemon = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let mut hub = bind("127.0.0.1:0".parse().unwrap(), &daemon.runtime_id()).unwrap();
|
||||
let endpoint = format!("rudp://{}", hub.local_addr().unwrap());
|
||||
let registry = schema_registry().unwrap();
|
||||
let stopping = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let mut clients = Vec::new();
|
||||
for (name, connection_id) in
|
||||
[("a", CULTNET_OPERATION_CONNECTION_ID), ("b", CULTNET_OPERATION_CONNECTION_ID), ("c", 0x4355_4c55)]
|
||||
{
|
||||
let mut client = CultMesh::create_rudp_client_for_endpoint(
|
||||
format!("eureka-state-{name}"),
|
||||
connection_id,
|
||||
&endpoint,
|
||||
CultMeshRudpSocketOptions::default(),
|
||||
)
|
||||
.unwrap();
|
||||
client.connect(Vec::new()).unwrap();
|
||||
clients.push(client);
|
||||
}
|
||||
// The two admitted peers connect; the third's packets are dropped on
|
||||
// their connection id and it never becomes a session.
|
||||
for _ in 0..200 {
|
||||
while hub.receive_event_once().unwrap().is_some() {}
|
||||
for client in clients.iter_mut() {
|
||||
let _ = client.receive_once();
|
||||
}
|
||||
if clients[0].connected() && clients[1].connected() {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
assert!(clients[0].connected() && clients[1].connected());
|
||||
assert!(!clients[2].connected(), "another connection id is not served");
|
||||
assert_eq!(hub.sessions().len(), 2);
|
||||
|
||||
let requests = [
|
||||
("m-a", HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)]))),
|
||||
("m-b", HuginnMindRequest::Query { instance: slug(INSTANCE), query: PipelineQuery::default() }),
|
||||
];
|
||||
for (client, (message_id, request)) in clients.iter_mut().zip(requests.iter()) {
|
||||
client.send_schema_message(&encode_request(message_id, request, None).unwrap()).unwrap();
|
||||
}
|
||||
// The third client cannot even send: the hub dropped every packet
|
||||
// carrying its connection id, so it never became a session.
|
||||
let whoami = encode_request("m-c", &HuginnMindRequest::Whoami, None).unwrap();
|
||||
assert!(clients[2].send_schema_message(&whoami).is_err(), "an unserved connection id has no session");
|
||||
|
||||
let loop_stopping = Arc::clone(&stopping);
|
||||
let serving = std::thread::spawn(move || {
|
||||
let result = run(&mut daemon, &mut hub, ®istry, &loop_stopping, &ServeOptions::default());
|
||||
(result, daemon)
|
||||
});
|
||||
|
||||
for (index, message_id) in ["m-a", "m-b"].iter().enumerate() {
|
||||
let mut reply = None;
|
||||
for _ in 0..500 {
|
||||
clients[index].poll_resends().unwrap();
|
||||
if let Some(message) = clients[index].receive_schema_message_once().unwrap() {
|
||||
reply = Some(message);
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
let reply = reply.unwrap_or_else(|| panic!("client {index} got no reply"));
|
||||
let (correlation, answer) = decode_response(&reply).unwrap();
|
||||
assert_eq!(&correlation, message_id, "each session gets its own reply");
|
||||
match (index, answer.unwrap()) {
|
||||
(0, HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { .. })) => {}
|
||||
(1, HuginnMindResponse::Query(page)) => assert_eq!(page.matched, 1),
|
||||
(index, other) => panic!("client {index} got {other:?}"),
|
||||
}
|
||||
}
|
||||
// The third client, on its own connection id, is not answered.
|
||||
for _ in 0..50 {
|
||||
clients[2].poll_resends().unwrap();
|
||||
assert!(clients[2].receive_schema_message_once().unwrap().is_none());
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
}
|
||||
|
||||
stopping.store(true, Ordering::Relaxed);
|
||||
let (result, mut daemon) = serving.join().unwrap();
|
||||
result.unwrap();
|
||||
assert_eq!(documents(&mut daemon), 1, "the admission that crossed the wire landed");
|
||||
}
|
||||
|
||||
/// Ruling 15 as an order in the one place both happen: a mind that will not
|
||||
/// open leaves the port free, so the socket cannot precede the refusal.
|
||||
#[test]
|
||||
fn startup_opens_the_mind_before_it_binds_anything() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let held = Daemon::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let probe = UdpSocket::bind("127.0.0.1:0").unwrap();
|
||||
let port = probe.local_addr().unwrap();
|
||||
drop(probe);
|
||||
|
||||
let options = Options { state_root: root.path().to_path_buf(), instance: slug(INSTANCE), bind: port };
|
||||
let error = startup(&options).err().expect("a held mind refuses");
|
||||
assert!(format!("{error:#}").contains("MindAlreadyOwned"), "{error:#}");
|
||||
drop(held);
|
||||
// Nothing bound the port, so the test can.
|
||||
UdpSocket::bind(port).expect("startup bound nothing before the mind refused");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_are_exactly_state_root_instance_and_bind() {
|
||||
let root = if cfg!(windows) { r"C:\state" } else { "/state" };
|
||||
let args = |values: &[&str]| values.iter().map(|value| value.to_string()).collect::<Vec<_>>().into_iter();
|
||||
assert_eq!(
|
||||
parse_options(args(&["--state-root", root, "--instance", "yggdrasil", "--bind", "127.0.0.1:17872"]))
|
||||
.unwrap(),
|
||||
Options {
|
||||
state_root: PathBuf::from(root),
|
||||
instance: slug("yggdrasil"),
|
||||
bind: "127.0.0.1:17872".parse().unwrap(),
|
||||
}
|
||||
);
|
||||
for bad in [
|
||||
vec!["--state-root", root, "--instance", "yggdrasil"],
|
||||
vec!["--state-root", root, "--bind", "127.0.0.1:1"],
|
||||
vec!["--instance", "yggdrasil", "--bind", "127.0.0.1:1"],
|
||||
vec!["--state-root", root, "--instance", "yggdrasil", "--bind", "127.0.0.1:1", "--bind", "127.0.0.1:2"],
|
||||
vec!["--state-root", root, "--instance", "yggdrasil", "--bind", "not-an-address"],
|
||||
vec!["--state-root", "relative", "--instance", "yggdrasil", "--bind", "127.0.0.1:1"],
|
||||
vec!["--state-root", root, "--instance", "yggdrasil", "--bind", "127.0.0.1:1", "--idunn-anchor", "x"],
|
||||
] {
|
||||
assert!(parse_options(args(&bad)).is_err(), "{bad:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue