sombrax_agentic_core 0.1.1

SombraX Agentic Core (SAC) — a provider-agnostic Rust library for building LLM agents: content-modifying hooks, tool/MCP integration, and context optimization.
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
//! Task tool for spawning sub-agents
//!
//! The TaskTool enables spawning sub-agents to handle complex, multi-step tasks.
//! It requires an [`AgentRuntime`] to be injected for real execution; otherwise,
//! it operates in no-op mode.
//!
//! # Example
//!
//! ```rust,no_run
//! use sombrax_agentic_core::tools::{ToolContext, TaskTool};
//! use sombrax_agentic_core::tools::agent::{AgentRuntime, SubAgentRequest, SubAgentResponse};
//! use std::sync::Arc;
//! use std::path::PathBuf;
//!
//! // With a real runtime
//! # struct MyRuntime;
//! # #[async_trait::async_trait]
//! # impl AgentRuntime for MyRuntime {
//! #     async fn spawn_subagent(&self, _: SubAgentRequest) -> Result<SubAgentResponse, String> { todo!() }
//! # }
//! let context = ToolContext::new("session".into(), PathBuf::from("."));
//! let runtime: Arc<dyn AgentRuntime> = Arc::new(MyRuntime);
//! let task_tool = TaskTool::new(context).with_runtime(runtime);
//! ```

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tracing::instrument;

use super::runtime::{no_op_runtime, AgentRuntime, SubAgentRequest};
use crate::tools::context::ToolContext;
use crate::tools::error::ToolError;
use crate::tools::registry::{Tool, ToolDefinition};

/// Spawn sub-agents for complex tasks
#[derive(Clone)]
pub struct TaskTool {
    context: ToolContext,
    runtime: Arc<dyn AgentRuntime>,
}

impl TaskTool {
    /// Create a new task tool with no-op runtime
    ///
    /// The tool will operate in no-op mode until a runtime is injected via
    /// [`with_runtime`](Self::with_runtime).
    pub fn new(context: ToolContext) -> Self {
        Self {
            context,
            runtime: no_op_runtime(),
        }
    }

    /// Set the agent runtime for sub-agent execution
    ///
    /// This enables real sub-agent spawning. Without a runtime, the tool
    /// returns an error indicating that execution is not available.
    pub fn with_runtime(mut self, runtime: Arc<dyn AgentRuntime>) -> Self {
        self.runtime = runtime;
        self
    }

    /// Set maximum recursion depth
    ///
    /// Note: The actual max depth is determined by the runtime.
    /// This method is kept for API compatibility.
    #[deprecated(note = "Max depth is now controlled by the AgentRuntime")]
    pub fn with_max_depth(self, _depth: usize) -> Self {
        self
    }

    /// Check if a runtime is configured (not no-op)
    pub fn has_runtime(&self) -> bool {
        // Check if the runtime supports any subagent type
        // NoOpRuntime returns false for all types
        self.runtime.supports_subagent_type("general-purpose")
    }
}

/// Arguments for the task tool
#[derive(Debug, Deserialize, JsonSchema)]
pub struct TaskArgs {
    /// Short description (3-5 words)
    pub description: String,
    /// Detailed prompt for the sub-agent
    pub prompt: String,
    /// Type of sub-agent to spawn
    pub subagent_type: String,
    /// Override model (optional, inherits from parent)
    #[serde(default)]
    pub model: Option<String>,
}

/// Output of the task tool
#[derive(Debug, Serialize)]
pub struct TaskOutput {
    /// Child session ID
    pub child_session_id: String,
    /// Sub-agent type used
    pub subagent_type: String,
    /// Task description
    pub description: String,
    /// Response from sub-agent
    pub response: String,
    /// Number of conversation turns (if available)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conversation_turns: Option<usize>,
}

impl Tool for TaskTool {
    const NAME: &'static str = "task";
    type Args = TaskArgs;
    type Output = TaskOutput;
    type Error = ToolError;

