1use super::{
3 input::{MAX_WAIT_SECONDS, MIN_SCOPE_IDS, RUN_LIST_LIMIT, SCOPE_IDS},
4 tool::{PluginTool, ReadTool, ToolId, VerifyTool},
5};
6use crate::{inspect::DEFAULT_LOG_TAIL_LINES, types::MAX_RUN_COMMENT_CHARS};
7use serde_json::{Map, Value, json};
8use std::borrow::Cow;
9
10pub struct Tool {
12 pub name: &'static str,
13 pub description: Cow<'static, str>,
14 pub schema: Map<String, Value>,
15 pub hints: Hints,
16}
17
18pub struct Hints {
19 pub open_world: bool,
20 pub read_only: bool,
21 pub destructive: bool,
22 pub idempotent: bool,
23}
24
25const REQUEST_KEY_CHARS: usize = 128;
27
28const LOG_TAIL_LINES: usize = 200;
30const LOG_SEARCH_CHARS: usize = 2048;
32const BACKEND_NAME_CHARS: usize = 64;
35
36macro_rules! object {
38 ({$($key:literal : $value:tt),+ $(,)?}) => {
39 Map::from_iter([$(($key.into(), json!($value))),+])
40 };
41}
42
43pub fn tools() -> Vec<Tool> {
44 let scope = json!({"oneOf": [
45 {"type":"object","properties":{"kind":{"const":"all"}},"required":["kind"],"additionalProperties":false},
46 {"type":"object","properties":{"kind":{"const":"artifact"},"artifactId":{"type":"string"}},"required":["kind","artifactId"],"additionalProperties":false},
47 {"type":"object","properties":{"kind":{"const":"eval"},"evalId":{"type":"string"}},"required":["kind","evalId"],"additionalProperties":false},
48 {"type":"object","properties":{"kind":{"const":"artifacts"},"artifactIds":{"type":"array","items":{"type":"string"},"minItems":MIN_SCOPE_IDS,"maxItems":SCOPE_IDS}},"required":["kind","artifactIds"],"additionalProperties":false},
49 {"type":"object","properties":{"kind":{"const":"evals"},"evalIds":{"type":"array","items":{"type":"string"},"minItems":MIN_SCOPE_IDS,"maxItems":SCOPE_IDS}},"required":["kind","evalIds"],"additionalProperties":false}
50 ]});
51 let string = json!({"type":"string"});
52 vec![
53 tool(
54 ReadTool::Plugins,
55 "List the built-in and installed plugins by namespace.",
56 Map::new(),
57 &[],
58 ),
59 tool(
60 PluginTool::Add,
61 "Install a plugin folder from an absolute path. Only run for a package the user trusts.",
62 object!({"path":string}),
63 &["path"],
64 ),
65 tool(
66 PluginTool::Remove,
67 "Remove an installed plugin by its namespace.",
68 object!({"namespace":string}),
69 &["namespace"],
70 ),
71 tool(
72 VerifyTool::Start,
73 "Submit a durable Run and return its reference; acceptance is not approval. A request key retries the same attempt in this project; different arguments for the same key are refused. A comment, a short note on what you are working on, is shown to people with the Run and changes nothing it does. Speculation defaults off; concurrency is unlimited unless jobs is explicitly given; backend limits still apply. Sign-offs the Run selected never hold up other evals (one outside the scope does, and the Run ends incomplete until the scope includes it): the Run stays waiting_human while one is claimed or its idle grace runs, then ends yellow when only sign-offs remain, which is not full approval.",
74 object!({"scope":scope,"jobs":{"type":"integer","minimum":1,"maximum":u32::MAX},"request_key":{"type":"string","pattern":format!("^[A-Za-z0-9._:-]{{1,{REQUEST_KEY_CHARS}}}$")},"comment":{"type":"string","minLength":1,"maxLength":MAX_RUN_COMMENT_CHARS},"speculate":{"type":"boolean","default":false}}),
75 &["scope"],
76 ),
77 tool(
78 VerifyTool::Cancel,
79 "Explicitly request cancellation; committed evidence remains. Observe settlement for cleanup.",
80 object!({"run":string}),
81 &["run"],
82 ),
83 tool(
84 ReadTool::Status,
85 "Predict execute/reuse/wait using the CLI's status operation. May run trusted fingerprint commands.",
86 object!({"scope":scope}),
87 &[],
88 ),
89 tool(
90 ReadTool::Graph,
91 "Discover static Artifacts, eval criteria, input/consumer relations and cycles without running commands. Scope selects required inputs; impact includes downstream consumers and their inputs, conservatively, not exact cache invalidation.",
92 object!({"scope":scope,"impact":{"type":"boolean","default":false}}),
93 &[],
94 ),
95 tool(
96 ReadTool::Logs,
97 format!(
98 "Inspect runtime stdout/stderr without the full request. Defaults to last {DEFAULT_LOG_TAIL_LINES} lines per stream; explicit evidence distinguishes full local capture, partial, saved audit and missing. Search is literal. Tail/range exclusive. next_start continues forward queries."
99 ),
100 object!({"id":string,"stream":{"enum":["stdout","stderr","both"],"default":"both"},"tail":{"type":"integer","minimum":1,"maximum":LOG_TAIL_LINES},"start":{"type":"integer","minimum":1},"end":{"type":"integer","minimum":1},"search":{"type":"string","minLength":1,"maxLength":LOG_SEARCH_CHARS}}),
101 &["id"],
102 ),
103 tool(
104 ReadTool::Config,
105 "Validate declarations, without hashing or executing project tools.",
106 Map::new(),
107 &[],
108 ),
109 tool(
110 ReadTool::Expand,
111 "Each Artifact's binding as written (file, line, macro) and the declaration it expands to, as .artf code with macros and toolsets filled in. One Artifact, or all.",
112 object!({"artifact":string}),
113 &[],
114 ),
115 tool(
116 ReadTool::Symbols,
117 "Every global binding of the project and its plugins: Artifacts, tools, profiles and evals, macros with their parameters and defaults, and where each is declared.",
118 Map::new(),
119 &[],
120 ),
121 tool(
122 ReadTool::Runs,
123 "List this repository's saved Runs, newest first. Offset selects older records.",
124 object!({"limit":{"type":"integer","minimum":0,"maximum":RUN_LIST_LIMIT,"default":RUN_LIST_LIMIT},"offset":{"type":"integer","minimum":0}}),
125 &[],
126 ),
127 tool(
128 ReadTool::Run,
129 "Read saved Run audit, same JSON as CLI run show. With seconds greater than 0, wait up to that bound for a change or settlement before reading. Cancelling the read never cancels the Run.",
130 object!({"run":string,"seconds":{"type":"integer","minimum":0,"maximum":MAX_WAIT_SECONDS,"default":0}}),
131 &["run"],
132 ),
133 tool(
134 ReadTool::Requests,
135 "Read requests from this repository, optionally one Run. No sign-off authority.",
136 object!({"run":string}),
137 &[],
138 ),
139 tool(
140 ReadTool::Request,
141 "Read saved request audit and criteria. No claim or submit.",
142 object!({"id":string}),
143 &["id"],
144 ),
145 tool(
146 ReadTool::Session,
147 "Read saved Agent conversation or summary. Never send a follow-up.",
148 object!({"reference":string,"summary":{"type":"boolean","default":false}}),
149 &["reference"],
150 ),
151 tool(
152 ReadTool::Tools,
153 "Static preflight only; never execute tools or start sessions.",
154 object!({"eval":string,"artifact":string,"audience":{"enum":["agent","human"]},"tool":string}),
155 &[],
156 ),
157 tool(
158 ReadTool::Doctor,
159 "Inspect backend and state readiness without changing credentials.",
160 Map::new(),
161 &[],
162 ),
163 tool(
164 ReadTool::Models,
165 "List models available from the named built-in or configured custom Verify backend.",
166 object!({"provider":{"type":"string","pattern":format!("^[A-Za-z0-9][A-Za-z0-9_-]{{0,{}}}$", BACKEND_NAME_CHARS - 1)}}),
167 &["provider"],
168 ),
169 tool(
170 ReadTool::Remote,
171 "Inspect configured review store, token source and reachability; never disclose the token.",
172 Map::new(),
173 &[],
174 ),
175 tool(
176 ReadTool::Cache,
177 "Read latest reusable record, or full history; never remove a record.",
178 object!({"key":string,"history":{"type":"boolean","default":false}}),
179 &["key"],
180 ),
181 ]
182}
183fn tool(
184 id: impl Into<ToolId>,
185 description: impl Into<Cow<'static, str>>,
186 mut properties: Map<String, Value>,
187 required: &[&str],
188) -> Tool {
189 let id = id.into();
190 let description = description.into();
191 let description = if matches!(id, ToolId::Read(_)) {
192 properties.insert("cursor".into(), json!({"type":"string"}));
193 format!(
194 "{description} Returns bounded pages of exact CLI JSON: concatenate entries.text, following next_cursor, then parse JSON."
195 ).into()
196 } else {
197 description
198 };
199 properties.insert(
200 "project".into(),
201 json!({"type":"string","description":"The absolute project folder; needed only when the host names no project."}),
202 );
203 let schema = object!({"type":"object","properties":properties,"required":required,"additionalProperties":false});
204 Tool {
205 name: id.name(),
206 description,
207 schema,
208 hints: annotations(id),
209 }
210}
211
212enum Effect {
213 ReadOnly,
214 Mutating(Mutation),
215}
216
217enum Mutation {
218 NonIdempotent,
219 IdempotentDestructive,
220}
221
222enum Reach {
223 Closed,
224 Open,
225}
226
227fn annotations(id: ToolId) -> Hints {
228 use ReadTool as Read;
229 use VerifyTool as Verify;
230 let (effect, reach) = match id {
231 ToolId::Verify(Verify::Start) => (Effect::Mutating(Mutation::NonIdempotent), Reach::Open),
232 ToolId::Verify(Verify::Cancel) => (
233 Effect::Mutating(Mutation::IdempotentDestructive),
234 Reach::Closed,
235 ),
236 ToolId::Plugin(PluginTool::Add) => {
237 (Effect::Mutating(Mutation::NonIdempotent), Reach::Closed)
238 }
239 ToolId::Plugin(PluginTool::Remove) => (
240 Effect::Mutating(Mutation::IdempotentDestructive),
241 Reach::Closed,
242 ),
243 ToolId::Read(Read::Status) => (Effect::Mutating(Mutation::NonIdempotent), Reach::Closed),
244 ToolId::Read(Read::Models | Read::Remote) => (Effect::ReadOnly, Reach::Open),
245 ToolId::Read(
246 Read::Graph
247 | Read::Logs
248 | Read::Config
249 | Read::Expand
250 | Read::Symbols
251 | Read::Runs
252 | Read::Run
253 | Read::Requests
254 | Read::Request
255 | Read::Session
256 | Read::Tools
257 | Read::Doctor
258 | Read::Cache
259 | Read::Plugins,
260 ) => (Effect::ReadOnly, Reach::Closed),
261 };
262 Hints {
263 open_world: matches!(reach, Reach::Open),
264 read_only: matches!(effect, Effect::ReadOnly),
265 destructive: matches!(effect, Effect::Mutating(Mutation::IdempotentDestructive)),
266 idempotent: !matches!(effect, Effect::Mutating(Mutation::NonIdempotent)),
267 }
268}