//! The daemon half of the fixtures: an in-process Huginn daemon over a //! temporary mind, on a loopback port, shared by the client and the server //! tests. #![allow(dead_code)] use std::net::SocketAddr; use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::thread::JoinHandle; use std::time::Duration; use chrono::Utc; use cultnet_rs::{CultNetMessage, CultNetRudpServerEvent, CultNetWireContract, decode_cultnet_message_from_slice}; use huginn_daemon::serve::{bind, run, schema_registry}; use huginn_daemon::{Daemon, Handled, Hits, IndexSink, SearchTicket, ServeOptions}; use huginn_mind::eureka_pipeline::{ Date, DocRef, OrgRepo, PipelineCampaign, PipelineDocument, PipelineInstance, PipelineRef, PipelineStewardship, Sha, Short, Slug, Title, }; use huginn_mind::{ Faculty, HuginnMindRequest, HuginnMindResponse, IndexStatus, Mind, MindStore, OwnedRedbMessagePackBackingStore, PipelineAdmissionBatch, PipelineAdmissionOutcome, PipelineProvenance, }; use serde_json::{Value, json}; use tempfile::TempDir; pub const INSTANCE: &str = "eureka"; pub fn slug(value: &str) -> Slug { Slug(value.into()) } pub fn identity(name: &str) -> PipelineDocument { PipelineDocument::Instance(PipelineInstance { instance: slug(name), display_name: Short(format!("{name} mind")), created_at: Date("2026-09-29".into()), host: Short(name.into()), }) } pub fn provenance() -> PipelineProvenance { PipelineProvenance { faculty: Faculty::Hands, agent: Short("claude".into()), session: Short("session-1".into()), tool: Short("admit".into()), imported_from: None, } } pub fn batch(instance: &str, documents: Vec) -> PipelineAdmissionBatch { PipelineAdmissionBatch { instance: slug(instance), provenance: provenance(), documents } } /// A minimal `gamecult.persona_state.v0` document: the published schema's /// required fields and nothing else. pub fn persona(id: &str, updated_at: &str) -> Value { json!({ "schemaVersion": "gamecult.persona_state.v0", "provenance": { "sourceSystem": "huginn", "sourceDocumentId": id, "sourceUpdatedAt": updated_at, "exportedAt": updated_at, "authority": "canonical" }, "personaId": id, "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": updated_at }) } /// No index: the client's subject is the transport, not the projection. pub struct Inert; impl IndexSink for Inert { fn committed(&mut self, _mind: &Mind, _writes: &[PipelineRef]) -> anyhow::Result<()> { Ok(()) } fn status(&self) -> IndexStatus { IndexStatus::Current } fn search(&mut self, _text: &str, _top_k: u32, _among: Vec) -> Result { Err("this index cannot search".into()) } fn search_sources(&mut self, _query: &huginn_mind::SourceQuery) -> Result { Err("this index cannot search".into()) } fn sources_searched(&mut self) -> Vec<(SearchTicket, Result)> { Vec::new() } fn search_deadline(&self) -> Duration { Duration::ZERO } fn searched(&mut self) -> Vec<(SearchTicket, Result)> { Vec::new() } fn abandon(&mut self, _ticket: SearchTicket) {} } pub type TestDaemon = Daemon; pub fn answered(handled: Handled) -> HuginnMindResponse { match handled { Handled::Answered(response) => response, Handled::Searching(_) => panic!("the inert index cannot search"), } } /// A daemon over a temporary mind whose identity is admitted, and whatever /// else `seed` admits. pub fn mind(seed: Vec>) -> (TempDir, TestDaemon) { mind_as(INSTANCE, seed) } /// `mind` for another instance. pub fn mind_as(instance: &str, seed: Vec>) -> (TempDir, TestDaemon) { let root = tempfile::tempdir().unwrap(); let mut daemon = Daemon::new(Mind::open(root.path(), &slug(instance)).unwrap(), Inert); for documents in std::iter::once(vec![identity(instance)]).chain(seed) { let outcome = answered(daemon.handle(HuginnMindRequest::Admit(batch(instance, documents)), Utc::now())); assert!(matches!(outcome, HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { .. })), "{outcome:?}"); } (root, daemon) } /// A server on a loopback port that stops when dropped. pub struct Server { pub addr: SocketAddr, pub stopping: Arc, pub thread: Option>, /// The sessions a scripted server has seen end: what the client's /// disconnect looks like from the daemon's side. pub ended: Arc, pub _root: Option, } impl Drop for Server { fn drop(&mut self) { self.stopping.store(true, Ordering::Relaxed); if let Some(thread) = self.thread.take() { let _ = thread.join(); } } } pub fn serve + Send + 'static>( root: TempDir, mut daemon: Daemon, ) -> Server { let mut hub = bind("127.0.0.1:0".parse().unwrap(), &daemon.runtime_id()).unwrap(); let addr = hub.local_addr().unwrap(); let stopping = Arc::new(AtomicBool::new(false)); let loop_stopping = Arc::clone(&stopping); let registry = schema_registry().unwrap(); let thread = std::thread::spawn(move || { run(&mut daemon, &mut hub, ®istry, None, &loop_stopping, &ServeOptions::default()).unwrap(); }); Server { addr, stopping, thread: Some(thread), ended: Arc::default(), _root: Some(root) } } /// A server that answers each schema message with whatever `script` says. pub fn scripted(mut script: impl FnMut(&CultNetMessage) -> CultNetMessage + Send + 'static) -> Server { scripted_or_silent(move |message| Some(script(message))) } /// `scripted`, where `None` leaves that request unanswered. pub fn scripted_or_silent(mut script: impl FnMut(&CultNetMessage) -> Option + Send + 'static) -> Server { let mut hub = bind("127.0.0.1:0".parse().unwrap(), "scripted").unwrap(); let addr = hub.local_addr().unwrap(); let stopping = Arc::new(AtomicBool::new(false)); let loop_stopping = Arc::clone(&stopping); let ended = Arc::new(AtomicUsize::new(0)); let loop_ended = Arc::clone(&ended); let thread = std::thread::spawn(move || { while !loop_stopping.load(Ordering::Relaxed) { hub.poll_resends().unwrap(); while let Some(event) = hub.receive_event_once().unwrap() { if matches!(event, CultNetRudpServerEvent::Disconnected { .. }) { loop_ended.fetch_add(1, Ordering::Relaxed); } let CultNetRudpServerEvent::Frame { session, frame } = event else { continue }; let message = decode_cultnet_message_from_slice(&frame.payload, CultNetWireContract::CultNetSchemaV0).unwrap(); if let Some(reply) = script(&message) { hub.send_schema_message(&session, &reply).unwrap(); } } std::thread::sleep(Duration::from_millis(2)); } }); Server { addr, stopping, thread: Some(thread), ended, _root: None } } /// A scripted server whose every answer is `reply`. pub fn answering(reply: CultNetMessage) -> Server { scripted(move |_| reply.clone()) } pub const CAMPAIGN: &str = "eureka-state"; pub const REPO: &str = "GameCult/Epiphany"; pub fn sha() -> Sha { Sha("5f98228d".into()) } pub fn campaign() -> PipelineDocument { PipelineDocument::Campaign(PipelineCampaign { slug: slug(CAMPAIGN), title: Title("Eureka pipeline state".into()), repos: vec![OrgRepo(REPO.into())], working_branch: Short("hands/cut13a".into()), target_doc: DocRef { path: Short("notes/target.md".into()), start_line: 1, end_line: 9, commit: sha() }, }) } pub fn stewardship() -> PipelineDocument { stewardship_of(INSTANCE) } pub fn stewardship_of(instance: &str) -> PipelineDocument { PipelineDocument::Stewardship(PipelineStewardship { instance: slug(instance), repo: OrgRepo(REPO.into()), sequence: 1, assigned_on: Date("2026-09-29".into()), note: "assigned".into(), names: vec![], }) }