1use super::{
3 input::{MAX_WAIT_SECONDS, MIN_SCOPE_IDS, RUN_LIST_LIMIT, SCOPE_IDS},
4 page::PAGE_ENTRIES,
5 tool::{PluginTool, ReadTool, ToolId, VerifyTool},
6};
7use crate::{
8 inspect::DEFAULT_LOG_TAIL_LINES, runs::RESULT_PAGE_SIZE, types::MAX_RUN_COMMENT_CHARS,
9};
10use serde_json::{Map, Value, json};
11use std::borrow::Cow;
12
13pub struct Tool {
15 pub name: &'static str,
16 pub title: &'static str,
18 pub description: Cow<'static, str>,
19 pub schema: Map<String, Value>,
20 pub output_schema: Map<String, Value>,
22 pub hints: Hints,
23}
24
25pub struct Hints {
26 pub open_world: bool,
27 pub read_only: bool,
28 pub destructive: bool,
29 pub idempotent: bool,
30}
31
32const REQUEST_KEY_CHARS: usize = 128;
34
35const LOG_TAIL_LINES: usize = 200;
37const LOG_SEARCH_CHARS: usize = 2048;
39const BACKEND_NAME_CHARS: usize = 64;
42
43macro_rules! object {
45 ({$($key:literal : $value:tt),+ $(,)?}) => {
46 Map::from_iter([$(($key.into(), json!($value))),+])
47 };
48}
49
50pub fn tools() -> Vec<Tool> {
51 let scope = json!({"oneOf": [
52 {"type":"object","properties":{"kind":{"const":"all"}},"required":["kind"],"additionalProperties":false},
53 {"type":"object","properties":{"kind":{"const":"artifact"},"artifactId":{"type":"string"}},"required":["kind","artifactId"],"additionalProperties":false},
54 {"type":"object","properties":{"kind":{"const":"eval"},"evalId":{"type":"string"}},"required":["kind","evalId"],"additionalProperties":false},
55 {"type":"object","properties":{"kind":{"const":"artifacts"},"artifactIds":{"type":"array","items":{"type":"string"},"minItems":MIN_SCOPE_IDS,"maxItems":SCOPE_IDS}},"required":["kind","artifactIds"],"additionalProperties":false},
56 {"type":"object","properties":{"kind":{"const":"evals"},"evalIds":{"type":"array","items":{"type":"string"},"minItems":MIN_SCOPE_IDS,"maxItems":SCOPE_IDS}},"required":["kind","evalIds"],"additionalProperties":false}
57 ]});
58 let string = json!({"type":"string"});
59 vec![
60 tool(
61 ReadTool::Plugins,
62 "List plugins",
63 "List the built-in and installed plugins by namespace.",
64 Map::new(),
65 &[],
66 ),
67 tool(
68 PluginTool::Add,
69 "Add a plugin",
70 "Install a plugin folder from an absolute path. Only run for a package the user trusts.",
71 object!({"path":string}),
72 &["path"],
73 ),
74 tool(
75 PluginTool::Remove,
76 "Remove a plugin",
77 "Remove an installed plugin by its namespace.",
78 object!({"namespace":string}),
79 &["namespace"],
80 ),
81 tool(
82 VerifyTool::Start,
83 "Verify",
84 "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.",
85 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}}),
86 &["scope"],
87 ),
88 tool(
89 VerifyTool::Cancel,
90 "Cancel a Run",
91 "Explicitly request cancellation; committed evidence remains. Observe settlement for cleanup.",
92 object!({"run":string}),
93 &["run"],
94 ),
95 tool(
96 ReadTool::Status,
97 "Predict a verify",
98 "Predict execute/reuse/wait using the CLI's status operation. May run trusted fingerprint commands.",
99 object!({"scope":scope}),
100 &[],
101 ),
102 tool(
103 ReadTool::Graph,
104 "Show the Artifact graph",
105 "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.",
106 object!({"scope":scope,"impact":{"type":"boolean","default":false}}),
107 &[],
108 ),
109 tool(
110 ReadTool::Logs,
111 "Read request logs",
112 format!(
113 "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."
114 ),
115 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}}),
116 &["id"],
117 ),
118 tool(
119 ReadTool::Config,
120 "Check declarations",
121 "Validate declarations, without hashing or executing project tools.",
122 Map::new(),
123 &[],
124 ),
125 tool(
126 ReadTool::Expand,
127 "Expand declarations",
128 "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.",
129 object!({"artifact":string}),
130 &[],
131 ),
132 tool(
133 ReadTool::Symbols,
134 "List symbols",
135 "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.",
136 Map::new(),
137 &[],
138 ),
139 tool(
140 ReadTool::Runs,
141 "List Runs",
142 "List this repository's saved Runs, newest first. Offset selects older records.",
143 object!({"limit":{"type":"integer","minimum":0,"maximum":RUN_LIST_LIMIT,"default":RUN_LIST_LIMIT},"offset":{"type":"integer","minimum":0}}),
144 &[],
145 ),
146 tool(
147 ReadTool::Run,
148 "Show a Run",
149 "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.",
150 object!({"run":string,"seconds":{"type":"integer","minimum":0,"maximum":MAX_WAIT_SECONDS,"default":0}}),
151 &["run"],
152 ),
153 tool(
154 ReadTool::Requests,
155 "List requests",
156 "Read requests from this repository, optionally one Run. No sign-off authority.",
157 object!({"run":string}),
158 &[],
159 ),
160 tool(
161 ReadTool::Request,
162 "Show a request",
163 "Read saved request audit and criteria. No claim or submit.",
164 object!({"id":string}),
165 &["id"],
166 ),
167 tool(
168 ReadTool::Session,
169 "Show an Agent session",
170 "Read saved Agent conversation or summary. Never send a follow-up.",
171 object!({"reference":string,"summary":{"type":"boolean","default":false}}),
172 &["reference"],
173 ),
174 tool(
175 ReadTool::Tools,
176 "Check review tools",
177 "Static preflight only; never execute tools or start sessions.",
178 object!({"eval":string,"artifact":string,"audience":{"enum":["agent","human"]},"tool":string}),
179 &[],
180 ),
181 tool(
182 ReadTool::Doctor,
183 "Check readiness",
184 "Inspect backend and state readiness without changing credentials.",
185 Map::new(),
186 &[],
187 ),
188 tool(
189 ReadTool::Models,
190 "List models",
191 "List models available from the named built-in or configured custom Verify backend.",
192 object!({"provider":{"type":"string","pattern":format!("^[A-Za-z0-9][A-Za-z0-9_-]{{0,{}}}$", BACKEND_NAME_CHARS - 1)}}),
193 &["provider"],
194 ),
195 tool(
196 ReadTool::Remote,
197 "Show the remote store",
198 "Inspect configured review store, token source and reachability; never disclose the token.",
199 Map::new(),
200 &[],
201 ),
202 tool(
203 ReadTool::Cache,
204 "Show a cache record",
205 "Read latest reusable record, or full history; never remove a record.",
206 object!({"key":string,"history":{"type":"boolean","default":false}}),
207 &["key"],
208 ),
209 ]
210}
211fn tool(
212 id: impl Into<ToolId>,
213 title: &'static str,
214 description: impl Into<Cow<'static, str>>,
215 mut properties: Map<String, Value>,
216 required: &[&str],
217) -> Tool {
218 let id = id.into();
219 let description = description.into();
220 let description = if matches!(id, ToolId::Read(_)) {
221 properties.insert("cursor".into(), json!({"type":"string"}));
222 format!(
223 "{description} Returns bounded pages of exact CLI JSON: concatenate entries.text, following next_cursor, then parse JSON."
224 ).into()
225 } else {
226 description
227 };
228 properties.insert(
229 "project".into(),
230 json!({"type":"string","description":"The absolute project folder; needed only when the host names no project."}),
231 );
232 let schema = object!({"type":"object","properties":properties,"required":required,"additionalProperties":false});
233 Tool {
234 name: id.name(),
235 title,
236 description,
237 schema,
238 output_schema: output(id),
239 hints: annotations(id),
240 }
241}
242
243fn output(id: ToolId) -> Map<String, Value> {
246 let string = json!({"type":"string"});
247 match id {
248 ToolId::Read(_) => object!({
249 "type":"object",
250 "properties":{
251 "encoding":{"const":"json"},
252 "entries":{"type":"array","minItems":PAGE_ENTRIES,"maxItems":PAGE_ENTRIES,"items":{
253 "type":"object",
254 "properties":{"offset":{"type":"integer","minimum":0},"text":string},
255 "required":["offset","text"],
256 "additionalProperties":false
257 }},
258 "next_cursor":{"type":["string","null"]}
259 },
260 "required":["encoding","entries","next_cursor"],
261 "additionalProperties":false
262 }),
263 ToolId::Verify(VerifyTool::Start) => object!({
265 "type":"object",
266 "properties":{
267 "run":string,
268 "outcome":{"enum":["GREEN","RED","INCOMPLETE","YELLOW","ERROR","CANCELLED"]},
269 "waiting_signoff_count":{"type":"integer","minimum":0},
270 "waiting_signoffs":{"type":"array","maxItems":RESULT_PAGE_SIZE,"items":{
271 "type":"object",
272 "properties":{"request":string,"eval":string},
273 "required":["request","eval"],
274 "additionalProperties":false
275 }}
276 },
277 "required":["run"],
278 "dependentRequired":{"outcome":["waiting_signoff_count","waiting_signoffs"]},
279 "additionalProperties":false
280 }),
281 ToolId::Verify(VerifyTool::Cancel) => object!({
282 "type":"object",
283 "properties":{
284 "run":string,
285 "state":{"enum":["preparing","running","waiting","waiting_human","cancelling","green","red","incomplete","yellow","error","cancelled"]},
286 "cancellation_requested":{"type":"boolean"}
287 },
288 "required":["run","state","cancellation_requested"],
289 "additionalProperties":false
290 }),
291 ToolId::Plugin(PluginTool::Add) => object!({
292 "type":"object",
293 "properties":{
294 "action":{"const":"added"},
295 "namespace":string,
296 "title":string,
297 "builtin":{"type":"boolean"},
298 "viewer":{"type":"boolean"}
299 },
300 "required":["action","namespace","title","builtin","viewer"],
301 "additionalProperties":false
302 }),
303 ToolId::Plugin(PluginTool::Remove) => object!({
304 "type":"object",
305 "properties":{"action":{"const":"removed"},"namespace":string},
306 "required":["action","namespace"],
307 "additionalProperties":false
308 }),
309 }
310}
311
312enum Effect {
313 ReadOnly,
314 Mutating(Mutation),
315}
316
317enum Mutation {
318 NonIdempotent,
319 IdempotentDestructive,
320}
321
322enum Reach {
323 Closed,
324 Open,
325}
326
327fn annotations(id: ToolId) -> Hints {
328 use ReadTool as Read;
329 use VerifyTool as Verify;
330 let (effect, reach) = match id {
331 ToolId::Verify(Verify::Start) => (Effect::Mutating(Mutation::NonIdempotent), Reach::Open),
332 ToolId::Verify(Verify::Cancel) => (
333 Effect::Mutating(Mutation::IdempotentDestructive),
334 Reach::Closed,
335 ),
336 ToolId::Plugin(PluginTool::Add) => {
337 (Effect::Mutating(Mutation::NonIdempotent), Reach::Closed)
338 }
339 ToolId::Plugin(PluginTool::Remove) => (
340 Effect::Mutating(Mutation::IdempotentDestructive),
341 Reach::Closed,
342 ),
343 ToolId::Read(Read::Status) => (Effect::Mutating(Mutation::NonIdempotent), Reach::Closed),
344 ToolId::Read(Read::Models | Read::Remote) => (Effect::ReadOnly, Reach::Open),
345 ToolId::Read(
346 Read::Graph
347 | Read::Logs
348 | Read::Config
349 | Read::Expand
350 | Read::Symbols
351 | Read::Runs
352 | Read::Run
353 | Read::Requests
354 | Read::Request
355 | Read::Session
356 | Read::Tools
357 | Read::Doctor
358 | Read::Cache
359 | Read::Plugins,
360 ) => (Effect::ReadOnly, Reach::Closed),
361 };
362 Hints {
363 open_world: matches!(reach, Reach::Open),
364 read_only: matches!(effect, Effect::ReadOnly),
365 destructive: matches!(effect, Effect::Mutating(Mutation::IdempotentDestructive)),
366 idempotent: !matches!(effect, Effect::Mutating(Mutation::NonIdempotent)),
367 }
368}