Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
493 changes: 493 additions & 0 deletions .github/workflows/ci-guards.yml

Large diffs are not rendered by default.

470 changes: 31 additions & 439 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions scripts/ci/detect_ci_change_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ def normalize_path(path: str) -> str:


CI_WORKFLOW_PATH = ".github/workflows/ci.yml"
GUARD_WORKFLOW_PATH = ".github/workflows/ci-guards.yml"


def is_other_workflow_config(path: str) -> bool:
# ci.yml's macOS and web jobs read no other workflow file. An edit to one is
# checked by workflow-guard-tests and by that workflow's own triggers.
# checked by the reusable guard workflow and by that workflow's own triggers.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if path == CI_WORKFLOW_PATH:
return False
return path.startswith(".github/workflows/") or path == ".github/actionlint.yaml"
Expand All @@ -60,7 +61,7 @@ def forces_all_areas(path: str) -> bool:
is_direct_ci_python = path.startswith(ci_script_prefix) and path.endswith(".py")
if is_direct_ci_python:
is_direct_ci_python = "/" not in path[len(ci_script_prefix) :]
return path == CI_WORKFLOW_PATH or is_direct_ci_python or path == "tests/test_ci_change_areas.py"
return path in {CI_WORKFLOW_PATH, GUARD_WORKFLOW_PATH} or is_direct_ci_python or path == "tests/test_ci_change_areas.py"
Comment thread
teamleaderleo marked this conversation as resolved.
Resolved


