rto_exec/lib.rs
1//! The analyzer execution seam: one contract, interchangeable backends.
2//!
3//! Running an external analyzer (`cargo-audit`, `semgrep`, successors) can happen
4//! in CI, on a developer's machine, or — later — locally inside a sandbox. This
5//! crate exists so those stop being competing architectures: every backend
6//! implements one [`AnalyzerRunner`] trait, takes one [`AnalysisRequest`], and
7//! returns one [`AnalysisResponse`] of normalized findings plus run evidence. A
8//! caller never learns which backend produced a result, so adding the sandboxed
9//! and subprocess backends later changes no call site.
10//!
11//! Today there is exactly one implementation, [`IngestRunner`], which consumes a
12//! normalized report produced elsewhere. It is the zero-install default, not a
13//! fallback: it needs no container runtime and adds no isolation surface, and
14//! what it produces is byte-for-byte the shape a sandboxed run will produce.
15//!
16//! # What this crate does not do
17//!
18//! It does not decide how results are *stored*. Persistence lives in `rto-graph`,
19//! which files findings in their own tables — never `nodes`/`edges`, never a
20//! provenance class, never in the exported graph artifact (ADR-0012). Nothing
21//! here can move the published `GraphArtifact` by a byte, and that is checked by
22//! test rather than assumed.
23//!
24//! No analyzer is implemented here, and no sandbox dependency is pulled in; the
25//! backends arrive behind their own features (ADR-0014).
26//!
27//! @rto:0014
28//! @rto:0012
29//!
30//! # Example
31//!
32//! ```
33//! use rto_exec::{AnalysisRequest, AnalyzerRunner, Consent, IngestRunner, Worktree};
34//! use rto_graph::SourceIdentity;
35//!
36//! let report = br#"{
37//! "schema": "roteiro.findings/v1",
38//! "analyzer": "cargo-audit",
39//! "analyzer_version": "0.21.0",
40//! "started_at": "2026-08-15T09:00:00Z",
41//! "ended_at": "2026-08-15T09:00:04Z",
42//! "exit_status": 1,
43//! "findings": [{
44//! "identity": ["RUSTSEC-2024-0001", "openssl", "0.10.5", "lock123"],
45//! "rule": "RUSTSEC-2024-0001",
46//! "severity": "high",
47//! "title": "openssl is vulnerable",
48//! "message": "upgrade to 0.10.66"
49//! }]
50//! }"#;
51//!
52//! let request = AnalysisRequest {
53//! analyzer: "cargo-audit".to_owned(),
54//! worktree: Worktree::read_only("/repo".as_ref()).expect("worktree"),
55//! network: rto_graph::NetworkPolicy::Deny,
56//! consent: Consent::Granted,
57//! source: SourceIdentity::default(),
58//! };
59//! let response = IngestRunner::new(report.to_vec()).run(&request).expect("ingest");
60//! assert_eq!(response.findings.len(), 1);
61//! assert_eq!(response.run.isolation, rto_graph::Isolation::Ingested);
62//! ```
63
64pub mod adapter;
65// Asset provisioning is **always compiled**, behind no feature at all.
66//
67// It used to be `cfg(any(exec-subprocess, exec-boxlite))`, on the reading that
68// provisioning belongs to whichever backend consumes the assets. That was the
69// wrong shape and this module half-said so already: it is shared between the
70// backends and owned by neither, and the note on `SANDBOX_RUNTIME_NOTICE` below
71// records that an `exec-subprocess`-only build provisions *for a later
72// `exec-boxlite` build* — provisioning already served a backend that was not
73// compiled in.
74//
75// The bootstrap argument settles it. `AGENTS.md` tells a contributor to run
76// `roteiro security prefetch --allow-download` *before* building
77// `--features exec-boxlite`, because that build script requires the verified
78// archive at compile time. If prefetch lived behind an execution feature, you
79// would need a build with a *different* execution backend compiled in before you
80// could provision the one you actually wanted. That is circular.
81//
82// Nothing here executes anything: it downloads, digests, pins and reports. Every
83// `Command::new` in this crate is in `subprocess.rs` or `boxlite.rs`, and both
84// stay behind their features. Provisioning is not execution.
85pub mod assets;
86#[cfg(feature = "exec-boxlite")]
87pub mod boxlite;
88mod clock;
89pub mod crossref;
90mod ingest;
91mod runner;
92/// The pinned sandbox-runtime archives, and the host-platform selection.
93///
94/// Its source carries no `//!` header because `build.rs` pulls the same file in
95/// with `include!`, where an inner doc comment is a syntax error — so the module
96/// documentation lives here instead. Read the file's own comments for what is
97/// pinned and why it has to be.
98pub mod runtime_pins;
99pub mod snippet;
100#[cfg(feature = "exec-subprocess")]
101pub mod subprocess;
102
103pub use adapter::{
104 ADAPTERS, Adapter, AssetPaths, Invocation, NO_SNIPPET, NativeContext, UNKNOWN_VERSION,
105 adapter_for, known_analyzers, snippet_hash, snippet_hash_at,
106};
107pub use assets::{
108 ASSETS, AssetKind, AssetSource, AssetSpec, AssetStatus, DownloadFile, Fetcher, InstalledAsset,
109 MissingAsset, SANDBOX, asset, asset_path, asset_root, assets_for, provision, provision_with,
110 resolve, status,
111};
112#[cfg(feature = "exec-boxlite")]
113pub use boxlite::{BoxliteRunner, SandboxError, SandboxProbe, sandbox_probe};
114pub use clock::{age_in_days, rfc3339_from_unix, rfc3339_utc, unix_from_rfc3339};
115pub use crossref::{Correspondence, Report, cross_reference};
116pub use ingest::{
117 IngestRunner, MAX_REPORT_FINDINGS, NormalizedReport, REPORT_SCHEMA, ReportFinding,
118 normalize_native,
119};
120pub use runner::{
121 AnalysisRequest, AnalysisResponse, AnalyzerRunner, Consent, ExecError, Worktree,
122 check_reported_path, check_request, worktree_id,
123};
124pub use runtime_pins::{
125 PinnedArchive, RUNTIME_ARCHIVES, RUNTIME_ASSET, RUNTIME_FILE, RUNTIME_VERSION, archive_for,
126 runtime_target,
127};
128pub use snippet::{NoSnippets, SnippetSource, WorktreeSnippets};
129#[cfg(feature = "exec-subprocess")]
130pub use subprocess::{SubprocessError, SubprocessRunner};
131
132/// The licence notice for the third-party binaries an `exec-boxlite` build
133/// embeds, compiled in so it cannot be separated from what it describes.
134///
135/// `roteiro security prefetch` prints it before installing the sandbox runtime,
136/// which is the same disclose-then-consent shape `roteiro model pull` uses. It
137/// is compiled into every build, because every build can provision the runtime —
138/// including one with no execution backend at all, which prefetches it for a
139/// later `exec-boxlite` build — so the obligations travel with the artifact
140/// rather than living only in the repository.
141pub const SANDBOX_RUNTIME_NOTICE: &str = include_str!("../NOTICE-boxlite-runtime.md");
142
143/// Lowercase hex SHA-256 of `bytes`.
144///
145/// Used for the report digest that ties an `AnalysisRun` to the exact bytes it
146/// was derived from, and for deriving an opaque worktree id from a path.
147#[must_use]
148pub fn sha256_hex(bytes: &[u8]) -> String {
149 use sha2::{Digest, Sha256};
150 let digest = Sha256::digest(bytes);
151 let mut out = String::with_capacity(64);
152 for byte in digest {
153 use std::fmt::Write as _;
154 let _ = write!(out, "{byte:02x}");
155 }
156 out
157}
158
159#[cfg(test)]
160mod tests {
161 use super::sha256_hex;
162
163 #[test]
164 fn hashes_the_known_vector() {
165 assert_eq!(
166 sha256_hex(b"abc"),
167 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
168 );
169 }
170}