Skip to main content

locode_exec/
lib.rs

1//! locode-exec — the minimal headless CLI (Task 14, ADR-0009), as a library.
2//!
3//! The crate ships both a binary (the stock CLI) and this lib target so
4//! downstream binary crates can reuse the *entire* CLI — flags, session
5//! assembly, stdout discipline, exit codes — while injecting custom providers
6//! (ADR-0015):
7//!
8//! ```no_run
9//! use locode_exec::{ProviderRegistry, main_with};
10//!
11//! fn main() -> std::process::ExitCode {
12//!     main_with(ProviderRegistry::builtin() /* .register("my-wire", …) */)
13//! }
14//! ```
15//!
16//! Stdout discipline: stdout carries exactly one machine-readable artifact
17//! (the JSON report, the final message, or the JSONL event stream — by
18//! `--output-format`), enforced structurally: the workspace denies
19//! `print_stdout`/`print_stderr`, and the ONLY allows live in `output.rs`
20//! (Codex-exec's pattern). All diagnostics go to stderr; exit codes map the
21//! run's terminal status (0 = structured, 1 = fatal, 2 = usage via clap).
22
23use std::process::ExitCode;
24
25use clap::Parser;
26
27pub mod cli;
28mod logging;
29mod output;
30pub mod run;
31#[cfg(unix)]
32mod signal;
33
34pub use cli::{Cli, Harness, OutputFormat};
35pub use locode_core::ProviderRegistry;
36
37/// Parse the CLI, install logging, and drive one session to its exit code —
38/// the whole binary, minus `fn main`. Custom providers come in through
39/// `registry`; the stock binary passes [`ProviderRegistry::builtin`].
40#[must_use]
41// By-value on purpose: the downstream `fn main` is a single expression
42// (`main_with(builtin().register(…))`) with no borrow to scope.
43#[allow(clippy::needless_pass_by_value)]
44pub fn main_with(registry: ProviderRegistry) -> ExitCode {
45    let cli = cli::Cli::parse(); // clap usage errors exit 2 on their own
46    run_headless(cli, registry)
47}
48
49/// Drive one headless run from an already-parsed [`Cli`] — installs logging,
50/// builds the runtime, runs the engine, and emits per `--output-format`.
51///
52/// Split out of [`main_with`] so a unified front-end (the `locode` binary's
53/// `-p` mode) can reuse the exact headless engine without re-parsing argv.
54/// When `locode-exec` retires, this logic migrates into the front-end crate
55/// (ADR-0019 amendment 2026-07-21).
56#[must_use]
57#[allow(clippy::needless_pass_by_value)]
58pub fn run_headless(cli: cli::Cli, registry: ProviderRegistry) -> ExitCode {
59    logging::init();
60
61    // `ExitCode` return (not `process::exit`) so buffered stdout is flushed —
62    // the "exactly one JSON document" contract survives (plan §3.2).
63    let runtime = match tokio::runtime::Builder::new_multi_thread()
64        .enable_all()
65        .build()
66    {
67        Ok(rt) => rt,
68        Err(e) => {
69            output::error_line(&format!("tokio runtime: {e}"));
70            return ExitCode::from(1);
71        }
72    };
73    match runtime.block_on(run::run(cli, &registry)) {
74        Ok(code) => code,
75        Err(pre_run) => {
76            // Config/setup failed before a run existed: no report, exit 1
77            // (Codex's pre-run pattern). Never a partial report on stdout.
78            output::error_line(&pre_run.0);
79            ExitCode::from(1)
80        }
81    }
82}