hematite-cli 0.13.0

Senior SysAdmin, Network Admin, Data Analyst, and Software Engineer living in your terminal. A high-precision local AI agent harness for LM Studio, Ollama, and other local OpenAI-compatible runtimes that runs 100% on your own silicon. Reads repos, edits files, runs builds, inspects full network state and workstation telemetry, and runs real Python/JS for data analysis.
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
use serde_json::Value;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

pub async fn execute(args: &Value) -> Result<String, String> {
    let root = if let Some(r) = args.get("_root").and_then(|v| v.as_str()) {
        PathBuf::from(r)
    } else {
        crate::tools::file_ops::workspace_root()
    };

    let mut sections: Vec<String> = Vec::new();
    let mut manifests_found = 0usize;

    // ── Rust (Cargo.toml) ────────────────────────────────────────────────────
    if let Some(s) = audit_cargo(&root) {
        sections.push(s);
        manifests_found += 1;
    }

    // ── Node.js (package.json) ───────────────────────────────────────────────
    if let Some(s) = audit_npm(&root) {
        sections.push(s);
        manifests_found += 1;
    }

    // ── Python (requirements.txt / pyproject.toml / setup.cfg) ──────────────
    if let Some(s) = audit_python(&root) {
        sections.push(s);
        manifests_found += 1;
    }

    // ── Go (go.mod) ──────────────────────────────────────────────────────────
    if let Some(s) = audit_go(&root) {
        sections.push(s);
        manifests_found += 1;
    }

    if manifests_found == 0 {
        return Ok("dependency_audit: no supported manifest files found.\n\
             Supports: Cargo.toml (Rust), package.json (Node.js), \
             requirements.txt / pyproject.toml (Python), go.mod (Go)."
            .to_string());
    }

    let mut out = format!(
        "DEPENDENCY AUDIT\n{}\n\
         Manifests scanned: {manifests_found}\n\n",
        "".repeat(60)
    );
    out.push_str(&sections.join("\n"));
    out.push_str(
        "\n── Recommendations ──\n\
         • Run `cargo update` / `npm update` / `pip install --upgrade` to update dependencies.\n\
         • Use `cargo audit` (Rust), `npm audit` (Node), or `safety check` (Python) for CVE scanning.\n\
         • Pin major versions in production; allow minor/patch updates for security fixes.\n\
         • Add a CI step (Dependabot / Renovate) to catch outdated deps automatically.",
    );

    Ok(out)
}

// ── Cargo.toml ───────────────────────────────────────────────────────────────

fn audit_cargo(root: &Path) -> Option<String> {
    let cargo_toml = root.join("Cargo.toml");
    let text = std::fs::read_to_string(&cargo_toml).ok()?;

    let mut deps: BTreeMap<String, String> = BTreeMap::new();
    let mut section = "";
    for line in text.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('[') {
            section = if trimmed.contains("dependencies") {
                "dep"
            } else {
                ""
            };
            continue;
        }
        if section != "dep" || trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        if let Some((name, rest)) = trimmed.split_once('=') {
            let name = name.trim().to_string();
            let version = extract_cargo_version(rest.trim());
            deps.insert(name, version);
        }
    }

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

    // Flag wildcard versions
    for (name, ver) in &deps {
        if ver == "*" {
            flags.push(format!(
                "  [WILDCARD] {name} = \"*\" — unpinned, any version will match"
            ));
        }
        // Flag very old-looking major versions for common crates
        flag_outdated_crate(name, ver, &mut flags);
    }

    // Check for Cargo.lock
    let lock_exists = root.join("Cargo.lock").exists();
    let workspace_lock = root.join("..").join("Cargo.lock").exists();

    let mut out = format!(
        "RUST (Cargo.toml)\n\
         Dependencies listed: {}\n\
         Cargo.lock present : {}\n",
        deps.len(),
        if lock_exists || workspace_lock {
            "yes"
        } else {
            "NO — add to version control for reproducible builds"
        }
    );

    if flags.is_empty() {
        out.push_str("No obvious issues detected.\n");
    } else {
        out.push_str("Issues found:\n");
        for f in &flags {
            out.push_str(f);
            out.push('\n');
        }
    }

    // Detect dev-only security-relevant crates that should be non-dev
    if text.contains("openssl") && !text.contains("rustls") {
        out.push_str(
            "  [INFO] openssl dependency found — consider rustls for pure-Rust TLS \
             (no C library required, better portability).\n",
        );
    }
    if text.contains("unsafe") && text.contains("[profile.release]") {
        // Can't check unsafe usage from toml, skip
    }

    out.push('\n');
    Some(out)
}