    async fn definition(&self, _prompt: String) -> ToolDefinition {
        let schema = schemars::schema_for!(TaskArgs);
        ToolDefinition {
            name: Self::NAME.to_string(),
            description: r#"Launch a sub-agent to handle complex, multi-step tasks autonomously.

## BEFORE CALLING THIS TOOL

Think step-by-step:
1. Is this task complex enough to need a sub-agent?
2. What type of sub-agent is best suited for this task?
3. What clear instructions should I provide?

## PARAMETERS

- `description` (REQUIRED, STRING): Short description (3-5 words)
  CORRECT: "Analyze security vulnerabilities"
  CORRECT: "Search for API endpoints"
  WRONG: {"description": "..."} <-- Do NOT pass JSON objects!
  WRONG: {} <-- Empty object is invalid!

- `prompt` (REQUIRED, STRING): Detailed instructions for the sub-agent
  Be specific about what the sub-agent should do and return.

- `subagent_type` (REQUIRED, STRING): Type of sub-agent to spawn
  Common types: "general-purpose", "explore", "research"
  The available types depend on the runtime configuration.

- `model` (optional, STRING): Override the model for this sub-agent
  If not specified, inherits from parent agent.

## EXAMPLES

Launch an exploration sub-agent:
  description: "Find auth handlers"
  prompt: "Search the codebase for authentication and authorization handlers. Return a list of files and their purposes."
  subagent_type: "explore"

Launch a research sub-agent:
  description: "Research API patterns"
  prompt: "Analyze the existing API patterns in this codebase and summarize the conventions used."
  subagent_type: "general-purpose"

## WHEN TO USE THIS TOOL

- Complex tasks requiring multiple steps
- Tasks that benefit from focused, autonomous execution
- Parallel exploration of different aspects of a problem

## WHEN NOT TO USE THIS TOOL

- Simple, single-step operations
- Tasks you can do directly with other tools
- When sub-agent runtime is not configured

## COMMON MISTAKES TO AVOID

1. Do NOT pass JSON objects as parameters - use plain strings
2. Do NOT use empty or vague descriptions
3. Do NOT spawn sub-agents for trivial tasks
4. Do NOT exceed maximum recursion depth (sub-agents spawning sub-agents)
"#.to_string(),
            parameters: serde_json::to_value(schema).unwrap_or_default(),
        }
    }

    #[instrument(skip(self), fields(tool = "task", subagent_type = %args.subagent_type))]
    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        // Check recursion depth
        let max_depth = self.runtime.max_depth();
        if self.context.current_depth() >= max_depth {
            tracing::warn!(
                current_depth = %self.context.current_depth(),
                max_depth = %max_depth,
                "Max recursion depth exceeded"
            );
            return Err(ToolError::MaxRecursionDepth);
        }

        // Validate arguments
        if args.description.trim().is_empty() {
            return Err(ToolError::Validation("description cannot be empty".into()));
        }
        if args.prompt.trim().is_empty() {
            return Err(ToolError::Validation("prompt cannot be empty".into()));
        }
        if args.subagent_type.trim().is_empty() {
            return Err(ToolError::Validation(
                "subagent_type cannot be empty".into(),
            ));
        }

        // Validate subagent type is supported
        if !self.runtime.supports_subagent_type(&args.subagent_type) {
            // Log warning for unknown types but allow them through
            // The runtime will handle actual validation
            tracing::warn!(
                subagent_type = %args.subagent_type,
                "Unknown or unsupported subagent type"
            );
        }

        // Build the request
        let request = SubAgentRequest {
            description: args.description.clone(),
            prompt: args.prompt,
            subagent_type: args.subagent_type.clone(),
            model: args.model,
            parent_session_id: self.context.session_id().to_string(),
            current_depth: self.context.current_depth(),
            initial_messages: self.context.initial_messages().to_vec(),
        };

        // Delegate to runtime
        tracing::info!(
            description = %args.description,
            subagent_type = %args.subagent_type,
            "Spawning sub-agent via runtime"
        );

        let response = self
            .runtime
            .spawn_subagent(request)
            .await
            .map_err(|e| ToolError::Validation(format!("Sub-agent execution failed: {}", e)))?;

        tracing::info!(
            child_session_id = %response.child_session_id,
            response_len = %response.response.len(),
            "Sub-agent completed"
        );

