meerkat-tools 0.4.4

Tool validation and dispatch for Meerkat
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
//! Configuration and error types for sub-agent tools

use meerkat_core::config::ResolvedSubAgentConfig;
use meerkat_core::ops::ConcurrencyLimits;
use serde::{Deserialize, Serialize};

/// Error types for sub-agent tool operations
#[derive(Debug, thiserror::Error)]
pub enum SubAgentError {
    /// Sub-agent was not found
    #[error("Sub-agent not found: {id}")]
    NotFound { id: String },

    /// Sub-agent is not in the expected state
    #[error("Sub-agent {id} is {actual_state}, expected {expected_state}")]
    InvalidState {
        id: String,
        actual_state: String,
        expected_state: String,
    },

    /// Concurrency limit exceeded
    #[error("Concurrency limit exceeded: {message}")]
    ConcurrencyLimitExceeded { message: String },

    /// Depth limit exceeded
    #[error("Maximum sub-agent depth ({max_depth}) exceeded")]
    DepthLimitExceeded { max_depth: u32 },

    /// Invalid provider specified
    #[error("Invalid provider: {provider}")]
    InvalidProvider { provider: String },

    /// Invalid model specified (not in allowlist)
    #[error("Model '{model}' is not in allowlist for provider '{provider}'. Allowed models: {}", allowed.join(", "))]
    InvalidModel {
        model: String,
        provider: String,
        allowed: Vec<String>,
    },

    /// Missing API key for provider
    #[error("Missing API key for provider: {provider}")]
    MissingApiKey { provider: String },

    /// Invalid arguments provided to tool
    #[error("Invalid arguments: {message}")]
    InvalidArguments { message: String },

    /// Sub-agent execution failed
    #[error("Sub-agent execution failed: {message}")]
    ExecutionFailed { message: String },

    /// Tool access policy violation
    #[error("Tool access denied: {tool_name}")]
    ToolAccessDenied { tool_name: String },

    /// Client factory error
    #[error("Client factory error: {message}")]
    ClientFactoryError { message: String },
}

impl SubAgentError {
    /// Create a NotFound error
    pub fn not_found(id: impl Into<String>) -> Self {
        Self::NotFound { id: id.into() }
    }

    /// Create an InvalidState error
    pub fn invalid_state(
        id: impl Into<String>,
        actual: impl Into<String>,
        expected: impl Into<String>,
    ) -> Self {
        Self::InvalidState {
            id: id.into(),
            actual_state: actual.into(),
            expected_state: expected.into(),
        }
    }

    /// Create a ConcurrencyLimitExceeded error
    pub fn concurrency_limit(message: impl Into<String>) -> Self {
        Self::ConcurrencyLimitExceeded {
            message: message.into(),
        }
    }

    /// Create a DepthLimitExceeded error
    pub fn depth_limit(max_depth: u32) -> Self {
        Self::DepthLimitExceeded { max_depth }
    }

    /// Create an InvalidProvider error
    pub fn invalid_provider(provider: impl Into<String>) -> Self {
        Self::InvalidProvider {
            provider: provider.into(),
        }
    }

    /// Create a MissingApiKey error
    pub fn missing_api_key(provider: impl Into<String>) -> Self {
        Self::MissingApiKey {
            provider: provider.into(),
        }
    }

    /// Create an InvalidArguments error
    pub fn invalid_arguments(message: impl Into<String>) -> Self {
        Self::InvalidArguments {
            message: message.into(),
        }
    }

    /// Create an ExecutionFailed error
    pub fn execution_failed(message: impl Into<String>) -> Self {
        Self::ExecutionFailed {
            message: message.into(),
        }
    }

    /// Create a ToolAccessDenied error
    pub fn tool_access_denied(tool_name: impl Into<String>) -> Self {
        Self::ToolAccessDenied {
            tool_name: tool_name.into(),
        }
    }

    /// Create a ClientFactoryError
    pub fn client_factory_error(message: impl Into<String>) -> Self {
        Self::ClientFactoryError {
            message: message.into(),
        }
    }
}