fn extract_cargo_version(rest: &str) -> String {
    let rest = rest.trim().trim_matches('"');
    if rest.starts_with('{') {
        // { version = "1.0", features = [...] }
        if let Some(start) = rest.find("version") {
            let after = &rest[start + 7..].trim_start_matches([' ', '=', '"', ' ']);
            if let Some(end) = after.find(['"', ',', '}']) {
                return after[..end].to_string();
            }
        }
        return "(complex)".to_string();
    }
    rest.trim_matches('"').to_string()
}

fn flag_outdated_crate(name: &str, ver: &str, flags: &mut Vec<String>) {
    // Known minimum expected major versions for common Rust crates (as of 2025)
    let known_minimums: &[(&str, u64, &str)] = &[
        ("tokio", 1, "1.x"),
        ("serde", 1, "1.x"),
        ("serde_json", 1, "1.x"),
        ("reqwest", 0, "0.12+"),
        ("clap", 4, "4.x"),
        ("axum", 0, "0.7+"),
        ("actix-web", 4, "4.x"),
        ("sqlx", 0, "0.7+"),
        ("hyper", 1, "1.x"),
        ("tracing", 0, "0.1+"),
    ];
    let major = ver
        .trim_start_matches(['^', '~', '=', '>', '<', ' '])
        .split('.')
        .next()
        .and_then(|s| s.parse::<u64>().ok())
        .unwrap_or(0);

    for (crate_name, min_major, expected) in known_minimums {
        if name == *crate_name && major < *min_major {
            flags.push(format!(
                "  [OUTDATED] {name} = \"{ver}\" — expected {expected}; upgrade recommended"
            ));
        }
    }
}

// ── package.json ─────────────────────────────────────────────────────────────

fn audit_npm(root: &Path) -> Option<String> {
    let pkg_json = root.join("package.json");
    let text = std::fs::read_to_string(&pkg_json).ok()?;
    let json: Value = serde_json::from_str(&text).ok()?;

    let mut dep_count = 0usize;
    let mut dev_dep_count = 0usize;
    let mut issues: Vec<String> = Vec::new();

    for key in &["dependencies", "peerDependencies"] {
        if let Some(deps) = json.get(key).and_then(|v| v.as_object()) {
            for (name, ver) in deps {
                dep_count += 1;
                let ver_str = ver.as_str().unwrap_or("?");
                flag_npm_version(name, ver_str, &mut issues);
            }
        }
    }
    if let Some(deps) = json.get("devDependencies").and_then(|v| v.as_object()) {
        dev_dep_count = deps.len();
    }

    let node_modules = root.join("node_modules");
    let lock_yarn = root.join("yarn.lock").exists();
    let lock_npm = root.join("package-lock.json").exists();
    let lock_pnpm = root.join("pnpm-lock.yaml").exists();
    let has_lock = lock_yarn || lock_npm || lock_pnpm;

    let name = json
        .get("name")
        .and_then(|v| v.as_str())
        .unwrap_or("(unnamed)");
    let version = json.get("version").and_then(|v| v.as_str()).unwrap_or("?");
    let engines = json
        .get("engines")
        .map(|e| format!("{e}"))
        .unwrap_or_default();

    let mut out = format!(
        "NODE.JS (package.json)\n\
         Package         : {name} v{version}\n\
         Dependencies    : {dep_count} prod, {dev_dep_count} dev\n\
         Lock file       : {}\n",
        if has_lock {
            if lock_yarn {
                "yarn.lock"
            } else if lock_npm {
                "package-lock.json"
            } else {
                "pnpm-lock.yaml"
            }
        } else {
            "MISSING — run npm install / yarn install"
        }
    );
    if !engines.is_empty() {
        out.push_str(&format!("Node engines req : {engines}\n"));
    }
    if node_modules.exists() {
        out.push_str("node_modules     : present\n");
    } else {
        out.push_str("node_modules     : NOT found — run npm install\n");
    }

    if issues.is_empty() {
        out.push_str("No obvious version issues detected.\n");
    } else {
        out.push_str("Issues found:\n");
        for i in &issues {
            out.push_str(i);
            out.push('\n');
        }
    }
    out.push('\n');
    Some(out)
}

