app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

commit 769125b310c4d4fc3b796d2b2ec6f9f6ed36c2b1
parent 5d4339aabdba86e7f88eb4c05fe7fefc4975bb09
Author: triesap <tyson@radroots.org>
Date:   Tue,  6 Oct 2026 06:02:31 +0000

build: Add conservative affected checks and native test inputs

- Select runtime checks from capsule merge bases and current ownership.
- Fingerprint bounded source and default tool inputs without accepting reuse.
- Add explicit aggregate checks while preserving runtime tool boundaries.
- Track native library bytes in Gradle tests and verify rerun behavior.

Diffstat:
MMakefile | 24++++++++++++++++++++++++
MREADME.md | 31+++++++++++++++++++++++++------
Mbuild-logic/plugins/src/functionalTest/kotlin/org/harvestcircle/buildlogic/plugins/ConventionPluginSmokeTest.kt | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mbuild-logic/plugins/src/main/kotlin/org/harvestcircle/buildlogic/plugins/HarvestCircleRustFfiPlugin.kt | 7+++++++
Mconfig/repository/runtime-ownership.v1.json | 10++++++++++
Atools/check-all.sh | 36++++++++++++++++++++++++++++++++++++
Atools/xtask/src/affected.rs | 616+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/xtask/src/lib.rs | 1+
Mtools/xtask/src/main.rs | 45+++++++++++++++++++++++++++++++++++++++++++++
Atools/xtask/tests/affected_cli.rs | 516+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 files changed, 1379 insertions(+), 6 deletions(-)

