Skip to main content

harn_cli/commands/run/
lifecycle.rs

1use std::path::PathBuf;
2
3/// Opt-in profiling. When `text` is true the run prints a categorical
4/// breakdown to stderr after execution; when `json_path` is set the same
5/// rollup is serialized to that path. Either flag enables span tracing
6/// (i.e. `harn_vm::tracing::set_tracing_enabled(true)`).
7#[derive(Clone, Debug, Default, PartialEq, Eq)]
8pub struct RunProfileOptions {
9    pub text: bool,
10    pub json_path: Option<PathBuf>,
11}
12
13impl RunProfileOptions {
14    pub fn is_enabled(&self) -> bool {
15        self.text || self.json_path.is_some()
16    }
17}
18
19/// Terminal VM outcomes that still complete through the normal CLI cleanup
20/// path. An explicit Harn `exit(code)` is not a runtime error: the CLI must
21/// preserve its captured output and emit the same receipts it would for a
22/// returned value.
23pub(super) enum TerminalRun {
24    Returned(harn_vm::VmValue),
25    ProcessExited(i32),
26}
27
28impl TerminalRun {
29    pub(super) fn exit_code(&self) -> i32 {
30        match self {
31            Self::Returned(value) => super::exit_code_from_return_value(value),
32            Self::ProcessExited(code) => *code,
33        }
34    }
35
36    pub(super) fn json_value(&self) -> serde_json::Value {
37        match self {
38            Self::Returned(value) => harn_vm::llm::vm_value_to_json(value),
39            Self::ProcessExited(_) => serde_json::Value::Null,
40        }
41    }
42
43    pub(super) fn nonzero_return_diagnostic(&self) -> Option<String> {
44        match self {
45            Self::Returned(value) if self.exit_code() != 0 => {
46                Some(super::render_return_value_error(value))
47            }
48            Self::Returned(_) | Self::ProcessExited(_) => None,
49        }
50    }
51}
52
53pub(super) enum RunExecution {
54    Terminal(TerminalRun),
55    Failed(String),
56}