microclaw-engine 0.10.1

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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! `@`-prefix context references in user messages.
//!
//! Lets the user write `@file:src/main.rs`, `@folder:src/`, `@diff`, `@staged`,
//! or `@url:https://example.com` and have those expand server-side into an
//! attached context block before the LLM sees the message. Avoids round-trips
//! through `read_file` / `web_fetch` for trivial cases that the user clearly
//! intends.
//!
//! Hermes reference: `agent/context_references.py`. This is a focused port:
//! we cover file (with optional `:line` or `:start-end` suffix), folder
//! (1-deep listing), diff/staged, and url. We don't yet implement quoted
//! values (`@file:"path with spaces"`) or `@git:` refs.
//!
//! Sensitive-path guard: refuses to read out of `~/.ssh`, `~/.aws`, `~/.gnupg`,
//! `~/.kube`, `~/.docker`, dotfiles like `~/.bashrc`/`~/.netrc`/`~/.npmrc`.
//! The existing `path_guard` module catches these too, but enforcing here
//! avoids spending time fetching only to reject.
//!
//! URL fetching delegates to `crate::internal::tool_runtime::web_fetch::fetch_url_with_timeout`,
//! which already enforces the SSRF guard from PR #335.

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

use regex::Regex;

const MAX_FILE_BYTES: usize = 64 * 1024;
const MAX_FOLDER_ENTRIES: usize = 200;
const MAX_DIFF_BYTES: usize = 32 * 1024;
const MAX_URL_BYTES: usize = 64 * 1024;
const URL_FETCH_TIMEOUT_SECS: u64 = 15;

fn reference_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        // Matches:  @diff, @staged, @file:VALUE, @folder:VALUE, @url:VALUE.
        // VALUE is whitespace-terminated, optionally followed by `:N` or `:N-M`.
        // The leading boundary uses `\s` rather than a look-behind because the
        // `regex` crate's default engine doesn't support look-around; we
        // post-filter for "start-of-line OR preceded by whitespace" separately.
        Regex::new(r"@(?:(?P<simple>diff|staged)\b|(?P<kind>file|folder|url):(?P<value>\S+))")
            .expect("static regex")
    })
}

const SENSITIVE_HOME_DIRS: &[&str] = &[".ssh", ".aws", ".gnupg", ".kube", ".docker", ".azure"];

const SENSITIVE_HOME_FILES: &[&str] = &[
    ".bashrc",
    ".zshrc",
    ".profile",
    ".bash_profile",
    ".zprofile",
    ".netrc",
    ".pgpass",
    ".npmrc",
    ".pypirc",
];

/// Result of reference expansion.
#[derive(Debug, Clone)]
pub struct ExpansionResult {
    /// The user-visible part of the message, with `@…` tokens stripped.
    pub stripped_message: String,
    /// Final message to pass to the LLM (stripped + attached blocks + warnings).
    pub final_message: String,
    /// Per-reference warnings to surface to the user.
    pub warnings: Vec<String>,
    /// Whether any reference was successfully expanded.
    pub expanded: bool,
}

/// Expand all `@`-prefix references in `message` against `cwd`. URL fetches
/// honor the same SSRF guard `web_fetch` uses.
///
/// If `message` contains no references, returns the original text unchanged.
pub async fn expand_references(message: &str, cwd: &Path) -> ExpansionResult {
    let refs: Vec<ReferenceMatch> = parse_references(message);
    if refs.is_empty() {
        return ExpansionResult {
            stripped_message: message.to_string(),
            final_message: message.to_string(),
            warnings: Vec::new(),
            expanded: false,
        };
    }

    let mut blocks: Vec<String> = Vec::new();
    let mut warnings: Vec<String> = Vec::new();

    for r in &refs {
        match expand_one(r, cwd).await {
            Ok(block) => blocks.push(block),
            Err(w) => warnings.push(w),
        }
    }

    let stripped = strip_reference_tokens(message, &refs);
    let mut final_msg = stripped.clone();
    if !warnings.is_empty() {
        final_msg.push_str("\n\n--- Context Warnings ---\n");
        for w in &warnings {
            final_msg.push_str("- ");
            final_msg.push_str(w);
            final_msg.push('\n');
        }
    }
    if !blocks.is_empty() {
        final_msg.push_str("\n\n--- Attached Context ---\n\n");
        final_msg.push_str(&blocks.join("\n\n"));
    }

    ExpansionResult {
        stripped_message: stripped,
        final_message: final_msg.trim().to_string(),
        warnings,
        expanded: !blocks.is_empty(),
    }
}

