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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! `ToolHandler` foreign-callback trampoline + `blazen_agent_new` constructor.
//! Bridges a C vtable of function pointers into a Rust `Arc<dyn ToolHandler>`
//! so the agent loop can invoke foreign-language tool implementations.
//!
//! Phase R5 Agent B.
//!
//! ## Why a vtable, not a single callback
//!
//! Foreign hosts (Ruby's `ffi` gem, Dart's `NativeCallable`, Crystal's `->`
//! procs, Lua's `ffi.cast`, PHP's FFI) each have their own way of
//! materialising a function pointer that can be invoked from Rust. To keep
//! the C side trivially describable in any of those, the cabi surface accepts
//! a flat `#[repr(C)]` vtable carrying `(user_data, drop_user_data, execute)`
//! function pointers. The foreign side owns the lifecycle of `user_data`
//! (its boxed Ruby block, its Dart isolate token, etc.) and provides the
//! `drop_user_data` thunk we call when the wrapper is dropped.
//!
//! ## Thread-safety
//!
//! `BlazenToolHandlerVTable` is `Send + Sync`: the foreign side is
//! responsible for making `user_data` and the `execute` thunk safe to call
//! from arbitrary tokio worker threads. Ruby's `ffi` gem reacquires the GVL
//! around the callback; Dart's `NativeCallable` shuttles work back to the
//! isolate; Crystal and Lua run their callbacks on the calling thread. None
//! of those mechanisms are visible from Rust, so we trust the foreign side
//! to uphold the `Send + Sync` contract on its end.
//!
//! ## Call-path
//!
//! The agent loop invokes `ToolHandler::execute` from inside a tokio task.
//! We don't know whether the foreign `execute` thunk is willing to block
//! (Ruby's GVL acquisition can park), so the trampoline always dispatches
//! the call through `tokio::task::spawn_blocking`. That keeps a single slow
//! tool handler from starving other tasks on the runtime's worker threads.
use ;
use Arc;
use async_trait;
use ;
use ;
use crateBlazenAgent;
use crateBlazenError;
use crateBlazenCompletionModel;
use crateBlazenTool;
use cratecstr_to_opt_string;
// ---------------------------------------------------------------------------
// Error-out helpers (module-private, mirror the agent.rs / workflow.rs shape)
// ---------------------------------------------------------------------------
/// Writes `e` to the out-param if non-null and returns `-1`.
///
/// # Safety
///
/// `out_err` must be null OR a valid destination for a single
/// `*mut BlazenError` write.
unsafe
/// Writes a synthesised `Internal` error to the out-param and returns `-1`.
///
/// # Safety
///
/// Same contract as [`write_error`].
unsafe
// ---------------------------------------------------------------------------
// VTable
// ---------------------------------------------------------------------------
/// Function-pointer vtable a foreign caller fills out to implement
/// [`ToolHandler`] across the C ABI.
///
/// `user_data` is opaque to Rust — it's whatever the foreign side wants to
/// associate with the handler (a boxed Ruby block, a Dart isolate token, a
/// Crystal proc, etc.). It is passed back unchanged to every `execute`
/// invocation and to `drop_user_data` when the wrapper is dropped.
///
/// ## `execute` contract
///
/// - `tool_name` and `arguments_json` are caller-owned C strings that remain
/// valid for the duration of the call. The callback MUST NOT free them.
/// - On success, the callback writes a heap-allocated NUL-terminated UTF-8
/// string into `*out_result_json`. The string must have been allocated in
/// a way that is compatible with [`crate::string::blazen_string_free`]
/// (i.e. `CString::into_raw` on the Rust side, or anything the foreign
/// side hands back through `blazen_string_alloc` if one exists). The
/// cabi surface frees the string with `blazen_string_free`.
/// - On failure, the callback writes a caller-owned `*mut BlazenError` into
/// `*out_err`. The cabi surface frees it with `blazen_error_free`.
/// - The return value is `0` on success and `-1` on failure. Any other
/// non-zero value is treated as a failure but logged as a protocol
/// violation in the synthesised `Internal` error message.
///
/// ## `drop_user_data` contract
///
/// Called exactly once when the [`CToolHandler`] wrapper is dropped — i.e.
/// when the [`InnerAgent`] referencing this handler is dropped and the last
/// reference goes away. The foreign side releases whatever resources back
/// `user_data` here.
// SAFETY: the foreign side guarantees its `user_data` + `execute` thunk are
// safe to use from arbitrary tokio worker threads. Ruby's `ffi` gem
// reacquires the GVL around the callback; Dart's `NativeCallable` ships work
// back to its owning isolate; Crystal / Lua / PHP callbacks run on the
// calling thread. None of these guarantees are visible from Rust — the
// vtable is a pure foreign-language contract, and the cabi surface trusts
// the foreign side to uphold it on its end.
unsafe
// SAFETY: see the `Send` impl. Calling `execute` concurrently from multiple
// threads is part of the same foreign-side contract.
unsafe
// ---------------------------------------------------------------------------
// Wrapper struct
// ---------------------------------------------------------------------------
/// Rust-side wrapper that owns a [`BlazenToolHandlerVTable`] and implements
/// [`ToolHandler`] by dispatching through the vtable's function pointers.
///
/// One instance is constructed per [`blazen_agent_new`] call. The wrapper
/// is held inside an `Arc<dyn ToolHandler>` that the inner agent owns; once
/// the agent is dropped, the wrapper drops, and the foreign-side
/// `user_data` is released via `drop_user_data`.
pub
// ---------------------------------------------------------------------------
// C entry point: blazen_agent_new
// ---------------------------------------------------------------------------
/// Constructs a [`BlazenAgent`] from a completion model, optional system
/// prompt, tool list, tool-handler vtable, and max iterations.
///
/// On success returns `0` and writes a fresh `*mut BlazenAgent` into
/// `*out_agent`. On failure returns `-1` and writes a fresh `*mut BlazenError`
/// into `*out_err`. Both out-params may be null to discard the corresponding
/// side of the result.
///
/// ## Ownership
///
/// - `model` is BORROWED — the underlying `Arc<CompletionModel>` is cloned
/// into the agent. The caller retains its handle and is still responsible
/// for freeing it.
/// - `system_prompt` is BORROWED for the duration of this call (it is copied
/// into the agent before the call returns). A null pointer means "no
/// system prompt".
/// - `tools` is BORROWED at the array level, but each `*mut BlazenTool`
/// element is CONSUMED — the inner `Tool` record is moved into the agent's
/// tool vec. Callers must NOT free the individual tool handles afterwards
/// (the array itself remains caller-owned).
/// - `tool_handler` is CONSUMED — ownership of `user_data` transfers to the
/// constructed [`CToolHandler`], which releases it via `drop_user_data`
/// when the wrapper drops. Early-return error paths still invoke
/// `drop_user_data` so the foreign side doesn't leak.
/// - `out_agent` receives a caller-owned `*mut BlazenAgent`. Free with
/// [`crate::agent::blazen_agent_free`].
///
/// # Safety
///
/// `model` must be null OR a live `BlazenCompletionModel` produced by the
/// cabi surface. `system_prompt` must be null OR a NUL-terminated UTF-8
/// buffer valid for the duration of this call. When `tools_count > 0`,
/// `tools` must point to an array of exactly `tools_count` valid
/// `*mut BlazenTool` entries, each produced by the cabi surface; ownership
/// of each element transfers to this function (the array itself stays
/// caller-owned). `tool_handler.user_data` and the `execute` /
/// `drop_user_data` thunks must satisfy the contracts documented on
/// [`BlazenToolHandlerVTable`]. `out_agent` and `out_err` must each be null
/// OR a writable slot for a single `*mut` write.
pub unsafe extern "C"