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
//! Compose a role's declared tools into the typed canonical request's
//! `tools` array (ARCH §3.3, §4.3, §4.4).
//!
//! §4.3 lets a role declare its enabled tools as `tools: [...]` in the
//! per-repo `providers.yaml`. §3.3 says each tool's JSON schema is
//! committed under `descriptions/tools/<name>.json` in the branch
//! worktree and is **sent verbatim as the `input_schema`** of that
//! tool's entry in the model call's `tools: [...]` array. This module is
//! the consumer: it turns the declared names plus the on-disk schemas
//! into `Vec<brazen::Tool>` so the model is TOLD which tools it may call
//! (previously omitted — the model flew blind about its toolset).
//!
//! **Availability is the intersection.** A declared tool contributes an
//! entry only if its `descriptions/tools/<name>.json` schema is present
//! in the worktree; a name with no schema file is silently dropped (the
//! `descriptions/**` tree is populated by the descriptions-always
//! mechanism, §3.3 — a role may out-declare what a given conversation
//! carries). A schema file that is present but unreadable or not valid
//! JSON is a config fault, surfaced rather than dropped (PRINCIPLES
//! "Decline illegal operations"). Declared order is preserved.
//!
//! The `description` field (§3.3 point 3) is sourced from the tool's
//! `SKILL.md` frontmatter, snapshotted alongside the schema by the
//! descriptions-always producer (`crate::template::descriptions`) into
//! `descriptions/skills/<name>.md`. A tool whose schema is present but
//! whose skill frontmatter is absent composes with `description: None`
//! — the transient producer-ordering state §3.3 sanctions — rather than
//! being dropped; a present-but-malformed frontmatter is a config fault,
//! surfaced rather than dropped (PRINCIPLES "Decline illegal
//! operations").
//!
//! [`compose`] is the single home for the question "what does this
//! request declare?", and it has three answers to add up, not one:
//!
//! 1. **Injection** — a toolset no config declares, from either of two
//! sources: the calling role's *procedure* (the compactor's
//! `write_summary` / `mark_for_deletion`, §2.7) and the *binding's*
//! host injection (`cmd::Fx::tool_injection`, §3.3
//! *Host-injected tools* — read off the executor, which is the thing
//! that will answer them). One list, because they are one kind of
//! thing; [`injected`] is where the two sources meet.
//! 2. **Election** — the intersection above, what the role may offer.
//! 3. **Closure** — a provider validates the request *as a whole*: a
//! `tool_use` / `tool_result` pair naming a tool the `tools: [...]`
//! array omits is refused outright, so the array must additionally
//! cover every tool the history it ships already names. This one
//! reads the opposite way from election — a name the history
//! references cannot be dropped, because the exchange has happened.
//!
//! **Injection outranks election, and it is spliced first.** A name can
//! have only one entry, and when both sources carry it the injected
//! definition wins — because it also wins at execution (an installed
//! host router answers every invocation, §3.3 binary resolution standing
//! behind it for no name at all), so the schema the model reads is
//! always the schema of the thing that will run. The ordinary case is
//! disjoint sets and the order is then invisible.
//!
//! Declaring is not permitting: what a role may *call* is decided at
//! execution ([`super::tool_step`]), and nothing here widens it.
use crateError;
use cratecompactor;
use crateToolExecutor;
use crateInjectedTool;
use crateskill;
use ;
use ;
use Path;
/// Worktree-relative directory holding the committed tool schemas
/// (ARCH §3.3 — `descriptions/tools/<name>.json`, sent verbatim as
/// `input_schema`).
const TOOLS_DESC_DIR: &str = "descriptions/tools";
/// Worktree-relative directory holding the committed skill frontmatter
/// (ARCH §3.3 — `descriptions/skills/<name>.md`; a tool's `description`
/// is its own skill's frontmatter `description`).
const SKILLS_DESC_DIR: &str = "descriptions/skills";
/// Everything injected into `role`'s next model call — its procedure's
/// own toolset (§2.7, role-keyed) and the binding's host injection
/// (§3.3), as one list.
///
/// The two sources are read here and nowhere else, so the composer below
/// and the execution gate ([`super::tool_step::refusal`]) union the same
/// pair: a tool that is declared but not permitted, or permitted but
/// never declared, has no way to arise. `workspace` and `agent` name
/// whose request this is (bl-ddaa): the host's declared set may be
/// per-agent state, and every caller of this function is assembling or
/// gating for exactly one agent.
pub
/// Everything the next model call declares: the `injected` toolset
/// first, then the role's `declared` names intersected with the schemas under
/// `<worktree>/descriptions/tools/` (in declared order, minus any name
/// injection already took), then the closure over `history` (§3.3, §4.3,
/// §2.7).
pub
/// One `tools: [...]` entry for an injected definition. Its three facts
/// are already the entry's three facts — the whole difference from an
/// elected tool is that they came from the harness or the binding rather
/// than from `descriptions/**`.
/// Append a declaration for every tool `history` names that `tools` does
/// not already carry (ARCH §3.3 — the request's referential integrity).
///
/// The history is not rewritten to fit the declaration; the declaration
/// is widened to fit the history. Transcript entries are immutable
/// (§2.3) and the wire framing is transcript-backed (§3.3), so the
/// alternative — stripping or textualizing a `tool_use` block whose tool
/// this role does not offer — would make the model call disagree with
/// the branch's own record.
///
/// Where the committed schema exists it is used verbatim, exactly as for
/// an elected tool. Where it does not — a name the model invented, whose
/// exchange nonetheless landed in the transcript — a bare
/// `{"type": "object"}` stands in: the entry exists to make the history
/// legible, not to offer the tool.
/// Tool names the `tool_use` blocks of `history` reference, in
/// first-appearance order and deduplicated.
/// The declared name of a composed entry, whichever variant carries it.
/// One `tools: [...]` entry for `name` around an already-read schema.
/// The committed `descriptions/tools/<name>.json` schema, sent verbatim
/// (and read by the `python` built-in for the same tool's stub function,
/// `docs/DESIGN_CODE_EXECUTION.md` §2.7 — one home for where a tool's
/// committed definition lives)
/// as `input_schema` (§3.3). `None` when the file is absent — the caller
/// decides what absence means (dropped for an elected tool, stood in for
/// one the history already names). Present-but-unreadable or
/// present-but-malformed is a config fault, surfaced.
pub
/// The `description` for tool `name`, from its skill frontmatter at
/// `<worktree>/descriptions/skills/<name>.md` (§3.3 point 3). Absent
/// frontmatter → `None` (the schema-before-description ordering §3.3
/// sanctions); a present-but-malformed frontmatter is surfaced.
pub