agent_base/tool/activation.rs
1//! Session-scoped tool activation: which `Deferred` tools the model has opened.
2//!
3//! A `Deferred` tool is named in the roster the model sees but costs no schema
4//! until it is opened. Opening one is one call; keeping it open is free. See
5//! `notes/tool-face-design.md` for why the roster is names-plus-a-line rather
6//! than a search index.
7//!
8//! **Sticky for the session.** The alternative — closing again after each use —
9//! would charge a turn every time the model reaches for it, which is exactly
10//! the cost the roster exists to avoid.
11
12use std::collections::BTreeSet;
13use std::sync::{Arc, RwLock};
14
15/// Names of the `Deferred` tools the model has opened this session.
16///
17/// Cheap to clone ([`Arc`]) so an [`crate::tool::ActivateTool`] can hold one
18/// while the engine holds another.
19#[derive(Clone, Default)]
20pub struct ToolActivation {
21 inner: Arc<RwLock<BTreeSet<String>>>,
22}
23
24impl ToolActivation {
25 pub fn new() -> Self {
26 Self::default()
27 }
28
29 /// Open a tool for the rest of the session. Idempotent; returns whether
30 /// this call is what opened it.
31 pub fn activate(&self, name: &str) -> bool {
32 self.inner
33 .write()
34 .unwrap_or_else(|e| e.into_inner())
35 .insert(name.to_string())
36 }
37
38 /// Has the model opened this tool?
39 pub fn is_active(&self, name: &str) -> bool {
40 self.inner
41 .read()
42 .unwrap_or_else(|e| e.into_inner())
43 .contains(name)
44 }
45
46 /// Opened names in sorted order — what
47 /// [`agent_types::ActivationContext::current_tools`] carries.
48 pub fn snapshot(&self) -> Vec<String> {
49 self.inner
50 .read()
51 .unwrap_or_else(|e| e.into_inner())
52 .iter()
53 .cloned()
54 .collect()
55 }
56
57 /// Forget everything. A new session starts closed.
58 pub fn clear(&self) {
59 self.inner
60 .write()
61 .unwrap_or_else(|e| e.into_inner())
62 .clear();
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 /// Value: protects=the cost model of the whole roster — a tool opened once
71 /// must stay open, or the model pays a turn each time it reaches for it and
72 /// the roster saves nothing; fails_when=re-activation is required.
73 /// why_new=activation is new. seam=session state; no_claim=that stickiness
74 /// is right across a `/resume`, only within one running session.
75 #[test]
76 fn opening_a_tool_once_keeps_it_open() {
77 let act = ToolActivation::new();
78 assert!(
79 act.activate("browser_navigate"),
80 "the first open reports it"
81 );
82 assert!(
83 !act.activate("browser_navigate"),
84 "and the second is a no-op"
85 );
86 assert!(act.is_active("browser_navigate"), "still open");
87 }
88
89 /// Value: protects=the roster's promise that unopened tools cost nothing —
90 /// a tool the model never asked for must never appear; fails_when=a closed
91 /// tool reports as open. why_new=activation is new. seam=session state;
92 /// no_claim=about tools activated by other means (a predicate override).
93 #[test]
94 fn a_tool_that_was_never_opened_stays_closed() {
95 let act = ToolActivation::new();
96 act.activate("a");
97 assert!(!act.is_active("b"), "opening one tool opens one tool");
98 }
99
100 /// Value: protects=determinism of the tool list — the snapshot drives which
101 /// schemas go on the wire, so it must be a stable order or the request
102 /// prefix churns and prompt caching misses every turn; fails_when=the
103 /// iteration order follows the hash map. why_new=activation is new.
104 /// seam=session state -> request; no_claim=that sorted order is required by
105 /// any provider, only that it is stable.
106 #[test]
107 fn the_snapshot_is_stable_so_the_request_prefix_is() {
108 let act = ToolActivation::new();
109 act.activate("zeta");
110 act.activate("alpha");
111 act.activate("mid");
112 assert_eq!(act.snapshot(), vec!["alpha", "mid", "zeta"]);
113 }
114
115 /// Value: protects=session boundaries — one session's openings must not
116 /// leak into the next, or a tool from a past task shows up in this one;
117 /// fails_when=clear leaves names behind. why_new=activation is new.
118 /// seam=session state; no_claim=about process-level reuse across sessions.
119 #[test]
120 fn clear_forgets_every_opening() {
121 let act = ToolActivation::new();
122 act.activate("a");
123 act.clear();
124 assert!(act.snapshot().is_empty());
125 assert!(!act.is_active("a"));
126 }
127}