//! 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 chrono::{DateTime, Utc}; use cultnet_rs::Selection; use crate::sources::forge::HttpForge; use huginn_mind::desk_wire::{DeskRequest, DeskResponse}; use huginn_mind::eureka_pipeline::{PipelineRef, Slug}; use huginn_mind::wire::{ACTIVITY_REPOS_MAX, HuginnMindRequest, HuginnMindResponse, IndexStatus}; use huginn_mind::{ChatHits, ChatQuery, ChatWindow, ChatWindowQuery, Members, Mind, MindRefusal, MindStore, PipelineAdmissionOutcome, SemanticQuery, SourceHits, SourceQuery}; /// Where the 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, and the call must not wait on /// the index's transport. `status` is what `whoami` reports of it. /// /// A semantic query is asked in two steps because the index answers on its own /// time: `search` starts one and returns a ticket at once, and `searched` /// hands over the searches that have finished since it was last called. The /// candidates are the index's word about nearness and nothing else; the daemon /// judges them through the mind. pub trait IndexSink { fn committed(&mut self, mind: &Mind, writes: &[PipelineRef]) -> anyhow::Result<()>; fn status(&self) -> IndexStatus; /// Starts a search for the documents nearest `text`: the index scores only /// `among`, the member document ids the mind chose, and answers the /// nearest first, at most `top_k`. `Err` says why the index cannot be /// asked now. fn search(&mut self, text: &str, top_k: u32, among: Vec) -> Result; /// How long after it was asked a search is worth answering: the index's /// own bound on it, which the loop waiting for the answer must share. /// Starts a source search. The source index is not the mind's: it needs no /// member ids, and nothing about the mind's own index state refuses it. fn search_sources(&mut self, query: &SourceQuery) -> Result; /// Starts a chat search: the same queue and worker as a source search, /// over the same collection. fn search_chat(&mut self, query: &ChatQuery) -> Result; /// Starts a chat window: the same queue and worker, no embedding. fn chat_window(&mut self, query: &ChatWindowQuery) -> Result; /// Every source or chat search that finished since the last call, by /// ticket: `Found::Sources`, `Found::Chat` or `Found::Failed`. fn sources_searched(&mut self) -> Vec<(SearchTicket, Found)>; fn search_deadline(&self) -> std::time::Duration; /// Every search that finished since the last call, by ticket: its /// candidates and scores, or why it failed. fn searched(&mut self) -> Vec<(SearchTicket, Result)>; /// The asker has stopped waiting for `ticket`. A search not yet started is /// dropped, so a queue of searches nobody wants cannot hold the index's /// writes off. fn abandon(&mut self, ticket: SearchTicket); } /// Names one search the index was asked, so the loop can match the answer to /// the client that is waiting for it. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct SearchTicket(pub u64); /// What a search returns: candidate documents and how near each scored, best /// first. Nothing else about them. pub type Hits = Vec<(PipelineRef, f32)>; /// A semantic query the index has been asked and has not yet answered. The /// serve loop holds it beside the session that asked, and hands it back to /// `Daemon::finish` with the index's answer. pub struct Search { pub ticket: SearchTicket, /// What the mind owes the asker of a mind search; `None` for a source /// search, whose answer is the source index's hits as it holds them. mind: Option, } /// A mind search in flight: the asked selection and query, and the members the /// mind chose before the index was asked. struct MindSearch { selection: Selection, semantic: SemanticQuery, members: Members, } /// What a finished search came to: the mind's candidates, the source index's /// hits, or why it failed. pub enum Found { Mind(Hits), Sources(SourceHits), Chat(ChatHits), Window(ChatWindow), /// The index answered with a typed refusal rather than failing. Refused(MindRefusal), Failed(String), } /// What a request came to: an answer, or a search the index has yet to /// finish. pub enum Handled { Answered(HuginnMindResponse), Searching(Search), } impl Handled { /// The answer, for a request that cannot be a search. #[cfg(test)] pub(crate) fn answered(self) -> HuginnMindResponse { match self { Self::Answered(response) => response, Self::Searching(search) => panic!("expected an answer, got the search {:?}", search.ticket), } } } /// What the log says of the hits the mind held no document for: how many, and /// the first few by name (a restored mind can leave hundreds), or nothing when /// the mind held them all. fn dropped_note(dropped: &[PipelineRef]) -> Option { if dropped.is_empty() { return None; } let named: Vec<&str> = dropped.iter().take(5).map(|hit| hit.id.0.as_str()).collect(); Some(format!( "huginn: the index returned {} ids this mind does not hold (first {named:?}); they are left out", dropped.len() )) } /// The envelope's `source_runtime_id` on every response a daemon serving this /// instance sends. Also what the socket is bound under, before a daemon exists. pub fn runtime_id(instance: &Slug) -> String { format!("huginn-{}", instance.0) } /// One mind and whatever indexes what lands in it. pub struct Daemon> { mind: Mind, index: I, /// The forge a persona's activity read goes to; none, and the read is refused as unavailable. forge: Option, } impl> Daemon { /// A daemon over a mind that is already open. `Mind::open` takes the /// store's exclusive lock for the daemon's lifetime, and ruling 15 has it /// refuse before any socket exists: the caller opens the mind first. pub fn new(mind: Mind, index: I) -> Self { Self { mind, index, forge: None } } /// Lets the daemon read the forge's activity for the persona it serves. The read is its own and touches no document of the mind. pub fn with_forge(mut self, forge: HttpForge) -> Self { self.forge = Some(forge); self } pub fn with_index>(self, index: J) -> Daemon { Daemon { mind: self.mind, index, forge: self.forge } } pub fn mind(&self) -> &Mind { &self.mind } pub fn index(&self) -> &I { &self.index } /// The envelope's `source_runtime_id` on every response this daemon sends. pub fn runtime_id(&self) -> String { runtime_id(self.mind.instance()) } /// 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 /// leaf's own grammar before it compares identity, so a name outside the /// grammar (a fullwidth fold, say) is refused by name rather than read as /// merely a foreign mind, and the daemon asks nothing twice. pub fn handle(&mut self, request: HuginnMindRequest, now: DateTime) -> Handled { if let Some(declared) = request.instance() && !matches!(request, HuginnMindRequest::Admit(_)) && let Err(refusal) = self.mind.require_instance(declared) { return Handled::Answered(HuginnMindResponse::Refused(refusal)); } let answer = match request { HuginnMindRequest::Whoami => HuginnMindResponse::Whoami(self.mind.status(self.index.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::>().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.map(Box::new)), Err(refusal) => HuginnMindResponse::Refused(refusal), }, HuginnMindRequest::PersonaGet { .. } => match self.mind.persona() { Ok(view) => HuginnMindResponse::Persona(view), Err(refusal) => HuginnMindResponse::Refused(refusal), }, HuginnMindRequest::PersonaPut { instance, provenance, state, expected_updated_at } => { HuginnMindResponse::PersonaPut(self.mind.put_persona(&instance, state, expected_updated_at, provenance, now)) } HuginnMindRequest::Query { selection, semantic: None, .. } => match self.mind.query(&selection) { Ok(page) => HuginnMindResponse::Query(page), Err(refusal) => HuginnMindResponse::Refused(refusal), }, HuginnMindRequest::Query { selection, semantic: Some(semantic), .. } => { return self.begin_search(selection, semantic); } HuginnMindRequest::RootsTouched { .. } => match self.mind.roots_touched() { Ok(roots) => HuginnMindResponse::RootsTouched(roots), Err(refusal) => HuginnMindResponse::Refused(refusal), }, HuginnMindRequest::SearchChat { query, .. } => { if let Err(refusal) = query.check() { return Handled::Answered(HuginnMindResponse::Refused(refusal)); } return match self.index.search_chat(&query) { Ok(ticket) => Handled::Searching(Search { ticket, mind: None }), Err(detail) => Handled::Answered(HuginnMindResponse::Refused(MindRefusal::Unavailable { detail })), }; } HuginnMindRequest::ChatWindow { query, .. } => { if let Err(refusal) = query.check() { return Handled::Answered(HuginnMindResponse::Refused(refusal)); } return match self.index.chat_window(&query) { Ok(ticket) => Handled::Searching(Search { ticket, mind: None }), Err(detail) => Handled::Answered(HuginnMindResponse::Refused(MindRefusal::Unavailable { detail })), }; } HuginnMindRequest::StewardedActivity { instance, repos, after } => { let refused = |message: &str| MindRefusal::SelectionInvalid { field: "repos".into(), value: None, message: message.into() }; if repos.len() > ACTIVITY_REPOS_MAX { return Handled::Answered(HuginnMindResponse::Refused(refused("an activity read names at most 64 repos"))); } if repos.iter().any(|repo| repo.validate_org_repo().is_err()) { return Handled::Answered(HuginnMindResponse::Refused(refused("a repo is not an Org/Repo"))); } let unavailable = |detail: &str| HuginnMindResponse::Refused(MindRefusal::Unavailable { detail: detail.into() }); match &self.forge { None => unavailable("this daemon has no forge to read"), Some(forge) => forge.activity(&repos, &instance, after, now).map_or_else(|_| unavailable("the forge could not be read in time"), HuginnMindResponse::Activity), } } HuginnMindRequest::SearchSources { query, .. } => { if let Err(refusal) = query.check() { return Handled::Answered(HuginnMindResponse::Refused(refusal)); } return match self.index.search_sources(&query) { Ok(ticket) => Handled::Searching(Search { ticket, mind: None }), Err(detail) => Handled::Answered(HuginnMindResponse::Refused(MindRefusal::Unavailable { detail })), }; } }; Handled::Answered(answer) } /// One desk request, answered by the Mind method it names. The Mind is the instance's own /// (`mind` is checked against it); `deciders` is this call's list, handed to the Mind and /// dropped. A desk answer is not indexed (`index_text` gives it no text), so nothing here tells the index. /// The reads are handed `subject` and `deciders`: the Mind builds a decider's view or a /// non-decider's (ruling `desk-read-visibility-withheld-ids`). pub fn handle_desk(&mut self, request: DeskRequest, now: DateTime) -> DeskResponse { if let Err(refusal) = self.mind.require_instance(request.mind()) { return DeskResponse::Refused(refusal); } match request { DeskRequest::Answer { subject, authority, deciders, request, .. } => { self.mind.desk_answer(&deciders, subject, authority, request, now) } DeskRequest::Forks { subject, deciders, status, cursor, limit, .. } => self.mind.desk_forks(&subject, &deciders, status, cursor, limit), DeskRequest::Fork { subject, deciders, fork_id, .. } => self.mind.desk_fork(&subject, &deciders, &fork_id), } } /// A semantic query starts only if the mind accepts it, and the mind /// decides its members before the index is asked anything. A selection /// with no members is answered at once: nothing is embedded and the index /// is not called. Otherwise the index scores those members and the answer /// waits in a `Search`. fn begin_search(&mut self, selection: Selection, semantic: SemanticQuery) -> Handled { let refused = |refusal: MindRefusal| Handled::Answered(HuginnMindResponse::Refused(refusal)); let members = match self.mind.members(&selection, &semantic) { Ok(members) => members, Err(refusal) => return refused(refusal), }; if members.ids.is_empty() { return match self.mind.rank(&selection, &semantic, &members, &[]) { Ok(ranked) => Handled::Answered(HuginnMindResponse::Query(ranked.page)), Err(refusal) => refused(refusal), }; } let among = members.ids.iter().map(|id| id.id.0.clone()).collect(); match self.index.search(&semantic.text.0, semantic.top_k, among) { Ok(ticket) => Handled::Searching(Search { ticket, mind: Some(MindSearch { selection, semantic, members }) }), Err(detail) => refused(MindRefusal::Unavailable { detail }), } } /// How long a search is worth waiting for. pub fn search_deadline(&self) -> std::time::Duration { self.index.search_deadline() } /// Searches the index has finished since this was last called. pub fn searched(&mut self) -> Vec<(SearchTicket, Found)> { let mind = self.index.searched().into_iter().map(|(ticket, found)| (ticket, found.map_or_else(Found::Failed, Found::Mind))); mind.chain(self.index.sources_searched()).collect() } /// The client has stopped waiting for `ticket`. pub fn abandon(&mut self, ticket: SearchTicket) { self.index.abandon(ticket); } /// The answer to a search: the index's scores for the members ranked /// through the mind (`Mind::rank` answers as of the snapshot the members /// were taken at and drops what is not a member), or, when the index could not answer, a typed /// `Unavailable`. Never an empty page in place of a failure. pub fn finish(&mut self, search: Search, found: Found) -> HuginnMindResponse { let unavailable = |detail: String| HuginnMindResponse::Refused(MindRefusal::Unavailable { detail }); let (selection, semantic, members, hits) = match (search.mind, found) { (_, Found::Failed(detail)) => return unavailable(detail), (None, Found::Sources(hits)) => return HuginnMindResponse::Sources(hits), (None, Found::Chat(hits)) => return HuginnMindResponse::Chat(hits), (None, Found::Window(window)) => return HuginnMindResponse::ChatWindow(window), (None, Found::Refused(refusal)) => return HuginnMindResponse::Refused(refusal), (Some(asked), Found::Mind(hits)) => (asked.selection, asked.semantic, asked.members, hits), _ => return unavailable("the index answered another kind of search than the one asked".into()), }; match self.mind.rank(&selection, &semantic, &members, &hits) { Ok(ranked) => { if let Some(note) = dropped_note(&ranked.dropped) { eprintln!("{note}"); } HuginnMindResponse::Query(ranked.page) } Err(refusal) => HuginnMindResponse::Refused(refusal), } } } #[cfg(test)] pub(crate) mod tests { use super::*; use chrono::TimeZone; use huginn_mind::eureka_pipeline::{ AuthorityMap, CodeLocation, CutDelete, CutVerification, Date, DocRef, FileChange, Line, NegativeCheck, OrgRepo, PipelineCampaign, PipelineCutSpec, PipelineDocument, PipelineInstance, PipelineKind, PipelineQuestion, PipelineRuling, PipelineStewardship, QuestionOption, RulingAuthority, Sha, Short, StructuralDelta, Title, VerificationTest, }; use huginn_mind::wire::MindStatus; use huginn_mind::ChatQuery; use huginn_mind::{MindRefusal, OwnedRedbMessagePackBackingStore}; use std::path::Path; use cultnet_rs::Selection; use huginn_mind::{Faculty, PipelineAdmissionBatch, PipelineProvenance, PipelinePageItems, PipelineStatus}; use std::sync::{Arc, Mutex}; use tempfile::TempDir; pub(crate) const INSTANCE: &str = "yggdrasil"; pub(crate) const OTHER: &str = "thought-cage"; /// `INSTANCE`'s own length, differing in its last byte alone. pub(crate) const NEAR: &str = "yggdrasix"; /// `INSTANCE` whole, with more after it. pub(crate) const PREFIXED: &str = "yggdrasil-two"; /// `INSTANCE` in another case, which a slug label permits. pub(crate) const CASED: &str = "Yggdrasil"; pub(crate) const CAMPAIGN: &str = "eureka-state"; pub(crate) fn slug(value: &str) -> Slug { value.into() } pub(crate) fn now() -> DateTime { 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()), imported_from: None, } } 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) -> 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(), title: Title("Who owns the state?".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(), title: Title("An instance owns its mind".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) } pub(crate) const REPO: &str = "GameCult/Epiphany"; /// A `Line` and a `Short` at the leaf's own bound, so a document built out /// of them is as wide as any writer may make it. pub(crate) fn line(n: usize) -> Line { Line(format!("{n:0>1000}")) } pub(crate) fn short(n: usize) -> Short { Short(format!("{n:0>200}")) } fn sha() -> Sha { Sha("5f98228d".into()) } fn doc_ref() -> DocRef { DocRef { path: Short("notes/target.md".into()), start_line: 1, end_line: 9, commit: sha() } } /// The three documents a cut spec needs before it may be admitted: the /// mind's identity, its stewardship of the repo, and the campaign that /// names the repo. pub(crate) fn campaign_seed() -> Vec { vec![ identity(INSTANCE), PipelineDocument::Stewardship(PipelineStewardship { instance: slug(INSTANCE), repo: OrgRepo(REPO.into()), sequence: 1, assigned_on: Date("2026-09-16".into()), note: "assigned".into(), names: vec![], }), PipelineDocument::Campaign(PipelineCampaign { slug: slug(CAMPAIGN), title: Title("Eureka pipeline state".into()), repos: vec![OrgRepo(REPO.into())], working_branch: Short("codex/eureka-pipeline-state".into()), target_doc: doc_ref(), }), ] } /// One cut spec with every list the leaf bounds filled to its maximum and /// every text at its own: `file_changes` file changes, 64 deletes, keeps, /// adds, builds, tests, negative checks and operator checks, a full /// authority map and a full structural delta. At 256 file changes that is /// roughly a megabyte of field content, which is what a real-sized answer /// looks like at the leaf's bounds rather than at a fixture's convenience. /// `depends_on`, `rulings` and `questions` stay empty because each is a /// reference admission resolves. `reads` and location symbols stay absent: /// `FITTING_CHANGES` is calibrated to `MAX_RESPONSE_BYTES` without them. pub(crate) fn cut_spec(cut: &str, file_changes: usize) -> PipelineDocument { let lines = |count: usize| (0..count).map(line).collect::>(); let shorts = |count: usize| (0..count).map(short).collect::>(); let location = |n: usize| CodeLocation { path: short(n), line: 1, end_line: Some(9), symbol: None }; PipelineDocument::CutSpec(PipelineCutSpec { campaign: slug(CAMPAIGN), cut: cut.into(), revision: 1, title: Title(short(0).0), repo: OrgRepo(REPO.into()), branch: short(1), base: sha(), depends_on: vec![], first: lines(16), deletes: (0..64).map(|n| CutDelete { path: short(n), lines: 9, note: line(n) }).collect(), keeps_moves: lines(64), adds: lines(64), file_changes: (0..file_changes).map(|n| FileChange { location: location(n), change: line(n) }).collect(), reads: vec![], authority_map: Some(AuthorityMap { owner: line(0), inputs: lines(16), outputs: lines(16), derived_state: lines(16), forbidden_writers: lines(16), shared_paths: lines(16), deletion_line: line(1), }), verification: CutVerification { builds: lines(64), tests: (0..64).map(|n| VerificationTest { name: short(n), pins: line(n) }).collect(), negative: (0..64).map(|n| NegativeCheck { pattern: short(n), scope: line(n) }).collect(), operator: lines(64), }, estimate: StructuralDelta { lines_added: 0, lines_removed: 0, dependencies_added: shorts(64), dependencies_removed: shorts(64), formats_added: shorts(64), formats_removed: shorts(64), targets_added: shorts(64), targets_removed: shorts(64), }, rulings: vec![], questions: vec![], }) } /// The cut spec at the leaf's own maximum, and one narrow enough that its /// answer still fits a single send. `WIDE_CHANGES` is the leaf's bound; /// `FITTING_CHANGES` is chosen so the view of that spec lands inside the /// window with less than a packet-count's slack, which is what makes the /// window's own value load-bearing rather than merely generous. pub(crate) const WIDE_CUT: &str = "wide"; pub(crate) const WIDE_CHANGES: usize = 256; pub(crate) const FITTING_CUT: &str = "fits"; pub(crate) const FITTING_CHANGES: usize = 180; /// A daemon whose mind holds the campaign seed, one cut spec as wide as the /// leaf permits, and one narrow enough to answer, so a read over it is a /// real-sized answer either way. pub(crate) fn seeded_wide() -> (TempDir, Daemon) { let root = tempfile::tempdir().unwrap(); let mut daemon = open_unindexed(root.path(), &slug(INSTANCE)).unwrap(); for documents in [ campaign_seed(), vec![cut_spec(WIDE_CUT, WIDE_CHANGES), cut_spec(FITTING_CUT, FITTING_CHANGES)], ] { let outcome = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, documents)), now()).answered(); assert!( matches!(outcome, HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { .. })), "{outcome:?}" ); } (root, daemon) } /// 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) { let root = tempfile::tempdir().unwrap(); let mut daemon = open_unindexed(root.path(), &slug(INSTANCE)).unwrap(); let outcome = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now()).answered(); assert!( matches!(outcome, HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { .. })), "{outcome:?}" ); (root, daemon) } /// The log names how many hits the mind did not hold and the first five, and /// says nothing when it held them all. #[test] fn the_dropped_hits_are_reported_by_count_and_the_first_five_names() { assert_eq!(dropped_note(&[]), None); let dropped: Vec = (0..7).map(|n| PipelineRef { kind: PipelineKind::Question, id: Short(format!("gone-{n}")) }).collect(); let note = dropped_note(&dropped).unwrap(); assert!(note.contains("returned 7 ids"), "{note}"); assert!(note.contains("gone-0") && note.contains("gone-4") && !note.contains("gone-5"), "{note}"); assert_eq!(dropped_note(&dropped[..1]).unwrap().matches("gone-").count(), 1); } /// No index: for the tests whose subject is not the projection. pub(crate) struct NoIndex; impl IndexSink for NoIndex { 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_chat(&mut self, _query: &ChatQuery) -> Result { Err("this index cannot search".into()) } fn chat_window(&mut self, _query: &ChatWindowQuery) -> Result { Err("this index cannot search".into()) } fn search_sources(&mut self, _query: &SourceQuery) -> Result { Err("this index cannot search".into()) } fn sources_searched(&mut self) -> Vec<(SearchTicket, Found)> { Vec::new() } fn search_deadline(&self) -> std::time::Duration { std::time::Duration::ZERO } fn searched(&mut self) -> Vec<(SearchTicket, Result)> { Vec::new() } fn abandon(&mut self, _ticket: SearchTicket) {} } /// An index that answers source searches from a script and counts how often /// it was asked. struct SourcesIndex { asked: Arc>, hits: SourceHits, } impl IndexSink for SourcesIndex { 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 the mind".into()) } fn search_chat(&mut self, _query: &ChatQuery) -> Result { Err("this index cannot search".into()) } fn chat_window(&mut self, _query: &ChatWindowQuery) -> Result { Err("this index cannot search".into()) } fn search_sources(&mut self, _query: &SourceQuery) -> Result { *self.asked.lock().unwrap() += 1; Ok(SearchTicket(7)) } fn sources_searched(&mut self) -> Vec<(SearchTicket, Found)> { vec![(SearchTicket(7), Found::Sources(self.hits.clone()))] } fn search_deadline(&self) -> std::time::Duration { std::time::Duration::ZERO } fn searched(&mut self) -> Vec<(SearchTicket, Result)> { Vec::new() } fn abandon(&mut self, _ticket: SearchTicket) {} } /// A source search is checked at the door before the index is asked, is /// answered with the hits the index found and nothing joined from the mind, /// turns a failure into `Unavailable`, and is refused for another mind. #[test] fn a_source_search_reaches_the_index_only_when_well_formed_and_answers_its_hits() { use huginn_mind::{SourceHit, SourceModel}; let hits = SourceHits { indexed_under: SourceModel { name: "m".into(), digest: "d".into() }, hits: vec![SourceHit { repo: "R".into(), path: "p.rs".into(), commit: "c".into(), line_start: 1, line_end: 2, score: 0.5, text: "t".into() }], }; let asked = Arc::new(Mutex::new(0)); let root = tempfile::tempdir().unwrap(); let mind = Mind::open(root.path(), &slug(INSTANCE)).unwrap(); let mut daemon = Daemon::new(mind, SourcesIndex { asked: Arc::clone(&asked), hits: hits.clone() }); let request = |instance: &str, top_k: u32| HuginnMindRequest::SearchSources { instance: slug(instance), query: SourceQuery { text: huginn_mind::eureka_pipeline::Line("find".into()), texts: vec![], top_k, repo: None, front: false }, }; let Handled::Searching(search) = daemon.handle(request(INSTANCE, 5), now()) else { panic!("a well-formed source search is a search") }; assert_eq!((search.ticket, *asked.lock().unwrap()), (SearchTicket(7), 1)); let mut finished = daemon.searched(); let (ticket, found) = finished.remove(0); assert_eq!(ticket, SearchTicket(7)); assert_eq!(daemon.finish(search, found), HuginnMindResponse::Sources(hits)); let Handled::Searching(search) = daemon.handle(request(INSTANCE, 5), now()) else { panic!() }; let failed = daemon.finish(search, Found::Failed("the index fell over".into())); assert!(matches!(failed, HuginnMindResponse::Refused(MindRefusal::Unavailable { ref detail }) if detail.contains("fell over")), "{failed:?}"); for refused in [request(INSTANCE, 0), request(INSTANCE, 51), request(OTHER, 5)] { let answer = daemon.handle(refused, now()).answered(); assert!(matches!(answer, HuginnMindResponse::Refused(_)), "{answer:?}"); } let texts = |texts: &[&str]| HuginnMindRequest::SearchSources { instance: slug(INSTANCE), query: SourceQuery { text: huginn_mind::eureka_pipeline::Line::default(), texts: texts.iter().map(|text| huginn_mind::eureka_pipeline::Line((*text).into())).collect(), top_k: 5, repo: None, front: false }, }; for refused in [texts(&[]), texts(&["a"; 9]), texts(&["a", " "])] { let answer = daemon.handle(refused, now()).answered(); assert!(matches!(answer, HuginnMindResponse::Refused(MindRefusal::SelectionInvalid { ref field, .. }) if field == "texts"), "{answer:?}"); } assert!(matches!(daemon.handle(texts(&["a", "b"]), now()), Handled::Searching(_)), "two texts are one queued search"); assert_eq!(*asked.lock().unwrap(), 3, "a refused query never reached the index"); } /// A chat search is checked at the door before the index is asked, is /// refused for another mind, turns an index that cannot be asked into /// `Unavailable`, and its finished hits are the answer as they are. #[test] fn a_chat_search_is_checked_at_the_door_and_answers_its_hits() { use huginn_mind::eureka_pipeline::{Line, Short}; use huginn_mind::{ChatHits, SourceModel}; let root = tempfile::tempdir().unwrap(); let mut daemon = Daemon::new(Mind::open(root.path(), &slug(INSTANCE)).unwrap(), NoIndex); let request = |instance: &str, rooms: &[&str], top_k: u32| HuginnMindRequest::SearchChat { instance: slug(instance), query: ChatQuery { texts: vec![Line("find".into())], rooms: rooms.iter().map(|room| Short((*room).into())).collect(), after_ms: None, before_ms: None, top_k }, }; for (refused, field) in [(request(INSTANCE, &[], 0), "top_k"), (request(INSTANCE, &["swarm"], 5), "rooms")] { let answer = daemon.handle(refused, now()).answered(); assert!(matches!(answer, HuginnMindResponse::Refused(MindRefusal::SelectionInvalid { field: ref named, .. }) if named == field), "{answer:?}"); } assert!(matches!(daemon.handle(request(OTHER, &[], 5), now()).answered(), HuginnMindResponse::Refused(_))); let answer = daemon.handle(request(INSTANCE, &["#swarm:x"], 5), now()).answered(); assert!(matches!(answer, HuginnMindResponse::Refused(MindRefusal::Unavailable { .. })), "{answer:?}"); let hits = ChatHits { indexed_under: SourceModel { name: "m".into(), digest: "d".into() }, truncated: false, hits: vec![] }; let search = Search { ticket: SearchTicket(1), mind: None }; assert_eq!(daemon.finish(search, Found::Chat(hits.clone())), HuginnMindResponse::Chat(hits)); } /// A daemon over the instance's mind under `state_root`, with no index. pub(crate) fn open_unindexed( state_root: &Path, instance: &Slug, ) -> Result, MindRefusal> { Ok(Daemon::new(Mind::open(state_root, instance)?, NoIndex)) } pub(crate) type Seen = Vec<(Vec, Vec)>; #[derive(Clone, Default)] pub(crate) struct RecordingIndex { pub(crate) seen: Arc>, } impl IndexSink for RecordingIndex { fn committed(&mut self, mind: &Mind, 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(()) } 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_chat(&mut self, _query: &ChatQuery) -> Result { Err("this index cannot search".into()) } fn chat_window(&mut self, _query: &ChatWindowQuery) -> Result { Err("this index cannot search".into()) } fn search_sources(&mut self, _query: &SourceQuery) -> Result { Err("this index cannot search".into()) } fn sources_searched(&mut self) -> Vec<(SearchTicket, Found)> { Vec::new() } fn search_deadline(&self) -> std::time::Duration { std::time::Duration::ZERO } fn searched(&mut self) -> Vec<(SearchTicket, Result)> { Vec::new() } fn abandon(&mut self, _ticket: SearchTicket) {} } struct FailingIndex; impl IndexSink for FailingIndex { fn committed(&mut self, _mind: &Mind, _writes: &[PipelineRef]) -> anyhow::Result<()> { Err(anyhow::anyhow!("the index is not there")) } 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_chat(&mut self, _query: &ChatQuery) -> Result { Err("this index cannot search".into()) } fn chat_window(&mut self, _query: &ChatWindowQuery) -> Result { Err("this index cannot search".into()) } fn search_sources(&mut self, _query: &SourceQuery) -> Result { Err("this index cannot search".into()) } fn sources_searched(&mut self) -> Vec<(SearchTicket, Found)> { Vec::new() } fn search_deadline(&self) -> std::time::Duration { std::time::Duration::ZERO } fn searched(&mut self) -> Vec<(SearchTicket, Result)> { Vec::new() } fn abandon(&mut self, _ticket: SearchTicket) {} } fn status(daemon: &mut Daemon>) -> MindStatus { match daemon.handle(HuginnMindRequest::Whoami, now()).answered() { 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 = open_unindexed(root.path(), &slug(INSTANCE)).unwrap(); let outcome = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now()).answered(); 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.v2".into(), documents: 1, receipts: 1, index: IndexStatus::Current, persona: huginn_mind::wire::PersonaStatus::Absent, } ); assert_eq!(daemon.runtime_id(), "huginn-yggdrasil"); let instances = Selection { schemas: Some(vec![PipelineKind::Instance.type_id().into()]), ..Selection::default() }; let HuginnMindResponse::Query(page) = daemon.handle( HuginnMindRequest::Query { instance: slug(INSTANCE), selection: instances.clone(), semantic: None }, now(), ).answered() else { panic!("expected a page"); }; assert_eq!(page.matched, 1); let PipelinePageItems::Headers(headers) = &page.items else { panic!("the default projection is a header") }; assert_eq!(headers[0].admission.provenance, provenance()); assert_eq!(headers[0].status, huginn_mind::PipelineStatusSummary::InForce); let id = writes[0].clone(); let whole = Selection { projection: "document".into(), ..instances }; let HuginnMindResponse::Query(page) = daemon.handle(HuginnMindRequest::Query { instance: slug(INSTANCE), selection: whole, semantic: None }, now()).answered() else { panic!("expected a page"); }; let PipelinePageItems::Documents(views) = &page.items else { panic!("the document projection is the view") }; assert_eq!(views[0].status, PipelineStatus::InForce); let view = daemon.handle(HuginnMindRequest::View { instance: slug(INSTANCE), id: id.clone() }, now()).answered(); assert_eq!(view, HuginnMindResponse::View(Some(Box::new(views[0].clone())))); } /// 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 = open_unindexed(root.path(), &slug(INSTANCE)).unwrap(); let landed = daemon.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now()).answered(); 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), selection: Selection::default(), semantic: None }; assert_eq!(daemon.handle(query, now()).answered(), HuginnMindResponse::Refused(foreign.clone())); let admit = HuginnMindRequest::Admit(batch(OTHER, vec![identity(OTHER)])); assert_eq!( daemon.handle(admit, now()).answered(), HuginnMindResponse::Admit(PipelineAdmissionOutcome::Refused(foreign.clone())) ); // The declared instance travels to the mind as the client sent it. A // batch whose documents name no instance at all is still refused on // the declaration alone, so nothing between the wire and A1 may // rewrite it into the mind's own. let (question, _) = question_and_ruling(); let admit = HuginnMindRequest::Admit(batch(OTHER, vec![question])); assert_eq!( daemon.handle(admit, now()).answered(), HuginnMindResponse::Admit(PipelineAdmissionOutcome::Refused(foreign.clone())) ); let view = HuginnMindRequest::View { instance: slug(OTHER), id: writes[0].clone() }; assert_eq!(daemon.handle(view, now()).answered(), HuginnMindResponse::Refused(foreign)); // Three names a comparison could mistake for this mind's. `NEAR` is // `INSTANCE`'s own length and differs in its last byte, so a check // reading lengths, first bytes, or running only when the declared name // is longer admits it; `PREFIXED` carries `INSTANCE` whole, so a prefix // test admits it; `CASED` is `INSTANCE` in another case, which a slug // label permits, so a check folding case admits it. Each is refused on // the read side and the write side. assert_eq!(NEAR.len(), INSTANCE.len()); assert!(PREFIXED.starts_with(INSTANCE)); assert_eq!(CASED.to_ascii_lowercase(), INSTANCE); for declared in [NEAR, PREFIXED, CASED] { let refusal = MindRefusal::ForeignInstance { declared: declared.into(), mind: INSTANCE.into() }; let query = HuginnMindRequest::Query { instance: slug(declared), selection: Selection::default(), semantic: None }; assert_eq!(daemon.handle(query, now()).answered(), HuginnMindResponse::Refused(refusal.clone()), "{declared}"); let view = HuginnMindRequest::View { instance: slug(declared), id: writes[0].clone() }; assert_eq!(daemon.handle(view, now()).answered(), HuginnMindResponse::Refused(refusal.clone()), "{declared}"); let admit = HuginnMindRequest::Admit(batch(declared, vec![identity(declared)])); assert_eq!( daemon.handle(admit, now()).answered(), HuginnMindResponse::Admit(PipelineAdmissionOutcome::Refused(refusal)), "{declared}" ); } assert_eq!(status(&mut daemon).documents, 1, "nothing landed"); } /// The persona operations name an instance like every other read: another /// mind's name is refused before the mind is asked, and a mind with no /// persona answers `Persona(None)`. #[test] fn the_persona_operations_name_this_mind_and_an_empty_mind_has_none() { let (_root, mut daemon) = seeded(); assert_eq!( daemon.handle(HuginnMindRequest::PersonaGet { instance: slug(INSTANCE) }, now()).answered(), HuginnMindResponse::Persona(None) ); let foreign = MindRefusal::ForeignInstance { declared: OTHER.into(), mind: INSTANCE.into() }; assert_eq!( daemon.handle(HuginnMindRequest::PersonaGet { instance: slug(OTHER) }, now()).answered(), HuginnMindResponse::Refused(foreign.clone()) ); let put = HuginnMindRequest::PersonaPut { instance: slug(OTHER), provenance: provenance(), state: serde_json::Value::Null, expected_updated_at: None, }; assert_eq!(daemon.handle(put, now()).answered(), HuginnMindResponse::Refused(foreign)); assert_eq!(status(&mut daemon).persona, huginn_mind::wire::PersonaStatus::Absent); } /// 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 /// mind a client could compose. A fullwidth Latin small letter y is used /// because it is the shape Soul named: a check that folds it to plain /// ASCII `y` before validating would let this probe pass as grammatical /// and reach `require_instance`, which would then answer `ForeignInstance` /// instead — the wrong refusal, reached the wrong way. #[test] fn a_declared_instance_outside_the_grammar_is_refused_by_name_before_the_identity_check() { let (_root, mut daemon) = seeded(); let fullwidth = "\u{FF59}ggdrasil"; assert_ne!(fullwidth, INSTANCE, "not the mind's own bytes"); assert!(!fullwidth.is_ascii(), "outside the grammar: a Slug is dot-joined ASCII labels"); let expected = MindRefusal::Document(huginn_mind::eureka_pipeline::PipelineRefusal::InvalidFormat { field: "declared".into(), value: fullwidth.into(), }); let query = HuginnMindRequest::Query { instance: slug(fullwidth), selection: Selection::default(), semantic: None }; assert_eq!(daemon.handle(query, now()).answered(), HuginnMindResponse::Refused(expected.clone())); let view = HuginnMindRequest::View { instance: slug(fullwidth), id: PipelineRef { kind: PipelineKind::Question, id: Short("not-reached".into()) }, }; assert_eq!(daemon.handle(view, now()).answered(), HuginnMindResponse::Refused(expected)); } /// `Admit` is excluded from the daemon's own pre-check (its batch is not a /// read naming an instance the mind is asked about first), but it reaches /// the identical `require_instance` from inside `admit_steps`'s A1, so a /// fullwidth *declared batch instance* is `InvalidFormat` there too, /// named and valued the same as the read path's. One door, /// `Mind::require_instance`, decides both. The batch's one document names /// the mind's own real instance, `INSTANCE`, so it clears `admit`'s own /// per-document `Bounded` check and the only fullwidth text anywhere in /// the request is the batch's declared instance, the thing this test /// pins. #[test] fn a_fullwidth_instance_is_refused_the_same_way_by_admit_and_by_query() { let (_root, mut daemon) = seeded(); let fullwidth = "\u{FF59}ggdrasil"; let expected = MindRefusal::Document(huginn_mind::eureka_pipeline::PipelineRefusal::InvalidFormat { field: "declared".into(), value: fullwidth.into(), }); let admit = HuginnMindRequest::Admit(batch(fullwidth, vec![identity(INSTANCE)])); assert_eq!( daemon.handle(admit, now()).answered(), HuginnMindResponse::Admit(PipelineAdmissionOutcome::Refused(expected.clone())) ); let query = HuginnMindRequest::Query { instance: slug(fullwidth), selection: Selection::default(), semantic: None }; assert_eq!(daemon.handle(query, now()).answered(), HuginnMindResponse::Refused(expected)); } /// A refusal a read method raised is the answer the dispatch returns, whole. /// The instance check refuses before the method runs and is pinned above; /// these are the refusals the methods themselves raise, so nothing here is /// the daemon's own. Each must arrive as the mind spelled it: not rewrapped /// as `Unavailable`, not rewrapped as another read's refusal, and not /// swallowed into an empty answer, which would read as "no such document" /// rather than "that is not a reference". #[test] fn a_refusal_a_read_raised_is_the_answer_the_dispatch_returns_whole() { let (_root, mut daemon) = seeded(); // A reference whose kind and id disagree names no document any writer // could have composed; the leaf's own refusal says so. let invalid = PipelineRef { kind: PipelineKind::Question, id: "not a reference".into() }; let malformed = MindRefusal::Document(huginn_mind::eureka_pipeline::PipelineRefusal::InvalidFormat { field: "ref.id".into(), value: "not a reference".into(), }); let view = HuginnMindRequest::View { instance: slug(INSTANCE), id: invalid }; assert_eq!(daemon.handle(view, now()).answered(), HuginnMindResponse::Refused(malformed)); // And the same reads answer rather than refuse when they are asked // something answerable, so the arms above are refusing on the mind's // word and not on their own shape. let absent = PipelineRef { kind: PipelineKind::Question, id: Short(format!("{CAMPAIGN}:question:Q1")) }; let present = HuginnMindRequest::View { instance: slug(INSTANCE), id: absent }; assert_eq!(daemon.handle(present, now()).answered(), HuginnMindResponse::View(None)); let plain = HuginnMindRequest::Query { instance: slug(INSTANCE), selection: Selection::default(), semantic: None }; let HuginnMindResponse::Query(page) = daemon.handle(plain, now()).answered() else { panic!("expected a page") }; assert_eq!(page.matched, 1); } /// A refusal of the selection crosses the dispatch as itself: typed, on /// the field the client sent, and never swallowed into an empty page or /// rewrapped as an unavailable store. A stale cursor is the organ's own /// defect, so a valid walk never meets it. #[test] fn a_selection_refusal_crosses_the_dispatch_as_itself() { let (_root, mut daemon) = seeded(); let ask = |daemon: &mut Daemon, selection: Selection| { daemon.handle(HuginnMindRequest::Query { instance: slug(INSTANCE), selection, semantic: None }, now()).answered() }; let predicate = |index: &str, value: &str| Selection { fields: Some(vec![cultnet_rs::FieldPredicate { index: index.into(), op: "any_of".into(), values: Some(vec![value.into()]), number: None, }]), ..Selection::default() }; let HuginnMindResponse::Refused(MindRefusal::SelectionInvalid { field, .. }) = ask(&mut daemon, predicate("nonsense", "x")) else { panic!("an undeclared alias is the substrate's refusal"); }; assert_eq!(field, "fields[0].index"); let HuginnMindResponse::Refused(MindRefusal::SelectionInvalid { field, value, .. }) = ask(&mut daemon, predicate("repo", "a/b/c")) else { panic!("a value outside its domain is the organ's refusal"); }; assert_eq!((field.as_str(), value.as_deref()), ("fields[0].values", Some("a/b/c"))); let garbled = Selection { cursor: Some("not a cursor".into()), ..Selection::default() }; assert!(matches!(ask(&mut daemon, garbled), HuginnMindResponse::Refused(MindRefusal::CursorInvalid { .. }))); assert_eq!(ask(&mut daemon, predicate("repo", "a/b/c")).status(), "rejected"); } /// 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()).answered(); let HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { writes, .. }) = outcome else { panic!("expected a commit"); }; let landed: Vec = 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 = open_unindexed(root.path(), &slug(INSTANCE)).unwrap().with_index(FailingIndex); fresh.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now()).answered(); let outcome = fresh.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![question, ruling])), now()).answered(); 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()).answered(); 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 opening it 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 = open_unindexed(root.path(), &slug(INSTANCE)).unwrap(); let path = Mind::::store_path_for(root.path(), &slug(INSTANCE)).unwrap(); assert_eq!( open_unindexed(root.path(), &slug(INSTANCE)).err(), Some(MindRefusal::MindAlreadyOwned { path: path.display().to_string() }) ); drop(held); let mut planted = open_unindexed(root.path(), &slug(INSTANCE)).unwrap(); planted.handle(HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)])), now()).answered(); drop(planted); let moved = Mind::::store_path_for(root.path(), &slug(OTHER)).unwrap(); std::fs::create_dir_all(moved.parent().unwrap()).unwrap(); std::fs::copy(&path, &moved).unwrap(); assert_eq!( open_unindexed(root.path(), &slug(OTHER)).err(), Some(MindRefusal::ForeignInstance { declared: OTHER.into(), mind: INSTANCE.into() }) ); } /// A roots read through the daemon's handle answers from the mind it serves: the campaign with its repos in force and the /// instance's own root with none; a request naming another instance is refused at the door, as every read is, before the mind /// is asked. #[test] fn roots_touched_answers_on_the_wire() { let (_root, mut daemon) = seeded_wide(); let HuginnMindResponse::RootsTouched(roots) = daemon.handle(HuginnMindRequest::RootsTouched { instance: slug(INSTANCE) }, now()).answered() else { panic!("a roots read answers the roots"); }; let by_root = |name: &str| roots.iter().find(|root| root.root.0 == name).unwrap_or_else(|| panic!("{name} in {roots:?}")); assert_eq!(by_root(CAMPAIGN).repos, [OrgRepo(REPO.into())]); assert!(by_root(INSTANCE).repos.is_empty()); assert_eq!(roots.len(), 2, "{roots:?}"); let bytes = rmp_serde::to_vec_named(&HuginnMindResponse::RootsTouched(roots.clone())).unwrap(); assert_eq!(rmp_serde::from_slice::(&bytes).unwrap(), HuginnMindResponse::RootsTouched(roots)); for declared in [OTHER, NEAR, PREFIXED, CASED] { assert_eq!( daemon.handle(HuginnMindRequest::RootsTouched { instance: slug(declared) }, now()).answered(), HuginnMindResponse::Refused(MindRefusal::ForeignInstance { declared: declared.into(), mind: INSTANCE.into() }), "{declared}" ); } } fn activity_request(repos: &[&str]) -> HuginnMindRequest { HuginnMindRequest::StewardedActivity { instance: slug(INSTANCE), repos: repos.iter().map(|repo| OrgRepo::from(*repo)).collect(), after: Some(1) } } /// A daemon without a forge refuses the activity read as unavailable and serves everything else; one that is asked about too many /// repos, or a name outside `Org/Repo`, refuses by field and echoes no value. #[test] fn no_forge_url_answers_unavailable() { let (_root, mut daemon) = seeded(); let answer = daemon.handle(activity_request(&["GameCult/Huginn"]), now()).answered(); assert!(matches!(answer, HuginnMindResponse::Refused(MindRefusal::Unavailable { .. })), "{answer:?}"); assert!(matches!(daemon.handle(HuginnMindRequest::Whoami, now()).answered(), HuginnMindResponse::Whoami(_))); let exactly: Vec = (0..ACTIVITY_REPOS_MAX).map(|n| format!("GameCult/r{n}")).collect(); let exactly: Vec<&str> = exactly.iter().map(String::as_str).collect(); let answer = daemon.handle(activity_request(&exactly), now()).answered(); assert!(matches!(answer, HuginnMindResponse::Refused(MindRefusal::Unavailable { .. })), "64 repos are within the cap and reach the (absent) forge: {answer:?}"); let many: Vec = (0..=ACTIVITY_REPOS_MAX).map(|n| format!("GameCult/r{n}")).collect(); let many: Vec<&str> = many.iter().map(String::as_str).collect(); for (request, canary) in [(activity_request(&many), "r64"), (activity_request(&["not a repo CANARY"]), "CANARY")] { let answer = daemon.handle(request, now()).answered(); let HuginnMindResponse::Refused(MindRefusal::SelectionInvalid { field, value, message }) = &answer else { panic!("{answer:?}") }; assert_eq!((field.as_str(), value), ("repos", &None)); assert!(!message.contains(canary), "{message}"); } } /// A forge that never answers refuses the read inside the bound, and the mind answers the next request at once: the one thread /// the daemon serves on is not held. #[test] fn a_hung_forge_answers_unavailable_within_its_bound() { let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let base = format!("http://{}", listener.local_addr().unwrap()); let _held = std::thread::spawn(move || listener.incoming().map(|stream| stream.unwrap()).collect::>()); let (_root, daemon) = seeded(); let mut daemon = daemon.with_forge(crate::sources::forge::HttpForge::new(&base)); let started = std::time::Instant::now(); let answer = daemon.handle(activity_request(&["GameCult/Huginn"]), now()).answered(); assert!(matches!(answer, HuginnMindResponse::Refused(MindRefusal::Unavailable { .. })), "{answer:?}"); assert!(started.elapsed() < std::time::Duration::from_secs(3), "{:?}", started.elapsed()); let started = std::time::Instant::now(); assert!(matches!(daemon.handle(HuginnMindRequest::Whoami, now()).answered(), HuginnMindResponse::Whoami(_))); assert!(started.elapsed() < std::time::Duration::from_millis(500)); } }