        Ok(TaskOutput {
            child_session_id: response.child_session_id,
            subagent_type: args.subagent_type,
            description: args.description,
            response: response.response,
            conversation_turns: response.conversation_turns,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::agent::runtime::SubAgentResponse;
    use std::path::PathBuf;

    struct MockRuntime {
        response: String,
    }

    #[async_trait::async_trait]
    impl AgentRuntime for MockRuntime {
        async fn spawn_subagent(
            &self,
            request: SubAgentRequest,
        ) -> Result<SubAgentResponse, String> {
            Ok(SubAgentResponse {
                child_session_id: format!("{}-child-mock", request.parent_session_id),
                response: self.response.clone(),
                conversation_turns: Some(1),
            })
        }
    }

    #[tokio::test]
    async fn test_task_tool_with_runtime() {
        let context = ToolContext::new("test-session".into(), PathBuf::from("."));
        let runtime: Arc<dyn AgentRuntime> = Arc::new(MockRuntime {
            response: "Task completed successfully".into(),
        });
        let tool = TaskTool::new(context).with_runtime(runtime);

        assert!(tool.has_runtime());

        let args = TaskArgs {
            description: "Test task".into(),
            prompt: "Do something useful".into(),
            subagent_type: "general-purpose".into(),
            model: None,
        };

        let result = tool.call(args).await;
        assert!(result.is_ok());

        let output = result.unwrap();
        assert_eq!(output.child_session_id, "test-session-child-mock");
        assert_eq!(output.response, "Task completed successfully");
        assert_eq!(output.conversation_turns, Some(1));
    }

    #[tokio::test]
    async fn test_task_tool_without_runtime() {
        let context = ToolContext::new("test-session".into(), PathBuf::from("."));
        let tool = TaskTool::new(context);

        assert!(!tool.has_runtime());

        let args = TaskArgs {
            description: "Test task".into(),
            prompt: "Do something".into(),
            subagent_type: "general-purpose".into(),
            model: None,
        };

        let result = tool.call(args).await;
        assert!(result.is_err());

        let err = result.unwrap_err();
        assert!(err.to_string().contains("No AgentRuntime is configured"));
    }

    #[tokio::test]
    async fn test_task_tool_validates_empty_fields() {
        let context = ToolContext::new("test-session".into(), PathBuf::from("."));
        let runtime: Arc<dyn AgentRuntime> = Arc::new(MockRuntime {
            response: "ok".into(),
        });
        let tool = TaskTool::new(context).with_runtime(runtime);

        // Empty description
        let args = TaskArgs {
            description: "".into(),
            prompt: "Do something".into(),
            subagent_type: "general-purpose".into(),
            model: None,
        };
        assert!(tool.call(args).await.is_err());

        // Empty prompt
        let context = ToolContext::new("test-session".into(), PathBuf::from("."));
        let runtime: Arc<dyn AgentRuntime> = Arc::new(MockRuntime {
            response: "ok".into(),
        });
        let tool = TaskTool::new(context).with_runtime(runtime);
        let args = TaskArgs {
            description: "Test".into(),
            prompt: "".into(),
            subagent_type: "general-purpose".into(),
            model: None,
        };
        assert!(tool.call(args).await.is_err());

        // Empty subagent_type
        let context = ToolContext::new("test-session".into(), PathBuf::from("."));
        let runtime: Arc<dyn AgentRuntime> = Arc::new(MockRuntime {
            response: "ok".into(),
        });
        let tool = TaskTool::new(context).with_runtime(runtime);
        let args = TaskArgs {
            description: "Test".into(),
            prompt: "Do something".into(),
            subagent_type: "".into(),
            model: None,
        };
        assert!(tool.call(args).await.is_err());
    }

    #[tokio::test]
    async fn test_definition() {
        let context = ToolContext::new("test-session".into(), PathBuf::from("."));
        let tool = TaskTool::new(context);

        let def = tool.definition("test prompt".into()).await;
        assert_eq!(def.name, "task");
        assert!(def.description.contains("sub-agent"));
    }

    #[tokio::test]
    async fn test_task_tool_passes_initial_messages() {
        use crate::message::{AssistantContent, Message};
        use std::sync::atomic::{AtomicUsize, Ordering};

        // Runtime that captures the initial_messages count
        struct CapturingRuntime {
            message_count: Arc<AtomicUsize>,
        }

        #[async_trait::async_trait]
        impl AgentRuntime for CapturingRuntime {
            async fn spawn_subagent(
                &self,
                request: SubAgentRequest,
            ) -> Result<SubAgentResponse, String> {
                self.message_count
                    .store(request.initial_messages.len(), Ordering::SeqCst);
                Ok(SubAgentResponse {
                    child_session_id: format!("{}-child", request.parent_session_id),
                    response: "done".into(),
                    conversation_turns: Some(1),
                })
            }
        }

        // Create context with initial messages
        let initial_msgs = vec![
            Message::Assistant {
                id: None,
                content: vec![AssistantContent::Text {
                    text: "Source code context".into(),
                }],
                reasoning: None,
            },
            Message::Assistant {
                id: None,
                content: vec![AssistantContent::Text {
                    text: "More context".into(),
                }],
                reasoning: None,
            },
        ];

        let context = ToolContext::new("test-session".into(), PathBuf::from("."))
            .with_initial_messages(initial_msgs);

        let message_count = Arc::new(AtomicUsize::new(0));
        let runtime: Arc<dyn AgentRuntime> = Arc::new(CapturingRuntime {
            message_count: message_count.clone(),
        });
        let tool = TaskTool::new(context).with_runtime(runtime);

        let args = TaskArgs {
            description: "Test".into(),
            prompt: "Do task".into(),
            subagent_type: "general-purpose".into(),
            model: None,
        };

        let result = tool.call(args).await;
        assert!(result.is_ok());
        assert_eq!(message_count.load(Ordering::SeqCst), 2);
    }
}