microclaw-engine 0.30.0

Channel-independent Agent Engine and default services for MicroClaw
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
//! `explore`: a synchronous, read-only sub-agent for wide searches.
//!
//! The calling agent hands over a question ("where is the retry policy
//! configured and who calls it?"); a fresh loop with only read/search tools
//! answers it and returns a short report. The main conversation receives the
//! conclusion instead of every file it took to get there, which keeps its
//! context small. The tool is read-only, so several `explore` calls in one
//! reply run in parallel.
//!
//! Project agents: a trusted workspace can define focused helpers in
//! `.microclaw/agents/<name>.md` (front matter `description`, optional
//! `model` and `tools`; the body is the system prompt), selected with
//! `agent` here and in `sessions_spawn`.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use async_trait::async_trait;
use serde_json::json;

use super::{schema_object, Tool, ToolRegistry, ToolResult};
use crate::config::Config;
use crate::internal::storage::db::Database;
use crate::internal::tool_runtime::runtime::{
    auth_context_from_input, tool_concurrency_class, ToolAuthContext, ToolConcurrencyClass,
};
use microclaw_core::llm_types::{
    ContentBlock, Message, MessageContent, ResponseContentBlock, ToolDefinition,
};

const MAX_ITERATIONS: usize = 30;
const MAX_REPORT_CHARS: usize = 16_000;
/// Tool output kept per call inside the explore loop.
const MAX_TOOL_OUTPUT_CHARS: usize = 24_000;

const DEFAULT_PROMPT: &str = "You are an exploration helper working for another agent. Answer its question by reading and searching the workspace (read_file, grep, glob, and web tools when the question needs them). You cannot change anything. Be efficient: search before reading, read only what you need. Finish with a concise report: the answer, the exact file paths and line numbers that support it, and anything uncertain. No preamble.";

/// A project-defined helper agent.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProjectAgent {
    pub name: String,
    pub description: String,
    pub model: Option<String>,
    /// Tools the agent may use (`tools:` in the front matter); empty means
    /// the caller's usual set. It can only narrow that set.
    pub tools: Vec<String>,
    pub prompt: String,
}

impl ProjectAgent {
    /// Whether the agent may use `tool` (always true without a `tools` list).
    pub fn allows(&self, tool: &str) -> bool {
        self.tools.is_empty() || self.tools.iter().any(|t| t == tool)
    }
}

/// Front-matter tool names to engine tool names (`read` → `read_file`).
fn tool_names(spec: &str) -> Vec<String> {
    let spec = spec.trim().trim_start_matches('[').trim_end_matches(']');
    let mut tools = Vec::new();
    for raw in spec.split(',') {
        let name = raw.trim().trim_matches(['"', '\'']).to_ascii_lowercase();
        let mapped: &[&str] = match name.as_str() {
            "" => &[],
            "read" => &["read_file"],
            "write" => &["write_file"],
            "edit" => &["edit_file", "apply_patch"],
            "shell" | "bash" => &["bash", "bash_output", "bash_input", "bash_kill"],
            "web" => &["web_fetch", "web_search"],
            "search" => &["grep", "glob"],
            other => {
                tools.push(other.to_string());
                continue;
            }
        };
        tools.extend(mapped.iter().map(|s| s.to_string()));
    }
    tools.sort();
    tools.dedup();
    tools
}

/// Find a project agent by name in `dir`.
pub fn find_project_agent(dir: Option<&str>, name: &str) -> Option<ProjectAgent> {
    load_project_agents(Path::new(dir?))
        .into_iter()
        .find(|agent| agent.name == name)
}

