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