ci(tests): skip backend and frontend jobs that a pull request cannot affect (#8435)

* ci(tests): skip backend and frontend jobs that a pull request cannot affect

Every pull request ran all six backend shards and the frontend unit and lint
jobs, whatever it changed. A shared `changes` job now runs
scripts/ci_changed_scopes.py on the PR merge commit and gates the backend
(unit shards, default-install collection, lint-backend) and frontend
(unit tests, lint-frontend) jobs on the scope the diff touches.

The rules follow what each suite actually reads, measured by running the six
CI shards under strace: backend tests read files across the whole repository,
so the backend scope only skips frontend sources, tests and assets that no
backend test opens; frontend tests read contracts/, examples/, skills/public/
and the backend capability catalog, so those join the frontend scope. Pushes,
changes under .github/ and an unknown diff run every scope.
test_ci_changed_scopes.py runs the real script on a PR-shaped merge commit,
pins the workflow wiring, and fails when either side starts reading a path
outside its scope. lint-check also cancels superseded runs.

* ci(tests): check frontend reads on every PR and fail open on undecodable diffs

The guard that frontend code reads no path outside the frontend scope ran in
the backend shards, so a frontend-only PR that adds such a read skipped it.
The scanner now lives in scripts/ci_changed_scopes.py behind
--check-frontend-reads, and an ungated frontend-scope-reads job in
lint-check.yml runs it on every pull request.

The scanner also reads frontend/Makefile and package.json, which found that
`make test` runs pnpm through scripts/pnpm.py; that launcher joins the
frontend scope.

`git diff -z` prints raw path bytes, so a non-UTF-8 path raised
UnicodeDecodeError and the changes job wrote no outputs, skipping every gated
job. An undecodable diff is now an unknown diff and runs every scope.

* ci(tests): follow URL and pathlib references in scope guards

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
Xinmin Zeng 2026-11-20 19:40:08 -08:00 committed by GitHub
parent 62b7009f5e
commit 9337f5b032
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 645 additions and 6 deletions

View File

@ -14,11 +14,32 @@ permissions:
contents: read
jobs:
changes:
# Path scoping lives in scripts/ci_changed_scopes.py so every workflow reads
# one rule set. It fails open: pushes and CI config changes run every scope.
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
backend: ${{ steps.scopes.outputs.backend }}
frontend: ${{ steps.scopes.outputs.frontend }}
steps:
- name: Checkout
uses: actions/checkout@v6
with:
# The PR merge commit and its first parent, the base branch tip.
fetch-depth: 2
- name: Detect changed scopes
id: scopes
run: python3 scripts/ci_changed_scopes.py --event "${{ github.event_name }}" >> "$GITHUB_OUTPUT"
default-install-collection:
# The main job installs --extra postgres; this job proves the documented
# contributor path (uv sync --group dev, no extras) still collects the
# whole suite, so optional-dependency imports must stay lazy.
if: github.event.pull_request.draft == false
needs: changes
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
timeout-minutes: 10
@ -46,7 +67,8 @@ jobs:
run: uv run pytest --collect-only -q
backend-unit-tests:
if: github.event.pull_request.draft == false
needs: changes
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:

View File

@ -14,9 +14,30 @@ permissions:
contents: read
jobs:
frontend-unit-tests:
changes:
# Path scoping lives in scripts/ci_changed_scopes.py so every workflow reads
# one rule set. It fails open: pushes and CI config changes run every scope.
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
backend: ${{ steps.scopes.outputs.backend }}
frontend: ${{ steps.scopes.outputs.frontend }}
steps:
- name: Checkout
uses: actions/checkout@v6
with:
# The PR merge commit and its first parent, the base branch tip.
fetch-depth: 2
- name: Detect changed scopes
id: scopes
run: python3 scripts/ci_changed_scopes.py --event "${{ github.event_name }}" >> "$GITHUB_OUTPUT"
frontend-unit-tests:
needs: changes
if: needs.changes.outputs.frontend == 'true'
runs-on: ubuntu-latest
timeout-minutes: 15
steps:

View File

@ -6,10 +6,33 @@ on:
pull_request:
branches: [ '*' ]
concurrency:
group: lint-check-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
permissions:
contents: read
jobs:
changes:
# Path scoping lives in scripts/ci_changed_scopes.py so every workflow reads
# one rule set. It fails open: pushes and CI config changes run every scope.
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
backend: ${{ steps.scopes.outputs.backend }}
frontend: ${{ steps.scopes.outputs.frontend }}
steps:
- name: Checkout
uses: actions/checkout@v6
with:
# The PR merge commit and its first parent, the base branch tip.
fetch-depth: 2
- name: Detect changed scopes
id: scopes
run: python3 scripts/ci_changed_scopes.py --event "${{ github.event_name }}" >> "$GITHUB_OUTPUT"
agent-guidance:
runs-on: ubuntu-latest
steps:
@ -38,6 +61,17 @@ jobs:
--after "${{ github.sha }}"
--github-annotations
frontend-scope-reads:
# Not gated on a scope: the PR that makes frontend code read a new path
# outside the frontend scope may only run frontend jobs.
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v6
- name: Check frontend reads stay inside the frontend scope
run: python3 scripts/ci_changed_scopes.py --check-frontend-reads
label-declarations:
runs-on: ubuntu-latest
steps:
@ -52,6 +86,8 @@ jobs:
run: python scripts/check_label_declarations.py
lint-backend:
needs: changes
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
@ -81,6 +117,8 @@ jobs:
run: make lint
lint-frontend:
needs: changes
if: needs.changes.outputs.frontend == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6

View File

@ -388,13 +388,22 @@ must also set `DEER_FLOW_RUN_LIVE_TESTS=1`.
### PR Regression Checks
Every pull request triggers the following CI workflows:
Pull requests trigger the following CI workflows:
- **Backend unit tests** — [.github/workflows/backend-unit-tests.yml](.github/workflows/backend-unit-tests.yml)
- **Backend unit tests** — [.github/workflows/backend-unit-tests.yml](.github/workflows/backend-unit-tests.yml) (skipped only when every changed file is one the backend suite never reads)
- **Bundled extension tests** — [.github/workflows/bundled-extension-tests.yml](.github/workflows/bundled-extension-tests.yml) (triggered by example packages, `backend/packages/**`, `backend/app/gateway/**` and the marked test modules)
- **Frontend unit tests** — [.github/workflows/frontend-unit-tests.yml](.github/workflows/frontend-unit-tests.yml)
- **Frontend unit tests** — [.github/workflows/frontend-unit-tests.yml](.github/workflows/frontend-unit-tests.yml) (run when `frontend/` or a file it reads changes)
- **Frontend E2E tests** — [.github/workflows/e2e-tests.yml](.github/workflows/e2e-tests.yml) (triggered only when `frontend/` files change)
[scripts/ci_changed_scopes.py](scripts/ci_changed_scopes.py) holds the path rules
for the unit test and lint workflows. Pushes to `main` and changes under
`.github/` run every scope. When a test starts reading a file outside its own
tree, add that path to the script. On every pull request, the
`frontend-scope-reads` lint job flags literal references outside the frontend
scope, including paths based on `import.meta.url`. The backend guard in
`backend/tests/test_ci_changed_scopes.py` flags frontend paths that would skip
the backend suite, including paths built from module-level Python constants.
The browser E2E and full-stack replay jobs use the official Playwright image,
which includes Chromium and its system libraries. When upgrading the locked
Playwright version, update the image tags in both browser workflows to match.

View File

@ -0,0 +1,349 @@
"""Path-scoped CI: the scope rules, the real script on a PR merge commit, and the workflow wiring."""
from __future__ import annotations
import ast
import importlib.util
import os
import shutil
import subprocess
import sys
from pathlib import Path
import pytest
import yaml
REPO_ROOT = Path(__file__).resolve().parents[2]
SCRIPT = REPO_ROOT / "scripts" / "ci_changed_scopes.py"
WORKFLOWS = REPO_ROOT / ".github" / "workflows"
FRONTEND_ROOT = REPO_ROOT / "frontend"
_spec = importlib.util.spec_from_file_location("ci_changed_scopes", SCRIPT)
scopes_module = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(scopes_module)
# workflow file -> {job: scope it is gated on}
GATED_JOBS = {
"backend-unit-tests.yml": {"default-install-collection": "backend", "backend-unit-tests": "backend"},
"frontend-unit-tests.yml": {"frontend-unit-tests": "frontend"},
"lint-check.yml": {"lint-backend": "backend", "lint-frontend": "frontend"},
}
ALWAYS_RUN_JOBS = {"lint-check.yml": {"agent-guidance", "frontend-scope-reads", "label-declarations"}}
def _workflow(name: str) -> dict:
# BaseLoader keeps GitHub Actions' `on` key instead of YAML 1.1 bool True.
return yaml.load((WORKFLOWS / name).read_text(encoding="utf-8"), Loader=yaml.BaseLoader)
# --- scope rules -----------------------------------------------------------
@pytest.mark.parametrize(
("changed", "expected"),
[
(["backend/app/gateway/app.py"], {"backend": True, "frontend": False}),
(["backend/uv.lock", "docker/nginx/nginx.conf", "Makefile"], {"backend": True, "frontend": False}),
(["frontend/src/app/page.tsx", "frontend/tests/unit/app/page.test.ts"], {"backend": False, "frontend": True}),
# Frontend files that backend tests read keep the backend scope.
(["frontend/package.json"], {"backend": True, "frontend": True}),
(["frontend/src/content/en/harness/memory.mdx"], {"backend": True, "frontend": True}),
(["frontend/src/core/memory/AGENTS.md"], {"backend": True, "frontend": True}),
(["frontend/src/app/page.tsx", "backend/app/gateway/app.py"], {"backend": True, "frontend": True}),
(["contracts/thread_goal_contract.json"], {"backend": True, "frontend": True}),
(["examples/deerflow-extension-bookmarks/manifest.json"], {"backend": True, "frontend": True}),
(["skills/public/data-analysis/SKILL.md"], {"backend": True, "frontend": True}),
# frontend/Makefile runs `make test` through this launcher; backend tests also read it.
(["scripts/pnpm.py"], {"backend": True, "frontend": True}),
# A frontend test reads the backend's built-in capability catalog.
(["backend/packages/harness/deerflow/capabilities/builtin.json"], {"backend": True, "frontend": True}),
# A new top-level directory is unknown to both lists: backend runs (skip-list), frontend does not.
(["brand-new-dir/tool.py"], {"backend": True, "frontend": False}),
(["scripts/ci_changed_scopes.py"], {"backend": True, "frontend": True}),
([".github/workflows/e2e-tests.yml"], {"backend": True, "frontend": True}),
([], {"backend": True, "frontend": True}),
],
)
def test_scopes_follow_what_each_suite_reads(changed: list[str], expected: dict[str, bool]) -> None:
assert scopes_module.scopes_for(changed) == expected
# Frontend files the backend suite opened when all six CI shards ran under
# `strace -f -e trace=%file` (Linux, Python 3.12, PostgreSQL and Redis up).
TRACED_BACKEND_READS_UNDER_FRONTEND = (
"frontend/AGENTS.md",
"frontend/Makefile",
"frontend/next.config.js",
"frontend/package.json",
"frontend/pnpm-lock.yaml",
"frontend/src/AGENTS.md",
"frontend/src/components/workspace/AGENTS.md",
"frontend/src/components/workspace/browser-view/AGENTS.md",
"frontend/src/content/en/application/configuration.mdx",
"frontend/src/content/en/harness/configuration.mdx",
"frontend/src/content/en/harness/customization.mdx",
"frontend/src/content/en/harness/memory.mdx",
"frontend/src/content/en/harness/middlewares.mdx",
"frontend/src/content/en/harness/tools.mdx",
"frontend/src/content/en/tutorials/create-your-first-harness.mdx",
"frontend/src/content/en/tutorials/use-tools-and-skills.mdx",
"frontend/src/content/zh/application/configuration.mdx",
"frontend/src/content/zh/harness/configuration.mdx",
"frontend/src/content/zh/harness/customization.mdx",
"frontend/src/content/zh/harness/memory.mdx",
"frontend/src/content/zh/harness/middlewares.mdx",
"frontend/src/content/zh/harness/tools.mdx",
"frontend/src/content/zh/tutorials/create-your-first-harness.mdx",
"frontend/src/content/zh/tutorials/use-tools-and-skills.mdx",
"frontend/src/core/api/api-client.ts",
"frontend/src/core/auth/AGENTS.md",
"frontend/src/core/background-tasks/AGENTS.md",
"frontend/src/core/extensions/bundled-documentation.json",
"frontend/src/core/memory/AGENTS.md",
"frontend/src/core/models/AGENTS.md",
"frontend/tests/e2e/AGENTS.md",
)
@pytest.mark.parametrize("path", TRACED_BACKEND_READS_UNDER_FRONTEND)
def test_frontend_files_the_backend_suite_reads_keep_the_backend_scope(path: str) -> None:
assert scopes_module.scopes_for([path])["backend"] is True
def _frontend_path_chain(value: ast.expr, constants: dict[str, tuple[str, ...]]) -> tuple[str, ...]:
suffix: list[ast.expr] = []
while isinstance(value, ast.BinOp) and isinstance(value.op, ast.Div):
suffix.append(value.right)
value = value.left
segments = list(constants.get(value.id, ())) if isinstance(value, ast.Name) else []
if not segments:
suffix.append(value)
for part in reversed(suffix):
if isinstance(part, ast.Constant) and isinstance(part.value, str):
segments.append(part.value)
elif segments:
segments.append("<any>")
break
return tuple(segments) if segments and segments[0] == "frontend" else ()
def _frontend_paths_named_in_python(source: Path) -> set[str]:
"""Find frontend path literals and pathlib chains, including module-level aliases."""
named: set[str] = set()
tree = ast.parse(source.read_text(encoding="utf-8"))
constants: dict[str, tuple[str, ...]] = {}
for statement in tree.body:
if isinstance(statement, ast.Assign):
targets, value = statement.targets, statement.value
elif isinstance(statement, ast.AnnAssign) and statement.value is not None:
targets, value = [statement.target], statement.value
else:
continue
path = _frontend_path_chain(value, constants)
if path and path[-1] != "<any>":
constants.update((target.id, path) for target in targets if isinstance(target, ast.Name))
# Only the outermost `/` chain names the full path; its left operands are partial chains.
inner = {id(node.left) for node in ast.walk(tree) if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Div)}
for node in ast.walk(tree):
if isinstance(node, ast.Constant) and isinstance(node.value, str) and node.value.startswith("frontend/"):
named.add(node.value)
elif isinstance(node, ast.BinOp) and isinstance(node.op, ast.Div) and id(node) not in inner:
path = _frontend_path_chain(node, constants)
if path:
named.add("/".join(path))
return named
def test_backend_scope_scan_follows_frontend_path_constants(tmp_path: Path) -> None:
source = tmp_path / "test_frontend_reads.py"
source.write_text(
"""\
FRONTEND = ROOT / "frontend"
SOURCE_DIR: Path = FRONTEND / "src"
SOURCE_ALIAS = SOURCE_DIR
(FRONTEND / "src" / "env.js").read_text()
(SOURCE_ALIAS / filename).read_text()
""",
encoding="utf-8",
)
assert {"frontend/src/env.js", "frontend/src/<any>"} <= _frontend_paths_named_in_python(source)
# Existing frontend paths backend code names as data, without opening them.
NAMED_BUT_NOT_READ = {
# Matched against .dockerignore patterns; only .dockerignore is read.
("backend/tests/test_dockerignore_excludes_runtime_data.py", "frontend/src/env.js"),
("backend/tests/test_dockerignore_excludes_runtime_data.py", "frontend/tests/unit/core/extensions/bookmarks.test.ts"),
}
def test_backend_code_names_no_frontend_path_outside_the_backend_scope() -> None:
# A static scan cannot tell a read from a mention, so it flags every existing
# frontend path that backend code names and would not run the backend scope.
own_files = {SCRIPT, Path(__file__).resolve()}
sources = [*(REPO_ROOT / "backend").glob("tests/**/*.py"), *(REPO_ROOT / "scripts").glob("*.py"), *(REPO_ROOT / "backend" / "scripts").rglob("*.py")]
named = {(source.relative_to(REPO_ROOT).as_posix(), path) for source in sources if source.resolve() not in own_files for path in _frontend_paths_named_in_python(source)}
assert any(path.startswith("frontend/src/content/") for _, path in named), "the scanner no longer sees the documentation tests' path chains"
skipped = set()
for source, path in named:
target = REPO_ROOT / path.split("/<any>", 1)[0]
if not target.exists():
continue
probe = f"{path.split('/<any>', 1)[0].rstrip('/')}/any" if target.is_dir() else path
if not scopes_module.scopes_for([probe])["backend"]:
skipped.add((source, path))
assert skipped - NAMED_BUT_NOT_READ == set(), "add the read paths to BACKEND_READS_UNDER_SKIPPED in scripts/ci_changed_scopes.py"
assert NAMED_BUT_NOT_READ <= named, "drop entries from NAMED_BUT_NOT_READ that backend code no longer names"
# --- the real script on a PR-shaped checkout ---------------------------------
def _git(cwd: Path, *args: str) -> str:
return subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True, text=True).stdout
def _run_script(checkout: Path, event: str) -> dict[str, str]:
result = subprocess.run(
[sys.executable, "scripts/ci_changed_scopes.py", "--event", event],
cwd=checkout,
check=True,
capture_output=True,
text=True,
)
return dict(line.split("=", 1) for line in result.stdout.splitlines())
@pytest.fixture
def checkout(tmp_path: Path) -> Path:
if shutil.which("git") is None:
pytest.skip("git is not installed")
repo = tmp_path / "repo"
(repo / "scripts").mkdir(parents=True)
shutil.copy2(SCRIPT, repo / "scripts" / SCRIPT.name)
for relative in ("backend/app.py", "frontend/src/page.tsx"):
(repo / relative).parent.mkdir(parents=True, exist_ok=True)
(repo / relative).write_text("v1\n", encoding="utf-8", newline="\n")
_git(repo, "init", "-q", "-b", "main")
_git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "add", ".")
_git(repo, "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", "base")
return repo
def _open_pr_merge_commit(repo: Path, changes: dict[str, str], base_moves: dict[str, str]) -> None:
"""Shape HEAD like GitHub's refs/pull/N/merge: parent 1 is the base tip, parent 2 the PR head."""
ident = ("-c", "user.name=t", "-c", "user.email=t@t")
_git(repo, "checkout", "-q", "-b", "pr")
for relative, text in changes.items():
(repo / relative).parent.mkdir(parents=True, exist_ok=True)
(repo / relative).write_text(text, encoding="utf-8", newline="\n")
_git(repo, *ident, "add", ".")
_git(repo, *ident, "commit", "-q", "-m", "pr")
_git(repo, "checkout", "-q", "main")
for relative, text in base_moves.items():
(repo / relative).write_text(text, encoding="utf-8", newline="\n")
_git(repo, *ident, "commit", "-q", "-am", "main moved on")
_git(repo, *ident, "merge", "-q", "--no-ff", "-m", "merge pr", "pr")
def test_script_scopes_only_the_pr_changes_not_base_branch_progress(checkout: Path) -> None:
# The base branch changed backend after the PR branched; only the PR's frontend change counts.
_open_pr_merge_commit(checkout, {"frontend/src/page.tsx": "v2\n"}, base_moves={"backend/app.py": "v2\n"})
assert _run_script(checkout, "pull_request") == {"backend": "false", "frontend": "true"}
def test_script_runs_every_scope_on_push(checkout: Path) -> None:
_open_pr_merge_commit(checkout, {"frontend/src/page.tsx": "v2\n"}, base_moves={"backend/app.py": "v2\n"})
assert _run_script(checkout, "push") == {"backend": "true", "frontend": "true"}
def test_script_fails_open_when_the_diff_is_unknown(checkout: Path) -> None:
# A single root commit has no HEAD^1, as with a too-shallow checkout.
assert _run_script(checkout, "pull_request") == {"backend": "true", "frontend": "true"}
@pytest.mark.skipif(sys.platform == "win32", reason="the stub git is a POSIX shell script")
def test_script_fails_open_when_a_changed_path_is_not_utf8(checkout: Path, tmp_path: Path) -> None:
# `git diff -z` prints raw path bytes; many filesystems refuse to create such a file, so stub git.
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
stub = bin_dir / "git"
stub.write_text("#!/bin/sh\nprintf 'frontend/src/\\377.tsx\\0'\n", encoding="utf-8")
stub.chmod(0o755)
result = subprocess.run(
[sys.executable, "scripts/ci_changed_scopes.py", "--event", "pull_request"],
cwd=checkout,
check=True,
capture_output=True,
text=True,
env={**os.environ, "PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}"},
)
assert dict(line.split("=", 1) for line in result.stdout.splitlines()) == {"backend": "true", "frontend": "true"}
assert "running every scope" in result.stderr
# --- workflow wiring --------------------------------------------------------
@pytest.mark.parametrize("name", sorted(GATED_JOBS))
def test_workflow_gates_heavy_jobs_on_the_shared_change_job(name: str) -> None:
jobs = _workflow(name)["jobs"]
changes = jobs["changes"]
checkout_step = next(step for step in changes["steps"] if step.get("uses", "").startswith("actions/checkout@"))
assert checkout_step["with"]["fetch-depth"] == "2"
assert any("scripts/ci_changed_scopes.py --event" in step.get("run", "") and step.get("id") == "scopes" for step in changes["steps"])
assert set(changes["outputs"]) == set(scopes_module.SCOPES)
for job, scope in GATED_JOBS[name].items():
assert jobs[job]["needs"] == "changes", f"{name}:{job} must wait for the change job"
assert jobs[job]["if"] == f"needs.changes.outputs.{scope} == 'true'"
for job in ALWAYS_RUN_JOBS.get(name, ()):
assert "needs" not in jobs[job] and "if" not in jobs[job], f"{name}:{job} is cheap and must always run"
@pytest.mark.parametrize("name", sorted(GATED_JOBS))
def test_workflow_cancels_superseded_runs(name: str) -> None:
concurrency = _workflow(name)["concurrency"]
assert "github.event.pull_request.number || github.ref" in concurrency["group"]
assert concurrency["cancel-in-progress"] == "true"
# --- frontend reads stay inside the frontend scope ---------------------------
# lint-check.yml runs this check on every PR; these tests pin the scanner.
def test_frontend_only_reads_paths_inside_its_scope() -> None:
reads = scopes_module.frontend_reads_outside_frontend(REPO_ROOT)
assert reads, "the scanner found no reads outside frontend/; it no longer recognizes the read patterns"
assert reads.get("scripts/pnpm.py") == {"Makefile"}, "the scanner no longer reads frontend/Makefile"
outside = {path: sorted(files) for path, files in reads.items() if not scopes_module.scopes_for([path])["frontend"]}
assert outside == {}, "add these paths to FRONTEND_PREFIXES in scripts/ci_changed_scopes.py"
@pytest.mark.parametrize(
"read",
[
pytest.param('readFileSync(path.resolve(__dirname, "../../../backend/pyproject.toml"));\n', id="dirname"),
pytest.param('readFileSync(new URL(\n "../../../backend/pyproject.toml",\n import.meta.url,\n));\n', id="import-meta-url"),
],
)
def test_frontend_read_check_fails_on_a_new_read_outside_the_scope(tmp_path: Path, capsys: pytest.CaptureFixture[str], read: str) -> None:
unit = tmp_path / "frontend" / "tests" / "unit"
unit.mkdir(parents=True)
(tmp_path / "backend").mkdir()
(tmp_path / "backend" / "pyproject.toml").write_text("", encoding="utf-8")
(tmp_path / "contracts").mkdir()
(unit / "contract.test.ts").write_text('readFileSync(path.join(process.cwd(), "../contracts"));\n', encoding="utf-8")
assert scopes_module.check_frontend_reads(tmp_path) == 0
(unit / "version.test.ts").write_text(read, encoding="utf-8")
assert scopes_module.check_frontend_reads(tmp_path) == 1
assert "tests/unit/version.test.ts reads backend/pyproject.toml" in capsys.readouterr().err