/// Configuration for sub-agent tools
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubAgentConfig {
    /// Default provider for spawned agents (e.g., "anthropic", "openai", "gemini")
    #[serde(default = "default_provider")]
    pub default_provider: String,

    /// Default model for spawned agents (provider-specific)
    #[serde(default)]
    pub default_model: Option<String>,

    /// Concurrency limits for sub-agents
    #[serde(default)]
    pub concurrency_limits: ConcurrencyLimits,

    /// Whether sub-agents can spawn further sub-agents by default
    #[serde(default = "default_allow_nested_spawn")]
    pub allow_nested_spawn: bool,

    /// Maximum budget (in tokens) that can be allocated to a single sub-agent
    #[serde(default)]
    pub max_budget_per_agent: Option<u64>,

    /// Default budget (in tokens) for spawned agents when not specified
    #[serde(default)]
    pub default_budget: Option<u64>,

    /// Whether to inherit system prompt by default
    #[serde(default = "default_inherit_system_prompt")]
    pub inherit_system_prompt: bool,

    /// Whether to enable comms for sub-agents (parent-child communication)
    #[serde(default)]
    pub enable_comms: bool,

    /// Base directory for sub-agent comms identity files
    /// Defaults to a temporary directory
    #[serde(default)]
    pub comms_base_dir: Option<std::path::PathBuf>,

    /// Resolved sub-agent model policy from the global config.
    /// When set, overrides the hardcoded allowlists and default provider/model.
    #[serde(skip)]
    pub resolved_policy: Option<ResolvedSubAgentConfig>,
}

fn default_provider() -> String {
    "anthropic".to_string()
}

fn default_allow_nested_spawn() -> bool {
    true
}

fn default_inherit_system_prompt() -> bool {
    true
}

impl Default for SubAgentConfig {
    fn default() -> Self {
        Self {
            default_provider: default_provider(),
            default_model: None,
            concurrency_limits: ConcurrencyLimits::default(),
            allow_nested_spawn: default_allow_nested_spawn(),
            max_budget_per_agent: None,
            default_budget: Some(50000), // 50k tokens default
            inherit_system_prompt: default_inherit_system_prompt(),
            enable_comms: false,
            comms_base_dir: None,
            resolved_policy: None,
        }
    }
}

impl SubAgentConfig {
    /// Create a new config with defaults
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the default provider
    pub fn with_default_provider(mut self, provider: impl Into<String>) -> Self {
        self.default_provider = provider.into();
        self
    }

    /// Set the default model
    pub fn with_default_model(mut self, model: impl Into<String>) -> Self {
        self.default_model = Some(model.into());
        self
    }

    /// Set concurrency limits
    pub fn with_concurrency_limits(mut self, limits: ConcurrencyLimits) -> Self {
        self.concurrency_limits = limits;
        self
    }

    /// Set whether nested spawn is allowed
    pub fn with_allow_nested_spawn(mut self, allow: bool) -> Self {
        self.allow_nested_spawn = allow;
        self
    }

    /// Set maximum budget per agent
    pub fn with_max_budget_per_agent(mut self, max_budget: u64) -> Self {
        self.max_budget_per_agent = Some(max_budget);
        self
    }

    /// Set default budget
    pub fn with_default_budget(mut self, budget: u64) -> Self {
        self.default_budget = Some(budget);
        self
    }

    /// Set whether to inherit system prompt
    pub fn with_inherit_system_prompt(mut self, inherit: bool) -> Self {
        self.inherit_system_prompt = inherit;
        self
    }

    /// Enable comms for sub-agents
    pub fn with_enable_comms(mut self, enable: bool) -> Self {
        self.enable_comms = enable;
        self
    }

    /// Set the comms base directory
    pub fn with_comms_base_dir(mut self, dir: std::path::PathBuf) -> Self {
        self.comms_base_dir = Some(dir);
        self
    }