_TEST_REFERENCE_RE = re.compile(r"tests/[A-Za-z0-9_./-]*")
Expand Down Expand Up @@ -159,15 +160,24 @@ def macos_job_test_references(workflow: str) -> Optional[tuple[frozenset[str], f


def load_macos_job_test_references() -> Optional[tuple[frozenset[str], frozenset[str]]]:
macos: set[str] = set()
everywhere: set[str] = set()
try:
return macos_job_test_references(Path(CI_WORKFLOW_PATH).read_text(encoding="utf-8"))
for workflow_path in (CI_WORKFLOW_PATH, GUARD_WORKFLOW_PATH):
references = macos_job_test_references(Path(workflow_path).read_text(encoding="utf-8"))
if references is None:
return None
workflow_macos, workflow_everywhere = references
macos.update(workflow_macos)
everywhere.update(workflow_everywhere)
except OSError:
return None
return frozenset(macos), frozenset(everywhere)


def is_guard_only_test(path: str, references: Optional[tuple[frozenset[str], frozenset[str]]]) -> bool:
# A tests/ file is macOS-neutral only when ci.yml names it and every job
# that names it runs on Linux. An unnamed file may be imported by a test a
# A tests/ file is macOS-neutral only when a CI workflow names it and every
# job that names it runs on Linux. An unnamed file may be imported by a test a
# macOS job runs, so it stays macOS-relevant.
if references is None or not path.startswith("tests/"):
return False
Expand Down
32 changes: 20 additions & 12 deletions tests/test_ci_app_host_home_isolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

ROOT = Path(__file__).resolve().parents[1]
WORKFLOW_PATH = ROOT / ".github/workflows/ci.yml"
WORKFLOW = yaml.safe_load(WORKFLOW_PATH.read_text(encoding="utf-8"))
GUARD_WORKFLOW_PATH = ROOT / ".github/workflows/ci-guards.yml"
WORKFLOWS = [
yaml.safe_load(WORKFLOW_PATH.read_text(encoding="utf-8")),
yaml.safe_load(GUARD_WORKFLOW_PATH.read_text(encoding="utf-8")),
]
CONSOLE_WRAPPER = (ROOT / "scripts/ci/run-in-console-session.sh").read_text(
encoding="utf-8"
)
Expand Down Expand Up @@ -111,17 +115,21 @@ def require_no_test_runner_scheme_overrides(scheme: str) -> None:


def require_job(job_name: str) -> dict:
if not isinstance(WORKFLOW, dict):
raise SystemExit("FAIL: workflow must be a mapping")

jobs = WORKFLOW.get("jobs")
if not isinstance(jobs, dict):
raise SystemExit("FAIL: workflow jobs must be a mapping")

job = jobs.get(job_name)
if not isinstance(job, dict):
raise SystemExit(f"FAIL: workflow job {job_name!r} is missing")
return job
matches = []
for workflow in WORKFLOWS:
if not isinstance(workflow, dict):
raise SystemExit("FAIL: workflow must be a mapping")
jobs = workflow.get("jobs")
if not isinstance(jobs, dict):
raise SystemExit("FAIL: workflow jobs must be a mapping")
job = jobs.get(job_name)
if isinstance(job, dict):
matches.append(job)
if len(matches) != 1:
raise SystemExit(
f"FAIL: workflow job {job_name!r} must exist in exactly one CI workflow"
)
return matches[0]


def require_step(job_name: str, step_name: str) -> dict:
Expand Down
95 changes: 78 additions & 17 deletions tests/test_ci_change_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
ROOT = Path(__file__).resolve().parents[1]
HELPER = ROOT / "scripts" / "ci" / "detect_ci_change_areas.py"
CI_WORKFLOW = ROOT / ".github" / "workflows" / "ci.yml"
GUARD_WORKFLOW = ROOT / ".github" / "workflows" / "ci-guards.yml"
WEB_VALIDATION_WORKFLOW = ROOT / ".github" / "workflows" / "web-validation.yml"
GUARD_JOBS = (
"static-preflight",
"workflow-guard-tests",
"workflow-guard-history",
"workflow-guard-cli-scripts",
"workflow-guard-source-lints",
)
GUARD_ROUTE_JOBS = {
"linux_guard_tests": "workflow-guard-tests",
"linux_guard_history": "workflow-guard-history",
"linux_guard_cli": "workflow-guard-cli-scripts",
"linux_guard_source": "workflow-guard-source-lints",
}
CI_STATUS_FALLBACK_WORKFLOW = ROOT / ".github" / "workflows" / "ci-status-fallback.yml"
PERF_ACTIVATION_WORKFLOW = ROOT / ".github" / "workflows" / "perf-activation.yml"

Expand Down Expand Up @@ -84,6 +90,7 @@ def test_anything_the_app_can_build_from_runs_the_release_build() -> None:
def test_release_build_follows_the_other_areas_when_macos_is_skipped_or_forced() -> None:
assert module.classify_files(["docs/ci.md"]).release_build is False
assert module.classify_files([".github/workflows/ci.yml"]).release_build is True
assert module.classify_files([".github/workflows/ci-guards.yml"]) == module.ChangeAreas.all()
assert module.ChangeAreas.all().release_build is True


Expand Down Expand Up @@ -226,14 +233,14 @@ def test_other_workflow_changes_skip_macos_and_web() -> None:
# ci.yml's macOS and web jobs never read another workflow file. Those edits
# are validated by workflow-guard-tests and by the edited workflow itself.
assert_areas(
[".github/workflows/relay-tls.yml", ".github/actionlint.yaml"],
[".github/workflows/relay-tls.yml", ".github/workflows/ci-guards.yml", ".github/actionlint.yaml"],
macos=False,
web=False,
)


def test_guard_only_tests_skip_macos() -> None:
# Referenced in ci.yml only by Linux jobs.
# Referenced only by Linux jobs in the CI caller or reusable guard workflow.
assert_areas(["tests/test_ci_self_hosted_guard.sh"], macos=False, web=False)
assert_areas(
[".github/workflows/ios-testflight.yml", "tests/test_ios_testflight_main_push_filter.py"],
Expand Down Expand Up @@ -594,10 +601,7 @@ def linux_preflight_needs(
job_results = {
"changes": "success",
"static-preflight": "success",
"workflow-guard-tests": "success",
"workflow-guard-history": "success",
"workflow-guard-cli-scripts": "success",
"workflow-guard-source-lints": "success",
"guards": "success",
"ghosttykit-release-check": "success",
"web-typecheck": "success",
"react-apps-check": "success",
Expand All @@ -613,6 +617,35 @@ def linux_preflight_needs(
}


def run_guard_status(
*,
inputs: dict[str, str] | None = None,
results: dict[str, str] | None = None,
) -> subprocess.CompletedProcess[str]:
route_inputs = dict.fromkeys(GUARD_ROUTE_JOBS, "true") if inputs is None else dict(inputs)
job_results = dict.fromkeys(GUARD_ROUTE_JOBS.values(), "success")
if results:
job_results.update(results)
script = workflow_job_step_script(
"guard-status", "Check routed guard jobs", GUARD_WORKFLOW
)
env = {
**os.environ,
"GUARD_INPUTS": json.dumps(route_inputs),
"GUARD_NEEDS": json.dumps(
{name: {"result": result} for name, result in job_results.items()}
),
}
return subprocess.run(
["bash", "-c", script],
cwd=ROOT,
env=env,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)


def run_detect_step_for_paths(
paths: list[str],
workflow_path: Path = CI_WORKFLOW,
Expand Down Expand Up @@ -971,7 +1004,8 @@ def test_ci_status_job_accepts_skipped_routed_jobs() -> None:

for job_name in [
"changes",
*GUARD_JOBS,
"static-preflight",
"guards",
"web-typecheck",
"react-apps-check",
"diff-sidecar-check",
Expand Down Expand Up @@ -1568,39 +1602,64 @@ def test_macos_compile_admission_precedes_expensive_shards() -> None:
)


def test_guard_workflow_call_preserves_routes_and_static_gate() -> None:
block = workflow_job_block("guards")

assert " needs: [changes, static-preflight]" in block
assert " uses: ./.github/workflows/ci-guards.yml" in block
for route in GUARD_ROUTE_JOBS:
assert f" {route}: ${{{{ needs.changes.outputs.{route} }}}}" in block
assert f"needs.changes.outputs.{route} != 'false'" in block


def test_linux_preflight_blocks_macos_on_cheap_layer_failure() -> None:
block = workflow_job_block("linux-preflight")

assert "name: linux-preflight" in block
assert " - changes" in block
assert " - static-preflight" in block
assert " - guards" in block
for guard_job in GUARD_JOBS:
assert f" - {guard_job}" in block
assert f" - {guard_job}" not in block
assert " - ghosttykit-release-check" in block
assert " - web-typecheck" in block
assert " - react-apps-check" in block
assert " - diff-sidecar-check" in block
assert " - web-db-migrations" in block
assert " - agent-session-web-resources" in block
assert "if: ${{ always() }}" in block
assert 'guard_routes = (' in block
assert 'bad[f"guards.{route}"]' in block
assert 'bad["guards"] = f"{guard_result} (one or more guard routes=true)"' in block
assert 'allowed_routed = {' in block
assert 'routed_outputs = {' in block
assert 'bad[name] = f"{result} (route {route}=true)"' in block


def test_linux_preflight_requires_every_guard_job() -> None:
def test_linux_preflight_requires_guard_aggregate_when_any_guard_is_routed() -> None:
assert run_linux_preflight(linux_preflight_needs()).returncode == 0

for guard_job in GUARD_JOBS:
for outcome in ("failure", "cancelled", "skipped"):
result = run_linux_preflight(linux_preflight_needs(results={guard_job: outcome}))
for outcome in ("failure", "cancelled", "skipped"):
result = run_linux_preflight(linux_preflight_needs(results={"guards": outcome}))

assert result.returncode != 0, (guard_job, outcome)
assert f"{guard_job}: {outcome}" in result.stderr
assert result.returncode != 0, outcome
assert f"guards: {outcome} (one or more guard routes=true)" in result.stderr


def test_linux_preflight_allows_skipped_guard_call_when_all_guard_routes_are_false() -> None:
result = run_linux_preflight(
linux_preflight_needs(
outputs=dict.fromkeys(GUARD_ROUTE_JOBS, "false"),
results={"guards": "skipped"},
)
)

assert result.returncode == 0, result.stderr


def test_only_the_history_guard_job_fetches_full_history() -> None:
for guard_job in GUARD_JOBS:
fetches_history = "fetch-depth: 0" in workflow_job_block(guard_job)
fetches_history = "fetch-depth: 0" in workflow_job_block(guard_job, GUARD_WORKFLOW)
assert fetches_history == (guard_job == "workflow-guard-history"), guard_job


Expand Down Expand Up @@ -1695,7 +1754,9 @@ def test_settings_store_noop_persistence_uses_a_nontolerant_focused_gate() -> No


def test_determinism_workflow_runs_self_test_before_strict_scan() -> None:
script = workflow_job_step_script("workflow-guard-tests", "Validate test determinism gate")
script = workflow_job_step_script(
"workflow-guard-tests", "Validate test determinism gate", GUARD_WORKFLOW
)

assert "scripts/check-test-determinism.py --self-test" in script
assert "scripts/check-test-determinism.py --strict" in script
Expand Down
62 changes: 46 additions & 16 deletions tests/test_ci_linux_guard_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pathlib import Path

from test_ci_change_areas import (
linux_preflight_needs, run_linux_preflight, workflow_job_step_script,
linux_preflight_needs, run_guard_status, run_linux_preflight, workflow_job_step_script,
)


Expand All @@ -22,6 +22,9 @@
"linux_guard_source": "workflow-guard-source-lints",
"ghosttykit_release": "ghosttykit-release-check",
}
REUSABLE_GUARDS = {
route: job for route, job in JOBS.items() if route != "ghosttykit_release"
}


def route(paths, event="pull_request", macos="false"):
Expand Down Expand Up @@ -65,9 +68,14 @@ def test_docs_skip_all_five_guards_and_gate_succeeds(self):
with self.subTest(path=path):
outputs = route([path])
self.assertEqual(outputs, dict.fromkeys(JOBS, "false"))
guard_result = run_guard_status(
inputs={route_name: outputs[route_name] for route_name in REUSABLE_GUARDS},
results=dict.fromkeys(REUSABLE_GUARDS.values(), "skipped"),
)
self.assertEqual(guard_result.returncode, 0, guard_result.stderr)
result = run_linux_preflight(linux_preflight_needs(
outputs=outputs,
results=dict.fromkeys(JOBS.values(), "skipped"),
results={"guards": "skipped", "ghosttykit-release-check": "skipped"},
))
self.assertEqual(result.returncode, 0, result.stderr)

Expand All @@ -93,10 +101,18 @@ def test_cloud_skill_and_its_known_test_keep_only_the_owning_guard(self):
with self.subTest(changed=changed):
outputs = route(changed)
self.assertEqual(outputs, expected)
results = {job: "success" if outputs[name] == "true" else "skipped"
for name, job in JOBS.items()}
guard_results = {
job: "success" if outputs[route_name] == "true" else "skipped"
for route_name, job in REUSABLE_GUARDS.items()
}
guard_result = run_guard_status(
inputs={route_name: outputs[route_name] for route_name in REUSABLE_GUARDS},
results=guard_results,
)
self.assertEqual(guard_result.returncode, 0, guard_result.stderr)
result = run_linux_preflight(linux_preflight_needs(
outputs=outputs, results=results,
outputs=outputs,
results={"guards": "success", "ghosttykit-release-check": "skipped"},
))
self.assertEqual(result.returncode, 0, result.stderr)
for unknown in ("tests/test_new_cloud_contract.py",
Expand Down Expand Up @@ -155,23 +171,37 @@ def test_missing_empty_or_uncertain_diff_and_non_pr_events_run_all(self):
self.assertEqual(route(["README.md"], macos=""), dict.fromkeys(JOBS, "true"))

def test_gate_rejects_selected_guard_skip_failure_or_cancellation(self):
for route_name, job in JOBS.items():
for route_name, job in REUSABLE_GUARDS.items():
for outcome in ("skipped", "failure", "cancelled"):
with self.subTest(job=job, outcome=outcome):
result = run_linux_preflight(linux_preflight_needs(
outputs={route_name: "true"}, results={job: outcome},
))
result = run_guard_status(results={job: outcome})
self.assertNotEqual(result.returncode, 0)
self.assertIn(f"{job}: {outcome} (route {route_name}=true)", result.stderr)

for outcome in ("skipped", "failure", "cancelled"):
with self.subTest(job="ghosttykit-release-check", outcome=outcome):
result = run_linux_preflight(linux_preflight_needs(
results={"ghosttykit-release-check": outcome},
))
self.assertNotEqual(result.returncode, 0)

def test_gate_rejects_bad_or_missing_route_even_if_job_succeeded(self):
for route_name in JOBS:
needs = linux_preflight_needs()
del needs["changes"]["outputs"][route_name]
self.assertNotEqual(run_linux_preflight(needs).returncode, 0)
valid_guard_inputs = dict.fromkeys(REUSABLE_GUARDS, "true")
for route_name in REUSABLE_GUARDS:
missing = dict(valid_guard_inputs)
del missing[route_name]
self.assertNotEqual(run_guard_status(inputs=missing).returncode, 0)
for value in ("", "False", "invalid"):
needs["changes"]["outputs"][route_name] = value
self.assertNotEqual(run_linux_preflight(needs).returncode, 0)

invalid = dict(valid_guard_inputs)
invalid[route_name] = value
self.assertNotEqual(run_guard_status(inputs=invalid).returncode, 0)

needs = linux_preflight_needs()
del needs["changes"]["outputs"]["ghosttykit_release"]
self.assertNotEqual(run_linux_preflight(needs).returncode, 0)
for value in ("", "False", "invalid"):
needs["changes"]["outputs"]["ghosttykit_release"] = value
self.assertNotEqual(run_linux_preflight(needs).returncode, 0)

if __name__ == "__main__":
unittest.main()
Loading
Loading