/// Agents defined in `dir` (`<name>.md` with optional front matter).
pub fn load_project_agents(dir: &Path) -> Vec<ProjectAgent> {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return Vec::new();
    };
    let mut agents: Vec<ProjectAgent> = entries
        .filter_map(|entry| entry.ok())
        .map(|entry| entry.path())
        .filter(|path| path.extension().is_some_and(|ext| ext == "md"))
        .filter_map(|path| {
            let name = path.file_stem()?.to_str()?.to_string();
            let text = std::fs::read_to_string(&path).ok()?;
            Some(parse_agent(&name, &text))
        })
        .filter(|agent| !agent.prompt.trim().is_empty())
        .collect();
    agents.sort_by(|a, b| a.name.cmp(&b.name));
    agents
}

fn parse_agent(name: &str, text: &str) -> ProjectAgent {
    let mut description = String::new();
    let mut model = None;
    let mut tools = Vec::new();
    let mut body = text;
    if let Some(rest) = text.strip_prefix("---") {
        if let Some(end) = rest.find("\n---") {
            for line in rest[..end].lines() {
                if let Some((key, value)) = line.split_once(':') {
                    let value = value.trim().trim_matches('"').to_string();
                    match key.trim() {
                        "description" => description = value,
                        "model" if !value.is_empty() => model = Some(value),
                        "tools" => tools = tool_names(&value),
                        _ => {}
                    }
                }
            }
            body = rest[end + 4..].trim_start_matches(['\r', '\n']);
        }
    }
    ProjectAgent {
        name: name.to_string(),
        description,
        model,
        tools,
        prompt: body.trim().to_string(),
    }
}

pub struct ExploreTool {
    config: Config,
    db: Arc<Database>,
    agents: Vec<ProjectAgent>,
}

impl ExploreTool {
    pub fn new(config: &Config, db: Arc<Database>) -> Self {
        let agents = config
            .project_agents_dir
            .as_deref()
            .map(|dir| load_project_agents(&PathBuf::from(dir)))
            .unwrap_or_default();
        Self {
            config: config.clone(),
            db,
            agents,
        }
    }
}

fn truncate(text: &str, max: usize) -> String {
    crate::context_window::truncate_chars(text, max)
}

#[async_trait]
impl Tool for ExploreTool {
    fn name(&self) -> &str {
        "explore"
    }

    fn definition(&self) -> ToolDefinition {
        let mut description = String::from(
            "Delegate a read-only investigation to a helper with its own context: finding where something is defined or used, understanding a subsystem, surveying many files. Returns a short report with file paths and line numbers, so your own context only gets the conclusion. Several explore calls in one reply run in parallel. The helper cannot edit files or run commands. Give it a precise question.",
        );
        if !self.agents.is_empty() {
            description.push_str(" Project agents (pass as `agent`): ");
            let list: Vec<String> = self
                .agents
                .iter()
                .map(|a| {
                    if a.description.is_empty() {
                        a.name.clone()
                    } else {
                        format!("{} ({})", a.name, a.description)
                    }
                })
                .collect();
            description.push_str(&list.join("; "));
            description.push('.');
        }
        let mut properties = json!({
            "task": {
                "type": "string",
                "description": "The question to answer, with any paths or names you already know"
            }
        });
        if !self.agents.is_empty() {
            properties["agent"] = json!({
                "type": "string",
                "enum": self.agents.iter().map(|a| a.name.clone()).collect::<Vec<_>>(),
                "description": "Project agent to use instead of the default explorer"
            });
        }
        ToolDefinition {
            name: "explore".into(),
            description,
            input_schema: schema_object(properties, &["task"]),
        }
    }

    async fn execute(&self, input: serde_json::Value) -> ToolResult {
        let Some(task) = input
            .get("task")
            .and_then(|v| v.as_str())
            .map(str::trim)
            .filter(|t| !t.is_empty())
        else {
            return ToolResult::error("explore: `task` is required".into());
        };
        let agent = match input.get("agent").and_then(|v| v.as_str()) {
            Some(name) => match self.agents.iter().find(|a| a.name == name) {
                Some(agent) => Some(agent.clone()),
                None => return ToolResult::error(format!("explore: unknown agent `{name}`")),
            },
            None => None,
        };
        let parent = auth_context_from_input(&input);
        let auth = ToolAuthContext {
            caller_channel: parent
                .as_ref()
                .map(|a| a.caller_channel.clone())
                .unwrap_or_else(|| "code".into()),
            caller_chat_id: parent.as_ref().map(|a| a.caller_chat_id).unwrap_or(0),
            principal: "subagent:explore".into(),
            control_chat_ids: Vec::new(),
            env_files: Vec::new(),
        };
        match run_explore(&self.config, self.db.clone(), &auth, task, agent.as_ref()).await {
            Ok(report) => ToolResult::success(report),
            Err(e) => ToolResult::error(format!("explore failed: {e}")),
        }
    }
}