#[derive(Debug, Clone)]
struct ReferenceMatch {
    #[allow(dead_code)]
    raw: String,
    kind: ReferenceKind,
    target: String,
    line_start: Option<usize>,
    line_end: Option<usize>,
    /// Byte offsets in the original message of the `@…` token, for stripping.
    token_start: usize,
    token_end: usize,
}

#[derive(Debug, Clone, Copy)]
enum ReferenceKind {
    File,
    Folder,
    Url,
    Diff,
    Staged,
}

fn parse_references(message: &str) -> Vec<ReferenceMatch> {
    let mut out = Vec::new();
    for cap in reference_re().captures_iter(message) {
        let m = cap.get(0).unwrap();
        // Reject mid-word matches: `foo@file:bar` should not parse. The match
        // must be at start-of-string or preceded by whitespace.
        if m.start() > 0 {
            let prev = message[..m.start()].chars().next_back();
            if let Some(c) = prev {
                if !c.is_whitespace() {
                    continue;
                }
            }
        }
        if let Some(simple) = cap.name("simple") {
            let kind = if simple.as_str() == "staged" {
                ReferenceKind::Staged
            } else {
                ReferenceKind::Diff
            };
            out.push(ReferenceMatch {
                raw: m.as_str().to_string(),
                kind,
                target: String::new(),
                line_start: None,
                line_end: None,
                token_start: m.start(),
                token_end: m.end(),
            });
            continue;
        }

        let kind = match cap.name("kind").map(|m| m.as_str()) {
            Some("file") => ReferenceKind::File,
            Some("folder") => ReferenceKind::Folder,
            Some("url") => ReferenceKind::Url,
            _ => continue,
        };
        let value_raw = cap.name("value").map(|m| m.as_str()).unwrap_or("");
        let value = strip_trailing_punctuation(value_raw);
        let (target, line_start, line_end) = match kind {
            ReferenceKind::File => parse_file_value(value),
            _ => (value.to_string(), None, None),
        };
        out.push(ReferenceMatch {
            raw: m.as_str().to_string(),
            kind,
            target,
            line_start,
            line_end,
            token_start: m.start(),
            token_end: m.end(),
        });
    }
    out
}

fn strip_trailing_punctuation(value: &str) -> &str {
    value.trim_end_matches([',', '.', ';', '!', '?'])
}

fn parse_file_value(value: &str) -> (String, Option<usize>, Option<usize>) {
    // Last `:` separates path from line spec, but only if the suffix is digits
    // (or digits-digits). Otherwise treat the whole thing as a path (handles
    // Windows drive letters).
    if let Some((rest, suffix)) = value.rsplit_once(':') {
        if let Some((a, b)) = suffix.split_once('-') {
            if let (Ok(a), Ok(b)) = (a.parse(), b.parse()) {
                return (rest.to_string(), Some(a), Some(b));
            }
        } else if let Ok(n) = suffix.parse() {
            return (rest.to_string(), Some(n), Some(n));
        }
    }
    (value.to_string(), None, None)
}

fn strip_reference_tokens(message: &str, refs: &[ReferenceMatch]) -> String {
    if refs.is_empty() {
        return message.to_string();
    }
    let mut out = String::with_capacity(message.len());
    let mut last = 0;
    for r in refs {
        if r.token_start >= last {
            out.push_str(&message[last..r.token_start]);
            last = r.token_end;
        }
    }
    out.push_str(&message[last..]);
    // Collapse runs of whitespace introduced by removal.
    let collapsed = out.trim().to_string();
    collapsed
}

