agentmux_srv/drone/executor/blocks/
approval.rs1use serde_json::{json, Value};
17
18use crate::backend::drone_approval::ApprovalItem;
19use crate::drone::data_flow::ExecutionScope;
20use crate::drone::types::FlowNode;
21
22pub const DEFAULT_TIMEOUT_S: u64 = 24 * 3600;
24const MAX_ITEMS: usize = 20;
26const MAX_TEXT: usize = 2000;
27
28#[derive(Debug)]
30pub struct Card {
31 pub title: String,
32 pub items: Vec<ApprovalItem>,
33}
34
35fn clip(text: String) -> String {
36 if text.chars().count() <= MAX_TEXT {
37 return text;
38 }
39 let mut cut: String = text.chars().take(MAX_TEXT).collect();
40 cut.push('…');
41 cut
42}
43
44fn items_of(node: &FlowNode, scope: &ExecutionScope) -> Result<Vec<ApprovalItem>, String> {
46 let pairs: Vec<(String, String)> = match node.data.get("show") {
47 None | Some(Value::Null) => Vec::new(),
48 Some(Value::Array(list)) => list
49 .iter()
50 .map(|e| {
51 let label = e.get("label").and_then(Value::as_str).unwrap_or_default().to_string();
52 let value = e.get("value").and_then(Value::as_str).ok_or("approval block: each `show` item is { label, value }")?;
53 Ok((label, value.to_string()))
54 })
55 .collect::<Result<_, String>>()?,
56 Some(Value::Object(map)) => map.iter().filter_map(|(k, v)| v.as_str().map(|v| (k.clone(), v.to_string()))).collect(),
57 Some(_) => return Err("approval block: `show` is a list of { label, value }".to_string()),
58 };
59 let pairs: Vec<(String, String)> = pairs.into_iter().filter(|(l, v)| !(l.trim().is_empty() && v.trim().is_empty())).collect();
61 if pairs.len() > MAX_ITEMS {
62 return Err(format!("approval block: `show` has at most {MAX_ITEMS} items"));
63 }
64 Ok(pairs
65 .into_iter()
66 .map(|(label, template)| {
67 let (tainted, derived) = scope.bits_of(&template);
68 ApprovalItem { label, text: clip(scope.resolve(&template)), tainted: tainted || derived }
69 })
70 .collect())
71}
72
73pub fn card(node: &FlowNode, scope: &ExecutionScope) -> Result<Card, String> {
75 let raw = node.data.get("title").and_then(Value::as_str).unwrap_or_default();
76 if raw.trim().is_empty() {
77 return Err("approval block: `title` says what is proposed, and is empty".to_string());
78 }
79 let (tainted, derived) = scope.bits_of(raw);
80 if tainted || derived {
81 return Err("approval block: `title` may not read outside text: put it in `show`, where it is marked".to_string());
82 }
83 Ok(Card { title: clip(scope.resolve(raw)), items: items_of(node, scope)? })
84}
85
86pub fn decide(answer: &Value) -> Result<Value, String> {
88 let by = answer.get("by").and_then(Value::as_str).unwrap_or("the person");
89 match answer.get("approve").and_then(Value::as_bool) {
90 Some(true) => Ok(json!({ "approved": true, "by": by })),
91 _ => Err(format!("approval_rejected: {by} said no")),
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 fn node(data: Value) -> FlowNode {
100 serde_json::from_value(json!({ "id": "ap", "position": { "x": 0, "y": 0 }, "data": data })).unwrap()
101 }
102
103 #[test]
104 fn the_card_marks_the_items_that_came_from_outside_text() {
105 let mut scope = ExecutionScope::for_drone("d1", json!({ "title": "a pull request title" }));
106 scope.outputs.insert("agent".into(), json!({ "output": "looks fine" }));
107 scope.bits.insert("agent".into(), (true, false));
108 let n = node(json!({
109 "kind": "approval", "title": "Merge the pull request",
110 "show": [{ "label": "Agent says", "value": "{{agent.output}}" }, { "label": "Fixed", "value": "the base branch" }],
111 }));
112 let card = card(&n, &scope).unwrap();
113 assert_eq!(card.title, "Merge the pull request");
114 assert_eq!(card.items.len(), 2);
115 assert_eq!((card.items[0].label.as_str(), card.items[0].text.as_str(), card.items[0].tainted), ("Agent says", "looks fine", true));
116 assert!(!card.items[1].tainted, "the graph's own text is clean");
117 }
118
119 #[test]
120 fn a_title_that_reads_outside_text_or_says_nothing_is_refused() {
121 let mut scope = ExecutionScope::for_drone("d1", json!({ "title": "x" }));
122 scope.outputs.insert("agent".into(), json!({ "output": "ok" }));
123 scope.bits.insert("agent".into(), (true, false));
124 let spoof = card(&node(json!({ "title": "Approve {{agent.output}}" })), &scope).unwrap_err();
125 assert!(spoof.contains("may not read outside text"), "{spoof}");
126 assert!(card(&node(json!({ "title": " " })), &scope).unwrap_err().contains("is empty"));
127 let many: Vec<Value> = (0..=MAX_ITEMS).map(|i| json!({ "label": i.to_string(), "value": "x" })).collect();
128 assert!(card(&node(json!({ "title": "t", "show": many })), &scope).unwrap_err().contains("at most"));
129 }
130
131 #[test]
132 fn a_long_text_is_cut_and_an_answer_is_a_yes_or_a_rejection_that_says_who() {
133 let scope = ExecutionScope::for_drone("d1", Value::Null);
134 let long = "x".repeat(MAX_TEXT + 50);
135 let c = card(&node(json!({ "title": "t", "show": { "Body": long } })), &scope).unwrap();
136 assert_eq!(c.items[0].text.chars().count(), MAX_TEXT + 1);
137 assert_eq!(decide(&json!({ "approve": true, "by": "operator" })).unwrap(), json!({ "approved": true, "by": "operator" }));
138 assert_eq!(decide(&json!({ "approve": false, "by": "operator" })).unwrap_err(), "approval_rejected: operator said no");
139 assert!(decide(&json!({})).unwrap_err().starts_with("approval_rejected"), "no clear yes is a no");
140 }
141}