fn flag_npm_version(name: &str, ver: &str, issues: &mut Vec<String>) {
    if ver == "*" || ver == "latest" {
        issues.push(format!(
            "  [PINNING] {name}: \"{ver}\" — unpinned; specify an exact or range version"
        ));
        return;
    }
    // Warn on known deprecated packages
    let deprecated = &[
        "request",
        "node-uuid",
        "querystring",
        "url",
        "punycode",
        "rimraf@2",
        "glob@7",
        "lodash@3",
        "lodash@4.17.0",
    ];
    for dep in deprecated {
        if dep.contains('@') {
            let parts: Vec<&str> = dep.splitn(2, '@').collect();
            if name == parts[0] && ver.starts_with(parts[1]) {
                issues.push(format!(
                    "  [DEPRECATED] {name}@{ver} — known deprecated version"
                ));
            }
        } else if name == *dep {
            issues.push(format!(
                "  [DEPRECATED] {name} — package is deprecated; find an alternative"
            ));
        }
    }
}

// ── Python ───────────────────────────────────────────────────────────────────

fn audit_python(root: &Path) -> Option<String> {
    let req_txt = root.join("requirements.txt");
    let pyproject = root.join("pyproject.toml");
    let setup_cfg = root.join("setup.cfg");
    let setup_py = root.join("setup.py");

    let mut packages: Vec<(String, String)> = Vec::new();
    let source: &str;

    if req_txt.exists() {
        source = "requirements.txt";
        if let Ok(text) = std::fs::read_to_string(&req_txt) {
            for line in text.lines() {
                let line = line.trim();
                if line.is_empty() || line.starts_with('#') || line.starts_with('-') {
                    continue;
                }
                let (name, ver) = split_python_req(line);
                packages.push((name, ver));
            }
        }
    } else if pyproject.exists() {
        source = "pyproject.toml";
        if let Ok(text) = std::fs::read_to_string(&pyproject) {
            let mut in_deps = false;
            for line in text.lines() {
                let trimmed = line.trim();
                if trimmed.contains("dependencies") && trimmed.contains('=') {
                    in_deps = true;
                    continue;
                }
                if in_deps {
                    if trimmed.starts_with('[') {
                        in_deps = false;
                        continue;
                    }
                    let clean = trimmed.trim_matches(['"', '\'', ',', ' ']);
                    if !clean.is_empty() && !clean.starts_with('#') {
                        let (name, ver) = split_python_req(clean);
                        packages.push((name, ver));
                    }
                }
            }
        }
    } else if setup_cfg.exists() || setup_py.exists() {
        source = if setup_cfg.exists() {
            "setup.cfg"
        } else {
            "setup.py"
        };
    } else {
        return None;
    }

    let venv = root.join(".venv").exists() || root.join("venv").exists();
    let mut issues: Vec<String> = Vec::new();

    for (name, ver) in &packages {
        flag_python_package(name, ver, &mut issues);
    }

    let mut out = format!(
        "PYTHON ({source})\n\
         Packages listed : {}\n\
         Virtual env     : {}\n",
        packages.len(),
        if venv {
            "present"
        } else {
            "not found (.venv / venv)"
        }
    );

    if issues.is_empty() {
        out.push_str("No obvious version issues detected.\n");
    } else {
        out.push_str("Issues found:\n");
        for i in &issues {
            out.push_str(i);
            out.push('\n');
        }
    }
    out.push('\n');
    Some(out)
}

