diff --git a/.github/workflows/smoke-tests.yml b/.github/workflows/smoke-tests.yml index 54ef1892..27927ea3 100644 --- a/.github/workflows/smoke-tests.yml +++ b/.github/workflows/smoke-tests.yml @@ -557,6 +557,48 @@ jobs: echo "No config/build/profile_smoke.yaml — profile validation skipped." fi + # The PR gate's copy of workspace-validation.yml's guard: the runner + # below writes into test-results/ and the upload step globs it (plus + # every smoke_timings.json), so a result file the workspace repo + # tracks would ride into the timings artifact here exactly as it rode + # into the release report there. Same step text, kept in step by + # tests/test_workflow_wiring.py. + - name: Discard result files the workspace checkout carries + # A workspace repo must never ship its own test-results/ — but one did: + # autolens_workspace_test#311 committed a local smoke run's + # `*__script.json` / `.md` / `smoke_timings.json` (three stale failures, + # `/home/` paths). The shard uploaded the whole directory, the + # analyze stage merged those failures into the report, and two nightly + # releases (2026-09-09, 2026-09-10) stopped at Stage 3 while every + # script that actually ran passed. The runner below writes its own + # reports into test-results/; anything already there arrived from git + # and is not evidence about this run, so it goes — loudly, one + # ::warning:: per file, so a leaking repo is visible in the run log + # rather than silently laundered. cache_state.json is kept: it is this + # run's sidecar (written by the cache steps), not a result. The shell + # is self-contained (no `${{ }}`) so tests/test_workflow_wiring.py can + # execute it verbatim against a planted stale file. + shell: bash + run: | + set -u + ws="${WORKSPACE_DIR:-workspace}" + [ -d "$ws" ] || { echo "no $ws checkout — nothing to discard"; exit 0; } + found=0 + while IFS= read -r f; do + found=1 + echo "::warning title=Stale result file in the workspace checkout::$f was committed to the workspace repo and is not a result of this run — discarded" + rm -f "$f" + done < <( + { [ -d "$ws/test-results" ] && find "$ws/test-results" -type f \ + \( -name '*__script.json' -o -name '*__script.md' \ + -o -name 'smoke_timings.json' -o -name 'report.json' \) \ + ! -name 'cache_state.json'; } ; + find "$ws" -path "$ws/.git" -prune -o -path "$ws/test-results" -prune \ + -o -type f -name 'smoke_timings.json' -print + ) + [ "$found" -eq 0 ] && echo "no stale result files under $ws" + exit 0 + # The second half of `setup_s`. Placed IMMEDIATELY before the runner and # nowhere else: the runner step's own command is the one thing in this # workflow that must stay byte-identical (a workspace owns its runner), diff --git a/.github/workflows/workspace-validation.yml b/.github/workflows/workspace-validation.yml index a83985d8..55953129 100644 --- a/.github/workflows/workspace-validation.yml +++ b/.github/workflows/workspace-validation.yml @@ -223,6 +223,41 @@ jobs: repository: PyAutoLabs/${{ steps.ws.outputs.repo }} ref: main path: workspace + - name: Discard result files the workspace checkout carries + # A workspace repo must never ship its own test-results/ — but one did: + # autolens_workspace_test#311 committed a local smoke run's + # `*__script.json` / `.md` / `smoke_timings.json` (three stale failures, + # `/home/` paths). The shard uploaded the whole directory, the + # analyze stage merged those failures into the report, and two nightly + # releases (2026-09-09, 2026-09-10) stopped at Stage 3 while every + # script that actually ran passed. The runner below writes its own + # reports into test-results/; anything already there arrived from git + # and is not evidence about this run, so it goes — loudly, one + # ::warning:: per file, so a leaking repo is visible in the run log + # rather than silently laundered. cache_state.json is kept: it is this + # run's sidecar (written by the cache steps), not a result. The shell + # is self-contained (no `${{ }}`) so tests/test_workflow_wiring.py can + # execute it verbatim against a planted stale file. + shell: bash + run: | + set -u + ws="${WORKSPACE_DIR:-workspace}" + [ -d "$ws" ] || { echo "no $ws checkout — nothing to discard"; exit 0; } + found=0 + while IFS= read -r f; do + found=1 + echo "::warning title=Stale result file in the workspace checkout::$f was committed to the workspace repo and is not a result of this run — discarded" + rm -f "$f" + done < <( + { [ -d "$ws/test-results" ] && find "$ws/test-results" -type f \ + \( -name '*__script.json' -o -name '*__script.md' \ + -o -name 'smoke_timings.json' -o -name 'report.json' \) \ + ! -name 'cache_state.json'; } ; + find "$ws" -path "$ws/.git" -prune -o -path "$ws/test-results" -prune \ + -o -type f -name 'smoke_timings.json' -print + ) + [ "$found" -eq 0 ] && echo "no stale result files under $ws" + exit 0 - name: "Checkout libraries (current main; source-shadowed via PYTHONPATH) [mode=smoke]" if: needs.find_scripts.outputs.mode == 'smoke' run: | @@ -467,6 +502,42 @@ jobs: repository: PyAutoLabs/${{ steps.ws.outputs.repo }} ref: main path: workspace + - name: Discard result files the workspace checkout carries + if: steps.gate.outputs.run == 'true' + # A workspace repo must never ship its own test-results/ — but one did: + # autolens_workspace_test#311 committed a local smoke run's + # `*__script.json` / `.md` / `smoke_timings.json` (three stale failures, + # `/home/` paths). The shard uploaded the whole directory, the + # analyze stage merged those failures into the report, and two nightly + # releases (2026-09-09, 2026-09-10) stopped at Stage 3 while every + # script that actually ran passed. The runner below writes its own + # reports into test-results/; anything already there arrived from git + # and is not evidence about this run, so it goes — loudly, one + # ::warning:: per file, so a leaking repo is visible in the run log + # rather than silently laundered. cache_state.json is kept: it is this + # run's sidecar (written by the cache steps), not a result. The shell + # is self-contained (no `${{ }}`) so tests/test_workflow_wiring.py can + # execute it verbatim against a planted stale file. + shell: bash + run: | + set -u + ws="${WORKSPACE_DIR:-workspace}" + [ -d "$ws" ] || { echo "no $ws checkout — nothing to discard"; exit 0; } + found=0 + while IFS= read -r f; do + found=1 + echo "::warning title=Stale result file in the workspace checkout::$f was committed to the workspace repo and is not a result of this run — discarded" + rm -f "$f" + done < <( + { [ -d "$ws/test-results" ] && find "$ws/test-results" -type f \ + \( -name '*__script.json' -o -name '*__script.md' \ + -o -name 'smoke_timings.json' -o -name 'report.json' \) \ + ! -name 'cache_state.json'; } ; + find "$ws" -path "$ws/.git" -prune -o -path "$ws/test-results" -prune \ + -o -type f -name 'smoke_timings.json' -print + ) + [ "$found" -eq 0 ] && echo "no stale result files under $ws" + exit 0 - name: Checkout libraries (source-shadowed via PYTHONPATH) if: steps.gate.outputs.run == 'true' run: | diff --git a/tests/test_workflow_wiring.py b/tests/test_workflow_wiring.py index 5e60162f..f3e41e6e 100644 --- a/tests/test_workflow_wiring.py +++ b/tests/test_workflow_wiring.py @@ -225,3 +225,127 @@ def test_timing_artifacts_stay_out_of_the_aggregate_namespace(): prefix = pattern.rstrip("*") for job_name, _leg in TIMING_LEGS: assert not _timing_step(job_name)["with"]["name"].startswith(prefix) + + +# --- the discard-stale-results guard -------------------------------------- +# +# A workspace repo that tracks its own test-results/ (one did, by accident, in +# 2026-09) hands every shard a report about some other run: the +# runner writes beside it, the upload step ships the directory whole, and the +# analyze stage folds the stale failures into the release report. Two nightly +# releases stopped at Stage 3 on exactly that (2026-09-09/10) while every +# script that ran passed. The guard is one step, placed between the workspace +# checkout and the runner in both shard jobs (and in the PR gate), whose shell +# is self-contained so it can be executed here against a planted file. + +import os +import subprocess + +DISCARD_STEP = "Discard result files the workspace checkout carries" + + +def _job_steps(workflow, job): + return _load(workflow)["jobs"][job]["steps"] + + +def _index(steps, needle): + for i, step in enumerate(steps): + if needle in step.get("name", ""): + return i + raise AssertionError(f"no step named like {needle!r}") + + +def _run_discard(step, cwd, env=None): + return subprocess.run( + ["bash", "-c", step["run"]], cwd=cwd, text=True, capture_output=True, + env={**os.environ, **(env or {})}, + ) + + +def test_both_shard_jobs_discard_committed_results_after_checkout_and_before_running(): + for job, run_step in (("run_scripts", "Run Python scripts"), + ("run_notebooks", "Run notebooks")): + steps = _job_steps("workspace-validation.yml", job) + i = _index(steps, DISCARD_STEP) + assert _index(steps, "Checkout workspace") < i < _index(steps, run_step), job + assert i < _index(steps, "Upload"), job + # No `${{ }}` in the shell: the step must be runnable verbatim (below), + # and nothing in it should depend on a matrix that differs per job. + assert "${{" not in steps[i]["run"], job + # The notebooks leg is gated like its checkout; the scripts leg is not. + nb = _job_steps("workspace-validation.yml", "run_notebooks") + assert nb[_index(nb, DISCARD_STEP)]["if"] == nb[_index(nb, "Checkout workspace")]["if"] + assert "if" not in _job_steps("workspace-validation.yml", "run_scripts")[ + _index(_job_steps("workspace-validation.yml", "run_scripts"), DISCARD_STEP)] + + +def test_the_pr_gate_carries_the_same_guard_before_its_runner(): + """smoke-tests.yml uploads test-results/ and every smoke_timings.json into + the timings artifact, so the leak rides there too. One step text, so a + fix to the pattern list lands in both places or neither.""" + gate = _job_steps("smoke-tests.yml", "smoke") + i = _index(gate, DISCARD_STEP) + assert _index(gate, "Checkout the workspace") < i < _index(gate, "Mark scripts start") \ + if any("Checkout the workspace" in s.get("name", "") for s in gate) \ + else i < _index(gate, "Mark scripts start") + body = _job_steps("workspace-validation.yml", "run_scripts") + assert gate[i]["run"] == body[_index(body, DISCARD_STEP)]["run"] + + +def test_the_guard_removes_planted_result_files_loudly_and_keeps_the_sidecar(tmp_path): + steps = _job_steps("workspace-validation.yml", "run_scripts") + step = steps[_index(steps, DISCARD_STEP)] + ws = tmp_path / "workspace" + results = ws / "test-results" + results.mkdir(parents=True) + stale_json = results / "some_workspace_test__scripts__script.json" + stale_json.write_text('{"results": [{"file": "/home/someone/scripts/x.py", "status": "failed"}]}') + stale_md = results / "some_workspace_test__scripts__script.md" + stale_md.write_text("| x.py | FAIL |\n") + stale_timings = results / "smoke_timings.json" + stale_timings.write_text("{}") + nested_timings = ws / "scripts" / "smoke_timings.json" + nested_timings.parent.mkdir() + nested_timings.write_text("{}") + sidecar = results / "cache_state.json" + sidecar.write_text('{"before": {}}') + # Not a result file: an ordinary tracked script must be untouched, and so + # must anything under .git (pruned, never walked). + script = ws / "scripts" / "x.py" + script.write_text("print(1)\n") + dotgit = ws / ".git" / "smoke_timings.json" + dotgit.parent.mkdir() + dotgit.write_text("{}") + + res = _run_discard(step, tmp_path) + + assert res.returncode == 0, res.stderr + for gone in (stale_json, stale_md, stale_timings, nested_timings): + assert not gone.exists(), gone + for kept in (sidecar, script, dotgit): + assert kept.exists(), kept + # One ::warning:: per discarded file, naming it — a leak is never silent. + warnings = [l for l in res.stdout.splitlines() if l.startswith("::warning")] + assert len(warnings) == 4, res.stdout + assert any(stale_json.name in w for w in warnings) + + +def test_the_guard_is_a_quiet_no_op_on_a_clean_checkout(tmp_path): + steps = _job_steps("workspace-validation.yml", "run_scripts") + step = steps[_index(steps, DISCARD_STEP)] + # No checkout at all (the notebooks leg's gate may skip it): exit 0. + res = _run_discard(step, tmp_path) + assert res.returncode == 0 and "::warning" not in res.stdout + # A checkout with a fresh sidecar and no reports: exit 0, sidecar kept. + results = tmp_path / "workspace" / "test-results" + results.mkdir(parents=True) + (results / "cache_state.json").write_text("{}") + res = _run_discard(step, tmp_path) + assert res.returncode == 0 and "::warning" not in res.stdout, res.stdout + assert (results / "cache_state.json").exists() + # The directory is configurable for callers that check out elsewhere. + other = tmp_path / "elsewhere" / "test-results" + other.mkdir(parents=True) + (other / "a__script.json").write_text("{}") + res = _run_discard(step, tmp_path, env={"WORKSPACE_DIR": "elsewhere"}) + assert res.returncode == 0 and not (other / "a__script.json").exists()