hematite-cli 0.4.3

Local AI coding harness for LM Studio with TUI, voice, retrieval, and grounded workstation tooling
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
use serde_json::Value;
use std::fs;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

#[derive(Debug)]
struct ArchitectureSketch {
    relative_path: String,
    role: &'static str,
    score: i32,
    symbols: Vec<String>,
}

pub async fn map_project(args: &Value) -> Result<String, String> {
    let root = crate::tools::file_ops::workspace_root();
    let focus = args.get("focus").and_then(|v| v.as_str()).unwrap_or(".");
    let include_symbols = args
        .get("include_symbols")
        .and_then(|v| v.as_bool())
        .unwrap_or(true);
    let max_depth = args
        .get("max_depth")
        .and_then(value_as_usize)
        .unwrap_or(4)
        .min(6);
    let focus_root = resolve_focus_root(&root, focus)?;

    let mut report = String::new();
    report.push_str(&format!("Project Root: {}\n", root.display()));
    if focus != "." {
        report.push_str(&format!("Focus Path: {}\n", focus_root.display()));
    }

    report.push_str("\n-- Configuration DNA --\n");
    append_configuration_dna(&root, &mut report);

    let sketches = collect_architecture_sketches(&root, &focus_root, include_symbols)?;
    if !sketches.is_empty() {
        append_architecture_map(&sketches, &mut report);
    }

    report.push_str("\n-- Directory Structure --\n");
    let mut lines = Vec::new();
    build_tree(&root, &focus_root, 0, max_depth, &mut lines)?;
    report.push_str(&lines.join("\n"));

    Ok(report)
}

fn resolve_focus_root(root: &Path, focus: &str) -> Result<PathBuf, String> {
    if focus == "." {
        return Ok(root.to_path_buf());
    }

    let candidate = if Path::new(focus).is_absolute() {
        PathBuf::from(focus)
    } else {
        root.join(focus)
    };

    let canonical = candidate
        .canonicalize()
        .map_err(|e| format!("map_project: could not resolve focus '{}': {}", focus, e))?;
    crate::tools::guard::path_is_safe(root, &canonical)
        .map_err(|e| format!("map_project: invalid focus '{}': {}", focus, e))?;
    if canonical.is_file() {
        return canonical.parent().map(Path::to_path_buf).ok_or_else(|| {
            format!(
                "map_project: focus '{}' has no readable parent directory",
                focus
            )
        });
    }
    Ok(canonical)
}

fn append_configuration_dna(root: &Path, report: &mut String) {
    let markers = [
        "Cargo.toml",
        "package.json",
        "go.mod",
        "requirements.txt",
        "pyproject.toml",
        "README.md",
        "CLAUDE.md",
        "CAPABILITIES.md",
        "Taskfile.yml",
        ".env.example",
    ];

    for marker in markers {
        let path = root.join(marker);
        if !path.exists() {
            continue;
        }
        if let Ok(content) = fs::read_to_string(&path) {
            let snippet = content.chars().take(600).collect::<String>();
            report.push_str(&format!("### File: {}\n```\n{}\n```\n", marker, snippet));
        }
    }
}

fn append_architecture_map(sketches: &[ArchitectureSketch], report: &mut String) {
    report.push_str("\n-- Architecture Map --\n");

    let entrypoints: Vec<_> = sketches
        .iter()
        .filter(|s| is_entrypoint_path(&s.relative_path))
        .collect();
    if !entrypoints.is_empty() {
        report.push_str("Likely entrypoints\n");
        for sketch in entrypoints.iter().take(4) {
            report.push_str(&format!("- {} [{}]\n", sketch.relative_path, sketch.role));
            if !sketch.symbols.is_empty() {
                report.push_str(&format!("  symbols: {}\n", sketch.symbols.join(", ")));
            }
        }
    }

    report.push_str("Core owner files\n");
    for sketch in sketches.iter().take(12) {
        report.push_str(&format!("- {} [{}]\n", sketch.relative_path, sketch.role));
        if !sketch.symbols.is_empty() {
            report.push_str(&format!("  symbols: {}\n", sketch.symbols.join(", ")));
        }
    }
}

