1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! Owner approvals the agent kernel delivered for exact call subjects.
//!
//! A tool whose ask depends on state it reads before the call (the page a
//! browser action lands on, the app computer_use controls) judges the call
//! in `ToolSemantics::owner_approval_request`, and the kernel's resolver
//! hands the owner's answer back through `Tool::record_owner_answer`. The
//! tool keeps that approval here, for its session and the exact subject the
//! owner saw, and the call it approved consumes it. At execution the tool
//! judges the call again; an ask whose subject was not approved (the target
//! changed after the card, or the call never reached the kernel) is refused
//! rather than run or asked about inside the tool.
use std::collections::HashMap;
use std::sync::Mutex;
/// Approvals by (session, subject), each good for one call.
#[derive(Debug, Default)]
pub struct OwnerAnsweredCalls(Mutex<HashMap<(String, String), u32>>);
impl OwnerAnsweredCalls {
/// The owner approved `subject` in `session` for one call.
pub fn approve(&self, session: &str, subject: &str) {
*self
.0
.lock()
.unwrap_or_else(|e| e.into_inner())
.entry((session.to_string(), subject.to_string()))
.or_default() += 1;
}
/// Consume one approval of `subject` in `session`, if there is one.
pub fn take(&self, session: &str, subject: &str) -> bool {
let mut approved = self.0.lock().unwrap_or_else(|e| e.into_inner());
let key = (session.to_string(), subject.to_string());
match approved.get_mut(&key) {
Some(count) if *count > 1 => {
*count -= 1;
true
}
Some(_) => {
approved.remove(&key);
true
}
None => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_approval_is_good_for_one_call_in_its_session() {
let answers = OwnerAnsweredCalls::default();
answers.approve("session-a", "subject");
assert!(!answers.take("session-b", "subject"));
assert!(!answers.take("session-a", "other"));
assert!(answers.take("session-a", "subject"));
assert!(!answers.take("session-a", "subject"));
answers.approve("session-a", "subject");
answers.approve("session-a", "subject");
assert!(answers.take("session-a", "subject"));
assert!(answers.take("session-a", "subject"));
assert!(!answers.take("session-a", "subject"));
}
}