1use std::collections::HashSet;
2use std::fs;
3use std::io::{self, Write};
4use std::path::{Path, PathBuf};
5use std::process;
6use std::sync::atomic::{AtomicBool, Ordering};
7use std::sync::{Arc, Mutex};
8
9use harn_parser::DiagnosticSeverity;
10use harn_vm::event_log::EventLog;
11
12use crate::commands::mcp::{self, AuthResolution};
13use crate::package;
14use crate::parse_source_file;
15use crate::skill_loader::{
16 canonicalize_cli_dirs, emit_loader_warnings, install_skills_global, load_skills,
17 SkillLoaderInputs,
18};
19
20mod explain_cost;
21
22pub(crate) enum RunFileMcpServeMode {
23 Stdio,
24 Http {
25 options: harn_serve::McpHttpServeOptions,
26 auth_policy: harn_serve::AuthPolicy,
27 },
28}
29
30const CORE_BUILTINS: &[&str] = &[
32 "println",
33 "print",
34 "log",
35 "type_of",
36 "to_string",
37 "to_int",
38 "to_float",
39 "len",
40 "assert",
41 "assert_eq",
42 "assert_ne",
43 "json_parse",
44 "json_stringify",
45 "runtime_context",
46 "task_current",
47 "runtime_context_values",
48 "runtime_context_get",
49 "runtime_context_set",
50 "runtime_context_clear",
51];
52
53pub(crate) fn build_denied_builtins(
58 deny_csv: Option<&str>,
59 allow_csv: Option<&str>,
60) -> HashSet<String> {
61 if let Some(csv) = deny_csv {
62 csv.split(',')
63 .map(|s| s.trim().to_string())
64 .filter(|s| !s.is_empty())
65 .collect()
66 } else if let Some(csv) = allow_csv {
67 let allowed: HashSet<String> = csv
70 .split(',')
71 .map(|s| s.trim().to_string())
72 .filter(|s| !s.is_empty())
73 .collect();
74 let core: HashSet<&str> = CORE_BUILTINS.iter().copied().collect();
75
76 let mut tmp = harn_vm::Vm::new();
78 harn_vm::register_vm_stdlib(&mut tmp);
79 harn_vm::register_store_builtins(&mut tmp, std::path::Path::new("."));
80 harn_vm::register_metadata_builtins(&mut tmp, std::path::Path::new("."));
81
82 tmp.builtin_names()
83 .into_iter()
84 .filter(|name| !allowed.contains(name) && !core.contains(name.as_str()))
85 .collect()
86 } else {
87 HashSet::new()
88 }
89}
90
91fn typecheck_with_imports(
96 program: &[harn_parser::SNode],
97 path: &Path,
98 source: &str,
99) -> Vec<harn_parser::TypeDiagnostic> {
100 if let Err(error) = package::ensure_dependencies_materialized(path) {
101 eprintln!("error: {error}");
102 process::exit(1);
103 }
104 let graph = harn_modules::build(&[path.to_path_buf()]);
105 let mut checker = harn_parser::TypeChecker::new();
106 if let Some(imported) = graph.imported_names_for_file(path) {
107 checker = checker.with_imported_names(imported);
108 }
109 if let Some(imported) = graph.imported_type_declarations_for_file(path) {
110 checker = checker.with_imported_type_decls(imported);
111 }
112 if let Some(imported) = graph.imported_callable_declarations_for_file(path) {
113 checker = checker.with_imported_callable_decls(imported);
114 }
115 checker.check_with_source(program, source)
116}
117
118pub(crate) fn prepare_eval_temp_file(
129 code: &str,
130) -> Result<(String, tempfile::NamedTempFile), String> {
131 let (header, body) = split_eval_header(code);
132 let wrapped = if header.is_empty() {
133 format!("pipeline main(task) {{\n{body}\n}}")
134 } else {
135 format!("{header}\npipeline main(task) {{\n{body}\n}}")
136 };
137
138 let tmp = create_eval_temp_file()?;
139 Ok((wrapped, tmp))
140}
141
142fn create_eval_temp_file() -> Result<tempfile::NamedTempFile, String> {
147 if let Some(dir) = std::env::current_dir().ok().as_deref() {
148 match tempfile::Builder::new()
151 .prefix(".harn-eval-")
152 .suffix(".harn")
153 .tempfile_in(dir)
154 {
155 Ok(tmp) => return Ok(tmp),
156 Err(error) => eprintln!(
157 "warning: harn run -e: could not create temp file in {}: {error}; \
158 relative imports will not resolve",
159 dir.display()
160 ),
161 }
162 }
163 tempfile::Builder::new()
164 .prefix("harn-eval-")
165 .suffix(".harn")
166 .tempfile()
167 .map_err(|e| format!("failed to create temp file for -e: {e}"))
168}
169
170fn split_eval_header(code: &str) -> (String, String) {
178 let mut header_end = 0usize;
179 let mut last_kept = 0usize;
180 for (idx, line) in code.lines().enumerate() {
181 let trimmed = line.trim_start();
182 if trimmed.is_empty() || trimmed.starts_with("//") {
183 header_end = idx + 1;
184 continue;
185 }
186 let is_import = trimmed.starts_with("import ")
187 || trimmed.starts_with("import\t")
188 || trimmed.starts_with("import\"")
189 || trimmed.starts_with("pub import ")
190 || trimmed.starts_with("pub import\t");
191 if is_import {
192 header_end = idx + 1;
193 last_kept = idx + 1;
194 } else {
195 break;
196 }
197 }
198 if last_kept == 0 {
199 return (String::new(), code.to_string());
200 }
201 let mut header_lines: Vec<&str> = Vec::new();
202 let mut body_lines: Vec<&str> = Vec::new();
203 for (idx, line) in code.lines().enumerate() {
204 if idx < header_end {
205 header_lines.push(line);
206 } else {
207 body_lines.push(line);
208 }
209 }
210 (header_lines.join("\n"), body_lines.join("\n"))
211}
212
213#[derive(Clone, Debug, Default, PartialEq, Eq)]
214pub enum CliLlmMockMode {
215 #[default]
216 Off,
217 Replay {
218 fixture_path: PathBuf,
219 },
220 Record {
221 fixture_path: PathBuf,
222 },
223}
224
225#[derive(Clone, Debug, Default, PartialEq, Eq)]
226pub struct RunAttestationOptions {
227 pub receipt_out: Option<PathBuf>,
228 pub agent_id: Option<String>,
229}
230
231#[derive(Clone, Debug, Default, PartialEq, Eq)]
236pub struct RunProfileOptions {
237 pub text: bool,
238 pub json_path: Option<PathBuf>,
239}
240
241impl RunProfileOptions {
242 pub fn is_enabled(&self) -> bool {
243 self.text || self.json_path.is_some()
244 }
245}
246
247#[derive(Clone)]
248pub struct RunInterruptTokens {
249 pub cancel_token: Arc<AtomicBool>,
250 pub signal_token: Arc<Mutex<Option<String>>>,
251}
252
253struct ExecuteRunInputs<'a> {
254 path: &'a str,
255 trace: bool,
256 denied_builtins: HashSet<String>,
257 script_argv: Vec<String>,
258 skill_dirs_raw: Vec<String>,
259 llm_mock_mode: CliLlmMockMode,
260 attestation: Option<RunAttestationOptions>,
261 profile: RunProfileOptions,
262 interrupt_tokens: Option<RunInterruptTokens>,
263}
264
265#[derive(Clone, Debug, Default)]
269pub struct RunOutcome {
270 pub stdout: String,
271 pub stderr: String,
272 pub exit_code: i32,
273}
274
275pub fn install_cli_llm_mock_mode(mode: &CliLlmMockMode) -> Result<(), String> {
276 harn_vm::llm::clear_cli_llm_mock_mode();
277 match mode {
278 CliLlmMockMode::Off => Ok(()),
279 CliLlmMockMode::Replay { fixture_path } => {
280 let mocks = harn_vm::llm::load_llm_mocks_jsonl(fixture_path)?;
281 harn_vm::llm::install_cli_llm_mocks(mocks);
282 Ok(())
283 }
284 CliLlmMockMode::Record { .. } => {
285 harn_vm::llm::enable_cli_llm_mock_recording();
286 Ok(())
287 }
288 }
289}
290
291pub fn persist_cli_llm_mock_recording(mode: &CliLlmMockMode) -> Result<(), String> {
292 let CliLlmMockMode::Record { fixture_path } = mode else {
293 return Ok(());
294 };
295 if let Some(parent) = fixture_path.parent() {
296 if !parent.as_os_str().is_empty() {
297 fs::create_dir_all(parent).map_err(|error| {
298 format!(
299 "failed to create fixture directory {}: {error}",
300 parent.display()
301 )
302 })?;
303 }
304 }
305
306 let lines = harn_vm::llm::take_cli_llm_recordings()
307 .into_iter()
308 .map(harn_vm::llm::serialize_llm_mock)
309 .collect::<Result<Vec<_>, _>>()?;
310 let body = if lines.is_empty() {
311 String::new()
312 } else {
313 format!("{}\n", lines.join("\n"))
314 };
315 fs::write(fixture_path, body)
316 .map_err(|error| format!("failed to write {}: {error}", fixture_path.display()))
317}
318
319pub(crate) async fn run_file(
320 path: &str,
321 trace: bool,
322 denied_builtins: HashSet<String>,
323 script_argv: Vec<String>,
324 llm_mock_mode: CliLlmMockMode,
325 attestation: Option<RunAttestationOptions>,
326 profile: RunProfileOptions,
327) {
328 run_file_with_skill_dirs(
329 path,
330 trace,
331 denied_builtins,
332 script_argv,
333 Vec::new(),
334 llm_mock_mode,
335 attestation,
336 profile,
337 )
338 .await;
339}
340
341pub(crate) fn run_explain_cost_file_with_skill_dirs(path: &str) {
342 let outcome = execute_explain_cost(path);
343 if !outcome.stderr.is_empty() {
344 io::stderr().write_all(outcome.stderr.as_bytes()).ok();
345 }
346 if !outcome.stdout.is_empty() {
347 io::stdout().write_all(outcome.stdout.as_bytes()).ok();
348 }
349 if outcome.exit_code != 0 {
350 process::exit(outcome.exit_code);
351 }
352}
353
354pub(crate) async fn run_file_with_skill_dirs(
355 path: &str,
356 trace: bool,
357 denied_builtins: HashSet<String>,
358 script_argv: Vec<String>,
359 skill_dirs_raw: Vec<String>,
360 llm_mock_mode: CliLlmMockMode,
361 attestation: Option<RunAttestationOptions>,
362 profile: RunProfileOptions,
363) {
364 let interrupt_tokens = install_signal_shutdown_handler();
366
367 let _stdout_passthrough = StdoutPassthroughGuard::enable();
368 let outcome = execute_run_inner(ExecuteRunInputs {
369 path,
370 trace,
371 denied_builtins,
372 script_argv,
373 skill_dirs_raw,
374 llm_mock_mode,
375 attestation,
376 profile,
377 interrupt_tokens: Some(interrupt_tokens.clone()),
378 })
379 .await;
380
381 if !outcome.stderr.is_empty() {
384 io::stderr().write_all(outcome.stderr.as_bytes()).ok();
385 }
386 if !outcome.stdout.is_empty() {
387 io::stdout().write_all(outcome.stdout.as_bytes()).ok();
388 }
389
390 let mut exit_code = outcome.exit_code;
391 if exit_code != 0 && interrupt_tokens.cancel_token.load(Ordering::SeqCst) {
392 exit_code = 124;
393 }
394 if exit_code != 0 {
395 process::exit(exit_code);
396 }
397}
398
399pub fn execute_explain_cost(path: &str) -> RunOutcome {
400 let stdout = String::new();
401 let mut stderr = String::new();
402
403 let (source, program) = parse_source_file(path);
404
405 let mut had_type_error = false;
406 let type_diagnostics = typecheck_with_imports(&program, Path::new(path), &source);
407 for diag in &type_diagnostics {
408 let rendered = harn_parser::diagnostic::render_type_diagnostic(&source, path, diag);
409 if matches!(diag.severity, DiagnosticSeverity::Error) {
410 had_type_error = true;
411 }
412 stderr.push_str(&rendered);
413 }
414 if had_type_error {
415 return RunOutcome {
416 stdout,
417 stderr,
418 exit_code: 1,
419 };
420 }
421
422 let extensions = package::load_runtime_extensions(Path::new(path));
423 package::install_runtime_extensions(&extensions);
424 RunOutcome {
425 stdout: explain_cost::render_explain_cost(path, &program),
426 stderr,
427 exit_code: 0,
428 }
429}
430
431struct StdoutPassthroughGuard {
432 previous: bool,
433}
434
435impl StdoutPassthroughGuard {
436 fn enable() -> Self {
437 Self {
438 previous: harn_vm::set_stdout_passthrough(true),
439 }
440 }
441}
442
443impl Drop for StdoutPassthroughGuard {
444 fn drop(&mut self) {
445 harn_vm::set_stdout_passthrough(self.previous);
446 }
447}
448
449fn install_signal_shutdown_handler() -> RunInterruptTokens {
450 let tokens = RunInterruptTokens {
451 cancel_token: Arc::new(AtomicBool::new(false)),
452 signal_token: Arc::new(Mutex::new(None)),
453 };
454 let tokens_clone = tokens.clone();
455 tokio::spawn(async move {
456 #[cfg(unix)]
457 {
458 use tokio::signal::unix::{signal, SignalKind};
459 let mut sigterm = signal(SignalKind::terminate()).expect("SIGTERM handler");
460 let mut sigint = signal(SignalKind::interrupt()).expect("SIGINT handler");
461 let mut sighup = signal(SignalKind::hangup()).expect("SIGHUP handler");
462 let mut seen_signal = false;
463 loop {
464 let signal_name = tokio::select! {
465 _ = sigterm.recv() => "SIGTERM",
466 _ = sigint.recv() => "SIGINT",
467 _ = sighup.recv() => "SIGHUP",
468 };
469 if seen_signal {
470 eprintln!("[harn] second signal received, terminating");
471 process::exit(124);
472 }
473 seen_signal = true;
474 request_vm_interrupt(&tokens_clone, signal_name);
475 eprintln!("[harn] signal received, interrupting VM...");
476 }
477 }
478 #[cfg(not(unix))]
479 {
480 let mut seen_signal = false;
481 loop {
482 let _ = tokio::signal::ctrl_c().await;
483 if seen_signal {
484 eprintln!("[harn] second signal received, terminating");
485 process::exit(124);
486 }
487 seen_signal = true;
488 request_vm_interrupt(&tokens_clone, "SIGINT");
489 eprintln!("[harn] signal received, interrupting VM...");
490 }
491 }
492 });
493 tokens
494}
495
496fn request_vm_interrupt(tokens: &RunInterruptTokens, signal_name: &str) {
497 if let Ok(mut signal) = tokens.signal_token.lock() {
498 *signal = Some(signal_name.to_string());
499 }
500 tokens.cancel_token.store(true, Ordering::SeqCst);
501}
502
503pub async fn execute_run(
509 path: &str,
510 trace: bool,
511 denied_builtins: HashSet<String>,
512 script_argv: Vec<String>,
513 skill_dirs_raw: Vec<String>,
514 llm_mock_mode: CliLlmMockMode,
515 attestation: Option<RunAttestationOptions>,
516 profile: RunProfileOptions,
517) -> RunOutcome {
518 execute_run_inner(ExecuteRunInputs {
519 path,
520 trace,
521 denied_builtins,
522 script_argv,
523 skill_dirs_raw,
524 llm_mock_mode,
525 attestation,
526 profile,
527 interrupt_tokens: None,
528 })
529 .await
530}
531
532async fn execute_run_inner(inputs: ExecuteRunInputs<'_>) -> RunOutcome {
533 let ExecuteRunInputs {
534 path,
535 trace,
536 denied_builtins,
537 script_argv,
538 skill_dirs_raw,
539 llm_mock_mode,
540 attestation,
541 profile,
542 interrupt_tokens,
543 } = inputs;
544
545 let mut stderr = String::new();
546 let mut stdout = String::new();
547
548 let (source, program) = parse_source_file(path);
549
550 let mut had_type_error = false;
551 let type_diagnostics = typecheck_with_imports(&program, Path::new(path), &source);
552 for diag in &type_diagnostics {
553 let rendered = harn_parser::diagnostic::render_type_diagnostic(&source, path, diag);
554 if matches!(diag.severity, DiagnosticSeverity::Error) {
555 had_type_error = true;
556 }
557 stderr.push_str(&rendered);
558 }
559 if had_type_error {
560 return RunOutcome {
561 stdout,
562 stderr,
563 exit_code: 1,
564 };
565 }
566
567 let chunk = match harn_vm::Compiler::new().compile(&program) {
568 Ok(c) => c,
569 Err(e) => {
570 stderr.push_str(&format!("error: compile error: {e}\n"));
571 return RunOutcome {
572 stdout,
573 stderr,
574 exit_code: 1,
575 };
576 }
577 };
578
579 if trace {
580 harn_vm::llm::enable_tracing();
581 }
582 if profile.is_enabled() {
583 harn_vm::tracing::set_tracing_enabled(true);
584 }
585 if let Err(error) = install_cli_llm_mock_mode(&llm_mock_mode) {
586 stderr.push_str(&format!("error: {error}\n"));
587 return RunOutcome {
588 stdout,
589 stderr,
590 exit_code: 1,
591 };
592 }
593
594 let mut vm = harn_vm::Vm::new();
595 if let Some(interrupt_tokens) = interrupt_tokens {
596 vm.install_interrupt_signal_token(interrupt_tokens.signal_token);
597 vm.install_cancel_token(interrupt_tokens.cancel_token);
598 }
599 harn_vm::register_vm_stdlib(&mut vm);
600 crate::install_default_hostlib(&mut vm);
601 let source_parent = std::path::Path::new(path)
602 .parent()
603 .unwrap_or(std::path::Path::new("."));
604 let project_root = harn_vm::stdlib::process::find_project_root(source_parent);
606 let store_base = project_root.as_deref().unwrap_or(source_parent);
607 let attestation_started_at_ms = now_ms();
608 let attestation_log = if attestation.is_some() {
609 Some(harn_vm::event_log::install_memory_for_current_thread(256))
610 } else {
611 None
612 };
613 if let Some(log) = attestation_log.as_ref() {
614 append_run_provenance_event(
615 log,
616 "started",
617 serde_json::json!({
618 "pipeline": path,
619 "argv": &script_argv,
620 "project_root": store_base.display().to_string(),
621 }),
622 )
623 .await;
624 }
625 harn_vm::register_store_builtins(&mut vm, store_base);
626 harn_vm::register_metadata_builtins(&mut vm, store_base);
627 let pipeline_name = std::path::Path::new(path)
628 .file_stem()
629 .and_then(|s| s.to_str())
630 .unwrap_or("default");
631 harn_vm::register_checkpoint_builtins(&mut vm, store_base, pipeline_name);
632 vm.set_source_info(path, &source);
633 if !denied_builtins.is_empty() {
634 vm.set_denied_builtins(denied_builtins);
635 }
636 if let Some(ref root) = project_root {
637 vm.set_project_root(root);
638 }
639
640 if let Some(p) = std::path::Path::new(path).parent() {
641 if !p.as_os_str().is_empty() {
642 vm.set_source_dir(p);
643 }
644 }
645
646 let cli_dirs = canonicalize_cli_dirs(&skill_dirs_raw, None);
649 let loaded = load_skills(&SkillLoaderInputs {
650 cli_dirs,
651 source_path: Some(std::path::PathBuf::from(path)),
652 });
653 emit_loader_warnings(&loaded.loader_warnings);
654 install_skills_global(&mut vm, &loaded);
655
656 let argv_values: Vec<harn_vm::VmValue> = script_argv
659 .iter()
660 .map(|s| harn_vm::VmValue::String(std::rc::Rc::from(s.as_str())))
661 .collect();
662 vm.set_global(
663 "argv",
664 harn_vm::VmValue::List(std::rc::Rc::new(argv_values)),
665 );
666
667 let extensions = package::load_runtime_extensions(Path::new(path));
668 package::install_runtime_extensions(&extensions);
669 if let Some(manifest) = extensions.root_manifest.as_ref() {
670 if !manifest.mcp.is_empty() {
671 connect_mcp_servers(&manifest.mcp, &mut vm).await;
672 }
673 }
674 if let Err(error) = package::install_manifest_triggers(&mut vm, &extensions).await {
675 stderr.push_str(&format!(
676 "error: failed to install manifest triggers: {error}\n"
677 ));
678 return RunOutcome {
679 stdout,
680 stderr,
681 exit_code: 1,
682 };
683 }
684 if let Err(error) = package::install_manifest_hooks(&mut vm, &extensions).await {
685 stderr.push_str(&format!(
686 "error: failed to install manifest hooks: {error}\n"
687 ));
688 return RunOutcome {
689 stdout,
690 stderr,
691 exit_code: 1,
692 };
693 }
694
695 let local = tokio::task::LocalSet::new();
697 let execution = local
698 .run_until(async {
699 match vm.execute(&chunk).await {
700 Ok(value) => Ok((vm.output(), value)),
701 Err(e) => Err(vm.format_runtime_error(&e)),
702 }
703 })
704 .await;
705 if let Err(error) = persist_cli_llm_mock_recording(&llm_mock_mode) {
706 stderr.push_str(&format!("error: {error}\n"));
707 return RunOutcome {
708 stdout,
709 stderr,
710 exit_code: 1,
711 };
712 }
713
714 let buffered_stderr = harn_vm::take_stderr_buffer();
716 stderr.push_str(&buffered_stderr);
717
718 let exit_code = match &execution {
719 Ok((_, return_value)) => exit_code_from_return_value(return_value),
720 Err(_) => 1,
721 };
722
723 if let (Some(options), Some(log)) = (attestation.as_ref(), attestation_log.as_ref()) {
724 if let Err(error) = emit_run_attestation(
725 log,
726 path,
727 store_base,
728 attestation_started_at_ms,
729 exit_code,
730 options,
731 &mut stderr,
732 )
733 .await
734 {
735 stderr.push_str(&format!(
736 "error: failed to emit provenance receipt: {error}\n"
737 ));
738 return RunOutcome {
739 stdout,
740 stderr,
741 exit_code: 1,
742 };
743 }
744 harn_vm::event_log::reset_active_event_log();
745 }
746
747 match execution {
748 Ok((output, return_value)) => {
749 stdout.push_str(output);
750 if trace {
751 stderr.push_str(&render_trace_summary());
752 }
753 if profile.is_enabled() {
754 if let Err(error) = render_and_persist_profile(&profile, &mut stderr) {
755 stderr.push_str(&format!("warning: failed to write profile: {error}\n"));
756 }
757 }
758 if exit_code != 0 {
759 stderr.push_str(&render_return_value_error(&return_value));
760 }
761 RunOutcome {
762 stdout,
763 stderr,
764 exit_code,
765 }
766 }
767 Err(rendered_error) => {
768 stderr.push_str(&rendered_error);
769 if profile.is_enabled() {
770 if let Err(error) = render_and_persist_profile(&profile, &mut stderr) {
771 stderr.push_str(&format!("warning: failed to write profile: {error}\n"));
772 }
773 }
774 RunOutcome {
775 stdout,
776 stderr,
777 exit_code: 1,
778 }
779 }
780 }
781}
782
783fn render_and_persist_profile(
784 options: &RunProfileOptions,
785 stderr: &mut String,
786) -> Result<(), String> {
787 let spans = harn_vm::tracing::peek_spans();
788 let profile = harn_vm::profile::build(&spans);
789 if options.text {
790 stderr.push_str(&harn_vm::profile::render(&profile));
791 }
792 if let Some(path) = options.json_path.as_ref() {
793 if let Some(parent) = path.parent() {
794 if !parent.as_os_str().is_empty() {
795 fs::create_dir_all(parent)
796 .map_err(|error| format!("create {}: {error}", parent.display()))?;
797 }
798 }
799 let json = serde_json::to_string_pretty(&profile)
800 .map_err(|error| format!("serialize profile: {error}"))?;
801 fs::write(path, json).map_err(|error| format!("write {}: {error}", path.display()))?;
802 }
803 Ok(())
804}
805
806async fn append_run_provenance_event(
807 log: &Arc<harn_vm::event_log::AnyEventLog>,
808 kind: &str,
809 payload: serde_json::Value,
810) {
811 let Ok(topic) = harn_vm::event_log::Topic::new("run.provenance") else {
812 return;
813 };
814 let _ = log
815 .append(&topic, harn_vm::event_log::LogEvent::new(kind, payload))
816 .await;
817}
818
819async fn emit_run_attestation(
820 log: &Arc<harn_vm::event_log::AnyEventLog>,
821 path: &str,
822 store_base: &Path,
823 started_at_ms: i64,
824 exit_code: i32,
825 options: &RunAttestationOptions,
826 stderr: &mut String,
827) -> Result<(), String> {
828 let finished_at_ms = now_ms();
829 let status = if exit_code == 0 { "success" } else { "failure" };
830 append_run_provenance_event(
831 log,
832 "finished",
833 serde_json::json!({
834 "pipeline": path,
835 "status": status,
836 "exit_code": exit_code,
837 }),
838 )
839 .await;
840 log.flush()
841 .await
842 .map_err(|error| format!("failed to flush attestation event log: {error}"))?;
843 let secret_provider = harn_vm::secrets::configured_default_chain("harn.provenance")
844 .map_err(|error| format!("failed to configure provenance secrets: {error}"))?;
845 let (signing_key, key_id) =
846 harn_vm::load_or_generate_agent_signing_key(&secret_provider, options.agent_id.as_deref())
847 .await
848 .map_err(|error| format!("failed to load provenance signing key: {error}"))?;
849 let receipt = harn_vm::build_signed_receipt(
850 log,
851 harn_vm::ReceiptBuildOptions {
852 pipeline: path.to_string(),
853 status: status.to_string(),
854 started_at_ms,
855 finished_at_ms,
856 exit_code,
857 producer_name: "harn-cli".to_string(),
858 producer_version: env!("CARGO_PKG_VERSION").to_string(),
859 },
860 &signing_key,
861 key_id,
862 )
863 .await
864 .map_err(|error| format!("failed to build provenance receipt: {error}"))?;
865 let receipt_path = receipt_output_path(store_base, options, &receipt.receipt_id);
866 if let Some(parent) = receipt_path.parent() {
867 fs::create_dir_all(parent)
868 .map_err(|error| format!("failed to create {}: {error}", parent.display()))?;
869 }
870 let encoded = serde_json::to_vec_pretty(&receipt)
871 .map_err(|error| format!("failed to encode provenance receipt: {error}"))?;
872 fs::write(&receipt_path, encoded)
873 .map_err(|error| format!("failed to write {}: {error}", receipt_path.display()))?;
874 stderr.push_str(&format!("provenance receipt: {}\n", receipt_path.display()));
875 Ok(())
876}
877
878fn receipt_output_path(
879 store_base: &Path,
880 options: &RunAttestationOptions,
881 receipt_id: &str,
882) -> PathBuf {
883 if let Some(path) = options.receipt_out.as_ref() {
884 return path.clone();
885 }
886 harn_vm::runtime_paths::state_root(store_base)
887 .join("receipts")
888 .join(format!("{receipt_id}.json"))
889}
890
891fn now_ms() -> i64 {
892 std::time::SystemTime::now()
893 .duration_since(std::time::UNIX_EPOCH)
894 .map(|duration| duration.as_millis() as i64)
895 .unwrap_or(0)
896}
897
898fn exit_code_from_return_value(value: &harn_vm::VmValue) -> i32 {
905 use harn_vm::VmValue;
906 match value {
907 VmValue::Int(n) => (*n).clamp(0, 255) as i32,
908 VmValue::EnumVariant {
909 enum_name,
910 variant,
911 fields,
912 } if enum_name.as_ref() == "Result" && variant.as_ref() == "Err" => 1,
913 _ => 0,
914 }
915}
916
917fn render_return_value_error(value: &harn_vm::VmValue) -> String {
918 let harn_vm::VmValue::EnumVariant {
919 enum_name,
920 variant,
921 fields,
922 } = value
923 else {
924 return String::new();
925 };
926 if enum_name.as_ref() != "Result" || variant.as_ref() != "Err" {
927 return String::new();
928 }
929 let rendered = fields.first().map(|p| p.display()).unwrap_or_default();
930 if rendered.is_empty() {
931 "error\n".to_string()
932 } else if rendered.ends_with('\n') {
933 rendered
934 } else {
935 format!("{rendered}\n")
936 }
937}
938
939pub(crate) async fn connect_mcp_servers(
948 servers: &[package::McpServerConfig],
949 vm: &mut harn_vm::Vm,
950) {
951 use std::collections::BTreeMap;
952 use std::rc::Rc;
953 use std::time::Duration;
954
955 let mut mcp_dict: BTreeMap<String, harn_vm::VmValue> = BTreeMap::new();
956 let mut registrations: Vec<harn_vm::RegisteredMcpServer> = Vec::new();
957
958 for server in servers {
959 let resolved_auth = match mcp::resolve_auth_for_server(server).await {
960 Ok(resolution) => resolution,
961 Err(error) => {
962 eprintln!(
963 "warning: mcp: failed to load auth for '{}': {}",
964 server.name, error
965 );
966 AuthResolution::None
967 }
968 };
969 let spec = serde_json::json!({
970 "name": server.name,
971 "transport": server.transport.clone().unwrap_or_else(|| "stdio".to_string()),
972 "command": server.command,
973 "args": server.args,
974 "env": server.env,
975 "url": server.url,
976 "auth_token": match resolved_auth {
977 AuthResolution::Bearer(token) => Some(token),
978 AuthResolution::None => server.auth_token.clone(),
979 },
980 "protocol_version": server.protocol_version,
981 "proxy_server_name": server.proxy_server_name,
982 });
983
984 registrations.push(harn_vm::RegisteredMcpServer {
987 name: server.name.clone(),
988 spec: spec.clone(),
989 lazy: server.lazy,
990 card: server.card.clone(),
991 keep_alive: server.keep_alive_ms.map(Duration::from_millis),
992 });
993
994 if server.lazy {
995 eprintln!(
996 "[harn] mcp: deferred '{}' (lazy, boots on first use)",
997 server.name
998 );
999 continue;
1000 }
1001
1002 match harn_vm::connect_mcp_server_from_json(&spec).await {
1003 Ok(handle) => {
1004 eprintln!("[harn] mcp: connected to '{}'", server.name);
1005 harn_vm::mcp_install_active(&server.name, handle.clone());
1006 mcp_dict.insert(server.name.clone(), harn_vm::VmValue::McpClient(handle));
1007 }
1008 Err(e) => {
1009 eprintln!(
1010 "warning: mcp: failed to connect to '{}': {}",
1011 server.name, e
1012 );
1013 }
1014 }
1015 }
1016
1017 harn_vm::mcp_register_servers(registrations);
1020
1021 if !mcp_dict.is_empty() {
1022 vm.set_global("mcp", harn_vm::VmValue::Dict(Rc::new(mcp_dict)));
1023 }
1024}
1025
1026pub(crate) fn render_trace_summary() -> String {
1027 use std::fmt::Write;
1028 let entries = harn_vm::llm::take_trace();
1029 if entries.is_empty() {
1030 return String::new();
1031 }
1032 let mut out = String::new();
1033 let _ = writeln!(out, "\n\x1b[2m─── LLM trace ───\x1b[0m");
1034 let mut total_input = 0i64;
1035 let mut total_output = 0i64;
1036 let mut total_ms = 0u64;
1037 for (i, entry) in entries.iter().enumerate() {
1038 let _ = writeln!(
1039 out,
1040 " #{}: {} | {} in + {} out tokens | {} ms",
1041 i + 1,
1042 entry.model,
1043 entry.input_tokens,
1044 entry.output_tokens,
1045 entry.duration_ms,
1046 );
1047 total_input += entry.input_tokens;
1048 total_output += entry.output_tokens;
1049 total_ms += entry.duration_ms;
1050 }
1051 let total_tokens = total_input + total_output;
1052 let cost = (total_input as f64 * 3.0 + total_output as f64 * 15.0) / 1_000_000.0;
1054 let _ = writeln!(
1055 out,
1056 " \x1b[1m{} call{}, {} tokens ({}in + {}out), {} ms, ~${:.4}\x1b[0m",
1057 entries.len(),
1058 if entries.len() == 1 { "" } else { "s" },
1059 total_tokens,
1060 total_input,
1061 total_output,
1062 total_ms,
1063 cost,
1064 );
1065 out
1066}
1067
1068pub(crate) async fn run_file_mcp_serve(
1082 path: &str,
1083 card_source: Option<&str>,
1084 mode: RunFileMcpServeMode,
1085) {
1086 let (source, program) = crate::parse_source_file(path);
1087
1088 let type_diagnostics = typecheck_with_imports(&program, Path::new(path), &source);
1089 for diag in &type_diagnostics {
1090 match diag.severity {
1091 DiagnosticSeverity::Error => {
1092 let rendered = harn_parser::diagnostic::render_type_diagnostic(&source, path, diag);
1093 eprint!("{rendered}");
1094 process::exit(1);
1095 }
1096 DiagnosticSeverity::Warning => {
1097 let rendered = harn_parser::diagnostic::render_type_diagnostic(&source, path, diag);
1098 eprint!("{rendered}");
1099 }
1100 }
1101 }
1102
1103 let chunk = match harn_vm::Compiler::new().compile(&program) {
1104 Ok(c) => c,
1105 Err(e) => {
1106 eprintln!("error: compile error: {e}");
1107 process::exit(1);
1108 }
1109 };
1110
1111 let mut vm = harn_vm::Vm::new();
1112 harn_vm::register_vm_stdlib(&mut vm);
1113 crate::install_default_hostlib(&mut vm);
1114 let source_parent = std::path::Path::new(path)
1115 .parent()
1116 .unwrap_or(std::path::Path::new("."));
1117 let project_root = harn_vm::stdlib::process::find_project_root(source_parent);
1118 let store_base = project_root.as_deref().unwrap_or(source_parent);
1119 harn_vm::register_store_builtins(&mut vm, store_base);
1120 harn_vm::register_metadata_builtins(&mut vm, store_base);
1121 let pipeline_name = std::path::Path::new(path)
1122 .file_stem()
1123 .and_then(|s| s.to_str())
1124 .unwrap_or("default");
1125 harn_vm::register_checkpoint_builtins(&mut vm, store_base, pipeline_name);
1126 vm.set_source_info(path, &source);
1127 if let Some(ref root) = project_root {
1128 vm.set_project_root(root);
1129 }
1130 if let Some(p) = std::path::Path::new(path).parent() {
1131 if !p.as_os_str().is_empty() {
1132 vm.set_source_dir(p);
1133 }
1134 }
1135
1136 let loaded = load_skills(&SkillLoaderInputs {
1138 cli_dirs: Vec::new(),
1139 source_path: Some(std::path::PathBuf::from(path)),
1140 });
1141 emit_loader_warnings(&loaded.loader_warnings);
1142 install_skills_global(&mut vm, &loaded);
1143
1144 let extensions = package::load_runtime_extensions(Path::new(path));
1145 package::install_runtime_extensions(&extensions);
1146 if let Some(manifest) = extensions.root_manifest.as_ref() {
1147 if !manifest.mcp.is_empty() {
1148 connect_mcp_servers(&manifest.mcp, &mut vm).await;
1149 }
1150 }
1151 if let Err(error) = package::install_manifest_triggers(&mut vm, &extensions).await {
1152 eprintln!("error: failed to install manifest triggers: {error}");
1153 process::exit(1);
1154 }
1155 if let Err(error) = package::install_manifest_hooks(&mut vm, &extensions).await {
1156 eprintln!("error: failed to install manifest hooks: {error}");
1157 process::exit(1);
1158 }
1159
1160 let local = tokio::task::LocalSet::new();
1161 local
1162 .run_until(async {
1163 match vm.execute(&chunk).await {
1164 Ok(_) => {}
1165 Err(e) => {
1166 eprint!("{}", vm.format_runtime_error(&e));
1167 process::exit(1);
1168 }
1169 }
1170
1171 let output = vm.output();
1173 if !output.is_empty() {
1174 eprint!("{output}");
1175 }
1176
1177 let registry = match harn_vm::take_mcp_serve_registry() {
1178 Some(r) => r,
1179 None => {
1180 eprintln!("error: pipeline did not call mcp_serve(registry)");
1181 eprintln!("hint: call mcp_serve(tools) at the end of your pipeline");
1182 process::exit(1);
1183 }
1184 };
1185
1186 let tools = match harn_vm::tool_registry_to_mcp_tools(®istry) {
1187 Ok(t) => t,
1188 Err(e) => {
1189 eprintln!("error: {e}");
1190 process::exit(1);
1191 }
1192 };
1193
1194 let resources = harn_vm::take_mcp_serve_resources();
1195 let resource_templates = harn_vm::take_mcp_serve_resource_templates();
1196 let prompts = harn_vm::take_mcp_serve_prompts();
1197
1198 let server_name = std::path::Path::new(path)
1199 .file_stem()
1200 .and_then(|s| s.to_str())
1201 .unwrap_or("harn")
1202 .to_string();
1203
1204 let mut caps = Vec::new();
1205 if !tools.is_empty() {
1206 caps.push(format!(
1207 "{} tool{}",
1208 tools.len(),
1209 if tools.len() == 1 { "" } else { "s" }
1210 ));
1211 }
1212 let total_resources = resources.len() + resource_templates.len();
1213 if total_resources > 0 {
1214 caps.push(format!(
1215 "{total_resources} resource{}",
1216 if total_resources == 1 { "" } else { "s" }
1217 ));
1218 }
1219 if !prompts.is_empty() {
1220 caps.push(format!(
1221 "{} prompt{}",
1222 prompts.len(),
1223 if prompts.len() == 1 { "" } else { "s" }
1224 ));
1225 }
1226 eprintln!(
1227 "[harn] serve mcp: serving {} as '{server_name}'",
1228 caps.join(", ")
1229 );
1230
1231 let mut server =
1232 harn_vm::McpServer::new(server_name, tools, resources, resource_templates, prompts);
1233 if let Some(source) = card_source {
1234 match resolve_card_source(source) {
1235 Ok(card) => server = server.with_server_card(card),
1236 Err(e) => {
1237 eprintln!("error: --card: {e}");
1238 process::exit(1);
1239 }
1240 }
1241 }
1242 match mode {
1243 RunFileMcpServeMode::Stdio => {
1244 if let Err(e) = server.run(&mut vm).await {
1245 eprintln!("error: MCP server error: {e}");
1246 process::exit(1);
1247 }
1248 }
1249 RunFileMcpServeMode::Http {
1250 options,
1251 auth_policy,
1252 } => {
1253 if let Err(e) = crate::commands::serve::run_script_mcp_http_server(
1254 server,
1255 vm,
1256 options,
1257 auth_policy,
1258 )
1259 .await
1260 {
1261 eprintln!("error: MCP server error: {e}");
1262 process::exit(1);
1263 }
1264 }
1265 }
1266 })
1267 .await;
1268}
1269
1270pub(crate) fn resolve_card_source(source: &str) -> Result<serde_json::Value, String> {
1275 let trimmed = source.trim_start();
1276 if trimmed.starts_with('{') || trimmed.starts_with('[') {
1277 return serde_json::from_str(source).map_err(|e| format!("inline JSON parse error: {e}"));
1278 }
1279 let path = std::path::Path::new(source);
1280 harn_vm::load_server_card_from_path(path).map_err(|e| format!("{e}"))
1281}
1282
1283pub(crate) async fn run_watch(path: &str, denied_builtins: HashSet<String>) {
1284 use notify::{Event, EventKind, RecursiveMode, Watcher};
1285
1286 let abs_path = std::fs::canonicalize(path).unwrap_or_else(|e| {
1287 eprintln!("Error: {e}");
1288 process::exit(1);
1289 });
1290 let watch_dir = abs_path.parent().unwrap_or(Path::new("."));
1291
1292 eprintln!("\x1b[2m[watch] running {path}...\x1b[0m");
1293 run_file(
1294 path,
1295 false,
1296 denied_builtins.clone(),
1297 Vec::new(),
1298 CliLlmMockMode::Off,
1299 None,
1300 RunProfileOptions::default(),
1301 )
1302 .await;
1303
1304 let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(1);
1305 let _watcher = {
1306 let tx = tx.clone();
1307 let mut watcher = notify::recommended_watcher(move |res: Result<Event, _>| {
1308 if let Ok(event) = res {
1309 if matches!(
1310 event.kind,
1311 EventKind::Modify(_) | EventKind::Create(_) | EventKind::Remove(_)
1312 ) {
1313 let has_harn = event
1314 .paths
1315 .iter()
1316 .any(|p| p.extension().is_some_and(|ext| ext == "harn"));
1317 if has_harn {
1318 let _ = tx.blocking_send(());
1319 }
1320 }
1321 }
1322 })
1323 .unwrap_or_else(|e| {
1324 eprintln!("Error setting up file watcher: {e}");
1325 process::exit(1);
1326 });
1327 watcher
1328 .watch(watch_dir, RecursiveMode::Recursive)
1329 .unwrap_or_else(|e| {
1330 eprintln!("Error watching directory: {e}");
1331 process::exit(1);
1332 });
1333 watcher };
1335
1336 eprintln!(
1337 "\x1b[2m[watch] watching {} for .harn changes (ctrl-c to stop)\x1b[0m",
1338 watch_dir.display()
1339 );
1340
1341 loop {
1342 rx.recv().await;
1343 tokio::time::sleep(std::time::Duration::from_millis(200)).await;
1345 while rx.try_recv().is_ok() {}
1346
1347 eprintln!();
1348 eprintln!("\x1b[2m[watch] change detected, re-running {path}...\x1b[0m");
1349 run_file(
1350 path,
1351 false,
1352 denied_builtins.clone(),
1353 Vec::new(),
1354 CliLlmMockMode::Off,
1355 None,
1356 RunProfileOptions::default(),
1357 )
1358 .await;
1359 }
1360}
1361
1362#[cfg(test)]
1363mod tests {
1364 use super::{
1365 execute_explain_cost, execute_run, split_eval_header, CliLlmMockMode, RunProfileOptions,
1366 StdoutPassthroughGuard,
1367 };
1368 use std::collections::HashSet;
1369
1370 #[test]
1371 fn split_eval_header_no_imports_returns_full_body() {
1372 let (header, body) = split_eval_header("println(1 + 2)");
1373 assert_eq!(header, "");
1374 assert_eq!(body, "println(1 + 2)");
1375 }
1376
1377 #[test]
1378 fn split_eval_header_lifts_leading_imports() {
1379 let code = "import \"./lib\"\nimport { x } from \"std/math\"\nprintln(x)";
1380 let (header, body) = split_eval_header(code);
1381 assert_eq!(header, "import \"./lib\"\nimport { x } from \"std/math\"");
1382 assert_eq!(body, "println(x)");
1383 }
1384
1385 #[test]
1386 fn split_eval_header_keeps_pub_import_and_comments_in_header() {
1387 let code = "// header comment\npub import { y } from \"./lib\"\n\nfoo()";
1388 let (header, body) = split_eval_header(code);
1389 assert_eq!(
1390 header,
1391 "// header comment\npub import { y } from \"./lib\"\n"
1392 );
1393 assert_eq!(body, "foo()");
1394 }
1395
1396 #[test]
1397 fn split_eval_header_does_not_lift_imports_after_other_statements() {
1398 let code = "let a = 1\nimport \"./lib\"";
1399 let (header, body) = split_eval_header(code);
1400 assert_eq!(header, "");
1401 assert_eq!(body, "let a = 1\nimport \"./lib\"");
1402 }
1403
1404 #[test]
1405 fn cli_llm_mock_roundtrips_logprobs() {
1406 let mock = harn_vm::llm::parse_llm_mock_value(&serde_json::json!({
1407 "text": "visible",
1408 "logprobs": [{"token": "visible", "logprob": 0.0}]
1409 }))
1410 .expect("parse mock");
1411 assert_eq!(mock.logprobs.len(), 1);
1412
1413 let line = harn_vm::llm::serialize_llm_mock(mock).expect("serialize mock");
1414 let value: serde_json::Value = serde_json::from_str(&line).expect("json line");
1415 assert_eq!(value["logprobs"][0]["token"].as_str(), Some("visible"));
1416
1417 let reparsed = harn_vm::llm::parse_llm_mock_value(&value).expect("reparse mock");
1418 assert_eq!(reparsed.logprobs.len(), 1);
1419 assert_eq!(reparsed.logprobs[0]["logprob"].as_f64(), Some(0.0));
1420 }
1421
1422 #[test]
1423 fn stdout_passthrough_guard_restores_previous_state() {
1424 let original = harn_vm::set_stdout_passthrough(false);
1425 {
1426 let _guard = StdoutPassthroughGuard::enable();
1427 assert!(harn_vm::set_stdout_passthrough(true));
1428 }
1429 assert!(!harn_vm::set_stdout_passthrough(original));
1430 }
1431
1432 #[test]
1433 fn execute_explain_cost_does_not_execute_script() {
1434 let temp = tempfile::TempDir::new().expect("temp dir");
1435 let script = temp.path().join("main.harn");
1436 std::fs::write(
1437 &script,
1438 r#"
1439pipeline main() {
1440 write_file("executed.txt", "bad")
1441 llm_call("hello", nil, {provider: "mock", model: "mock"})
1442}
1443"#,
1444 )
1445 .expect("write script");
1446
1447 let outcome = execute_explain_cost(&script.to_string_lossy());
1448
1449 assert_eq!(outcome.exit_code, 0, "stderr:\n{}", outcome.stderr);
1450 assert!(outcome.stdout.contains("LLM cost estimate"));
1451 assert!(
1452 !temp.path().join("executed.txt").exists(),
1453 "--explain-cost must not execute pipeline side effects"
1454 );
1455 }
1456
1457 #[cfg(feature = "hostlib")]
1458 #[tokio::test]
1459 async fn execute_run_installs_hostlib_gate() {
1460 let temp = tempfile::NamedTempFile::new().expect("temp file");
1461 std::fs::write(
1462 temp.path(),
1463 r#"
1464pipeline main() {
1465 let _ = hostlib_enable("tools:deterministic")
1466 println("enabled")
1467}
1468"#,
1469 )
1470 .expect("write script");
1471
1472 let outcome = execute_run(
1473 &temp.path().to_string_lossy(),
1474 false,
1475 HashSet::new(),
1476 Vec::new(),
1477 Vec::new(),
1478 CliLlmMockMode::Off,
1479 None,
1480 RunProfileOptions::default(),
1481 )
1482 .await;
1483
1484 assert_eq!(outcome.exit_code, 0, "stderr:\n{}", outcome.stderr);
1485 assert_eq!(outcome.stdout.trim(), "enabled");
1486 }
1487
1488 #[cfg(all(feature = "hostlib", unix))]
1489 #[tokio::test]
1490 async fn execute_run_can_read_hostlib_command_artifacts() {
1491 let temp = tempfile::NamedTempFile::new().expect("temp file");
1492 std::fs::write(
1493 temp.path(),
1494 r#"
1495pipeline main() {
1496 let _ = hostlib_enable("tools:deterministic")
1497 let result = hostlib_tools_run_command({
1498 argv: ["sh", "-c", "i=0; while [ $i -lt 2000 ]; do printf x; i=$((i+1)); done"],
1499 capture: {max_inline_bytes: 8},
1500 timeout_ms: 5000,
1501 })
1502 println(starts_with(result.command_id, "cmd_"))
1503 println(len(result.stdout))
1504 println(result.byte_count)
1505 let window = hostlib_tools_read_command_output({
1506 command_id: result.command_id,
1507 offset: 1990,
1508 length: 20,
1509 })
1510 println(len(window.content))
1511 println(window.eof)
1512}
1513"#,
1514 )
1515 .expect("write script");
1516
1517 let outcome = execute_run(
1518 &temp.path().to_string_lossy(),
1519 false,
1520 HashSet::new(),
1521 Vec::new(),
1522 Vec::new(),
1523 CliLlmMockMode::Off,
1524 None,
1525 RunProfileOptions::default(),
1526 )
1527 .await;
1528
1529 assert_eq!(outcome.exit_code, 0, "stderr:\n{}", outcome.stderr);
1530 assert_eq!(outcome.stdout.trim(), "true\n8\n2000\n10\ntrue");
1531 }
1532}