agent-base 0.13.0

A lightweight Agent Runtime Kernel for building AI agents in Rust
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use async_trait::async_trait;
use serde_json::{Value, json};
use tokio::sync::mpsc;

use crate::engine::SessionStore;
use crate::types::{AgentError, AgentResult, RuntimeEvent, SessionId, UserEvent};

pub mod auto_continue;
pub mod policy;
pub mod update_plan;

pub use auto_continue::AutoContinueTool;
pub use update_plan::UpdatePlanTool;

pub use policy::{DenyAllToolPolicy, ToolDecision, ToolPolicy};

// Re-export pure types from agent-types
pub use agent_types::{
    ActivationContext, Content, ToolExposure, ToolMetadata, content_details, content_images,
    content_text,
};

/// Per-call execution-clock control, shared between the tool and the
/// pipeline's timeout ([`crate::engine::DefaultPipeline`]).
///
/// The timeout measures **active** execution time: stretches spent waiting on
/// a human are excluded ([`ExecClock::pause`]), so a tool that asks the user a
/// question is never killed by its own execution budget while the user is
/// still reading. Every [`ToolContext`] built per call gets a fresh clock —
/// pauses never leak into a later call.
#[derive(Clone, Default)]
pub struct ExecClock {
    inner: Arc<ExecClockInner>,
}

#[derive(Default)]
struct ExecClockInner {
    state: std::sync::Mutex<ExecClockState>,
}

#[derive(Default)]
struct ExecClockState {
    /// Live pause guards. Non-zero ⇒ the clock is paused (nesting supported).
    live: usize,
    /// When the current pause began (`Some` while paused).
    started: Option<std::time::Instant>,
    /// Paused time already banked from closed pauses.
    total: std::time::Duration,
}

impl ExecClock {
    /// Hold the clock paused for as long as the returned guard lives.
    /// Nesting is fine: the clock resumes when the outermost guard drops.
    pub fn pause(&self) -> ExecPauseGuard {
        let mut state = self.lock();
        state.live += 1;
        if state.live == 1 {
            state.started = Some(std::time::Instant::now());
        }
        ExecPauseGuard {
            clock: self.clone(),
        }
    }

    /// Paused time so far, including the open pause if any. The pipeline's
    /// deadline is `start + budget + paused_total()`, so a pause pushes the
    /// timeout out by exactly the time the human spent.
    pub fn paused_total(&self) -> std::time::Duration {
        let state = self.lock();
        state.total
            + state
                .started
                .map_or(std::time::Duration::ZERO, |s| s.elapsed())
    }

    fn resume(&self) {
        let mut state = self.lock();
        state.live = state.live.saturating_sub(1);
        if state.live == 0
            && let Some(started) = state.started.take()
        {
            state.total += started.elapsed();
        }
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, ExecClockState> {
        // A panic while holding the clock must not poison it — worst case the
        // accounting is slightly off, never "the tool can never time out".
        self.inner
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }
}

/// RAII pause handle — see [`ExecClock::pause`].
pub struct ExecPauseGuard {
    clock: ExecClock,
}

impl Drop for ExecPauseGuard {
    fn drop(&mut self) {
        self.clock.resume();
    }
}

#[derive(Clone)]
pub struct ToolContext {
    pub session_id: SessionId,
    /// Channel for sending user-space events (progress, sub-agent, structured).
    /// Tools should use `emit_user_event()` or `emit_progress()`.
    pub user_event_tx: mpsc::UnboundedSender<UserEvent>,
    pub llm_client: Option<Arc<dyn llm_trait::LlmProvider>>,
    pub session_store: Option<Arc<dyn SessionStore>>,
    /// Language preference for tool output.
    /// Defaults to `Language::En` if not set.
    pub language: crate::types::Language,
    /// Cancellation token for checking if the operation should be cancelled.
    pub cancel_token: tokio_util::sync::CancellationToken,
    /// Output budget for this call, in characters (set from the engine's
    /// `max_tool_output_chars`). Tools that can return large results (e.g.
    /// `read_file`) should self-truncate to this bound and mark the cut, so
    /// the engine's hard reject (§6.5) never fires for a paginated read.
    pub max_output_chars: Option<usize>,
    /// Execution clock for this call — see [`ExecClock`]. Pause it while the
    /// tool waits on a human ([`ToolContext::pause_execution_clock`]).
    pub exec_clock: ExecClock,
    /// Set by [`ToolContext::mark_waiting_for_human`] when the call left a
    /// human step pending (a pick, a confirmation) instead of finishing the
    /// work. The engine folds it into the run state after the call.
    pub waiting_for_human: Arc<AtomicBool>,
    /// Internal runtime event bus (framework tools emit `RuntimeEvent`s here).
    /// `pub(crate)` — engine-internal; user tools should use `emit_user_event()`.
    pub(crate) event_bus: crate::engine::EventBus,
}

impl ToolContext {
    /// Pause this call's execution clock for as long as the guard lives.
    ///
    /// Call this while waiting on a human (a confirmation popup, a login):
    /// the pipeline's tool timeout does not count the paused stretch.
    pub fn pause_execution_clock(&self) -> ExecPauseGuard {
        self.exec_clock.pause()
    }

