1#![recursion_limit = "256"]
2
3pub mod acp;
4mod bootstrap;
5pub mod cli;
6mod cli_bytecode;
7pub mod commands;
8mod compiler_context;
9#[doc(hidden)]
10pub mod dispatch;
11mod entrypoint;
12pub mod env_guard;
13mod eval_cli;
14mod exit;
15pub mod format;
16pub mod json_envelope;
17pub use commands::check::{
18 decode_lint_envelope, decode_lint_json, lint_json_schema, DecodedLintEnvelope, LintDecodeError,
19 LintDecodeOptions, LintReportWire,
20};
21mod net;
22pub mod package;
23mod path_policy;
24mod provider_bootstrap;
25mod provider_info;
26mod run_records;
27mod runtime;
28pub mod skill_loader;
29pub mod skill_provenance;
30mod source_exec;
31pub mod test_report;
32pub mod test_runner;
33pub mod test_timing;
34#[doc(hidden)]
35pub mod tests;
36mod typecheck_imports;
37pub use commands::dispatch_explain::DISPATCH_AUDIT_SCHEMA_VERSION;
38pub(crate) use compiler_context::{
39 compiler_for_source, compiler_with_imported_enum_candidates,
40 ensure_builtin_signatures_installed, imported_enum_candidates_for_source,
41};
42pub use harn_skills::{get_embedded_skill, list_embedded_skills, EmbeddedSkill, SkillFrontmatter};
43pub(crate) use self::entrypoint::*;
47pub(crate) use self::eval_cli::*;
48pub(crate) use self::exit::*;
49pub(crate) use self::provider_info::*;
50pub(crate) use self::run_records::*;
51pub(crate) use self::source_exec::*;
52
53use clap::{error::ErrorKind, CommandFactory, Parser as ClapParser};
54use std::path::{Path, PathBuf};
55use std::sync::{Arc, Once};
56use std::{env, fs, panic, process, thread};
57
58use cli::{
59 Cli, Command, CompletionShell, EvalCommand, GuardCommand, MergeCaptainCommand,
60 MergeCaptainMockCommand, ModelInfoArgs, PackageArtifactsCommand, PackageCacheCommand,
61 PackageCommand, PackageScaffoldCommand, PgCommand, ProviderCommand, SkillCommand,
62 SkillKeyCommand, SkillTrustCommand, TimeCommand, ToolCommand,
63};
64use harn_lexer::Lexer;
65use harn_modules::project_config;
66use harn_parser::{DiagnosticSeverity, Parser, TypeChecker};
67use runtime::{build_cli_runtime, cli_runtime_mode, CliRuntimeMode};
68pub const CLI_RUNTIME_STACK_SIZE: usize = harn_vm::RUNTIME_STACK_SIZE;
71static BROKEN_PIPE_PANIC_HOOK: Once = Once::new();
72
73#[cfg(feature = "hostlib")]
74pub(crate) fn install_default_hostlib(vm: &mut harn_vm::Vm) {
75 let _ = harn_hostlib::install_default(vm);
76 harn_rules_hostlib::install(vm);
80}
81
82#[cfg(not(feature = "hostlib"))]
83pub(crate) fn install_default_hostlib(_vm: &mut harn_vm::Vm) {}
84
85pub fn run() {
88 install_broken_pipe_panic_hook();
89 harn_vm::initialize_runtime_assets();
90 let raw_args = normalize_serve_args(bootstrap::args_after_pre_runtime_command());
91 harn_vm::stdlib::force_link();
96
97 ensure_builtin_signatures_installed();
98
99 let runtime_mode = cli_runtime_mode(&raw_args);
100
101 let handle = thread::Builder::new()
102 .name("harn-cli".to_string())
103 .stack_size(CLI_RUNTIME_STACK_SIZE)
104 .spawn(move || {
105 let runtime = build_cli_runtime(runtime_mode);
106 runtime.block_on(async_main(raw_args, runtime_mode));
107 if let Err(error) = harn_vm::events::shutdown_otel_sink() {
114 eprintln!("[harn] OTel exporter shutdown failed: {error}");
115 }
116 })
117 .unwrap_or_else(|error| {
118 eprintln!("failed to start CLI runtime thread: {error}");
119 process::exit(1);
120 });
121
122 if let Err(payload) = handle.join() {
123 if runtime::is_broken_pipe_panic_payload(payload.as_ref()) {
124 process::exit(0);
125 }
126 std::panic::resume_unwind(payload);
127 }
128}
129
130fn install_broken_pipe_panic_hook() {
131 BROKEN_PIPE_PANIC_HOOK.call_once(|| {
132 let previous = panic::take_hook();
133 panic::set_hook(Box::new(move |info| {
134 if runtime::is_broken_pipe_panic_payload(info.payload()) {
135 return;
136 }
137 previous(info);
138 }));
139 });
140}