async fn expand_one(r: &ReferenceMatch, cwd: &Path) -> Result<String, String> {
    match r.kind {
        ReferenceKind::File => expand_file(&r.target, r.line_start, r.line_end, cwd).await,
        ReferenceKind::Folder => expand_folder(&r.target, cwd).await,
        ReferenceKind::Url => expand_url(&r.target).await,
        ReferenceKind::Diff => expand_git_diff(false, cwd).await,
        ReferenceKind::Staged => expand_git_diff(true, cwd).await,
    }
}

fn resolve_path_safe(target: &str, cwd: &Path) -> Result<PathBuf, String> {
    let raw = PathBuf::from(target);
    let path = if raw.is_absolute() {
        raw
    } else {
        cwd.join(raw)
    };
    let canonical = path
        .canonicalize()
        .map_err(|e| format!("@{} not found: {e}", target))?;
    // Sensitive-path block list, evaluated against canonical. We walk the
    // resolved path's components and reject when any component name matches
    // a known sensitive directory — works on both Unix and Windows because
    // we never split on a separator string.
    let component_names: Vec<String> = canonical
        .components()
        .filter_map(|c| match c {
            std::path::Component::Normal(s) => Some(s.to_string_lossy().to_string()),
            _ => None,
        })
        .collect();
    for d in SENSITIVE_HOME_DIRS {
        if component_names.iter().any(|name| name == d) {
            return Err(format!("@{} blocked: refuses to read inside `{d}`", target));
        }
    }
    if let Some(home) = dirs_home() {
        for d in SENSITIVE_HOME_DIRS {
            let blocked = home.join(d);
            if canonical.starts_with(&blocked) {
                return Err(format!(
                    "@{} blocked: refuses to read inside {}",
                    target,
                    blocked.display()
                ));
            }
        }
        for f in SENSITIVE_HOME_FILES {
            if canonical == home.join(f) {
                return Err(format!(
                    "@{} blocked: refuses to read sensitive dotfile {}",
                    target, f
                ));
            }
        }
    }
    Ok(canonical)
}

fn dirs_home() -> Option<PathBuf> {
    std::env::var_os("HOME").map(PathBuf::from)
}

async fn expand_file(
    target: &str,
    line_start: Option<usize>,
    line_end: Option<usize>,
    cwd: &Path,
) -> Result<String, String> {
    let path = resolve_path_safe(target, cwd)?;
    let bytes = tokio::fs::read(&path)
        .await
        .map_err(|e| format!("@file:{target} read failed: {e}"))?;
    if bytes.len() > MAX_FILE_BYTES * 8 {
        return Err(format!(
            "@file:{target} too large ({} bytes); read with read_file instead",
            bytes.len()
        ));
    }
    let text = String::from_utf8_lossy(&bytes).into_owned();
    let snippet = match (line_start, line_end) {
        (Some(s), Some(e)) if e >= s => {
            let lines: Vec<&str> = text.lines().collect();
            let s = s.saturating_sub(1).min(lines.len());
            let e = e.min(lines.len());
            lines[s..e].join("\n")
        }
        _ => text,
    };
    let snippet = if snippet.len() > MAX_FILE_BYTES {
        format!(
            "{}\n... [truncated to {} bytes]",
            &snippet[..MAX_FILE_BYTES],
            MAX_FILE_BYTES
        )
    } else {
        snippet
    };
    let mut header = format!("@file:{}", target);
    if let (Some(s), Some(e)) = (line_start, line_end) {
        header.push_str(&format!(" (lines {s}-{e})"));
    }
    Ok(format!("**{}**\n```\n{}\n```", header, snippet))
}

