1#![recursion_limit = "256"]
2
3pub mod acp;
4pub mod cli;
5mod cli_bytecode;
6pub mod commands;
7pub mod config;
8#[doc(hidden)]
9pub mod dispatch;
10pub mod env_guard;
11pub mod format;
12pub mod json_envelope;
13pub mod package;
14mod provider_bootstrap;
15pub mod skill_loader;
16pub mod skill_provenance;
17pub mod test_report;
18pub mod test_runner;
19#[doc(hidden)]
20pub mod tests;
21
22pub use harn_skills::{get_embedded_skill, list_embedded_skills, EmbeddedSkill, SkillFrontmatter};
23
24use clap::{error::ErrorKind, CommandFactory, Parser as ClapParser};
25use std::path::{Path, PathBuf};
26use std::sync::{Arc, Once};
27use std::{env, fs, panic, process, thread};
28
29use cli::{
30 Cli, Command, CompletionShell, EvalCommand, MergeCaptainCommand, MergeCaptainMockCommand,
31 ModelInfoArgs, PackageArtifactsCommand, PackageCacheCommand, PackageCommand,
32 PackageScaffoldCommand, PersonaCommand, PersonaSupervisionCommand, PgCommand, ProvidersCommand,
33 RunsCommand, ServeCommand, SkillCommand, SkillKeyCommand, SkillTrustCommand, SkillsCommand,
34 TimeCommand, ToolCommand,
35};
36use harn_lexer::Lexer;
37use harn_parser::{DiagnosticSeverity, Parser, TypeChecker};
38
39pub const CLI_RUNTIME_STACK_SIZE: usize = 16 * 1024 * 1024;
40
41static BROKEN_PIPE_PANIC_HOOK: Once = Once::new();
42
43pub(crate) fn ensure_builtin_signatures_installed() {
57 harn_parser::install_builtin_signatures(harn_vm::stdlib::all_builtin_signatures());
58}
59
60#[cfg(feature = "hostlib")]
61pub(crate) fn install_default_hostlib(vm: &mut harn_vm::Vm) {
62 let _ = harn_hostlib::install_default(vm);
63}
64
65#[cfg(not(feature = "hostlib"))]
66pub(crate) fn install_default_hostlib(_vm: &mut harn_vm::Vm) {}
67
68pub fn run() {
71 install_broken_pipe_panic_hook();
72
73 harn_vm::stdlib::force_link();
78
79 ensure_builtin_signatures_installed();
80
81 let handle = thread::Builder::new()
82 .name("harn-cli".to_string())
83 .stack_size(CLI_RUNTIME_STACK_SIZE)
84 .spawn(|| {
85 let runtime = tokio::runtime::Builder::new_multi_thread()
86 .enable_all()
87 .build()
88 .unwrap_or_else(|error| {
89 eprintln!("failed to start async runtime: {error}");
90 process::exit(1);
91 });
92 runtime.block_on(async_main());
93 if let Err(error) = harn_vm::events::shutdown_otel_sink() {
100 eprintln!("[harn] OTel exporter shutdown failed: {error}");
101 }
102 })
103 .unwrap_or_else(|error| {
104 eprintln!("failed to start CLI runtime thread: {error}");
105 process::exit(1);
106 });
107
108 if let Err(payload) = handle.join() {
109 if is_broken_pipe_panic_payload(payload.as_ref()) {
110 process::exit(0);
111 }
112 std::panic::resume_unwind(payload);
113 }
114}
115
116fn install_broken_pipe_panic_hook() {
117 BROKEN_PIPE_PANIC_HOOK.call_once(|| {
118 let previous = panic::take_hook();
119 panic::set_hook(Box::new(move |info| {
120 if is_broken_pipe_panic_payload(info.payload()) {
121 return;
122 }
123 previous(info);
124 }));
125 });
126}
127
128fn is_broken_pipe_panic_payload(payload: &(dyn std::any::Any + Send)) -> bool {
129 let message = if let Some(message) = payload.downcast_ref::<String>() {
130 message.as_str()
131 } else if let Some(message) = payload.downcast_ref::<&str>() {
132 message
133 } else {
134 return false;
135 };
136
137 let print_failure = message.contains("failed printing to stdout")
138 || message.contains("failed printing to stderr");
139 let broken_pipe = message.contains("Broken pipe")
140 || message.contains("os error 32")
141 || message.contains("EPIPE");
142 print_failure && broken_pipe
143}
144
145#[allow(clippy::large_stack_frames)] async fn async_main() {
147 if let Err(error) = harn_vm::events::install_otel_sink_from_env() {
154 eprintln!("[harn] OTel exporter disabled: {error}");
155 }
156
157 let raw_args = normalize_serve_args(env::args().collect());
158 if raw_args.len() == 2 && raw_args[1].ends_with(".harn") {
159 provider_bootstrap::maybe_seed_ollama_for_run_file(Path::new(&raw_args[1]), false, false)
160 .await;
161 commands::run::run_file(
162 &raw_args[1],
163 false,
164 std::collections::HashSet::new(),
165 Vec::new(),
166 commands::run::CliLlmMockMode::Off,
167 None,
168 commands::run::RunProfileOptions::default(),
169 )
170 .await;
171 return;
172 }
173
174 let cli = match Cli::try_parse_from(&raw_args) {
175 Ok(cli) => cli,
176 Err(error) => {
177 if matches!(
178 error.kind(),
179 ErrorKind::DisplayHelp | ErrorKind::DisplayVersion
180 ) {
181 error.exit();
182 }
183 error.exit();
184 }
185 };
186
187 if cli.json_schemas {
188 commands::json_schemas::run(cli.schema_command.as_deref());
189 return;
190 }
191
192 let Some(subcommand) = cli.command else {
193 let mut cmd = Cli::command();
197 cmd.print_help().ok();
198 return;
199 };
200 match subcommand {
201 Command::Version(args) => {
202 let exit = run_version(args).await;
203 if exit != 0 {
204 process::exit(exit);
205 }
206 }
207 Command::Upgrade(args) => {
208 if let Err(error) = commands::upgrade::run(args).await {
209 eprintln!("error: {error}");
210 process::exit(1);
211 }
212 }
213 Command::Skill(args) => match args.command {
214 SkillCommand::Key(key_args) => match key_args.command {
215 SkillKeyCommand::Generate(generate) => commands::skill::run_key_generate(&generate),
216 },
217 SkillCommand::Sign(sign) => commands::skill::run_sign(&sign),
218 SkillCommand::Endorse(endorse) => commands::skill::run_endorse(&endorse),
219 SkillCommand::Verify(verify) => commands::skill::run_verify(&verify),
220 SkillCommand::WhoSigned(who_signed) => {
221 commands::skill::run_who_signed(&who_signed).await;
222 }
223 SkillCommand::Trust(trust_args) => match trust_args.command {
224 SkillTrustCommand::Add(add) => commands::skill::run_trust_add(&add),
225 SkillTrustCommand::List(list) => commands::skill::run_trust_list(&list),
226 },
227 SkillCommand::New(new_args) => commands::skills::run_new(&new_args),
228 },
229 Command::Run(args) => {
230 if !args.explain_cost {
231 match (args.eval.as_deref(), args.file.as_deref()) {
232 (Some(code), None) => {
233 provider_bootstrap::maybe_seed_ollama_for_inline(
234 code,
235 args.yes,
236 args.llm_mock.is_some(),
237 )
238 .await;
239 }
240 (None, Some(file)) => {
241 provider_bootstrap::maybe_seed_ollama_for_run_file(
242 Path::new(file),
243 args.yes,
244 args.llm_mock.is_some(),
245 )
246 .await;
247 }
248 _ => {}
249 }
250 }
251 let denied =
252 commands::run::build_denied_builtins(args.deny.as_deref(), args.allow.as_deref());
253 let llm_mock_mode = if let Some(path) = args.llm_mock.as_ref() {
254 commands::run::CliLlmMockMode::Replay {
255 fixture_path: PathBuf::from(path),
256 }
257 } else if let Some(path) = args.llm_mock_record.as_ref() {
258 commands::run::CliLlmMockMode::Record {
259 fixture_path: PathBuf::from(path),
260 }
261 } else {
262 commands::run::CliLlmMockMode::Off
263 };
264 let attestation = args.attest.then(|| commands::run::RunAttestationOptions {
265 receipt_out: args.receipt_out.as_ref().map(PathBuf::from),
266 agent_id: args.attest_agent.clone(),
267 });
268 let profile_options = run_profile_options(&args.profile);
269 let sandbox_options = if args.no_sandbox {
270 commands::run::RunSandboxOptions::disabled()
271 } else {
272 commands::run::RunSandboxOptions::default()
273 };
274 let json_options = args
275 .json
276 .then_some(commands::run::RunJsonOptions { quiet: args.quiet });
277 let aux_options = commands::run::run_aux_options_from_args(&args);
278 let harnpack_options = commands::run::harnpack::HarnpackRunOptions {
279 allow_unsigned: args.allow_unsigned,
280 dry_run_verify: args.dry_run_verify,
281 };
282
283 if let Some(resume_target) = args.resume.as_deref() {
284 commands::run::run_resume_with_skill_dirs(
285 resume_target,
286 args.trace,
287 denied,
288 args.argv.clone(),
289 args.skill_dir.clone(),
290 llm_mock_mode,
291 attestation,
292 profile_options,
293 sandbox_options.clone(),
294 json_options,
295 aux_options,
296 )
297 .await;
298 return;
299 }
300
301 match (args.eval.as_deref(), args.file.as_deref()) {
302 (Some(code), None) => {
303 if args.allow_unsigned || args.dry_run_verify {
304 command_error(
305 "`--allow-unsigned` and `--dry-run-verify` apply to `.harnpack` inputs; \
306 they cannot be combined with `-e`",
307 );
308 }
309 let (wrapped, tmp) = commands::run::prepare_eval_temp_file(code)
310 .unwrap_or_else(|e| command_error(&e));
311 let tmp_path: PathBuf = tmp.path().to_path_buf();
312 fs::write(&tmp_path, &wrapped).unwrap_or_else(|e| {
313 command_error(&format!("failed to write temp file for -e: {e}"))
314 });
315 let tmp_str = tmp_path.to_string_lossy().into_owned();
316 if args.explain_cost {
317 commands::run::run_explain_cost_file_with_skill_dirs(&tmp_str);
318 } else {
319 commands::run::run_file_with_skill_dirs(
320 &tmp_str,
321 args.trace,
322 denied,
323 args.argv.clone(),
324 args.skill_dir.clone(),
325 llm_mock_mode.clone(),
326 attestation.clone(),
327 profile_options.clone(),
328 sandbox_options.clone(),
329 json_options.clone(),
330 aux_options.clone(),
331 harnpack_options.clone(),
332 )
333 .await;
334 }
335 drop(tmp);
336 }
337 (None, Some(file)) => {
338 if args.explain_cost {
339 commands::run::run_explain_cost_file_with_skill_dirs(file);
340 } else {
341 commands::run::run_file_with_skill_dirs(
342 file,
343 args.trace,
344 denied,
345 args.argv.clone(),
346 args.skill_dir.clone(),
347 llm_mock_mode,
348 attestation,
349 profile_options,
350 sandbox_options,
351 json_options,
352 aux_options,
353 harnpack_options,
354 )
355 .await;
356 }
357 }
358 (Some(_), Some(_)) => command_error(
359 "`harn run` accepts either `-e <code>` or `<file.harn>`, not both",
360 ),
361 (None, None) => command_error(
362 "`harn run` requires `--resume <snapshot>`, `-e <code>`, or `<file.harn>`",
363 ),
364 }
365 }
366 Command::Check(args) => {
367 let json_format_alias =
368 !args.json && matches!(args.format, cli::CheckOutputFormat::Json);
369 let matrix_format = if args.json {
370 if !matches!(args.format, cli::CheckOutputFormat::Text) {
371 command_error("`harn check` accepts either `--json` or `--format`, not both");
372 }
373 cli::CheckOutputFormat::Json
374 } else {
375 args.format
376 };
377 if args.provider_matrix {
378 let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
379 let extensions = package::load_runtime_extensions(&cwd);
380 package::install_runtime_extensions(&extensions);
381 commands::check::provider_matrix::run(
382 matrix_format,
383 args.filter.as_deref(),
384 json_format_alias,
385 );
386 return;
387 }
388 if args.connector_matrix {
389 commands::check::connector_matrix::run(
390 matrix_format,
391 args.filter.as_deref(),
392 &args.targets,
393 json_format_alias,
394 );
395 return;
396 }
397 let mut target_strings: Vec<String> = args.targets.clone();
398 if args.workspace {
399 let anchor = target_strings.first().map(Path::new);
400 match package::load_workspace_config(anchor) {
401 Some((workspace, manifest_dir)) if !workspace.pipelines.is_empty() => {
402 for pipeline in &workspace.pipelines {
403 let candidate = Path::new(pipeline);
404 let resolved = if candidate.is_absolute() {
405 candidate.to_path_buf()
406 } else {
407 manifest_dir.join(candidate)
408 };
409 target_strings.push(resolved.to_string_lossy().into_owned());
410 }
411 }
412 Some(_) => command_error(
413 "--workspace requires `[workspace].pipelines` in the nearest harn.toml",
414 ),
415 None => command_error(
416 "--workspace could not find a harn.toml walking up from the target(s)",
417 ),
418 }
419 }
420 if target_strings.is_empty() {
421 if args.json {
422 print_check_error(
423 "missing_targets",
424 "`harn check` requires at least one target path, or `--workspace` with `[workspace].pipelines`",
425 );
426 }
427 command_error(
428 "`harn check` requires at least one target path, or `--workspace` with `[workspace].pipelines`",
429 );
430 }
431 for target in &target_strings {
432 if let Err(error) = package::validate_runtime_manifest_extensions(Path::new(target))
433 {
434 if args.json {
435 print_check_error(
436 "manifest_extension_error",
437 &format!("manifest extension validation failed: {error}"),
438 );
439 }
440 command_error(&format!("manifest extension validation failed: {error}"));
441 }
442 }
443 let targets: Vec<&str> = target_strings.iter().map(String::as_str).collect();
444 let files = commands::check::collect_harn_targets(&targets);
445 if files.is_empty() {
446 if args.json {
447 print_check_error(
448 "no_harn_files",
449 "no .harn files found under the given target(s)",
450 );
451 }
452 command_error("no .harn files found under the given target(s)");
453 }
454 let module_graph = commands::check::build_module_graph(&files);
455 let cross_file_imports = commands::check::collect_cross_file_imports(&module_graph);
456 let mut analysis = harn_parser::analysis::AnalysisDatabase::new();
457 let mut should_fail = false;
458 let mut json_files = Vec::new();
459 for file in &files {
460 let mut config = package::load_check_config(Some(file));
461 if let Some(path) = args.host_capabilities.as_ref() {
462 config.host_capabilities_path = Some(path.clone());
463 }
464 if let Some(path) = args.bundle_root.as_ref() {
465 config.bundle_root = Some(path.clone());
466 }
467 if args.strict_types {
468 config.strict_types = true;
469 }
470 if let Some(sev) = args.preflight.as_deref() {
471 config.preflight_severity = Some(sev.to_string());
472 }
473 if args.json {
474 let report = commands::check::check_file_report(
475 &mut analysis,
476 file,
477 &config,
478 &cross_file_imports,
479 &module_graph,
480 args.invariants,
481 );
482 should_fail |= report.outcome().should_fail(config.strict);
483 json_files.push(report);
484 } else {
485 let outcome = commands::check::check_file_inner(
486 &mut analysis,
487 file,
488 &config,
489 &cross_file_imports,
490 &module_graph,
491 args.invariants,
492 );
493 should_fail |= outcome.should_fail(config.strict);
494 }
495 }
496 if args.json {
497 let report = commands::check::CheckReport::from_files(json_files);
498 let envelope = if should_fail {
499 json_envelope::JsonEnvelope {
500 schema_version: commands::check::CHECK_SCHEMA_VERSION,
501 ok: false,
502 data: Some(report),
503 error: Some(json_envelope::JsonError {
504 code: "check_failed".to_string(),
505 message: "one or more files failed `harn check`".to_string(),
506 details: serde_json::Value::Null,
507 }),
508 warnings: Vec::new(),
509 }
510 } else {
511 json_envelope::JsonEnvelope::ok(commands::check::CHECK_SCHEMA_VERSION, report)
512 };
513 println!("{}", json_envelope::to_string_pretty(&envelope));
514 if should_fail {
515 process::exit(1);
516 }
517 return;
518 }
519 if should_fail {
520 process::exit(1);
521 }
522 }
523 Command::Parse(args) => {
524 if let Err(error) = commands::parse_tokens::run_parse(&args) {
525 command_error(&error);
526 }
527 }
528 Command::Tokens(args) => {
529 if let Err(error) = commands::parse_tokens::run_tokens(&args) {
530 command_error(&error);
531 }
532 }
533 Command::Config(args) => {
534 if let Err(error) = commands::config_cmd::run(args).await {
535 command_error(&error);
536 }
537 }
538 Command::Explain(args) => {
539 let code = commands::explain::run_explain(&args).await;
540 if code != 0 {
541 process::exit(code);
542 }
543 }
544 Command::Fix(args) => {
545 if let Err(error) = commands::fix::run(&args) {
546 if error.is_partial_failure() {
547 eprintln!("error: {}", error.message());
548 process::exit(1);
549 }
550 command_error(error.message());
551 }
552 }
553 Command::Contracts(args) => {
554 commands::contracts::handle_contracts_command(args).await;
555 }
556 Command::Connect(args) => {
557 commands::connect::run_connect(*args).await;
558 }
559 Command::Lint(args) => {
560 let targets: Vec<&str> = args.targets.iter().map(String::as_str).collect();
561 let files = commands::check::collect_harn_targets(&targets);
562 let prompt_files = commands::check::collect_prompt_targets(&targets);
563 if files.is_empty() && prompt_files.is_empty() {
564 if args.json {
565 print_lint_error(
566 "no_lint_targets",
567 "no .harn or .harn.prompt files found under the given target(s)",
568 );
569 }
570 command_error("no .harn or .harn.prompt files found under the given target(s)");
571 }
572 let module_graph = commands::check::build_module_graph(&files);
573 let cross_file_imports = commands::check::collect_cross_file_imports(&module_graph);
574 let mut analysis = harn_parser::analysis::AnalysisDatabase::new();
575 if args.json {
576 let mut should_fail = false;
581 let mut json_files: Vec<commands::check::LintFileReport> = Vec::new();
582 for file in &files {
583 let mut config = package::load_check_config(Some(file));
584 commands::check::apply_harn_lint_config(file, &mut config);
585 let require_header = args.require_file_header
586 || commands::check::harn_lint_require_file_header(file);
587 let complexity_threshold =
588 commands::check::harn_lint_complexity_threshold(file);
589 let persona_step_allowlist =
590 commands::check::harn_lint_persona_step_allowlist(file);
591 let report = commands::check::lint_file_report(
592 &mut analysis,
593 file,
594 &config,
595 &cross_file_imports,
596 &module_graph,
597 require_header,
598 complexity_threshold,
599 &persona_step_allowlist,
600 );
601 should_fail |= report.outcome().should_fail(config.strict);
602 json_files.push(report);
603 }
604 let report = commands::check::LintReport::from_files(json_files);
605 let envelope = if should_fail {
606 json_envelope::JsonEnvelope {
607 schema_version: commands::check::LINT_SCHEMA_VERSION,
608 ok: false,
609 data: Some(report),
610 error: Some(json_envelope::JsonError {
611 code: "lint_failed".to_string(),
612 message: "one or more files failed `harn lint`".to_string(),
613 details: serde_json::Value::Null,
614 }),
615 warnings: Vec::new(),
616 }
617 } else {
618 json_envelope::JsonEnvelope::ok(commands::check::LINT_SCHEMA_VERSION, report)
619 };
620 println!("{}", json_envelope::to_string_pretty(&envelope));
621 if should_fail {
622 process::exit(1);
623 }
624 return;
625 }
626 if args.fix {
627 for file in &files {
628 let mut config = package::load_check_config(Some(file));
629 commands::check::apply_harn_lint_config(file, &mut config);
630 let require_header = args.require_file_header
631 || commands::check::harn_lint_require_file_header(file);
632 let complexity_threshold =
633 commands::check::harn_lint_complexity_threshold(file);
634 let persona_step_allowlist =
635 commands::check::harn_lint_persona_step_allowlist(file);
636 commands::check::lint_fix_file(
637 &mut analysis,
638 file,
639 &config,
640 &cross_file_imports,
641 &module_graph,
642 require_header,
643 complexity_threshold,
644 &persona_step_allowlist,
645 );
646 }
647 for file in &prompt_files {
648 let threshold =
649 commands::check::harn_lint_template_variant_branch_threshold(file);
650 let disabled = commands::check::harn_lint_disabled_rules(file);
651 commands::check::lint_prompt_file_inner(file, threshold, &disabled);
656 }
657 } else {
658 let mut should_fail = false;
659 for file in &files {
660 let mut config = package::load_check_config(Some(file));
661 commands::check::apply_harn_lint_config(file, &mut config);
662 let require_header = args.require_file_header
663 || commands::check::harn_lint_require_file_header(file);
664 let complexity_threshold =
665 commands::check::harn_lint_complexity_threshold(file);
666 let persona_step_allowlist =
667 commands::check::harn_lint_persona_step_allowlist(file);
668 let outcome = commands::check::lint_file_inner(
669 &mut analysis,
670 file,
671 &config,
672 &cross_file_imports,
673 &module_graph,
674 require_header,
675 complexity_threshold,
676 &persona_step_allowlist,
677 );
678 should_fail |= outcome.should_fail(config.strict);
679 }
680 for file in &prompt_files {
681 let threshold =
682 commands::check::harn_lint_template_variant_branch_threshold(file);
683 let disabled = commands::check::harn_lint_disabled_rules(file);
684 let config = package::load_check_config(Some(file));
685 let outcome =
686 commands::check::lint_prompt_file_inner(file, threshold, &disabled);
687 should_fail |= outcome.should_fail(config.strict);
688 }
689 if should_fail {
690 process::exit(1);
691 }
692 }
693 }
694 Command::Fmt(args) => {
695 let targets: Vec<&str> = args.targets.iter().map(String::as_str).collect();
696 let anchor = targets.first().map(Path::new).unwrap_or(Path::new("."));
699 let loaded = match config::load_for_path(anchor) {
700 Ok(c) => c,
701 Err(e) => {
702 eprintln!("warning: {e}");
703 config::HarnConfig::default()
704 }
705 };
706 let mut opts = harn_fmt::FmtOptions::default();
707 if let Some(w) = loaded.fmt.line_width {
708 opts.line_width = w;
709 }
710 if let Some(w) = loaded.fmt.separator_width {
711 opts.separator_width = w;
712 }
713 if let Some(w) = args.line_width {
714 opts.line_width = w;
715 }
716 if let Some(w) = args.separator_width {
717 opts.separator_width = w;
718 }
719 let mode = commands::check::FmtMode::from_check_flag(args.check);
720 if args.json {
721 let envelope = commands::check::fmt_targets_json(&targets, mode, &opts);
722 let failed = !envelope.ok;
723 println!("{}", json_envelope::to_string_pretty(&envelope));
724 if failed {
725 process::exit(1);
726 }
727 } else {
728 commands::check::fmt_targets(&targets, mode, &opts);
729 }
730 }
731 Command::Test(args) => {
732 if args.watch && (args.junit.is_some() || args.json_out.is_some()) {
733 command_error(
734 "`harn test --watch` cannot combine with --junit or --json-out; the watch loop never terminates so the report would never be written",
735 );
736 }
737 if args.target.as_deref() == Some("agents-conformance") {
738 if args.selection.is_some() {
739 command_error(
740 "`harn test agents-conformance` does not accept a second positional target; use --category instead",
741 );
742 }
743 if args.evals || args.determinism || args.record || args.replay || args.watch {
744 command_error(
745 "`harn test agents-conformance` cannot be combined with --evals, --determinism, --record, --replay, or --watch",
746 );
747 }
748 let Some(target_url) = args.agents_target.clone() else {
749 command_error("`harn test agents-conformance` requires --target <url>");
750 };
751 commands::agents_conformance::run_agents_conformance(
752 commands::agents_conformance::AgentsConformanceConfig {
753 target_url,
754 api_key: args.agents_api_key.clone(),
755 categories: args.agents_category.clone(),
756 timeout_ms: args.timeout,
757 verbose: args.verbose,
758 json: args.json,
759 json_out: args.json_out.clone(),
760 workspace_id: args.agents_workspace_id.clone(),
761 session_id: args.agents_session_id.clone(),
762 },
763 )
764 .await;
765 return;
766 }
767 if args.target.as_deref() == Some("protocols") {
768 if args.evals || args.determinism || args.record || args.replay || args.watch {
769 command_error(
770 "`harn test protocols` cannot be combined with --evals, --determinism, --record, --replay, or --watch",
771 );
772 }
773 if args.junit.is_some()
774 || args.agents_target.is_some()
775 || args.agents_api_key.is_some()
776 || !args.agents_category.is_empty()
777 || args.json
778 || args.json_out.is_some()
779 || args.agents_workspace_id.is_some()
780 || args.agents_session_id.is_some()
781 || args.parallel
782 || !args.skill_dir.is_empty()
783 {
784 command_error(
785 "`harn test protocols` accepts only --filter, --verbose, --timing, and an optional fixture selection",
786 );
787 }
788 commands::protocol_conformance::run_protocol_conformance(
789 args.selection.as_deref(),
790 args.filter.as_deref(),
791 args.verbose || args.timing,
792 );
793 return;
794 }
795 if args.evals {
796 if args.determinism || args.record || args.replay || args.watch {
797 command_error("--evals cannot be combined with --determinism, --record, --replay, or --watch");
798 }
799 if args.target.as_deref() != Some("package") || args.selection.is_some() {
800 command_error("package evals are run with `harn test package --evals`");
801 }
802 run_package_evals();
803 } else if args.determinism {
804 let cli_skill_dirs: Vec<PathBuf> =
805 args.skill_dir.iter().map(PathBuf::from).collect();
806 if args.watch {
807 command_error("--determinism cannot be combined with --watch");
808 }
809 if args.record || args.replay {
810 command_error("--determinism manages its own record/replay cycle");
811 }
812 if let Some(t) = args.target.as_deref() {
813 if t == "conformance" {
814 commands::test::run_conformance_determinism_tests(
815 t,
816 args.selection.as_deref(),
817 args.filter.as_deref(),
818 args.timeout,
819 &cli_skill_dirs,
820 )
821 .await;
822 } else if args.selection.is_some() {
823 command_error(
824 "only `harn test conformance` accepts a second positional target",
825 );
826 } else {
827 commands::test::run_determinism_tests(
828 t,
829 args.filter.as_deref(),
830 args.timeout,
831 &cli_skill_dirs,
832 )
833 .await;
834 }
835 } else {
836 let test_dir = if PathBuf::from("tests").is_dir() {
837 "tests".to_string()
838 } else {
839 command_error("no path specified and no tests/ directory found");
840 };
841 if args.selection.is_some() {
842 command_error(
843 "only `harn test conformance` accepts a second positional target",
844 );
845 }
846 commands::test::run_determinism_tests(
847 &test_dir,
848 args.filter.as_deref(),
849 args.timeout,
850 &cli_skill_dirs,
851 )
852 .await;
853 }
854 } else {
855 let cli_skill_dirs: Vec<PathBuf> =
856 args.skill_dir.iter().map(PathBuf::from).collect();
857 if args.record {
858 harn_vm::llm::set_replay_mode(
859 harn_vm::llm::LlmReplayMode::Record,
860 ".harn-fixtures",
861 );
862 } else if args.replay {
863 harn_vm::llm::set_replay_mode(
864 harn_vm::llm::LlmReplayMode::Replay,
865 ".harn-fixtures",
866 );
867 }
868
869 if let Some(t) = args.target.as_deref() {
870 if t == "conformance" {
871 commands::test::run_conformance_tests(
872 t,
873 args.selection.as_deref(),
874 args.filter.as_deref(),
875 args.junit.as_deref(),
876 args.timeout,
877 commands::test::ConformanceRunOptions {
878 verbose: args.verbose,
879 timing: args.timing,
880 differential_optimizations: args.differential_optimizations,
881 json: args.json,
882 cli_skill_dirs: &cli_skill_dirs,
883 },
884 )
885 .await;
886 } else if args.selection.is_some() {
887 command_error(
888 "only `harn test conformance` accepts a second positional target",
889 );
890 } else {
891 let run_args = commands::test::UserTestRunArgs {
892 filter: args.filter.as_deref(),
893 timeout_ms: args.timeout,
894 parallel: args.parallel,
895 jobs: args.jobs,
896 verbose: args.verbose,
897 timing: args.timing,
898 diagnose: args.diagnose,
899 cli_skill_dirs: &cli_skill_dirs,
900 };
901 if args.watch {
902 commands::test::run_watch_tests(t, run_args).await;
903 } else {
904 commands::test::run_user_tests(
905 t,
906 run_args,
907 commands::test::UserTestReportConfig {
908 junit_path: args.junit.as_deref(),
909 json_out_path: args.json_out.as_deref(),
910 },
911 )
912 .await;
913 }
914 }
915 } else {
916 let test_dir = if PathBuf::from("tests").is_dir() {
917 "tests".to_string()
918 } else {
919 command_error("no path specified and no tests/ directory found");
920 };
921 if args.selection.is_some() {
922 command_error(
923 "only `harn test conformance` accepts a second positional target",
924 );
925 }
926 let run_args = commands::test::UserTestRunArgs {
927 filter: args.filter.as_deref(),
928 timeout_ms: args.timeout,
929 parallel: args.parallel,
930 jobs: args.jobs,
931 verbose: args.verbose,
932 timing: args.timing,
933 diagnose: args.diagnose,
934 cli_skill_dirs: &cli_skill_dirs,
935 };
936 if args.watch {
937 commands::test::run_watch_tests(&test_dir, run_args).await;
938 } else {
939 commands::test::run_user_tests(
940 &test_dir,
941 run_args,
942 commands::test::UserTestReportConfig {
943 junit_path: args.junit.as_deref(),
944 json_out_path: args.json_out.as_deref(),
945 },
946 )
947 .await;
948 }
949 }
950 }
951 }
952 Command::Init(args) => {
953 commands::init::init_project(args.name.as_deref(), args.template).await;
954 }
955 Command::New(args) => match commands::init::resolve_new_args(&args) {
956 Ok((name, template)) => commands::init::init_project(name.as_deref(), template).await,
957 Err(error) => {
958 eprintln!("error: {error}");
959 process::exit(1);
960 }
961 },
962 Command::Doctor(args) => {
963 commands::doctor::run_doctor_with_options(commands::doctor::DoctorOptions {
964 json: args.json,
965 check_providers: args.check_providers,
966 check_targets: args.check_targets,
967 })
968 .await;
969 }
970 Command::Models(args) => commands::models::run(args).await,
971 Command::Local(args) => commands::local::run(args).await,
972 Command::Providers(args) => match args.command {
973 ProvidersCommand::Refresh(refresh) => {
974 if let Err(error) = commands::providers::run_refresh(&refresh).await {
975 command_error(&error);
976 }
977 }
978 ProvidersCommand::Validate(validate) => {
979 if let Err(error) = commands::providers::run_validate(&validate) {
980 command_error(&error);
981 }
982 }
983 ProvidersCommand::Export(export) => {
984 if let Err(error) = commands::providers::run_export(&export) {
985 command_error(&error);
986 }
987 }
988 ProvidersCommand::Matrix(matrix) => {
989 if let Err(error) = commands::providers::run_matrix(&matrix) {
990 command_error(&error);
991 }
992 }
993 ProvidersCommand::Support(support) => {
994 if let Err(error) = commands::provider_support::run(&support) {
995 command_error(&error);
996 }
997 }
998 ProvidersCommand::Recommend(recommend) => {
999 if let Err(error) = commands::providers::run_recommend(&recommend).await {
1000 command_error(&error);
1001 }
1002 }
1003 },
1004 Command::Provider(args) => commands::provider_capabilities::run_or_exit(args),
1005 Command::Try(args) => commands::try_cmd::run(args).await,
1006 Command::Quickstart(args) => {
1007 if let Err(error) = commands::quickstart::run_quickstart(&args).await {
1008 command_error(&error);
1009 }
1010 }
1011 Command::Demo(args) => {
1012 let code = commands::demo::run(args).await;
1013 if code != 0 {
1014 process::exit(code);
1015 }
1016 }
1017 Command::Serve(args) => match args.command {
1018 ServeCommand::Acp(args) => {
1019 if let Err(error) = commands::serve::run_acp_server(&args).await {
1020 command_error(&error);
1021 }
1022 }
1023 ServeCommand::A2a(args) => {
1024 if let Err(error) = commands::serve::run_a2a_server(&args).await {
1025 command_error(&error);
1026 }
1027 }
1028 ServeCommand::Api(args) => {
1029 if let Err(error) = commands::serve::run_api_server(&args).await {
1030 command_error(&error);
1031 }
1032 }
1033 ServeCommand::Mcp(args) => {
1034 if let Err(error) = commands::serve::run_mcp_server(&args).await {
1035 command_error(&error);
1036 }
1037 }
1038 },
1039 Command::Connector(args) => {
1040 if let Err(error) = commands::connector::handle_connector_command(args).await {
1041 eprintln!("error: {error}");
1042 process::exit(1);
1043 }
1044 }
1045 Command::Mcp(args) => commands::mcp::handle_mcp_command(&args.command).await,
1046 Command::Watch(args) => {
1047 let denied =
1048 commands::run::build_denied_builtins(args.deny.as_deref(), args.allow.as_deref());
1049 commands::run::run_watch(&args.file, denied).await;
1050 }
1051 Command::Dev(args) => {
1052 commands::dev::run(args).await;
1053 }
1054 Command::Portal(args) => {
1055 commands::portal::run_portal(
1056 &args.dir,
1057 args.manifest,
1058 args.persona_state_dir,
1059 &args.host,
1060 args.port,
1061 args.open,
1062 args.allow_remote_launch,
1063 )
1064 .await;
1065 }
1066 Command::Trigger(args) => {
1067 if let Err(error) = commands::trigger::handle(args).await {
1068 eprintln!("error: {error}");
1069 process::exit(1);
1070 }
1071 }
1072 Command::Graph(args) => {
1073 let code = commands::graph::run(args).await;
1074 if code != 0 {
1075 process::exit(code);
1076 }
1077 }
1078 Command::Routes(args) => {
1079 let code = commands::routes::run(args).await;
1080 if code != 0 {
1081 process::exit(code);
1082 }
1083 }
1084 Command::Flow(args) => match commands::flow::run_flow(&args) {
1085 Ok(code) => {
1086 if code != 0 {
1087 process::exit(code);
1088 }
1089 }
1090 Err(error) => command_error(&error),
1091 },
1092 Command::Workflow(args) => match commands::workflow::handle(args) {
1093 Ok(code) => {
1094 if code != 0 {
1095 process::exit(code);
1096 }
1097 }
1098 Err(error) => command_error(&error),
1099 },
1100 Command::Supervisor(args) => {
1101 if let Err(error) = commands::supervisor::handle(args).await {
1102 eprintln!("error: {error}");
1103 process::exit(1);
1104 }
1105 }
1106 Command::Trace(args) => {
1107 if let Err(error) = commands::trace::handle(args).await {
1108 eprintln!("error: {error}");
1109 process::exit(1);
1110 }
1111 }
1112 Command::Crystallize(args) => {
1113 if let Err(error) = commands::crystallize::run(args) {
1114 eprintln!("error: {error}");
1115 process::exit(1);
1116 }
1117 }
1118 Command::Trust(args) | Command::TrustGraph(args) => {
1119 if let Err(error) = commands::trust::handle(args).await {
1120 eprintln!("error: {error}");
1121 process::exit(1);
1122 }
1123 }
1124 Command::Verify(args) => {
1125 if let Err(error) = verify_provenance_receipt(&args.receipt, args.json) {
1126 eprintln!("error: {error}");
1127 process::exit(1);
1128 }
1129 }
1130 Command::Completions(args) => print_completions(args.shell),
1131 Command::Orchestrator(args) => {
1132 if let Err(error) = commands::orchestrator::handle(args).await {
1133 eprintln!("error: {error}");
1134 process::exit(1);
1135 }
1136 }
1137 Command::Playground(args) => {
1138 provider_bootstrap::maybe_seed_ollama_for_playground(
1139 Path::new(&args.host),
1140 Path::new(&args.script),
1141 args.yes,
1142 args.llm.is_some(),
1143 args.llm_mock.is_some(),
1144 )
1145 .await;
1146 let llm_mock_mode = if let Some(path) = args.llm_mock.as_ref() {
1147 commands::run::CliLlmMockMode::Replay {
1148 fixture_path: PathBuf::from(path),
1149 }
1150 } else if let Some(path) = args.llm_mock_record.as_ref() {
1151 commands::run::CliLlmMockMode::Record {
1152 fixture_path: PathBuf::from(path),
1153 }
1154 } else {
1155 commands::run::CliLlmMockMode::Off
1156 };
1157 if let Err(error) = commands::playground::run_command(args, llm_mock_mode).await {
1158 eprint!("{error}");
1159 process::exit(1);
1160 }
1161 }
1162 Command::Runs(args) => match args.command {
1163 RunsCommand::Inspect(inspect) => {
1164 inspect_run_record(&inspect.path, inspect.compare.as_deref());
1165 }
1166 },
1167 Command::Session(args) => commands::session::run(args),
1168 Command::Replay(args) => {
1169 let exit = commands::replay::run(args);
1170 if exit != 0 {
1171 process::exit(exit);
1172 }
1173 }
1174 Command::Eval(args) => match args.command {
1175 Some(EvalCommand::CodingAgent(coding_agent_args)) => {
1176 let code = commands::eval_coding_agent::run(coding_agent_args).await;
1177 if code != 0 {
1178 process::exit(code);
1179 }
1180 }
1181 Some(EvalCommand::Context(context_args)) => {
1182 let code = commands::eval_context::run(context_args).await;
1183 if code != 0 {
1184 process::exit(code);
1185 }
1186 }
1187 Some(EvalCommand::Prompt(prompt_args)) => {
1188 let code = commands::eval_prompt::run(prompt_args).await;
1189 if code != 0 {
1190 process::exit(code);
1191 }
1192 }
1193 Some(EvalCommand::ScopeTriage(scope_args)) => {
1194 process::exit(commands::eval_scope_triage::run(scope_args).await)
1195 }
1196 Some(EvalCommand::ToolCalls(tool_calls_args)) => {
1197 let code = commands::eval_tool_calls::run(tool_calls_args).await;
1198 if code != 0 {
1199 process::exit(code);
1200 }
1201 }
1202 None => {
1203 let Some(path) = args.path else {
1204 eprintln!("error: `harn eval` requires a path or a subcommand (e.g. `prompt`).\nSee `harn eval --help`.");
1205 process::exit(2);
1206 };
1207 let llm_mock_mode = if let Some(path) = args.llm_mock.as_ref() {
1208 commands::run::CliLlmMockMode::Replay {
1209 fixture_path: PathBuf::from(path),
1210 }
1211 } else if let Some(path) = args.llm_mock_record.as_ref() {
1212 commands::run::CliLlmMockMode::Record {
1213 fixture_path: PathBuf::from(path),
1214 }
1215 } else {
1216 commands::run::CliLlmMockMode::Off
1217 };
1218 eval_run_record(
1219 &path,
1220 args.compare.as_deref(),
1221 args.structural_experiment.as_deref(),
1222 &args.argv,
1223 &llm_mock_mode,
1224 );
1225 }
1226 },
1227 Command::Repl => commands::repl::run_repl().await,
1228 Command::Bench(args) => commands::bench::run(args).await,
1229 Command::Precompile(args) => commands::precompile::run(args).await,
1230 Command::Pack(args) => commands::pack::run(args),
1231 Command::TestBench(args) => commands::test_bench::run(args.command).await,
1232 Command::Viz(args) => commands::viz::run_viz(&args.file, args.output.as_deref()),
1233 Command::Install(args) => package::install_packages(
1234 args.frozen || args.locked || args.offline,
1235 args.refetch.as_deref(),
1236 args.offline,
1237 args.json,
1238 ),
1239 Command::Add(args) => package::add_package_with_registry(
1240 &args.name_or_spec,
1241 args.alias.as_deref(),
1242 args.git.as_deref(),
1243 args.tag.as_deref(),
1244 args.rev.as_deref(),
1245 args.branch.as_deref(),
1246 args.path.as_deref(),
1247 args.registry.as_deref(),
1248 ),
1249 Command::Update(args) => {
1250 package::update_packages(args.alias.as_deref(), args.all, args.json);
1251 }
1252 Command::Remove(args) => package::remove_package(&args.alias),
1253 Command::Lock => package::lock_packages(),
1254 Command::Package(args) => match args.command {
1255 PackageCommand::List(list) => package::list_packages(list.json),
1256 PackageCommand::Doctor(doctor) => package::doctor_packages(doctor.json),
1257 PackageCommand::Search(search) => package::search_package_registry(
1258 search.query.as_deref(),
1259 search.registry.as_deref(),
1260 search.json,
1261 ),
1262 PackageCommand::Info(info) => {
1263 package::show_package_registry_info(
1264 &info.name,
1265 info.registry.as_deref(),
1266 info.json,
1267 );
1268 }
1269 PackageCommand::Check(check) => {
1270 package::check_package(check.package.as_deref(), check.json);
1271 }
1272 PackageCommand::Pack(pack) => package::pack_package(
1273 pack.package.as_deref(),
1274 pack.output.as_deref(),
1275 pack.dry_run,
1276 pack.json,
1277 ),
1278 PackageCommand::Docs(docs) => package::generate_package_docs(
1279 docs.package.as_deref(),
1280 docs.output.as_deref(),
1281 docs.check,
1282 ),
1283 PackageCommand::Cache(cache) => match cache.command {
1284 PackageCacheCommand::List => package::list_package_cache(),
1285 PackageCacheCommand::Clean(clean) => package::clean_package_cache(clean.all),
1286 PackageCacheCommand::Verify(verify) => {
1287 package::verify_package_cache(verify.materialized);
1288 }
1289 },
1290 PackageCommand::Outdated(args) => package::outdated_packages(
1291 args.refresh,
1292 args.remote,
1293 args.registry.as_deref(),
1294 args.json,
1295 ),
1296 PackageCommand::Audit(args) => {
1297 package::audit_packages(
1298 args.registry.as_deref(),
1299 args.skip_materialized,
1300 args.json,
1301 );
1302 }
1303 PackageCommand::Artifacts(args) => match args.command {
1304 PackageArtifactsCommand::Manifest(manifest) => {
1305 package::artifacts_manifest(manifest.output.as_deref());
1306 }
1307 PackageArtifactsCommand::Check(check) => {
1308 package::artifacts_check(&check.manifest, check.json);
1309 }
1310 },
1311 PackageCommand::Scaffold(args) => match args.command {
1312 PackageScaffoldCommand::Openapi(openapi) => {
1313 if let Err(error) = commands::package_scaffold::run_openapi(&openapi).await {
1314 eprintln!("error: {error}");
1315 process::exit(1);
1316 }
1317 }
1318 },
1319 },
1320 Command::Publish(args) => package::publish_package(
1321 args.package.as_deref(),
1322 args.dry_run,
1323 &args.remote,
1324 &args.index_repo,
1325 &args.index_path,
1326 args.registry_name.as_deref(),
1327 args.skip_index_pr,
1328 args.registry.as_deref(),
1329 args.json,
1330 ),
1331 Command::MergeCaptain(args) => match args.command {
1332 MergeCaptainCommand::Run(run) => {
1333 let code = commands::merge_captain::run_driver(&run);
1334 if code != 0 {
1335 process::exit(code);
1336 }
1337 }
1338 MergeCaptainCommand::Ladder(ladder) => {
1339 let code = commands::merge_captain::run_ladder(&ladder);
1340 if code != 0 {
1341 process::exit(code);
1342 }
1343 }
1344 MergeCaptainCommand::Iterate(iterate) => {
1345 let code = commands::merge_captain::run_iterate(&iterate);
1346 if code != 0 {
1347 process::exit(code);
1348 }
1349 }
1350 MergeCaptainCommand::Audit(audit) => {
1351 let code = commands::merge_captain::run_audit(&audit);
1352 if code != 0 {
1353 process::exit(code);
1354 }
1355 }
1356 MergeCaptainCommand::Mock(mock) => {
1357 let code = match mock {
1358 MergeCaptainMockCommand::Init(args) => {
1359 commands::merge_captain_mock::run_init(&args)
1360 }
1361 MergeCaptainMockCommand::Step(args) => {
1362 commands::merge_captain_mock::run_step(&args)
1363 }
1364 MergeCaptainMockCommand::Status(args) => {
1365 commands::merge_captain_mock::run_status(&args)
1366 }
1367 MergeCaptainMockCommand::Serve(args) => {
1368 commands::merge_captain_mock::run_serve(&args).await
1369 }
1370 MergeCaptainMockCommand::Cleanup(args) => {
1371 commands::merge_captain_mock::run_cleanup(&args)
1372 }
1373 MergeCaptainMockCommand::Scenarios => {
1374 commands::merge_captain_mock::run_scenarios()
1375 }
1376 };
1377 if code != 0 {
1378 process::exit(code);
1379 }
1380 }
1381 },
1382 Command::Pg(args) => match args.command {
1383 PgCommand::Codegen(codegen) => {
1384 let code = commands::pg_codegen::run(&codegen);
1385 if code != 0 {
1386 process::exit(code);
1387 }
1388 }
1389 },
1390 Command::Persona(args) => match args.command {
1391 PersonaCommand::New(new) => {
1392 if let Err(error) = commands::persona_scaffold::run_new(&new) {
1393 eprintln!("error: {error}");
1394 process::exit(1);
1395 }
1396 }
1397 PersonaCommand::Doctor(doctor) => {
1398 if let Err(error) =
1399 commands::persona_doctor::run_doctor(args.manifest.as_deref(), &doctor).await
1400 {
1401 eprintln!("error: {error}");
1402 process::exit(1);
1403 }
1404 }
1405 PersonaCommand::Check(check) => {
1406 commands::persona::run_check(args.manifest.as_deref(), &check);
1407 }
1408 PersonaCommand::List(list) => {
1409 commands::persona::run_list(args.manifest.as_deref(), &list);
1410 }
1411 PersonaCommand::Inspect(inspect) => {
1412 commands::persona::run_inspect(args.manifest.as_deref(), &inspect);
1413 }
1414 PersonaCommand::Status(status) => {
1415 if let Err(error) = commands::persona::run_status(
1416 args.manifest.as_deref(),
1417 &args.state_dir,
1418 &status,
1419 )
1420 .await
1421 {
1422 eprintln!("error: {error}");
1423 process::exit(1);
1424 }
1425 }
1426 PersonaCommand::Pause(control) => {
1427 if let Err(error) = commands::persona::run_pause(
1428 args.manifest.as_deref(),
1429 &args.state_dir,
1430 &control,
1431 )
1432 .await
1433 {
1434 eprintln!("error: {error}");
1435 process::exit(1);
1436 }
1437 }
1438 PersonaCommand::Resume(control) => {
1439 if let Err(error) = commands::persona::run_resume(
1440 args.manifest.as_deref(),
1441 &args.state_dir,
1442 &control,
1443 )
1444 .await
1445 {
1446 eprintln!("error: {error}");
1447 process::exit(1);
1448 }
1449 }
1450 PersonaCommand::Disable(control) => {
1451 if let Err(error) = commands::persona::run_disable(
1452 args.manifest.as_deref(),
1453 &args.state_dir,
1454 &control,
1455 )
1456 .await
1457 {
1458 eprintln!("error: {error}");
1459 process::exit(1);
1460 }
1461 }
1462 PersonaCommand::Tick(tick) => {
1463 if let Err(error) =
1464 commands::persona::run_tick(args.manifest.as_deref(), &args.state_dir, &tick)
1465 .await
1466 {
1467 eprintln!("error: {error}");
1468 process::exit(1);
1469 }
1470 }
1471 PersonaCommand::Trigger(trigger) => {
1472 if let Err(error) = commands::persona::run_trigger(
1473 args.manifest.as_deref(),
1474 &args.state_dir,
1475 &trigger,
1476 )
1477 .await
1478 {
1479 eprintln!("error: {error}");
1480 process::exit(1);
1481 }
1482 }
1483 PersonaCommand::Spend(spend) => {
1484 if let Err(error) =
1485 commands::persona::run_spend(args.manifest.as_deref(), &args.state_dir, &spend)
1486 .await
1487 {
1488 eprintln!("error: {error}");
1489 process::exit(1);
1490 }
1491 }
1492 PersonaCommand::Supervision(supervision) => match supervision.command {
1493 PersonaSupervisionCommand::Tail(tail) => {
1494 if let Err(error) = commands::persona_supervision::run_tail(
1495 args.manifest.as_deref(),
1496 &args.state_dir,
1497 &tail,
1498 )
1499 .await
1500 {
1501 eprintln!("error: {error}");
1502 process::exit(1);
1503 }
1504 }
1505 },
1506 },
1507 Command::ModelInfo(args) => {
1508 if !print_model_info(&args).await {
1509 process::exit(1);
1510 }
1511 }
1512 Command::ProviderCatalog(args) => {
1513 if std::env::var("HARN_CLI_IMPL").as_deref() == Ok("rust") {
1514 print_provider_catalog(args.available_only);
1515 } else {
1516 let exit_code = dispatch_provider_catalog(args.available_only).await;
1517 if exit_code != 0 {
1518 process::exit(exit_code);
1519 }
1520 }
1521 }
1522 Command::ProviderReady(args) => {
1523 run_provider_ready(
1524 &args.provider,
1525 args.model.as_deref(),
1526 args.base_url.as_deref(),
1527 args.json,
1528 )
1529 .await;
1530 }
1531 Command::ProviderProbe(args) => commands::provider::run_provider_probe(args).await,
1532 Command::ProviderToolProbe(args) => commands::provider::run_provider_tool_probe(args).await,
1533 Command::Skills(args) => match args.command {
1534 SkillsCommand::List(list) => commands::skills::run_list(&list),
1535 SkillsCommand::Get(get) => commands::skills::run_get(&get),
1536 SkillsCommand::Dump(dump) => commands::skills::run_dump(&dump),
1537 SkillsCommand::Resolved(resolved) => commands::skills::run_resolved(&resolved),
1538 SkillsCommand::Inspect(inspect) => commands::skills::run_inspect(&inspect),
1539 SkillsCommand::Match(matcher) => commands::skills::run_match(&matcher),
1540 SkillsCommand::Install(install) => commands::skills::run_install(&install),
1541 SkillsCommand::New(new_args) => commands::skills::run_new(&new_args),
1542 },
1543 Command::Tool(args) => match args.command {
1544 ToolCommand::New(new_args) => {
1545 if let Err(error) = commands::tool::run_new(&new_args).await {
1546 eprintln!("error: {error}");
1547 process::exit(1);
1548 }
1549 }
1550 },
1551 Command::DumpHighlightKeywords(args) => {
1552 commands::dump_highlight_keywords::run(&args.output, args.check);
1553 }
1554 Command::DumpTriggerQuickref(args) => {
1555 commands::dump_trigger_quickref::run(&args.output, args.check);
1556 }
1557 Command::DumpConnectorMatrix(args) => {
1558 commands::check::connector_matrix::run_docs(&args.output, &args.sources, args.check);
1559 }
1560 Command::DumpProtocolArtifacts(args) => {
1561 commands::dump_protocol_artifacts::run(&args.output_dir, args.check);
1562 }
1563 Command::Time(args) => match args.command {
1564 TimeCommand::Run(time_args) => commands::time::run(time_args).await,
1565 },
1566 }
1567}
1568
1569fn run_profile_options(args: &cli::ProfileArgs) -> commands::run::RunProfileOptions {
1570 commands::run::RunProfileOptions {
1571 text: args.text,
1572 json_path: args.json_path.clone(),
1573 }
1574}
1575
1576fn print_completions(shell: CompletionShell) {
1577 let mut command = Cli::command();
1578 let shell = clap_complete::Shell::from(shell);
1579 clap_complete::generate(shell, &mut command, "harn", &mut std::io::stdout());
1580}
1581
1582fn normalize_serve_args(mut raw_args: Vec<String>) -> Vec<String> {
1583 if raw_args.len() > 2
1584 && raw_args.get(1).is_some_and(|arg| arg == "serve")
1585 && !matches!(
1586 raw_args.get(2).map(String::as_str),
1587 Some("acp" | "a2a" | "api" | "mcp" | "-h" | "--help")
1588 )
1589 {
1590 raw_args.insert(2, "a2a".to_string());
1591 }
1592 raw_args
1593}
1594
1595fn print_version() {
1596 println!(
1597 r"
1598 ╱▔▔╲
1599 ╱ ╲ harn v{}
1600 │ ◆ │ the agent harness language
1601 │ │
1602 ╰──╯╱
1603 ╱╱
1604",
1605 env!("CARGO_PKG_VERSION")
1606 );
1607}
1608
1609pub(crate) const VERSION_SCHEMA_VERSION: u32 = 1;
1612
1613#[derive(serde::Serialize)]
1614struct VersionInfo {
1615 name: &'static str,
1616 version: &'static str,
1617 description: &'static str,
1618}
1619
1620fn print_version_json() {
1621 let payload = VersionInfo {
1622 name: env!("CARGO_PKG_NAME"),
1623 version: env!("CARGO_PKG_VERSION"),
1624 description: env!("CARGO_PKG_DESCRIPTION"),
1625 };
1626 let envelope = json_envelope::JsonEnvelope::ok(VERSION_SCHEMA_VERSION, payload);
1627 println!("{}", json_envelope::to_string_pretty(&envelope));
1628}
1629
1630async fn run_version(args: cli::VersionArgs) -> i32 {
1634 if std::env::var("HARN_CLI_IMPL").as_deref() == Ok("rust") {
1635 if args.json {
1636 print_version_json();
1637 } else {
1638 print_version();
1639 }
1640 return 0;
1641 }
1642 let _name = env_guard::ScopedEnvVar::set("HARN_BUILD_NAME", env!("CARGO_PKG_NAME"));
1646 let _version = env_guard::ScopedEnvVar::set("HARN_BUILD_VERSION", env!("CARGO_PKG_VERSION"));
1647 let _description =
1648 env_guard::ScopedEnvVar::set("HARN_BUILD_DESCRIPTION", env!("CARGO_PKG_DESCRIPTION"));
1649 let argv = if args.json {
1650 vec!["--json".to_string()]
1651 } else {
1652 Vec::new()
1653 };
1654 dispatch::dispatch_to_embedded_script("version", argv, args.json).await
1655}
1656
1657async fn print_model_info(args: &ModelInfoArgs) -> bool {
1658 let resolved = harn_vm::llm_config::resolve_model_info(&args.model);
1659 let api_key_result = harn_vm::llm::resolve_api_key(&resolved.provider);
1660 let api_key_set = api_key_result.is_ok();
1661 let api_key = api_key_result.unwrap_or_default();
1662 let context_window =
1663 harn_vm::llm::fetch_provider_max_context(&resolved.provider, &resolved.id, &api_key).await;
1664 let readiness = local_openai_readiness(&resolved.provider, &resolved.id, &api_key).await;
1665 let catalog = harn_vm::llm_config::model_catalog_entry(&resolved.id);
1666 let runtime_context_window = catalog
1667 .as_ref()
1668 .and_then(|entry| entry.runtime_context_window);
1669 let capabilities = harn_vm::llm::capabilities::lookup(&resolved.provider, &resolved.id);
1670 let mut payload = serde_json::json!({
1671 "alias": args.model,
1672 "id": resolved.id,
1673 "provider": resolved.provider,
1674 "resolved_alias": resolved.alias,
1675 "tool_format": resolved.tool_format,
1676 "tier": resolved.tier,
1677 "api_key_set": api_key_set,
1678 "context_window": context_window,
1679 "runtime_context_window": runtime_context_window,
1680 "readiness": readiness,
1681 "catalog": catalog,
1682 "capabilities": {
1683 "native_tools": capabilities.native_tools,
1684 "defer_loading": capabilities.defer_loading,
1685 "tool_search": capabilities.tool_search,
1686 "max_tools": capabilities.max_tools,
1687 "prompt_caching": capabilities.prompt_caching,
1688 "vision": capabilities.vision,
1689 "vision_supported": capabilities.vision_supported,
1690 "audio": capabilities.audio,
1691 "pdf": capabilities.pdf,
1692 "files_api_supported": capabilities.files_api_supported,
1693 "json_schema": capabilities.json_schema,
1694 "prefers_xml_scaffolding": capabilities.prefers_xml_scaffolding,
1695 "prefers_markdown_scaffolding": capabilities.prefers_markdown_scaffolding,
1696 "structured_output_mode": capabilities.structured_output_mode,
1697 "supports_assistant_prefill": capabilities.supports_assistant_prefill,
1698 "prefers_role_developer": capabilities.prefers_role_developer,
1699 "prefers_xml_tools": capabilities.prefers_xml_tools,
1700 "thinking": !capabilities.thinking_modes.is_empty(),
1701 "thinking_block_style": capabilities.thinking_block_style,
1702 "thinking_modes": capabilities.thinking_modes,
1703 "interleaved_thinking_supported": capabilities.interleaved_thinking_supported,
1704 "anthropic_beta_features": capabilities.anthropic_beta_features,
1705 "preserve_thinking": capabilities.preserve_thinking,
1706 "server_parser": capabilities.server_parser,
1707 "honors_chat_template_kwargs": capabilities.honors_chat_template_kwargs,
1708 "recommended_endpoint": capabilities.recommended_endpoint,
1709 "text_tool_wire_format_supported": capabilities.text_tool_wire_format_supported,
1710 "preferred_tool_format": capabilities.preferred_tool_format,
1711 "tool_mode_parity": capabilities.tool_mode_parity,
1712 "tool_mode_parity_notes": capabilities.tool_mode_parity_notes,
1713 },
1714 "qc_default_model": harn_vm::llm_config::qc_default_model(&resolved.provider),
1715 });
1716
1717 let should_verify = args.verify || args.warm;
1718 let mut ok = true;
1719 if should_verify {
1720 if resolved.provider == "ollama" {
1721 let mut readiness = harn_vm::llm::OllamaReadinessOptions::new(resolved.id.clone());
1722 readiness.warm = args.warm;
1723 readiness.observe_loaded = true;
1724 readiness.keep_alive = args
1725 .keep_alive
1726 .as_deref()
1727 .and_then(harn_vm::llm::normalize_ollama_keep_alive);
1728 let result = harn_vm::llm::ollama_readiness(readiness).await;
1729 ok = result.valid;
1730 payload["readiness"] = serde_json::to_value(&result).unwrap_or_else(|error| {
1731 serde_json::json!({
1732 "valid": false,
1733 "status": "serialization_error",
1734 "message": format!("failed to serialize readiness result: {error}"),
1735 })
1736 });
1737 } else {
1738 ok = false;
1739 payload["readiness"] = serde_json::json!({
1740 "valid": false,
1741 "status": "unsupported_provider",
1742 "message": format!(
1743 "model-info --verify is only supported for Ollama models; resolved provider is '{}'",
1744 resolved.provider
1745 ),
1746 "provider": resolved.provider,
1747 });
1748 }
1749 }
1750
1751 println!(
1752 "{}",
1753 serde_json::to_string(&payload).unwrap_or_else(|error| {
1754 command_error(&format!("failed to serialize model info: {error}"))
1755 })
1756 );
1757 ok
1758}
1759
1760async fn local_openai_readiness(
1761 provider: &str,
1762 model: &str,
1763 api_key: &str,
1764) -> Option<serde_json::Value> {
1765 let def = harn_vm::llm_config::provider_config(provider)?;
1766 if def.auth_style != "none" || !harn_vm::llm::supports_model_readiness_probe(&def) {
1767 return None;
1768 }
1769 let readiness = harn_vm::llm::probe_openai_compatible_model(provider, model, api_key).await;
1770 Some(serde_json::json!({
1771 "valid": readiness.valid,
1772 "category": readiness.category,
1773 "message": readiness.message,
1774 "provider": readiness.provider,
1775 "model": readiness.model,
1776 "url": readiness.url,
1777 "status": readiness.status,
1778 "available_models": readiness.available_models,
1779 }))
1780}
1781
1782fn build_provider_catalog_payload(available_only: bool) -> serde_json::Value {
1783 let provider_names = if available_only {
1784 harn_vm::llm_config::available_provider_names()
1785 } else {
1786 harn_vm::llm_config::provider_names()
1787 };
1788 let providers: Vec<_> = provider_names
1789 .into_iter()
1790 .filter_map(|name| {
1791 harn_vm::llm_config::provider_config(&name).map(|def| {
1792 serde_json::json!({
1793 "name": name,
1794 "display_name": def.display_name,
1795 "icon": def.icon,
1796 "base_url": harn_vm::llm_config::resolve_base_url(&def),
1797 "base_url_env": def.base_url_env,
1798 "auth_style": def.auth_style,
1799 "auth_envs": harn_vm::llm_config::auth_env_names(&def.auth_env),
1800 "auth_available": harn_vm::llm_config::provider_key_available(&name),
1801 "features": def.features,
1802 "cost_per_1k_in": def.cost_per_1k_in,
1803 "cost_per_1k_out": def.cost_per_1k_out,
1804 "latency_p50_ms": def.latency_p50_ms,
1805 })
1806 })
1807 })
1808 .collect();
1809 let models: Vec<_> = harn_vm::llm_config::model_catalog_entries()
1810 .into_iter()
1811 .map(|(id, model)| {
1812 serde_json::json!({
1813 "id": id,
1814 "name": model.name,
1815 "provider": model.provider,
1816 "context_window": model.context_window,
1817 "runtime_context_window": model.runtime_context_window,
1818 "stream_timeout": model.stream_timeout,
1819 "capabilities": model.capabilities,
1820 "pricing": model.pricing,
1821 })
1822 })
1823 .collect();
1824 let aliases: Vec<_> = harn_vm::llm_config::alias_entries()
1825 .into_iter()
1826 .map(|(name, alias)| {
1827 serde_json::json!({
1828 "name": name,
1829 "id": alias.id,
1830 "provider": alias.provider,
1831 "tool_format": alias.tool_format,
1832 "tool_calling": harn_vm::llm_config::alias_tool_calling_entry(&name),
1833 })
1834 })
1835 .collect();
1836 serde_json::json!({
1837 "providers": providers,
1838 "known_model_names": harn_vm::llm_config::known_model_names(),
1839 "available_providers": harn_vm::llm_config::available_provider_names(),
1840 "aliases": aliases,
1841 "models": models,
1842 "qc_defaults": harn_vm::llm_config::qc_defaults(),
1843 })
1844}
1845
1846fn print_provider_catalog(available_only: bool) {
1847 let payload = build_provider_catalog_payload(available_only);
1848 println!(
1849 "{}",
1850 serde_json::to_string(&payload).unwrap_or_else(|error| {
1851 command_error(&format!("failed to serialize provider catalog: {error}"))
1852 })
1853 );
1854}
1855
1856async fn dispatch_provider_catalog(available_only: bool) -> i32 {
1865 static DISPATCH_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
1866 let payload = build_provider_catalog_payload(available_only);
1867 let payload_json = match serde_json::to_string(&payload) {
1868 Ok(json) => json,
1869 Err(error) => {
1870 eprintln!("error: failed to serialise provider catalog payload: {error}");
1871 return 1;
1872 }
1873 };
1874 let _guard = DISPATCH_LOCK.lock().await;
1875 let _payload_guard =
1876 crate::env_guard::ScopedEnvVar::set("HARN_PROVIDER_CATALOG_PAYLOAD_JSON", &payload_json);
1877 crate::dispatch::dispatch_to_embedded_script("providers/catalog", Vec::new(), true).await
1881}
1882
1883async fn run_provider_ready(
1884 provider: &str,
1885 model: Option<&str>,
1886 base_url: Option<&str>,
1887 json: bool,
1888) {
1889 let readiness =
1890 harn_vm::llm::readiness::probe_provider_readiness(provider, model, base_url).await;
1891 if json {
1892 match serde_json::to_string_pretty(&readiness) {
1893 Ok(payload) => println!("{payload}"),
1894 Err(error) => command_error(&format!("failed to serialize readiness result: {error}")),
1895 }
1896 } else if readiness.ok {
1897 println!("{}", readiness.message);
1898 } else {
1899 eprintln!("{}", readiness.message);
1900 }
1901 if !readiness.ok {
1902 process::exit(1);
1903 }
1904}
1905
1906fn command_error(message: &str) -> ! {
1907 Cli::command()
1908 .error(ErrorKind::ValueValidation, message)
1909 .exit()
1910}
1911
1912fn print_check_error(code: &str, message: &str) -> ! {
1913 let envelope: json_envelope::JsonEnvelope<commands::check::CheckReport> =
1914 json_envelope::JsonEnvelope::err(commands::check::CHECK_SCHEMA_VERSION, code, message);
1915 println!("{}", json_envelope::to_string_pretty(&envelope));
1916 process::exit(1);
1917}
1918
1919fn print_lint_error(code: &str, message: &str) -> ! {
1920 let envelope: json_envelope::JsonEnvelope<commands::check::LintReport> =
1921 json_envelope::JsonEnvelope::err(commands::check::LINT_SCHEMA_VERSION, code, message);
1922 println!("{}", json_envelope::to_string_pretty(&envelope));
1923 process::exit(1);
1924}
1925
1926fn verify_provenance_receipt(path: &str, json: bool) -> Result<(), String> {
1927 let raw =
1928 fs::read_to_string(path).map_err(|error| format!("failed to read {path}: {error}"))?;
1929 let receipt: harn_vm::ProvenanceReceipt = serde_json::from_str(&raw)
1930 .map_err(|error| format!("failed to parse provenance receipt {path}: {error}"))?;
1931 let report = harn_vm::verify_receipt(&receipt);
1932 if json {
1933 println!(
1934 "{}",
1935 serde_json::to_string_pretty(&report).map_err(|error| error.to_string())?
1936 );
1937 } else if report.verified {
1938 println!(
1939 "verified receipt={} events={} receipt_hash={} event_root_hash={}",
1940 report.receipt_id.unwrap_or_else(|| "-".to_string()),
1941 report.event_count,
1942 report.receipt_hash.unwrap_or_else(|| "-".to_string()),
1943 report.event_root_hash.unwrap_or_else(|| "-".to_string())
1944 );
1945 } else {
1946 println!(
1947 "failed receipt={} events={}",
1948 report.receipt_id.unwrap_or_else(|| "-".to_string()),
1949 report.event_count
1950 );
1951 for error in &report.errors {
1952 println!(" {error}");
1953 }
1954 return Err("provenance receipt verification failed".to_string());
1955 }
1956 Ok(())
1957}
1958
1959fn load_run_record_or_exit(path: &Path) -> harn_vm::orchestration::RunRecord {
1960 match harn_vm::orchestration::load_run_record(path) {
1961 Ok(run) => run,
1962 Err(error) => {
1963 eprintln!("Failed to load run record: {error}");
1964 process::exit(1);
1965 }
1966 }
1967}
1968
1969fn load_eval_suite_manifest_or_exit(path: &Path) -> harn_vm::orchestration::EvalSuiteManifest {
1970 harn_vm::orchestration::load_eval_suite_manifest(path).unwrap_or_else(|error| {
1971 eprintln!("Failed to load eval manifest {}: {error}", path.display());
1972 process::exit(1);
1973 })
1974}
1975
1976fn load_eval_pack_manifest_or_exit(path: &Path) -> harn_vm::orchestration::EvalPackManifest {
1977 harn_vm::orchestration::load_eval_pack_manifest(path).unwrap_or_else(|error| {
1978 eprintln!("Failed to load eval pack {}: {error}", path.display());
1979 process::exit(1);
1980 })
1981}
1982
1983fn load_persona_eval_ladder_manifest_or_exit(
1984 path: &Path,
1985) -> harn_vm::orchestration::PersonaEvalLadderManifest {
1986 harn_vm::orchestration::load_persona_eval_ladder_manifest(path).unwrap_or_else(|error| {
1987 eprintln!(
1988 "Failed to load persona eval ladder {}: {error}",
1989 path.display()
1990 );
1991 process::exit(1);
1992 })
1993}
1994
1995fn file_looks_like_eval_manifest(path: &Path) -> bool {
1996 if path.file_name().and_then(|name| name.to_str()) == Some("harn.eval.toml") {
1997 return true;
1998 }
1999 if path.extension().and_then(|ext| ext.to_str()) == Some("toml") {
2000 let Ok(content) = fs::read_to_string(path) else {
2001 return false;
2002 };
2003 return toml::from_str::<harn_vm::orchestration::EvalPackManifest>(&content)
2004 .is_ok_and(|manifest| !manifest.cases.is_empty() || !manifest.ladders.is_empty());
2005 }
2006 let Ok(content) = fs::read_to_string(path) else {
2007 return false;
2008 };
2009 let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) else {
2010 return false;
2011 };
2012 json.get("_type").and_then(|value| value.as_str()) == Some("eval_suite_manifest")
2013 || json.get("cases").is_some()
2014}
2015
2016fn file_looks_like_eval_pack_manifest(path: &Path) -> bool {
2017 if path.file_name().and_then(|name| name.to_str()) == Some("harn.eval.toml") {
2018 return true;
2019 }
2020 if path.extension().and_then(|ext| ext.to_str()) == Some("toml") {
2021 return file_looks_like_eval_manifest(path);
2022 }
2023 let Ok(content) = fs::read_to_string(path) else {
2024 return false;
2025 };
2026 let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) else {
2027 return false;
2028 };
2029 json.get("version").is_some()
2030 && (json.get("cases").is_some() || json.get("ladders").is_some())
2031 && json.get("_type").and_then(|value| value.as_str()) != Some("eval_suite_manifest")
2032}
2033
2034fn file_looks_like_persona_eval_ladder_manifest(path: &Path) -> bool {
2035 let Ok(content) = fs::read_to_string(path) else {
2036 return false;
2037 };
2038 if path.extension().and_then(|ext| ext.to_str()) == Some("json") {
2039 let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) else {
2040 return false;
2041 };
2042 return json.get("_type").and_then(|value| value.as_str())
2043 == Some("persona_eval_ladder_manifest")
2044 || json.get("timeout_tiers").is_some()
2045 || json.get("timeout-tiers").is_some();
2046 }
2047 toml::from_str::<harn_vm::orchestration::PersonaEvalLadderManifest>(&content).is_ok_and(
2048 |manifest| {
2049 manifest
2050 .type_name
2051 .eq_ignore_ascii_case("persona_eval_ladder_manifest")
2052 || (!manifest.timeout_tiers.is_empty() && manifest.backend.path.is_some())
2053 },
2054 )
2055}
2056
2057fn collect_run_record_paths(path: &str) -> Vec<PathBuf> {
2058 let path = Path::new(path);
2059 if path.is_file() {
2060 return vec![path.to_path_buf()];
2061 }
2062 if path.is_dir() {
2063 let mut entries: Vec<PathBuf> = fs::read_dir(path)
2064 .unwrap_or_else(|error| {
2065 eprintln!("Failed to read run directory {}: {error}", path.display());
2066 process::exit(1);
2067 })
2068 .filter_map(|entry| entry.ok().map(|entry| entry.path()))
2069 .filter(|entry| entry.extension().and_then(|ext| ext.to_str()) == Some("json"))
2070 .collect();
2071 entries.sort();
2072 return entries;
2073 }
2074 eprintln!("Run path does not exist: {}", path.display());
2075 process::exit(1);
2076}
2077
2078fn print_run_diff(diff: &harn_vm::orchestration::RunDiffReport) {
2079 println!(
2080 "Diff: {} -> {} [{} -> {}]",
2081 diff.left_run_id, diff.right_run_id, diff.left_status, diff.right_status
2082 );
2083 println!("Identical: {}", diff.identical);
2084 println!("Stage diffs: {}", diff.stage_diffs.len());
2085 println!("Tool diffs: {}", diff.tool_diffs.len());
2086 println!("Observability diffs: {}", diff.observability_diffs.len());
2087 println!("Transition delta: {}", diff.transition_count_delta);
2088 println!("Artifact delta: {}", diff.artifact_count_delta);
2089 println!("Checkpoint delta: {}", diff.checkpoint_count_delta);
2090 for stage in &diff.stage_diffs {
2091 println!("- {} [{}]", stage.node_id, stage.change);
2092 for detail in &stage.details {
2093 println!(" {detail}");
2094 }
2095 }
2096 for tool in &diff.tool_diffs {
2097 println!("- tool {} [{}]", tool.tool_name, tool.args_hash);
2098 println!(" left: {:?}", tool.left_result);
2099 println!(" right: {:?}", tool.right_result);
2100 }
2101 for item in &diff.observability_diffs {
2102 println!("- {} [{}]", item.label, item.section);
2103 for detail in &item.details {
2104 println!(" {detail}");
2105 }
2106 }
2107}
2108
2109fn inspect_run_record(path: &str, compare: Option<&str>) {
2110 let run = load_run_record_or_exit(Path::new(path));
2111 println!("Run: {}", run.id);
2112 println!(
2113 "Workflow: {}",
2114 run.workflow_name
2115 .clone()
2116 .unwrap_or_else(|| run.workflow_id.clone())
2117 );
2118 println!("Status: {}", run.status);
2119 println!("Task: {}", run.task);
2120 println!("Stages: {}", run.stages.len());
2121 println!("Artifacts: {}", run.artifacts.len());
2122 println!("Transitions: {}", run.transitions.len());
2123 println!("Checkpoints: {}", run.checkpoints.len());
2124 println!("HITL questions: {}", run.hitl_questions.len());
2125 if let Some(observability) = &run.observability {
2126 println!("Planner rounds: {}", observability.planner_rounds.len());
2127 println!("Research facts: {}", observability.research_fact_count);
2128 println!("Workers: {}", observability.worker_lineage.len());
2129 println!(
2130 "Action graph: {} nodes / {} edges",
2131 observability.action_graph_nodes.len(),
2132 observability.action_graph_edges.len()
2133 );
2134 println!(
2135 "Transcript pointers: {}",
2136 observability.transcript_pointers.len()
2137 );
2138 println!("Daemon events: {}", observability.daemon_events.len());
2139 }
2140 if let Some(parent_worker_id) = run
2141 .metadata
2142 .get("parent_worker_id")
2143 .and_then(|value| value.as_str())
2144 {
2145 println!("Parent worker: {parent_worker_id}");
2146 }
2147 if let Some(parent_stage_id) = run
2148 .metadata
2149 .get("parent_stage_id")
2150 .and_then(|value| value.as_str())
2151 {
2152 println!("Parent stage: {parent_stage_id}");
2153 }
2154 if run
2155 .metadata
2156 .get("delegated")
2157 .and_then(|value| value.as_bool())
2158 .unwrap_or(false)
2159 {
2160 println!("Delegated: true");
2161 }
2162 println!(
2163 "Pending nodes: {}",
2164 if run.pending_nodes.is_empty() {
2165 "-".to_string()
2166 } else {
2167 run.pending_nodes.join(", ")
2168 }
2169 );
2170 println!(
2171 "Replay fixture: {}",
2172 if run.replay_fixture.is_some() {
2173 "embedded"
2174 } else {
2175 "derived"
2176 }
2177 );
2178 for stage in &run.stages {
2179 let worker = stage.metadata.get("worker");
2180 let worker_suffix = worker
2181 .and_then(|value| value.get("name"))
2182 .and_then(|value| value.as_str())
2183 .map(|name| format!(" worker={name}"))
2184 .unwrap_or_default();
2185 println!(
2186 "- {} [{}] status={} outcome={} branch={}{}",
2187 stage.node_id,
2188 stage.kind,
2189 stage.status,
2190 stage.outcome,
2191 stage.branch.clone().unwrap_or_else(|| "-".to_string()),
2192 worker_suffix,
2193 );
2194 if let Some(worker) = worker {
2195 if let Some(worker_id) = worker.get("id").and_then(|value| value.as_str()) {
2196 println!(" worker_id: {worker_id}");
2197 }
2198 if let Some(child_run_id) = worker.get("child_run_id").and_then(|value| value.as_str())
2199 {
2200 println!(" child_run_id: {child_run_id}");
2201 }
2202 if let Some(child_run_path) = worker
2203 .get("child_run_path")
2204 .and_then(|value| value.as_str())
2205 {
2206 println!(" child_run_path: {child_run_path}");
2207 }
2208 }
2209 }
2210 if let Some(observability) = &run.observability {
2211 for round in &observability.planner_rounds {
2212 println!(
2213 "- planner {} iterations={} llm_calls={} tools={} research_facts={}",
2214 round.node_id,
2215 round.iteration_count,
2216 round.llm_call_count,
2217 round.tool_execution_count,
2218 round.research_facts.len()
2219 );
2220 }
2221 for pointer in &observability.transcript_pointers {
2222 println!(
2223 "- transcript {} [{}] available={} {}",
2224 pointer.label,
2225 pointer.kind,
2226 pointer.available,
2227 pointer
2228 .path
2229 .clone()
2230 .unwrap_or_else(|| pointer.location.clone())
2231 );
2232 }
2233 for event in &observability.daemon_events {
2234 println!(
2235 "- daemon {} [{:?}] at {}",
2236 event.name, event.kind, event.timestamp
2237 );
2238 println!(" id: {}", event.daemon_id);
2239 println!(" persist_path: {}", event.persist_path);
2240 if let Some(summary) = &event.payload_summary {
2241 println!(" payload: {summary}");
2242 }
2243 }
2244 }
2245 if let Some(compare_path) = compare {
2246 let baseline = load_run_record_or_exit(Path::new(compare_path));
2247 print_run_diff(&harn_vm::orchestration::diff_run_records(&baseline, &run));
2248 }
2249}
2250
2251fn eval_run_record(
2252 path: &str,
2253 compare: Option<&str>,
2254 structural_experiment: Option<&str>,
2255 argv: &[String],
2256 llm_mock_mode: &commands::run::CliLlmMockMode,
2257) {
2258 if let Some(experiment) = structural_experiment {
2259 let path_buf = PathBuf::from(path);
2260 if !path_buf.is_file() || path_buf.extension().and_then(|ext| ext.to_str()) != Some("harn")
2261 {
2262 eprintln!(
2263 "--structural-experiment currently requires a .harn pipeline path, got {path}"
2264 );
2265 process::exit(1);
2266 }
2267 if compare.is_some() {
2268 eprintln!("--compare cannot be combined with --structural-experiment");
2269 process::exit(1);
2270 }
2271 if matches!(llm_mock_mode, commands::run::CliLlmMockMode::Record { .. }) {
2272 eprintln!("--llm-mock-record cannot be combined with --structural-experiment");
2273 process::exit(1);
2274 }
2275 let path_buf = fs::canonicalize(&path_buf).unwrap_or_else(|error| {
2276 command_error(&format!(
2277 "failed to canonicalize structural eval pipeline {}: {error}",
2278 path_buf.display()
2279 ))
2280 });
2281 run_structural_experiment_eval(&path_buf, experiment, argv, llm_mock_mode);
2282 return;
2283 }
2284
2285 let path_buf = PathBuf::from(path);
2286 if path_buf.is_file() && file_looks_like_persona_eval_ladder_manifest(&path_buf) {
2287 if compare.is_some() {
2288 eprintln!("--compare is not supported with persona eval ladder manifests");
2289 process::exit(1);
2290 }
2291 let manifest = load_persona_eval_ladder_manifest_or_exit(&path_buf);
2292 let report =
2293 harn_vm::orchestration::run_persona_eval_ladder(&manifest).unwrap_or_else(|error| {
2294 eprintln!(
2295 "Failed to evaluate persona eval ladder {}: {error}",
2296 path_buf.display()
2297 );
2298 process::exit(1);
2299 });
2300 print_persona_ladder_report(&report);
2301 if !report.pass {
2302 process::exit(1);
2303 }
2304 return;
2305 }
2306
2307 if path_buf.is_file() && file_looks_like_eval_pack_manifest(&path_buf) {
2308 if compare.is_some() {
2309 eprintln!("--compare is not supported with eval pack manifests");
2310 process::exit(1);
2311 }
2312 let manifest = load_eval_pack_manifest_or_exit(&path_buf);
2313 let report = harn_vm::orchestration::evaluate_eval_pack_manifest(&manifest).unwrap_or_else(
2314 |error| {
2315 eprintln!(
2316 "Failed to evaluate eval pack {}: {error}",
2317 path_buf.display()
2318 );
2319 process::exit(1);
2320 },
2321 );
2322 print_eval_pack_report(&report);
2323 if !report.pass {
2324 process::exit(1);
2325 }
2326 return;
2327 }
2328
2329 if path_buf.is_file() && file_looks_like_eval_manifest(&path_buf) {
2330 if compare.is_some() {
2331 eprintln!("--compare is not supported with eval suite manifests");
2332 process::exit(1);
2333 }
2334 let manifest = load_eval_suite_manifest_or_exit(&path_buf);
2335 let suite = harn_vm::orchestration::evaluate_run_suite_manifest(&manifest).unwrap_or_else(
2336 |error| {
2337 eprintln!(
2338 "Failed to evaluate manifest {}: {error}",
2339 path_buf.display()
2340 );
2341 process::exit(1);
2342 },
2343 );
2344 println!(
2345 "{} {} passed, {} failed, {} total",
2346 if suite.pass { "PASS" } else { "FAIL" },
2347 suite.passed,
2348 suite.failed,
2349 suite.total
2350 );
2351 for case in &suite.cases {
2352 println!(
2353 "- {} [{}] {}",
2354 case.label.clone().unwrap_or_else(|| case.run_id.clone()),
2355 case.workflow_id,
2356 if case.pass { "PASS" } else { "FAIL" }
2357 );
2358 if let Some(path) = &case.source_path {
2359 println!(" path: {path}");
2360 }
2361 if let Some(comparison) = &case.comparison {
2362 println!(" baseline identical: {}", comparison.identical);
2363 if !comparison.identical {
2364 println!(
2365 " baseline status: {} -> {}",
2366 comparison.left_status, comparison.right_status
2367 );
2368 }
2369 }
2370 for failure in &case.failures {
2371 println!(" {failure}");
2372 }
2373 }
2374 if !suite.pass {
2375 process::exit(1);
2376 }
2377 return;
2378 }
2379
2380 let paths = collect_run_record_paths(path);
2381 if paths.len() > 1 {
2382 let mut cases = Vec::new();
2383 for path in &paths {
2384 let run = load_run_record_or_exit(path);
2385 let fixture = run
2386 .replay_fixture
2387 .clone()
2388 .unwrap_or_else(|| harn_vm::orchestration::replay_fixture_from_run(&run));
2389 cases.push((run, fixture, Some(path.display().to_string())));
2390 }
2391 let suite = harn_vm::orchestration::evaluate_run_suite(cases);
2392 println!(
2393 "{} {} passed, {} failed, {} total",
2394 if suite.pass { "PASS" } else { "FAIL" },
2395 suite.passed,
2396 suite.failed,
2397 suite.total
2398 );
2399 for case in &suite.cases {
2400 println!(
2401 "- {} [{}] {}",
2402 case.run_id,
2403 case.workflow_id,
2404 if case.pass { "PASS" } else { "FAIL" }
2405 );
2406 if let Some(path) = &case.source_path {
2407 println!(" path: {path}");
2408 }
2409 if let Some(comparison) = &case.comparison {
2410 println!(" baseline identical: {}", comparison.identical);
2411 }
2412 for failure in &case.failures {
2413 println!(" {failure}");
2414 }
2415 }
2416 if !suite.pass {
2417 process::exit(1);
2418 }
2419 return;
2420 }
2421
2422 let run = load_run_record_or_exit(&paths[0]);
2423 let fixture = run
2424 .replay_fixture
2425 .clone()
2426 .unwrap_or_else(|| harn_vm::orchestration::replay_fixture_from_run(&run));
2427 let report = harn_vm::orchestration::evaluate_run_against_fixture(&run, &fixture);
2428 println!("{}", if report.pass { "PASS" } else { "FAIL" });
2429 println!("Stages: {}", report.stage_count);
2430 if let Some(compare_path) = compare {
2431 let baseline = load_run_record_or_exit(Path::new(compare_path));
2432 print_run_diff(&harn_vm::orchestration::diff_run_records(&baseline, &run));
2433 }
2434 if !report.failures.is_empty() {
2435 for failure in &report.failures {
2436 println!("- {failure}");
2437 }
2438 }
2439 if !report.pass {
2440 process::exit(1);
2441 }
2442}
2443
2444fn print_eval_pack_report(report: &harn_vm::orchestration::EvalPackReport) {
2445 println!(
2446 "{} {} passed, {} blocking failed, {} warning, {} informational, {} total",
2447 if report.pass { "PASS" } else { "FAIL" },
2448 report.passed,
2449 report.blocking_failed,
2450 report.warning_failed,
2451 report.informational_failed,
2452 report.total
2453 );
2454 for case in &report.cases {
2455 println!(
2456 "- {} [{}] {} ({})",
2457 case.label,
2458 case.workflow_id,
2459 if case.pass { "PASS" } else { "FAIL" },
2460 case.severity
2461 );
2462 if let Some(path) = &case.source_path {
2463 println!(" path: {path}");
2464 }
2465 if let Some(comparison) = &case.comparison {
2466 println!(" baseline identical: {}", comparison.identical);
2467 if !comparison.identical {
2468 println!(
2469 " baseline status: {} -> {}",
2470 comparison.left_status, comparison.right_status
2471 );
2472 }
2473 }
2474 for failure in &case.failures {
2475 println!(" {failure}");
2476 }
2477 for warning in &case.warnings {
2478 println!(" warning: {warning}");
2479 }
2480 for item in &case.informational {
2481 println!(" info: {item}");
2482 }
2483 }
2484 for ladder in &report.ladders {
2485 println!(
2486 "- ladder {} [{}] {} ({}) first_correct={}/{}",
2487 ladder.id,
2488 ladder.persona,
2489 if ladder.pass { "PASS" } else { "FAIL" },
2490 ladder.severity,
2491 ladder.first_correct_route.as_deref().unwrap_or("<none>"),
2492 ladder.first_correct_tier.as_deref().unwrap_or("<none>")
2493 );
2494 println!(" artifacts: {}", ladder.artifact_root);
2495 for tier in &ladder.tiers {
2496 println!(
2497 " - {} [{}] {} tools={} models={} latency={}ms cost=${:.6}",
2498 tier.timeout_tier,
2499 tier.route_id,
2500 tier.outcome,
2501 tier.tool_calls,
2502 tier.model_calls,
2503 tier.latency_ms,
2504 tier.cost_usd
2505 );
2506 for reason in &tier.degradation_reasons {
2507 println!(" {reason}");
2508 }
2509 }
2510 }
2511}
2512
2513fn print_persona_ladder_report(report: &harn_vm::orchestration::PersonaEvalLadderReport) {
2514 println!(
2515 "{} ladder {} passed, {} degraded/looped, {} total",
2516 if report.pass { "PASS" } else { "FAIL" },
2517 report.passed,
2518 report.failed,
2519 report.total
2520 );
2521 println!(
2522 "first_correct: {}/{}",
2523 report.first_correct_route.as_deref().unwrap_or("<none>"),
2524 report.first_correct_tier.as_deref().unwrap_or("<none>")
2525 );
2526 println!("artifacts: {}", report.artifact_root);
2527 for tier in &report.tiers {
2528 println!(
2529 "- {} [{}] {} tools={} models={} latency={}ms cost=${:.6}",
2530 tier.timeout_tier,
2531 tier.route_id,
2532 tier.outcome,
2533 tier.tool_calls,
2534 tier.model_calls,
2535 tier.latency_ms,
2536 tier.cost_usd
2537 );
2538 for reason in &tier.degradation_reasons {
2539 println!(" {reason}");
2540 }
2541 }
2542}
2543
2544fn run_package_evals() {
2545 let paths = package::load_package_eval_pack_paths(None).unwrap_or_else(|error| {
2546 eprintln!("{error}");
2547 process::exit(1);
2548 });
2549 let mut all_pass = true;
2550 for path in &paths {
2551 println!("Eval pack: {}", path.display());
2552 let manifest = load_eval_pack_manifest_or_exit(path);
2553 let report = harn_vm::orchestration::evaluate_eval_pack_manifest(&manifest).unwrap_or_else(
2554 |error| {
2555 eprintln!("Failed to evaluate eval pack {}: {error}", path.display());
2556 process::exit(1);
2557 },
2558 );
2559 print_eval_pack_report(&report);
2560 all_pass &= report.pass;
2561 }
2562 if !all_pass {
2563 process::exit(1);
2564 }
2565}
2566
2567fn run_structural_experiment_eval(
2568 path: &Path,
2569 experiment: &str,
2570 argv: &[String],
2571 llm_mock_mode: &commands::run::CliLlmMockMode,
2572) {
2573 let baseline_dir = tempfile::Builder::new()
2574 .prefix("harn-eval-baseline-")
2575 .tempdir()
2576 .unwrap_or_else(|error| {
2577 command_error(&format!("failed to create baseline tempdir: {error}"))
2578 });
2579 let variant_dir = tempfile::Builder::new()
2580 .prefix("harn-eval-variant-")
2581 .tempdir()
2582 .unwrap_or_else(|error| {
2583 command_error(&format!("failed to create variant tempdir: {error}"))
2584 });
2585
2586 let baseline = spawn_eval_pipeline_run(path, baseline_dir.path(), None, argv, llm_mock_mode);
2587 if !baseline.status.success() {
2588 relay_subprocess_failure("baseline", &baseline);
2589 }
2590
2591 let variant = spawn_eval_pipeline_run(
2592 path,
2593 variant_dir.path(),
2594 Some(experiment),
2595 argv,
2596 llm_mock_mode,
2597 );
2598 if !variant.status.success() {
2599 relay_subprocess_failure("variant", &variant);
2600 }
2601
2602 let baseline_runs = collect_structural_eval_runs(baseline_dir.path());
2603 let variant_runs = collect_structural_eval_runs(variant_dir.path());
2604 if baseline_runs.is_empty() || variant_runs.is_empty() {
2605 eprintln!(
2606 "structural eval expected workflow run records under {} and {}, but one side was empty",
2607 baseline_dir.path().display(),
2608 variant_dir.path().display()
2609 );
2610 process::exit(1);
2611 }
2612 if baseline_runs.len() != variant_runs.len() {
2613 eprintln!(
2614 "structural eval produced different run counts: baseline={} variant={}",
2615 baseline_runs.len(),
2616 variant_runs.len()
2617 );
2618 process::exit(1);
2619 }
2620
2621 let mut baseline_ok = 0usize;
2622 let mut variant_ok = 0usize;
2623 let mut any_failures = false;
2624
2625 println!("Structural experiment: {experiment}");
2626 println!("Cases: {}", baseline_runs.len());
2627 for (baseline_run, variant_run) in baseline_runs.iter().zip(variant_runs.iter()) {
2628 let baseline_fixture = baseline_run
2629 .replay_fixture
2630 .clone()
2631 .unwrap_or_else(|| harn_vm::orchestration::replay_fixture_from_run(baseline_run));
2632 let variant_fixture = variant_run
2633 .replay_fixture
2634 .clone()
2635 .unwrap_or_else(|| harn_vm::orchestration::replay_fixture_from_run(variant_run));
2636 let baseline_report =
2637 harn_vm::orchestration::evaluate_run_against_fixture(baseline_run, &baseline_fixture);
2638 let variant_report =
2639 harn_vm::orchestration::evaluate_run_against_fixture(variant_run, &variant_fixture);
2640 let diff = harn_vm::orchestration::diff_run_records(baseline_run, variant_run);
2641 if baseline_report.pass {
2642 baseline_ok += 1;
2643 }
2644 if variant_report.pass {
2645 variant_ok += 1;
2646 }
2647 any_failures |= !baseline_report.pass || !variant_report.pass;
2648 println!(
2649 "- {} [{}]",
2650 variant_run
2651 .workflow_name
2652 .clone()
2653 .unwrap_or_else(|| variant_run.workflow_id.clone()),
2654 variant_run.task
2655 );
2656 println!(
2657 " baseline: {}",
2658 if baseline_report.pass { "PASS" } else { "FAIL" }
2659 );
2660 for failure in &baseline_report.failures {
2661 println!(" {failure}");
2662 }
2663 println!(
2664 " variant: {}",
2665 if variant_report.pass { "PASS" } else { "FAIL" }
2666 );
2667 for failure in &variant_report.failures {
2668 println!(" {failure}");
2669 }
2670 println!(" diff identical: {}", diff.identical);
2671 println!(" stage diffs: {}", diff.stage_diffs.len());
2672 println!(" tool diffs: {}", diff.tool_diffs.len());
2673 println!(" observability diffs: {}", diff.observability_diffs.len());
2674 }
2675
2676 println!("Baseline {} / {} passed", baseline_ok, baseline_runs.len());
2677 println!("Variant {} / {} passed", variant_ok, variant_runs.len());
2678
2679 if any_failures {
2680 process::exit(1);
2681 }
2682}
2683
2684fn spawn_eval_pipeline_run(
2685 path: &Path,
2686 run_dir: &Path,
2687 structural_experiment: Option<&str>,
2688 argv: &[String],
2689 llm_mock_mode: &commands::run::CliLlmMockMode,
2690) -> std::process::Output {
2691 let exe = env::current_exe().unwrap_or_else(|error| {
2692 command_error(&format!("failed to resolve current executable: {error}"))
2693 });
2694 let mut command = std::process::Command::new(exe);
2695 command.current_dir(path.parent().unwrap_or_else(|| Path::new(".")));
2696 command.arg("run");
2697 match llm_mock_mode {
2698 commands::run::CliLlmMockMode::Off => {}
2699 commands::run::CliLlmMockMode::Replay { fixture_path } => {
2700 command
2701 .arg("--llm-mock")
2702 .arg(absolute_cli_path(fixture_path));
2703 }
2704 commands::run::CliLlmMockMode::Record { fixture_path } => {
2705 command
2706 .arg("--llm-mock-record")
2707 .arg(absolute_cli_path(fixture_path));
2708 }
2709 }
2710 command.arg(path);
2711 if !argv.is_empty() {
2712 command.arg("--");
2713 command.args(argv);
2714 }
2715 command.env(harn_vm::runtime_paths::HARN_RUN_DIR_ENV, run_dir);
2716 if let Some(experiment) = structural_experiment {
2717 command.env("HARN_STRUCTURAL_EXPERIMENT", experiment);
2718 }
2719 command.output().unwrap_or_else(|error| {
2720 command_error(&format!(
2721 "failed to spawn `harn run {}` for structural eval: {error}",
2722 path.display()
2723 ))
2724 })
2725}
2726
2727fn absolute_cli_path(path: &Path) -> PathBuf {
2728 if path.is_absolute() {
2729 return path.to_path_buf();
2730 }
2731 env::current_dir()
2732 .unwrap_or_else(|_| PathBuf::from("."))
2733 .join(path)
2734}
2735
2736fn relay_subprocess_failure(label: &str, output: &std::process::Output) -> ! {
2737 let stdout = String::from_utf8_lossy(&output.stdout);
2738 let stderr = String::from_utf8_lossy(&output.stderr);
2739 if !stdout.trim().is_empty() {
2740 eprintln!("[{label}] stdout:\n{stdout}");
2741 }
2742 if !stderr.trim().is_empty() {
2743 eprintln!("[{label}] stderr:\n{stderr}");
2744 }
2745 process::exit(output.status.code().unwrap_or(1));
2746}
2747
2748fn collect_structural_eval_runs(dir: &Path) -> Vec<harn_vm::orchestration::RunRecord> {
2749 let mut paths: Vec<PathBuf> = fs::read_dir(dir)
2750 .unwrap_or_else(|error| {
2751 command_error(&format!(
2752 "failed to read structural eval run dir {}: {error}",
2753 dir.display()
2754 ))
2755 })
2756 .filter_map(|entry| entry.ok().map(|entry| entry.path()))
2757 .filter(|entry| entry.extension().and_then(|ext| ext.to_str()) == Some("json"))
2758 .collect();
2759 paths.sort();
2760 let mut runs: Vec<_> = paths
2761 .iter()
2762 .map(|path| load_run_record_or_exit(path))
2763 .collect();
2764 runs.sort_by(|left, right| {
2765 (
2766 left.started_at.as_str(),
2767 left.workflow_id.as_str(),
2768 left.task.as_str(),
2769 )
2770 .cmp(&(
2771 right.started_at.as_str(),
2772 right.workflow_id.as_str(),
2773 right.task.as_str(),
2774 ))
2775 });
2776 runs
2777}
2778
2779pub(crate) fn parse_source_file(path: &str) -> (String, Vec<harn_parser::SNode>) {
2781 ensure_builtin_signatures_installed();
2782
2783 let source = match fs::read_to_string(path) {
2784 Ok(s) => s,
2785 Err(e) => {
2786 eprintln!("Error reading {path}: {e}");
2787 process::exit(1);
2788 }
2789 };
2790
2791 let mut lexer = Lexer::new(&source);
2792 let tokens = match lexer.tokenize() {
2793 Ok(t) => t,
2794 Err(e) => {
2795 let diagnostic = harn_parser::diagnostic::render_diagnostic_with_code(
2796 &source,
2797 path,
2798 &error_span_from_lex(&e),
2799 "error",
2800 harn_parser::diagnostic::lexer_error_code(&e),
2801 &e.to_string(),
2802 Some("here"),
2803 None,
2804 );
2805 eprint!("{diagnostic}");
2806 process::exit(1);
2807 }
2808 };
2809
2810 let mut parser = Parser::new(tokens);
2811 let program = match parser.parse() {
2812 Ok(p) => p,
2813 Err(err) => {
2814 if parser.all_errors().is_empty() {
2815 let span = error_span_from_parse(&err);
2816 let diagnostic = harn_parser::diagnostic::render_diagnostic_with_code(
2817 &source,
2818 path,
2819 &span,
2820 "error",
2821 harn_parser::diagnostic::parser_error_code(&err),
2822 &harn_parser::diagnostic::parser_error_message(&err),
2823 Some(harn_parser::diagnostic::parser_error_label(&err)),
2824 harn_parser::diagnostic::parser_error_help(&err),
2825 );
2826 eprint!("{diagnostic}");
2827 } else {
2828 for e in parser.all_errors() {
2829 let span = error_span_from_parse(e);
2830 let diagnostic = harn_parser::diagnostic::render_diagnostic_with_code(
2831 &source,
2832 path,
2833 &span,
2834 "error",
2835 harn_parser::diagnostic::parser_error_code(e),
2836 &harn_parser::diagnostic::parser_error_message(e),
2837 Some(harn_parser::diagnostic::parser_error_label(e)),
2838 harn_parser::diagnostic::parser_error_help(e),
2839 );
2840 eprint!("{diagnostic}");
2841 }
2842 }
2843 process::exit(1);
2844 }
2845 };
2846
2847 (source, program)
2848}
2849
2850fn error_span_from_lex(e: &harn_lexer::LexerError) -> harn_lexer::Span {
2851 match e {
2852 harn_lexer::LexerError::UnexpectedCharacter(_, span)
2853 | harn_lexer::LexerError::UnterminatedString(span)
2854 | harn_lexer::LexerError::UnterminatedBlockComment(span) => *span,
2855 }
2856}
2857
2858fn error_span_from_parse(e: &harn_parser::ParserError) -> harn_lexer::Span {
2859 match e {
2860 harn_parser::ParserError::Unexpected { span, .. } => *span,
2861 harn_parser::ParserError::UnexpectedEof { span, .. } => *span,
2862 }
2863}
2864
2865pub(crate) async fn execute(source: &str, source_path: Option<&Path>) -> Result<String, String> {
2867 execute_with_skill_dirs(source, source_path, &[]).await
2868}
2869
2870pub(crate) async fn execute_with_skill_dirs(
2871 source: &str,
2872 source_path: Option<&Path>,
2873 cli_skill_dirs: &[PathBuf],
2874) -> Result<String, String> {
2875 execute_with_skill_dirs_and_optional_harness(source, source_path, cli_skill_dirs, None).await
2876}
2877
2878pub(crate) async fn execute_with_skill_dirs_and_harness(
2879 source: &str,
2880 source_path: Option<&Path>,
2881 cli_skill_dirs: &[PathBuf],
2882 harness: harn_vm::Harness,
2883) -> Result<String, String> {
2884 execute_with_skill_dirs_and_optional_harness(source, source_path, cli_skill_dirs, Some(harness))
2885 .await
2886}
2887
2888async fn execute_with_skill_dirs_and_optional_harness(
2889 source: &str,
2890 source_path: Option<&Path>,
2891 cli_skill_dirs: &[PathBuf],
2892 harness: Option<harn_vm::Harness>,
2893) -> Result<String, String> {
2894 let mut lexer = Lexer::new(source);
2895 let tokens = lexer.tokenize().map_err(|e| e.to_string())?;
2896 let mut parser = Parser::new(tokens);
2897 let program = parser.parse().map_err(|e| e.to_string())?;
2898
2899 let mut checker = TypeChecker::new();
2904 if let Some(path) = source_path {
2905 let graph = harn_modules::build(&[path.to_path_buf()]);
2906 if let Some(imported) = graph.imported_names_for_file(path) {
2907 checker = checker.with_imported_names(imported);
2908 }
2909 if let Some(imported) = graph.imported_type_declarations_for_file(path) {
2910 checker = checker.with_imported_type_decls(imported);
2911 }
2912 if let Some(imported) = graph.imported_callable_declarations_for_file(path) {
2913 checker = checker.with_imported_callable_decls(imported);
2914 }
2915 }
2916 let type_diagnostics = checker.check(&program);
2917 let mut warning_lines = Vec::new();
2918 for diag in &type_diagnostics {
2919 match diag.severity {
2920 DiagnosticSeverity::Error => return Err(diag.message.clone()),
2921 DiagnosticSeverity::Warning => {
2922 warning_lines.push(format!("warning: {}", diag.message));
2923 }
2924 }
2925 }
2926
2927 let chunk = harn_vm::Compiler::new()
2928 .compile(&program)
2929 .map_err(|e| e.to_string())?;
2930
2931 let local = tokio::task::LocalSet::new();
2932 local
2933 .run_until(async {
2934 let mut vm = harn_vm::Vm::new();
2935 harn_vm::register_vm_stdlib(&mut vm);
2936 install_default_hostlib(&mut vm);
2937 let source_parent = source_path
2938 .and_then(|p| p.parent())
2939 .unwrap_or(std::path::Path::new("."));
2940 let project_root = harn_vm::stdlib::process::find_project_root(source_parent);
2941 let store_base = project_root.as_deref().unwrap_or(source_parent);
2942 let execution_cwd = std::env::current_dir()
2943 .unwrap_or_else(|_| std::path::PathBuf::from("."))
2944 .to_string_lossy()
2945 .into_owned();
2946 let source_dir = source_parent.to_string_lossy().into_owned();
2947 if source_path.is_some_and(is_conformance_path) {
2948 harn_vm::event_log::install_memory_for_current_thread(64);
2949 }
2950 harn_vm::register_store_builtins(&mut vm, store_base);
2951 harn_vm::register_metadata_builtins(&mut vm, store_base);
2952 let pipeline_name = source_path
2953 .and_then(|p| p.file_stem())
2954 .and_then(|s| s.to_str())
2955 .unwrap_or("default");
2956 harn_vm::register_checkpoint_builtins(&mut vm, store_base, pipeline_name);
2957 harn_vm::stdlib::process::set_thread_execution_context(Some(
2958 harn_vm::orchestration::RunExecutionRecord {
2959 cwd: Some(execution_cwd),
2960 source_dir: Some(source_dir),
2961 env: std::collections::BTreeMap::new(),
2962 adapter: None,
2963 repo_path: None,
2964 worktree_path: None,
2965 branch: None,
2966 base_ref: None,
2967 cleanup: None,
2968 },
2969 ));
2970 if let Some(ref root) = project_root {
2971 vm.set_project_root(root);
2972 }
2973 if let Some(path) = source_path {
2974 if let Some(parent) = path.parent() {
2975 if !parent.as_os_str().is_empty() {
2976 vm.set_source_dir(parent);
2977 }
2978 }
2979 }
2980 let loaded = skill_loader::load_skills(&skill_loader::SkillLoaderInputs {
2984 cli_dirs: cli_skill_dirs.to_vec(),
2985 source_path: source_path.map(Path::to_path_buf),
2986 });
2987 skill_loader::emit_loader_warnings(&loaded.loader_warnings);
2988 skill_loader::install_skills_global(&mut vm, &loaded);
2989 vm.set_harness(harness.unwrap_or_else(harn_vm::Harness::real));
2990 if let Some(path) = source_path {
2991 let extensions = package::load_runtime_extensions(path);
2992 package::install_runtime_extensions(&extensions);
2993 package::install_manifest_triggers(&mut vm, &extensions)
2994 .await
2995 .map_err(|error| format!("failed to install manifest triggers: {error}"))?;
2996 package::install_manifest_hooks(&mut vm, &extensions)
2997 .await
2998 .map_err(|error| format!("failed to install manifest hooks: {error}"))?;
2999 }
3000 let _event_log = harn_vm::event_log::active_event_log()
3001 .unwrap_or_else(|| harn_vm::event_log::install_memory_for_current_thread(64));
3002 let connector_clients_installed =
3003 should_install_default_connector_clients(source, source_path);
3004 if connector_clients_installed {
3005 install_default_connector_clients(store_base)
3006 .await
3007 .map_err(|error| format!("failed to initialize connector clients: {error}"))?;
3008 }
3009 let execution_result = vm.execute(&chunk).await.map_err(|e| e.to_string());
3010 harn_vm::egress::reset_egress_policy_for_host();
3011 if connector_clients_installed {
3012 harn_vm::clear_active_connector_clients();
3013 }
3014 harn_vm::stdlib::process::set_thread_execution_context(None);
3015 execution_result?;
3016 let mut output = String::new();
3017 for wl in &warning_lines {
3018 output.push_str(wl);
3019 output.push('\n');
3020 }
3021 output.push_str(vm.output());
3022 Ok(output)
3023 })
3024 .await
3025}
3026
3027fn should_install_default_connector_clients(source: &str, source_path: Option<&Path>) -> bool {
3028 if !source_path.is_some_and(is_conformance_path) {
3029 return true;
3030 }
3031 source.contains("connector_call")
3032 || source.contains("std/connectors")
3033 || source.contains("connectors/")
3034}
3035
3036fn is_conformance_path(path: &Path) -> bool {
3037 path.components()
3038 .any(|component| component.as_os_str() == "conformance")
3039}
3040
3041async fn install_default_connector_clients(base_dir: &Path) -> Result<(), String> {
3042 let event_log = harn_vm::event_log::active_event_log()
3043 .unwrap_or_else(|| harn_vm::event_log::install_memory_for_current_thread(64));
3044 let secret_namespace = connector_secret_namespace(base_dir);
3045 let secrets: Arc<dyn harn_vm::secrets::SecretProvider> = Arc::new(
3046 harn_vm::secrets::configured_default_chain(secret_namespace)
3047 .map_err(|error| format!("failed to configure secret providers: {error}"))?,
3048 );
3049
3050 let registry = harn_vm::ConnectorRegistry::default();
3051 let metrics = Arc::new(harn_vm::MetricsRegistry::default());
3052 let inbox = Arc::new(
3053 harn_vm::InboxIndex::new(event_log.clone(), metrics.clone())
3054 .await
3055 .map_err(|error| error.to_string())?,
3056 );
3057 registry
3058 .init_all(harn_vm::ConnectorCtx {
3059 event_log,
3060 secrets,
3061 inbox,
3062 metrics,
3063 rate_limiter: Arc::new(harn_vm::RateLimiterFactory::default()),
3064 })
3065 .await
3066 .map_err(|error| error.to_string())?;
3067 let clients = registry.client_map().await;
3068 harn_vm::install_active_connector_clients(clients);
3069 Ok(())
3070}
3071
3072fn connector_secret_namespace(base_dir: &Path) -> String {
3073 match std::env::var("HARN_SECRET_NAMESPACE") {
3074 Ok(namespace) if !namespace.trim().is_empty() => namespace,
3075 _ => {
3076 let leaf = base_dir
3077 .file_name()
3078 .and_then(|name| name.to_str())
3079 .filter(|name| !name.is_empty())
3080 .unwrap_or("workspace");
3081 format!("harn/{leaf}")
3082 }
3083 }
3084}
3085
3086#[cfg(test)]
3087mod main_tests {
3088 use super::{
3089 is_broken_pipe_panic_payload, normalize_serve_args,
3090 should_install_default_connector_clients,
3091 };
3092 use std::path::Path;
3093
3094 #[test]
3095 fn normalize_serve_args_inserts_a2a_for_legacy_shape() {
3096 let args = normalize_serve_args(vec![
3097 "harn".to_string(),
3098 "serve".to_string(),
3099 "--port".to_string(),
3100 "3000".to_string(),
3101 "agent.harn".to_string(),
3102 ]);
3103 assert_eq!(
3104 args,
3105 vec![
3106 "harn".to_string(),
3107 "serve".to_string(),
3108 "a2a".to_string(),
3109 "--port".to_string(),
3110 "3000".to_string(),
3111 "agent.harn".to_string(),
3112 ]
3113 );
3114 }
3115
3116 #[test]
3117 fn normalize_serve_args_preserves_explicit_subcommands() {
3118 let args = normalize_serve_args(vec![
3119 "harn".to_string(),
3120 "serve".to_string(),
3121 "acp".to_string(),
3122 "server.harn".to_string(),
3123 ]);
3124 assert_eq!(
3125 args,
3126 vec![
3127 "harn".to_string(),
3128 "serve".to_string(),
3129 "acp".to_string(),
3130 "server.harn".to_string(),
3131 ]
3132 );
3133 }
3134
3135 #[test]
3136 fn conformance_skips_connector_clients_unless_fixture_uses_connectors() {
3137 let path = Path::new("conformance/tests/language/basic.harn");
3138 assert!(!should_install_default_connector_clients(
3139 "__io_println(1)",
3140 Some(path)
3141 ));
3142 assert!(!should_install_default_connector_clients(
3143 "trust_graph_verify_chain()",
3144 Some(path)
3145 ));
3146 assert!(should_install_default_connector_clients(
3147 "import { post_message } from \"std/connectors/slack\"",
3148 Some(path)
3149 ));
3150 assert!(should_install_default_connector_clients(
3151 "__io_println(1)",
3152 Some(Path::new("examples/demo.harn"))
3153 ));
3154 }
3155
3156 #[test]
3157 fn broken_pipe_print_panic_is_classified_as_clean_consumer_close() {
3158 let payload = String::from("failed printing to stdout: Broken pipe (os error 32)");
3159 assert!(is_broken_pipe_panic_payload(&payload));
3160 }
3161
3162 #[test]
3163 fn unrelated_panic_is_not_classified_as_broken_pipe() {
3164 let payload = String::from("assertion failed: expected true");
3165 assert!(!is_broken_pipe_panic_payload(&payload));
3166 }
3167}