fn main() {
guard_source_contamination();
stamp_build_info();
if validate_runtime_channels().is_err() {
eprintln!(
"BUILD BLOCKED: invalid or incomplete staging or production runtime channel policy"
);
std::process::exit(1);
}
println!("cargo::rerun-if-changed=src");
}
fn validate_runtime_channels() -> Result<(), ()> {
const STAGING_NAMES: [&str; 3] = [
"LEANCTX_STAGING_RUNTIME_CHANNEL_URL",
"LEANCTX_STAGING_RUNTIME_CHANNEL_SIGNATURE_URL",
"LEANCTX_STAGING_RUNTIME_CHANNEL_ROOT_KEY_HEX",
];
const PRODUCTION_NAMES: [&str; 3] = [
"LEANCTX_PRODUCTION_RUNTIME_CHANNEL_URL",
"LEANCTX_PRODUCTION_RUNTIME_CHANNEL_SIGNATURE_URL",
"LEANCTX_PRODUCTION_RUNTIME_CHANNEL_ROOT_KEY_HEX",
];
validate_channel(STAGING_NAMES)?;
validate_channel(PRODUCTION_NAMES)?;
validate_cockpit_channel(
[
"LEANCTX_STAGING_COCKPIT_CHANNEL_URL",
"LEANCTX_STAGING_COCKPIT_CHANNEL_SIGNATURE_URL",
],
STAGING_NAMES[2],
)?;
validate_cockpit_channel(
[
"LEANCTX_PRODUCTION_COCKPIT_CHANNEL_URL",
"LEANCTX_PRODUCTION_COCKPIT_CHANNEL_SIGNATURE_URL",
],
PRODUCTION_NAMES[2],
)
}
fn validate_cockpit_channel(names: [&str; 2], root_key: &str) -> Result<(), ()> {
for name in names {
println!("cargo::rerun-if-env-changed={name}");
}
match names.map(std::env::var) {
[
Err(std::env::VarError::NotPresent),
Err(std::env::VarError::NotPresent),
] => Ok(()),
[Ok(url), Ok(signature)]
if build_channel_url(&url)
&& build_channel_url(&signature)
&& std::env::var(root_key).is_ok() =>
{
Ok(())
}
_ => Err(()),
}
}
fn validate_channel(names: [&str; 3]) -> Result<(), ()> {
for name in names {
println!("cargo::rerun-if-env-changed={name}");
}
let values = names.map(std::env::var);
let absent = std::env::VarError::NotPresent;
match &values {
[Err(a), Err(b), Err(c)] if *a == absent && *b == absent && *c == absent => Ok(()),
[Ok(url), Ok(signature), Ok(key)]
if build_channel_url(url)
&& build_channel_url(signature)
&& key.len() == 64
&& key
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) =>
{
Ok(())
}
_ => Err(()),
}
}
fn build_channel_url(value: &str) -> bool {
value.len() <= 2048
&& value.bytes().all(|b| (33..127).contains(&b))
&& !value.contains(['@', '?', '#', '\\', '"'])
&& (value.starts_with("https://")
|| value.starts_with("http://127.0.0.1:")
|| value.starts_with("http://[::1]:"))
}
fn guard_source_contamination() {
let src = std::path::Path::new("src");
if !src.is_dir() {
return;
}
let mut contaminated = Vec::new();
visit_rs_files(src, &mut contaminated);
if !contaminated.is_empty() {
let list = contaminated.join(
"
",
);
panic!(
"
[1;31mBUILD BLOCKED: lean-ctx marker contamination detected[0m
The following source files contain `--- lean-ctx:` lines injected
by shell hooks during in-place editing. Remove them before building:
{list}
Prevention: use StrReplace (not perl/sed) for source edits, or set
LEAN_CTX_SHELL_PASSTHROUGH=1 before running in-place edit commands.
"
);
}
}
fn visit_rs_files(dir: &std::path::Path, out: &mut Vec<String>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
visit_rs_files(&path, out);
} else if path.extension().is_some_and(|e| e == "rs")
&& let Ok(text) = std::fs::read_to_string(&path)
&& text.lines().any(|line| line.starts_with("--- lean-ctx:"))
{
out.push(path.display().to_string());
}
}
}
fn stamp_build_info() {
for name in [
"GITHUB_ACTIONS",
"GITHUB_REPOSITORY",
"GITHUB_EVENT_NAME",
"GITHUB_WORKFLOW_REF",
"GITHUB_REF",
"GITHUB_SHA",
"LEANCTX_BUILD_KIND",
"LEANCTX_BUILD_GIT_STAMP",
] {
println!("cargo::rerun-if-env-changed={name}");
}
let (kind, commit, dirty) = build_info();
println!("cargo::rustc-env=LEANCTX_BUILD_KIND={kind}");
println!(
"cargo::rustc-env=LEANCTX_BUILD_COMMIT={}",
commit.unwrap_or_default()
);
println!("cargo::rustc-env=LEANCTX_BUILD_DIRTY={}", u8::from(dirty));
}
#[path = "src/core/build_origin.rs"]
mod build_origin;
use build_origin::is_commit;
fn build_info() -> (&'static str, Option<String>, bool) {
let env = |name: &str| std::env::var(name).ok().filter(|v| !v.is_empty());
if let Some(sha) = build_origin::release_commit(&env) {
return ("official", Some(sha), false);
}
let requested = match env("LEANCTX_BUILD_KIND").as_deref() {
None => None,
Some("dev") => Some("dev"),
Some("dogfood") => Some("dogfood"),
Some(other) => panic!(
"LEANCTX_BUILD_KIND={other:?}: only \"dev\" or \"dogfood\"; \
\"official\" is set by the release workflow alone"
),
};
if let Ok(vcs) = std::fs::read_to_string(".cargo_vcs_info.json") {
return (requested.unwrap_or("source"), vcs_info_sha1(&vcs), false);
}
if git_stamp_off() {
return (requested.unwrap_or("source"), None, false);
}
if let Some(head) = git(&["rev-parse", "HEAD"]).filter(|s| is_commit(s)) {
watch_git_head();
let dirty = git(&[
"--no-optional-locks",
"status",
"--porcelain",
"--untracked-files=no",
"--",
".",
])
.is_some_and(|s| !s.is_empty());
let promoted = git(&[
"rev-parse",
"-q",
"--verify",
"refs/remotes/origin/dogfood/instance",
])
.is_some_and(|s| s == head)
|| git(&["symbolic-ref", "-q", "--short", "HEAD"]).as_deref() == Some("dogfood");
let kind = requested.unwrap_or(if promoted { "dogfood" } else { "dev" });
return (kind, Some(head), dirty);
}
(requested.unwrap_or("source"), None, false)
}
fn git_stamp_off() -> bool {
std::env::var("LEANCTX_BUILD_GIT_STAMP").is_ok_and(|v| v == "off")
}
fn git(args: &[&str]) -> Option<String> {
let out = std::process::Command::new("git")
.args(args)
.stderr(std::process::Stdio::null())
.output()
.ok()?;
out.status
.success()
.then(|| String::from_utf8_lossy(&out.stdout).trim().to_owned())
}
fn watch_git_head() {
let mut paths = vec!["HEAD".to_owned(), "packed-refs".to_owned()];
if let Some(branch) = git(&["symbolic-ref", "-q", "HEAD"]) {
paths.push(branch);
}
for path in paths {
if let Some(abs) = git(&["rev-parse", "--path-format=absolute", "--git-path", &path])
&& std::path::Path::new(&abs).exists()
{
println!("cargo::rerun-if-changed={abs}");
}
}
}
fn vcs_info_sha1(text: &str) -> Option<String> {
let rest = &text[text.find("\"sha1\"")? + 6..];
let start = rest.find('"')? + 1;
let sha = rest[start..].get(..40)?;
is_commit(sha).then(|| sha.to_owned())
}