    /// Set the resolved sub-agent model policy from global config.
    pub fn with_resolved_policy(mut self, policy: ResolvedSubAgentConfig) -> Self {
        self.resolved_policy = Some(policy);
        self
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn test_sub_agent_error_not_found() {
        let err = SubAgentError::not_found("agent-123");
        assert_eq!(err.to_string(), "Sub-agent not found: agent-123");
    }

    #[test]
    fn test_sub_agent_error_invalid_state() {
        let err = SubAgentError::invalid_state("agent-123", "completed", "running");
        assert_eq!(
            err.to_string(),
            "Sub-agent agent-123 is completed, expected running"
        );
    }

    #[test]
    fn test_sub_agent_error_concurrency_limit() {
        let err = SubAgentError::concurrency_limit("max 8 agents exceeded");
        assert_eq!(
            err.to_string(),
            "Concurrency limit exceeded: max 8 agents exceeded"
        );
    }

    #[test]
    fn test_sub_agent_error_depth_limit() {
        let err = SubAgentError::depth_limit(3);
        assert_eq!(err.to_string(), "Maximum sub-agent depth (3) exceeded");
    }

    #[test]
    fn test_sub_agent_error_invalid_provider() {
        let err = SubAgentError::invalid_provider("unknown");
        assert_eq!(err.to_string(), "Invalid provider: unknown");
    }

    #[test]
    fn test_sub_agent_error_missing_api_key() {
        let err = SubAgentError::missing_api_key("openai");
        assert_eq!(err.to_string(), "Missing API key for provider: openai");
    }

    #[test]
    fn test_sub_agent_error_invalid_arguments() {
        let err = SubAgentError::invalid_arguments("missing prompt field");
        assert_eq!(err.to_string(), "Invalid arguments: missing prompt field");
    }

    #[test]
    fn test_sub_agent_error_execution_failed() {
        let err = SubAgentError::execution_failed("timeout");
        assert_eq!(err.to_string(), "Sub-agent execution failed: timeout");
    }

    #[test]
    fn test_sub_agent_error_tool_access_denied() {
        let err = SubAgentError::tool_access_denied("dangerous_tool");
        assert_eq!(err.to_string(), "Tool access denied: dangerous_tool");
    }

    #[test]
    fn test_sub_agent_config_default() {
        let config = SubAgentConfig::default();
        assert_eq!(config.default_provider, "anthropic");
        assert!(config.default_model.is_none());
        assert!(config.allow_nested_spawn);
        assert_eq!(config.default_budget, Some(50000));
        assert!(config.inherit_system_prompt);
    }

    #[test]
    fn test_sub_agent_config_builder() {
        let config = SubAgentConfig::new()
            .with_default_provider("openai")
            .with_default_model("gpt-4")
            .with_allow_nested_spawn(false)
            .with_max_budget_per_agent(100000)
            .with_default_budget(25000)
            .with_inherit_system_prompt(false);

        assert_eq!(config.default_provider, "openai");
        assert_eq!(config.default_model, Some("gpt-4".to_string()));
        assert!(!config.allow_nested_spawn);
        assert_eq!(config.max_budget_per_agent, Some(100000));
        assert_eq!(config.default_budget, Some(25000));
        assert!(!config.inherit_system_prompt);
    }

    #[test]
    fn test_sub_agent_config_serde_roundtrip() {
        let config = SubAgentConfig::new()
            .with_default_provider("gemini")
            .with_default_model("gemini-pro");

        let json = serde_json::to_string(&config).unwrap();
        let parsed: SubAgentConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed.default_provider, "gemini");
        assert_eq!(parsed.default_model, Some("gemini-pro".to_string()));
    }

    #[test]
    fn test_sub_agent_config_serde_defaults() {
        // Empty JSON should use all defaults
        let json = "{}";
        let config: SubAgentConfig = serde_json::from_str(json).unwrap();

        assert_eq!(config.default_provider, "anthropic");
        assert!(config.allow_nested_spawn);
        assert!(config.inherit_system_prompt);
    }

    #[test]
    fn test_sub_agent_error_debug() {
        let err = SubAgentError::not_found("test");
        let debug = format!("{err:?}");
        assert!(debug.contains("NotFound"));
        assert!(debug.contains("test"));
    }
}