From 0e590f36383c25aca410932d9082b24a9934a4ea Mon Sep 17 00:00:00 2001 From: Eureka Date: Tue, 10 Nov 2026 20:43:31 +0000 Subject: [PATCH] Desk wire envelope delta: failing tests first (empty deciders refused at decode, minItems 1, refusal round trip) Co-Authored-By: Claude Sonnet 5.5 --- crates/huginn-mind/src/desk_wire.rs | 30 +++++++++++--------- crates/huginn-mind/src/envelope.rs | 43 +++++++++++++++++++++++++++++ crates/huginn-mind/src/wire.rs | 2 +- 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/crates/huginn-mind/src/desk_wire.rs b/crates/huginn-mind/src/desk_wire.rs index 7863b5d..35382f4 100644 --- a/crates/huginn-mind/src/desk_wire.rs +++ b/crates/huginn-mind/src/desk_wire.rs @@ -1561,25 +1561,29 @@ mod tests { } #[test] - fn a_duplicate_decider_is_refused_naming_deciders_and_an_empty_list_decodes() { + fn an_empty_or_duplicated_decider_list_is_refused_at_decode_and_one_is_accepted() { let mut mind = desk(); let hash = hash_of(&mind, FORK); + let before = (head(&mind), stored_count(&mind, FORK)); let mut twice = list_of(3); twice.push(s("d1")); - for sent in desk_requests(INSTANCE, &twice, &hash) { - let failure = through_the_door(&sent).unwrap_err(); - assert_eq!(failure.code, "payload-not-a-request"); - assert!(failure.message.contains("deciders") && !failure.message.contains("d1"), "{}", failure.message); + for (what, list) in [("empty", vec![]), ("duplicate", twice)] { + for sent in desk_requests(INSTANCE, &list, &hash) { + let failure = through_the_door(&sent).unwrap_err(); + assert_eq!(failure.code, "payload-not-a-request", "{what} {}", sent.operation()); + assert!(failure.message.contains("deciders") && !failure.message.contains("d1"), "{what} {}: {}", sent.operation(), failure.message); + } } - // An empty list is a list: it decodes, and the Mind's showability gate is what says no. - let before = head(&mind); - let [answer, forks, fork] = desk_requests(INSTANCE, &[], &hash); - assert!(through_the_door(&forks).is_ok() && through_the_door(&fork).is_ok()); - let DeskRequest::Answer { subject, authority, deciders, request, .. } = through_the_door(&answer).unwrap() else { panic!("not an answer") }; - assert!(deciders.is_empty()); - let refused = not_stored(mind.desk_answer(&deciders, subject, authority, request, now()).0); + // One decider is a list. + for sent in desk_requests(INSTANCE, &list_of(1), &hash) { + assert_eq!(through_the_door(&sent).unwrap(), sent); + } + assert_eq!((head(&mind), stored_count(&mind, FORK)), before); + // The Mind's showability gate is the second line: an empty list handed to it directly + // still refuses the fork and stores nothing. + let refused = not_stored(mind.desk_answer(&[], s("d0"), DeskAuthority::Member, wire(FORK, &hash), now()).0); assert_eq!(refused.code, ForkRefusalCode::ForkUnknown); - assert_eq!(head(&mind), before); + assert_eq!((head(&mind), stored_count(&mind, FORK)), before); } #[test] diff --git a/crates/huginn-mind/src/envelope.rs b/crates/huginn-mind/src/envelope.rs index 2bf520d..a2e9e21 100644 --- a/crates/huginn-mind/src/envelope.rs +++ b/crates/huginn-mind/src/envelope.rs @@ -630,4 +630,47 @@ mod tests { assert!(wire(&message) <= DESK_MAX_RESPONSE_BYTES && wire(&message) > 4000, "{}", wire(&message)); assert_eq!(decode_desk_response(&message).unwrap().1, Ok(response)); } + + /// A desk call's refusal rides `huginn.desk_response.v1` whole: code and detail survive the + /// wire, and the published schema declares the variant. One row per refusal a desk call can + /// return, the four the door maps to `NotStored` included, because the response type can + /// carry any of them. + #[test] + fn every_refusal_a_desk_call_can_return_round_trips_losslessly() { + use crate::desk_wire::DESK_RESPONSE_SCHEMA_JSON; + use eureka_pipeline::{PipelineKind, PipelineRefusal}; + let refusals = [ + MindRefusal::Unavailable { detail: "the store did not answer".into() }, + MindRefusal::ForeignInstance { declared: OTHER.into(), mind: INSTANCE.into() }, + MindRefusal::MissingIdentity, + MindRefusal::DeskAnswerOutsideDesk { id: "eureka-state:desk_answer:Q1.n1".into() }, + MindRefusal::MissingReference { kind: PipelineKind::Question, id: "eureka-state:question:Q9".into() }, + MindRefusal::CampaignMoved { campaign: "eureka-state".into(), to: OTHER.into() }, + MindRefusal::DeskRequestInvalid { field: "desk_answer.note".into() }, + MindRefusal::DeskRequestIdConflict { request_id: "00000000-0000-4000-8000-000000000001".into() }, + MindRefusal::Document(PipelineRefusal::FieldBound { field: "note".into(), limit: 4000, actual: 4001 }), + MindRefusal::Document(PipelineRefusal::InvalidFormat { field: "request_id".into(), value: "x".into() }), + MindRefusal::Document(PipelineRefusal::InvalidIdentity { kind: PipelineKind::DeskAnswer, key: "k".into(), expected: "e".into() }), + MindRefusal::Document(PipelineRefusal::DuplicateRepo { field: "repos".into(), repo: "GameCult/Huginn".into() }), + MindRefusal::Document(PipelineRefusal::ForeignStore { r#type: "other".into() }), + ]; + let schema: serde_json::Value = serde_json::from_str(DESK_RESPONSE_SCHEMA_JSON).unwrap(); + assert!(schema["$defs"]["MindRefusal"].is_object() && schema["$defs"]["PipelineRefusal"].is_object()); + let declared = serde_json::to_string(&schema).unwrap(); + for refusal in refusals { + let response = DeskResponse::Refused(refusal.clone()); + let message = encode_desk_response("m-12", "answer", &response, "huginn-yggdrasil").unwrap(); + let (_, decoded) = decode_desk_response(&message).unwrap(); + assert_eq!(decoded, Ok(DeskResponse::Refused(refusal.clone())), "{refusal:?}"); + let value = serde_json::to_value(&refusal).unwrap(); + let variant = value.as_str().map(str::to_string).or_else(|| value.as_object().and_then(|object| object.keys().next().cloned())).unwrap(); + assert!(declared.contains(&format!("\"{variant}\"")), "the published schema declares {variant}"); + // Each detail field is declared too, so a validator can hold the payload to the schema. + if let Some(object) = value.get(&variant).and_then(|inner| inner.as_object()) { + for field in object.keys() { + assert!(declared.contains(&format!("\"{field}\"")), "{variant}.{field} is declared"); + } + } + } + } } diff --git a/crates/huginn-mind/src/wire.rs b/crates/huginn-mind/src/wire.rs index 3a5124a..8f7b10a 100644 --- a/crates/huginn-mind/src/wire.rs +++ b/crates/huginn-mind/src/wire.rs @@ -592,7 +592,7 @@ mod tests { assert_eq!(DESK_REQUEST_SCHEMA_JSON.replace("\r\n", "\n"), request); assert_eq!(DESK_RESPONSE_SCHEMA_JSON.replace("\r\n", "\n"), response); // The deciders bound is in the published schema, not only in the decoder. - assert!(request.contains("\"maxItems\": 64") && request.contains("\"uniqueItems\": true"), "deciders bound"); + assert!(request.contains("\"maxItems\": 64") && request.contains("\"uniqueItems\": true") && request.contains("\"minItems\": 1"), "deciders bound"); let index: serde_json::Value = serde_json::from_str(include_str!("../../../schemas/cultnet/index.json")).unwrap(); for (id, title) in [(DESK_REQUEST_SCHEMA, "Huginn desk request"), (DESK_RESPONSE_SCHEMA, "Huginn desk response")] { let rows: Vec<_> = index["schemas"].as_array().unwrap().iter().filter(|row| row["schemaId"] == id).collect();