async fn expand_folder(target: &str, cwd: &Path) -> Result<String, String> {
    let path = resolve_path_safe(target, cwd)?;
    let mut rd = tokio::fs::read_dir(&path)
        .await
        .map_err(|e| format!("@folder:{target} read failed: {e}"))?;
    let mut entries: Vec<String> = Vec::new();
    let mut total = 0usize;
    while let Ok(Some(entry)) = rd.next_entry().await {
        if total >= MAX_FOLDER_ENTRIES {
            entries.push(format!("... (more, capped at {MAX_FOLDER_ENTRIES})"));
            break;
        }
        let name = entry.file_name();
        let name = name.to_string_lossy();
        let kind = entry
            .file_type()
            .await
            .ok()
            .map(|t| if t.is_dir() { "/" } else { "" })
            .unwrap_or("");
        entries.push(format!("{name}{kind}"));
        total += 1;
    }
    entries.sort();
    Ok(format!(
        "**@folder:{}** ({} entries)\n```\n{}\n```",
        target,
        entries.len(),
        entries.join("\n")
    ))
}

async fn expand_url(target: &str) -> Result<String, String> {
    let body = crate::internal::tool_runtime::web_fetch::fetch_url_with_timeout(
        target,
        URL_FETCH_TIMEOUT_SECS,
    )
    .await
    .map_err(|e| format!("@url:{target} fetch failed: {e}"))?;
    let snippet = if body.len() > MAX_URL_BYTES {
        format!(
            "{}\n... [truncated to {} bytes]",
            &body[..MAX_URL_BYTES],
            MAX_URL_BYTES
        )
    } else {
        body
    };
    Ok(format!("**@url:{}**\n```\n{}\n```", target, snippet))
}

