Skip to main content

harn_cli/
lib.rs

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