    /// Mark that this call left a human step pending (a design-canvas screen
    /// awaiting a pick, a confirmation). The engine records it on the run: a
    /// run that ends here is *waiting for the user*, not an incomplete task,
    /// so the guard must not nudge "continue working" over the human's turn.
    pub fn mark_waiting_for_human(&self) {
        self.waiting_for_human.store(true, Ordering::Relaxed);
    }

    /// Send a user-space event (progress, sub-agent forwarding, structured data).
    ///
    /// Dual-write: the event goes to `user_event_tx` (the tool select arm
    /// renders it in real time) and to the event bus for external subscribers
    /// (bridge remote clients, persistence). The bus copy carries `agent_id:
    /// None` — the self-echo marker — so bus loopback drops it instead of
    /// re-rendering (see `crate::engine::runtime::is_self_echo_user_event`).
    pub fn emit_user_event(&self, event: UserEvent) {
        let _ = self.user_event_tx.send(event.clone());
        self.event_bus.emit(RuntimeEvent::UserEvent {
            session_id: self.session_id.clone(),
            event,
            agent_id: None,
            trace_id: None,
        });
    }

    /// Convenience: send a progress event with text.
    pub fn emit_progress(&self, text: impl Into<String>) {
        self.emit_user_event(UserEvent::Progress { text: text.into() });
    }

    /// Convenience: emit a partial result during long-running tool execution.
    /// `is_partial: true` means more output is coming; `false` means final.
    pub fn emit_partial_result(
        &self,
        tool_call_id: &str,
        content: impl Into<String>,
        is_partial: bool,
    ) {
        self.emit_user_event(UserEvent::ToolPartialResult {
            tool_call_id: tool_call_id.to_string(),
            content: content.into(),
            is_partial,
        });
    }

    /// Test-only constructor: a `ToolContext` with a disconnected event
    /// channel and no LLM/session backends. Lets downstream tests drop their
    /// bespoke `dummy_ctx()` helpers.
    pub fn for_test() -> Self {
        let (tx, _rx) = mpsc::unbounded_channel();
        ToolContext {
            session_id: SessionId::new(0),
            user_event_tx: tx,
            llm_client: None,
            session_store: None,
            language: crate::types::Language::En,
            cancel_token: tokio_util::sync::CancellationToken::new(),
            max_output_chars: None,
            exec_clock: ExecClock::default(),
            waiting_for_human: Arc::new(AtomicBool::new(false)),
            event_bus: crate::engine::EventBus::new(1),
        }
    }

    /// Host-side constructor for host-initiated tool execution outside the
    /// engine loop (e.g. a REPL driving a tool through the same approval path
    /// as the engine). User events are accepted but dropped — there is no
    /// engine event loop to render them.
    pub fn new(
        session_id: SessionId,
        llm_client: Option<Arc<dyn crate::llm_trait::LlmProvider>>,
        session_store: Option<Arc<dyn SessionStore>>,
        cancel_token: tokio_util::sync::CancellationToken,
    ) -> Self {
        let (tx, _rx) = mpsc::unbounded_channel();
        ToolContext {
            session_id,
            user_event_tx: tx,
            llm_client,
            session_store,
            language: crate::types::Language::En,
            cancel_token,
            max_output_chars: None,
            exec_clock: ExecClock::default(),
            waiting_for_human: Arc::new(AtomicBool::new(false)),
            event_bus: crate::engine::EventBus::new(1),
        }
    }
}

#[async_trait]
pub trait Tool: Send + Sync {
    fn name(&self) -> &'static str;
    /// Human-readable description of what this tool does and when to use it.
    fn description(&self) -> &'static str;
    /// JSON Schema for the tool's input arguments (MCP `inputSchema` shape,
    /// without the provider envelope).
    fn schema(&self) -> Value;
    async fn call(&self, args: &Value, ctx: &ToolContext) -> AgentResult<Vec<Content>>;

    /// Timeout for this tool in milliseconds.
    ///
    /// Returns `Some(ms)` to enforce a timeout, or `None` to use the
    /// framework's default timeout from `ToolConfig.default_tool_timeout_ms`.
    /// Tools that need more or less time should override this method.
    fn timeout_ms(&self) -> Option<u64> {
        None // Use framework default
    }