fn split_python_req(line: &str) -> (String, String) {
    for sep in &["==", ">=", "<=", "~=", "!=", ">", "<", "@"] {
        if let Some(pos) = line.find(sep) {
            return (
                line[..pos].trim().to_string(),
                line[pos..].trim().to_string(),
            );
        }
    }
    (line.trim().to_string(), "unpinned".to_string())
}

fn flag_python_package(name: &str, ver: &str, issues: &mut Vec<String>) {
    let lower = name.to_lowercase();
    if ver == "unpinned" {
        issues.push(format!(
            "  [PINNING] {name}: no version pin — add ==X.Y.Z for reproducible installs"
        ));
        return;
    }
    // Known deprecated / insecure
    let deprecated = &[
        "pycrypto", // replaced by pycryptodome
        "md5",      // stdlib-only, never a pip package
        "sha",      // deprecated stdlib
        "urllib3",  // ok but flag very old
        "requests", // ok but note if using 1.x
    ];
    for dep in deprecated {
        if lower == *dep {
            issues.push(format!(
                "  [INFO] {name}: review for deprecation or known CVEs"
            ));
        }
    }
}

// ── go.mod ───────────────────────────────────────────────────────────────────

fn audit_go(root: &Path) -> Option<String> {
    let go_mod = root.join("go.mod");
    let text = std::fs::read_to_string(&go_mod).ok()?;

    let mut module_name = String::new();
    let mut go_version = String::new();
    let mut direct_deps: Vec<(String, String)> = Vec::new();
    let mut indirect_deps = 0usize;
    let mut in_require = false;

    for line in text.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("module ") {
            module_name = trimmed[7..].trim().to_string();
        } else if trimmed.starts_with("go ") {
            go_version = trimmed[3..].trim().to_string();
        } else if trimmed == "require (" {
            in_require = true;
        } else if trimmed == ")" {
            in_require = false;
        } else if in_require && !trimmed.is_empty() && !trimmed.starts_with("//") {
            let parts: Vec<&str> = trimmed.splitn(3, ' ').collect();
            if parts.len() >= 2 {
                let is_indirect = trimmed.contains("// indirect");
                if is_indirect {
                    indirect_deps += 1;
                } else {
                    direct_deps.push((parts[0].to_string(), parts[1].to_string()));
                }
            }
        } else if trimmed.starts_with("require ") && !trimmed.contains('(') {
            let rest = &trimmed[8..];
            let parts: Vec<&str> = rest.splitn(3, ' ').collect();
            if parts.len() >= 2 {
                direct_deps.push((parts[0].to_string(), parts[1].to_string()));
            }
        }
    }

    let go_sum = root.join("go.sum").exists();
    let vendor = root.join("vendor").exists();

    let out = format!(
        "GO (go.mod)\n\
         Module          : {module_name}\n\
         Go version      : {go_version}\n\
         Direct deps     : {}\n\
         Indirect deps   : {indirect_deps}\n\
         go.sum present  : {}\n\
         vendor dir      : {}\n\
         No obvious issues detected (run `go mod tidy` and `govulncheck ./...` for full audit).\n\n",
        direct_deps.len(),
        if go_sum { "yes" } else { "NO — run go mod tidy" },
        if vendor { "yes (vendored)" } else { "no" }
    );

    Some(out)
}