diff --git a/Makefile b/Makefile @@ -5,6 +5,9 @@ override CARGO := cargo +1.97.1 CARGO_MANIFEST := core/Cargo.toml XTASK_MANIFEST := tools/xtask/Cargo.toml BUILD_MODE ?= standalone +# Recipes use this mode explicitly. Standalone fixture Make processes must not +# inherit it as an environment default; recursive governed targets pass it. +unexport BUILD_MODE VALID_BUILD_MODES := standalone governed ifeq ($(filter $(BUILD_MODE),$(VALID_BUILD_MODES)),) @@ -21,6 +24,26 @@ endif .PHONY: native-doctor native-dev native-check native-build native-package-check .PHONY: web-doctor web-install web-dev web-check web-build .PHONY: interop-check +.PHONY: repo-check web-integration-check check-all affected-check + +repo-check: + HARVESTCIRCLE_BUILD_MODE=$(BUILD_MODE) $(BUILD_RUNNER) $(CARGO) run --manifest-path $(XTASK_MANIFEST) --locked -- repo-audit + +# Missing BASE deliberately widens selection; this diagnostic executes no lane. +affected-check: export HARVESTCIRCLE_AFFECTED_BASE = $(BASE) +affected-check: + HARVESTCIRCLE_BUILD_MODE=$(BUILD_MODE) $(BUILD_RUNNER) $(CARGO) run --manifest-path $(XTASK_MANIFEST) --locked -- affected-report + +web-integration-check: web-doctor + cd web && $(BUILD_RUNNER) corepack pnpm run test:integration + cd web && $(BUILD_RUNNER) corepack pnpm run test:e2e + +check-all: + HARVESTCIRCLE_BUILD_MODE=$(BUILD_MODE) $(BUILD_RUNNER) tools/check-all.sh + +ifeq ($(BUILD_MODE),governed) +repo-check affected-check check-all: governed-doctor +endif interop-check: $(BUILD_RUNNER) tools/check-interop.sh @@ -60,6 +83,7 @@ native-package-check: package-check help: @printf '%s\n' 'Unqualified targets below retain native meaning.' 'Browser: web-doctor web-install web-dev web-check web-build' @printf '%s\n' 'Explicit cross-runtime fixture gate: interop-check' + @printf '%s\n' 'Repository: repo-check affected-check BASE=<commit>' 'Browser integration: web-integration-check' 'Explicit all-runtime source aggregate: check-all' @printf '%s\n' doctor governed-doctor lock metadata build-logic-check build-logic-stability-check mode-check design-source-check design-goldens-update format format-fix lint test check governed-check build bindings api-check dev-check dev run audit licenses foundation-check package host-package-check governed-package-check source-check governed-source-check package-check integration-check governed-integration-check development-check governed-development-check governed-linux-x86_64-development-check host-ui-lifecycle-check acceptance-check unsigned-release-check signing-check notarization-check release-check clean design-source-check: doctor diff --git a/README.md b/README.md @@ -107,12 +107,31 @@ authority. Listing a target does not authorize its deferred effects. ## Conformance and source provenance -Planned public test vectors under `contracts/interop/` will compare browser -Radroots adapters with the exact pinned public Rust oracle. Ordinary browser -conformance will use checked vectors without invoking native tools; explicit -interop qualification will execute both real consumers. Food/Message interop -remains unimplemented; the current Node-only conformance script checks the -actual bootstrap static output. +Checked Food fixtures under `contracts/interop/` exercise the exact pinned +public Rust codec and browser adapters. `make interop-check` runs both actual +consumers and reports unsupported browser cases explicitly. Ordinary browser +conformance uses checked vectors without invoking native tools. This is source +conformance evidence; Message interoperability and independent-client, relay, +extension and deployed Tera qualification remain pending. + +`make repo-check` runs fresh repository source policy without Node. +`make web-integration-check` runs web integration and browser tests without +native tooling. `make check-all` explicitly requires both toolchains and runs +repository, native, web, browser integration and interop checks; an unavailable +tool or failed lane fails the aggregate. It does not install dependencies. +Existing unqualified `check`, `build`, `dev` and `package-check` retain their +native meanings. + +`make affected-check BASE=<commit>` reports the actual Git merge-base diff, +dirty inputs, runtime selection and complete input fingerprints. Renames, +deletions, unknown paths, shallow or missing history widen selection to all +runtimes. It always runs fresh source policy. The report records source HEAD +separately from runtime input identity; selection and equal fingerprints do +not mean runtime checks executed or qualify reuse of old artifact provenance. +Missing tools are explicit, and the diagnostic never installs them. +Fingerprints cover default repository source checks. Custom compiler wrappers, +external build configuration or additional command arguments require separate +evidence; the diagnostic never qualifies their artifacts or automatic reuse. The native artifact contract v3 requires an exact clean Git-revision tree source archive, including newly tracked `web/` files. A native-only filtered diff --git a/build-logic/plugins/src/functionalTest/kotlin/org/harvestcircle/buildlogic/plugins/ConventionPluginSmokeTest.kt b/build-logic/plugins/src/functionalTest/kotlin/org/harvestcircle/buildlogic/plugins/ConventionPluginSmokeTest.kt @@ -1,11 +1,13 @@ package org.harvestcircle.buildlogic.plugins import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome import org.gradle.testkit.runner.UnexpectedBuildFailure import kotlin.io.path.createTempDirectory import kotlin.io.path.createDirectories import kotlin.io.path.writeText import kotlin.test.Test +import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -251,6 +253,103 @@ class ConventionPluginSmokeTest { } @Test + fun nativeLibraryBytesInvalidateOnlyTheirActualTestConsumers() { + val fixture = createTempDirectory("harvestcircle-native-test-inputs-") + prepareRustBuild( + fixture, + """ + // Synthetic library files exercise the real Test engine and input cache. + // Native producers are disabled; this fixture never executes Cargo. + tasks.matching { + it.name in setOf( + "buildRustCoreDebug", "buildRustCoreRelease", "buildRustTestBridgeDebug", + "generateUniFfiKotlin", "generateTestBridgeUniFfiKotlin", + "generateCompatibilityExpectations", "verifyTestBridgeIsolation", + "ktlintIntegrationTestSourceSetCheck", "detektIntegrationTest", + ) + }.configureEach { enabled = false } + val fixtureJUnit = configurations.create("fixtureJUnit") + dependencies.add(fixtureJUnit.name, "junit:junit:4.13.2") + listOf("test", "integrationTest").forEach { testName -> + val compileFixture = tasks.register<org.gradle.api.tasks.compile.JavaCompile>("compileFixture" + testName) { + source(fileTree("src/" + testName + "/java")) + classpath = fixtureJUnit + destinationDirectory.set(layout.buildDirectory.dir("fixture-classes/" + testName)) + options.release.set(21) + } + tasks.named<org.gradle.api.tasks.testing.Test>(testName) { + testClassesDirs = files(compileFixture.flatMap { it.destinationDirectory }) + classpath = testClassesDirs + fixtureJUnit + } + } + tasks.withType<org.gradle.api.tasks.testing.Test>().configureEach { + systemProperty("fixture.production", rootProject.file("core/target/debug/libharvestcircle_ffi.so")) + systemProperty("fixture.bridge", rootProject.file("core/target/debug/libharvestcircle_test_bridge.so")) + } + """.trimIndent(), + ) + val libraries = fixture.resolve("core/target/debug").createDirectories() + val production = libraries.resolve("libharvestcircle_ffi.so") + val bridge = libraries.resolve("libharvestcircle_test_bridge.so") + production.writeText("production-one") + bridge.writeText("bridge-one") + listOf("test" to false, "integrationTest" to true).forEach { (sourceSet, includesBridge) -> + val bridgeAssertion = + if (includesBridge) { + """assertTrue(Files.readString(Path.of(System.getProperty("fixture.bridge"))).startsWith("bridge-"));""" + } else { + "" + } + fixture.resolve("app/desktop/src/$sourceSet/java").createDirectories() + .resolve("NativeInputTest.java").writeText( + """ + import java.nio.file.Files; + import java.nio.file.Path; + import org.junit.Test; + import static org.junit.Assert.assertTrue; + public class NativeInputTest { + @Test public void consumesDeclaredLibraries() throws Exception { + assertTrue(Files.readString(Path.of(System.getProperty("fixture.production"))).startsWith("production-")); + $bridgeAssertion + } + } + """.trimIndent(), + ) + } + val runner = + GradleRunner.create() + .withProjectDir(fixture.toFile()) + .withPluginClasspath() + // No Cargo runs; keep synthetic libraries inside this owned fixture. + .withEnvironment( + (System.getenv() - "CARGO_TARGET_DIR") + + ("EXT_BUILD_GRADLE_BUILD_DIR" to fixture.resolve("fixture-build-output").toString()), + ) + .withArguments( + ":app:desktop:test", ":app:desktop:integrationTest", + "-PnativeOs=Linux", "-PnativeArch=amd64", "--no-build-cache", "--stacktrace", + "-x", ":app:desktop:verifyTestBridgeIsolation", + "-x", ":app:desktop:ktlintIntegrationTestSourceSetCheck", + "-x", ":app:desktop:detektIntegrationTest", + ) + fun outcomes(unit: TaskOutcome, integration: TaskOutcome) { + val result = runner.build() + println("native-input outcomes: unit=" + result.task(":app:desktop:test")?.outcome + + "; integration=" + result.task(":app:desktop:integrationTest")?.outcome) + assertEquals(unit, result.task(":app:desktop:test")?.outcome, result.output) + assertEquals(integration, result.task(":app:desktop:integrationTest")?.outcome, result.output) + } + outcomes(TaskOutcome.SUCCESS, TaskOutcome.SUCCESS) + outcomes(TaskOutcome.UP_TO_DATE, TaskOutcome.UP_TO_DATE) + production.writeText("production-two") + outcomes(TaskOutcome.SUCCESS, TaskOutcome.SUCCESS) + outcomes(TaskOutcome.UP_TO_DATE, TaskOutcome.UP_TO_DATE) + bridge.writeText("bridge-two") + outcomes(TaskOutcome.UP_TO_DATE, TaskOutcome.SUCCESS) + outcomes(TaskOutcome.UP_TO_DATE, TaskOutcome.UP_TO_DATE) + } + + @Test fun rustPluginRejectsStaleGeneratedCompatibilityKotlin() { val fixture = createTempDirectory("harvestcircle-rust-stale-") prepareRustBuild( diff --git a/build-logic/plugins/src/main/kotlin/org/harvestcircle/buildlogic/plugins/HarvestCircleRustFfiPlugin.kt b/build-logic/plugins/src/main/kotlin/org/harvestcircle/buildlogic/plugins/HarvestCircleRustFfiPlugin.kt @@ -6,6 +6,7 @@ import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.JavaExec +import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.testing.Test import org.gradle.jvm.tasks.Jar import org.harvestcircle.buildlogic.contracts.FfiCompatibilityBaseline @@ -246,10 +247,16 @@ public class HarvestCircleRustFfiPlugin : Plugin<Project> { } target.tasks.named("integrationTest", Test::class.java) { task -> task.dependsOn(verifyTestIsolation) + task.inputs.file(testBridgeLibraryFile) + .withPropertyName("integrationBridgeNativeLibrary") + .withPathSensitivity(PathSensitivity.NONE) task.systemProperty("jna.library.path", testBridgeLibraryFile.parentFile.absolutePath) } target.tasks.withType(Test::class.java).configureEach { task -> task.dependsOn(buildDebug) + task.inputs.file(debugLibraryFile) + .withPropertyName("productionNativeLibrary") + .withPathSensitivity(PathSensitivity.NONE) val nativeTestData = target.layout.buildDirectory.dir("native-test-data").get().asFile.absolutePath task.environment(productEnvironment("DEVELOPMENT_DATA_DIR"), nativeTestData) task.systemProperty("${productCoordinates["product.slug"]}.development.data.dir", nativeTestData) diff --git a/config/repository/runtime-ownership.v1.json b/config/repository/runtime-ownership.v1.json @@ -319,6 +319,16 @@ "interop", "governance" ] + }, + { + "path": "tools/check-all.sh", + "kind": "exact", + "owners": [ + "native", + "web", + "interop", + "governance" + ] } ], "outputs": [ diff --git a/tools/check-all.sh b/tools/check-all.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-3.0-only +set -eu +fail() { printf '%s\n' "HarvestCircle aggregate: $*" >&2; exit 1; } +[ "$#" -eq 0 ] || fail "unexpected arguments" +repository_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$repository_root" +# Keep each selected lane's explicit mode, without exporting command-line Make +# overrides into its standalone test fixtures or starting inherited parallel jobs. +unset MAKEFLAGS MAKEOVERRIDES MFLAGS +for tool in make rustup cargo java node corepack; do + command -v "$tool" >/dev/null 2>&1 || fail "required tool is unavailable: $tool" +done +[ -x ./gradlew ] || fail "Gradle wrapper is unavailable" +java_major=$(java -version 2>&1 | sed -n 's/.*version "\([0-9]*\).*/\1/p' | head -n 1) +[ "$java_major" = 21 ] || fail "selected JDK must be 21; install/select it explicitly" +# Preflight before any consumer. Neither rustup run nor offline Corepack installs. +rustup run 1.97.1 cargo --version +# Refuse a wrapper download when its selected distribution is not installed. +gradle_version=$(sed -n 's@^distributionUrl=.*\/gradle-\([0-9.]*\)-bin.zip$@\1@p' gradle/wrapper/gradle-wrapper.properties) +[ -n "$gradle_version" ] || fail "unrecognized pinned Gradle distribution" +gradle_cache_root=${GRADLE_USER_HOME:-${HOME:?}/.gradle} +gradle_ready=false +for executable in "$gradle_cache_root/wrapper/dists/gradle-$gradle_version-bin/"*/"gradle-$gradle_version/bin/gradle"; do + if [ -x "$executable" ]; then gradle_ready=true; fi +done +[ "$gradle_ready" = true ] || fail "pinned Gradle distribution is not cached; install explicitly" +export COREPACK_ENABLE_NETWORK=0 +make --no-print-directory MAKEOVERRIDES= BUILD_MODE="${HARVESTCIRCLE_BUILD_MODE:-standalone}" web-doctor +for lane in repo-check native-check web-check web-integration-check interop-check; do + printf '%s\n' "harvestcircle.aggregate.$lane=running" + make --no-print-directory MAKEOVERRIDES= BUILD_MODE="${HARVESTCIRCLE_BUILD_MODE:-standalone}" \ + GRADLE='./gradlew -Porg.gradle.java.installations.auto-download=false' "$lane" + printf '%s\n' "harvestcircle.aggregate.$lane=pass" +done +printf '%s\n' 'harvestcircle.aggregate.qualification=source_checks_only' diff --git a/tools/xtask/src/affected.rs b/tools/xtask/src/affected.rs @@ -0,0 +1,616 @@ +//! Conservative diagnostics. Selection and input equality never certify execution. +use crate::runtime_ownership::{Owner, OwnershipMap}; +use crate::{ + Command, Inventory, bounded_no_follow_bytes, git_command, load_ownership, run, + validate_product_root, +}; +use serde_json::{Value, json}; +use sha2::{Digest, Sha256}; +use std::collections::BTreeSet; +use std::path::{Path, PathBuf}; +use std::process::Command as ProcessCommand; + +fn git(root: &Path, args: &[&str]) -> Result<Vec<u8>, String> { + let output = git_command(root) + .args(args) + .output() + .map_err(|_| "Git unavailable".to_owned())?; + if !output.status.success() { + return Err(format!("Git operation failed: {}", args[0])); + } + Ok(output.stdout) +} + +fn revision(root: &Path, name: &str) -> Result<String, String> { + if name.is_empty() || name.len() > 1024 || name.chars().any(char::is_control) { + return Err("invalid revision argument".to_owned()); + } + let expression = format!("{name}^{{commit}}"); + let bytes = git( + root, + &["rev-parse", "--verify", "--end-of-options", &expression], + )?; + let value = std::str::from_utf8(&bytes) + .map_err(|_| "invalid revision output")? + .trim(); + if value.len() != 40 || !value.bytes().all(|b| b.is_ascii_hexdigit()) { + return Err("unsupported Git object identity".to_owned()); + } + Ok(value.to_owned()) +} + +pub fn discover_root(start: &Path) -> Result<PathBuf, String> { + let mut root = start + .canonicalize() + .map_err(|_| "unable to resolve current directory")?; + loop { + if root + .join("config/product/harvestcircle-v1.properties") + .is_file() + { + return validate_product_root(&root); + } + if !root.pop() { + return Err("no HarvestCircle product root above current directory".to_owned()); + } + } +} + +/// Parse Git's NUL-delimited name-status stream without shell/path quoting. +fn changed_paths(bytes: &[u8]) -> Result<(BTreeSet<String>, bool), String> { + if !bytes.is_empty() && bytes.last() != Some(&0) { + return Err("unterminated Git name-status stream".to_owned()); + } + let mut tokens = bytes.split(|b| *b == 0).collect::<Vec<_>>(); + if tokens.last() == Some(&&b""[..]) { + tokens.pop(); + } + let mut position = 0; + let mut paths = BTreeSet::new(); + let mut ambiguous = false; + while position < tokens.len() { + let status = std::str::from_utf8(tokens[position]).map_err(|_| "invalid Git status")?; + position += 1; + let count = match status.as_bytes().first() { + Some(b'R' | b'C') + if status[1..].bytes().all(|b| b.is_ascii_digit()) && status.len() > 1 => + { + ambiguous = true; + 2 + } + Some(b'D') if status == "D" => { + ambiguous = true; + 1 + } + Some(b'A' | b'M' | b'T') if status.len() == 1 => 1, + _ => return Err("ambiguous Git status".to_owned()), + }; + for _ in 0..count { + let raw = tokens.get(position).ok_or("missing Git path")?; + let path = std::str::from_utf8(raw).map_err(|_| "Git path is not UTF-8")?; + // Reuse the ownership parser's literal path validation, including escapes. + // The report caller classifies every admitted path before narrow selection. + if path.is_empty() { + return Err("empty Git path".to_owned()); + } + paths.insert(path.to_owned()); + position += 1; + } + } + Ok((paths, ambiguous)) +} + +fn selection(root: &Path, map: &OwnershipMap, base: Option<&str>, head: &str) -> Value { + let mut reasons = Vec::new(); + let mut paths = BTreeSet::new(); + let mut selected = BTreeSet::new(); + let mut merge_base = None; + let mut resolved_head = None; + let mut checked_out_head = None; + let result = (|| -> Result<(), String> { + let top = git(root, &["rev-parse", "--show-toplevel"])?; + let top = std::str::from_utf8(&top) + .map_err(|_| "Git root is not UTF-8")? + .trim_end_matches('\n'); + if Path::new(top).canonicalize().ok().as_deref() != Some(root) { + return Err( + "capsule has no independent Git history; enclosing history is not its base" + .to_owned(), + ); + } + let current = revision(root, "HEAD")?; + checked_out_head = Some(current.clone()); + if git(root, &["rev-parse", "--is-shallow-repository"])? != b"false\n" { + return Err("shallow history cannot prove complete selection".to_owned()); + } + let requested = revision(root, head)?; + resolved_head = Some(requested.clone()); + if current != requested { + return Err("requested head differs from checked-out source".to_owned()); + } + let base = revision(root, base.ok_or("base not supplied")?)?; + let common = git(root, &["merge-base", &base, &requested])?; + let common = std::str::from_utf8(&common) + .map_err(|_| "invalid merge base")? + .trim() + .to_owned(); + if common.len() != 40 { + return Err("missing unique merge base".to_owned()); + } + merge_base = Some(common.clone()); + for pair in [vec![common.as_str(), requested.as_str()], vec!["HEAD"]] { + let mut args = vec![ + "diff", + "--name-status", + "-z", + "--find-renames", + "--no-ext-diff", + "--no-textconv", + ]; + args.extend(pair); + args.push("--"); + let (changed, ambiguous) = changed_paths(&git(root, &args)?)?; + paths.extend(changed); + if ambiguous { + reasons.push("rename, copy or deletion widens all runtimes".to_owned()); + } + } + let untracked = git( + root, + &[ + "ls-files", + "--others", + "--exclude-standard", + "-z", + "--", + ".", + ], + )?; + for raw in untracked.split(|b| *b == 0).filter(|p| !p.is_empty()) { + paths.insert( + std::str::from_utf8(raw) + .map_err(|_| "untracked path is not UTF-8")? + .to_owned(), + ); + } + for path in &paths { + let classification = map.classify(path)?; + if let Some(reason) = classification.reason { + reasons.push(reason); + } + for owner in classification.owners { + if owner == Owner::Governance { + selected.extend([Owner::Native, Owner::Web, Owner::Interop]); + } else { + selected.insert(owner); + } + } + } + Ok(()) + })(); + if let Err(reason) = result { + reasons.push(reason); + } + if !reasons.is_empty() { + selected.extend([Owner::Native, Owner::Web, Owner::Interop]); + } + json!({"merge_base": merge_base, "resolved_head": resolved_head, "checked_out_head": checked_out_head, "changed_paths": paths, + "reasons": reasons, "native": selected.contains(&Owner::Native), "web": selected.contains(&Owner::Web), + "interop": selected.contains(&Owner::Interop), "repository_check": "REQUIRED_FRESH", "execution": "NOT_EXECUTED"}) +} + +fn digest(bytes: &[u8]) -> String { + format!("{:x}", Sha256::digest(bytes)) +} + +fn tool(root: &Path, program: &str, arguments: &[&str]) -> Option<String> { + let output = ProcessCommand::new(program) + .args(arguments) + .current_dir(root) + .env("COREPACK_ENABLE_NETWORK", "0") + .env_remove("JAVA_TOOL_OPTIONS") + .env_remove("JDK_JAVA_OPTIONS") + .env_remove("_JAVA_OPTIONS") + .env_remove("JAVA_OPTS") + .env_remove("GRADLE_OPTS") + .env_remove("NODE_OPTIONS") + .output() + .ok()?; + if !output.status.success() || output.stdout.len() + output.stderr.len() > 65536 { + return None; + } + let mut bytes = output.stdout; + bytes.extend(output.stderr); + let text = std::str::from_utf8(&bytes).ok()?.trim().to_owned(); + (!text.is_empty()).then_some(text) +} + +fn cached_gradle(root: &Path) -> Option<String> { + let properties = bounded_no_follow_bytes( + root, + Path::new("gradle/wrapper/gradle-wrapper.properties"), + 65536, + ) + .ok()?; + let properties = std::str::from_utf8(&properties).ok()?; + let version = properties + .lines() + .find(|line| line.starts_with("distributionUrl="))? + .rsplit_once("/gradle-")? + .1 + .strip_suffix("-bin.zip")?; + if version.is_empty() || !version.bytes().all(|b| b.is_ascii_digit() || b == b'.') { + return None; + } + let cache = std::env::var_os("GRADLE_USER_HOME") + .map(PathBuf::from) + .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".gradle")))?; + let directory = cache + .join("wrapper/dists") + .join(format!("gradle-{version}-bin")); + let mut candidates = std::fs::read_dir(directory) + .ok()? + .filter_map(Result::ok) + .map(|entry| entry.path().join(format!("gradle-{version}/bin/gradle"))) + .filter(|path| path.is_file()) + .collect::<Vec<_>>(); + if candidates.len() != 1 { + return None; + } + let executable = candidates.pop()?; + tool(root, executable.to_str()?, &["--offline", "--version"]) +} + +const COMPILER_ENV: &[&str] = &[ + "CC", "CXX", "AR", "LD", "RANLIB", "CFLAGS", "CXXFLAGS", "CPPFLAGS", "LDFLAGS", +]; + +fn relevant_native_environment(name: &str) -> bool { + [ + "CARGO_BUILD_", + "CARGO_TARGET_", + "CARGO_PROFILE_", + "ORG_GRADLE_PROJECT_", + ] + .iter() + .any(|prefix| name.starts_with(prefix)) + || COMPILER_ENV.iter().any(|base| { + name == *base + || name.starts_with(&format!("{base}_")) + || name == format!("HOST_{base}") + || name == format!("TARGET_{base}") + }) +} + +fn environment_identity( + fixed: &[&str], + dynamic_native: bool, +) -> Result<serde_json::Map<String, Value>, String> { + let mut names = fixed + .iter() + .map(|name| (*name).to_owned()) + .collect::<BTreeSet<_>>(); + if dynamic_native { + for (name, _) in std::env::vars_os() { + if let Some(name) = name.to_str() { + if relevant_native_environment(name) { + names.insert(name.to_owned()); + } + } else if name.as_encoded_bytes().starts_with(b"CARGO_BUILD_") + || name.as_encoded_bytes().starts_with(b"CARGO_TARGET_") + || name.as_encoded_bytes().starts_with(b"CARGO_PROFILE_") + || name.as_encoded_bytes().starts_with(b"ORG_GRADLE_PROJECT_") + || COMPILER_ENV.iter().any(|base| { + name.as_encoded_bytes() + .starts_with(format!("{base}_").as_bytes()) + }) + { + return Err("non-UTF-8 build environment name".to_owned()); + } + } + } + if names.len() > 128 { + return Err("too many relevant build environment inputs".to_owned()); + } + let mut result = serde_json::Map::new(); + let mut total = 0; + for name in names { + if name.len() > 256 || name.chars().any(char::is_control) { + return Err("invalid build environment name".to_owned()); + } + let value = std::env::var_os(&name) + .map(|value| { + value + .into_string() + .map_err(|_| "non-UTF-8 build environment value") + }) + .transpose()?; + if let Some(value) = &value { + total += value.len(); + if value.len() > 65536 || total > 256 * 1024 { + return Err("oversized build environment input set".to_owned()); + } + } + result.insert( + name, + value.map_or(Value::Null, |value| json!(digest(value.as_bytes()))), + ); + } + Ok(result) +} + +fn external_configuration_present(root: &Path, native: bool) -> bool { + if !native { + return false; + } + // Inspect existence only, never read external configuration/credentials. Such + // configuration needs its own authority; default source diagnostics cannot + // certify its contents from a path selector or the reported compiler version. + let cargo_home = std::env::var_os("CARGO_HOME") + .map(PathBuf::from) + .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".cargo"))); + let gradle_home = std::env::var_os("GRADLE_USER_HOME") + .map(PathBuf::from) + .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".gradle"))); + let mut candidates = Vec::new(); + if let Some(home) = cargo_home { + candidates.extend([home.join("config"), home.join("config.toml")]); + } + if let Some(home) = gradle_home { + candidates.extend([ + home.join("gradle.properties"), + home.join("init.gradle"), + home.join("init.gradle.kts"), + home.join("init.d"), + ]); + } + for parent in root.ancestors().skip(1) { + candidates.extend([ + parent.join(".cargo/config"), + parent.join(".cargo/config.toml"), + ]); + } + candidates + .iter() + .any(|path| std::fs::symlink_metadata(path).is_ok()) +} + +fn fingerprint( + root: &Path, + map: &OwnershipMap, + inventory: &Inventory, + owner: Owner, +) -> Result<Value, String> { + let mut files = Vec::new(); + for path in &inventory.paths { + let metadata = std::fs::symlink_metadata(root.join(path)) + .map_err(|_| format!("missing input: {path}"))?; + if metadata.is_dir() { + continue; + } // Archive inventories include ordinary directories. + let owners = map.classify(path)?.owners; + // Interop/governance are shared proof inputs, even for a runtime-only command. + if !owners.contains(&owner) + && !owners.contains(&Owner::Governance) + && !owners.contains(&Owner::Interop) + { + continue; + } + let bytes = bounded_no_follow_bytes(root, Path::new(path), 32 * 1024 * 1024)?; + #[cfg(unix)] + let mode = { + use std::os::unix::fs::PermissionsExt; + metadata.permissions().mode() & 0o777 + }; + #[cfg(not(unix))] + let mode = if metadata.permissions().readonly() { + 0o444 + } else { + 0o666 + }; + files.push(json!({"path":path,"mode":mode,"bytes":bytes.len(),"sha256":digest(&bytes)})); + } + let environment_names: &[&str] = if owner == Owner::Native { + &[ + "HARVESTCIRCLE_BUILD_MODE", + "HARVESTCIRCLE_NATIVE_FEATURES", + "HARVESTCIRCLE_NATIVE_PROFILE", + "SOURCE_DATE_EPOCH", + "HARVESTCIRCLE_BUILD_RADROOTS_REVISION", + "HARVESTCIRCLE_BUILD_RUST_TOOLCHAIN", + "HARVESTCIRCLE_BUILD_JAVA_TOOLCHAIN", + "HARVESTCIRCLE_BUILD_KOTLIN_TOOLCHAIN", + "RUSTFLAGS", + "CARGO_ENCODED_RUSTFLAGS", + "CARGO_ENCODED_RUSTDOCFLAGS", + "RUSTDOCFLAGS", + "RUSTC", + "RUSTDOC", + "RUSTFMT", + "RUSTC_WRAPPER", + "RUSTC_WORKSPACE_WRAPPER", + "CARGO_HOME", + "GRADLE_USER_HOME", + "PATH", + "CARGO_INCREMENTAL", + "CC", + "CXX", + "AR", + "LD", + "RANLIB", + "CFLAGS", + "CXXFLAGS", + "CPPFLAGS", + "LDFLAGS", + "JAVA_TOOL_OPTIONS", + "JDK_JAVA_OPTIONS", + "_JAVA_OPTIONS", + "JAVA_OPTS", + "GRADLE_OPTS", + "JAVA_HOME", + "RUSTUP_TOOLCHAIN", + ] + } else { + &[ + "HARVESTCIRCLE_BUILD_MODE", + "NODE_OPTIONS", + "NODE_ENV", + "CI", + "COREPACK_ENABLE_NETWORK", + "PATH", + "PNPM_HOME", + ] + }; + let environment = environment_identity(environment_names, owner == Owner::Native); + let environment_finding = environment.as_ref().err().cloned(); + let environment = environment.unwrap_or_default(); + let custom = owner == Owner::Native + && (external_configuration_present(root, true) + || environment + .keys() + .any(|name| name.starts_with("ORG_GRADLE_PROJECT_")) + || [ + "RUSTC", + "RUSTDOC", + "RUSTFMT", + "RUSTC_WRAPPER", + "RUSTC_WORKSPACE_WRAPPER", + "CARGO_BUILD_RUSTC", + "CARGO_BUILD_RUSTDOC", + "CARGO_BUILD_RUSTC_WRAPPER", + "CARGO_BUILD_RUSTC_WORKSPACE_WRAPPER", + ] + .iter() + .any(|name| std::env::var_os(name).is_some_and(|value| !value.is_empty()))); + let mut tools = serde_json::Map::new(); + if owner == Owner::Native { + // rustup run fails if the pinned toolchain is absent; it never installs it. + tools.insert( + "cargo".to_owned(), + json!(tool( + root, + "rustup", + &["run", "1.97.1", "cargo", "--version"] + )), + ); + tools.insert( + "rustc".to_owned(), + json!(tool(root, "rustup", &["run", "1.97.1", "rustc", "-vV"])), + ); + tools.insert("java".to_owned(), json!(tool(root, "java", &["-version"]))); + // Only invoke an already cached distribution, never a downloading wrapper. + tools.insert("gradle".to_owned(), json!(cached_gradle(root))); + } else { + tools.insert("node".to_owned(), json!(tool(root, "node", &["--version"]))); + tools.insert( + "pnpm".to_owned(), + json!(tool(&root.join("web"), "corepack", &["pnpm", "--version"])), + ); + } + let tools_available = tools.values().all(|v| !v.is_null()); + let available = !custom && environment_finding.is_none() && tools_available; + let input = json!({"schema":1,"files":files,"environment_sha256":environment,"tools":tools, + "platform":{"os":std::env::consts::OS,"arch":std::env::consts::ARCH}, + "native_features_policy":"default unless explicitly supplied by HARVESTCIRCLE_NATIVE_FEATURES; no execution claim", + "native_profile_policy":"debug unless explicitly supplied by HARVESTCIRCLE_NATIVE_PROFILE; no execution claim"}); + Ok( + json!({"source_identity":digest(&serde_json::to_vec(&input["files"]).unwrap()), + "input_identity":if available {Some(digest(&serde_json::to_vec(&input).unwrap()))} else {None}, + "availability":if environment_finding.is_some() {"INPUT_UNAVAILABLE"} else if !tools_available {"TOOL_UNAVAILABLE"} else if custom {"UNSUPPORTED_CUSTOM_COMMAND_INPUTS"} else {"DECLARED_INPUTS_AVAILABLE"}, "environment_finding":environment_finding, "custom_command_inputs":custom, "inputs":input, + "scope":"repository default source-check commands; arbitrary feature/target/profile/config arguments, Make GRADLE overrides and injected init/project settings require separate evidence", + "execution":"NOT_EXECUTED", "reuse":"NOT_ACCEPTED_BY_THIS_REPORT"}), + ) +} + +pub fn report(start: &Path, base: Option<&str>, head: &str) -> Result<(String, bool), String> { + let root = discover_root(start)?; + let map = load_ownership(&root)?; + // Always execute current global source policy; path selection never bypasses it. + let safety = run(&root, Command::RepoAudit); + let mut selected = selection(&root, &map, base, head); + let source = selected["checked_out_head"].clone(); + let dirty = if source.is_null() { + None + } else { + git( + &root, + &["status", "--porcelain=v1", "-z", "--untracked-files=all"], + ) + .ok() + .map(|bytes| !bytes.is_empty()) + }; + let mut provenance_environment = serde_json::Map::new(); + for name in [ + "HARVESTCIRCLE_BUILD_SOURCE_COMMIT", + "HARVESTCIRCLE_BUILD_SOURCE_DIRTY", + ] { + let value = std::env::var_os(name) + .map(|value| { + value + .into_string() + .map_err(|_| "non-UTF-8 source provenance input") + }) + .transpose()?; + if value.as_ref().is_some_and(|value| value.len() > 65536) { + return Err("oversized source provenance input".to_owned()); + } + provenance_environment.insert( + name.to_owned(), + value.map_or(Value::Null, |value| json!(digest(value.as_bytes()))), + ); + } + let unavailable = |reason: String| { + json!({"availability":"INPUT_UNAVAILABLE","finding":reason, + "input_identity":null,"execution":"NOT_EXECUTED","reuse":"NOT_ACCEPTED_BY_THIS_REPORT"}) + }; + let (native, web) = match Inventory::load(&root) { + Ok(inventory) => ( + fingerprint(&root, &map, &inventory, Owner::Native).unwrap_or_else(&unavailable), + fingerprint(&root, &map, &inventory, Owner::Web).unwrap_or_else(&unavailable), + ), + Err(reason) => (unavailable(reason.clone()), unavailable(reason)), + }; + if native["input_identity"].is_null() || web["input_identity"].is_null() { + for lane in ["native", "web", "interop"] { + selected[lane] = json!(true); + } + selected["reasons"].as_array_mut().unwrap().push(json!( + "complete runtime fingerprint unavailable; checks remain required" + )); + } + let report = json!({"schema":"harvestcircle.affected.v1","selection":selected, + "source_provenance":{"head":source,"dirty":dirty,"producer_environment_sha256":provenance_environment,"artifact_evidence":"NOT_PRODUCED"}, + "repository_safety":{"status":if safety.is_ok(){"PASS_FRESH"}else{"FAIL"},"findings":safety.err().unwrap_or_default()}, + "native":native, + "web":web}); + let green = report["repository_safety"]["status"] == "PASS_FRESH"; + Ok((serde_json::to_string_pretty(&report).unwrap() + "\n", green)) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn nul_status_preserves_both_literal_rename_paths() { + let (paths, all) = + changed_paths(b"R100\0web/old name.ts\0core/new name.rs\0M\0web/src/[id]/file.ts\0") + .unwrap(); + assert!(all); + assert_eq!(paths.len(), 3); + assert!(paths.contains("web/old name.ts")); + assert!(paths.contains("core/new name.rs")); + } + #[test] + fn deletion_and_malformed_streams_cannot_be_narrowed() { + assert!(changed_paths(b"D\0web/gone.ts\0").unwrap().1); + for bytes in [ + &b"M\0unterminated"[..], + &b"R100\0one\0"[..], + &b"U\0file\0"[..], + &b"M\0\xff\0"[..], + ] { + assert!(changed_paths(bytes).is_err()); + } + assert_eq!(changed_paths(b"").unwrap(), (BTreeSet::new(), false)); + } +} diff --git a/tools/xtask/src/lib.rs b/tools/xtask/src/lib.rs @@ -1,3 +1,4 @@ +pub mod affected; pub mod runtime_ownership; use sha2::{Digest, Sha256}; diff --git a/tools/xtask/src/main.rs b/tools/xtask/src/main.rs @@ -10,6 +10,51 @@ fn main() -> ExitCode { ); return ExitCode::FAILURE; }; + if command == "affected-report" { + let mut base = env::var("HARVESTCIRCLE_AFFECTED_BASE") + .ok() + .filter(|v| !v.is_empty()); + let mut head = "HEAD".to_owned(); + let mut seen_head = false; + let mut seen_base = false; + while let Some(flag) = arguments.next() { + let Some(value) = arguments.next() else { + eprintln!("affected-report requires --base <commit> and optional --head <commit>"); + return ExitCode::FAILURE; + }; + match flag.as_str() { + "--base" if !seen_base => { + base = Some(value); + seen_base = true; + } + "--head" if !seen_head => { + head = value; + seen_head = true; + } + _ => { + eprintln!("unknown or repeated affected-report option"); + return ExitCode::FAILURE; + } + } + } + let result = env::current_dir() + .map_err(|e| e.to_string()) + .and_then(|root| harvestcircle_xtask::affected::report(&root, base.as_deref(), &head)); + return match result { + Ok((report, green)) => { + print!("{report}"); + if green { + ExitCode::SUCCESS + } else { + ExitCode::FAILURE + } + } + Err(message) => { + eprintln!("{message}"); + ExitCode::FAILURE + } + }; + } if arguments.next().is_some() { eprintln!("xtask commands do not accept positional arguments"); return ExitCode::FAILURE; diff --git a/tools/xtask/tests/affected_cli.rs b/tools/xtask/tests/affected_cli.rs @@ -0,0 +1,516 @@ +//! Actual CLI, history and input-mutation regressions in complete standalone copies. +use serde_json::Value; +use std::fs; +use std::path::{Path, PathBuf}; +use std::process::{Command, Output}; +use std::sync::atomic::{AtomicU64, Ordering}; + +static NEXT: AtomicU64 = AtomicU64::new(0); +struct Fixture(PathBuf); +impl Fixture { + fn new() -> Self { + let path = std::env::temp_dir().join(format!( + "harvestcircle affected {} {}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + )); + fs::create_dir(&path).unwrap(); + let source = Path::new(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .parent() + .unwrap(); + copy_archive_source(source, source, &path); + Self(path) + } + fn init(&self) -> String { + git(&self.0, &["init", "-b", "master"]); + self.commit() + } + fn commit(&self) -> String { + git(&self.0, &["add", "."]); + git( + &self.0, + &[ + "-c", + "user.name=fixture", + "-c", + "user.email=fixture@example.invalid", + "-c", + "commit.gpgsign=false", + "commit", + "--allow-empty", + "-m", + "fixture", + ], + ); + String::from_utf8(git(&self.0, &["rev-parse", "HEAD"]).stdout) + .unwrap() + .trim() + .to_owned() + } +} +impl Drop for Fixture { + fn drop(&mut self) { + fs::remove_dir_all(&self.0).unwrap(); + } +} +fn git(root: &Path, args: &[&str]) -> Output { + let output = Command::new("git") + .arg("-C") + .arg(root) + .args(args) + .env_remove("GIT_DIR") + .env_remove("GIT_WORK_TREE") + .env_remove("GIT_INDEX_FILE") + .env("GIT_CONFIG_GLOBAL", "/dev/null") + .env("GIT_CONFIG_NOSYSTEM", "1") + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + output +} +fn report(root: &Path, base: Option<&str>, env: &[(&str, &str)]) -> (Output, Value) { + let mut command = Command::new(env!("CARGO_BIN_EXE_harvestcircle_xtask")); + command + .current_dir(root) + .arg("affected-report") + .env_remove("HARVESTCIRCLE_AFFECTED_BASE"); + if let Some(base) = base { + command.args(["--base", base]); + } + for (name, value) in env { + command.env(name, value); + } + let output = command.output().unwrap(); + let value = serde_json::from_slice(&output.stdout) + .unwrap_or_else(|e| panic!("{e}: {}", String::from_utf8_lossy(&output.stderr))); + (output, value) +} +fn assert_all(value: &Value) { + for lane in ["native", "web", "interop"] { + assert_eq!(value["selection"][lane], true, "{}", value["selection"]); + } +} +fn append(root: &Path, path: &str) { + let p = root.join(path); + let mut text = fs::read_to_string(&p).unwrap(); + text.push('\n'); + fs::write(p, text).unwrap(); +} + +#[test] +fn web_only_changes_keep_native_source_identity_but_not_head_provenance() { + let fixture = Fixture::new(); + let base = fixture.init(); + let (first, before) = report(&fixture.0, Some(&base), &[]); + assert!(first.status.success()); + append(&fixture.0, "web/src/app.html"); + fixture.commit(); + let (second, after) = report(&fixture.0.join("web/src"), Some(&base), &[]); + assert!(second.status.success()); + assert_eq!(after["selection"]["web"], true); + if after["native"]["input_identity"].is_null() || after["web"]["input_identity"].is_null() { + assert_all(&after); + } else { + assert_eq!(after["selection"]["native"], false); + } + assert_eq!( + before["native"]["source_identity"], + after["native"]["source_identity"] + ); + assert_ne!( + before["web"]["source_identity"], + after["web"]["source_identity"] + ); + assert_ne!( + before["source_provenance"]["head"], + after["source_provenance"]["head"] + ); + assert_eq!(after["native"]["execution"], "NOT_EXECUTED"); + assert_eq!(after["native"]["reuse"], "NOT_ACCEPTED_BY_THIS_REPORT"); +} +#[test] +fn old_and_new_rename_paths_deletion_unknown_and_missing_base_widen() { + let fixture = Fixture::new(); + fixture.init(); + fs::write( + fixture.0.join("web/old name.ts"), + "export const fixture = 1;\n", + ) + .unwrap(); + fixture.commit(); + fs::rename( + fixture.0.join("web/old name.ts"), + fixture.0.join("web/new name.ts"), + ) + .unwrap(); + fixture.commit(); + // Rename detection requires an actual old-path base, not a pre-add base. + let previous = String::from_utf8(git(&fixture.0, &["rev-parse", "HEAD^"]).stdout).unwrap(); + let (_, renamed) = report(&fixture.0, Some(previous.trim()), &[]); + assert_all(&renamed); + let paths = renamed["selection"]["changed_paths"].as_array().unwrap(); + assert!(paths.contains(&Value::from("web/old name.ts"))); + assert!(paths.contains(&Value::from("web/new name.ts"))); + let deletion_base = fixture.commit(); + fs::remove_file(fixture.0.join("web/new name.ts")).unwrap(); + let (deleted, value) = report(&fixture.0, Some(&deletion_base), &[]); + assert!(!deleted.status.success()); + assert_all(&value); + assert_eq!(value["repository_safety"]["status"], "FAIL"); + fixture.commit(); + fs::write( + fixture.0.join("unknown input.txt"), + "ordinary unknown input\n", + ) + .unwrap(); + let (_, unknown) = report(&fixture.0, Some(&deletion_base), &[]); + assert_all(&unknown); + for base in [None, Some("missing-revision"), Some("bad\nrevision")] { + let (_, v) = report(&fixture.0, base, &[]); + assert_all(&v); + assert!(!v["selection"]["reasons"].as_array().unwrap().is_empty()); + } +} +#[test] +fn native_lock_shared_fixture_make_and_environment_invalidate_inputs() { + let fixture = Fixture::new(); + let base = fixture.init(); + let (_, before) = report(&fixture.0, Some(&base), &[]); + append(&fixture.0, "core/Cargo.lock"); + let (_, native) = report(&fixture.0, Some(&base), &[]); + assert_eq!(native["selection"]["native"], true); + assert_ne!( + before["native"]["source_identity"], + native["native"]["source_identity"] + ); + append(&fixture.0, "Makefile"); + let (_, both) = report(&fixture.0, Some(&base), &[]); + assert_all(&both); + let (_, environment) = report( + &fixture.0, + Some(&base), + &[("RUSTFLAGS", "--cfg harvestcircle_input_probe")], + ); + assert_ne!( + both["native"]["inputs"]["environment_sha256"], + environment["native"]["inputs"]["environment_sha256"] + ); + assert_eq!( + both["native"]["source_identity"], + environment["native"]["source_identity"] + ); + let shared = fixture.0.join("contracts/interop/affected fixture.json"); + fs::write(shared, "{}\n").unwrap(); + let (_, shared) = report(&fixture.0, Some(&base), &[]); + assert_all(&shared); + assert_ne!( + both["web"]["source_identity"], + shared["web"]["source_identity"] + ); +} +#[test] +fn archive_nested_parent_and_shallow_history_are_explicit_conservative_fallbacks() { + let fixture = Fixture::new(); + let (archive, v) = report(&fixture.0, None, &[]); + assert!(archive.status.success()); + assert_all(&v); + let parent = fixture.0.parent().unwrap().join(format!( + "harvestcircle parent {}", + NEXT.fetch_add(1, Ordering::Relaxed) + )); + fs::create_dir(&parent).unwrap(); + let capsule = parent.join("capsule"); + fs::rename(&fixture.0, &capsule).unwrap(); + git(&parent, &["init", "-b", "master"]); + let (_, v) = report(&capsule, None, &[]); + assert_all(&v); + assert!(v["source_provenance"]["head"].is_null()); + assert!( + v["selection"]["reasons"] + .as_array() + .unwrap() + .iter() + .any(|v| v.as_str().unwrap().contains("enclosing history")) + ); + fs::rename(&capsule, &fixture.0).unwrap(); + fs::remove_dir_all(parent).unwrap(); + let base = fixture.init(); + fs::write(fixture.0.join(".git/shallow"), format!("{base}\n")).unwrap(); + let (_, v) = report(&fixture.0, Some(&base), &[]); + assert_all(&v); + assert!( + v["selection"]["reasons"] + .as_array() + .unwrap() + .iter() + .any(|v| v.as_str().unwrap().contains("shallow")) + ); +} +#[test] +fn forced_generated_input_never_escapes_fresh_policy_and_missing_tools_never_claim_reuse() { + let fixture = Fixture::new(); + let base = fixture.init(); + let tools = Fixture(std::env::temp_dir().join(format!( + "harvestcircle tool absence {} {}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + ))); + fs::create_dir(&tools.0).unwrap(); + let git_executable = std::env::split_paths(&std::env::var_os("PATH").unwrap()) + .map(|path| path.join("git")) + .find(|path| path.is_file()) + .unwrap(); + #[cfg(unix)] + std::os::unix::fs::symlink(git_executable, tools.0.join("git")).unwrap(); + #[cfg(not(unix))] + fs::copy(git_executable, tools.0.join("git.exe")).unwrap(); + let (_, v) = report( + &fixture.0, + Some(&base), + &[("PATH", tools.0.to_str().unwrap())], + ); + assert_all(&v); + assert_eq!(v["native"]["availability"], "TOOL_UNAVAILABLE"); + assert_eq!(v["web"]["availability"], "TOOL_UNAVAILABLE"); + assert!(v["native"]["input_identity"].is_null()); + fs::create_dir_all(fixture.0.join("web/build")).unwrap(); + fs::write( + fixture.0.join("web/build/forced.js"), + "ordinary generated output\n", + ) + .unwrap(); + git(&fixture.0, &["add", "-f", "web/build/forced.js"]); + let (output, v) = report(&fixture.0, Some(&base), &[]); + assert!(!output.status.success()); + assert_eq!(v["repository_safety"]["status"], "FAIL"); +} + +#[test] +fn linked_worktree_has_own_history_and_malformed_paths_fail_fresh_policy() { + let fixture = Fixture::new(); + let base = fixture.init(); + let linked = Fixture(std::env::temp_dir().join(format!( + "harvestcircle affected worktree {} {}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + ))); + git( + &fixture.0, + &[ + "worktree", + "add", + "--detach", + linked.0.to_str().unwrap(), + "HEAD", + ], + ); + append(&linked.0, "web/src/app.html"); + let (output, v) = report(&linked.0.join("web"), Some(&base), &[]); + assert!(output.status.success()); + assert_eq!(v["selection"]["merge_base"], base); + assert_eq!(v["source_provenance"]["head"], base); + assert_eq!(v["source_provenance"]["dirty"], true); + fs::write( + linked.0.join("web/literal%2fescape.ts"), + "export const fixture = true;\n", + ) + .unwrap(); + let (output, v) = report(&linked.0, Some(&base), &[]); + assert!(!output.status.success()); + assert_all(&v); + assert_eq!(v["repository_safety"]["status"], "FAIL"); +} +#[cfg(unix)] +#[test] +fn executable_mode_and_profile_feature_inputs_change_identity() { + use std::os::unix::fs::PermissionsExt; + let fixture = Fixture::new(); + let base = fixture.init(); + let (_, before) = report(&fixture.0, Some(&base), &[]); + fs::set_permissions( + fixture.0.join("Makefile"), + fs::Permissions::from_mode(0o755), + ) + .unwrap(); + let (_, after) = report(&fixture.0, Some(&base), &[]); + assert_all(&after); + assert_ne!( + before["native"]["source_identity"], + after["native"]["source_identity"] + ); + assert_ne!( + before["web"]["source_identity"], + after["web"]["source_identity"] + ); + let (_, configured) = report( + &fixture.0, + Some(&base), + &[ + ("HARVESTCIRCLE_NATIVE_FEATURES", "feature_probe"), + ("HARVESTCIRCLE_NATIVE_PROFILE", "release"), + ("HARVESTCIRCLE_BUILD_MODE", "governed"), + ], + ); + assert_eq!( + after["native"]["source_identity"], + configured["native"]["source_identity"] + ); + assert_ne!( + after["native"]["inputs"]["environment_sha256"], + configured["native"]["inputs"]["environment_sha256"] + ); +} + +#[test] +fn every_consumed_non_head_native_build_input_invalidates_the_fingerprint() { + let fixture = Fixture::new(); + let base = fixture.init(); + let (_, before) = report(&fixture.0, Some(&base), &[]); + for name in [ + "SOURCE_DATE_EPOCH", + "HARVESTCIRCLE_BUILD_RADROOTS_REVISION", + "HARVESTCIRCLE_BUILD_RUST_TOOLCHAIN", + "HARVESTCIRCLE_BUILD_JAVA_TOOLCHAIN", + "HARVESTCIRCLE_BUILD_KOTLIN_TOOLCHAIN", + ] { + let (_, after) = report(&fixture.0, Some(&base), &[(name, "input_probe")]); + assert_eq!( + before["native"]["source_identity"], + after["native"]["source_identity"] + ); + assert_ne!( + before["native"]["inputs"]["environment_sha256"], + after["native"]["inputs"]["environment_sha256"], + "{name}" + ); + if !before["native"]["input_identity"].is_null() { + assert_ne!( + before["native"]["input_identity"], after["native"]["input_identity"], + "{name}" + ); + } + } + let (_, provenance) = report( + &fixture.0, + Some(&base), + &[ + ("HARVESTCIRCLE_BUILD_SOURCE_COMMIT", "source_probe"), + ("HARVESTCIRCLE_BUILD_SOURCE_DIRTY", "source_probe"), + ], + ); + assert_eq!( + before["native"]["source_identity"], + provenance["native"]["source_identity"] + ); + assert_ne!( + before["source_provenance"]["producer_environment_sha256"], + provenance["source_provenance"]["producer_environment_sha256"] + ); +} + +#[test] +fn compiler_cargo_and_jvm_families_are_bounded_hashed_and_never_qualify_custom_artifacts() { + let fixture = Fixture::new(); + let base = fixture.init(); + let (_, before) = report(&fixture.0, Some(&base), &[]); + for name in [ + "RUSTC_WRAPPER", + "CARGO_BUILD_TARGET", + "CARGO_PROFILE_RELEASE_OPT_LEVEL", + "CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUSTFLAGS", + "CFLAGS_x86_64_unknown_linux_gnu", + "HOST_CXXFLAGS", + "TARGET_CC", + "CARGO_ENCODED_RUSTDOCFLAGS", + "JAVA_OPTS", + "_JAVA_OPTIONS", + "ORG_GRADLE_PROJECT_nativeOs", + ] { + let (_, after) = report(&fixture.0, Some(&base), &[(name, "controlled_input_probe")]); + assert_eq!( + before["native"]["source_identity"], + after["native"]["source_identity"] + ); + assert_ne!( + before["native"]["inputs"]["environment_sha256"], + after["native"]["inputs"]["environment_sha256"], + "{name}" + ); + assert!( + !serde_json::to_string(&after) + .unwrap() + .contains("controlled_input_probe") + ); + assert_eq!(after["native"]["reuse"], "NOT_ACCEPTED_BY_THIS_REPORT"); + if !before["native"]["input_identity"].is_null() { + assert_ne!( + before["native"]["input_identity"], after["native"]["input_identity"], + "{name}" + ); + } + } + let oversized = "x".repeat(65537); + let (_, after) = report(&fixture.0, Some(&base), &[("RUSTFLAGS", &oversized)]); + assert_all(&after); + assert_eq!(after["native"]["availability"], "INPUT_UNAVAILABLE"); + assert!(after["native"]["input_identity"].is_null()); + assert_eq!( + after["native"]["source_identity"], + before["native"]["source_identity"] + ); +} + +fn copy_archive_source(source: &Path, directory: &Path, destination: &Path) { + for entry in fs::read_dir(directory).unwrap() { + let entry = entry.unwrap(); + let path = entry.path(); + let relative = path.strip_prefix(source).unwrap(); + let parts = relative + .components() + .map(|part| part.as_os_str().to_str().unwrap()) + .collect::<Vec<_>>(); + if matches!( + parts[0], + ".git" | ".gradle" | ".kotlin" | ".idea" | "build" | "target" | "out" + ) || parts + .iter() + .any(|part| matches!(*part, "build" | "target" | "out")) + || (matches!(parts[0], "build-logic" | "buildSrc") + && parts + .get(1) + .is_some_and(|part| matches!(*part, ".gradle" | ".kotlin"))) + || (parts[0] == "web" + && (parts.contains(&"node_modules") + || parts.get(1).is_some_and(|part| { + matches!( + *part, + ".svelte-kit" | "coverage" | "test-results" | "playwright-report" + ) + }))) + { + continue; + } + let file_type = entry.file_type().unwrap(); + assert!( + !file_type.is_symlink(), + "fixture source symlink: {relative:?}" + ); + if file_type.is_dir() { + fs::create_dir_all(destination.join(relative)).unwrap(); + copy_archive_source(source, &path, destination); + } else { + assert!( + file_type.is_file(), + "fixture source is not regular: {relative:?}" + ); + fs::create_dir_all(destination.join(relative).parent().unwrap()).unwrap(); + fs::copy(&path, destination.join(relative)).unwrap(); + } + } +}