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 *always* produce something to store, either. [`lint`] runs a
19//! linter and returns a report the caller prints: no [`AnalysisRun`], no layer,
20//! no store. A lint name is a symbol in a compiler rather than an assigned
21//! identifier, so it is an opinion about the code as it stands today rather than
22//! a durable fact about the repository (ADR-0020 v1.1) — and everything below
23//! about persistence simply does not apply to it.
24//!
25//! [`AnalysisRun`]: rto_graph::AnalysisRun
26//! [`lint`]: crate::lint
27//!
28//! It does not decide how results are *stored*. Persistence lives in `rto-graph`,
29//! which files findings in their own tables — never `nodes`/`edges`, never a
30//! provenance class, never in the exported graph artifact (ADR-0012). Nothing
31//! here can move the published `GraphArtifact` by a byte, and that is checked by
32//! test rather than assumed.
33//!
34//! No analyzer is implemented here, and no sandbox dependency is pulled in; the
35//! backends arrive behind their own features (ADR-0014).
36//!
37//! @rto:0014
38//! @rto:0012
39//!
40//! # Example
41//!
42//! ```
43//! use rto_exec::{AnalysisRequest, AnalyzerRunner, Consent, IngestRunner, Worktree};
44//! use rto_graph::SourceIdentity;
45//!
46//! let report = br#"{
47//! "schema": "roteiro.findings/v1",
48//! "analyzer": "cargo-audit",
49//! "analyzer_version": "0.21.0",
50//! "started_at": "2026-08-15T09:00:00Z",
51//! "ended_at": "2026-08-15T09:00:04Z",
52//! "exit_status": 1,
53//! "findings": [{
54//! "identity": ["RUSTSEC-2024-0001", "openssl", "0.10.5", "lock123"],
55//! "rule": "RUSTSEC-2024-0001",
56//! "severity": "high",
57//! "title": "openssl is vulnerable",
58//! "message": "upgrade to 0.10.66"
59//! }]
60//! }"#;
61//!
62//! let request = AnalysisRequest {
63//! analyzer: "cargo-audit".to_owned(),
64//! worktree: Worktree::read_only("/repo".as_ref()).expect("worktree"),
65//! network: rto_graph::NetworkPolicy::Deny,
66//! consent: Consent::Granted,
67//! source: SourceIdentity::default(),
68//! };
69//! let response = IngestRunner::new(report.to_vec()).run(&request).expect("ingest");
70//! assert_eq!(response.findings.len(), 1);
71//! assert_eq!(response.run.isolation, rto_graph::Isolation::Ingested);
72//! ```
73
74pub mod adapter;
75/// Where the pinned-asset cache lives, and the precedence that decides it.
76///
77/// Its source carries no `//!` header because `build.rs` pulls the same file in
78/// with `include!`, where an inner doc comment is a syntax error — so the module
79/// documentation lives here instead. `build.rs` needs it to find the sandbox
80/// runtime `roteiro security prefetch` installed, which is the same cache
81/// [`asset_paths::asset_root`] names; read the file's own comments for why that
82/// is shared rather than copied.
83pub mod asset_paths;
84// Asset provisioning is **always compiled**, behind no feature at all.
85//
86// It used to be `cfg(any(exec-subprocess, exec-boxlite))`, on the reading that
87// provisioning belongs to whichever backend consumes the assets. That was the
88// wrong shape and this module half-said so already: it is shared between the
89// backends and owned by neither, and the note on `SANDBOX_RUNTIME_NOTICE` below
90// records that an `exec-subprocess`-only build provisions *for a later
91// `exec-boxlite` build* — provisioning already served a backend that was not
92// compiled in.
93//
94// The bootstrap argument settles it. `AGENTS.md` tells a contributor to run
95// `roteiro security prefetch --allow-download` *before* building
96// `--features exec-boxlite`, because that build script requires the verified
97// archive at compile time. If prefetch lived behind an execution feature, you
98// would need a build with a *different* execution backend compiled in before you
99// could provision the one you actually wanted. That is circular.
100//
101// Nothing here executes anything: it downloads, digests, pins and reports. Every
102// `Command::new` in this crate is in `subprocess.rs` or `boxlite.rs`, and both
103// stay behind their features. Provisioning is not execution.
104pub mod assets;
105#[cfg(feature = "exec-boxlite")]
106pub mod boxlite;
107#[cfg(any(feature = "exec-subprocess", feature = "exec-boxlite"))]
108mod child_env;
109pub mod crossref;
110/// Emitting a `file://` URL for a local path, and reading one back.
111///
112/// Its source carries no `//!` header because `build.rs` pulls the same file in
113/// with `include!`, where an inner doc comment is a syntax error — so the module
114/// documentation lives here instead. `build.rs` is this crate's only emitter: it
115/// prints the `BOXLITE_RUNTIME_URL=` recipe an operator pastes, and parses that
116/// variable back when it is set. What reads the URL in between is `boxlite`'s
117/// own `curl`, which percent-decodes and rejects an unencoded space outright —
118/// read the file's own comments for the measurements, and for why the encoder
119/// and the decoder have to be one file rather than two.
120pub mod file_url;
121pub mod guidance;
122pub mod image_ref;
123mod ingest;
124#[cfg(feature = "exec-subprocess")]
125pub mod lint;
126pub mod lint_grant;
127#[cfg(all(feature = "exec-boxlite", feature = "exec-subprocess"))]
128pub mod lint_sandbox;
129mod runner;
130/// The per-file digests of the extracted sandbox runtime — **generated**.
131///
132/// Derived from the archives in [`runtime_pins`] by
133/// `scripts/derive-runtime-file-pins.py`, and verified by `build.rs` against
134/// what `boxlite` actually extracted, since those files rather than the archive
135/// are what `include_bytes!` puts in the binary. Same `include!` arrangement,
136/// and so the same standalone constraint; its module documentation lives here
137/// for the same reason [`runtime_pins`]'s does.
138pub mod runtime_file_pins;
139/// The pinned sandbox-runtime archives, and the host-platform selection.
140///
141/// Its source carries no `//!` header because `build.rs` pulls the same file in
142/// with `include!`, where an inner doc comment is a syntax error — so the module
143/// documentation lives here instead. Read the file's own comments for what is
144/// pinned and why it has to be.
145pub mod runtime_pins;
146pub mod sandbox_store;
147pub mod snippet;
148#[cfg(feature = "exec-subprocess")]
149pub mod subprocess;
150pub mod tool_security;
151
152pub use adapter::{
153 ADAPTERS, Adapter, AssetPaths, Invocation, LINT_ANALYZERS, NO_SNIPPET, NativeContext,
154 UNKNOWN_VERSION, adapter_for, known_analyzers, snippet_hash, snippet_hash_at,
155};
156// The linter's adapter is re-exported like any other, and — unlike any other —
157// is **not** in [`ADAPTERS`], so `ingest` cannot resolve it and nothing can file
158// its output as a layer. See [`adapter::clippy`].
159pub use adapter::clippy::{Clippy, FeatureSet};
160pub use assets::{
161 ASSETS, AssetKind, AssetSource, AssetSpec, AssetStatus, DownloadFile, Fetcher, InstalledAsset,
162 MissingAsset, SANDBOX, asset, asset_path, asset_root, assets_for, provision, provision_with,
163 resolve, status,
164};
165#[cfg(feature = "exec-boxlite")]
166pub use boxlite::{BoxliteRunner, SandboxError, SandboxProbe, sandbox_probe};
167// The RFC 3339 UTC formatter moved to `rto-graph` in #667: `rto-exec` is an
168// optional dependency of the CLI, and the CLI's render path — which is gated on
169// nothing — needs the same function to stamp a bundle's timestamps. It is
170// `okf_instant` that needs it today; the workspace-vault renderer that needed it
171// when #667 was filed was deleted by #671, whose OKF replacement reintroduced the
172// same unlinked-crate error at a new line. The move is aimed at that recurrence,
173// not at either call site.
174//
175// Re-exported under the names it has always had so this crate's own callers, and
176// `rto_exec::rfc3339_utc` in the CLI's `execution`/`remote` paths, did not have
177// to move with it.
178pub use crossref::{
179 Correspondence, Report, across_analyzers as cross_reference_across_analyzers, cross_reference,
180};
181pub use guidance::{Guidance, Line as GuidanceLine};
182pub use image_ref::{NotPinned, PinDefect, pinned_digest as image_pinned_digest};
183pub use ingest::{
184 IngestRunner, MAX_REPORT_FINDINGS, NormalizedReport, REPORT_SCHEMA, ReportFinding,
185 normalize_native,
186};
187#[cfg(feature = "exec-subprocess")]
188pub use lint::{LintError, LintOutcome, Toolchain, invocation as lint_invocation, run as run_lint};
189pub use rto_graph::{age_in_days, rfc3339_from_unix, rfc3339_utc, unix_from_rfc3339};
190// ADR-0020 §6's grant. Re-exported under `lint_`-prefixed names because the
191// concepts have twins in `rto-remote` (ADR-0019 §3) and a reader who meets
192// `ConfigGrant` in the binary must be able to see which of the two it is.
193pub use lint_grant::{
194 Backend as LintBackend, ConfigGrant as LintConfigGrant, Decision as LintDecision,
195 Reason as LintReason, Requested as LintRequested, decide as decide_lint_host,
196};
197#[cfg(all(feature = "exec-boxlite", feature = "exec-subprocess"))]
198pub use lint_sandbox::BuilderError as LintBuilderError;
199pub use runner::{
200 AnalysisRequest, AnalysisResponse, AnalyzerRunner, Consent, ExecError, Worktree,
201 check_reported_path, check_request, worktree_id,
202};
203pub use runtime_file_pins::{
204 PinnedFile, PinnedRuntimeFiles, RUNTIME_FILES, RUNTIME_FILES_VERSION, runtime_files_for,
205};
206pub use runtime_pins::{
207 PinnedArchive, RUNTIME_ARCHIVES, RUNTIME_ASSET, RUNTIME_FILE, RUNTIME_VERSION, archive_for,
208 runtime_target,
209};
210pub use sandbox_store::{
211 Attribution, CachedImage, ClearReport, ImageBytes, Objects, Preserved, RemovedImage,
212 SANDBOX_CLEAR_SCHEMA, SANDBOX_STATUS_SCHEMA, SANDBOX_STORE_DIR, SandboxStatus, Scope,
213 StoreError, Unattributed, VerifiedImage, clear as sandbox_clear, plan as sandbox_plan,
214 status as sandbox_status, store_root as sandbox_store_root,
215};
216pub use snippet::{NoSnippets, SnippetSource, WorktreeSnippets};
217#[cfg(feature = "exec-subprocess")]
218pub use subprocess::{SubprocessError, SubprocessRunner};
219pub use tool_security::{
220 AnalyzerCoverage, Coverage, CrossReference, CrossReferenceReport, LayerStaleness, MachineScope,
221 Readiness, RepositoryScope, SecurityListReport, TOOL_SECURITY_LIST_SCHEMA,
222 TOOL_SECURITY_STATUS_SCHEMA, ToolFindingsLayer, ToolSecurityList, ToolSecurityStatus,
223 coverage_matrix, coverage_matrix_with, layer_staleness, security_list, security_status,
224};
225
226/// The licence notice for the third-party binaries an `exec-boxlite` build
227/// embeds, compiled in so it cannot be separated from what it describes.
228///
229/// `roteiro security prefetch` prints it before installing the sandbox runtime,
230/// which is the same disclose-then-consent shape `roteiro model pull` uses. It
231/// is compiled into every build, because every build can provision the runtime —
232/// including one with no execution backend at all, which prefetches it for a
233/// later `exec-boxlite` build — so the obligations travel with the artifact
234/// rather than living only in the repository.
235pub const SANDBOX_RUNTIME_NOTICE: &str = include_str!("../NOTICE-boxlite-runtime.md");
236
237/// Lowercase hex SHA-256 of `bytes`.
238///
239/// Used for the report digest that ties an `AnalysisRun` to the exact bytes it
240/// was derived from, and for deriving an opaque worktree id from a path.
241#[must_use]
242pub fn sha256_hex(bytes: &[u8]) -> String {
243 use sha2::{Digest, Sha256};
244 let digest = Sha256::digest(bytes);
245 let mut out = String::with_capacity(64);
246 for byte in digest {
247 use std::fmt::Write as _;
248 let _ = write!(out, "{byte:02x}");
249 }
250 out
251}
252
253#[cfg(test)]
254mod tests {
255 use super::sha256_hex;
256
257 #[test]
258 fn hashes_the_known_vector() {
259 assert_eq!(
260 sha256_hex(b"abc"),
261 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
262 );
263 }
264}