From 20424fb8fb549285f855cc7bc3d78ddd13d54f97 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Fri, 23 Oct 2026 07:18:08 +0530 Subject: [PATCH 1/2] test(consequence): a host denial turns a service-level approval into a decline Pins review_approval at the service: a task paused at NeedsApproval, approved over ContinueTask, is declined when the host's consequence review denies it; the host is asked once and no run is allowed to act. --- .../tinybus_module/tinybus_module_tests.rs | 1 + .../consequence_review_tests.rs | 157 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs diff --git a/crates/tinycomputer/src/tinybus_module/tinybus_module_tests.rs b/crates/tinycomputer/src/tinybus_module/tinybus_module_tests.rs index a59f84a71..b2d4fda1b 100644 --- a/crates/tinycomputer/src/tinybus_module/tinybus_module_tests.rs +++ b/crates/tinycomputer/src/tinybus_module/tinybus_module_tests.rs @@ -12,6 +12,7 @@ use serde_json::json; mod browser_tests; mod config_tests; +mod consequence_review_tests; mod manifest_tests; mod tasks_tests; mod wire_tests; diff --git a/crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs b/crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs new file mode 100644 index 000000000..14139e5a1 --- /dev/null +++ b/crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs @@ -0,0 +1,157 @@ +//! The host's consequence review, at the service: an approval the host +//! denies reaches the task as a decline. + +use std::collections::VecDeque; +use std::sync::{Arc, Mutex}; + +use serde_json::json; +use tinybus::Interface; +use tinycomputer_bus::agent::{AgentResponse, TaskConstraints, TaskId, TaskStatus, TaskView}; +use tinycomputer_bus::consequence_host::{ReviewActionRequest, ReviewActionResponse}; +use tinycomputer_bus::{ + DesktopResponse, FlowRunResult, FlowStopReason, JevMetrics, JevTarget, RunFlowRequest, + StepOutcome, StepReport, names, +}; +use tinycomputer_engine::{FlowFuture, FlowRunner, Tasks}; + +use super::service; +use crate::tinybus_module::consequence_gate::{ConsequencePort, PortFuture}; + +/// A runner whose first flow stops before pressing "Send", and every later +/// one completes. It records whether each run was allowed to act. +#[derive(Default)] +struct GatedOnce { + replies: Mutex>, + allowed: Mutex>, +} + +fn run(stop: FlowStopReason, steps: Vec, pending: Option<&str>) -> FlowRunResult { + FlowRunResult { + stop, + steps, + vars: Default::default(), + pending: pending.map(|name| JevTarget { + ref_id: "e9".to_owned(), + role: "button".to_owned(), + name: Some(name.to_owned()), + }), + learned: Vec::new(), + actions: 1, + metrics: JevMetrics::default(), + trace: Vec::new(), + dialog_left_open: false, + } +} + +impl FlowRunner for GatedOnce { + fn run(&self, _: &TaskId, _: &TaskConstraints, request: RunFlowRequest) -> FlowFuture { + self.allowed.lock().unwrap().push(request.allow_destructive); + let result = self + .replies + .lock() + .unwrap() + .pop_front() + .unwrap_or_else(|| run(FlowStopReason::Completed, Vec::new(), None)); + let reply = DesktopResponse::ok("run-flow", serde_json::to_value(result).unwrap()); + Box::pin(async move { reply }) + } +} + +#[derive(Debug, Default)] +struct DenyingHost { + seen: Mutex>, +} + +impl ConsequencePort for Arc { + fn review(&self, request: ReviewActionRequest) -> PortFuture<'_, ReviewActionResponse> { + self.seen.lock().unwrap().push(request); + Box::pin(async { + Ok(ReviewActionResponse { + allow: false, + reason: Some("not today".to_owned()), + }) + }) + } +} + +async fn awaited(service: &crate::tinybus_module::DesktopService, id: &TaskId) -> TaskView { + let settled = service + .call( + &names::methods::AWAIT_TASK.try_into().unwrap(), + json!([{"id": id, "timeout_ms": 5_000}]), + ) + .await + .unwrap(); + serde_json::from_value::>(settled) + .unwrap() + .data + .unwrap() +} + +#[tokio::test] +async fn a_host_denial_turns_an_approval_into_a_decline() { + let runner = Arc::new(GatedOnce::default()); + runner.replies.lock().unwrap().push_back(run( + FlowStopReason::StoppedBeforeDestructive, + vec![StepReport { + path: "1".to_owned(), + kind: "stop_before".to_owned(), + text: "sending the email".to_owned(), + outcome: StepOutcome::Gated, + turns: 1, + jev_calls: 1, + actions: Vec::new(), + loops: Vec::new(), + confidence: None, + note: "found it".to_owned(), + }], + Some("Send"), + )); + let host = Arc::new(DenyingHost::default()); + let mut service = service().with_consequence_review(Box::new(host.clone())); + service.tasks = Arc::new(Tasks::new(runner.clone())); + + let started = service + .call( + &names::methods::START_TASK.try_into().unwrap(), + json!([{"flow": {"app": "Mail", "steps": [ + {"stop_before": "sending the email"}, + "start another email message" + ]}}]), + ) + .await + .unwrap(); + let id = serde_json::from_value::>(started) + .unwrap() + .data + .unwrap() + .id; + let paused = awaited(&service, &id).await; + assert!( + matches!(paused.status, TaskStatus::NeedsApproval { ref target, .. } if target == "Send"), + "{:?}", + paused.status + ); + + service + .call( + &names::methods::CONTINUE_TASK.try_into().unwrap(), + json!([{"id": id, "approve": true}]), + ) + .await + .unwrap(); + + assert_eq!(awaited(&service, &id).await.status, TaskStatus::Cancelled); + let seen = host.seen.lock().unwrap(); + assert_eq!(seen.len(), 1, "the host is asked once"); + assert_eq!(seen[0].target, "Send"); + assert!( + !runner + .allowed + .lock() + .unwrap() + .iter() + .any(|allowed| *allowed), + "no run was allowed to take the denied action" + ); +} From 5a29fd0104782a0c460d192ed69552fa1a35daef Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Fri, 23 Oct 2026 08:27:23 +0530 Subject: [PATCH 2/2] test(consequence): build the flow result's vars with BTreeMap::new clippy::default_trait_access under -D warnings. --- .../tinybus_module_tests/consequence_review_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs b/crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs index 14139e5a1..d73214524 100644 --- a/crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs +++ b/crates/tinycomputer/src/tinybus_module/tinybus_module_tests/consequence_review_tests.rs @@ -29,7 +29,7 @@ fn run(stop: FlowStopReason, steps: Vec, pending: Option<&str>) -> F FlowRunResult { stop, steps, - vars: Default::default(), + vars: std::collections::BTreeMap::new(), pending: pending.map(|name| JevTarget { ref_id: "e9".to_owned(), role: "button".to_owned(),