/// Read-only tools the explorer may use.
fn explorer_tool(name: &str) -> bool {
    tool_concurrency_class(name) == ToolConcurrencyClass::ReadOnly
        && matches!(
            name,
            "read_file" | "grep" | "glob" | "web_fetch" | "web_search" | "fetch_artifact"
        )
}

async fn run_explore(
    config: &Config,
    db: Arc<Database>,
    auth: &ToolAuthContext,
    task: &str,
    agent: Option<&ProjectAgent>,
) -> anyhow::Result<String> {
    let tools = ToolRegistry::new_sub_agent(config, db.clone(), None, false);
    let tool_defs: Vec<ToolDefinition> = tools
        .definitions_for(auth)
        .into_iter()
        .filter(|d| explorer_tool(&d.name))
        .filter(|d| agent.is_none_or(|a| a.allows(&d.name)))
        .collect();
    let llm = crate::llm::create_provider(config);
    let model = agent
        .and_then(|a| a.model.clone())
        .unwrap_or_else(|| config.aux_models.subagent_model(&config.model).to_string());
    let system = match agent {
        Some(agent) => format!(
            "{}\n\nYou are working for another agent and cannot change anything; only read and search. End with a concise report including file paths and line numbers.",
            agent.prompt
        ),
        None => DEFAULT_PROMPT.to_string(),
    };
    let mut messages = vec![Message {
        role: "user".into(),
        content: MessageContent::Text(task.to_string()),
    }];
    let mut files_read = 0usize;
    for _ in 0..MAX_ITERATIONS {
        let response = llm
            .send_message_with_model(
                &system,
                messages.clone(),
                Some(tool_defs.clone()),
                Some(&model),
            )
            .await?;
        if let Some(usage) = &response.usage {
            let (channel, provider, model_name) = (
                auth.caller_channel.clone(),
                config.llm_provider.clone(),
                model.clone(),
            );
            let (chat_id, input_tokens, output_tokens) = (
                auth.caller_chat_id,
                i64::from(usage.input_tokens),
                i64::from(usage.output_tokens),
            );
            let _ = crate::internal::storage::db::call_blocking(db.clone(), move |db| {
                db.log_llm_usage(
                    chat_id,
                    &channel,
                    &provider,
                    &model_name,
                    input_tokens,
                    output_tokens,
                    "explore",
                )
                .map(|_| ())
            })
            .await;
        }
        let mut text = String::new();
        let mut calls = Vec::new();
        for block in &response.content {
            match block {
                ResponseContentBlock::Text { text: t } => text.push_str(t),
                ResponseContentBlock::ToolUse {
                    id, name, input, ..
                } => calls.push((id.clone(), name.clone(), input.clone())),
                _ => {}
            }
        }
        if calls.is_empty() {
            let report = if text.trim().is_empty() {
                "(the explorer found nothing to report)".to_string()
            } else {
                truncate(text.trim(), MAX_REPORT_CHARS)
            };
            return Ok(format!("{report}\n\n[explore: {files_read} tool calls]"));
        }
        let mut assistant_blocks = Vec::new();
        if !text.trim().is_empty() {
            assistant_blocks.push(ContentBlock::Text { text: text.clone() });
        }
        for (id, name, input) in &calls {
            assistant_blocks.push(ContentBlock::ToolUse {
                id: id.clone(),
                name: name.clone(),
                input: input.clone(),
                thought_signature: None,
            });
        }
        messages.push(Message {
            role: "assistant".into(),
            content: MessageContent::Blocks(assistant_blocks),
        });
        let results = futures_util::future::join_all(calls.iter().map(|(_, name, input)| {
            let name = name.clone();
            let input = input.clone();
            let tools = &tools;
            async move {
                if !explorer_tool(&name) {
                    return ToolResult::error(format!(
                        "`{name}` is not available to the explorer (read-only tools only)"
                    ));
                }
                if agent.is_some_and(|a| !a.allows(&name)) {
                    return ToolResult::error(format!(
                        "`{name}` is not in this agent's `tools` list"
                    ));
                }
                tools.execute_with_auth(&name, input, auth).await
            }
        }))
        .await;
        files_read += calls.len();
        let result_blocks = calls
            .iter()
            .zip(results)
            .map(|((id, _, _), result)| ContentBlock::ToolResult {
                tool_use_id: id.clone(),
                content: truncate(&result.content, MAX_TOOL_OUTPUT_CHARS),
                is_error: result.is_error.then_some(true),
            })
            .collect();
        messages.push(Message {
            role: "user".into(),
            content: MessageContent::Blocks(result_blocks),
        });
    }
    anyhow::bail!("no report after {MAX_ITERATIONS} steps; ask a narrower question")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_agent_front_matter() {
        let agent = parse_agent(
            "db-expert",
            "---\ndescription: Knows the storage layer\nmodel: small-model\n---\nYou explain the database code.\n",
        );
        assert_eq!(agent.description, "Knows the storage layer");
        assert_eq!(agent.model.as_deref(), Some("small-model"));
        assert_eq!(agent.prompt, "You explain the database code.");
        let plain = parse_agent("plain", "Just a prompt.");
        assert_eq!(plain.prompt, "Just a prompt.");
        assert!(plain.description.is_empty());
    }

    #[test]
    fn loads_agents_from_a_directory() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("a.md"),
            "---\ndescription: A\n---\nPrompt A",
        )
        .unwrap();
        std::fs::write(dir.path().join("empty.md"), "---\ndescription: none\n---\n").unwrap();
        std::fs::write(dir.path().join("notes.txt"), "ignored").unwrap();
        let agents = load_project_agents(dir.path());
        assert_eq!(agents.len(), 1);
        assert_eq!(agents[0].name, "a");
    }

    #[test]
    fn agent_tools_narrow_the_tool_set_and_accept_short_names() {
        let agent = parse_agent(
            "reviewer",
            "---\ndescription: Reviews\ntools: [read, grep, \"web\"]\n---\nReview the code.\n",
        );
        assert_eq!(
            agent.tools,
            vec!["grep", "read_file", "web_fetch", "web_search"]
        );
        assert!(agent.allows("read_file"));
        assert!(!agent.allows("bash"));
        let all = parse_agent("all", "Do anything.");
        assert!(all.tools.is_empty());
        assert!(all.allows("bash"));
        assert_eq!(
            tool_names("shell, edit"),
            vec![
                "apply_patch",
                "bash",
                "bash_input",
                "bash_kill",
                "bash_output",
                "edit_file"
            ]
        );
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("reviewer.md"),
            "---\ntools: read\n---\nReview.",
        )
        .unwrap();
        let found = find_project_agent(dir.path().to_str(), "reviewer").unwrap();
        assert_eq!(found.tools, vec!["read_file"]);
        assert!(find_project_agent(dir.path().to_str(), "missing").is_none());
        assert!(find_project_agent(None, "reviewer").is_none());
    }

    #[test]
    fn explorer_tools_are_read_only() {
        assert!(explorer_tool("read_file"));
        assert!(explorer_tool("grep"));
        assert!(!explorer_tool("bash"));
        assert!(!explorer_tool("write_file"));
        assert!(!explorer_tool("explore"));
    }
}