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};
35// The permission-posture notices, shared verbatim with the interactive UI so the
36// two surfaces cannot drift (ADR-0008 amendment 2026-07-24).
37pub use locode_core::ProviderRegistry;
38pub use output::{RESTRICTED_MODE_NOTICE, UNRESTRICTED_MODE_NOTICE};
39
40/// Parse the CLI, install logging, and drive one session to its exit code —
41/// the whole binary, minus `fn main`. Custom providers come in through
42/// `registry`; the stock binary passes [`ProviderRegistry::builtin`].
43#[must_use]
44// By-value on purpose: the downstream `fn main` is a single expression
45// (`main_with(builtin().register(…))`) with no borrow to scope.
46#[allow(clippy::needless_pass_by_value)]
47pub fn main_with(registry: ProviderRegistry) -> ExitCode {
48    let cli = cli::Cli::parse(); // clap usage errors exit 2 on their own
49    run_headless(cli, registry)
50}
51
52/// Drive one headless run from an already-parsed [`Cli`] — installs logging,
53/// builds the runtime, runs the engine, and emits per `--output-format`.
54///
55/// Split out of [`main_with`] so a unified front-end (the `locode` binary's
56/// `-p` mode) can reuse the exact headless engine without re-parsing argv.
57/// When `locode-exec` retires, this logic migrates into the front-end crate
58/// (ADR-0019 amendment 2026-07-21).
59#[must_use]
60#[allow(clippy::needless_pass_by_value)]
61pub fn run_headless(cli: cli::Cli, registry: ProviderRegistry) -> ExitCode {
62    logging::init();
63
64    // `ExitCode` return (not `process::exit`) so buffered stdout is flushed —
65    // the "exactly one JSON document" contract survives (plan §3.2).
66    let runtime = match tokio::runtime::Builder::new_multi_thread()
67        .enable_all()
68        .build()
69    {
70        Ok(rt) => rt,
71        Err(e) => {
72            output::error_line(&format!("tokio runtime: {e}"));
73            return ExitCode::from(1);
74        }
75    };
76    match runtime.block_on(run::run(cli, &registry)) {
77        Ok(code) => code,
78        Err(pre_run) => {
79            // Config/setup failed before a run existed: no report, exit 1
80            // (Codex's pre-run pattern). Never a partial report on stdout.
81            output::error_line(&pre_run.0);
82            ExitCode::from(1)
83        }
84    }
85}