    /// Execution budget for a specific call, derived from its `args`.
    ///
    /// The pipeline watchdog consults this before arming: `Some(ms)` overrides
    /// [`Tool::timeout_ms`] for this invocation, and `Some(0)` means unbounded
    /// (no watchdog — the tool enforces its own limits, or the call is
    /// fire-and-forget). `None` falls back to [`Tool::timeout_ms`] / config.
    ///
    /// Tools whose arguments carry a per-call timeout (e.g. `timeout_ms`)
    /// MUST surface it here. Otherwise the pipeline arms its own watchdog
    /// from the tool-level default and can kill a call that the tool was
    /// explicitly promised more time for (session 20261018_7844215b: a
    /// 660s `execute_command` budget was cut at the 600s framework default).
    fn call_timeout_ms(&self, _args: &Value) -> Option<u64> {
        self.timeout_ms()
    }

    /// Machine-readable metadata for tool introspection.
    ///
    /// The default implementation derives `name` and `description` from
    /// [`Tool::name`] and [`Tool::description`], sets `origin` to `"custom"`,
    /// and leaves `requirements` empty. Tool authors are encouraged to
    /// override this to provide an accurate `origin` and `version`.
    fn metadata(&self) -> ToolMetadata {
        ToolMetadata {
            name: self.name().to_string(),
            description: self.description().to_string(),
            origin: "custom".to_string(),
            version: "unknown".to_string(),
            requirements: vec![],
        }
    }

    /// Visibility level for this tool. Default: `Direct` (always visible).
    ///
    /// Override to return `Deferred` (conditionally visible) or `Hidden`
    /// (never visible to the model). `Deferred` tools are only included
    /// when [`Tool::should_activate`] returns `true`.
    fn exposure(&self) -> ToolExposure {
        ToolExposure::Direct
    }

    /// Activation condition for `Deferred` tools.
    ///
    /// Called once per turn for each `Deferred` tool. Return `true` to
    /// include this tool in the model's tool list this turn. Ignored for
    /// `Direct` and `Hidden` tools.
    fn should_activate(&self, _ctx: &ActivationContext) -> bool {
        true
    }
}

#[async_trait]
pub trait TypedTool: Send + Sync {
    type Args: serde::de::DeserializeOwned + schemars::JsonSchema;
    type Output: serde::Serialize;

    fn name(&self) -> &'static str;
    fn description(&self) -> &'static str;
    async fn call_typed(&self, args: Self::Args, ctx: &ToolContext) -> AgentResult<Self::Output>;

    fn format_output(&self, output: Self::Output) -> Content {
        // A `String` output is emitted verbatim; any other serializable type is
        // rendered as JSON. This avoids `serde_json::to_string` wrapping a plain
        // string in literal double quotes (`hello` → `"hello"`), which would leak
        // quotes into the LLM-visible tool result.
        match serde_json::to_value(&output) {
            Ok(serde_json::Value::String(s)) => Content::text(s),
            Ok(other) => Content::text(other.to_string()),
            Err(_) => Content::text(String::new()),
        }
    }

    /// Machine-readable origin of this tool (crate name, `"agent-base"`, or `"custom"`).
    fn origin(&self) -> &'static str {
        "custom"
    }

    /// Crate/package version, or `"unknown"` when built outside a crate.
    fn version(&self) -> &'static str {
        "unknown"
    }

    /// Visibility level for this tool. Default: `Direct` (always visible).
    fn exposure(&self) -> ToolExposure {
        ToolExposure::Direct
    }

    /// Activation condition for `Deferred` tools. Ignored for `Direct`/`Hidden`.
    fn should_activate(&self, _ctx: &ActivationContext) -> bool {
        true
    }
}

#[async_trait]
impl<T: TypedTool + Send + Sync + 'static> Tool for T {
    fn name(&self) -> &'static str {
        TypedTool::name(self)
    }

    fn description(&self) -> &'static str {
        TypedTool::description(self)
    }

    fn schema(&self) -> Value {
        // Generate a provider-safe JSON Schema: Draft 7 (not 2020-12), with
        // nested subschemas inlined (no `$ref`/`$defs`/`/definitions`) and no
        // root `$schema`/meta-schema key. OpenAI-compatible function-calling
        // rejects `$ref` and 2020-12's `$defs`, so the default 2020-12 output
        // would break any `Args` containing a nested enum or struct.
        let settings = schemars::generate::SchemaSettings::draft07().with(|s| {
            s.inline_subschemas = true;
            s.meta_schema = None;
        });
        let generator = schemars::SchemaGenerator::new(settings);
        let schema = generator.into_root_schema_for::<T::Args>();
        serde_json::to_value(schema).unwrap_or(Value::Null)
    }

    fn metadata(&self) -> ToolMetadata {
        ToolMetadata {
            name: self.name().to_string(),
            description: self.description().to_string(),
            origin: self.origin().to_string(),
            version: self.version().to_string(),
            requirements: vec![],
        }
    }

    fn exposure(&self) -> ToolExposure {
        TypedTool::exposure(self)
    }

    fn should_activate(&self, ctx: &ActivationContext) -> bool {
        TypedTool::should_activate(self, ctx)
    }

    async fn call(&self, args: &Value, ctx: &ToolContext) -> AgentResult<Vec<Content>> {
        let typed_args: T::Args =
            serde_json::from_value(args.clone()).map_err(|e| AgentError::ToolArgsInvalid {
                name: self.name().to_string(),
                // Carry the serde detail ("missing field `task` …") — the model
                // can only fix what the message names. Same "{e} (args: …)"
                // shape as the phase1 JSON-parse failure site (tool_engine.rs)
                // and the contract asserted in recovery.rs.
                raw: format!("{e} (args: {args})"),
            })?;
        let output = self.call_typed(typed_args, ctx).await?;
        Ok(vec![self.format_output(output)])
    }
}