async fn expand_git_diff(staged: bool, cwd: &Path) -> Result<String, String> {
    let args: &[&str] = if staged {
        &["diff", "--cached"]
    } else {
        &["diff"]
    };
    let label = if staged { "@staged" } else { "@diff" };
    let output = tokio::process::Command::new("git")
        .args(args)
        .current_dir(cwd)
        .output()
        .await
        .map_err(|e| format!("{label}: git not available ({e})"))?;
    if !output.status.success() {
        return Err(format!(
            "{label} failed: {}",
            String::from_utf8_lossy(&output.stderr).trim()
        ));
    }
    let body = String::from_utf8_lossy(&output.stdout).into_owned();
    if body.trim().is_empty() {
        return Ok(format!("**{label}** (no changes)"));
    }
    let snippet = if body.len() > MAX_DIFF_BYTES {
        format!(
            "{}\n... [truncated to {} bytes]",
            &body[..MAX_DIFF_BYTES],
            MAX_DIFF_BYTES
        )
    } else {
        body
    };
    Ok(format!("**{label}**\n```diff\n{}\n```", snippet))
}

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

    fn tmp_dir() -> PathBuf {
        let dir = std::env::temp_dir().join(format!("microclaw_ctxref_{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[tokio::test]
    async fn no_refs_passthrough() {
        let cwd = tmp_dir();
        let r = expand_references("plain text, no refs", &cwd).await;
        assert!(!r.expanded);
        assert_eq!(r.final_message, "plain text, no refs");
        let _ = std::fs::remove_dir_all(&cwd);
    }

    #[tokio::test]
    async fn expands_file_reference() {
        let cwd = tmp_dir();
        std::fs::write(cwd.join("hello.txt"), "world").unwrap();
        let r = expand_references("look at @file:hello.txt please", &cwd).await;
        assert!(r.expanded);
        assert!(r.final_message.contains("@file:hello.txt"));
        assert!(r.final_message.contains("world"));
        assert!(r.final_message.contains("look at"));
        assert!(r.final_message.contains("please"));
        // The @file:hello.txt token itself was stripped from the user-visible
        // sentence (it appears only inside the attached-context header).
        assert!(!r.stripped_message.contains("@file:hello.txt"));
        let _ = std::fs::remove_dir_all(&cwd);
    }

    #[tokio::test]
    async fn file_with_line_range() {
        let cwd = tmp_dir();
        std::fs::write(cwd.join("multi.txt"), "line1\nline2\nline3\nline4\n").unwrap();
        let r = expand_references("@file:multi.txt:2-3", &cwd).await;
        assert!(r.expanded);
        assert!(r.final_message.contains("line2\nline3"));
        assert!(!r.final_message.contains("line1"));
        assert!(!r.final_message.contains("line4"));
        let _ = std::fs::remove_dir_all(&cwd);
    }

    #[tokio::test]
    async fn folder_lists_entries() {
        let cwd = tmp_dir();
        std::fs::create_dir_all(cwd.join("sub/inner")).unwrap();
        std::fs::write(cwd.join("sub/a.txt"), "a").unwrap();
        std::fs::write(cwd.join("sub/b.txt"), "b").unwrap();
        let r = expand_references("@folder:sub", &cwd).await;
        assert!(r.expanded);
        assert!(r.final_message.contains("a.txt"));
        assert!(r.final_message.contains("b.txt"));
        assert!(r.final_message.contains("inner/"));
        let _ = std::fs::remove_dir_all(&cwd);
    }

    #[tokio::test]
    async fn missing_file_warns_does_not_panic() {
        let cwd = tmp_dir();
        let r = expand_references("@file:nope.txt", &cwd).await;
        assert!(!r.expanded);
        assert!(r.warnings.iter().any(|w| w.contains("nope.txt")));
        let _ = std::fs::remove_dir_all(&cwd);
    }

    #[test]
    fn sensitive_path_blocked() {
        // Component-based block list works on both Unix and Windows. We
        // run the expansion under env_lock and then drop the lock before
        // asserting, so a failed assertion can't poison the lock for other
        // tests.
        let home = std::env::temp_dir().join(format!("fake_home_{}", uuid::Uuid::new_v4()));
        let secrets = home.join(".ssh");
        std::fs::create_dir_all(&secrets).unwrap();
        std::fs::write(secrets.join("id_rsa"), "secret").unwrap();
        let target = format!("@file:{}/id_rsa", secrets.display());

        let r = {
            let _guard = crate::test_support::env_lock();
            let prev_home = std::env::var_os("HOME");
            std::env::set_var("HOME", &home);

            let r = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap()
                .block_on(expand_references(&target, &home));

            if let Some(prev) = prev_home {
                std::env::set_var("HOME", prev);
            } else {
                std::env::remove_var("HOME");
            }
            r
            // _guard dropped here, before any assert!
        };

        let _ = std::fs::remove_dir_all(&home);
        assert!(!r.expanded, "expected expansion to be blocked");
        assert!(
            r.warnings.iter().any(|w| w.contains("blocked")),
            "expected a 'blocked' warning, got: {:?}",
            r.warnings
        );
    }

    #[test]
    fn parse_extracts_token_offsets() {
        let msg = "before @file:foo.txt:10-20 after";
        let refs = parse_references(msg);
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].target, "foo.txt");
        assert_eq!(refs[0].line_start, Some(10));
        assert_eq!(refs[0].line_end, Some(20));
        assert_eq!(refs[0].token_start, 7);
        assert_eq!(
            &msg[refs[0].token_start..refs[0].token_end],
            "@file:foo.txt:10-20"
        );
    }

    #[test]
    fn parse_handles_trailing_punctuation() {
        let refs = parse_references("see @file:hello.txt, please");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].target, "hello.txt");
    }

    #[test]
    fn parse_recognizes_diff_and_staged() {
        let refs = parse_references("@diff and @staged");
        assert_eq!(refs.len(), 2);
        assert!(matches!(refs[0].kind, ReferenceKind::Diff));
        assert!(matches!(refs[1].kind, ReferenceKind::Staged));
    }

    #[test]
    fn ref_at_start_of_message_matches() {
        let refs = parse_references("@file:foo.txt explain");
        assert_eq!(refs.len(), 1);
        assert_eq!(refs[0].target, "foo.txt");
    }
}