fn collect_architecture_sketches(
    root: &Path,
    focus_root: &Path,
    include_symbols: bool,
) -> Result<Vec<ArchitectureSketch>, String> {
    let mut sketches = Vec::new();

    for entry in WalkDir::new(focus_root).follow_links(false).max_depth(4) {
        let entry = entry.map_err(|e| format!("map_project: {}", e))?;
        if !entry.file_type().is_file() {
            continue;
        }

        let path = entry.path();
        if path_has_hidden_segment(path) || !is_architecture_candidate(path) {
            continue;
        }

        let relative_path = to_relative_display(root, path);
        let role = classify_file_role(&relative_path);
        let score = score_architecture_file(&relative_path);
        let symbols = if include_symbols {
            extract_top_symbols(path).unwrap_or_default()
        } else {
            Vec::new()
        };

        sketches.push(ArchitectureSketch {
            relative_path,
            role,
            score,
            symbols,
        });
    }

    sketches.sort_by(|a, b| {
        b.score
            .cmp(&a.score)
            .then_with(|| a.relative_path.cmp(&b.relative_path))
    });
    sketches.truncate(18);
    Ok(sketches)
}

fn build_tree(
    root: &Path,
    dir: &Path,
    depth: usize,
    max_depth: usize,
    lines: &mut Vec<String>,
) -> Result<(), String> {
    if depth > max_depth {
        return Ok(());
    }

    let mut entries: Vec<_> = fs::read_dir(dir)
        .map_err(|e| format!("Failed to read dir {dir:?}: {}", e))?
        .filter_map(Result::ok)
        .collect();

    entries.sort_by_key(|e| {
        (
            e.file_type().map(|ft| ft.is_file()).unwrap_or(false),
            e.file_name(),
        )
    });

    for entry in entries {
        let file_type = entry
            .file_type()
            .map_err(|e| format!("Failed to inspect entry {:?}: {}", entry.path(), e))?;
        let name = entry.file_name().to_string_lossy().into_owned();
        if name.starts_with('.') || name == "target" || name == "node_modules" || name == "vendor" {
            continue;
        }

        let indent = "  ".repeat(depth);
        let path = entry.path();
        let rel = to_relative_display(root, &path);
        let prefix = if file_type.is_dir() { "[D]" } else { "[F]" };
        lines.push(format!("{indent}{prefix} {rel}"));

        if file_type.is_dir() {
            build_tree(root, &path, depth + 1, max_depth, lines)?;
        }
    }
    Ok(())
}

fn path_has_hidden_segment(path: &Path) -> bool {
    path.components().any(|component| {
        let segment = component.as_os_str().to_string_lossy();
        (segment.starts_with('.') && segment != "." && segment != "..")
            || segment == "target"
            || segment == "node_modules"
            || segment == "__pycache__"
    })
}

fn is_architecture_candidate(path: &Path) -> bool {
    let Some(ext) = path.extension().and_then(|s| s.to_str()) else {
        return false;
    };
    matches!(
        ext,
        "rs" | "py" | "ts" | "tsx" | "js" | "jsx" | "go" | "cs" | "java" | "kt"
    )
}

fn to_relative_display(root: &Path, path: &Path) -> String {
    let root_display = normalize_display_path(root);
    let path_display = normalize_display_path(path);

    if let Some(stripped) = path_display.strip_prefix(&root_display) {
        return stripped.trim_start_matches('/').to_string();
    }

    path.strip_prefix(root)
        .unwrap_or(path)
        .to_string_lossy()
        .replace('\\', "/")
        .trim_start_matches("//?/")
        .trim_start_matches("\\\\?\\")
        .to_string()
}

fn normalize_display_path(path: &Path) -> String {
    path.to_string_lossy()
        .replace('\\', "/")
        .trim_start_matches("//?/")
        .trim_start_matches("\\\\?\\")
        .trim_end_matches('/')
        .to_string()
}

fn value_as_usize(value: &Value) -> Option<usize> {
    if let Some(v) = value.as_u64() {
        return usize::try_from(v).ok();
    }

    if let Some(v) = value.as_f64() {
        if v.is_finite() && v >= 0.0 && v.fract() == 0.0 && v <= (usize::MAX as f64) {
            return Some(v as usize);
        }
    }

    value.as_str().and_then(|s| s.trim().parse::<usize>().ok())
}

