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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! Stable plugin API surface shared between `trusty-agents` and external agent crates.
//!
//! Why: The original design placed `ToolExecutor` / `AgentPlugin` / `ToolResult`
//! inside the host crate's `lib.rs`. That created a hard cargo dependency
//! cycle (trusty-agents → cto-assistant → trusty-agents), because external agent
//! crates need the trait to implement it AND `trusty-agents` needs the agent
//! crate to inject the plugin at startup. Cargo cannot resolve circular
//! path dependencies even when they are logically one-directional at the
//! binary level. Extracting the minimal trait surface into this tiny
//! crate breaks the cycle: both `trusty-agents` and every agent crate depend
//! on `trusty-agents-common`, but never on each other through the lib.
//! What: Re-defines the previously trusty-agents-internal types — `ToolExecutor`
//! trait, `ToolResult` enum, `ToolExecutionTier` enum, `ServiceTier`
//! enum (RBAC tiers), and `AgentPlugin` struct — as the public surface.
//! Also hosts the harness-adapter framework (`adapters`) and the
//! JSON-backed session ledger (`session_registry`), both moved here in
//! Wave 1 of the trusty-agents-common build-out (issue #862, refs #830/#832).
//! `trusty-agents` re-exports them via `trusty_agents::agent_api`,
//! `trusty_agents::adapters`, and `trusty_agents::session_registry` for
//! source-level compatibility with the existing call sites in
//! `crates/trusty-agents/src/**`.
//! Test: Compile-tested transitively via `crates/trusty-agents` (host);
//! `ToolResult`'s predicates are covered by
//! `tool_result_is_error_distinguishes_variants`.
// docs.rs builds a release's documentation once, from the uploaded tarball,
// so a broken intra-doc link is baked into that version forever and only a new
// release can correct it. Deny keeps this crate at zero rather than letting the
// ratchet in `scripts/check_rustdoc_links.sh` absorb a new one.
// #7735: shared closing-block extraction (Improvement recommendations /
// Prompt feedback), slice 1 of docs/specs/self-improvement-loop.md.
use Arc;
use async_trait;
use ;
use Value;
/// Structured result of a tool execution.
///
/// Why: Hard-failing the LLM loop on every tool error is brittle — the model
/// often can recover (retry with different args, fall back to another
/// tool, or explain the failure in its final answer). Returning a
/// structured `Error { recoverable }` lets us surface the failure back
/// to the LLM as a `tool_result` with `is_error: true` while keeping the
/// loop running, unless `recoverable = false` in which case callers may
/// choose to stop.
/// What: `Success(String)` carries a successful textual result; `Error`
/// carries a message plus a `recoverable` flag.
/// Test: `ToolResult::err(...).is_error()` is true; `ok(...).content()`
/// returns the success string. Exercised across `trusty-agents/tools/**`.
/// Two-tier tool execution model (trusty-agents #447).
///
/// Why: The dispatch path treats always-on tools fundamentally differently
/// from on-demand tools — they run automatically, their output becomes
/// context rather than a `tool_result`, and they must not appear in the
/// LLM's tool list. Encoding the distinction as an enum on the trait
/// makes it impossible to accidentally schedule an `AlwaysOn` tool as
/// `OnDemand` or vice-versa.
/// What: `OnDemand` is the default (current behavior); `AlwaysOn` opts the
/// tool into the pre-LLM context-building pipeline.
/// Test: Default exercised by every existing tool; `AlwaysOn` exercised by
/// `trusty-agents`'s `tools/always_on::build_live_context_*`.
/// RBAC service tier (trusty-agents #445).
///
/// Why: Different transports (CLI, Slack, Telegram, HTTP) expose the same
/// tool registry to users with different trust levels. Tools opt into
/// RBAC by listing the tiers that must be denied access. Defined here
/// (not in `trusty-agents/rbac`) because the `ToolExecutor::restricted_tiers`
/// signature returns `&[ServiceTier]` — external agent crates would not
/// be able to implement the trait without seeing the enum.
/// What: `All` (full access — controller / authenticated operator),
/// `Analytics` (read + analytical queries, no mutations), `ReadOnly`
/// (passive observation only, the strictest tier).
/// Serializes as `snake_case` so TOML/JSON authors can write
/// `tier = "read_only"` rather than the variant name. `Default` is
/// `All` so callsites that forget to set a tier degrade open at the
/// controller (unauthenticated transports MUST set a stricter default).
/// Test: `trusty-agents/rbac` covers serde + ordering; `trusty-agents/tools/mod::dispatch_for_user_*`
/// covers integration with dispatch.
/// A tool invocable by an LLM through function calling.
///
/// Why: Replaces hardcoded string-match dispatch with polymorphic execution.
/// Living in `trusty-agents-common` (not in `trusty-agents`) so external agent
/// crates can implement it without depending on the full host crate,
/// breaking the cargo dependency cycle.
/// What: Supplies OpenAI-compatible JSON schema via `schema()` and executes
/// parsed arguments in `execute()`. Returns a structured `ToolResult`
/// so failures can be surfaced back to the LLM without tearing down
/// the loop.
/// Test: See unit tests in `trusty-agents/tools/mod.rs` for `ToolRegistry`.
/// Named bundle of `ToolExecutor`s for a specific persona.
///
/// Why: Replaces hard-coded persona-to-tool branches in `trusty-agents`'s
/// `ctrl/mod.rs` with a data-driven injection point. New agent crates
/// register by adding themselves to the plugin list constructed in
/// `trusty-agents`'s `main.rs`; ctrl never needs to learn their names.
/// Lives here (not in `trusty-agents`) so agent crates can construct one
/// without depending on the host.
/// What: Holds the persona name the plugin's tools apply to plus an
/// `Arc<dyn ToolExecutor>` per tool. Cloning is cheap (Arc reference
/// counts) so the plugin can be reused across sessions.
/// Test: `cargo test -p cto-assistant agent_plugin_targets_cto_assistant`.