/// Render a single tool's definition into the OpenAI function-calling
/// envelope. Tools only provide `name`/`description`/`schema`; the envelope is
/// assembled here at the LLM boundary (Anthropic gets its own renderer, and
/// tool authors stay protocol-agnostic).
pub fn render_tool_definition(tool: &dyn Tool) -> Value {
    json!({
        "type": "function",
        "function": {
            "name": tool.name(),
            "description": tool.description(),
            "parameters": tool.schema(),
        }
    })
}

pub(crate) type ToolRef = Arc<dyn Tool>;

#[derive(Clone, Default)]
pub struct ToolRegistry {
    tools: HashMap<String, ToolRef>,
}

impl ToolRegistry {
    pub fn register(&mut self, tool: impl Tool + 'static) {
        self.tools.insert(tool.name().to_string(), Arc::new(tool));
    }

    pub fn register_arc(&mut self, tool: Arc<dyn Tool>) {
        self.tools.insert(tool.name().to_string(), tool);
    }

    /// Remove a tool from the registry by name.
    pub fn remove(&mut self, name: &str) {
        self.tools.remove(name);
    }

    pub fn get(&self, name: &str) -> Option<ToolRef> {
        self.tools.get(name).cloned()
    }

    pub fn definitions(&self) -> Vec<Value> {
        let mut tools: Vec<_> = self.tools.values().collect();
        tools.sort_by_key(|t| t.name());
        tools
            .into_iter()
            .map(|t| render_tool_definition(t.as_ref()))
            .collect()
    }

    /// Return tool definitions filtered by [`ToolExposure`].
    ///
    /// - `Direct` tools are always included.
    /// - `Deferred` tools are included only when `should_activate(ctx)` returns `true`.
    /// - `Hidden` tools are never included.
    pub fn definitions_filtered(&self, ctx: &ActivationContext) -> Vec<Value> {
        let mut tools: Vec<_> = self.tools.values().collect();
        tools.sort_by_key(|t| t.name());

        // Single sequential pass: collect Direct names first, then evaluate
        // Deferred tools in sorted order. Each Deferred tool sees the names of
        // all Direct + previously-activated Deferred tools in ctx.current_tools.
        // Evaluation order matters: a later Deferred tool can see an earlier one,
        // but not vice versa.
        let direct_names: Vec<String> = tools
            .iter()
            .filter(|t| t.exposure() == ToolExposure::Direct)
            .map(|t| t.name().to_string())
            .collect();

        let mut activated_names = direct_names.clone();
        for t in &tools {
            if t.exposure() == ToolExposure::Deferred {
                let mut ctx_with_tools = ctx.clone();
                ctx_with_tools.current_tools = activated_names.clone();
                if t.should_activate(&ctx_with_tools) {
                    activated_names.push(t.name().to_string());
                }
            }
        }

        tools
            .into_iter()
            .filter(|t| match t.exposure() {
                ToolExposure::Direct => true,
                ToolExposure::Deferred => activated_names.contains(&t.name().to_string()),
                ToolExposure::Hidden => false,
            })
            .map(|t| render_tool_definition(t.as_ref()))
            .collect()
    }

    pub fn len(&self) -> usize {
        self.tools.len()
    }

    pub fn is_empty(&self) -> bool {
        self.tools.is_empty()
    }

    /// Collect metadata for every registered tool, sorted by name.
    ///
    /// This is the preferred introspection API for consumers — it returns a
    /// stable `ToolMetadata` struct per tool instead of having callers parse
    /// the LLM-facing JSON definitions.
    pub fn metadatas(&self) -> Vec<ToolMetadata> {
        let mut list: Vec<_> = self.tools.values().map(|tool| tool.metadata()).collect();
        list.sort_by(|a, b| a.name.cmp(&b.name));
        list
    }
}

#[cfg(test)]
mod tests;