agentmux_srv/server/
vault_ask.rs1use std::time::Duration;
11
12use crate::backend::vault::agent_set;
13use crate::backend::vault::uses::{self, Ended, UseAnswer, UseRequest};
14
15use super::browser_vault_fill::Waiting;
16use super::AppState;
17
18const GRACE: Duration = Duration::from_secs(5);
21
22const ASKED: &str = "asked to use it";
24
25pub(super) async fn ask(state: &AppState, attention_block: &str, mut request: UseRequest, wait: Duration, headline: &str) -> (Ended, Option<UseAnswer>) {
31 request.block_id = shown_pane(state, &request.block_id);
32 agent_set::apply(&state.identity_store, &mut request);
34 let was_unseen = !request.set_by.is_empty();
35 let (id, entry, label, agent) = (request.id.clone(), request.entry_id.clone(), request.entry_label.clone(), request.agent.clone());
36 let agent_reason = request.agent_reason.clone();
37 let target = target_of(&request);
38 let attention_block = if request.block_id.is_empty() { attention_block.to_string() } else { request.block_id.clone() };
39 let headline = notice(headline, uses::goes_ahead(&request));
40 let answer = uses::uses().ask(request, wait);
41 let _waiting = Waiting::new(id, state.clone());
42 super::vault_handlers::publish(state);
43 let _asking = crate::backend::user_attention::Asking::start(&state.broker, &state.mstore, &attention_block, "vault", &headline);
44 let answer = match tokio::time::timeout(wait + GRACE, answer).await {
45 Ok(Ok(a)) => Some(a),
46 Ok(Err(_)) => Some(UseAnswer::of(uses::Decision::Deny)),
48 Err(_) => None,
49 };
50 let mut ended = Ended::of(answer.as_ref());
51 let replaced_meanwhile = !was_unseen && agent_set::is_unseen(&state.identity_store, &entry);
54 if replaced_meanwhile {
55 ended = match ended {
56 Ended::WentAhead => Ended::NoAnswer,
57 Ended::Always => Ended::Once,
58 other => other,
59 };
60 }
61 if matches!(ended, Ended::Once | Ended::Always) && !replaced_meanwhile {
63 agent_set::clear(&state.identity_store, &entry);
64 }
65 if ended == Ended::WentAhead {
66 let line = crate::backend::vault::Line { entry_id: &entry, label: &label, actor: &agent, action: ASKED, target: &target, outcome: uses::WENT_AHEAD_OUTCOME, reason: &agent_reason };
67 crate::backend::vault::record_line(&state.identity_store, line);
68 }
69 (ended, answer)
70}
71
72fn notice(headline: &str, goes_ahead: bool) -> String {
76 if goes_ahead {
77 format!("{headline}: goes ahead in {} s unless you deny", uses::VETO.as_secs())
78 } else {
79 format!("{headline}: needs your yes")
80 }
81}
82
83pub(super) fn with_approval(mut out: serde_json::Value, went_ahead: bool) -> serde_json::Value {
86 if went_ahead {
87 out["approval"] = serde_json::json!(uses::WENT_AHEAD);
88 }
89 out
90}
91
92pub(super) fn approval(labels: &[String]) -> Option<String> {
95 (!labels.is_empty()).then(|| format!("{} ({})", uses::WENT_AHEAD, labels.join(", ")))
96}
97
98fn target_of(r: &UseRequest) -> String {
100 if !r.command.is_empty() {
101 r.command.chars().take(160).collect()
102 } else if r.field.is_empty() {
103 r.origin.clone()
104 } else {
105 format!("{} ({})", r.origin, r.field)
106 }
107}
108
109pub(super) fn shown_pane(state: &AppState, block_id: &str) -> String {
113 let shown = !block_id.is_empty() && crate::backend::notify::router::resolve_click_target(&state.mstore, block_id).is_some_and(|t| !t.window_ids.is_empty());
114 if shown {
115 block_id.to_string()
116 } else {
117 String::new()
118 }
119}
120
121pub(super) fn agent_pane(state: &AppState, caller: Option<&crate::server::caller::Caller>, auth: &agentmux_common::api_types::UiAutomationAuth) -> String {
124 super::ui_handlers::verified_block_id(state, caller, auth).unwrap_or_default()
125}
126
127#[cfg(test)]
128mod tests {
129 use super::notice;
130
131 #[test]
132 fn the_notice_says_what_silence_does() {
133 assert_eq!(notice("Builder wants to use Stripe", true), "Builder wants to use Stripe: goes ahead in 30 s unless you deny");
134 assert_eq!(notice("Builder wants to fill your passport", false), "Builder wants to fill your passport: needs your yes");
135 }
136}