View File

@ -0,0 +1,200 @@
#!/usr/bin/env python3
"""Decide which test scopes a pull request needs and print them as GitHub outputs.
Prints one ``<scope>=true|false`` line per scope, for ``>> "$GITHUB_OUTPUT"``.
The rules fail open: every scope runs on pushes, when CI configuration changes,
and when the changed files cannot be determined. Only the PR's own changes are
compared, as the diff between the checked-out merge commit and its first parent
(the base branch tip), so this needs ``actions/checkout`` with ``fetch-depth: 2``.
- ``backend`` runs unless every changed file is one the backend suite never
reads. Backend tests read files across the whole repository (scripts, Compose,
Helm, examples, contracts, root config, frontend docs), so this scope lists
what to skip; ``test_ci_changed_scopes.py`` fails when backend code names a
skipped frontend path.
- ``frontend`` runs when a file that frontend tests or the frontend build read
changes. Those reads are explicit relative paths from ``frontend/``, so this
scope lists what to include. ``--check-frontend-reads`` fails when a frontend
file starts reading a path outside it; ``lint-check.yml`` runs it on every PR,
because the PR that adds such a read may only run the frontend scope.
"""
from __future__ import annotations
import argparse
import re
import subprocess
import sys
from collections.abc import Iterable
from pathlib import Path
SCOPES = ("backend", "frontend")
# Any change here can alter what CI runs, so it runs everything.
RUN_ALL_PREFIXES = (".github/", "scripts/ci_changed_scopes.py")
FRONTEND_PREFIXES = (
"frontend/",
# frontend/Makefile runs pnpm, and so `make test`, through this launcher.
"scripts/pnpm.py",
"contracts/",
"examples/",
"skills/public/",
"backend/packages/harness/deerflow/capabilities/builtin.json",
)
# The backend suite never reads frontend sources, tests or static assets ...
BACKEND_SKIP_PREFIXES = ("frontend/src/", "frontend/tests/", "frontend/public/")
# ... except these, which backend documentation, contract and guidance tests read.
# Frontend root files (package.json, the lockfile, Makefile, next.config.js) stay
# in the backend scope because version, Compose and serve tests read them.
BACKEND_READS_UNDER_SKIPPED = (
"frontend/src/content/",
"frontend/src/core/api/api-client.ts",
"frontend/src/core/extensions/bundled-documentation.json",
)
# The agent guidance tests walk every AGENTS.md and CLAUDE.md in the repository.
BACKEND_READS_BY_NAME = ("AGENTS.md", "CLAUDE.md")
def _matches(path: str, prefixes: Iterable[str]) -> bool:
return any(path == prefix or path.startswith(prefix) for prefix in prefixes)
def _backend_reads(path: str) -> bool:
return (
not _matches(path, BACKEND_SKIP_PREFIXES)
or _matches(path, BACKEND_READS_UNDER_SKIPPED)
or path.rsplit("/", 1)[-1] in BACKEND_READS_BY_NAME
)
def scopes_for(changed: list[str]) -> dict[str, bool]:
"""Map the changed repository paths to the scopes that must run."""
if not changed or any(_matches(path, RUN_ALL_PREFIXES) for path in changed):
return dict.fromkeys(SCOPES, True)
return {
"backend": any(_backend_reads(path) for path in changed),
"frontend": any(_matches(path, FRONTEND_PREFIXES) for path in changed),
}
def changed_files() -> list[str] | None:
"""Return the files the checked-out PR merge commit changes, or None if unknown."""
try:
result = subprocess.run(
["git", "diff", "--name-only", "--no-renames", "-z", "HEAD^1", "HEAD"],
check=True,
capture_output=True,
text=True,
encoding="utf-8",
)
# -z prints raw path bytes, so a non-UTF-8 path is an unknown diff, not a crash.
except (OSError, subprocess.CalledProcessError, UnicodeDecodeError) as exc:
print(
f"ci_changed_scopes: cannot diff HEAD^1..HEAD ({exc}); running every scope",
file=sys.stderr,
)
return None
return [path for path in result.stdout.split("\0") if path]
_RELATIVE_IMPORT = re.compile(
r"""(?:from\s+|import\(\s*|require\(\s*)["']((?:\.\./)+[^"']+)["']"""
)
_DIRNAME_PATH = re.compile(r"""__dirname\s*,\s*["']((?:\.\./)+[^"']*)["']""")
_IMPORT_META_URL_PATH = re.compile(
r"""\bnew\s+URL\s*\(\s*["']((?:\.\.?/)+[^"']*)["']\s*,\s*import\.meta\.url\s*,?\s*\)"""
)
_CWD_PATH = re.compile(r"""process\.cwd\(\)\s*,\s*["']((?:\.\./)+[^"']*)["']""")
_COMMAND_PATH = re.compile(r"""(?<![\w./])((?:\.\./)+[\w./-]+)""")
def frontend_reads_outside_frontend(repo_root: Path) -> dict[str, set[str]]:
"""Map each path outside frontend/ that frontend code reads to the files reading it."""
frontend = repo_root / "frontend"
sources = [
path
for base in ("src", "tests/unit")
for path in (frontend / base).rglob("*")
if path.suffix in {".ts", ".tsx", ".js", ".mjs"}
]
sources += [path for path in frontend.glob("*.config.*") if path.is_file()]
commands = [frontend / "Makefile", frontend / "package.json"]
reads: dict[str, set[str]] = {}
for source in [*sources, *commands]:
if not source.is_file():
continue
text = source.read_text(encoding="utf-8", errors="ignore")
if source in commands:
# Make recipes and package scripts run from frontend/.
targets = [frontend / match for match in _COMMAND_PATH.findall(text)]
else:
targets = [
source.parent / match
for pattern in (_RELATIVE_IMPORT, _DIRNAME_PATH, _IMPORT_META_URL_PATH)
for match in pattern.findall(text)
]
targets += [frontend / match for match in _CWD_PATH.findall(text)]
for target in targets:
resolved = target.resolve()
relative = resolved.relative_to(repo_root.resolve()).as_posix()
relative += "/" if resolved.is_dir() else ""
if not relative.startswith("frontend/"):
reads.setdefault(relative, set()).add(
source.relative_to(frontend).as_posix()
)
return reads
def check_frontend_reads(repo_root: Path) -> int:
"""Fail when frontend code reads a path that does not run the frontend scope."""
reads = frontend_reads_outside_frontend(repo_root)
if not reads:
print(
"ci_changed_scopes: found no frontend reads outside frontend/; "
"the scanner no longer recognizes the read patterns",
file=sys.stderr,
)
return 1
outside = {
path: sorted(files)
for path, files in sorted(reads.items())
if not scopes_for([path])["frontend"]
}
for path, files in outside.items():
print(
f"ci_changed_scopes: {', '.join(files)} reads {path}, which does not run "
"the frontend scope; add it to FRONTEND_PREFIXES",
file=sys.stderr,
)
return 1 if outside else 0
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
mode = parser.add_mutually_exclusive_group(required=True)
mode.add_argument("--event", help="github.event_name of the workflow run")
mode.add_argument(
"--check-frontend-reads",
action="store_true",
help="fail when frontend code reads a path outside the frontend scope",
)
args = parser.parse_args(argv)
if args.check_frontend_reads:
return check_frontend_reads(Path(__file__).resolve().parents[1])
changed = changed_files() if args.event == "pull_request" else None
scopes = scopes_for(changed or [])
for scope in SCOPES:
print(f"{scope}={'true' if scopes[scope] else 'false'}")
reason = "pull request diff" if changed else f"{args.event} event or unknown diff"
print(
f"ci_changed_scopes: {scopes} from {reason} ({len(changed or [])} files)",
file=sys.stderr,
)
return 0
if __name__ == "__main__":
raise SystemExit(main())