From 1b024dd0088f5543f6291100071e0bc77a746b11 Mon Sep 17 00:00:00 2001 From: ualtinok <94532+ualtinok@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:57:56 +0100 Subject: [PATCH 1/2] mason: stage native CI binaries from private Cargo outputs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cargo reports the final target path even for fresh artifacts. Keep the existing target as the shared intermediate build directory and isolate each job’s final outputs before staging the reported executable and any matching MSVC PDB. The workflow contracts now assert that job-local source instead of the shared path; release artifact names and debug-symbol identity checks remain unchanged. Co-authored-by: alfonso-aft <289616620+alfonso-aft@users.noreply.github.com> --- .github/workflows/_e2e-suite.yml | 21 ++-- .github/workflows/_unit-suite.yml | 14 ++- .github/workflows/release.yml | 19 +-- .../fix-ci-native-binary-provenance.json | 18 +++ scripts/ci-native-state.py | 3 + scripts/ci-stage-aft.py | 77 ++++++++++++ scripts/lib/test_native_release_workflow.py | 15 ++- scripts/lib/test_workflow_gates.py | 114 +++++++++++++++++- 8 files changed, 250 insertions(+), 31 deletions(-) create mode 100644 benchmarks/aft-search/slice-descriptors/fix-ci-native-binary-provenance.json create mode 100644 scripts/ci-stage-aft.py diff --git a/.github/workflows/_e2e-suite.yml b/.github/workflows/_e2e-suite.yml index cc2a74ace0..624a3760c7 100644 --- a/.github/workflows/_e2e-suite.yml +++ b/.github/workflows/_e2e-suite.yml @@ -527,10 +527,11 @@ jobs: save-if: ${{ github.ref == 'refs/heads/main' }} - name: Build AFT binary (Windows x64) - run: cargo build --profile $env:CARGO_PROFILE --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --features wasm-grammars + run: python scripts/ci-stage-aft.py cargo build --profile $env:CARGO_PROFILE --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --features wasm-grammars shell: pwsh env: - CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} + CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target + CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} - name: Install workspace deps + build aft-bridge run: | @@ -548,7 +549,7 @@ jobs: pwsh -NoProfile -ExecutionPolicy Bypass -File tests/windows-e2e/run.ps1 shell: pwsh env: - AFT_BINARY_PATH: ${{ env.AFT_CI_TARGET_DIR }}\x86_64-pc-windows-msvc\${{ inputs.cargo_profile == 'stage' && 'stage' || 'release' }}\aft.exe + AFT_BINARY_PATH: ${{ env.AFT_CI_JOB_DIR }}/aft.exe AFT_PLUGIN_DIST: ${{ github.workspace }}\packages\opencode-plugin\dist - name: Collect Windows plugin log on failure @@ -598,7 +599,6 @@ jobs: env: CARGO_INCREMENTAL: "0" CARGO_PROFILE: ${{ inputs.cargo_profile == 'stage' && 'dev' || 'release' }} - CARGO_PROFILE_DIR: ${{ inputs.cargo_profile == 'stage' && 'debug' || 'release' }} CARGO_PROFILE_DEV_DEBUG: line-tables-only steps: - uses: actions/checkout@v5 @@ -656,9 +656,10 @@ jobs: save-if: ${{ github.ref == 'refs/heads/main' }} - name: Build AFT binary (macOS native) - run: cargo build --profile "$CARGO_PROFILE" --bin aft -p agent-file-tools --timings --features wasm-grammars + run: python3 scripts/ci-stage-aft.py cargo build --profile "$CARGO_PROFILE" --bin aft -p agent-file-tools --timings --features wasm-grammars env: - CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} + CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target + CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} # Ad-hoc sign + verify + exec once: an unsigned fresh binary gets # SIGKILLed on newer macOS, and the first exec pays a syspolicyd @@ -667,9 +668,9 @@ jobs: # corrupt-signature shape #238 shipped, with no exec needed. - name: Sign, verify, and warm the binary run: | - codesign -f -s - "$AFT_CI_TARGET_DIR/$CARGO_PROFILE_DIR/aft" - codesign --verify --strict "$AFT_CI_TARGET_DIR/$CARGO_PROFILE_DIR/aft" - "$AFT_CI_TARGET_DIR/$CARGO_PROFILE_DIR/aft" --version + codesign -f -s - "$AFT_CI_JOB_DIR/aft" + codesign --verify --strict "$AFT_CI_JOB_DIR/aft" + "$AFT_CI_JOB_DIR/aft" --version - name: Install workspace deps + build aft-bridge run: | @@ -684,7 +685,7 @@ jobs: unset CARGO_TARGET_DIR CARGO_BUILD_TARGET_DIR bash tests/macos-e2e/run.sh env: - AFT_BINARY_PATH: ${{ env.AFT_CI_TARGET_DIR }}/${{ env.CARGO_PROFILE_DIR }}/aft + AFT_BINARY_PATH: ${{ env.AFT_CI_JOB_DIR }}/aft AFT_PLUGIN_DIST: ${{ github.workspace }}/packages/opencode-plugin/dist - name: Upload macOS plugin log on failure diff --git a/.github/workflows/_unit-suite.yml b/.github/workflows/_unit-suite.yml index ed43fde818..1a0939bd8e 100644 --- a/.github/workflows/_unit-suite.yml +++ b/.github/workflows/_unit-suite.yml @@ -1698,9 +1698,10 @@ jobs: run: bun run typecheck - name: Cargo build (debug — needed by plugin tests) - run: cargo build --features wasm-grammars -p agent-file-tools + run: python3 scripts/ci-stage-aft.py cargo build --features wasm-grammars -p agent-file-tools --bin aft env: - CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} + CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target + CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} - name: Rust conformance gate (macOS) run: | @@ -1778,7 +1779,7 @@ jobs: env: AFT_TEST_NATIVE_FIXTURE_TIMING: "1" AFT_CACHE_DIR: ${{ runner.temp }}/aft-cache - AFT_BINARY_PATH: ${{ env.AFT_CI_TARGET_DIR }}/debug/aft + AFT_BINARY_PATH: ${{ env.AFT_CI_JOB_DIR }}/aft SUBC_CORE_BIN: ${{ steps.subc_core.outputs.path }} SUBC_CORE_WIRE_VERSION: ${{ steps.subc_core.outputs.wire_version }} @@ -2370,10 +2371,11 @@ jobs: shell: pwsh - name: Cargo build (debug — bash.test.ts spawns the real aft binary) - run: cargo build --features wasm-grammars -p agent-file-tools + run: python scripts/ci-stage-aft.py cargo build --features wasm-grammars -p agent-file-tools --bin aft shell: pwsh env: - CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} + CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target + CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} - name: Bun test (bash permission flow — full bridge + plugin + Rust) run: | @@ -2383,7 +2385,7 @@ jobs: shell: pwsh env: AFT_CACHE_DIR: ${{ runner.temp }}/aft-cache - AFT_BINARY_PATH: ${{ env.AFT_CI_TARGET_DIR }}/debug/aft.exe + AFT_BINARY_PATH: ${{ env.AFT_CI_JOB_DIR }}/aft.exe - name: Clean native test processes if: always() diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9c900ea0d3..dd9bbffcda 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -506,14 +506,16 @@ jobs: - name: Build # MSVC keeps debug info in a PDB, not in the shipped executable. - run: cargo build --release --target aarch64-pc-windows-msvc --config 'profile.release.strip=false' --features wasm-grammars + run: python scripts/ci-stage-aft.py --pdb cargo build --release --bin aft --target aarch64-pc-windows-msvc -p agent-file-tools --config 'profile.release.strip=false' --features wasm-grammars + shell: pwsh env: - CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} + CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target + CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} - name: Stage the built binary for the existing artifact contract shell: pwsh run: | - $source = "$env:AFT_CI_TARGET_DIR/aarch64-pc-windows-msvc/release/aft.exe" + $source = "$env:AFT_CI_JOB_DIR/aft.exe" $destination = Join-Path $env:GITHUB_WORKSPACE "target/aarch64-pc-windows-msvc/release/aft.exe" $null = New-Item -ItemType Directory -Force -Path (Split-Path $destination) Copy-Item -LiteralPath $source -Destination $destination -Force @@ -523,7 +525,7 @@ jobs: - name: Verify and package Windows PDB shell: pwsh - run: python scripts/release-debug-symbols.py windows --binary target/aarch64-pc-windows-msvc/release/aft.exe --pdb "$env:AFT_CI_TARGET_DIR/aarch64-pc-windows-msvc/release/aft.pdb" --archive release-debug/aft-windows-arm64-pdb.zip + run: python scripts/release-debug-symbols.py windows --binary target/aarch64-pc-windows-msvc/release/aft.exe --pdb "$env:AFT_CI_JOB_DIR/aft.pdb" --archive release-debug/aft-windows-arm64-pdb.zip - uses: actions/upload-artifact@v4 with: name: windows-arm64-pdb @@ -611,15 +613,16 @@ jobs: CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} - name: Build - run: cargo build --release --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --config 'profile.release.strip=false' --features wasm-grammars + run: python scripts/ci-stage-aft.py --pdb cargo build --release --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --config 'profile.release.strip=false' --features wasm-grammars shell: pwsh env: - CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} + CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target + CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} - name: Stage the built binary for the existing artifact contract shell: pwsh run: | - $source = "$env:AFT_CI_TARGET_DIR/x86_64-pc-windows-msvc/release/aft.exe" + $source = "$env:AFT_CI_JOB_DIR/aft.exe" $destination = Join-Path $env:GITHUB_WORKSPACE "target/x86_64-pc-windows-msvc/release/aft.exe" $null = New-Item -ItemType Directory -Force -Path (Split-Path $destination) Copy-Item -LiteralPath $source -Destination $destination -Force @@ -629,7 +632,7 @@ jobs: - name: Verify and package Windows PDB shell: pwsh - run: python scripts/release-debug-symbols.py windows --binary target/x86_64-pc-windows-msvc/release/aft.exe --pdb "$env:AFT_CI_TARGET_DIR/x86_64-pc-windows-msvc/release/aft.pdb" --archive release-debug/aft-windows-x64-pdb.zip + run: python scripts/release-debug-symbols.py windows --binary target/x86_64-pc-windows-msvc/release/aft.exe --pdb "$env:AFT_CI_JOB_DIR/aft.pdb" --archive release-debug/aft-windows-x64-pdb.zip - uses: actions/upload-artifact@v4 with: name: windows-x64-pdb diff --git a/benchmarks/aft-search/slice-descriptors/fix-ci-native-binary-provenance.json b/benchmarks/aft-search/slice-descriptors/fix-ci-native-binary-provenance.json new file mode 100644 index 0000000000..13418ed1e3 --- /dev/null +++ b/benchmarks/aft-search/slice-descriptors/fix-ci-native-binary-provenance.json @@ -0,0 +1,18 @@ +{ + "slice_class": "non_ranking", + "targeted_mechanism": "none", + "kind": "harness", + "fixtures": [ + ".github/workflows/_e2e-suite.yml", + ".github/workflows/_unit-suite.yml", + ".github/workflows/release.yml", + "scripts/ci-native-state.py", + "scripts/ci-stage-aft.py", + "scripts/lib/test_native_release_workflow.py", + "scripts/lib/test_workflow_gates.py" + ], + "slices": [ + "fix-ci-native-binary-provenance" + ], + "note": "Native CI keeps Cargo intermediates warm but stages each job's reported executable and release PDB from private outputs. No product or ranking code changes." +} diff --git a/scripts/ci-native-state.py b/scripts/ci-native-state.py index cbe512c420..a1b70badad 100644 --- a/scripts/ci-native-state.py +++ b/scripts/ci-native-state.py @@ -284,8 +284,11 @@ def main() -> None: phase = sys.argv[1] if os.environ.get("RUNNER_ENVIRONMENT") != "self-hosted": if phase == "setup": + runner_temp = Path(os.environ["RUNNER_TEMP"]) + job_dir = Path(tempfile.mkdtemp(prefix="aft-ci-job-", dir=runner_temp)) with open(os.environ["GITHUB_ENV"], "a", encoding="utf-8") as env: print(f"AFT_CI_ARCHIVE_DIR={os.environ['RUNNER_TEMP']}", file=env) + print(f"AFT_CI_JOB_DIR={job_dir.as_posix()}", file=env) return root = Path.home() / "aft-ci" run_dir = run_directory(root, os.environ["GITHUB_RUN_ID"], os.environ["GITHUB_RUN_ATTEMPT"]) diff --git a/scripts/ci-stage-aft.py b/scripts/ci-stage-aft.py new file mode 100644 index 0000000000..7b4e3b03fa --- /dev/null +++ b/scripts/ci-stage-aft.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +"""Build aft and stage the executable (and optional MSVC PDB) for this CI job.""" + +from __future__ import annotations + +import argparse +from collections.abc import Iterable +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys + + +def read_aft_artifact(lines: Iterable[str]) -> dict | None: + artifact = None + for line in lines: + # Cargo may relay plain stdout from tools in addition to its JSON records. + if not line.startswith("{"): + print(line, end="", flush=True) + continue + message = json.loads(line) + if message["reason"] == "compiler-message": + print(message["message"]["rendered"] or "", end="", file=sys.stderr, flush=True) + if (message["reason"] == "compiler-artifact" and message["target"]["name"] == "aft" + and "bin" in message["target"]["kind"] and message.get("executable")): + artifact = message + return artifact + + +def stage_artifact(artifact: dict | None, target_dir: Path, job_dir: Path, with_pdb: bool) -> None: + if artifact is None: + raise ValueError("Cargo did not report an executable for the aft binary") + executable = Path(artifact["executable"]).resolve(strict=True) + if not executable.is_relative_to(target_dir) or executable.name not in ("aft", "aft.exe"): + raise ValueError(f"Cargo reported an unexpected aft executable: {executable}") + outputs = [executable] + if with_pdb: + pdb = executable.with_suffix(".pdb") + if executable.suffix != ".exe" or pdb not in (Path(p).resolve() for p in artifact["filenames"]): + raise ValueError("Cargo did not report the aft.pdb paired with the aft executable") + outputs.append(pdb) + for source in outputs: + # Copy rather than hardlink: ad-hoc signing must not alter Cargo's cache. + destination = job_dir / source.name + shutil.copy2(source, destination) + print(f"Staged {destination} from {source}") + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--pdb", action="store_true", help="also stage the PDB from the same Cargo artifact") + parser.add_argument("command", nargs=argparse.REMAINDER, help="cargo build command and arguments") + args = parser.parse_args() + if args.command[:2] != ["cargo", "build"]: + parser.error("expected a cargo build command") + job_dir = Path(os.environ["AFT_CI_JOB_DIR"]).resolve(strict=True) + target_dir = Path(os.environ["CARGO_TARGET_DIR"]).resolve() + # Cargo reports the final target-dir path, even for reused artifacts. + # Two builds can overwrite that path unless each job has its own target dir. + if target_dir == job_dir or not target_dir.is_relative_to(job_dir): + parser.error("CARGO_TARGET_DIR must be inside AFT_CI_JOB_DIR") + with subprocess.Popen([*args.command, "--message-format=json-render-diagnostics"], + stdout=subprocess.PIPE, text=True, encoding="utf-8") as cargo: + assert cargo.stdout is not None + artifact = read_aft_artifact(cargo.stdout) + if code := cargo.wait(): + raise SystemExit(code) + try: + stage_artifact(artifact, target_dir, job_dir, args.pdb) + except (OSError, ValueError) as error: + parser.exit(1, f"{error}\n") + + +if __name__ == "__main__": + main() diff --git a/scripts/lib/test_native_release_workflow.py b/scripts/lib/test_native_release_workflow.py index f08dc78720..46f07ac4b7 100644 --- a/scripts/lib/test_native_release_workflow.py +++ b/scripts/lib/test_native_release_workflow.py @@ -158,11 +158,17 @@ def test_windows_release_jobs_reuse_narli_without_mutating_rust_or_the_artifact_ self.assertEqual(steps[-1], {"uses": "./.github/actions/native-ci-state", "if": "always()", "with": {"phase": "cleanup"}}) if job_id.startswith("build-"): + build = next(i for i, step in enumerate(steps) if step.get("name") == "Build") + self.assertIn(f"ci-stage-aft.py --pdb cargo build --release --bin aft --target {target}", steps[build]["run"]) + self.assertEqual(steps[build]["env"]["CARGO_TARGET_DIR"], "${{ env.AFT_CI_JOB_DIR }}/cargo-target") + self.assertEqual(steps[build]["env"]["CARGO_BUILD_BUILD_DIR"], + "${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }}") stage = next(i for i, step in enumerate(steps) if step.get("name") == "Stage the built binary for the existing artifact contract") - self.assertIn(f'$env:AFT_CI_TARGET_DIR/{target}/release/aft.exe', steps[stage]["run"]) + self.assertIn('$source = "$env:AFT_CI_JOB_DIR/aft.exe"', steps[stage]["run"]) self.assertIn("Get-FileHash $source", steps[stage]["run"]) upload = next(i for i, step in enumerate(steps) if step.get("uses") == "actions/upload-artifact@v4") + self.assertLess(build, stage) self.assertLess(stage, upload) arm = commands(jobs["build-win32-arm64"]) self.assertIn("Hostx64/arm64/cl.exe", arm) @@ -211,13 +217,14 @@ def test_main_and_train_native_e2e_use_the_staging_selector(self): def test_macos_landing_e2e_uses_debug_without_changing_publication(self): job = self.workflow("_e2e-suite.yml")["jobs"]["e2e-macos"] self.assertEqual(job["env"]["CARGO_PROFILE"], "${{ inputs.cargo_profile == 'stage' && 'dev' || 'release' }}") - self.assertEqual(job["env"]["CARGO_PROFILE_DIR"], "${{ inputs.cargo_profile == 'stage' && 'debug' || 'release' }}") self.assertEqual(job["env"]["CARGO_PROFILE_DEV_DEBUG"], "line-tables-only") self.assertIn('cargo build --profile "$CARGO_PROFILE"', commands(job)) - for command in ('codesign -f -s -', 'codesign --verify --strict', '"$AFT_CI_TARGET_DIR/$CARGO_PROFILE_DIR/aft" --version'): + for command in ('codesign -f -s - "$AFT_CI_JOB_DIR/aft"', + 'codesign --verify --strict "$AFT_CI_JOB_DIR/aft"', + '"$AFT_CI_JOB_DIR/aft" --version'): self.assertIn(command, commands(job)) run = next(step for step in job["steps"] if "bash tests/macos-e2e/run.sh" in step.get("run", "").splitlines()) - self.assertEqual(run["env"]["AFT_BINARY_PATH"], "${{ env.AFT_CI_TARGET_DIR }}/${{ env.CARGO_PROFILE_DIR }}/aft") + self.assertEqual(run["env"]["AFT_BINARY_PATH"], "${{ env.AFT_CI_JOB_DIR }}/aft") def test_macos_e2e_reuses_coreutils_without_write_access_to_homebrew(self): source = (ROOT / "tests/macos-e2e/run.sh").read_text() diff --git a/scripts/lib/test_workflow_gates.py b/scripts/lib/test_workflow_gates.py index ab75deb954..21bff7dc1c 100644 --- a/scripts/lib/test_workflow_gates.py +++ b/scripts/lib/test_workflow_gates.py @@ -78,7 +78,7 @@ def test_windows_verifies_pdb_identity_before_shipping(self): command = steps[verify_index]["run"] self.assertIn("scripts/release-debug-symbols.py windows", command) self.assertIn(f"{target}-pc-windows-msvc/release/aft.exe", command) - self.assertIn(f"$env:AFT_CI_TARGET_DIR/{target}-pc-windows-msvc/release/aft.pdb", command) + self.assertIn("$env:AFT_CI_JOB_DIR/aft.pdb", command) self.assertIn(f"aft-windows-{arch}-pdb.zip", command) uploads = [(i, step["with"]) for i, step in enumerate(steps) if step.get("uses") == "actions/upload-artifact@v4"] self.assertTrue(all(i > verify_index for i, _ in uploads)) @@ -615,6 +615,7 @@ def test_real_daemon_runner_isolates_each_test_process_and_cleans_up(self) -> No homes.append(paths[0]) self.assertNotEqual(*homes) + @unittest.skipUnless(sys.platform == "linux", "daemon runner uses GNU stat") def test_real_daemon_gate_rejects_insecure_ancestors_before_running_tests(self) -> None: root = Path(__file__).resolve().parents[2] with tempfile.TemporaryDirectory() as directory: @@ -1330,7 +1331,60 @@ def test_macos_conformance_starts_with_its_own_build_not_archive_artifacts(self) self.assertNotIn("actions/download-artifact", conformance) self.assertIn("python3 scripts/check-path-deps.py", conformance) self.assertIn("cargo build --features wasm-grammars -p agent-file-tools", conformance) - self.assertIn("AFT_BINARY_PATH: ${{ env.AFT_CI_TARGET_DIR }}/debug/aft", conformance) + self.assertIn("AFT_BINARY_PATH: ${{ env.AFT_CI_JOB_DIR }}/aft", conformance) + + +class StageAftTests(unittest.TestCase): + def setUp(self) -> None: + self.stage = runpy.run_path(str(REPO_ROOT / "scripts/ci-stage-aft.py")) + + def test_stages_the_executable_and_pdb_from_a_fresh_cargo_artifact(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + job = Path(tmp).resolve() + target = job / "cargo-target" + output = target / "unexpected profile directory" + output.mkdir(parents=True) + executable = output / "aft.exe" + executable.write_bytes(b"right executable") + pdb = output / "aft.pdb" + pdb.write_bytes(b"matching symbols") + (target / "aft.exe").write_bytes(b"wrong executable") + line = json.dumps({"reason": "compiler-artifact", "target": {"name": "aft", "kind": ["bin"]}, + "fresh": True, "executable": str(executable), + "filenames": [str(executable), str(pdb)]}) + artifact = self.stage["read_aft_artifact"]([line]) + self.stage["stage_artifact"](artifact, target, job, True) + self.assertEqual((job / "aft.exe").read_bytes(), b"right executable") + self.assertEqual((job / "aft.pdb").read_bytes(), b"matching symbols") + (job / "aft.exe").write_bytes(b"signed copy") + self.assertEqual(executable.read_bytes(), b"right executable") + + def test_refuses_to_stage_when_cargo_reports_no_aft_executable(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + job = Path(tmp).resolve() + target = job / "cargo-target" + target.mkdir() + (target / "aft").write_bytes(b"stale executable") + stream = [json.dumps({"reason": "compiler-artifact", "target": {"name": "aft", "kind": ["bin"]}, + "fresh": True, "executable": None, "filenames": []}), + json.dumps({"reason": "build-finished", "success": True})] + artifact = self.stage["read_aft_artifact"](stream) + with self.assertRaisesRegex(ValueError, "Cargo did not report an executable for the aft binary"): + self.stage["stage_artifact"](artifact, target, job, False) + self.assertFalse((job / "aft").exists()) + + def test_refuses_an_executable_from_the_shared_target(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp).resolve() + shared = root / "shared-target" + shared.mkdir() + executable = shared / "aft" + executable.touch() + job = root / "job" + job.mkdir() + with self.assertRaisesRegex(ValueError, "unexpected aft executable"): + self.stage["stage_artifact"]({"executable": str(executable)}, job / "cargo-target", job, False) + self.assertFalse((job / "aft").exists()) class NativeSelfHostedWorkflowTests(unittest.TestCase): @@ -1398,6 +1452,14 @@ def test_required_native_check_names_are_unchanged(self) -> None: for platform in ("macOS", "Windows") for shard in (1, 2, 3)) self.assertEqual(required - names, set()) + def test_native_e2e_uses_the_job_local_binary(self) -> None: + jobs = self.suites[1]["jobs"] + for job, binary in (("e2e-windows", "aft.exe"), ("e2e-macos", "aft")): + with self.subTest(job=job): + paths = [step["env"]["AFT_BINARY_PATH"] for step in jobs[job]["steps"] + if "AFT_BINARY_PATH" in step.get("env", {})] + self.assertEqual(paths, [f"${{{{ env.AFT_CI_JOB_DIR }}}}/{binary}"]) + def test_every_native_job_prepares_persistent_state_and_always_cleans_processes(self) -> None: jobs = self.native_jobs() self.assertEqual(len(jobs), 20) @@ -1560,7 +1622,12 @@ def test_native_test_steps_clear_cargo_target_overrides_and_builds_scope_the_war run = step.get("run", "") env = step.get("env", {}) if "CARGO_TARGET_DIR" in env: - self.assertEqual(env["CARGO_TARGET_DIR"], "${{ env.AFT_CI_TARGET_DIR }}", name) + if "scripts/ci-stage-aft.py" in run: + self.assertEqual(env["CARGO_TARGET_DIR"], "${{ env.AFT_CI_JOB_DIR }}/cargo-target", name) + self.assertEqual(env["CARGO_BUILD_BUILD_DIR"], + "${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }}", name) + else: + self.assertEqual(env["CARGO_TARGET_DIR"], "${{ env.AFT_CI_TARGET_DIR }}", name) self.assertRegex(run, r"cargo (build|check|nextest (archive|list)|test)|ci-macos-integration-archive.py", name) if any(command in run for command in ( "rust-test-gate.sh", "ci-windows-probes.py", "bun run test:unit", "bun test ", @@ -1569,6 +1636,7 @@ def test_native_test_steps_clear_cargo_target_overrides_and_builds_scope_the_war checked += 1 self.assertNotIn("CARGO_TARGET_DIR", env, name) self.assertNotIn("CARGO_BUILD_TARGET_DIR", env, name) + self.assertNotIn("CARGO_BUILD_BUILD_DIR", env, name) shell = step.get("shell", "pwsh" if job["runs-on"] == ["self-hosted", "narli"] else "bash") clear = ("Remove-Item Env:CARGO_TARGET_DIR, Env:CARGO_BUILD_TARGET_DIR" if shell == "pwsh" else "unset CARGO_TARGET_DIR CARGO_BUILD_TARGET_DIR") @@ -1642,6 +1710,46 @@ def test_interleaved_runs_preserve_each_others_archives_and_warm_target(self) -> with self.assertRaises(ValueError): self.state.run_directory(root, "../target", "1") + def test_native_cleanup_removes_private_outputs_without_clearing_shared_state(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + home = Path(tmp) + root = home / "aft-ci" + run = self.state.run_directory(root, "100", "1") + job = run / "attempt-1-job-fixture" + output = job / "cargo-target" / "debug" + output.mkdir(parents=True) + (output / "aft").touch() + (job / "aft").touch() + archive = run / "archive.tar" + archive.touch() + warm = root / "target" + warm.mkdir() + (warm / "dependency").touch() + with unittest.mock.patch.object(self.state.Path, "home", return_value=home), \ + unittest.mock.patch.object(self.state, "cleanup_processes"), \ + unittest.mock.patch.object(sys, "argv", ["ci-native-state.py", "cleanup"]), \ + unittest.mock.patch.dict(os.environ, {"RUNNER_ENVIRONMENT": "self-hosted", + "GITHUB_RUN_ID": "100", "GITHUB_RUN_ATTEMPT": "1", + "AFT_CI_JOB_DIR": str(job)}): + self.state.main() + self.assertFalse(job.exists()) + self.assertTrue(archive.is_file()) + self.assertTrue((warm / "dependency").is_file()) + + def test_hosted_setup_also_publishes_a_job_folder(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + runner_temp = Path(tmp).resolve() + env = runner_temp / "env" + with unittest.mock.patch.object(sys, "argv", ["ci-native-state.py", "setup"]), \ + unittest.mock.patch.dict(os.environ, {"RUNNER_ENVIRONMENT": "github-hosted", + "RUNNER_TEMP": str(runner_temp), "GITHUB_ENV": str(env)}): + self.state.main() + values = dict(line.split("=", 1) for line in env.read_text().splitlines()) + job = Path(values["AFT_CI_JOB_DIR"]) + self.assertEqual(job.parent, runner_temp) + self.assertTrue(job.is_dir()) + self.assertNotIn("AFT_CI_TARGET_DIR", values, "hosted builds use their workspace cache") + def test_shared_target_is_cleaned_whole_only_above_the_runner_cap_on_self_hosted(self) -> None: for environment, cap, size, expected in (("github-hosted", 30, 78, False), ("self-hosted", 30, 30, False), ("self-hosted", 30, 31, True), From 5472ebfa3c1e792b57a7c4e27e83f5b875d778dd Mon Sep 17 00:00:00 2001 From: ualtinok <94532+ualtinok@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:19:35 +0100 Subject: [PATCH 2/2] mason: relink the legacy MSVC aft output before staging On Cargo 1.99 the shared deps/aft.exe is also an unhashed slot: changing rustc metadata by commit does not change that filename. Remove just the affected target/profile exe and PDB before linking. Keep both private final outputs and the shared dependency cache. Update the workflow contracts to verify the scoped relink instead of accepting a plain build. Co-authored-by: alfonso-aft <289616620+alfonso-aft@users.noreply.github.com> --- .github/workflows/_e2e-suite.yml | 4 +-- .github/workflows/_unit-suite.yml | 4 +-- .github/workflows/release.yml | 4 +-- scripts/ci-stage-aft.py | 30 ++++++++++++++++++--- scripts/lib/test_native_release_workflow.py | 6 +++-- scripts/lib/test_workflow_gates.py | 20 +++++++++++--- 6 files changed, 53 insertions(+), 15 deletions(-) diff --git a/.github/workflows/_e2e-suite.yml b/.github/workflows/_e2e-suite.yml index 624a3760c7..a169d5377e 100644 --- a/.github/workflows/_e2e-suite.yml +++ b/.github/workflows/_e2e-suite.yml @@ -527,7 +527,7 @@ jobs: save-if: ${{ github.ref == 'refs/heads/main' }} - name: Build AFT binary (Windows x64) - run: python scripts/ci-stage-aft.py cargo build --profile $env:CARGO_PROFILE --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --features wasm-grammars + run: python scripts/ci-stage-aft.py --legacy-msvc-profile-dir "x86_64-pc-windows-msvc/$env:CARGO_PROFILE" cargo rustc --profile $env:CARGO_PROFILE --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --features wasm-grammars shell: pwsh env: CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target @@ -656,7 +656,7 @@ jobs: save-if: ${{ github.ref == 'refs/heads/main' }} - name: Build AFT binary (macOS native) - run: python3 scripts/ci-stage-aft.py cargo build --profile "$CARGO_PROFILE" --bin aft -p agent-file-tools --timings --features wasm-grammars + run: python3 scripts/ci-stage-aft.py cargo rustc --profile "$CARGO_PROFILE" --bin aft -p agent-file-tools --timings --features wasm-grammars env: CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} diff --git a/.github/workflows/_unit-suite.yml b/.github/workflows/_unit-suite.yml index 1a0939bd8e..2f4b005a3f 100644 --- a/.github/workflows/_unit-suite.yml +++ b/.github/workflows/_unit-suite.yml @@ -1698,7 +1698,7 @@ jobs: run: bun run typecheck - name: Cargo build (debug — needed by plugin tests) - run: python3 scripts/ci-stage-aft.py cargo build --features wasm-grammars -p agent-file-tools --bin aft + run: python3 scripts/ci-stage-aft.py cargo rustc --features wasm-grammars -p agent-file-tools --bin aft env: CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target CARGO_BUILD_BUILD_DIR: ${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }} @@ -2371,7 +2371,7 @@ jobs: shell: pwsh - name: Cargo build (debug — bash.test.ts spawns the real aft binary) - run: python scripts/ci-stage-aft.py cargo build --features wasm-grammars -p agent-file-tools --bin aft + run: python scripts/ci-stage-aft.py --legacy-msvc-profile-dir debug cargo rustc --features wasm-grammars -p agent-file-tools --bin aft shell: pwsh env: CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index dd9bbffcda..36b6139c09 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -506,7 +506,7 @@ jobs: - name: Build # MSVC keeps debug info in a PDB, not in the shipped executable. - run: python scripts/ci-stage-aft.py --pdb cargo build --release --bin aft --target aarch64-pc-windows-msvc -p agent-file-tools --config 'profile.release.strip=false' --features wasm-grammars + run: python scripts/ci-stage-aft.py --pdb --legacy-msvc-profile-dir aarch64-pc-windows-msvc/release cargo rustc --release --bin aft --target aarch64-pc-windows-msvc -p agent-file-tools --config 'profile.release.strip=false' --features wasm-grammars shell: pwsh env: CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target @@ -613,7 +613,7 @@ jobs: CARGO_TARGET_DIR: ${{ env.AFT_CI_TARGET_DIR }} - name: Build - run: python scripts/ci-stage-aft.py --pdb cargo build --release --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --config 'profile.release.strip=false' --features wasm-grammars + run: python scripts/ci-stage-aft.py --pdb --legacy-msvc-profile-dir x86_64-pc-windows-msvc/release cargo rustc --release --bin aft --target x86_64-pc-windows-msvc -p agent-file-tools --timings --config 'profile.release.strip=false' --features wasm-grammars shell: pwsh env: CARGO_TARGET_DIR: ${{ env.AFT_CI_JOB_DIR }}/cargo-target diff --git a/scripts/ci-stage-aft.py b/scripts/ci-stage-aft.py index 7b4e3b03fa..7f29d2576d 100644 --- a/scripts/ci-stage-aft.py +++ b/scripts/ci-stage-aft.py @@ -48,20 +48,42 @@ def stage_artifact(artifact: dict | None, target_dir: Path, job_dir: Path, with_ print(f"Staged {destination} from {source}") +def invalidate_legacy_msvc_binary(build_dir: Path, profile_dir: Path) -> None: + deps = (build_dir / profile_dir / "deps").resolve() + if not deps.is_relative_to(build_dir): + raise ValueError(f"MSVC profile directory is outside the Cargo build directory: {profile_dir}") + # Cargo 1.99 can treat a unit as fresh after another build overwrites the + # unversioned deps/aft.exe. Remove only that link output and its PDB so + # Cargo relinks aft but keeps the dependency cache. Remove this workaround + # when the Windows runner uses Cargo 1.100's per-unit output directories. + (deps / "aft.exe").unlink(missing_ok=True) + (deps / "aft.pdb").unlink(missing_ok=True) + + def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--pdb", action="store_true", help="also stage the PDB from the same Cargo artifact") - parser.add_argument("command", nargs=argparse.REMAINDER, help="cargo build command and arguments") + parser.add_argument("--legacy-msvc-profile-dir", type=Path, help="profile path inside a shared legacy build dir") + parser.add_argument("command", nargs=argparse.REMAINDER, help="cargo rustc command and Cargo arguments") args = parser.parse_args() - if args.command[:2] != ["cargo", "build"]: - parser.error("expected a cargo build command") + if args.command[:2] != ["cargo", "rustc"] or "--" in args.command: + parser.error("expected a cargo rustc command without rustc arguments") job_dir = Path(os.environ["AFT_CI_JOB_DIR"]).resolve(strict=True) target_dir = Path(os.environ["CARGO_TARGET_DIR"]).resolve() # Cargo reports the final target-dir path, even for reused artifacts. # Two builds can overwrite that path unless each job has its own target dir. if target_dir == job_dir or not target_dir.is_relative_to(job_dir): parser.error("CARGO_TARGET_DIR must be inside AFT_CI_JOB_DIR") - with subprocess.Popen([*args.command, "--message-format=json-render-diagnostics"], + if args.legacy_msvc_profile_dir and os.environ.get("RUNNER_ENVIRONMENT") == "self-hosted": + version = subprocess.check_output(["cargo", "--version"], text=True).split()[1] + major, minor, _ = version.split(".", 2) + if (int(major), int(minor)) < (1, 100): + invalidate_legacy_msvc_binary(Path(os.environ["CARGO_BUILD_BUILD_DIR"]).resolve(), + args.legacy_msvc_profile_dir) + # Only the final bin gets the revision key; the library and dependencies + # retain their existing compiler flags and fingerprints. + metadata = f"metadata=aft-{os.environ['GITHUB_SHA']}" + with subprocess.Popen([*args.command, "--message-format=json-render-diagnostics", "--", "-C", metadata], stdout=subprocess.PIPE, text=True, encoding="utf-8") as cargo: assert cargo.stdout is not None artifact = read_aft_artifact(cargo.stdout) diff --git a/scripts/lib/test_native_release_workflow.py b/scripts/lib/test_native_release_workflow.py index 46f07ac4b7..b6a79fe88e 100644 --- a/scripts/lib/test_native_release_workflow.py +++ b/scripts/lib/test_native_release_workflow.py @@ -159,7 +159,9 @@ def test_windows_release_jobs_reuse_narli_without_mutating_rust_or_the_artifact_ "with": {"phase": "cleanup"}}) if job_id.startswith("build-"): build = next(i for i, step in enumerate(steps) if step.get("name") == "Build") - self.assertIn(f"ci-stage-aft.py --pdb cargo build --release --bin aft --target {target}", steps[build]["run"]) + self.assertIn(f"--legacy-msvc-profile-dir {target}/release", steps[build]["run"]) + self.assertIn(f"cargo rustc --release --bin aft --target {target}", steps[build]["run"]) + self.assertIn("ci-stage-aft.py --pdb", steps[build]["run"]) self.assertEqual(steps[build]["env"]["CARGO_TARGET_DIR"], "${{ env.AFT_CI_JOB_DIR }}/cargo-target") self.assertEqual(steps[build]["env"]["CARGO_BUILD_BUILD_DIR"], "${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }}") @@ -218,7 +220,7 @@ def test_macos_landing_e2e_uses_debug_without_changing_publication(self): job = self.workflow("_e2e-suite.yml")["jobs"]["e2e-macos"] self.assertEqual(job["env"]["CARGO_PROFILE"], "${{ inputs.cargo_profile == 'stage' && 'dev' || 'release' }}") self.assertEqual(job["env"]["CARGO_PROFILE_DEV_DEBUG"], "line-tables-only") - self.assertIn('cargo build --profile "$CARGO_PROFILE"', commands(job)) + self.assertIn('cargo rustc --profile "$CARGO_PROFILE"', commands(job)) for command in ('codesign -f -s - "$AFT_CI_JOB_DIR/aft"', 'codesign --verify --strict "$AFT_CI_JOB_DIR/aft"', '"$AFT_CI_JOB_DIR/aft" --version'): diff --git a/scripts/lib/test_workflow_gates.py b/scripts/lib/test_workflow_gates.py index 21bff7dc1c..45f1e72619 100644 --- a/scripts/lib/test_workflow_gates.py +++ b/scripts/lib/test_workflow_gates.py @@ -71,7 +71,7 @@ def test_windows_verifies_pdb_identity_before_shipping(self): for arch, target in (("x64", "x86_64"), ("arm64", "aarch64")): with self.subTest(arch=arch): steps = self.jobs[f"build-win32-{arch}"]["steps"] - builds = [step for step in steps if "cargo build --release" in step.get("run", "")] + builds = [step for step in steps if re.search(r"cargo (build|rustc) --release", step.get("run", ""))] self.assertTrue(builds) self.assertTrue(all("profile.release.strip=false" in step["run"] for step in builds)) verify_index = next(i for i, step in enumerate(steps) if step.get("name") == "Verify and package Windows PDB") @@ -1330,7 +1330,7 @@ def test_macos_conformance_starts_with_its_own_build_not_archive_artifacts(self) self.assertNotIn("needs:", dependencies) self.assertNotIn("actions/download-artifact", conformance) self.assertIn("python3 scripts/check-path-deps.py", conformance) - self.assertIn("cargo build --features wasm-grammars -p agent-file-tools", conformance) + self.assertIn("cargo rustc --features wasm-grammars -p agent-file-tools", conformance) self.assertIn("AFT_BINARY_PATH: ${{ env.AFT_CI_JOB_DIR }}/aft", conformance) @@ -1386,6 +1386,20 @@ def test_refuses_an_executable_from_the_shared_target(self) -> None: self.stage["stage_artifact"]({"executable": str(executable)}, job / "cargo-target", job, False) self.assertFalse((job / "aft").exists()) + def test_legacy_msvc_invalidation_removes_only_the_profile_binary_and_pdb(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + build = Path(tmp).resolve() + profile = Path("x86_64-pc-windows-msvc/stage") + removed = [profile / "deps" / file for file in ("aft.exe", "aft.pdb")] + kept = [profile / "aft.exe", profile / "deps/libaft.rlib", + Path("aarch64-pc-windows-msvc/stage/deps/aft.exe")] + for path in removed + kept: + (build / path).parent.mkdir(parents=True, exist_ok=True) + (build / path).touch() + self.stage["invalidate_legacy_msvc_binary"](build, profile) + self.assertTrue(all(not (build / path).exists() for path in removed)) + self.assertTrue(all((build / path).is_file() for path in kept)) + class NativeSelfHostedWorkflowTests(unittest.TestCase): def setUp(self) -> None: @@ -1628,7 +1642,7 @@ def test_native_test_steps_clear_cargo_target_overrides_and_builds_scope_the_war "${{ env.AFT_CI_TARGET_DIR || format('{0}/target', github.workspace) }}", name) else: self.assertEqual(env["CARGO_TARGET_DIR"], "${{ env.AFT_CI_TARGET_DIR }}", name) - self.assertRegex(run, r"cargo (build|check|nextest (archive|list)|test)|ci-macos-integration-archive.py", name) + self.assertRegex(run, r"cargo (build|rustc|check|nextest (archive|list)|test)|ci-macos-integration-archive.py", name) if any(command in run for command in ( "rust-test-gate.sh", "ci-windows-probes.py", "bun run test:unit", "bun test ", "tests/macos-e2e/run.sh", "tests/windows-e2e/run.ps1", "cargo test",