fn is_core_library_path(relative_path: &str) -> bool {
    relative_path.to_lowercase().ends_with("lib.rs")
}

fn is_entrypoint_path(relative_path: &str) -> bool {
    let lower = relative_path.to_lowercase();
    lower.ends_with("main.rs")
        || (lower.contains("/bin/") && lower.ends_with(".rs"))
        || lower.ends_with("app.rs")
        || lower.ends_with("server.rs")
        || lower.ends_with("cli.rs")
        || lower.ends_with("__main__.py")
        || lower.ends_with("main.py")
        || lower.ends_with("index.ts")
        || lower.ends_with("index.js")
}

fn classify_file_role(relative_path: &str) -> &'static str {
    let lower = relative_path.to_lowercase();
    if is_entrypoint_path(relative_path) {
        "entrypoint"
    } else if is_core_library_path(relative_path) {
        "core library"
    } else if lower.ends_with("src/runtime.rs") || lower.contains("/runtime/") {
        "runtime assembly"
    } else if lower.contains("/ui/") || lower.contains("tui") || lower.contains("voice") {
        "ui / operator surface"
    } else if lower.contains("/agent/")
        || lower.contains("conversation")
        || lower.contains("inference")
    {
        "agent orchestration"
    } else if lower.contains("/tools/") || lower.contains("shell") || lower.contains("git") {
        "tooling layer"
    } else if lower.contains("/memory/") || lower.contains("vein") || lower.contains("compaction") {
        "memory / retrieval"
    } else if lower.contains("/lsp/") {
        "language-server integration"
    } else {
        "workspace code"
    }
}

fn score_architecture_file(relative_path: &str) -> i32 {
    let lower = relative_path.to_lowercase();
    let mut score = 0;

    if lower.starts_with("src/") {
        score += 5;
    }
    if is_entrypoint_path(relative_path) {
        score += 12;
    } else if is_core_library_path(relative_path) {
        score += 7;
    }
    if lower.ends_with("src/runtime.rs") {
        score += 14;
    }
    for needle in [
        "/agent/",
        "/ui/",
        "/runtime/",
        "/tools/",
        "/memory/",
        "/lsp/",
        "conversation",
        "inference",
        "prompt",
        "runtime",
        "voice",
        "tui",
        "main",
    ] {
        if lower.contains(needle) {
            score += 4;
        }
    }

    score
}

fn extract_top_symbols(path: &Path) -> Result<Vec<String>, String> {
    let content = fs::read_to_string(path)
        .map_err(|e| format!("symbol scan failed for {:?}: {}", path, e))?;
    let ext = path
        .extension()
        .and_then(|s| s.to_str())
        .unwrap_or_default();
    let mut symbols = Vec::new();

    let patterns: &[&str] = match ext {
        "rs" => &[
            r"(?m)^\s*pub\s+struct\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*struct\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*pub\s+enum\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*enum\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*pub\s+trait\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*trait\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*pub\s+async\s+fn\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*pub\s+fn\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*async\s+fn\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*fn\s+([A-Za-z_][A-Za-z0-9_]*)",
        ],
        "py" => &[
            r"(?m)^\s*class\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*def\s+([A-Za-z_][A-Za-z0-9_]*)",
        ],
        "ts" | "tsx" | "js" | "jsx" => &[
            r"(?m)^\s*export\s+class\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*class\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*export\s+function\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*function\s+([A-Za-z_][A-Za-z0-9_]*)",
            r"(?m)^\s*export\s+const\s+([A-Za-z_][A-Za-z0-9_]*)",
        ],
        "go" => &[
            r"(?m)^\s*type\s+([A-Za-z_][A-Za-z0-9_]*)\s+struct",
            r"(?m)^\s*func\s+([A-Za-z_][A-Za-z0-9_]*)",
        ],
        _ => &[],
    };

    for pattern in patterns {
        let regex =
            regex::Regex::new(pattern).map_err(|e| format!("invalid symbol regex: {}", e))?;
        for capture in regex.captures_iter(&content) {
            let Some(name) = capture.get(1).map(|m| m.as_str().to_string()) else {
                continue;
            };
            if !symbols.contains(&name) {
                symbols.push(name);
            }
            if symbols.len() >= 4 {
                return Ok(symbols);
            }
        }
    }

    Ok(symbols)
}