diff --git a/.github/workflows/scripts-test.yml b/.github/workflows/scripts-test.yml index f18604a197..6542c84e2a 100644 --- a/.github/workflows/scripts-test.yml +++ b/.github/workflows/scripts-test.yml @@ -1,6 +1,11 @@ name: scripts-test on: + pull_request: + paths: + - "scripts/worker-preflight.sh" + - "scripts/tests/test_worker_preflight.py" + - ".github/workflows/scripts-test.yml" push: branches: [main] paths: @@ -9,6 +14,8 @@ on: - "pkgs/id/scripts/update-nixpkgs-inputs.py" - "pkgs/id/scripts/update-nixpkgs-inputs.sh" - "scripts/tests/test_update_nixpkgs_inputs.py" + - "scripts/worker-preflight.sh" + - "scripts/tests/test_worker_preflight.py" - ".github/workflows/scripts-test.yml" workflow_dispatch: @@ -38,3 +45,5 @@ jobs: python-version: ${{ matrix.python-version }} - name: Run update-nixpkgs-inputs contract tests (#603) run: python3 -m unittest discover -s scripts/tests -p 'test_update_nixpkgs_inputs.py' -v + - name: Run worker preflight contract tests (#4862) + run: python3 -m unittest discover -s scripts/tests -p 'test_worker_preflight.py' -v diff --git a/AGENTS.md b/AGENTS.md index 55a739950d..b7ef1f4da6 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -34,6 +34,10 @@ When retrieving public web content with `curl`, `wget`, Python HTTP clients, or - **CI-first verification:** Review the checks actually triggered on the PR and use [`.github/ci-policy.md`](.github/ci-policy.md) to distinguish the basic merge-gating PR tier from the full main tier; formatting is not a merge gate. Run targeted local checks on the laptop when useful; sandboxed workers use CI and must not run builds or tests in the sandbox. If a policy-listed check is missing from the PR, inspect the workflow trigger and report the coverage gap instead of assuming it ran. - Document unresolved ambiguities, design concerns, or known gaps by creating GitHub issues instead of blocking progress. +## Worker Git preflight + +At the start of each task, before relying on Git names such as `origin/main`, run `bash "$(git rev-parse --show-toplevel)/scripts/worker-preflight.sh"`. If it reports `refs/heads/origin/main`, stop and escalate; do not delete the branch or alter refs as part of task work. That local branch shadows the remote-tracking ref. + ## Fleet schedules The unified fleet schedule registry is `doc/fleet-schedule-registry.md` diff --git a/scripts/tests/test_worker_preflight.py b/scripts/tests/test_worker_preflight.py new file mode 100644 index 0000000000..7a23830bf3 --- /dev/null +++ b/scripts/tests/test_worker_preflight.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""Contract tests for the worker preflight's origin/main shadow guard.""" + +import os +import subprocess +import tempfile +import unittest +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[2] +PREFLIGHT = ROOT / "scripts" / "worker-preflight.sh" + + +class TestWorkerPreflight(unittest.TestCase): + def test_clean_repo_passes_and_local_origin_main_branch_fails(self): + with tempfile.TemporaryDirectory(prefix="worker-preflight-") as tmp: + repo = Path(tmp) + env = os.environ.copy() + env.update( + GIT_AUTHOR_NAME="Worker preflight test", + GIT_AUTHOR_EMAIL="worker-preflight@example.invalid", + GIT_COMMITTER_NAME="Worker preflight test", + GIT_COMMITTER_EMAIL="worker-preflight@example.invalid", + ) + + subprocess.run(["git", "init", "--quiet", str(repo)], check=True) + subprocess.run( + ["git", "-C", str(repo), "-c", "user.name=Worker preflight test", + "-c", "user.email=worker-preflight@example.invalid", + "commit", "--quiet", "--allow-empty", "-m", "fixture"], + check=True, + env=env, + ) + + clean = subprocess.run( + ["bash", str(PREFLIGHT)], + cwd=repo, + text=True, + capture_output=True, + check=False, + ) + self.assertEqual(clean.returncode, 0, clean.stderr) + self.assertIn("worker-preflight: clean", clean.stdout) + + head = subprocess.check_output( + ["git", "-C", str(repo), "rev-parse", "HEAD"], text=True + ).strip() + subprocess.run( + ["git", "-C", str(repo), "update-ref", "refs/heads/origin/main", head], + check=True, + ) + + shadowed = subprocess.run( + ["bash", str(PREFLIGHT)], + cwd=repo, + text=True, + capture_output=True, + check=False, + ) + self.assertEqual(shadowed.returncode, 1) + self.assertIn("refs/heads/origin/main", shadowed.stderr) + self.assertIn("shadows refs/remotes/origin/main", shadowed.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/scripts/worker-preflight.sh b/scripts/worker-preflight.sh new file mode 100755 index 0000000000..64a1d76d60 --- /dev/null +++ b/scripts/worker-preflight.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +set -euo pipefail + +repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || { + printf 'worker-preflight: must run inside a Git worktree\n' >&2 + exit 2 +} + +stray_ref='refs/heads/origin/main' +if git -C "$repo_root" show-ref --verify --quiet "$stray_ref"; then + sha="$(git -C "$repo_root" rev-parse --verify "$stray_ref^{commit}" 2>/dev/null || printf 'unreadable')" + printf 'worker-preflight: refusing to continue: local branch %s exists at %s and shadows refs/remotes/origin/main; stop and escalate before doing Git-based work.\n' "$stray_ref" "$sha" >&2 + exit 1 +else + status=$? + if [[ "$status" -ne 1 ]]; then + printf 'worker-preflight: could not verify %s (git show-ref exited %s)\n' "$stray_ref" "$status" >&2 + exit 2 + fi +fi + +printf 'worker-preflight: clean (%s; no local %s)\n' "$repo_root" "$stray_ref"