Add EscalateGoal wire request, schema, and daemon enforcement
Add HuginnMindRequest::EscalateGoal (accepted by Door::Persona while Admit(_) remains NotThisDoor), regenerate huginn.mind_request.v1.schema.json, and enforce owner=self-run, agent=mind-persona, locations=[], source=<campaign>:campaign:self, and no imported_from in Daemon::handle.
This commit is contained in:
parent
70079777a8
commit
7162412bc2
3 changed files with 280 additions and 14 deletions
|
|
@ -6,9 +6,12 @@ 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::{AccountId, PipelineRef, Short, Slug};
|
||||
use huginn_mind::eureka_pipeline::{AccountId, PipelineDocument, PipelineFollowUp, PipelineKind, PipelineRef, Short, 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};
|
||||
use huginn_mind::{
|
||||
ChatHits, ChatQuery, ChatWindow, ChatWindowQuery, Members, Mind, MindRefusal, MindStore, PipelineAdmissionBatch,
|
||||
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
|
||||
|
|
@ -213,15 +216,43 @@ impl<S: MindStore, I: IndexSink<S>> Daemon<S, I> {
|
|||
}
|
||||
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::<Vec<_>>().join(", ");
|
||||
eprintln!("huginn: the index refused the landed writes [{refs}]: {error:#}");
|
||||
HuginnMindRequest::Admit(batch) => self.admit_and_index(batch, now),
|
||||
HuginnMindRequest::EscalateGoal {
|
||||
instance,
|
||||
mut provenance,
|
||||
campaign,
|
||||
label,
|
||||
repo,
|
||||
item,
|
||||
why_it_can_wait,
|
||||
origin,
|
||||
} => {
|
||||
if provenance.imported_from.is_some() {
|
||||
return Handled::Answered(HuginnMindResponse::Refused(MindRefusal::SelectionInvalid {
|
||||
field: "provenance.imported_from".into(),
|
||||
value: None,
|
||||
message: "an escalated goal cannot carry imported_from".into(),
|
||||
}));
|
||||
}
|
||||
HuginnMindResponse::Admit(outcome)
|
||||
provenance.agent = Short("mind-persona".into());
|
||||
let follow_up = PipelineDocument::FollowUp(PipelineFollowUp {
|
||||
source: PipelineRef {
|
||||
kind: PipelineKind::Campaign,
|
||||
id: Short(format!("{}:campaign:self", campaign.0)),
|
||||
},
|
||||
campaign,
|
||||
label,
|
||||
repo,
|
||||
locations: Vec::new(),
|
||||
item,
|
||||
why_it_can_wait,
|
||||
owner: Short("self-run".into()),
|
||||
origin,
|
||||
});
|
||||
self.admit_and_index(
|
||||
PipelineAdmissionBatch { instance, provenance, documents: vec![follow_up] },
|
||||
now,
|
||||
)
|
||||
}
|
||||
HuginnMindRequest::View { id, .. } => match self.mind.view(&id) {
|
||||
Ok(view) => HuginnMindResponse::View(view.map(Box::new)),
|
||||
|
|
@ -308,6 +339,17 @@ impl<S: MindStore, I: IndexSink<S>> Daemon<S, I> {
|
|||
}
|
||||
}
|
||||
|
||||
fn admit_and_index(&mut self, batch: PipelineAdmissionBatch, now: DateTime<Utc>) -> HuginnMindResponse {
|
||||
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)
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
|
@ -1102,6 +1144,119 @@ pub(crate) mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn daemon_escalate_goal_enforces_persona_door_bounds() {
|
||||
use huginn_mind::eureka_pipeline::{Label, MemoryOrigin, PipelineFollowUp};
|
||||
use huginn_mind::ImportSource;
|
||||
|
||||
let indexed = Arc::new(Mutex::new(Vec::<PipelineRef>::new()));
|
||||
struct RecordingIndex(Arc<Mutex<Vec<PipelineRef>>>);
|
||||
impl<S: MindStore> IndexSink<S> for RecordingIndex {
|
||||
fn committed(&mut self, _mind: &Mind<S>, writes: &[PipelineRef]) -> anyhow::Result<()> {
|
||||
self.0.lock().unwrap().extend(writes.iter().cloned());
|
||||
Ok(())
|
||||
}
|
||||
fn status(&self) -> IndexStatus { IndexStatus::Current }
|
||||
fn search(&mut self, _t: &str, _k: u32, _a: Vec<String>) -> Result<SearchTicket, String> { Err("unused".into()) }
|
||||
fn search_sources(&mut self, _q: &SourceQuery) -> Result<SearchTicket, String> { Err("unused".into()) }
|
||||
fn search_chat(&mut self, _q: &ChatQuery) -> Result<SearchTicket, String> { Err("unused".into()) }
|
||||
fn chat_window(&mut self, _q: &ChatWindowQuery) -> Result<SearchTicket, String> { Err("unused".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<Hits, String>)> { Vec::new() }
|
||||
fn abandon(&mut self, _t: SearchTicket) {}
|
||||
}
|
||||
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let mind = Mind::open(root.path(), &slug(INSTANCE)).unwrap();
|
||||
let mut daemon = Daemon::new(mind, RecordingIndex(Arc::clone(&indexed)));
|
||||
let seed = daemon.handle_at(Door::Full, HuginnMindRequest::Admit(batch(INSTANCE, campaign_seed())), now()).answered();
|
||||
assert!(matches!(seed, HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { .. })), "{seed:?}");
|
||||
indexed.lock().unwrap().clear();
|
||||
|
||||
let admit = HuginnMindRequest::Admit(batch(INSTANCE, vec![identity(INSTANCE)]));
|
||||
assert_eq!(daemon.handle_at(Door::Persona, admit, now()).answered(), HuginnMindResponse::Refused(MindRefusal::NotThisDoor));
|
||||
|
||||
let mut caller_prov = provenance();
|
||||
caller_prov.agent = Short("forged-agent".into());
|
||||
let escalate = HuginnMindRequest::EscalateGoal {
|
||||
instance: slug(INSTANCE),
|
||||
provenance: caller_prov,
|
||||
campaign: slug(CAMPAIGN),
|
||||
label: Label("esc-door-1".into()),
|
||||
repo: OrgRepo(REPO.into()),
|
||||
item: Line("Check lock stall.".into()),
|
||||
why_it_can_wait: Line("Next Self run.".into()),
|
||||
origin: Some(MemoryOrigin::Direct { room: Short("!dm:gamecult.org".into()), with: vec![Short("@meta:gamecult.org".into())] }),
|
||||
};
|
||||
let HuginnMindResponse::Admit(PipelineAdmissionOutcome::Committed { writes, .. }) =
|
||||
daemon.handle_at(Door::Persona, escalate, now()).answered()
|
||||
else {
|
||||
panic!("EscalateGoal must commit through Door::Persona");
|
||||
};
|
||||
assert_eq!(writes.len(), 1);
|
||||
assert_eq!(indexed.lock().unwrap().as_slice(), writes.as_slice());
|
||||
|
||||
let HuginnMindResponse::View(Some(view)) =
|
||||
daemon.handle_at(Door::Persona, HuginnMindRequest::View { instance: slug(INSTANCE), id: writes[0].clone() }, now()).answered()
|
||||
else {
|
||||
panic!("committed FollowUp must be viewable");
|
||||
};
|
||||
assert_eq!(view.admission.provenance.agent.0, "mind-persona");
|
||||
assert!(view.admission.provenance.imported_from.is_none());
|
||||
let PipelineDocument::FollowUp(PipelineFollowUp { source, owner, locations, origin, .. }) = view.document else {
|
||||
panic!("expected FollowUp");
|
||||
};
|
||||
assert_eq!(owner.0, "self-run");
|
||||
assert!(locations.is_empty());
|
||||
assert_eq!(source, PipelineRef { kind: PipelineKind::Campaign, id: Short(format!("{CAMPAIGN}:campaign:self")) });
|
||||
assert_eq!(origin, Some(MemoryOrigin::Direct { room: Short("!dm:gamecult.org".into()), with: vec![Short("@meta:gamecult.org".into())] }));
|
||||
|
||||
// Foreign instance refused before admission.
|
||||
let foreign = HuginnMindRequest::EscalateGoal {
|
||||
instance: slug(OTHER),
|
||||
provenance: provenance(),
|
||||
campaign: slug(CAMPAIGN),
|
||||
label: Label("esc-foreign".into()),
|
||||
repo: OrgRepo(REPO.into()),
|
||||
item: Line("Foreign.".into()),
|
||||
why_it_can_wait: Line("Wait.".into()),
|
||||
origin: None,
|
||||
};
|
||||
assert_eq!(
|
||||
daemon.handle_at(Door::Persona, foreign, now()).answered(),
|
||||
HuginnMindResponse::Refused(MindRefusal::ForeignInstance { declared: OTHER.into(), mind: INSTANCE.into() })
|
||||
);
|
||||
|
||||
// imported_from refused.
|
||||
let mut with_import = provenance();
|
||||
with_import.imported_from = Some(ImportSource {
|
||||
instance: slug(OTHER),
|
||||
receipt_id: Short("mind-commit-1".into()),
|
||||
ordinal: 1,
|
||||
committed_at: Short("2026-10-10T10:00:00Z".into()),
|
||||
faculty: Faculty::Hands,
|
||||
agent: Short("importer".into()),
|
||||
session: Short("s1".into()),
|
||||
tool: Short("huginn".into()),
|
||||
external: vec![],
|
||||
});
|
||||
let imported = HuginnMindRequest::EscalateGoal {
|
||||
instance: slug(INSTANCE),
|
||||
provenance: with_import,
|
||||
campaign: slug(CAMPAIGN),
|
||||
label: Label("esc-import".into()),
|
||||
repo: OrgRepo(REPO.into()),
|
||||
item: Line("Imported.".into()),
|
||||
why_it_can_wait: Line("Wait.".into()),
|
||||
origin: None,
|
||||
};
|
||||
assert!(matches!(
|
||||
daemon.handle_at(Door::Persona, imported, now()).answered(),
|
||||
HuginnMindResponse::Refused(MindRefusal::SelectionInvalid { ref field, .. }) if field == "provenance.imported_from"
|
||||
));
|
||||
}
|
||||
|
||||
/// Every read the persona door answers reaches the mind there: none of the
|
||||
/// source, chat, activity and roots reads is refused as `NotThisDoor`.
|
||||
/// What the mind then says (a search, or `Unavailable` with no index or
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
use cultcache_rs::DatabaseEntry;
|
||||
use cultnet_rs::{CultMeshCdnArtifactManifest, Selection};
|
||||
use eureka_pipeline::{Line, OrgRepo, PIPELINE_SCHEMA_EPOCH, PipelineKind, PipelineRef, Short, Slug};
|
||||
use eureka_pipeline::{Label, Line, MemoryOrigin, OrgRepo, PIPELINE_SCHEMA_EPOCH, PipelineKind, PipelineRef, Short, Slug};
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
|
@ -108,6 +108,17 @@ pub enum HuginnMindRequest {
|
|||
/// Every root the mind holds a document under, with its newest admission and a campaign's repos in force: the mind's own
|
||||
/// records, one pass at the head.
|
||||
RootsTouched { instance: Slug },
|
||||
/// Escalates a follow-up brief from a Persona session to Self on the persona door.
|
||||
EscalateGoal {
|
||||
instance: Slug,
|
||||
provenance: PipelineProvenance,
|
||||
campaign: Slug,
|
||||
label: Label,
|
||||
repo: OrgRepo,
|
||||
item: Line,
|
||||
why_it_can_wait: Line,
|
||||
origin: Option<MemoryOrigin>,
|
||||
},
|
||||
}
|
||||
|
||||
/// The most repos one activity read names.
|
||||
|
|
@ -427,6 +438,7 @@ impl HuginnMindRequest {
|
|||
Self::ChatWindow { .. } => "chat_window",
|
||||
Self::StewardedActivity { .. } => "stewarded_activity",
|
||||
Self::RootsTouched { .. } => "roots_touched",
|
||||
Self::EscalateGoal { .. } => "escalate_goal",
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -447,7 +459,8 @@ impl HuginnMindRequest {
|
|||
| Self::SearchChat { .. }
|
||||
| Self::ChatWindow { .. }
|
||||
| Self::StewardedActivity { .. }
|
||||
| Self::RootsTouched { .. } => true,
|
||||
| Self::RootsTouched { .. }
|
||||
| Self::EscalateGoal { .. } => true,
|
||||
Self::Admit(_) => false,
|
||||
}
|
||||
}
|
||||
|
|
@ -465,7 +478,8 @@ impl HuginnMindRequest {
|
|||
| Self::SearchChat { instance, .. }
|
||||
| Self::ChatWindow { instance, .. }
|
||||
| Self::StewardedActivity { instance, .. }
|
||||
| Self::RootsTouched { instance } => Some(instance),
|
||||
| Self::RootsTouched { instance }
|
||||
| Self::EscalateGoal { instance, .. } => Some(instance),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -626,6 +640,19 @@ mod tests {
|
|||
},
|
||||
HuginnMindRequest::ChatWindow { instance: slug(INSTANCE), query: ChatWindowQuery { event_id: Short("$a".into()), before: 5, after: 0 } },
|
||||
HuginnMindRequest::RootsTouched { instance: slug(INSTANCE) },
|
||||
HuginnMindRequest::EscalateGoal {
|
||||
instance: slug(INSTANCE),
|
||||
provenance: provenance(Faculty::Member),
|
||||
campaign: slug(CAMPAIGN),
|
||||
label: Label("esc-1".into()),
|
||||
repo: OrgRepo("GameCult/Huginn".into()),
|
||||
item: Line("Investigate lock stall.".into()),
|
||||
why_it_can_wait: Line("Next Self run.".into()),
|
||||
origin: Some(MemoryOrigin::Room {
|
||||
rooms: Short("#swarm:gamecult.org".into()),
|
||||
heard_from: vec![Short("@alice:gamecult.org".into())],
|
||||
}),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
|
|
@ -713,13 +740,42 @@ mod tests {
|
|||
#[test]
|
||||
fn the_wire_vocabulary_is_the_minds_methods_and_status_is_derived_from_the_response() {
|
||||
let names: Vec<&str> = requests().iter().map(|request| request.operation()).collect();
|
||||
assert_eq!(names, ["whoami", "admit", "view", "query", "persona_get", "persona_put", "search_sources", "search_sources", "search_chat", "chat_window", "roots_touched"]);
|
||||
assert_eq!(
|
||||
names,
|
||||
[
|
||||
"whoami",
|
||||
"admit",
|
||||
"view",
|
||||
"query",
|
||||
"persona_get",
|
||||
"persona_put",
|
||||
"search_sources",
|
||||
"search_sources",
|
||||
"search_chat",
|
||||
"chat_window",
|
||||
"roots_touched",
|
||||
"escalate_goal",
|
||||
]
|
||||
);
|
||||
for request in requests() {
|
||||
assert_eq!(request.reaches_persona_door(), !matches!(request, HuginnMindRequest::Admit(_)), "{request:?}");
|
||||
let declared = request.instance().is_none();
|
||||
assert_eq!(declared, matches!(request, HuginnMindRequest::Whoami), "{request:?}");
|
||||
let bytes = rmp_serde::to_vec_named(&request).unwrap();
|
||||
assert_eq!(rmp_serde::from_slice::<HuginnMindRequest>(&bytes).unwrap(), request);
|
||||
}
|
||||
let escalate_no_origin = HuginnMindRequest::EscalateGoal {
|
||||
instance: slug(INSTANCE),
|
||||
provenance: provenance(Faculty::Member),
|
||||
campaign: slug(CAMPAIGN),
|
||||
label: Label("esc-2".into()),
|
||||
repo: OrgRepo("GameCult/Huginn".into()),
|
||||
item: Line("Follow up.".into()),
|
||||
why_it_can_wait: Line("Next run.".into()),
|
||||
origin: None,
|
||||
};
|
||||
let bytes = rmp_serde::to_vec_named(&escalate_no_origin).unwrap();
|
||||
assert_eq!(rmp_serde::from_slice::<HuginnMindRequest>(&bytes).unwrap(), escalate_no_origin);
|
||||
assert_eq!(
|
||||
requests()[3].instance(),
|
||||
Some(&slug(OTHER_INSTANCE)),
|
||||
|
|
|
|||
|
|
@ -264,6 +264,61 @@
|
|||
"required": [
|
||||
"RootsTouched"
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "Escalates a follow-up brief from a Persona session to Self on the persona door.",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"EscalateGoal": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"campaign": {
|
||||
"$ref": "#/$defs/Slug"
|
||||
},
|
||||
"instance": {
|
||||
"$ref": "#/$defs/Slug"
|
||||
},
|
||||
"item": {
|
||||
"$ref": "#/$defs/Line"
|
||||
},
|
||||
"label": {
|
||||
"$ref": "#/$defs/Label"
|
||||
},
|
||||
"origin": {
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/$defs/MemoryOrigin"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"provenance": {
|
||||
"$ref": "#/$defs/PipelineProvenance"
|
||||
},
|
||||
"repo": {
|
||||
"$ref": "#/$defs/OrgRepo"
|
||||
},
|
||||
"why_it_can_wait": {
|
||||
"$ref": "#/$defs/Line"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"instance",
|
||||
"provenance",
|
||||
"campaign",
|
||||
"label",
|
||||
"repo",
|
||||
"item",
|
||||
"why_it_can_wait"
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"EscalateGoal"
|
||||
]
|
||||
}
|
||||
],
|
||||
"$defs": {
|
||||
|
|
|
|||
Loading…
Reference in a new issue