1use crate::agent::config;
2use crate::agent::inference::InferenceEvent;
3use serde_json::Value;
4use tokio::sync::mpsc;
5
6const BUILD_TIMEOUT_SECS: u64 = 120;
7
8pub async fn execute_streaming(
11 args: &Value,
12 tx: mpsc::Sender<InferenceEvent>,
13) -> Result<String, String> {
14 let cwd =
15 std::env::current_dir().map_err(|e| format!("Cannot determine working directory: {e}"))?;
16 let action = args
17 .get("action")
18 .and_then(|v| v.as_str())
19 .unwrap_or("build");
20 let explicit_profile = args.get("profile").and_then(|v| v.as_str());
21 let timeout_override = args.get("timeout_secs").and_then(|v| v.as_u64());
22
23 let config = config::load_config();
24 if let Some(profile_name) = explicit_profile {
25 let profile = config.verify.profiles.get(profile_name).ok_or_else(|| {
26 format!(
27 "Unknown verify profile `{}`. Define it in `.hematite/settings.json` or omit the profile argument.",
28 profile_name
29 )
30 })?;
31 if let Some(command) = profile_command(profile, action) {
32 let timeout_secs = timeout_override
33 .or(profile.timeout_secs)
34 .unwrap_or(BUILD_TIMEOUT_SECS);
35 return run_profile_command_streaming(profile_name, action, command, timeout_secs, tx)
36 .await;
37 }
38
39 return Err(format!(
40 "VERIFY PROFILE MISSING [{profile_name}] action `{action}`.\n\
41 Configure `.hematite/settings.json` with a `{action}` command for this profile, \
42 or call `verify_build` with a different action/profile."
43 ));
44 }
45
46 if let Some(default_profile) = config.verify.default_profile.as_deref() {
47 let profile = config.verify.profiles.get(default_profile).ok_or_else(|| {
48 format!(
49 "Configured default verify profile `{}` was not found in `.hematite/settings.json`.",
50 default_profile
51 )
52 })?;
53 if let Some(command) = profile_command(profile, action) {
54 let timeout_secs = timeout_override
55 .or(profile.timeout_secs)
56 .unwrap_or(BUILD_TIMEOUT_SECS);
57 return run_profile_command_streaming(
58 default_profile,
59 action,
60 command,
61 timeout_secs,
62 tx,
63 )
64 .await;
65 }
66
67 return Err(format!(
68 "VERIFY PROFILE MISSING [{default_profile}] action `{action}`.\n\
69 Configure `.hematite/settings.json` with a `{action}` command for the default profile, \
70 or call `verify_build` with an explicit profile."
71 ));
72 }
73
74 let (label, command, timeout_secs) = autodetect_command(&cwd, action, timeout_override)?;
75 run_profile_command_streaming(label, action, &command, timeout_secs, tx).await
76}
77
78pub async fn execute(args: &Value) -> Result<String, String> {
79 let cwd =
80 std::env::current_dir().map_err(|e| format!("Cannot determine working directory: {e}"))?;
81 let action = args
82 .get("action")
83 .and_then(|v| v.as_str())
84 .unwrap_or("build");
85 let explicit_profile = args.get("profile").and_then(|v| v.as_str());
86 let timeout_override = args.get("timeout_secs").and_then(|v| v.as_u64());
87
88 let config = config::load_config();
89 if let Some(profile_name) = explicit_profile {
90 let profile = config.verify.profiles.get(profile_name).ok_or_else(|| {
91 format!(
92 "Unknown verify profile `{}`. Define it in `.hematite/settings.json` or omit the profile argument.",
93 profile_name
94 )
95 })?;
96 if let Some(command) = profile_command(profile, action) {
97 let timeout_secs = timeout_override
98 .or(profile.timeout_secs)
99 .unwrap_or(BUILD_TIMEOUT_SECS);
100 return run_profile_command(profile_name, action, command, timeout_secs).await;
101 }
102
103 return Err(format!(
104 "VERIFY PROFILE MISSING [{profile_name}] action `{action}`.\n\
105 Configure `.hematite/settings.json` with a `{action}` command for this profile, \
106 or call `verify_build` with a different action/profile."
107 ));
108 }
109
110 if let Some(default_profile) = config.verify.default_profile.as_deref() {
111 let profile = config.verify.profiles.get(default_profile).ok_or_else(|| {
112 format!(
113 "Configured default verify profile `{}` was not found in `.hematite/settings.json`.",
114 default_profile
115 )
116 })?;
117 if let Some(command) = profile_command(profile, action) {
118 let timeout_secs = timeout_override
119 .or(profile.timeout_secs)
120 .unwrap_or(BUILD_TIMEOUT_SECS);
121 return run_profile_command(default_profile, action, command, timeout_secs).await;
122 }
123
124 return Err(format!(
125 "VERIFY PROFILE MISSING [{default_profile}] action `{action}`.\n\
126 Configure `.hematite/settings.json` with a `{action}` command for the default profile, \
127 or call `verify_build` with an explicit profile."
128 ));
129 }
130
131 let (label, command, timeout_secs) = autodetect_command(&cwd, action, timeout_override)?;
132 run_profile_command(label, action, &command, timeout_secs).await
133}
134
135fn profile_command<'a>(profile: &'a config::VerifyProfile, action: &str) -> Option<&'a str> {
136 match action {
137 "build" => profile.build.as_deref(),
138 "test" => profile.test.as_deref(),
139 "lint" => profile.lint.as_deref(),
140 "fix" => profile.fix.as_deref(),
141 _ => None,
142 }
143}
144
145fn autodetect_command(
146 cwd: &std::path::Path,
147 action: &str,
148 timeout_override: Option<u64>,
149) -> Result<(&'static str, String, u64), String> {
150 let timeout_secs = timeout_override.unwrap_or(BUILD_TIMEOUT_SECS);
151 let command = if cwd.join("Cargo.toml").exists() {
152 match action {
153 "build" => ("Rust/Cargo", "cargo build --color never".to_string()),
154 "test" => ("Rust/Cargo", "cargo test --color never".to_string()),
155 "lint" => (
156 "Rust/Cargo",
157 "cargo clippy --all-targets --all-features -- -D warnings".to_string(),
158 ),
159 "fix" => ("Rust/Cargo", "cargo fmt".to_string()),
160 _ => return Err(unknown_action(action)),
161 }
162 } else if cwd.join("go.mod").exists() {
163 match action {
164 "build" => ("Go", "go build ./...".to_string()),
165 "test" => ("Go", "go test ./...".to_string()),
166 "lint" => ("Go", "go vet ./...".to_string()),
167 "fix" => ("Go", "gofmt -w .".to_string()),
168 _ => return Err(unknown_action(action)),
169 }
170 } else if cwd.join("CMakeLists.txt").exists() {
171 let build_dir = if cwd.join("build").exists() {
173 "build"
174 } else {
175 "build"
176 };
177 match action {
178 "build" => (
179 "C++/CMake",
180 format!("cmake -B {build_dir} -DCMAKE_BUILD_TYPE=Release && cmake --build {build_dir} --parallel"),
181 ),
182 "test" => (
183 "C++/CMake",
184 format!("ctest --test-dir {build_dir} --output-on-failure"),
185 ),
186 "lint" => return Err(missing_profile_msg("C++/CMake", action)),
187 "fix" => return Err(missing_profile_msg("C++/CMake", action)),
188 _ => return Err(unknown_action(action)),
189 }
190 } else if cwd.join("package.json").exists() {
191 let pm = if cwd.join("pnpm-lock.yaml").exists()
193 || cwd.join(".npmrc").exists() && {
194 let rc = std::fs::read_to_string(cwd.join(".npmrc")).unwrap_or_default();
195 rc.contains("pnpm")
196 } {
197 "pnpm"
198 } else if cwd.join("yarn.lock").exists() {
199 "yarn"
200 } else if cwd.join("bun.lockb").exists() {
201 "bun"
202 } else {
203 "npm"
204 };
205 let label: &'static str = if cwd.join("tsconfig.json").exists() {
207 match pm {
208 "pnpm" => "TypeScript/pnpm",
209 "yarn" => "TypeScript/yarn",
210 "bun" => "TypeScript/bun",
211 _ => "TypeScript/npm",
212 }
213 } else {
214 match pm {
215 "pnpm" => "Node/pnpm",
216 "yarn" => "Node/yarn",
217 "bun" => "Node/bun",
218 _ => "Node/npm",
219 }
220 };
221 match action {
222 "build" => (label, format!("{pm} run build")),
223 "test" => (label, format!("{pm} test")),
224 "lint" => (label, format!("{pm} run lint")),
225 "fix" => (label, format!("{pm} run format")),
226 _ => return Err(unknown_action(action)),
227 }
228 } else if cwd.join("pyproject.toml").exists()
229 || cwd.join("setup.py").exists()
230 || cwd.join("requirements.txt").exists()
231 || cwd.join(".venv").is_dir()
232 || cwd.join("venv").is_dir()
233 || cwd.join("env").is_dir()
234 {
235 let py = resolve_python_cmd(cwd);
238 match action {
239 "build" => ("Python", format!("{py} -m compileall -q .")),
240 "test" => ("Python", format!("{py} -m pytest -q")),
241 "lint" => (
242 "Python",
243 format!("{py} -m ruff check . || {py} -m flake8 ."),
244 ),
245 "fix" => (
246 "Python",
247 format!("{py} -m ruff format . || {py} -m black ."),
248 ),
249 _ => return Err(unknown_action(action)),
250 }
251 } else if cwd.join("tsconfig.json").exists() {
252 match action {
254 "build" => ("TypeScript/tsc", "tsc --noEmit".to_string()),
255 "test" => return Err(missing_profile_msg("TypeScript/tsc", action)),
256 "lint" => return Err(missing_profile_msg("TypeScript/tsc", action)),
257 "fix" => return Err(missing_profile_msg("TypeScript/tsc", action)),
258 _ => return Err(unknown_action(action)),
259 }
260 } else if cwd.join("index.html").exists() {
261 match action {
262 "build" => ("Static Web", "echo \"BUILD OK (Static assets ready)\"".to_string()),
263 "test" => (
264 "Static Web",
265 "echo \"TEST OK (No test runner found; manual visual check and link verification suggested)\"".to_string(),
266 ),
267 "lint" => ("Static Web", "echo \"LINT OK (Basic structure verified)\"".to_string()),
268 "fix" => ("Static Web", "echo \"FIX OK (No auto-formatter found for static assets)\"".to_string()),
269 _ => return Err(unknown_action(action)),
270 }
271 } else {
272 return Err(format!(
273 "No recognized project root (Cargo.toml, package.json, go.mod, CMakeLists.txt, pyproject.toml, etc.) \
274 found in {}.\nUse an explicit profile or configure a default verify profile in `.hematite/settings.json`.",
275 cwd.display()
276 ));
277 };
278
279 Ok((command.0, command.1, timeout_secs))
280}
281
282fn resolve_python_cmd(cwd: &std::path::Path) -> String {
283 if cwd.join("poetry.lock").exists() {
285 return "poetry run python".to_string();
286 }
287 if cwd.join("Pipfile.lock").exists() || cwd.join("Pipfile").exists() {
289 return "pipenv run python".to_string();
290 }
291 let venv_folders = [".venv", "venv", "env"];
293 for folder in venv_folders {
294 if cwd.join(folder).is_dir() {
295 let rel_path = if cfg!(windows) {
296 format!("{}\\Scripts\\python.exe", folder)
297 } else {
298 format!("{}/bin/python", folder)
299 };
300 if cwd.join(&rel_path).exists() {
301 return format!(".{}{}", if cfg!(windows) { "\\" } else { "/" }, rel_path);
302 }
303 }
304 }
305
306 "python".to_string()
307}
308
309fn missing_profile_msg(stack: &str, action: &str) -> String {
310 format!(
311 "No auto-detected `{action}` command for [{stack}].\n\
312 Add a verify profile in `.hematite/settings.json` if you want Hematite to run `{action}` for this project."
313 )
314}
315
316fn unknown_action(action: &str) -> String {
317 format!(
318 "Unknown verify_build action `{}`. Use one of: build, test, lint, fix.",
319 action
320 )
321}
322
323async fn run_profile_command(
324 profile_name: &str,
325 action: &str,
326 command: &str,
327 timeout_secs: u64,
328) -> Result<String, String> {
329 let output = crate::tools::shell::execute(&serde_json::json!({
330 "command": command,
331 "timeout_secs": timeout_secs,
332 "reason": format!("verify_build:{}:{}", profile_name, action),
333 }))
334 .await?;
335
336 if output.contains("[exit code: 0]") || !output.contains("[exit code:") {
337 Ok(format!(
338 "BUILD OK [{}:{}]\ncommand: {}\n{}",
339 profile_name,
340 action,
341 command,
342 output.trim()
343 ))
344 } else if should_fallback_to_cargo_check(action, command, &output) {
345 run_windows_self_hosted_check_fallback(profile_name, action, command, timeout_secs, &output)
346 .await
347 } else {
348 Err(format!(
349 "BUILD FAILED [{}:{}]\ncommand: {}\n{}",
350 profile_name,
351 action,
352 command,
353 output.trim()
354 ))
355 }
356}
357
358async fn run_profile_command_streaming(
359 profile_name: &str,
360 action: &str,
361 command: &str,
362 timeout_secs: u64,
363 tx: mpsc::Sender<InferenceEvent>,
364) -> Result<String, String> {
365 let output = crate::tools::shell::execute_streaming(
366 &serde_json::json!({
367 "command": command,
368 "timeout_secs": timeout_secs,
369 "reason": format!("verify_build:{}:{}", profile_name, action),
370 }),
371 tx.clone(),
372 )
373 .await?;
374
375 if output.contains("[exit code: 0]") || !output.contains("[exit code:") {
376 Ok(format!(
377 "BUILD OK [{}:{}]\ncommand: {}\n{}",
378 profile_name,
379 action,
380 command,
381 output.trim()
382 ))
383 } else if should_fallback_to_cargo_check(action, command, &output) {
384 run_windows_self_hosted_check_fallback_streaming(
385 profile_name,
386 action,
387 command,
388 timeout_secs,
389 &output,
390 tx,
391 )
392 .await
393 } else {
394 Err(format!(
395 "BUILD FAILED [{}:{}]\ncommand: {}\n{}",
396 profile_name,
397 action,
398 command,
399 output.trim()
400 ))
401 }
402}
403
404async fn run_windows_self_hosted_check_fallback_streaming(
405 profile_name: &str,
406 action: &str,
407 original_command: &str,
408 timeout_secs: u64,
409 original_output: &str,
410 tx: mpsc::Sender<InferenceEvent>,
411) -> Result<String, String> {
412 let fallback_command = "cargo check --color never";
413 let fallback_output = crate::tools::shell::execute_streaming(
414 &serde_json::json!({
415 "command": fallback_command,
416 "timeout_secs": timeout_secs,
417 "reason": format!("verify_build:{}:{}:self_hosted_windows_fallback", profile_name, action),
418 }),
419 tx,
420 )
421 .await?;
422
423 if fallback_output.contains("[exit code: 0]") || !fallback_output.contains("[exit code:") {
424 Ok(format!(
425 "BUILD OK [{}:{}]\ncommand: {}\n\
426 Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, so Hematite fell back to `cargo check` to verify code health without deleting the live binary.\n\
427 original build output:\n{}\n\
428 fallback command: {}\n{}",
429 profile_name,
430 action,
431 original_command,
432 original_output.trim(),
433 fallback_command,
434 fallback_output.trim()
435 ))
436 } else {
437 Err(format!(
438 "BUILD FAILED [{}:{}]\ncommand: {}\n\
439 Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, and the fallback `cargo check` also failed.\n\
440 original build output:\n{}\n\
441 fallback command: {}\n{}",
442 profile_name,
443 action,
444 original_command,
445 original_output.trim(),
446 fallback_command,
447 fallback_output.trim()
448 ))
449 }
450}
451
452fn should_fallback_to_cargo_check(action: &str, command: &str, output: &str) -> bool {
453 if action != "build" || command.trim() != "cargo build --color never" {
454 return false;
455 }
456
457 if cfg!(windows) {
458 looks_like_windows_self_hosted_build_lock(output)
459 } else {
460 false
461 }
462}
463
464fn looks_like_windows_self_hosted_build_lock(output: &str) -> bool {
465 let lower = output.to_ascii_lowercase();
466 lower.contains("failed to remove file")
467 && lower.contains("target\\debug\\hematite.exe")
468 && (lower.contains("access is denied")
469 || lower.contains("being used by another process")
470 || lower.contains("permission denied"))
471}
472
473async fn run_windows_self_hosted_check_fallback(
474 profile_name: &str,
475 action: &str,
476 original_command: &str,
477 timeout_secs: u64,
478 original_output: &str,
479) -> Result<String, String> {
480 let fallback_command = "cargo check --color never";
481 let fallback_output = crate::tools::shell::execute(&serde_json::json!({
482 "command": fallback_command,
483 "timeout_secs": timeout_secs,
484 "reason": format!("verify_build:{}:{}:self_hosted_windows_fallback", profile_name, action),
485 }))
486 .await?;
487
488 if fallback_output.contains("[exit code: 0]") || !fallback_output.contains("[exit code:") {
489 Ok(format!(
490 "BUILD OK [{}:{}]\ncommand: {}\n\
491 Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, so Hematite fell back to `cargo check` to verify code health without deleting the live binary.\n\
492 original build output:\n{}\n\
493 fallback command: {}\n{}",
494 profile_name,
495 action,
496 original_command,
497 original_output.trim(),
498 fallback_command,
499 fallback_output.trim()
500 ))
501 } else {
502 Err(format!(
503 "BUILD FAILED [{}:{}]\ncommand: {}\n\
504 Windows self-hosted note: `cargo build` could not replace the running `target\\\\debug\\\\hematite.exe`, and the fallback `cargo check` also failed.\n\
505 original build output:\n{}\n\
506 fallback command: {}\n{}",
507 profile_name,
508 action,
509 original_command,
510 original_output.trim(),
511 fallback_command,
512 fallback_output.trim()
513 ))
514 }
515}
516
517#[cfg(test)]
518mod tests {
519 use super::*;
520
521 #[test]
522 fn detects_windows_self_hosted_build_lock_pattern() {
523 let sample = "[stderr] error: failed to remove file `C:\\Users\\ocean\\AntigravityProjects\\Hematite-CLI\\target\\debug\\hematite.exe`\r\nAccess is denied. (os error 5)";
524 assert!(looks_like_windows_self_hosted_build_lock(sample));
525 }
526
527 #[test]
528 fn ignores_unrelated_build_failures() {
529 let sample = "[stderr] error[E0425]: cannot find value `foo` in this scope";
530 assert!(!looks_like_windows_self_hosted_build_lock(sample));
531 assert!(!should_fallback_to_cargo_check(
532 "build",
533 "cargo build --color never",
534 sample
535 ));
536 }
537
538 #[test]
539 fn autodetect_rust_stack() {
540 let dir = tempfile::tempdir().unwrap();
541 std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
542 let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
543 assert_eq!(label, "Rust/Cargo");
544 assert!(cmd.contains("cargo build"));
545 let (_, test_cmd, _) = autodetect_command(dir.path(), "test", None).unwrap();
546 assert!(test_cmd.contains("cargo test"));
547 let (_, lint_cmd, _) = autodetect_command(dir.path(), "lint", None).unwrap();
548 assert!(lint_cmd.contains("clippy"));
549 }
550
551 #[test]
552 fn autodetect_go_stack() {
553 let dir = tempfile::tempdir().unwrap();
554 std::fs::write(
555 dir.path().join("go.mod"),
556 "module example.com/foo\ngo 1.21\n",
557 )
558 .unwrap();
559 let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
560 assert_eq!(label, "Go");
561 assert!(cmd.contains("go build"));
562 let (_, test_cmd, _) = autodetect_command(dir.path(), "test", None).unwrap();
563 assert!(test_cmd.contains("go test"));
564 let (_, lint_cmd, _) = autodetect_command(dir.path(), "lint", None).unwrap();
565 assert!(lint_cmd.contains("go vet"));
566 }
567
568 #[test]
569 fn autodetect_cmake_stack() {
570 let dir = tempfile::tempdir().unwrap();
571 std::fs::write(
572 dir.path().join("CMakeLists.txt"),
573 "cmake_minimum_required(VERSION 3.20)\n",
574 )
575 .unwrap();
576 let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
577 assert_eq!(label, "C++/CMake");
578 assert!(cmd.contains("cmake"));
579 assert!(cmd.contains("--build"));
580 }
581
582 #[test]
583 fn autodetect_node_npm_stack() {
584 let dir = tempfile::tempdir().unwrap();
585 std::fs::write(dir.path().join("package.json"), "{}").unwrap();
586 let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
587 assert!(label.contains("Node") || label.contains("TypeScript"));
588 assert!(cmd.contains("npm run build"));
589 }
590
591 #[test]
592 fn autodetect_node_yarn_stack() {
593 let dir = tempfile::tempdir().unwrap();
594 std::fs::write(dir.path().join("package.json"), "{}").unwrap();
595 std::fs::write(dir.path().join("yarn.lock"), "").unwrap();
596 let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
597 assert!(label.contains("yarn"));
598 assert!(cmd.contains("yarn run build"));
599 }
600
601 #[test]
602 fn autodetect_node_pnpm_stack() {
603 let dir = tempfile::tempdir().unwrap();
604 std::fs::write(dir.path().join("package.json"), "{}").unwrap();
605 std::fs::write(dir.path().join("pnpm-lock.yaml"), "").unwrap();
606 let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
607 assert!(label.contains("pnpm"));
608 assert!(cmd.contains("pnpm run build"));
609 }
610
611 #[test]
612 fn autodetect_python_stack_pyproject() {
613 let dir = tempfile::tempdir().unwrap();
614 std::fs::write(dir.path().join("pyproject.toml"), "[build-system]\n").unwrap();
615 let (label, cmd, _) = autodetect_command(dir.path(), "build", None).unwrap();
616 assert_eq!(label, "Python");
617 assert!(cmd.contains("compileall"));
618 let (_, test_cmd, _) = autodetect_command(dir.path(), "test", None).unwrap();
619 assert!(test_cmd.contains("pytest"));
620 }
621
622 #[test]
623 fn autodetect_python_stack_requirements() {
624 let dir = tempfile::tempdir().unwrap();
625 std::fs::write(dir.path().join("requirements.txt"), "fastapi\n").unwrap();
626 let (label, _, _) = autodetect_command(dir.path(), "build", None).unwrap();
627 assert_eq!(label, "Python");
628 }
629
630 #[test]
631 fn resolves_local_venv_python() {
632 let dir = tempfile::tempdir().unwrap();
633 let venv = dir.path().join(".venv");
634 std::fs::create_dir(&venv).unwrap();
635
636 let bin_sub = if cfg!(windows) { "Scripts" } else { "bin" };
638 let exe_name = if cfg!(windows) {
639 "python.exe"
640 } else {
641 "python"
642 };
643 let bin_dir = venv.join(bin_sub);
644 std::fs::create_dir(&bin_dir).unwrap();
645 std::fs::write(bin_dir.join(exe_name), "").unwrap();
646
647 let cmd = resolve_python_cmd(dir.path());
648 assert!(cmd.contains(".venv"));
649 assert!(cmd.contains(bin_sub));
650 }
651
652 #[test]
653 fn resolves_poetry_run() {
654 let dir = tempfile::tempdir().unwrap();
655 std::fs::write(dir.path().join("poetry.lock"), "").unwrap();
656 let cmd = resolve_python_cmd(dir.path());
657 assert_eq!(cmd, "poetry run python");
658 }
659
660 #[test]
661 fn autodetect_no_project_returns_err() {
662 let dir = tempfile::tempdir().unwrap();
663 let result = autodetect_command(dir.path(), "build", None);
664 assert!(result.is_err());
665 let msg = result.unwrap_err();
666 assert!(msg.contains("No recognized project root"));
667 assert!(msg.contains("Cargo.toml"));
668 assert!(msg.contains("CMakeLists.txt"));
669 }
670}